Merge trunk version 221445 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
blobde3c0ddaa270670b45bfc497ecfcb4ab56adb1a2
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 "tree-upc.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "calls.h"
34 #include "stringpool.h"
35 #include "attribs.h"
36 #include "varasm.h"
37 #include "trans-mem.h"
38 #include "flags.h"
39 #include "c-pragma.h"
40 #include "c-objc.h"
41 #include "tm_p.h"
42 #include "obstack.h"
43 #include "cpplib.h"
44 #include "target.h"
45 #include "common/common-target.h"
46 #include "langhooks.h"
47 #include "tree-inline.h"
48 #include "toplev.h"
49 #include "diagnostic.h"
50 #include "tree-iterator.h"
51 #include "hashtab.h"
52 #include "opts.h"
53 #include "hash-map.h"
54 #include "is-a.h"
55 #include "plugin-api.h"
56 #include "vec.h"
57 #include "hash-set.h"
58 #include "machmode.h"
59 #include "hard-reg-set.h"
60 #include "input.h"
61 #include "function.h"
62 #include "ipa-ref.h"
63 #include "cgraph.h"
64 #include "target-def.h"
65 #include "gimplify.h"
66 #include "wide-int-print.h"
67 #include "gimple-expr.h"
69 cpp_reader *parse_in; /* Declared in c-pragma.h. */
71 /* The following symbols are subsumed in the c_global_trees array, and
72 listed here individually for documentation purposes.
74 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
76 tree short_integer_type_node;
77 tree long_integer_type_node;
78 tree long_long_integer_type_node;
80 tree short_unsigned_type_node;
81 tree long_unsigned_type_node;
82 tree long_long_unsigned_type_node;
84 tree truthvalue_type_node;
85 tree truthvalue_false_node;
86 tree truthvalue_true_node;
88 tree ptrdiff_type_node;
90 tree unsigned_char_type_node;
91 tree signed_char_type_node;
92 tree wchar_type_node;
94 tree char16_type_node;
95 tree char32_type_node;
97 tree float_type_node;
98 tree double_type_node;
99 tree long_double_type_node;
101 tree complex_integer_type_node;
102 tree complex_float_type_node;
103 tree complex_double_type_node;
104 tree complex_long_double_type_node;
106 tree dfloat32_type_node;
107 tree dfloat64_type_node;
108 tree_dfloat128_type_node;
110 tree intQI_type_node;
111 tree intHI_type_node;
112 tree intSI_type_node;
113 tree intDI_type_node;
114 tree intTI_type_node;
116 tree unsigned_intQI_type_node;
117 tree unsigned_intHI_type_node;
118 tree unsigned_intSI_type_node;
119 tree unsigned_intDI_type_node;
120 tree unsigned_intTI_type_node;
122 tree widest_integer_literal_type_node;
123 tree widest_unsigned_literal_type_node;
125 Nodes for types `void *' and `const void *'.
127 tree ptr_type_node, const_ptr_type_node;
129 Nodes for types `char *' and `const char *'.
131 tree string_type_node, const_string_type_node;
133 Type `char[SOMENUMBER]'.
134 Used when an array of char is needed and the size is irrelevant.
136 tree char_array_type_node;
138 Type `wchar_t[SOMENUMBER]' or something like it.
139 Used when a wide string literal is created.
141 tree wchar_array_type_node;
143 Type `char16_t[SOMENUMBER]' or something like it.
144 Used when a UTF-16 string literal is created.
146 tree char16_array_type_node;
148 Type `char32_t[SOMENUMBER]' or something like it.
149 Used when a UTF-32 string literal is created.
151 tree char32_array_type_node;
153 Type `int ()' -- used for implicit declaration of functions.
155 tree default_function_type;
157 A VOID_TYPE node, packaged in a TREE_LIST.
159 tree void_list_node;
161 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
162 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
163 VAR_DECLS, but C++ does.)
165 tree function_name_decl_node;
166 tree pretty_function_name_decl_node;
167 tree c99_function_name_decl_node;
169 Stack of nested function name VAR_DECLs.
171 tree saved_function_name_decls;
175 tree c_global_trees[CTI_MAX];
177 /* Switches common to the C front ends. */
179 /* Nonzero means don't output line number information. */
181 char flag_no_line_commands;
183 /* Nonzero causes -E output not to be done, but directives such as
184 #define that have side effects are still obeyed. */
186 char flag_no_output;
188 /* Nonzero means dump macros in some fashion. */
190 char flag_dump_macros;
192 /* Nonzero means pass #include lines through to the output. */
194 char flag_dump_includes;
196 /* Nonzero means process PCH files while preprocessing. */
198 bool flag_pch_preprocess;
200 /* The file name to which we should write a precompiled header, or
201 NULL if no header will be written in this compile. */
203 const char *pch_file;
205 /* Nonzero if an ISO standard was selected. It rejects macros in the
206 user's namespace. */
207 int flag_iso;
210 /* C/ObjC language option variables. */
213 /* Nonzero means allow type mismatches in conditional expressions;
214 just make their values `void'. */
216 int flag_cond_mismatch;
218 /* Nonzero means enable C89 Amendment 1 features. */
220 int flag_isoc94;
222 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
224 int flag_isoc99;
226 /* Nonzero means use the ISO C11 dialect of C. */
228 int flag_isoc11;
230 /* Nonzero means that we have builtin functions, and main is an int. */
232 int flag_hosted = 1;
235 /* ObjC language option variables. */
238 /* Tells the compiler that this is a special run. Do not perform any
239 compiling, instead we are to test some platform dependent features
240 and output a C header file with appropriate definitions. */
242 int print_struct_values;
244 /* Tells the compiler what is the constant string class for ObjC. */
246 const char *constant_string_class_name;
248 /* UPC language option variables. */
250 /* Nonzero whenever UPC -fupc-threads-N is asserted.
251 The value N gives the number of UPC threads to be
252 defined at compile-time. */
253 int flag_upc_threads;
255 /* Nonzero whenever UPC -fupc-pthreads-model-* is asserted. */
256 int flag_upc_pthreads;
258 /* The implementation model for UPC threads that
259 are mapped to POSIX threads, specified at compilation
260 time by the -fupc-pthreads-model-* switch. */
261 upc_pthreads_model_kind upc_pthreads_model;
263 /* C++ language option variables. */
266 /* Nonzero means generate separate instantiation control files and
267 juggle them at link time. */
269 int flag_use_repository;
271 /* The C++ dialect being used. C++98 is the default. */
273 enum cxx_dialect cxx_dialect = cxx98;
275 /* Maximum template instantiation depth. This limit exists to limit the
276 time it takes to notice excessively recursive template instantiations.
278 The default is lower than the 1024 recommended by the C++0x standard
279 because G++ runs out of stack before 1024 with highly recursive template
280 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
282 int max_tinst_depth = 900;
284 /* The elements of `ridpointers' are identifier nodes for the reserved
285 type names and storage classes. It is indexed by a RID_... value. */
286 tree *ridpointers;
288 tree (*make_fname_decl) (location_t, tree, int);
290 /* Nonzero means don't warn about problems that occur when the code is
291 executed. */
292 int c_inhibit_evaluation_warnings;
294 /* Whether we are building a boolean conversion inside
295 convert_for_assignment, or some other late binary operation. If
296 build_binary_op is called for C (from code shared by C and C++) in
297 this case, then the operands have already been folded and the
298 result will not be folded again, so C_MAYBE_CONST_EXPR should not
299 be generated. */
300 bool in_late_binary_op;
302 /* Whether lexing has been completed, so subsequent preprocessor
303 errors should use the compiler's input_location. */
304 bool done_lexing = false;
306 /* Information about how a function name is generated. */
307 struct fname_var_t
309 tree *const decl; /* pointer to the VAR_DECL. */
310 const unsigned rid; /* RID number for the identifier. */
311 const int pretty; /* How pretty is it? */
314 /* The three ways of getting then name of the current function. */
316 const struct fname_var_t fname_vars[] =
318 /* C99 compliant __func__, must be first. */
319 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
320 /* GCC __FUNCTION__ compliant. */
321 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
322 /* GCC __PRETTY_FUNCTION__ compliant. */
323 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
324 {NULL, 0, 0},
327 /* Global visibility options. */
328 struct visibility_flags visibility_options;
330 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
331 static tree check_case_value (location_t, tree);
332 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
334 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
341 int, bool *);
342 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
343 int, bool *);
344 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
345 bool *);
346 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_always_inline_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
360 bool *);
361 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
362 bool *);
363 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_transparent_union_attribute (tree *, tree, tree,
365 int, bool *);
366 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
372 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
373 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
376 static tree handle_visibility_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_tls_model_attribute (tree *, tree, tree, int,
379 bool *);
380 static tree handle_no_instrument_function_attribute (tree *, tree,
381 tree, int, bool *);
382 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
385 bool *);
386 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_deprecated_attribute (tree *, tree, tree, int,
391 bool *);
392 static tree handle_vector_size_attribute (tree *, tree, tree, int,
393 bool *);
394 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
395 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
396 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
397 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
398 bool *);
399 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
400 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
401 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
402 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
403 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
404 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
405 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
406 static tree ignore_attribute (tree *, tree, tree, int, bool *);
407 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
408 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
409 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
410 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
411 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
412 bool *);
413 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
414 bool *);
415 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
416 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
417 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
418 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
420 static void check_function_nonnull (tree, int, tree *);
421 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
422 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
423 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
424 static int resort_field_decl_cmp (const void *, const void *);
426 /* Reserved words. The third field is a mask: keywords are disabled
427 if they match the mask.
429 Masks for languages:
430 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
431 C --std=c99: D_CXXONLY | D_OBJC
432 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
433 UPC is like C except that D_UPC is not set
434 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | D_UPC
435 C++ --std=c0x: D_CONLY | D_OBJC | D_UPC
436 ObjC++ is like C++ except that D_OBJC is not set
438 If -fno-asm is used, D_ASM is added to the mask. If
439 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
440 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
441 In C with -Wc++-compat, we warn if D_CXXWARN is set.
443 Note the complication of the D_CXX_OBJC keywords. These are
444 reserved words such as 'class'. In C++, 'class' is a reserved
445 word. In Objective-C++ it is too. In Objective-C, it is a
446 reserved word too, but only if it follows an '@' sign.
448 const struct c_common_resword c_common_reswords[] =
450 { "_Alignas", RID_ALIGNAS, D_CONLY },
451 { "_Alignof", RID_ALIGNOF, D_CONLY },
452 { "_Atomic", RID_ATOMIC, D_CONLY },
453 { "_Bool", RID_BOOL, D_CONLY },
454 { "_Complex", RID_COMPLEX, 0 },
455 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
456 { "_Cilk_sync", RID_CILK_SYNC, 0 },
457 { "_Cilk_for", RID_CILK_FOR, 0 },
458 { "_Imaginary", RID_IMAGINARY, D_CONLY },
459 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
460 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
461 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
462 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
463 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
464 { "_Sat", RID_SAT, D_CONLY | D_EXT },
465 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
466 { "_Noreturn", RID_NORETURN, D_CONLY },
467 { "_Generic", RID_GENERIC, D_CONLY },
468 { "_Thread_local", RID_THREAD, D_CONLY },
469 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
470 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
471 { "__alignof", RID_ALIGNOF, 0 },
472 { "__alignof__", RID_ALIGNOF, 0 },
473 { "__asm", RID_ASM, 0 },
474 { "__asm__", RID_ASM, 0 },
475 { "__attribute", RID_ATTRIBUTE, 0 },
476 { "__attribute__", RID_ATTRIBUTE, 0 },
477 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
478 { "__bases", RID_BASES, D_CXXONLY },
479 { "__builtin_call_with_static_chain",
480 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
481 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
482 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
483 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
484 { "__builtin_offsetof", RID_OFFSETOF, 0 },
485 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
486 { "__builtin_va_arg", RID_VA_ARG, 0 },
487 { "__complex", RID_COMPLEX, 0 },
488 { "__complex__", RID_COMPLEX, 0 },
489 { "__const", RID_CONST, 0 },
490 { "__const__", RID_CONST, 0 },
491 { "__decltype", RID_DECLTYPE, D_CXXONLY },
492 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
493 { "__extension__", RID_EXTENSION, 0 },
494 { "__func__", RID_C99_FUNCTION_NAME, 0 },
495 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
496 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
497 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
498 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
499 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
500 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
501 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
502 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
503 { "__imag", RID_IMAGPART, 0 },
504 { "__imag__", RID_IMAGPART, 0 },
505 { "__inline", RID_INLINE, 0 },
506 { "__inline__", RID_INLINE, 0 },
507 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
508 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
509 { "__is_class", RID_IS_CLASS, D_CXXONLY },
510 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
511 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
512 { "__is_final", RID_IS_FINAL, D_CXXONLY },
513 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
514 { "__is_pod", RID_IS_POD, D_CXXONLY },
515 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
516 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
517 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
518 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
519 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
520 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
521 { "__is_union", RID_IS_UNION, D_CXXONLY },
522 { "__label__", RID_LABEL, 0 },
523 { "__null", RID_NULL, 0 },
524 { "__real", RID_REALPART, 0 },
525 { "__real__", RID_REALPART, 0 },
526 { "__restrict", RID_RESTRICT, 0 },
527 { "__restrict__", RID_RESTRICT, 0 },
528 { "__signed", RID_SIGNED, 0 },
529 { "__signed__", RID_SIGNED, 0 },
530 { "__thread", RID_THREAD, 0 },
531 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
532 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
533 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
534 { "__typeof", RID_TYPEOF, 0 },
535 { "__typeof__", RID_TYPEOF, 0 },
536 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
537 { "__volatile", RID_VOLATILE, 0 },
538 { "__volatile__", RID_VOLATILE, 0 },
539 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
540 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
541 { "asm", RID_ASM, D_ASM },
542 { "auto", RID_AUTO, 0 },
543 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
544 { "break", RID_BREAK, 0 },
545 { "case", RID_CASE, 0 },
546 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
547 { "char", RID_CHAR, 0 },
548 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
549 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
550 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
551 { "const", RID_CONST, 0 },
552 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
553 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
554 { "continue", RID_CONTINUE, 0 },
555 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
556 { "default", RID_DEFAULT, 0 },
557 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
558 { "do", RID_DO, 0 },
559 { "double", RID_DOUBLE, 0 },
560 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
561 { "else", RID_ELSE, 0 },
562 { "enum", RID_ENUM, 0 },
563 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
564 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
565 { "extern", RID_EXTERN, 0 },
566 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
567 { "float", RID_FLOAT, 0 },
568 { "for", RID_FOR, 0 },
569 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
570 { "goto", RID_GOTO, 0 },
571 { "if", RID_IF, 0 },
572 { "inline", RID_INLINE, D_EXT89 },
573 { "int", RID_INT, 0 },
574 { "long", RID_LONG, 0 },
575 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
576 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
577 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
578 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
579 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
580 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
581 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
582 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
583 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
584 { "register", RID_REGISTER, 0 },
585 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
586 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
587 { "return", RID_RETURN, 0 },
588 { "short", RID_SHORT, 0 },
589 { "signed", RID_SIGNED, 0 },
590 { "sizeof", RID_SIZEOF, 0 },
591 { "static", RID_STATIC, 0 },
592 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
593 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
594 { "struct", RID_STRUCT, 0 },
595 { "switch", RID_SWITCH, 0 },
596 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
597 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
598 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
599 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
600 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
601 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
602 { "typedef", RID_TYPEDEF, 0 },
603 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
604 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
605 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
606 { "union", RID_UNION, 0 },
607 { "unsigned", RID_UNSIGNED, 0 },
608 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
609 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
610 { "void", RID_VOID, 0 },
611 { "volatile", RID_VOLATILE, 0 },
612 { "wchar_t", RID_WCHAR, D_CXXONLY },
613 { "while", RID_WHILE, 0 },
614 /* These Objective-C keywords are recognized only immediately after
615 an '@'. */
616 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
617 { "defs", RID_AT_DEFS, D_OBJC },
618 { "encode", RID_AT_ENCODE, D_OBJC },
619 { "end", RID_AT_END, D_OBJC },
620 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
621 { "interface", RID_AT_INTERFACE, D_OBJC },
622 { "protocol", RID_AT_PROTOCOL, D_OBJC },
623 { "selector", RID_AT_SELECTOR, D_OBJC },
624 { "finally", RID_AT_FINALLY, D_OBJC },
625 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
626 { "optional", RID_AT_OPTIONAL, D_OBJC },
627 { "required", RID_AT_REQUIRED, D_OBJC },
628 { "property", RID_AT_PROPERTY, D_OBJC },
629 { "package", RID_AT_PACKAGE, D_OBJC },
630 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
631 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
632 /* These are recognized only in protocol-qualifier context
633 (see above) */
634 { "bycopy", RID_BYCOPY, D_OBJC },
635 { "byref", RID_BYREF, D_OBJC },
636 { "in", RID_IN, D_OBJC },
637 { "inout", RID_INOUT, D_OBJC },
638 { "oneway", RID_ONEWAY, D_OBJC },
639 { "out", RID_OUT, D_OBJC },
641 /* UPC keywords */
642 { "shared", RID_SHARED, D_UPC },
643 { "relaxed", RID_RELAXED, D_UPC },
644 { "strict", RID_STRICT, D_UPC },
645 { "upc_barrier", RID_UPC_BARRIER, D_UPC },
646 { "upc_blocksizeof", RID_UPC_BLOCKSIZEOF, D_UPC },
647 { "upc_elemsizeof", RID_UPC_ELEMSIZEOF, D_UPC },
648 { "upc_forall", RID_UPC_FORALL, D_UPC },
649 { "upc_localsizeof", RID_UPC_LOCALSIZEOF, D_UPC },
650 { "upc_notify", RID_UPC_NOTIFY, D_UPC },
651 { "upc_wait", RID_UPC_WAIT, D_UPC },
653 /* These are recognized inside a property attribute list */
654 { "assign", RID_ASSIGN, D_OBJC },
655 { "copy", RID_COPY, D_OBJC },
656 { "getter", RID_GETTER, D_OBJC },
657 { "nonatomic", RID_NONATOMIC, D_OBJC },
658 { "readonly", RID_READONLY, D_OBJC },
659 { "readwrite", RID_READWRITE, D_OBJC },
660 { "retain", RID_RETAIN, D_OBJC },
661 { "setter", RID_SETTER, D_OBJC },
664 const unsigned int num_c_common_reswords =
665 sizeof c_common_reswords / sizeof (struct c_common_resword);
667 /* Table of machine-independent attributes common to all C-like languages.
669 All attributes referencing arguments should be additionally processed
670 in chkp_copy_function_type_adding_bounds for correct instrumentation
671 by Pointer Bounds Checker.
672 Current list of processed common attributes: nonnull. */
673 const struct attribute_spec c_common_attribute_table[] =
675 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
676 affects_type_identity } */
677 { "packed", 0, 0, false, false, false,
678 handle_packed_attribute , false},
679 { "nocommon", 0, 0, true, false, false,
680 handle_nocommon_attribute, false},
681 { "common", 0, 0, true, false, false,
682 handle_common_attribute, false },
683 /* FIXME: logically, noreturn attributes should be listed as
684 "false, true, true" and apply to function types. But implementing this
685 would require all the places in the compiler that use TREE_THIS_VOLATILE
686 on a decl to identify non-returning functions to be located and fixed
687 to check the function type instead. */
688 { "noreturn", 0, 0, true, false, false,
689 handle_noreturn_attribute, false },
690 { "volatile", 0, 0, true, false, false,
691 handle_noreturn_attribute, false },
692 { "stack_protect", 0, 0, true, false, false,
693 handle_stack_protect_attribute, false },
694 { "noinline", 0, 0, true, false, false,
695 handle_noinline_attribute, false },
696 { "noclone", 0, 0, true, false, false,
697 handle_noclone_attribute, false },
698 { "no_icf", 0, 0, true, false, false,
699 handle_noicf_attribute, false },
700 { "leaf", 0, 0, true, false, false,
701 handle_leaf_attribute, false },
702 { "always_inline", 0, 0, true, false, false,
703 handle_always_inline_attribute, false },
704 { "gnu_inline", 0, 0, true, false, false,
705 handle_gnu_inline_attribute, false },
706 { "artificial", 0, 0, true, false, false,
707 handle_artificial_attribute, false },
708 { "flatten", 0, 0, true, false, false,
709 handle_flatten_attribute, false },
710 { "used", 0, 0, true, false, false,
711 handle_used_attribute, false },
712 { "unused", 0, 0, false, false, false,
713 handle_unused_attribute, false },
714 { "externally_visible", 0, 0, true, false, false,
715 handle_externally_visible_attribute, false },
716 { "no_reorder", 0, 0, true, false, false,
717 handle_no_reorder_attribute, false },
718 /* The same comments as for noreturn attributes apply to const ones. */
719 { "const", 0, 0, true, false, false,
720 handle_const_attribute, false },
721 { "transparent_union", 0, 0, false, false, false,
722 handle_transparent_union_attribute, false },
723 { "constructor", 0, 1, true, false, false,
724 handle_constructor_attribute, false },
725 { "destructor", 0, 1, true, false, false,
726 handle_destructor_attribute, false },
727 { "mode", 1, 1, false, true, false,
728 handle_mode_attribute, false },
729 { "section", 1, 1, true, false, false,
730 handle_section_attribute, false },
731 { "aligned", 0, 1, false, false, false,
732 handle_aligned_attribute, false },
733 { "weak", 0, 0, true, false, false,
734 handle_weak_attribute, false },
735 { "ifunc", 1, 1, true, false, false,
736 handle_ifunc_attribute, false },
737 { "alias", 1, 1, true, false, false,
738 handle_alias_attribute, false },
739 { "weakref", 0, 1, true, false, false,
740 handle_weakref_attribute, false },
741 { "no_instrument_function", 0, 0, true, false, false,
742 handle_no_instrument_function_attribute,
743 false },
744 { "malloc", 0, 0, true, false, false,
745 handle_malloc_attribute, false },
746 { "returns_twice", 0, 0, true, false, false,
747 handle_returns_twice_attribute, false },
748 { "no_stack_limit", 0, 0, true, false, false,
749 handle_no_limit_stack_attribute, false },
750 { "pure", 0, 0, true, false, false,
751 handle_pure_attribute, false },
752 { "transaction_callable", 0, 0, false, true, false,
753 handle_tm_attribute, false },
754 { "transaction_unsafe", 0, 0, false, true, false,
755 handle_tm_attribute, false },
756 { "transaction_safe", 0, 0, false, true, false,
757 handle_tm_attribute, false },
758 { "transaction_may_cancel_outer", 0, 0, false, true, false,
759 handle_tm_attribute, false },
760 /* ??? These two attributes didn't make the transition from the
761 Intel language document to the multi-vendor language document. */
762 { "transaction_pure", 0, 0, false, true, false,
763 handle_tm_attribute, false },
764 { "transaction_wrap", 1, 1, true, false, false,
765 handle_tm_wrap_attribute, false },
766 /* For internal use (marking of builtins) only. The name contains space
767 to prevent its usage in source code. */
768 { "no vops", 0, 0, true, false, false,
769 handle_novops_attribute, false },
770 { "deprecated", 0, 1, false, false, false,
771 handle_deprecated_attribute, false },
772 { "vector_size", 1, 1, false, true, false,
773 handle_vector_size_attribute, false },
774 { "visibility", 1, 1, false, false, false,
775 handle_visibility_attribute, false },
776 { "tls_model", 1, 1, true, false, false,
777 handle_tls_model_attribute, false },
778 { "nonnull", 0, -1, false, true, true,
779 handle_nonnull_attribute, false },
780 { "nothrow", 0, 0, true, false, false,
781 handle_nothrow_attribute, false },
782 { "may_alias", 0, 0, false, true, false, NULL, false },
783 { "cleanup", 1, 1, true, false, false,
784 handle_cleanup_attribute, false },
785 { "warn_unused_result", 0, 0, false, true, true,
786 handle_warn_unused_result_attribute, false },
787 { "sentinel", 0, 1, false, true, true,
788 handle_sentinel_attribute, false },
789 /* For internal use (marking of builtins) only. The name contains space
790 to prevent its usage in source code. */
791 { "type generic", 0, 0, false, true, true,
792 handle_type_generic_attribute, false },
793 { "alloc_size", 1, 2, false, true, true,
794 handle_alloc_size_attribute, false },
795 { "cold", 0, 0, true, false, false,
796 handle_cold_attribute, false },
797 { "hot", 0, 0, true, false, false,
798 handle_hot_attribute, false },
799 { "no_address_safety_analysis",
800 0, 0, true, false, false,
801 handle_no_address_safety_analysis_attribute,
802 false },
803 { "no_sanitize_address", 0, 0, true, false, false,
804 handle_no_sanitize_address_attribute,
805 false },
806 { "no_sanitize_thread", 0, 0, true, false, false,
807 handle_no_sanitize_address_attribute,
808 false },
809 { "no_sanitize_undefined", 0, 0, true, false, false,
810 handle_no_sanitize_undefined_attribute,
811 false },
812 { "warning", 1, 1, true, false, false,
813 handle_error_attribute, false },
814 { "error", 1, 1, true, false, false,
815 handle_error_attribute, false },
816 { "target", 1, -1, true, false, false,
817 handle_target_attribute, false },
818 { "optimize", 1, -1, true, false, false,
819 handle_optimize_attribute, false },
820 /* For internal use only. The leading '*' both prevents its usage in
821 source code and signals that it may be overridden by machine tables. */
822 { "*tm regparm", 0, 0, false, true, true,
823 ignore_attribute, false },
824 { "no_split_stack", 0, 0, true, false, false,
825 handle_no_split_stack_attribute, false },
826 /* For internal use (marking of builtins and runtime functions) only.
827 The name contains space to prevent its usage in source code. */
828 { "fn spec", 1, 1, false, true, true,
829 handle_fnspec_attribute, false },
830 { "warn_unused", 0, 0, false, false, false,
831 handle_warn_unused_attribute, false },
832 { "returns_nonnull", 0, 0, false, true, true,
833 handle_returns_nonnull_attribute, false },
834 { "omp declare simd", 0, -1, true, false, false,
835 handle_omp_declare_simd_attribute, false },
836 { "cilk simd function", 0, -1, true, false, false,
837 handle_omp_declare_simd_attribute, false },
838 { "omp declare target", 0, 0, true, false, false,
839 handle_omp_declare_target_attribute, false },
840 { "alloc_align", 1, 1, false, true, true,
841 handle_alloc_align_attribute, false },
842 { "assume_aligned", 1, 2, false, true, true,
843 handle_assume_aligned_attribute, false },
844 { "designated_init", 0, 0, false, true, false,
845 handle_designated_init_attribute, false },
846 { "bnd_variable_size", 0, 0, true, false, false,
847 handle_bnd_variable_size_attribute, false },
848 { "bnd_legacy", 0, 0, true, false, false,
849 handle_bnd_legacy, false },
850 { "bnd_instrument", 0, 0, true, false, false,
851 handle_bnd_instrument, false },
852 { NULL, 0, 0, false, false, false, NULL, false }
855 /* Give the specifications for the format attributes, used by C and all
856 descendants.
858 All attributes referencing arguments should be additionally processed
859 in chkp_copy_function_type_adding_bounds for correct instrumentation
860 by Pointer Bounds Checker.
861 Current list of processed format attributes: format, format_arg. */
862 const struct attribute_spec c_common_format_attribute_table[] =
864 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
865 affects_type_identity } */
866 { "format", 3, 3, false, true, true,
867 handle_format_attribute, false },
868 { "format_arg", 1, 1, false, true, true,
869 handle_format_arg_attribute, false },
870 { NULL, 0, 0, false, false, false, NULL, false }
873 /* Return identifier for address space AS. */
875 const char *
876 c_addr_space_name (addr_space_t as)
878 int rid = RID_FIRST_ADDR_SPACE + as;
879 gcc_assert (ridpointers [rid]);
880 return IDENTIFIER_POINTER (ridpointers [rid]);
883 /* Push current bindings for the function name VAR_DECLS. */
885 void
886 start_fname_decls (void)
888 unsigned ix;
889 tree saved = NULL_TREE;
891 for (ix = 0; fname_vars[ix].decl; ix++)
893 tree decl = *fname_vars[ix].decl;
895 if (decl)
897 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
898 saved);
899 *fname_vars[ix].decl = NULL_TREE;
902 if (saved || saved_function_name_decls)
903 /* Normally they'll have been NULL, so only push if we've got a
904 stack, or they are non-NULL. */
905 saved_function_name_decls = tree_cons (saved, NULL_TREE,
906 saved_function_name_decls);
909 /* Finish up the current bindings, adding them into the current function's
910 statement tree. This must be done _before_ finish_stmt_tree is called.
911 If there is no current function, we must be at file scope and no statements
912 are involved. Pop the previous bindings. */
914 void
915 finish_fname_decls (void)
917 unsigned ix;
918 tree stmts = NULL_TREE;
919 tree stack = saved_function_name_decls;
921 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
922 append_to_statement_list (TREE_VALUE (stack), &stmts);
924 if (stmts)
926 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
928 if (TREE_CODE (*bodyp) == BIND_EXPR)
929 bodyp = &BIND_EXPR_BODY (*bodyp);
931 append_to_statement_list_force (*bodyp, &stmts);
932 *bodyp = stmts;
935 for (ix = 0; fname_vars[ix].decl; ix++)
936 *fname_vars[ix].decl = NULL_TREE;
938 if (stack)
940 /* We had saved values, restore them. */
941 tree saved;
943 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
945 tree decl = TREE_PURPOSE (saved);
946 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
948 *fname_vars[ix].decl = decl;
950 stack = TREE_CHAIN (stack);
952 saved_function_name_decls = stack;
955 /* Return the text name of the current function, suitably prettified
956 by PRETTY_P. Return string must be freed by caller. */
958 const char *
959 fname_as_string (int pretty_p)
961 const char *name = "top level";
962 char *namep;
963 int vrb = 2, len;
964 cpp_string cstr = { 0, 0 }, strname;
966 if (!pretty_p)
968 name = "";
969 vrb = 0;
972 if (current_function_decl)
973 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
975 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
977 namep = XNEWVEC (char, len);
978 snprintf (namep, len, "\"%s\"", name);
979 strname.text = (unsigned char *) namep;
980 strname.len = len - 1;
982 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
984 XDELETEVEC (namep);
985 return (const char *) cstr.text;
988 return namep;
991 /* Return the VAR_DECL for a const char array naming the current
992 function. If the VAR_DECL has not yet been created, create it
993 now. RID indicates how it should be formatted and IDENTIFIER_NODE
994 ID is its name (unfortunately C and C++ hold the RID values of
995 keywords in different places, so we can't derive RID from ID in
996 this language independent code. LOC is the location of the
997 function. */
999 tree
1000 fname_decl (location_t loc, unsigned int rid, tree id)
1002 unsigned ix;
1003 tree decl = NULL_TREE;
1005 for (ix = 0; fname_vars[ix].decl; ix++)
1006 if (fname_vars[ix].rid == rid)
1007 break;
1009 decl = *fname_vars[ix].decl;
1010 if (!decl)
1012 /* If a tree is built here, it would normally have the lineno of
1013 the current statement. Later this tree will be moved to the
1014 beginning of the function and this line number will be wrong.
1015 To avoid this problem set the lineno to 0 here; that prevents
1016 it from appearing in the RTL. */
1017 tree stmts;
1018 location_t saved_location = input_location;
1019 input_location = UNKNOWN_LOCATION;
1021 stmts = push_stmt_list ();
1022 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1023 stmts = pop_stmt_list (stmts);
1024 if (!IS_EMPTY_STMT (stmts))
1025 saved_function_name_decls
1026 = tree_cons (decl, stmts, saved_function_name_decls);
1027 *fname_vars[ix].decl = decl;
1028 input_location = saved_location;
1030 if (!ix && !current_function_decl)
1031 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1033 return decl;
1036 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1038 tree
1039 fix_string_type (tree value)
1041 int length = TREE_STRING_LENGTH (value);
1042 int nchars;
1043 tree e_type, i_type, a_type;
1045 /* Compute the number of elements, for the array type. */
1046 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1048 nchars = length;
1049 e_type = char_type_node;
1051 else if (TREE_TYPE (value) == char16_array_type_node)
1053 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1054 e_type = char16_type_node;
1056 else if (TREE_TYPE (value) == char32_array_type_node)
1058 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1059 e_type = char32_type_node;
1061 else
1063 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1064 e_type = wchar_type_node;
1067 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1068 limit in C++98 Annex B is very large (65536) and is not normative,
1069 so we do not diagnose it (warn_overlength_strings is forced off
1070 in c_common_post_options). */
1071 if (warn_overlength_strings)
1073 const int nchars_max = flag_isoc99 ? 4095 : 509;
1074 const int relevant_std = flag_isoc99 ? 99 : 90;
1075 if (nchars - 1 > nchars_max)
1076 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1077 separate the %d from the 'C'. 'ISO' should not be
1078 translated, but it may be moved after 'C%d' in languages
1079 where modifiers follow nouns. */
1080 pedwarn (input_location, OPT_Woverlength_strings,
1081 "string length %qd is greater than the length %qd "
1082 "ISO C%d compilers are required to support",
1083 nchars - 1, nchars_max, relevant_std);
1086 /* Create the array type for the string constant. The ISO C++
1087 standard says that a string literal has type `const char[N]' or
1088 `const wchar_t[N]'. We use the same logic when invoked as a C
1089 front-end with -Wwrite-strings.
1090 ??? We should change the type of an expression depending on the
1091 state of a warning flag. We should just be warning -- see how
1092 this is handled in the C++ front-end for the deprecated implicit
1093 conversion from string literals to `char*' or `wchar_t*'.
1095 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1096 array type being the unqualified version of that type.
1097 Therefore, if we are constructing an array of const char, we must
1098 construct the matching unqualified array type first. The C front
1099 end does not require this, but it does no harm, so we do it
1100 unconditionally. */
1101 i_type = build_index_type (size_int (nchars - 1));
1102 a_type = build_array_type (e_type, i_type);
1103 if (c_dialect_cxx() || warn_write_strings)
1104 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1106 TREE_TYPE (value) = a_type;
1107 TREE_CONSTANT (value) = 1;
1108 TREE_READONLY (value) = 1;
1109 TREE_STATIC (value) = 1;
1110 return value;
1113 /* If DISABLE is true, stop issuing warnings. This is used when
1114 parsing code that we know will not be executed. This function may
1115 be called multiple times, and works as a stack. */
1117 static void
1118 c_disable_warnings (bool disable)
1120 if (disable)
1122 ++c_inhibit_evaluation_warnings;
1123 fold_defer_overflow_warnings ();
1127 /* If ENABLE is true, reenable issuing warnings. */
1129 static void
1130 c_enable_warnings (bool enable)
1132 if (enable)
1134 --c_inhibit_evaluation_warnings;
1135 fold_undefer_and_ignore_overflow_warnings ();
1139 /* Fully fold EXPR, an expression that was not folded (beyond integer
1140 constant expressions and null pointer constants) when being built
1141 up. If IN_INIT, this is in a static initializer and certain
1142 changes are made to the folding done. Clear *MAYBE_CONST if
1143 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1144 expression because it contains an evaluated operator (in C99) or an
1145 operator outside of sizeof returning an integer constant (in C90)
1146 not permitted in constant expressions, or because it contains an
1147 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1148 set to true by callers before calling this function.) Return the
1149 folded expression. Function arguments have already been folded
1150 before calling this function, as have the contents of SAVE_EXPR,
1151 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1152 C_MAYBE_CONST_EXPR. */
1154 tree
1155 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1157 tree ret;
1158 tree eptype = NULL_TREE;
1159 bool dummy = true;
1160 bool maybe_const_itself = true;
1161 location_t loc = EXPR_LOCATION (expr);
1163 /* This function is not relevant to C++ because C++ folds while
1164 parsing, and may need changes to be correct for C++ when C++
1165 stops folding while parsing. */
1166 if (c_dialect_cxx ())
1167 gcc_unreachable ();
1169 if (!maybe_const)
1170 maybe_const = &dummy;
1171 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1173 eptype = TREE_TYPE (expr);
1174 expr = TREE_OPERAND (expr, 0);
1176 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1177 &maybe_const_itself);
1178 if (eptype)
1179 ret = fold_convert_loc (loc, eptype, ret);
1180 *maybe_const &= maybe_const_itself;
1181 return ret;
1184 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1185 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1186 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1187 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1188 both evaluated and unevaluated subexpressions while
1189 *MAYBE_CONST_ITSELF is carried from only evaluated
1190 subexpressions). */
1192 static tree
1193 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1194 bool *maybe_const_itself)
1196 tree ret = expr;
1197 enum tree_code code = TREE_CODE (expr);
1198 enum tree_code_class kind = TREE_CODE_CLASS (code);
1199 location_t loc = EXPR_LOCATION (expr);
1200 tree op0, op1, op2, op3;
1201 tree orig_op0, orig_op1, orig_op2;
1202 bool op0_const = true, op1_const = true, op2_const = true;
1203 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1204 bool nowarning = TREE_NO_WARNING (expr);
1205 bool unused_p;
1207 /* This function is not relevant to C++ because C++ folds while
1208 parsing, and may need changes to be correct for C++ when C++
1209 stops folding while parsing. */
1210 if (c_dialect_cxx ())
1211 gcc_unreachable ();
1213 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1214 anything else not counted as an expression cannot usefully be
1215 folded further at this point. */
1216 if (!IS_EXPR_CODE_CLASS (kind)
1217 || kind == tcc_statement
1218 || code == SAVE_EXPR)
1219 return expr;
1221 /* Operands of variable-length expressions (function calls) have
1222 already been folded, as have __builtin_* function calls, and such
1223 expressions cannot occur in constant expressions. */
1224 if (kind == tcc_vl_exp)
1226 *maybe_const_operands = false;
1227 ret = fold (expr);
1228 goto out;
1231 if (code == C_MAYBE_CONST_EXPR)
1233 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1234 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1235 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1236 *maybe_const_operands = false;
1237 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1238 *maybe_const_itself = false;
1239 if (pre && !in_init)
1240 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1241 else
1242 ret = inner;
1243 goto out;
1246 /* Assignment, increment, decrement, function call and comma
1247 operators, and statement expressions, cannot occur in constant
1248 expressions if evaluated / outside of sizeof. (Function calls
1249 were handled above, though VA_ARG_EXPR is treated like a function
1250 call here, and statement expressions are handled through
1251 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1252 switch (code)
1254 case MODIFY_EXPR:
1255 case PREDECREMENT_EXPR:
1256 case PREINCREMENT_EXPR:
1257 case POSTDECREMENT_EXPR:
1258 case POSTINCREMENT_EXPR:
1259 case COMPOUND_EXPR:
1260 *maybe_const_operands = false;
1261 break;
1263 case VA_ARG_EXPR:
1264 case TARGET_EXPR:
1265 case BIND_EXPR:
1266 case OBJ_TYPE_REF:
1267 *maybe_const_operands = false;
1268 ret = fold (expr);
1269 goto out;
1271 default:
1272 break;
1275 /* Fold individual tree codes as appropriate. */
1276 switch (code)
1278 case COMPOUND_LITERAL_EXPR:
1279 /* Any non-constancy will have been marked in a containing
1280 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1281 goto out;
1283 case COMPONENT_REF:
1284 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1285 op1 = TREE_OPERAND (expr, 1);
1286 op2 = TREE_OPERAND (expr, 2);
1287 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1288 maybe_const_itself);
1289 STRIP_TYPE_NOPS (op0);
1290 if (op0 != orig_op0)
1291 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1292 if (ret != expr)
1294 TREE_READONLY (ret) = TREE_READONLY (expr);
1295 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1297 goto out;
1299 case ARRAY_REF:
1300 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1301 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1302 op2 = TREE_OPERAND (expr, 2);
1303 op3 = TREE_OPERAND (expr, 3);
1304 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1305 maybe_const_itself);
1306 STRIP_TYPE_NOPS (op0);
1307 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1308 maybe_const_itself);
1309 STRIP_TYPE_NOPS (op1);
1310 op1 = decl_constant_value_for_optimization (op1);
1311 if (op0 != orig_op0 || op1 != orig_op1)
1312 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1313 if (ret != expr)
1315 TREE_READONLY (ret) = TREE_READONLY (expr);
1316 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1317 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1319 ret = fold (ret);
1320 goto out;
1322 case COMPOUND_EXPR:
1323 case MODIFY_EXPR:
1324 case PREDECREMENT_EXPR:
1325 case PREINCREMENT_EXPR:
1326 case POSTDECREMENT_EXPR:
1327 case POSTINCREMENT_EXPR:
1328 case PLUS_EXPR:
1329 case MINUS_EXPR:
1330 case MULT_EXPR:
1331 case POINTER_PLUS_EXPR:
1332 case TRUNC_DIV_EXPR:
1333 case CEIL_DIV_EXPR:
1334 case FLOOR_DIV_EXPR:
1335 case TRUNC_MOD_EXPR:
1336 case RDIV_EXPR:
1337 case EXACT_DIV_EXPR:
1338 case LSHIFT_EXPR:
1339 case RSHIFT_EXPR:
1340 case BIT_IOR_EXPR:
1341 case BIT_XOR_EXPR:
1342 case BIT_AND_EXPR:
1343 case LT_EXPR:
1344 case LE_EXPR:
1345 case GT_EXPR:
1346 case GE_EXPR:
1347 case EQ_EXPR:
1348 case NE_EXPR:
1349 case COMPLEX_EXPR:
1350 case TRUTH_AND_EXPR:
1351 case TRUTH_OR_EXPR:
1352 case TRUTH_XOR_EXPR:
1353 case UNORDERED_EXPR:
1354 case ORDERED_EXPR:
1355 case UNLT_EXPR:
1356 case UNLE_EXPR:
1357 case UNGT_EXPR:
1358 case UNGE_EXPR:
1359 case UNEQ_EXPR:
1360 /* Binary operations evaluating both arguments (increment and
1361 decrement are binary internally in GCC). */
1362 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1363 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1364 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1365 maybe_const_itself);
1366 STRIP_TYPE_NOPS (op0);
1367 if (code != MODIFY_EXPR
1368 && code != PREDECREMENT_EXPR
1369 && code != PREINCREMENT_EXPR
1370 && code != POSTDECREMENT_EXPR
1371 && code != POSTINCREMENT_EXPR)
1372 op0 = decl_constant_value_for_optimization (op0);
1373 /* The RHS of a MODIFY_EXPR was fully folded when building that
1374 expression for the sake of conversion warnings. */
1375 if (code != MODIFY_EXPR)
1376 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1377 maybe_const_itself);
1378 STRIP_TYPE_NOPS (op1);
1379 op1 = decl_constant_value_for_optimization (op1);
1380 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1381 ret = in_init
1382 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1383 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1384 else
1385 ret = fold (expr);
1386 if (TREE_OVERFLOW_P (ret)
1387 && !TREE_OVERFLOW_P (op0)
1388 && !TREE_OVERFLOW_P (op1))
1389 overflow_warning (EXPR_LOCATION (expr), ret);
1390 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1391 && TREE_CODE (orig_op1) != INTEGER_CST
1392 && TREE_CODE (op1) == INTEGER_CST
1393 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1394 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1395 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1396 && c_inhibit_evaluation_warnings == 0)
1398 if (tree_int_cst_sgn (op1) < 0)
1399 warning_at (loc, 0, (code == LSHIFT_EXPR
1400 ? G_("left shift count is negative")
1401 : G_("right shift count is negative")));
1402 else if (compare_tree_int (op1,
1403 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1404 >= 0)
1405 warning_at (loc, 0, (code == LSHIFT_EXPR
1406 ? G_("left shift count >= width of type")
1407 : G_("right shift count >= width of type")));
1409 if ((code == TRUNC_DIV_EXPR
1410 || code == CEIL_DIV_EXPR
1411 || code == FLOOR_DIV_EXPR
1412 || code == EXACT_DIV_EXPR
1413 || code == TRUNC_MOD_EXPR)
1414 && TREE_CODE (orig_op1) != INTEGER_CST
1415 && TREE_CODE (op1) == INTEGER_CST
1416 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1417 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1418 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1419 warn_for_div_by_zero (loc, op1);
1420 goto out;
1422 case INDIRECT_REF:
1423 case FIX_TRUNC_EXPR:
1424 case FLOAT_EXPR:
1425 CASE_CONVERT:
1426 case ADDR_SPACE_CONVERT_EXPR:
1427 case VIEW_CONVERT_EXPR:
1428 case NON_LVALUE_EXPR:
1429 case NEGATE_EXPR:
1430 case BIT_NOT_EXPR:
1431 case TRUTH_NOT_EXPR:
1432 case ADDR_EXPR:
1433 case CONJ_EXPR:
1434 case REALPART_EXPR:
1435 case IMAGPART_EXPR:
1436 /* Unary operations. */
1437 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1438 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1439 maybe_const_itself);
1440 STRIP_TYPE_NOPS (op0);
1441 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1442 op0 = decl_constant_value_for_optimization (op0);
1443 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1444 not prepared to deal with them if they occur in initializers.
1445 Avoid attempts to fold references to UPC shared components
1446 due to the complexities of UPC pointer-to-shared arithmetic. */
1447 if (op0 != orig_op0
1448 && code == ADDR_EXPR
1449 && (op1 = get_base_address (op0)) != NULL_TREE
1450 && TREE_CODE (op1) == INDIRECT_REF
1451 && !upc_shared_type_p (TREE_TYPE (op1))
1452 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1453 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1454 else if (op0 != orig_op0 || in_init)
1455 ret = in_init
1456 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1457 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1458 else
1459 ret = fold (expr);
1460 if (code == INDIRECT_REF
1461 && ret != expr
1462 && TREE_CODE (ret) == INDIRECT_REF)
1464 TREE_READONLY (ret) = TREE_READONLY (expr);
1465 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1466 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1468 switch (code)
1470 case FIX_TRUNC_EXPR:
1471 case FLOAT_EXPR:
1472 CASE_CONVERT:
1473 /* Don't warn about explicit conversions. We will already
1474 have warned about suspect implicit conversions. */
1475 break;
1477 default:
1478 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1479 overflow_warning (EXPR_LOCATION (expr), ret);
1480 break;
1482 goto out;
1484 case TRUTH_ANDIF_EXPR:
1485 case TRUTH_ORIF_EXPR:
1486 /* Binary operations not necessarily evaluating both
1487 arguments. */
1488 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1489 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1490 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1491 STRIP_TYPE_NOPS (op0);
1493 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1494 ? truthvalue_false_node
1495 : truthvalue_true_node));
1496 c_disable_warnings (unused_p);
1497 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1498 STRIP_TYPE_NOPS (op1);
1499 c_enable_warnings (unused_p);
1501 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1502 ret = in_init
1503 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1504 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1505 else
1506 ret = fold (expr);
1507 *maybe_const_operands &= op0_const;
1508 *maybe_const_itself &= op0_const_self;
1509 if (!(flag_isoc99
1510 && op0_const
1511 && op0_const_self
1512 && (code == TRUTH_ANDIF_EXPR
1513 ? op0 == truthvalue_false_node
1514 : op0 == truthvalue_true_node)))
1515 *maybe_const_operands &= op1_const;
1516 if (!(op0_const
1517 && op0_const_self
1518 && (code == TRUTH_ANDIF_EXPR
1519 ? op0 == truthvalue_false_node
1520 : op0 == truthvalue_true_node)))
1521 *maybe_const_itself &= op1_const_self;
1522 goto out;
1524 case COND_EXPR:
1525 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1526 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1527 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1528 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1530 STRIP_TYPE_NOPS (op0);
1531 c_disable_warnings (op0 == truthvalue_false_node);
1532 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1533 STRIP_TYPE_NOPS (op1);
1534 c_enable_warnings (op0 == truthvalue_false_node);
1536 c_disable_warnings (op0 == truthvalue_true_node);
1537 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1538 STRIP_TYPE_NOPS (op2);
1539 c_enable_warnings (op0 == truthvalue_true_node);
1541 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1542 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1543 else
1544 ret = fold (expr);
1545 *maybe_const_operands &= op0_const;
1546 *maybe_const_itself &= op0_const_self;
1547 if (!(flag_isoc99
1548 && op0_const
1549 && op0_const_self
1550 && op0 == truthvalue_false_node))
1551 *maybe_const_operands &= op1_const;
1552 if (!(op0_const
1553 && op0_const_self
1554 && op0 == truthvalue_false_node))
1555 *maybe_const_itself &= op1_const_self;
1556 if (!(flag_isoc99
1557 && op0_const
1558 && op0_const_self
1559 && op0 == truthvalue_true_node))
1560 *maybe_const_operands &= op2_const;
1561 if (!(op0_const
1562 && op0_const_self
1563 && op0 == truthvalue_true_node))
1564 *maybe_const_itself &= op2_const_self;
1565 goto out;
1567 case EXCESS_PRECISION_EXPR:
1568 /* Each case where an operand with excess precision may be
1569 encountered must remove the EXCESS_PRECISION_EXPR around
1570 inner operands and possibly put one around the whole
1571 expression or possibly convert to the semantic type (which
1572 c_fully_fold does); we cannot tell at this stage which is
1573 appropriate in any particular case. */
1574 gcc_unreachable ();
1576 default:
1577 /* Various codes may appear through folding built-in functions
1578 and their arguments. */
1579 goto out;
1582 out:
1583 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1584 have been done by this point, so remove them again. */
1585 nowarning |= TREE_NO_WARNING (ret);
1586 STRIP_TYPE_NOPS (ret);
1587 if (nowarning && !TREE_NO_WARNING (ret))
1589 if (!CAN_HAVE_LOCATION_P (ret))
1590 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1591 TREE_NO_WARNING (ret) = 1;
1593 if (ret != expr)
1594 protected_set_expr_location (ret, loc);
1595 return ret;
1598 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1599 return EXP. Otherwise, return either EXP or its known constant
1600 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1601 Is the BLKmode test appropriate? */
1603 tree
1604 decl_constant_value_for_optimization (tree exp)
1606 tree ret;
1608 /* This function is only used by C, for c_fully_fold and other
1609 optimization, and may not be correct for C++. */
1610 if (c_dialect_cxx ())
1611 gcc_unreachable ();
1613 if (!optimize
1614 || TREE_CODE (exp) != VAR_DECL
1615 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1616 || DECL_MODE (exp) == BLKmode)
1617 return exp;
1619 ret = decl_constant_value (exp);
1620 /* Avoid unwanted tree sharing between the initializer and current
1621 function's body where the tree can be modified e.g. by the
1622 gimplifier. */
1623 if (ret != exp && TREE_STATIC (exp))
1624 ret = unshare_expr (ret);
1625 return ret;
1628 /* Print a warning if a constant expression had overflow in folding.
1629 Invoke this function on every expression that the language
1630 requires to be a constant expression.
1631 Note the ANSI C standard says it is erroneous for a
1632 constant expression to overflow. */
1634 void
1635 constant_expression_warning (tree value)
1637 if (warn_overflow && pedantic
1638 && (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 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1646 /* The same as above but print an unconditional error. */
1647 void
1648 constant_expression_error (tree value)
1650 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1651 || TREE_CODE (value) == FIXED_CST
1652 || TREE_CODE (value) == VECTOR_CST
1653 || TREE_CODE (value) == COMPLEX_CST)
1654 && TREE_OVERFLOW (value))
1655 error ("overflow in constant expression");
1658 /* Print a warning if an expression had overflow in folding and its
1659 operands hadn't.
1661 Invoke this function on every expression that
1662 (1) appears in the source code, and
1663 (2) is a constant expression that overflowed, and
1664 (3) is not already checked by convert_and_check;
1665 however, do not invoke this function on operands of explicit casts
1666 or when the expression is the result of an operator and any operand
1667 already overflowed. */
1669 void
1670 overflow_warning (location_t loc, tree value)
1672 if (c_inhibit_evaluation_warnings != 0)
1673 return;
1675 switch (TREE_CODE (value))
1677 case INTEGER_CST:
1678 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1679 break;
1681 case REAL_CST:
1682 warning_at (loc, OPT_Woverflow,
1683 "floating point overflow in expression");
1684 break;
1686 case FIXED_CST:
1687 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1688 break;
1690 case VECTOR_CST:
1691 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1692 break;
1694 case COMPLEX_CST:
1695 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1696 warning_at (loc, OPT_Woverflow,
1697 "complex integer overflow in expression");
1698 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1699 warning_at (loc, OPT_Woverflow,
1700 "complex floating point overflow in expression");
1701 break;
1703 default:
1704 break;
1708 /* Warn about uses of logical || / && operator in a context where it
1709 is likely that the bitwise equivalent was intended by the
1710 programmer. We have seen an expression in which CODE is a binary
1711 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1712 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1713 void
1714 warn_logical_operator (location_t location, enum tree_code code, tree type,
1715 enum tree_code code_left, tree op_left,
1716 enum tree_code ARG_UNUSED (code_right), tree op_right)
1718 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1719 int in0_p, in1_p, in_p;
1720 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1721 bool strict_overflow_p = false;
1723 if (code != TRUTH_ANDIF_EXPR
1724 && code != TRUTH_AND_EXPR
1725 && code != TRUTH_ORIF_EXPR
1726 && code != TRUTH_OR_EXPR)
1727 return;
1729 /* Warn if &&/|| are being used in a context where it is
1730 likely that the bitwise equivalent was intended by the
1731 programmer. That is, an expression such as op && MASK
1732 where op should not be any boolean expression, nor a
1733 constant, and mask seems to be a non-boolean integer constant. */
1734 if (!truth_value_p (code_left)
1735 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1736 && !CONSTANT_CLASS_P (op_left)
1737 && !TREE_NO_WARNING (op_left)
1738 && TREE_CODE (op_right) == INTEGER_CST
1739 && !integer_zerop (op_right)
1740 && !integer_onep (op_right))
1742 if (or_op)
1743 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1744 " applied to non-boolean constant");
1745 else
1746 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1747 " applied to non-boolean constant");
1748 TREE_NO_WARNING (op_left) = true;
1749 return;
1752 /* We do not warn for constants because they are typical of macro
1753 expansions that test for features. */
1754 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1755 return;
1757 /* This warning only makes sense with logical operands. */
1758 if (!(truth_value_p (TREE_CODE (op_left))
1759 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1760 || !(truth_value_p (TREE_CODE (op_right))
1761 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1762 return;
1764 /* The range computations only work with scalars. */
1765 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1766 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1767 return;
1769 /* We first test whether either side separately is trivially true
1770 (with OR) or trivially false (with AND). If so, do not warn.
1771 This is a common idiom for testing ranges of data types in
1772 portable code. */
1773 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1774 if (!lhs)
1775 return;
1776 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1777 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1779 /* If this is an OR operation, invert both sides; now, the result
1780 should be always false to get a warning. */
1781 if (or_op)
1782 in0_p = !in0_p;
1784 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1785 if (tem && integer_zerop (tem))
1786 return;
1788 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1789 if (!rhs)
1790 return;
1791 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1792 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1794 /* If this is an OR operation, invert both sides; now, the result
1795 should be always false to get a warning. */
1796 if (or_op)
1797 in1_p = !in1_p;
1799 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1800 if (tem && integer_zerop (tem))
1801 return;
1803 /* If both expressions have the same operand, if we can merge the
1804 ranges, and if the range test is always false, then warn. */
1805 if (operand_equal_p (lhs, rhs, 0)
1806 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1807 in1_p, low1, high1)
1808 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1809 type, lhs, in_p, low, high))
1810 && integer_zerop (tem))
1812 if (or_op)
1813 warning_at (location, OPT_Wlogical_op,
1814 "logical %<or%> "
1815 "of collectively exhaustive tests is always true");
1816 else
1817 warning_at (location, OPT_Wlogical_op,
1818 "logical %<and%> "
1819 "of mutually exclusive tests is always false");
1823 /* Warn about logical not used on the left hand side operand of a comparison.
1824 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1825 Do not warn if RHS is of a boolean type. */
1827 void
1828 warn_logical_not_parentheses (location_t location, enum tree_code code,
1829 tree rhs)
1831 if (TREE_CODE_CLASS (code) != tcc_comparison
1832 || TREE_TYPE (rhs) == NULL_TREE
1833 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1834 return;
1836 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1837 !(x == 0) or !(y != 0). */
1838 if ((code == EQ_EXPR || code == NE_EXPR)
1839 && integer_zerop (rhs))
1840 return;
1842 warning_at (location, OPT_Wlogical_not_parentheses,
1843 "logical not is only applied to the left hand side of "
1844 "comparison");
1847 /* Warn if EXP contains any computations whose results are not used.
1848 Return true if a warning is printed; false otherwise. LOCUS is the
1849 (potential) location of the expression. */
1851 bool
1852 warn_if_unused_value (const_tree exp, location_t locus)
1854 restart:
1855 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1856 return false;
1858 /* Don't warn about void constructs. This includes casting to void,
1859 void function calls, and statement expressions with a final cast
1860 to void. */
1861 if (VOID_TYPE_P (TREE_TYPE (exp)))
1862 return false;
1864 if (EXPR_HAS_LOCATION (exp))
1865 locus = EXPR_LOCATION (exp);
1867 switch (TREE_CODE (exp))
1869 case PREINCREMENT_EXPR:
1870 case POSTINCREMENT_EXPR:
1871 case PREDECREMENT_EXPR:
1872 case POSTDECREMENT_EXPR:
1873 case MODIFY_EXPR:
1874 case INIT_EXPR:
1875 case TARGET_EXPR:
1876 case CALL_EXPR:
1877 case TRY_CATCH_EXPR:
1878 case WITH_CLEANUP_EXPR:
1879 case EXIT_EXPR:
1880 case VA_ARG_EXPR:
1881 return false;
1883 case BIND_EXPR:
1884 /* For a binding, warn if no side effect within it. */
1885 exp = BIND_EXPR_BODY (exp);
1886 goto restart;
1888 case SAVE_EXPR:
1889 case NON_LVALUE_EXPR:
1890 case NOP_EXPR:
1891 exp = TREE_OPERAND (exp, 0);
1892 goto restart;
1894 case TRUTH_ORIF_EXPR:
1895 case TRUTH_ANDIF_EXPR:
1896 /* In && or ||, warn if 2nd operand has no side effect. */
1897 exp = TREE_OPERAND (exp, 1);
1898 goto restart;
1900 case COMPOUND_EXPR:
1901 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1902 return true;
1903 /* Let people do `(foo (), 0)' without a warning. */
1904 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1905 return false;
1906 exp = TREE_OPERAND (exp, 1);
1907 goto restart;
1909 case COND_EXPR:
1910 /* If this is an expression with side effects, don't warn; this
1911 case commonly appears in macro expansions. */
1912 if (TREE_SIDE_EFFECTS (exp))
1913 return false;
1914 goto warn;
1916 case INDIRECT_REF:
1917 /* Don't warn about automatic dereferencing of references, since
1918 the user cannot control it. */
1919 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1921 exp = TREE_OPERAND (exp, 0);
1922 goto restart;
1924 /* Fall through. */
1926 default:
1927 /* Referencing a volatile value is a side effect, so don't warn. */
1928 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1929 && TREE_THIS_VOLATILE (exp))
1930 return false;
1932 /* If this is an expression which has no operands, there is no value
1933 to be unused. There are no such language-independent codes,
1934 but front ends may define such. */
1935 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1936 return false;
1938 warn:
1939 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1944 /* Print a warning about casts that might indicate violation
1945 of strict aliasing rules if -Wstrict-aliasing is used and
1946 strict aliasing mode is in effect. OTYPE is the original
1947 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1949 bool
1950 strict_aliasing_warning (tree otype, tree type, tree expr)
1952 /* Strip pointer conversion chains and get to the correct original type. */
1953 STRIP_NOPS (expr);
1954 otype = TREE_TYPE (expr);
1956 if (!(flag_strict_aliasing
1957 && POINTER_TYPE_P (type)
1958 && POINTER_TYPE_P (otype)
1959 && !VOID_TYPE_P (TREE_TYPE (type)))
1960 /* If the type we are casting to is a ref-all pointer
1961 dereferencing it is always valid. */
1962 || TYPE_REF_CAN_ALIAS_ALL (type))
1963 return false;
1965 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1966 && (DECL_P (TREE_OPERAND (expr, 0))
1967 || handled_component_p (TREE_OPERAND (expr, 0))))
1969 /* Casting the address of an object to non void pointer. Warn
1970 if the cast breaks type based aliasing. */
1971 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1973 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1974 "might break strict-aliasing rules");
1975 return true;
1977 else
1979 /* warn_strict_aliasing >= 3. This includes the default (3).
1980 Only warn if the cast is dereferenced immediately. */
1981 alias_set_type set1 =
1982 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1983 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1985 if (set1 != set2 && set2 != 0
1986 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1988 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1989 "pointer will break strict-aliasing rules");
1990 return true;
1992 else if (warn_strict_aliasing == 2
1993 && !alias_sets_must_conflict_p (set1, set2))
1995 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1996 "pointer might break strict-aliasing rules");
1997 return true;
2001 else
2002 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2004 /* At this level, warn for any conversions, even if an address is
2005 not taken in the same statement. This will likely produce many
2006 false positives, but could be useful to pinpoint problems that
2007 are not revealed at higher levels. */
2008 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2009 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2010 if (!COMPLETE_TYPE_P (type)
2011 || !alias_sets_must_conflict_p (set1, set2))
2013 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2014 "pointer might break strict-aliasing rules");
2015 return true;
2019 return false;
2022 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2023 sizeof as last operand of certain builtins. */
2025 void
2026 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
2027 vec<tree, va_gc> *params, tree *sizeof_arg,
2028 bool (*comp_types) (tree, tree))
2030 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
2031 bool strop = false, cmp = false;
2032 unsigned int idx = ~0;
2033 location_t loc;
2035 if (TREE_CODE (callee) != FUNCTION_DECL
2036 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2037 || vec_safe_length (params) <= 1)
2038 return;
2040 switch (DECL_FUNCTION_CODE (callee))
2042 case BUILT_IN_STRNCMP:
2043 case BUILT_IN_STRNCASECMP:
2044 cmp = true;
2045 /* FALLTHRU */
2046 case BUILT_IN_STRNCPY:
2047 case BUILT_IN_STRNCPY_CHK:
2048 case BUILT_IN_STRNCAT:
2049 case BUILT_IN_STRNCAT_CHK:
2050 case BUILT_IN_STPNCPY:
2051 case BUILT_IN_STPNCPY_CHK:
2052 strop = true;
2053 /* FALLTHRU */
2054 case BUILT_IN_MEMCPY:
2055 case BUILT_IN_MEMCPY_CHK:
2056 case BUILT_IN_MEMMOVE:
2057 case BUILT_IN_MEMMOVE_CHK:
2058 if (params->length () < 3)
2059 return;
2060 src = (*params)[1];
2061 dest = (*params)[0];
2062 idx = 2;
2063 break;
2064 case BUILT_IN_BCOPY:
2065 if (params->length () < 3)
2066 return;
2067 src = (*params)[0];
2068 dest = (*params)[1];
2069 idx = 2;
2070 break;
2071 case BUILT_IN_MEMCMP:
2072 case BUILT_IN_BCMP:
2073 if (params->length () < 3)
2074 return;
2075 src = (*params)[1];
2076 dest = (*params)[0];
2077 idx = 2;
2078 cmp = true;
2079 break;
2080 case BUILT_IN_MEMSET:
2081 case BUILT_IN_MEMSET_CHK:
2082 if (params->length () < 3)
2083 return;
2084 dest = (*params)[0];
2085 idx = 2;
2086 break;
2087 case BUILT_IN_BZERO:
2088 dest = (*params)[0];
2089 idx = 1;
2090 break;
2091 case BUILT_IN_STRNDUP:
2092 src = (*params)[0];
2093 strop = true;
2094 idx = 1;
2095 break;
2096 case BUILT_IN_MEMCHR:
2097 if (params->length () < 3)
2098 return;
2099 src = (*params)[0];
2100 idx = 2;
2101 break;
2102 case BUILT_IN_SNPRINTF:
2103 case BUILT_IN_SNPRINTF_CHK:
2104 case BUILT_IN_VSNPRINTF:
2105 case BUILT_IN_VSNPRINTF_CHK:
2106 dest = (*params)[0];
2107 idx = 1;
2108 strop = true;
2109 break;
2110 default:
2111 break;
2114 if (idx >= 3)
2115 return;
2117 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2118 return;
2120 type = TYPE_P (sizeof_arg[idx])
2121 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2122 if (!POINTER_TYPE_P (type))
2123 return;
2125 if (dest
2126 && (tem = tree_strip_nop_conversions (dest))
2127 && POINTER_TYPE_P (TREE_TYPE (tem))
2128 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2129 return;
2131 if (src
2132 && (tem = tree_strip_nop_conversions (src))
2133 && POINTER_TYPE_P (TREE_TYPE (tem))
2134 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2135 return;
2137 loc = sizeof_arg_loc[idx];
2139 if (dest && !cmp)
2141 if (!TYPE_P (sizeof_arg[idx])
2142 && operand_equal_p (dest, sizeof_arg[idx], 0)
2143 && comp_types (TREE_TYPE (dest), type))
2145 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2146 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2147 "argument to %<sizeof%> in %qD call is the same "
2148 "expression as the destination; did you mean to "
2149 "remove the addressof?", callee);
2150 else if ((TYPE_PRECISION (TREE_TYPE (type))
2151 == TYPE_PRECISION (char_type_node))
2152 || strop)
2153 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2154 "argument to %<sizeof%> in %qD call is the same "
2155 "expression as the destination; did you mean to "
2156 "provide an explicit length?", callee);
2157 else
2158 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2159 "argument to %<sizeof%> in %qD call is the same "
2160 "expression as the destination; did you mean to "
2161 "dereference it?", callee);
2162 return;
2165 if (POINTER_TYPE_P (TREE_TYPE (dest))
2166 && !strop
2167 && comp_types (TREE_TYPE (dest), type)
2168 && !VOID_TYPE_P (TREE_TYPE (type)))
2170 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2171 "argument to %<sizeof%> in %qD call is the same "
2172 "pointer type %qT as the destination; expected %qT "
2173 "or an explicit length", callee, TREE_TYPE (dest),
2174 TREE_TYPE (TREE_TYPE (dest)));
2175 return;
2179 if (src && !cmp)
2181 if (!TYPE_P (sizeof_arg[idx])
2182 && operand_equal_p (src, sizeof_arg[idx], 0)
2183 && comp_types (TREE_TYPE (src), type))
2185 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2186 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2187 "argument to %<sizeof%> in %qD call is the same "
2188 "expression as the source; did you mean to "
2189 "remove the addressof?", callee);
2190 else if ((TYPE_PRECISION (TREE_TYPE (type))
2191 == TYPE_PRECISION (char_type_node))
2192 || strop)
2193 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2194 "argument to %<sizeof%> in %qD call is the same "
2195 "expression as the source; did you mean to "
2196 "provide an explicit length?", callee);
2197 else
2198 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2199 "argument to %<sizeof%> in %qD call is the same "
2200 "expression as the source; did you mean to "
2201 "dereference it?", callee);
2202 return;
2205 if (POINTER_TYPE_P (TREE_TYPE (src))
2206 && !strop
2207 && comp_types (TREE_TYPE (src), type)
2208 && !VOID_TYPE_P (TREE_TYPE (type)))
2210 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2211 "argument to %<sizeof%> in %qD call is the same "
2212 "pointer type %qT as the source; expected %qT "
2213 "or an explicit length", callee, TREE_TYPE (src),
2214 TREE_TYPE (TREE_TYPE (src)));
2215 return;
2219 if (dest)
2221 if (!TYPE_P (sizeof_arg[idx])
2222 && operand_equal_p (dest, sizeof_arg[idx], 0)
2223 && comp_types (TREE_TYPE (dest), type))
2225 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2226 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2227 "argument to %<sizeof%> in %qD call is the same "
2228 "expression as the first source; did you mean to "
2229 "remove the addressof?", callee);
2230 else if ((TYPE_PRECISION (TREE_TYPE (type))
2231 == TYPE_PRECISION (char_type_node))
2232 || strop)
2233 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2234 "argument to %<sizeof%> in %qD call is the same "
2235 "expression as the first source; did you mean to "
2236 "provide an explicit length?", callee);
2237 else
2238 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2239 "argument to %<sizeof%> in %qD call is the same "
2240 "expression as the first source; did you mean to "
2241 "dereference it?", callee);
2242 return;
2245 if (POINTER_TYPE_P (TREE_TYPE (dest))
2246 && !strop
2247 && comp_types (TREE_TYPE (dest), type)
2248 && !VOID_TYPE_P (TREE_TYPE (type)))
2250 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2251 "argument to %<sizeof%> in %qD call is the same "
2252 "pointer type %qT as the first source; expected %qT "
2253 "or an explicit length", callee, TREE_TYPE (dest),
2254 TREE_TYPE (TREE_TYPE (dest)));
2255 return;
2259 if (src)
2261 if (!TYPE_P (sizeof_arg[idx])
2262 && operand_equal_p (src, sizeof_arg[idx], 0)
2263 && comp_types (TREE_TYPE (src), type))
2265 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2266 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2267 "argument to %<sizeof%> in %qD call is the same "
2268 "expression as the second source; did you mean to "
2269 "remove the addressof?", callee);
2270 else if ((TYPE_PRECISION (TREE_TYPE (type))
2271 == TYPE_PRECISION (char_type_node))
2272 || strop)
2273 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2274 "argument to %<sizeof%> in %qD call is the same "
2275 "expression as the second source; did you mean to "
2276 "provide an explicit length?", callee);
2277 else
2278 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2279 "argument to %<sizeof%> in %qD call is the same "
2280 "expression as the second source; did you mean to "
2281 "dereference it?", callee);
2282 return;
2285 if (POINTER_TYPE_P (TREE_TYPE (src))
2286 && !strop
2287 && comp_types (TREE_TYPE (src), type)
2288 && !VOID_TYPE_P (TREE_TYPE (type)))
2290 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2291 "argument to %<sizeof%> in %qD call is the same "
2292 "pointer type %qT as the second source; expected %qT "
2293 "or an explicit length", callee, TREE_TYPE (src),
2294 TREE_TYPE (TREE_TYPE (src)));
2295 return;
2301 /* Warn for unlikely, improbable, or stupid DECL declarations
2302 of `main'. */
2304 void
2305 check_main_parameter_types (tree decl)
2307 function_args_iterator iter;
2308 tree type;
2309 int argct = 0;
2311 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2313 /* XXX void_type_node belies the abstraction. */
2314 if (type == void_type_node || type == error_mark_node )
2315 break;
2317 tree t = type;
2318 if (TYPE_ATOMIC (t))
2319 pedwarn (input_location, OPT_Wmain,
2320 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2321 type, decl);
2322 while (POINTER_TYPE_P (t))
2324 t = TREE_TYPE (t);
2325 if (TYPE_ATOMIC (t))
2326 pedwarn (input_location, OPT_Wmain,
2327 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2328 type, decl);
2331 ++argct;
2332 switch (argct)
2334 case 1:
2335 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2336 pedwarn (input_location, OPT_Wmain,
2337 "first argument of %q+D should be %<int%>", decl);
2338 break;
2340 case 2:
2341 if (TREE_CODE (type) != POINTER_TYPE
2342 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2343 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2344 != char_type_node))
2345 pedwarn (input_location, OPT_Wmain,
2346 "second argument of %q+D should be %<char **%>", decl);
2347 break;
2349 case 3:
2350 if (TREE_CODE (type) != POINTER_TYPE
2351 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2352 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2353 != char_type_node))
2354 pedwarn (input_location, OPT_Wmain,
2355 "third argument of %q+D should probably be "
2356 "%<char **%>", decl);
2357 break;
2361 /* It is intentional that this message does not mention the third
2362 argument because it's only mentioned in an appendix of the
2363 standard. */
2364 if (argct > 0 && (argct < 2 || argct > 3))
2365 pedwarn (input_location, OPT_Wmain,
2366 "%q+D takes only zero or two arguments", decl);
2368 if (stdarg_p (TREE_TYPE (decl)))
2369 pedwarn (input_location, OPT_Wmain,
2370 "%q+D declared as variadic function", decl);
2373 /* vector_targets_convertible_p is used for vector pointer types. The
2374 callers perform various checks that the qualifiers are satisfactory,
2375 while OTOH vector_targets_convertible_p ignores the number of elements
2376 in the vectors. That's fine with vector pointers as we can consider,
2377 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2378 and that does not require and conversion of the pointer values.
2379 In contrast, vector_types_convertible_p and
2380 vector_types_compatible_elements_p are used for vector value types. */
2381 /* True if pointers to distinct types T1 and T2 can be converted to
2382 each other without an explicit cast. Only returns true for opaque
2383 vector types. */
2384 bool
2385 vector_targets_convertible_p (const_tree t1, const_tree t2)
2387 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2388 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2389 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2390 return true;
2392 return false;
2395 /* vector_types_convertible_p is used for vector value types.
2396 It could in principle call vector_targets_convertible_p as a subroutine,
2397 but then the check for vector type would be duplicated with its callers,
2398 and also the purpose of vector_targets_convertible_p would become
2399 muddled.
2400 Where vector_types_convertible_p returns true, a conversion might still be
2401 needed to make the types match.
2402 In contrast, vector_targets_convertible_p is used for vector pointer
2403 values, and vector_types_compatible_elements_p is used specifically
2404 in the context for binary operators, as a check if use is possible without
2405 conversion. */
2406 /* True if vector types T1 and T2 can be converted to each other
2407 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2408 can only be converted with -flax-vector-conversions yet that is not
2409 in effect, emit a note telling the user about that option if such
2410 a note has not previously been emitted. */
2411 bool
2412 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2414 static bool emitted_lax_note = false;
2415 bool convertible_lax;
2417 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2418 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2419 return true;
2421 convertible_lax =
2422 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2423 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2424 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2425 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2426 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2428 if (!convertible_lax || flag_lax_vector_conversions)
2429 return convertible_lax;
2431 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2432 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2433 return true;
2435 if (emit_lax_note && !emitted_lax_note)
2437 emitted_lax_note = true;
2438 inform (input_location, "use -flax-vector-conversions to permit "
2439 "conversions between vectors with differing "
2440 "element types or numbers of subparts");
2443 return false;
2446 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2447 and have vector types, V0 has the same type as V1, and the number of
2448 elements of V0, V1, MASK is the same.
2450 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2451 called with two arguments. In this case implementation passes the
2452 first argument twice in order to share the same tree code. This fact
2453 could enable the mask-values being twice the vector length. This is
2454 an implementation accident and this semantics is not guaranteed to
2455 the user. */
2456 tree
2457 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2458 bool complain)
2460 tree ret;
2461 bool wrap = true;
2462 bool maybe_const = false;
2463 bool two_arguments = false;
2465 if (v1 == NULL_TREE)
2467 two_arguments = true;
2468 v1 = v0;
2471 if (v0 == error_mark_node || v1 == error_mark_node
2472 || mask == error_mark_node)
2473 return error_mark_node;
2475 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2476 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2478 if (complain)
2479 error_at (loc, "__builtin_shuffle last argument must "
2480 "be an integer vector");
2481 return error_mark_node;
2484 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2485 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2487 if (complain)
2488 error_at (loc, "__builtin_shuffle arguments must be vectors");
2489 return error_mark_node;
2492 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2494 if (complain)
2495 error_at (loc, "__builtin_shuffle argument vectors must be of "
2496 "the same type");
2497 return error_mark_node;
2500 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2501 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2502 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2503 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2505 if (complain)
2506 error_at (loc, "__builtin_shuffle number of elements of the "
2507 "argument vector(s) and the mask vector should "
2508 "be the same");
2509 return error_mark_node;
2512 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2513 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2515 if (complain)
2516 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2517 "must have the same size as inner type of the mask");
2518 return error_mark_node;
2521 if (!c_dialect_cxx ())
2523 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2524 v0 = c_fully_fold (v0, false, &maybe_const);
2525 wrap &= maybe_const;
2527 if (two_arguments)
2528 v1 = v0 = save_expr (v0);
2529 else
2531 v1 = c_fully_fold (v1, false, &maybe_const);
2532 wrap &= maybe_const;
2535 mask = c_fully_fold (mask, false, &maybe_const);
2536 wrap &= maybe_const;
2538 else if (two_arguments)
2539 v1 = v0 = save_expr (v0);
2541 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2543 if (!c_dialect_cxx () && !wrap)
2544 ret = c_wrap_maybe_const (ret, true);
2546 return ret;
2549 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2550 to integral type. */
2552 static tree
2553 c_common_get_narrower (tree op, int *unsignedp_ptr)
2555 op = get_narrower (op, unsignedp_ptr);
2557 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2558 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2560 /* C++0x scoped enumerations don't implicitly convert to integral
2561 type; if we stripped an explicit conversion to a larger type we
2562 need to replace it so common_type will still work. */
2563 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2564 TYPE_UNSIGNED (TREE_TYPE (op)));
2565 op = fold_convert (type, op);
2567 return op;
2570 /* This is a helper function of build_binary_op.
2572 For certain operations if both args were extended from the same
2573 smaller type, do the arithmetic in that type and then extend.
2575 BITWISE indicates a bitwise operation.
2576 For them, this optimization is safe only if
2577 both args are zero-extended or both are sign-extended.
2578 Otherwise, we might change the result.
2579 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2580 but calculated in (unsigned short) it would be (unsigned short)-1.
2582 tree
2583 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2585 int unsigned0, unsigned1;
2586 tree arg0, arg1;
2587 int uns;
2588 tree type;
2590 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2591 excessive narrowing when we call get_narrower below. For
2592 example, suppose that OP0 is of unsigned int extended
2593 from signed char and that RESULT_TYPE is long long int.
2594 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2595 like
2597 (long long int) (unsigned int) signed_char
2599 which get_narrower would narrow down to
2601 (unsigned int) signed char
2603 If we do not cast OP0 first, get_narrower would return
2604 signed_char, which is inconsistent with the case of the
2605 explicit cast. */
2606 op0 = convert (result_type, op0);
2607 op1 = convert (result_type, op1);
2609 arg0 = c_common_get_narrower (op0, &unsigned0);
2610 arg1 = c_common_get_narrower (op1, &unsigned1);
2612 /* UNS is 1 if the operation to be done is an unsigned one. */
2613 uns = TYPE_UNSIGNED (result_type);
2615 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2616 but it *requires* conversion to FINAL_TYPE. */
2618 if ((TYPE_PRECISION (TREE_TYPE (op0))
2619 == TYPE_PRECISION (TREE_TYPE (arg0)))
2620 && TREE_TYPE (op0) != result_type)
2621 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2622 if ((TYPE_PRECISION (TREE_TYPE (op1))
2623 == TYPE_PRECISION (TREE_TYPE (arg1)))
2624 && TREE_TYPE (op1) != result_type)
2625 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2627 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2629 /* For bitwise operations, signedness of nominal type
2630 does not matter. Consider only how operands were extended. */
2631 if (bitwise)
2632 uns = unsigned0;
2634 /* Note that in all three cases below we refrain from optimizing
2635 an unsigned operation on sign-extended args.
2636 That would not be valid. */
2638 /* Both args variable: if both extended in same way
2639 from same width, do it in that width.
2640 Do it unsigned if args were zero-extended. */
2641 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2642 < TYPE_PRECISION (result_type))
2643 && (TYPE_PRECISION (TREE_TYPE (arg1))
2644 == TYPE_PRECISION (TREE_TYPE (arg0)))
2645 && unsigned0 == unsigned1
2646 && (unsigned0 || !uns))
2647 return c_common_signed_or_unsigned_type
2648 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2650 else if (TREE_CODE (arg0) == INTEGER_CST
2651 && (unsigned1 || !uns)
2652 && (TYPE_PRECISION (TREE_TYPE (arg1))
2653 < TYPE_PRECISION (result_type))
2654 && (type
2655 = c_common_signed_or_unsigned_type (unsigned1,
2656 TREE_TYPE (arg1)))
2657 && !POINTER_TYPE_P (type)
2658 && int_fits_type_p (arg0, type))
2659 return type;
2661 else if (TREE_CODE (arg1) == INTEGER_CST
2662 && (unsigned0 || !uns)
2663 && (TYPE_PRECISION (TREE_TYPE (arg0))
2664 < TYPE_PRECISION (result_type))
2665 && (type
2666 = c_common_signed_or_unsigned_type (unsigned0,
2667 TREE_TYPE (arg0)))
2668 && !POINTER_TYPE_P (type)
2669 && int_fits_type_p (arg1, type))
2670 return type;
2672 return result_type;
2675 /* Checks if expression EXPR of real/integer type cannot be converted
2676 to the real/integer type TYPE. Function returns non-zero when:
2677 * EXPR is a constant which cannot be exactly converted to TYPE.
2678 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2679 for EXPR type and TYPE being both integers or both real.
2680 * EXPR is not a constant of real type and TYPE is an integer.
2681 * EXPR is not a constant of integer type which cannot be
2682 exactly converted to real type.
2683 Function allows conversions between types of different signedness and
2684 can return SAFE_CONVERSION (zero) in that case. Function can produce
2685 signedness warnings if PRODUCE_WARNS is true. */
2687 enum conversion_safety
2688 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2690 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2691 tree expr_type = TREE_TYPE (expr);
2692 loc = expansion_point_location_if_in_system_header (loc);
2694 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2696 /* Warn for real constant that is not an exact integer converted
2697 to integer type. */
2698 if (TREE_CODE (expr_type) == REAL_TYPE
2699 && TREE_CODE (type) == INTEGER_TYPE)
2701 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2702 give_warning = UNSAFE_REAL;
2704 /* Warn for an integer constant that does not fit into integer type. */
2705 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2706 && TREE_CODE (type) == INTEGER_TYPE
2707 && !int_fits_type_p (expr, type))
2709 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2710 && tree_int_cst_sgn (expr) < 0)
2712 if (produce_warns)
2713 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2714 " implicitly converted to unsigned type");
2716 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2718 if (produce_warns)
2719 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2720 " constant value to negative integer");
2722 else
2723 give_warning = UNSAFE_OTHER;
2725 else if (TREE_CODE (type) == REAL_TYPE)
2727 /* Warn for an integer constant that does not fit into real type. */
2728 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2730 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2731 if (!exact_real_truncate (TYPE_MODE (type), &a))
2732 give_warning = UNSAFE_REAL;
2734 /* Warn for a real constant that does not fit into a smaller
2735 real type. */
2736 else if (TREE_CODE (expr_type) == REAL_TYPE
2737 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2739 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2740 if (!exact_real_truncate (TYPE_MODE (type), &a))
2741 give_warning = UNSAFE_REAL;
2745 else
2747 /* Warn for real types converted to integer types. */
2748 if (TREE_CODE (expr_type) == REAL_TYPE
2749 && TREE_CODE (type) == INTEGER_TYPE)
2750 give_warning = UNSAFE_REAL;
2752 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2753 && TREE_CODE (type) == INTEGER_TYPE)
2755 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2756 expr = get_unwidened (expr, 0);
2757 expr_type = TREE_TYPE (expr);
2759 /* Don't warn for short y; short x = ((int)y & 0xff); */
2760 if (TREE_CODE (expr) == BIT_AND_EXPR
2761 || TREE_CODE (expr) == BIT_IOR_EXPR
2762 || TREE_CODE (expr) == BIT_XOR_EXPR)
2764 /* If both args were extended from a shortest type,
2765 use that type if that is safe. */
2766 expr_type = shorten_binary_op (expr_type,
2767 TREE_OPERAND (expr, 0),
2768 TREE_OPERAND (expr, 1),
2769 /* bitwise */1);
2771 if (TREE_CODE (expr) == BIT_AND_EXPR)
2773 tree op0 = TREE_OPERAND (expr, 0);
2774 tree op1 = TREE_OPERAND (expr, 1);
2775 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2776 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2778 /* If one of the operands is a non-negative constant
2779 that fits in the target type, then the type of the
2780 other operand does not matter. */
2781 if ((TREE_CODE (op0) == INTEGER_CST
2782 && int_fits_type_p (op0, c_common_signed_type (type))
2783 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2784 || (TREE_CODE (op1) == INTEGER_CST
2785 && int_fits_type_p (op1, c_common_signed_type (type))
2786 && int_fits_type_p (op1,
2787 c_common_unsigned_type (type))))
2788 return SAFE_CONVERSION;
2789 /* If constant is unsigned and fits in the target
2790 type, then the result will also fit. */
2791 else if ((TREE_CODE (op0) == INTEGER_CST
2792 && unsigned0
2793 && int_fits_type_p (op0, type))
2794 || (TREE_CODE (op1) == INTEGER_CST
2795 && unsigned1
2796 && int_fits_type_p (op1, type)))
2797 return SAFE_CONVERSION;
2800 /* Warn for integer types converted to smaller integer types. */
2801 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2802 give_warning = UNSAFE_OTHER;
2804 /* When they are the same width but different signedness,
2805 then the value may change. */
2806 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2807 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2808 /* Even when converted to a bigger type, if the type is
2809 unsigned but expr is signed, then negative values
2810 will be changed. */
2811 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2812 && produce_warns)
2813 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2814 "may change the sign of the result",
2815 type, expr_type);
2818 /* Warn for integer types converted to real types if and only if
2819 all the range of values of the integer type cannot be
2820 represented by the real type. */
2821 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2822 && TREE_CODE (type) == REAL_TYPE)
2824 tree type_low_bound, type_high_bound;
2825 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2827 /* Don't warn about char y = 0xff; float x = (int) y; */
2828 expr = get_unwidened (expr, 0);
2829 expr_type = TREE_TYPE (expr);
2831 type_low_bound = TYPE_MIN_VALUE (expr_type);
2832 type_high_bound = TYPE_MAX_VALUE (expr_type);
2833 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2834 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2836 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2837 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2838 give_warning = UNSAFE_OTHER;
2841 /* Warn for real types converted to smaller real types. */
2842 else if (TREE_CODE (expr_type) == REAL_TYPE
2843 && TREE_CODE (type) == REAL_TYPE
2844 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2845 give_warning = UNSAFE_REAL;
2848 return give_warning;
2851 /* Warns if the conversion of EXPR to TYPE may alter a value.
2852 This is a helper function for warnings_for_convert_and_check. */
2854 static void
2855 conversion_warning (location_t loc, tree type, tree expr)
2857 tree expr_type = TREE_TYPE (expr);
2858 enum conversion_safety conversion_kind;
2860 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2861 return;
2863 /* This may happen, because for LHS op= RHS we preevaluate
2864 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2865 means we could no longer see the code of the EXPR. */
2866 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2867 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2868 if (TREE_CODE (expr) == SAVE_EXPR)
2869 expr = TREE_OPERAND (expr, 0);
2871 switch (TREE_CODE (expr))
2873 case EQ_EXPR:
2874 case NE_EXPR:
2875 case LE_EXPR:
2876 case GE_EXPR:
2877 case LT_EXPR:
2878 case GT_EXPR:
2879 case TRUTH_ANDIF_EXPR:
2880 case TRUTH_ORIF_EXPR:
2881 case TRUTH_AND_EXPR:
2882 case TRUTH_OR_EXPR:
2883 case TRUTH_XOR_EXPR:
2884 case TRUTH_NOT_EXPR:
2885 /* Conversion from boolean to a signed:1 bit-field (which only
2886 can hold the values 0 and -1) doesn't lose information - but
2887 it does change the value. */
2888 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2889 warning_at (loc, OPT_Wconversion,
2890 "conversion to %qT from boolean expression", type);
2891 return;
2893 case REAL_CST:
2894 case INTEGER_CST:
2895 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2896 if (conversion_kind == UNSAFE_REAL)
2897 warning_at (loc, OPT_Wfloat_conversion,
2898 "conversion to %qT alters %qT constant value",
2899 type, expr_type);
2900 else if (conversion_kind)
2901 warning_at (loc, OPT_Wconversion,
2902 "conversion to %qT alters %qT constant value",
2903 type, expr_type);
2904 return;
2906 case COND_EXPR:
2908 /* In case of COND_EXPR, we do not care about the type of
2909 COND_EXPR, only about the conversion of each operand. */
2910 tree op1 = TREE_OPERAND (expr, 1);
2911 tree op2 = TREE_OPERAND (expr, 2);
2913 conversion_warning (loc, type, op1);
2914 conversion_warning (loc, type, op2);
2915 return;
2918 default: /* 'expr' is not a constant. */
2919 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2920 if (conversion_kind == UNSAFE_REAL)
2921 warning_at (loc, OPT_Wfloat_conversion,
2922 "conversion to %qT from %qT may alter its value",
2923 type, expr_type);
2924 else if (conversion_kind)
2925 warning_at (loc, OPT_Wconversion,
2926 "conversion to %qT from %qT may alter its value",
2927 type, expr_type);
2931 /* Produce warnings after a conversion. RESULT is the result of
2932 converting EXPR to TYPE. This is a helper function for
2933 convert_and_check and cp_convert_and_check. */
2935 void
2936 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2937 tree result)
2939 loc = expansion_point_location_if_in_system_header (loc);
2941 if (TREE_CODE (expr) == INTEGER_CST
2942 && (TREE_CODE (type) == INTEGER_TYPE
2943 || TREE_CODE (type) == ENUMERAL_TYPE)
2944 && !int_fits_type_p (expr, type))
2946 /* Do not diagnose overflow in a constant expression merely
2947 because a conversion overflowed. */
2948 if (TREE_OVERFLOW (result))
2949 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2951 if (TYPE_UNSIGNED (type))
2953 /* This detects cases like converting -129 or 256 to
2954 unsigned char. */
2955 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2956 warning_at (loc, OPT_Woverflow,
2957 "large integer implicitly truncated to unsigned type");
2958 else
2959 conversion_warning (loc, type, expr);
2961 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2962 warning_at (loc, OPT_Woverflow,
2963 "overflow in implicit constant conversion");
2964 /* No warning for converting 0x80000000 to int. */
2965 else if (pedantic
2966 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2967 || TYPE_PRECISION (TREE_TYPE (expr))
2968 != TYPE_PRECISION (type)))
2969 warning_at (loc, OPT_Woverflow,
2970 "overflow in implicit constant conversion");
2972 else
2973 conversion_warning (loc, type, expr);
2975 else if ((TREE_CODE (result) == INTEGER_CST
2976 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2977 warning_at (loc, OPT_Woverflow,
2978 "overflow in implicit constant conversion");
2979 else
2980 conversion_warning (loc, type, expr);
2984 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2985 Invoke this function on every expression that is converted implicitly,
2986 i.e. because of language rules and not because of an explicit cast. */
2988 tree
2989 convert_and_check (location_t loc, tree type, tree expr)
2991 tree result;
2992 tree expr_for_warning;
2994 /* Convert from a value with possible excess precision rather than
2995 via the semantic type, but do not warn about values not fitting
2996 exactly in the semantic type. */
2997 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2999 tree orig_type = TREE_TYPE (expr);
3000 expr = TREE_OPERAND (expr, 0);
3001 expr_for_warning = convert (orig_type, expr);
3002 if (orig_type == type)
3003 return expr_for_warning;
3005 else
3006 expr_for_warning = expr;
3008 if (TREE_TYPE (expr) == type)
3009 return expr;
3011 result = convert (type, expr);
3013 if (c_inhibit_evaluation_warnings == 0
3014 && !TREE_OVERFLOW_P (expr)
3015 && result != error_mark_node)
3016 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
3018 return result;
3021 /* A node in a list that describes references to variables (EXPR), which are
3022 either read accesses if WRITER is zero, or write accesses, in which case
3023 WRITER is the parent of EXPR. */
3024 struct tlist
3026 struct tlist *next;
3027 tree expr, writer;
3030 /* Used to implement a cache the results of a call to verify_tree. We only
3031 use this for SAVE_EXPRs. */
3032 struct tlist_cache
3034 struct tlist_cache *next;
3035 struct tlist *cache_before_sp;
3036 struct tlist *cache_after_sp;
3037 tree expr;
3040 /* Obstack to use when allocating tlist structures, and corresponding
3041 firstobj. */
3042 static struct obstack tlist_obstack;
3043 static char *tlist_firstobj = 0;
3045 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3046 warnings. */
3047 static struct tlist *warned_ids;
3048 /* SAVE_EXPRs need special treatment. We process them only once and then
3049 cache the results. */
3050 static struct tlist_cache *save_expr_cache;
3052 static void add_tlist (struct tlist **, struct tlist *, tree, int);
3053 static void merge_tlist (struct tlist **, struct tlist *, int);
3054 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3055 static int warning_candidate_p (tree);
3056 static bool candidate_equal_p (const_tree, const_tree);
3057 static void warn_for_collisions (struct tlist *);
3058 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3059 static struct tlist *new_tlist (struct tlist *, tree, tree);
3061 /* Create a new struct tlist and fill in its fields. */
3062 static struct tlist *
3063 new_tlist (struct tlist *next, tree t, tree writer)
3065 struct tlist *l;
3066 l = XOBNEW (&tlist_obstack, struct tlist);
3067 l->next = next;
3068 l->expr = t;
3069 l->writer = writer;
3070 return l;
3073 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3074 is nonnull, we ignore any node we find which has a writer equal to it. */
3076 static void
3077 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3079 while (add)
3081 struct tlist *next = add->next;
3082 if (!copy)
3083 add->next = *to;
3084 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3085 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3086 add = next;
3090 /* Merge the nodes of ADD into TO. This merging process is done so that for
3091 each variable that already exists in TO, no new node is added; however if
3092 there is a write access recorded in ADD, and an occurrence on TO is only
3093 a read access, then the occurrence in TO will be modified to record the
3094 write. */
3096 static void
3097 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3099 struct tlist **end = to;
3101 while (*end)
3102 end = &(*end)->next;
3104 while (add)
3106 int found = 0;
3107 struct tlist *tmp2;
3108 struct tlist *next = add->next;
3110 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3111 if (candidate_equal_p (tmp2->expr, add->expr))
3113 found = 1;
3114 if (!tmp2->writer)
3115 tmp2->writer = add->writer;
3117 if (!found)
3119 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3120 end = &(*end)->next;
3121 *end = 0;
3123 add = next;
3127 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3128 references in list LIST conflict with it, excluding reads if ONLY writers
3129 is nonzero. */
3131 static void
3132 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3133 int only_writes)
3135 struct tlist *tmp;
3137 /* Avoid duplicate warnings. */
3138 for (tmp = warned_ids; tmp; tmp = tmp->next)
3139 if (candidate_equal_p (tmp->expr, written))
3140 return;
3142 while (list)
3144 if (candidate_equal_p (list->expr, written)
3145 && !candidate_equal_p (list->writer, writer)
3146 && (!only_writes || list->writer))
3148 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3149 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3150 OPT_Wsequence_point, "operation on %qE may be undefined",
3151 list->expr);
3153 list = list->next;
3157 /* Given a list LIST of references to variables, find whether any of these
3158 can cause conflicts due to missing sequence points. */
3160 static void
3161 warn_for_collisions (struct tlist *list)
3163 struct tlist *tmp;
3165 for (tmp = list; tmp; tmp = tmp->next)
3167 if (tmp->writer)
3168 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3172 /* Return nonzero if X is a tree that can be verified by the sequence point
3173 warnings. */
3174 static int
3175 warning_candidate_p (tree x)
3177 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3178 return 0;
3180 if (TREE_CODE (x) == BLOCK)
3181 return 0;
3183 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3184 (lvalue_p) crash on TRY/CATCH. */
3185 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3186 return 0;
3188 if (!lvalue_p (x))
3189 return 0;
3191 /* No point to track non-const calls, they will never satisfy
3192 operand_equal_p. */
3193 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3194 return 0;
3196 if (TREE_CODE (x) == STRING_CST)
3197 return 0;
3199 return 1;
3202 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3203 static bool
3204 candidate_equal_p (const_tree x, const_tree y)
3206 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3209 /* Walk the tree X, and record accesses to variables. If X is written by the
3210 parent tree, WRITER is the parent.
3211 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3212 expression or its only operand forces a sequence point, then everything up
3213 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3214 in PNO_SP.
3215 Once we return, we will have emitted warnings if any subexpression before
3216 such a sequence point could be undefined. On a higher level, however, the
3217 sequence point may not be relevant, and we'll merge the two lists.
3219 Example: (b++, a) + b;
3220 The call that processes the COMPOUND_EXPR will store the increment of B
3221 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3222 processes the PLUS_EXPR will need to merge the two lists so that
3223 eventually, all accesses end up on the same list (and we'll warn about the
3224 unordered subexpressions b++ and b.
3226 A note on merging. If we modify the former example so that our expression
3227 becomes
3228 (b++, b) + a
3229 care must be taken not simply to add all three expressions into the final
3230 PNO_SP list. The function merge_tlist takes care of that by merging the
3231 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3232 way, so that no more than one access to B is recorded. */
3234 static void
3235 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3236 tree writer)
3238 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3239 enum tree_code code;
3240 enum tree_code_class cl;
3242 /* X may be NULL if it is the operand of an empty statement expression
3243 ({ }). */
3244 if (x == NULL)
3245 return;
3247 restart:
3248 code = TREE_CODE (x);
3249 cl = TREE_CODE_CLASS (code);
3251 if (warning_candidate_p (x))
3252 *pno_sp = new_tlist (*pno_sp, x, writer);
3254 switch (code)
3256 case CONSTRUCTOR:
3257 case SIZEOF_EXPR:
3258 return;
3260 case COMPOUND_EXPR:
3261 case TRUTH_ANDIF_EXPR:
3262 case TRUTH_ORIF_EXPR:
3263 tmp_before = tmp_nosp = tmp_list3 = 0;
3264 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3265 warn_for_collisions (tmp_nosp);
3266 merge_tlist (pbefore_sp, tmp_before, 0);
3267 merge_tlist (pbefore_sp, tmp_nosp, 0);
3268 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3269 merge_tlist (pbefore_sp, tmp_list3, 0);
3270 return;
3272 case COND_EXPR:
3273 tmp_before = tmp_list2 = 0;
3274 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3275 warn_for_collisions (tmp_list2);
3276 merge_tlist (pbefore_sp, tmp_before, 0);
3277 merge_tlist (pbefore_sp, tmp_list2, 0);
3279 tmp_list3 = tmp_nosp = 0;
3280 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3281 warn_for_collisions (tmp_nosp);
3282 merge_tlist (pbefore_sp, tmp_list3, 0);
3284 tmp_list3 = tmp_list2 = 0;
3285 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3286 warn_for_collisions (tmp_list2);
3287 merge_tlist (pbefore_sp, tmp_list3, 0);
3288 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3289 two first, to avoid warning for (a ? b++ : b++). */
3290 merge_tlist (&tmp_nosp, tmp_list2, 0);
3291 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3292 return;
3294 case PREDECREMENT_EXPR:
3295 case PREINCREMENT_EXPR:
3296 case POSTDECREMENT_EXPR:
3297 case POSTINCREMENT_EXPR:
3298 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3299 return;
3301 case MODIFY_EXPR:
3302 tmp_before = tmp_nosp = tmp_list3 = 0;
3303 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3304 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3305 /* Expressions inside the LHS are not ordered wrt. the sequence points
3306 in the RHS. Example:
3307 *a = (a++, 2)
3308 Despite the fact that the modification of "a" is in the before_sp
3309 list (tmp_before), it conflicts with the use of "a" in the LHS.
3310 We can handle this by adding the contents of tmp_list3
3311 to those of tmp_before, and redoing the collision warnings for that
3312 list. */
3313 add_tlist (&tmp_before, tmp_list3, x, 1);
3314 warn_for_collisions (tmp_before);
3315 /* Exclude the LHS itself here; we first have to merge it into the
3316 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3317 didn't exclude the LHS, we'd get it twice, once as a read and once
3318 as a write. */
3319 add_tlist (pno_sp, tmp_list3, x, 0);
3320 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3322 merge_tlist (pbefore_sp, tmp_before, 0);
3323 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3324 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3325 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3326 return;
3328 case CALL_EXPR:
3329 /* We need to warn about conflicts among arguments and conflicts between
3330 args and the function address. Side effects of the function address,
3331 however, are not ordered by the sequence point of the call. */
3333 call_expr_arg_iterator iter;
3334 tree arg;
3335 tmp_before = tmp_nosp = 0;
3336 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3337 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3339 tmp_list2 = tmp_list3 = 0;
3340 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3341 merge_tlist (&tmp_list3, tmp_list2, 0);
3342 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3344 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3345 warn_for_collisions (tmp_before);
3346 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3347 return;
3350 case TREE_LIST:
3351 /* Scan all the list, e.g. indices of multi dimensional array. */
3352 while (x)
3354 tmp_before = tmp_nosp = 0;
3355 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3356 merge_tlist (&tmp_nosp, tmp_before, 0);
3357 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3358 x = TREE_CHAIN (x);
3360 return;
3362 case SAVE_EXPR:
3364 struct tlist_cache *t;
3365 for (t = save_expr_cache; t; t = t->next)
3366 if (candidate_equal_p (t->expr, x))
3367 break;
3369 if (!t)
3371 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3372 t->next = save_expr_cache;
3373 t->expr = x;
3374 save_expr_cache = t;
3376 tmp_before = tmp_nosp = 0;
3377 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3378 warn_for_collisions (tmp_nosp);
3380 tmp_list3 = 0;
3381 merge_tlist (&tmp_list3, tmp_nosp, 0);
3382 t->cache_before_sp = tmp_before;
3383 t->cache_after_sp = tmp_list3;
3385 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3386 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3387 return;
3390 case ADDR_EXPR:
3391 x = TREE_OPERAND (x, 0);
3392 if (DECL_P (x))
3393 return;
3394 writer = 0;
3395 goto restart;
3397 default:
3398 /* For other expressions, simply recurse on their operands.
3399 Manual tail recursion for unary expressions.
3400 Other non-expressions need not be processed. */
3401 if (cl == tcc_unary)
3403 x = TREE_OPERAND (x, 0);
3404 writer = 0;
3405 goto restart;
3407 else if (IS_EXPR_CODE_CLASS (cl))
3409 int lp;
3410 int max = TREE_OPERAND_LENGTH (x);
3411 for (lp = 0; lp < max; lp++)
3413 tmp_before = tmp_nosp = 0;
3414 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3415 merge_tlist (&tmp_nosp, tmp_before, 0);
3416 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3419 return;
3423 /* Try to warn for undefined behavior in EXPR due to missing sequence
3424 points. */
3426 DEBUG_FUNCTION void
3427 verify_sequence_points (tree expr)
3429 struct tlist *before_sp = 0, *after_sp = 0;
3431 warned_ids = 0;
3432 save_expr_cache = 0;
3433 if (tlist_firstobj == 0)
3435 gcc_obstack_init (&tlist_obstack);
3436 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3439 verify_tree (expr, &before_sp, &after_sp, 0);
3440 warn_for_collisions (after_sp);
3441 obstack_free (&tlist_obstack, tlist_firstobj);
3444 /* Validate the expression after `case' and apply default promotions. */
3446 static tree
3447 check_case_value (location_t loc, tree value)
3449 if (value == NULL_TREE)
3450 return value;
3452 if (TREE_CODE (value) == INTEGER_CST)
3453 /* Promote char or short to int. */
3454 value = perform_integral_promotions (value);
3455 else if (value != error_mark_node)
3457 error_at (loc, "case label does not reduce to an integer constant");
3458 value = error_mark_node;
3461 constant_expression_warning (value);
3463 return value;
3466 /* See if the case values LOW and HIGH are in the range of the original
3467 type (i.e. before the default conversion to int) of the switch testing
3468 expression.
3469 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3470 the type before promoting it. CASE_LOW_P is a pointer to the lower
3471 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3472 if the case is not a case range.
3473 The caller has to make sure that we are not called with NULL for
3474 CASE_LOW_P (i.e. the default case).
3475 Returns true if the case label is in range of ORIG_TYPE (saturated or
3476 untouched) or false if the label is out of range. */
3478 static bool
3479 check_case_bounds (location_t loc, tree type, tree orig_type,
3480 tree *case_low_p, tree *case_high_p)
3482 tree min_value, max_value;
3483 tree case_low = *case_low_p;
3484 tree case_high = case_high_p ? *case_high_p : case_low;
3486 /* If there was a problem with the original type, do nothing. */
3487 if (orig_type == error_mark_node)
3488 return true;
3490 min_value = TYPE_MIN_VALUE (orig_type);
3491 max_value = TYPE_MAX_VALUE (orig_type);
3493 /* Case label is less than minimum for type. */
3494 if (tree_int_cst_compare (case_low, min_value) < 0
3495 && tree_int_cst_compare (case_high, min_value) < 0)
3497 warning_at (loc, 0, "case label value is less than minimum value "
3498 "for type");
3499 return false;
3502 /* Case value is greater than maximum for type. */
3503 if (tree_int_cst_compare (case_low, max_value) > 0
3504 && tree_int_cst_compare (case_high, max_value) > 0)
3506 warning_at (loc, 0, "case label value exceeds maximum value for type");
3507 return false;
3510 /* Saturate lower case label value to minimum. */
3511 if (tree_int_cst_compare (case_high, min_value) >= 0
3512 && tree_int_cst_compare (case_low, min_value) < 0)
3514 warning_at (loc, 0, "lower value in case label range"
3515 " less than minimum value for type");
3516 case_low = min_value;
3519 /* Saturate upper case label value to maximum. */
3520 if (tree_int_cst_compare (case_low, max_value) <= 0
3521 && tree_int_cst_compare (case_high, max_value) > 0)
3523 warning_at (loc, 0, "upper value in case label range"
3524 " exceeds maximum value for type");
3525 case_high = max_value;
3528 if (*case_low_p != case_low)
3529 *case_low_p = convert (type, case_low);
3530 if (case_high_p && *case_high_p != case_high)
3531 *case_high_p = convert (type, case_high);
3533 return true;
3536 /* Return an integer type with BITS bits of precision,
3537 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3539 tree
3540 c_common_type_for_size (unsigned int bits, int unsignedp)
3542 int i;
3544 if (bits == TYPE_PRECISION (integer_type_node))
3545 return unsignedp ? unsigned_type_node : integer_type_node;
3547 if (bits == TYPE_PRECISION (signed_char_type_node))
3548 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3550 if (bits == TYPE_PRECISION (short_integer_type_node))
3551 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3553 if (bits == TYPE_PRECISION (long_integer_type_node))
3554 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3556 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3557 return (unsignedp ? long_long_unsigned_type_node
3558 : long_long_integer_type_node);
3560 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3561 if (int_n_enabled_p[i]
3562 && bits == int_n_data[i].bitsize)
3563 return (unsignedp ? int_n_trees[i].unsigned_type
3564 : int_n_trees[i].signed_type);
3566 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3567 return (unsignedp ? widest_unsigned_literal_type_node
3568 : widest_integer_literal_type_node);
3570 if (bits <= TYPE_PRECISION (intQI_type_node))
3571 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3573 if (bits <= TYPE_PRECISION (intHI_type_node))
3574 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3576 if (bits <= TYPE_PRECISION (intSI_type_node))
3577 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3579 if (bits <= TYPE_PRECISION (intDI_type_node))
3580 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3582 return 0;
3585 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3586 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3587 and saturating if SATP is nonzero, otherwise not saturating. */
3589 tree
3590 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3591 int unsignedp, int satp)
3593 machine_mode mode;
3594 if (ibit == 0)
3595 mode = unsignedp ? UQQmode : QQmode;
3596 else
3597 mode = unsignedp ? UHAmode : HAmode;
3599 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3600 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3601 break;
3603 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3605 sorry ("GCC cannot support operators with integer types and "
3606 "fixed-point types that have too many integral and "
3607 "fractional bits together");
3608 return 0;
3611 return c_common_type_for_mode (mode, satp);
3614 /* Used for communication between c_common_type_for_mode and
3615 c_register_builtin_type. */
3616 tree registered_builtin_types;
3618 /* Return a data type that has machine mode MODE.
3619 If the mode is an integer,
3620 then UNSIGNEDP selects between signed and unsigned types.
3621 If the mode is a fixed-point mode,
3622 then UNSIGNEDP selects between saturating and nonsaturating types. */
3624 tree
3625 c_common_type_for_mode (machine_mode mode, int unsignedp)
3627 tree t;
3628 int i;
3630 if (mode == TYPE_MODE (integer_type_node))
3631 return unsignedp ? unsigned_type_node : integer_type_node;
3633 if (mode == TYPE_MODE (signed_char_type_node))
3634 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3636 if (mode == TYPE_MODE (short_integer_type_node))
3637 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3639 if (mode == TYPE_MODE (long_integer_type_node))
3640 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3642 if (mode == TYPE_MODE (long_long_integer_type_node))
3643 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3645 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3646 if (int_n_enabled_p[i]
3647 && mode == int_n_data[i].m)
3648 return (unsignedp ? int_n_trees[i].unsigned_type
3649 : int_n_trees[i].signed_type);
3651 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3652 return unsignedp ? widest_unsigned_literal_type_node
3653 : widest_integer_literal_type_node;
3655 if (mode == QImode)
3656 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3658 if (mode == HImode)
3659 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3661 if (mode == SImode)
3662 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3664 if (mode == DImode)
3665 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3667 #if HOST_BITS_PER_WIDE_INT >= 64
3668 if (mode == TYPE_MODE (intTI_type_node))
3669 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3670 #endif
3672 if (mode == TYPE_MODE (float_type_node))
3673 return float_type_node;
3675 if (mode == TYPE_MODE (double_type_node))
3676 return double_type_node;
3678 if (mode == TYPE_MODE (long_double_type_node))
3679 return long_double_type_node;
3681 if (mode == TYPE_MODE (void_type_node))
3682 return void_type_node;
3684 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3685 return (unsignedp
3686 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3687 : make_signed_type (GET_MODE_PRECISION (mode)));
3689 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3690 return (unsignedp
3691 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3692 : make_signed_type (GET_MODE_PRECISION (mode)));
3694 if (COMPLEX_MODE_P (mode))
3696 machine_mode inner_mode;
3697 tree inner_type;
3699 if (mode == TYPE_MODE (complex_float_type_node))
3700 return complex_float_type_node;
3701 if (mode == TYPE_MODE (complex_double_type_node))
3702 return complex_double_type_node;
3703 if (mode == TYPE_MODE (complex_long_double_type_node))
3704 return complex_long_double_type_node;
3706 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3707 return complex_integer_type_node;
3709 inner_mode = GET_MODE_INNER (mode);
3710 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3711 if (inner_type != NULL_TREE)
3712 return build_complex_type (inner_type);
3714 else if (VECTOR_MODE_P (mode))
3716 machine_mode inner_mode = GET_MODE_INNER (mode);
3717 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3718 if (inner_type != NULL_TREE)
3719 return build_vector_type_for_mode (inner_type, mode);
3722 if (mode == TYPE_MODE (dfloat32_type_node))
3723 return dfloat32_type_node;
3724 if (mode == TYPE_MODE (dfloat64_type_node))
3725 return dfloat64_type_node;
3726 if (mode == TYPE_MODE (dfloat128_type_node))
3727 return dfloat128_type_node;
3729 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3731 if (mode == TYPE_MODE (short_fract_type_node))
3732 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3733 if (mode == TYPE_MODE (fract_type_node))
3734 return unsignedp ? sat_fract_type_node : fract_type_node;
3735 if (mode == TYPE_MODE (long_fract_type_node))
3736 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3737 if (mode == TYPE_MODE (long_long_fract_type_node))
3738 return unsignedp ? sat_long_long_fract_type_node
3739 : long_long_fract_type_node;
3741 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3742 return unsignedp ? sat_unsigned_short_fract_type_node
3743 : unsigned_short_fract_type_node;
3744 if (mode == TYPE_MODE (unsigned_fract_type_node))
3745 return unsignedp ? sat_unsigned_fract_type_node
3746 : unsigned_fract_type_node;
3747 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3748 return unsignedp ? sat_unsigned_long_fract_type_node
3749 : unsigned_long_fract_type_node;
3750 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3751 return unsignedp ? sat_unsigned_long_long_fract_type_node
3752 : unsigned_long_long_fract_type_node;
3754 if (mode == TYPE_MODE (short_accum_type_node))
3755 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3756 if (mode == TYPE_MODE (accum_type_node))
3757 return unsignedp ? sat_accum_type_node : accum_type_node;
3758 if (mode == TYPE_MODE (long_accum_type_node))
3759 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3760 if (mode == TYPE_MODE (long_long_accum_type_node))
3761 return unsignedp ? sat_long_long_accum_type_node
3762 : long_long_accum_type_node;
3764 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3765 return unsignedp ? sat_unsigned_short_accum_type_node
3766 : unsigned_short_accum_type_node;
3767 if (mode == TYPE_MODE (unsigned_accum_type_node))
3768 return unsignedp ? sat_unsigned_accum_type_node
3769 : unsigned_accum_type_node;
3770 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3771 return unsignedp ? sat_unsigned_long_accum_type_node
3772 : unsigned_long_accum_type_node;
3773 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3774 return unsignedp ? sat_unsigned_long_long_accum_type_node
3775 : unsigned_long_long_accum_type_node;
3777 if (mode == QQmode)
3778 return unsignedp ? sat_qq_type_node : qq_type_node;
3779 if (mode == HQmode)
3780 return unsignedp ? sat_hq_type_node : hq_type_node;
3781 if (mode == SQmode)
3782 return unsignedp ? sat_sq_type_node : sq_type_node;
3783 if (mode == DQmode)
3784 return unsignedp ? sat_dq_type_node : dq_type_node;
3785 if (mode == TQmode)
3786 return unsignedp ? sat_tq_type_node : tq_type_node;
3788 if (mode == UQQmode)
3789 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3790 if (mode == UHQmode)
3791 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3792 if (mode == USQmode)
3793 return unsignedp ? sat_usq_type_node : usq_type_node;
3794 if (mode == UDQmode)
3795 return unsignedp ? sat_udq_type_node : udq_type_node;
3796 if (mode == UTQmode)
3797 return unsignedp ? sat_utq_type_node : utq_type_node;
3799 if (mode == HAmode)
3800 return unsignedp ? sat_ha_type_node : ha_type_node;
3801 if (mode == SAmode)
3802 return unsignedp ? sat_sa_type_node : sa_type_node;
3803 if (mode == DAmode)
3804 return unsignedp ? sat_da_type_node : da_type_node;
3805 if (mode == TAmode)
3806 return unsignedp ? sat_ta_type_node : ta_type_node;
3808 if (mode == UHAmode)
3809 return unsignedp ? sat_uha_type_node : uha_type_node;
3810 if (mode == USAmode)
3811 return unsignedp ? sat_usa_type_node : usa_type_node;
3812 if (mode == UDAmode)
3813 return unsignedp ? sat_uda_type_node : uda_type_node;
3814 if (mode == UTAmode)
3815 return unsignedp ? sat_uta_type_node : uta_type_node;
3818 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3819 if (TYPE_MODE (TREE_VALUE (t)) == mode
3820 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3821 return TREE_VALUE (t);
3823 return 0;
3826 tree
3827 c_common_unsigned_type (tree type)
3829 return c_common_signed_or_unsigned_type (1, type);
3832 /* Return a signed type the same as TYPE in other respects. */
3834 tree
3835 c_common_signed_type (tree type)
3837 return c_common_signed_or_unsigned_type (0, type);
3840 /* Return a type the same as TYPE except unsigned or
3841 signed according to UNSIGNEDP. */
3843 tree
3844 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3846 tree type1;
3847 int i;
3849 /* This block of code emulates the behavior of the old
3850 c_common_unsigned_type. In particular, it returns
3851 long_unsigned_type_node if passed a long, even when a int would
3852 have the same size. This is necessary for warnings to work
3853 correctly in archs where sizeof(int) == sizeof(long) */
3855 type1 = TYPE_MAIN_VARIANT (type);
3856 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3857 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3858 if (type1 == integer_type_node || type1 == unsigned_type_node)
3859 return unsignedp ? unsigned_type_node : integer_type_node;
3860 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3861 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3862 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3863 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3864 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3865 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3867 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3868 if (int_n_enabled_p[i]
3869 && (type1 == int_n_trees[i].unsigned_type
3870 || type1 == int_n_trees[i].signed_type))
3871 return (unsignedp ? int_n_trees[i].unsigned_type
3872 : int_n_trees[i].signed_type);
3874 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3875 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3876 #if HOST_BITS_PER_WIDE_INT >= 64
3877 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3878 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3879 #endif
3880 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3881 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3882 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3883 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3884 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3885 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3886 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3887 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3889 #define C_COMMON_FIXED_TYPES(NAME) \
3890 if (type1 == short_ ## NAME ## _type_node \
3891 || type1 == unsigned_short_ ## NAME ## _type_node) \
3892 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3893 : short_ ## NAME ## _type_node; \
3894 if (type1 == NAME ## _type_node \
3895 || type1 == unsigned_ ## NAME ## _type_node) \
3896 return unsignedp ? unsigned_ ## NAME ## _type_node \
3897 : NAME ## _type_node; \
3898 if (type1 == long_ ## NAME ## _type_node \
3899 || type1 == unsigned_long_ ## NAME ## _type_node) \
3900 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3901 : long_ ## NAME ## _type_node; \
3902 if (type1 == long_long_ ## NAME ## _type_node \
3903 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3904 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3905 : long_long_ ## NAME ## _type_node;
3907 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3908 if (type1 == NAME ## _type_node \
3909 || type1 == u ## NAME ## _type_node) \
3910 return unsignedp ? u ## NAME ## _type_node \
3911 : NAME ## _type_node;
3913 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3914 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3915 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3916 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3917 : sat_ ## short_ ## NAME ## _type_node; \
3918 if (type1 == sat_ ## NAME ## _type_node \
3919 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3920 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3921 : sat_ ## NAME ## _type_node; \
3922 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3923 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3924 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3925 : sat_ ## long_ ## NAME ## _type_node; \
3926 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3927 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3928 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3929 : sat_ ## long_long_ ## NAME ## _type_node;
3931 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3932 if (type1 == sat_ ## NAME ## _type_node \
3933 || type1 == sat_ ## u ## NAME ## _type_node) \
3934 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3935 : sat_ ## NAME ## _type_node;
3937 C_COMMON_FIXED_TYPES (fract);
3938 C_COMMON_FIXED_TYPES_SAT (fract);
3939 C_COMMON_FIXED_TYPES (accum);
3940 C_COMMON_FIXED_TYPES_SAT (accum);
3942 C_COMMON_FIXED_MODE_TYPES (qq);
3943 C_COMMON_FIXED_MODE_TYPES (hq);
3944 C_COMMON_FIXED_MODE_TYPES (sq);
3945 C_COMMON_FIXED_MODE_TYPES (dq);
3946 C_COMMON_FIXED_MODE_TYPES (tq);
3947 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3948 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3949 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3950 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3951 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3952 C_COMMON_FIXED_MODE_TYPES (ha);
3953 C_COMMON_FIXED_MODE_TYPES (sa);
3954 C_COMMON_FIXED_MODE_TYPES (da);
3955 C_COMMON_FIXED_MODE_TYPES (ta);
3956 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3957 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3958 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3959 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3961 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3962 the precision; they have precision set to match their range, but
3963 may use a wider mode to match an ABI. If we change modes, we may
3964 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3965 the precision as well, so as to yield correct results for
3966 bit-field types. C++ does not have these separate bit-field
3967 types, and producing a signed or unsigned variant of an
3968 ENUMERAL_TYPE may cause other problems as well. */
3970 if (!INTEGRAL_TYPE_P (type)
3971 || TYPE_UNSIGNED (type) == unsignedp)
3972 return type;
3974 #define TYPE_OK(node) \
3975 (TYPE_MODE (type) == TYPE_MODE (node) \
3976 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3977 if (TYPE_OK (signed_char_type_node))
3978 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3979 if (TYPE_OK (integer_type_node))
3980 return unsignedp ? unsigned_type_node : integer_type_node;
3981 if (TYPE_OK (short_integer_type_node))
3982 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3983 if (TYPE_OK (long_integer_type_node))
3984 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3985 if (TYPE_OK (long_long_integer_type_node))
3986 return (unsignedp ? long_long_unsigned_type_node
3987 : long_long_integer_type_node);
3989 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3990 if (int_n_enabled_p[i]
3991 && TYPE_MODE (type) == int_n_data[i].m
3992 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3993 return (unsignedp ? int_n_trees[i].unsigned_type
3994 : int_n_trees[i].signed_type);
3996 if (TYPE_OK (widest_integer_literal_type_node))
3997 return (unsignedp ? widest_unsigned_literal_type_node
3998 : widest_integer_literal_type_node);
4000 #if HOST_BITS_PER_WIDE_INT >= 64
4001 if (TYPE_OK (intTI_type_node))
4002 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4003 #endif
4004 if (TYPE_OK (intDI_type_node))
4005 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4006 if (TYPE_OK (intSI_type_node))
4007 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4008 if (TYPE_OK (intHI_type_node))
4009 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4010 if (TYPE_OK (intQI_type_node))
4011 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4012 #undef TYPE_OK
4014 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4017 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4019 tree
4020 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4022 int i;
4024 /* Extended integer types of the same width as a standard type have
4025 lesser rank, so those of the same width as int promote to int or
4026 unsigned int and are valid for printf formats expecting int or
4027 unsigned int. To avoid such special cases, avoid creating
4028 extended integer types for bit-fields if a standard integer type
4029 is available. */
4030 if (width == TYPE_PRECISION (integer_type_node))
4031 return unsignedp ? unsigned_type_node : integer_type_node;
4032 if (width == TYPE_PRECISION (signed_char_type_node))
4033 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4034 if (width == TYPE_PRECISION (short_integer_type_node))
4035 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4036 if (width == TYPE_PRECISION (long_integer_type_node))
4037 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4038 if (width == TYPE_PRECISION (long_long_integer_type_node))
4039 return (unsignedp ? long_long_unsigned_type_node
4040 : long_long_integer_type_node);
4041 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4042 if (int_n_enabled_p[i]
4043 && width == int_n_data[i].bitsize)
4044 return (unsignedp ? int_n_trees[i].unsigned_type
4045 : int_n_trees[i].signed_type);
4046 return build_nonstandard_integer_type (width, unsignedp);
4049 /* The C version of the register_builtin_type langhook. */
4051 void
4052 c_register_builtin_type (tree type, const char* name)
4054 tree decl;
4056 decl = build_decl (UNKNOWN_LOCATION,
4057 TYPE_DECL, get_identifier (name), type);
4058 DECL_ARTIFICIAL (decl) = 1;
4059 if (!TYPE_NAME (type))
4060 TYPE_NAME (type) = decl;
4061 pushdecl (decl);
4063 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4066 /* Print an error message for invalid operands to arith operation
4067 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4068 LOCATION is the location of the message. */
4070 void
4071 binary_op_error (location_t location, enum tree_code code,
4072 tree type0, tree type1)
4074 const char *opname;
4076 switch (code)
4078 case PLUS_EXPR:
4079 opname = "+"; break;
4080 case MINUS_EXPR:
4081 opname = "-"; break;
4082 case MULT_EXPR:
4083 opname = "*"; break;
4084 case MAX_EXPR:
4085 opname = "max"; break;
4086 case MIN_EXPR:
4087 opname = "min"; break;
4088 case EQ_EXPR:
4089 opname = "=="; break;
4090 case NE_EXPR:
4091 opname = "!="; break;
4092 case LE_EXPR:
4093 opname = "<="; break;
4094 case GE_EXPR:
4095 opname = ">="; break;
4096 case LT_EXPR:
4097 opname = "<"; break;
4098 case GT_EXPR:
4099 opname = ">"; break;
4100 case LSHIFT_EXPR:
4101 opname = "<<"; break;
4102 case RSHIFT_EXPR:
4103 opname = ">>"; break;
4104 case TRUNC_MOD_EXPR:
4105 case FLOOR_MOD_EXPR:
4106 opname = "%"; break;
4107 case TRUNC_DIV_EXPR:
4108 case FLOOR_DIV_EXPR:
4109 opname = "/"; break;
4110 case BIT_AND_EXPR:
4111 opname = "&"; break;
4112 case BIT_IOR_EXPR:
4113 opname = "|"; break;
4114 case TRUTH_ANDIF_EXPR:
4115 opname = "&&"; break;
4116 case TRUTH_ORIF_EXPR:
4117 opname = "||"; break;
4118 case BIT_XOR_EXPR:
4119 opname = "^"; break;
4120 default:
4121 gcc_unreachable ();
4123 error_at (location,
4124 "invalid operands to binary %s (have %qT and %qT)", opname,
4125 type0, type1);
4128 /* Given an expression as a tree, return its original type. Do this
4129 by stripping any conversion that preserves the sign and precision. */
4130 static tree
4131 expr_original_type (tree expr)
4133 STRIP_SIGN_NOPS (expr);
4134 return TREE_TYPE (expr);
4137 /* Subroutine of build_binary_op, used for comparison operations.
4138 See if the operands have both been converted from subword integer types
4139 and, if so, perhaps change them both back to their original type.
4140 This function is also responsible for converting the two operands
4141 to the proper common type for comparison.
4143 The arguments of this function are all pointers to local variables
4144 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4145 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4147 LOC is the location of the comparison.
4149 If this function returns nonzero, it means that the comparison has
4150 a constant value. What this function returns is an expression for
4151 that value. */
4153 tree
4154 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4155 tree *restype_ptr, enum tree_code *rescode_ptr)
4157 tree type;
4158 tree op0 = *op0_ptr;
4159 tree op1 = *op1_ptr;
4160 int unsignedp0, unsignedp1;
4161 int real1, real2;
4162 tree primop0, primop1;
4163 enum tree_code code = *rescode_ptr;
4165 /* Throw away any conversions to wider types
4166 already present in the operands. */
4168 primop0 = c_common_get_narrower (op0, &unsignedp0);
4169 primop1 = c_common_get_narrower (op1, &unsignedp1);
4171 /* If primopN is first sign-extended from primopN's precision to opN's
4172 precision, then zero-extended from opN's precision to
4173 *restype_ptr precision, shortenings might be invalid. */
4174 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4175 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4176 && !unsignedp0
4177 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4178 primop0 = op0;
4179 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4180 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4181 && !unsignedp1
4182 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4183 primop1 = op1;
4185 /* Handle the case that OP0 does not *contain* a conversion
4186 but it *requires* conversion to FINAL_TYPE. */
4188 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4189 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4190 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4191 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4193 /* If one of the operands must be floated, we cannot optimize. */
4194 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4195 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4197 /* If first arg is constant, swap the args (changing operation
4198 so value is preserved), for canonicalization. Don't do this if
4199 the second arg is 0. */
4201 if (TREE_CONSTANT (primop0)
4202 && !integer_zerop (primop1) && !real_zerop (primop1)
4203 && !fixed_zerop (primop1))
4205 tree tem = primop0;
4206 int temi = unsignedp0;
4207 primop0 = primop1;
4208 primop1 = tem;
4209 tem = op0;
4210 op0 = op1;
4211 op1 = tem;
4212 *op0_ptr = op0;
4213 *op1_ptr = op1;
4214 unsignedp0 = unsignedp1;
4215 unsignedp1 = temi;
4216 temi = real1;
4217 real1 = real2;
4218 real2 = temi;
4220 switch (code)
4222 case LT_EXPR:
4223 code = GT_EXPR;
4224 break;
4225 case GT_EXPR:
4226 code = LT_EXPR;
4227 break;
4228 case LE_EXPR:
4229 code = GE_EXPR;
4230 break;
4231 case GE_EXPR:
4232 code = LE_EXPR;
4233 break;
4234 default:
4235 break;
4237 *rescode_ptr = code;
4240 /* If comparing an integer against a constant more bits wide,
4241 maybe we can deduce a value of 1 or 0 independent of the data.
4242 Or else truncate the constant now
4243 rather than extend the variable at run time.
4245 This is only interesting if the constant is the wider arg.
4246 Also, it is not safe if the constant is unsigned and the
4247 variable arg is signed, since in this case the variable
4248 would be sign-extended and then regarded as unsigned.
4249 Our technique fails in this case because the lowest/highest
4250 possible unsigned results don't follow naturally from the
4251 lowest/highest possible values of the variable operand.
4252 For just EQ_EXPR and NE_EXPR there is another technique that
4253 could be used: see if the constant can be faithfully represented
4254 in the other operand's type, by truncating it and reextending it
4255 and see if that preserves the constant's value. */
4257 if (!real1 && !real2
4258 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4259 && TREE_CODE (primop1) == INTEGER_CST
4260 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4262 int min_gt, max_gt, min_lt, max_lt;
4263 tree maxval, minval;
4264 /* 1 if comparison is nominally unsigned. */
4265 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4266 tree val;
4268 type = c_common_signed_or_unsigned_type (unsignedp0,
4269 TREE_TYPE (primop0));
4271 maxval = TYPE_MAX_VALUE (type);
4272 minval = TYPE_MIN_VALUE (type);
4274 if (unsignedp && !unsignedp0)
4275 *restype_ptr = c_common_signed_type (*restype_ptr);
4277 if (TREE_TYPE (primop1) != *restype_ptr)
4279 /* Convert primop1 to target type, but do not introduce
4280 additional overflow. We know primop1 is an int_cst. */
4281 primop1 = force_fit_type (*restype_ptr,
4282 wide_int::from
4283 (primop1,
4284 TYPE_PRECISION (*restype_ptr),
4285 TYPE_SIGN (TREE_TYPE (primop1))),
4286 0, TREE_OVERFLOW (primop1));
4288 if (type != *restype_ptr)
4290 minval = convert (*restype_ptr, minval);
4291 maxval = convert (*restype_ptr, maxval);
4294 min_gt = tree_int_cst_lt (primop1, minval);
4295 max_gt = tree_int_cst_lt (primop1, maxval);
4296 min_lt = tree_int_cst_lt (minval, primop1);
4297 max_lt = tree_int_cst_lt (maxval, primop1);
4299 val = 0;
4300 /* This used to be a switch, but Genix compiler can't handle that. */
4301 if (code == NE_EXPR)
4303 if (max_lt || min_gt)
4304 val = truthvalue_true_node;
4306 else if (code == EQ_EXPR)
4308 if (max_lt || min_gt)
4309 val = truthvalue_false_node;
4311 else if (code == LT_EXPR)
4313 if (max_lt)
4314 val = truthvalue_true_node;
4315 if (!min_lt)
4316 val = truthvalue_false_node;
4318 else if (code == GT_EXPR)
4320 if (min_gt)
4321 val = truthvalue_true_node;
4322 if (!max_gt)
4323 val = truthvalue_false_node;
4325 else if (code == LE_EXPR)
4327 if (!max_gt)
4328 val = truthvalue_true_node;
4329 if (min_gt)
4330 val = truthvalue_false_node;
4332 else if (code == GE_EXPR)
4334 if (!min_lt)
4335 val = truthvalue_true_node;
4336 if (max_lt)
4337 val = truthvalue_false_node;
4340 /* If primop0 was sign-extended and unsigned comparison specd,
4341 we did a signed comparison above using the signed type bounds.
4342 But the comparison we output must be unsigned.
4344 Also, for inequalities, VAL is no good; but if the signed
4345 comparison had *any* fixed result, it follows that the
4346 unsigned comparison just tests the sign in reverse
4347 (positive values are LE, negative ones GE).
4348 So we can generate an unsigned comparison
4349 against an extreme value of the signed type. */
4351 if (unsignedp && !unsignedp0)
4353 if (val != 0)
4354 switch (code)
4356 case LT_EXPR:
4357 case GE_EXPR:
4358 primop1 = TYPE_MIN_VALUE (type);
4359 val = 0;
4360 break;
4362 case LE_EXPR:
4363 case GT_EXPR:
4364 primop1 = TYPE_MAX_VALUE (type);
4365 val = 0;
4366 break;
4368 default:
4369 break;
4371 type = c_common_unsigned_type (type);
4374 if (TREE_CODE (primop0) != INTEGER_CST)
4376 if (val == truthvalue_false_node)
4377 warning_at (loc, OPT_Wtype_limits,
4378 "comparison is always false due to limited range of data type");
4379 if (val == truthvalue_true_node)
4380 warning_at (loc, OPT_Wtype_limits,
4381 "comparison is always true due to limited range of data type");
4384 if (val != 0)
4386 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4387 if (TREE_SIDE_EFFECTS (primop0))
4388 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4389 return val;
4392 /* Value is not predetermined, but do the comparison
4393 in the type of the operand that is not constant.
4394 TYPE is already properly set. */
4397 /* If either arg is decimal float and the other is float, find the
4398 proper common type to use for comparison. */
4399 else if (real1 && real2
4400 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4401 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4402 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4404 /* If either arg is decimal float and the other is float, fail. */
4405 else if (real1 && real2
4406 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4407 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4408 return 0;
4410 else if (real1 && real2
4411 && (TYPE_PRECISION (TREE_TYPE (primop0))
4412 == TYPE_PRECISION (TREE_TYPE (primop1))))
4413 type = TREE_TYPE (primop0);
4415 /* If args' natural types are both narrower than nominal type
4416 and both extend in the same manner, compare them
4417 in the type of the wider arg.
4418 Otherwise must actually extend both to the nominal
4419 common type lest different ways of extending
4420 alter the result.
4421 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4423 else if (unsignedp0 == unsignedp1 && real1 == real2
4424 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4425 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4427 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4428 type = c_common_signed_or_unsigned_type (unsignedp0
4429 || TYPE_UNSIGNED (*restype_ptr),
4430 type);
4431 /* Make sure shorter operand is extended the right way
4432 to match the longer operand. */
4433 primop0
4434 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4435 TREE_TYPE (primop0)),
4436 primop0);
4437 primop1
4438 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4439 TREE_TYPE (primop1)),
4440 primop1);
4442 else
4444 /* Here we must do the comparison on the nominal type
4445 using the args exactly as we received them. */
4446 type = *restype_ptr;
4447 primop0 = op0;
4448 primop1 = op1;
4450 if (!real1 && !real2 && integer_zerop (primop1)
4451 && TYPE_UNSIGNED (*restype_ptr))
4453 tree value = 0;
4454 /* All unsigned values are >= 0, so we warn. However,
4455 if OP0 is a constant that is >= 0, the signedness of
4456 the comparison isn't an issue, so suppress the
4457 warning. */
4458 bool warn =
4459 warn_type_limits && !in_system_header_at (loc)
4460 && !(TREE_CODE (primop0) == INTEGER_CST
4461 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4462 primop0)))
4463 /* Do not warn for enumeration types. */
4464 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4466 switch (code)
4468 case GE_EXPR:
4469 if (warn)
4470 warning_at (loc, OPT_Wtype_limits,
4471 "comparison of unsigned expression >= 0 is always true");
4472 value = truthvalue_true_node;
4473 break;
4475 case LT_EXPR:
4476 if (warn)
4477 warning_at (loc, OPT_Wtype_limits,
4478 "comparison of unsigned expression < 0 is always false");
4479 value = truthvalue_false_node;
4480 break;
4482 default:
4483 break;
4486 if (value != 0)
4488 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4489 if (TREE_SIDE_EFFECTS (primop0))
4490 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4491 primop0, value);
4492 return value;
4497 *op0_ptr = convert (type, primop0);
4498 *op1_ptr = convert (type, primop1);
4500 *restype_ptr = truthvalue_type_node;
4502 return 0;
4505 /* Return a tree for the sum or difference (RESULTCODE says which)
4506 of pointer PTROP and integer INTOP. */
4508 tree
4509 pointer_int_sum (location_t loc, enum tree_code resultcode,
4510 tree ptrop, tree intop, bool complain)
4512 tree size_exp, ret;
4514 /* The result is a pointer of the same type that is being added. */
4515 tree result_type = TREE_TYPE (ptrop);
4517 /* If the pointer lives in UPC shared memory, then
4518 drop the 'shared' qualifier. */
4519 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4520 result_type = build_upc_unshared_type (result_type);
4522 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4524 if (complain && warn_pointer_arith)
4525 pedwarn (loc, OPT_Wpointer_arith,
4526 "pointer of type %<void *%> used in arithmetic");
4527 else if (!complain)
4528 return error_mark_node;
4529 size_exp = integer_one_node;
4531 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4533 if (complain && warn_pointer_arith)
4534 pedwarn (loc, OPT_Wpointer_arith,
4535 "pointer to a function used in arithmetic");
4536 else if (!complain)
4537 return error_mark_node;
4538 size_exp = integer_one_node;
4540 else
4541 size_exp = size_in_bytes (TREE_TYPE (result_type));
4543 /* We are manipulating pointer values, so we don't need to warn
4544 about relying on undefined signed overflow. We disable the
4545 warning here because we use integer types so fold won't know that
4546 they are really pointers. */
4547 fold_defer_overflow_warnings ();
4549 /* If what we are about to multiply by the size of the elements
4550 contains a constant term, apply distributive law
4551 and multiply that constant term separately.
4552 This helps produce common subexpressions. */
4553 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4554 && !TREE_CONSTANT (intop)
4555 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4556 && TREE_CONSTANT (size_exp)
4557 /* If the constant comes from pointer subtraction,
4558 skip this optimization--it would cause an error. */
4559 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4560 /* If the constant is unsigned, and smaller than the pointer size,
4561 then we must skip this optimization. This is because it could cause
4562 an overflow error if the constant is negative but INTOP is not. */
4563 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4564 || (TYPE_PRECISION (TREE_TYPE (intop))
4565 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4567 enum tree_code subcode = resultcode;
4568 tree int_type = TREE_TYPE (intop);
4569 if (TREE_CODE (intop) == MINUS_EXPR)
4570 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4571 /* Convert both subexpression types to the type of intop,
4572 because weird cases involving pointer arithmetic
4573 can result in a sum or difference with different type args. */
4574 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4575 subcode, ptrop,
4576 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4577 intop = convert (int_type, TREE_OPERAND (intop, 0));
4580 /* Convert the integer argument to a type the same size as sizetype
4581 so the multiply won't overflow spuriously. */
4582 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4583 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4584 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4585 TYPE_UNSIGNED (sizetype)), intop);
4587 /* Replace the integer argument with a suitable product by the object size.
4588 Do this multiplication as signed, then convert to the appropriate type
4589 for the pointer operation and disregard an overflow that occurred only
4590 because of the sign-extension change in the latter conversion. */
4592 tree t = build_binary_op (loc,
4593 MULT_EXPR, intop,
4594 convert (TREE_TYPE (intop), size_exp), 1);
4595 intop = convert (sizetype, t);
4596 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4597 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4600 /* Create the sum or difference. */
4601 if (resultcode == MINUS_EXPR)
4602 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4604 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4606 fold_undefer_and_ignore_overflow_warnings ();
4608 return ret;
4611 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4612 and if NON_CONST is known not to be permitted in an evaluated part
4613 of a constant expression. */
4615 tree
4616 c_wrap_maybe_const (tree expr, bool non_const)
4618 bool nowarning = TREE_NO_WARNING (expr);
4619 location_t loc = EXPR_LOCATION (expr);
4621 /* This should never be called for C++. */
4622 if (c_dialect_cxx ())
4623 gcc_unreachable ();
4625 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4626 STRIP_TYPE_NOPS (expr);
4627 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4628 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4629 if (nowarning)
4630 TREE_NO_WARNING (expr) = 1;
4631 protected_set_expr_location (expr, loc);
4633 return expr;
4636 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4637 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4638 around the SAVE_EXPR if needed so that c_fully_fold does not need
4639 to look inside SAVE_EXPRs. */
4641 tree
4642 c_save_expr (tree expr)
4644 bool maybe_const = true;
4645 if (c_dialect_cxx ())
4646 return save_expr (expr);
4647 expr = c_fully_fold (expr, false, &maybe_const);
4648 expr = save_expr (expr);
4649 if (!maybe_const)
4650 expr = c_wrap_maybe_const (expr, true);
4651 return expr;
4654 /* Return whether EXPR is a declaration whose address can never be
4655 NULL. */
4657 bool
4658 decl_with_nonnull_addr_p (const_tree expr)
4660 return (DECL_P (expr)
4661 && (TREE_CODE (expr) == PARM_DECL
4662 || TREE_CODE (expr) == LABEL_DECL
4663 || !DECL_WEAK (expr)));
4666 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4667 or for an `if' or `while' statement or ?..: exp. It should already
4668 have been validated to be of suitable type; otherwise, a bad
4669 diagnostic may result.
4671 The EXPR is located at LOCATION.
4673 This preparation consists of taking the ordinary
4674 representation of an expression expr and producing a valid tree
4675 boolean expression describing whether expr is nonzero. We could
4676 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4677 but we optimize comparisons, &&, ||, and !.
4679 The resulting type should always be `truthvalue_type_node'. */
4681 tree
4682 c_common_truthvalue_conversion (location_t location, tree expr)
4684 switch (TREE_CODE (expr))
4686 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4687 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4688 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4689 case ORDERED_EXPR: case UNORDERED_EXPR:
4690 if (TREE_TYPE (expr) == truthvalue_type_node)
4691 return expr;
4692 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4693 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4694 goto ret;
4696 case TRUTH_ANDIF_EXPR:
4697 case TRUTH_ORIF_EXPR:
4698 case TRUTH_AND_EXPR:
4699 case TRUTH_OR_EXPR:
4700 case TRUTH_XOR_EXPR:
4701 if (TREE_TYPE (expr) == truthvalue_type_node)
4702 return expr;
4703 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4704 c_common_truthvalue_conversion (location,
4705 TREE_OPERAND (expr, 0)),
4706 c_common_truthvalue_conversion (location,
4707 TREE_OPERAND (expr, 1)));
4708 goto ret;
4710 case TRUTH_NOT_EXPR:
4711 if (TREE_TYPE (expr) == truthvalue_type_node)
4712 return expr;
4713 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4714 c_common_truthvalue_conversion (location,
4715 TREE_OPERAND (expr, 0)));
4716 goto ret;
4718 case ERROR_MARK:
4719 return expr;
4721 case INTEGER_CST:
4722 return integer_zerop (expr) ? truthvalue_false_node
4723 : truthvalue_true_node;
4725 case REAL_CST:
4726 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4727 ? truthvalue_true_node
4728 : truthvalue_false_node;
4730 case FIXED_CST:
4731 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4732 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4733 ? truthvalue_true_node
4734 : truthvalue_false_node;
4736 case FUNCTION_DECL:
4737 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4738 /* Fall through. */
4740 case ADDR_EXPR:
4742 tree inner = TREE_OPERAND (expr, 0);
4743 if (decl_with_nonnull_addr_p (inner))
4745 /* Common Ada/Pascal programmer's mistake. */
4746 warning_at (location,
4747 OPT_Waddress,
4748 "the address of %qD will always evaluate as %<true%>",
4749 inner);
4750 return truthvalue_true_node;
4752 break;
4755 case COMPLEX_EXPR:
4756 expr = build_binary_op (EXPR_LOCATION (expr),
4757 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4758 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4759 c_common_truthvalue_conversion (location,
4760 TREE_OPERAND (expr, 0)),
4761 c_common_truthvalue_conversion (location,
4762 TREE_OPERAND (expr, 1)),
4764 goto ret;
4766 case NEGATE_EXPR:
4767 case ABS_EXPR:
4768 case FLOAT_EXPR:
4769 case EXCESS_PRECISION_EXPR:
4770 /* These don't change whether an object is nonzero or zero. */
4771 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4773 case LROTATE_EXPR:
4774 case RROTATE_EXPR:
4775 /* These don't change whether an object is zero or nonzero, but
4776 we can't ignore them if their second arg has side-effects. */
4777 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4779 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4780 TREE_OPERAND (expr, 1),
4781 c_common_truthvalue_conversion
4782 (location, TREE_OPERAND (expr, 0)));
4783 goto ret;
4785 else
4786 return c_common_truthvalue_conversion (location,
4787 TREE_OPERAND (expr, 0));
4789 case COND_EXPR:
4790 /* Distribute the conversion into the arms of a COND_EXPR. */
4791 if (c_dialect_cxx ())
4793 tree op1 = TREE_OPERAND (expr, 1);
4794 tree op2 = TREE_OPERAND (expr, 2);
4795 /* In C++ one of the arms might have void type if it is throw. */
4796 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4797 op1 = c_common_truthvalue_conversion (location, op1);
4798 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4799 op2 = c_common_truthvalue_conversion (location, op2);
4800 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4801 TREE_OPERAND (expr, 0), op1, op2);
4802 goto ret;
4804 else
4806 /* Folding will happen later for C. */
4807 expr = build3 (COND_EXPR, truthvalue_type_node,
4808 TREE_OPERAND (expr, 0),
4809 c_common_truthvalue_conversion (location,
4810 TREE_OPERAND (expr, 1)),
4811 c_common_truthvalue_conversion (location,
4812 TREE_OPERAND (expr, 2)));
4813 goto ret;
4816 CASE_CONVERT:
4818 tree totype = TREE_TYPE (expr);
4819 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4821 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4822 since that affects how `default_conversion' will behave. */
4823 if (TREE_CODE (totype) == REFERENCE_TYPE
4824 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4825 break;
4826 /* Don't strip a conversion from C++0x scoped enum, since they
4827 don't implicitly convert to other types. */
4828 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4829 && ENUM_IS_SCOPED (fromtype))
4830 break;
4831 /* If this isn't narrowing the argument, we can ignore it. */
4832 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4833 return c_common_truthvalue_conversion (location,
4834 TREE_OPERAND (expr, 0));
4836 break;
4838 case MODIFY_EXPR:
4839 if (!TREE_NO_WARNING (expr)
4840 && warn_parentheses)
4842 warning (OPT_Wparentheses,
4843 "suggest parentheses around assignment used as truth value");
4844 TREE_NO_WARNING (expr) = 1;
4846 break;
4848 default:
4849 break;
4852 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4854 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4855 expr = (build_binary_op
4856 (EXPR_LOCATION (expr),
4857 (TREE_SIDE_EFFECTS (expr)
4858 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4859 c_common_truthvalue_conversion
4860 (location,
4861 build_unary_op (location, REALPART_EXPR, t, 0)),
4862 c_common_truthvalue_conversion
4863 (location,
4864 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4865 0));
4866 goto ret;
4869 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4871 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4872 FCONST0 (TYPE_MODE
4873 (TREE_TYPE (expr))));
4874 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4876 else
4877 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4879 ret:
4880 protected_set_expr_location (expr, location);
4881 return expr;
4884 static void def_builtin_1 (enum built_in_function fncode,
4885 const char *name,
4886 enum built_in_class fnclass,
4887 tree fntype, tree libtype,
4888 bool both_p, bool fallback_p, bool nonansi_p,
4889 tree fnattrs, bool implicit_p);
4892 /* Apply the TYPE_QUALS to the new DECL. */
4894 void
4895 c_apply_type_quals_to_decl (int type_quals, tree decl)
4897 tree type = TREE_TYPE (decl);
4899 if (type == error_mark_node)
4900 return;
4902 if ((type_quals & TYPE_QUAL_CONST)
4903 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4904 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4905 constructor can produce constant init, so rely on cp_finish_decl to
4906 clear TREE_READONLY if the variable has non-constant init. */
4907 TREE_READONLY (decl) = 1;
4908 if (type_quals & TYPE_QUAL_VOLATILE)
4910 TREE_SIDE_EFFECTS (decl) = 1;
4911 TREE_THIS_VOLATILE (decl) = 1;
4913 if (type_quals & TYPE_QUAL_RESTRICT)
4915 while (type && TREE_CODE (type) == ARRAY_TYPE)
4916 /* Allow 'restrict' on arrays of pointers.
4917 FIXME currently we just ignore it. */
4918 type = TREE_TYPE (type);
4919 if (!type
4920 || !POINTER_TYPE_P (type)
4921 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4922 error ("invalid use of %<restrict%>");
4924 if (type_quals & TYPE_QUAL_UPC_SHARED)
4926 TREE_SHARED (decl) = 1;
4927 if (type_quals & TYPE_QUAL_UPC_STRICT)
4928 TREE_STRICT(decl) = 1;
4929 else if (type_quals & TYPE_QUAL_UPC_RELAXED)
4930 TREE_RELAXED(decl) = 1;
4931 /* The declaration's type should have been previously defined
4932 as a UPC shared type. */
4933 gcc_assert (upc_shared_type_p (type));
4937 struct c_type_hasher : ggc_hasher<tree>
4939 static hashval_t hash (tree);
4940 static bool equal (tree, tree);
4943 /* Hash function for the problem of multiple type definitions in
4944 different files. This must hash all types that will compare
4945 equal via comptypes to the same value. In practice it hashes
4946 on some of the simple stuff and leaves the details to comptypes. */
4948 hashval_t
4949 c_type_hasher::hash (tree t)
4951 int n_elements;
4952 int shift, size;
4953 tree t2;
4954 switch (TREE_CODE (t))
4956 /* For pointers, hash on pointee type plus some swizzling. */
4957 case POINTER_TYPE:
4958 return hash (TREE_TYPE (t)) ^ 0x3003003;
4959 /* Hash on number of elements and total size. */
4960 case ENUMERAL_TYPE:
4961 shift = 3;
4962 t2 = TYPE_VALUES (t);
4963 break;
4964 case RECORD_TYPE:
4965 shift = 0;
4966 t2 = TYPE_FIELDS (t);
4967 break;
4968 case QUAL_UNION_TYPE:
4969 shift = 1;
4970 t2 = TYPE_FIELDS (t);
4971 break;
4972 case UNION_TYPE:
4973 shift = 2;
4974 t2 = TYPE_FIELDS (t);
4975 break;
4976 default:
4977 gcc_unreachable ();
4979 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4980 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4981 n_elements = list_length (t2);
4982 /* We might have a VLA here. */
4983 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4984 size = 0;
4985 else
4986 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4987 return ((size << 24) | (n_elements << shift));
4990 bool
4991 c_type_hasher::equal (tree t1, tree t2)
4993 return lang_hooks.types_compatible_p (t1, t2);
4996 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4998 /* Return the typed-based alias set for T, which may be an expression
4999 or a type. Return -1 if we don't do anything special. */
5001 alias_set_type
5002 c_common_get_alias_set (tree t)
5004 tree u;
5006 /* For VLAs, use the alias set of the element type rather than the
5007 default of alias set 0 for types compared structurally. */
5008 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5010 if (TREE_CODE (t) == ARRAY_TYPE)
5011 return get_alias_set (TREE_TYPE (t));
5012 return -1;
5015 /* Permit type-punning when accessing a union, provided the access
5016 is directly through the union. For example, this code does not
5017 permit taking the address of a union member and then storing
5018 through it. Even the type-punning allowed here is a GCC
5019 extension, albeit a common and useful one; the C standard says
5020 that such accesses have implementation-defined behavior. */
5021 for (u = t;
5022 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5023 u = TREE_OPERAND (u, 0))
5024 if (TREE_CODE (u) == COMPONENT_REF
5025 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5026 return 0;
5028 /* That's all the expressions we handle specially. */
5029 if (!TYPE_P (t))
5030 return -1;
5032 /* The C standard guarantees that any object may be accessed via an
5033 lvalue that has character type. */
5034 if (t == char_type_node
5035 || t == signed_char_type_node
5036 || t == unsigned_char_type_node)
5037 return 0;
5039 /* The C standard specifically allows aliasing between signed and
5040 unsigned variants of the same type. We treat the signed
5041 variant as canonical. */
5042 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
5044 tree t1 = c_common_signed_type (t);
5046 /* t1 == t can happen for boolean nodes which are always unsigned. */
5047 if (t1 != t)
5048 return get_alias_set (t1);
5051 /* For the time being, make UPC pointers-to-shared conflict
5052 with everything else. Ideally, UPC pointers-to-shared should
5053 only conflict with the internal type used to represent
5054 the UPC pointer-to-shared (i.e., upc_pts_rep_type_node). */
5056 if (TYPE_P (t) ? (TREE_CODE (t) == POINTER_TYPE
5057 && upc_shared_type_p (TREE_TYPE (t)))
5058 : (TREE_TYPE(t)
5059 && TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
5060 && upc_shared_type_p (TREE_TYPE (TREE_TYPE (t)))))
5061 return 0;
5063 /* Handle the case of multiple type nodes referring to "the same" type,
5064 which occurs with IMA. These share an alias set. FIXME: Currently only
5065 C90 is handled. (In C99 type compatibility is not transitive, which
5066 complicates things mightily. The alias set splay trees can theoretically
5067 represent this, but insertion is tricky when you consider all the
5068 different orders things might arrive in.) */
5070 if (c_language != clk_c || flag_isoc99)
5071 return -1;
5073 /* Save time if there's only one input file. */
5074 if (num_in_fnames == 1)
5075 return -1;
5077 /* Pointers need special handling if they point to any type that
5078 needs special handling (below). */
5079 if (TREE_CODE (t) == POINTER_TYPE)
5081 tree t2;
5082 /* Find bottom type under any nested POINTERs. */
5083 for (t2 = TREE_TYPE (t);
5084 TREE_CODE (t2) == POINTER_TYPE;
5085 t2 = TREE_TYPE (t2))
5087 if (TREE_CODE (t2) != RECORD_TYPE
5088 && TREE_CODE (t2) != ENUMERAL_TYPE
5089 && TREE_CODE (t2) != QUAL_UNION_TYPE
5090 && TREE_CODE (t2) != UNION_TYPE)
5091 return -1;
5092 if (TYPE_SIZE (t2) == 0)
5093 return -1;
5095 /* These are the only cases that need special handling. */
5096 if (TREE_CODE (t) != RECORD_TYPE
5097 && TREE_CODE (t) != ENUMERAL_TYPE
5098 && TREE_CODE (t) != QUAL_UNION_TYPE
5099 && TREE_CODE (t) != UNION_TYPE
5100 && TREE_CODE (t) != POINTER_TYPE)
5101 return -1;
5102 /* Undefined? */
5103 if (TYPE_SIZE (t) == 0)
5104 return -1;
5106 /* Look up t in hash table. Only one of the compatible types within each
5107 alias set is recorded in the table. */
5108 if (!type_hash_table)
5109 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5110 tree *slot = type_hash_table->find_slot (t, INSERT);
5111 if (*slot != NULL)
5113 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5114 return TYPE_ALIAS_SET ((tree)*slot);
5116 else
5117 /* Our caller will assign and record (in t) a new alias set; all we need
5118 to do is remember t in the hash table. */
5119 *slot = t;
5121 return -1;
5124 /* Return the value of THREADS.
5126 UPC defines a reserved variable, THREADS, which returns the
5127 number of threads that will be created when the UPC program
5128 executes. The value of threads can be specified at runtime via
5129 the -fupc-threads=N switch, where N is an integer specifying
5130 the number of threads. When the value of THREADS is specified
5131 at compile-time, this is called the "static threads compilation
5132 environment".
5134 In the static threads compilation environment, THREADS is a
5135 pre-defined preprocessor macro with the value, N.
5137 If no value for threads is given at compile-time, then the value
5138 must be specified when the application program is executed.
5139 This method of establishing the value of THREADS is called
5140 the "dynamic threads compilation environment". */
5142 tree
5143 upc_num_threads (void)
5145 tree n;
5146 gcc_assert (flag_upc);
5147 n = flag_upc_threads ? ssize_int (flag_upc_threads)
5148 : lookup_name (get_identifier ("THREADS"));
5149 if (!n)
5151 error ("the UPC-required THREADS variable is undefined; "
5152 "when compiling pre-processed source, "
5153 "all -fupc-* switches must be passed on the command line, "
5154 "asserting the same values as supplied when the "
5155 "original source file was preprocessed");
5156 abort ();
5159 return n;
5162 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5163 the IS_SIZEOF parameter indicates which operator is being applied.
5164 The COMPLAIN flag controls whether we should diagnose possibly
5165 ill-formed constructs or not. LOC is the location of the SIZEOF or
5166 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5167 a type in any context should be returned, rather than the normal
5168 alignment for that type. */
5170 tree
5171 c_sizeof_or_alignof_type (location_t loc,
5172 tree type, bool is_sizeof, bool min_alignof,
5173 int complain)
5175 const char *op_name;
5176 tree value = NULL;
5177 enum tree_code type_code = TREE_CODE (type);
5179 op_name = is_sizeof ? "sizeof" : "__alignof__";
5181 if (type_code == FUNCTION_TYPE)
5183 if (is_sizeof)
5185 if (complain && warn_pointer_arith)
5186 pedwarn (loc, OPT_Wpointer_arith,
5187 "invalid application of %<sizeof%> to a function type");
5188 else if (!complain)
5189 return error_mark_node;
5190 value = size_one_node;
5192 else
5194 if (complain)
5196 if (c_dialect_cxx ())
5197 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5198 "%<alignof%> applied to a function type");
5199 else
5200 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5201 "%<_Alignof%> applied to a function type");
5203 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5206 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5208 if (complain)
5210 if (type_code == VOID_TYPE && upc_shared_type_p (type))
5211 error_at (loc, "invalid application of %qs"
5212 " to %<shared void%> type", op_name);
5213 else if (type_code == VOID_TYPE && warn_pointer_arith)
5214 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
5215 "invalid application of %qs to a void type", op_name);
5217 else
5218 return error_mark_node;
5219 value = size_one_node;
5221 else if (!COMPLETE_TYPE_P (type)
5222 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5224 if (complain)
5225 error_at (loc, "invalid application of %qs to incomplete type %qT",
5226 op_name, type);
5227 return error_mark_node;
5229 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5230 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5232 if (complain)
5233 error_at (loc, "invalid application of %qs to array type %qT of "
5234 "incomplete element type", op_name, type);
5235 return error_mark_node;
5237 else
5239 if (is_sizeof)
5240 /* Convert in case a char is more than one unit. */
5241 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5242 size_int (TYPE_PRECISION (char_type_node)
5243 / BITS_PER_UNIT));
5244 else if (min_alignof)
5245 value = size_int (min_align_of_type (type));
5246 else
5247 value = size_int (TYPE_ALIGN_UNIT (type));
5250 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
5251 && upc_shared_type_p (type)
5252 && TYPE_HAS_THREADS_FACTOR (type))
5254 const tree n_threads = convert (sizetype, upc_num_threads ());
5255 value = size_binop (MULT_EXPR, value, n_threads);
5258 /* VALUE will have the middle-end integer type sizetype.
5259 However, we should really return a value of type `size_t',
5260 which is just a typedef for an ordinary integer type. */
5261 value = fold_convert_loc (loc, size_type_node, value);
5263 return value;
5266 /* Implement the __alignof keyword: Return the minimum required
5267 alignment of EXPR, measured in bytes. For VAR_DECLs,
5268 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5269 from an "aligned" __attribute__ specification). LOC is the
5270 location of the ALIGNOF operator. */
5272 tree
5273 c_alignof_expr (location_t loc, tree expr)
5275 tree t;
5277 if (VAR_OR_FUNCTION_DECL_P (expr))
5278 t = size_int (DECL_ALIGN_UNIT (expr));
5280 else if (TREE_CODE (expr) == COMPONENT_REF
5281 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5283 error_at (loc, "%<__alignof%> applied to a bit-field");
5284 t = size_one_node;
5286 else if (TREE_CODE (expr) == COMPONENT_REF
5287 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5288 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5290 else if (TREE_CODE (expr) == INDIRECT_REF)
5292 tree t = TREE_OPERAND (expr, 0);
5293 tree best = t;
5294 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5296 while (CONVERT_EXPR_P (t)
5297 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5299 int thisalign;
5301 t = TREE_OPERAND (t, 0);
5302 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5303 if (thisalign > bestalign)
5304 best = t, bestalign = thisalign;
5306 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5308 else
5309 return c_alignof (loc, TREE_TYPE (expr));
5311 return fold_convert_loc (loc, size_type_node, t);
5314 /* Handle C and C++ default attributes. */
5316 enum built_in_attribute
5318 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5319 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5320 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5321 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5322 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5323 #include "builtin-attrs.def"
5324 #undef DEF_ATTR_NULL_TREE
5325 #undef DEF_ATTR_INT
5326 #undef DEF_ATTR_STRING
5327 #undef DEF_ATTR_IDENT
5328 #undef DEF_ATTR_TREE_LIST
5329 ATTR_LAST
5332 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5334 static void c_init_attributes (void);
5336 enum c_builtin_type
5338 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5339 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5340 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5341 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5342 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5343 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5344 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5345 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5346 ARG6) NAME,
5347 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5348 ARG6, ARG7) NAME,
5349 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5350 ARG6, ARG7, ARG8) NAME,
5351 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5352 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5353 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5354 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5355 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5356 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5357 NAME,
5358 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5359 ARG6, ARG7) NAME,
5360 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5361 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5362 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5363 #include "builtin-types.def"
5364 #undef DEF_PRIMITIVE_TYPE
5365 #undef DEF_FUNCTION_TYPE_0
5366 #undef DEF_FUNCTION_TYPE_1
5367 #undef DEF_FUNCTION_TYPE_2
5368 #undef DEF_FUNCTION_TYPE_3
5369 #undef DEF_FUNCTION_TYPE_4
5370 #undef DEF_FUNCTION_TYPE_5
5371 #undef DEF_FUNCTION_TYPE_6
5372 #undef DEF_FUNCTION_TYPE_7
5373 #undef DEF_FUNCTION_TYPE_8
5374 #undef DEF_FUNCTION_TYPE_VAR_0
5375 #undef DEF_FUNCTION_TYPE_VAR_1
5376 #undef DEF_FUNCTION_TYPE_VAR_2
5377 #undef DEF_FUNCTION_TYPE_VAR_3
5378 #undef DEF_FUNCTION_TYPE_VAR_4
5379 #undef DEF_FUNCTION_TYPE_VAR_5
5380 #undef DEF_FUNCTION_TYPE_VAR_7
5381 #undef DEF_FUNCTION_TYPE_VAR_11
5382 #undef DEF_POINTER_TYPE
5383 BT_LAST
5386 typedef enum c_builtin_type builtin_type;
5388 /* A temporary array for c_common_nodes_and_builtins. Used in
5389 communication with def_fn_type. */
5390 static tree builtin_types[(int) BT_LAST + 1];
5392 /* A helper function for c_common_nodes_and_builtins. Build function type
5393 for DEF with return type RET and N arguments. If VAR is true, then the
5394 function should be variadic after those N arguments.
5396 Takes special care not to ICE if any of the types involved are
5397 error_mark_node, which indicates that said type is not in fact available
5398 (see builtin_type_for_size). In which case the function type as a whole
5399 should be error_mark_node. */
5401 static void
5402 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5404 tree t;
5405 tree *args = XALLOCAVEC (tree, n);
5406 va_list list;
5407 int i;
5409 va_start (list, n);
5410 for (i = 0; i < n; ++i)
5412 builtin_type a = (builtin_type) va_arg (list, int);
5413 t = builtin_types[a];
5414 if (t == error_mark_node)
5415 goto egress;
5416 args[i] = t;
5419 t = builtin_types[ret];
5420 if (t == error_mark_node)
5421 goto egress;
5422 if (var)
5423 t = build_varargs_function_type_array (t, n, args);
5424 else
5425 t = build_function_type_array (t, n, args);
5427 egress:
5428 builtin_types[def] = t;
5429 va_end (list);
5432 /* Build builtin functions common to both C and C++ language
5433 frontends. */
5435 static void
5436 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5438 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5439 builtin_types[ENUM] = VALUE;
5440 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5441 def_fn_type (ENUM, RETURN, 0, 0);
5442 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5443 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5444 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5445 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5446 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5447 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5448 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5449 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5450 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5451 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5452 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5453 ARG6) \
5454 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5455 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5456 ARG6, ARG7) \
5457 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5458 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5459 ARG6, ARG7, ARG8) \
5460 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5461 ARG7, ARG8);
5462 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5463 def_fn_type (ENUM, RETURN, 1, 0);
5464 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5465 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5466 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5467 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5468 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5469 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5470 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5471 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5472 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5473 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5474 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5475 ARG6, ARG7) \
5476 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5477 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5478 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5479 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5480 ARG7, ARG8, ARG9, ARG10, ARG11);
5481 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5482 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5484 #include "builtin-types.def"
5486 #undef DEF_PRIMITIVE_TYPE
5487 #undef DEF_FUNCTION_TYPE_0
5488 #undef DEF_FUNCTION_TYPE_1
5489 #undef DEF_FUNCTION_TYPE_2
5490 #undef DEF_FUNCTION_TYPE_3
5491 #undef DEF_FUNCTION_TYPE_4
5492 #undef DEF_FUNCTION_TYPE_5
5493 #undef DEF_FUNCTION_TYPE_6
5494 #undef DEF_FUNCTION_TYPE_7
5495 #undef DEF_FUNCTION_TYPE_8
5496 #undef DEF_FUNCTION_TYPE_VAR_0
5497 #undef DEF_FUNCTION_TYPE_VAR_1
5498 #undef DEF_FUNCTION_TYPE_VAR_2
5499 #undef DEF_FUNCTION_TYPE_VAR_3
5500 #undef DEF_FUNCTION_TYPE_VAR_4
5501 #undef DEF_FUNCTION_TYPE_VAR_5
5502 #undef DEF_FUNCTION_TYPE_VAR_7
5503 #undef DEF_FUNCTION_TYPE_VAR_11
5504 #undef DEF_POINTER_TYPE
5505 builtin_types[(int) BT_LAST] = NULL_TREE;
5507 c_init_attributes ();
5509 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5510 NONANSI_P, ATTRS, IMPLICIT, COND) \
5511 if (NAME && COND) \
5512 def_builtin_1 (ENUM, NAME, CLASS, \
5513 builtin_types[(int) TYPE], \
5514 builtin_types[(int) LIBTYPE], \
5515 BOTH_P, FALLBACK_P, NONANSI_P, \
5516 built_in_attributes[(int) ATTRS], IMPLICIT);
5517 #include "builtins.def"
5518 #undef DEF_BUILTIN
5520 targetm.init_builtins ();
5522 build_common_builtin_nodes ();
5524 if (flag_cilkplus)
5525 cilk_init_builtins ();
5528 /* Like get_identifier, but avoid warnings about null arguments when
5529 the argument may be NULL for targets where GCC lacks stdint.h type
5530 information. */
5532 static inline tree
5533 c_get_ident (const char *id)
5535 return get_identifier (id);
5538 /* Build tree nodes and builtin functions common to both C and C++ language
5539 frontends. */
5541 void
5542 c_common_nodes_and_builtins (void)
5544 int char16_type_size;
5545 int char32_type_size;
5546 int wchar_type_size;
5547 tree array_domain_type;
5548 tree va_list_ref_type_node;
5549 tree va_list_arg_type_node;
5550 int i;
5552 build_common_tree_nodes (flag_signed_char, flag_short_double);
5554 /* Define `int' and `char' first so that dbx will output them first. */
5555 record_builtin_type (RID_INT, NULL, integer_type_node);
5556 record_builtin_type (RID_CHAR, "char", char_type_node);
5558 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5559 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5560 but not C. Are the conditionals here needed? */
5561 if (c_dialect_cxx ())
5562 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5563 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5564 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5565 record_builtin_type (RID_MAX, "long unsigned int",
5566 long_unsigned_type_node);
5568 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5570 char name[25];
5572 sprintf (name, "__int%d", int_n_data[i].bitsize);
5573 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5574 int_n_trees[i].signed_type);
5575 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5576 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5579 if (c_dialect_cxx ())
5580 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5581 record_builtin_type (RID_MAX, "long long int",
5582 long_long_integer_type_node);
5583 record_builtin_type (RID_MAX, "long long unsigned int",
5584 long_long_unsigned_type_node);
5585 if (c_dialect_cxx ())
5586 record_builtin_type (RID_MAX, "long long unsigned",
5587 long_long_unsigned_type_node);
5588 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5589 record_builtin_type (RID_MAX, "short unsigned int",
5590 short_unsigned_type_node);
5591 if (c_dialect_cxx ())
5592 record_builtin_type (RID_MAX, "unsigned short",
5593 short_unsigned_type_node);
5595 /* Define both `signed char' and `unsigned char'. */
5596 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5597 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5599 /* These are types that c_common_type_for_size and
5600 c_common_type_for_mode use. */
5601 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5602 TYPE_DECL, NULL_TREE,
5603 intQI_type_node));
5604 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5605 TYPE_DECL, NULL_TREE,
5606 intHI_type_node));
5607 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5608 TYPE_DECL, NULL_TREE,
5609 intSI_type_node));
5610 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5611 TYPE_DECL, NULL_TREE,
5612 intDI_type_node));
5613 #if HOST_BITS_PER_WIDE_INT >= 64
5614 /* Note that this is different than the __int128 type that's part of
5615 the generic __intN support. */
5616 if (targetm.scalar_mode_supported_p (TImode))
5617 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5618 TYPE_DECL,
5619 get_identifier ("__int128_t"),
5620 intTI_type_node));
5621 #endif
5622 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5623 TYPE_DECL, NULL_TREE,
5624 unsigned_intQI_type_node));
5625 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5626 TYPE_DECL, NULL_TREE,
5627 unsigned_intHI_type_node));
5628 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5629 TYPE_DECL, NULL_TREE,
5630 unsigned_intSI_type_node));
5631 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5632 TYPE_DECL, NULL_TREE,
5633 unsigned_intDI_type_node));
5634 #if HOST_BITS_PER_WIDE_INT >= 64
5635 if (targetm.scalar_mode_supported_p (TImode))
5636 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5637 TYPE_DECL,
5638 get_identifier ("__uint128_t"),
5639 unsigned_intTI_type_node));
5640 #endif
5642 /* Create the widest literal types. */
5643 widest_integer_literal_type_node
5644 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5645 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5646 TYPE_DECL, NULL_TREE,
5647 widest_integer_literal_type_node));
5649 widest_unsigned_literal_type_node
5650 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5651 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5652 TYPE_DECL, NULL_TREE,
5653 widest_unsigned_literal_type_node));
5655 signed_size_type_node = c_common_signed_type (size_type_node);
5657 pid_type_node =
5658 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5660 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5661 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5662 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5664 /* Only supported decimal floating point extension if the target
5665 actually supports underlying modes. */
5666 if (targetm.scalar_mode_supported_p (SDmode)
5667 && targetm.scalar_mode_supported_p (DDmode)
5668 && targetm.scalar_mode_supported_p (TDmode))
5670 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5671 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5672 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5675 if (targetm.fixed_point_supported_p ())
5677 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5678 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5679 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5680 record_builtin_type (RID_MAX, "long long _Fract",
5681 long_long_fract_type_node);
5682 record_builtin_type (RID_MAX, "unsigned short _Fract",
5683 unsigned_short_fract_type_node);
5684 record_builtin_type (RID_MAX, "unsigned _Fract",
5685 unsigned_fract_type_node);
5686 record_builtin_type (RID_MAX, "unsigned long _Fract",
5687 unsigned_long_fract_type_node);
5688 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5689 unsigned_long_long_fract_type_node);
5690 record_builtin_type (RID_MAX, "_Sat short _Fract",
5691 sat_short_fract_type_node);
5692 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5693 record_builtin_type (RID_MAX, "_Sat long _Fract",
5694 sat_long_fract_type_node);
5695 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5696 sat_long_long_fract_type_node);
5697 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5698 sat_unsigned_short_fract_type_node);
5699 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5700 sat_unsigned_fract_type_node);
5701 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5702 sat_unsigned_long_fract_type_node);
5703 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5704 sat_unsigned_long_long_fract_type_node);
5705 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5706 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5707 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5708 record_builtin_type (RID_MAX, "long long _Accum",
5709 long_long_accum_type_node);
5710 record_builtin_type (RID_MAX, "unsigned short _Accum",
5711 unsigned_short_accum_type_node);
5712 record_builtin_type (RID_MAX, "unsigned _Accum",
5713 unsigned_accum_type_node);
5714 record_builtin_type (RID_MAX, "unsigned long _Accum",
5715 unsigned_long_accum_type_node);
5716 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5717 unsigned_long_long_accum_type_node);
5718 record_builtin_type (RID_MAX, "_Sat short _Accum",
5719 sat_short_accum_type_node);
5720 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5721 record_builtin_type (RID_MAX, "_Sat long _Accum",
5722 sat_long_accum_type_node);
5723 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5724 sat_long_long_accum_type_node);
5725 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5726 sat_unsigned_short_accum_type_node);
5727 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5728 sat_unsigned_accum_type_node);
5729 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5730 sat_unsigned_long_accum_type_node);
5731 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5732 sat_unsigned_long_long_accum_type_node);
5736 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5737 TYPE_DECL,
5738 get_identifier ("complex int"),
5739 complex_integer_type_node));
5740 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5741 TYPE_DECL,
5742 get_identifier ("complex float"),
5743 complex_float_type_node));
5744 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5745 TYPE_DECL,
5746 get_identifier ("complex double"),
5747 complex_double_type_node));
5748 lang_hooks.decls.pushdecl
5749 (build_decl (UNKNOWN_LOCATION,
5750 TYPE_DECL, get_identifier ("complex long double"),
5751 complex_long_double_type_node));
5753 if (c_dialect_cxx ())
5754 /* For C++, make fileptr_type_node a distinct void * type until
5755 FILE type is defined. */
5756 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5758 record_builtin_type (RID_VOID, NULL, void_type_node);
5760 /* Set the TYPE_NAME for any variants that were built before
5761 record_builtin_type gave names to the built-in types. */
5763 tree void_name = TYPE_NAME (void_type_node);
5764 TYPE_NAME (void_type_node) = NULL_TREE;
5765 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5766 = void_name;
5767 TYPE_NAME (void_type_node) = void_name;
5770 void_list_node = build_void_list_node ();
5772 /* Make a type to be the domain of a few array types
5773 whose domains don't really matter.
5774 200 is small enough that it always fits in size_t
5775 and large enough that it can hold most function names for the
5776 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5777 array_domain_type = build_index_type (size_int (200));
5779 /* Make a type for arrays of characters.
5780 With luck nothing will ever really depend on the length of this
5781 array type. */
5782 char_array_type_node
5783 = build_array_type (char_type_node, array_domain_type);
5785 string_type_node = build_pointer_type (char_type_node);
5786 const_string_type_node
5787 = build_pointer_type (build_qualified_type
5788 (char_type_node, TYPE_QUAL_CONST));
5790 /* This is special for C++ so functions can be overloaded. */
5791 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5792 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5793 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5794 underlying_wchar_type_node = wchar_type_node;
5795 if (c_dialect_cxx ())
5797 if (TYPE_UNSIGNED (wchar_type_node))
5798 wchar_type_node = make_unsigned_type (wchar_type_size);
5799 else
5800 wchar_type_node = make_signed_type (wchar_type_size);
5801 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5804 /* This is for wide string constants. */
5805 wchar_array_type_node
5806 = build_array_type (wchar_type_node, array_domain_type);
5808 /* Define 'char16_t'. */
5809 char16_type_node = get_identifier (CHAR16_TYPE);
5810 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5811 char16_type_size = TYPE_PRECISION (char16_type_node);
5812 if (c_dialect_cxx ())
5814 char16_type_node = make_unsigned_type (char16_type_size);
5816 if (cxx_dialect >= cxx11)
5817 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5820 /* This is for UTF-16 string constants. */
5821 char16_array_type_node
5822 = build_array_type (char16_type_node, array_domain_type);
5824 /* Define 'char32_t'. */
5825 char32_type_node = get_identifier (CHAR32_TYPE);
5826 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5827 char32_type_size = TYPE_PRECISION (char32_type_node);
5828 if (c_dialect_cxx ())
5830 char32_type_node = make_unsigned_type (char32_type_size);
5832 if (cxx_dialect >= cxx11)
5833 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5836 /* This is for UTF-32 string constants. */
5837 char32_array_type_node
5838 = build_array_type (char32_type_node, array_domain_type);
5840 wint_type_node =
5841 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5843 intmax_type_node =
5844 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5845 uintmax_type_node =
5846 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5848 if (SIG_ATOMIC_TYPE)
5849 sig_atomic_type_node =
5850 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5851 if (INT8_TYPE)
5852 int8_type_node =
5853 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5854 if (INT16_TYPE)
5855 int16_type_node =
5856 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5857 if (INT32_TYPE)
5858 int32_type_node =
5859 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5860 if (INT64_TYPE)
5861 int64_type_node =
5862 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5863 if (UINT8_TYPE)
5864 uint8_type_node =
5865 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5866 if (UINT16_TYPE)
5867 c_uint16_type_node = uint16_type_node =
5868 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5869 if (UINT32_TYPE)
5870 c_uint32_type_node = uint32_type_node =
5871 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5872 if (UINT64_TYPE)
5873 c_uint64_type_node = uint64_type_node =
5874 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5875 if (INT_LEAST8_TYPE)
5876 int_least8_type_node =
5877 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5878 if (INT_LEAST16_TYPE)
5879 int_least16_type_node =
5880 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5881 if (INT_LEAST32_TYPE)
5882 int_least32_type_node =
5883 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5884 if (INT_LEAST64_TYPE)
5885 int_least64_type_node =
5886 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5887 if (UINT_LEAST8_TYPE)
5888 uint_least8_type_node =
5889 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5890 if (UINT_LEAST16_TYPE)
5891 uint_least16_type_node =
5892 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5893 if (UINT_LEAST32_TYPE)
5894 uint_least32_type_node =
5895 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5896 if (UINT_LEAST64_TYPE)
5897 uint_least64_type_node =
5898 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5899 if (INT_FAST8_TYPE)
5900 int_fast8_type_node =
5901 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5902 if (INT_FAST16_TYPE)
5903 int_fast16_type_node =
5904 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5905 if (INT_FAST32_TYPE)
5906 int_fast32_type_node =
5907 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5908 if (INT_FAST64_TYPE)
5909 int_fast64_type_node =
5910 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5911 if (UINT_FAST8_TYPE)
5912 uint_fast8_type_node =
5913 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5914 if (UINT_FAST16_TYPE)
5915 uint_fast16_type_node =
5916 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5917 if (UINT_FAST32_TYPE)
5918 uint_fast32_type_node =
5919 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5920 if (UINT_FAST64_TYPE)
5921 uint_fast64_type_node =
5922 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5923 if (INTPTR_TYPE)
5924 intptr_type_node =
5925 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5926 if (UINTPTR_TYPE)
5927 uintptr_type_node =
5928 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5930 default_function_type
5931 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5932 ptrdiff_type_node
5933 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5934 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5936 lang_hooks.decls.pushdecl
5937 (build_decl (UNKNOWN_LOCATION,
5938 TYPE_DECL, get_identifier ("__builtin_va_list"),
5939 va_list_type_node));
5940 if (targetm.enum_va_list_p)
5942 int l;
5943 const char *pname;
5944 tree ptype;
5946 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5948 lang_hooks.decls.pushdecl
5949 (build_decl (UNKNOWN_LOCATION,
5950 TYPE_DECL, get_identifier (pname),
5951 ptype));
5956 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5958 va_list_arg_type_node = va_list_ref_type_node =
5959 build_pointer_type (TREE_TYPE (va_list_type_node));
5961 else
5963 va_list_arg_type_node = va_list_type_node;
5964 va_list_ref_type_node = build_reference_type (va_list_type_node);
5967 if (!flag_preprocess_only)
5968 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5970 main_identifier_node = get_identifier ("main");
5972 /* Create the built-in __null node. It is important that this is
5973 not shared. */
5974 null_node = make_int_cst (1, 1);
5975 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5977 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5978 memset (builtin_types, 0, sizeof (builtin_types));
5981 /* The number of named compound-literals generated thus far. */
5982 static GTY(()) int compound_literal_number;
5984 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5986 void
5987 set_compound_literal_name (tree decl)
5989 char *name;
5990 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5991 compound_literal_number);
5992 compound_literal_number++;
5993 DECL_NAME (decl) = get_identifier (name);
5996 tree
5997 build_va_arg (location_t loc, tree expr, tree type)
5999 expr = build1 (VA_ARG_EXPR, type, expr);
6000 SET_EXPR_LOCATION (expr, loc);
6001 return expr;
6005 /* Linked list of disabled built-in functions. */
6007 typedef struct disabled_builtin
6009 const char *name;
6010 struct disabled_builtin *next;
6011 } disabled_builtin;
6012 static disabled_builtin *disabled_builtins = NULL;
6014 static bool builtin_function_disabled_p (const char *);
6016 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6017 begins with "__builtin_", give an error. */
6019 void
6020 disable_builtin_function (const char *name)
6022 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
6023 error ("cannot disable built-in function %qs", name);
6024 else
6026 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6027 new_disabled_builtin->name = name;
6028 new_disabled_builtin->next = disabled_builtins;
6029 disabled_builtins = new_disabled_builtin;
6034 /* Return true if the built-in function NAME has been disabled, false
6035 otherwise. */
6037 static bool
6038 builtin_function_disabled_p (const char *name)
6040 disabled_builtin *p;
6041 for (p = disabled_builtins; p != NULL; p = p->next)
6043 if (strcmp (name, p->name) == 0)
6044 return true;
6046 return false;
6050 /* Worker for DEF_BUILTIN.
6051 Possibly define a builtin function with one or two names.
6052 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6053 nonansi_p and flag_no_nonansi_builtin. */
6055 static void
6056 def_builtin_1 (enum built_in_function fncode,
6057 const char *name,
6058 enum built_in_class fnclass,
6059 tree fntype, tree libtype,
6060 bool both_p, bool fallback_p, bool nonansi_p,
6061 tree fnattrs, bool implicit_p)
6063 tree decl;
6064 const char *libname;
6066 if (fntype == error_mark_node)
6067 return;
6069 gcc_assert ((!both_p && !fallback_p)
6070 || !strncmp (name, "__builtin_",
6071 strlen ("__builtin_")));
6073 libname = name + strlen ("__builtin_");
6074 decl = add_builtin_function (name, fntype, fncode, fnclass,
6075 (fallback_p ? libname : NULL),
6076 fnattrs);
6078 set_builtin_decl (fncode, decl, implicit_p);
6080 if (both_p
6081 && !flag_no_builtin && !builtin_function_disabled_p (libname)
6082 && !(nonansi_p && flag_no_nonansi_builtin))
6083 add_builtin_function (libname, libtype, fncode, fnclass,
6084 NULL, fnattrs);
6087 /* Nonzero if the type T promotes to int. This is (nearly) the
6088 integral promotions defined in ISO C99 6.3.1.1/2. */
6090 bool
6091 c_promoting_integer_type_p (const_tree t)
6093 switch (TREE_CODE (t))
6095 case INTEGER_TYPE:
6096 return (TYPE_MAIN_VARIANT (t) == char_type_node
6097 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6098 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6099 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
6100 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6101 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
6103 case ENUMERAL_TYPE:
6104 /* ??? Technically all enumerations not larger than an int
6105 promote to an int. But this is used along code paths
6106 that only want to notice a size change. */
6107 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6109 case BOOLEAN_TYPE:
6110 return 1;
6112 default:
6113 return 0;
6117 /* Return 1 if PARMS specifies a fixed number of parameters
6118 and none of their types is affected by default promotions. */
6121 self_promoting_args_p (const_tree parms)
6123 const_tree t;
6124 for (t = parms; t; t = TREE_CHAIN (t))
6126 tree type = TREE_VALUE (t);
6128 if (type == error_mark_node)
6129 continue;
6131 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6132 return 0;
6134 if (type == 0)
6135 return 0;
6137 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6138 return 0;
6140 if (c_promoting_integer_type_p (type))
6141 return 0;
6143 return 1;
6146 /* Recursively remove any '*' or '&' operator from TYPE. */
6147 tree
6148 strip_pointer_operator (tree t)
6150 while (POINTER_TYPE_P (t))
6151 t = TREE_TYPE (t);
6152 return t;
6155 /* Recursively remove pointer or array type from TYPE. */
6156 tree
6157 strip_pointer_or_array_types (tree t)
6159 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6160 t = TREE_TYPE (t);
6161 return t;
6164 /* Used to compare case labels. K1 and K2 are actually tree nodes
6165 representing case labels, or NULL_TREE for a `default' label.
6166 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6167 K2, and 0 if K1 and K2 are equal. */
6170 case_compare (splay_tree_key k1, splay_tree_key k2)
6172 /* Consider a NULL key (such as arises with a `default' label) to be
6173 smaller than anything else. */
6174 if (!k1)
6175 return k2 ? -1 : 0;
6176 else if (!k2)
6177 return k1 ? 1 : 0;
6179 return tree_int_cst_compare ((tree) k1, (tree) k2);
6182 /* Process a case label, located at LOC, for the range LOW_VALUE
6183 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6184 then this case label is actually a `default' label. If only
6185 HIGH_VALUE is NULL_TREE, then case label was declared using the
6186 usual C/C++ syntax, rather than the GNU case range extension.
6187 CASES is a tree containing all the case ranges processed so far;
6188 COND is the condition for the switch-statement itself. Returns the
6189 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6190 is created. */
6192 tree
6193 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6194 tree low_value, tree high_value)
6196 tree type;
6197 tree label;
6198 tree case_label;
6199 splay_tree_node node;
6201 /* Create the LABEL_DECL itself. */
6202 label = create_artificial_label (loc);
6204 /* If there was an error processing the switch condition, bail now
6205 before we get more confused. */
6206 if (!cond || cond == error_mark_node)
6207 goto error_out;
6209 if ((low_value && TREE_TYPE (low_value)
6210 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6211 || (high_value && TREE_TYPE (high_value)
6212 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6214 error_at (loc, "pointers are not permitted as case values");
6215 goto error_out;
6218 /* Case ranges are a GNU extension. */
6219 if (high_value)
6220 pedwarn (loc, OPT_Wpedantic,
6221 "range expressions in switch statements are non-standard");
6223 type = TREE_TYPE (cond);
6224 if (low_value)
6226 low_value = check_case_value (loc, low_value);
6227 low_value = convert_and_check (loc, type, low_value);
6228 if (low_value == error_mark_node)
6229 goto error_out;
6231 if (high_value)
6233 high_value = check_case_value (loc, high_value);
6234 high_value = convert_and_check (loc, type, high_value);
6235 if (high_value == error_mark_node)
6236 goto error_out;
6239 if (low_value && high_value)
6241 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6242 really a case range, even though it was written that way.
6243 Remove the HIGH_VALUE to simplify later processing. */
6244 if (tree_int_cst_equal (low_value, high_value))
6245 high_value = NULL_TREE;
6246 else if (!tree_int_cst_lt (low_value, high_value))
6247 warning_at (loc, 0, "empty range specified");
6250 /* See if the case is in range of the type of the original testing
6251 expression. If both low_value and high_value are out of range,
6252 don't insert the case label and return NULL_TREE. */
6253 if (low_value
6254 && !check_case_bounds (loc, type, orig_type,
6255 &low_value, high_value ? &high_value : NULL))
6256 return NULL_TREE;
6258 /* Look up the LOW_VALUE in the table of case labels we already
6259 have. */
6260 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6261 /* If there was not an exact match, check for overlapping ranges.
6262 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6263 that's a `default' label and the only overlap is an exact match. */
6264 if (!node && (low_value || high_value))
6266 splay_tree_node low_bound;
6267 splay_tree_node high_bound;
6269 /* Even though there wasn't an exact match, there might be an
6270 overlap between this case range and another case range.
6271 Since we've (inductively) not allowed any overlapping case
6272 ranges, we simply need to find the greatest low case label
6273 that is smaller that LOW_VALUE, and the smallest low case
6274 label that is greater than LOW_VALUE. If there is an overlap
6275 it will occur in one of these two ranges. */
6276 low_bound = splay_tree_predecessor (cases,
6277 (splay_tree_key) low_value);
6278 high_bound = splay_tree_successor (cases,
6279 (splay_tree_key) low_value);
6281 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6282 the LOW_VALUE, so there is no need to check unless the
6283 LOW_BOUND is in fact itself a case range. */
6284 if (low_bound
6285 && CASE_HIGH ((tree) low_bound->value)
6286 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6287 low_value) >= 0)
6288 node = low_bound;
6289 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6290 range is bigger than the low end of the current range, so we
6291 are only interested if the current range is a real range, and
6292 not an ordinary case label. */
6293 else if (high_bound
6294 && high_value
6295 && (tree_int_cst_compare ((tree) high_bound->key,
6296 high_value)
6297 <= 0))
6298 node = high_bound;
6300 /* If there was an overlap, issue an error. */
6301 if (node)
6303 tree duplicate = CASE_LABEL ((tree) node->value);
6305 if (high_value)
6307 error_at (loc, "duplicate (or overlapping) case value");
6308 error_at (DECL_SOURCE_LOCATION (duplicate),
6309 "this is the first entry overlapping that value");
6311 else if (low_value)
6313 error_at (loc, "duplicate case value") ;
6314 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6316 else
6318 error_at (loc, "multiple default labels in one switch");
6319 error_at (DECL_SOURCE_LOCATION (duplicate),
6320 "this is the first default label");
6322 goto error_out;
6325 /* Add a CASE_LABEL to the statement-tree. */
6326 case_label = add_stmt (build_case_label (low_value, high_value, label));
6327 /* Register this case label in the splay tree. */
6328 splay_tree_insert (cases,
6329 (splay_tree_key) low_value,
6330 (splay_tree_value) case_label);
6332 return case_label;
6334 error_out:
6335 /* Add a label so that the back-end doesn't think that the beginning of
6336 the switch is unreachable. Note that we do not add a case label, as
6337 that just leads to duplicates and thence to failure later on. */
6338 if (!cases->root)
6340 tree t = create_artificial_label (loc);
6341 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6343 return error_mark_node;
6346 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6347 Used to verify that case values match up with enumerator values. */
6349 static void
6350 match_case_to_enum_1 (tree key, tree type, tree label)
6352 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6354 if (tree_fits_uhwi_p (key))
6355 print_dec (key, buf, UNSIGNED);
6356 else if (tree_fits_shwi_p (key))
6357 print_dec (key, buf, SIGNED);
6358 else
6359 print_hex (key, buf);
6361 if (TYPE_NAME (type) == 0)
6362 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6363 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6364 "case value %qs not in enumerated type",
6365 buf);
6366 else
6367 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6368 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6369 "case value %qs not in enumerated type %qT",
6370 buf, type);
6373 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6374 Used to verify that case values match up with enumerator values. */
6376 static int
6377 match_case_to_enum (splay_tree_node node, void *data)
6379 tree label = (tree) node->value;
6380 tree type = (tree) data;
6382 /* Skip default case. */
6383 if (!CASE_LOW (label))
6384 return 0;
6386 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6387 when we did our enum->case scan. Reset our scratch bit after. */
6388 if (!CASE_LOW_SEEN (label))
6389 match_case_to_enum_1 (CASE_LOW (label), type, label);
6390 else
6391 CASE_LOW_SEEN (label) = 0;
6393 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6394 not set, that means that CASE_HIGH did not appear when we did our
6395 enum->case scan. Reset our scratch bit after. */
6396 if (CASE_HIGH (label))
6398 if (!CASE_HIGH_SEEN (label))
6399 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6400 else
6401 CASE_HIGH_SEEN (label) = 0;
6404 return 0;
6407 /* Handle -Wswitch*. Called from the front end after parsing the
6408 switch construct. */
6409 /* ??? Should probably be somewhere generic, since other languages
6410 besides C and C++ would want this. At the moment, however, C/C++
6411 are the only tree-ssa languages that support enumerations at all,
6412 so the point is moot. */
6414 void
6415 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6416 tree type, tree cond)
6418 splay_tree_node default_node;
6419 splay_tree_node node;
6420 tree chain;
6422 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6423 return;
6425 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6426 if (!default_node)
6427 warning_at (switch_location, OPT_Wswitch_default,
6428 "switch missing default case");
6430 /* From here on, we only care about about enumerated types. */
6431 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6432 return;
6434 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6435 if (!warn_switch_enum && !warn_switch)
6436 return;
6438 /* Check the cases. Warn about case values which are not members of
6439 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6440 there is no default case, check that exactly all enumeration
6441 literals are covered by the cases. */
6443 /* Clearing COND if it is not an integer constant simplifies
6444 the tests inside the loop below. */
6445 if (TREE_CODE (cond) != INTEGER_CST)
6446 cond = NULL_TREE;
6448 /* The time complexity here is O(N*lg(N)) worst case, but for the
6449 common case of monotonically increasing enumerators, it is
6450 O(N), since the nature of the splay tree will keep the next
6451 element adjacent to the root at all times. */
6453 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6455 tree value = TREE_VALUE (chain);
6456 if (TREE_CODE (value) == CONST_DECL)
6457 value = DECL_INITIAL (value);
6458 node = splay_tree_lookup (cases, (splay_tree_key) value);
6459 if (node)
6461 /* Mark the CASE_LOW part of the case entry as seen. */
6462 tree label = (tree) node->value;
6463 CASE_LOW_SEEN (label) = 1;
6464 continue;
6467 /* Even though there wasn't an exact match, there might be a
6468 case range which includes the enumerator's value. */
6469 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6470 if (node && CASE_HIGH ((tree) node->value))
6472 tree label = (tree) node->value;
6473 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6474 if (cmp >= 0)
6476 /* If we match the upper bound exactly, mark the CASE_HIGH
6477 part of the case entry as seen. */
6478 if (cmp == 0)
6479 CASE_HIGH_SEEN (label) = 1;
6480 continue;
6484 /* We've now determined that this enumerated literal isn't
6485 handled by the case labels of the switch statement. */
6487 /* If the switch expression is a constant, we only really care
6488 about whether that constant is handled by the switch. */
6489 if (cond && tree_int_cst_compare (cond, value))
6490 continue;
6492 /* If there is a default_node, the only relevant option is
6493 Wswitch-enum. Otherwise, if both are enabled then we prefer
6494 to warn using -Wswitch because -Wswitch is enabled by -Wall
6495 while -Wswitch-enum is explicit. */
6496 warning_at (switch_location,
6497 (default_node || !warn_switch
6498 ? OPT_Wswitch_enum
6499 : OPT_Wswitch),
6500 "enumeration value %qE not handled in switch",
6501 TREE_PURPOSE (chain));
6504 /* Warn if there are case expressions that don't correspond to
6505 enumerators. This can occur since C and C++ don't enforce
6506 type-checking of assignments to enumeration variables.
6508 The time complexity here is now always O(N) worst case, since
6509 we should have marked both the lower bound and upper bound of
6510 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6511 above. This scan also resets those fields. */
6513 splay_tree_foreach (cases, match_case_to_enum, type);
6516 /* Finish an expression taking the address of LABEL (an
6517 IDENTIFIER_NODE). Returns an expression for the address.
6519 LOC is the location for the expression returned. */
6521 tree
6522 finish_label_address_expr (tree label, location_t loc)
6524 tree result;
6526 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6528 if (label == error_mark_node)
6529 return error_mark_node;
6531 label = lookup_label (label);
6532 if (label == NULL_TREE)
6533 result = null_pointer_node;
6534 else
6536 TREE_USED (label) = 1;
6537 result = build1 (ADDR_EXPR, ptr_type_node, label);
6538 /* The current function is not necessarily uninlinable.
6539 Computed gotos are incompatible with inlining, but the value
6540 here could be used only in a diagnostic, for example. */
6541 protected_set_expr_location (result, loc);
6544 return result;
6548 /* Given a boolean expression ARG, return a tree representing an increment
6549 or decrement (as indicated by CODE) of ARG. The front end must check for
6550 invalid cases (e.g., decrement in C++). */
6551 tree
6552 boolean_increment (enum tree_code code, tree arg)
6554 tree val;
6555 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6557 arg = stabilize_reference (arg);
6558 switch (code)
6560 case PREINCREMENT_EXPR:
6561 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6562 break;
6563 case POSTINCREMENT_EXPR:
6564 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6565 arg = save_expr (arg);
6566 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6567 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6568 break;
6569 case PREDECREMENT_EXPR:
6570 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6571 invert_truthvalue_loc (input_location, arg));
6572 break;
6573 case POSTDECREMENT_EXPR:
6574 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6575 invert_truthvalue_loc (input_location, arg));
6576 arg = save_expr (arg);
6577 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6578 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6579 break;
6580 default:
6581 gcc_unreachable ();
6583 TREE_SIDE_EFFECTS (val) = 1;
6584 return val;
6587 /* Built-in macros for stddef.h and stdint.h, that require macros
6588 defined in this file. */
6589 void
6590 c_stddef_cpp_builtins(void)
6592 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6593 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6594 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6595 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6596 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6597 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6598 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6599 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6600 if (SIG_ATOMIC_TYPE)
6601 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6602 if (INT8_TYPE)
6603 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6604 if (INT16_TYPE)
6605 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6606 if (INT32_TYPE)
6607 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6608 if (INT64_TYPE)
6609 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6610 if (UINT8_TYPE)
6611 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6612 if (UINT16_TYPE)
6613 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6614 if (UINT32_TYPE)
6615 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6616 if (UINT64_TYPE)
6617 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6618 if (INT_LEAST8_TYPE)
6619 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6620 if (INT_LEAST16_TYPE)
6621 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6622 if (INT_LEAST32_TYPE)
6623 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6624 if (INT_LEAST64_TYPE)
6625 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6626 if (UINT_LEAST8_TYPE)
6627 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6628 if (UINT_LEAST16_TYPE)
6629 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6630 if (UINT_LEAST32_TYPE)
6631 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6632 if (UINT_LEAST64_TYPE)
6633 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6634 if (INT_FAST8_TYPE)
6635 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6636 if (INT_FAST16_TYPE)
6637 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6638 if (INT_FAST32_TYPE)
6639 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6640 if (INT_FAST64_TYPE)
6641 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6642 if (UINT_FAST8_TYPE)
6643 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6644 if (UINT_FAST16_TYPE)
6645 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6646 if (UINT_FAST32_TYPE)
6647 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6648 if (UINT_FAST64_TYPE)
6649 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6650 if (INTPTR_TYPE)
6651 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6652 if (UINTPTR_TYPE)
6653 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6656 static void
6657 c_init_attributes (void)
6659 /* Fill in the built_in_attributes array. */
6660 #define DEF_ATTR_NULL_TREE(ENUM) \
6661 built_in_attributes[(int) ENUM] = NULL_TREE;
6662 #define DEF_ATTR_INT(ENUM, VALUE) \
6663 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6664 #define DEF_ATTR_STRING(ENUM, VALUE) \
6665 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6666 #define DEF_ATTR_IDENT(ENUM, STRING) \
6667 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6668 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6669 built_in_attributes[(int) ENUM] \
6670 = tree_cons (built_in_attributes[(int) PURPOSE], \
6671 built_in_attributes[(int) VALUE], \
6672 built_in_attributes[(int) CHAIN]);
6673 #include "builtin-attrs.def"
6674 #undef DEF_ATTR_NULL_TREE
6675 #undef DEF_ATTR_INT
6676 #undef DEF_ATTR_IDENT
6677 #undef DEF_ATTR_TREE_LIST
6680 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6681 identifier as an argument, so the front end shouldn't look it up. */
6683 bool
6684 attribute_takes_identifier_p (const_tree attr_id)
6686 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6687 if (spec == NULL)
6688 /* Unknown attribute that we'll end up ignoring, return true so we
6689 don't complain about an identifier argument. */
6690 return true;
6691 else if (!strcmp ("mode", spec->name)
6692 || !strcmp ("format", spec->name)
6693 || !strcmp ("cleanup", spec->name))
6694 return true;
6695 else
6696 return targetm.attribute_takes_identifier_p (attr_id);
6699 /* Attribute handlers common to C front ends. */
6701 /* Handle a "packed" attribute; arguments as in
6702 struct attribute_spec.handler. */
6704 static tree
6705 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6706 int flags, bool *no_add_attrs)
6708 if (TYPE_P (*node))
6710 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6711 *node = build_variant_type_copy (*node);
6712 TYPE_PACKED (*node) = 1;
6714 else if (TREE_CODE (*node) == FIELD_DECL)
6716 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6717 /* Still pack bitfields. */
6718 && ! DECL_INITIAL (*node))
6719 warning (OPT_Wattributes,
6720 "%qE attribute ignored for field of type %qT",
6721 name, TREE_TYPE (*node));
6722 else
6723 DECL_PACKED (*node) = 1;
6725 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6726 used for DECL_REGISTER. It wouldn't mean anything anyway.
6727 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6728 that changes what the typedef is typing. */
6729 else
6731 warning (OPT_Wattributes, "%qE attribute ignored", name);
6732 *no_add_attrs = true;
6735 return NULL_TREE;
6738 /* Handle a "nocommon" attribute; arguments as in
6739 struct attribute_spec.handler. */
6741 static tree
6742 handle_nocommon_attribute (tree *node, tree name,
6743 tree ARG_UNUSED (args),
6744 int ARG_UNUSED (flags), bool *no_add_attrs)
6746 if (TREE_CODE (*node) == VAR_DECL)
6747 DECL_COMMON (*node) = 0;
6748 else
6750 warning (OPT_Wattributes, "%qE attribute ignored", name);
6751 *no_add_attrs = true;
6754 return NULL_TREE;
6757 /* Handle a "common" attribute; arguments as in
6758 struct attribute_spec.handler. */
6760 static tree
6761 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6762 int ARG_UNUSED (flags), bool *no_add_attrs)
6764 if (TREE_CODE (*node) == VAR_DECL)
6765 DECL_COMMON (*node) = 1;
6766 else
6768 warning (OPT_Wattributes, "%qE attribute ignored", name);
6769 *no_add_attrs = true;
6772 return NULL_TREE;
6775 /* Handle a "noreturn" attribute; arguments as in
6776 struct attribute_spec.handler. */
6778 static tree
6779 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6780 int ARG_UNUSED (flags), bool *no_add_attrs)
6782 tree type = TREE_TYPE (*node);
6784 /* See FIXME comment in c_common_attribute_table. */
6785 if (TREE_CODE (*node) == FUNCTION_DECL
6786 || objc_method_decl (TREE_CODE (*node)))
6787 TREE_THIS_VOLATILE (*node) = 1;
6788 else if (TREE_CODE (type) == POINTER_TYPE
6789 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6790 TREE_TYPE (*node)
6791 = (build_qualified_type
6792 (build_pointer_type
6793 (build_type_variant (TREE_TYPE (type),
6794 TYPE_READONLY (TREE_TYPE (type)), 1)),
6795 TYPE_QUALS (type)));
6796 else
6798 warning (OPT_Wattributes, "%qE attribute ignored", name);
6799 *no_add_attrs = true;
6802 return NULL_TREE;
6805 /* Handle a "hot" and attribute; arguments as in
6806 struct attribute_spec.handler. */
6808 static tree
6809 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6810 int ARG_UNUSED (flags), bool *no_add_attrs)
6812 if (TREE_CODE (*node) == FUNCTION_DECL
6813 || TREE_CODE (*node) == LABEL_DECL)
6815 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6817 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6818 "with attribute %qs", name, "cold");
6819 *no_add_attrs = true;
6821 /* Most of the rest of the hot processing is done later with
6822 lookup_attribute. */
6824 else
6826 warning (OPT_Wattributes, "%qE attribute ignored", name);
6827 *no_add_attrs = true;
6830 return NULL_TREE;
6833 /* Handle a "cold" and attribute; arguments as in
6834 struct attribute_spec.handler. */
6836 static tree
6837 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6838 int ARG_UNUSED (flags), bool *no_add_attrs)
6840 if (TREE_CODE (*node) == FUNCTION_DECL
6841 || TREE_CODE (*node) == LABEL_DECL)
6843 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6845 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6846 "with attribute %qs", name, "hot");
6847 *no_add_attrs = true;
6849 /* Most of the rest of the cold processing is done later with
6850 lookup_attribute. */
6852 else
6854 warning (OPT_Wattributes, "%qE attribute ignored", name);
6855 *no_add_attrs = true;
6858 return NULL_TREE;
6861 /* Handle a "no_sanitize_address" attribute; arguments as in
6862 struct attribute_spec.handler. */
6864 static tree
6865 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6866 bool *no_add_attrs)
6868 if (TREE_CODE (*node) != FUNCTION_DECL)
6870 warning (OPT_Wattributes, "%qE attribute ignored", name);
6871 *no_add_attrs = true;
6874 return NULL_TREE;
6877 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6878 struct attribute_spec.handler. */
6880 static tree
6881 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6882 bool *no_add_attrs)
6884 if (TREE_CODE (*node) != FUNCTION_DECL)
6885 warning (OPT_Wattributes, "%qE attribute ignored", name);
6886 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6887 DECL_ATTRIBUTES (*node)
6888 = tree_cons (get_identifier ("no_sanitize_address"),
6889 NULL_TREE, DECL_ATTRIBUTES (*node));
6890 *no_add_attrs = true;
6891 return NULL_TREE;
6894 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6895 struct attribute_spec.handler. */
6897 static tree
6898 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6899 bool *no_add_attrs)
6901 if (TREE_CODE (*node) != FUNCTION_DECL)
6903 warning (OPT_Wattributes, "%qE attribute ignored", name);
6904 *no_add_attrs = true;
6907 return NULL_TREE;
6910 /* Handle a "stack_protect" attribute; arguments as in
6911 struct attribute_spec.handler. */
6912 static tree
6913 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6914 bool *no_add_attrs)
6916 if (TREE_CODE (*node) != FUNCTION_DECL)
6918 warning (OPT_Wattributes, "%qE attribute ignored", name);
6919 *no_add_attrs = true;
6921 else
6922 DECL_ATTRIBUTES (*node)
6923 = tree_cons (get_identifier ("stack_protect"),
6924 NULL_TREE, DECL_ATTRIBUTES (*node));
6926 return NULL_TREE;
6929 /* Handle a "noinline" attribute; arguments as in
6930 struct attribute_spec.handler. */
6932 static tree
6933 handle_noinline_attribute (tree *node, tree name,
6934 tree ARG_UNUSED (args),
6935 int ARG_UNUSED (flags), bool *no_add_attrs)
6937 if (TREE_CODE (*node) == FUNCTION_DECL)
6939 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6941 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6942 "with attribute %qs", name, "always_inline");
6943 *no_add_attrs = true;
6945 else
6946 DECL_UNINLINABLE (*node) = 1;
6948 else
6950 warning (OPT_Wattributes, "%qE attribute ignored", name);
6951 *no_add_attrs = true;
6954 return NULL_TREE;
6957 /* Handle a "noclone" attribute; arguments as in
6958 struct attribute_spec.handler. */
6960 static tree
6961 handle_noclone_attribute (tree *node, tree name,
6962 tree ARG_UNUSED (args),
6963 int ARG_UNUSED (flags), bool *no_add_attrs)
6965 if (TREE_CODE (*node) != FUNCTION_DECL)
6967 warning (OPT_Wattributes, "%qE attribute ignored", name);
6968 *no_add_attrs = true;
6971 return NULL_TREE;
6974 /* Handle a "no_icf" attribute; arguments as in
6975 struct attribute_spec.handler. */
6977 static tree
6978 handle_noicf_attribute (tree *node, tree name,
6979 tree ARG_UNUSED (args),
6980 int ARG_UNUSED (flags), bool *no_add_attrs)
6982 if (TREE_CODE (*node) != FUNCTION_DECL)
6984 warning (OPT_Wattributes, "%qE attribute ignored", name);
6985 *no_add_attrs = true;
6988 return NULL_TREE;
6992 /* Handle a "always_inline" attribute; arguments as in
6993 struct attribute_spec.handler. */
6995 static tree
6996 handle_always_inline_attribute (tree *node, tree name,
6997 tree ARG_UNUSED (args),
6998 int ARG_UNUSED (flags),
6999 bool *no_add_attrs)
7001 if (TREE_CODE (*node) == FUNCTION_DECL)
7003 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7005 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7006 "with %qs attribute", name, "noinline");
7007 *no_add_attrs = true;
7009 else
7010 /* Set the attribute and mark it for disregarding inline
7011 limits. */
7012 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
7014 else
7016 warning (OPT_Wattributes, "%qE attribute ignored", name);
7017 *no_add_attrs = true;
7020 return NULL_TREE;
7023 /* Handle a "gnu_inline" attribute; arguments as in
7024 struct attribute_spec.handler. */
7026 static tree
7027 handle_gnu_inline_attribute (tree *node, tree name,
7028 tree ARG_UNUSED (args),
7029 int ARG_UNUSED (flags),
7030 bool *no_add_attrs)
7032 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7034 /* Do nothing else, just set the attribute. We'll get at
7035 it later with lookup_attribute. */
7037 else
7039 warning (OPT_Wattributes, "%qE attribute ignored", name);
7040 *no_add_attrs = true;
7043 return NULL_TREE;
7046 /* Handle a "leaf" attribute; arguments as in
7047 struct attribute_spec.handler. */
7049 static tree
7050 handle_leaf_attribute (tree *node, tree name,
7051 tree ARG_UNUSED (args),
7052 int ARG_UNUSED (flags), bool *no_add_attrs)
7054 if (TREE_CODE (*node) != FUNCTION_DECL)
7056 warning (OPT_Wattributes, "%qE attribute ignored", name);
7057 *no_add_attrs = true;
7059 if (!TREE_PUBLIC (*node))
7061 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7062 *no_add_attrs = true;
7065 return NULL_TREE;
7068 /* Handle an "artificial" attribute; arguments as in
7069 struct attribute_spec.handler. */
7071 static tree
7072 handle_artificial_attribute (tree *node, tree name,
7073 tree ARG_UNUSED (args),
7074 int ARG_UNUSED (flags),
7075 bool *no_add_attrs)
7077 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7079 /* Do nothing else, just set the attribute. We'll get at
7080 it later with lookup_attribute. */
7082 else
7084 warning (OPT_Wattributes, "%qE attribute ignored", name);
7085 *no_add_attrs = true;
7088 return NULL_TREE;
7091 /* Handle a "flatten" attribute; arguments as in
7092 struct attribute_spec.handler. */
7094 static tree
7095 handle_flatten_attribute (tree *node, tree name,
7096 tree args ATTRIBUTE_UNUSED,
7097 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7099 if (TREE_CODE (*node) == FUNCTION_DECL)
7100 /* Do nothing else, just set the attribute. We'll get at
7101 it later with lookup_attribute. */
7103 else
7105 warning (OPT_Wattributes, "%qE attribute ignored", name);
7106 *no_add_attrs = true;
7109 return NULL_TREE;
7112 /* Handle a "warning" or "error" attribute; arguments as in
7113 struct attribute_spec.handler. */
7115 static tree
7116 handle_error_attribute (tree *node, tree name, tree args,
7117 int ARG_UNUSED (flags), bool *no_add_attrs)
7119 if (TREE_CODE (*node) == FUNCTION_DECL
7120 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7121 /* Do nothing else, just set the attribute. We'll get at
7122 it later with lookup_attribute. */
7124 else
7126 warning (OPT_Wattributes, "%qE attribute ignored", name);
7127 *no_add_attrs = true;
7130 return NULL_TREE;
7133 /* Handle a "used" attribute; arguments as in
7134 struct attribute_spec.handler. */
7136 static tree
7137 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7138 int ARG_UNUSED (flags), bool *no_add_attrs)
7140 tree node = *pnode;
7142 if (TREE_CODE (node) == FUNCTION_DECL
7143 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7144 || (TREE_CODE (node) == TYPE_DECL))
7146 TREE_USED (node) = 1;
7147 DECL_PRESERVE_P (node) = 1;
7148 if (TREE_CODE (node) == VAR_DECL)
7149 DECL_READ_P (node) = 1;
7151 else
7153 warning (OPT_Wattributes, "%qE attribute ignored", name);
7154 *no_add_attrs = true;
7157 return NULL_TREE;
7160 /* Handle a "unused" attribute; arguments as in
7161 struct attribute_spec.handler. */
7163 static tree
7164 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7165 int flags, bool *no_add_attrs)
7167 if (DECL_P (*node))
7169 tree decl = *node;
7171 if (TREE_CODE (decl) == PARM_DECL
7172 || TREE_CODE (decl) == VAR_DECL
7173 || TREE_CODE (decl) == FUNCTION_DECL
7174 || TREE_CODE (decl) == LABEL_DECL
7175 || TREE_CODE (decl) == TYPE_DECL)
7177 TREE_USED (decl) = 1;
7178 if (TREE_CODE (decl) == VAR_DECL
7179 || TREE_CODE (decl) == PARM_DECL)
7180 DECL_READ_P (decl) = 1;
7182 else
7184 warning (OPT_Wattributes, "%qE attribute ignored", name);
7185 *no_add_attrs = true;
7188 else
7190 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7191 *node = build_variant_type_copy (*node);
7192 TREE_USED (*node) = 1;
7195 return NULL_TREE;
7198 /* Handle a "externally_visible" attribute; arguments as in
7199 struct attribute_spec.handler. */
7201 static tree
7202 handle_externally_visible_attribute (tree *pnode, tree name,
7203 tree ARG_UNUSED (args),
7204 int ARG_UNUSED (flags),
7205 bool *no_add_attrs)
7207 tree node = *pnode;
7209 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7211 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7212 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7214 warning (OPT_Wattributes,
7215 "%qE attribute have effect only on public objects", name);
7216 *no_add_attrs = true;
7219 else
7221 warning (OPT_Wattributes, "%qE attribute ignored", name);
7222 *no_add_attrs = true;
7225 return NULL_TREE;
7228 /* Handle the "no_reorder" attribute. Arguments as in
7229 struct attribute_spec.handler. */
7231 static tree
7232 handle_no_reorder_attribute (tree *pnode,
7233 tree name,
7234 tree,
7235 int,
7236 bool *no_add_attrs)
7238 tree node = *pnode;
7240 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7241 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7243 warning (OPT_Wattributes,
7244 "%qE attribute only affects top level objects",
7245 name);
7246 *no_add_attrs = true;
7249 return NULL_TREE;
7252 /* Handle a "const" attribute; arguments as in
7253 struct attribute_spec.handler. */
7255 static tree
7256 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7257 int ARG_UNUSED (flags), bool *no_add_attrs)
7259 tree type = TREE_TYPE (*node);
7261 /* See FIXME comment on noreturn in c_common_attribute_table. */
7262 if (TREE_CODE (*node) == FUNCTION_DECL)
7263 TREE_READONLY (*node) = 1;
7264 else if (TREE_CODE (type) == POINTER_TYPE
7265 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7266 TREE_TYPE (*node)
7267 = (build_qualified_type
7268 (build_pointer_type
7269 (build_type_variant (TREE_TYPE (type), 1,
7270 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7271 TYPE_QUALS (type)));
7272 else
7274 warning (OPT_Wattributes, "%qE attribute ignored", name);
7275 *no_add_attrs = true;
7278 return NULL_TREE;
7281 /* Handle a "transparent_union" attribute; arguments as in
7282 struct attribute_spec.handler. */
7284 static tree
7285 handle_transparent_union_attribute (tree *node, tree name,
7286 tree ARG_UNUSED (args), int flags,
7287 bool *no_add_attrs)
7289 tree type;
7291 *no_add_attrs = true;
7294 if (TREE_CODE (*node) == TYPE_DECL
7295 && ! (flags & ATTR_FLAG_CXX11))
7296 node = &TREE_TYPE (*node);
7297 type = *node;
7299 if (TREE_CODE (type) == UNION_TYPE)
7301 /* Make sure that the first field will work for a transparent union.
7302 If the type isn't complete yet, leave the check to the code in
7303 finish_struct. */
7304 if (TYPE_SIZE (type))
7306 tree first = first_field (type);
7307 if (first == NULL_TREE
7308 || DECL_ARTIFICIAL (first)
7309 || TYPE_MODE (type) != DECL_MODE (first))
7310 goto ignored;
7313 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7315 /* If the type isn't complete yet, setting the flag
7316 on a variant wouldn't ever be checked. */
7317 if (!TYPE_SIZE (type))
7318 goto ignored;
7320 /* build_duplicate_type doesn't work for C++. */
7321 if (c_dialect_cxx ())
7322 goto ignored;
7324 /* A type variant isn't good enough, since we don't a cast
7325 to such a type removed as a no-op. */
7326 *node = type = build_duplicate_type (type);
7329 TYPE_TRANSPARENT_AGGR (type) = 1;
7330 return NULL_TREE;
7333 ignored:
7334 warning (OPT_Wattributes, "%qE attribute ignored", name);
7335 return NULL_TREE;
7338 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7339 get the requested priority for a constructor or destructor,
7340 possibly issuing diagnostics for invalid or reserved
7341 priorities. */
7343 static priority_type
7344 get_priority (tree args, bool is_destructor)
7346 HOST_WIDE_INT pri;
7347 tree arg;
7349 if (!args)
7350 return DEFAULT_INIT_PRIORITY;
7352 if (!SUPPORTS_INIT_PRIORITY)
7354 if (is_destructor)
7355 error ("destructor priorities are not supported");
7356 else
7357 error ("constructor priorities are not supported");
7358 return DEFAULT_INIT_PRIORITY;
7361 arg = TREE_VALUE (args);
7362 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7363 goto invalid;
7364 if (arg == error_mark_node)
7365 return DEFAULT_INIT_PRIORITY;
7366 arg = default_conversion (arg);
7367 if (!tree_fits_shwi_p (arg)
7368 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7369 goto invalid;
7371 pri = tree_to_shwi (arg);
7372 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7373 goto invalid;
7375 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7377 if (is_destructor)
7378 warning (0,
7379 "destructor priorities from 0 to %d are reserved "
7380 "for the implementation",
7381 MAX_RESERVED_INIT_PRIORITY);
7382 else
7383 warning (0,
7384 "constructor priorities from 0 to %d are reserved "
7385 "for the implementation",
7386 MAX_RESERVED_INIT_PRIORITY);
7388 return pri;
7390 invalid:
7391 if (is_destructor)
7392 error ("destructor priorities must be integers from 0 to %d inclusive",
7393 MAX_INIT_PRIORITY);
7394 else
7395 error ("constructor priorities must be integers from 0 to %d inclusive",
7396 MAX_INIT_PRIORITY);
7397 return DEFAULT_INIT_PRIORITY;
7400 /* Handle a "constructor" attribute; arguments as in
7401 struct attribute_spec.handler. */
7403 static tree
7404 handle_constructor_attribute (tree *node, tree name, tree args,
7405 int ARG_UNUSED (flags),
7406 bool *no_add_attrs)
7408 tree decl = *node;
7409 tree type = TREE_TYPE (decl);
7411 if (TREE_CODE (decl) == FUNCTION_DECL
7412 && TREE_CODE (type) == FUNCTION_TYPE
7413 && decl_function_context (decl) == 0)
7415 priority_type priority;
7416 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7417 priority = get_priority (args, /*is_destructor=*/false);
7418 SET_DECL_INIT_PRIORITY (decl, priority);
7419 TREE_USED (decl) = 1;
7421 else
7423 warning (OPT_Wattributes, "%qE attribute ignored", name);
7424 *no_add_attrs = true;
7427 return NULL_TREE;
7430 /* Handle a "destructor" attribute; arguments as in
7431 struct attribute_spec.handler. */
7433 static tree
7434 handle_destructor_attribute (tree *node, tree name, tree args,
7435 int ARG_UNUSED (flags),
7436 bool *no_add_attrs)
7438 tree decl = *node;
7439 tree type = TREE_TYPE (decl);
7441 if (TREE_CODE (decl) == FUNCTION_DECL
7442 && TREE_CODE (type) == FUNCTION_TYPE
7443 && decl_function_context (decl) == 0)
7445 priority_type priority;
7446 DECL_STATIC_DESTRUCTOR (decl) = 1;
7447 priority = get_priority (args, /*is_destructor=*/true);
7448 SET_DECL_FINI_PRIORITY (decl, priority);
7449 TREE_USED (decl) = 1;
7451 else
7453 warning (OPT_Wattributes, "%qE attribute ignored", name);
7454 *no_add_attrs = true;
7457 return NULL_TREE;
7460 /* Nonzero if the mode is a valid vector mode for this architecture.
7461 This returns nonzero even if there is no hardware support for the
7462 vector mode, but we can emulate with narrower modes. */
7464 static int
7465 vector_mode_valid_p (machine_mode mode)
7467 enum mode_class mclass = GET_MODE_CLASS (mode);
7468 machine_mode innermode;
7470 /* Doh! What's going on? */
7471 if (mclass != MODE_VECTOR_INT
7472 && mclass != MODE_VECTOR_FLOAT
7473 && mclass != MODE_VECTOR_FRACT
7474 && mclass != MODE_VECTOR_UFRACT
7475 && mclass != MODE_VECTOR_ACCUM
7476 && mclass != MODE_VECTOR_UACCUM)
7477 return 0;
7479 /* Hardware support. Woo hoo! */
7480 if (targetm.vector_mode_supported_p (mode))
7481 return 1;
7483 innermode = GET_MODE_INNER (mode);
7485 /* We should probably return 1 if requesting V4DI and we have no DI,
7486 but we have V2DI, but this is probably very unlikely. */
7488 /* If we have support for the inner mode, we can safely emulate it.
7489 We may not have V2DI, but me can emulate with a pair of DIs. */
7490 return targetm.scalar_mode_supported_p (innermode);
7494 /* Handle a "mode" attribute; arguments as in
7495 struct attribute_spec.handler. */
7497 static tree
7498 handle_mode_attribute (tree *node, tree name, tree args,
7499 int ARG_UNUSED (flags), bool *no_add_attrs)
7501 tree type = *node;
7502 tree ident = TREE_VALUE (args);
7504 *no_add_attrs = true;
7506 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7507 warning (OPT_Wattributes, "%qE attribute ignored", name);
7508 else
7510 int j;
7511 const char *p = IDENTIFIER_POINTER (ident);
7512 int len = strlen (p);
7513 machine_mode mode = VOIDmode;
7514 tree typefm;
7515 bool valid_mode;
7517 if (len > 4 && p[0] == '_' && p[1] == '_'
7518 && p[len - 1] == '_' && p[len - 2] == '_')
7520 char *newp = (char *) alloca (len - 1);
7522 strcpy (newp, &p[2]);
7523 newp[len - 4] = '\0';
7524 p = newp;
7527 /* Change this type to have a type with the specified mode.
7528 First check for the special modes. */
7529 if (!strcmp (p, "byte"))
7530 mode = byte_mode;
7531 else if (!strcmp (p, "word"))
7532 mode = word_mode;
7533 else if (!strcmp (p, "pointer"))
7534 mode = ptr_mode;
7535 else if (!strcmp (p, "libgcc_cmp_return"))
7536 mode = targetm.libgcc_cmp_return_mode ();
7537 else if (!strcmp (p, "libgcc_shift_count"))
7538 mode = targetm.libgcc_shift_count_mode ();
7539 else if (!strcmp (p, "unwind_word"))
7540 mode = targetm.unwind_word_mode ();
7541 else
7542 for (j = 0; j < NUM_MACHINE_MODES; j++)
7543 if (!strcmp (p, GET_MODE_NAME (j)))
7545 mode = (machine_mode) j;
7546 break;
7549 if (mode == VOIDmode)
7551 error ("unknown machine mode %qE", ident);
7552 return NULL_TREE;
7555 valid_mode = false;
7556 switch (GET_MODE_CLASS (mode))
7558 case MODE_INT:
7559 case MODE_PARTIAL_INT:
7560 case MODE_FLOAT:
7561 case MODE_DECIMAL_FLOAT:
7562 case MODE_FRACT:
7563 case MODE_UFRACT:
7564 case MODE_ACCUM:
7565 case MODE_UACCUM:
7566 valid_mode = targetm.scalar_mode_supported_p (mode);
7567 break;
7569 case MODE_COMPLEX_INT:
7570 case MODE_COMPLEX_FLOAT:
7571 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7572 break;
7574 case MODE_VECTOR_INT:
7575 case MODE_VECTOR_FLOAT:
7576 case MODE_VECTOR_FRACT:
7577 case MODE_VECTOR_UFRACT:
7578 case MODE_VECTOR_ACCUM:
7579 case MODE_VECTOR_UACCUM:
7580 warning (OPT_Wattributes, "specifying vector types with "
7581 "__attribute__ ((mode)) is deprecated");
7582 warning (OPT_Wattributes,
7583 "use __attribute__ ((vector_size)) instead");
7584 valid_mode = vector_mode_valid_p (mode);
7585 break;
7587 default:
7588 break;
7590 if (!valid_mode)
7592 error ("unable to emulate %qs", p);
7593 return NULL_TREE;
7596 if (POINTER_TYPE_P (type))
7598 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7599 tree (*fn)(tree, machine_mode, bool);
7601 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7603 error ("invalid pointer mode %qs", p);
7604 return NULL_TREE;
7607 if (TREE_CODE (type) == POINTER_TYPE)
7608 fn = build_pointer_type_for_mode;
7609 else
7610 fn = build_reference_type_for_mode;
7611 typefm = fn (TREE_TYPE (type), mode, false);
7613 else
7615 /* For fixed-point modes, we need to test if the signness of type
7616 and the machine mode are consistent. */
7617 if (ALL_FIXED_POINT_MODE_P (mode)
7618 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7620 error ("signedness of type and machine mode %qs don%'t match", p);
7621 return NULL_TREE;
7623 /* For fixed-point modes, we need to pass saturating info. */
7624 typefm = lang_hooks.types.type_for_mode (mode,
7625 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7626 : TYPE_UNSIGNED (type));
7629 if (typefm == NULL_TREE)
7631 error ("no data type for mode %qs", p);
7632 return NULL_TREE;
7634 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7636 /* For enumeral types, copy the precision from the integer
7637 type returned above. If not an INTEGER_TYPE, we can't use
7638 this mode for this type. */
7639 if (TREE_CODE (typefm) != INTEGER_TYPE)
7641 error ("cannot use mode %qs for enumeral types", p);
7642 return NULL_TREE;
7645 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7647 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7648 typefm = type;
7650 else
7652 /* We cannot build a type variant, as there's code that assumes
7653 that TYPE_MAIN_VARIANT has the same mode. This includes the
7654 debug generators. Instead, create a subrange type. This
7655 results in all of the enumeral values being emitted only once
7656 in the original, and the subtype gets them by reference. */
7657 if (TYPE_UNSIGNED (type))
7658 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7659 else
7660 typefm = make_signed_type (TYPE_PRECISION (typefm));
7661 TREE_TYPE (typefm) = type;
7664 else if (VECTOR_MODE_P (mode)
7665 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7666 : TREE_CODE (type) != TREE_CODE (typefm))
7668 error ("mode %qs applied to inappropriate type", p);
7669 return NULL_TREE;
7672 *node = typefm;
7675 return NULL_TREE;
7678 /* Handle a "section" attribute; arguments as in
7679 struct attribute_spec.handler. */
7681 static tree
7682 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7683 int ARG_UNUSED (flags), bool *no_add_attrs)
7685 tree decl = *node;
7687 if (targetm_common.have_named_sections)
7689 user_defined_section_attribute = true;
7691 if ((TREE_CODE (decl) == FUNCTION_DECL
7692 || TREE_CODE (decl) == VAR_DECL)
7693 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7695 if (TREE_CODE (decl) == VAR_DECL
7696 && current_function_decl != NULL_TREE
7697 && !TREE_STATIC (decl))
7699 error_at (DECL_SOURCE_LOCATION (decl),
7700 "section attribute cannot be specified for "
7701 "local variables");
7702 *no_add_attrs = true;
7705 /* The decl may have already been given a section attribute
7706 from a previous declaration. Ensure they match. */
7707 else if (DECL_SECTION_NAME (decl) != NULL
7708 && strcmp (DECL_SECTION_NAME (decl),
7709 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7711 error ("section of %q+D conflicts with previous declaration",
7712 *node);
7713 *no_add_attrs = true;
7715 else if (TREE_CODE (decl) == VAR_DECL
7716 && !targetm.have_tls && targetm.emutls.tmpl_section
7717 && DECL_THREAD_LOCAL_P (decl))
7719 error ("section of %q+D cannot be overridden", *node);
7720 *no_add_attrs = true;
7722 else
7723 set_decl_section_name (decl,
7724 TREE_STRING_POINTER (TREE_VALUE (args)));
7726 else
7728 error ("section attribute not allowed for %q+D", *node);
7729 *no_add_attrs = true;
7732 else
7734 error_at (DECL_SOURCE_LOCATION (*node),
7735 "section attributes are not supported for this target");
7736 *no_add_attrs = true;
7739 return NULL_TREE;
7742 /* Check whether ALIGN is a valid user-specified alignment. If so,
7743 return its base-2 log; if not, output an error and return -1. If
7744 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7745 no error. */
7747 check_user_alignment (const_tree align, bool allow_zero)
7749 int i;
7751 if (error_operand_p (align))
7752 return -1;
7753 if (TREE_CODE (align) != INTEGER_CST
7754 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7756 error ("requested alignment is not an integer constant");
7757 return -1;
7759 else if (allow_zero && integer_zerop (align))
7760 return -1;
7761 else if (tree_int_cst_sgn (align) == -1
7762 || (i = tree_log2 (align)) == -1)
7764 error ("requested alignment is not a positive power of 2");
7765 return -1;
7767 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7769 error ("requested alignment is too large");
7770 return -1;
7772 return i;
7776 If in c++-11, check if the c++-11 alignment constraint with respect
7777 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7778 c++-11 mode, does nothing.
7780 [dcl.align]2/ says:
7782 [* if the constant expression evaluates to a fundamental alignment,
7783 the alignment requirement of the declared entity shall be the
7784 specified fundamental alignment.
7786 * if the constant expression evaluates to an extended alignment
7787 and the implementation supports that alignment in the context
7788 of the declaration, the alignment of the declared entity shall
7789 be that alignment
7791 * if the constant expression evaluates to an extended alignment
7792 and the implementation does not support that alignment in the
7793 context of the declaration, the program is ill-formed]. */
7795 static bool
7796 check_cxx_fundamental_alignment_constraints (tree node,
7797 unsigned align_log,
7798 int flags)
7800 bool alignment_too_large_p = false;
7801 unsigned requested_alignment = 1U << align_log;
7802 unsigned max_align = 0;
7804 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7805 || (node == NULL_TREE || node == error_mark_node))
7806 return true;
7808 if (cxx_fundamental_alignment_p (requested_alignment))
7809 return true;
7811 if (DECL_P (node))
7813 if (TREE_STATIC (node))
7815 /* For file scope variables and static members, the target
7816 supports alignments that are at most
7817 MAX_OFILE_ALIGNMENT. */
7818 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7819 alignment_too_large_p = true;
7821 else
7823 #ifdef BIGGEST_FIELD_ALIGNMENT
7824 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7825 #else
7826 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7827 #endif
7828 /* For non-static members, the target supports either
7829 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7830 if it is defined or BIGGEST_ALIGNMENT. */
7831 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7832 if (TREE_CODE (node) == FIELD_DECL
7833 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7834 alignment_too_large_p = true;
7835 #undef MAX_TARGET_FIELD_ALIGNMENT
7836 /* For stack variables, the target supports at most
7837 MAX_STACK_ALIGNMENT. */
7838 else if (decl_function_context (node) != NULL
7839 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7840 alignment_too_large_p = true;
7843 else if (TYPE_P (node))
7845 /* Let's be liberal for types. */
7846 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7847 alignment_too_large_p = true;
7850 if (alignment_too_large_p)
7851 pedwarn (input_location, OPT_Wattributes,
7852 "requested alignment %d is larger than %d",
7853 requested_alignment, max_align);
7855 return !alignment_too_large_p;
7858 /* Handle a "aligned" attribute; arguments as in
7859 struct attribute_spec.handler. */
7861 static tree
7862 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7863 int flags, bool *no_add_attrs)
7865 tree decl = NULL_TREE;
7866 tree *type = NULL;
7867 int is_type = 0;
7868 tree align_expr;
7869 int i;
7871 if (args)
7873 align_expr = TREE_VALUE (args);
7874 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7875 && TREE_CODE (align_expr) != FUNCTION_DECL)
7876 align_expr = default_conversion (align_expr);
7878 else
7879 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7881 if (DECL_P (*node))
7883 decl = *node;
7884 type = &TREE_TYPE (decl);
7885 is_type = TREE_CODE (*node) == TYPE_DECL;
7887 else if (TYPE_P (*node))
7888 type = node, is_type = 1;
7890 if ((i = check_user_alignment (align_expr, false)) == -1
7891 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7892 *no_add_attrs = true;
7893 else if (is_type)
7895 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7896 /* OK, modify the type in place. */;
7897 /* If we have a TYPE_DECL, then copy the type, so that we
7898 don't accidentally modify a builtin type. See pushdecl. */
7899 else if (decl && TREE_TYPE (decl) != error_mark_node
7900 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7902 tree tt = TREE_TYPE (decl);
7903 *type = build_variant_type_copy (*type);
7904 DECL_ORIGINAL_TYPE (decl) = tt;
7905 TYPE_NAME (*type) = decl;
7906 TREE_USED (*type) = TREE_USED (decl);
7907 TREE_TYPE (decl) = *type;
7909 else
7910 *type = build_variant_type_copy (*type);
7912 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7913 TYPE_USER_ALIGN (*type) = 1;
7915 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7916 && TREE_CODE (decl) != FIELD_DECL)
7918 error ("alignment may not be specified for %q+D", decl);
7919 *no_add_attrs = true;
7921 else if (DECL_USER_ALIGN (decl)
7922 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7923 /* C++-11 [dcl.align/4]:
7925 When multiple alignment-specifiers are specified for an
7926 entity, the alignment requirement shall be set to the
7927 strictest specified alignment.
7929 This formally comes from the c++11 specification but we are
7930 doing it for the GNU attribute syntax as well. */
7931 *no_add_attrs = true;
7932 else if (TREE_CODE (decl) == FUNCTION_DECL
7933 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7935 if (DECL_USER_ALIGN (decl))
7936 error ("alignment for %q+D was previously specified as %d "
7937 "and may not be decreased", decl,
7938 DECL_ALIGN (decl) / BITS_PER_UNIT);
7939 else
7940 error ("alignment for %q+D must be at least %d", decl,
7941 DECL_ALIGN (decl) / BITS_PER_UNIT);
7942 *no_add_attrs = true;
7944 else
7946 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7947 DECL_USER_ALIGN (decl) = 1;
7950 return NULL_TREE;
7953 /* Handle a "weak" attribute; arguments as in
7954 struct attribute_spec.handler. */
7956 static tree
7957 handle_weak_attribute (tree *node, tree name,
7958 tree ARG_UNUSED (args),
7959 int ARG_UNUSED (flags),
7960 bool * ARG_UNUSED (no_add_attrs))
7962 if (TREE_CODE (*node) == FUNCTION_DECL
7963 && DECL_DECLARED_INLINE_P (*node))
7965 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7966 *no_add_attrs = true;
7968 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7970 error ("indirect function %q+D cannot be declared weak", *node);
7971 *no_add_attrs = true;
7972 return NULL_TREE;
7974 else if (TREE_CODE (*node) == FUNCTION_DECL
7975 || TREE_CODE (*node) == VAR_DECL)
7977 struct symtab_node *n = symtab_node::get (*node);
7978 if (n && n->refuse_visibility_changes)
7979 error ("%+D declared weak after being used", *node);
7980 declare_weak (*node);
7982 else
7983 warning (OPT_Wattributes, "%qE attribute ignored", name);
7985 return NULL_TREE;
7988 /* Handle an "alias" or "ifunc" attribute; arguments as in
7989 struct attribute_spec.handler, except that IS_ALIAS tells us
7990 whether this is an alias as opposed to ifunc attribute. */
7992 static tree
7993 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7994 bool *no_add_attrs)
7996 tree decl = *node;
7998 if (TREE_CODE (decl) != FUNCTION_DECL
7999 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
8001 warning (OPT_Wattributes, "%qE attribute ignored", name);
8002 *no_add_attrs = true;
8004 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
8005 || (TREE_CODE (decl) != FUNCTION_DECL
8006 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8007 /* A static variable declaration is always a tentative definition,
8008 but the alias is a non-tentative definition which overrides. */
8009 || (TREE_CODE (decl) != FUNCTION_DECL
8010 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
8012 error ("%q+D defined both normally and as %qE attribute", decl, name);
8013 *no_add_attrs = true;
8014 return NULL_TREE;
8016 else if (!is_alias
8017 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8018 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8020 error ("weak %q+D cannot be defined %qE", decl, name);
8021 *no_add_attrs = true;
8022 return NULL_TREE;
8025 /* Note that the very first time we process a nested declaration,
8026 decl_function_context will not be set. Indeed, *would* never
8027 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8028 we do below. After such frobbery, pushdecl would set the context.
8029 In any case, this is never what we want. */
8030 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
8032 tree id;
8034 id = TREE_VALUE (args);
8035 if (TREE_CODE (id) != STRING_CST)
8037 error ("attribute %qE argument not a string", name);
8038 *no_add_attrs = true;
8039 return NULL_TREE;
8041 id = get_identifier (TREE_STRING_POINTER (id));
8042 /* This counts as a use of the object pointed to. */
8043 TREE_USED (id) = 1;
8045 if (TREE_CODE (decl) == FUNCTION_DECL)
8046 DECL_INITIAL (decl) = error_mark_node;
8047 else
8048 TREE_STATIC (decl) = 1;
8050 if (!is_alias)
8051 /* ifuncs are also aliases, so set that attribute too. */
8052 DECL_ATTRIBUTES (decl)
8053 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8055 else
8057 warning (OPT_Wattributes, "%qE attribute ignored", name);
8058 *no_add_attrs = true;
8061 if (decl_in_symtab_p (*node))
8063 struct symtab_node *n = symtab_node::get (decl);
8064 if (n && n->refuse_visibility_changes)
8066 if (is_alias)
8067 error ("%+D declared alias after being used", decl);
8068 else
8069 error ("%+D declared ifunc after being used", decl);
8074 return NULL_TREE;
8077 /* Handle an "alias" or "ifunc" attribute; arguments as in
8078 struct attribute_spec.handler. */
8080 static tree
8081 handle_ifunc_attribute (tree *node, tree name, tree args,
8082 int ARG_UNUSED (flags), bool *no_add_attrs)
8084 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8087 /* Handle an "alias" or "ifunc" attribute; arguments as in
8088 struct attribute_spec.handler. */
8090 static tree
8091 handle_alias_attribute (tree *node, tree name, tree args,
8092 int ARG_UNUSED (flags), bool *no_add_attrs)
8094 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8097 /* Handle a "weakref" attribute; arguments as in struct
8098 attribute_spec.handler. */
8100 static tree
8101 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8102 int flags, bool *no_add_attrs)
8104 tree attr = NULL_TREE;
8106 /* We must ignore the attribute when it is associated with
8107 local-scoped decls, since attribute alias is ignored and many
8108 such symbols do not even have a DECL_WEAK field. */
8109 if (decl_function_context (*node)
8110 || current_function_decl
8111 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
8113 warning (OPT_Wattributes, "%qE attribute ignored", name);
8114 *no_add_attrs = true;
8115 return NULL_TREE;
8118 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8120 error ("indirect function %q+D cannot be declared weakref", *node);
8121 *no_add_attrs = true;
8122 return NULL_TREE;
8125 /* The idea here is that `weakref("name")' mutates into `weakref,
8126 alias("name")', and weakref without arguments, in turn,
8127 implicitly adds weak. */
8129 if (args)
8131 attr = tree_cons (get_identifier ("alias"), args, attr);
8132 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8134 *no_add_attrs = true;
8136 decl_attributes (node, attr, flags);
8138 else
8140 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8141 error_at (DECL_SOURCE_LOCATION (*node),
8142 "weakref attribute must appear before alias attribute");
8144 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8145 and that isn't supported; and because it wants to add it to
8146 the list of weak decls, which isn't helpful. */
8147 DECL_WEAK (*node) = 1;
8150 if (decl_in_symtab_p (*node))
8152 struct symtab_node *n = symtab_node::get (*node);
8153 if (n && n->refuse_visibility_changes)
8154 error ("%+D declared weakref after being used", *node);
8157 return NULL_TREE;
8160 /* Handle an "visibility" attribute; arguments as in
8161 struct attribute_spec.handler. */
8163 static tree
8164 handle_visibility_attribute (tree *node, tree name, tree args,
8165 int ARG_UNUSED (flags),
8166 bool *ARG_UNUSED (no_add_attrs))
8168 tree decl = *node;
8169 tree id = TREE_VALUE (args);
8170 enum symbol_visibility vis;
8172 if (TYPE_P (*node))
8174 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8175 /* OK */;
8176 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8178 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8179 name);
8180 return NULL_TREE;
8182 else if (TYPE_FIELDS (*node))
8184 error ("%qE attribute ignored because %qT is already defined",
8185 name, *node);
8186 return NULL_TREE;
8189 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8191 warning (OPT_Wattributes, "%qE attribute ignored", name);
8192 return NULL_TREE;
8195 if (TREE_CODE (id) != STRING_CST)
8197 error ("visibility argument not a string");
8198 return NULL_TREE;
8201 /* If this is a type, set the visibility on the type decl. */
8202 if (TYPE_P (decl))
8204 decl = TYPE_NAME (decl);
8205 if (!decl)
8206 return NULL_TREE;
8207 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8209 warning (OPT_Wattributes, "%qE attribute ignored on types",
8210 name);
8211 return NULL_TREE;
8215 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8216 vis = VISIBILITY_DEFAULT;
8217 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8218 vis = VISIBILITY_INTERNAL;
8219 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8220 vis = VISIBILITY_HIDDEN;
8221 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8222 vis = VISIBILITY_PROTECTED;
8223 else
8225 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8226 vis = VISIBILITY_DEFAULT;
8229 if (DECL_VISIBILITY_SPECIFIED (decl)
8230 && vis != DECL_VISIBILITY (decl))
8232 tree attributes = (TYPE_P (*node)
8233 ? TYPE_ATTRIBUTES (*node)
8234 : DECL_ATTRIBUTES (decl));
8235 if (lookup_attribute ("visibility", attributes))
8236 error ("%qD redeclared with different visibility", decl);
8237 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8238 && lookup_attribute ("dllimport", attributes))
8239 error ("%qD was declared %qs which implies default visibility",
8240 decl, "dllimport");
8241 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8242 && lookup_attribute ("dllexport", attributes))
8243 error ("%qD was declared %qs which implies default visibility",
8244 decl, "dllexport");
8247 DECL_VISIBILITY (decl) = vis;
8248 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8250 /* Go ahead and attach the attribute to the node as well. This is needed
8251 so we can determine whether we have VISIBILITY_DEFAULT because the
8252 visibility was not specified, or because it was explicitly overridden
8253 from the containing scope. */
8255 return NULL_TREE;
8258 /* Determine the ELF symbol visibility for DECL, which is either a
8259 variable or a function. It is an error to use this function if a
8260 definition of DECL is not available in this translation unit.
8261 Returns true if the final visibility has been determined by this
8262 function; false if the caller is free to make additional
8263 modifications. */
8265 bool
8266 c_determine_visibility (tree decl)
8268 gcc_assert (TREE_CODE (decl) == VAR_DECL
8269 || TREE_CODE (decl) == FUNCTION_DECL);
8271 /* If the user explicitly specified the visibility with an
8272 attribute, honor that. DECL_VISIBILITY will have been set during
8273 the processing of the attribute. We check for an explicit
8274 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8275 to distinguish the use of an attribute from the use of a "#pragma
8276 GCC visibility push(...)"; in the latter case we still want other
8277 considerations to be able to overrule the #pragma. */
8278 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8279 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8280 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8281 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8282 return true;
8284 /* Set default visibility to whatever the user supplied with
8285 visibility_specified depending on #pragma GCC visibility. */
8286 if (!DECL_VISIBILITY_SPECIFIED (decl))
8288 if (visibility_options.inpragma
8289 || DECL_VISIBILITY (decl) != default_visibility)
8291 DECL_VISIBILITY (decl) = default_visibility;
8292 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8293 /* If visibility changed and DECL already has DECL_RTL, ensure
8294 symbol flags are updated. */
8295 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8296 || TREE_CODE (decl) == FUNCTION_DECL)
8297 && DECL_RTL_SET_P (decl))
8298 make_decl_rtl (decl);
8301 return false;
8304 /* Handle an "tls_model" attribute; arguments as in
8305 struct attribute_spec.handler. */
8307 static tree
8308 handle_tls_model_attribute (tree *node, tree name, tree args,
8309 int ARG_UNUSED (flags), bool *no_add_attrs)
8311 tree id;
8312 tree decl = *node;
8313 enum tls_model kind;
8315 *no_add_attrs = true;
8317 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8319 warning (OPT_Wattributes, "%qE attribute ignored", name);
8320 return NULL_TREE;
8323 kind = DECL_TLS_MODEL (decl);
8324 id = TREE_VALUE (args);
8325 if (TREE_CODE (id) != STRING_CST)
8327 error ("tls_model argument not a string");
8328 return NULL_TREE;
8331 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8332 kind = TLS_MODEL_LOCAL_EXEC;
8333 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8334 kind = TLS_MODEL_INITIAL_EXEC;
8335 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8336 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8337 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8338 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8339 else
8340 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8342 set_decl_tls_model (decl, kind);
8343 return NULL_TREE;
8346 /* Handle a "no_instrument_function" attribute; arguments as in
8347 struct attribute_spec.handler. */
8349 static tree
8350 handle_no_instrument_function_attribute (tree *node, tree name,
8351 tree ARG_UNUSED (args),
8352 int ARG_UNUSED (flags),
8353 bool *no_add_attrs)
8355 tree decl = *node;
8357 if (TREE_CODE (decl) != FUNCTION_DECL)
8359 error_at (DECL_SOURCE_LOCATION (decl),
8360 "%qE attribute applies only to functions", name);
8361 *no_add_attrs = true;
8363 else
8364 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8366 return NULL_TREE;
8369 /* Handle a "malloc" attribute; arguments as in
8370 struct attribute_spec.handler. */
8372 static tree
8373 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8374 int ARG_UNUSED (flags), bool *no_add_attrs)
8376 if (TREE_CODE (*node) == FUNCTION_DECL
8377 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8378 DECL_IS_MALLOC (*node) = 1;
8379 else
8381 warning (OPT_Wattributes, "%qE attribute ignored", name);
8382 *no_add_attrs = true;
8385 return NULL_TREE;
8388 /* Handle a "alloc_size" attribute; arguments as in
8389 struct attribute_spec.handler. */
8391 static tree
8392 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8393 int ARG_UNUSED (flags), bool *no_add_attrs)
8395 unsigned arg_count = type_num_arguments (*node);
8396 for (; args; args = TREE_CHAIN (args))
8398 tree position = TREE_VALUE (args);
8399 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8400 && TREE_CODE (position) != FUNCTION_DECL)
8401 position = default_conversion (position);
8403 if (!tree_fits_uhwi_p (position)
8404 || !arg_count
8405 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8407 warning (OPT_Wattributes,
8408 "alloc_size parameter outside range");
8409 *no_add_attrs = true;
8410 return NULL_TREE;
8413 return NULL_TREE;
8416 /* Handle a "alloc_align" attribute; arguments as in
8417 struct attribute_spec.handler. */
8419 static tree
8420 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8421 bool *no_add_attrs)
8423 unsigned arg_count = type_num_arguments (*node);
8424 tree position = TREE_VALUE (args);
8425 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8426 position = default_conversion (position);
8428 if (!tree_fits_uhwi_p (position)
8429 || !arg_count
8430 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8432 warning (OPT_Wattributes,
8433 "alloc_align parameter outside range");
8434 *no_add_attrs = true;
8435 return NULL_TREE;
8437 return NULL_TREE;
8440 /* Handle a "assume_aligned" attribute; arguments as in
8441 struct attribute_spec.handler. */
8443 static tree
8444 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8445 bool *no_add_attrs)
8447 for (; args; args = TREE_CHAIN (args))
8449 tree position = TREE_VALUE (args);
8450 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8451 && TREE_CODE (position) != FUNCTION_DECL)
8452 position = default_conversion (position);
8454 if (TREE_CODE (position) != INTEGER_CST)
8456 warning (OPT_Wattributes,
8457 "assume_aligned parameter not integer constant");
8458 *no_add_attrs = true;
8459 return NULL_TREE;
8462 return NULL_TREE;
8465 /* Handle a "fn spec" attribute; arguments as in
8466 struct attribute_spec.handler. */
8468 static tree
8469 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8470 tree args, int ARG_UNUSED (flags),
8471 bool *no_add_attrs ATTRIBUTE_UNUSED)
8473 gcc_assert (args
8474 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8475 && !TREE_CHAIN (args));
8476 return NULL_TREE;
8479 /* Handle a "bnd_variable_size" attribute; arguments as in
8480 struct attribute_spec.handler. */
8482 static tree
8483 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8484 int ARG_UNUSED (flags), bool *no_add_attrs)
8486 if (TREE_CODE (*node) != FIELD_DECL)
8488 warning (OPT_Wattributes, "%qE attribute ignored", name);
8489 *no_add_attrs = true;
8492 return NULL_TREE;
8495 /* Handle a "bnd_legacy" attribute; arguments as in
8496 struct attribute_spec.handler. */
8498 static tree
8499 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8500 int ARG_UNUSED (flags), bool *no_add_attrs)
8502 if (TREE_CODE (*node) != FUNCTION_DECL)
8504 warning (OPT_Wattributes, "%qE attribute ignored", name);
8505 *no_add_attrs = true;
8508 return NULL_TREE;
8511 /* Handle a "bnd_instrument" attribute; arguments as in
8512 struct attribute_spec.handler. */
8514 static tree
8515 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8516 int ARG_UNUSED (flags), bool *no_add_attrs)
8518 if (TREE_CODE (*node) != FUNCTION_DECL)
8520 warning (OPT_Wattributes, "%qE attribute ignored", name);
8521 *no_add_attrs = true;
8524 return NULL_TREE;
8527 /* Handle a "warn_unused" attribute; arguments as in
8528 struct attribute_spec.handler. */
8530 static tree
8531 handle_warn_unused_attribute (tree *node, tree name,
8532 tree args ATTRIBUTE_UNUSED,
8533 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8535 if (TYPE_P (*node))
8536 /* Do nothing else, just set the attribute. We'll get at
8537 it later with lookup_attribute. */
8539 else
8541 warning (OPT_Wattributes, "%qE attribute ignored", name);
8542 *no_add_attrs = true;
8545 return NULL_TREE;
8548 /* Handle an "omp declare simd" attribute; arguments as in
8549 struct attribute_spec.handler. */
8551 static tree
8552 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8554 return NULL_TREE;
8557 /* Handle an "omp declare target" attribute; arguments as in
8558 struct attribute_spec.handler. */
8560 static tree
8561 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8563 return NULL_TREE;
8566 /* Handle a "returns_twice" attribute; arguments as in
8567 struct attribute_spec.handler. */
8569 static tree
8570 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8571 int ARG_UNUSED (flags), bool *no_add_attrs)
8573 if (TREE_CODE (*node) == FUNCTION_DECL)
8574 DECL_IS_RETURNS_TWICE (*node) = 1;
8575 else
8577 warning (OPT_Wattributes, "%qE attribute ignored", name);
8578 *no_add_attrs = true;
8581 return NULL_TREE;
8584 /* Handle a "no_limit_stack" attribute; arguments as in
8585 struct attribute_spec.handler. */
8587 static tree
8588 handle_no_limit_stack_attribute (tree *node, tree name,
8589 tree ARG_UNUSED (args),
8590 int ARG_UNUSED (flags),
8591 bool *no_add_attrs)
8593 tree decl = *node;
8595 if (TREE_CODE (decl) != FUNCTION_DECL)
8597 error_at (DECL_SOURCE_LOCATION (decl),
8598 "%qE attribute applies only to functions", name);
8599 *no_add_attrs = true;
8601 else if (DECL_INITIAL (decl))
8603 error_at (DECL_SOURCE_LOCATION (decl),
8604 "can%'t set %qE attribute after definition", name);
8605 *no_add_attrs = true;
8607 else
8608 DECL_NO_LIMIT_STACK (decl) = 1;
8610 return NULL_TREE;
8613 /* Handle a "pure" attribute; arguments as in
8614 struct attribute_spec.handler. */
8616 static tree
8617 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8618 int ARG_UNUSED (flags), bool *no_add_attrs)
8620 if (TREE_CODE (*node) == FUNCTION_DECL)
8621 DECL_PURE_P (*node) = 1;
8622 /* ??? TODO: Support types. */
8623 else
8625 warning (OPT_Wattributes, "%qE attribute ignored", name);
8626 *no_add_attrs = true;
8629 return NULL_TREE;
8632 /* Digest an attribute list destined for a transactional memory statement.
8633 ALLOWED is the set of attributes that are allowed for this statement;
8634 return the attribute we parsed. Multiple attributes are never allowed. */
8637 parse_tm_stmt_attr (tree attrs, int allowed)
8639 tree a_seen = NULL;
8640 int m_seen = 0;
8642 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8644 tree a = TREE_PURPOSE (attrs);
8645 int m = 0;
8647 if (is_attribute_p ("outer", a))
8648 m = TM_STMT_ATTR_OUTER;
8650 if ((m & allowed) == 0)
8652 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8653 continue;
8656 if (m_seen == 0)
8658 a_seen = a;
8659 m_seen = m;
8661 else if (m_seen == m)
8662 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8663 else
8664 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8667 return m_seen;
8670 /* Transform a TM attribute name into a maskable integer and back.
8671 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8672 to how the lack of an attribute is treated. */
8675 tm_attr_to_mask (tree attr)
8677 if (attr == NULL)
8678 return 0;
8679 if (is_attribute_p ("transaction_safe", attr))
8680 return TM_ATTR_SAFE;
8681 if (is_attribute_p ("transaction_callable", attr))
8682 return TM_ATTR_CALLABLE;
8683 if (is_attribute_p ("transaction_pure", attr))
8684 return TM_ATTR_PURE;
8685 if (is_attribute_p ("transaction_unsafe", attr))
8686 return TM_ATTR_IRREVOCABLE;
8687 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8688 return TM_ATTR_MAY_CANCEL_OUTER;
8689 return 0;
8692 tree
8693 tm_mask_to_attr (int mask)
8695 const char *str;
8696 switch (mask)
8698 case TM_ATTR_SAFE:
8699 str = "transaction_safe";
8700 break;
8701 case TM_ATTR_CALLABLE:
8702 str = "transaction_callable";
8703 break;
8704 case TM_ATTR_PURE:
8705 str = "transaction_pure";
8706 break;
8707 case TM_ATTR_IRREVOCABLE:
8708 str = "transaction_unsafe";
8709 break;
8710 case TM_ATTR_MAY_CANCEL_OUTER:
8711 str = "transaction_may_cancel_outer";
8712 break;
8713 default:
8714 gcc_unreachable ();
8716 return get_identifier (str);
8719 /* Return the first TM attribute seen in LIST. */
8721 tree
8722 find_tm_attribute (tree list)
8724 for (; list ; list = TREE_CHAIN (list))
8726 tree name = TREE_PURPOSE (list);
8727 if (tm_attr_to_mask (name) != 0)
8728 return name;
8730 return NULL_TREE;
8733 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8734 Here we accept only function types, and verify that none of the other
8735 function TM attributes are also applied. */
8736 /* ??? We need to accept class types for C++, but not C. This greatly
8737 complicates this function, since we can no longer rely on the extra
8738 processing given by function_type_required. */
8740 static tree
8741 handle_tm_attribute (tree *node, tree name, tree args,
8742 int flags, bool *no_add_attrs)
8744 /* Only one path adds the attribute; others don't. */
8745 *no_add_attrs = true;
8747 switch (TREE_CODE (*node))
8749 case RECORD_TYPE:
8750 case UNION_TYPE:
8751 /* Only tm_callable and tm_safe apply to classes. */
8752 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8753 goto ignored;
8754 /* FALLTHRU */
8756 case FUNCTION_TYPE:
8757 case METHOD_TYPE:
8759 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8760 if (old_name == name)
8762 else if (old_name != NULL_TREE)
8763 error ("type was previously declared %qE", old_name);
8764 else
8765 *no_add_attrs = false;
8767 break;
8769 case POINTER_TYPE:
8771 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8772 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8774 tree fn_tmp = TREE_TYPE (*node);
8775 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8776 *node = build_pointer_type (fn_tmp);
8777 break;
8780 /* FALLTHRU */
8782 default:
8783 /* If a function is next, pass it on to be tried next. */
8784 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8785 return tree_cons (name, args, NULL);
8787 ignored:
8788 warning (OPT_Wattributes, "%qE attribute ignored", name);
8789 break;
8792 return NULL_TREE;
8795 /* Handle the TM_WRAP attribute; arguments as in
8796 struct attribute_spec.handler. */
8798 static tree
8799 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8800 int ARG_UNUSED (flags), bool *no_add_attrs)
8802 tree decl = *node;
8804 /* We don't need the attribute even on success, since we
8805 record the entry in an external table. */
8806 *no_add_attrs = true;
8808 if (TREE_CODE (decl) != FUNCTION_DECL)
8809 warning (OPT_Wattributes, "%qE attribute ignored", name);
8810 else
8812 tree wrap_decl = TREE_VALUE (args);
8813 if (error_operand_p (wrap_decl))
8815 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8816 && TREE_CODE (wrap_decl) != VAR_DECL
8817 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8818 error ("%qE argument not an identifier", name);
8819 else
8821 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8822 wrap_decl = lookup_name (wrap_decl);
8823 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8825 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8826 TREE_TYPE (wrap_decl)))
8827 record_tm_replacement (wrap_decl, decl);
8828 else
8829 error ("%qD is not compatible with %qD", wrap_decl, decl);
8831 else
8832 error ("%qE argument is not a function", name);
8836 return NULL_TREE;
8839 /* Ignore the given attribute. Used when this attribute may be usefully
8840 overridden by the target, but is not used generically. */
8842 static tree
8843 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8844 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8845 bool *no_add_attrs)
8847 *no_add_attrs = true;
8848 return NULL_TREE;
8851 /* Handle a "no vops" attribute; arguments as in
8852 struct attribute_spec.handler. */
8854 static tree
8855 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8856 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8857 bool *ARG_UNUSED (no_add_attrs))
8859 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8860 DECL_IS_NOVOPS (*node) = 1;
8861 return NULL_TREE;
8864 /* Handle a "deprecated" attribute; arguments as in
8865 struct attribute_spec.handler. */
8867 static tree
8868 handle_deprecated_attribute (tree *node, tree name,
8869 tree args, int flags,
8870 bool *no_add_attrs)
8872 tree type = NULL_TREE;
8873 int warn = 0;
8874 tree what = NULL_TREE;
8876 if (!args)
8877 *no_add_attrs = true;
8878 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8880 error ("deprecated message is not a string");
8881 *no_add_attrs = true;
8884 if (DECL_P (*node))
8886 tree decl = *node;
8887 type = TREE_TYPE (decl);
8889 if (TREE_CODE (decl) == TYPE_DECL
8890 || TREE_CODE (decl) == PARM_DECL
8891 || TREE_CODE (decl) == VAR_DECL
8892 || TREE_CODE (decl) == FUNCTION_DECL
8893 || TREE_CODE (decl) == FIELD_DECL
8894 || objc_method_decl (TREE_CODE (decl)))
8895 TREE_DEPRECATED (decl) = 1;
8896 else
8897 warn = 1;
8899 else if (TYPE_P (*node))
8901 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8902 *node = build_variant_type_copy (*node);
8903 TREE_DEPRECATED (*node) = 1;
8904 type = *node;
8906 else
8907 warn = 1;
8909 if (warn)
8911 *no_add_attrs = true;
8912 if (type && TYPE_NAME (type))
8914 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8915 what = TYPE_NAME (*node);
8916 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8917 && DECL_NAME (TYPE_NAME (type)))
8918 what = DECL_NAME (TYPE_NAME (type));
8920 if (what)
8921 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8922 else
8923 warning (OPT_Wattributes, "%qE attribute ignored", name);
8926 return NULL_TREE;
8929 /* Handle a "vector_size" attribute; arguments as in
8930 struct attribute_spec.handler. */
8932 static tree
8933 handle_vector_size_attribute (tree *node, tree name, tree args,
8934 int ARG_UNUSED (flags),
8935 bool *no_add_attrs)
8937 unsigned HOST_WIDE_INT vecsize, nunits;
8938 machine_mode orig_mode;
8939 tree type = *node, new_type, size;
8941 *no_add_attrs = true;
8943 size = TREE_VALUE (args);
8944 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8945 && TREE_CODE (size) != FUNCTION_DECL)
8946 size = default_conversion (size);
8948 if (!tree_fits_uhwi_p (size))
8950 warning (OPT_Wattributes, "%qE attribute ignored", name);
8951 return NULL_TREE;
8954 /* Get the vector size (in bytes). */
8955 vecsize = tree_to_uhwi (size);
8957 /* We need to provide for vector pointers, vector arrays, and
8958 functions returning vectors. For example:
8960 __attribute__((vector_size(16))) short *foo;
8962 In this case, the mode is SI, but the type being modified is
8963 HI, so we need to look further. */
8965 while (POINTER_TYPE_P (type)
8966 || TREE_CODE (type) == FUNCTION_TYPE
8967 || TREE_CODE (type) == METHOD_TYPE
8968 || TREE_CODE (type) == ARRAY_TYPE
8969 || TREE_CODE (type) == OFFSET_TYPE)
8970 type = TREE_TYPE (type);
8972 /* Get the mode of the type being modified. */
8973 orig_mode = TYPE_MODE (type);
8975 if ((!INTEGRAL_TYPE_P (type)
8976 && !SCALAR_FLOAT_TYPE_P (type)
8977 && !FIXED_POINT_TYPE_P (type))
8978 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8979 && GET_MODE_CLASS (orig_mode) != MODE_INT
8980 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8981 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8982 || TREE_CODE (type) == BOOLEAN_TYPE)
8984 error ("invalid vector type for attribute %qE", name);
8985 return NULL_TREE;
8988 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8990 error ("vector size not an integral multiple of component size");
8991 return NULL;
8994 if (vecsize == 0)
8996 error ("zero vector size");
8997 return NULL;
9000 /* Calculate how many units fit in the vector. */
9001 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
9002 if (nunits & (nunits - 1))
9004 error ("number of components of the vector not a power of two");
9005 return NULL_TREE;
9008 new_type = build_vector_type (type, nunits);
9010 /* Build back pointers if needed. */
9011 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9013 return NULL_TREE;
9016 /* Handle the "nonnull" attribute. */
9017 static tree
9018 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9019 tree args, int ARG_UNUSED (flags),
9020 bool *no_add_attrs)
9022 tree type = *node;
9023 unsigned HOST_WIDE_INT attr_arg_num;
9025 /* If no arguments are specified, all pointer arguments should be
9026 non-null. Verify a full prototype is given so that the arguments
9027 will have the correct types when we actually check them later. */
9028 if (!args)
9030 if (!prototype_p (type))
9032 error ("nonnull attribute without arguments on a non-prototype");
9033 *no_add_attrs = true;
9035 return NULL_TREE;
9038 /* Argument list specified. Verify that each argument number references
9039 a pointer argument. */
9040 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9042 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9044 tree arg = TREE_VALUE (args);
9045 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9046 && TREE_CODE (arg) != FUNCTION_DECL)
9047 arg = default_conversion (arg);
9049 if (!get_nonnull_operand (arg, &arg_num))
9051 error ("nonnull argument has invalid operand number (argument %lu)",
9052 (unsigned long) attr_arg_num);
9053 *no_add_attrs = true;
9054 return NULL_TREE;
9057 if (prototype_p (type))
9059 function_args_iterator iter;
9060 tree argument;
9062 function_args_iter_init (&iter, type);
9063 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9065 argument = function_args_iter_cond (&iter);
9066 if (argument == NULL_TREE || ck_num == arg_num)
9067 break;
9070 if (!argument
9071 || TREE_CODE (argument) == VOID_TYPE)
9073 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9074 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9075 *no_add_attrs = true;
9076 return NULL_TREE;
9079 if (TREE_CODE (argument) != POINTER_TYPE)
9081 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9082 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9083 *no_add_attrs = true;
9084 return NULL_TREE;
9089 return NULL_TREE;
9092 /* Check the argument list of a function call for null in argument slots
9093 that are marked as requiring a non-null pointer argument. The NARGS
9094 arguments are passed in the array ARGARRAY.
9097 static void
9098 check_function_nonnull (tree attrs, int nargs, tree *argarray)
9100 tree a;
9101 int i;
9103 attrs = lookup_attribute ("nonnull", attrs);
9104 if (attrs == NULL_TREE)
9105 return;
9107 a = attrs;
9108 /* See if any of the nonnull attributes has no arguments. If so,
9109 then every pointer argument is checked (in which case the check
9110 for pointer type is done in check_nonnull_arg). */
9111 if (TREE_VALUE (a) != NULL_TREE)
9113 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9114 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9116 if (a != NULL_TREE)
9117 for (i = 0; i < nargs; i++)
9118 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9119 i + 1);
9120 else
9122 /* Walk the argument list. If we encounter an argument number we
9123 should check for non-null, do it. */
9124 for (i = 0; i < nargs; i++)
9126 for (a = attrs; ; a = TREE_CHAIN (a))
9128 a = lookup_attribute ("nonnull", a);
9129 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9130 break;
9133 if (a != NULL_TREE)
9134 check_function_arguments_recurse (check_nonnull_arg, NULL,
9135 argarray[i], i + 1);
9140 /* Check that the Nth argument of a function call (counting backwards
9141 from the end) is a (pointer)0. The NARGS arguments are passed in the
9142 array ARGARRAY. */
9144 static void
9145 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9147 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9149 if (attr)
9151 int len = 0;
9152 int pos = 0;
9153 tree sentinel;
9154 function_args_iterator iter;
9155 tree t;
9157 /* Skip over the named arguments. */
9158 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9160 if (len == nargs)
9161 break;
9162 len++;
9165 if (TREE_VALUE (attr))
9167 tree p = TREE_VALUE (TREE_VALUE (attr));
9168 pos = TREE_INT_CST_LOW (p);
9171 /* The sentinel must be one of the varargs, i.e.
9172 in position >= the number of fixed arguments. */
9173 if ((nargs - 1 - pos) < len)
9175 warning (OPT_Wformat_,
9176 "not enough variable arguments to fit a sentinel");
9177 return;
9180 /* Validate the sentinel. */
9181 sentinel = argarray[nargs - 1 - pos];
9182 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9183 || !integer_zerop (sentinel))
9184 /* Although __null (in C++) is only an integer we allow it
9185 nevertheless, as we are guaranteed that it's exactly
9186 as wide as a pointer, and we don't want to force
9187 users to cast the NULL they have written there.
9188 We warn with -Wstrict-null-sentinel, though. */
9189 && (warn_strict_null_sentinel || null_node != sentinel))
9190 warning (OPT_Wformat_, "missing sentinel in function call");
9194 /* Helper for check_function_nonnull; given a list of operands which
9195 must be non-null in ARGS, determine if operand PARAM_NUM should be
9196 checked. */
9198 static bool
9199 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9201 unsigned HOST_WIDE_INT arg_num = 0;
9203 for (; args; args = TREE_CHAIN (args))
9205 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9207 gcc_assert (found);
9209 if (arg_num == param_num)
9210 return true;
9212 return false;
9215 /* Check that the function argument PARAM (which is operand number
9216 PARAM_NUM) is non-null. This is called by check_function_nonnull
9217 via check_function_arguments_recurse. */
9219 static void
9220 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9221 unsigned HOST_WIDE_INT param_num)
9223 /* Just skip checking the argument if it's not a pointer. This can
9224 happen if the "nonnull" attribute was given without an operand
9225 list (which means to check every pointer argument). */
9227 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9228 return;
9230 if (integer_zerop (param))
9231 warning (OPT_Wnonnull, "null argument where non-null required "
9232 "(argument %lu)", (unsigned long) param_num);
9235 /* Helper for nonnull attribute handling; fetch the operand number
9236 from the attribute argument list. */
9238 static bool
9239 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9241 /* Verify the arg number is a small constant. */
9242 if (tree_fits_uhwi_p (arg_num_expr))
9244 *valp = TREE_INT_CST_LOW (arg_num_expr);
9245 return true;
9247 else
9248 return false;
9251 /* Handle a "nothrow" attribute; arguments as in
9252 struct attribute_spec.handler. */
9254 static tree
9255 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9256 int ARG_UNUSED (flags), bool *no_add_attrs)
9258 if (TREE_CODE (*node) == FUNCTION_DECL)
9259 TREE_NOTHROW (*node) = 1;
9260 /* ??? TODO: Support types. */
9261 else
9263 warning (OPT_Wattributes, "%qE attribute ignored", name);
9264 *no_add_attrs = true;
9267 return NULL_TREE;
9270 /* Handle a "cleanup" attribute; arguments as in
9271 struct attribute_spec.handler. */
9273 static tree
9274 handle_cleanup_attribute (tree *node, tree name, tree args,
9275 int ARG_UNUSED (flags), bool *no_add_attrs)
9277 tree decl = *node;
9278 tree cleanup_id, cleanup_decl;
9280 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9281 for global destructors in C++. This requires infrastructure that
9282 we don't have generically at the moment. It's also not a feature
9283 we'd be missing too much, since we do have attribute constructor. */
9284 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9286 warning (OPT_Wattributes, "%qE attribute ignored", name);
9287 *no_add_attrs = true;
9288 return NULL_TREE;
9291 /* Verify that the argument is a function in scope. */
9292 /* ??? We could support pointers to functions here as well, if
9293 that was considered desirable. */
9294 cleanup_id = TREE_VALUE (args);
9295 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9297 error ("cleanup argument not an identifier");
9298 *no_add_attrs = true;
9299 return NULL_TREE;
9301 cleanup_decl = lookup_name (cleanup_id);
9302 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9304 error ("cleanup argument not a function");
9305 *no_add_attrs = true;
9306 return NULL_TREE;
9309 /* That the function has proper type is checked with the
9310 eventual call to build_function_call. */
9312 return NULL_TREE;
9315 /* Handle a "warn_unused_result" attribute. No special handling. */
9317 static tree
9318 handle_warn_unused_result_attribute (tree *node, tree name,
9319 tree ARG_UNUSED (args),
9320 int ARG_UNUSED (flags), bool *no_add_attrs)
9322 /* Ignore the attribute for functions not returning any value. */
9323 if (VOID_TYPE_P (TREE_TYPE (*node)))
9325 warning (OPT_Wattributes, "%qE attribute ignored", name);
9326 *no_add_attrs = true;
9329 return NULL_TREE;
9332 /* Handle a "sentinel" attribute. */
9334 static tree
9335 handle_sentinel_attribute (tree *node, tree name, tree args,
9336 int ARG_UNUSED (flags), bool *no_add_attrs)
9338 if (!prototype_p (*node))
9340 warning (OPT_Wattributes,
9341 "%qE attribute requires prototypes with named arguments", name);
9342 *no_add_attrs = true;
9344 else
9346 if (!stdarg_p (*node))
9348 warning (OPT_Wattributes,
9349 "%qE attribute only applies to variadic functions", name);
9350 *no_add_attrs = true;
9354 if (args)
9356 tree position = TREE_VALUE (args);
9357 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9358 && TREE_CODE (position) != FUNCTION_DECL)
9359 position = default_conversion (position);
9361 if (TREE_CODE (position) != INTEGER_CST
9362 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9364 warning (OPT_Wattributes,
9365 "requested position is not an integer constant");
9366 *no_add_attrs = true;
9368 else
9370 if (tree_int_cst_lt (position, integer_zero_node))
9372 warning (OPT_Wattributes,
9373 "requested position is less than zero");
9374 *no_add_attrs = true;
9379 return NULL_TREE;
9382 /* Handle a "type_generic" attribute. */
9384 static tree
9385 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9386 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9387 bool * ARG_UNUSED (no_add_attrs))
9389 /* Ensure we have a function type. */
9390 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9392 /* Ensure we have a variadic function. */
9393 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9395 return NULL_TREE;
9398 /* Handle a "target" attribute. */
9400 static tree
9401 handle_target_attribute (tree *node, tree name, tree args, int flags,
9402 bool *no_add_attrs)
9404 /* Ensure we have a function type. */
9405 if (TREE_CODE (*node) != FUNCTION_DECL)
9407 warning (OPT_Wattributes, "%qE attribute ignored", name);
9408 *no_add_attrs = true;
9410 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9411 flags))
9412 *no_add_attrs = true;
9414 return NULL_TREE;
9417 /* Arguments being collected for optimization. */
9418 typedef const char *const_char_p; /* For DEF_VEC_P. */
9419 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9422 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9423 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9424 false for #pragma GCC optimize. */
9426 bool
9427 parse_optimize_options (tree args, bool attr_p)
9429 bool ret = true;
9430 unsigned opt_argc;
9431 unsigned i;
9432 int saved_flag_strict_aliasing;
9433 const char **opt_argv;
9434 struct cl_decoded_option *decoded_options;
9435 unsigned int decoded_options_count;
9436 tree ap;
9438 /* Build up argv vector. Just in case the string is stored away, use garbage
9439 collected strings. */
9440 vec_safe_truncate (optimize_args, 0);
9441 vec_safe_push (optimize_args, (const char *) NULL);
9443 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9445 tree value = TREE_VALUE (ap);
9447 if (TREE_CODE (value) == INTEGER_CST)
9449 char buffer[20];
9450 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9451 vec_safe_push (optimize_args, ggc_strdup (buffer));
9454 else if (TREE_CODE (value) == STRING_CST)
9456 /* Split string into multiple substrings. */
9457 size_t len = TREE_STRING_LENGTH (value);
9458 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9459 char *end = p + len;
9460 char *comma;
9461 char *next_p = p;
9463 while (next_p != NULL)
9465 size_t len2;
9466 char *q, *r;
9468 p = next_p;
9469 comma = strchr (p, ',');
9470 if (comma)
9472 len2 = comma - p;
9473 *comma = '\0';
9474 next_p = comma+1;
9476 else
9478 len2 = end - p;
9479 next_p = NULL;
9482 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9484 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9485 options. */
9486 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9488 ret = false;
9489 if (attr_p)
9490 warning (OPT_Wattributes,
9491 "bad option %s to optimize attribute", p);
9492 else
9493 warning (OPT_Wpragmas,
9494 "bad option %s to pragma attribute", p);
9495 continue;
9498 if (*p != '-')
9500 *r++ = '-';
9502 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9503 itself is -Os, and any other switch begins with a -f. */
9504 if ((*p >= '0' && *p <= '9')
9505 || (p[0] == 's' && p[1] == '\0'))
9506 *r++ = 'O';
9507 else if (*p != 'O')
9508 *r++ = 'f';
9511 memcpy (r, p, len2);
9512 r[len2] = '\0';
9513 vec_safe_push (optimize_args, (const char *) q);
9519 opt_argc = optimize_args->length ();
9520 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9522 for (i = 1; i < opt_argc; i++)
9523 opt_argv[i] = (*optimize_args)[i];
9525 saved_flag_strict_aliasing = flag_strict_aliasing;
9527 /* Now parse the options. */
9528 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9529 &decoded_options,
9530 &decoded_options_count);
9531 decode_options (&global_options, &global_options_set,
9532 decoded_options, decoded_options_count,
9533 input_location, global_dc);
9535 targetm.override_options_after_change();
9537 /* Don't allow changing -fstrict-aliasing. */
9538 flag_strict_aliasing = saved_flag_strict_aliasing;
9540 optimize_args->truncate (0);
9541 return ret;
9544 /* For handling "optimize" attribute. arguments as in
9545 struct attribute_spec.handler. */
9547 static tree
9548 handle_optimize_attribute (tree *node, tree name, tree args,
9549 int ARG_UNUSED (flags), bool *no_add_attrs)
9551 /* Ensure we have a function type. */
9552 if (TREE_CODE (*node) != FUNCTION_DECL)
9554 warning (OPT_Wattributes, "%qE attribute ignored", name);
9555 *no_add_attrs = true;
9557 else
9559 struct cl_optimization cur_opts;
9560 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9562 /* Save current options. */
9563 cl_optimization_save (&cur_opts, &global_options);
9565 /* If we previously had some optimization options, use them as the
9566 default. */
9567 if (old_opts)
9568 cl_optimization_restore (&global_options,
9569 TREE_OPTIMIZATION (old_opts));
9571 /* Parse options, and update the vector. */
9572 parse_optimize_options (args, true);
9573 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9574 = build_optimization_node (&global_options);
9576 /* Restore current options. */
9577 cl_optimization_restore (&global_options, &cur_opts);
9580 return NULL_TREE;
9583 /* Handle a "no_split_stack" attribute. */
9585 static tree
9586 handle_no_split_stack_attribute (tree *node, tree name,
9587 tree ARG_UNUSED (args),
9588 int ARG_UNUSED (flags),
9589 bool *no_add_attrs)
9591 tree decl = *node;
9593 if (TREE_CODE (decl) != FUNCTION_DECL)
9595 error_at (DECL_SOURCE_LOCATION (decl),
9596 "%qE attribute applies only to functions", name);
9597 *no_add_attrs = true;
9599 else if (DECL_INITIAL (decl))
9601 error_at (DECL_SOURCE_LOCATION (decl),
9602 "can%'t set %qE attribute after definition", name);
9603 *no_add_attrs = true;
9606 return NULL_TREE;
9609 /* Handle a "returns_nonnull" attribute; arguments as in
9610 struct attribute_spec.handler. */
9612 static tree
9613 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9614 bool *no_add_attrs)
9616 // Even without a prototype we still have a return type we can check.
9617 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9619 error ("returns_nonnull attribute on a function not returning a pointer");
9620 *no_add_attrs = true;
9622 return NULL_TREE;
9625 /* Handle a "designated_init" attribute; arguments as in
9626 struct attribute_spec.handler. */
9628 static tree
9629 handle_designated_init_attribute (tree *node, tree name, tree, int,
9630 bool *no_add_attrs)
9632 if (TREE_CODE (*node) != RECORD_TYPE)
9634 error ("%qE attribute is only valid on %<struct%> type", name);
9635 *no_add_attrs = true;
9637 return NULL_TREE;
9641 /* Check for valid arguments being passed to a function with FNTYPE.
9642 There are NARGS arguments in the array ARGARRAY. */
9643 void
9644 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9646 /* Check for null being passed in a pointer argument that must be
9647 non-null. We also need to do this if format checking is enabled. */
9649 if (warn_nonnull)
9650 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9652 /* Check for errors in format strings. */
9654 if (warn_format || warn_suggest_attribute_format)
9655 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9657 if (warn_format)
9658 check_function_sentinel (fntype, nargs, argarray);
9661 /* Generic argument checking recursion routine. PARAM is the argument to
9662 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9663 once the argument is resolved. CTX is context for the callback. */
9664 void
9665 check_function_arguments_recurse (void (*callback)
9666 (void *, tree, unsigned HOST_WIDE_INT),
9667 void *ctx, tree param,
9668 unsigned HOST_WIDE_INT param_num)
9670 if (CONVERT_EXPR_P (param)
9671 && (TYPE_PRECISION (TREE_TYPE (param))
9672 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9674 /* Strip coercion. */
9675 check_function_arguments_recurse (callback, ctx,
9676 TREE_OPERAND (param, 0), param_num);
9677 return;
9680 if (TREE_CODE (param) == CALL_EXPR)
9682 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9683 tree attrs;
9684 bool found_format_arg = false;
9686 /* See if this is a call to a known internationalization function
9687 that modifies a format arg. Such a function may have multiple
9688 format_arg attributes (for example, ngettext). */
9690 for (attrs = TYPE_ATTRIBUTES (type);
9691 attrs;
9692 attrs = TREE_CHAIN (attrs))
9693 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9695 tree inner_arg;
9696 tree format_num_expr;
9697 int format_num;
9698 int i;
9699 call_expr_arg_iterator iter;
9701 /* Extract the argument number, which was previously checked
9702 to be valid. */
9703 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9705 format_num = tree_to_uhwi (format_num_expr);
9707 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9708 inner_arg != 0;
9709 inner_arg = next_call_expr_arg (&iter), i++)
9710 if (i == format_num)
9712 check_function_arguments_recurse (callback, ctx,
9713 inner_arg, param_num);
9714 found_format_arg = true;
9715 break;
9719 /* If we found a format_arg attribute and did a recursive check,
9720 we are done with checking this argument. Otherwise, we continue
9721 and this will be considered a non-literal. */
9722 if (found_format_arg)
9723 return;
9726 if (TREE_CODE (param) == COND_EXPR)
9728 /* Check both halves of the conditional expression. */
9729 check_function_arguments_recurse (callback, ctx,
9730 TREE_OPERAND (param, 1), param_num);
9731 check_function_arguments_recurse (callback, ctx,
9732 TREE_OPERAND (param, 2), param_num);
9733 return;
9736 (*callback) (ctx, param, param_num);
9739 /* Checks for a builtin function FNDECL that the number of arguments
9740 NARGS against the required number REQUIRED and issues an error if
9741 there is a mismatch. Returns true if the number of arguments is
9742 correct, otherwise false. */
9744 static bool
9745 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9747 if (nargs < required)
9749 error_at (input_location,
9750 "not enough arguments to function %qE", fndecl);
9751 return false;
9753 else if (nargs > required)
9755 error_at (input_location,
9756 "too many arguments to function %qE", fndecl);
9757 return false;
9759 return true;
9762 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9763 Returns false if there was an error, otherwise true. */
9765 bool
9766 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9768 if (!DECL_BUILT_IN (fndecl)
9769 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9770 return true;
9772 switch (DECL_FUNCTION_CODE (fndecl))
9774 case BUILT_IN_CONSTANT_P:
9775 return builtin_function_validate_nargs (fndecl, nargs, 1);
9777 case BUILT_IN_ISFINITE:
9778 case BUILT_IN_ISINF:
9779 case BUILT_IN_ISINF_SIGN:
9780 case BUILT_IN_ISNAN:
9781 case BUILT_IN_ISNORMAL:
9782 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9784 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9786 error ("non-floating-point argument in call to "
9787 "function %qE", fndecl);
9788 return false;
9790 return true;
9792 return false;
9794 case BUILT_IN_ISGREATER:
9795 case BUILT_IN_ISGREATEREQUAL:
9796 case BUILT_IN_ISLESS:
9797 case BUILT_IN_ISLESSEQUAL:
9798 case BUILT_IN_ISLESSGREATER:
9799 case BUILT_IN_ISUNORDERED:
9800 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9802 enum tree_code code0, code1;
9803 code0 = TREE_CODE (TREE_TYPE (args[0]));
9804 code1 = TREE_CODE (TREE_TYPE (args[1]));
9805 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9806 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9807 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9809 error ("non-floating-point arguments in call to "
9810 "function %qE", fndecl);
9811 return false;
9813 return true;
9815 return false;
9817 case BUILT_IN_FPCLASSIFY:
9818 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9820 unsigned i;
9822 for (i=0; i<5; i++)
9823 if (TREE_CODE (args[i]) != INTEGER_CST)
9825 error ("non-const integer argument %u in call to function %qE",
9826 i+1, fndecl);
9827 return false;
9830 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9832 error ("non-floating-point argument in call to function %qE",
9833 fndecl);
9834 return false;
9836 return true;
9838 return false;
9840 case BUILT_IN_ASSUME_ALIGNED:
9841 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9843 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9845 error ("non-integer argument 3 in call to function %qE", fndecl);
9846 return false;
9848 return true;
9850 return false;
9852 case BUILT_IN_ADD_OVERFLOW:
9853 case BUILT_IN_SUB_OVERFLOW:
9854 case BUILT_IN_MUL_OVERFLOW:
9855 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9857 unsigned i;
9858 for (i = 0; i < 2; i++)
9859 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9861 error ("argument %u in call to function %qE does not have "
9862 "integral type", i + 1, fndecl);
9863 return false;
9865 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9866 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9868 error ("argument 3 in call to function %qE does not have "
9869 "pointer to integer type", fndecl);
9870 return false;
9872 return true;
9874 return false;
9876 default:
9877 return true;
9881 /* Function to help qsort sort FIELD_DECLs by name order. */
9884 field_decl_cmp (const void *x_p, const void *y_p)
9886 const tree *const x = (const tree *const) x_p;
9887 const tree *const y = (const tree *const) y_p;
9889 if (DECL_NAME (*x) == DECL_NAME (*y))
9890 /* A nontype is "greater" than a type. */
9891 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9892 if (DECL_NAME (*x) == NULL_TREE)
9893 return -1;
9894 if (DECL_NAME (*y) == NULL_TREE)
9895 return 1;
9896 if (DECL_NAME (*x) < DECL_NAME (*y))
9897 return -1;
9898 return 1;
9901 static struct {
9902 gt_pointer_operator new_value;
9903 void *cookie;
9904 } resort_data;
9906 /* This routine compares two fields like field_decl_cmp but using the
9907 pointer operator in resort_data. */
9909 static int
9910 resort_field_decl_cmp (const void *x_p, const void *y_p)
9912 const tree *const x = (const tree *const) x_p;
9913 const tree *const y = (const tree *const) y_p;
9915 if (DECL_NAME (*x) == DECL_NAME (*y))
9916 /* A nontype is "greater" than a type. */
9917 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9918 if (DECL_NAME (*x) == NULL_TREE)
9919 return -1;
9920 if (DECL_NAME (*y) == NULL_TREE)
9921 return 1;
9923 tree d1 = DECL_NAME (*x);
9924 tree d2 = DECL_NAME (*y);
9925 resort_data.new_value (&d1, resort_data.cookie);
9926 resort_data.new_value (&d2, resort_data.cookie);
9927 if (d1 < d2)
9928 return -1;
9930 return 1;
9933 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9935 void
9936 resort_sorted_fields (void *obj,
9937 void * ARG_UNUSED (orig_obj),
9938 gt_pointer_operator new_value,
9939 void *cookie)
9941 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9942 resort_data.new_value = new_value;
9943 resort_data.cookie = cookie;
9944 qsort (&sf->elts[0], sf->len, sizeof (tree),
9945 resort_field_decl_cmp);
9948 /* Subroutine of c_parse_error.
9949 Return the result of concatenating LHS and RHS. RHS is really
9950 a string literal, its first character is indicated by RHS_START and
9951 RHS_SIZE is its length (including the terminating NUL character).
9953 The caller is responsible for deleting the returned pointer. */
9955 static char *
9956 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9958 const int lhs_size = strlen (lhs);
9959 char *result = XNEWVEC (char, lhs_size + rhs_size);
9960 strncpy (result, lhs, lhs_size);
9961 strncpy (result + lhs_size, rhs_start, rhs_size);
9962 return result;
9965 /* Issue the error given by GMSGID, indicating that it occurred before
9966 TOKEN, which had the associated VALUE. */
9968 void
9969 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9970 tree value, unsigned char token_flags)
9972 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9974 char *message = NULL;
9976 if (token_type == CPP_EOF)
9977 message = catenate_messages (gmsgid, " at end of input");
9978 else if (token_type == CPP_CHAR
9979 || token_type == CPP_WCHAR
9980 || token_type == CPP_CHAR16
9981 || token_type == CPP_CHAR32)
9983 unsigned int val = TREE_INT_CST_LOW (value);
9984 const char *prefix;
9986 switch (token_type)
9988 default:
9989 prefix = "";
9990 break;
9991 case CPP_WCHAR:
9992 prefix = "L";
9993 break;
9994 case CPP_CHAR16:
9995 prefix = "u";
9996 break;
9997 case CPP_CHAR32:
9998 prefix = "U";
9999 break;
10002 if (val <= UCHAR_MAX && ISGRAPH (val))
10003 message = catenate_messages (gmsgid, " before %s'%c'");
10004 else
10005 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10007 error (message, prefix, val);
10008 free (message);
10009 message = NULL;
10011 else if (token_type == CPP_CHAR_USERDEF
10012 || token_type == CPP_WCHAR_USERDEF
10013 || token_type == CPP_CHAR16_USERDEF
10014 || token_type == CPP_CHAR32_USERDEF)
10015 message = catenate_messages (gmsgid,
10016 " before user-defined character literal");
10017 else if (token_type == CPP_STRING_USERDEF
10018 || token_type == CPP_WSTRING_USERDEF
10019 || token_type == CPP_STRING16_USERDEF
10020 || token_type == CPP_STRING32_USERDEF
10021 || token_type == CPP_UTF8STRING_USERDEF)
10022 message = catenate_messages (gmsgid, " before user-defined string literal");
10023 else if (token_type == CPP_STRING
10024 || token_type == CPP_WSTRING
10025 || token_type == CPP_STRING16
10026 || token_type == CPP_STRING32
10027 || token_type == CPP_UTF8STRING)
10028 message = catenate_messages (gmsgid, " before string constant");
10029 else if (token_type == CPP_NUMBER)
10030 message = catenate_messages (gmsgid, " before numeric constant");
10031 else if (token_type == CPP_NAME)
10033 message = catenate_messages (gmsgid, " before %qE");
10034 error (message, value);
10035 free (message);
10036 message = NULL;
10038 else if (token_type == CPP_PRAGMA)
10039 message = catenate_messages (gmsgid, " before %<#pragma%>");
10040 else if (token_type == CPP_PRAGMA_EOL)
10041 message = catenate_messages (gmsgid, " before end of line");
10042 else if (token_type == CPP_DECLTYPE)
10043 message = catenate_messages (gmsgid, " before %<decltype%>");
10044 else if (token_type < N_TTYPES)
10046 message = catenate_messages (gmsgid, " before %qs token");
10047 error (message, cpp_type2name (token_type, token_flags));
10048 free (message);
10049 message = NULL;
10051 else
10052 error (gmsgid);
10054 if (message)
10056 error (message);
10057 free (message);
10059 #undef catenate_messages
10062 /* Return the gcc option code associated with the reason for a cpp
10063 message, or 0 if none. */
10065 static int
10066 c_option_controlling_cpp_error (int reason)
10068 const struct cpp_reason_option_codes_t *entry;
10070 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10072 if (entry->reason == reason)
10073 return entry->option_code;
10075 return 0;
10078 /* Callback from cpp_error for PFILE to print diagnostics from the
10079 preprocessor. The diagnostic is of type LEVEL, with REASON set
10080 to the reason code if LEVEL is represents a warning, at location
10081 LOCATION unless this is after lexing and the compiler's location
10082 should be used instead, with column number possibly overridden by
10083 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10084 the arguments. Returns true if a diagnostic was emitted, false
10085 otherwise. */
10087 bool
10088 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10089 location_t location, unsigned int column_override,
10090 const char *msg, va_list *ap)
10092 diagnostic_info diagnostic;
10093 diagnostic_t dlevel;
10094 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10095 bool ret;
10097 switch (level)
10099 case CPP_DL_WARNING_SYSHDR:
10100 if (flag_no_output)
10101 return false;
10102 global_dc->dc_warn_system_headers = 1;
10103 /* Fall through. */
10104 case CPP_DL_WARNING:
10105 if (flag_no_output)
10106 return false;
10107 dlevel = DK_WARNING;
10108 break;
10109 case CPP_DL_PEDWARN:
10110 if (flag_no_output && !flag_pedantic_errors)
10111 return false;
10112 dlevel = DK_PEDWARN;
10113 break;
10114 case CPP_DL_ERROR:
10115 dlevel = DK_ERROR;
10116 break;
10117 case CPP_DL_ICE:
10118 dlevel = DK_ICE;
10119 break;
10120 case CPP_DL_NOTE:
10121 dlevel = DK_NOTE;
10122 break;
10123 case CPP_DL_FATAL:
10124 dlevel = DK_FATAL;
10125 break;
10126 default:
10127 gcc_unreachable ();
10129 if (done_lexing)
10130 location = input_location;
10131 diagnostic_set_info_translated (&diagnostic, msg, ap,
10132 location, dlevel);
10133 if (column_override)
10134 diagnostic_override_column (&diagnostic, column_override);
10135 diagnostic_override_option_index (&diagnostic,
10136 c_option_controlling_cpp_error (reason));
10137 ret = report_diagnostic (&diagnostic);
10138 if (level == CPP_DL_WARNING_SYSHDR)
10139 global_dc->dc_warn_system_headers = save_warn_system_headers;
10140 return ret;
10143 /* Convert a character from the host to the target execution character
10144 set. cpplib handles this, mostly. */
10146 HOST_WIDE_INT
10147 c_common_to_target_charset (HOST_WIDE_INT c)
10149 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10150 zero-extended under -fno-signed-char. cpplib insists that characters
10151 and character constants are always unsigned. Hence we must convert
10152 back and forth. */
10153 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10155 uc = cpp_host_to_exec_charset (parse_in, uc);
10157 if (flag_signed_char)
10158 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10159 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10160 else
10161 return uc;
10164 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10165 references with an INDIRECT_REF of a constant at the bottom; much like the
10166 traditional rendering of offsetof as a macro. Return the folded result. */
10168 tree
10169 fold_offsetof_1 (tree expr)
10171 tree base, off, t;
10173 switch (TREE_CODE (expr))
10175 case ERROR_MARK:
10176 return expr;
10178 case VAR_DECL:
10179 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10180 return error_mark_node;
10182 case CALL_EXPR:
10183 case TARGET_EXPR:
10184 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10185 return error_mark_node;
10187 case NOP_EXPR:
10188 case INDIRECT_REF:
10189 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10191 error ("cannot apply %<offsetof%> to a non constant address");
10192 return error_mark_node;
10194 return TREE_OPERAND (expr, 0);
10196 case COMPONENT_REF:
10197 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10198 if (base == error_mark_node)
10199 return base;
10201 t = TREE_OPERAND (expr, 1);
10202 if (DECL_C_BIT_FIELD (t))
10204 error ("attempt to take address of bit-field structure "
10205 "member %qD", t);
10206 return error_mark_node;
10208 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10209 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10210 / BITS_PER_UNIT));
10211 break;
10213 case ARRAY_REF:
10214 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10215 if (base == error_mark_node)
10216 return base;
10218 t = TREE_OPERAND (expr, 1);
10220 /* Check if the offset goes beyond the upper bound of the array. */
10221 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10223 tree upbound = array_ref_up_bound (expr);
10224 if (upbound != NULL_TREE
10225 && TREE_CODE (upbound) == INTEGER_CST
10226 && !tree_int_cst_equal (upbound,
10227 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10229 upbound = size_binop (PLUS_EXPR, upbound,
10230 build_int_cst (TREE_TYPE (upbound), 1));
10231 if (tree_int_cst_lt (upbound, t))
10233 tree v;
10235 for (v = TREE_OPERAND (expr, 0);
10236 TREE_CODE (v) == COMPONENT_REF;
10237 v = TREE_OPERAND (v, 0))
10238 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10239 == RECORD_TYPE)
10241 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10242 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10243 if (TREE_CODE (fld_chain) == FIELD_DECL)
10244 break;
10246 if (fld_chain)
10247 break;
10249 /* Don't warn if the array might be considered a poor
10250 man's flexible array member with a very permissive
10251 definition thereof. */
10252 if (TREE_CODE (v) == ARRAY_REF
10253 || TREE_CODE (v) == COMPONENT_REF)
10254 warning (OPT_Warray_bounds,
10255 "index %E denotes an offset "
10256 "greater than size of %qT",
10257 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10262 t = convert (sizetype, t);
10263 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10264 break;
10266 case COMPOUND_EXPR:
10267 /* Handle static members of volatile structs. */
10268 t = TREE_OPERAND (expr, 1);
10269 gcc_assert (TREE_CODE (t) == VAR_DECL);
10270 return fold_offsetof_1 (t);
10272 default:
10273 gcc_unreachable ();
10276 return fold_build_pointer_plus (base, off);
10279 /* Likewise, but convert it to the return type of offsetof. */
10281 tree
10282 fold_offsetof (tree expr)
10284 return convert (size_type_node, fold_offsetof_1 (expr));
10287 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10288 expression, because B will always be true. */
10290 void
10291 warn_for_omitted_condop (location_t location, tree cond)
10293 if (truth_value_p (TREE_CODE (cond)))
10294 warning_at (location, OPT_Wparentheses,
10295 "the omitted middle operand in ?: will always be %<true%>, "
10296 "suggest explicit middle operand");
10299 /* Give an error for storing into ARG, which is 'const'. USE indicates
10300 how ARG was being used. */
10302 void
10303 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10305 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10306 || use == lv_asm);
10307 /* Using this macro rather than (for example) arrays of messages
10308 ensures that all the format strings are checked at compile
10309 time. */
10310 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10311 : (use == lv_increment ? (I) \
10312 : (use == lv_decrement ? (D) : (AS))))
10313 if (TREE_CODE (arg) == COMPONENT_REF)
10315 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10316 error_at (loc, READONLY_MSG (G_("assignment of member "
10317 "%qD in read-only object"),
10318 G_("increment of member "
10319 "%qD in read-only object"),
10320 G_("decrement of member "
10321 "%qD in read-only object"),
10322 G_("member %qD in read-only object "
10323 "used as %<asm%> output")),
10324 TREE_OPERAND (arg, 1));
10325 else
10326 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10327 G_("increment of read-only member %qD"),
10328 G_("decrement of read-only member %qD"),
10329 G_("read-only member %qD used as %<asm%> output")),
10330 TREE_OPERAND (arg, 1));
10332 else if (TREE_CODE (arg) == VAR_DECL)
10333 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10334 G_("increment of read-only variable %qD"),
10335 G_("decrement of read-only variable %qD"),
10336 G_("read-only variable %qD used as %<asm%> output")),
10337 arg);
10338 else if (TREE_CODE (arg) == PARM_DECL)
10339 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10340 G_("increment of read-only parameter %qD"),
10341 G_("decrement of read-only parameter %qD"),
10342 G_("read-only parameter %qD use as %<asm%> output")),
10343 arg);
10344 else if (TREE_CODE (arg) == RESULT_DECL)
10346 gcc_assert (c_dialect_cxx ());
10347 error_at (loc, READONLY_MSG (G_("assignment of "
10348 "read-only named return value %qD"),
10349 G_("increment of "
10350 "read-only named return value %qD"),
10351 G_("decrement of "
10352 "read-only named return value %qD"),
10353 G_("read-only named return value %qD "
10354 "used as %<asm%>output")),
10355 arg);
10357 else if (TREE_CODE (arg) == FUNCTION_DECL)
10358 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10359 G_("increment of function %qD"),
10360 G_("decrement of function %qD"),
10361 G_("function %qD used as %<asm%> output")),
10362 arg);
10363 else
10364 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10365 G_("increment of read-only location %qE"),
10366 G_("decrement of read-only location %qE"),
10367 G_("read-only location %qE used as %<asm%> output")),
10368 arg);
10371 /* Print an error message for an invalid lvalue. USE says
10372 how the lvalue is being used and so selects the error message. LOC
10373 is the location for the error. */
10375 void
10376 lvalue_error (location_t loc, enum lvalue_use use)
10378 switch (use)
10380 case lv_assign:
10381 error_at (loc, "lvalue required as left operand of assignment");
10382 break;
10383 case lv_increment:
10384 error_at (loc, "lvalue required as increment operand");
10385 break;
10386 case lv_decrement:
10387 error_at (loc, "lvalue required as decrement operand");
10388 break;
10389 case lv_addressof:
10390 error_at (loc, "lvalue required as unary %<&%> operand");
10391 break;
10392 case lv_asm:
10393 error_at (loc, "lvalue required in asm statement");
10394 break;
10395 default:
10396 gcc_unreachable ();
10400 /* Print an error message for an invalid indirection of type TYPE.
10401 ERRSTRING is the name of the operator for the indirection. */
10403 void
10404 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10406 switch (errstring)
10408 case RO_NULL:
10409 gcc_assert (c_dialect_cxx ());
10410 error_at (loc, "invalid type argument (have %qT)", type);
10411 break;
10412 case RO_ARRAY_INDEXING:
10413 error_at (loc,
10414 "invalid type argument of array indexing (have %qT)",
10415 type);
10416 break;
10417 case RO_UNARY_STAR:
10418 error_at (loc,
10419 "invalid type argument of unary %<*%> (have %qT)",
10420 type);
10421 break;
10422 case RO_ARROW:
10423 error_at (loc,
10424 "invalid type argument of %<->%> (have %qT)",
10425 type);
10426 break;
10427 case RO_ARROW_STAR:
10428 error_at (loc,
10429 "invalid type argument of %<->*%> (have %qT)",
10430 type);
10431 break;
10432 case RO_IMPLICIT_CONVERSION:
10433 error_at (loc,
10434 "invalid type argument of implicit conversion (have %qT)",
10435 type);
10436 break;
10437 default:
10438 gcc_unreachable ();
10442 /* *PTYPE is an incomplete array. Complete it with a domain based on
10443 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10444 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10445 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10448 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10450 tree maxindex, type, main_type, elt, unqual_elt;
10451 int failure = 0, quals;
10452 hashval_t hashcode = 0;
10453 bool overflow_p = false;
10455 maxindex = size_zero_node;
10456 if (initial_value)
10458 if (TREE_CODE (initial_value) == STRING_CST)
10460 int eltsize
10461 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10462 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10464 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10466 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10468 if (vec_safe_is_empty (v))
10470 if (pedantic)
10471 failure = 3;
10472 maxindex = ssize_int (-1);
10474 else
10476 tree curindex;
10477 unsigned HOST_WIDE_INT cnt;
10478 constructor_elt *ce;
10479 bool fold_p = false;
10481 if ((*v)[0].index)
10482 maxindex = (*v)[0].index, fold_p = true;
10484 curindex = maxindex;
10486 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10488 bool curfold_p = false;
10489 if (ce->index)
10490 curindex = ce->index, curfold_p = true;
10491 else
10493 if (fold_p)
10495 /* Since we treat size types now as ordinary
10496 unsigned types, we need an explicit overflow
10497 check. */
10498 tree orig = curindex;
10499 curindex = fold_convert (sizetype, curindex);
10500 overflow_p |= tree_int_cst_lt (curindex, orig);
10502 curindex = size_binop (PLUS_EXPR, curindex,
10503 size_one_node);
10505 if (tree_int_cst_lt (maxindex, curindex))
10506 maxindex = curindex, fold_p = curfold_p;
10508 if (fold_p)
10510 tree orig = maxindex;
10511 maxindex = fold_convert (sizetype, maxindex);
10512 overflow_p |= tree_int_cst_lt (maxindex, orig);
10516 else
10518 /* Make an error message unless that happened already. */
10519 if (initial_value != error_mark_node)
10520 failure = 1;
10523 else
10525 failure = 2;
10526 if (!do_default)
10527 return failure;
10530 type = *ptype;
10531 /* Force an indefinite layout factor. */
10532 if (upc_shared_type_p (type))
10533 type = c_build_qualified_type_1 (type, TYPE_QUAL_UPC_SHARED,
10534 size_zero_node);
10535 elt = TREE_TYPE (type);
10536 quals = TYPE_QUALS (strip_array_types (elt));
10537 if (quals == 0)
10538 unqual_elt = elt;
10539 else
10540 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10542 /* Using build_distinct_type_copy and modifying things afterward instead
10543 of using build_array_type to create a new type preserves all of the
10544 TYPE_LANG_FLAG_? bits that the front end may have set. */
10545 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10546 TREE_TYPE (main_type) = unqual_elt;
10547 TYPE_DOMAIN (main_type)
10548 = build_range_type (TREE_TYPE (maxindex),
10549 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10550 layout_type (main_type);
10552 /* Make sure we have the canonical MAIN_TYPE. */
10553 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10554 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10555 hashcode);
10556 main_type = type_hash_canon (hashcode, main_type);
10558 /* Fix the canonical type. */
10559 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10560 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10561 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10562 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10563 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10564 != TYPE_DOMAIN (main_type)))
10565 TYPE_CANONICAL (main_type)
10566 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10567 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10568 else
10569 TYPE_CANONICAL (main_type) = main_type;
10571 if (quals == 0)
10572 type = main_type;
10573 else
10574 type = c_build_qualified_type (main_type, quals);
10576 if (COMPLETE_TYPE_P (type)
10577 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10578 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10580 error ("size of array is too large");
10581 /* If we proceed with the array type as it is, we'll eventually
10582 crash in tree_to_[su]hwi(). */
10583 type = error_mark_node;
10586 *ptype = type;
10587 return failure;
10590 /* Like c_mark_addressable but don't check register qualifier. */
10591 void
10592 c_common_mark_addressable_vec (tree t)
10594 while (handled_component_p (t))
10595 t = TREE_OPERAND (t, 0);
10596 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10597 return;
10598 TREE_ADDRESSABLE (t) = 1;
10603 /* Used to help initialize the builtin-types.def table. When a type of
10604 the correct size doesn't exist, use error_mark_node instead of NULL.
10605 The later results in segfaults even when a decl using the type doesn't
10606 get invoked. */
10608 tree
10609 builtin_type_for_size (int size, bool unsignedp)
10611 tree type = c_common_type_for_size (size, unsignedp);
10612 return type ? type : error_mark_node;
10615 /* A helper function for resolve_overloaded_builtin in resolving the
10616 overloaded __sync_ builtins. Returns a positive power of 2 if the
10617 first operand of PARAMS is a pointer to a supported data type.
10618 Returns 0 if an error is encountered. */
10620 static int
10621 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10623 tree type;
10624 int size;
10626 if (!params)
10628 error ("too few arguments to function %qE", function);
10629 return 0;
10632 type = TREE_TYPE ((*params)[0]);
10633 if (TREE_CODE (type) == ARRAY_TYPE)
10635 /* Force array-to-pointer decay for C++. */
10636 gcc_assert (c_dialect_cxx());
10637 (*params)[0] = default_conversion ((*params)[0]);
10638 type = TREE_TYPE ((*params)[0]);
10640 if (TREE_CODE (type) != POINTER_TYPE)
10641 goto incompatible;
10643 type = TREE_TYPE (type);
10644 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10645 goto incompatible;
10647 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10648 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10649 return size;
10651 incompatible:
10652 error ("incompatible type for argument %d of %qE", 1, function);
10653 return 0;
10656 /* A helper function for resolve_overloaded_builtin. Adds casts to
10657 PARAMS to make arguments match up with those of FUNCTION. Drops
10658 the variadic arguments at the end. Returns false if some error
10659 was encountered; true on success. */
10661 static bool
10662 sync_resolve_params (location_t loc, tree orig_function, tree function,
10663 vec<tree, va_gc> *params, bool orig_format)
10665 function_args_iterator iter;
10666 tree ptype;
10667 unsigned int parmnum;
10669 function_args_iter_init (&iter, TREE_TYPE (function));
10670 /* We've declared the implementation functions to use "volatile void *"
10671 as the pointer parameter, so we shouldn't get any complaints from the
10672 call to check_function_arguments what ever type the user used. */
10673 function_args_iter_next (&iter);
10674 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10675 ptype = TYPE_MAIN_VARIANT (ptype);
10677 /* For the rest of the values, we need to cast these to FTYPE, so that we
10678 don't get warnings for passing pointer types, etc. */
10679 parmnum = 0;
10680 while (1)
10682 tree val, arg_type;
10684 arg_type = function_args_iter_cond (&iter);
10685 /* XXX void_type_node belies the abstraction. */
10686 if (arg_type == void_type_node)
10687 break;
10689 ++parmnum;
10690 if (params->length () <= parmnum)
10692 error_at (loc, "too few arguments to function %qE", orig_function);
10693 return false;
10696 /* Only convert parameters if arg_type is unsigned integer type with
10697 new format sync routines, i.e. don't attempt to convert pointer
10698 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10699 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10700 kinds). */
10701 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10703 /* Ideally for the first conversion we'd use convert_for_assignment
10704 so that we get warnings for anything that doesn't match the pointer
10705 type. This isn't portable across the C and C++ front ends atm. */
10706 val = (*params)[parmnum];
10707 val = convert (ptype, val);
10708 val = convert (arg_type, val);
10709 (*params)[parmnum] = val;
10712 function_args_iter_next (&iter);
10715 /* __atomic routines are not variadic. */
10716 if (!orig_format && params->length () != parmnum + 1)
10718 error_at (loc, "too many arguments to function %qE", orig_function);
10719 return false;
10722 /* The definition of these primitives is variadic, with the remaining
10723 being "an optional list of variables protected by the memory barrier".
10724 No clue what that's supposed to mean, precisely, but we consider all
10725 call-clobbered variables to be protected so we're safe. */
10726 params->truncate (parmnum + 1);
10728 return true;
10731 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10732 RESULT to make it match the type of the first pointer argument in
10733 PARAMS. */
10735 static tree
10736 sync_resolve_return (tree first_param, tree result, bool orig_format)
10738 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10739 tree rtype = TREE_TYPE (result);
10740 ptype = TYPE_MAIN_VARIANT (ptype);
10742 /* New format doesn't require casting unless the types are the same size. */
10743 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10744 return convert (ptype, result);
10745 else
10746 return result;
10749 /* This function verifies the PARAMS to generic atomic FUNCTION.
10750 It returns the size if all the parameters are the same size, otherwise
10751 0 is returned if the parameters are invalid. */
10753 static int
10754 get_atomic_generic_size (location_t loc, tree function,
10755 vec<tree, va_gc> *params)
10757 unsigned int n_param;
10758 unsigned int n_model;
10759 unsigned int x;
10760 int size_0;
10761 tree type_0;
10763 /* Determine the parameter makeup. */
10764 switch (DECL_FUNCTION_CODE (function))
10766 case BUILT_IN_ATOMIC_EXCHANGE:
10767 n_param = 4;
10768 n_model = 1;
10769 break;
10770 case BUILT_IN_ATOMIC_LOAD:
10771 case BUILT_IN_ATOMIC_STORE:
10772 n_param = 3;
10773 n_model = 1;
10774 break;
10775 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10776 n_param = 6;
10777 n_model = 2;
10778 break;
10779 default:
10780 gcc_unreachable ();
10783 if (vec_safe_length (params) != n_param)
10785 error_at (loc, "incorrect number of arguments to function %qE", function);
10786 return 0;
10789 /* Get type of first parameter, and determine its size. */
10790 type_0 = TREE_TYPE ((*params)[0]);
10791 if (TREE_CODE (type_0) == ARRAY_TYPE)
10793 /* Force array-to-pointer decay for C++. */
10794 gcc_assert (c_dialect_cxx());
10795 (*params)[0] = default_conversion ((*params)[0]);
10796 type_0 = TREE_TYPE ((*params)[0]);
10798 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10800 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10801 function);
10802 return 0;
10805 /* Types must be compile time constant sizes. */
10806 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10808 error_at (loc,
10809 "argument 1 of %qE must be a pointer to a constant size type",
10810 function);
10811 return 0;
10814 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10816 /* Zero size objects are not allowed. */
10817 if (size_0 == 0)
10819 error_at (loc,
10820 "argument 1 of %qE must be a pointer to a nonzero size object",
10821 function);
10822 return 0;
10825 /* Check each other parameter is a pointer and the same size. */
10826 for (x = 0; x < n_param - n_model; x++)
10828 int size;
10829 tree type = TREE_TYPE ((*params)[x]);
10830 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10831 if (n_param == 6 && x == 3)
10832 continue;
10833 if (!POINTER_TYPE_P (type))
10835 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10836 function);
10837 return 0;
10839 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10840 size = type_size ? tree_to_uhwi (type_size) : 0;
10841 if (size != size_0)
10843 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10844 function);
10845 return 0;
10849 /* Check memory model parameters for validity. */
10850 for (x = n_param - n_model ; x < n_param; x++)
10852 tree p = (*params)[x];
10853 if (TREE_CODE (p) == INTEGER_CST)
10855 int i = tree_to_uhwi (p);
10856 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10858 warning_at (loc, OPT_Winvalid_memory_model,
10859 "invalid memory model argument %d of %qE", x + 1,
10860 function);
10863 else
10864 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10866 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10867 function);
10868 return 0;
10872 return size_0;
10876 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10877 at the beginning of the parameter list PARAMS representing the size of the
10878 objects. This is to match the library ABI requirement. LOC is the location
10879 of the function call.
10880 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10881 returned to allow the external call to be constructed. */
10883 static tree
10884 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10885 vec<tree, va_gc> *params)
10887 tree size_node;
10889 /* Insert a SIZE_T parameter as the first param. If there isn't
10890 enough space, allocate a new vector and recursively re-build with that. */
10891 if (!params->space (1))
10893 unsigned int z, len;
10894 vec<tree, va_gc> *v;
10895 tree f;
10897 len = params->length ();
10898 vec_alloc (v, len + 1);
10899 v->quick_push (build_int_cst (size_type_node, n));
10900 for (z = 0; z < len; z++)
10901 v->quick_push ((*params)[z]);
10902 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10903 vec_free (v);
10904 return f;
10907 /* Add the size parameter and leave as a function call for processing. */
10908 size_node = build_int_cst (size_type_node, n);
10909 params->quick_insert (0, size_node);
10910 return NULL_TREE;
10914 /* Return whether atomic operations for naturally aligned N-byte
10915 arguments are supported, whether inline or through libatomic. */
10916 static bool
10917 atomic_size_supported_p (int n)
10919 switch (n)
10921 case 1:
10922 case 2:
10923 case 4:
10924 case 8:
10925 return true;
10927 case 16:
10928 return targetm.scalar_mode_supported_p (TImode);
10930 default:
10931 return false;
10935 /* This will process an __atomic_exchange function call, determine whether it
10936 needs to be mapped to the _N variation, or turned into a library call.
10937 LOC is the location of the builtin call.
10938 FUNCTION is the DECL that has been invoked;
10939 PARAMS is the argument list for the call. The return value is non-null
10940 TRUE is returned if it is translated into the proper format for a call to the
10941 external library, and NEW_RETURN is set the tree for that function.
10942 FALSE is returned if processing for the _N variation is required, and
10943 NEW_RETURN is set to the the return value the result is copied into. */
10944 static bool
10945 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10946 vec<tree, va_gc> *params, tree *new_return)
10948 tree p0, p1, p2, p3;
10949 tree I_type, I_type_ptr;
10950 int n = get_atomic_generic_size (loc, function, params);
10952 /* Size of 0 is an error condition. */
10953 if (n == 0)
10955 *new_return = error_mark_node;
10956 return true;
10959 /* If not a lock-free size, change to the library generic format. */
10960 if (!atomic_size_supported_p (n))
10962 *new_return = add_atomic_size_parameter (n, loc, function, params);
10963 return true;
10966 /* Otherwise there is a lockfree match, transform the call from:
10967 void fn(T* mem, T* desired, T* return, model)
10968 into
10969 *return = (T) (fn (In* mem, (In) *desired, model)) */
10971 p0 = (*params)[0];
10972 p1 = (*params)[1];
10973 p2 = (*params)[2];
10974 p3 = (*params)[3];
10976 /* Create pointer to appropriate size. */
10977 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10978 I_type_ptr = build_pointer_type (I_type);
10980 /* Convert object pointer to required type. */
10981 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10982 (*params)[0] = p0;
10983 /* Convert new value to required type, and dereference it. */
10984 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10985 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10986 (*params)[1] = p1;
10988 /* Move memory model to the 3rd position, and end param list. */
10989 (*params)[2] = p3;
10990 params->truncate (3);
10992 /* Convert return pointer and dereference it for later assignment. */
10993 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10995 return false;
10999 /* This will process an __atomic_compare_exchange function call, determine
11000 whether it needs to be mapped to the _N variation, or turned into a lib call.
11001 LOC is the location of the builtin call.
11002 FUNCTION is the DECL that has been invoked;
11003 PARAMS is the argument list for the call. The return value is non-null
11004 TRUE is returned if it is translated into the proper format for a call to the
11005 external library, and NEW_RETURN is set the tree for that function.
11006 FALSE is returned if processing for the _N variation is required. */
11008 static bool
11009 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11010 vec<tree, va_gc> *params,
11011 tree *new_return)
11013 tree p0, p1, p2;
11014 tree I_type, I_type_ptr;
11015 int n = get_atomic_generic_size (loc, function, params);
11017 /* Size of 0 is an error condition. */
11018 if (n == 0)
11020 *new_return = error_mark_node;
11021 return true;
11024 /* If not a lock-free size, change to the library generic format. */
11025 if (!atomic_size_supported_p (n))
11027 /* The library generic format does not have the weak parameter, so
11028 remove it from the param list. Since a parameter has been removed,
11029 we can be sure that there is room for the SIZE_T parameter, meaning
11030 there will not be a recursive rebuilding of the parameter list, so
11031 there is no danger this will be done twice. */
11032 if (n > 0)
11034 (*params)[3] = (*params)[4];
11035 (*params)[4] = (*params)[5];
11036 params->truncate (5);
11038 *new_return = add_atomic_size_parameter (n, loc, function, params);
11039 return true;
11042 /* Otherwise, there is a match, so the call needs to be transformed from:
11043 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11044 into
11045 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11047 p0 = (*params)[0];
11048 p1 = (*params)[1];
11049 p2 = (*params)[2];
11051 /* Create pointer to appropriate size. */
11052 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11053 I_type_ptr = build_pointer_type (I_type);
11055 /* Convert object pointer to required type. */
11056 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11057 (*params)[0] = p0;
11059 /* Convert expected pointer to required type. */
11060 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11061 (*params)[1] = p1;
11063 /* Convert desired value to required type, and dereference it. */
11064 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11065 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11066 (*params)[2] = p2;
11068 /* The rest of the parameters are fine. NULL means no special return value
11069 processing.*/
11070 *new_return = NULL;
11071 return false;
11075 /* This will process an __atomic_load function call, determine whether it
11076 needs to be mapped to the _N variation, or turned into a library call.
11077 LOC is the location of the builtin call.
11078 FUNCTION is the DECL that has been invoked;
11079 PARAMS is the argument list for the call. The return value is non-null
11080 TRUE is returned if it is translated into the proper format for a call to the
11081 external library, and NEW_RETURN is set the tree for that function.
11082 FALSE is returned if processing for the _N variation is required, and
11083 NEW_RETURN is set to the the return value the result is copied into. */
11085 static bool
11086 resolve_overloaded_atomic_load (location_t loc, tree function,
11087 vec<tree, va_gc> *params, tree *new_return)
11089 tree p0, p1, p2;
11090 tree I_type, I_type_ptr;
11091 int n = get_atomic_generic_size (loc, function, params);
11093 /* Size of 0 is an error condition. */
11094 if (n == 0)
11096 *new_return = error_mark_node;
11097 return true;
11100 /* If not a lock-free size, change to the library generic format. */
11101 if (!atomic_size_supported_p (n))
11103 *new_return = add_atomic_size_parameter (n, loc, function, params);
11104 return true;
11107 /* Otherwise, there is a match, so the call needs to be transformed from:
11108 void fn(T* mem, T* return, model)
11109 into
11110 *return = (T) (fn ((In *) mem, model)) */
11112 p0 = (*params)[0];
11113 p1 = (*params)[1];
11114 p2 = (*params)[2];
11116 /* Create pointer to appropriate size. */
11117 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11118 I_type_ptr = build_pointer_type (I_type);
11120 /* Convert object pointer to required type. */
11121 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11122 (*params)[0] = p0;
11124 /* Move memory model to the 2nd position, and end param list. */
11125 (*params)[1] = p2;
11126 params->truncate (2);
11128 /* Convert return pointer and dereference it for later assignment. */
11129 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11131 return false;
11135 /* This will process an __atomic_store function call, determine whether it
11136 needs to be mapped to the _N variation, or turned into a library call.
11137 LOC is the location of the builtin call.
11138 FUNCTION is the DECL that has been invoked;
11139 PARAMS is the argument list for the call. The return value is non-null
11140 TRUE is returned if it is translated into the proper format for a call to the
11141 external library, and NEW_RETURN is set the tree for that function.
11142 FALSE is returned if processing for the _N variation is required, and
11143 NEW_RETURN is set to the the return value the result is copied into. */
11145 static bool
11146 resolve_overloaded_atomic_store (location_t loc, tree function,
11147 vec<tree, va_gc> *params, tree *new_return)
11149 tree p0, p1;
11150 tree I_type, I_type_ptr;
11151 int n = get_atomic_generic_size (loc, function, params);
11153 /* Size of 0 is an error condition. */
11154 if (n == 0)
11156 *new_return = error_mark_node;
11157 return true;
11160 /* If not a lock-free size, change to the library generic format. */
11161 if (!atomic_size_supported_p (n))
11163 *new_return = add_atomic_size_parameter (n, loc, function, params);
11164 return true;
11167 /* Otherwise, there is a match, so the call needs to be transformed from:
11168 void fn(T* mem, T* value, model)
11169 into
11170 fn ((In *) mem, (In) *value, model) */
11172 p0 = (*params)[0];
11173 p1 = (*params)[1];
11175 /* Create pointer to appropriate size. */
11176 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11177 I_type_ptr = build_pointer_type (I_type);
11179 /* Convert object pointer to required type. */
11180 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11181 (*params)[0] = p0;
11183 /* Convert new value to required type, and dereference it. */
11184 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11185 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11186 (*params)[1] = p1;
11188 /* The memory model is in the right spot already. Return is void. */
11189 *new_return = NULL_TREE;
11191 return false;
11195 /* Some builtin functions are placeholders for other expressions. This
11196 function should be called immediately after parsing the call expression
11197 before surrounding code has committed to the type of the expression.
11199 LOC is the location of the builtin call.
11201 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11202 PARAMS is the argument list for the call. The return value is non-null
11203 when expansion is complete, and null if normal processing should
11204 continue. */
11206 tree
11207 resolve_overloaded_builtin (location_t loc, tree function,
11208 vec<tree, va_gc> *params)
11210 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11211 bool orig_format = true;
11212 tree new_return = NULL_TREE;
11214 switch (DECL_BUILT_IN_CLASS (function))
11216 case BUILT_IN_NORMAL:
11217 break;
11218 case BUILT_IN_MD:
11219 if (targetm.resolve_overloaded_builtin)
11220 return targetm.resolve_overloaded_builtin (loc, function, params);
11221 else
11222 return NULL_TREE;
11223 default:
11224 return NULL_TREE;
11227 /* Handle BUILT_IN_NORMAL here. */
11228 switch (orig_code)
11230 case BUILT_IN_ATOMIC_EXCHANGE:
11231 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11232 case BUILT_IN_ATOMIC_LOAD:
11233 case BUILT_IN_ATOMIC_STORE:
11235 /* Handle these 4 together so that they can fall through to the next
11236 case if the call is transformed to an _N variant. */
11237 switch (orig_code)
11239 case BUILT_IN_ATOMIC_EXCHANGE:
11241 if (resolve_overloaded_atomic_exchange (loc, function, params,
11242 &new_return))
11243 return new_return;
11244 /* Change to the _N variant. */
11245 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11246 break;
11249 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11251 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11252 params,
11253 &new_return))
11254 return new_return;
11255 /* Change to the _N variant. */
11256 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11257 break;
11259 case BUILT_IN_ATOMIC_LOAD:
11261 if (resolve_overloaded_atomic_load (loc, function, params,
11262 &new_return))
11263 return new_return;
11264 /* Change to the _N variant. */
11265 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11266 break;
11268 case BUILT_IN_ATOMIC_STORE:
11270 if (resolve_overloaded_atomic_store (loc, function, params,
11271 &new_return))
11272 return new_return;
11273 /* Change to the _N variant. */
11274 orig_code = BUILT_IN_ATOMIC_STORE_N;
11275 break;
11277 default:
11278 gcc_unreachable ();
11280 /* Fallthrough to the normal processing. */
11282 case BUILT_IN_ATOMIC_EXCHANGE_N:
11283 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11284 case BUILT_IN_ATOMIC_LOAD_N:
11285 case BUILT_IN_ATOMIC_STORE_N:
11286 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11287 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11288 case BUILT_IN_ATOMIC_AND_FETCH_N:
11289 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11290 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11291 case BUILT_IN_ATOMIC_OR_FETCH_N:
11292 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11293 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11294 case BUILT_IN_ATOMIC_FETCH_AND_N:
11295 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11296 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11297 case BUILT_IN_ATOMIC_FETCH_OR_N:
11299 orig_format = false;
11300 /* Fallthru for parameter processing. */
11302 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11303 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11304 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11305 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11306 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11307 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11308 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11309 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11310 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11311 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11312 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11313 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11314 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11315 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11316 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11317 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11319 int n = sync_resolve_size (function, params);
11320 tree new_function, first_param, result;
11321 enum built_in_function fncode;
11323 if (n == 0)
11324 return error_mark_node;
11326 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11327 new_function = builtin_decl_explicit (fncode);
11328 if (!sync_resolve_params (loc, function, new_function, params,
11329 orig_format))
11330 return error_mark_node;
11332 first_param = (*params)[0];
11333 result = build_function_call_vec (loc, vNULL, new_function, params,
11334 NULL);
11335 if (result == error_mark_node)
11336 return result;
11337 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11338 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11339 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11340 result = sync_resolve_return (first_param, result, orig_format);
11342 /* If new_return is set, assign function to that expr and cast the
11343 result to void since the generic interface returned void. */
11344 if (new_return)
11346 /* Cast function result from I{1,2,4,8,16} to the required type. */
11347 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11348 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11349 result);
11350 TREE_SIDE_EFFECTS (result) = 1;
11351 protected_set_expr_location (result, loc);
11352 result = convert (void_type_node, result);
11354 return result;
11357 default:
11358 return NULL_TREE;
11362 /* vector_types_compatible_elements_p is used in type checks of vectors
11363 values used as operands of binary operators. Where it returns true, and
11364 the other checks of the caller succeed (being vector types in he first
11365 place, and matching number of elements), we can just treat the types
11366 as essentially the same.
11367 Contrast with vector_targets_convertible_p, which is used for vector
11368 pointer types, and vector_types_convertible_p, which will allow
11369 language-specific matches under the control of flag_lax_vector_conversions,
11370 and might still require a conversion. */
11371 /* True if vector types T1 and T2 can be inputs to the same binary
11372 operator without conversion.
11373 We don't check the overall vector size here because some of our callers
11374 want to give different error messages when the vectors are compatible
11375 except for the element count. */
11377 bool
11378 vector_types_compatible_elements_p (tree t1, tree t2)
11380 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11381 t1 = TREE_TYPE (t1);
11382 t2 = TREE_TYPE (t2);
11384 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11386 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11387 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11388 || c2 == FIXED_POINT_TYPE));
11390 t1 = c_common_signed_type (t1);
11391 t2 = c_common_signed_type (t2);
11392 /* Equality works here because c_common_signed_type uses
11393 TYPE_MAIN_VARIANT. */
11394 if (t1 == t2)
11395 return true;
11396 if (opaque && c1 == c2
11397 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11398 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11399 return true;
11400 return false;
11403 /* Check for missing format attributes on function pointers. LTYPE is
11404 the new type or left-hand side type. RTYPE is the old type or
11405 right-hand side type. Returns TRUE if LTYPE is missing the desired
11406 attribute. */
11408 bool
11409 check_missing_format_attribute (tree ltype, tree rtype)
11411 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11412 tree ra;
11414 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11415 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11416 break;
11417 if (ra)
11419 tree la;
11420 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11421 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11422 break;
11423 return !la;
11425 else
11426 return false;
11429 /* Subscripting with type char is likely to lose on a machine where
11430 chars are signed. So warn on any machine, but optionally. Don't
11431 warn for unsigned char since that type is safe. Don't warn for
11432 signed char because anyone who uses that must have done so
11433 deliberately. Furthermore, we reduce the false positive load by
11434 warning only for non-constant value of type char. */
11436 void
11437 warn_array_subscript_with_type_char (location_t loc, tree index)
11439 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11440 && TREE_CODE (index) != INTEGER_CST)
11441 warning_at (loc, OPT_Wchar_subscripts,
11442 "array subscript has type %<char%>");
11445 /* Implement -Wparentheses for the unexpected C precedence rules, to
11446 cover cases like x + y << z which readers are likely to
11447 misinterpret. We have seen an expression in which CODE is a binary
11448 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11449 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11450 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11451 expression was not formed using a binary or unary operator, or it
11452 was enclosed in parentheses. */
11454 void
11455 warn_about_parentheses (location_t loc, enum tree_code code,
11456 enum tree_code code_left, tree arg_left,
11457 enum tree_code code_right, tree arg_right)
11459 if (!warn_parentheses)
11460 return;
11462 /* This macro tests that the expression ARG with original tree code
11463 CODE appears to be a boolean expression. or the result of folding a
11464 boolean expression. */
11465 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11466 (truth_value_p (TREE_CODE (ARG)) \
11467 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11468 /* Folding may create 0 or 1 integers from other expressions. */ \
11469 || ((CODE) != INTEGER_CST \
11470 && (integer_onep (ARG) || integer_zerop (ARG))))
11472 switch (code)
11474 case LSHIFT_EXPR:
11475 if (code_left == PLUS_EXPR)
11476 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11477 "suggest parentheses around %<+%> inside %<<<%>");
11478 else if (code_right == PLUS_EXPR)
11479 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11480 "suggest parentheses around %<+%> inside %<<<%>");
11481 else if (code_left == MINUS_EXPR)
11482 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11483 "suggest parentheses around %<-%> inside %<<<%>");
11484 else if (code_right == MINUS_EXPR)
11485 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11486 "suggest parentheses around %<-%> inside %<<<%>");
11487 return;
11489 case RSHIFT_EXPR:
11490 if (code_left == PLUS_EXPR)
11491 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11492 "suggest parentheses around %<+%> inside %<>>%>");
11493 else if (code_right == PLUS_EXPR)
11494 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11495 "suggest parentheses around %<+%> inside %<>>%>");
11496 else if (code_left == MINUS_EXPR)
11497 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11498 "suggest parentheses around %<-%> inside %<>>%>");
11499 else if (code_right == MINUS_EXPR)
11500 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11501 "suggest parentheses around %<-%> inside %<>>%>");
11502 return;
11504 case TRUTH_ORIF_EXPR:
11505 if (code_left == TRUTH_ANDIF_EXPR)
11506 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11507 "suggest parentheses around %<&&%> within %<||%>");
11508 else if (code_right == TRUTH_ANDIF_EXPR)
11509 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11510 "suggest parentheses around %<&&%> within %<||%>");
11511 return;
11513 case BIT_IOR_EXPR:
11514 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11515 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11516 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11517 "suggest parentheses around arithmetic in operand of %<|%>");
11518 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11519 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11520 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11521 "suggest parentheses around arithmetic in operand of %<|%>");
11522 /* Check cases like x|y==z */
11523 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11524 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11525 "suggest parentheses around comparison in operand of %<|%>");
11526 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11527 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11528 "suggest parentheses around comparison in operand of %<|%>");
11529 /* Check cases like !x | y */
11530 else if (code_left == TRUTH_NOT_EXPR
11531 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11532 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11533 "suggest parentheses around operand of "
11534 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11535 return;
11537 case BIT_XOR_EXPR:
11538 if (code_left == BIT_AND_EXPR
11539 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11540 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11541 "suggest parentheses around arithmetic in operand of %<^%>");
11542 else if (code_right == BIT_AND_EXPR
11543 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11544 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11545 "suggest parentheses around arithmetic in operand of %<^%>");
11546 /* Check cases like x^y==z */
11547 else 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 case BIT_AND_EXPR:
11556 if (code_left == PLUS_EXPR)
11557 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11558 "suggest parentheses around %<+%> in operand of %<&%>");
11559 else if (code_right == PLUS_EXPR)
11560 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11561 "suggest parentheses around %<+%> in operand of %<&%>");
11562 else if (code_left == MINUS_EXPR)
11563 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11564 "suggest parentheses around %<-%> in operand of %<&%>");
11565 else if (code_right == MINUS_EXPR)
11566 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11567 "suggest parentheses around %<-%> in operand of %<&%>");
11568 /* Check cases like x&y==z */
11569 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11570 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11571 "suggest parentheses around comparison in operand of %<&%>");
11572 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11573 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11574 "suggest parentheses around comparison in operand of %<&%>");
11575 /* Check cases like !x & y */
11576 else if (code_left == TRUTH_NOT_EXPR
11577 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11578 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11579 "suggest parentheses around operand of "
11580 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11581 return;
11583 case EQ_EXPR:
11584 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11585 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11586 "suggest parentheses around comparison in operand of %<==%>");
11587 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11588 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11589 "suggest parentheses around comparison in operand of %<==%>");
11590 return;
11591 case NE_EXPR:
11592 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11593 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11594 "suggest parentheses around comparison in operand of %<!=%>");
11595 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11596 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11597 "suggest parentheses around comparison in operand of %<!=%>");
11598 return;
11600 default:
11601 if (TREE_CODE_CLASS (code) == tcc_comparison)
11603 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11604 && code_left != NE_EXPR && code_left != EQ_EXPR
11605 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11606 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11607 "comparisons like %<X<=Y<=Z%> do not "
11608 "have their mathematical meaning");
11609 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11610 && code_right != NE_EXPR && code_right != EQ_EXPR
11611 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11612 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11613 "comparisons like %<X<=Y<=Z%> do not "
11614 "have their mathematical meaning");
11616 return;
11618 #undef NOT_A_BOOLEAN_EXPR_P
11621 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11623 void
11624 warn_for_unused_label (tree label)
11626 if (!TREE_USED (label))
11628 if (DECL_INITIAL (label))
11629 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11630 else
11631 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11635 /* Warn for division by zero according to the value of DIVISOR. LOC
11636 is the location of the division operator. */
11638 void
11639 warn_for_div_by_zero (location_t loc, tree divisor)
11641 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11642 about division by zero. Do not issue a warning if DIVISOR has a
11643 floating-point type, since we consider 0.0/0.0 a valid way of
11644 generating a NaN. */
11645 if (c_inhibit_evaluation_warnings == 0
11646 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11647 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11650 /* Subroutine of build_binary_op. Give warnings for comparisons
11651 between signed and unsigned quantities that may fail. Do the
11652 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11653 so that casts will be considered, but default promotions won't
11656 LOCATION is the location of the comparison operator.
11658 The arguments of this function map directly to local variables
11659 of build_binary_op. */
11661 void
11662 warn_for_sign_compare (location_t location,
11663 tree orig_op0, tree orig_op1,
11664 tree op0, tree op1,
11665 tree result_type, enum tree_code resultcode)
11667 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11668 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11669 int unsignedp0, unsignedp1;
11671 /* In C++, check for comparison of different enum types. */
11672 if (c_dialect_cxx()
11673 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11674 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11675 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11676 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11678 warning_at (location,
11679 OPT_Wsign_compare, "comparison between types %qT and %qT",
11680 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11683 /* Do not warn if the comparison is being done in a signed type,
11684 since the signed type will only be chosen if it can represent
11685 all the values of the unsigned type. */
11686 if (!TYPE_UNSIGNED (result_type))
11687 /* OK */;
11688 /* Do not warn if both operands are unsigned. */
11689 else if (op0_signed == op1_signed)
11690 /* OK */;
11691 else
11693 tree sop, uop, base_type;
11694 bool ovf;
11696 if (op0_signed)
11697 sop = orig_op0, uop = orig_op1;
11698 else
11699 sop = orig_op1, uop = orig_op0;
11701 STRIP_TYPE_NOPS (sop);
11702 STRIP_TYPE_NOPS (uop);
11703 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11704 ? TREE_TYPE (result_type) : result_type);
11706 /* Do not warn if the signed quantity is an unsuffixed integer
11707 literal (or some static constant expression involving such
11708 literals or a conditional expression involving such literals)
11709 and it is non-negative. */
11710 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11711 /* OK */;
11712 /* Do not warn if the comparison is an equality operation, the
11713 unsigned quantity is an integral constant, and it would fit
11714 in the result if the result were signed. */
11715 else if (TREE_CODE (uop) == INTEGER_CST
11716 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11717 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11718 /* OK */;
11719 /* In C, do not warn if the unsigned quantity is an enumeration
11720 constant and its maximum value would fit in the result if the
11721 result were signed. */
11722 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11723 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11724 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11725 c_common_signed_type (base_type)))
11726 /* OK */;
11727 else
11728 warning_at (location,
11729 OPT_Wsign_compare,
11730 "comparison between signed and unsigned integer expressions");
11733 /* Warn if two unsigned values are being compared in a size larger
11734 than their original size, and one (and only one) is the result of
11735 a `~' operator. This comparison will always fail.
11737 Also warn if one operand is a constant, and the constant does not
11738 have all bits set that are set in the ~ operand when it is
11739 extended. */
11741 op0 = c_common_get_narrower (op0, &unsignedp0);
11742 op1 = c_common_get_narrower (op1, &unsignedp1);
11744 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11745 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11747 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11748 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11749 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11750 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11752 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11754 tree primop;
11755 HOST_WIDE_INT constant, mask;
11756 int unsignedp;
11757 unsigned int bits;
11759 if (tree_fits_shwi_p (op0))
11761 primop = op1;
11762 unsignedp = unsignedp1;
11763 constant = tree_to_shwi (op0);
11765 else
11767 primop = op0;
11768 unsignedp = unsignedp0;
11769 constant = tree_to_shwi (op1);
11772 bits = TYPE_PRECISION (TREE_TYPE (primop));
11773 if (bits < TYPE_PRECISION (result_type)
11774 && bits < HOST_BITS_PER_LONG && unsignedp)
11776 mask = (~ (HOST_WIDE_INT) 0) << bits;
11777 if ((mask & constant) != mask)
11779 if (constant == 0)
11780 warning_at (location, OPT_Wsign_compare,
11781 "promoted ~unsigned is always non-zero");
11782 else
11783 warning_at (location, OPT_Wsign_compare,
11784 "comparison of promoted ~unsigned with constant");
11788 else if (unsignedp0 && unsignedp1
11789 && (TYPE_PRECISION (TREE_TYPE (op0))
11790 < TYPE_PRECISION (result_type))
11791 && (TYPE_PRECISION (TREE_TYPE (op1))
11792 < TYPE_PRECISION (result_type)))
11793 warning_at (location, OPT_Wsign_compare,
11794 "comparison of promoted ~unsigned with unsigned");
11798 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11799 type via c_common_type. If -Wdouble-promotion is in use, and the
11800 conditions for warning have been met, issue a warning. GMSGID is
11801 the warning message. It must have two %T specifiers for the type
11802 that was converted (generally "float") and the type to which it was
11803 converted (generally "double), respectively. LOC is the location
11804 to which the awrning should refer. */
11806 void
11807 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11808 const char *gmsgid, location_t loc)
11810 tree source_type;
11812 if (!warn_double_promotion)
11813 return;
11814 /* If the conversion will not occur at run-time, there is no need to
11815 warn about it. */
11816 if (c_inhibit_evaluation_warnings)
11817 return;
11818 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11819 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11820 return;
11821 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11822 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11823 source_type = type1;
11824 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11825 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11826 source_type = type2;
11827 else
11828 return;
11829 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11832 /* Setup a TYPE_DECL node as a typedef representation.
11834 X is a TYPE_DECL for a typedef statement. Create a brand new
11835 ..._TYPE node (which will be just a variant of the existing
11836 ..._TYPE node with identical properties) and then install X
11837 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11839 The whole point here is to end up with a situation where each
11840 and every ..._TYPE node the compiler creates will be uniquely
11841 associated with AT MOST one node representing a typedef name.
11842 This way, even though the compiler substitutes corresponding
11843 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11844 early on, later parts of the compiler can always do the reverse
11845 translation and get back the corresponding typedef name. For
11846 example, given:
11848 typedef struct S MY_TYPE;
11849 MY_TYPE object;
11851 Later parts of the compiler might only know that `object' was of
11852 type `struct S' if it were not for code just below. With this
11853 code however, later parts of the compiler see something like:
11855 struct S' == struct S
11856 typedef struct S' MY_TYPE;
11857 struct S' object;
11859 And they can then deduce (from the node for type struct S') that
11860 the original object declaration was:
11862 MY_TYPE object;
11864 Being able to do this is important for proper support of protoize,
11865 and also for generating precise symbolic debugging information
11866 which takes full account of the programmer's (typedef) vocabulary.
11868 Obviously, we don't want to generate a duplicate ..._TYPE node if
11869 the TYPE_DECL node that we are now processing really represents a
11870 standard built-in type. */
11872 void
11873 set_underlying_type (tree x)
11875 if (x == error_mark_node)
11876 return;
11877 if (DECL_IS_BUILTIN (x))
11879 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11880 TYPE_NAME (TREE_TYPE (x)) = x;
11882 else if (TREE_TYPE (x) != error_mark_node
11883 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11885 tree tt = TREE_TYPE (x);
11886 DECL_ORIGINAL_TYPE (x) = tt;
11887 tt = build_variant_type_copy (tt);
11888 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11889 TYPE_NAME (tt) = x;
11890 TREE_USED (tt) = TREE_USED (x);
11891 TREE_TYPE (x) = tt;
11895 /* Record the types used by the current global variable declaration
11896 being parsed, so that we can decide later to emit their debug info.
11897 Those types are in types_used_by_cur_var_decl, and we are going to
11898 store them in the types_used_by_vars_hash hash table.
11899 DECL is the declaration of the global variable that has been parsed. */
11901 void
11902 record_types_used_by_current_var_decl (tree decl)
11904 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11906 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11908 tree type = types_used_by_cur_var_decl->pop ();
11909 types_used_by_var_decl_insert (type, decl);
11913 /* If DECL is a typedef that is declared in the current function,
11914 record it for the purpose of -Wunused-local-typedefs. */
11916 void
11917 record_locally_defined_typedef (tree decl)
11919 struct c_language_function *l;
11921 if (!warn_unused_local_typedefs
11922 || cfun == NULL
11923 /* if this is not a locally defined typedef then we are not
11924 interested. */
11925 || !is_typedef_decl (decl)
11926 || !decl_function_context (decl))
11927 return;
11929 l = (struct c_language_function *) cfun->language;
11930 vec_safe_push (l->local_typedefs, decl);
11933 /* If T is a TYPE_DECL declared locally, mark it as used. */
11935 void
11936 maybe_record_typedef_use (tree t)
11938 if (!is_typedef_decl (t))
11939 return;
11941 TREE_USED (t) = true;
11944 /* Warn if there are some unused locally defined typedefs in the
11945 current function. */
11947 void
11948 maybe_warn_unused_local_typedefs (void)
11950 int i;
11951 tree decl;
11952 /* The number of times we have emitted -Wunused-local-typedefs
11953 warnings. If this is different from errorcount, that means some
11954 unrelated errors have been issued. In which case, we'll avoid
11955 emitting "unused-local-typedefs" warnings. */
11956 static int unused_local_typedefs_warn_count;
11957 struct c_language_function *l;
11959 if (cfun == NULL)
11960 return;
11962 if ((l = (struct c_language_function *) cfun->language) == NULL)
11963 return;
11965 if (warn_unused_local_typedefs
11966 && errorcount == unused_local_typedefs_warn_count)
11968 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11969 if (!TREE_USED (decl))
11970 warning_at (DECL_SOURCE_LOCATION (decl),
11971 OPT_Wunused_local_typedefs,
11972 "typedef %qD locally defined but not used", decl);
11973 unused_local_typedefs_warn_count = errorcount;
11976 vec_free (l->local_typedefs);
11979 /* Warn about boolean expression compared with an integer value different
11980 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11981 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11982 are the operands of the comparison. The caller must ensure that
11983 either operand is a boolean expression. */
11985 void
11986 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11987 tree op1)
11989 if (TREE_CODE_CLASS (code) != tcc_comparison)
11990 return;
11992 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11993 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11994 if (!cst)
11995 return;
11997 if (!integer_zerop (cst) && !integer_onep (cst))
11999 int sign = (TREE_CODE (op0) == INTEGER_CST)
12000 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
12001 if (code == EQ_EXPR
12002 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12003 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12004 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12005 "with boolean expression is always false", cst);
12006 else
12007 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12008 "with boolean expression is always true", cst);
12012 /* The C and C++ parsers both use vectors to hold function arguments.
12013 For efficiency, we keep a cache of unused vectors. This is the
12014 cache. */
12016 typedef vec<tree, va_gc> *tree_gc_vec;
12017 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12019 /* Return a new vector from the cache. If the cache is empty,
12020 allocate a new vector. These vectors are GC'ed, so it is OK if the
12021 pointer is not released.. */
12023 vec<tree, va_gc> *
12024 make_tree_vector (void)
12026 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12027 return tree_vector_cache->pop ();
12028 else
12030 /* Passing 0 to vec::alloc returns NULL, and our callers require
12031 that we always return a non-NULL value. The vector code uses
12032 4 when growing a NULL vector, so we do too. */
12033 vec<tree, va_gc> *v;
12034 vec_alloc (v, 4);
12035 return v;
12039 /* Release a vector of trees back to the cache. */
12041 void
12042 release_tree_vector (vec<tree, va_gc> *vec)
12044 if (vec != NULL)
12046 vec->truncate (0);
12047 vec_safe_push (tree_vector_cache, vec);
12051 /* Get a new tree vector holding a single tree. */
12053 vec<tree, va_gc> *
12054 make_tree_vector_single (tree t)
12056 vec<tree, va_gc> *ret = make_tree_vector ();
12057 ret->quick_push (t);
12058 return ret;
12061 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12063 vec<tree, va_gc> *
12064 make_tree_vector_from_list (tree list)
12066 vec<tree, va_gc> *ret = make_tree_vector ();
12067 for (; list; list = TREE_CHAIN (list))
12068 vec_safe_push (ret, TREE_VALUE (list));
12069 return ret;
12072 /* Get a new tree vector which is a copy of an existing one. */
12074 vec<tree, va_gc> *
12075 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12077 vec<tree, va_gc> *ret;
12078 unsigned int ix;
12079 tree t;
12081 ret = make_tree_vector ();
12082 vec_safe_reserve (ret, vec_safe_length (orig));
12083 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12084 ret->quick_push (t);
12085 return ret;
12088 /* Return true if KEYWORD starts a type specifier. */
12090 bool
12091 keyword_begins_type_specifier (enum rid keyword)
12093 switch (keyword)
12095 case RID_AUTO_TYPE:
12096 case RID_INT:
12097 case RID_CHAR:
12098 case RID_FLOAT:
12099 case RID_DOUBLE:
12100 case RID_VOID:
12101 case RID_UNSIGNED:
12102 case RID_LONG:
12103 case RID_SHORT:
12104 case RID_SIGNED:
12105 case RID_DFLOAT32:
12106 case RID_DFLOAT64:
12107 case RID_DFLOAT128:
12108 case RID_FRACT:
12109 case RID_ACCUM:
12110 case RID_BOOL:
12111 case RID_WCHAR:
12112 case RID_CHAR16:
12113 case RID_CHAR32:
12114 case RID_SAT:
12115 case RID_COMPLEX:
12116 case RID_TYPEOF:
12117 case RID_STRUCT:
12118 case RID_CLASS:
12119 case RID_UNION:
12120 case RID_ENUM:
12121 return true;
12122 default:
12123 if (keyword >= RID_FIRST_INT_N
12124 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12125 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12126 return true;
12127 return false;
12131 /* Return true if KEYWORD names a type qualifier. */
12133 bool
12134 keyword_is_type_qualifier (enum rid keyword)
12136 switch (keyword)
12138 case RID_CONST:
12139 case RID_VOLATILE:
12140 case RID_RESTRICT:
12141 case RID_ATOMIC:
12142 return true;
12143 default:
12144 return false;
12148 /* Return true if KEYWORD names a storage class specifier.
12150 RID_TYPEDEF is not included in this list despite `typedef' being
12151 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12152 such for syntactic convenience only. */
12154 bool
12155 keyword_is_storage_class_specifier (enum rid keyword)
12157 switch (keyword)
12159 case RID_STATIC:
12160 case RID_EXTERN:
12161 case RID_REGISTER:
12162 case RID_AUTO:
12163 case RID_MUTABLE:
12164 case RID_THREAD:
12165 return true;
12166 default:
12167 return false;
12171 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12173 static bool
12174 keyword_is_function_specifier (enum rid keyword)
12176 switch (keyword)
12178 case RID_INLINE:
12179 case RID_NORETURN:
12180 case RID_VIRTUAL:
12181 case RID_EXPLICIT:
12182 return true;
12183 default:
12184 return false;
12188 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12189 declaration-specifier (C99 6.7). */
12191 bool
12192 keyword_is_decl_specifier (enum rid keyword)
12194 if (keyword_is_storage_class_specifier (keyword)
12195 || keyword_is_type_qualifier (keyword)
12196 || keyword_is_function_specifier (keyword))
12197 return true;
12199 switch (keyword)
12201 case RID_TYPEDEF:
12202 case RID_FRIEND:
12203 case RID_CONSTEXPR:
12204 return true;
12205 default:
12206 return false;
12210 /* Initialize language-specific-bits of tree_contains_struct. */
12212 void
12213 c_common_init_ts (void)
12215 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12216 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12217 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12218 MARK_TS_COMMON (UPC_FORALL_STMT);
12219 MARK_TS_COMMON (UPC_SYNC_STMT);
12222 /* Build a user-defined numeric literal out of an integer constant type VALUE
12223 with identifier SUFFIX. */
12225 tree
12226 build_userdef_literal (tree suffix_id, tree value,
12227 enum overflow_type overflow, tree num_string)
12229 tree literal = make_node (USERDEF_LITERAL);
12230 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12231 USERDEF_LITERAL_VALUE (literal) = value;
12232 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12233 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12234 return literal;
12237 /* For vector[index], convert the vector to a
12238 pointer of the underlying type. Return true if the resulting
12239 ARRAY_REF should not be an lvalue. */
12241 bool
12242 convert_vector_to_pointer_for_subscript (location_t loc,
12243 tree *vecp, tree index)
12245 bool ret = false;
12246 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12248 tree type = TREE_TYPE (*vecp);
12249 tree type1;
12251 ret = !lvalue_p (*vecp);
12252 if (TREE_CODE (index) == INTEGER_CST)
12253 if (!tree_fits_uhwi_p (index)
12254 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12255 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12257 if (ret)
12259 tree tmp = create_tmp_var_raw (type);
12260 DECL_SOURCE_LOCATION (tmp) = loc;
12261 *vecp = c_save_expr (*vecp);
12262 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12264 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12265 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12266 *vecp
12267 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12268 *vecp, NULL_TREE, NULL_TREE),
12269 non_const);
12271 else
12272 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12273 NULL_TREE, NULL_TREE);
12274 SET_EXPR_LOCATION (*vecp, loc);
12275 c_common_mark_addressable_vec (tmp);
12277 else
12278 c_common_mark_addressable_vec (*vecp);
12279 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12280 type1 = build_pointer_type (TREE_TYPE (*vecp));
12281 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12282 if (!ref_all
12283 && !DECL_P (*vecp))
12285 /* If the original vector isn't declared may_alias and it
12286 isn't a bare vector look if the subscripting would
12287 alias the vector we subscript, and if not, force ref-all. */
12288 alias_set_type vecset = get_alias_set (*vecp);
12289 alias_set_type sset = get_alias_set (type);
12290 if (!alias_sets_must_conflict_p (sset, vecset)
12291 && !alias_set_subset_of (sset, vecset))
12292 ref_all = true;
12294 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12295 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12296 *vecp = convert (type, *vecp);
12298 return ret;
12301 /* Determine which of the operands, if any, is a scalar that needs to be
12302 converted to a vector, for the range of operations. */
12303 enum stv_conv
12304 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12305 bool complain)
12307 tree type0 = TREE_TYPE (op0);
12308 tree type1 = TREE_TYPE (op1);
12309 bool integer_only_op = false;
12310 enum stv_conv ret = stv_firstarg;
12312 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12313 || TREE_CODE (type1) == VECTOR_TYPE);
12314 switch (code)
12316 /* Most GENERIC binary expressions require homogeneous arguments.
12317 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12318 argument that is a vector and a second one that is a scalar, so
12319 we never return stv_secondarg for them. */
12320 case RSHIFT_EXPR:
12321 case LSHIFT_EXPR:
12322 if (TREE_CODE (type0) == INTEGER_TYPE
12323 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12325 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12327 if (complain)
12328 error_at (loc, "conversion of scalar %qT to vector %qT "
12329 "involves truncation", type0, type1);
12330 return stv_error;
12332 else
12333 return stv_firstarg;
12335 break;
12337 case BIT_IOR_EXPR:
12338 case BIT_XOR_EXPR:
12339 case BIT_AND_EXPR:
12340 integer_only_op = true;
12341 /* ... fall through ... */
12343 case VEC_COND_EXPR:
12345 case PLUS_EXPR:
12346 case MINUS_EXPR:
12347 case MULT_EXPR:
12348 case TRUNC_DIV_EXPR:
12349 case CEIL_DIV_EXPR:
12350 case FLOOR_DIV_EXPR:
12351 case ROUND_DIV_EXPR:
12352 case EXACT_DIV_EXPR:
12353 case TRUNC_MOD_EXPR:
12354 case FLOOR_MOD_EXPR:
12355 case RDIV_EXPR:
12356 case EQ_EXPR:
12357 case NE_EXPR:
12358 case LE_EXPR:
12359 case GE_EXPR:
12360 case LT_EXPR:
12361 case GT_EXPR:
12362 /* What about UNLT_EXPR? */
12363 if (TREE_CODE (type0) == VECTOR_TYPE)
12365 tree tmp;
12366 ret = stv_secondarg;
12367 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12368 tmp = type0; type0 = type1; type1 = tmp;
12369 tmp = op0; op0 = op1; op1 = tmp;
12372 if (TREE_CODE (type0) == INTEGER_TYPE
12373 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12375 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12377 if (complain)
12378 error_at (loc, "conversion of scalar %qT to vector %qT "
12379 "involves truncation", type0, type1);
12380 return stv_error;
12382 return ret;
12384 else if (!integer_only_op
12385 /* Allow integer --> real conversion if safe. */
12386 && (TREE_CODE (type0) == REAL_TYPE
12387 || TREE_CODE (type0) == INTEGER_TYPE)
12388 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12390 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12392 if (complain)
12393 error_at (loc, "conversion of scalar %qT to vector %qT "
12394 "involves truncation", type0, type1);
12395 return stv_error;
12397 return ret;
12399 default:
12400 break;
12403 return stv_nothing;
12406 /* Return true iff ALIGN is an integral constant that is a fundamental
12407 alignment, as defined by [basic.align] in the c++-11
12408 specifications.
12410 That is:
12412 [A fundamental alignment is represented by an alignment less than or
12413 equal to the greatest alignment supported by the implementation
12414 in all contexts, which is equal to
12415 alignof(max_align_t)]. */
12417 bool
12418 cxx_fundamental_alignment_p (unsigned align)
12420 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12421 TYPE_ALIGN (long_double_type_node)));
12424 /* Return true if T is a pointer to a zero-sized aggregate. */
12426 bool
12427 pointer_to_zero_sized_aggr_p (tree t)
12429 if (!POINTER_TYPE_P (t))
12430 return false;
12431 t = TREE_TYPE (t);
12432 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12435 #include "gt-c-family-c-common.h"