Merged trunk revision 223002 into the hsa branch.
[official-gcc.git] / gcc / c-family / c-common.c
blob0f547c683cc7fb6409f9608ccc267203f50142be
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #define GCC_C_COMMON_C
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "input.h"
26 #include "c-common.h"
27 #include "tm.h"
28 #include "intl.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "calls.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "varasm.h"
36 #include "trans-mem.h"
37 #include "flags.h"
38 #include "c-pragma.h"
39 #include "c-objc.h"
40 #include "tm_p.h"
41 #include "obstack.h"
42 #include "cpplib.h"
43 #include "target.h"
44 #include "common/common-target.h"
45 #include "langhooks.h"
46 #include "tree-inline.h"
47 #include "toplev.h"
48 #include "diagnostic.h"
49 #include "tree-iterator.h"
50 #include "hashtab.h"
51 #include "opts.h"
52 #include "hash-map.h"
53 #include "is-a.h"
54 #include "plugin-api.h"
55 #include "vec.h"
56 #include "hash-set.h"
57 #include "machmode.h"
58 #include "hard-reg-set.h"
59 #include "input.h"
60 #include "function.h"
61 #include "ipa-ref.h"
62 #include "cgraph.h"
63 #include "target-def.h"
64 #include "gimplify.h"
65 #include "wide-int-print.h"
66 #include "gimple-expr.h"
68 cpp_reader *parse_in; /* Declared in c-pragma.h. */
70 /* Mode used to build pointers (VOIDmode means ptr_mode). */
72 machine_mode c_default_pointer_mode = VOIDmode;
74 /* The following symbols are subsumed in the c_global_trees array, and
75 listed here individually for documentation purposes.
77 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
79 tree short_integer_type_node;
80 tree long_integer_type_node;
81 tree long_long_integer_type_node;
83 tree short_unsigned_type_node;
84 tree long_unsigned_type_node;
85 tree long_long_unsigned_type_node;
87 tree truthvalue_type_node;
88 tree truthvalue_false_node;
89 tree truthvalue_true_node;
91 tree ptrdiff_type_node;
93 tree unsigned_char_type_node;
94 tree signed_char_type_node;
95 tree wchar_type_node;
97 tree char16_type_node;
98 tree char32_type_node;
100 tree float_type_node;
101 tree double_type_node;
102 tree long_double_type_node;
104 tree complex_integer_type_node;
105 tree complex_float_type_node;
106 tree complex_double_type_node;
107 tree complex_long_double_type_node;
109 tree dfloat32_type_node;
110 tree dfloat64_type_node;
111 tree_dfloat128_type_node;
113 tree intQI_type_node;
114 tree intHI_type_node;
115 tree intSI_type_node;
116 tree intDI_type_node;
117 tree intTI_type_node;
119 tree unsigned_intQI_type_node;
120 tree unsigned_intHI_type_node;
121 tree unsigned_intSI_type_node;
122 tree unsigned_intDI_type_node;
123 tree unsigned_intTI_type_node;
125 tree widest_integer_literal_type_node;
126 tree widest_unsigned_literal_type_node;
128 Nodes for types `void *' and `const void *'.
130 tree ptr_type_node, const_ptr_type_node;
132 Nodes for types `char *' and `const char *'.
134 tree string_type_node, const_string_type_node;
136 Type `char[SOMENUMBER]'.
137 Used when an array of char is needed and the size is irrelevant.
139 tree char_array_type_node;
141 Type `wchar_t[SOMENUMBER]' or something like it.
142 Used when a wide string literal is created.
144 tree wchar_array_type_node;
146 Type `char16_t[SOMENUMBER]' or something like it.
147 Used when a UTF-16 string literal is created.
149 tree char16_array_type_node;
151 Type `char32_t[SOMENUMBER]' or something like it.
152 Used when a UTF-32 string literal is created.
154 tree char32_array_type_node;
156 Type `int ()' -- used for implicit declaration of functions.
158 tree default_function_type;
160 A VOID_TYPE node, packaged in a TREE_LIST.
162 tree void_list_node;
164 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
165 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
166 VAR_DECLS, but C++ does.)
168 tree function_name_decl_node;
169 tree pretty_function_name_decl_node;
170 tree c99_function_name_decl_node;
172 Stack of nested function name VAR_DECLs.
174 tree saved_function_name_decls;
178 tree c_global_trees[CTI_MAX];
180 /* Switches common to the C front ends. */
182 /* Nonzero means don't output line number information. */
184 char flag_no_line_commands;
186 /* Nonzero causes -E output not to be done, but directives such as
187 #define that have side effects are still obeyed. */
189 char flag_no_output;
191 /* Nonzero means dump macros in some fashion. */
193 char flag_dump_macros;
195 /* Nonzero means pass #include lines through to the output. */
197 char flag_dump_includes;
199 /* Nonzero means process PCH files while preprocessing. */
201 bool flag_pch_preprocess;
203 /* The file name to which we should write a precompiled header, or
204 NULL if no header will be written in this compile. */
206 const char *pch_file;
208 /* Nonzero if an ISO standard was selected. It rejects macros in the
209 user's namespace. */
210 int flag_iso;
212 /* C/ObjC language option variables. */
215 /* Nonzero means allow type mismatches in conditional expressions;
216 just make their values `void'. */
218 int flag_cond_mismatch;
220 /* Nonzero means enable C89 Amendment 1 features. */
222 int flag_isoc94;
224 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
226 int flag_isoc99;
228 /* Nonzero means use the ISO C11 dialect of C. */
230 int flag_isoc11;
232 /* Nonzero means that we have builtin functions, and main is an int. */
234 int flag_hosted = 1;
237 /* ObjC language option variables. */
240 /* Tells the compiler that this is a special run. Do not perform any
241 compiling, instead we are to test some platform dependent features
242 and output a C header file with appropriate definitions. */
244 int print_struct_values;
246 /* Tells the compiler what is the constant string class for ObjC. */
248 const char *constant_string_class_name;
251 /* C++ language option variables. */
254 /* Nonzero means generate separate instantiation control files and
255 juggle them at link time. */
257 int flag_use_repository;
259 /* The C++ dialect being used. Default set in c_common_post_options. */
261 enum cxx_dialect cxx_dialect = cxx_unset;
263 /* Maximum template instantiation depth. This limit exists to limit the
264 time it takes to notice excessively recursive template instantiations.
266 The default is lower than the 1024 recommended by the C++0x standard
267 because G++ runs out of stack before 1024 with highly recursive template
268 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
270 int max_tinst_depth = 900;
272 /* The elements of `ridpointers' are identifier nodes for the reserved
273 type names and storage classes. It is indexed by a RID_... value. */
274 tree *ridpointers;
276 tree (*make_fname_decl) (location_t, tree, int);
278 /* Nonzero means don't warn about problems that occur when the code is
279 executed. */
280 int c_inhibit_evaluation_warnings;
282 /* Whether we are building a boolean conversion inside
283 convert_for_assignment, or some other late binary operation. If
284 build_binary_op is called for C (from code shared by C and C++) in
285 this case, then the operands have already been folded and the
286 result will not be folded again, so C_MAYBE_CONST_EXPR should not
287 be generated. */
288 bool in_late_binary_op;
290 /* Whether lexing has been completed, so subsequent preprocessor
291 errors should use the compiler's input_location. */
292 bool done_lexing = false;
294 /* Information about how a function name is generated. */
295 struct fname_var_t
297 tree *const decl; /* pointer to the VAR_DECL. */
298 const unsigned rid; /* RID number for the identifier. */
299 const int pretty; /* How pretty is it? */
302 /* The three ways of getting then name of the current function. */
304 const struct fname_var_t fname_vars[] =
306 /* C99 compliant __func__, must be first. */
307 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
308 /* GCC __FUNCTION__ compliant. */
309 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
310 /* GCC __PRETTY_FUNCTION__ compliant. */
311 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
312 {NULL, 0, 0},
315 /* Global visibility options. */
316 struct visibility_flags visibility_options;
318 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
319 static tree check_case_value (location_t, tree);
320 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
322 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
329 int, bool *);
330 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
331 int, bool *);
332 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
333 bool *);
334 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_hsa_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_always_inline_attribute (tree *, tree, tree, int,
341 bool *);
342 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
349 bool *);
350 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
351 bool *);
352 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_transparent_union_attribute (tree *, tree, tree,
354 int, bool *);
355 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
361 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
362 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
365 static tree handle_visibility_attribute (tree *, tree, tree, int,
366 bool *);
367 static tree handle_tls_model_attribute (tree *, tree, tree, int,
368 bool *);
369 static tree handle_no_instrument_function_attribute (tree *, tree,
370 tree, int, bool *);
371 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
374 bool *);
375 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_deprecated_attribute (tree *, tree, tree, int,
380 bool *);
381 static tree handle_vector_size_attribute (tree *, tree, tree, int,
382 bool *);
383 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
387 bool *);
388 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
392 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
394 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
395 static tree ignore_attribute (tree *, tree, tree, int, bool *);
396 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
397 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
398 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
399 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
400 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
401 bool *);
402 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
403 bool *);
404 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
405 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
406 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
407 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
409 static void check_function_nonnull (tree, int, tree *);
410 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
411 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
412 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
413 static int resort_field_decl_cmp (const void *, const void *);
415 /* Reserved words. The third field is a mask: keywords are disabled
416 if they match the mask.
418 Masks for languages:
419 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
420 C --std=c99: D_CXXONLY | D_OBJC
421 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
422 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
423 C++ --std=c0x: D_CONLY | D_OBJC
424 ObjC++ is like C++ except that D_OBJC is not set
426 If -fno-asm is used, D_ASM is added to the mask. If
427 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
428 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
429 In C with -Wc++-compat, we warn if D_CXXWARN is set.
431 Note the complication of the D_CXX_OBJC keywords. These are
432 reserved words such as 'class'. In C++, 'class' is a reserved
433 word. In Objective-C++ it is too. In Objective-C, it is a
434 reserved word too, but only if it follows an '@' sign.
436 const struct c_common_resword c_common_reswords[] =
438 { "_Alignas", RID_ALIGNAS, D_CONLY },
439 { "_Alignof", RID_ALIGNOF, D_CONLY },
440 { "_Atomic", RID_ATOMIC, D_CONLY },
441 { "_Bool", RID_BOOL, D_CONLY },
442 { "_Complex", RID_COMPLEX, 0 },
443 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
444 { "_Cilk_sync", RID_CILK_SYNC, 0 },
445 { "_Cilk_for", RID_CILK_FOR, 0 },
446 { "_Imaginary", RID_IMAGINARY, D_CONLY },
447 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
448 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
449 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
450 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
451 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
452 { "_Sat", RID_SAT, D_CONLY | D_EXT },
453 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
454 { "_Noreturn", RID_NORETURN, D_CONLY },
455 { "_Generic", RID_GENERIC, D_CONLY },
456 { "_Thread_local", RID_THREAD, D_CONLY },
457 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
458 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
459 { "__alignof", RID_ALIGNOF, 0 },
460 { "__alignof__", RID_ALIGNOF, 0 },
461 { "__asm", RID_ASM, 0 },
462 { "__asm__", RID_ASM, 0 },
463 { "__attribute", RID_ATTRIBUTE, 0 },
464 { "__attribute__", RID_ATTRIBUTE, 0 },
465 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
466 { "__bases", RID_BASES, D_CXXONLY },
467 { "__builtin_call_with_static_chain",
468 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
469 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
470 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
471 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
472 { "__builtin_offsetof", RID_OFFSETOF, 0 },
473 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
474 { "__builtin_va_arg", RID_VA_ARG, 0 },
475 { "__complex", RID_COMPLEX, 0 },
476 { "__complex__", RID_COMPLEX, 0 },
477 { "__const", RID_CONST, 0 },
478 { "__const__", RID_CONST, 0 },
479 { "__decltype", RID_DECLTYPE, D_CXXONLY },
480 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
481 { "__extension__", RID_EXTENSION, 0 },
482 { "__func__", RID_C99_FUNCTION_NAME, 0 },
483 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
484 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
485 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
486 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
487 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
488 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
489 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
490 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
491 { "__imag", RID_IMAGPART, 0 },
492 { "__imag__", RID_IMAGPART, 0 },
493 { "__inline", RID_INLINE, 0 },
494 { "__inline__", RID_INLINE, 0 },
495 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
496 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
497 { "__is_class", RID_IS_CLASS, D_CXXONLY },
498 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
499 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
500 { "__is_final", RID_IS_FINAL, D_CXXONLY },
501 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
502 { "__is_pod", RID_IS_POD, D_CXXONLY },
503 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
504 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
505 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
506 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
507 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
508 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
509 { "__is_union", RID_IS_UNION, D_CXXONLY },
510 { "__label__", RID_LABEL, 0 },
511 { "__null", RID_NULL, 0 },
512 { "__real", RID_REALPART, 0 },
513 { "__real__", RID_REALPART, 0 },
514 { "__restrict", RID_RESTRICT, 0 },
515 { "__restrict__", RID_RESTRICT, 0 },
516 { "__signed", RID_SIGNED, 0 },
517 { "__signed__", RID_SIGNED, 0 },
518 { "__thread", RID_THREAD, 0 },
519 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
520 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
521 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
522 { "__typeof", RID_TYPEOF, 0 },
523 { "__typeof__", RID_TYPEOF, 0 },
524 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
525 { "__volatile", RID_VOLATILE, 0 },
526 { "__volatile__", RID_VOLATILE, 0 },
527 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
528 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
529 { "asm", RID_ASM, D_ASM },
530 { "auto", RID_AUTO, 0 },
531 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
532 { "break", RID_BREAK, 0 },
533 { "case", RID_CASE, 0 },
534 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
535 { "char", RID_CHAR, 0 },
536 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
537 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
538 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
539 { "const", RID_CONST, 0 },
540 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
541 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
542 { "continue", RID_CONTINUE, 0 },
543 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
544 { "default", RID_DEFAULT, 0 },
545 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
546 { "do", RID_DO, 0 },
547 { "double", RID_DOUBLE, 0 },
548 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
549 { "else", RID_ELSE, 0 },
550 { "enum", RID_ENUM, 0 },
551 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
552 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
553 { "extern", RID_EXTERN, 0 },
554 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
555 { "float", RID_FLOAT, 0 },
556 { "for", RID_FOR, 0 },
557 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
558 { "goto", RID_GOTO, 0 },
559 { "if", RID_IF, 0 },
560 { "inline", RID_INLINE, D_EXT89 },
561 { "int", RID_INT, 0 },
562 { "long", RID_LONG, 0 },
563 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
564 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
565 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
566 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
567 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
568 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
569 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
570 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
571 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
572 { "register", RID_REGISTER, 0 },
573 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
574 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
575 { "return", RID_RETURN, 0 },
576 { "short", RID_SHORT, 0 },
577 { "signed", RID_SIGNED, 0 },
578 { "sizeof", RID_SIZEOF, 0 },
579 { "static", RID_STATIC, 0 },
580 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
581 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
582 { "struct", RID_STRUCT, 0 },
583 { "switch", RID_SWITCH, 0 },
584 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
585 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
586 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
587 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
588 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
589 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
590 { "typedef", RID_TYPEDEF, 0 },
591 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
592 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
593 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
594 { "union", RID_UNION, 0 },
595 { "unsigned", RID_UNSIGNED, 0 },
596 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
597 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
598 { "void", RID_VOID, 0 },
599 { "volatile", RID_VOLATILE, 0 },
600 { "wchar_t", RID_WCHAR, D_CXXONLY },
601 { "while", RID_WHILE, 0 },
602 /* These Objective-C keywords are recognized only immediately after
603 an '@'. */
604 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
605 { "defs", RID_AT_DEFS, D_OBJC },
606 { "encode", RID_AT_ENCODE, D_OBJC },
607 { "end", RID_AT_END, D_OBJC },
608 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
609 { "interface", RID_AT_INTERFACE, D_OBJC },
610 { "protocol", RID_AT_PROTOCOL, D_OBJC },
611 { "selector", RID_AT_SELECTOR, D_OBJC },
612 { "finally", RID_AT_FINALLY, D_OBJC },
613 { "synchronized", RID_AT_SYNCHRONIZED, 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 { "hsa", 0, 0, true, false, false,
674 handle_hsa_attribute, false },
675 { "hsakernel", 0, 0, true, false, false,
676 handle_hsa_attribute, false },
677 { "hsafunc", 0, 0, true, false, false,
678 handle_hsa_attribute, false },
679 { "no_icf", 0, 0, true, false, false,
680 handle_noicf_attribute, false },
681 { "leaf", 0, 0, true, false, false,
682 handle_leaf_attribute, false },
683 { "always_inline", 0, 0, true, false, false,
684 handle_always_inline_attribute, false },
685 { "gnu_inline", 0, 0, true, false, false,
686 handle_gnu_inline_attribute, false },
687 { "artificial", 0, 0, true, false, false,
688 handle_artificial_attribute, false },
689 { "flatten", 0, 0, true, false, false,
690 handle_flatten_attribute, false },
691 { "used", 0, 0, true, false, false,
692 handle_used_attribute, false },
693 { "unused", 0, 0, false, false, false,
694 handle_unused_attribute, false },
695 { "externally_visible", 0, 0, true, false, false,
696 handle_externally_visible_attribute, false },
697 { "no_reorder", 0, 0, true, false, false,
698 handle_no_reorder_attribute, false },
699 /* The same comments as for noreturn attributes apply to const ones. */
700 { "const", 0, 0, true, false, false,
701 handle_const_attribute, false },
702 { "transparent_union", 0, 0, false, false, false,
703 handle_transparent_union_attribute, false },
704 { "constructor", 0, 1, true, false, false,
705 handle_constructor_attribute, false },
706 { "destructor", 0, 1, true, false, false,
707 handle_destructor_attribute, false },
708 { "mode", 1, 1, false, true, false,
709 handle_mode_attribute, false },
710 { "section", 1, 1, true, false, false,
711 handle_section_attribute, false },
712 { "aligned", 0, 1, false, false, false,
713 handle_aligned_attribute, false },
714 { "weak", 0, 0, true, false, false,
715 handle_weak_attribute, false },
716 { "ifunc", 1, 1, true, false, false,
717 handle_ifunc_attribute, false },
718 { "alias", 1, 1, true, false, false,
719 handle_alias_attribute, false },
720 { "weakref", 0, 1, true, false, false,
721 handle_weakref_attribute, false },
722 { "no_instrument_function", 0, 0, true, false, false,
723 handle_no_instrument_function_attribute,
724 false },
725 { "malloc", 0, 0, true, false, false,
726 handle_malloc_attribute, false },
727 { "returns_twice", 0, 0, true, false, false,
728 handle_returns_twice_attribute, false },
729 { "no_stack_limit", 0, 0, true, false, false,
730 handle_no_limit_stack_attribute, false },
731 { "pure", 0, 0, true, false, false,
732 handle_pure_attribute, false },
733 { "transaction_callable", 0, 0, false, true, false,
734 handle_tm_attribute, false },
735 { "transaction_unsafe", 0, 0, false, true, false,
736 handle_tm_attribute, false },
737 { "transaction_safe", 0, 0, false, true, false,
738 handle_tm_attribute, false },
739 { "transaction_may_cancel_outer", 0, 0, false, true, false,
740 handle_tm_attribute, false },
741 /* ??? These two attributes didn't make the transition from the
742 Intel language document to the multi-vendor language document. */
743 { "transaction_pure", 0, 0, false, true, false,
744 handle_tm_attribute, false },
745 { "transaction_wrap", 1, 1, true, false, false,
746 handle_tm_wrap_attribute, false },
747 /* For internal use (marking of builtins) only. The name contains space
748 to prevent its usage in source code. */
749 { "no vops", 0, 0, true, false, false,
750 handle_novops_attribute, false },
751 { "deprecated", 0, 1, false, false, false,
752 handle_deprecated_attribute, false },
753 { "vector_size", 1, 1, false, true, false,
754 handle_vector_size_attribute, false },
755 { "visibility", 1, 1, false, false, false,
756 handle_visibility_attribute, false },
757 { "tls_model", 1, 1, true, false, false,
758 handle_tls_model_attribute, false },
759 { "nonnull", 0, -1, false, true, true,
760 handle_nonnull_attribute, false },
761 { "nothrow", 0, 0, true, false, false,
762 handle_nothrow_attribute, false },
763 { "may_alias", 0, 0, false, true, false, NULL, false },
764 { "cleanup", 1, 1, true, false, false,
765 handle_cleanup_attribute, false },
766 { "warn_unused_result", 0, 0, false, true, true,
767 handle_warn_unused_result_attribute, false },
768 { "sentinel", 0, 1, false, true, true,
769 handle_sentinel_attribute, false },
770 /* For internal use (marking of builtins) only. The name contains space
771 to prevent its usage in source code. */
772 { "type generic", 0, 0, false, true, true,
773 handle_type_generic_attribute, false },
774 { "alloc_size", 1, 2, false, true, true,
775 handle_alloc_size_attribute, false },
776 { "cold", 0, 0, true, false, false,
777 handle_cold_attribute, false },
778 { "hot", 0, 0, true, false, false,
779 handle_hot_attribute, false },
780 { "no_address_safety_analysis",
781 0, 0, true, false, false,
782 handle_no_address_safety_analysis_attribute,
783 false },
784 { "no_sanitize_address", 0, 0, true, false, false,
785 handle_no_sanitize_address_attribute,
786 false },
787 { "no_sanitize_thread", 0, 0, true, false, false,
788 handle_no_sanitize_address_attribute,
789 false },
790 { "no_sanitize_undefined", 0, 0, true, false, false,
791 handle_no_sanitize_undefined_attribute,
792 false },
793 { "warning", 1, 1, true, false, false,
794 handle_error_attribute, false },
795 { "error", 1, 1, true, false, false,
796 handle_error_attribute, false },
797 { "target", 1, -1, true, false, false,
798 handle_target_attribute, false },
799 { "optimize", 1, -1, true, false, false,
800 handle_optimize_attribute, false },
801 /* For internal use only. The leading '*' both prevents its usage in
802 source code and signals that it may be overridden by machine tables. */
803 { "*tm regparm", 0, 0, false, true, true,
804 ignore_attribute, false },
805 { "no_split_stack", 0, 0, true, false, false,
806 handle_no_split_stack_attribute, false },
807 /* For internal use (marking of builtins and runtime functions) only.
808 The name contains space to prevent its usage in source code. */
809 { "fn spec", 1, 1, false, true, true,
810 handle_fnspec_attribute, false },
811 { "warn_unused", 0, 0, false, false, false,
812 handle_warn_unused_attribute, false },
813 { "returns_nonnull", 0, 0, false, true, true,
814 handle_returns_nonnull_attribute, false },
815 { "omp declare simd", 0, -1, true, false, false,
816 handle_omp_declare_simd_attribute, false },
817 { "cilk simd function", 0, -1, true, false, false,
818 handle_omp_declare_simd_attribute, false },
819 { "omp declare target", 0, 0, true, false, false,
820 handle_omp_declare_target_attribute, false },
821 { "alloc_align", 1, 1, false, true, true,
822 handle_alloc_align_attribute, false },
823 { "assume_aligned", 1, 2, false, true, true,
824 handle_assume_aligned_attribute, false },
825 { "designated_init", 0, 0, false, true, false,
826 handle_designated_init_attribute, false },
827 { "bnd_variable_size", 0, 0, true, false, false,
828 handle_bnd_variable_size_attribute, false },
829 { "bnd_legacy", 0, 0, true, false, false,
830 handle_bnd_legacy, false },
831 { "bnd_instrument", 0, 0, true, false, false,
832 handle_bnd_instrument, false },
833 { NULL, 0, 0, false, false, false, NULL, false }
836 /* Give the specifications for the format attributes, used by C and all
837 descendants.
839 All attributes referencing arguments should be additionally processed
840 in chkp_copy_function_type_adding_bounds for correct instrumentation
841 by Pointer Bounds Checker.
842 Current list of processed format attributes: format, format_arg. */
843 const struct attribute_spec c_common_format_attribute_table[] =
845 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
846 affects_type_identity } */
847 { "format", 3, 3, false, true, true,
848 handle_format_attribute, false },
849 { "format_arg", 1, 1, false, true, true,
850 handle_format_arg_attribute, false },
851 { NULL, 0, 0, false, false, false, NULL, false }
854 /* Return identifier for address space AS. */
856 const char *
857 c_addr_space_name (addr_space_t as)
859 int rid = RID_FIRST_ADDR_SPACE + as;
860 gcc_assert (ridpointers [rid]);
861 return IDENTIFIER_POINTER (ridpointers [rid]);
864 /* Push current bindings for the function name VAR_DECLS. */
866 void
867 start_fname_decls (void)
869 unsigned ix;
870 tree saved = NULL_TREE;
872 for (ix = 0; fname_vars[ix].decl; ix++)
874 tree decl = *fname_vars[ix].decl;
876 if (decl)
878 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
879 saved);
880 *fname_vars[ix].decl = NULL_TREE;
883 if (saved || saved_function_name_decls)
884 /* Normally they'll have been NULL, so only push if we've got a
885 stack, or they are non-NULL. */
886 saved_function_name_decls = tree_cons (saved, NULL_TREE,
887 saved_function_name_decls);
890 /* Finish up the current bindings, adding them into the current function's
891 statement tree. This must be done _before_ finish_stmt_tree is called.
892 If there is no current function, we must be at file scope and no statements
893 are involved. Pop the previous bindings. */
895 void
896 finish_fname_decls (void)
898 unsigned ix;
899 tree stmts = NULL_TREE;
900 tree stack = saved_function_name_decls;
902 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
903 append_to_statement_list (TREE_VALUE (stack), &stmts);
905 if (stmts)
907 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
909 if (TREE_CODE (*bodyp) == BIND_EXPR)
910 bodyp = &BIND_EXPR_BODY (*bodyp);
912 append_to_statement_list_force (*bodyp, &stmts);
913 *bodyp = stmts;
916 for (ix = 0; fname_vars[ix].decl; ix++)
917 *fname_vars[ix].decl = NULL_TREE;
919 if (stack)
921 /* We had saved values, restore them. */
922 tree saved;
924 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
926 tree decl = TREE_PURPOSE (saved);
927 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
929 *fname_vars[ix].decl = decl;
931 stack = TREE_CHAIN (stack);
933 saved_function_name_decls = stack;
936 /* Return the text name of the current function, suitably prettified
937 by PRETTY_P. Return string must be freed by caller. */
939 const char *
940 fname_as_string (int pretty_p)
942 const char *name = "top level";
943 char *namep;
944 int vrb = 2, len;
945 cpp_string cstr = { 0, 0 }, strname;
947 if (!pretty_p)
949 name = "";
950 vrb = 0;
953 if (current_function_decl)
954 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
956 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
958 namep = XNEWVEC (char, len);
959 snprintf (namep, len, "\"%s\"", name);
960 strname.text = (unsigned char *) namep;
961 strname.len = len - 1;
963 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
965 XDELETEVEC (namep);
966 return (const char *) cstr.text;
969 return namep;
972 /* Return the VAR_DECL for a const char array naming the current
973 function. If the VAR_DECL has not yet been created, create it
974 now. RID indicates how it should be formatted and IDENTIFIER_NODE
975 ID is its name (unfortunately C and C++ hold the RID values of
976 keywords in different places, so we can't derive RID from ID in
977 this language independent code. LOC is the location of the
978 function. */
980 tree
981 fname_decl (location_t loc, unsigned int rid, tree id)
983 unsigned ix;
984 tree decl = NULL_TREE;
986 for (ix = 0; fname_vars[ix].decl; ix++)
987 if (fname_vars[ix].rid == rid)
988 break;
990 decl = *fname_vars[ix].decl;
991 if (!decl)
993 /* If a tree is built here, it would normally have the lineno of
994 the current statement. Later this tree will be moved to the
995 beginning of the function and this line number will be wrong.
996 To avoid this problem set the lineno to 0 here; that prevents
997 it from appearing in the RTL. */
998 tree stmts;
999 location_t saved_location = input_location;
1000 input_location = UNKNOWN_LOCATION;
1002 stmts = push_stmt_list ();
1003 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1004 stmts = pop_stmt_list (stmts);
1005 if (!IS_EMPTY_STMT (stmts))
1006 saved_function_name_decls
1007 = tree_cons (decl, stmts, saved_function_name_decls);
1008 *fname_vars[ix].decl = decl;
1009 input_location = saved_location;
1011 if (!ix && !current_function_decl)
1012 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1014 return decl;
1017 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1019 tree
1020 fix_string_type (tree value)
1022 int length = TREE_STRING_LENGTH (value);
1023 int nchars;
1024 tree e_type, i_type, a_type;
1026 /* Compute the number of elements, for the array type. */
1027 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1029 nchars = length;
1030 e_type = char_type_node;
1032 else if (TREE_TYPE (value) == char16_array_type_node)
1034 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1035 e_type = char16_type_node;
1037 else if (TREE_TYPE (value) == char32_array_type_node)
1039 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1040 e_type = char32_type_node;
1042 else
1044 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1045 e_type = wchar_type_node;
1048 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1049 limit in C++98 Annex B is very large (65536) and is not normative,
1050 so we do not diagnose it (warn_overlength_strings is forced off
1051 in c_common_post_options). */
1052 if (warn_overlength_strings)
1054 const int nchars_max = flag_isoc99 ? 4095 : 509;
1055 const int relevant_std = flag_isoc99 ? 99 : 90;
1056 if (nchars - 1 > nchars_max)
1057 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1058 separate the %d from the 'C'. 'ISO' should not be
1059 translated, but it may be moved after 'C%d' in languages
1060 where modifiers follow nouns. */
1061 pedwarn (input_location, OPT_Woverlength_strings,
1062 "string length %qd is greater than the length %qd "
1063 "ISO C%d compilers are required to support",
1064 nchars - 1, nchars_max, relevant_std);
1067 /* Create the array type for the string constant. The ISO C++
1068 standard says that a string literal has type `const char[N]' or
1069 `const wchar_t[N]'. We use the same logic when invoked as a C
1070 front-end with -Wwrite-strings.
1071 ??? We should change the type of an expression depending on the
1072 state of a warning flag. We should just be warning -- see how
1073 this is handled in the C++ front-end for the deprecated implicit
1074 conversion from string literals to `char*' or `wchar_t*'.
1076 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1077 array type being the unqualified version of that type.
1078 Therefore, if we are constructing an array of const char, we must
1079 construct the matching unqualified array type first. The C front
1080 end does not require this, but it does no harm, so we do it
1081 unconditionally. */
1082 i_type = build_index_type (size_int (nchars - 1));
1083 a_type = build_array_type (e_type, i_type);
1084 if (c_dialect_cxx() || warn_write_strings)
1085 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1087 TREE_TYPE (value) = a_type;
1088 TREE_CONSTANT (value) = 1;
1089 TREE_READONLY (value) = 1;
1090 TREE_STATIC (value) = 1;
1091 return value;
1094 /* If DISABLE is true, stop issuing warnings. This is used when
1095 parsing code that we know will not be executed. This function may
1096 be called multiple times, and works as a stack. */
1098 static void
1099 c_disable_warnings (bool disable)
1101 if (disable)
1103 ++c_inhibit_evaluation_warnings;
1104 fold_defer_overflow_warnings ();
1108 /* If ENABLE is true, reenable issuing warnings. */
1110 static void
1111 c_enable_warnings (bool enable)
1113 if (enable)
1115 --c_inhibit_evaluation_warnings;
1116 fold_undefer_and_ignore_overflow_warnings ();
1120 /* Fully fold EXPR, an expression that was not folded (beyond integer
1121 constant expressions and null pointer constants) when being built
1122 up. If IN_INIT, this is in a static initializer and certain
1123 changes are made to the folding done. Clear *MAYBE_CONST if
1124 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1125 expression because it contains an evaluated operator (in C99) or an
1126 operator outside of sizeof returning an integer constant (in C90)
1127 not permitted in constant expressions, or because it contains an
1128 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1129 set to true by callers before calling this function.) Return the
1130 folded expression. Function arguments have already been folded
1131 before calling this function, as have the contents of SAVE_EXPR,
1132 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1133 C_MAYBE_CONST_EXPR. */
1135 tree
1136 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1138 tree ret;
1139 tree eptype = NULL_TREE;
1140 bool dummy = true;
1141 bool maybe_const_itself = true;
1142 location_t loc = EXPR_LOCATION (expr);
1144 /* This function is not relevant to C++ because C++ folds while
1145 parsing, and may need changes to be correct for C++ when C++
1146 stops folding while parsing. */
1147 if (c_dialect_cxx ())
1148 gcc_unreachable ();
1150 if (!maybe_const)
1151 maybe_const = &dummy;
1152 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1154 eptype = TREE_TYPE (expr);
1155 expr = TREE_OPERAND (expr, 0);
1157 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1158 &maybe_const_itself);
1159 if (eptype)
1160 ret = fold_convert_loc (loc, eptype, ret);
1161 *maybe_const &= maybe_const_itself;
1162 return ret;
1165 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1166 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1167 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1168 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1169 both evaluated and unevaluated subexpressions while
1170 *MAYBE_CONST_ITSELF is carried from only evaluated
1171 subexpressions). */
1173 static tree
1174 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1175 bool *maybe_const_itself)
1177 tree ret = expr;
1178 enum tree_code code = TREE_CODE (expr);
1179 enum tree_code_class kind = TREE_CODE_CLASS (code);
1180 location_t loc = EXPR_LOCATION (expr);
1181 tree op0, op1, op2, op3;
1182 tree orig_op0, orig_op1, orig_op2;
1183 bool op0_const = true, op1_const = true, op2_const = true;
1184 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1185 bool nowarning = TREE_NO_WARNING (expr);
1186 bool unused_p;
1188 /* This function is not relevant to C++ because C++ folds while
1189 parsing, and may need changes to be correct for C++ when C++
1190 stops folding while parsing. */
1191 if (c_dialect_cxx ())
1192 gcc_unreachable ();
1194 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1195 anything else not counted as an expression cannot usefully be
1196 folded further at this point. */
1197 if (!IS_EXPR_CODE_CLASS (kind)
1198 || kind == tcc_statement
1199 || code == SAVE_EXPR)
1200 return expr;
1202 /* Operands of variable-length expressions (function calls) have
1203 already been folded, as have __builtin_* function calls, and such
1204 expressions cannot occur in constant expressions. */
1205 if (kind == tcc_vl_exp)
1207 *maybe_const_operands = false;
1208 ret = fold (expr);
1209 goto out;
1212 if (code == C_MAYBE_CONST_EXPR)
1214 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1215 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1216 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1217 *maybe_const_operands = false;
1218 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1219 *maybe_const_itself = false;
1220 if (pre && !in_init)
1221 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1222 else
1223 ret = inner;
1224 goto out;
1227 /* Assignment, increment, decrement, function call and comma
1228 operators, and statement expressions, cannot occur in constant
1229 expressions if evaluated / outside of sizeof. (Function calls
1230 were handled above, though VA_ARG_EXPR is treated like a function
1231 call here, and statement expressions are handled through
1232 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1233 switch (code)
1235 case MODIFY_EXPR:
1236 case PREDECREMENT_EXPR:
1237 case PREINCREMENT_EXPR:
1238 case POSTDECREMENT_EXPR:
1239 case POSTINCREMENT_EXPR:
1240 case COMPOUND_EXPR:
1241 *maybe_const_operands = false;
1242 break;
1244 case VA_ARG_EXPR:
1245 case TARGET_EXPR:
1246 case BIND_EXPR:
1247 case OBJ_TYPE_REF:
1248 *maybe_const_operands = false;
1249 ret = fold (expr);
1250 goto out;
1252 default:
1253 break;
1256 /* Fold individual tree codes as appropriate. */
1257 switch (code)
1259 case COMPOUND_LITERAL_EXPR:
1260 /* Any non-constancy will have been marked in a containing
1261 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1262 goto out;
1264 case COMPONENT_REF:
1265 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1266 op1 = TREE_OPERAND (expr, 1);
1267 op2 = TREE_OPERAND (expr, 2);
1268 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1269 maybe_const_itself);
1270 STRIP_TYPE_NOPS (op0);
1271 if (op0 != orig_op0)
1272 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1273 if (ret != expr)
1275 TREE_READONLY (ret) = TREE_READONLY (expr);
1276 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1278 goto out;
1280 case ARRAY_REF:
1281 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1282 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1283 op2 = TREE_OPERAND (expr, 2);
1284 op3 = TREE_OPERAND (expr, 3);
1285 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1286 maybe_const_itself);
1287 STRIP_TYPE_NOPS (op0);
1288 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1289 maybe_const_itself);
1290 STRIP_TYPE_NOPS (op1);
1291 op1 = decl_constant_value_for_optimization (op1);
1292 if (op0 != orig_op0 || op1 != orig_op1)
1293 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1294 if (ret != expr)
1296 TREE_READONLY (ret) = TREE_READONLY (expr);
1297 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1298 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1300 ret = fold (ret);
1301 goto out;
1303 case COMPOUND_EXPR:
1304 case MODIFY_EXPR:
1305 case PREDECREMENT_EXPR:
1306 case PREINCREMENT_EXPR:
1307 case POSTDECREMENT_EXPR:
1308 case POSTINCREMENT_EXPR:
1309 case PLUS_EXPR:
1310 case MINUS_EXPR:
1311 case MULT_EXPR:
1312 case POINTER_PLUS_EXPR:
1313 case TRUNC_DIV_EXPR:
1314 case CEIL_DIV_EXPR:
1315 case FLOOR_DIV_EXPR:
1316 case TRUNC_MOD_EXPR:
1317 case RDIV_EXPR:
1318 case EXACT_DIV_EXPR:
1319 case LSHIFT_EXPR:
1320 case RSHIFT_EXPR:
1321 case BIT_IOR_EXPR:
1322 case BIT_XOR_EXPR:
1323 case BIT_AND_EXPR:
1324 case LT_EXPR:
1325 case LE_EXPR:
1326 case GT_EXPR:
1327 case GE_EXPR:
1328 case EQ_EXPR:
1329 case NE_EXPR:
1330 case COMPLEX_EXPR:
1331 case TRUTH_AND_EXPR:
1332 case TRUTH_OR_EXPR:
1333 case TRUTH_XOR_EXPR:
1334 case UNORDERED_EXPR:
1335 case ORDERED_EXPR:
1336 case UNLT_EXPR:
1337 case UNLE_EXPR:
1338 case UNGT_EXPR:
1339 case UNGE_EXPR:
1340 case UNEQ_EXPR:
1341 /* Binary operations evaluating both arguments (increment and
1342 decrement are binary internally in GCC). */
1343 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1344 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1345 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1346 maybe_const_itself);
1347 STRIP_TYPE_NOPS (op0);
1348 if (code != MODIFY_EXPR
1349 && code != PREDECREMENT_EXPR
1350 && code != PREINCREMENT_EXPR
1351 && code != POSTDECREMENT_EXPR
1352 && code != POSTINCREMENT_EXPR)
1353 op0 = decl_constant_value_for_optimization (op0);
1354 /* The RHS of a MODIFY_EXPR was fully folded when building that
1355 expression for the sake of conversion warnings. */
1356 if (code != MODIFY_EXPR)
1357 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1358 maybe_const_itself);
1359 STRIP_TYPE_NOPS (op1);
1360 op1 = decl_constant_value_for_optimization (op1);
1361 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1362 ret = in_init
1363 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1364 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1365 else
1366 ret = fold (expr);
1367 if (TREE_OVERFLOW_P (ret)
1368 && !TREE_OVERFLOW_P (op0)
1369 && !TREE_OVERFLOW_P (op1))
1370 overflow_warning (EXPR_LOCATION (expr), ret);
1371 if (code == LSHIFT_EXPR
1372 && TREE_CODE (orig_op0) != INTEGER_CST
1373 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1374 && TREE_CODE (op0) == INTEGER_CST
1375 && c_inhibit_evaluation_warnings == 0
1376 && tree_int_cst_sgn (op0) < 0)
1377 warning_at (loc, OPT_Wshift_negative_value,
1378 "left shift of negative value");
1379 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1380 && TREE_CODE (orig_op1) != INTEGER_CST
1381 && TREE_CODE (op1) == INTEGER_CST
1382 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1383 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1384 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1385 && c_inhibit_evaluation_warnings == 0)
1387 if (tree_int_cst_sgn (op1) < 0)
1388 warning_at (loc, OPT_Wshift_count_negative,
1389 (code == LSHIFT_EXPR
1390 ? G_("left shift count is negative")
1391 : G_("right shift count is negative")));
1392 else if (compare_tree_int (op1,
1393 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1394 >= 0)
1395 warning_at (loc, OPT_Wshift_count_overflow,
1396 (code == LSHIFT_EXPR
1397 ? G_("left shift count >= width of type")
1398 : G_("right shift count >= width of type")));
1400 if ((code == TRUNC_DIV_EXPR
1401 || code == CEIL_DIV_EXPR
1402 || code == FLOOR_DIV_EXPR
1403 || code == EXACT_DIV_EXPR
1404 || code == TRUNC_MOD_EXPR)
1405 && TREE_CODE (orig_op1) != INTEGER_CST
1406 && TREE_CODE (op1) == INTEGER_CST
1407 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1408 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1409 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1410 warn_for_div_by_zero (loc, op1);
1411 goto out;
1413 case INDIRECT_REF:
1414 case FIX_TRUNC_EXPR:
1415 case FLOAT_EXPR:
1416 CASE_CONVERT:
1417 case ADDR_SPACE_CONVERT_EXPR:
1418 case VIEW_CONVERT_EXPR:
1419 case NON_LVALUE_EXPR:
1420 case NEGATE_EXPR:
1421 case BIT_NOT_EXPR:
1422 case TRUTH_NOT_EXPR:
1423 case ADDR_EXPR:
1424 case CONJ_EXPR:
1425 case REALPART_EXPR:
1426 case IMAGPART_EXPR:
1427 /* Unary operations. */
1428 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1429 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1430 maybe_const_itself);
1431 STRIP_TYPE_NOPS (op0);
1432 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1433 op0 = decl_constant_value_for_optimization (op0);
1434 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1435 not prepared to deal with them if they occur in initializers. */
1436 if (op0 != orig_op0
1437 && code == ADDR_EXPR
1438 && (op1 = get_base_address (op0)) != NULL_TREE
1439 && TREE_CODE (op1) == INDIRECT_REF
1440 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1441 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1442 else if (op0 != orig_op0 || in_init)
1443 ret = in_init
1444 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1445 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1446 else
1447 ret = fold (expr);
1448 if (code == INDIRECT_REF
1449 && ret != expr
1450 && TREE_CODE (ret) == INDIRECT_REF)
1452 TREE_READONLY (ret) = TREE_READONLY (expr);
1453 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1454 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1456 switch (code)
1458 case FIX_TRUNC_EXPR:
1459 case FLOAT_EXPR:
1460 CASE_CONVERT:
1461 /* Don't warn about explicit conversions. We will already
1462 have warned about suspect implicit conversions. */
1463 break;
1465 default:
1466 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1467 overflow_warning (EXPR_LOCATION (expr), ret);
1468 break;
1470 goto out;
1472 case TRUTH_ANDIF_EXPR:
1473 case TRUTH_ORIF_EXPR:
1474 /* Binary operations not necessarily evaluating both
1475 arguments. */
1476 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1477 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1478 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1479 STRIP_TYPE_NOPS (op0);
1481 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1482 ? truthvalue_false_node
1483 : truthvalue_true_node));
1484 c_disable_warnings (unused_p);
1485 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1486 STRIP_TYPE_NOPS (op1);
1487 c_enable_warnings (unused_p);
1489 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1490 ret = in_init
1491 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1492 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1493 else
1494 ret = fold (expr);
1495 *maybe_const_operands &= op0_const;
1496 *maybe_const_itself &= op0_const_self;
1497 if (!(flag_isoc99
1498 && op0_const
1499 && op0_const_self
1500 && (code == TRUTH_ANDIF_EXPR
1501 ? op0 == truthvalue_false_node
1502 : op0 == truthvalue_true_node)))
1503 *maybe_const_operands &= op1_const;
1504 if (!(op0_const
1505 && op0_const_self
1506 && (code == TRUTH_ANDIF_EXPR
1507 ? op0 == truthvalue_false_node
1508 : op0 == truthvalue_true_node)))
1509 *maybe_const_itself &= op1_const_self;
1510 goto out;
1512 case COND_EXPR:
1513 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1514 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1515 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1516 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1518 STRIP_TYPE_NOPS (op0);
1519 c_disable_warnings (op0 == truthvalue_false_node);
1520 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1521 STRIP_TYPE_NOPS (op1);
1522 c_enable_warnings (op0 == truthvalue_false_node);
1524 c_disable_warnings (op0 == truthvalue_true_node);
1525 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1526 STRIP_TYPE_NOPS (op2);
1527 c_enable_warnings (op0 == truthvalue_true_node);
1529 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1530 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1531 else
1532 ret = fold (expr);
1533 *maybe_const_operands &= op0_const;
1534 *maybe_const_itself &= op0_const_self;
1535 if (!(flag_isoc99
1536 && op0_const
1537 && op0_const_self
1538 && op0 == truthvalue_false_node))
1539 *maybe_const_operands &= op1_const;
1540 if (!(op0_const
1541 && op0_const_self
1542 && op0 == truthvalue_false_node))
1543 *maybe_const_itself &= op1_const_self;
1544 if (!(flag_isoc99
1545 && op0_const
1546 && op0_const_self
1547 && op0 == truthvalue_true_node))
1548 *maybe_const_operands &= op2_const;
1549 if (!(op0_const
1550 && op0_const_self
1551 && op0 == truthvalue_true_node))
1552 *maybe_const_itself &= op2_const_self;
1553 goto out;
1555 case EXCESS_PRECISION_EXPR:
1556 /* Each case where an operand with excess precision may be
1557 encountered must remove the EXCESS_PRECISION_EXPR around
1558 inner operands and possibly put one around the whole
1559 expression or possibly convert to the semantic type (which
1560 c_fully_fold does); we cannot tell at this stage which is
1561 appropriate in any particular case. */
1562 gcc_unreachable ();
1564 default:
1565 /* Various codes may appear through folding built-in functions
1566 and their arguments. */
1567 goto out;
1570 out:
1571 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1572 have been done by this point, so remove them again. */
1573 nowarning |= TREE_NO_WARNING (ret);
1574 STRIP_TYPE_NOPS (ret);
1575 if (nowarning && !TREE_NO_WARNING (ret))
1577 if (!CAN_HAVE_LOCATION_P (ret))
1578 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1579 TREE_NO_WARNING (ret) = 1;
1581 if (ret != expr)
1582 protected_set_expr_location (ret, loc);
1583 return ret;
1586 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1587 return EXP. Otherwise, return either EXP or its known constant
1588 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1589 Is the BLKmode test appropriate? */
1591 tree
1592 decl_constant_value_for_optimization (tree exp)
1594 tree ret;
1596 /* This function is only used by C, for c_fully_fold and other
1597 optimization, and may not be correct for C++. */
1598 if (c_dialect_cxx ())
1599 gcc_unreachable ();
1601 if (!optimize
1602 || TREE_CODE (exp) != VAR_DECL
1603 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1604 || DECL_MODE (exp) == BLKmode)
1605 return exp;
1607 ret = decl_constant_value (exp);
1608 /* Avoid unwanted tree sharing between the initializer and current
1609 function's body where the tree can be modified e.g. by the
1610 gimplifier. */
1611 if (ret != exp && TREE_STATIC (exp))
1612 ret = unshare_expr (ret);
1613 return ret;
1616 /* Print a warning if a constant expression had overflow in folding.
1617 Invoke this function on every expression that the language
1618 requires to be a constant expression.
1619 Note the ANSI C standard says it is erroneous for a
1620 constant expression to overflow. */
1622 void
1623 constant_expression_warning (tree value)
1625 if (warn_overflow && pedantic
1626 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1627 || TREE_CODE (value) == FIXED_CST
1628 || TREE_CODE (value) == VECTOR_CST
1629 || TREE_CODE (value) == COMPLEX_CST)
1630 && TREE_OVERFLOW (value))
1631 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1634 /* The same as above but print an unconditional error. */
1635 void
1636 constant_expression_error (tree value)
1638 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1639 || TREE_CODE (value) == FIXED_CST
1640 || TREE_CODE (value) == VECTOR_CST
1641 || TREE_CODE (value) == COMPLEX_CST)
1642 && TREE_OVERFLOW (value))
1643 error ("overflow in constant expression");
1646 /* Print a warning if an expression had overflow in folding and its
1647 operands hadn't.
1649 Invoke this function on every expression that
1650 (1) appears in the source code, and
1651 (2) is a constant expression that overflowed, and
1652 (3) is not already checked by convert_and_check;
1653 however, do not invoke this function on operands of explicit casts
1654 or when the expression is the result of an operator and any operand
1655 already overflowed. */
1657 void
1658 overflow_warning (location_t loc, tree value)
1660 if (c_inhibit_evaluation_warnings != 0)
1661 return;
1663 switch (TREE_CODE (value))
1665 case INTEGER_CST:
1666 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1667 break;
1669 case REAL_CST:
1670 warning_at (loc, OPT_Woverflow,
1671 "floating point overflow in expression");
1672 break;
1674 case FIXED_CST:
1675 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1676 break;
1678 case VECTOR_CST:
1679 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1680 break;
1682 case COMPLEX_CST:
1683 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1684 warning_at (loc, OPT_Woverflow,
1685 "complex integer overflow in expression");
1686 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1687 warning_at (loc, OPT_Woverflow,
1688 "complex floating point overflow in expression");
1689 break;
1691 default:
1692 break;
1696 /* Warn about uses of logical || / && operator in a context where it
1697 is likely that the bitwise equivalent was intended by the
1698 programmer. We have seen an expression in which CODE is a binary
1699 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1700 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1701 void
1702 warn_logical_operator (location_t location, enum tree_code code, tree type,
1703 enum tree_code code_left, tree op_left,
1704 enum tree_code ARG_UNUSED (code_right), tree op_right)
1706 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1707 int in0_p, in1_p, in_p;
1708 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1709 bool strict_overflow_p = false;
1711 if (code != TRUTH_ANDIF_EXPR
1712 && code != TRUTH_AND_EXPR
1713 && code != TRUTH_ORIF_EXPR
1714 && code != TRUTH_OR_EXPR)
1715 return;
1717 /* We don't want to warn if either operand comes from a macro
1718 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1719 see PR61534. */
1720 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1721 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1722 return;
1724 /* Warn if &&/|| are being used in a context where it is
1725 likely that the bitwise equivalent was intended by the
1726 programmer. That is, an expression such as op && MASK
1727 where op should not be any boolean expression, nor a
1728 constant, and mask seems to be a non-boolean integer constant. */
1729 if (!truth_value_p (code_left)
1730 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1731 && !CONSTANT_CLASS_P (op_left)
1732 && !TREE_NO_WARNING (op_left)
1733 && TREE_CODE (op_right) == INTEGER_CST
1734 && !integer_zerop (op_right)
1735 && !integer_onep (op_right))
1737 if (or_op)
1738 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1739 " applied to non-boolean constant");
1740 else
1741 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1742 " applied to non-boolean constant");
1743 TREE_NO_WARNING (op_left) = true;
1744 return;
1747 /* We do not warn for constants because they are typical of macro
1748 expansions that test for features. */
1749 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1750 return;
1752 /* This warning only makes sense with logical operands. */
1753 if (!(truth_value_p (TREE_CODE (op_left))
1754 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1755 || !(truth_value_p (TREE_CODE (op_right))
1756 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1757 return;
1759 /* The range computations only work with scalars. */
1760 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1761 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1762 return;
1764 /* We first test whether either side separately is trivially true
1765 (with OR) or trivially false (with AND). If so, do not warn.
1766 This is a common idiom for testing ranges of data types in
1767 portable code. */
1768 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1769 if (!lhs)
1770 return;
1771 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1772 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1774 /* If this is an OR operation, invert both sides; now, the result
1775 should be always false to get a warning. */
1776 if (or_op)
1777 in0_p = !in0_p;
1779 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1780 if (tem && integer_zerop (tem))
1781 return;
1783 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1784 if (!rhs)
1785 return;
1786 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1787 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1789 /* If this is an OR operation, invert both sides; now, the result
1790 should be always false to get a warning. */
1791 if (or_op)
1792 in1_p = !in1_p;
1794 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1795 if (tem && integer_zerop (tem))
1796 return;
1798 /* If both expressions have the same operand, if we can merge the
1799 ranges, ... */
1800 if (operand_equal_p (lhs, rhs, 0)
1801 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1802 in1_p, low1, high1))
1804 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1805 /* ... and if the range test is always false, then warn. */
1806 if (tem && integer_zerop (tem))
1808 if (or_op)
1809 warning_at (location, OPT_Wlogical_op,
1810 "logical %<or%> of collectively exhaustive tests is "
1811 "always true");
1812 else
1813 warning_at (location, OPT_Wlogical_op,
1814 "logical %<and%> of mutually exclusive tests is "
1815 "always false");
1817 /* Or warn if the operands have exactly the same range, e.g.
1818 A > 0 && A > 0. */
1819 else if (low0 == low1 && high0 == high1)
1821 if (or_op)
1822 warning_at (location, OPT_Wlogical_op,
1823 "logical %<or%> of equal expressions");
1824 else
1825 warning_at (location, OPT_Wlogical_op,
1826 "logical %<and%> of equal expressions");
1831 /* Warn about logical not used on the left hand side operand of a comparison.
1832 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1833 Do not warn if RHS is of a boolean type. */
1835 void
1836 warn_logical_not_parentheses (location_t location, enum tree_code code,
1837 tree rhs)
1839 if (TREE_CODE_CLASS (code) != tcc_comparison
1840 || TREE_TYPE (rhs) == NULL_TREE
1841 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1842 return;
1844 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1845 !(x == 0) or !(y != 0). */
1846 if ((code == EQ_EXPR || code == NE_EXPR)
1847 && integer_zerop (rhs))
1848 return;
1850 warning_at (location, OPT_Wlogical_not_parentheses,
1851 "logical not is only applied to the left hand side of "
1852 "comparison");
1855 /* Warn if EXP contains any computations whose results are not used.
1856 Return true if a warning is printed; false otherwise. LOCUS is the
1857 (potential) location of the expression. */
1859 bool
1860 warn_if_unused_value (const_tree exp, location_t locus)
1862 restart:
1863 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1864 return false;
1866 /* Don't warn about void constructs. This includes casting to void,
1867 void function calls, and statement expressions with a final cast
1868 to void. */
1869 if (VOID_TYPE_P (TREE_TYPE (exp)))
1870 return false;
1872 if (EXPR_HAS_LOCATION (exp))
1873 locus = EXPR_LOCATION (exp);
1875 switch (TREE_CODE (exp))
1877 case PREINCREMENT_EXPR:
1878 case POSTINCREMENT_EXPR:
1879 case PREDECREMENT_EXPR:
1880 case POSTDECREMENT_EXPR:
1881 case MODIFY_EXPR:
1882 case INIT_EXPR:
1883 case TARGET_EXPR:
1884 case CALL_EXPR:
1885 case TRY_CATCH_EXPR:
1886 case WITH_CLEANUP_EXPR:
1887 case EXIT_EXPR:
1888 case VA_ARG_EXPR:
1889 return false;
1891 case BIND_EXPR:
1892 /* For a binding, warn if no side effect within it. */
1893 exp = BIND_EXPR_BODY (exp);
1894 goto restart;
1896 case SAVE_EXPR:
1897 case NON_LVALUE_EXPR:
1898 case NOP_EXPR:
1899 exp = TREE_OPERAND (exp, 0);
1900 goto restart;
1902 case TRUTH_ORIF_EXPR:
1903 case TRUTH_ANDIF_EXPR:
1904 /* In && or ||, warn if 2nd operand has no side effect. */
1905 exp = TREE_OPERAND (exp, 1);
1906 goto restart;
1908 case COMPOUND_EXPR:
1909 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1910 return true;
1911 /* Let people do `(foo (), 0)' without a warning. */
1912 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1913 return false;
1914 exp = TREE_OPERAND (exp, 1);
1915 goto restart;
1917 case COND_EXPR:
1918 /* If this is an expression with side effects, don't warn; this
1919 case commonly appears in macro expansions. */
1920 if (TREE_SIDE_EFFECTS (exp))
1921 return false;
1922 goto warn;
1924 case INDIRECT_REF:
1925 /* Don't warn about automatic dereferencing of references, since
1926 the user cannot control it. */
1927 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1929 exp = TREE_OPERAND (exp, 0);
1930 goto restart;
1932 /* Fall through. */
1934 default:
1935 /* Referencing a volatile value is a side effect, so don't warn. */
1936 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1937 && TREE_THIS_VOLATILE (exp))
1938 return false;
1940 /* If this is an expression which has no operands, there is no value
1941 to be unused. There are no such language-independent codes,
1942 but front ends may define such. */
1943 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1944 return false;
1946 warn:
1947 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1952 /* Print a warning about casts that might indicate violation
1953 of strict aliasing rules if -Wstrict-aliasing is used and
1954 strict aliasing mode is in effect. OTYPE is the original
1955 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1957 bool
1958 strict_aliasing_warning (tree otype, tree type, tree expr)
1960 /* Strip pointer conversion chains and get to the correct original type. */
1961 STRIP_NOPS (expr);
1962 otype = TREE_TYPE (expr);
1964 if (!(flag_strict_aliasing
1965 && POINTER_TYPE_P (type)
1966 && POINTER_TYPE_P (otype)
1967 && !VOID_TYPE_P (TREE_TYPE (type)))
1968 /* If the type we are casting to is a ref-all pointer
1969 dereferencing it is always valid. */
1970 || TYPE_REF_CAN_ALIAS_ALL (type))
1971 return false;
1973 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1974 && (DECL_P (TREE_OPERAND (expr, 0))
1975 || handled_component_p (TREE_OPERAND (expr, 0))))
1977 /* Casting the address of an object to non void pointer. Warn
1978 if the cast breaks type based aliasing. */
1979 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1981 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1982 "might break strict-aliasing rules");
1983 return true;
1985 else
1987 /* warn_strict_aliasing >= 3. This includes the default (3).
1988 Only warn if the cast is dereferenced immediately. */
1989 alias_set_type set1 =
1990 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1991 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1993 if (set1 != set2 && set2 != 0
1994 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1996 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1997 "pointer will break strict-aliasing rules");
1998 return true;
2000 else if (warn_strict_aliasing == 2
2001 && !alias_sets_must_conflict_p (set1, set2))
2003 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2004 "pointer might break strict-aliasing rules");
2005 return true;
2009 else
2010 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2012 /* At this level, warn for any conversions, even if an address is
2013 not taken in the same statement. This will likely produce many
2014 false positives, but could be useful to pinpoint problems that
2015 are not revealed at higher levels. */
2016 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2017 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2018 if (!COMPLETE_TYPE_P (type)
2019 || !alias_sets_must_conflict_p (set1, set2))
2021 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2022 "pointer might break strict-aliasing rules");
2023 return true;
2027 return false;
2030 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2031 sizeof as last operand of certain builtins. */
2033 void
2034 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
2035 vec<tree, va_gc> *params, tree *sizeof_arg,
2036 bool (*comp_types) (tree, tree))
2038 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
2039 bool strop = false, cmp = false;
2040 unsigned int idx = ~0;
2041 location_t loc;
2043 if (TREE_CODE (callee) != FUNCTION_DECL
2044 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2045 || vec_safe_length (params) <= 1)
2046 return;
2048 switch (DECL_FUNCTION_CODE (callee))
2050 case BUILT_IN_STRNCMP:
2051 case BUILT_IN_STRNCASECMP:
2052 cmp = true;
2053 /* FALLTHRU */
2054 case BUILT_IN_STRNCPY:
2055 case BUILT_IN_STRNCPY_CHK:
2056 case BUILT_IN_STRNCAT:
2057 case BUILT_IN_STRNCAT_CHK:
2058 case BUILT_IN_STPNCPY:
2059 case BUILT_IN_STPNCPY_CHK:
2060 strop = true;
2061 /* FALLTHRU */
2062 case BUILT_IN_MEMCPY:
2063 case BUILT_IN_MEMCPY_CHK:
2064 case BUILT_IN_MEMMOVE:
2065 case BUILT_IN_MEMMOVE_CHK:
2066 if (params->length () < 3)
2067 return;
2068 src = (*params)[1];
2069 dest = (*params)[0];
2070 idx = 2;
2071 break;
2072 case BUILT_IN_BCOPY:
2073 if (params->length () < 3)
2074 return;
2075 src = (*params)[0];
2076 dest = (*params)[1];
2077 idx = 2;
2078 break;
2079 case BUILT_IN_MEMCMP:
2080 case BUILT_IN_BCMP:
2081 if (params->length () < 3)
2082 return;
2083 src = (*params)[1];
2084 dest = (*params)[0];
2085 idx = 2;
2086 cmp = true;
2087 break;
2088 case BUILT_IN_MEMSET:
2089 case BUILT_IN_MEMSET_CHK:
2090 if (params->length () < 3)
2091 return;
2092 dest = (*params)[0];
2093 idx = 2;
2094 break;
2095 case BUILT_IN_BZERO:
2096 dest = (*params)[0];
2097 idx = 1;
2098 break;
2099 case BUILT_IN_STRNDUP:
2100 src = (*params)[0];
2101 strop = true;
2102 idx = 1;
2103 break;
2104 case BUILT_IN_MEMCHR:
2105 if (params->length () < 3)
2106 return;
2107 src = (*params)[0];
2108 idx = 2;
2109 break;
2110 case BUILT_IN_SNPRINTF:
2111 case BUILT_IN_SNPRINTF_CHK:
2112 case BUILT_IN_VSNPRINTF:
2113 case BUILT_IN_VSNPRINTF_CHK:
2114 dest = (*params)[0];
2115 idx = 1;
2116 strop = true;
2117 break;
2118 default:
2119 break;
2122 if (idx >= 3)
2123 return;
2125 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2126 return;
2128 type = TYPE_P (sizeof_arg[idx])
2129 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2130 if (!POINTER_TYPE_P (type))
2131 return;
2133 if (dest
2134 && (tem = tree_strip_nop_conversions (dest))
2135 && POINTER_TYPE_P (TREE_TYPE (tem))
2136 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2137 return;
2139 if (src
2140 && (tem = tree_strip_nop_conversions (src))
2141 && POINTER_TYPE_P (TREE_TYPE (tem))
2142 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2143 return;
2145 loc = sizeof_arg_loc[idx];
2147 if (dest && !cmp)
2149 if (!TYPE_P (sizeof_arg[idx])
2150 && operand_equal_p (dest, sizeof_arg[idx], 0)
2151 && comp_types (TREE_TYPE (dest), type))
2153 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2154 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2155 "argument to %<sizeof%> in %qD call is the same "
2156 "expression as the destination; did you mean to "
2157 "remove the addressof?", callee);
2158 else if ((TYPE_PRECISION (TREE_TYPE (type))
2159 == TYPE_PRECISION (char_type_node))
2160 || strop)
2161 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2162 "argument to %<sizeof%> in %qD call is the same "
2163 "expression as the destination; did you mean to "
2164 "provide an explicit length?", callee);
2165 else
2166 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2167 "argument to %<sizeof%> in %qD call is the same "
2168 "expression as the destination; did you mean to "
2169 "dereference it?", callee);
2170 return;
2173 if (POINTER_TYPE_P (TREE_TYPE (dest))
2174 && !strop
2175 && comp_types (TREE_TYPE (dest), type)
2176 && !VOID_TYPE_P (TREE_TYPE (type)))
2178 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2179 "argument to %<sizeof%> in %qD call is the same "
2180 "pointer type %qT as the destination; expected %qT "
2181 "or an explicit length", callee, TREE_TYPE (dest),
2182 TREE_TYPE (TREE_TYPE (dest)));
2183 return;
2187 if (src && !cmp)
2189 if (!TYPE_P (sizeof_arg[idx])
2190 && operand_equal_p (src, sizeof_arg[idx], 0)
2191 && comp_types (TREE_TYPE (src), type))
2193 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2194 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2195 "argument to %<sizeof%> in %qD call is the same "
2196 "expression as the source; did you mean to "
2197 "remove the addressof?", callee);
2198 else if ((TYPE_PRECISION (TREE_TYPE (type))
2199 == TYPE_PRECISION (char_type_node))
2200 || strop)
2201 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2202 "argument to %<sizeof%> in %qD call is the same "
2203 "expression as the source; did you mean to "
2204 "provide an explicit length?", callee);
2205 else
2206 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2207 "argument to %<sizeof%> in %qD call is the same "
2208 "expression as the source; did you mean to "
2209 "dereference it?", callee);
2210 return;
2213 if (POINTER_TYPE_P (TREE_TYPE (src))
2214 && !strop
2215 && comp_types (TREE_TYPE (src), type)
2216 && !VOID_TYPE_P (TREE_TYPE (type)))
2218 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2219 "argument to %<sizeof%> in %qD call is the same "
2220 "pointer type %qT as the source; expected %qT "
2221 "or an explicit length", callee, TREE_TYPE (src),
2222 TREE_TYPE (TREE_TYPE (src)));
2223 return;
2227 if (dest)
2229 if (!TYPE_P (sizeof_arg[idx])
2230 && operand_equal_p (dest, sizeof_arg[idx], 0)
2231 && comp_types (TREE_TYPE (dest), type))
2233 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2234 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2235 "argument to %<sizeof%> in %qD call is the same "
2236 "expression as the first source; did you mean to "
2237 "remove the addressof?", callee);
2238 else if ((TYPE_PRECISION (TREE_TYPE (type))
2239 == TYPE_PRECISION (char_type_node))
2240 || strop)
2241 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2242 "argument to %<sizeof%> in %qD call is the same "
2243 "expression as the first source; did you mean to "
2244 "provide an explicit length?", callee);
2245 else
2246 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2247 "argument to %<sizeof%> in %qD call is the same "
2248 "expression as the first source; did you mean to "
2249 "dereference it?", callee);
2250 return;
2253 if (POINTER_TYPE_P (TREE_TYPE (dest))
2254 && !strop
2255 && comp_types (TREE_TYPE (dest), type)
2256 && !VOID_TYPE_P (TREE_TYPE (type)))
2258 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2259 "argument to %<sizeof%> in %qD call is the same "
2260 "pointer type %qT as the first source; expected %qT "
2261 "or an explicit length", callee, TREE_TYPE (dest),
2262 TREE_TYPE (TREE_TYPE (dest)));
2263 return;
2267 if (src)
2269 if (!TYPE_P (sizeof_arg[idx])
2270 && operand_equal_p (src, sizeof_arg[idx], 0)
2271 && comp_types (TREE_TYPE (src), type))
2273 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2274 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2275 "argument to %<sizeof%> in %qD call is the same "
2276 "expression as the second source; did you mean to "
2277 "remove the addressof?", callee);
2278 else if ((TYPE_PRECISION (TREE_TYPE (type))
2279 == TYPE_PRECISION (char_type_node))
2280 || strop)
2281 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2282 "argument to %<sizeof%> in %qD call is the same "
2283 "expression as the second source; did you mean to "
2284 "provide an explicit length?", callee);
2285 else
2286 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2287 "argument to %<sizeof%> in %qD call is the same "
2288 "expression as the second source; did you mean to "
2289 "dereference it?", callee);
2290 return;
2293 if (POINTER_TYPE_P (TREE_TYPE (src))
2294 && !strop
2295 && comp_types (TREE_TYPE (src), type)
2296 && !VOID_TYPE_P (TREE_TYPE (type)))
2298 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2299 "argument to %<sizeof%> in %qD call is the same "
2300 "pointer type %qT as the second source; expected %qT "
2301 "or an explicit length", callee, TREE_TYPE (src),
2302 TREE_TYPE (TREE_TYPE (src)));
2303 return;
2309 /* Warn for unlikely, improbable, or stupid DECL declarations
2310 of `main'. */
2312 void
2313 check_main_parameter_types (tree decl)
2315 function_args_iterator iter;
2316 tree type;
2317 int argct = 0;
2319 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2321 /* XXX void_type_node belies the abstraction. */
2322 if (type == void_type_node || type == error_mark_node )
2323 break;
2325 tree t = type;
2326 if (TYPE_ATOMIC (t))
2327 pedwarn (input_location, OPT_Wmain,
2328 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2329 type, decl);
2330 while (POINTER_TYPE_P (t))
2332 t = TREE_TYPE (t);
2333 if (TYPE_ATOMIC (t))
2334 pedwarn (input_location, OPT_Wmain,
2335 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2336 type, decl);
2339 ++argct;
2340 switch (argct)
2342 case 1:
2343 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2344 pedwarn (input_location, OPT_Wmain,
2345 "first argument of %q+D should be %<int%>", decl);
2346 break;
2348 case 2:
2349 if (TREE_CODE (type) != POINTER_TYPE
2350 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2351 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2352 != char_type_node))
2353 pedwarn (input_location, OPT_Wmain,
2354 "second argument of %q+D should be %<char **%>", decl);
2355 break;
2357 case 3:
2358 if (TREE_CODE (type) != POINTER_TYPE
2359 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2360 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2361 != char_type_node))
2362 pedwarn (input_location, OPT_Wmain,
2363 "third argument of %q+D should probably be "
2364 "%<char **%>", decl);
2365 break;
2369 /* It is intentional that this message does not mention the third
2370 argument because it's only mentioned in an appendix of the
2371 standard. */
2372 if (argct > 0 && (argct < 2 || argct > 3))
2373 pedwarn (input_location, OPT_Wmain,
2374 "%q+D takes only zero or two arguments", decl);
2376 if (stdarg_p (TREE_TYPE (decl)))
2377 pedwarn (input_location, OPT_Wmain,
2378 "%q+D declared as variadic function", decl);
2381 /* vector_targets_convertible_p is used for vector pointer types. The
2382 callers perform various checks that the qualifiers are satisfactory,
2383 while OTOH vector_targets_convertible_p ignores the number of elements
2384 in the vectors. That's fine with vector pointers as we can consider,
2385 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2386 and that does not require and conversion of the pointer values.
2387 In contrast, vector_types_convertible_p and
2388 vector_types_compatible_elements_p are used for vector value types. */
2389 /* True if pointers to distinct types T1 and T2 can be converted to
2390 each other without an explicit cast. Only returns true for opaque
2391 vector types. */
2392 bool
2393 vector_targets_convertible_p (const_tree t1, const_tree t2)
2395 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2396 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2397 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2398 return true;
2400 return false;
2403 /* vector_types_convertible_p is used for vector value types.
2404 It could in principle call vector_targets_convertible_p as a subroutine,
2405 but then the check for vector type would be duplicated with its callers,
2406 and also the purpose of vector_targets_convertible_p would become
2407 muddled.
2408 Where vector_types_convertible_p returns true, a conversion might still be
2409 needed to make the types match.
2410 In contrast, vector_targets_convertible_p is used for vector pointer
2411 values, and vector_types_compatible_elements_p is used specifically
2412 in the context for binary operators, as a check if use is possible without
2413 conversion. */
2414 /* True if vector types T1 and T2 can be converted to each other
2415 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2416 can only be converted with -flax-vector-conversions yet that is not
2417 in effect, emit a note telling the user about that option if such
2418 a note has not previously been emitted. */
2419 bool
2420 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2422 static bool emitted_lax_note = false;
2423 bool convertible_lax;
2425 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2426 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2427 return true;
2429 convertible_lax =
2430 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2431 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2432 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2433 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2434 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2436 if (!convertible_lax || flag_lax_vector_conversions)
2437 return convertible_lax;
2439 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2440 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2441 return true;
2443 if (emit_lax_note && !emitted_lax_note)
2445 emitted_lax_note = true;
2446 inform (input_location, "use -flax-vector-conversions to permit "
2447 "conversions between vectors with differing "
2448 "element types or numbers of subparts");
2451 return false;
2454 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2455 and have vector types, V0 has the same type as V1, and the number of
2456 elements of V0, V1, MASK is the same.
2458 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2459 called with two arguments. In this case implementation passes the
2460 first argument twice in order to share the same tree code. This fact
2461 could enable the mask-values being twice the vector length. This is
2462 an implementation accident and this semantics is not guaranteed to
2463 the user. */
2464 tree
2465 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2466 bool complain)
2468 tree ret;
2469 bool wrap = true;
2470 bool maybe_const = false;
2471 bool two_arguments = false;
2473 if (v1 == NULL_TREE)
2475 two_arguments = true;
2476 v1 = v0;
2479 if (v0 == error_mark_node || v1 == error_mark_node
2480 || mask == error_mark_node)
2481 return error_mark_node;
2483 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2484 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2486 if (complain)
2487 error_at (loc, "__builtin_shuffle last argument must "
2488 "be an integer vector");
2489 return error_mark_node;
2492 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2493 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2495 if (complain)
2496 error_at (loc, "__builtin_shuffle arguments must be vectors");
2497 return error_mark_node;
2500 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2502 if (complain)
2503 error_at (loc, "__builtin_shuffle argument vectors must be of "
2504 "the same type");
2505 return error_mark_node;
2508 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2509 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2510 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2511 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2513 if (complain)
2514 error_at (loc, "__builtin_shuffle number of elements of the "
2515 "argument vector(s) and the mask vector should "
2516 "be the same");
2517 return error_mark_node;
2520 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2521 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2523 if (complain)
2524 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2525 "must have the same size as inner type of the mask");
2526 return error_mark_node;
2529 if (!c_dialect_cxx ())
2531 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2532 v0 = c_fully_fold (v0, false, &maybe_const);
2533 wrap &= maybe_const;
2535 if (two_arguments)
2536 v1 = v0 = save_expr (v0);
2537 else
2539 v1 = c_fully_fold (v1, false, &maybe_const);
2540 wrap &= maybe_const;
2543 mask = c_fully_fold (mask, false, &maybe_const);
2544 wrap &= maybe_const;
2546 else if (two_arguments)
2547 v1 = v0 = save_expr (v0);
2549 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2551 if (!c_dialect_cxx () && !wrap)
2552 ret = c_wrap_maybe_const (ret, true);
2554 return ret;
2557 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2558 to integral type. */
2560 static tree
2561 c_common_get_narrower (tree op, int *unsignedp_ptr)
2563 op = get_narrower (op, unsignedp_ptr);
2565 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2566 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2568 /* C++0x scoped enumerations don't implicitly convert to integral
2569 type; if we stripped an explicit conversion to a larger type we
2570 need to replace it so common_type will still work. */
2571 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2572 TYPE_UNSIGNED (TREE_TYPE (op)));
2573 op = fold_convert (type, op);
2575 return op;
2578 /* This is a helper function of build_binary_op.
2580 For certain operations if both args were extended from the same
2581 smaller type, do the arithmetic in that type and then extend.
2583 BITWISE indicates a bitwise operation.
2584 For them, this optimization is safe only if
2585 both args are zero-extended or both are sign-extended.
2586 Otherwise, we might change the result.
2587 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2588 but calculated in (unsigned short) it would be (unsigned short)-1.
2590 tree
2591 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2593 int unsigned0, unsigned1;
2594 tree arg0, arg1;
2595 int uns;
2596 tree type;
2598 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2599 excessive narrowing when we call get_narrower below. For
2600 example, suppose that OP0 is of unsigned int extended
2601 from signed char and that RESULT_TYPE is long long int.
2602 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2603 like
2605 (long long int) (unsigned int) signed_char
2607 which get_narrower would narrow down to
2609 (unsigned int) signed char
2611 If we do not cast OP0 first, get_narrower would return
2612 signed_char, which is inconsistent with the case of the
2613 explicit cast. */
2614 op0 = convert (result_type, op0);
2615 op1 = convert (result_type, op1);
2617 arg0 = c_common_get_narrower (op0, &unsigned0);
2618 arg1 = c_common_get_narrower (op1, &unsigned1);
2620 /* UNS is 1 if the operation to be done is an unsigned one. */
2621 uns = TYPE_UNSIGNED (result_type);
2623 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2624 but it *requires* conversion to FINAL_TYPE. */
2626 if ((TYPE_PRECISION (TREE_TYPE (op0))
2627 == TYPE_PRECISION (TREE_TYPE (arg0)))
2628 && TREE_TYPE (op0) != result_type)
2629 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2630 if ((TYPE_PRECISION (TREE_TYPE (op1))
2631 == TYPE_PRECISION (TREE_TYPE (arg1)))
2632 && TREE_TYPE (op1) != result_type)
2633 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2635 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2637 /* For bitwise operations, signedness of nominal type
2638 does not matter. Consider only how operands were extended. */
2639 if (bitwise)
2640 uns = unsigned0;
2642 /* Note that in all three cases below we refrain from optimizing
2643 an unsigned operation on sign-extended args.
2644 That would not be valid. */
2646 /* Both args variable: if both extended in same way
2647 from same width, do it in that width.
2648 Do it unsigned if args were zero-extended. */
2649 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2650 < TYPE_PRECISION (result_type))
2651 && (TYPE_PRECISION (TREE_TYPE (arg1))
2652 == TYPE_PRECISION (TREE_TYPE (arg0)))
2653 && unsigned0 == unsigned1
2654 && (unsigned0 || !uns))
2655 return c_common_signed_or_unsigned_type
2656 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2658 else if (TREE_CODE (arg0) == INTEGER_CST
2659 && (unsigned1 || !uns)
2660 && (TYPE_PRECISION (TREE_TYPE (arg1))
2661 < TYPE_PRECISION (result_type))
2662 && (type
2663 = c_common_signed_or_unsigned_type (unsigned1,
2664 TREE_TYPE (arg1)))
2665 && !POINTER_TYPE_P (type)
2666 && int_fits_type_p (arg0, type))
2667 return type;
2669 else if (TREE_CODE (arg1) == INTEGER_CST
2670 && (unsigned0 || !uns)
2671 && (TYPE_PRECISION (TREE_TYPE (arg0))
2672 < TYPE_PRECISION (result_type))
2673 && (type
2674 = c_common_signed_or_unsigned_type (unsigned0,
2675 TREE_TYPE (arg0)))
2676 && !POINTER_TYPE_P (type)
2677 && int_fits_type_p (arg1, type))
2678 return type;
2680 return result_type;
2683 /* Checks if expression EXPR of real/integer type cannot be converted
2684 to the real/integer type TYPE. Function returns non-zero when:
2685 * EXPR is a constant which cannot be exactly converted to TYPE.
2686 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2687 for EXPR type and TYPE being both integers or both real.
2688 * EXPR is not a constant of real type and TYPE is an integer.
2689 * EXPR is not a constant of integer type which cannot be
2690 exactly converted to real type.
2691 Function allows conversions between types of different signedness and
2692 can return SAFE_CONVERSION (zero) in that case. Function can produce
2693 signedness warnings if PRODUCE_WARNS is true. */
2695 enum conversion_safety
2696 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2698 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2699 tree expr_type = TREE_TYPE (expr);
2700 loc = expansion_point_location_if_in_system_header (loc);
2702 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2704 /* Warn for real constant that is not an exact integer converted
2705 to integer type. */
2706 if (TREE_CODE (expr_type) == REAL_TYPE
2707 && TREE_CODE (type) == INTEGER_TYPE)
2709 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2710 give_warning = UNSAFE_REAL;
2712 /* Warn for an integer constant that does not fit into integer type. */
2713 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2714 && TREE_CODE (type) == INTEGER_TYPE
2715 && !int_fits_type_p (expr, type))
2717 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2718 && tree_int_cst_sgn (expr) < 0)
2720 if (produce_warns)
2721 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2722 " implicitly converted to unsigned type");
2724 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2726 if (produce_warns)
2727 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2728 " constant value to negative integer");
2730 else
2731 give_warning = UNSAFE_OTHER;
2733 else if (TREE_CODE (type) == REAL_TYPE)
2735 /* Warn for an integer constant that does not fit into real type. */
2736 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2738 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2739 if (!exact_real_truncate (TYPE_MODE (type), &a))
2740 give_warning = UNSAFE_REAL;
2742 /* Warn for a real constant that does not fit into a smaller
2743 real type. */
2744 else if (TREE_CODE (expr_type) == REAL_TYPE
2745 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2747 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2748 if (!exact_real_truncate (TYPE_MODE (type), &a))
2749 give_warning = UNSAFE_REAL;
2753 else
2755 /* Warn for real types converted to integer types. */
2756 if (TREE_CODE (expr_type) == REAL_TYPE
2757 && TREE_CODE (type) == INTEGER_TYPE)
2758 give_warning = UNSAFE_REAL;
2760 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2761 && TREE_CODE (type) == INTEGER_TYPE)
2763 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2764 expr = get_unwidened (expr, 0);
2765 expr_type = TREE_TYPE (expr);
2767 /* Don't warn for short y; short x = ((int)y & 0xff); */
2768 if (TREE_CODE (expr) == BIT_AND_EXPR
2769 || TREE_CODE (expr) == BIT_IOR_EXPR
2770 || TREE_CODE (expr) == BIT_XOR_EXPR)
2772 /* If both args were extended from a shortest type,
2773 use that type if that is safe. */
2774 expr_type = shorten_binary_op (expr_type,
2775 TREE_OPERAND (expr, 0),
2776 TREE_OPERAND (expr, 1),
2777 /* bitwise */1);
2779 if (TREE_CODE (expr) == BIT_AND_EXPR)
2781 tree op0 = TREE_OPERAND (expr, 0);
2782 tree op1 = TREE_OPERAND (expr, 1);
2783 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2784 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2786 /* If one of the operands is a non-negative constant
2787 that fits in the target type, then the type of the
2788 other operand does not matter. */
2789 if ((TREE_CODE (op0) == INTEGER_CST
2790 && int_fits_type_p (op0, c_common_signed_type (type))
2791 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2792 || (TREE_CODE (op1) == INTEGER_CST
2793 && int_fits_type_p (op1, c_common_signed_type (type))
2794 && int_fits_type_p (op1,
2795 c_common_unsigned_type (type))))
2796 return SAFE_CONVERSION;
2797 /* If constant is unsigned and fits in the target
2798 type, then the result will also fit. */
2799 else if ((TREE_CODE (op0) == INTEGER_CST
2800 && unsigned0
2801 && int_fits_type_p (op0, type))
2802 || (TREE_CODE (op1) == INTEGER_CST
2803 && unsigned1
2804 && int_fits_type_p (op1, type)))
2805 return SAFE_CONVERSION;
2808 /* Warn for integer types converted to smaller integer types. */
2809 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2810 give_warning = UNSAFE_OTHER;
2812 /* When they are the same width but different signedness,
2813 then the value may change. */
2814 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2815 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2816 /* Even when converted to a bigger type, if the type is
2817 unsigned but expr is signed, then negative values
2818 will be changed. */
2819 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2820 && produce_warns)
2821 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2822 "may change the sign of the result",
2823 type, expr_type);
2826 /* Warn for integer types converted to real types if and only if
2827 all the range of values of the integer type cannot be
2828 represented by the real type. */
2829 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2830 && TREE_CODE (type) == REAL_TYPE)
2832 tree type_low_bound, type_high_bound;
2833 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2835 /* Don't warn about char y = 0xff; float x = (int) y; */
2836 expr = get_unwidened (expr, 0);
2837 expr_type = TREE_TYPE (expr);
2839 type_low_bound = TYPE_MIN_VALUE (expr_type);
2840 type_high_bound = TYPE_MAX_VALUE (expr_type);
2841 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2842 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2844 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2845 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2846 give_warning = UNSAFE_OTHER;
2849 /* Warn for real types converted to smaller real types. */
2850 else if (TREE_CODE (expr_type) == REAL_TYPE
2851 && TREE_CODE (type) == REAL_TYPE
2852 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2853 give_warning = UNSAFE_REAL;
2856 return give_warning;
2859 /* Warns if the conversion of EXPR to TYPE may alter a value.
2860 This is a helper function for warnings_for_convert_and_check. */
2862 static void
2863 conversion_warning (location_t loc, tree type, tree expr)
2865 tree expr_type = TREE_TYPE (expr);
2866 enum conversion_safety conversion_kind;
2868 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2869 return;
2871 /* This may happen, because for LHS op= RHS we preevaluate
2872 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2873 means we could no longer see the code of the EXPR. */
2874 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2875 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2876 if (TREE_CODE (expr) == SAVE_EXPR)
2877 expr = TREE_OPERAND (expr, 0);
2879 switch (TREE_CODE (expr))
2881 case EQ_EXPR:
2882 case NE_EXPR:
2883 case LE_EXPR:
2884 case GE_EXPR:
2885 case LT_EXPR:
2886 case GT_EXPR:
2887 case TRUTH_ANDIF_EXPR:
2888 case TRUTH_ORIF_EXPR:
2889 case TRUTH_AND_EXPR:
2890 case TRUTH_OR_EXPR:
2891 case TRUTH_XOR_EXPR:
2892 case TRUTH_NOT_EXPR:
2893 /* Conversion from boolean to a signed:1 bit-field (which only
2894 can hold the values 0 and -1) doesn't lose information - but
2895 it does change the value. */
2896 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2897 warning_at (loc, OPT_Wconversion,
2898 "conversion to %qT from boolean expression", type);
2899 return;
2901 case REAL_CST:
2902 case INTEGER_CST:
2903 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2904 if (conversion_kind == UNSAFE_REAL)
2905 warning_at (loc, OPT_Wfloat_conversion,
2906 "conversion to %qT alters %qT constant value",
2907 type, expr_type);
2908 else if (conversion_kind)
2909 warning_at (loc, OPT_Wconversion,
2910 "conversion to %qT alters %qT constant value",
2911 type, expr_type);
2912 return;
2914 case COND_EXPR:
2916 /* In case of COND_EXPR, we do not care about the type of
2917 COND_EXPR, only about the conversion of each operand. */
2918 tree op1 = TREE_OPERAND (expr, 1);
2919 tree op2 = TREE_OPERAND (expr, 2);
2921 conversion_warning (loc, type, op1);
2922 conversion_warning (loc, type, op2);
2923 return;
2926 default: /* 'expr' is not a constant. */
2927 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2928 if (conversion_kind == UNSAFE_REAL)
2929 warning_at (loc, OPT_Wfloat_conversion,
2930 "conversion to %qT from %qT may alter its value",
2931 type, expr_type);
2932 else if (conversion_kind)
2933 warning_at (loc, OPT_Wconversion,
2934 "conversion to %qT from %qT may alter its value",
2935 type, expr_type);
2939 /* Produce warnings after a conversion. RESULT is the result of
2940 converting EXPR to TYPE. This is a helper function for
2941 convert_and_check and cp_convert_and_check. */
2943 void
2944 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2945 tree result)
2947 loc = expansion_point_location_if_in_system_header (loc);
2949 if (TREE_CODE (expr) == INTEGER_CST
2950 && (TREE_CODE (type) == INTEGER_TYPE
2951 || TREE_CODE (type) == ENUMERAL_TYPE)
2952 && !int_fits_type_p (expr, type))
2954 /* Do not diagnose overflow in a constant expression merely
2955 because a conversion overflowed. */
2956 if (TREE_OVERFLOW (result))
2957 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2959 if (TYPE_UNSIGNED (type))
2961 /* This detects cases like converting -129 or 256 to
2962 unsigned char. */
2963 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2964 warning_at (loc, OPT_Woverflow,
2965 "large integer implicitly truncated to unsigned type");
2966 else
2967 conversion_warning (loc, type, expr);
2969 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2970 warning_at (loc, OPT_Woverflow,
2971 "overflow in implicit constant conversion");
2972 /* No warning for converting 0x80000000 to int. */
2973 else if (pedantic
2974 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2975 || TYPE_PRECISION (TREE_TYPE (expr))
2976 != TYPE_PRECISION (type)))
2977 warning_at (loc, OPT_Woverflow,
2978 "overflow in implicit constant conversion");
2980 else
2981 conversion_warning (loc, type, expr);
2983 else if ((TREE_CODE (result) == INTEGER_CST
2984 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2985 warning_at (loc, OPT_Woverflow,
2986 "overflow in implicit constant conversion");
2987 else
2988 conversion_warning (loc, type, expr);
2992 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2993 Invoke this function on every expression that is converted implicitly,
2994 i.e. because of language rules and not because of an explicit cast. */
2996 tree
2997 convert_and_check (location_t loc, tree type, tree expr)
2999 tree result;
3000 tree expr_for_warning;
3002 /* Convert from a value with possible excess precision rather than
3003 via the semantic type, but do not warn about values not fitting
3004 exactly in the semantic type. */
3005 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3007 tree orig_type = TREE_TYPE (expr);
3008 expr = TREE_OPERAND (expr, 0);
3009 expr_for_warning = convert (orig_type, expr);
3010 if (orig_type == type)
3011 return expr_for_warning;
3013 else
3014 expr_for_warning = expr;
3016 if (TREE_TYPE (expr) == type)
3017 return expr;
3019 result = convert (type, expr);
3021 if (c_inhibit_evaluation_warnings == 0
3022 && !TREE_OVERFLOW_P (expr)
3023 && result != error_mark_node)
3024 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
3026 return result;
3029 /* A node in a list that describes references to variables (EXPR), which are
3030 either read accesses if WRITER is zero, or write accesses, in which case
3031 WRITER is the parent of EXPR. */
3032 struct tlist
3034 struct tlist *next;
3035 tree expr, writer;
3038 /* Used to implement a cache the results of a call to verify_tree. We only
3039 use this for SAVE_EXPRs. */
3040 struct tlist_cache
3042 struct tlist_cache *next;
3043 struct tlist *cache_before_sp;
3044 struct tlist *cache_after_sp;
3045 tree expr;
3048 /* Obstack to use when allocating tlist structures, and corresponding
3049 firstobj. */
3050 static struct obstack tlist_obstack;
3051 static char *tlist_firstobj = 0;
3053 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3054 warnings. */
3055 static struct tlist *warned_ids;
3056 /* SAVE_EXPRs need special treatment. We process them only once and then
3057 cache the results. */
3058 static struct tlist_cache *save_expr_cache;
3060 static void add_tlist (struct tlist **, struct tlist *, tree, int);
3061 static void merge_tlist (struct tlist **, struct tlist *, int);
3062 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3063 static int warning_candidate_p (tree);
3064 static bool candidate_equal_p (const_tree, const_tree);
3065 static void warn_for_collisions (struct tlist *);
3066 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3067 static struct tlist *new_tlist (struct tlist *, tree, tree);
3069 /* Create a new struct tlist and fill in its fields. */
3070 static struct tlist *
3071 new_tlist (struct tlist *next, tree t, tree writer)
3073 struct tlist *l;
3074 l = XOBNEW (&tlist_obstack, struct tlist);
3075 l->next = next;
3076 l->expr = t;
3077 l->writer = writer;
3078 return l;
3081 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3082 is nonnull, we ignore any node we find which has a writer equal to it. */
3084 static void
3085 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3087 while (add)
3089 struct tlist *next = add->next;
3090 if (!copy)
3091 add->next = *to;
3092 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3093 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3094 add = next;
3098 /* Merge the nodes of ADD into TO. This merging process is done so that for
3099 each variable that already exists in TO, no new node is added; however if
3100 there is a write access recorded in ADD, and an occurrence on TO is only
3101 a read access, then the occurrence in TO will be modified to record the
3102 write. */
3104 static void
3105 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3107 struct tlist **end = to;
3109 while (*end)
3110 end = &(*end)->next;
3112 while (add)
3114 int found = 0;
3115 struct tlist *tmp2;
3116 struct tlist *next = add->next;
3118 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3119 if (candidate_equal_p (tmp2->expr, add->expr))
3121 found = 1;
3122 if (!tmp2->writer)
3123 tmp2->writer = add->writer;
3125 if (!found)
3127 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3128 end = &(*end)->next;
3129 *end = 0;
3131 add = next;
3135 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3136 references in list LIST conflict with it, excluding reads if ONLY writers
3137 is nonzero. */
3139 static void
3140 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3141 int only_writes)
3143 struct tlist *tmp;
3145 /* Avoid duplicate warnings. */
3146 for (tmp = warned_ids; tmp; tmp = tmp->next)
3147 if (candidate_equal_p (tmp->expr, written))
3148 return;
3150 while (list)
3152 if (candidate_equal_p (list->expr, written)
3153 && !candidate_equal_p (list->writer, writer)
3154 && (!only_writes || list->writer))
3156 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3157 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3158 OPT_Wsequence_point, "operation on %qE may be undefined",
3159 list->expr);
3161 list = list->next;
3165 /* Given a list LIST of references to variables, find whether any of these
3166 can cause conflicts due to missing sequence points. */
3168 static void
3169 warn_for_collisions (struct tlist *list)
3171 struct tlist *tmp;
3173 for (tmp = list; tmp; tmp = tmp->next)
3175 if (tmp->writer)
3176 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3180 /* Return nonzero if X is a tree that can be verified by the sequence point
3181 warnings. */
3182 static int
3183 warning_candidate_p (tree x)
3185 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3186 return 0;
3188 if (TREE_CODE (x) == BLOCK)
3189 return 0;
3191 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3192 (lvalue_p) crash on TRY/CATCH. */
3193 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3194 return 0;
3196 if (!lvalue_p (x))
3197 return 0;
3199 /* No point to track non-const calls, they will never satisfy
3200 operand_equal_p. */
3201 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3202 return 0;
3204 if (TREE_CODE (x) == STRING_CST)
3205 return 0;
3207 return 1;
3210 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3211 static bool
3212 candidate_equal_p (const_tree x, const_tree y)
3214 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3217 /* Walk the tree X, and record accesses to variables. If X is written by the
3218 parent tree, WRITER is the parent.
3219 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3220 expression or its only operand forces a sequence point, then everything up
3221 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3222 in PNO_SP.
3223 Once we return, we will have emitted warnings if any subexpression before
3224 such a sequence point could be undefined. On a higher level, however, the
3225 sequence point may not be relevant, and we'll merge the two lists.
3227 Example: (b++, a) + b;
3228 The call that processes the COMPOUND_EXPR will store the increment of B
3229 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3230 processes the PLUS_EXPR will need to merge the two lists so that
3231 eventually, all accesses end up on the same list (and we'll warn about the
3232 unordered subexpressions b++ and b.
3234 A note on merging. If we modify the former example so that our expression
3235 becomes
3236 (b++, b) + a
3237 care must be taken not simply to add all three expressions into the final
3238 PNO_SP list. The function merge_tlist takes care of that by merging the
3239 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3240 way, so that no more than one access to B is recorded. */
3242 static void
3243 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3244 tree writer)
3246 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3247 enum tree_code code;
3248 enum tree_code_class cl;
3250 /* X may be NULL if it is the operand of an empty statement expression
3251 ({ }). */
3252 if (x == NULL)
3253 return;
3255 restart:
3256 code = TREE_CODE (x);
3257 cl = TREE_CODE_CLASS (code);
3259 if (warning_candidate_p (x))
3260 *pno_sp = new_tlist (*pno_sp, x, writer);
3262 switch (code)
3264 case CONSTRUCTOR:
3265 case SIZEOF_EXPR:
3266 return;
3268 case COMPOUND_EXPR:
3269 case TRUTH_ANDIF_EXPR:
3270 case TRUTH_ORIF_EXPR:
3271 tmp_before = tmp_nosp = tmp_list3 = 0;
3272 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3273 warn_for_collisions (tmp_nosp);
3274 merge_tlist (pbefore_sp, tmp_before, 0);
3275 merge_tlist (pbefore_sp, tmp_nosp, 0);
3276 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3277 merge_tlist (pbefore_sp, tmp_list3, 0);
3278 return;
3280 case COND_EXPR:
3281 tmp_before = tmp_list2 = 0;
3282 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3283 warn_for_collisions (tmp_list2);
3284 merge_tlist (pbefore_sp, tmp_before, 0);
3285 merge_tlist (pbefore_sp, tmp_list2, 0);
3287 tmp_list3 = tmp_nosp = 0;
3288 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3289 warn_for_collisions (tmp_nosp);
3290 merge_tlist (pbefore_sp, tmp_list3, 0);
3292 tmp_list3 = tmp_list2 = 0;
3293 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3294 warn_for_collisions (tmp_list2);
3295 merge_tlist (pbefore_sp, tmp_list3, 0);
3296 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3297 two first, to avoid warning for (a ? b++ : b++). */
3298 merge_tlist (&tmp_nosp, tmp_list2, 0);
3299 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3300 return;
3302 case PREDECREMENT_EXPR:
3303 case PREINCREMENT_EXPR:
3304 case POSTDECREMENT_EXPR:
3305 case POSTINCREMENT_EXPR:
3306 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3307 return;
3309 case MODIFY_EXPR:
3310 tmp_before = tmp_nosp = tmp_list3 = 0;
3311 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3312 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3313 /* Expressions inside the LHS are not ordered wrt. the sequence points
3314 in the RHS. Example:
3315 *a = (a++, 2)
3316 Despite the fact that the modification of "a" is in the before_sp
3317 list (tmp_before), it conflicts with the use of "a" in the LHS.
3318 We can handle this by adding the contents of tmp_list3
3319 to those of tmp_before, and redoing the collision warnings for that
3320 list. */
3321 add_tlist (&tmp_before, tmp_list3, x, 1);
3322 warn_for_collisions (tmp_before);
3323 /* Exclude the LHS itself here; we first have to merge it into the
3324 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3325 didn't exclude the LHS, we'd get it twice, once as a read and once
3326 as a write. */
3327 add_tlist (pno_sp, tmp_list3, x, 0);
3328 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3330 merge_tlist (pbefore_sp, tmp_before, 0);
3331 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3332 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3333 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3334 return;
3336 case CALL_EXPR:
3337 /* We need to warn about conflicts among arguments and conflicts between
3338 args and the function address. Side effects of the function address,
3339 however, are not ordered by the sequence point of the call. */
3341 call_expr_arg_iterator iter;
3342 tree arg;
3343 tmp_before = tmp_nosp = 0;
3344 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3345 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3347 tmp_list2 = tmp_list3 = 0;
3348 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3349 merge_tlist (&tmp_list3, tmp_list2, 0);
3350 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3352 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3353 warn_for_collisions (tmp_before);
3354 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3355 return;
3358 case TREE_LIST:
3359 /* Scan all the list, e.g. indices of multi dimensional array. */
3360 while (x)
3362 tmp_before = tmp_nosp = 0;
3363 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3364 merge_tlist (&tmp_nosp, tmp_before, 0);
3365 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3366 x = TREE_CHAIN (x);
3368 return;
3370 case SAVE_EXPR:
3372 struct tlist_cache *t;
3373 for (t = save_expr_cache; t; t = t->next)
3374 if (candidate_equal_p (t->expr, x))
3375 break;
3377 if (!t)
3379 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3380 t->next = save_expr_cache;
3381 t->expr = x;
3382 save_expr_cache = t;
3384 tmp_before = tmp_nosp = 0;
3385 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3386 warn_for_collisions (tmp_nosp);
3388 tmp_list3 = 0;
3389 merge_tlist (&tmp_list3, tmp_nosp, 0);
3390 t->cache_before_sp = tmp_before;
3391 t->cache_after_sp = tmp_list3;
3393 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3394 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3395 return;
3398 case ADDR_EXPR:
3399 x = TREE_OPERAND (x, 0);
3400 if (DECL_P (x))
3401 return;
3402 writer = 0;
3403 goto restart;
3405 default:
3406 /* For other expressions, simply recurse on their operands.
3407 Manual tail recursion for unary expressions.
3408 Other non-expressions need not be processed. */
3409 if (cl == tcc_unary)
3411 x = TREE_OPERAND (x, 0);
3412 writer = 0;
3413 goto restart;
3415 else if (IS_EXPR_CODE_CLASS (cl))
3417 int lp;
3418 int max = TREE_OPERAND_LENGTH (x);
3419 for (lp = 0; lp < max; lp++)
3421 tmp_before = tmp_nosp = 0;
3422 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3423 merge_tlist (&tmp_nosp, tmp_before, 0);
3424 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3427 return;
3431 /* Try to warn for undefined behavior in EXPR due to missing sequence
3432 points. */
3434 DEBUG_FUNCTION void
3435 verify_sequence_points (tree expr)
3437 struct tlist *before_sp = 0, *after_sp = 0;
3439 warned_ids = 0;
3440 save_expr_cache = 0;
3441 if (tlist_firstobj == 0)
3443 gcc_obstack_init (&tlist_obstack);
3444 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3447 verify_tree (expr, &before_sp, &after_sp, 0);
3448 warn_for_collisions (after_sp);
3449 obstack_free (&tlist_obstack, tlist_firstobj);
3452 /* Validate the expression after `case' and apply default promotions. */
3454 static tree
3455 check_case_value (location_t loc, tree value)
3457 if (value == NULL_TREE)
3458 return value;
3460 if (TREE_CODE (value) == INTEGER_CST)
3461 /* Promote char or short to int. */
3462 value = perform_integral_promotions (value);
3463 else if (value != error_mark_node)
3465 error_at (loc, "case label does not reduce to an integer constant");
3466 value = error_mark_node;
3469 constant_expression_warning (value);
3471 return value;
3474 /* See if the case values LOW and HIGH are in the range of the original
3475 type (i.e. before the default conversion to int) of the switch testing
3476 expression.
3477 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3478 the type before promoting it. CASE_LOW_P is a pointer to the lower
3479 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3480 if the case is not a case range.
3481 The caller has to make sure that we are not called with NULL for
3482 CASE_LOW_P (i.e. the default case).
3483 Returns true if the case label is in range of ORIG_TYPE (saturated or
3484 untouched) or false if the label is out of range. */
3486 static bool
3487 check_case_bounds (location_t loc, tree type, tree orig_type,
3488 tree *case_low_p, tree *case_high_p)
3490 tree min_value, max_value;
3491 tree case_low = *case_low_p;
3492 tree case_high = case_high_p ? *case_high_p : case_low;
3494 /* If there was a problem with the original type, do nothing. */
3495 if (orig_type == error_mark_node)
3496 return true;
3498 min_value = TYPE_MIN_VALUE (orig_type);
3499 max_value = TYPE_MAX_VALUE (orig_type);
3501 /* Case label is less than minimum for type. */
3502 if (tree_int_cst_compare (case_low, min_value) < 0
3503 && tree_int_cst_compare (case_high, min_value) < 0)
3505 warning_at (loc, 0, "case label value is less than minimum value "
3506 "for type");
3507 return false;
3510 /* Case value is greater than maximum for type. */
3511 if (tree_int_cst_compare (case_low, max_value) > 0
3512 && tree_int_cst_compare (case_high, max_value) > 0)
3514 warning_at (loc, 0, "case label value exceeds maximum value for type");
3515 return false;
3518 /* Saturate lower case label value to minimum. */
3519 if (tree_int_cst_compare (case_high, min_value) >= 0
3520 && tree_int_cst_compare (case_low, min_value) < 0)
3522 warning_at (loc, 0, "lower value in case label range"
3523 " less than minimum value for type");
3524 case_low = min_value;
3527 /* Saturate upper case label value to maximum. */
3528 if (tree_int_cst_compare (case_low, max_value) <= 0
3529 && tree_int_cst_compare (case_high, max_value) > 0)
3531 warning_at (loc, 0, "upper value in case label range"
3532 " exceeds maximum value for type");
3533 case_high = max_value;
3536 if (*case_low_p != case_low)
3537 *case_low_p = convert (type, case_low);
3538 if (case_high_p && *case_high_p != case_high)
3539 *case_high_p = convert (type, case_high);
3541 return true;
3544 /* Return an integer type with BITS bits of precision,
3545 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3547 tree
3548 c_common_type_for_size (unsigned int bits, int unsignedp)
3550 int i;
3552 if (bits == TYPE_PRECISION (integer_type_node))
3553 return unsignedp ? unsigned_type_node : integer_type_node;
3555 if (bits == TYPE_PRECISION (signed_char_type_node))
3556 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3558 if (bits == TYPE_PRECISION (short_integer_type_node))
3559 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3561 if (bits == TYPE_PRECISION (long_integer_type_node))
3562 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3564 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3565 return (unsignedp ? long_long_unsigned_type_node
3566 : long_long_integer_type_node);
3568 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3569 if (int_n_enabled_p[i]
3570 && bits == int_n_data[i].bitsize)
3571 return (unsignedp ? int_n_trees[i].unsigned_type
3572 : int_n_trees[i].signed_type);
3574 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3575 return (unsignedp ? widest_unsigned_literal_type_node
3576 : widest_integer_literal_type_node);
3578 if (bits <= TYPE_PRECISION (intQI_type_node))
3579 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3581 if (bits <= TYPE_PRECISION (intHI_type_node))
3582 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3584 if (bits <= TYPE_PRECISION (intSI_type_node))
3585 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3587 if (bits <= TYPE_PRECISION (intDI_type_node))
3588 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3590 return 0;
3593 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3594 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3595 and saturating if SATP is nonzero, otherwise not saturating. */
3597 tree
3598 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3599 int unsignedp, int satp)
3601 machine_mode mode;
3602 if (ibit == 0)
3603 mode = unsignedp ? UQQmode : QQmode;
3604 else
3605 mode = unsignedp ? UHAmode : HAmode;
3607 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3608 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3609 break;
3611 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3613 sorry ("GCC cannot support operators with integer types and "
3614 "fixed-point types that have too many integral and "
3615 "fractional bits together");
3616 return 0;
3619 return c_common_type_for_mode (mode, satp);
3622 /* Used for communication between c_common_type_for_mode and
3623 c_register_builtin_type. */
3624 tree registered_builtin_types;
3626 /* Return a data type that has machine mode MODE.
3627 If the mode is an integer,
3628 then UNSIGNEDP selects between signed and unsigned types.
3629 If the mode is a fixed-point mode,
3630 then UNSIGNEDP selects between saturating and nonsaturating types. */
3632 tree
3633 c_common_type_for_mode (machine_mode mode, int unsignedp)
3635 tree t;
3636 int i;
3638 if (mode == TYPE_MODE (integer_type_node))
3639 return unsignedp ? unsigned_type_node : integer_type_node;
3641 if (mode == TYPE_MODE (signed_char_type_node))
3642 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3644 if (mode == TYPE_MODE (short_integer_type_node))
3645 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3647 if (mode == TYPE_MODE (long_integer_type_node))
3648 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3650 if (mode == TYPE_MODE (long_long_integer_type_node))
3651 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3653 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3654 if (int_n_enabled_p[i]
3655 && mode == int_n_data[i].m)
3656 return (unsignedp ? int_n_trees[i].unsigned_type
3657 : int_n_trees[i].signed_type);
3659 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3660 return unsignedp ? widest_unsigned_literal_type_node
3661 : widest_integer_literal_type_node;
3663 if (mode == QImode)
3664 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3666 if (mode == HImode)
3667 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3669 if (mode == SImode)
3670 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3672 if (mode == DImode)
3673 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3675 #if HOST_BITS_PER_WIDE_INT >= 64
3676 if (mode == TYPE_MODE (intTI_type_node))
3677 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3678 #endif
3680 if (mode == TYPE_MODE (float_type_node))
3681 return float_type_node;
3683 if (mode == TYPE_MODE (double_type_node))
3684 return double_type_node;
3686 if (mode == TYPE_MODE (long_double_type_node))
3687 return long_double_type_node;
3689 if (mode == TYPE_MODE (void_type_node))
3690 return void_type_node;
3692 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3693 return (unsignedp
3694 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3695 : make_signed_type (GET_MODE_PRECISION (mode)));
3697 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3698 return (unsignedp
3699 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3700 : make_signed_type (GET_MODE_PRECISION (mode)));
3702 if (COMPLEX_MODE_P (mode))
3704 machine_mode inner_mode;
3705 tree inner_type;
3707 if (mode == TYPE_MODE (complex_float_type_node))
3708 return complex_float_type_node;
3709 if (mode == TYPE_MODE (complex_double_type_node))
3710 return complex_double_type_node;
3711 if (mode == TYPE_MODE (complex_long_double_type_node))
3712 return complex_long_double_type_node;
3714 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3715 return complex_integer_type_node;
3717 inner_mode = GET_MODE_INNER (mode);
3718 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3719 if (inner_type != NULL_TREE)
3720 return build_complex_type (inner_type);
3722 else if (VECTOR_MODE_P (mode))
3724 machine_mode inner_mode = GET_MODE_INNER (mode);
3725 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3726 if (inner_type != NULL_TREE)
3727 return build_vector_type_for_mode (inner_type, mode);
3730 if (mode == TYPE_MODE (dfloat32_type_node))
3731 return dfloat32_type_node;
3732 if (mode == TYPE_MODE (dfloat64_type_node))
3733 return dfloat64_type_node;
3734 if (mode == TYPE_MODE (dfloat128_type_node))
3735 return dfloat128_type_node;
3737 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3739 if (mode == TYPE_MODE (short_fract_type_node))
3740 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3741 if (mode == TYPE_MODE (fract_type_node))
3742 return unsignedp ? sat_fract_type_node : fract_type_node;
3743 if (mode == TYPE_MODE (long_fract_type_node))
3744 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3745 if (mode == TYPE_MODE (long_long_fract_type_node))
3746 return unsignedp ? sat_long_long_fract_type_node
3747 : long_long_fract_type_node;
3749 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3750 return unsignedp ? sat_unsigned_short_fract_type_node
3751 : unsigned_short_fract_type_node;
3752 if (mode == TYPE_MODE (unsigned_fract_type_node))
3753 return unsignedp ? sat_unsigned_fract_type_node
3754 : unsigned_fract_type_node;
3755 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3756 return unsignedp ? sat_unsigned_long_fract_type_node
3757 : unsigned_long_fract_type_node;
3758 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3759 return unsignedp ? sat_unsigned_long_long_fract_type_node
3760 : unsigned_long_long_fract_type_node;
3762 if (mode == TYPE_MODE (short_accum_type_node))
3763 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3764 if (mode == TYPE_MODE (accum_type_node))
3765 return unsignedp ? sat_accum_type_node : accum_type_node;
3766 if (mode == TYPE_MODE (long_accum_type_node))
3767 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3768 if (mode == TYPE_MODE (long_long_accum_type_node))
3769 return unsignedp ? sat_long_long_accum_type_node
3770 : long_long_accum_type_node;
3772 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3773 return unsignedp ? sat_unsigned_short_accum_type_node
3774 : unsigned_short_accum_type_node;
3775 if (mode == TYPE_MODE (unsigned_accum_type_node))
3776 return unsignedp ? sat_unsigned_accum_type_node
3777 : unsigned_accum_type_node;
3778 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3779 return unsignedp ? sat_unsigned_long_accum_type_node
3780 : unsigned_long_accum_type_node;
3781 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3782 return unsignedp ? sat_unsigned_long_long_accum_type_node
3783 : unsigned_long_long_accum_type_node;
3785 if (mode == QQmode)
3786 return unsignedp ? sat_qq_type_node : qq_type_node;
3787 if (mode == HQmode)
3788 return unsignedp ? sat_hq_type_node : hq_type_node;
3789 if (mode == SQmode)
3790 return unsignedp ? sat_sq_type_node : sq_type_node;
3791 if (mode == DQmode)
3792 return unsignedp ? sat_dq_type_node : dq_type_node;
3793 if (mode == TQmode)
3794 return unsignedp ? sat_tq_type_node : tq_type_node;
3796 if (mode == UQQmode)
3797 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3798 if (mode == UHQmode)
3799 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3800 if (mode == USQmode)
3801 return unsignedp ? sat_usq_type_node : usq_type_node;
3802 if (mode == UDQmode)
3803 return unsignedp ? sat_udq_type_node : udq_type_node;
3804 if (mode == UTQmode)
3805 return unsignedp ? sat_utq_type_node : utq_type_node;
3807 if (mode == HAmode)
3808 return unsignedp ? sat_ha_type_node : ha_type_node;
3809 if (mode == SAmode)
3810 return unsignedp ? sat_sa_type_node : sa_type_node;
3811 if (mode == DAmode)
3812 return unsignedp ? sat_da_type_node : da_type_node;
3813 if (mode == TAmode)
3814 return unsignedp ? sat_ta_type_node : ta_type_node;
3816 if (mode == UHAmode)
3817 return unsignedp ? sat_uha_type_node : uha_type_node;
3818 if (mode == USAmode)
3819 return unsignedp ? sat_usa_type_node : usa_type_node;
3820 if (mode == UDAmode)
3821 return unsignedp ? sat_uda_type_node : uda_type_node;
3822 if (mode == UTAmode)
3823 return unsignedp ? sat_uta_type_node : uta_type_node;
3826 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3827 if (TYPE_MODE (TREE_VALUE (t)) == mode
3828 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3829 return TREE_VALUE (t);
3831 return 0;
3834 tree
3835 c_common_unsigned_type (tree type)
3837 return c_common_signed_or_unsigned_type (1, type);
3840 /* Return a signed type the same as TYPE in other respects. */
3842 tree
3843 c_common_signed_type (tree type)
3845 return c_common_signed_or_unsigned_type (0, type);
3848 /* Return a type the same as TYPE except unsigned or
3849 signed according to UNSIGNEDP. */
3851 tree
3852 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3854 tree type1;
3855 int i;
3857 /* This block of code emulates the behavior of the old
3858 c_common_unsigned_type. In particular, it returns
3859 long_unsigned_type_node if passed a long, even when a int would
3860 have the same size. This is necessary for warnings to work
3861 correctly in archs where sizeof(int) == sizeof(long) */
3863 type1 = TYPE_MAIN_VARIANT (type);
3864 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3865 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3866 if (type1 == integer_type_node || type1 == unsigned_type_node)
3867 return unsignedp ? unsigned_type_node : integer_type_node;
3868 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3869 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3870 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3871 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3872 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3873 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3875 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3876 if (int_n_enabled_p[i]
3877 && (type1 == int_n_trees[i].unsigned_type
3878 || type1 == int_n_trees[i].signed_type))
3879 return (unsignedp ? int_n_trees[i].unsigned_type
3880 : int_n_trees[i].signed_type);
3882 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3883 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3884 #if HOST_BITS_PER_WIDE_INT >= 64
3885 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3886 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3887 #endif
3888 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3889 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3890 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3891 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3892 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3893 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3894 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3895 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3897 #define C_COMMON_FIXED_TYPES(NAME) \
3898 if (type1 == short_ ## NAME ## _type_node \
3899 || type1 == unsigned_short_ ## NAME ## _type_node) \
3900 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3901 : short_ ## NAME ## _type_node; \
3902 if (type1 == NAME ## _type_node \
3903 || type1 == unsigned_ ## NAME ## _type_node) \
3904 return unsignedp ? unsigned_ ## NAME ## _type_node \
3905 : NAME ## _type_node; \
3906 if (type1 == long_ ## NAME ## _type_node \
3907 || type1 == unsigned_long_ ## NAME ## _type_node) \
3908 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3909 : long_ ## NAME ## _type_node; \
3910 if (type1 == long_long_ ## NAME ## _type_node \
3911 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3912 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3913 : long_long_ ## NAME ## _type_node;
3915 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3916 if (type1 == NAME ## _type_node \
3917 || type1 == u ## NAME ## _type_node) \
3918 return unsignedp ? u ## NAME ## _type_node \
3919 : NAME ## _type_node;
3921 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3922 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3923 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3924 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3925 : sat_ ## short_ ## NAME ## _type_node; \
3926 if (type1 == sat_ ## NAME ## _type_node \
3927 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3928 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3929 : sat_ ## NAME ## _type_node; \
3930 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3931 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3932 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3933 : sat_ ## long_ ## NAME ## _type_node; \
3934 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3935 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3936 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3937 : sat_ ## long_long_ ## NAME ## _type_node;
3939 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3940 if (type1 == sat_ ## NAME ## _type_node \
3941 || type1 == sat_ ## u ## NAME ## _type_node) \
3942 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3943 : sat_ ## NAME ## _type_node;
3945 C_COMMON_FIXED_TYPES (fract);
3946 C_COMMON_FIXED_TYPES_SAT (fract);
3947 C_COMMON_FIXED_TYPES (accum);
3948 C_COMMON_FIXED_TYPES_SAT (accum);
3950 C_COMMON_FIXED_MODE_TYPES (qq);
3951 C_COMMON_FIXED_MODE_TYPES (hq);
3952 C_COMMON_FIXED_MODE_TYPES (sq);
3953 C_COMMON_FIXED_MODE_TYPES (dq);
3954 C_COMMON_FIXED_MODE_TYPES (tq);
3955 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3956 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3957 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3958 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3959 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3960 C_COMMON_FIXED_MODE_TYPES (ha);
3961 C_COMMON_FIXED_MODE_TYPES (sa);
3962 C_COMMON_FIXED_MODE_TYPES (da);
3963 C_COMMON_FIXED_MODE_TYPES (ta);
3964 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3965 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3966 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3967 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3969 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3970 the precision; they have precision set to match their range, but
3971 may use a wider mode to match an ABI. If we change modes, we may
3972 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3973 the precision as well, so as to yield correct results for
3974 bit-field types. C++ does not have these separate bit-field
3975 types, and producing a signed or unsigned variant of an
3976 ENUMERAL_TYPE may cause other problems as well. */
3978 if (!INTEGRAL_TYPE_P (type)
3979 || TYPE_UNSIGNED (type) == unsignedp)
3980 return type;
3982 #define TYPE_OK(node) \
3983 (TYPE_MODE (type) == TYPE_MODE (node) \
3984 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3985 if (TYPE_OK (signed_char_type_node))
3986 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3987 if (TYPE_OK (integer_type_node))
3988 return unsignedp ? unsigned_type_node : integer_type_node;
3989 if (TYPE_OK (short_integer_type_node))
3990 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3991 if (TYPE_OK (long_integer_type_node))
3992 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3993 if (TYPE_OK (long_long_integer_type_node))
3994 return (unsignedp ? long_long_unsigned_type_node
3995 : long_long_integer_type_node);
3997 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3998 if (int_n_enabled_p[i]
3999 && TYPE_MODE (type) == int_n_data[i].m
4000 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4001 return (unsignedp ? int_n_trees[i].unsigned_type
4002 : int_n_trees[i].signed_type);
4004 if (TYPE_OK (widest_integer_literal_type_node))
4005 return (unsignedp ? widest_unsigned_literal_type_node
4006 : widest_integer_literal_type_node);
4008 #if HOST_BITS_PER_WIDE_INT >= 64
4009 if (TYPE_OK (intTI_type_node))
4010 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4011 #endif
4012 if (TYPE_OK (intDI_type_node))
4013 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4014 if (TYPE_OK (intSI_type_node))
4015 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4016 if (TYPE_OK (intHI_type_node))
4017 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4018 if (TYPE_OK (intQI_type_node))
4019 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4020 #undef TYPE_OK
4022 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4025 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4027 tree
4028 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4030 int i;
4032 /* Extended integer types of the same width as a standard type have
4033 lesser rank, so those of the same width as int promote to int or
4034 unsigned int and are valid for printf formats expecting int or
4035 unsigned int. To avoid such special cases, avoid creating
4036 extended integer types for bit-fields if a standard integer type
4037 is available. */
4038 if (width == TYPE_PRECISION (integer_type_node))
4039 return unsignedp ? unsigned_type_node : integer_type_node;
4040 if (width == TYPE_PRECISION (signed_char_type_node))
4041 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4042 if (width == TYPE_PRECISION (short_integer_type_node))
4043 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4044 if (width == TYPE_PRECISION (long_integer_type_node))
4045 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4046 if (width == TYPE_PRECISION (long_long_integer_type_node))
4047 return (unsignedp ? long_long_unsigned_type_node
4048 : long_long_integer_type_node);
4049 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4050 if (int_n_enabled_p[i]
4051 && width == int_n_data[i].bitsize)
4052 return (unsignedp ? int_n_trees[i].unsigned_type
4053 : int_n_trees[i].signed_type);
4054 return build_nonstandard_integer_type (width, unsignedp);
4057 /* The C version of the register_builtin_type langhook. */
4059 void
4060 c_register_builtin_type (tree type, const char* name)
4062 tree decl;
4064 decl = build_decl (UNKNOWN_LOCATION,
4065 TYPE_DECL, get_identifier (name), type);
4066 DECL_ARTIFICIAL (decl) = 1;
4067 if (!TYPE_NAME (type))
4068 TYPE_NAME (type) = decl;
4069 pushdecl (decl);
4071 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4074 /* Print an error message for invalid operands to arith operation
4075 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4076 LOCATION is the location of the message. */
4078 void
4079 binary_op_error (location_t location, enum tree_code code,
4080 tree type0, tree type1)
4082 const char *opname;
4084 switch (code)
4086 case PLUS_EXPR:
4087 opname = "+"; break;
4088 case MINUS_EXPR:
4089 opname = "-"; break;
4090 case MULT_EXPR:
4091 opname = "*"; break;
4092 case MAX_EXPR:
4093 opname = "max"; break;
4094 case MIN_EXPR:
4095 opname = "min"; break;
4096 case EQ_EXPR:
4097 opname = "=="; break;
4098 case NE_EXPR:
4099 opname = "!="; break;
4100 case LE_EXPR:
4101 opname = "<="; break;
4102 case GE_EXPR:
4103 opname = ">="; break;
4104 case LT_EXPR:
4105 opname = "<"; break;
4106 case GT_EXPR:
4107 opname = ">"; break;
4108 case LSHIFT_EXPR:
4109 opname = "<<"; break;
4110 case RSHIFT_EXPR:
4111 opname = ">>"; break;
4112 case TRUNC_MOD_EXPR:
4113 case FLOOR_MOD_EXPR:
4114 opname = "%"; break;
4115 case TRUNC_DIV_EXPR:
4116 case FLOOR_DIV_EXPR:
4117 opname = "/"; break;
4118 case BIT_AND_EXPR:
4119 opname = "&"; break;
4120 case BIT_IOR_EXPR:
4121 opname = "|"; break;
4122 case TRUTH_ANDIF_EXPR:
4123 opname = "&&"; break;
4124 case TRUTH_ORIF_EXPR:
4125 opname = "||"; break;
4126 case BIT_XOR_EXPR:
4127 opname = "^"; break;
4128 default:
4129 gcc_unreachable ();
4131 error_at (location,
4132 "invalid operands to binary %s (have %qT and %qT)", opname,
4133 type0, type1);
4136 /* Given an expression as a tree, return its original type. Do this
4137 by stripping any conversion that preserves the sign and precision. */
4138 static tree
4139 expr_original_type (tree expr)
4141 STRIP_SIGN_NOPS (expr);
4142 return TREE_TYPE (expr);
4145 /* Subroutine of build_binary_op, used for comparison operations.
4146 See if the operands have both been converted from subword integer types
4147 and, if so, perhaps change them both back to their original type.
4148 This function is also responsible for converting the two operands
4149 to the proper common type for comparison.
4151 The arguments of this function are all pointers to local variables
4152 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4153 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4155 LOC is the location of the comparison.
4157 If this function returns nonzero, it means that the comparison has
4158 a constant value. What this function returns is an expression for
4159 that value. */
4161 tree
4162 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4163 tree *restype_ptr, enum tree_code *rescode_ptr)
4165 tree type;
4166 tree op0 = *op0_ptr;
4167 tree op1 = *op1_ptr;
4168 int unsignedp0, unsignedp1;
4169 int real1, real2;
4170 tree primop0, primop1;
4171 enum tree_code code = *rescode_ptr;
4173 /* Throw away any conversions to wider types
4174 already present in the operands. */
4176 primop0 = c_common_get_narrower (op0, &unsignedp0);
4177 primop1 = c_common_get_narrower (op1, &unsignedp1);
4179 /* If primopN is first sign-extended from primopN's precision to opN's
4180 precision, then zero-extended from opN's precision to
4181 *restype_ptr precision, shortenings might be invalid. */
4182 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4183 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4184 && !unsignedp0
4185 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4186 primop0 = op0;
4187 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4188 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4189 && !unsignedp1
4190 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4191 primop1 = op1;
4193 /* Handle the case that OP0 does not *contain* a conversion
4194 but it *requires* conversion to FINAL_TYPE. */
4196 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4197 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4198 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4199 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4201 /* If one of the operands must be floated, we cannot optimize. */
4202 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4203 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4205 /* If first arg is constant, swap the args (changing operation
4206 so value is preserved), for canonicalization. Don't do this if
4207 the second arg is 0. */
4209 if (TREE_CONSTANT (primop0)
4210 && !integer_zerop (primop1) && !real_zerop (primop1)
4211 && !fixed_zerop (primop1))
4213 tree tem = primop0;
4214 int temi = unsignedp0;
4215 primop0 = primop1;
4216 primop1 = tem;
4217 tem = op0;
4218 op0 = op1;
4219 op1 = tem;
4220 *op0_ptr = op0;
4221 *op1_ptr = op1;
4222 unsignedp0 = unsignedp1;
4223 unsignedp1 = temi;
4224 temi = real1;
4225 real1 = real2;
4226 real2 = temi;
4228 switch (code)
4230 case LT_EXPR:
4231 code = GT_EXPR;
4232 break;
4233 case GT_EXPR:
4234 code = LT_EXPR;
4235 break;
4236 case LE_EXPR:
4237 code = GE_EXPR;
4238 break;
4239 case GE_EXPR:
4240 code = LE_EXPR;
4241 break;
4242 default:
4243 break;
4245 *rescode_ptr = code;
4248 /* If comparing an integer against a constant more bits wide,
4249 maybe we can deduce a value of 1 or 0 independent of the data.
4250 Or else truncate the constant now
4251 rather than extend the variable at run time.
4253 This is only interesting if the constant is the wider arg.
4254 Also, it is not safe if the constant is unsigned and the
4255 variable arg is signed, since in this case the variable
4256 would be sign-extended and then regarded as unsigned.
4257 Our technique fails in this case because the lowest/highest
4258 possible unsigned results don't follow naturally from the
4259 lowest/highest possible values of the variable operand.
4260 For just EQ_EXPR and NE_EXPR there is another technique that
4261 could be used: see if the constant can be faithfully represented
4262 in the other operand's type, by truncating it and reextending it
4263 and see if that preserves the constant's value. */
4265 if (!real1 && !real2
4266 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4267 && TREE_CODE (primop1) == INTEGER_CST
4268 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4270 int min_gt, max_gt, min_lt, max_lt;
4271 tree maxval, minval;
4272 /* 1 if comparison is nominally unsigned. */
4273 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4274 tree val;
4276 type = c_common_signed_or_unsigned_type (unsignedp0,
4277 TREE_TYPE (primop0));
4279 maxval = TYPE_MAX_VALUE (type);
4280 minval = TYPE_MIN_VALUE (type);
4282 if (unsignedp && !unsignedp0)
4283 *restype_ptr = c_common_signed_type (*restype_ptr);
4285 if (TREE_TYPE (primop1) != *restype_ptr)
4287 /* Convert primop1 to target type, but do not introduce
4288 additional overflow. We know primop1 is an int_cst. */
4289 primop1 = force_fit_type (*restype_ptr,
4290 wide_int::from
4291 (primop1,
4292 TYPE_PRECISION (*restype_ptr),
4293 TYPE_SIGN (TREE_TYPE (primop1))),
4294 0, TREE_OVERFLOW (primop1));
4296 if (type != *restype_ptr)
4298 minval = convert (*restype_ptr, minval);
4299 maxval = convert (*restype_ptr, maxval);
4302 min_gt = tree_int_cst_lt (primop1, minval);
4303 max_gt = tree_int_cst_lt (primop1, maxval);
4304 min_lt = tree_int_cst_lt (minval, primop1);
4305 max_lt = tree_int_cst_lt (maxval, primop1);
4307 val = 0;
4308 /* This used to be a switch, but Genix compiler can't handle that. */
4309 if (code == NE_EXPR)
4311 if (max_lt || min_gt)
4312 val = truthvalue_true_node;
4314 else if (code == EQ_EXPR)
4316 if (max_lt || min_gt)
4317 val = truthvalue_false_node;
4319 else if (code == LT_EXPR)
4321 if (max_lt)
4322 val = truthvalue_true_node;
4323 if (!min_lt)
4324 val = truthvalue_false_node;
4326 else if (code == GT_EXPR)
4328 if (min_gt)
4329 val = truthvalue_true_node;
4330 if (!max_gt)
4331 val = truthvalue_false_node;
4333 else if (code == LE_EXPR)
4335 if (!max_gt)
4336 val = truthvalue_true_node;
4337 if (min_gt)
4338 val = truthvalue_false_node;
4340 else if (code == GE_EXPR)
4342 if (!min_lt)
4343 val = truthvalue_true_node;
4344 if (max_lt)
4345 val = truthvalue_false_node;
4348 /* If primop0 was sign-extended and unsigned comparison specd,
4349 we did a signed comparison above using the signed type bounds.
4350 But the comparison we output must be unsigned.
4352 Also, for inequalities, VAL is no good; but if the signed
4353 comparison had *any* fixed result, it follows that the
4354 unsigned comparison just tests the sign in reverse
4355 (positive values are LE, negative ones GE).
4356 So we can generate an unsigned comparison
4357 against an extreme value of the signed type. */
4359 if (unsignedp && !unsignedp0)
4361 if (val != 0)
4362 switch (code)
4364 case LT_EXPR:
4365 case GE_EXPR:
4366 primop1 = TYPE_MIN_VALUE (type);
4367 val = 0;
4368 break;
4370 case LE_EXPR:
4371 case GT_EXPR:
4372 primop1 = TYPE_MAX_VALUE (type);
4373 val = 0;
4374 break;
4376 default:
4377 break;
4379 type = c_common_unsigned_type (type);
4382 if (TREE_CODE (primop0) != INTEGER_CST)
4384 if (val == truthvalue_false_node)
4385 warning_at (loc, OPT_Wtype_limits,
4386 "comparison is always false due to limited range of data type");
4387 if (val == truthvalue_true_node)
4388 warning_at (loc, OPT_Wtype_limits,
4389 "comparison is always true due to limited range of data type");
4392 if (val != 0)
4394 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4395 if (TREE_SIDE_EFFECTS (primop0))
4396 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4397 return val;
4400 /* Value is not predetermined, but do the comparison
4401 in the type of the operand that is not constant.
4402 TYPE is already properly set. */
4405 /* If either arg is decimal float and the other is float, find the
4406 proper common type to use for comparison. */
4407 else if (real1 && real2
4408 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4409 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4410 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4412 /* If either arg is decimal float and the other is float, fail. */
4413 else if (real1 && real2
4414 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4415 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4416 return 0;
4418 else if (real1 && real2
4419 && (TYPE_PRECISION (TREE_TYPE (primop0))
4420 == TYPE_PRECISION (TREE_TYPE (primop1))))
4421 type = TREE_TYPE (primop0);
4423 /* If args' natural types are both narrower than nominal type
4424 and both extend in the same manner, compare them
4425 in the type of the wider arg.
4426 Otherwise must actually extend both to the nominal
4427 common type lest different ways of extending
4428 alter the result.
4429 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4431 else if (unsignedp0 == unsignedp1 && real1 == real2
4432 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4433 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4435 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4436 type = c_common_signed_or_unsigned_type (unsignedp0
4437 || TYPE_UNSIGNED (*restype_ptr),
4438 type);
4439 /* Make sure shorter operand is extended the right way
4440 to match the longer operand. */
4441 primop0
4442 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4443 TREE_TYPE (primop0)),
4444 primop0);
4445 primop1
4446 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4447 TREE_TYPE (primop1)),
4448 primop1);
4450 else
4452 /* Here we must do the comparison on the nominal type
4453 using the args exactly as we received them. */
4454 type = *restype_ptr;
4455 primop0 = op0;
4456 primop1 = op1;
4458 if (!real1 && !real2 && integer_zerop (primop1)
4459 && TYPE_UNSIGNED (*restype_ptr))
4461 tree value = 0;
4462 /* All unsigned values are >= 0, so we warn. However,
4463 if OP0 is a constant that is >= 0, the signedness of
4464 the comparison isn't an issue, so suppress the
4465 warning. */
4466 bool warn =
4467 warn_type_limits && !in_system_header_at (loc)
4468 && !(TREE_CODE (primop0) == INTEGER_CST
4469 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4470 primop0)))
4471 /* Do not warn for enumeration types. */
4472 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4474 switch (code)
4476 case GE_EXPR:
4477 if (warn)
4478 warning_at (loc, OPT_Wtype_limits,
4479 "comparison of unsigned expression >= 0 is always true");
4480 value = truthvalue_true_node;
4481 break;
4483 case LT_EXPR:
4484 if (warn)
4485 warning_at (loc, OPT_Wtype_limits,
4486 "comparison of unsigned expression < 0 is always false");
4487 value = truthvalue_false_node;
4488 break;
4490 default:
4491 break;
4494 if (value != 0)
4496 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4497 if (TREE_SIDE_EFFECTS (primop0))
4498 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4499 primop0, value);
4500 return value;
4505 *op0_ptr = convert (type, primop0);
4506 *op1_ptr = convert (type, primop1);
4508 *restype_ptr = truthvalue_type_node;
4510 return 0;
4513 /* Return a tree for the sum or difference (RESULTCODE says which)
4514 of pointer PTROP and integer INTOP. */
4516 tree
4517 pointer_int_sum (location_t loc, enum tree_code resultcode,
4518 tree ptrop, tree intop, bool complain)
4520 tree size_exp, ret;
4522 /* The result is a pointer of the same type that is being added. */
4523 tree result_type = TREE_TYPE (ptrop);
4525 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4527 if (complain && warn_pointer_arith)
4528 pedwarn (loc, OPT_Wpointer_arith,
4529 "pointer of type %<void *%> used in arithmetic");
4530 else if (!complain)
4531 return error_mark_node;
4532 size_exp = integer_one_node;
4534 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4536 if (complain && warn_pointer_arith)
4537 pedwarn (loc, OPT_Wpointer_arith,
4538 "pointer to a function used in arithmetic");
4539 else if (!complain)
4540 return error_mark_node;
4541 size_exp = integer_one_node;
4543 else
4544 size_exp = size_in_bytes (TREE_TYPE (result_type));
4546 /* We are manipulating pointer values, so we don't need to warn
4547 about relying on undefined signed overflow. We disable the
4548 warning here because we use integer types so fold won't know that
4549 they are really pointers. */
4550 fold_defer_overflow_warnings ();
4552 /* If what we are about to multiply by the size of the elements
4553 contains a constant term, apply distributive law
4554 and multiply that constant term separately.
4555 This helps produce common subexpressions. */
4556 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4557 && !TREE_CONSTANT (intop)
4558 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4559 && TREE_CONSTANT (size_exp)
4560 /* If the constant comes from pointer subtraction,
4561 skip this optimization--it would cause an error. */
4562 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4563 /* If the constant is unsigned, and smaller than the pointer size,
4564 then we must skip this optimization. This is because it could cause
4565 an overflow error if the constant is negative but INTOP is not. */
4566 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4567 || (TYPE_PRECISION (TREE_TYPE (intop))
4568 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4570 enum tree_code subcode = resultcode;
4571 tree int_type = TREE_TYPE (intop);
4572 if (TREE_CODE (intop) == MINUS_EXPR)
4573 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4574 /* Convert both subexpression types to the type of intop,
4575 because weird cases involving pointer arithmetic
4576 can result in a sum or difference with different type args. */
4577 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4578 subcode, ptrop,
4579 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4580 intop = convert (int_type, TREE_OPERAND (intop, 0));
4583 /* Convert the integer argument to a type the same size as sizetype
4584 so the multiply won't overflow spuriously. */
4585 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4586 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4587 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4588 TYPE_UNSIGNED (sizetype)), intop);
4590 /* Replace the integer argument with a suitable product by the object size.
4591 Do this multiplication as signed, then convert to the appropriate type
4592 for the pointer operation and disregard an overflow that occurred only
4593 because of the sign-extension change in the latter conversion. */
4595 tree t = build_binary_op (loc,
4596 MULT_EXPR, intop,
4597 convert (TREE_TYPE (intop), size_exp), 1);
4598 intop = convert (sizetype, t);
4599 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4600 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4603 /* Create the sum or difference. */
4604 if (resultcode == MINUS_EXPR)
4605 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4607 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4609 fold_undefer_and_ignore_overflow_warnings ();
4611 return ret;
4614 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4615 and if NON_CONST is known not to be permitted in an evaluated part
4616 of a constant expression. */
4618 tree
4619 c_wrap_maybe_const (tree expr, bool non_const)
4621 bool nowarning = TREE_NO_WARNING (expr);
4622 location_t loc = EXPR_LOCATION (expr);
4624 /* This should never be called for C++. */
4625 if (c_dialect_cxx ())
4626 gcc_unreachable ();
4628 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4629 STRIP_TYPE_NOPS (expr);
4630 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4631 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4632 if (nowarning)
4633 TREE_NO_WARNING (expr) = 1;
4634 protected_set_expr_location (expr, loc);
4636 return expr;
4639 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4640 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4641 around the SAVE_EXPR if needed so that c_fully_fold does not need
4642 to look inside SAVE_EXPRs. */
4644 tree
4645 c_save_expr (tree expr)
4647 bool maybe_const = true;
4648 if (c_dialect_cxx ())
4649 return save_expr (expr);
4650 expr = c_fully_fold (expr, false, &maybe_const);
4651 expr = save_expr (expr);
4652 if (!maybe_const)
4653 expr = c_wrap_maybe_const (expr, true);
4654 return expr;
4657 /* Return whether EXPR is a declaration whose address can never be
4658 NULL. */
4660 bool
4661 decl_with_nonnull_addr_p (const_tree expr)
4663 return (DECL_P (expr)
4664 && (TREE_CODE (expr) == PARM_DECL
4665 || TREE_CODE (expr) == LABEL_DECL
4666 || !DECL_WEAK (expr)));
4669 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4670 or for an `if' or `while' statement or ?..: exp. It should already
4671 have been validated to be of suitable type; otherwise, a bad
4672 diagnostic may result.
4674 The EXPR is located at LOCATION.
4676 This preparation consists of taking the ordinary
4677 representation of an expression expr and producing a valid tree
4678 boolean expression describing whether expr is nonzero. We could
4679 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4680 but we optimize comparisons, &&, ||, and !.
4682 The resulting type should always be `truthvalue_type_node'. */
4684 tree
4685 c_common_truthvalue_conversion (location_t location, tree expr)
4687 switch (TREE_CODE (expr))
4689 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4690 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4691 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4692 case ORDERED_EXPR: case UNORDERED_EXPR:
4693 if (TREE_TYPE (expr) == truthvalue_type_node)
4694 return expr;
4695 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4696 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4697 goto ret;
4699 case TRUTH_ANDIF_EXPR:
4700 case TRUTH_ORIF_EXPR:
4701 case TRUTH_AND_EXPR:
4702 case TRUTH_OR_EXPR:
4703 case TRUTH_XOR_EXPR:
4704 if (TREE_TYPE (expr) == truthvalue_type_node)
4705 return expr;
4706 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4707 c_common_truthvalue_conversion (location,
4708 TREE_OPERAND (expr, 0)),
4709 c_common_truthvalue_conversion (location,
4710 TREE_OPERAND (expr, 1)));
4711 goto ret;
4713 case TRUTH_NOT_EXPR:
4714 if (TREE_TYPE (expr) == truthvalue_type_node)
4715 return expr;
4716 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4717 c_common_truthvalue_conversion (location,
4718 TREE_OPERAND (expr, 0)));
4719 goto ret;
4721 case ERROR_MARK:
4722 return expr;
4724 case INTEGER_CST:
4725 return integer_zerop (expr) ? truthvalue_false_node
4726 : truthvalue_true_node;
4728 case REAL_CST:
4729 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4730 ? truthvalue_true_node
4731 : truthvalue_false_node;
4733 case FIXED_CST:
4734 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4735 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4736 ? truthvalue_true_node
4737 : truthvalue_false_node;
4739 case FUNCTION_DECL:
4740 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4741 /* Fall through. */
4743 case ADDR_EXPR:
4745 tree inner = TREE_OPERAND (expr, 0);
4746 if (decl_with_nonnull_addr_p (inner))
4748 /* Common Ada/Pascal programmer's mistake. */
4749 warning_at (location,
4750 OPT_Waddress,
4751 "the address of %qD will always evaluate as %<true%>",
4752 inner);
4753 return truthvalue_true_node;
4755 break;
4758 case COMPLEX_EXPR:
4759 expr = build_binary_op (EXPR_LOCATION (expr),
4760 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4761 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4762 c_common_truthvalue_conversion (location,
4763 TREE_OPERAND (expr, 0)),
4764 c_common_truthvalue_conversion (location,
4765 TREE_OPERAND (expr, 1)),
4767 goto ret;
4769 case NEGATE_EXPR:
4770 case ABS_EXPR:
4771 case FLOAT_EXPR:
4772 case EXCESS_PRECISION_EXPR:
4773 /* These don't change whether an object is nonzero or zero. */
4774 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4776 case LROTATE_EXPR:
4777 case RROTATE_EXPR:
4778 /* These don't change whether an object is zero or nonzero, but
4779 we can't ignore them if their second arg has side-effects. */
4780 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4782 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4783 TREE_OPERAND (expr, 1),
4784 c_common_truthvalue_conversion
4785 (location, TREE_OPERAND (expr, 0)));
4786 goto ret;
4788 else
4789 return c_common_truthvalue_conversion (location,
4790 TREE_OPERAND (expr, 0));
4792 case COND_EXPR:
4793 /* Distribute the conversion into the arms of a COND_EXPR. */
4794 if (c_dialect_cxx ())
4796 tree op1 = TREE_OPERAND (expr, 1);
4797 tree op2 = TREE_OPERAND (expr, 2);
4798 /* In C++ one of the arms might have void type if it is throw. */
4799 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4800 op1 = c_common_truthvalue_conversion (location, op1);
4801 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4802 op2 = c_common_truthvalue_conversion (location, op2);
4803 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4804 TREE_OPERAND (expr, 0), op1, op2);
4805 goto ret;
4807 else
4809 /* Folding will happen later for C. */
4810 expr = build3 (COND_EXPR, truthvalue_type_node,
4811 TREE_OPERAND (expr, 0),
4812 c_common_truthvalue_conversion (location,
4813 TREE_OPERAND (expr, 1)),
4814 c_common_truthvalue_conversion (location,
4815 TREE_OPERAND (expr, 2)));
4816 goto ret;
4819 CASE_CONVERT:
4821 tree totype = TREE_TYPE (expr);
4822 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4824 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4825 since that affects how `default_conversion' will behave. */
4826 if (TREE_CODE (totype) == REFERENCE_TYPE
4827 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4828 break;
4829 /* Don't strip a conversion from C++0x scoped enum, since they
4830 don't implicitly convert to other types. */
4831 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4832 && ENUM_IS_SCOPED (fromtype))
4833 break;
4834 /* If this isn't narrowing the argument, we can ignore it. */
4835 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4836 return c_common_truthvalue_conversion (location,
4837 TREE_OPERAND (expr, 0));
4839 break;
4841 case MODIFY_EXPR:
4842 if (!TREE_NO_WARNING (expr)
4843 && warn_parentheses)
4845 warning (OPT_Wparentheses,
4846 "suggest parentheses around assignment used as truth value");
4847 TREE_NO_WARNING (expr) = 1;
4849 break;
4851 default:
4852 break;
4855 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4857 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4858 expr = (build_binary_op
4859 (EXPR_LOCATION (expr),
4860 (TREE_SIDE_EFFECTS (expr)
4861 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4862 c_common_truthvalue_conversion
4863 (location,
4864 build_unary_op (location, REALPART_EXPR, t, 0)),
4865 c_common_truthvalue_conversion
4866 (location,
4867 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4868 0));
4869 goto ret;
4872 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4874 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4875 FCONST0 (TYPE_MODE
4876 (TREE_TYPE (expr))));
4877 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4879 else
4880 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4882 ret:
4883 protected_set_expr_location (expr, location);
4884 return expr;
4887 static void def_builtin_1 (enum built_in_function fncode,
4888 const char *name,
4889 enum built_in_class fnclass,
4890 tree fntype, tree libtype,
4891 bool both_p, bool fallback_p, bool nonansi_p,
4892 tree fnattrs, bool implicit_p);
4895 /* Apply the TYPE_QUALS to the new DECL. */
4897 void
4898 c_apply_type_quals_to_decl (int type_quals, tree decl)
4900 tree type = TREE_TYPE (decl);
4902 if (type == error_mark_node)
4903 return;
4905 if ((type_quals & TYPE_QUAL_CONST)
4906 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4907 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4908 constructor can produce constant init, so rely on cp_finish_decl to
4909 clear TREE_READONLY if the variable has non-constant init. */
4910 TREE_READONLY (decl) = 1;
4911 if (type_quals & TYPE_QUAL_VOLATILE)
4913 TREE_SIDE_EFFECTS (decl) = 1;
4914 TREE_THIS_VOLATILE (decl) = 1;
4916 if (type_quals & TYPE_QUAL_RESTRICT)
4918 while (type && TREE_CODE (type) == ARRAY_TYPE)
4919 /* Allow 'restrict' on arrays of pointers.
4920 FIXME currently we just ignore it. */
4921 type = TREE_TYPE (type);
4922 if (!type
4923 || !POINTER_TYPE_P (type)
4924 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4925 error ("invalid use of %<restrict%>");
4929 struct c_type_hasher : ggc_hasher<tree>
4931 static hashval_t hash (tree);
4932 static bool equal (tree, tree);
4935 /* Hash function for the problem of multiple type definitions in
4936 different files. This must hash all types that will compare
4937 equal via comptypes to the same value. In practice it hashes
4938 on some of the simple stuff and leaves the details to comptypes. */
4940 hashval_t
4941 c_type_hasher::hash (tree t)
4943 int n_elements;
4944 int shift, size;
4945 tree t2;
4946 switch (TREE_CODE (t))
4948 /* For pointers, hash on pointee type plus some swizzling. */
4949 case POINTER_TYPE:
4950 return hash (TREE_TYPE (t)) ^ 0x3003003;
4951 /* Hash on number of elements and total size. */
4952 case ENUMERAL_TYPE:
4953 shift = 3;
4954 t2 = TYPE_VALUES (t);
4955 break;
4956 case RECORD_TYPE:
4957 shift = 0;
4958 t2 = TYPE_FIELDS (t);
4959 break;
4960 case QUAL_UNION_TYPE:
4961 shift = 1;
4962 t2 = TYPE_FIELDS (t);
4963 break;
4964 case UNION_TYPE:
4965 shift = 2;
4966 t2 = TYPE_FIELDS (t);
4967 break;
4968 default:
4969 gcc_unreachable ();
4971 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4972 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4973 n_elements = list_length (t2);
4974 /* We might have a VLA here. */
4975 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4976 size = 0;
4977 else
4978 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4979 return ((size << 24) | (n_elements << shift));
4982 bool
4983 c_type_hasher::equal (tree t1, tree t2)
4985 return lang_hooks.types_compatible_p (t1, t2);
4988 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4990 /* Return the typed-based alias set for T, which may be an expression
4991 or a type. Return -1 if we don't do anything special. */
4993 alias_set_type
4994 c_common_get_alias_set (tree t)
4996 tree u;
4998 /* For VLAs, use the alias set of the element type rather than the
4999 default of alias set 0 for types compared structurally. */
5000 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5002 if (TREE_CODE (t) == ARRAY_TYPE)
5003 return get_alias_set (TREE_TYPE (t));
5004 return -1;
5007 /* Permit type-punning when accessing a union, provided the access
5008 is directly through the union. For example, this code does not
5009 permit taking the address of a union member and then storing
5010 through it. Even the type-punning allowed here is a GCC
5011 extension, albeit a common and useful one; the C standard says
5012 that such accesses have implementation-defined behavior. */
5013 for (u = t;
5014 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5015 u = TREE_OPERAND (u, 0))
5016 if (TREE_CODE (u) == COMPONENT_REF
5017 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5018 return 0;
5020 /* That's all the expressions we handle specially. */
5021 if (!TYPE_P (t))
5022 return -1;
5024 /* The C standard guarantees that any object may be accessed via an
5025 lvalue that has character type. */
5026 if (t == char_type_node
5027 || t == signed_char_type_node
5028 || t == unsigned_char_type_node)
5029 return 0;
5031 /* The C standard specifically allows aliasing between signed and
5032 unsigned variants of the same type. We treat the signed
5033 variant as canonical. */
5034 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
5036 tree t1 = c_common_signed_type (t);
5038 /* t1 == t can happen for boolean nodes which are always unsigned. */
5039 if (t1 != t)
5040 return get_alias_set (t1);
5043 /* Handle the case of multiple type nodes referring to "the same" type,
5044 which occurs with IMA. These share an alias set. FIXME: Currently only
5045 C90 is handled. (In C99 type compatibility is not transitive, which
5046 complicates things mightily. The alias set splay trees can theoretically
5047 represent this, but insertion is tricky when you consider all the
5048 different orders things might arrive in.) */
5050 if (c_language != clk_c || flag_isoc99)
5051 return -1;
5053 /* Save time if there's only one input file. */
5054 if (num_in_fnames == 1)
5055 return -1;
5057 /* Pointers need special handling if they point to any type that
5058 needs special handling (below). */
5059 if (TREE_CODE (t) == POINTER_TYPE)
5061 tree t2;
5062 /* Find bottom type under any nested POINTERs. */
5063 for (t2 = TREE_TYPE (t);
5064 TREE_CODE (t2) == POINTER_TYPE;
5065 t2 = TREE_TYPE (t2))
5067 if (TREE_CODE (t2) != RECORD_TYPE
5068 && TREE_CODE (t2) != ENUMERAL_TYPE
5069 && TREE_CODE (t2) != QUAL_UNION_TYPE
5070 && TREE_CODE (t2) != UNION_TYPE)
5071 return -1;
5072 if (TYPE_SIZE (t2) == 0)
5073 return -1;
5075 /* These are the only cases that need special handling. */
5076 if (TREE_CODE (t) != RECORD_TYPE
5077 && TREE_CODE (t) != ENUMERAL_TYPE
5078 && TREE_CODE (t) != QUAL_UNION_TYPE
5079 && TREE_CODE (t) != UNION_TYPE
5080 && TREE_CODE (t) != POINTER_TYPE)
5081 return -1;
5082 /* Undefined? */
5083 if (TYPE_SIZE (t) == 0)
5084 return -1;
5086 /* Look up t in hash table. Only one of the compatible types within each
5087 alias set is recorded in the table. */
5088 if (!type_hash_table)
5089 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5090 tree *slot = type_hash_table->find_slot (t, INSERT);
5091 if (*slot != NULL)
5093 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5094 return TYPE_ALIAS_SET ((tree)*slot);
5096 else
5097 /* Our caller will assign and record (in t) a new alias set; all we need
5098 to do is remember t in the hash table. */
5099 *slot = t;
5101 return -1;
5104 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5105 the IS_SIZEOF parameter indicates which operator is being applied.
5106 The COMPLAIN flag controls whether we should diagnose possibly
5107 ill-formed constructs or not. LOC is the location of the SIZEOF or
5108 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5109 a type in any context should be returned, rather than the normal
5110 alignment for that type. */
5112 tree
5113 c_sizeof_or_alignof_type (location_t loc,
5114 tree type, bool is_sizeof, bool min_alignof,
5115 int complain)
5117 const char *op_name;
5118 tree value = NULL;
5119 enum tree_code type_code = TREE_CODE (type);
5121 op_name = is_sizeof ? "sizeof" : "__alignof__";
5123 if (type_code == FUNCTION_TYPE)
5125 if (is_sizeof)
5127 if (complain && warn_pointer_arith)
5128 pedwarn (loc, OPT_Wpointer_arith,
5129 "invalid application of %<sizeof%> to a function type");
5130 else if (!complain)
5131 return error_mark_node;
5132 value = size_one_node;
5134 else
5136 if (complain)
5138 if (c_dialect_cxx ())
5139 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5140 "%<alignof%> applied to a function type");
5141 else
5142 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5143 "%<_Alignof%> applied to a function type");
5145 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5148 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5150 if (type_code == VOID_TYPE
5151 && complain && warn_pointer_arith)
5152 pedwarn (loc, OPT_Wpointer_arith,
5153 "invalid application of %qs to a void type", op_name);
5154 else if (!complain)
5155 return error_mark_node;
5156 value = size_one_node;
5158 else if (!COMPLETE_TYPE_P (type)
5159 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5161 if (complain)
5162 error_at (loc, "invalid application of %qs to incomplete type %qT",
5163 op_name, type);
5164 return error_mark_node;
5166 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5167 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5169 if (complain)
5170 error_at (loc, "invalid application of %qs to array type %qT of "
5171 "incomplete element type", op_name, type);
5172 return error_mark_node;
5174 else
5176 if (is_sizeof)
5177 /* Convert in case a char is more than one unit. */
5178 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5179 size_int (TYPE_PRECISION (char_type_node)
5180 / BITS_PER_UNIT));
5181 else if (min_alignof)
5182 value = size_int (min_align_of_type (type));
5183 else
5184 value = size_int (TYPE_ALIGN_UNIT (type));
5187 /* VALUE will have the middle-end integer type sizetype.
5188 However, we should really return a value of type `size_t',
5189 which is just a typedef for an ordinary integer type. */
5190 value = fold_convert_loc (loc, size_type_node, value);
5192 return value;
5195 /* Implement the __alignof keyword: Return the minimum required
5196 alignment of EXPR, measured in bytes. For VAR_DECLs,
5197 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5198 from an "aligned" __attribute__ specification). LOC is the
5199 location of the ALIGNOF operator. */
5201 tree
5202 c_alignof_expr (location_t loc, tree expr)
5204 tree t;
5206 if (VAR_OR_FUNCTION_DECL_P (expr))
5207 t = size_int (DECL_ALIGN_UNIT (expr));
5209 else if (TREE_CODE (expr) == COMPONENT_REF
5210 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5212 error_at (loc, "%<__alignof%> applied to a bit-field");
5213 t = size_one_node;
5215 else if (TREE_CODE (expr) == COMPONENT_REF
5216 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5217 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5219 else if (TREE_CODE (expr) == INDIRECT_REF)
5221 tree t = TREE_OPERAND (expr, 0);
5222 tree best = t;
5223 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5225 while (CONVERT_EXPR_P (t)
5226 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5228 int thisalign;
5230 t = TREE_OPERAND (t, 0);
5231 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5232 if (thisalign > bestalign)
5233 best = t, bestalign = thisalign;
5235 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5237 else
5238 return c_alignof (loc, TREE_TYPE (expr));
5240 return fold_convert_loc (loc, size_type_node, t);
5243 /* Handle C and C++ default attributes. */
5245 enum built_in_attribute
5247 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5248 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5249 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5250 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5251 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5252 #include "builtin-attrs.def"
5253 #undef DEF_ATTR_NULL_TREE
5254 #undef DEF_ATTR_INT
5255 #undef DEF_ATTR_STRING
5256 #undef DEF_ATTR_IDENT
5257 #undef DEF_ATTR_TREE_LIST
5258 ATTR_LAST
5261 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5263 static void c_init_attributes (void);
5265 enum c_builtin_type
5267 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5268 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5269 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5270 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5271 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5272 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5273 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5274 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5275 ARG6) NAME,
5276 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5277 ARG6, ARG7) NAME,
5278 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5279 ARG6, ARG7, ARG8) NAME,
5280 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5281 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5282 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5283 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5284 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5285 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5286 NAME,
5287 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5288 ARG6, ARG7) NAME,
5289 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5290 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5291 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5292 #include "builtin-types.def"
5293 #undef DEF_PRIMITIVE_TYPE
5294 #undef DEF_FUNCTION_TYPE_0
5295 #undef DEF_FUNCTION_TYPE_1
5296 #undef DEF_FUNCTION_TYPE_2
5297 #undef DEF_FUNCTION_TYPE_3
5298 #undef DEF_FUNCTION_TYPE_4
5299 #undef DEF_FUNCTION_TYPE_5
5300 #undef DEF_FUNCTION_TYPE_6
5301 #undef DEF_FUNCTION_TYPE_7
5302 #undef DEF_FUNCTION_TYPE_8
5303 #undef DEF_FUNCTION_TYPE_VAR_0
5304 #undef DEF_FUNCTION_TYPE_VAR_1
5305 #undef DEF_FUNCTION_TYPE_VAR_2
5306 #undef DEF_FUNCTION_TYPE_VAR_3
5307 #undef DEF_FUNCTION_TYPE_VAR_4
5308 #undef DEF_FUNCTION_TYPE_VAR_5
5309 #undef DEF_FUNCTION_TYPE_VAR_7
5310 #undef DEF_FUNCTION_TYPE_VAR_11
5311 #undef DEF_POINTER_TYPE
5312 BT_LAST
5315 typedef enum c_builtin_type builtin_type;
5317 /* A temporary array for c_common_nodes_and_builtins. Used in
5318 communication with def_fn_type. */
5319 static tree builtin_types[(int) BT_LAST + 1];
5321 /* A helper function for c_common_nodes_and_builtins. Build function type
5322 for DEF with return type RET and N arguments. If VAR is true, then the
5323 function should be variadic after those N arguments.
5325 Takes special care not to ICE if any of the types involved are
5326 error_mark_node, which indicates that said type is not in fact available
5327 (see builtin_type_for_size). In which case the function type as a whole
5328 should be error_mark_node. */
5330 static void
5331 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5333 tree t;
5334 tree *args = XALLOCAVEC (tree, n);
5335 va_list list;
5336 int i;
5338 va_start (list, n);
5339 for (i = 0; i < n; ++i)
5341 builtin_type a = (builtin_type) va_arg (list, int);
5342 t = builtin_types[a];
5343 if (t == error_mark_node)
5344 goto egress;
5345 args[i] = t;
5348 t = builtin_types[ret];
5349 if (t == error_mark_node)
5350 goto egress;
5351 if (var)
5352 t = build_varargs_function_type_array (t, n, args);
5353 else
5354 t = build_function_type_array (t, n, args);
5356 egress:
5357 builtin_types[def] = t;
5358 va_end (list);
5361 /* Build builtin functions common to both C and C++ language
5362 frontends. */
5364 static void
5365 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5367 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5368 builtin_types[ENUM] = VALUE;
5369 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5370 def_fn_type (ENUM, RETURN, 0, 0);
5371 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5372 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5373 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5374 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5375 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5376 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5377 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5378 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5379 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5380 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5381 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5382 ARG6) \
5383 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5384 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5385 ARG6, ARG7) \
5386 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5387 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5388 ARG6, ARG7, ARG8) \
5389 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5390 ARG7, ARG8);
5391 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5392 def_fn_type (ENUM, RETURN, 1, 0);
5393 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5394 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5395 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5396 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5397 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5398 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5399 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5400 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5401 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5402 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5403 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5404 ARG6, ARG7) \
5405 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5406 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5407 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5408 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5409 ARG7, ARG8, ARG9, ARG10, ARG11);
5410 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5411 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5413 #include "builtin-types.def"
5415 #undef DEF_PRIMITIVE_TYPE
5416 #undef DEF_FUNCTION_TYPE_0
5417 #undef DEF_FUNCTION_TYPE_1
5418 #undef DEF_FUNCTION_TYPE_2
5419 #undef DEF_FUNCTION_TYPE_3
5420 #undef DEF_FUNCTION_TYPE_4
5421 #undef DEF_FUNCTION_TYPE_5
5422 #undef DEF_FUNCTION_TYPE_6
5423 #undef DEF_FUNCTION_TYPE_7
5424 #undef DEF_FUNCTION_TYPE_8
5425 #undef DEF_FUNCTION_TYPE_VAR_0
5426 #undef DEF_FUNCTION_TYPE_VAR_1
5427 #undef DEF_FUNCTION_TYPE_VAR_2
5428 #undef DEF_FUNCTION_TYPE_VAR_3
5429 #undef DEF_FUNCTION_TYPE_VAR_4
5430 #undef DEF_FUNCTION_TYPE_VAR_5
5431 #undef DEF_FUNCTION_TYPE_VAR_7
5432 #undef DEF_FUNCTION_TYPE_VAR_11
5433 #undef DEF_POINTER_TYPE
5434 builtin_types[(int) BT_LAST] = NULL_TREE;
5436 c_init_attributes ();
5438 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5439 NONANSI_P, ATTRS, IMPLICIT, COND) \
5440 if (NAME && COND) \
5441 def_builtin_1 (ENUM, NAME, CLASS, \
5442 builtin_types[(int) TYPE], \
5443 builtin_types[(int) LIBTYPE], \
5444 BOTH_P, FALLBACK_P, NONANSI_P, \
5445 built_in_attributes[(int) ATTRS], IMPLICIT);
5446 #include "builtins.def"
5447 #undef DEF_BUILTIN
5449 targetm.init_builtins ();
5451 build_common_builtin_nodes ();
5453 if (flag_cilkplus)
5454 cilk_init_builtins ();
5457 /* Like get_identifier, but avoid warnings about null arguments when
5458 the argument may be NULL for targets where GCC lacks stdint.h type
5459 information. */
5461 static inline tree
5462 c_get_ident (const char *id)
5464 return get_identifier (id);
5467 /* Build tree nodes and builtin functions common to both C and C++ language
5468 frontends. */
5470 void
5471 c_common_nodes_and_builtins (void)
5473 int char16_type_size;
5474 int char32_type_size;
5475 int wchar_type_size;
5476 tree array_domain_type;
5477 tree va_list_ref_type_node;
5478 tree va_list_arg_type_node;
5479 int i;
5481 build_common_tree_nodes (flag_signed_char, flag_short_double);
5483 /* Define `int' and `char' first so that dbx will output them first. */
5484 record_builtin_type (RID_INT, NULL, integer_type_node);
5485 record_builtin_type (RID_CHAR, "char", char_type_node);
5487 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5488 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5489 but not C. Are the conditionals here needed? */
5490 if (c_dialect_cxx ())
5491 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5492 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5493 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5494 record_builtin_type (RID_MAX, "long unsigned int",
5495 long_unsigned_type_node);
5497 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5499 char name[25];
5501 sprintf (name, "__int%d", int_n_data[i].bitsize);
5502 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5503 int_n_trees[i].signed_type);
5504 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5505 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5508 if (c_dialect_cxx ())
5509 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5510 record_builtin_type (RID_MAX, "long long int",
5511 long_long_integer_type_node);
5512 record_builtin_type (RID_MAX, "long long unsigned int",
5513 long_long_unsigned_type_node);
5514 if (c_dialect_cxx ())
5515 record_builtin_type (RID_MAX, "long long unsigned",
5516 long_long_unsigned_type_node);
5517 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5518 record_builtin_type (RID_MAX, "short unsigned int",
5519 short_unsigned_type_node);
5520 if (c_dialect_cxx ())
5521 record_builtin_type (RID_MAX, "unsigned short",
5522 short_unsigned_type_node);
5524 /* Define both `signed char' and `unsigned char'. */
5525 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5526 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5528 /* These are types that c_common_type_for_size and
5529 c_common_type_for_mode use. */
5530 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5531 TYPE_DECL, NULL_TREE,
5532 intQI_type_node));
5533 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5534 TYPE_DECL, NULL_TREE,
5535 intHI_type_node));
5536 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5537 TYPE_DECL, NULL_TREE,
5538 intSI_type_node));
5539 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5540 TYPE_DECL, NULL_TREE,
5541 intDI_type_node));
5542 #if HOST_BITS_PER_WIDE_INT >= 64
5543 /* Note that this is different than the __int128 type that's part of
5544 the generic __intN support. */
5545 if (targetm.scalar_mode_supported_p (TImode))
5546 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5547 TYPE_DECL,
5548 get_identifier ("__int128_t"),
5549 intTI_type_node));
5550 #endif
5551 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5552 TYPE_DECL, NULL_TREE,
5553 unsigned_intQI_type_node));
5554 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5555 TYPE_DECL, NULL_TREE,
5556 unsigned_intHI_type_node));
5557 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5558 TYPE_DECL, NULL_TREE,
5559 unsigned_intSI_type_node));
5560 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5561 TYPE_DECL, NULL_TREE,
5562 unsigned_intDI_type_node));
5563 #if HOST_BITS_PER_WIDE_INT >= 64
5564 if (targetm.scalar_mode_supported_p (TImode))
5565 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5566 TYPE_DECL,
5567 get_identifier ("__uint128_t"),
5568 unsigned_intTI_type_node));
5569 #endif
5571 /* Create the widest literal types. */
5572 widest_integer_literal_type_node
5573 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5574 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5575 TYPE_DECL, NULL_TREE,
5576 widest_integer_literal_type_node));
5578 widest_unsigned_literal_type_node
5579 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5580 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5581 TYPE_DECL, NULL_TREE,
5582 widest_unsigned_literal_type_node));
5584 signed_size_type_node = c_common_signed_type (size_type_node);
5586 pid_type_node =
5587 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5589 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5590 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5591 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5593 /* Only supported decimal floating point extension if the target
5594 actually supports underlying modes. */
5595 if (targetm.scalar_mode_supported_p (SDmode)
5596 && targetm.scalar_mode_supported_p (DDmode)
5597 && targetm.scalar_mode_supported_p (TDmode))
5599 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5600 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5601 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5604 if (targetm.fixed_point_supported_p ())
5606 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5607 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5608 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5609 record_builtin_type (RID_MAX, "long long _Fract",
5610 long_long_fract_type_node);
5611 record_builtin_type (RID_MAX, "unsigned short _Fract",
5612 unsigned_short_fract_type_node);
5613 record_builtin_type (RID_MAX, "unsigned _Fract",
5614 unsigned_fract_type_node);
5615 record_builtin_type (RID_MAX, "unsigned long _Fract",
5616 unsigned_long_fract_type_node);
5617 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5618 unsigned_long_long_fract_type_node);
5619 record_builtin_type (RID_MAX, "_Sat short _Fract",
5620 sat_short_fract_type_node);
5621 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5622 record_builtin_type (RID_MAX, "_Sat long _Fract",
5623 sat_long_fract_type_node);
5624 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5625 sat_long_long_fract_type_node);
5626 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5627 sat_unsigned_short_fract_type_node);
5628 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5629 sat_unsigned_fract_type_node);
5630 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5631 sat_unsigned_long_fract_type_node);
5632 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5633 sat_unsigned_long_long_fract_type_node);
5634 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5635 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5636 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5637 record_builtin_type (RID_MAX, "long long _Accum",
5638 long_long_accum_type_node);
5639 record_builtin_type (RID_MAX, "unsigned short _Accum",
5640 unsigned_short_accum_type_node);
5641 record_builtin_type (RID_MAX, "unsigned _Accum",
5642 unsigned_accum_type_node);
5643 record_builtin_type (RID_MAX, "unsigned long _Accum",
5644 unsigned_long_accum_type_node);
5645 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5646 unsigned_long_long_accum_type_node);
5647 record_builtin_type (RID_MAX, "_Sat short _Accum",
5648 sat_short_accum_type_node);
5649 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5650 record_builtin_type (RID_MAX, "_Sat long _Accum",
5651 sat_long_accum_type_node);
5652 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5653 sat_long_long_accum_type_node);
5654 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5655 sat_unsigned_short_accum_type_node);
5656 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5657 sat_unsigned_accum_type_node);
5658 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5659 sat_unsigned_long_accum_type_node);
5660 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5661 sat_unsigned_long_long_accum_type_node);
5665 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5666 TYPE_DECL,
5667 get_identifier ("complex int"),
5668 complex_integer_type_node));
5669 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5670 TYPE_DECL,
5671 get_identifier ("complex float"),
5672 complex_float_type_node));
5673 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5674 TYPE_DECL,
5675 get_identifier ("complex double"),
5676 complex_double_type_node));
5677 lang_hooks.decls.pushdecl
5678 (build_decl (UNKNOWN_LOCATION,
5679 TYPE_DECL, get_identifier ("complex long double"),
5680 complex_long_double_type_node));
5682 if (c_dialect_cxx ())
5683 /* For C++, make fileptr_type_node a distinct void * type until
5684 FILE type is defined. */
5685 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5687 record_builtin_type (RID_VOID, NULL, void_type_node);
5689 /* Set the TYPE_NAME for any variants that were built before
5690 record_builtin_type gave names to the built-in types. */
5692 tree void_name = TYPE_NAME (void_type_node);
5693 TYPE_NAME (void_type_node) = NULL_TREE;
5694 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5695 = void_name;
5696 TYPE_NAME (void_type_node) = void_name;
5699 void_list_node = build_void_list_node ();
5701 /* Make a type to be the domain of a few array types
5702 whose domains don't really matter.
5703 200 is small enough that it always fits in size_t
5704 and large enough that it can hold most function names for the
5705 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5706 array_domain_type = build_index_type (size_int (200));
5708 /* Make a type for arrays of characters.
5709 With luck nothing will ever really depend on the length of this
5710 array type. */
5711 char_array_type_node
5712 = build_array_type (char_type_node, array_domain_type);
5714 string_type_node = build_pointer_type (char_type_node);
5715 const_string_type_node
5716 = build_pointer_type (build_qualified_type
5717 (char_type_node, TYPE_QUAL_CONST));
5719 /* This is special for C++ so functions can be overloaded. */
5720 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5721 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5722 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5723 underlying_wchar_type_node = wchar_type_node;
5724 if (c_dialect_cxx ())
5726 if (TYPE_UNSIGNED (wchar_type_node))
5727 wchar_type_node = make_unsigned_type (wchar_type_size);
5728 else
5729 wchar_type_node = make_signed_type (wchar_type_size);
5730 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5733 /* This is for wide string constants. */
5734 wchar_array_type_node
5735 = build_array_type (wchar_type_node, array_domain_type);
5737 /* Define 'char16_t'. */
5738 char16_type_node = get_identifier (CHAR16_TYPE);
5739 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5740 char16_type_size = TYPE_PRECISION (char16_type_node);
5741 if (c_dialect_cxx ())
5743 char16_type_node = make_unsigned_type (char16_type_size);
5745 if (cxx_dialect >= cxx11)
5746 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5749 /* This is for UTF-16 string constants. */
5750 char16_array_type_node
5751 = build_array_type (char16_type_node, array_domain_type);
5753 /* Define 'char32_t'. */
5754 char32_type_node = get_identifier (CHAR32_TYPE);
5755 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5756 char32_type_size = TYPE_PRECISION (char32_type_node);
5757 if (c_dialect_cxx ())
5759 char32_type_node = make_unsigned_type (char32_type_size);
5761 if (cxx_dialect >= cxx11)
5762 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5765 /* This is for UTF-32 string constants. */
5766 char32_array_type_node
5767 = build_array_type (char32_type_node, array_domain_type);
5769 wint_type_node =
5770 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5772 intmax_type_node =
5773 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5774 uintmax_type_node =
5775 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5777 if (SIG_ATOMIC_TYPE)
5778 sig_atomic_type_node =
5779 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5780 if (INT8_TYPE)
5781 int8_type_node =
5782 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5783 if (INT16_TYPE)
5784 int16_type_node =
5785 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5786 if (INT32_TYPE)
5787 int32_type_node =
5788 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5789 if (INT64_TYPE)
5790 int64_type_node =
5791 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5792 if (UINT8_TYPE)
5793 uint8_type_node =
5794 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5795 if (UINT16_TYPE)
5796 c_uint16_type_node = uint16_type_node =
5797 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5798 if (UINT32_TYPE)
5799 c_uint32_type_node = uint32_type_node =
5800 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5801 if (UINT64_TYPE)
5802 c_uint64_type_node = uint64_type_node =
5803 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5804 if (INT_LEAST8_TYPE)
5805 int_least8_type_node =
5806 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5807 if (INT_LEAST16_TYPE)
5808 int_least16_type_node =
5809 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5810 if (INT_LEAST32_TYPE)
5811 int_least32_type_node =
5812 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5813 if (INT_LEAST64_TYPE)
5814 int_least64_type_node =
5815 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5816 if (UINT_LEAST8_TYPE)
5817 uint_least8_type_node =
5818 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5819 if (UINT_LEAST16_TYPE)
5820 uint_least16_type_node =
5821 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5822 if (UINT_LEAST32_TYPE)
5823 uint_least32_type_node =
5824 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5825 if (UINT_LEAST64_TYPE)
5826 uint_least64_type_node =
5827 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5828 if (INT_FAST8_TYPE)
5829 int_fast8_type_node =
5830 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5831 if (INT_FAST16_TYPE)
5832 int_fast16_type_node =
5833 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5834 if (INT_FAST32_TYPE)
5835 int_fast32_type_node =
5836 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5837 if (INT_FAST64_TYPE)
5838 int_fast64_type_node =
5839 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5840 if (UINT_FAST8_TYPE)
5841 uint_fast8_type_node =
5842 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5843 if (UINT_FAST16_TYPE)
5844 uint_fast16_type_node =
5845 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5846 if (UINT_FAST32_TYPE)
5847 uint_fast32_type_node =
5848 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5849 if (UINT_FAST64_TYPE)
5850 uint_fast64_type_node =
5851 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5852 if (INTPTR_TYPE)
5853 intptr_type_node =
5854 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5855 if (UINTPTR_TYPE)
5856 uintptr_type_node =
5857 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5859 default_function_type
5860 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5861 ptrdiff_type_node
5862 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5863 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5865 lang_hooks.decls.pushdecl
5866 (build_decl (UNKNOWN_LOCATION,
5867 TYPE_DECL, get_identifier ("__builtin_va_list"),
5868 va_list_type_node));
5869 if (targetm.enum_va_list_p)
5871 int l;
5872 const char *pname;
5873 tree ptype;
5875 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5877 lang_hooks.decls.pushdecl
5878 (build_decl (UNKNOWN_LOCATION,
5879 TYPE_DECL, get_identifier (pname),
5880 ptype));
5885 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5887 va_list_arg_type_node = va_list_ref_type_node =
5888 build_pointer_type (TREE_TYPE (va_list_type_node));
5890 else
5892 va_list_arg_type_node = va_list_type_node;
5893 va_list_ref_type_node = build_reference_type (va_list_type_node);
5896 if (!flag_preprocess_only)
5897 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5899 main_identifier_node = get_identifier ("main");
5901 /* Create the built-in __null node. It is important that this is
5902 not shared. */
5903 null_node = make_int_cst (1, 1);
5904 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5906 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5907 memset (builtin_types, 0, sizeof (builtin_types));
5910 /* The number of named compound-literals generated thus far. */
5911 static GTY(()) int compound_literal_number;
5913 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5915 void
5916 set_compound_literal_name (tree decl)
5918 char *name;
5919 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5920 compound_literal_number);
5921 compound_literal_number++;
5922 DECL_NAME (decl) = get_identifier (name);
5925 tree
5926 build_va_arg (location_t loc, tree expr, tree type)
5928 /* In gimplify_va_arg_expr we take the address of the ap argument, mark it
5929 addressable now. */
5930 mark_addressable (expr);
5932 expr = build1 (VA_ARG_EXPR, type, expr);
5933 SET_EXPR_LOCATION (expr, loc);
5934 return expr;
5938 /* Linked list of disabled built-in functions. */
5940 typedef struct disabled_builtin
5942 const char *name;
5943 struct disabled_builtin *next;
5944 } disabled_builtin;
5945 static disabled_builtin *disabled_builtins = NULL;
5947 static bool builtin_function_disabled_p (const char *);
5949 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5950 begins with "__builtin_", give an error. */
5952 void
5953 disable_builtin_function (const char *name)
5955 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5956 error ("cannot disable built-in function %qs", name);
5957 else
5959 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5960 new_disabled_builtin->name = name;
5961 new_disabled_builtin->next = disabled_builtins;
5962 disabled_builtins = new_disabled_builtin;
5967 /* Return true if the built-in function NAME has been disabled, false
5968 otherwise. */
5970 static bool
5971 builtin_function_disabled_p (const char *name)
5973 disabled_builtin *p;
5974 for (p = disabled_builtins; p != NULL; p = p->next)
5976 if (strcmp (name, p->name) == 0)
5977 return true;
5979 return false;
5983 /* Worker for DEF_BUILTIN.
5984 Possibly define a builtin function with one or two names.
5985 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5986 nonansi_p and flag_no_nonansi_builtin. */
5988 static void
5989 def_builtin_1 (enum built_in_function fncode,
5990 const char *name,
5991 enum built_in_class fnclass,
5992 tree fntype, tree libtype,
5993 bool both_p, bool fallback_p, bool nonansi_p,
5994 tree fnattrs, bool implicit_p)
5996 tree decl;
5997 const char *libname;
5999 if (fntype == error_mark_node)
6000 return;
6002 gcc_assert ((!both_p && !fallback_p)
6003 || !strncmp (name, "__builtin_",
6004 strlen ("__builtin_")));
6006 libname = name + strlen ("__builtin_");
6007 decl = add_builtin_function (name, fntype, fncode, fnclass,
6008 (fallback_p ? libname : NULL),
6009 fnattrs);
6011 set_builtin_decl (fncode, decl, implicit_p);
6013 if (both_p
6014 && !flag_no_builtin && !builtin_function_disabled_p (libname)
6015 && !(nonansi_p && flag_no_nonansi_builtin))
6016 add_builtin_function (libname, libtype, fncode, fnclass,
6017 NULL, fnattrs);
6020 /* Nonzero if the type T promotes to int. This is (nearly) the
6021 integral promotions defined in ISO C99 6.3.1.1/2. */
6023 bool
6024 c_promoting_integer_type_p (const_tree t)
6026 switch (TREE_CODE (t))
6028 case INTEGER_TYPE:
6029 return (TYPE_MAIN_VARIANT (t) == char_type_node
6030 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6031 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6032 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
6033 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6034 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
6036 case ENUMERAL_TYPE:
6037 /* ??? Technically all enumerations not larger than an int
6038 promote to an int. But this is used along code paths
6039 that only want to notice a size change. */
6040 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6042 case BOOLEAN_TYPE:
6043 return 1;
6045 default:
6046 return 0;
6050 /* Return 1 if PARMS specifies a fixed number of parameters
6051 and none of their types is affected by default promotions. */
6054 self_promoting_args_p (const_tree parms)
6056 const_tree t;
6057 for (t = parms; t; t = TREE_CHAIN (t))
6059 tree type = TREE_VALUE (t);
6061 if (type == error_mark_node)
6062 continue;
6064 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6065 return 0;
6067 if (type == 0)
6068 return 0;
6070 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6071 return 0;
6073 if (c_promoting_integer_type_p (type))
6074 return 0;
6076 return 1;
6079 /* Recursively remove any '*' or '&' operator from TYPE. */
6080 tree
6081 strip_pointer_operator (tree t)
6083 while (POINTER_TYPE_P (t))
6084 t = TREE_TYPE (t);
6085 return t;
6088 /* Recursively remove pointer or array type from TYPE. */
6089 tree
6090 strip_pointer_or_array_types (tree t)
6092 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6093 t = TREE_TYPE (t);
6094 return t;
6097 /* Used to compare case labels. K1 and K2 are actually tree nodes
6098 representing case labels, or NULL_TREE for a `default' label.
6099 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6100 K2, and 0 if K1 and K2 are equal. */
6103 case_compare (splay_tree_key k1, splay_tree_key k2)
6105 /* Consider a NULL key (such as arises with a `default' label) to be
6106 smaller than anything else. */
6107 if (!k1)
6108 return k2 ? -1 : 0;
6109 else if (!k2)
6110 return k1 ? 1 : 0;
6112 return tree_int_cst_compare ((tree) k1, (tree) k2);
6115 /* Process a case label, located at LOC, for the range LOW_VALUE
6116 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6117 then this case label is actually a `default' label. If only
6118 HIGH_VALUE is NULL_TREE, then case label was declared using the
6119 usual C/C++ syntax, rather than the GNU case range extension.
6120 CASES is a tree containing all the case ranges processed so far;
6121 COND is the condition for the switch-statement itself. Returns the
6122 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6123 is created. */
6125 tree
6126 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6127 tree low_value, tree high_value)
6129 tree type;
6130 tree label;
6131 tree case_label;
6132 splay_tree_node node;
6134 /* Create the LABEL_DECL itself. */
6135 label = create_artificial_label (loc);
6137 /* If there was an error processing the switch condition, bail now
6138 before we get more confused. */
6139 if (!cond || cond == error_mark_node)
6140 goto error_out;
6142 if ((low_value && TREE_TYPE (low_value)
6143 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6144 || (high_value && TREE_TYPE (high_value)
6145 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6147 error_at (loc, "pointers are not permitted as case values");
6148 goto error_out;
6151 /* Case ranges are a GNU extension. */
6152 if (high_value)
6153 pedwarn (loc, OPT_Wpedantic,
6154 "range expressions in switch statements are non-standard");
6156 type = TREE_TYPE (cond);
6157 if (low_value)
6159 low_value = check_case_value (loc, low_value);
6160 low_value = convert_and_check (loc, type, low_value);
6161 if (low_value == error_mark_node)
6162 goto error_out;
6164 if (high_value)
6166 high_value = check_case_value (loc, high_value);
6167 high_value = convert_and_check (loc, type, high_value);
6168 if (high_value == error_mark_node)
6169 goto error_out;
6172 if (low_value && high_value)
6174 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6175 really a case range, even though it was written that way.
6176 Remove the HIGH_VALUE to simplify later processing. */
6177 if (tree_int_cst_equal (low_value, high_value))
6178 high_value = NULL_TREE;
6179 else if (!tree_int_cst_lt (low_value, high_value))
6180 warning_at (loc, 0, "empty range specified");
6183 /* See if the case is in range of the type of the original testing
6184 expression. If both low_value and high_value are out of range,
6185 don't insert the case label and return NULL_TREE. */
6186 if (low_value
6187 && !check_case_bounds (loc, type, orig_type,
6188 &low_value, high_value ? &high_value : NULL))
6189 return NULL_TREE;
6191 /* Look up the LOW_VALUE in the table of case labels we already
6192 have. */
6193 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6194 /* If there was not an exact match, check for overlapping ranges.
6195 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6196 that's a `default' label and the only overlap is an exact match. */
6197 if (!node && (low_value || high_value))
6199 splay_tree_node low_bound;
6200 splay_tree_node high_bound;
6202 /* Even though there wasn't an exact match, there might be an
6203 overlap between this case range and another case range.
6204 Since we've (inductively) not allowed any overlapping case
6205 ranges, we simply need to find the greatest low case label
6206 that is smaller that LOW_VALUE, and the smallest low case
6207 label that is greater than LOW_VALUE. If there is an overlap
6208 it will occur in one of these two ranges. */
6209 low_bound = splay_tree_predecessor (cases,
6210 (splay_tree_key) low_value);
6211 high_bound = splay_tree_successor (cases,
6212 (splay_tree_key) low_value);
6214 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6215 the LOW_VALUE, so there is no need to check unless the
6216 LOW_BOUND is in fact itself a case range. */
6217 if (low_bound
6218 && CASE_HIGH ((tree) low_bound->value)
6219 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6220 low_value) >= 0)
6221 node = low_bound;
6222 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6223 range is bigger than the low end of the current range, so we
6224 are only interested if the current range is a real range, and
6225 not an ordinary case label. */
6226 else if (high_bound
6227 && high_value
6228 && (tree_int_cst_compare ((tree) high_bound->key,
6229 high_value)
6230 <= 0))
6231 node = high_bound;
6233 /* If there was an overlap, issue an error. */
6234 if (node)
6236 tree duplicate = CASE_LABEL ((tree) node->value);
6238 if (high_value)
6240 error_at (loc, "duplicate (or overlapping) case value");
6241 error_at (DECL_SOURCE_LOCATION (duplicate),
6242 "this is the first entry overlapping that value");
6244 else if (low_value)
6246 error_at (loc, "duplicate case value") ;
6247 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6249 else
6251 error_at (loc, "multiple default labels in one switch");
6252 error_at (DECL_SOURCE_LOCATION (duplicate),
6253 "this is the first default label");
6255 goto error_out;
6258 /* Add a CASE_LABEL to the statement-tree. */
6259 case_label = add_stmt (build_case_label (low_value, high_value, label));
6260 /* Register this case label in the splay tree. */
6261 splay_tree_insert (cases,
6262 (splay_tree_key) low_value,
6263 (splay_tree_value) case_label);
6265 return case_label;
6267 error_out:
6268 /* Add a label so that the back-end doesn't think that the beginning of
6269 the switch is unreachable. Note that we do not add a case label, as
6270 that just leads to duplicates and thence to failure later on. */
6271 if (!cases->root)
6273 tree t = create_artificial_label (loc);
6274 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6276 return error_mark_node;
6279 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6280 Used to verify that case values match up with enumerator values. */
6282 static void
6283 match_case_to_enum_1 (tree key, tree type, tree label)
6285 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6287 if (tree_fits_uhwi_p (key))
6288 print_dec (key, buf, UNSIGNED);
6289 else if (tree_fits_shwi_p (key))
6290 print_dec (key, buf, SIGNED);
6291 else
6292 print_hex (key, buf);
6294 if (TYPE_NAME (type) == 0)
6295 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6296 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6297 "case value %qs not in enumerated type",
6298 buf);
6299 else
6300 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6301 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6302 "case value %qs not in enumerated type %qT",
6303 buf, type);
6306 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6307 Used to verify that case values match up with enumerator values. */
6309 static int
6310 match_case_to_enum (splay_tree_node node, void *data)
6312 tree label = (tree) node->value;
6313 tree type = (tree) data;
6315 /* Skip default case. */
6316 if (!CASE_LOW (label))
6317 return 0;
6319 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6320 when we did our enum->case scan. Reset our scratch bit after. */
6321 if (!CASE_LOW_SEEN (label))
6322 match_case_to_enum_1 (CASE_LOW (label), type, label);
6323 else
6324 CASE_LOW_SEEN (label) = 0;
6326 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6327 not set, that means that CASE_HIGH did not appear when we did our
6328 enum->case scan. Reset our scratch bit after. */
6329 if (CASE_HIGH (label))
6331 if (!CASE_HIGH_SEEN (label))
6332 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6333 else
6334 CASE_HIGH_SEEN (label) = 0;
6337 return 0;
6340 /* Handle -Wswitch*. Called from the front end after parsing the
6341 switch construct. */
6342 /* ??? Should probably be somewhere generic, since other languages
6343 besides C and C++ would want this. At the moment, however, C/C++
6344 are the only tree-ssa languages that support enumerations at all,
6345 so the point is moot. */
6347 void
6348 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6349 tree type, tree cond)
6351 splay_tree_node default_node;
6352 splay_tree_node node;
6353 tree chain;
6355 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6356 return;
6358 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6359 if (!default_node)
6360 warning_at (switch_location, OPT_Wswitch_default,
6361 "switch missing default case");
6363 /* From here on, we only care about about enumerated types. */
6364 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6365 return;
6367 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6368 if (!warn_switch_enum && !warn_switch)
6369 return;
6371 /* Check the cases. Warn about case values which are not members of
6372 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6373 there is no default case, check that exactly all enumeration
6374 literals are covered by the cases. */
6376 /* Clearing COND if it is not an integer constant simplifies
6377 the tests inside the loop below. */
6378 if (TREE_CODE (cond) != INTEGER_CST)
6379 cond = NULL_TREE;
6381 /* The time complexity here is O(N*lg(N)) worst case, but for the
6382 common case of monotonically increasing enumerators, it is
6383 O(N), since the nature of the splay tree will keep the next
6384 element adjacent to the root at all times. */
6386 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6388 tree value = TREE_VALUE (chain);
6389 if (TREE_CODE (value) == CONST_DECL)
6390 value = DECL_INITIAL (value);
6391 node = splay_tree_lookup (cases, (splay_tree_key) value);
6392 if (node)
6394 /* Mark the CASE_LOW part of the case entry as seen. */
6395 tree label = (tree) node->value;
6396 CASE_LOW_SEEN (label) = 1;
6397 continue;
6400 /* Even though there wasn't an exact match, there might be a
6401 case range which includes the enumerator's value. */
6402 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6403 if (node && CASE_HIGH ((tree) node->value))
6405 tree label = (tree) node->value;
6406 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6407 if (cmp >= 0)
6409 /* If we match the upper bound exactly, mark the CASE_HIGH
6410 part of the case entry as seen. */
6411 if (cmp == 0)
6412 CASE_HIGH_SEEN (label) = 1;
6413 continue;
6417 /* We've now determined that this enumerated literal isn't
6418 handled by the case labels of the switch statement. */
6420 /* If the switch expression is a constant, we only really care
6421 about whether that constant is handled by the switch. */
6422 if (cond && tree_int_cst_compare (cond, value))
6423 continue;
6425 /* If there is a default_node, the only relevant option is
6426 Wswitch-enum. Otherwise, if both are enabled then we prefer
6427 to warn using -Wswitch because -Wswitch is enabled by -Wall
6428 while -Wswitch-enum is explicit. */
6429 warning_at (switch_location,
6430 (default_node || !warn_switch
6431 ? OPT_Wswitch_enum
6432 : OPT_Wswitch),
6433 "enumeration value %qE not handled in switch",
6434 TREE_PURPOSE (chain));
6437 /* Warn if there are case expressions that don't correspond to
6438 enumerators. This can occur since C and C++ don't enforce
6439 type-checking of assignments to enumeration variables.
6441 The time complexity here is now always O(N) worst case, since
6442 we should have marked both the lower bound and upper bound of
6443 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6444 above. This scan also resets those fields. */
6446 splay_tree_foreach (cases, match_case_to_enum, type);
6449 /* Finish an expression taking the address of LABEL (an
6450 IDENTIFIER_NODE). Returns an expression for the address.
6452 LOC is the location for the expression returned. */
6454 tree
6455 finish_label_address_expr (tree label, location_t loc)
6457 tree result;
6459 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6461 if (label == error_mark_node)
6462 return error_mark_node;
6464 label = lookup_label (label);
6465 if (label == NULL_TREE)
6466 result = null_pointer_node;
6467 else
6469 TREE_USED (label) = 1;
6470 result = build1 (ADDR_EXPR, ptr_type_node, label);
6471 /* The current function is not necessarily uninlinable.
6472 Computed gotos are incompatible with inlining, but the value
6473 here could be used only in a diagnostic, for example. */
6474 protected_set_expr_location (result, loc);
6477 return result;
6481 /* Given a boolean expression ARG, return a tree representing an increment
6482 or decrement (as indicated by CODE) of ARG. The front end must check for
6483 invalid cases (e.g., decrement in C++). */
6484 tree
6485 boolean_increment (enum tree_code code, tree arg)
6487 tree val;
6488 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6490 arg = stabilize_reference (arg);
6491 switch (code)
6493 case PREINCREMENT_EXPR:
6494 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6495 break;
6496 case POSTINCREMENT_EXPR:
6497 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6498 arg = save_expr (arg);
6499 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6500 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6501 break;
6502 case PREDECREMENT_EXPR:
6503 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6504 invert_truthvalue_loc (input_location, arg));
6505 break;
6506 case POSTDECREMENT_EXPR:
6507 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6508 invert_truthvalue_loc (input_location, arg));
6509 arg = save_expr (arg);
6510 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6511 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6512 break;
6513 default:
6514 gcc_unreachable ();
6516 TREE_SIDE_EFFECTS (val) = 1;
6517 return val;
6520 /* Built-in macros for stddef.h and stdint.h, that require macros
6521 defined in this file. */
6522 void
6523 c_stddef_cpp_builtins(void)
6525 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6526 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6527 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6528 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6529 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6530 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6531 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6532 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6533 if (SIG_ATOMIC_TYPE)
6534 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6535 if (INT8_TYPE)
6536 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6537 if (INT16_TYPE)
6538 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6539 if (INT32_TYPE)
6540 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6541 if (INT64_TYPE)
6542 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6543 if (UINT8_TYPE)
6544 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6545 if (UINT16_TYPE)
6546 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6547 if (UINT32_TYPE)
6548 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6549 if (UINT64_TYPE)
6550 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6551 if (INT_LEAST8_TYPE)
6552 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6553 if (INT_LEAST16_TYPE)
6554 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6555 if (INT_LEAST32_TYPE)
6556 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6557 if (INT_LEAST64_TYPE)
6558 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6559 if (UINT_LEAST8_TYPE)
6560 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6561 if (UINT_LEAST16_TYPE)
6562 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6563 if (UINT_LEAST32_TYPE)
6564 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6565 if (UINT_LEAST64_TYPE)
6566 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6567 if (INT_FAST8_TYPE)
6568 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6569 if (INT_FAST16_TYPE)
6570 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6571 if (INT_FAST32_TYPE)
6572 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6573 if (INT_FAST64_TYPE)
6574 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6575 if (UINT_FAST8_TYPE)
6576 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6577 if (UINT_FAST16_TYPE)
6578 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6579 if (UINT_FAST32_TYPE)
6580 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6581 if (UINT_FAST64_TYPE)
6582 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6583 if (INTPTR_TYPE)
6584 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6585 if (UINTPTR_TYPE)
6586 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6589 static void
6590 c_init_attributes (void)
6592 /* Fill in the built_in_attributes array. */
6593 #define DEF_ATTR_NULL_TREE(ENUM) \
6594 built_in_attributes[(int) ENUM] = NULL_TREE;
6595 #define DEF_ATTR_INT(ENUM, VALUE) \
6596 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6597 #define DEF_ATTR_STRING(ENUM, VALUE) \
6598 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6599 #define DEF_ATTR_IDENT(ENUM, STRING) \
6600 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6601 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6602 built_in_attributes[(int) ENUM] \
6603 = tree_cons (built_in_attributes[(int) PURPOSE], \
6604 built_in_attributes[(int) VALUE], \
6605 built_in_attributes[(int) CHAIN]);
6606 #include "builtin-attrs.def"
6607 #undef DEF_ATTR_NULL_TREE
6608 #undef DEF_ATTR_INT
6609 #undef DEF_ATTR_IDENT
6610 #undef DEF_ATTR_TREE_LIST
6613 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6614 identifier as an argument, so the front end shouldn't look it up. */
6616 bool
6617 attribute_takes_identifier_p (const_tree attr_id)
6619 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6620 if (spec == NULL)
6621 /* Unknown attribute that we'll end up ignoring, return true so we
6622 don't complain about an identifier argument. */
6623 return true;
6624 else if (!strcmp ("mode", spec->name)
6625 || !strcmp ("format", spec->name)
6626 || !strcmp ("cleanup", spec->name))
6627 return true;
6628 else
6629 return targetm.attribute_takes_identifier_p (attr_id);
6632 /* Attribute handlers common to C front ends. */
6634 /* Handle a "packed" attribute; arguments as in
6635 struct attribute_spec.handler. */
6637 static tree
6638 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6639 int flags, bool *no_add_attrs)
6641 if (TYPE_P (*node))
6643 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6644 *node = build_variant_type_copy (*node);
6645 TYPE_PACKED (*node) = 1;
6647 else if (TREE_CODE (*node) == FIELD_DECL)
6649 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6650 /* Still pack bitfields. */
6651 && ! DECL_INITIAL (*node))
6652 warning (OPT_Wattributes,
6653 "%qE attribute ignored for field of type %qT",
6654 name, TREE_TYPE (*node));
6655 else
6656 DECL_PACKED (*node) = 1;
6658 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6659 used for DECL_REGISTER. It wouldn't mean anything anyway.
6660 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6661 that changes what the typedef is typing. */
6662 else
6664 warning (OPT_Wattributes, "%qE attribute ignored", name);
6665 *no_add_attrs = true;
6668 return NULL_TREE;
6671 /* Handle a "nocommon" attribute; arguments as in
6672 struct attribute_spec.handler. */
6674 static tree
6675 handle_nocommon_attribute (tree *node, tree name,
6676 tree ARG_UNUSED (args),
6677 int ARG_UNUSED (flags), bool *no_add_attrs)
6679 if (TREE_CODE (*node) == VAR_DECL)
6680 DECL_COMMON (*node) = 0;
6681 else
6683 warning (OPT_Wattributes, "%qE attribute ignored", name);
6684 *no_add_attrs = true;
6687 return NULL_TREE;
6690 /* Handle a "common" attribute; arguments as in
6691 struct attribute_spec.handler. */
6693 static tree
6694 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6695 int ARG_UNUSED (flags), bool *no_add_attrs)
6697 if (TREE_CODE (*node) == VAR_DECL)
6698 DECL_COMMON (*node) = 1;
6699 else
6701 warning (OPT_Wattributes, "%qE attribute ignored", name);
6702 *no_add_attrs = true;
6705 return NULL_TREE;
6708 /* Handle a "noreturn" attribute; arguments as in
6709 struct attribute_spec.handler. */
6711 static tree
6712 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6713 int ARG_UNUSED (flags), bool *no_add_attrs)
6715 tree type = TREE_TYPE (*node);
6717 /* See FIXME comment in c_common_attribute_table. */
6718 if (TREE_CODE (*node) == FUNCTION_DECL
6719 || objc_method_decl (TREE_CODE (*node)))
6720 TREE_THIS_VOLATILE (*node) = 1;
6721 else if (TREE_CODE (type) == POINTER_TYPE
6722 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6723 TREE_TYPE (*node)
6724 = (build_qualified_type
6725 (build_pointer_type
6726 (build_type_variant (TREE_TYPE (type),
6727 TYPE_READONLY (TREE_TYPE (type)), 1)),
6728 TYPE_QUALS (type)));
6729 else
6731 warning (OPT_Wattributes, "%qE attribute ignored", name);
6732 *no_add_attrs = true;
6735 return NULL_TREE;
6738 /* Handle a "hot" and attribute; arguments as in
6739 struct attribute_spec.handler. */
6741 static tree
6742 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6743 int ARG_UNUSED (flags), bool *no_add_attrs)
6745 if (TREE_CODE (*node) == FUNCTION_DECL
6746 || TREE_CODE (*node) == LABEL_DECL)
6748 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6750 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6751 "with attribute %qs", name, "cold");
6752 *no_add_attrs = true;
6754 /* Most of the rest of the hot processing is done later with
6755 lookup_attribute. */
6757 else
6759 warning (OPT_Wattributes, "%qE attribute ignored", name);
6760 *no_add_attrs = true;
6763 return NULL_TREE;
6766 /* Handle a "cold" and attribute; arguments as in
6767 struct attribute_spec.handler. */
6769 static tree
6770 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6771 int ARG_UNUSED (flags), bool *no_add_attrs)
6773 if (TREE_CODE (*node) == FUNCTION_DECL
6774 || TREE_CODE (*node) == LABEL_DECL)
6776 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6778 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6779 "with attribute %qs", name, "hot");
6780 *no_add_attrs = true;
6782 /* Most of the rest of the cold processing is done later with
6783 lookup_attribute. */
6785 else
6787 warning (OPT_Wattributes, "%qE attribute ignored", name);
6788 *no_add_attrs = true;
6791 return NULL_TREE;
6794 /* Handle a "no_sanitize_address" attribute; arguments as in
6795 struct attribute_spec.handler. */
6797 static tree
6798 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6799 bool *no_add_attrs)
6801 if (TREE_CODE (*node) != FUNCTION_DECL)
6803 warning (OPT_Wattributes, "%qE attribute ignored", name);
6804 *no_add_attrs = true;
6807 return NULL_TREE;
6810 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6811 struct attribute_spec.handler. */
6813 static tree
6814 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6815 bool *no_add_attrs)
6817 if (TREE_CODE (*node) != FUNCTION_DECL)
6818 warning (OPT_Wattributes, "%qE attribute ignored", name);
6819 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6820 DECL_ATTRIBUTES (*node)
6821 = tree_cons (get_identifier ("no_sanitize_address"),
6822 NULL_TREE, DECL_ATTRIBUTES (*node));
6823 *no_add_attrs = true;
6824 return NULL_TREE;
6827 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6828 struct attribute_spec.handler. */
6830 static tree
6831 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6832 bool *no_add_attrs)
6834 if (TREE_CODE (*node) != FUNCTION_DECL)
6836 warning (OPT_Wattributes, "%qE attribute ignored", name);
6837 *no_add_attrs = true;
6840 return NULL_TREE;
6843 /* Handle a "stack_protect" attribute; arguments as in
6844 struct attribute_spec.handler. */
6845 static tree
6846 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6847 bool *no_add_attrs)
6849 if (TREE_CODE (*node) != FUNCTION_DECL)
6851 warning (OPT_Wattributes, "%qE attribute ignored", name);
6852 *no_add_attrs = true;
6854 else
6855 DECL_ATTRIBUTES (*node)
6856 = tree_cons (get_identifier ("stack_protect"),
6857 NULL_TREE, DECL_ATTRIBUTES (*node));
6859 return NULL_TREE;
6862 /* Handle a "noinline" attribute; arguments as in
6863 struct attribute_spec.handler. */
6865 static tree
6866 handle_noinline_attribute (tree *node, tree name,
6867 tree ARG_UNUSED (args),
6868 int ARG_UNUSED (flags), bool *no_add_attrs)
6870 if (TREE_CODE (*node) == FUNCTION_DECL)
6872 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6874 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6875 "with attribute %qs", name, "always_inline");
6876 *no_add_attrs = true;
6878 else
6879 DECL_UNINLINABLE (*node) = 1;
6881 else
6883 warning (OPT_Wattributes, "%qE attribute ignored", name);
6884 *no_add_attrs = true;
6887 return NULL_TREE;
6890 /* Handle a "noclone" attribute; arguments as in
6891 struct attribute_spec.handler. */
6893 static tree
6894 handle_noclone_attribute (tree *node, tree name,
6895 tree ARG_UNUSED (args),
6896 int ARG_UNUSED (flags), bool *no_add_attrs)
6898 if (TREE_CODE (*node) != FUNCTION_DECL)
6900 warning (OPT_Wattributes, "%qE attribute ignored", name);
6901 *no_add_attrs = true;
6904 return NULL_TREE;
6907 /* Handle a "no_icf" attribute; arguments as in
6908 struct attribute_spec.handler. */
6910 static tree
6911 handle_noicf_attribute (tree *node, tree name,
6912 tree ARG_UNUSED (args),
6913 int ARG_UNUSED (flags), bool *no_add_attrs)
6915 if (TREE_CODE (*node) != FUNCTION_DECL)
6917 warning (OPT_Wattributes, "%qE attribute ignored", name);
6918 *no_add_attrs = true;
6921 return NULL_TREE;
6924 /* Handle a "hsa" attribute; arguments as in
6925 struct attribute_spec.handler. */
6927 static tree
6928 handle_hsa_attribute (tree *node, tree name,
6929 tree ARG_UNUSED (args),
6930 int ARG_UNUSED (flags), bool *no_add_attrs)
6932 if (TREE_CODE (*node) != FUNCTION_DECL)
6934 warning (OPT_Wattributes, "%qE attribute ignored", name);
6935 *no_add_attrs = true;
6936 return NULL_TREE;
6939 TREE_USED (*node) = 1;
6940 DECL_UNINLINABLE (*node) = 1;
6941 if (strcmp ("hsakernel", IDENTIFIER_POINTER (name)) == 0
6942 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (*node))))
6943 == void_type_node))
6944 warning (OPT_Wattributes, "%qE attribute on a function with fixed number "
6945 "of argument makes no sense", name);
6947 return NULL_TREE;
6950 /* Handle a "always_inline" attribute; arguments as in
6951 struct attribute_spec.handler. */
6953 static tree
6954 handle_always_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)
6961 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6963 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6964 "with %qs attribute", name, "noinline");
6965 *no_add_attrs = true;
6967 else
6968 /* Set the attribute and mark it for disregarding inline
6969 limits. */
6970 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6972 else
6974 warning (OPT_Wattributes, "%qE attribute ignored", name);
6975 *no_add_attrs = true;
6978 return NULL_TREE;
6981 /* Handle a "gnu_inline" attribute; arguments as in
6982 struct attribute_spec.handler. */
6984 static tree
6985 handle_gnu_inline_attribute (tree *node, tree name,
6986 tree ARG_UNUSED (args),
6987 int ARG_UNUSED (flags),
6988 bool *no_add_attrs)
6990 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6992 /* Do nothing else, just set the attribute. We'll get at
6993 it later with lookup_attribute. */
6995 else
6997 warning (OPT_Wattributes, "%qE attribute ignored", name);
6998 *no_add_attrs = true;
7001 return NULL_TREE;
7004 /* Handle a "leaf" attribute; arguments as in
7005 struct attribute_spec.handler. */
7007 static tree
7008 handle_leaf_attribute (tree *node, tree name,
7009 tree ARG_UNUSED (args),
7010 int ARG_UNUSED (flags), bool *no_add_attrs)
7012 if (TREE_CODE (*node) != FUNCTION_DECL)
7014 warning (OPT_Wattributes, "%qE attribute ignored", name);
7015 *no_add_attrs = true;
7017 if (!TREE_PUBLIC (*node))
7019 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7020 *no_add_attrs = true;
7023 return NULL_TREE;
7026 /* Handle an "artificial" attribute; arguments as in
7027 struct attribute_spec.handler. */
7029 static tree
7030 handle_artificial_attribute (tree *node, tree name,
7031 tree ARG_UNUSED (args),
7032 int ARG_UNUSED (flags),
7033 bool *no_add_attrs)
7035 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7037 /* Do nothing else, just set the attribute. We'll get at
7038 it later with lookup_attribute. */
7040 else
7042 warning (OPT_Wattributes, "%qE attribute ignored", name);
7043 *no_add_attrs = true;
7046 return NULL_TREE;
7049 /* Handle a "flatten" attribute; arguments as in
7050 struct attribute_spec.handler. */
7052 static tree
7053 handle_flatten_attribute (tree *node, tree name,
7054 tree args ATTRIBUTE_UNUSED,
7055 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7057 if (TREE_CODE (*node) == FUNCTION_DECL)
7058 /* Do nothing else, just set the attribute. We'll get at
7059 it later with lookup_attribute. */
7061 else
7063 warning (OPT_Wattributes, "%qE attribute ignored", name);
7064 *no_add_attrs = true;
7067 return NULL_TREE;
7070 /* Handle a "warning" or "error" attribute; arguments as in
7071 struct attribute_spec.handler. */
7073 static tree
7074 handle_error_attribute (tree *node, tree name, tree args,
7075 int ARG_UNUSED (flags), bool *no_add_attrs)
7077 if (TREE_CODE (*node) == FUNCTION_DECL
7078 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7079 /* Do nothing else, just set the attribute. We'll get at
7080 it later with lookup_attribute. */
7082 else
7084 warning (OPT_Wattributes, "%qE attribute ignored", name);
7085 *no_add_attrs = true;
7088 return NULL_TREE;
7091 /* Handle a "used" attribute; arguments as in
7092 struct attribute_spec.handler. */
7094 static tree
7095 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7096 int ARG_UNUSED (flags), bool *no_add_attrs)
7098 tree node = *pnode;
7100 if (TREE_CODE (node) == FUNCTION_DECL
7101 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7102 || (TREE_CODE (node) == TYPE_DECL))
7104 TREE_USED (node) = 1;
7105 DECL_PRESERVE_P (node) = 1;
7106 if (TREE_CODE (node) == VAR_DECL)
7107 DECL_READ_P (node) = 1;
7109 else
7111 warning (OPT_Wattributes, "%qE attribute ignored", name);
7112 *no_add_attrs = true;
7115 return NULL_TREE;
7118 /* Handle a "unused" attribute; arguments as in
7119 struct attribute_spec.handler. */
7121 static tree
7122 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7123 int flags, bool *no_add_attrs)
7125 if (DECL_P (*node))
7127 tree decl = *node;
7129 if (TREE_CODE (decl) == PARM_DECL
7130 || TREE_CODE (decl) == VAR_DECL
7131 || TREE_CODE (decl) == FUNCTION_DECL
7132 || TREE_CODE (decl) == LABEL_DECL
7133 || TREE_CODE (decl) == TYPE_DECL)
7135 TREE_USED (decl) = 1;
7136 if (TREE_CODE (decl) == VAR_DECL
7137 || TREE_CODE (decl) == PARM_DECL)
7138 DECL_READ_P (decl) = 1;
7140 else
7142 warning (OPT_Wattributes, "%qE attribute ignored", name);
7143 *no_add_attrs = true;
7146 else
7148 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7149 *node = build_variant_type_copy (*node);
7150 TREE_USED (*node) = 1;
7153 return NULL_TREE;
7156 /* Handle a "externally_visible" attribute; arguments as in
7157 struct attribute_spec.handler. */
7159 static tree
7160 handle_externally_visible_attribute (tree *pnode, tree name,
7161 tree ARG_UNUSED (args),
7162 int ARG_UNUSED (flags),
7163 bool *no_add_attrs)
7165 tree node = *pnode;
7167 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7169 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7170 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7172 warning (OPT_Wattributes,
7173 "%qE attribute have effect only on public objects", name);
7174 *no_add_attrs = true;
7177 else
7179 warning (OPT_Wattributes, "%qE attribute ignored", name);
7180 *no_add_attrs = true;
7183 return NULL_TREE;
7186 /* Handle the "no_reorder" attribute. Arguments as in
7187 struct attribute_spec.handler. */
7189 static tree
7190 handle_no_reorder_attribute (tree *pnode,
7191 tree name,
7192 tree,
7193 int,
7194 bool *no_add_attrs)
7196 tree node = *pnode;
7198 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7199 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7201 warning (OPT_Wattributes,
7202 "%qE attribute only affects top level objects",
7203 name);
7204 *no_add_attrs = true;
7207 return NULL_TREE;
7210 /* Handle a "const" attribute; arguments as in
7211 struct attribute_spec.handler. */
7213 static tree
7214 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7215 int ARG_UNUSED (flags), bool *no_add_attrs)
7217 tree type = TREE_TYPE (*node);
7219 /* See FIXME comment on noreturn in c_common_attribute_table. */
7220 if (TREE_CODE (*node) == FUNCTION_DECL)
7221 TREE_READONLY (*node) = 1;
7222 else if (TREE_CODE (type) == POINTER_TYPE
7223 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7224 TREE_TYPE (*node)
7225 = (build_qualified_type
7226 (build_pointer_type
7227 (build_type_variant (TREE_TYPE (type), 1,
7228 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7229 TYPE_QUALS (type)));
7230 else
7232 warning (OPT_Wattributes, "%qE attribute ignored", name);
7233 *no_add_attrs = true;
7236 return NULL_TREE;
7239 /* Handle a "transparent_union" attribute; arguments as in
7240 struct attribute_spec.handler. */
7242 static tree
7243 handle_transparent_union_attribute (tree *node, tree name,
7244 tree ARG_UNUSED (args), int flags,
7245 bool *no_add_attrs)
7247 tree type;
7249 *no_add_attrs = true;
7252 if (TREE_CODE (*node) == TYPE_DECL
7253 && ! (flags & ATTR_FLAG_CXX11))
7254 node = &TREE_TYPE (*node);
7255 type = *node;
7257 if (TREE_CODE (type) == UNION_TYPE)
7259 /* Make sure that the first field will work for a transparent union.
7260 If the type isn't complete yet, leave the check to the code in
7261 finish_struct. */
7262 if (TYPE_SIZE (type))
7264 tree first = first_field (type);
7265 if (first == NULL_TREE
7266 || DECL_ARTIFICIAL (first)
7267 || TYPE_MODE (type) != DECL_MODE (first))
7268 goto ignored;
7271 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7273 /* If the type isn't complete yet, setting the flag
7274 on a variant wouldn't ever be checked. */
7275 if (!TYPE_SIZE (type))
7276 goto ignored;
7278 /* build_duplicate_type doesn't work for C++. */
7279 if (c_dialect_cxx ())
7280 goto ignored;
7282 /* A type variant isn't good enough, since we don't a cast
7283 to such a type removed as a no-op. */
7284 *node = type = build_duplicate_type (type);
7287 TYPE_TRANSPARENT_AGGR (type) = 1;
7288 return NULL_TREE;
7291 ignored:
7292 warning (OPT_Wattributes, "%qE attribute ignored", name);
7293 return NULL_TREE;
7296 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7297 get the requested priority for a constructor or destructor,
7298 possibly issuing diagnostics for invalid or reserved
7299 priorities. */
7301 static priority_type
7302 get_priority (tree args, bool is_destructor)
7304 HOST_WIDE_INT pri;
7305 tree arg;
7307 if (!args)
7308 return DEFAULT_INIT_PRIORITY;
7310 if (!SUPPORTS_INIT_PRIORITY)
7312 if (is_destructor)
7313 error ("destructor priorities are not supported");
7314 else
7315 error ("constructor priorities are not supported");
7316 return DEFAULT_INIT_PRIORITY;
7319 arg = TREE_VALUE (args);
7320 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7321 goto invalid;
7322 if (arg == error_mark_node)
7323 return DEFAULT_INIT_PRIORITY;
7324 arg = default_conversion (arg);
7325 if (!tree_fits_shwi_p (arg)
7326 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7327 goto invalid;
7329 pri = tree_to_shwi (arg);
7330 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7331 goto invalid;
7333 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7335 if (is_destructor)
7336 warning (0,
7337 "destructor priorities from 0 to %d are reserved "
7338 "for the implementation",
7339 MAX_RESERVED_INIT_PRIORITY);
7340 else
7341 warning (0,
7342 "constructor priorities from 0 to %d are reserved "
7343 "for the implementation",
7344 MAX_RESERVED_INIT_PRIORITY);
7346 return pri;
7348 invalid:
7349 if (is_destructor)
7350 error ("destructor priorities must be integers from 0 to %d inclusive",
7351 MAX_INIT_PRIORITY);
7352 else
7353 error ("constructor priorities must be integers from 0 to %d inclusive",
7354 MAX_INIT_PRIORITY);
7355 return DEFAULT_INIT_PRIORITY;
7358 /* Handle a "constructor" attribute; arguments as in
7359 struct attribute_spec.handler. */
7361 static tree
7362 handle_constructor_attribute (tree *node, tree name, tree args,
7363 int ARG_UNUSED (flags),
7364 bool *no_add_attrs)
7366 tree decl = *node;
7367 tree type = TREE_TYPE (decl);
7369 if (TREE_CODE (decl) == FUNCTION_DECL
7370 && TREE_CODE (type) == FUNCTION_TYPE
7371 && decl_function_context (decl) == 0)
7373 priority_type priority;
7374 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7375 priority = get_priority (args, /*is_destructor=*/false);
7376 SET_DECL_INIT_PRIORITY (decl, priority);
7377 TREE_USED (decl) = 1;
7379 else
7381 warning (OPT_Wattributes, "%qE attribute ignored", name);
7382 *no_add_attrs = true;
7385 return NULL_TREE;
7388 /* Handle a "destructor" attribute; arguments as in
7389 struct attribute_spec.handler. */
7391 static tree
7392 handle_destructor_attribute (tree *node, tree name, tree args,
7393 int ARG_UNUSED (flags),
7394 bool *no_add_attrs)
7396 tree decl = *node;
7397 tree type = TREE_TYPE (decl);
7399 if (TREE_CODE (decl) == FUNCTION_DECL
7400 && TREE_CODE (type) == FUNCTION_TYPE
7401 && decl_function_context (decl) == 0)
7403 priority_type priority;
7404 DECL_STATIC_DESTRUCTOR (decl) = 1;
7405 priority = get_priority (args, /*is_destructor=*/true);
7406 SET_DECL_FINI_PRIORITY (decl, priority);
7407 TREE_USED (decl) = 1;
7409 else
7411 warning (OPT_Wattributes, "%qE attribute ignored", name);
7412 *no_add_attrs = true;
7415 return NULL_TREE;
7418 /* Nonzero if the mode is a valid vector mode for this architecture.
7419 This returns nonzero even if there is no hardware support for the
7420 vector mode, but we can emulate with narrower modes. */
7422 static int
7423 vector_mode_valid_p (machine_mode mode)
7425 enum mode_class mclass = GET_MODE_CLASS (mode);
7426 machine_mode innermode;
7428 /* Doh! What's going on? */
7429 if (mclass != MODE_VECTOR_INT
7430 && mclass != MODE_VECTOR_FLOAT
7431 && mclass != MODE_VECTOR_FRACT
7432 && mclass != MODE_VECTOR_UFRACT
7433 && mclass != MODE_VECTOR_ACCUM
7434 && mclass != MODE_VECTOR_UACCUM)
7435 return 0;
7437 /* Hardware support. Woo hoo! */
7438 if (targetm.vector_mode_supported_p (mode))
7439 return 1;
7441 innermode = GET_MODE_INNER (mode);
7443 /* We should probably return 1 if requesting V4DI and we have no DI,
7444 but we have V2DI, but this is probably very unlikely. */
7446 /* If we have support for the inner mode, we can safely emulate it.
7447 We may not have V2DI, but me can emulate with a pair of DIs. */
7448 return targetm.scalar_mode_supported_p (innermode);
7452 /* Handle a "mode" attribute; arguments as in
7453 struct attribute_spec.handler. */
7455 static tree
7456 handle_mode_attribute (tree *node, tree name, tree args,
7457 int ARG_UNUSED (flags), bool *no_add_attrs)
7459 tree type = *node;
7460 tree ident = TREE_VALUE (args);
7462 *no_add_attrs = true;
7464 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7465 warning (OPT_Wattributes, "%qE attribute ignored", name);
7466 else
7468 int j;
7469 const char *p = IDENTIFIER_POINTER (ident);
7470 int len = strlen (p);
7471 machine_mode mode = VOIDmode;
7472 tree typefm;
7473 bool valid_mode;
7475 if (len > 4 && p[0] == '_' && p[1] == '_'
7476 && p[len - 1] == '_' && p[len - 2] == '_')
7478 char *newp = (char *) alloca (len - 1);
7480 strcpy (newp, &p[2]);
7481 newp[len - 4] = '\0';
7482 p = newp;
7485 /* Change this type to have a type with the specified mode.
7486 First check for the special modes. */
7487 if (!strcmp (p, "byte"))
7488 mode = byte_mode;
7489 else if (!strcmp (p, "word"))
7490 mode = word_mode;
7491 else if (!strcmp (p, "pointer"))
7492 mode = ptr_mode;
7493 else if (!strcmp (p, "libgcc_cmp_return"))
7494 mode = targetm.libgcc_cmp_return_mode ();
7495 else if (!strcmp (p, "libgcc_shift_count"))
7496 mode = targetm.libgcc_shift_count_mode ();
7497 else if (!strcmp (p, "unwind_word"))
7498 mode = targetm.unwind_word_mode ();
7499 else
7500 for (j = 0; j < NUM_MACHINE_MODES; j++)
7501 if (!strcmp (p, GET_MODE_NAME (j)))
7503 mode = (machine_mode) j;
7504 break;
7507 if (mode == VOIDmode)
7509 error ("unknown machine mode %qE", ident);
7510 return NULL_TREE;
7513 valid_mode = false;
7514 switch (GET_MODE_CLASS (mode))
7516 case MODE_INT:
7517 case MODE_PARTIAL_INT:
7518 case MODE_FLOAT:
7519 case MODE_DECIMAL_FLOAT:
7520 case MODE_FRACT:
7521 case MODE_UFRACT:
7522 case MODE_ACCUM:
7523 case MODE_UACCUM:
7524 valid_mode = targetm.scalar_mode_supported_p (mode);
7525 break;
7527 case MODE_COMPLEX_INT:
7528 case MODE_COMPLEX_FLOAT:
7529 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7530 break;
7532 case MODE_VECTOR_INT:
7533 case MODE_VECTOR_FLOAT:
7534 case MODE_VECTOR_FRACT:
7535 case MODE_VECTOR_UFRACT:
7536 case MODE_VECTOR_ACCUM:
7537 case MODE_VECTOR_UACCUM:
7538 warning (OPT_Wattributes, "specifying vector types with "
7539 "__attribute__ ((mode)) is deprecated");
7540 warning (OPT_Wattributes,
7541 "use __attribute__ ((vector_size)) instead");
7542 valid_mode = vector_mode_valid_p (mode);
7543 break;
7545 default:
7546 break;
7548 if (!valid_mode)
7550 error ("unable to emulate %qs", p);
7551 return NULL_TREE;
7554 if (POINTER_TYPE_P (type))
7556 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7557 tree (*fn)(tree, machine_mode, bool);
7559 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7561 error ("invalid pointer mode %qs", p);
7562 return NULL_TREE;
7565 if (TREE_CODE (type) == POINTER_TYPE)
7566 fn = build_pointer_type_for_mode;
7567 else
7568 fn = build_reference_type_for_mode;
7569 typefm = fn (TREE_TYPE (type), mode, false);
7571 else
7573 /* For fixed-point modes, we need to test if the signness of type
7574 and the machine mode are consistent. */
7575 if (ALL_FIXED_POINT_MODE_P (mode)
7576 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7578 error ("signedness of type and machine mode %qs don%'t match", p);
7579 return NULL_TREE;
7581 /* For fixed-point modes, we need to pass saturating info. */
7582 typefm = lang_hooks.types.type_for_mode (mode,
7583 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7584 : TYPE_UNSIGNED (type));
7587 if (typefm == NULL_TREE)
7589 error ("no data type for mode %qs", p);
7590 return NULL_TREE;
7592 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7594 /* For enumeral types, copy the precision from the integer
7595 type returned above. If not an INTEGER_TYPE, we can't use
7596 this mode for this type. */
7597 if (TREE_CODE (typefm) != INTEGER_TYPE)
7599 error ("cannot use mode %qs for enumeral types", p);
7600 return NULL_TREE;
7603 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7605 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7606 typefm = type;
7608 else
7610 /* We cannot build a type variant, as there's code that assumes
7611 that TYPE_MAIN_VARIANT has the same mode. This includes the
7612 debug generators. Instead, create a subrange type. This
7613 results in all of the enumeral values being emitted only once
7614 in the original, and the subtype gets them by reference. */
7615 if (TYPE_UNSIGNED (type))
7616 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7617 else
7618 typefm = make_signed_type (TYPE_PRECISION (typefm));
7619 TREE_TYPE (typefm) = type;
7622 else if (VECTOR_MODE_P (mode)
7623 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7624 : TREE_CODE (type) != TREE_CODE (typefm))
7626 error ("mode %qs applied to inappropriate type", p);
7627 return NULL_TREE;
7630 *node = typefm;
7633 return NULL_TREE;
7636 /* Handle a "section" attribute; arguments as in
7637 struct attribute_spec.handler. */
7639 static tree
7640 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7641 int ARG_UNUSED (flags), bool *no_add_attrs)
7643 tree decl = *node;
7645 if (!targetm_common.have_named_sections)
7647 error_at (DECL_SOURCE_LOCATION (*node),
7648 "section attributes are not supported for this target");
7649 goto fail;
7652 user_defined_section_attribute = true;
7654 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
7656 error ("section attribute not allowed for %q+D", *node);
7657 goto fail;
7660 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7662 error ("section attribute argument not a string constant");
7663 goto fail;
7666 if (TREE_CODE (decl) == VAR_DECL
7667 && current_function_decl != NULL_TREE
7668 && !TREE_STATIC (decl))
7670 error_at (DECL_SOURCE_LOCATION (decl),
7671 "section attribute cannot be specified for local variables");
7672 goto fail;
7675 /* The decl may have already been given a section attribute
7676 from a previous declaration. Ensure they match. */
7677 if (DECL_SECTION_NAME (decl) != NULL
7678 && strcmp (DECL_SECTION_NAME (decl),
7679 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7681 error ("section of %q+D conflicts with previous declaration", *node);
7682 goto fail;
7685 if (TREE_CODE (decl) == VAR_DECL
7686 && !targetm.have_tls && targetm.emutls.tmpl_section
7687 && DECL_THREAD_LOCAL_P (decl))
7689 error ("section of %q+D cannot be overridden", *node);
7690 goto fail;
7693 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7694 return NULL_TREE;
7696 fail:
7697 *no_add_attrs = true;
7698 return NULL_TREE;
7701 /* Check whether ALIGN is a valid user-specified alignment. If so,
7702 return its base-2 log; if not, output an error and return -1. If
7703 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7704 no error. */
7706 check_user_alignment (const_tree align, bool allow_zero)
7708 int i;
7710 if (error_operand_p (align))
7711 return -1;
7712 if (TREE_CODE (align) != INTEGER_CST
7713 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7715 error ("requested alignment is not an integer constant");
7716 return -1;
7718 else if (allow_zero && integer_zerop (align))
7719 return -1;
7720 else if (tree_int_cst_sgn (align) == -1
7721 || (i = tree_log2 (align)) == -1)
7723 error ("requested alignment is not a positive power of 2");
7724 return -1;
7726 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7728 error ("requested alignment is too large");
7729 return -1;
7731 return i;
7735 If in c++-11, check if the c++-11 alignment constraint with respect
7736 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7737 c++-11 mode, does nothing.
7739 [dcl.align]2/ says:
7741 [* if the constant expression evaluates to a fundamental alignment,
7742 the alignment requirement of the declared entity shall be the
7743 specified fundamental alignment.
7745 * if the constant expression evaluates to an extended alignment
7746 and the implementation supports that alignment in the context
7747 of the declaration, the alignment of the declared entity shall
7748 be that alignment
7750 * if the constant expression evaluates to an extended alignment
7751 and the implementation does not support that alignment in the
7752 context of the declaration, the program is ill-formed]. */
7754 static bool
7755 check_cxx_fundamental_alignment_constraints (tree node,
7756 unsigned align_log,
7757 int flags)
7759 bool alignment_too_large_p = false;
7760 unsigned requested_alignment = 1U << align_log;
7761 unsigned max_align = 0;
7763 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7764 || (node == NULL_TREE || node == error_mark_node))
7765 return true;
7767 if (cxx_fundamental_alignment_p (requested_alignment))
7768 return true;
7770 if (DECL_P (node))
7772 if (TREE_STATIC (node))
7774 /* For file scope variables and static members, the target
7775 supports alignments that are at most
7776 MAX_OFILE_ALIGNMENT. */
7777 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7778 alignment_too_large_p = true;
7780 else
7782 #ifdef BIGGEST_FIELD_ALIGNMENT
7783 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7784 #else
7785 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7786 #endif
7787 /* For non-static members, the target supports either
7788 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7789 if it is defined or BIGGEST_ALIGNMENT. */
7790 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7791 if (TREE_CODE (node) == FIELD_DECL
7792 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7793 alignment_too_large_p = true;
7794 #undef MAX_TARGET_FIELD_ALIGNMENT
7795 /* For stack variables, the target supports at most
7796 MAX_STACK_ALIGNMENT. */
7797 else if (decl_function_context (node) != NULL
7798 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7799 alignment_too_large_p = true;
7802 else if (TYPE_P (node))
7804 /* Let's be liberal for types. */
7805 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7806 alignment_too_large_p = true;
7809 if (alignment_too_large_p)
7810 pedwarn (input_location, OPT_Wattributes,
7811 "requested alignment %d is larger than %d",
7812 requested_alignment, max_align);
7814 return !alignment_too_large_p;
7817 /* Handle a "aligned" attribute; arguments as in
7818 struct attribute_spec.handler. */
7820 static tree
7821 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7822 int flags, bool *no_add_attrs)
7824 tree decl = NULL_TREE;
7825 tree *type = NULL;
7826 int is_type = 0;
7827 tree align_expr;
7828 int i;
7830 if (args)
7832 align_expr = TREE_VALUE (args);
7833 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7834 && TREE_CODE (align_expr) != FUNCTION_DECL)
7835 align_expr = default_conversion (align_expr);
7837 else
7838 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7840 if (DECL_P (*node))
7842 decl = *node;
7843 type = &TREE_TYPE (decl);
7844 is_type = TREE_CODE (*node) == TYPE_DECL;
7846 else if (TYPE_P (*node))
7847 type = node, is_type = 1;
7849 if ((i = check_user_alignment (align_expr, false)) == -1
7850 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7851 *no_add_attrs = true;
7852 else if (is_type)
7854 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7855 /* OK, modify the type in place. */;
7856 /* If we have a TYPE_DECL, then copy the type, so that we
7857 don't accidentally modify a builtin type. See pushdecl. */
7858 else if (decl && TREE_TYPE (decl) != error_mark_node
7859 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7861 tree tt = TREE_TYPE (decl);
7862 *type = build_variant_type_copy (*type);
7863 DECL_ORIGINAL_TYPE (decl) = tt;
7864 TYPE_NAME (*type) = decl;
7865 TREE_USED (*type) = TREE_USED (decl);
7866 TREE_TYPE (decl) = *type;
7868 else
7869 *type = build_variant_type_copy (*type);
7871 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7872 TYPE_USER_ALIGN (*type) = 1;
7874 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7875 && TREE_CODE (decl) != FIELD_DECL)
7877 error ("alignment may not be specified for %q+D", decl);
7878 *no_add_attrs = true;
7880 else if (DECL_USER_ALIGN (decl)
7881 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7882 /* C++-11 [dcl.align/4]:
7884 When multiple alignment-specifiers are specified for an
7885 entity, the alignment requirement shall be set to the
7886 strictest specified alignment.
7888 This formally comes from the c++11 specification but we are
7889 doing it for the GNU attribute syntax as well. */
7890 *no_add_attrs = true;
7891 else if (TREE_CODE (decl) == FUNCTION_DECL
7892 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7894 if (DECL_USER_ALIGN (decl))
7895 error ("alignment for %q+D was previously specified as %d "
7896 "and may not be decreased", decl,
7897 DECL_ALIGN (decl) / BITS_PER_UNIT);
7898 else
7899 error ("alignment for %q+D must be at least %d", decl,
7900 DECL_ALIGN (decl) / BITS_PER_UNIT);
7901 *no_add_attrs = true;
7903 else
7905 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7906 DECL_USER_ALIGN (decl) = 1;
7909 return NULL_TREE;
7912 /* Handle a "weak" attribute; arguments as in
7913 struct attribute_spec.handler. */
7915 static tree
7916 handle_weak_attribute (tree *node, tree name,
7917 tree ARG_UNUSED (args),
7918 int ARG_UNUSED (flags),
7919 bool * ARG_UNUSED (no_add_attrs))
7921 if (TREE_CODE (*node) == FUNCTION_DECL
7922 && DECL_DECLARED_INLINE_P (*node))
7924 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7925 *no_add_attrs = true;
7927 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7929 error ("indirect function %q+D cannot be declared weak", *node);
7930 *no_add_attrs = true;
7931 return NULL_TREE;
7933 else if (TREE_CODE (*node) == FUNCTION_DECL
7934 || TREE_CODE (*node) == VAR_DECL)
7936 struct symtab_node *n = symtab_node::get (*node);
7937 if (n && n->refuse_visibility_changes)
7938 error ("%+D declared weak after being used", *node);
7939 declare_weak (*node);
7941 else
7942 warning (OPT_Wattributes, "%qE attribute ignored", name);
7944 return NULL_TREE;
7947 /* Handle an "alias" or "ifunc" attribute; arguments as in
7948 struct attribute_spec.handler, except that IS_ALIAS tells us
7949 whether this is an alias as opposed to ifunc attribute. */
7951 static tree
7952 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7953 bool *no_add_attrs)
7955 tree decl = *node;
7957 if (TREE_CODE (decl) != FUNCTION_DECL
7958 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7960 warning (OPT_Wattributes, "%qE attribute ignored", name);
7961 *no_add_attrs = true;
7963 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7964 || (TREE_CODE (decl) != FUNCTION_DECL
7965 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7966 /* A static variable declaration is always a tentative definition,
7967 but the alias is a non-tentative definition which overrides. */
7968 || (TREE_CODE (decl) != FUNCTION_DECL
7969 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7971 error ("%q+D defined both normally and as %qE attribute", decl, name);
7972 *no_add_attrs = true;
7973 return NULL_TREE;
7975 else if (!is_alias
7976 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7977 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7979 error ("weak %q+D cannot be defined %qE", decl, name);
7980 *no_add_attrs = true;
7981 return NULL_TREE;
7984 /* Note that the very first time we process a nested declaration,
7985 decl_function_context will not be set. Indeed, *would* never
7986 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7987 we do below. After such frobbery, pushdecl would set the context.
7988 In any case, this is never what we want. */
7989 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7991 tree id;
7993 id = TREE_VALUE (args);
7994 if (TREE_CODE (id) != STRING_CST)
7996 error ("attribute %qE argument not a string", name);
7997 *no_add_attrs = true;
7998 return NULL_TREE;
8000 id = get_identifier (TREE_STRING_POINTER (id));
8001 /* This counts as a use of the object pointed to. */
8002 TREE_USED (id) = 1;
8004 if (TREE_CODE (decl) == FUNCTION_DECL)
8005 DECL_INITIAL (decl) = error_mark_node;
8006 else
8007 TREE_STATIC (decl) = 1;
8009 if (!is_alias)
8010 /* ifuncs are also aliases, so set that attribute too. */
8011 DECL_ATTRIBUTES (decl)
8012 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8014 else
8016 warning (OPT_Wattributes, "%qE attribute ignored", name);
8017 *no_add_attrs = true;
8020 if (decl_in_symtab_p (*node))
8022 struct symtab_node *n = symtab_node::get (decl);
8023 if (n && n->refuse_visibility_changes)
8025 if (is_alias)
8026 error ("%+D declared alias after being used", decl);
8027 else
8028 error ("%+D declared ifunc after being used", decl);
8033 return NULL_TREE;
8036 /* Handle an "alias" or "ifunc" attribute; arguments as in
8037 struct attribute_spec.handler. */
8039 static tree
8040 handle_ifunc_attribute (tree *node, tree name, tree args,
8041 int ARG_UNUSED (flags), bool *no_add_attrs)
8043 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8046 /* Handle an "alias" or "ifunc" attribute; arguments as in
8047 struct attribute_spec.handler. */
8049 static tree
8050 handle_alias_attribute (tree *node, tree name, tree args,
8051 int ARG_UNUSED (flags), bool *no_add_attrs)
8053 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8056 /* Handle a "weakref" attribute; arguments as in struct
8057 attribute_spec.handler. */
8059 static tree
8060 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8061 int flags, bool *no_add_attrs)
8063 tree attr = NULL_TREE;
8065 /* We must ignore the attribute when it is associated with
8066 local-scoped decls, since attribute alias is ignored and many
8067 such symbols do not even have a DECL_WEAK field. */
8068 if (decl_function_context (*node)
8069 || current_function_decl
8070 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
8072 warning (OPT_Wattributes, "%qE attribute ignored", name);
8073 *no_add_attrs = true;
8074 return NULL_TREE;
8077 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8079 error ("indirect function %q+D cannot be declared weakref", *node);
8080 *no_add_attrs = true;
8081 return NULL_TREE;
8084 /* The idea here is that `weakref("name")' mutates into `weakref,
8085 alias("name")', and weakref without arguments, in turn,
8086 implicitly adds weak. */
8088 if (args)
8090 attr = tree_cons (get_identifier ("alias"), args, attr);
8091 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8093 *no_add_attrs = true;
8095 decl_attributes (node, attr, flags);
8097 else
8099 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8100 error_at (DECL_SOURCE_LOCATION (*node),
8101 "weakref attribute must appear before alias attribute");
8103 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8104 and that isn't supported; and because it wants to add it to
8105 the list of weak decls, which isn't helpful. */
8106 DECL_WEAK (*node) = 1;
8109 if (decl_in_symtab_p (*node))
8111 struct symtab_node *n = symtab_node::get (*node);
8112 if (n && n->refuse_visibility_changes)
8113 error ("%+D declared weakref after being used", *node);
8116 return NULL_TREE;
8119 /* Handle an "visibility" attribute; arguments as in
8120 struct attribute_spec.handler. */
8122 static tree
8123 handle_visibility_attribute (tree *node, tree name, tree args,
8124 int ARG_UNUSED (flags),
8125 bool *ARG_UNUSED (no_add_attrs))
8127 tree decl = *node;
8128 tree id = TREE_VALUE (args);
8129 enum symbol_visibility vis;
8131 if (TYPE_P (*node))
8133 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8134 /* OK */;
8135 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8137 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8138 name);
8139 return NULL_TREE;
8141 else if (TYPE_FIELDS (*node))
8143 error ("%qE attribute ignored because %qT is already defined",
8144 name, *node);
8145 return NULL_TREE;
8148 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8150 warning (OPT_Wattributes, "%qE attribute ignored", name);
8151 return NULL_TREE;
8154 if (TREE_CODE (id) != STRING_CST)
8156 error ("visibility argument not a string");
8157 return NULL_TREE;
8160 /* If this is a type, set the visibility on the type decl. */
8161 if (TYPE_P (decl))
8163 decl = TYPE_NAME (decl);
8164 if (!decl)
8165 return NULL_TREE;
8166 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8168 warning (OPT_Wattributes, "%qE attribute ignored on types",
8169 name);
8170 return NULL_TREE;
8174 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8175 vis = VISIBILITY_DEFAULT;
8176 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8177 vis = VISIBILITY_INTERNAL;
8178 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8179 vis = VISIBILITY_HIDDEN;
8180 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8181 vis = VISIBILITY_PROTECTED;
8182 else
8184 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8185 vis = VISIBILITY_DEFAULT;
8188 if (DECL_VISIBILITY_SPECIFIED (decl)
8189 && vis != DECL_VISIBILITY (decl))
8191 tree attributes = (TYPE_P (*node)
8192 ? TYPE_ATTRIBUTES (*node)
8193 : DECL_ATTRIBUTES (decl));
8194 if (lookup_attribute ("visibility", attributes))
8195 error ("%qD redeclared with different visibility", decl);
8196 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8197 && lookup_attribute ("dllimport", attributes))
8198 error ("%qD was declared %qs which implies default visibility",
8199 decl, "dllimport");
8200 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8201 && lookup_attribute ("dllexport", attributes))
8202 error ("%qD was declared %qs which implies default visibility",
8203 decl, "dllexport");
8206 DECL_VISIBILITY (decl) = vis;
8207 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8209 /* Go ahead and attach the attribute to the node as well. This is needed
8210 so we can determine whether we have VISIBILITY_DEFAULT because the
8211 visibility was not specified, or because it was explicitly overridden
8212 from the containing scope. */
8214 return NULL_TREE;
8217 /* Determine the ELF symbol visibility for DECL, which is either a
8218 variable or a function. It is an error to use this function if a
8219 definition of DECL is not available in this translation unit.
8220 Returns true if the final visibility has been determined by this
8221 function; false if the caller is free to make additional
8222 modifications. */
8224 bool
8225 c_determine_visibility (tree decl)
8227 gcc_assert (TREE_CODE (decl) == VAR_DECL
8228 || TREE_CODE (decl) == FUNCTION_DECL);
8230 /* If the user explicitly specified the visibility with an
8231 attribute, honor that. DECL_VISIBILITY will have been set during
8232 the processing of the attribute. We check for an explicit
8233 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8234 to distinguish the use of an attribute from the use of a "#pragma
8235 GCC visibility push(...)"; in the latter case we still want other
8236 considerations to be able to overrule the #pragma. */
8237 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8238 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8239 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8240 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8241 return true;
8243 /* Set default visibility to whatever the user supplied with
8244 visibility_specified depending on #pragma GCC visibility. */
8245 if (!DECL_VISIBILITY_SPECIFIED (decl))
8247 if (visibility_options.inpragma
8248 || DECL_VISIBILITY (decl) != default_visibility)
8250 DECL_VISIBILITY (decl) = default_visibility;
8251 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8252 /* If visibility changed and DECL already has DECL_RTL, ensure
8253 symbol flags are updated. */
8254 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8255 || TREE_CODE (decl) == FUNCTION_DECL)
8256 && DECL_RTL_SET_P (decl))
8257 make_decl_rtl (decl);
8260 return false;
8263 /* Handle an "tls_model" attribute; arguments as in
8264 struct attribute_spec.handler. */
8266 static tree
8267 handle_tls_model_attribute (tree *node, tree name, tree args,
8268 int ARG_UNUSED (flags), bool *no_add_attrs)
8270 tree id;
8271 tree decl = *node;
8272 enum tls_model kind;
8274 *no_add_attrs = true;
8276 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8278 warning (OPT_Wattributes, "%qE attribute ignored", name);
8279 return NULL_TREE;
8282 kind = DECL_TLS_MODEL (decl);
8283 id = TREE_VALUE (args);
8284 if (TREE_CODE (id) != STRING_CST)
8286 error ("tls_model argument not a string");
8287 return NULL_TREE;
8290 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8291 kind = TLS_MODEL_LOCAL_EXEC;
8292 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8293 kind = TLS_MODEL_INITIAL_EXEC;
8294 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8295 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8296 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8297 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8298 else
8299 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8301 set_decl_tls_model (decl, kind);
8302 return NULL_TREE;
8305 /* Handle a "no_instrument_function" attribute; arguments as in
8306 struct attribute_spec.handler. */
8308 static tree
8309 handle_no_instrument_function_attribute (tree *node, tree name,
8310 tree ARG_UNUSED (args),
8311 int ARG_UNUSED (flags),
8312 bool *no_add_attrs)
8314 tree decl = *node;
8316 if (TREE_CODE (decl) != FUNCTION_DECL)
8318 error_at (DECL_SOURCE_LOCATION (decl),
8319 "%qE attribute applies only to functions", name);
8320 *no_add_attrs = true;
8322 else
8323 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8325 return NULL_TREE;
8328 /* Handle a "malloc" attribute; arguments as in
8329 struct attribute_spec.handler. */
8331 static tree
8332 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8333 int ARG_UNUSED (flags), bool *no_add_attrs)
8335 if (TREE_CODE (*node) == FUNCTION_DECL
8336 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8337 DECL_IS_MALLOC (*node) = 1;
8338 else
8340 warning (OPT_Wattributes, "%qE attribute ignored", name);
8341 *no_add_attrs = true;
8344 return NULL_TREE;
8347 /* Handle a "alloc_size" attribute; arguments as in
8348 struct attribute_spec.handler. */
8350 static tree
8351 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8352 int ARG_UNUSED (flags), bool *no_add_attrs)
8354 unsigned arg_count = type_num_arguments (*node);
8355 for (; args; args = TREE_CHAIN (args))
8357 tree position = TREE_VALUE (args);
8358 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8359 && TREE_CODE (position) != FUNCTION_DECL)
8360 position = default_conversion (position);
8362 if (!tree_fits_uhwi_p (position)
8363 || !arg_count
8364 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8366 warning (OPT_Wattributes,
8367 "alloc_size parameter outside range");
8368 *no_add_attrs = true;
8369 return NULL_TREE;
8372 return NULL_TREE;
8375 /* Handle a "alloc_align" attribute; arguments as in
8376 struct attribute_spec.handler. */
8378 static tree
8379 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8380 bool *no_add_attrs)
8382 unsigned arg_count = type_num_arguments (*node);
8383 tree position = TREE_VALUE (args);
8384 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8385 position = default_conversion (position);
8387 if (!tree_fits_uhwi_p (position)
8388 || !arg_count
8389 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8391 warning (OPT_Wattributes,
8392 "alloc_align parameter outside range");
8393 *no_add_attrs = true;
8394 return NULL_TREE;
8396 return NULL_TREE;
8399 /* Handle a "assume_aligned" attribute; arguments as in
8400 struct attribute_spec.handler. */
8402 static tree
8403 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8404 bool *no_add_attrs)
8406 for (; args; args = TREE_CHAIN (args))
8408 tree position = TREE_VALUE (args);
8409 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8410 && TREE_CODE (position) != FUNCTION_DECL)
8411 position = default_conversion (position);
8413 if (TREE_CODE (position) != INTEGER_CST)
8415 warning (OPT_Wattributes,
8416 "assume_aligned parameter not integer constant");
8417 *no_add_attrs = true;
8418 return NULL_TREE;
8421 return NULL_TREE;
8424 /* Handle a "fn spec" attribute; arguments as in
8425 struct attribute_spec.handler. */
8427 static tree
8428 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8429 tree args, int ARG_UNUSED (flags),
8430 bool *no_add_attrs ATTRIBUTE_UNUSED)
8432 gcc_assert (args
8433 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8434 && !TREE_CHAIN (args));
8435 return NULL_TREE;
8438 /* Handle a "bnd_variable_size" attribute; arguments as in
8439 struct attribute_spec.handler. */
8441 static tree
8442 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8443 int ARG_UNUSED (flags), bool *no_add_attrs)
8445 if (TREE_CODE (*node) != FIELD_DECL)
8447 warning (OPT_Wattributes, "%qE attribute ignored", name);
8448 *no_add_attrs = true;
8451 return NULL_TREE;
8454 /* Handle a "bnd_legacy" attribute; arguments as in
8455 struct attribute_spec.handler. */
8457 static tree
8458 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8459 int ARG_UNUSED (flags), bool *no_add_attrs)
8461 if (TREE_CODE (*node) != FUNCTION_DECL)
8463 warning (OPT_Wattributes, "%qE attribute ignored", name);
8464 *no_add_attrs = true;
8467 return NULL_TREE;
8470 /* Handle a "bnd_instrument" attribute; arguments as in
8471 struct attribute_spec.handler. */
8473 static tree
8474 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8475 int ARG_UNUSED (flags), bool *no_add_attrs)
8477 if (TREE_CODE (*node) != FUNCTION_DECL)
8479 warning (OPT_Wattributes, "%qE attribute ignored", name);
8480 *no_add_attrs = true;
8483 return NULL_TREE;
8486 /* Handle a "warn_unused" attribute; arguments as in
8487 struct attribute_spec.handler. */
8489 static tree
8490 handle_warn_unused_attribute (tree *node, tree name,
8491 tree args ATTRIBUTE_UNUSED,
8492 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8494 if (TYPE_P (*node))
8495 /* Do nothing else, just set the attribute. We'll get at
8496 it later with lookup_attribute. */
8498 else
8500 warning (OPT_Wattributes, "%qE attribute ignored", name);
8501 *no_add_attrs = true;
8504 return NULL_TREE;
8507 /* Handle an "omp declare simd" attribute; arguments as in
8508 struct attribute_spec.handler. */
8510 static tree
8511 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8513 return NULL_TREE;
8516 /* Handle an "omp declare target" attribute; arguments as in
8517 struct attribute_spec.handler. */
8519 static tree
8520 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8522 return NULL_TREE;
8525 /* Handle a "returns_twice" attribute; arguments as in
8526 struct attribute_spec.handler. */
8528 static tree
8529 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8530 int ARG_UNUSED (flags), bool *no_add_attrs)
8532 if (TREE_CODE (*node) == FUNCTION_DECL)
8533 DECL_IS_RETURNS_TWICE (*node) = 1;
8534 else
8536 warning (OPT_Wattributes, "%qE attribute ignored", name);
8537 *no_add_attrs = true;
8540 return NULL_TREE;
8543 /* Handle a "no_limit_stack" attribute; arguments as in
8544 struct attribute_spec.handler. */
8546 static tree
8547 handle_no_limit_stack_attribute (tree *node, tree name,
8548 tree ARG_UNUSED (args),
8549 int ARG_UNUSED (flags),
8550 bool *no_add_attrs)
8552 tree decl = *node;
8554 if (TREE_CODE (decl) != FUNCTION_DECL)
8556 error_at (DECL_SOURCE_LOCATION (decl),
8557 "%qE attribute applies only to functions", name);
8558 *no_add_attrs = true;
8560 else if (DECL_INITIAL (decl))
8562 error_at (DECL_SOURCE_LOCATION (decl),
8563 "can%'t set %qE attribute after definition", name);
8564 *no_add_attrs = true;
8566 else
8567 DECL_NO_LIMIT_STACK (decl) = 1;
8569 return NULL_TREE;
8572 /* Handle a "pure" attribute; arguments as in
8573 struct attribute_spec.handler. */
8575 static tree
8576 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8577 int ARG_UNUSED (flags), bool *no_add_attrs)
8579 if (TREE_CODE (*node) == FUNCTION_DECL)
8580 DECL_PURE_P (*node) = 1;
8581 /* ??? TODO: Support types. */
8582 else
8584 warning (OPT_Wattributes, "%qE attribute ignored", name);
8585 *no_add_attrs = true;
8588 return NULL_TREE;
8591 /* Digest an attribute list destined for a transactional memory statement.
8592 ALLOWED is the set of attributes that are allowed for this statement;
8593 return the attribute we parsed. Multiple attributes are never allowed. */
8596 parse_tm_stmt_attr (tree attrs, int allowed)
8598 tree a_seen = NULL;
8599 int m_seen = 0;
8601 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8603 tree a = TREE_PURPOSE (attrs);
8604 int m = 0;
8606 if (is_attribute_p ("outer", a))
8607 m = TM_STMT_ATTR_OUTER;
8609 if ((m & allowed) == 0)
8611 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8612 continue;
8615 if (m_seen == 0)
8617 a_seen = a;
8618 m_seen = m;
8620 else if (m_seen == m)
8621 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8622 else
8623 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8626 return m_seen;
8629 /* Transform a TM attribute name into a maskable integer and back.
8630 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8631 to how the lack of an attribute is treated. */
8634 tm_attr_to_mask (tree attr)
8636 if (attr == NULL)
8637 return 0;
8638 if (is_attribute_p ("transaction_safe", attr))
8639 return TM_ATTR_SAFE;
8640 if (is_attribute_p ("transaction_callable", attr))
8641 return TM_ATTR_CALLABLE;
8642 if (is_attribute_p ("transaction_pure", attr))
8643 return TM_ATTR_PURE;
8644 if (is_attribute_p ("transaction_unsafe", attr))
8645 return TM_ATTR_IRREVOCABLE;
8646 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8647 return TM_ATTR_MAY_CANCEL_OUTER;
8648 return 0;
8651 tree
8652 tm_mask_to_attr (int mask)
8654 const char *str;
8655 switch (mask)
8657 case TM_ATTR_SAFE:
8658 str = "transaction_safe";
8659 break;
8660 case TM_ATTR_CALLABLE:
8661 str = "transaction_callable";
8662 break;
8663 case TM_ATTR_PURE:
8664 str = "transaction_pure";
8665 break;
8666 case TM_ATTR_IRREVOCABLE:
8667 str = "transaction_unsafe";
8668 break;
8669 case TM_ATTR_MAY_CANCEL_OUTER:
8670 str = "transaction_may_cancel_outer";
8671 break;
8672 default:
8673 gcc_unreachable ();
8675 return get_identifier (str);
8678 /* Return the first TM attribute seen in LIST. */
8680 tree
8681 find_tm_attribute (tree list)
8683 for (; list ; list = TREE_CHAIN (list))
8685 tree name = TREE_PURPOSE (list);
8686 if (tm_attr_to_mask (name) != 0)
8687 return name;
8689 return NULL_TREE;
8692 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8693 Here we accept only function types, and verify that none of the other
8694 function TM attributes are also applied. */
8695 /* ??? We need to accept class types for C++, but not C. This greatly
8696 complicates this function, since we can no longer rely on the extra
8697 processing given by function_type_required. */
8699 static tree
8700 handle_tm_attribute (tree *node, tree name, tree args,
8701 int flags, bool *no_add_attrs)
8703 /* Only one path adds the attribute; others don't. */
8704 *no_add_attrs = true;
8706 switch (TREE_CODE (*node))
8708 case RECORD_TYPE:
8709 case UNION_TYPE:
8710 /* Only tm_callable and tm_safe apply to classes. */
8711 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8712 goto ignored;
8713 /* FALLTHRU */
8715 case FUNCTION_TYPE:
8716 case METHOD_TYPE:
8718 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8719 if (old_name == name)
8721 else if (old_name != NULL_TREE)
8722 error ("type was previously declared %qE", old_name);
8723 else
8724 *no_add_attrs = false;
8726 break;
8728 case POINTER_TYPE:
8730 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8731 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8733 tree fn_tmp = TREE_TYPE (*node);
8734 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8735 *node = build_pointer_type (fn_tmp);
8736 break;
8739 /* FALLTHRU */
8741 default:
8742 /* If a function is next, pass it on to be tried next. */
8743 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8744 return tree_cons (name, args, NULL);
8746 ignored:
8747 warning (OPT_Wattributes, "%qE attribute ignored", name);
8748 break;
8751 return NULL_TREE;
8754 /* Handle the TM_WRAP attribute; arguments as in
8755 struct attribute_spec.handler. */
8757 static tree
8758 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8759 int ARG_UNUSED (flags), bool *no_add_attrs)
8761 tree decl = *node;
8763 /* We don't need the attribute even on success, since we
8764 record the entry in an external table. */
8765 *no_add_attrs = true;
8767 if (TREE_CODE (decl) != FUNCTION_DECL)
8768 warning (OPT_Wattributes, "%qE attribute ignored", name);
8769 else
8771 tree wrap_decl = TREE_VALUE (args);
8772 if (error_operand_p (wrap_decl))
8774 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8775 && TREE_CODE (wrap_decl) != VAR_DECL
8776 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8777 error ("%qE argument not an identifier", name);
8778 else
8780 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8781 wrap_decl = lookup_name (wrap_decl);
8782 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8784 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8785 TREE_TYPE (wrap_decl)))
8786 record_tm_replacement (wrap_decl, decl);
8787 else
8788 error ("%qD is not compatible with %qD", wrap_decl, decl);
8790 else
8791 error ("%qE argument is not a function", name);
8795 return NULL_TREE;
8798 /* Ignore the given attribute. Used when this attribute may be usefully
8799 overridden by the target, but is not used generically. */
8801 static tree
8802 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8803 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8804 bool *no_add_attrs)
8806 *no_add_attrs = true;
8807 return NULL_TREE;
8810 /* Handle a "no vops" attribute; arguments as in
8811 struct attribute_spec.handler. */
8813 static tree
8814 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8815 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8816 bool *ARG_UNUSED (no_add_attrs))
8818 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8819 DECL_IS_NOVOPS (*node) = 1;
8820 return NULL_TREE;
8823 /* Handle a "deprecated" attribute; arguments as in
8824 struct attribute_spec.handler. */
8826 static tree
8827 handle_deprecated_attribute (tree *node, tree name,
8828 tree args, int flags,
8829 bool *no_add_attrs)
8831 tree type = NULL_TREE;
8832 int warn = 0;
8833 tree what = NULL_TREE;
8835 if (!args)
8836 *no_add_attrs = true;
8837 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8839 error ("deprecated message is not a string");
8840 *no_add_attrs = true;
8843 if (DECL_P (*node))
8845 tree decl = *node;
8846 type = TREE_TYPE (decl);
8848 if (TREE_CODE (decl) == TYPE_DECL
8849 || TREE_CODE (decl) == PARM_DECL
8850 || TREE_CODE (decl) == VAR_DECL
8851 || TREE_CODE (decl) == FUNCTION_DECL
8852 || TREE_CODE (decl) == FIELD_DECL
8853 || objc_method_decl (TREE_CODE (decl)))
8854 TREE_DEPRECATED (decl) = 1;
8855 else
8856 warn = 1;
8858 else if (TYPE_P (*node))
8860 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8861 *node = build_variant_type_copy (*node);
8862 TREE_DEPRECATED (*node) = 1;
8863 type = *node;
8865 else
8866 warn = 1;
8868 if (warn)
8870 *no_add_attrs = true;
8871 if (type && TYPE_NAME (type))
8873 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8874 what = TYPE_NAME (*node);
8875 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8876 && DECL_NAME (TYPE_NAME (type)))
8877 what = DECL_NAME (TYPE_NAME (type));
8879 if (what)
8880 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8881 else
8882 warning (OPT_Wattributes, "%qE attribute ignored", name);
8885 return NULL_TREE;
8888 /* Handle a "vector_size" attribute; arguments as in
8889 struct attribute_spec.handler. */
8891 static tree
8892 handle_vector_size_attribute (tree *node, tree name, tree args,
8893 int ARG_UNUSED (flags),
8894 bool *no_add_attrs)
8896 unsigned HOST_WIDE_INT vecsize, nunits;
8897 machine_mode orig_mode;
8898 tree type = *node, new_type, size;
8900 *no_add_attrs = true;
8902 size = TREE_VALUE (args);
8903 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8904 && TREE_CODE (size) != FUNCTION_DECL)
8905 size = default_conversion (size);
8907 if (!tree_fits_uhwi_p (size))
8909 warning (OPT_Wattributes, "%qE attribute ignored", name);
8910 return NULL_TREE;
8913 /* Get the vector size (in bytes). */
8914 vecsize = tree_to_uhwi (size);
8916 /* We need to provide for vector pointers, vector arrays, and
8917 functions returning vectors. For example:
8919 __attribute__((vector_size(16))) short *foo;
8921 In this case, the mode is SI, but the type being modified is
8922 HI, so we need to look further. */
8924 while (POINTER_TYPE_P (type)
8925 || TREE_CODE (type) == FUNCTION_TYPE
8926 || TREE_CODE (type) == METHOD_TYPE
8927 || TREE_CODE (type) == ARRAY_TYPE
8928 || TREE_CODE (type) == OFFSET_TYPE)
8929 type = TREE_TYPE (type);
8931 /* Get the mode of the type being modified. */
8932 orig_mode = TYPE_MODE (type);
8934 if ((!INTEGRAL_TYPE_P (type)
8935 && !SCALAR_FLOAT_TYPE_P (type)
8936 && !FIXED_POINT_TYPE_P (type))
8937 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8938 && GET_MODE_CLASS (orig_mode) != MODE_INT
8939 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8940 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8941 || TREE_CODE (type) == BOOLEAN_TYPE)
8943 error ("invalid vector type for attribute %qE", name);
8944 return NULL_TREE;
8947 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8949 error ("vector size not an integral multiple of component size");
8950 return NULL;
8953 if (vecsize == 0)
8955 error ("zero vector size");
8956 return NULL;
8959 /* Calculate how many units fit in the vector. */
8960 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8961 if (nunits & (nunits - 1))
8963 error ("number of components of the vector not a power of two");
8964 return NULL_TREE;
8967 new_type = build_vector_type (type, nunits);
8969 /* Build back pointers if needed. */
8970 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8972 return NULL_TREE;
8975 /* Handle the "nonnull" attribute. */
8976 static tree
8977 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8978 tree args, int ARG_UNUSED (flags),
8979 bool *no_add_attrs)
8981 tree type = *node;
8982 unsigned HOST_WIDE_INT attr_arg_num;
8984 /* If no arguments are specified, all pointer arguments should be
8985 non-null. Verify a full prototype is given so that the arguments
8986 will have the correct types when we actually check them later. */
8987 if (!args)
8989 if (!prototype_p (type))
8991 error ("nonnull attribute without arguments on a non-prototype");
8992 *no_add_attrs = true;
8994 return NULL_TREE;
8997 /* Argument list specified. Verify that each argument number references
8998 a pointer argument. */
8999 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9001 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9003 tree arg = TREE_VALUE (args);
9004 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9005 && TREE_CODE (arg) != FUNCTION_DECL)
9006 arg = default_conversion (arg);
9008 if (!get_nonnull_operand (arg, &arg_num))
9010 error ("nonnull argument has invalid operand number (argument %lu)",
9011 (unsigned long) attr_arg_num);
9012 *no_add_attrs = true;
9013 return NULL_TREE;
9016 if (prototype_p (type))
9018 function_args_iterator iter;
9019 tree argument;
9021 function_args_iter_init (&iter, type);
9022 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9024 argument = function_args_iter_cond (&iter);
9025 if (argument == NULL_TREE || ck_num == arg_num)
9026 break;
9029 if (!argument
9030 || TREE_CODE (argument) == VOID_TYPE)
9032 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9033 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9034 *no_add_attrs = true;
9035 return NULL_TREE;
9038 if (TREE_CODE (argument) != POINTER_TYPE)
9040 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9041 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9042 *no_add_attrs = true;
9043 return NULL_TREE;
9048 return NULL_TREE;
9051 /* Check the argument list of a function call for null in argument slots
9052 that are marked as requiring a non-null pointer argument. The NARGS
9053 arguments are passed in the array ARGARRAY.
9056 static void
9057 check_function_nonnull (tree attrs, int nargs, tree *argarray)
9059 tree a;
9060 int i;
9062 attrs = lookup_attribute ("nonnull", attrs);
9063 if (attrs == NULL_TREE)
9064 return;
9066 a = attrs;
9067 /* See if any of the nonnull attributes has no arguments. If so,
9068 then every pointer argument is checked (in which case the check
9069 for pointer type is done in check_nonnull_arg). */
9070 if (TREE_VALUE (a) != NULL_TREE)
9072 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9073 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9075 if (a != NULL_TREE)
9076 for (i = 0; i < nargs; i++)
9077 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9078 i + 1);
9079 else
9081 /* Walk the argument list. If we encounter an argument number we
9082 should check for non-null, do it. */
9083 for (i = 0; i < nargs; i++)
9085 for (a = attrs; ; a = TREE_CHAIN (a))
9087 a = lookup_attribute ("nonnull", a);
9088 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9089 break;
9092 if (a != NULL_TREE)
9093 check_function_arguments_recurse (check_nonnull_arg, NULL,
9094 argarray[i], i + 1);
9099 /* Check that the Nth argument of a function call (counting backwards
9100 from the end) is a (pointer)0. The NARGS arguments are passed in the
9101 array ARGARRAY. */
9103 static void
9104 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9106 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9108 if (attr)
9110 int len = 0;
9111 int pos = 0;
9112 tree sentinel;
9113 function_args_iterator iter;
9114 tree t;
9116 /* Skip over the named arguments. */
9117 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9119 if (len == nargs)
9120 break;
9121 len++;
9124 if (TREE_VALUE (attr))
9126 tree p = TREE_VALUE (TREE_VALUE (attr));
9127 pos = TREE_INT_CST_LOW (p);
9130 /* The sentinel must be one of the varargs, i.e.
9131 in position >= the number of fixed arguments. */
9132 if ((nargs - 1 - pos) < len)
9134 warning (OPT_Wformat_,
9135 "not enough variable arguments to fit a sentinel");
9136 return;
9139 /* Validate the sentinel. */
9140 sentinel = argarray[nargs - 1 - pos];
9141 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9142 || !integer_zerop (sentinel))
9143 /* Although __null (in C++) is only an integer we allow it
9144 nevertheless, as we are guaranteed that it's exactly
9145 as wide as a pointer, and we don't want to force
9146 users to cast the NULL they have written there.
9147 We warn with -Wstrict-null-sentinel, though. */
9148 && (warn_strict_null_sentinel || null_node != sentinel))
9149 warning (OPT_Wformat_, "missing sentinel in function call");
9153 /* Helper for check_function_nonnull; given a list of operands which
9154 must be non-null in ARGS, determine if operand PARAM_NUM should be
9155 checked. */
9157 static bool
9158 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9160 unsigned HOST_WIDE_INT arg_num = 0;
9162 for (; args; args = TREE_CHAIN (args))
9164 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9166 gcc_assert (found);
9168 if (arg_num == param_num)
9169 return true;
9171 return false;
9174 /* Check that the function argument PARAM (which is operand number
9175 PARAM_NUM) is non-null. This is called by check_function_nonnull
9176 via check_function_arguments_recurse. */
9178 static void
9179 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9180 unsigned HOST_WIDE_INT param_num)
9182 /* Just skip checking the argument if it's not a pointer. This can
9183 happen if the "nonnull" attribute was given without an operand
9184 list (which means to check every pointer argument). */
9186 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9187 return;
9189 if (integer_zerop (param))
9190 warning (OPT_Wnonnull, "null argument where non-null required "
9191 "(argument %lu)", (unsigned long) param_num);
9194 /* Helper for nonnull attribute handling; fetch the operand number
9195 from the attribute argument list. */
9197 static bool
9198 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9200 /* Verify the arg number is a small constant. */
9201 if (tree_fits_uhwi_p (arg_num_expr))
9203 *valp = TREE_INT_CST_LOW (arg_num_expr);
9204 return true;
9206 else
9207 return false;
9210 /* Handle a "nothrow" attribute; arguments as in
9211 struct attribute_spec.handler. */
9213 static tree
9214 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9215 int ARG_UNUSED (flags), bool *no_add_attrs)
9217 if (TREE_CODE (*node) == FUNCTION_DECL)
9218 TREE_NOTHROW (*node) = 1;
9219 /* ??? TODO: Support types. */
9220 else
9222 warning (OPT_Wattributes, "%qE attribute ignored", name);
9223 *no_add_attrs = true;
9226 return NULL_TREE;
9229 /* Handle a "cleanup" attribute; arguments as in
9230 struct attribute_spec.handler. */
9232 static tree
9233 handle_cleanup_attribute (tree *node, tree name, tree args,
9234 int ARG_UNUSED (flags), bool *no_add_attrs)
9236 tree decl = *node;
9237 tree cleanup_id, cleanup_decl;
9239 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9240 for global destructors in C++. This requires infrastructure that
9241 we don't have generically at the moment. It's also not a feature
9242 we'd be missing too much, since we do have attribute constructor. */
9243 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9245 warning (OPT_Wattributes, "%qE attribute ignored", name);
9246 *no_add_attrs = true;
9247 return NULL_TREE;
9250 /* Verify that the argument is a function in scope. */
9251 /* ??? We could support pointers to functions here as well, if
9252 that was considered desirable. */
9253 cleanup_id = TREE_VALUE (args);
9254 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9256 error ("cleanup argument not an identifier");
9257 *no_add_attrs = true;
9258 return NULL_TREE;
9260 cleanup_decl = lookup_name (cleanup_id);
9261 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9263 error ("cleanup argument not a function");
9264 *no_add_attrs = true;
9265 return NULL_TREE;
9268 /* That the function has proper type is checked with the
9269 eventual call to build_function_call. */
9271 return NULL_TREE;
9274 /* Handle a "warn_unused_result" attribute. No special handling. */
9276 static tree
9277 handle_warn_unused_result_attribute (tree *node, tree name,
9278 tree ARG_UNUSED (args),
9279 int ARG_UNUSED (flags), bool *no_add_attrs)
9281 /* Ignore the attribute for functions not returning any value. */
9282 if (VOID_TYPE_P (TREE_TYPE (*node)))
9284 warning (OPT_Wattributes, "%qE attribute ignored", name);
9285 *no_add_attrs = true;
9288 return NULL_TREE;
9291 /* Handle a "sentinel" attribute. */
9293 static tree
9294 handle_sentinel_attribute (tree *node, tree name, tree args,
9295 int ARG_UNUSED (flags), bool *no_add_attrs)
9297 if (!prototype_p (*node))
9299 warning (OPT_Wattributes,
9300 "%qE attribute requires prototypes with named arguments", name);
9301 *no_add_attrs = true;
9303 else
9305 if (!stdarg_p (*node))
9307 warning (OPT_Wattributes,
9308 "%qE attribute only applies to variadic functions", name);
9309 *no_add_attrs = true;
9313 if (args)
9315 tree position = TREE_VALUE (args);
9316 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9317 && TREE_CODE (position) != FUNCTION_DECL)
9318 position = default_conversion (position);
9320 if (TREE_CODE (position) != INTEGER_CST
9321 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9323 warning (OPT_Wattributes,
9324 "requested position is not an integer constant");
9325 *no_add_attrs = true;
9327 else
9329 if (tree_int_cst_lt (position, integer_zero_node))
9331 warning (OPT_Wattributes,
9332 "requested position is less than zero");
9333 *no_add_attrs = true;
9338 return NULL_TREE;
9341 /* Handle a "type_generic" attribute. */
9343 static tree
9344 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9345 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9346 bool * ARG_UNUSED (no_add_attrs))
9348 /* Ensure we have a function type. */
9349 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9351 /* Ensure we have a variadic function. */
9352 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9354 return NULL_TREE;
9357 /* Handle a "target" attribute. */
9359 static tree
9360 handle_target_attribute (tree *node, tree name, tree args, int flags,
9361 bool *no_add_attrs)
9363 /* Ensure we have a function type. */
9364 if (TREE_CODE (*node) != FUNCTION_DECL)
9366 warning (OPT_Wattributes, "%qE attribute ignored", name);
9367 *no_add_attrs = true;
9369 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9370 flags))
9371 *no_add_attrs = true;
9373 return NULL_TREE;
9376 /* Arguments being collected for optimization. */
9377 typedef const char *const_char_p; /* For DEF_VEC_P. */
9378 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9381 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9382 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9383 false for #pragma GCC optimize. */
9385 bool
9386 parse_optimize_options (tree args, bool attr_p)
9388 bool ret = true;
9389 unsigned opt_argc;
9390 unsigned i;
9391 int saved_flag_strict_aliasing;
9392 const char **opt_argv;
9393 struct cl_decoded_option *decoded_options;
9394 unsigned int decoded_options_count;
9395 tree ap;
9397 /* Build up argv vector. Just in case the string is stored away, use garbage
9398 collected strings. */
9399 vec_safe_truncate (optimize_args, 0);
9400 vec_safe_push (optimize_args, (const char *) NULL);
9402 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9404 tree value = TREE_VALUE (ap);
9406 if (TREE_CODE (value) == INTEGER_CST)
9408 char buffer[20];
9409 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9410 vec_safe_push (optimize_args, ggc_strdup (buffer));
9413 else if (TREE_CODE (value) == STRING_CST)
9415 /* Split string into multiple substrings. */
9416 size_t len = TREE_STRING_LENGTH (value);
9417 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9418 char *end = p + len;
9419 char *comma;
9420 char *next_p = p;
9422 while (next_p != NULL)
9424 size_t len2;
9425 char *q, *r;
9427 p = next_p;
9428 comma = strchr (p, ',');
9429 if (comma)
9431 len2 = comma - p;
9432 *comma = '\0';
9433 next_p = comma+1;
9435 else
9437 len2 = end - p;
9438 next_p = NULL;
9441 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9443 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9444 options. */
9445 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9447 ret = false;
9448 if (attr_p)
9449 warning (OPT_Wattributes,
9450 "bad option %s to optimize attribute", p);
9451 else
9452 warning (OPT_Wpragmas,
9453 "bad option %s to pragma attribute", p);
9454 continue;
9457 if (*p != '-')
9459 *r++ = '-';
9461 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9462 itself is -Os, and any other switch begins with a -f. */
9463 if ((*p >= '0' && *p <= '9')
9464 || (p[0] == 's' && p[1] == '\0'))
9465 *r++ = 'O';
9466 else if (*p != 'O')
9467 *r++ = 'f';
9470 memcpy (r, p, len2);
9471 r[len2] = '\0';
9472 vec_safe_push (optimize_args, (const char *) q);
9478 opt_argc = optimize_args->length ();
9479 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9481 for (i = 1; i < opt_argc; i++)
9482 opt_argv[i] = (*optimize_args)[i];
9484 saved_flag_strict_aliasing = flag_strict_aliasing;
9486 /* Now parse the options. */
9487 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9488 &decoded_options,
9489 &decoded_options_count);
9490 decode_options (&global_options, &global_options_set,
9491 decoded_options, decoded_options_count,
9492 input_location, global_dc);
9494 targetm.override_options_after_change();
9496 /* Don't allow changing -fstrict-aliasing. */
9497 flag_strict_aliasing = saved_flag_strict_aliasing;
9499 optimize_args->truncate (0);
9500 return ret;
9503 /* For handling "optimize" attribute. arguments as in
9504 struct attribute_spec.handler. */
9506 static tree
9507 handle_optimize_attribute (tree *node, tree name, tree args,
9508 int ARG_UNUSED (flags), bool *no_add_attrs)
9510 /* Ensure we have a function type. */
9511 if (TREE_CODE (*node) != FUNCTION_DECL)
9513 warning (OPT_Wattributes, "%qE attribute ignored", name);
9514 *no_add_attrs = true;
9516 else
9518 struct cl_optimization cur_opts;
9519 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9521 /* Save current options. */
9522 cl_optimization_save (&cur_opts, &global_options);
9524 /* If we previously had some optimization options, use them as the
9525 default. */
9526 if (old_opts)
9527 cl_optimization_restore (&global_options,
9528 TREE_OPTIMIZATION (old_opts));
9530 /* Parse options, and update the vector. */
9531 parse_optimize_options (args, true);
9532 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9533 = build_optimization_node (&global_options);
9535 /* Restore current options. */
9536 cl_optimization_restore (&global_options, &cur_opts);
9539 return NULL_TREE;
9542 /* Handle a "no_split_stack" attribute. */
9544 static tree
9545 handle_no_split_stack_attribute (tree *node, tree name,
9546 tree ARG_UNUSED (args),
9547 int ARG_UNUSED (flags),
9548 bool *no_add_attrs)
9550 tree decl = *node;
9552 if (TREE_CODE (decl) != FUNCTION_DECL)
9554 error_at (DECL_SOURCE_LOCATION (decl),
9555 "%qE attribute applies only to functions", name);
9556 *no_add_attrs = true;
9558 else if (DECL_INITIAL (decl))
9560 error_at (DECL_SOURCE_LOCATION (decl),
9561 "can%'t set %qE attribute after definition", name);
9562 *no_add_attrs = true;
9565 return NULL_TREE;
9568 /* Handle a "returns_nonnull" attribute; arguments as in
9569 struct attribute_spec.handler. */
9571 static tree
9572 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9573 bool *no_add_attrs)
9575 // Even without a prototype we still have a return type we can check.
9576 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9578 error ("returns_nonnull attribute on a function not returning a pointer");
9579 *no_add_attrs = true;
9581 return NULL_TREE;
9584 /* Handle a "designated_init" attribute; arguments as in
9585 struct attribute_spec.handler. */
9587 static tree
9588 handle_designated_init_attribute (tree *node, tree name, tree, int,
9589 bool *no_add_attrs)
9591 if (TREE_CODE (*node) != RECORD_TYPE)
9593 error ("%qE attribute is only valid on %<struct%> type", name);
9594 *no_add_attrs = true;
9596 return NULL_TREE;
9600 /* Check for valid arguments being passed to a function with FNTYPE.
9601 There are NARGS arguments in the array ARGARRAY. */
9602 void
9603 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9605 /* Check for null being passed in a pointer argument that must be
9606 non-null. We also need to do this if format checking is enabled. */
9608 if (warn_nonnull)
9609 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9611 /* Check for errors in format strings. */
9613 if (warn_format || warn_suggest_attribute_format)
9614 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9616 if (warn_format)
9617 check_function_sentinel (fntype, nargs, argarray);
9620 /* Generic argument checking recursion routine. PARAM is the argument to
9621 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9622 once the argument is resolved. CTX is context for the callback. */
9623 void
9624 check_function_arguments_recurse (void (*callback)
9625 (void *, tree, unsigned HOST_WIDE_INT),
9626 void *ctx, tree param,
9627 unsigned HOST_WIDE_INT param_num)
9629 if (CONVERT_EXPR_P (param)
9630 && (TYPE_PRECISION (TREE_TYPE (param))
9631 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9633 /* Strip coercion. */
9634 check_function_arguments_recurse (callback, ctx,
9635 TREE_OPERAND (param, 0), param_num);
9636 return;
9639 if (TREE_CODE (param) == CALL_EXPR)
9641 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9642 tree attrs;
9643 bool found_format_arg = false;
9645 /* See if this is a call to a known internationalization function
9646 that modifies a format arg. Such a function may have multiple
9647 format_arg attributes (for example, ngettext). */
9649 for (attrs = TYPE_ATTRIBUTES (type);
9650 attrs;
9651 attrs = TREE_CHAIN (attrs))
9652 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9654 tree inner_arg;
9655 tree format_num_expr;
9656 int format_num;
9657 int i;
9658 call_expr_arg_iterator iter;
9660 /* Extract the argument number, which was previously checked
9661 to be valid. */
9662 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9664 format_num = tree_to_uhwi (format_num_expr);
9666 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9667 inner_arg != 0;
9668 inner_arg = next_call_expr_arg (&iter), i++)
9669 if (i == format_num)
9671 check_function_arguments_recurse (callback, ctx,
9672 inner_arg, param_num);
9673 found_format_arg = true;
9674 break;
9678 /* If we found a format_arg attribute and did a recursive check,
9679 we are done with checking this argument. Otherwise, we continue
9680 and this will be considered a non-literal. */
9681 if (found_format_arg)
9682 return;
9685 if (TREE_CODE (param) == COND_EXPR)
9687 /* Check both halves of the conditional expression. */
9688 check_function_arguments_recurse (callback, ctx,
9689 TREE_OPERAND (param, 1), param_num);
9690 check_function_arguments_recurse (callback, ctx,
9691 TREE_OPERAND (param, 2), param_num);
9692 return;
9695 (*callback) (ctx, param, param_num);
9698 /* Checks for a builtin function FNDECL that the number of arguments
9699 NARGS against the required number REQUIRED and issues an error if
9700 there is a mismatch. Returns true if the number of arguments is
9701 correct, otherwise false. */
9703 static bool
9704 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9706 if (nargs < required)
9708 error_at (input_location,
9709 "not enough arguments to function %qE", fndecl);
9710 return false;
9712 else if (nargs > required)
9714 error_at (input_location,
9715 "too many arguments to function %qE", fndecl);
9716 return false;
9718 return true;
9721 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9722 Returns false if there was an error, otherwise true. */
9724 bool
9725 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9727 if (!DECL_BUILT_IN (fndecl)
9728 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9729 return true;
9731 switch (DECL_FUNCTION_CODE (fndecl))
9733 case BUILT_IN_CONSTANT_P:
9734 return builtin_function_validate_nargs (fndecl, nargs, 1);
9736 case BUILT_IN_ISFINITE:
9737 case BUILT_IN_ISINF:
9738 case BUILT_IN_ISINF_SIGN:
9739 case BUILT_IN_ISNAN:
9740 case BUILT_IN_ISNORMAL:
9741 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9743 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9745 error ("non-floating-point argument in call to "
9746 "function %qE", fndecl);
9747 return false;
9749 return true;
9751 return false;
9753 case BUILT_IN_ISGREATER:
9754 case BUILT_IN_ISGREATEREQUAL:
9755 case BUILT_IN_ISLESS:
9756 case BUILT_IN_ISLESSEQUAL:
9757 case BUILT_IN_ISLESSGREATER:
9758 case BUILT_IN_ISUNORDERED:
9759 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9761 enum tree_code code0, code1;
9762 code0 = TREE_CODE (TREE_TYPE (args[0]));
9763 code1 = TREE_CODE (TREE_TYPE (args[1]));
9764 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9765 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9766 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9768 error ("non-floating-point arguments in call to "
9769 "function %qE", fndecl);
9770 return false;
9772 return true;
9774 return false;
9776 case BUILT_IN_FPCLASSIFY:
9777 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9779 unsigned i;
9781 for (i=0; i<5; i++)
9782 if (TREE_CODE (args[i]) != INTEGER_CST)
9784 error ("non-const integer argument %u in call to function %qE",
9785 i+1, fndecl);
9786 return false;
9789 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9791 error ("non-floating-point argument in call to function %qE",
9792 fndecl);
9793 return false;
9795 return true;
9797 return false;
9799 case BUILT_IN_ASSUME_ALIGNED:
9800 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9802 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9804 error ("non-integer argument 3 in call to function %qE", fndecl);
9805 return false;
9807 return true;
9809 return false;
9811 case BUILT_IN_ADD_OVERFLOW:
9812 case BUILT_IN_SUB_OVERFLOW:
9813 case BUILT_IN_MUL_OVERFLOW:
9814 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9816 unsigned i;
9817 for (i = 0; i < 2; i++)
9818 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9820 error ("argument %u in call to function %qE does not have "
9821 "integral type", i + 1, fndecl);
9822 return false;
9824 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9825 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9827 error ("argument 3 in call to function %qE does not have "
9828 "pointer to integer type", fndecl);
9829 return false;
9831 return true;
9833 return false;
9835 default:
9836 return true;
9840 /* Function to help qsort sort FIELD_DECLs by name order. */
9843 field_decl_cmp (const void *x_p, const void *y_p)
9845 const tree *const x = (const tree *const) x_p;
9846 const tree *const y = (const tree *const) y_p;
9848 if (DECL_NAME (*x) == DECL_NAME (*y))
9849 /* A nontype is "greater" than a type. */
9850 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9851 if (DECL_NAME (*x) == NULL_TREE)
9852 return -1;
9853 if (DECL_NAME (*y) == NULL_TREE)
9854 return 1;
9855 if (DECL_NAME (*x) < DECL_NAME (*y))
9856 return -1;
9857 return 1;
9860 static struct {
9861 gt_pointer_operator new_value;
9862 void *cookie;
9863 } resort_data;
9865 /* This routine compares two fields like field_decl_cmp but using the
9866 pointer operator in resort_data. */
9868 static int
9869 resort_field_decl_cmp (const void *x_p, const void *y_p)
9871 const tree *const x = (const tree *const) x_p;
9872 const tree *const y = (const tree *const) y_p;
9874 if (DECL_NAME (*x) == DECL_NAME (*y))
9875 /* A nontype is "greater" than a type. */
9876 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9877 if (DECL_NAME (*x) == NULL_TREE)
9878 return -1;
9879 if (DECL_NAME (*y) == NULL_TREE)
9880 return 1;
9882 tree d1 = DECL_NAME (*x);
9883 tree d2 = DECL_NAME (*y);
9884 resort_data.new_value (&d1, resort_data.cookie);
9885 resort_data.new_value (&d2, resort_data.cookie);
9886 if (d1 < d2)
9887 return -1;
9889 return 1;
9892 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9894 void
9895 resort_sorted_fields (void *obj,
9896 void * ARG_UNUSED (orig_obj),
9897 gt_pointer_operator new_value,
9898 void *cookie)
9900 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9901 resort_data.new_value = new_value;
9902 resort_data.cookie = cookie;
9903 qsort (&sf->elts[0], sf->len, sizeof (tree),
9904 resort_field_decl_cmp);
9907 /* Subroutine of c_parse_error.
9908 Return the result of concatenating LHS and RHS. RHS is really
9909 a string literal, its first character is indicated by RHS_START and
9910 RHS_SIZE is its length (including the terminating NUL character).
9912 The caller is responsible for deleting the returned pointer. */
9914 static char *
9915 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9917 const int lhs_size = strlen (lhs);
9918 char *result = XNEWVEC (char, lhs_size + rhs_size);
9919 strncpy (result, lhs, lhs_size);
9920 strncpy (result + lhs_size, rhs_start, rhs_size);
9921 return result;
9924 /* Issue the error given by GMSGID, indicating that it occurred before
9925 TOKEN, which had the associated VALUE. */
9927 void
9928 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9929 tree value, unsigned char token_flags)
9931 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9933 char *message = NULL;
9935 if (token_type == CPP_EOF)
9936 message = catenate_messages (gmsgid, " at end of input");
9937 else if (token_type == CPP_CHAR
9938 || token_type == CPP_WCHAR
9939 || token_type == CPP_CHAR16
9940 || token_type == CPP_CHAR32)
9942 unsigned int val = TREE_INT_CST_LOW (value);
9943 const char *prefix;
9945 switch (token_type)
9947 default:
9948 prefix = "";
9949 break;
9950 case CPP_WCHAR:
9951 prefix = "L";
9952 break;
9953 case CPP_CHAR16:
9954 prefix = "u";
9955 break;
9956 case CPP_CHAR32:
9957 prefix = "U";
9958 break;
9961 if (val <= UCHAR_MAX && ISGRAPH (val))
9962 message = catenate_messages (gmsgid, " before %s'%c'");
9963 else
9964 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9966 error (message, prefix, val);
9967 free (message);
9968 message = NULL;
9970 else if (token_type == CPP_CHAR_USERDEF
9971 || token_type == CPP_WCHAR_USERDEF
9972 || token_type == CPP_CHAR16_USERDEF
9973 || token_type == CPP_CHAR32_USERDEF)
9974 message = catenate_messages (gmsgid,
9975 " before user-defined character literal");
9976 else if (token_type == CPP_STRING_USERDEF
9977 || token_type == CPP_WSTRING_USERDEF
9978 || token_type == CPP_STRING16_USERDEF
9979 || token_type == CPP_STRING32_USERDEF
9980 || token_type == CPP_UTF8STRING_USERDEF)
9981 message = catenate_messages (gmsgid, " before user-defined string literal");
9982 else if (token_type == CPP_STRING
9983 || token_type == CPP_WSTRING
9984 || token_type == CPP_STRING16
9985 || token_type == CPP_STRING32
9986 || token_type == CPP_UTF8STRING)
9987 message = catenate_messages (gmsgid, " before string constant");
9988 else if (token_type == CPP_NUMBER)
9989 message = catenate_messages (gmsgid, " before numeric constant");
9990 else if (token_type == CPP_NAME)
9992 message = catenate_messages (gmsgid, " before %qE");
9993 error (message, value);
9994 free (message);
9995 message = NULL;
9997 else if (token_type == CPP_PRAGMA)
9998 message = catenate_messages (gmsgid, " before %<#pragma%>");
9999 else if (token_type == CPP_PRAGMA_EOL)
10000 message = catenate_messages (gmsgid, " before end of line");
10001 else if (token_type == CPP_DECLTYPE)
10002 message = catenate_messages (gmsgid, " before %<decltype%>");
10003 else if (token_type < N_TTYPES)
10005 message = catenate_messages (gmsgid, " before %qs token");
10006 error (message, cpp_type2name (token_type, token_flags));
10007 free (message);
10008 message = NULL;
10010 else
10011 error (gmsgid);
10013 if (message)
10015 error (message);
10016 free (message);
10018 #undef catenate_messages
10021 /* Return the gcc option code associated with the reason for a cpp
10022 message, or 0 if none. */
10024 static int
10025 c_option_controlling_cpp_error (int reason)
10027 const struct cpp_reason_option_codes_t *entry;
10029 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10031 if (entry->reason == reason)
10032 return entry->option_code;
10034 return 0;
10037 /* Callback from cpp_error for PFILE to print diagnostics from the
10038 preprocessor. The diagnostic is of type LEVEL, with REASON set
10039 to the reason code if LEVEL is represents a warning, at location
10040 LOCATION unless this is after lexing and the compiler's location
10041 should be used instead, with column number possibly overridden by
10042 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10043 the arguments. Returns true if a diagnostic was emitted, false
10044 otherwise. */
10046 bool
10047 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10048 location_t location, unsigned int column_override,
10049 const char *msg, va_list *ap)
10051 diagnostic_info diagnostic;
10052 diagnostic_t dlevel;
10053 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10054 bool ret;
10056 switch (level)
10058 case CPP_DL_WARNING_SYSHDR:
10059 if (flag_no_output)
10060 return false;
10061 global_dc->dc_warn_system_headers = 1;
10062 /* Fall through. */
10063 case CPP_DL_WARNING:
10064 if (flag_no_output)
10065 return false;
10066 dlevel = DK_WARNING;
10067 break;
10068 case CPP_DL_PEDWARN:
10069 if (flag_no_output && !flag_pedantic_errors)
10070 return false;
10071 dlevel = DK_PEDWARN;
10072 break;
10073 case CPP_DL_ERROR:
10074 dlevel = DK_ERROR;
10075 break;
10076 case CPP_DL_ICE:
10077 dlevel = DK_ICE;
10078 break;
10079 case CPP_DL_NOTE:
10080 dlevel = DK_NOTE;
10081 break;
10082 case CPP_DL_FATAL:
10083 dlevel = DK_FATAL;
10084 break;
10085 default:
10086 gcc_unreachable ();
10088 if (done_lexing)
10089 location = input_location;
10090 diagnostic_set_info_translated (&diagnostic, msg, ap,
10091 location, dlevel);
10092 if (column_override)
10093 diagnostic_override_column (&diagnostic, column_override);
10094 diagnostic_override_option_index (&diagnostic,
10095 c_option_controlling_cpp_error (reason));
10096 ret = report_diagnostic (&diagnostic);
10097 if (level == CPP_DL_WARNING_SYSHDR)
10098 global_dc->dc_warn_system_headers = save_warn_system_headers;
10099 return ret;
10102 /* Convert a character from the host to the target execution character
10103 set. cpplib handles this, mostly. */
10105 HOST_WIDE_INT
10106 c_common_to_target_charset (HOST_WIDE_INT c)
10108 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10109 zero-extended under -fno-signed-char. cpplib insists that characters
10110 and character constants are always unsigned. Hence we must convert
10111 back and forth. */
10112 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10114 uc = cpp_host_to_exec_charset (parse_in, uc);
10116 if (flag_signed_char)
10117 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10118 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10119 else
10120 return uc;
10123 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10124 references with an INDIRECT_REF of a constant at the bottom; much like the
10125 traditional rendering of offsetof as a macro. Return the folded result. */
10127 tree
10128 fold_offsetof_1 (tree expr)
10130 tree base, off, t;
10132 switch (TREE_CODE (expr))
10134 case ERROR_MARK:
10135 return expr;
10137 case VAR_DECL:
10138 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10139 return error_mark_node;
10141 case CALL_EXPR:
10142 case TARGET_EXPR:
10143 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10144 return error_mark_node;
10146 case NOP_EXPR:
10147 case INDIRECT_REF:
10148 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10150 error ("cannot apply %<offsetof%> to a non constant address");
10151 return error_mark_node;
10153 return TREE_OPERAND (expr, 0);
10155 case COMPONENT_REF:
10156 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10157 if (base == error_mark_node)
10158 return base;
10160 t = TREE_OPERAND (expr, 1);
10161 if (DECL_C_BIT_FIELD (t))
10163 error ("attempt to take address of bit-field structure "
10164 "member %qD", t);
10165 return error_mark_node;
10167 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10168 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10169 / BITS_PER_UNIT));
10170 break;
10172 case ARRAY_REF:
10173 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10174 if (base == error_mark_node)
10175 return base;
10177 t = TREE_OPERAND (expr, 1);
10179 /* Check if the offset goes beyond the upper bound of the array. */
10180 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10182 tree upbound = array_ref_up_bound (expr);
10183 if (upbound != NULL_TREE
10184 && TREE_CODE (upbound) == INTEGER_CST
10185 && !tree_int_cst_equal (upbound,
10186 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10188 upbound = size_binop (PLUS_EXPR, upbound,
10189 build_int_cst (TREE_TYPE (upbound), 1));
10190 if (tree_int_cst_lt (upbound, t))
10192 tree v;
10194 for (v = TREE_OPERAND (expr, 0);
10195 TREE_CODE (v) == COMPONENT_REF;
10196 v = TREE_OPERAND (v, 0))
10197 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10198 == RECORD_TYPE)
10200 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10201 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10202 if (TREE_CODE (fld_chain) == FIELD_DECL)
10203 break;
10205 if (fld_chain)
10206 break;
10208 /* Don't warn if the array might be considered a poor
10209 man's flexible array member with a very permissive
10210 definition thereof. */
10211 if (TREE_CODE (v) == ARRAY_REF
10212 || TREE_CODE (v) == COMPONENT_REF)
10213 warning (OPT_Warray_bounds,
10214 "index %E denotes an offset "
10215 "greater than size of %qT",
10216 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10221 t = convert (sizetype, t);
10222 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10223 break;
10225 case COMPOUND_EXPR:
10226 /* Handle static members of volatile structs. */
10227 t = TREE_OPERAND (expr, 1);
10228 gcc_assert (TREE_CODE (t) == VAR_DECL);
10229 return fold_offsetof_1 (t);
10231 default:
10232 gcc_unreachable ();
10235 return fold_build_pointer_plus (base, off);
10238 /* Likewise, but convert it to the return type of offsetof. */
10240 tree
10241 fold_offsetof (tree expr)
10243 return convert (size_type_node, fold_offsetof_1 (expr));
10246 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10247 expression, because B will always be true. */
10249 void
10250 warn_for_omitted_condop (location_t location, tree cond)
10252 if (truth_value_p (TREE_CODE (cond)))
10253 warning_at (location, OPT_Wparentheses,
10254 "the omitted middle operand in ?: will always be %<true%>, "
10255 "suggest explicit middle operand");
10258 /* Give an error for storing into ARG, which is 'const'. USE indicates
10259 how ARG was being used. */
10261 void
10262 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10264 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10265 || use == lv_asm);
10266 /* Using this macro rather than (for example) arrays of messages
10267 ensures that all the format strings are checked at compile
10268 time. */
10269 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10270 : (use == lv_increment ? (I) \
10271 : (use == lv_decrement ? (D) : (AS))))
10272 if (TREE_CODE (arg) == COMPONENT_REF)
10274 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10275 error_at (loc, READONLY_MSG (G_("assignment of member "
10276 "%qD in read-only object"),
10277 G_("increment of member "
10278 "%qD in read-only object"),
10279 G_("decrement of member "
10280 "%qD in read-only object"),
10281 G_("member %qD in read-only object "
10282 "used as %<asm%> output")),
10283 TREE_OPERAND (arg, 1));
10284 else
10285 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10286 G_("increment of read-only member %qD"),
10287 G_("decrement of read-only member %qD"),
10288 G_("read-only member %qD used as %<asm%> output")),
10289 TREE_OPERAND (arg, 1));
10291 else if (TREE_CODE (arg) == VAR_DECL)
10292 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10293 G_("increment of read-only variable %qD"),
10294 G_("decrement of read-only variable %qD"),
10295 G_("read-only variable %qD used as %<asm%> output")),
10296 arg);
10297 else if (TREE_CODE (arg) == PARM_DECL)
10298 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10299 G_("increment of read-only parameter %qD"),
10300 G_("decrement of read-only parameter %qD"),
10301 G_("read-only parameter %qD use as %<asm%> output")),
10302 arg);
10303 else if (TREE_CODE (arg) == RESULT_DECL)
10305 gcc_assert (c_dialect_cxx ());
10306 error_at (loc, READONLY_MSG (G_("assignment of "
10307 "read-only named return value %qD"),
10308 G_("increment of "
10309 "read-only named return value %qD"),
10310 G_("decrement of "
10311 "read-only named return value %qD"),
10312 G_("read-only named return value %qD "
10313 "used as %<asm%>output")),
10314 arg);
10316 else if (TREE_CODE (arg) == FUNCTION_DECL)
10317 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10318 G_("increment of function %qD"),
10319 G_("decrement of function %qD"),
10320 G_("function %qD used as %<asm%> output")),
10321 arg);
10322 else
10323 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10324 G_("increment of read-only location %qE"),
10325 G_("decrement of read-only location %qE"),
10326 G_("read-only location %qE used as %<asm%> output")),
10327 arg);
10330 /* Print an error message for an invalid lvalue. USE says
10331 how the lvalue is being used and so selects the error message. LOC
10332 is the location for the error. */
10334 void
10335 lvalue_error (location_t loc, enum lvalue_use use)
10337 switch (use)
10339 case lv_assign:
10340 error_at (loc, "lvalue required as left operand of assignment");
10341 break;
10342 case lv_increment:
10343 error_at (loc, "lvalue required as increment operand");
10344 break;
10345 case lv_decrement:
10346 error_at (loc, "lvalue required as decrement operand");
10347 break;
10348 case lv_addressof:
10349 error_at (loc, "lvalue required as unary %<&%> operand");
10350 break;
10351 case lv_asm:
10352 error_at (loc, "lvalue required in asm statement");
10353 break;
10354 default:
10355 gcc_unreachable ();
10359 /* Print an error message for an invalid indirection of type TYPE.
10360 ERRSTRING is the name of the operator for the indirection. */
10362 void
10363 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10365 switch (errstring)
10367 case RO_NULL:
10368 gcc_assert (c_dialect_cxx ());
10369 error_at (loc, "invalid type argument (have %qT)", type);
10370 break;
10371 case RO_ARRAY_INDEXING:
10372 error_at (loc,
10373 "invalid type argument of array indexing (have %qT)",
10374 type);
10375 break;
10376 case RO_UNARY_STAR:
10377 error_at (loc,
10378 "invalid type argument of unary %<*%> (have %qT)",
10379 type);
10380 break;
10381 case RO_ARROW:
10382 error_at (loc,
10383 "invalid type argument of %<->%> (have %qT)",
10384 type);
10385 break;
10386 case RO_ARROW_STAR:
10387 error_at (loc,
10388 "invalid type argument of %<->*%> (have %qT)",
10389 type);
10390 break;
10391 case RO_IMPLICIT_CONVERSION:
10392 error_at (loc,
10393 "invalid type argument of implicit conversion (have %qT)",
10394 type);
10395 break;
10396 default:
10397 gcc_unreachable ();
10401 /* *PTYPE is an incomplete array. Complete it with a domain based on
10402 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10403 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10404 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10407 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10409 tree maxindex, type, main_type, elt, unqual_elt;
10410 int failure = 0, quals;
10411 hashval_t hashcode = 0;
10412 bool overflow_p = false;
10414 maxindex = size_zero_node;
10415 if (initial_value)
10417 if (TREE_CODE (initial_value) == STRING_CST)
10419 int eltsize
10420 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10421 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10423 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10425 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10427 if (vec_safe_is_empty (v))
10429 if (pedantic)
10430 failure = 3;
10431 maxindex = ssize_int (-1);
10433 else
10435 tree curindex;
10436 unsigned HOST_WIDE_INT cnt;
10437 constructor_elt *ce;
10438 bool fold_p = false;
10440 if ((*v)[0].index)
10441 maxindex = (*v)[0].index, fold_p = true;
10443 curindex = maxindex;
10445 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10447 bool curfold_p = false;
10448 if (ce->index)
10449 curindex = ce->index, curfold_p = true;
10450 else
10452 if (fold_p)
10454 /* Since we treat size types now as ordinary
10455 unsigned types, we need an explicit overflow
10456 check. */
10457 tree orig = curindex;
10458 curindex = fold_convert (sizetype, curindex);
10459 overflow_p |= tree_int_cst_lt (curindex, orig);
10461 curindex = size_binop (PLUS_EXPR, curindex,
10462 size_one_node);
10464 if (tree_int_cst_lt (maxindex, curindex))
10465 maxindex = curindex, fold_p = curfold_p;
10467 if (fold_p)
10469 tree orig = maxindex;
10470 maxindex = fold_convert (sizetype, maxindex);
10471 overflow_p |= tree_int_cst_lt (maxindex, orig);
10475 else
10477 /* Make an error message unless that happened already. */
10478 if (initial_value != error_mark_node)
10479 failure = 1;
10482 else
10484 failure = 2;
10485 if (!do_default)
10486 return failure;
10489 type = *ptype;
10490 elt = TREE_TYPE (type);
10491 quals = TYPE_QUALS (strip_array_types (elt));
10492 if (quals == 0)
10493 unqual_elt = elt;
10494 else
10495 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10497 /* Using build_distinct_type_copy and modifying things afterward instead
10498 of using build_array_type to create a new type preserves all of the
10499 TYPE_LANG_FLAG_? bits that the front end may have set. */
10500 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10501 TREE_TYPE (main_type) = unqual_elt;
10502 TYPE_DOMAIN (main_type)
10503 = build_range_type (TREE_TYPE (maxindex),
10504 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10505 layout_type (main_type);
10507 /* Make sure we have the canonical MAIN_TYPE. */
10508 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10509 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10510 hashcode);
10511 main_type = type_hash_canon (hashcode, main_type);
10513 /* Fix the canonical type. */
10514 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10515 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10516 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10517 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10518 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10519 != TYPE_DOMAIN (main_type)))
10520 TYPE_CANONICAL (main_type)
10521 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10522 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10523 else
10524 TYPE_CANONICAL (main_type) = main_type;
10526 if (quals == 0)
10527 type = main_type;
10528 else
10529 type = c_build_qualified_type (main_type, quals);
10531 if (COMPLETE_TYPE_P (type)
10532 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10533 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10535 error ("size of array is too large");
10536 /* If we proceed with the array type as it is, we'll eventually
10537 crash in tree_to_[su]hwi(). */
10538 type = error_mark_node;
10541 *ptype = type;
10542 return failure;
10545 /* Like c_mark_addressable but don't check register qualifier. */
10546 void
10547 c_common_mark_addressable_vec (tree t)
10549 while (handled_component_p (t))
10550 t = TREE_OPERAND (t, 0);
10551 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10552 return;
10553 TREE_ADDRESSABLE (t) = 1;
10558 /* Used to help initialize the builtin-types.def table. When a type of
10559 the correct size doesn't exist, use error_mark_node instead of NULL.
10560 The later results in segfaults even when a decl using the type doesn't
10561 get invoked. */
10563 tree
10564 builtin_type_for_size (int size, bool unsignedp)
10566 tree type = c_common_type_for_size (size, unsignedp);
10567 return type ? type : error_mark_node;
10570 /* A helper function for resolve_overloaded_builtin in resolving the
10571 overloaded __sync_ builtins. Returns a positive power of 2 if the
10572 first operand of PARAMS is a pointer to a supported data type.
10573 Returns 0 if an error is encountered. */
10575 static int
10576 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10578 tree type;
10579 int size;
10581 if (!params)
10583 error ("too few arguments to function %qE", function);
10584 return 0;
10587 type = TREE_TYPE ((*params)[0]);
10588 if (TREE_CODE (type) == ARRAY_TYPE)
10590 /* Force array-to-pointer decay for C++. */
10591 gcc_assert (c_dialect_cxx());
10592 (*params)[0] = default_conversion ((*params)[0]);
10593 type = TREE_TYPE ((*params)[0]);
10595 if (TREE_CODE (type) != POINTER_TYPE)
10596 goto incompatible;
10598 type = TREE_TYPE (type);
10599 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10600 goto incompatible;
10602 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10603 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10604 return size;
10606 incompatible:
10607 error ("incompatible type for argument %d of %qE", 1, function);
10608 return 0;
10611 /* A helper function for resolve_overloaded_builtin. Adds casts to
10612 PARAMS to make arguments match up with those of FUNCTION. Drops
10613 the variadic arguments at the end. Returns false if some error
10614 was encountered; true on success. */
10616 static bool
10617 sync_resolve_params (location_t loc, tree orig_function, tree function,
10618 vec<tree, va_gc> *params, bool orig_format)
10620 function_args_iterator iter;
10621 tree ptype;
10622 unsigned int parmnum;
10624 function_args_iter_init (&iter, TREE_TYPE (function));
10625 /* We've declared the implementation functions to use "volatile void *"
10626 as the pointer parameter, so we shouldn't get any complaints from the
10627 call to check_function_arguments what ever type the user used. */
10628 function_args_iter_next (&iter);
10629 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10630 ptype = TYPE_MAIN_VARIANT (ptype);
10632 /* For the rest of the values, we need to cast these to FTYPE, so that we
10633 don't get warnings for passing pointer types, etc. */
10634 parmnum = 0;
10635 while (1)
10637 tree val, arg_type;
10639 arg_type = function_args_iter_cond (&iter);
10640 /* XXX void_type_node belies the abstraction. */
10641 if (arg_type == void_type_node)
10642 break;
10644 ++parmnum;
10645 if (params->length () <= parmnum)
10647 error_at (loc, "too few arguments to function %qE", orig_function);
10648 return false;
10651 /* Only convert parameters if arg_type is unsigned integer type with
10652 new format sync routines, i.e. don't attempt to convert pointer
10653 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10654 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10655 kinds). */
10656 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10658 /* Ideally for the first conversion we'd use convert_for_assignment
10659 so that we get warnings for anything that doesn't match the pointer
10660 type. This isn't portable across the C and C++ front ends atm. */
10661 val = (*params)[parmnum];
10662 val = convert (ptype, val);
10663 val = convert (arg_type, val);
10664 (*params)[parmnum] = val;
10667 function_args_iter_next (&iter);
10670 /* __atomic routines are not variadic. */
10671 if (!orig_format && params->length () != parmnum + 1)
10673 error_at (loc, "too many arguments to function %qE", orig_function);
10674 return false;
10677 /* The definition of these primitives is variadic, with the remaining
10678 being "an optional list of variables protected by the memory barrier".
10679 No clue what that's supposed to mean, precisely, but we consider all
10680 call-clobbered variables to be protected so we're safe. */
10681 params->truncate (parmnum + 1);
10683 return true;
10686 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10687 RESULT to make it match the type of the first pointer argument in
10688 PARAMS. */
10690 static tree
10691 sync_resolve_return (tree first_param, tree result, bool orig_format)
10693 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10694 tree rtype = TREE_TYPE (result);
10695 ptype = TYPE_MAIN_VARIANT (ptype);
10697 /* New format doesn't require casting unless the types are the same size. */
10698 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10699 return convert (ptype, result);
10700 else
10701 return result;
10704 /* This function verifies the PARAMS to generic atomic FUNCTION.
10705 It returns the size if all the parameters are the same size, otherwise
10706 0 is returned if the parameters are invalid. */
10708 static int
10709 get_atomic_generic_size (location_t loc, tree function,
10710 vec<tree, va_gc> *params)
10712 unsigned int n_param;
10713 unsigned int n_model;
10714 unsigned int x;
10715 int size_0;
10716 tree type_0;
10718 /* Determine the parameter makeup. */
10719 switch (DECL_FUNCTION_CODE (function))
10721 case BUILT_IN_ATOMIC_EXCHANGE:
10722 n_param = 4;
10723 n_model = 1;
10724 break;
10725 case BUILT_IN_ATOMIC_LOAD:
10726 case BUILT_IN_ATOMIC_STORE:
10727 n_param = 3;
10728 n_model = 1;
10729 break;
10730 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10731 n_param = 6;
10732 n_model = 2;
10733 break;
10734 default:
10735 gcc_unreachable ();
10738 if (vec_safe_length (params) != n_param)
10740 error_at (loc, "incorrect number of arguments to function %qE", function);
10741 return 0;
10744 /* Get type of first parameter, and determine its size. */
10745 type_0 = TREE_TYPE ((*params)[0]);
10746 if (TREE_CODE (type_0) == ARRAY_TYPE)
10748 /* Force array-to-pointer decay for C++. */
10749 gcc_assert (c_dialect_cxx());
10750 (*params)[0] = default_conversion ((*params)[0]);
10751 type_0 = TREE_TYPE ((*params)[0]);
10753 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10755 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10756 function);
10757 return 0;
10760 /* Types must be compile time constant sizes. */
10761 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10763 error_at (loc,
10764 "argument 1 of %qE must be a pointer to a constant size type",
10765 function);
10766 return 0;
10769 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10771 /* Zero size objects are not allowed. */
10772 if (size_0 == 0)
10774 error_at (loc,
10775 "argument 1 of %qE must be a pointer to a nonzero size object",
10776 function);
10777 return 0;
10780 /* Check each other parameter is a pointer and the same size. */
10781 for (x = 0; x < n_param - n_model; x++)
10783 int size;
10784 tree type = TREE_TYPE ((*params)[x]);
10785 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10786 if (n_param == 6 && x == 3)
10787 continue;
10788 if (!POINTER_TYPE_P (type))
10790 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10791 function);
10792 return 0;
10794 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10795 size = type_size ? tree_to_uhwi (type_size) : 0;
10796 if (size != size_0)
10798 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10799 function);
10800 return 0;
10804 /* Check memory model parameters for validity. */
10805 for (x = n_param - n_model ; x < n_param; x++)
10807 tree p = (*params)[x];
10808 if (TREE_CODE (p) == INTEGER_CST)
10810 int i = tree_to_uhwi (p);
10811 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10813 warning_at (loc, OPT_Winvalid_memory_model,
10814 "invalid memory model argument %d of %qE", x + 1,
10815 function);
10818 else
10819 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10821 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10822 function);
10823 return 0;
10827 return size_0;
10831 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10832 at the beginning of the parameter list PARAMS representing the size of the
10833 objects. This is to match the library ABI requirement. LOC is the location
10834 of the function call.
10835 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10836 returned to allow the external call to be constructed. */
10838 static tree
10839 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10840 vec<tree, va_gc> *params)
10842 tree size_node;
10844 /* Insert a SIZE_T parameter as the first param. If there isn't
10845 enough space, allocate a new vector and recursively re-build with that. */
10846 if (!params->space (1))
10848 unsigned int z, len;
10849 vec<tree, va_gc> *v;
10850 tree f;
10852 len = params->length ();
10853 vec_alloc (v, len + 1);
10854 v->quick_push (build_int_cst (size_type_node, n));
10855 for (z = 0; z < len; z++)
10856 v->quick_push ((*params)[z]);
10857 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10858 vec_free (v);
10859 return f;
10862 /* Add the size parameter and leave as a function call for processing. */
10863 size_node = build_int_cst (size_type_node, n);
10864 params->quick_insert (0, size_node);
10865 return NULL_TREE;
10869 /* Return whether atomic operations for naturally aligned N-byte
10870 arguments are supported, whether inline or through libatomic. */
10871 static bool
10872 atomic_size_supported_p (int n)
10874 switch (n)
10876 case 1:
10877 case 2:
10878 case 4:
10879 case 8:
10880 return true;
10882 case 16:
10883 return targetm.scalar_mode_supported_p (TImode);
10885 default:
10886 return false;
10890 /* This will process an __atomic_exchange function call, determine whether it
10891 needs to be mapped to the _N variation, or turned into a library call.
10892 LOC is the location of the builtin call.
10893 FUNCTION is the DECL that has been invoked;
10894 PARAMS is the argument list for the call. The return value is non-null
10895 TRUE is returned if it is translated into the proper format for a call to the
10896 external library, and NEW_RETURN is set the tree for that function.
10897 FALSE is returned if processing for the _N variation is required, and
10898 NEW_RETURN is set to the the return value the result is copied into. */
10899 static bool
10900 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10901 vec<tree, va_gc> *params, tree *new_return)
10903 tree p0, p1, p2, p3;
10904 tree I_type, I_type_ptr;
10905 int n = get_atomic_generic_size (loc, function, params);
10907 /* Size of 0 is an error condition. */
10908 if (n == 0)
10910 *new_return = error_mark_node;
10911 return true;
10914 /* If not a lock-free size, change to the library generic format. */
10915 if (!atomic_size_supported_p (n))
10917 *new_return = add_atomic_size_parameter (n, loc, function, params);
10918 return true;
10921 /* Otherwise there is a lockfree match, transform the call from:
10922 void fn(T* mem, T* desired, T* return, model)
10923 into
10924 *return = (T) (fn (In* mem, (In) *desired, model)) */
10926 p0 = (*params)[0];
10927 p1 = (*params)[1];
10928 p2 = (*params)[2];
10929 p3 = (*params)[3];
10931 /* Create pointer to appropriate size. */
10932 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10933 I_type_ptr = build_pointer_type (I_type);
10935 /* Convert object pointer to required type. */
10936 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10937 (*params)[0] = p0;
10938 /* Convert new value to required type, and dereference it. */
10939 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10940 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10941 (*params)[1] = p1;
10943 /* Move memory model to the 3rd position, and end param list. */
10944 (*params)[2] = p3;
10945 params->truncate (3);
10947 /* Convert return pointer and dereference it for later assignment. */
10948 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10950 return false;
10954 /* This will process an __atomic_compare_exchange function call, determine
10955 whether it needs to be mapped to the _N variation, or turned into a lib call.
10956 LOC is the location of the builtin call.
10957 FUNCTION is the DECL that has been invoked;
10958 PARAMS is the argument list for the call. The return value is non-null
10959 TRUE is returned if it is translated into the proper format for a call to the
10960 external library, and NEW_RETURN is set the tree for that function.
10961 FALSE is returned if processing for the _N variation is required. */
10963 static bool
10964 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10965 vec<tree, va_gc> *params,
10966 tree *new_return)
10968 tree p0, p1, p2;
10969 tree I_type, I_type_ptr;
10970 int n = get_atomic_generic_size (loc, function, params);
10972 /* Size of 0 is an error condition. */
10973 if (n == 0)
10975 *new_return = error_mark_node;
10976 return true;
10979 /* If not a lock-free size, change to the library generic format. */
10980 if (!atomic_size_supported_p (n))
10982 /* The library generic format does not have the weak parameter, so
10983 remove it from the param list. Since a parameter has been removed,
10984 we can be sure that there is room for the SIZE_T parameter, meaning
10985 there will not be a recursive rebuilding of the parameter list, so
10986 there is no danger this will be done twice. */
10987 if (n > 0)
10989 (*params)[3] = (*params)[4];
10990 (*params)[4] = (*params)[5];
10991 params->truncate (5);
10993 *new_return = add_atomic_size_parameter (n, loc, function, params);
10994 return true;
10997 /* Otherwise, there is a match, so the call needs to be transformed from:
10998 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10999 into
11000 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11002 p0 = (*params)[0];
11003 p1 = (*params)[1];
11004 p2 = (*params)[2];
11006 /* Create pointer to appropriate size. */
11007 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11008 I_type_ptr = build_pointer_type (I_type);
11010 /* Convert object pointer to required type. */
11011 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11012 (*params)[0] = p0;
11014 /* Convert expected pointer to required type. */
11015 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11016 (*params)[1] = p1;
11018 /* Convert desired value to required type, and dereference it. */
11019 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11020 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11021 (*params)[2] = p2;
11023 /* The rest of the parameters are fine. NULL means no special return value
11024 processing.*/
11025 *new_return = NULL;
11026 return false;
11030 /* This will process an __atomic_load function call, determine whether it
11031 needs to be mapped to the _N variation, or turned into a library call.
11032 LOC is the location of the builtin call.
11033 FUNCTION is the DECL that has been invoked;
11034 PARAMS is the argument list for the call. The return value is non-null
11035 TRUE is returned if it is translated into the proper format for a call to the
11036 external library, and NEW_RETURN is set the tree for that function.
11037 FALSE is returned if processing for the _N variation is required, and
11038 NEW_RETURN is set to the the return value the result is copied into. */
11040 static bool
11041 resolve_overloaded_atomic_load (location_t loc, tree function,
11042 vec<tree, va_gc> *params, tree *new_return)
11044 tree p0, p1, p2;
11045 tree I_type, I_type_ptr;
11046 int n = get_atomic_generic_size (loc, function, params);
11048 /* Size of 0 is an error condition. */
11049 if (n == 0)
11051 *new_return = error_mark_node;
11052 return true;
11055 /* If not a lock-free size, change to the library generic format. */
11056 if (!atomic_size_supported_p (n))
11058 *new_return = add_atomic_size_parameter (n, loc, function, params);
11059 return true;
11062 /* Otherwise, there is a match, so the call needs to be transformed from:
11063 void fn(T* mem, T* return, model)
11064 into
11065 *return = (T) (fn ((In *) mem, model)) */
11067 p0 = (*params)[0];
11068 p1 = (*params)[1];
11069 p2 = (*params)[2];
11071 /* Create pointer to appropriate size. */
11072 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11073 I_type_ptr = build_pointer_type (I_type);
11075 /* Convert object pointer to required type. */
11076 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11077 (*params)[0] = p0;
11079 /* Move memory model to the 2nd position, and end param list. */
11080 (*params)[1] = p2;
11081 params->truncate (2);
11083 /* Convert return pointer and dereference it for later assignment. */
11084 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11086 return false;
11090 /* This will process an __atomic_store function call, determine whether it
11091 needs to be mapped to the _N variation, or turned into a library call.
11092 LOC is the location of the builtin call.
11093 FUNCTION is the DECL that has been invoked;
11094 PARAMS is the argument list for the call. The return value is non-null
11095 TRUE is returned if it is translated into the proper format for a call to the
11096 external library, and NEW_RETURN is set the tree for that function.
11097 FALSE is returned if processing for the _N variation is required, and
11098 NEW_RETURN is set to the the return value the result is copied into. */
11100 static bool
11101 resolve_overloaded_atomic_store (location_t loc, tree function,
11102 vec<tree, va_gc> *params, tree *new_return)
11104 tree p0, p1;
11105 tree I_type, I_type_ptr;
11106 int n = get_atomic_generic_size (loc, function, params);
11108 /* Size of 0 is an error condition. */
11109 if (n == 0)
11111 *new_return = error_mark_node;
11112 return true;
11115 /* If not a lock-free size, change to the library generic format. */
11116 if (!atomic_size_supported_p (n))
11118 *new_return = add_atomic_size_parameter (n, loc, function, params);
11119 return true;
11122 /* Otherwise, there is a match, so the call needs to be transformed from:
11123 void fn(T* mem, T* value, model)
11124 into
11125 fn ((In *) mem, (In) *value, model) */
11127 p0 = (*params)[0];
11128 p1 = (*params)[1];
11130 /* Create pointer to appropriate size. */
11131 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11132 I_type_ptr = build_pointer_type (I_type);
11134 /* Convert object pointer to required type. */
11135 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11136 (*params)[0] = p0;
11138 /* Convert new value to required type, and dereference it. */
11139 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11140 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11141 (*params)[1] = p1;
11143 /* The memory model is in the right spot already. Return is void. */
11144 *new_return = NULL_TREE;
11146 return false;
11150 /* Some builtin functions are placeholders for other expressions. This
11151 function should be called immediately after parsing the call expression
11152 before surrounding code has committed to the type of the expression.
11154 LOC is the location of the builtin call.
11156 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11157 PARAMS is the argument list for the call. The return value is non-null
11158 when expansion is complete, and null if normal processing should
11159 continue. */
11161 tree
11162 resolve_overloaded_builtin (location_t loc, tree function,
11163 vec<tree, va_gc> *params)
11165 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11166 bool orig_format = true;
11167 tree new_return = NULL_TREE;
11169 switch (DECL_BUILT_IN_CLASS (function))
11171 case BUILT_IN_NORMAL:
11172 break;
11173 case BUILT_IN_MD:
11174 if (targetm.resolve_overloaded_builtin)
11175 return targetm.resolve_overloaded_builtin (loc, function, params);
11176 else
11177 return NULL_TREE;
11178 default:
11179 return NULL_TREE;
11182 /* Handle BUILT_IN_NORMAL here. */
11183 switch (orig_code)
11185 case BUILT_IN_ATOMIC_EXCHANGE:
11186 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11187 case BUILT_IN_ATOMIC_LOAD:
11188 case BUILT_IN_ATOMIC_STORE:
11190 /* Handle these 4 together so that they can fall through to the next
11191 case if the call is transformed to an _N variant. */
11192 switch (orig_code)
11194 case BUILT_IN_ATOMIC_EXCHANGE:
11196 if (resolve_overloaded_atomic_exchange (loc, function, params,
11197 &new_return))
11198 return new_return;
11199 /* Change to the _N variant. */
11200 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11201 break;
11204 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11206 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11207 params,
11208 &new_return))
11209 return new_return;
11210 /* Change to the _N variant. */
11211 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11212 break;
11214 case BUILT_IN_ATOMIC_LOAD:
11216 if (resolve_overloaded_atomic_load (loc, function, params,
11217 &new_return))
11218 return new_return;
11219 /* Change to the _N variant. */
11220 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11221 break;
11223 case BUILT_IN_ATOMIC_STORE:
11225 if (resolve_overloaded_atomic_store (loc, function, params,
11226 &new_return))
11227 return new_return;
11228 /* Change to the _N variant. */
11229 orig_code = BUILT_IN_ATOMIC_STORE_N;
11230 break;
11232 default:
11233 gcc_unreachable ();
11235 /* Fallthrough to the normal processing. */
11237 case BUILT_IN_ATOMIC_EXCHANGE_N:
11238 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11239 case BUILT_IN_ATOMIC_LOAD_N:
11240 case BUILT_IN_ATOMIC_STORE_N:
11241 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11242 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11243 case BUILT_IN_ATOMIC_AND_FETCH_N:
11244 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11245 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11246 case BUILT_IN_ATOMIC_OR_FETCH_N:
11247 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11248 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11249 case BUILT_IN_ATOMIC_FETCH_AND_N:
11250 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11251 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11252 case BUILT_IN_ATOMIC_FETCH_OR_N:
11254 orig_format = false;
11255 /* Fallthru for parameter processing. */
11257 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11258 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11259 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11260 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11261 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11262 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11263 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11264 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11265 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11266 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11267 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11268 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11269 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11270 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11271 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11272 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11274 int n = sync_resolve_size (function, params);
11275 tree new_function, first_param, result;
11276 enum built_in_function fncode;
11278 if (n == 0)
11279 return error_mark_node;
11281 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11282 new_function = builtin_decl_explicit (fncode);
11283 if (!sync_resolve_params (loc, function, new_function, params,
11284 orig_format))
11285 return error_mark_node;
11287 first_param = (*params)[0];
11288 result = build_function_call_vec (loc, vNULL, new_function, params,
11289 NULL);
11290 if (result == error_mark_node)
11291 return result;
11292 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11293 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11294 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11295 result = sync_resolve_return (first_param, result, orig_format);
11297 /* If new_return is set, assign function to that expr and cast the
11298 result to void since the generic interface returned void. */
11299 if (new_return)
11301 /* Cast function result from I{1,2,4,8,16} to the required type. */
11302 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11303 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11304 result);
11305 TREE_SIDE_EFFECTS (result) = 1;
11306 protected_set_expr_location (result, loc);
11307 result = convert (void_type_node, result);
11309 return result;
11312 default:
11313 return NULL_TREE;
11317 /* vector_types_compatible_elements_p is used in type checks of vectors
11318 values used as operands of binary operators. Where it returns true, and
11319 the other checks of the caller succeed (being vector types in he first
11320 place, and matching number of elements), we can just treat the types
11321 as essentially the same.
11322 Contrast with vector_targets_convertible_p, which is used for vector
11323 pointer types, and vector_types_convertible_p, which will allow
11324 language-specific matches under the control of flag_lax_vector_conversions,
11325 and might still require a conversion. */
11326 /* True if vector types T1 and T2 can be inputs to the same binary
11327 operator without conversion.
11328 We don't check the overall vector size here because some of our callers
11329 want to give different error messages when the vectors are compatible
11330 except for the element count. */
11332 bool
11333 vector_types_compatible_elements_p (tree t1, tree t2)
11335 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11336 t1 = TREE_TYPE (t1);
11337 t2 = TREE_TYPE (t2);
11339 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11341 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11342 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11343 || c2 == FIXED_POINT_TYPE));
11345 t1 = c_common_signed_type (t1);
11346 t2 = c_common_signed_type (t2);
11347 /* Equality works here because c_common_signed_type uses
11348 TYPE_MAIN_VARIANT. */
11349 if (t1 == t2)
11350 return true;
11351 if (opaque && c1 == c2
11352 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11353 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11354 return true;
11355 return false;
11358 /* Check for missing format attributes on function pointers. LTYPE is
11359 the new type or left-hand side type. RTYPE is the old type or
11360 right-hand side type. Returns TRUE if LTYPE is missing the desired
11361 attribute. */
11363 bool
11364 check_missing_format_attribute (tree ltype, tree rtype)
11366 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11367 tree ra;
11369 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11370 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11371 break;
11372 if (ra)
11374 tree la;
11375 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11376 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11377 break;
11378 return !la;
11380 else
11381 return false;
11384 /* Subscripting with type char is likely to lose on a machine where
11385 chars are signed. So warn on any machine, but optionally. Don't
11386 warn for unsigned char since that type is safe. Don't warn for
11387 signed char because anyone who uses that must have done so
11388 deliberately. Furthermore, we reduce the false positive load by
11389 warning only for non-constant value of type char. */
11391 void
11392 warn_array_subscript_with_type_char (location_t loc, tree index)
11394 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11395 && TREE_CODE (index) != INTEGER_CST)
11396 warning_at (loc, OPT_Wchar_subscripts,
11397 "array subscript has type %<char%>");
11400 /* Implement -Wparentheses for the unexpected C precedence rules, to
11401 cover cases like x + y << z which readers are likely to
11402 misinterpret. We have seen an expression in which CODE is a binary
11403 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11404 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11405 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11406 expression was not formed using a binary or unary operator, or it
11407 was enclosed in parentheses. */
11409 void
11410 warn_about_parentheses (location_t loc, enum tree_code code,
11411 enum tree_code code_left, tree arg_left,
11412 enum tree_code code_right, tree arg_right)
11414 if (!warn_parentheses)
11415 return;
11417 /* This macro tests that the expression ARG with original tree code
11418 CODE appears to be a boolean expression. or the result of folding a
11419 boolean expression. */
11420 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11421 (truth_value_p (TREE_CODE (ARG)) \
11422 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11423 /* Folding may create 0 or 1 integers from other expressions. */ \
11424 || ((CODE) != INTEGER_CST \
11425 && (integer_onep (ARG) || integer_zerop (ARG))))
11427 switch (code)
11429 case LSHIFT_EXPR:
11430 if (code_left == PLUS_EXPR)
11431 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11432 "suggest parentheses around %<+%> inside %<<<%>");
11433 else if (code_right == PLUS_EXPR)
11434 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11435 "suggest parentheses around %<+%> inside %<<<%>");
11436 else if (code_left == MINUS_EXPR)
11437 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11438 "suggest parentheses around %<-%> inside %<<<%>");
11439 else if (code_right == MINUS_EXPR)
11440 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11441 "suggest parentheses around %<-%> inside %<<<%>");
11442 return;
11444 case RSHIFT_EXPR:
11445 if (code_left == PLUS_EXPR)
11446 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11447 "suggest parentheses around %<+%> inside %<>>%>");
11448 else if (code_right == PLUS_EXPR)
11449 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11450 "suggest parentheses around %<+%> inside %<>>%>");
11451 else if (code_left == MINUS_EXPR)
11452 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11453 "suggest parentheses around %<-%> inside %<>>%>");
11454 else if (code_right == MINUS_EXPR)
11455 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11456 "suggest parentheses around %<-%> inside %<>>%>");
11457 return;
11459 case TRUTH_ORIF_EXPR:
11460 if (code_left == TRUTH_ANDIF_EXPR)
11461 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11462 "suggest parentheses around %<&&%> within %<||%>");
11463 else if (code_right == TRUTH_ANDIF_EXPR)
11464 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11465 "suggest parentheses around %<&&%> within %<||%>");
11466 return;
11468 case BIT_IOR_EXPR:
11469 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11470 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11471 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11472 "suggest parentheses around arithmetic in operand of %<|%>");
11473 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11474 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11475 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11476 "suggest parentheses around arithmetic in operand of %<|%>");
11477 /* Check cases like x|y==z */
11478 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11479 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11480 "suggest parentheses around comparison in operand of %<|%>");
11481 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11482 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11483 "suggest parentheses around comparison in operand of %<|%>");
11484 /* Check cases like !x | y */
11485 else if (code_left == TRUTH_NOT_EXPR
11486 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11487 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11488 "suggest parentheses around operand of "
11489 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11490 return;
11492 case BIT_XOR_EXPR:
11493 if (code_left == BIT_AND_EXPR
11494 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11495 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11496 "suggest parentheses around arithmetic in operand of %<^%>");
11497 else if (code_right == BIT_AND_EXPR
11498 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11499 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11500 "suggest parentheses around arithmetic in operand of %<^%>");
11501 /* Check cases like x^y==z */
11502 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11503 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11504 "suggest parentheses around comparison in operand of %<^%>");
11505 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11506 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11507 "suggest parentheses around comparison in operand of %<^%>");
11508 return;
11510 case BIT_AND_EXPR:
11511 if (code_left == PLUS_EXPR)
11512 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11513 "suggest parentheses around %<+%> in operand of %<&%>");
11514 else if (code_right == PLUS_EXPR)
11515 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11516 "suggest parentheses around %<+%> in operand of %<&%>");
11517 else if (code_left == MINUS_EXPR)
11518 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11519 "suggest parentheses around %<-%> in operand of %<&%>");
11520 else if (code_right == MINUS_EXPR)
11521 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11522 "suggest parentheses around %<-%> in operand of %<&%>");
11523 /* Check cases like x&y==z */
11524 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11525 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11526 "suggest parentheses around comparison in operand of %<&%>");
11527 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11528 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11529 "suggest parentheses around comparison in operand of %<&%>");
11530 /* Check cases like !x & y */
11531 else if (code_left == TRUTH_NOT_EXPR
11532 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11533 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11534 "suggest parentheses around operand of "
11535 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11536 return;
11538 case EQ_EXPR:
11539 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11540 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11541 "suggest parentheses around comparison in operand of %<==%>");
11542 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11543 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11544 "suggest parentheses around comparison in operand of %<==%>");
11545 return;
11546 case NE_EXPR:
11547 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11548 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11549 "suggest parentheses around comparison in operand of %<!=%>");
11550 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11551 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11552 "suggest parentheses around comparison in operand of %<!=%>");
11553 return;
11555 default:
11556 if (TREE_CODE_CLASS (code) == tcc_comparison)
11558 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11559 && code_left != NE_EXPR && code_left != EQ_EXPR
11560 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11561 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11562 "comparisons like %<X<=Y<=Z%> do not "
11563 "have their mathematical meaning");
11564 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11565 && code_right != NE_EXPR && code_right != EQ_EXPR
11566 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11567 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11568 "comparisons like %<X<=Y<=Z%> do not "
11569 "have their mathematical meaning");
11571 return;
11573 #undef NOT_A_BOOLEAN_EXPR_P
11576 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11578 void
11579 warn_for_unused_label (tree label)
11581 if (!TREE_USED (label))
11583 if (DECL_INITIAL (label))
11584 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11585 else
11586 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11590 /* Warn for division by zero according to the value of DIVISOR. LOC
11591 is the location of the division operator. */
11593 void
11594 warn_for_div_by_zero (location_t loc, tree divisor)
11596 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11597 about division by zero. Do not issue a warning if DIVISOR has a
11598 floating-point type, since we consider 0.0/0.0 a valid way of
11599 generating a NaN. */
11600 if (c_inhibit_evaluation_warnings == 0
11601 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11602 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11605 /* Subroutine of build_binary_op. Give warnings for comparisons
11606 between signed and unsigned quantities that may fail. Do the
11607 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11608 so that casts will be considered, but default promotions won't
11611 LOCATION is the location of the comparison operator.
11613 The arguments of this function map directly to local variables
11614 of build_binary_op. */
11616 void
11617 warn_for_sign_compare (location_t location,
11618 tree orig_op0, tree orig_op1,
11619 tree op0, tree op1,
11620 tree result_type, enum tree_code resultcode)
11622 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11623 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11624 int unsignedp0, unsignedp1;
11626 /* In C++, check for comparison of different enum types. */
11627 if (c_dialect_cxx()
11628 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11629 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11630 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11631 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11633 warning_at (location,
11634 OPT_Wsign_compare, "comparison between types %qT and %qT",
11635 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11638 /* Do not warn if the comparison is being done in a signed type,
11639 since the signed type will only be chosen if it can represent
11640 all the values of the unsigned type. */
11641 if (!TYPE_UNSIGNED (result_type))
11642 /* OK */;
11643 /* Do not warn if both operands are unsigned. */
11644 else if (op0_signed == op1_signed)
11645 /* OK */;
11646 else
11648 tree sop, uop, base_type;
11649 bool ovf;
11651 if (op0_signed)
11652 sop = orig_op0, uop = orig_op1;
11653 else
11654 sop = orig_op1, uop = orig_op0;
11656 STRIP_TYPE_NOPS (sop);
11657 STRIP_TYPE_NOPS (uop);
11658 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11659 ? TREE_TYPE (result_type) : result_type);
11661 /* Do not warn if the signed quantity is an unsuffixed integer
11662 literal (or some static constant expression involving such
11663 literals or a conditional expression involving such literals)
11664 and it is non-negative. */
11665 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11666 /* OK */;
11667 /* Do not warn if the comparison is an equality operation, the
11668 unsigned quantity is an integral constant, and it would fit
11669 in the result if the result were signed. */
11670 else if (TREE_CODE (uop) == INTEGER_CST
11671 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11672 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11673 /* OK */;
11674 /* In C, do not warn if the unsigned quantity is an enumeration
11675 constant and its maximum value would fit in the result if the
11676 result were signed. */
11677 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11678 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11679 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11680 c_common_signed_type (base_type)))
11681 /* OK */;
11682 else
11683 warning_at (location,
11684 OPT_Wsign_compare,
11685 "comparison between signed and unsigned integer expressions");
11688 /* Warn if two unsigned values are being compared in a size larger
11689 than their original size, and one (and only one) is the result of
11690 a `~' operator. This comparison will always fail.
11692 Also warn if one operand is a constant, and the constant does not
11693 have all bits set that are set in the ~ operand when it is
11694 extended. */
11696 op0 = c_common_get_narrower (op0, &unsignedp0);
11697 op1 = c_common_get_narrower (op1, &unsignedp1);
11699 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11700 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11702 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11703 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11704 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11705 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11707 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11709 tree primop;
11710 HOST_WIDE_INT constant, mask;
11711 int unsignedp;
11712 unsigned int bits;
11714 if (tree_fits_shwi_p (op0))
11716 primop = op1;
11717 unsignedp = unsignedp1;
11718 constant = tree_to_shwi (op0);
11720 else
11722 primop = op0;
11723 unsignedp = unsignedp0;
11724 constant = tree_to_shwi (op1);
11727 bits = TYPE_PRECISION (TREE_TYPE (primop));
11728 if (bits < TYPE_PRECISION (result_type)
11729 && bits < HOST_BITS_PER_LONG && unsignedp)
11731 mask = (~ (HOST_WIDE_INT) 0) << bits;
11732 if ((mask & constant) != mask)
11734 if (constant == 0)
11735 warning_at (location, OPT_Wsign_compare,
11736 "promoted ~unsigned is always non-zero");
11737 else
11738 warning_at (location, OPT_Wsign_compare,
11739 "comparison of promoted ~unsigned with constant");
11743 else if (unsignedp0 && unsignedp1
11744 && (TYPE_PRECISION (TREE_TYPE (op0))
11745 < TYPE_PRECISION (result_type))
11746 && (TYPE_PRECISION (TREE_TYPE (op1))
11747 < TYPE_PRECISION (result_type)))
11748 warning_at (location, OPT_Wsign_compare,
11749 "comparison of promoted ~unsigned with unsigned");
11753 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11754 type via c_common_type. If -Wdouble-promotion is in use, and the
11755 conditions for warning have been met, issue a warning. GMSGID is
11756 the warning message. It must have two %T specifiers for the type
11757 that was converted (generally "float") and the type to which it was
11758 converted (generally "double), respectively. LOC is the location
11759 to which the awrning should refer. */
11761 void
11762 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11763 const char *gmsgid, location_t loc)
11765 tree source_type;
11767 if (!warn_double_promotion)
11768 return;
11769 /* If the conversion will not occur at run-time, there is no need to
11770 warn about it. */
11771 if (c_inhibit_evaluation_warnings)
11772 return;
11773 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11774 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11775 return;
11776 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11777 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11778 source_type = type1;
11779 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11780 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11781 source_type = type2;
11782 else
11783 return;
11784 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11787 /* Setup a TYPE_DECL node as a typedef representation.
11789 X is a TYPE_DECL for a typedef statement. Create a brand new
11790 ..._TYPE node (which will be just a variant of the existing
11791 ..._TYPE node with identical properties) and then install X
11792 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11794 The whole point here is to end up with a situation where each
11795 and every ..._TYPE node the compiler creates will be uniquely
11796 associated with AT MOST one node representing a typedef name.
11797 This way, even though the compiler substitutes corresponding
11798 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11799 early on, later parts of the compiler can always do the reverse
11800 translation and get back the corresponding typedef name. For
11801 example, given:
11803 typedef struct S MY_TYPE;
11804 MY_TYPE object;
11806 Later parts of the compiler might only know that `object' was of
11807 type `struct S' if it were not for code just below. With this
11808 code however, later parts of the compiler see something like:
11810 struct S' == struct S
11811 typedef struct S' MY_TYPE;
11812 struct S' object;
11814 And they can then deduce (from the node for type struct S') that
11815 the original object declaration was:
11817 MY_TYPE object;
11819 Being able to do this is important for proper support of protoize,
11820 and also for generating precise symbolic debugging information
11821 which takes full account of the programmer's (typedef) vocabulary.
11823 Obviously, we don't want to generate a duplicate ..._TYPE node if
11824 the TYPE_DECL node that we are now processing really represents a
11825 standard built-in type. */
11827 void
11828 set_underlying_type (tree x)
11830 if (x == error_mark_node)
11831 return;
11832 if (DECL_IS_BUILTIN (x))
11834 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11835 TYPE_NAME (TREE_TYPE (x)) = x;
11837 else if (TREE_TYPE (x) != error_mark_node
11838 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11840 tree tt = TREE_TYPE (x);
11841 DECL_ORIGINAL_TYPE (x) = tt;
11842 tt = build_variant_type_copy (tt);
11843 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11844 TYPE_NAME (tt) = x;
11845 TREE_USED (tt) = TREE_USED (x);
11846 TREE_TYPE (x) = tt;
11850 /* Record the types used by the current global variable declaration
11851 being parsed, so that we can decide later to emit their debug info.
11852 Those types are in types_used_by_cur_var_decl, and we are going to
11853 store them in the types_used_by_vars_hash hash table.
11854 DECL is the declaration of the global variable that has been parsed. */
11856 void
11857 record_types_used_by_current_var_decl (tree decl)
11859 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11861 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11863 tree type = types_used_by_cur_var_decl->pop ();
11864 types_used_by_var_decl_insert (type, decl);
11868 /* If DECL is a typedef that is declared in the current function,
11869 record it for the purpose of -Wunused-local-typedefs. */
11871 void
11872 record_locally_defined_typedef (tree decl)
11874 struct c_language_function *l;
11876 if (!warn_unused_local_typedefs
11877 || cfun == NULL
11878 /* if this is not a locally defined typedef then we are not
11879 interested. */
11880 || !is_typedef_decl (decl)
11881 || !decl_function_context (decl))
11882 return;
11884 l = (struct c_language_function *) cfun->language;
11885 vec_safe_push (l->local_typedefs, decl);
11888 /* If T is a TYPE_DECL declared locally, mark it as used. */
11890 void
11891 maybe_record_typedef_use (tree t)
11893 if (!is_typedef_decl (t))
11894 return;
11896 TREE_USED (t) = true;
11899 /* Warn if there are some unused locally defined typedefs in the
11900 current function. */
11902 void
11903 maybe_warn_unused_local_typedefs (void)
11905 int i;
11906 tree decl;
11907 /* The number of times we have emitted -Wunused-local-typedefs
11908 warnings. If this is different from errorcount, that means some
11909 unrelated errors have been issued. In which case, we'll avoid
11910 emitting "unused-local-typedefs" warnings. */
11911 static int unused_local_typedefs_warn_count;
11912 struct c_language_function *l;
11914 if (cfun == NULL)
11915 return;
11917 if ((l = (struct c_language_function *) cfun->language) == NULL)
11918 return;
11920 if (warn_unused_local_typedefs
11921 && errorcount == unused_local_typedefs_warn_count)
11923 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11924 if (!TREE_USED (decl))
11925 warning_at (DECL_SOURCE_LOCATION (decl),
11926 OPT_Wunused_local_typedefs,
11927 "typedef %qD locally defined but not used", decl);
11928 unused_local_typedefs_warn_count = errorcount;
11931 vec_free (l->local_typedefs);
11934 /* Warn about boolean expression compared with an integer value different
11935 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11936 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11937 are the operands of the comparison. The caller must ensure that
11938 either operand is a boolean expression. */
11940 void
11941 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11942 tree op1)
11944 if (TREE_CODE_CLASS (code) != tcc_comparison)
11945 return;
11947 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11948 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11949 if (!cst)
11950 return;
11952 if (!integer_zerop (cst) && !integer_onep (cst))
11954 int sign = (TREE_CODE (op0) == INTEGER_CST
11955 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
11956 if (code == EQ_EXPR
11957 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11958 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11959 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11960 "with boolean expression is always false", cst);
11961 else
11962 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11963 "with boolean expression is always true", cst);
11965 else if (integer_zerop (cst) || integer_onep (cst))
11967 /* If the non-constant operand isn't of a boolean type, we
11968 don't want to warn here. */
11969 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
11970 /* Handle booleans promoted to integers. */
11971 if (CONVERT_EXPR_P (noncst)
11972 && TREE_TYPE (noncst) == integer_type_node
11973 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
11974 /* Warn. */;
11975 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
11976 && !truth_value_p (TREE_CODE (noncst)))
11977 return;
11978 /* Do some magic to get the right diagnostics. */
11979 bool flag = TREE_CODE (op0) == INTEGER_CST;
11980 flag = integer_zerop (cst) ? flag : !flag;
11981 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
11982 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11983 "with boolean expression is always true", cst);
11984 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
11985 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11986 "with boolean expression is always false", cst);
11990 /* The C and C++ parsers both use vectors to hold function arguments.
11991 For efficiency, we keep a cache of unused vectors. This is the
11992 cache. */
11994 typedef vec<tree, va_gc> *tree_gc_vec;
11995 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11997 /* Return a new vector from the cache. If the cache is empty,
11998 allocate a new vector. These vectors are GC'ed, so it is OK if the
11999 pointer is not released.. */
12001 vec<tree, va_gc> *
12002 make_tree_vector (void)
12004 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12005 return tree_vector_cache->pop ();
12006 else
12008 /* Passing 0 to vec::alloc returns NULL, and our callers require
12009 that we always return a non-NULL value. The vector code uses
12010 4 when growing a NULL vector, so we do too. */
12011 vec<tree, va_gc> *v;
12012 vec_alloc (v, 4);
12013 return v;
12017 /* Release a vector of trees back to the cache. */
12019 void
12020 release_tree_vector (vec<tree, va_gc> *vec)
12022 if (vec != NULL)
12024 vec->truncate (0);
12025 vec_safe_push (tree_vector_cache, vec);
12029 /* Get a new tree vector holding a single tree. */
12031 vec<tree, va_gc> *
12032 make_tree_vector_single (tree t)
12034 vec<tree, va_gc> *ret = make_tree_vector ();
12035 ret->quick_push (t);
12036 return ret;
12039 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12041 vec<tree, va_gc> *
12042 make_tree_vector_from_list (tree list)
12044 vec<tree, va_gc> *ret = make_tree_vector ();
12045 for (; list; list = TREE_CHAIN (list))
12046 vec_safe_push (ret, TREE_VALUE (list));
12047 return ret;
12050 /* Get a new tree vector which is a copy of an existing one. */
12052 vec<tree, va_gc> *
12053 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12055 vec<tree, va_gc> *ret;
12056 unsigned int ix;
12057 tree t;
12059 ret = make_tree_vector ();
12060 vec_safe_reserve (ret, vec_safe_length (orig));
12061 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12062 ret->quick_push (t);
12063 return ret;
12066 /* Return true if KEYWORD starts a type specifier. */
12068 bool
12069 keyword_begins_type_specifier (enum rid keyword)
12071 switch (keyword)
12073 case RID_AUTO_TYPE:
12074 case RID_INT:
12075 case RID_CHAR:
12076 case RID_FLOAT:
12077 case RID_DOUBLE:
12078 case RID_VOID:
12079 case RID_UNSIGNED:
12080 case RID_LONG:
12081 case RID_SHORT:
12082 case RID_SIGNED:
12083 case RID_DFLOAT32:
12084 case RID_DFLOAT64:
12085 case RID_DFLOAT128:
12086 case RID_FRACT:
12087 case RID_ACCUM:
12088 case RID_BOOL:
12089 case RID_WCHAR:
12090 case RID_CHAR16:
12091 case RID_CHAR32:
12092 case RID_SAT:
12093 case RID_COMPLEX:
12094 case RID_TYPEOF:
12095 case RID_STRUCT:
12096 case RID_CLASS:
12097 case RID_UNION:
12098 case RID_ENUM:
12099 return true;
12100 default:
12101 if (keyword >= RID_FIRST_INT_N
12102 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12103 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12104 return true;
12105 return false;
12109 /* Return true if KEYWORD names a type qualifier. */
12111 bool
12112 keyword_is_type_qualifier (enum rid keyword)
12114 switch (keyword)
12116 case RID_CONST:
12117 case RID_VOLATILE:
12118 case RID_RESTRICT:
12119 case RID_ATOMIC:
12120 return true;
12121 default:
12122 return false;
12126 /* Return true if KEYWORD names a storage class specifier.
12128 RID_TYPEDEF is not included in this list despite `typedef' being
12129 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12130 such for syntactic convenience only. */
12132 bool
12133 keyword_is_storage_class_specifier (enum rid keyword)
12135 switch (keyword)
12137 case RID_STATIC:
12138 case RID_EXTERN:
12139 case RID_REGISTER:
12140 case RID_AUTO:
12141 case RID_MUTABLE:
12142 case RID_THREAD:
12143 return true;
12144 default:
12145 return false;
12149 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12151 static bool
12152 keyword_is_function_specifier (enum rid keyword)
12154 switch (keyword)
12156 case RID_INLINE:
12157 case RID_NORETURN:
12158 case RID_VIRTUAL:
12159 case RID_EXPLICIT:
12160 return true;
12161 default:
12162 return false;
12166 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12167 declaration-specifier (C99 6.7). */
12169 bool
12170 keyword_is_decl_specifier (enum rid keyword)
12172 if (keyword_is_storage_class_specifier (keyword)
12173 || keyword_is_type_qualifier (keyword)
12174 || keyword_is_function_specifier (keyword))
12175 return true;
12177 switch (keyword)
12179 case RID_TYPEDEF:
12180 case RID_FRIEND:
12181 case RID_CONSTEXPR:
12182 return true;
12183 default:
12184 return false;
12188 /* Initialize language-specific-bits of tree_contains_struct. */
12190 void
12191 c_common_init_ts (void)
12193 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12194 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12195 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12198 /* Build a user-defined numeric literal out of an integer constant type VALUE
12199 with identifier SUFFIX. */
12201 tree
12202 build_userdef_literal (tree suffix_id, tree value,
12203 enum overflow_type overflow, tree num_string)
12205 tree literal = make_node (USERDEF_LITERAL);
12206 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12207 USERDEF_LITERAL_VALUE (literal) = value;
12208 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12209 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12210 return literal;
12213 /* For vector[index], convert the vector to a
12214 pointer of the underlying type. Return true if the resulting
12215 ARRAY_REF should not be an lvalue. */
12217 bool
12218 convert_vector_to_pointer_for_subscript (location_t loc,
12219 tree *vecp, tree index)
12221 bool ret = false;
12222 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12224 tree type = TREE_TYPE (*vecp);
12225 tree type1;
12227 ret = !lvalue_p (*vecp);
12228 if (TREE_CODE (index) == INTEGER_CST)
12229 if (!tree_fits_uhwi_p (index)
12230 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12231 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12233 if (ret)
12235 tree tmp = create_tmp_var_raw (type);
12236 DECL_SOURCE_LOCATION (tmp) = loc;
12237 *vecp = c_save_expr (*vecp);
12238 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12240 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12241 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12242 *vecp
12243 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12244 *vecp, NULL_TREE, NULL_TREE),
12245 non_const);
12247 else
12248 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12249 NULL_TREE, NULL_TREE);
12250 SET_EXPR_LOCATION (*vecp, loc);
12251 c_common_mark_addressable_vec (tmp);
12253 else
12254 c_common_mark_addressable_vec (*vecp);
12255 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12256 type1 = build_pointer_type (TREE_TYPE (*vecp));
12257 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12258 if (!ref_all
12259 && !DECL_P (*vecp))
12261 /* If the original vector isn't declared may_alias and it
12262 isn't a bare vector look if the subscripting would
12263 alias the vector we subscript, and if not, force ref-all. */
12264 alias_set_type vecset = get_alias_set (*vecp);
12265 alias_set_type sset = get_alias_set (type);
12266 if (!alias_sets_must_conflict_p (sset, vecset)
12267 && !alias_set_subset_of (sset, vecset))
12268 ref_all = true;
12270 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12271 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12272 *vecp = convert (type, *vecp);
12274 return ret;
12277 /* Determine which of the operands, if any, is a scalar that needs to be
12278 converted to a vector, for the range of operations. */
12279 enum stv_conv
12280 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12281 bool complain)
12283 tree type0 = TREE_TYPE (op0);
12284 tree type1 = TREE_TYPE (op1);
12285 bool integer_only_op = false;
12286 enum stv_conv ret = stv_firstarg;
12288 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12289 || TREE_CODE (type1) == VECTOR_TYPE);
12290 switch (code)
12292 /* Most GENERIC binary expressions require homogeneous arguments.
12293 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12294 argument that is a vector and a second one that is a scalar, so
12295 we never return stv_secondarg for them. */
12296 case RSHIFT_EXPR:
12297 case LSHIFT_EXPR:
12298 if (TREE_CODE (type0) == INTEGER_TYPE
12299 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12301 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12303 if (complain)
12304 error_at (loc, "conversion of scalar %qT to vector %qT "
12305 "involves truncation", type0, type1);
12306 return stv_error;
12308 else
12309 return stv_firstarg;
12311 break;
12313 case BIT_IOR_EXPR:
12314 case BIT_XOR_EXPR:
12315 case BIT_AND_EXPR:
12316 integer_only_op = true;
12317 /* ... fall through ... */
12319 case VEC_COND_EXPR:
12321 case PLUS_EXPR:
12322 case MINUS_EXPR:
12323 case MULT_EXPR:
12324 case TRUNC_DIV_EXPR:
12325 case CEIL_DIV_EXPR:
12326 case FLOOR_DIV_EXPR:
12327 case ROUND_DIV_EXPR:
12328 case EXACT_DIV_EXPR:
12329 case TRUNC_MOD_EXPR:
12330 case FLOOR_MOD_EXPR:
12331 case RDIV_EXPR:
12332 case EQ_EXPR:
12333 case NE_EXPR:
12334 case LE_EXPR:
12335 case GE_EXPR:
12336 case LT_EXPR:
12337 case GT_EXPR:
12338 /* What about UNLT_EXPR? */
12339 if (TREE_CODE (type0) == VECTOR_TYPE)
12341 tree tmp;
12342 ret = stv_secondarg;
12343 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12344 tmp = type0; type0 = type1; type1 = tmp;
12345 tmp = op0; op0 = op1; op1 = tmp;
12348 if (TREE_CODE (type0) == INTEGER_TYPE
12349 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12351 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12353 if (complain)
12354 error_at (loc, "conversion of scalar %qT to vector %qT "
12355 "involves truncation", type0, type1);
12356 return stv_error;
12358 return ret;
12360 else if (!integer_only_op
12361 /* Allow integer --> real conversion if safe. */
12362 && (TREE_CODE (type0) == REAL_TYPE
12363 || TREE_CODE (type0) == INTEGER_TYPE)
12364 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12366 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12368 if (complain)
12369 error_at (loc, "conversion of scalar %qT to vector %qT "
12370 "involves truncation", type0, type1);
12371 return stv_error;
12373 return ret;
12375 default:
12376 break;
12379 return stv_nothing;
12382 /* Return true iff ALIGN is an integral constant that is a fundamental
12383 alignment, as defined by [basic.align] in the c++-11
12384 specifications.
12386 That is:
12388 [A fundamental alignment is represented by an alignment less than or
12389 equal to the greatest alignment supported by the implementation
12390 in all contexts, which is equal to
12391 alignof(max_align_t)]. */
12393 bool
12394 cxx_fundamental_alignment_p (unsigned align)
12396 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12397 TYPE_ALIGN (long_double_type_node)));
12400 /* Return true if T is a pointer to a zero-sized aggregate. */
12402 bool
12403 pointer_to_zero_sized_aggr_p (tree t)
12405 if (!POINTER_TYPE_P (t))
12406 return false;
12407 t = TREE_TYPE (t);
12408 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12411 #include "gt-c-family-c-common.h"