Merge trunk version 221103 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
blob24d20cec45458f0e2b4f6dd52fc914329305a23a
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 warning_at (location, OPT_Wlogical_not_parentheses,
1837 "logical not is only applied to the left hand side of "
1838 "comparison");
1841 /* Warn if EXP contains any computations whose results are not used.
1842 Return true if a warning is printed; false otherwise. LOCUS is the
1843 (potential) location of the expression. */
1845 bool
1846 warn_if_unused_value (const_tree exp, location_t locus)
1848 restart:
1849 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1850 return false;
1852 /* Don't warn about void constructs. This includes casting to void,
1853 void function calls, and statement expressions with a final cast
1854 to void. */
1855 if (VOID_TYPE_P (TREE_TYPE (exp)))
1856 return false;
1858 if (EXPR_HAS_LOCATION (exp))
1859 locus = EXPR_LOCATION (exp);
1861 switch (TREE_CODE (exp))
1863 case PREINCREMENT_EXPR:
1864 case POSTINCREMENT_EXPR:
1865 case PREDECREMENT_EXPR:
1866 case POSTDECREMENT_EXPR:
1867 case MODIFY_EXPR:
1868 case INIT_EXPR:
1869 case TARGET_EXPR:
1870 case CALL_EXPR:
1871 case TRY_CATCH_EXPR:
1872 case WITH_CLEANUP_EXPR:
1873 case EXIT_EXPR:
1874 case VA_ARG_EXPR:
1875 return false;
1877 case BIND_EXPR:
1878 /* For a binding, warn if no side effect within it. */
1879 exp = BIND_EXPR_BODY (exp);
1880 goto restart;
1882 case SAVE_EXPR:
1883 case NON_LVALUE_EXPR:
1884 case NOP_EXPR:
1885 exp = TREE_OPERAND (exp, 0);
1886 goto restart;
1888 case TRUTH_ORIF_EXPR:
1889 case TRUTH_ANDIF_EXPR:
1890 /* In && or ||, warn if 2nd operand has no side effect. */
1891 exp = TREE_OPERAND (exp, 1);
1892 goto restart;
1894 case COMPOUND_EXPR:
1895 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1896 return true;
1897 /* Let people do `(foo (), 0)' without a warning. */
1898 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1899 return false;
1900 exp = TREE_OPERAND (exp, 1);
1901 goto restart;
1903 case COND_EXPR:
1904 /* If this is an expression with side effects, don't warn; this
1905 case commonly appears in macro expansions. */
1906 if (TREE_SIDE_EFFECTS (exp))
1907 return false;
1908 goto warn;
1910 case INDIRECT_REF:
1911 /* Don't warn about automatic dereferencing of references, since
1912 the user cannot control it. */
1913 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1915 exp = TREE_OPERAND (exp, 0);
1916 goto restart;
1918 /* Fall through. */
1920 default:
1921 /* Referencing a volatile value is a side effect, so don't warn. */
1922 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1923 && TREE_THIS_VOLATILE (exp))
1924 return false;
1926 /* If this is an expression which has no operands, there is no value
1927 to be unused. There are no such language-independent codes,
1928 but front ends may define such. */
1929 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1930 return false;
1932 warn:
1933 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1938 /* Print a warning about casts that might indicate violation
1939 of strict aliasing rules if -Wstrict-aliasing is used and
1940 strict aliasing mode is in effect. OTYPE is the original
1941 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1943 bool
1944 strict_aliasing_warning (tree otype, tree type, tree expr)
1946 /* Strip pointer conversion chains and get to the correct original type. */
1947 STRIP_NOPS (expr);
1948 otype = TREE_TYPE (expr);
1950 if (!(flag_strict_aliasing
1951 && POINTER_TYPE_P (type)
1952 && POINTER_TYPE_P (otype)
1953 && !VOID_TYPE_P (TREE_TYPE (type)))
1954 /* If the type we are casting to is a ref-all pointer
1955 dereferencing it is always valid. */
1956 || TYPE_REF_CAN_ALIAS_ALL (type))
1957 return false;
1959 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1960 && (DECL_P (TREE_OPERAND (expr, 0))
1961 || handled_component_p (TREE_OPERAND (expr, 0))))
1963 /* Casting the address of an object to non void pointer. Warn
1964 if the cast breaks type based aliasing. */
1965 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1967 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1968 "might break strict-aliasing rules");
1969 return true;
1971 else
1973 /* warn_strict_aliasing >= 3. This includes the default (3).
1974 Only warn if the cast is dereferenced immediately. */
1975 alias_set_type set1 =
1976 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1977 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1979 if (set1 != set2 && set2 != 0
1980 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1982 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1983 "pointer will break strict-aliasing rules");
1984 return true;
1986 else if (warn_strict_aliasing == 2
1987 && !alias_sets_must_conflict_p (set1, set2))
1989 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1990 "pointer might break strict-aliasing rules");
1991 return true;
1995 else
1996 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1998 /* At this level, warn for any conversions, even if an address is
1999 not taken in the same statement. This will likely produce many
2000 false positives, but could be useful to pinpoint problems that
2001 are not revealed at higher levels. */
2002 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2003 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2004 if (!COMPLETE_TYPE_P (type)
2005 || !alias_sets_must_conflict_p (set1, set2))
2007 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2008 "pointer might break strict-aliasing rules");
2009 return true;
2013 return false;
2016 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2017 sizeof as last operand of certain builtins. */
2019 void
2020 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
2021 vec<tree, va_gc> *params, tree *sizeof_arg,
2022 bool (*comp_types) (tree, tree))
2024 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
2025 bool strop = false, cmp = false;
2026 unsigned int idx = ~0;
2027 location_t loc;
2029 if (TREE_CODE (callee) != FUNCTION_DECL
2030 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2031 || vec_safe_length (params) <= 1)
2032 return;
2034 switch (DECL_FUNCTION_CODE (callee))
2036 case BUILT_IN_STRNCMP:
2037 case BUILT_IN_STRNCASECMP:
2038 cmp = true;
2039 /* FALLTHRU */
2040 case BUILT_IN_STRNCPY:
2041 case BUILT_IN_STRNCPY_CHK:
2042 case BUILT_IN_STRNCAT:
2043 case BUILT_IN_STRNCAT_CHK:
2044 case BUILT_IN_STPNCPY:
2045 case BUILT_IN_STPNCPY_CHK:
2046 strop = true;
2047 /* FALLTHRU */
2048 case BUILT_IN_MEMCPY:
2049 case BUILT_IN_MEMCPY_CHK:
2050 case BUILT_IN_MEMMOVE:
2051 case BUILT_IN_MEMMOVE_CHK:
2052 if (params->length () < 3)
2053 return;
2054 src = (*params)[1];
2055 dest = (*params)[0];
2056 idx = 2;
2057 break;
2058 case BUILT_IN_BCOPY:
2059 if (params->length () < 3)
2060 return;
2061 src = (*params)[0];
2062 dest = (*params)[1];
2063 idx = 2;
2064 break;
2065 case BUILT_IN_MEMCMP:
2066 case BUILT_IN_BCMP:
2067 if (params->length () < 3)
2068 return;
2069 src = (*params)[1];
2070 dest = (*params)[0];
2071 idx = 2;
2072 cmp = true;
2073 break;
2074 case BUILT_IN_MEMSET:
2075 case BUILT_IN_MEMSET_CHK:
2076 if (params->length () < 3)
2077 return;
2078 dest = (*params)[0];
2079 idx = 2;
2080 break;
2081 case BUILT_IN_BZERO:
2082 dest = (*params)[0];
2083 idx = 1;
2084 break;
2085 case BUILT_IN_STRNDUP:
2086 src = (*params)[0];
2087 strop = true;
2088 idx = 1;
2089 break;
2090 case BUILT_IN_MEMCHR:
2091 if (params->length () < 3)
2092 return;
2093 src = (*params)[0];
2094 idx = 2;
2095 break;
2096 case BUILT_IN_SNPRINTF:
2097 case BUILT_IN_SNPRINTF_CHK:
2098 case BUILT_IN_VSNPRINTF:
2099 case BUILT_IN_VSNPRINTF_CHK:
2100 dest = (*params)[0];
2101 idx = 1;
2102 strop = true;
2103 break;
2104 default:
2105 break;
2108 if (idx >= 3)
2109 return;
2111 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2112 return;
2114 type = TYPE_P (sizeof_arg[idx])
2115 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2116 if (!POINTER_TYPE_P (type))
2117 return;
2119 if (dest
2120 && (tem = tree_strip_nop_conversions (dest))
2121 && POINTER_TYPE_P (TREE_TYPE (tem))
2122 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2123 return;
2125 if (src
2126 && (tem = tree_strip_nop_conversions (src))
2127 && POINTER_TYPE_P (TREE_TYPE (tem))
2128 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2129 return;
2131 loc = sizeof_arg_loc[idx];
2133 if (dest && !cmp)
2135 if (!TYPE_P (sizeof_arg[idx])
2136 && operand_equal_p (dest, sizeof_arg[idx], 0)
2137 && comp_types (TREE_TYPE (dest), type))
2139 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2140 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2141 "argument to %<sizeof%> in %qD call is the same "
2142 "expression as the destination; did you mean to "
2143 "remove the addressof?", callee);
2144 else if ((TYPE_PRECISION (TREE_TYPE (type))
2145 == TYPE_PRECISION (char_type_node))
2146 || strop)
2147 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2148 "argument to %<sizeof%> in %qD call is the same "
2149 "expression as the destination; did you mean to "
2150 "provide an explicit length?", callee);
2151 else
2152 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2153 "argument to %<sizeof%> in %qD call is the same "
2154 "expression as the destination; did you mean to "
2155 "dereference it?", callee);
2156 return;
2159 if (POINTER_TYPE_P (TREE_TYPE (dest))
2160 && !strop
2161 && comp_types (TREE_TYPE (dest), type)
2162 && !VOID_TYPE_P (TREE_TYPE (type)))
2164 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2165 "argument to %<sizeof%> in %qD call is the same "
2166 "pointer type %qT as the destination; expected %qT "
2167 "or an explicit length", callee, TREE_TYPE (dest),
2168 TREE_TYPE (TREE_TYPE (dest)));
2169 return;
2173 if (src && !cmp)
2175 if (!TYPE_P (sizeof_arg[idx])
2176 && operand_equal_p (src, sizeof_arg[idx], 0)
2177 && comp_types (TREE_TYPE (src), type))
2179 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2180 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2181 "argument to %<sizeof%> in %qD call is the same "
2182 "expression as the source; did you mean to "
2183 "remove the addressof?", callee);
2184 else if ((TYPE_PRECISION (TREE_TYPE (type))
2185 == TYPE_PRECISION (char_type_node))
2186 || strop)
2187 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2188 "argument to %<sizeof%> in %qD call is the same "
2189 "expression as the source; did you mean to "
2190 "provide an explicit length?", callee);
2191 else
2192 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2193 "argument to %<sizeof%> in %qD call is the same "
2194 "expression as the source; did you mean to "
2195 "dereference it?", callee);
2196 return;
2199 if (POINTER_TYPE_P (TREE_TYPE (src))
2200 && !strop
2201 && comp_types (TREE_TYPE (src), type)
2202 && !VOID_TYPE_P (TREE_TYPE (type)))
2204 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2205 "argument to %<sizeof%> in %qD call is the same "
2206 "pointer type %qT as the source; expected %qT "
2207 "or an explicit length", callee, TREE_TYPE (src),
2208 TREE_TYPE (TREE_TYPE (src)));
2209 return;
2213 if (dest)
2215 if (!TYPE_P (sizeof_arg[idx])
2216 && operand_equal_p (dest, sizeof_arg[idx], 0)
2217 && comp_types (TREE_TYPE (dest), type))
2219 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2220 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2221 "argument to %<sizeof%> in %qD call is the same "
2222 "expression as the first source; did you mean to "
2223 "remove the addressof?", callee);
2224 else if ((TYPE_PRECISION (TREE_TYPE (type))
2225 == TYPE_PRECISION (char_type_node))
2226 || strop)
2227 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2228 "argument to %<sizeof%> in %qD call is the same "
2229 "expression as the first source; did you mean to "
2230 "provide an explicit length?", callee);
2231 else
2232 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2233 "argument to %<sizeof%> in %qD call is the same "
2234 "expression as the first source; did you mean to "
2235 "dereference it?", callee);
2236 return;
2239 if (POINTER_TYPE_P (TREE_TYPE (dest))
2240 && !strop
2241 && comp_types (TREE_TYPE (dest), type)
2242 && !VOID_TYPE_P (TREE_TYPE (type)))
2244 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2245 "argument to %<sizeof%> in %qD call is the same "
2246 "pointer type %qT as the first source; expected %qT "
2247 "or an explicit length", callee, TREE_TYPE (dest),
2248 TREE_TYPE (TREE_TYPE (dest)));
2249 return;
2253 if (src)
2255 if (!TYPE_P (sizeof_arg[idx])
2256 && operand_equal_p (src, sizeof_arg[idx], 0)
2257 && comp_types (TREE_TYPE (src), type))
2259 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2260 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2261 "argument to %<sizeof%> in %qD call is the same "
2262 "expression as the second source; did you mean to "
2263 "remove the addressof?", callee);
2264 else if ((TYPE_PRECISION (TREE_TYPE (type))
2265 == TYPE_PRECISION (char_type_node))
2266 || strop)
2267 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2268 "argument to %<sizeof%> in %qD call is the same "
2269 "expression as the second source; did you mean to "
2270 "provide an explicit length?", callee);
2271 else
2272 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2273 "argument to %<sizeof%> in %qD call is the same "
2274 "expression as the second source; did you mean to "
2275 "dereference it?", callee);
2276 return;
2279 if (POINTER_TYPE_P (TREE_TYPE (src))
2280 && !strop
2281 && comp_types (TREE_TYPE (src), type)
2282 && !VOID_TYPE_P (TREE_TYPE (type)))
2284 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2285 "argument to %<sizeof%> in %qD call is the same "
2286 "pointer type %qT as the second source; expected %qT "
2287 "or an explicit length", callee, TREE_TYPE (src),
2288 TREE_TYPE (TREE_TYPE (src)));
2289 return;
2295 /* Warn for unlikely, improbable, or stupid DECL declarations
2296 of `main'. */
2298 void
2299 check_main_parameter_types (tree decl)
2301 function_args_iterator iter;
2302 tree type;
2303 int argct = 0;
2305 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2307 /* XXX void_type_node belies the abstraction. */
2308 if (type == void_type_node || type == error_mark_node )
2309 break;
2311 tree t = type;
2312 if (TYPE_ATOMIC (t))
2313 pedwarn (input_location, OPT_Wmain,
2314 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2315 type, decl);
2316 while (POINTER_TYPE_P (t))
2318 t = TREE_TYPE (t);
2319 if (TYPE_ATOMIC (t))
2320 pedwarn (input_location, OPT_Wmain,
2321 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2322 type, decl);
2325 ++argct;
2326 switch (argct)
2328 case 1:
2329 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2330 pedwarn (input_location, OPT_Wmain,
2331 "first argument of %q+D should be %<int%>", decl);
2332 break;
2334 case 2:
2335 if (TREE_CODE (type) != POINTER_TYPE
2336 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2337 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2338 != char_type_node))
2339 pedwarn (input_location, OPT_Wmain,
2340 "second argument of %q+D should be %<char **%>", decl);
2341 break;
2343 case 3:
2344 if (TREE_CODE (type) != POINTER_TYPE
2345 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2346 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2347 != char_type_node))
2348 pedwarn (input_location, OPT_Wmain,
2349 "third argument of %q+D should probably be "
2350 "%<char **%>", decl);
2351 break;
2355 /* It is intentional that this message does not mention the third
2356 argument because it's only mentioned in an appendix of the
2357 standard. */
2358 if (argct > 0 && (argct < 2 || argct > 3))
2359 pedwarn (input_location, OPT_Wmain,
2360 "%q+D takes only zero or two arguments", decl);
2362 if (stdarg_p (TREE_TYPE (decl)))
2363 pedwarn (input_location, OPT_Wmain,
2364 "%q+D declared as variadic function", decl);
2367 /* vector_targets_convertible_p is used for vector pointer types. The
2368 callers perform various checks that the qualifiers are satisfactory,
2369 while OTOH vector_targets_convertible_p ignores the number of elements
2370 in the vectors. That's fine with vector pointers as we can consider,
2371 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2372 and that does not require and conversion of the pointer values.
2373 In contrast, vector_types_convertible_p and
2374 vector_types_compatible_elements_p are used for vector value types. */
2375 /* True if pointers to distinct types T1 and T2 can be converted to
2376 each other without an explicit cast. Only returns true for opaque
2377 vector types. */
2378 bool
2379 vector_targets_convertible_p (const_tree t1, const_tree t2)
2381 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2382 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2383 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2384 return true;
2386 return false;
2389 /* vector_types_convertible_p is used for vector value types.
2390 It could in principle call vector_targets_convertible_p as a subroutine,
2391 but then the check for vector type would be duplicated with its callers,
2392 and also the purpose of vector_targets_convertible_p would become
2393 muddled.
2394 Where vector_types_convertible_p returns true, a conversion might still be
2395 needed to make the types match.
2396 In contrast, vector_targets_convertible_p is used for vector pointer
2397 values, and vector_types_compatible_elements_p is used specifically
2398 in the context for binary operators, as a check if use is possible without
2399 conversion. */
2400 /* True if vector types T1 and T2 can be converted to each other
2401 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2402 can only be converted with -flax-vector-conversions yet that is not
2403 in effect, emit a note telling the user about that option if such
2404 a note has not previously been emitted. */
2405 bool
2406 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2408 static bool emitted_lax_note = false;
2409 bool convertible_lax;
2411 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2412 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2413 return true;
2415 convertible_lax =
2416 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2417 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2418 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2419 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2420 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2422 if (!convertible_lax || flag_lax_vector_conversions)
2423 return convertible_lax;
2425 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2426 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2427 return true;
2429 if (emit_lax_note && !emitted_lax_note)
2431 emitted_lax_note = true;
2432 inform (input_location, "use -flax-vector-conversions to permit "
2433 "conversions between vectors with differing "
2434 "element types or numbers of subparts");
2437 return false;
2440 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2441 and have vector types, V0 has the same type as V1, and the number of
2442 elements of V0, V1, MASK is the same.
2444 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2445 called with two arguments. In this case implementation passes the
2446 first argument twice in order to share the same tree code. This fact
2447 could enable the mask-values being twice the vector length. This is
2448 an implementation accident and this semantics is not guaranteed to
2449 the user. */
2450 tree
2451 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2452 bool complain)
2454 tree ret;
2455 bool wrap = true;
2456 bool maybe_const = false;
2457 bool two_arguments = false;
2459 if (v1 == NULL_TREE)
2461 two_arguments = true;
2462 v1 = v0;
2465 if (v0 == error_mark_node || v1 == error_mark_node
2466 || mask == error_mark_node)
2467 return error_mark_node;
2469 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2470 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2472 if (complain)
2473 error_at (loc, "__builtin_shuffle last argument must "
2474 "be an integer vector");
2475 return error_mark_node;
2478 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2479 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2481 if (complain)
2482 error_at (loc, "__builtin_shuffle arguments must be vectors");
2483 return error_mark_node;
2486 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2488 if (complain)
2489 error_at (loc, "__builtin_shuffle argument vectors must be of "
2490 "the same type");
2491 return error_mark_node;
2494 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2495 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2496 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2497 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2499 if (complain)
2500 error_at (loc, "__builtin_shuffle number of elements of the "
2501 "argument vector(s) and the mask vector should "
2502 "be the same");
2503 return error_mark_node;
2506 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2507 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2509 if (complain)
2510 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2511 "must have the same size as inner type of the mask");
2512 return error_mark_node;
2515 if (!c_dialect_cxx ())
2517 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2518 v0 = c_fully_fold (v0, false, &maybe_const);
2519 wrap &= maybe_const;
2521 if (two_arguments)
2522 v1 = v0 = save_expr (v0);
2523 else
2525 v1 = c_fully_fold (v1, false, &maybe_const);
2526 wrap &= maybe_const;
2529 mask = c_fully_fold (mask, false, &maybe_const);
2530 wrap &= maybe_const;
2532 else if (two_arguments)
2533 v1 = v0 = save_expr (v0);
2535 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2537 if (!c_dialect_cxx () && !wrap)
2538 ret = c_wrap_maybe_const (ret, true);
2540 return ret;
2543 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2544 to integral type. */
2546 static tree
2547 c_common_get_narrower (tree op, int *unsignedp_ptr)
2549 op = get_narrower (op, unsignedp_ptr);
2551 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2552 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2554 /* C++0x scoped enumerations don't implicitly convert to integral
2555 type; if we stripped an explicit conversion to a larger type we
2556 need to replace it so common_type will still work. */
2557 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2558 TYPE_UNSIGNED (TREE_TYPE (op)));
2559 op = fold_convert (type, op);
2561 return op;
2564 /* This is a helper function of build_binary_op.
2566 For certain operations if both args were extended from the same
2567 smaller type, do the arithmetic in that type and then extend.
2569 BITWISE indicates a bitwise operation.
2570 For them, this optimization is safe only if
2571 both args are zero-extended or both are sign-extended.
2572 Otherwise, we might change the result.
2573 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2574 but calculated in (unsigned short) it would be (unsigned short)-1.
2576 tree
2577 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2579 int unsigned0, unsigned1;
2580 tree arg0, arg1;
2581 int uns;
2582 tree type;
2584 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2585 excessive narrowing when we call get_narrower below. For
2586 example, suppose that OP0 is of unsigned int extended
2587 from signed char and that RESULT_TYPE is long long int.
2588 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2589 like
2591 (long long int) (unsigned int) signed_char
2593 which get_narrower would narrow down to
2595 (unsigned int) signed char
2597 If we do not cast OP0 first, get_narrower would return
2598 signed_char, which is inconsistent with the case of the
2599 explicit cast. */
2600 op0 = convert (result_type, op0);
2601 op1 = convert (result_type, op1);
2603 arg0 = c_common_get_narrower (op0, &unsigned0);
2604 arg1 = c_common_get_narrower (op1, &unsigned1);
2606 /* UNS is 1 if the operation to be done is an unsigned one. */
2607 uns = TYPE_UNSIGNED (result_type);
2609 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2610 but it *requires* conversion to FINAL_TYPE. */
2612 if ((TYPE_PRECISION (TREE_TYPE (op0))
2613 == TYPE_PRECISION (TREE_TYPE (arg0)))
2614 && TREE_TYPE (op0) != result_type)
2615 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2616 if ((TYPE_PRECISION (TREE_TYPE (op1))
2617 == TYPE_PRECISION (TREE_TYPE (arg1)))
2618 && TREE_TYPE (op1) != result_type)
2619 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2621 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2623 /* For bitwise operations, signedness of nominal type
2624 does not matter. Consider only how operands were extended. */
2625 if (bitwise)
2626 uns = unsigned0;
2628 /* Note that in all three cases below we refrain from optimizing
2629 an unsigned operation on sign-extended args.
2630 That would not be valid. */
2632 /* Both args variable: if both extended in same way
2633 from same width, do it in that width.
2634 Do it unsigned if args were zero-extended. */
2635 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2636 < TYPE_PRECISION (result_type))
2637 && (TYPE_PRECISION (TREE_TYPE (arg1))
2638 == TYPE_PRECISION (TREE_TYPE (arg0)))
2639 && unsigned0 == unsigned1
2640 && (unsigned0 || !uns))
2641 return c_common_signed_or_unsigned_type
2642 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2644 else if (TREE_CODE (arg0) == INTEGER_CST
2645 && (unsigned1 || !uns)
2646 && (TYPE_PRECISION (TREE_TYPE (arg1))
2647 < TYPE_PRECISION (result_type))
2648 && (type
2649 = c_common_signed_or_unsigned_type (unsigned1,
2650 TREE_TYPE (arg1)))
2651 && !POINTER_TYPE_P (type)
2652 && int_fits_type_p (arg0, type))
2653 return type;
2655 else if (TREE_CODE (arg1) == INTEGER_CST
2656 && (unsigned0 || !uns)
2657 && (TYPE_PRECISION (TREE_TYPE (arg0))
2658 < TYPE_PRECISION (result_type))
2659 && (type
2660 = c_common_signed_or_unsigned_type (unsigned0,
2661 TREE_TYPE (arg0)))
2662 && !POINTER_TYPE_P (type)
2663 && int_fits_type_p (arg1, type))
2664 return type;
2666 return result_type;
2669 /* Checks if expression EXPR of real/integer type cannot be converted
2670 to the real/integer type TYPE. Function returns non-zero when:
2671 * EXPR is a constant which cannot be exactly converted to TYPE.
2672 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2673 for EXPR type and TYPE being both integers or both real.
2674 * EXPR is not a constant of real type and TYPE is an integer.
2675 * EXPR is not a constant of integer type which cannot be
2676 exactly converted to real type.
2677 Function allows conversions between types of different signedness and
2678 can return SAFE_CONVERSION (zero) in that case. Function can produce
2679 signedness warnings if PRODUCE_WARNS is true. */
2681 enum conversion_safety
2682 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2684 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2685 tree expr_type = TREE_TYPE (expr);
2686 loc = expansion_point_location_if_in_system_header (loc);
2688 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2690 /* Warn for real constant that is not an exact integer converted
2691 to integer type. */
2692 if (TREE_CODE (expr_type) == REAL_TYPE
2693 && TREE_CODE (type) == INTEGER_TYPE)
2695 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2696 give_warning = UNSAFE_REAL;
2698 /* Warn for an integer constant that does not fit into integer type. */
2699 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2700 && TREE_CODE (type) == INTEGER_TYPE
2701 && !int_fits_type_p (expr, type))
2703 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2704 && tree_int_cst_sgn (expr) < 0)
2706 if (produce_warns)
2707 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2708 " implicitly converted to unsigned type");
2710 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2712 if (produce_warns)
2713 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2714 " constant value to negative integer");
2716 else
2717 give_warning = UNSAFE_OTHER;
2719 else if (TREE_CODE (type) == REAL_TYPE)
2721 /* Warn for an integer constant that does not fit into real type. */
2722 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2724 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2725 if (!exact_real_truncate (TYPE_MODE (type), &a))
2726 give_warning = UNSAFE_REAL;
2728 /* Warn for a real constant that does not fit into a smaller
2729 real type. */
2730 else if (TREE_CODE (expr_type) == REAL_TYPE
2731 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2733 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2734 if (!exact_real_truncate (TYPE_MODE (type), &a))
2735 give_warning = UNSAFE_REAL;
2739 else
2741 /* Warn for real types converted to integer types. */
2742 if (TREE_CODE (expr_type) == REAL_TYPE
2743 && TREE_CODE (type) == INTEGER_TYPE)
2744 give_warning = UNSAFE_REAL;
2746 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2747 && TREE_CODE (type) == INTEGER_TYPE)
2749 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2750 expr = get_unwidened (expr, 0);
2751 expr_type = TREE_TYPE (expr);
2753 /* Don't warn for short y; short x = ((int)y & 0xff); */
2754 if (TREE_CODE (expr) == BIT_AND_EXPR
2755 || TREE_CODE (expr) == BIT_IOR_EXPR
2756 || TREE_CODE (expr) == BIT_XOR_EXPR)
2758 /* If both args were extended from a shortest type,
2759 use that type if that is safe. */
2760 expr_type = shorten_binary_op (expr_type,
2761 TREE_OPERAND (expr, 0),
2762 TREE_OPERAND (expr, 1),
2763 /* bitwise */1);
2765 if (TREE_CODE (expr) == BIT_AND_EXPR)
2767 tree op0 = TREE_OPERAND (expr, 0);
2768 tree op1 = TREE_OPERAND (expr, 1);
2769 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2770 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2772 /* If one of the operands is a non-negative constant
2773 that fits in the target type, then the type of the
2774 other operand does not matter. */
2775 if ((TREE_CODE (op0) == INTEGER_CST
2776 && int_fits_type_p (op0, c_common_signed_type (type))
2777 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2778 || (TREE_CODE (op1) == INTEGER_CST
2779 && int_fits_type_p (op1, c_common_signed_type (type))
2780 && int_fits_type_p (op1,
2781 c_common_unsigned_type (type))))
2782 return SAFE_CONVERSION;
2783 /* If constant is unsigned and fits in the target
2784 type, then the result will also fit. */
2785 else if ((TREE_CODE (op0) == INTEGER_CST
2786 && unsigned0
2787 && int_fits_type_p (op0, type))
2788 || (TREE_CODE (op1) == INTEGER_CST
2789 && unsigned1
2790 && int_fits_type_p (op1, type)))
2791 return SAFE_CONVERSION;
2794 /* Warn for integer types converted to smaller integer types. */
2795 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2796 give_warning = UNSAFE_OTHER;
2798 /* When they are the same width but different signedness,
2799 then the value may change. */
2800 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2801 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2802 /* Even when converted to a bigger type, if the type is
2803 unsigned but expr is signed, then negative values
2804 will be changed. */
2805 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2806 && produce_warns)
2807 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2808 "may change the sign of the result",
2809 type, expr_type);
2812 /* Warn for integer types converted to real types if and only if
2813 all the range of values of the integer type cannot be
2814 represented by the real type. */
2815 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2816 && TREE_CODE (type) == REAL_TYPE)
2818 tree type_low_bound, type_high_bound;
2819 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2821 /* Don't warn about char y = 0xff; float x = (int) y; */
2822 expr = get_unwidened (expr, 0);
2823 expr_type = TREE_TYPE (expr);
2825 type_low_bound = TYPE_MIN_VALUE (expr_type);
2826 type_high_bound = TYPE_MAX_VALUE (expr_type);
2827 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2828 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2830 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2831 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2832 give_warning = UNSAFE_OTHER;
2835 /* Warn for real types converted to smaller real types. */
2836 else if (TREE_CODE (expr_type) == REAL_TYPE
2837 && TREE_CODE (type) == REAL_TYPE
2838 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2839 give_warning = UNSAFE_REAL;
2842 return give_warning;
2845 /* Warns if the conversion of EXPR to TYPE may alter a value.
2846 This is a helper function for warnings_for_convert_and_check. */
2848 static void
2849 conversion_warning (location_t loc, tree type, tree expr)
2851 tree expr_type = TREE_TYPE (expr);
2852 enum conversion_safety conversion_kind;
2854 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2855 return;
2857 /* This may happen, because for LHS op= RHS we preevaluate
2858 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2859 means we could no longer see the code of the EXPR. */
2860 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2861 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2862 if (TREE_CODE (expr) == SAVE_EXPR)
2863 expr = TREE_OPERAND (expr, 0);
2865 switch (TREE_CODE (expr))
2867 case EQ_EXPR:
2868 case NE_EXPR:
2869 case LE_EXPR:
2870 case GE_EXPR:
2871 case LT_EXPR:
2872 case GT_EXPR:
2873 case TRUTH_ANDIF_EXPR:
2874 case TRUTH_ORIF_EXPR:
2875 case TRUTH_AND_EXPR:
2876 case TRUTH_OR_EXPR:
2877 case TRUTH_XOR_EXPR:
2878 case TRUTH_NOT_EXPR:
2879 /* Conversion from boolean to a signed:1 bit-field (which only
2880 can hold the values 0 and -1) doesn't lose information - but
2881 it does change the value. */
2882 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2883 warning_at (loc, OPT_Wconversion,
2884 "conversion to %qT from boolean expression", type);
2885 return;
2887 case REAL_CST:
2888 case INTEGER_CST:
2889 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2890 if (conversion_kind == UNSAFE_REAL)
2891 warning_at (loc, OPT_Wfloat_conversion,
2892 "conversion to %qT alters %qT constant value",
2893 type, expr_type);
2894 else if (conversion_kind)
2895 warning_at (loc, OPT_Wconversion,
2896 "conversion to %qT alters %qT constant value",
2897 type, expr_type);
2898 return;
2900 case COND_EXPR:
2902 /* In case of COND_EXPR, we do not care about the type of
2903 COND_EXPR, only about the conversion of each operand. */
2904 tree op1 = TREE_OPERAND (expr, 1);
2905 tree op2 = TREE_OPERAND (expr, 2);
2907 conversion_warning (loc, type, op1);
2908 conversion_warning (loc, type, op2);
2909 return;
2912 default: /* 'expr' is not a constant. */
2913 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2914 if (conversion_kind == UNSAFE_REAL)
2915 warning_at (loc, OPT_Wfloat_conversion,
2916 "conversion to %qT from %qT may alter its value",
2917 type, expr_type);
2918 else if (conversion_kind)
2919 warning_at (loc, OPT_Wconversion,
2920 "conversion to %qT from %qT may alter its value",
2921 type, expr_type);
2925 /* Produce warnings after a conversion. RESULT is the result of
2926 converting EXPR to TYPE. This is a helper function for
2927 convert_and_check and cp_convert_and_check. */
2929 void
2930 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2931 tree result)
2933 loc = expansion_point_location_if_in_system_header (loc);
2935 if (TREE_CODE (expr) == INTEGER_CST
2936 && (TREE_CODE (type) == INTEGER_TYPE
2937 || TREE_CODE (type) == ENUMERAL_TYPE)
2938 && !int_fits_type_p (expr, type))
2940 /* Do not diagnose overflow in a constant expression merely
2941 because a conversion overflowed. */
2942 if (TREE_OVERFLOW (result))
2943 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2945 if (TYPE_UNSIGNED (type))
2947 /* This detects cases like converting -129 or 256 to
2948 unsigned char. */
2949 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2950 warning_at (loc, OPT_Woverflow,
2951 "large integer implicitly truncated to unsigned type");
2952 else
2953 conversion_warning (loc, type, expr);
2955 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2956 warning_at (loc, OPT_Woverflow,
2957 "overflow in implicit constant conversion");
2958 /* No warning for converting 0x80000000 to int. */
2959 else if (pedantic
2960 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2961 || TYPE_PRECISION (TREE_TYPE (expr))
2962 != TYPE_PRECISION (type)))
2963 warning_at (loc, OPT_Woverflow,
2964 "overflow in implicit constant conversion");
2966 else
2967 conversion_warning (loc, type, expr);
2969 else if ((TREE_CODE (result) == INTEGER_CST
2970 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2971 warning_at (loc, OPT_Woverflow,
2972 "overflow in implicit constant conversion");
2973 else
2974 conversion_warning (loc, type, expr);
2978 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2979 Invoke this function on every expression that is converted implicitly,
2980 i.e. because of language rules and not because of an explicit cast. */
2982 tree
2983 convert_and_check (location_t loc, tree type, tree expr)
2985 tree result;
2986 tree expr_for_warning;
2988 /* Convert from a value with possible excess precision rather than
2989 via the semantic type, but do not warn about values not fitting
2990 exactly in the semantic type. */
2991 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2993 tree orig_type = TREE_TYPE (expr);
2994 expr = TREE_OPERAND (expr, 0);
2995 expr_for_warning = convert (orig_type, expr);
2996 if (orig_type == type)
2997 return expr_for_warning;
2999 else
3000 expr_for_warning = expr;
3002 if (TREE_TYPE (expr) == type)
3003 return expr;
3005 result = convert (type, expr);
3007 if (c_inhibit_evaluation_warnings == 0
3008 && !TREE_OVERFLOW_P (expr)
3009 && result != error_mark_node)
3010 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
3012 return result;
3015 /* A node in a list that describes references to variables (EXPR), which are
3016 either read accesses if WRITER is zero, or write accesses, in which case
3017 WRITER is the parent of EXPR. */
3018 struct tlist
3020 struct tlist *next;
3021 tree expr, writer;
3024 /* Used to implement a cache the results of a call to verify_tree. We only
3025 use this for SAVE_EXPRs. */
3026 struct tlist_cache
3028 struct tlist_cache *next;
3029 struct tlist *cache_before_sp;
3030 struct tlist *cache_after_sp;
3031 tree expr;
3034 /* Obstack to use when allocating tlist structures, and corresponding
3035 firstobj. */
3036 static struct obstack tlist_obstack;
3037 static char *tlist_firstobj = 0;
3039 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3040 warnings. */
3041 static struct tlist *warned_ids;
3042 /* SAVE_EXPRs need special treatment. We process them only once and then
3043 cache the results. */
3044 static struct tlist_cache *save_expr_cache;
3046 static void add_tlist (struct tlist **, struct tlist *, tree, int);
3047 static void merge_tlist (struct tlist **, struct tlist *, int);
3048 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3049 static int warning_candidate_p (tree);
3050 static bool candidate_equal_p (const_tree, const_tree);
3051 static void warn_for_collisions (struct tlist *);
3052 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3053 static struct tlist *new_tlist (struct tlist *, tree, tree);
3055 /* Create a new struct tlist and fill in its fields. */
3056 static struct tlist *
3057 new_tlist (struct tlist *next, tree t, tree writer)
3059 struct tlist *l;
3060 l = XOBNEW (&tlist_obstack, struct tlist);
3061 l->next = next;
3062 l->expr = t;
3063 l->writer = writer;
3064 return l;
3067 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3068 is nonnull, we ignore any node we find which has a writer equal to it. */
3070 static void
3071 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3073 while (add)
3075 struct tlist *next = add->next;
3076 if (!copy)
3077 add->next = *to;
3078 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3079 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3080 add = next;
3084 /* Merge the nodes of ADD into TO. This merging process is done so that for
3085 each variable that already exists in TO, no new node is added; however if
3086 there is a write access recorded in ADD, and an occurrence on TO is only
3087 a read access, then the occurrence in TO will be modified to record the
3088 write. */
3090 static void
3091 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3093 struct tlist **end = to;
3095 while (*end)
3096 end = &(*end)->next;
3098 while (add)
3100 int found = 0;
3101 struct tlist *tmp2;
3102 struct tlist *next = add->next;
3104 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3105 if (candidate_equal_p (tmp2->expr, add->expr))
3107 found = 1;
3108 if (!tmp2->writer)
3109 tmp2->writer = add->writer;
3111 if (!found)
3113 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3114 end = &(*end)->next;
3115 *end = 0;
3117 add = next;
3121 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3122 references in list LIST conflict with it, excluding reads if ONLY writers
3123 is nonzero. */
3125 static void
3126 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3127 int only_writes)
3129 struct tlist *tmp;
3131 /* Avoid duplicate warnings. */
3132 for (tmp = warned_ids; tmp; tmp = tmp->next)
3133 if (candidate_equal_p (tmp->expr, written))
3134 return;
3136 while (list)
3138 if (candidate_equal_p (list->expr, written)
3139 && !candidate_equal_p (list->writer, writer)
3140 && (!only_writes || list->writer))
3142 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3143 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3144 OPT_Wsequence_point, "operation on %qE may be undefined",
3145 list->expr);
3147 list = list->next;
3151 /* Given a list LIST of references to variables, find whether any of these
3152 can cause conflicts due to missing sequence points. */
3154 static void
3155 warn_for_collisions (struct tlist *list)
3157 struct tlist *tmp;
3159 for (tmp = list; tmp; tmp = tmp->next)
3161 if (tmp->writer)
3162 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3166 /* Return nonzero if X is a tree that can be verified by the sequence point
3167 warnings. */
3168 static int
3169 warning_candidate_p (tree x)
3171 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3172 return 0;
3174 if (TREE_CODE (x) == BLOCK)
3175 return 0;
3177 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3178 (lvalue_p) crash on TRY/CATCH. */
3179 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3180 return 0;
3182 if (!lvalue_p (x))
3183 return 0;
3185 /* No point to track non-const calls, they will never satisfy
3186 operand_equal_p. */
3187 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3188 return 0;
3190 if (TREE_CODE (x) == STRING_CST)
3191 return 0;
3193 return 1;
3196 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3197 static bool
3198 candidate_equal_p (const_tree x, const_tree y)
3200 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3203 /* Walk the tree X, and record accesses to variables. If X is written by the
3204 parent tree, WRITER is the parent.
3205 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3206 expression or its only operand forces a sequence point, then everything up
3207 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3208 in PNO_SP.
3209 Once we return, we will have emitted warnings if any subexpression before
3210 such a sequence point could be undefined. On a higher level, however, the
3211 sequence point may not be relevant, and we'll merge the two lists.
3213 Example: (b++, a) + b;
3214 The call that processes the COMPOUND_EXPR will store the increment of B
3215 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3216 processes the PLUS_EXPR will need to merge the two lists so that
3217 eventually, all accesses end up on the same list (and we'll warn about the
3218 unordered subexpressions b++ and b.
3220 A note on merging. If we modify the former example so that our expression
3221 becomes
3222 (b++, b) + a
3223 care must be taken not simply to add all three expressions into the final
3224 PNO_SP list. The function merge_tlist takes care of that by merging the
3225 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3226 way, so that no more than one access to B is recorded. */
3228 static void
3229 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3230 tree writer)
3232 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3233 enum tree_code code;
3234 enum tree_code_class cl;
3236 /* X may be NULL if it is the operand of an empty statement expression
3237 ({ }). */
3238 if (x == NULL)
3239 return;
3241 restart:
3242 code = TREE_CODE (x);
3243 cl = TREE_CODE_CLASS (code);
3245 if (warning_candidate_p (x))
3246 *pno_sp = new_tlist (*pno_sp, x, writer);
3248 switch (code)
3250 case CONSTRUCTOR:
3251 case SIZEOF_EXPR:
3252 return;
3254 case COMPOUND_EXPR:
3255 case TRUTH_ANDIF_EXPR:
3256 case TRUTH_ORIF_EXPR:
3257 tmp_before = tmp_nosp = tmp_list3 = 0;
3258 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3259 warn_for_collisions (tmp_nosp);
3260 merge_tlist (pbefore_sp, tmp_before, 0);
3261 merge_tlist (pbefore_sp, tmp_nosp, 0);
3262 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3263 merge_tlist (pbefore_sp, tmp_list3, 0);
3264 return;
3266 case COND_EXPR:
3267 tmp_before = tmp_list2 = 0;
3268 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3269 warn_for_collisions (tmp_list2);
3270 merge_tlist (pbefore_sp, tmp_before, 0);
3271 merge_tlist (pbefore_sp, tmp_list2, 0);
3273 tmp_list3 = tmp_nosp = 0;
3274 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3275 warn_for_collisions (tmp_nosp);
3276 merge_tlist (pbefore_sp, tmp_list3, 0);
3278 tmp_list3 = tmp_list2 = 0;
3279 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3280 warn_for_collisions (tmp_list2);
3281 merge_tlist (pbefore_sp, tmp_list3, 0);
3282 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3283 two first, to avoid warning for (a ? b++ : b++). */
3284 merge_tlist (&tmp_nosp, tmp_list2, 0);
3285 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3286 return;
3288 case PREDECREMENT_EXPR:
3289 case PREINCREMENT_EXPR:
3290 case POSTDECREMENT_EXPR:
3291 case POSTINCREMENT_EXPR:
3292 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3293 return;
3295 case MODIFY_EXPR:
3296 tmp_before = tmp_nosp = tmp_list3 = 0;
3297 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3298 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3299 /* Expressions inside the LHS are not ordered wrt. the sequence points
3300 in the RHS. Example:
3301 *a = (a++, 2)
3302 Despite the fact that the modification of "a" is in the before_sp
3303 list (tmp_before), it conflicts with the use of "a" in the LHS.
3304 We can handle this by adding the contents of tmp_list3
3305 to those of tmp_before, and redoing the collision warnings for that
3306 list. */
3307 add_tlist (&tmp_before, tmp_list3, x, 1);
3308 warn_for_collisions (tmp_before);
3309 /* Exclude the LHS itself here; we first have to merge it into the
3310 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3311 didn't exclude the LHS, we'd get it twice, once as a read and once
3312 as a write. */
3313 add_tlist (pno_sp, tmp_list3, x, 0);
3314 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3316 merge_tlist (pbefore_sp, tmp_before, 0);
3317 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3318 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3319 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3320 return;
3322 case CALL_EXPR:
3323 /* We need to warn about conflicts among arguments and conflicts between
3324 args and the function address. Side effects of the function address,
3325 however, are not ordered by the sequence point of the call. */
3327 call_expr_arg_iterator iter;
3328 tree arg;
3329 tmp_before = tmp_nosp = 0;
3330 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3331 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3333 tmp_list2 = tmp_list3 = 0;
3334 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3335 merge_tlist (&tmp_list3, tmp_list2, 0);
3336 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3338 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3339 warn_for_collisions (tmp_before);
3340 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3341 return;
3344 case TREE_LIST:
3345 /* Scan all the list, e.g. indices of multi dimensional array. */
3346 while (x)
3348 tmp_before = tmp_nosp = 0;
3349 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3350 merge_tlist (&tmp_nosp, tmp_before, 0);
3351 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3352 x = TREE_CHAIN (x);
3354 return;
3356 case SAVE_EXPR:
3358 struct tlist_cache *t;
3359 for (t = save_expr_cache; t; t = t->next)
3360 if (candidate_equal_p (t->expr, x))
3361 break;
3363 if (!t)
3365 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3366 t->next = save_expr_cache;
3367 t->expr = x;
3368 save_expr_cache = t;
3370 tmp_before = tmp_nosp = 0;
3371 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3372 warn_for_collisions (tmp_nosp);
3374 tmp_list3 = 0;
3375 merge_tlist (&tmp_list3, tmp_nosp, 0);
3376 t->cache_before_sp = tmp_before;
3377 t->cache_after_sp = tmp_list3;
3379 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3380 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3381 return;
3384 case ADDR_EXPR:
3385 x = TREE_OPERAND (x, 0);
3386 if (DECL_P (x))
3387 return;
3388 writer = 0;
3389 goto restart;
3391 default:
3392 /* For other expressions, simply recurse on their operands.
3393 Manual tail recursion for unary expressions.
3394 Other non-expressions need not be processed. */
3395 if (cl == tcc_unary)
3397 x = TREE_OPERAND (x, 0);
3398 writer = 0;
3399 goto restart;
3401 else if (IS_EXPR_CODE_CLASS (cl))
3403 int lp;
3404 int max = TREE_OPERAND_LENGTH (x);
3405 for (lp = 0; lp < max; lp++)
3407 tmp_before = tmp_nosp = 0;
3408 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3409 merge_tlist (&tmp_nosp, tmp_before, 0);
3410 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3413 return;
3417 /* Try to warn for undefined behavior in EXPR due to missing sequence
3418 points. */
3420 DEBUG_FUNCTION void
3421 verify_sequence_points (tree expr)
3423 struct tlist *before_sp = 0, *after_sp = 0;
3425 warned_ids = 0;
3426 save_expr_cache = 0;
3427 if (tlist_firstobj == 0)
3429 gcc_obstack_init (&tlist_obstack);
3430 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3433 verify_tree (expr, &before_sp, &after_sp, 0);
3434 warn_for_collisions (after_sp);
3435 obstack_free (&tlist_obstack, tlist_firstobj);
3438 /* Validate the expression after `case' and apply default promotions. */
3440 static tree
3441 check_case_value (location_t loc, tree value)
3443 if (value == NULL_TREE)
3444 return value;
3446 if (TREE_CODE (value) == INTEGER_CST)
3447 /* Promote char or short to int. */
3448 value = perform_integral_promotions (value);
3449 else if (value != error_mark_node)
3451 error_at (loc, "case label does not reduce to an integer constant");
3452 value = error_mark_node;
3455 constant_expression_warning (value);
3457 return value;
3460 /* See if the case values LOW and HIGH are in the range of the original
3461 type (i.e. before the default conversion to int) of the switch testing
3462 expression.
3463 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3464 the type before promoting it. CASE_LOW_P is a pointer to the lower
3465 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3466 if the case is not a case range.
3467 The caller has to make sure that we are not called with NULL for
3468 CASE_LOW_P (i.e. the default case).
3469 Returns true if the case label is in range of ORIG_TYPE (saturated or
3470 untouched) or false if the label is out of range. */
3472 static bool
3473 check_case_bounds (location_t loc, tree type, tree orig_type,
3474 tree *case_low_p, tree *case_high_p)
3476 tree min_value, max_value;
3477 tree case_low = *case_low_p;
3478 tree case_high = case_high_p ? *case_high_p : case_low;
3480 /* If there was a problem with the original type, do nothing. */
3481 if (orig_type == error_mark_node)
3482 return true;
3484 min_value = TYPE_MIN_VALUE (orig_type);
3485 max_value = TYPE_MAX_VALUE (orig_type);
3487 /* Case label is less than minimum for type. */
3488 if (tree_int_cst_compare (case_low, min_value) < 0
3489 && tree_int_cst_compare (case_high, min_value) < 0)
3491 warning_at (loc, 0, "case label value is less than minimum value "
3492 "for type");
3493 return false;
3496 /* Case value is greater than maximum for type. */
3497 if (tree_int_cst_compare (case_low, max_value) > 0
3498 && tree_int_cst_compare (case_high, max_value) > 0)
3500 warning_at (loc, 0, "case label value exceeds maximum value for type");
3501 return false;
3504 /* Saturate lower case label value to minimum. */
3505 if (tree_int_cst_compare (case_high, min_value) >= 0
3506 && tree_int_cst_compare (case_low, min_value) < 0)
3508 warning_at (loc, 0, "lower value in case label range"
3509 " less than minimum value for type");
3510 case_low = min_value;
3513 /* Saturate upper case label value to maximum. */
3514 if (tree_int_cst_compare (case_low, max_value) <= 0
3515 && tree_int_cst_compare (case_high, max_value) > 0)
3517 warning_at (loc, 0, "upper value in case label range"
3518 " exceeds maximum value for type");
3519 case_high = max_value;
3522 if (*case_low_p != case_low)
3523 *case_low_p = convert (type, case_low);
3524 if (case_high_p && *case_high_p != case_high)
3525 *case_high_p = convert (type, case_high);
3527 return true;
3530 /* Return an integer type with BITS bits of precision,
3531 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3533 tree
3534 c_common_type_for_size (unsigned int bits, int unsignedp)
3536 int i;
3538 if (bits == TYPE_PRECISION (integer_type_node))
3539 return unsignedp ? unsigned_type_node : integer_type_node;
3541 if (bits == TYPE_PRECISION (signed_char_type_node))
3542 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3544 if (bits == TYPE_PRECISION (short_integer_type_node))
3545 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3547 if (bits == TYPE_PRECISION (long_integer_type_node))
3548 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3550 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3551 return (unsignedp ? long_long_unsigned_type_node
3552 : long_long_integer_type_node);
3554 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3555 if (int_n_enabled_p[i]
3556 && bits == int_n_data[i].bitsize)
3557 return (unsignedp ? int_n_trees[i].unsigned_type
3558 : int_n_trees[i].signed_type);
3560 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3561 return (unsignedp ? widest_unsigned_literal_type_node
3562 : widest_integer_literal_type_node);
3564 if (bits <= TYPE_PRECISION (intQI_type_node))
3565 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3567 if (bits <= TYPE_PRECISION (intHI_type_node))
3568 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3570 if (bits <= TYPE_PRECISION (intSI_type_node))
3571 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3573 if (bits <= TYPE_PRECISION (intDI_type_node))
3574 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3576 return 0;
3579 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3580 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3581 and saturating if SATP is nonzero, otherwise not saturating. */
3583 tree
3584 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3585 int unsignedp, int satp)
3587 machine_mode mode;
3588 if (ibit == 0)
3589 mode = unsignedp ? UQQmode : QQmode;
3590 else
3591 mode = unsignedp ? UHAmode : HAmode;
3593 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3594 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3595 break;
3597 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3599 sorry ("GCC cannot support operators with integer types and "
3600 "fixed-point types that have too many integral and "
3601 "fractional bits together");
3602 return 0;
3605 return c_common_type_for_mode (mode, satp);
3608 /* Used for communication between c_common_type_for_mode and
3609 c_register_builtin_type. */
3610 tree registered_builtin_types;
3612 /* Return a data type that has machine mode MODE.
3613 If the mode is an integer,
3614 then UNSIGNEDP selects between signed and unsigned types.
3615 If the mode is a fixed-point mode,
3616 then UNSIGNEDP selects between saturating and nonsaturating types. */
3618 tree
3619 c_common_type_for_mode (machine_mode mode, int unsignedp)
3621 tree t;
3622 int i;
3624 if (mode == TYPE_MODE (integer_type_node))
3625 return unsignedp ? unsigned_type_node : integer_type_node;
3627 if (mode == TYPE_MODE (signed_char_type_node))
3628 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3630 if (mode == TYPE_MODE (short_integer_type_node))
3631 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3633 if (mode == TYPE_MODE (long_integer_type_node))
3634 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3636 if (mode == TYPE_MODE (long_long_integer_type_node))
3637 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3639 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3640 if (int_n_enabled_p[i]
3641 && mode == int_n_data[i].m)
3642 return (unsignedp ? int_n_trees[i].unsigned_type
3643 : int_n_trees[i].signed_type);
3645 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3646 return unsignedp ? widest_unsigned_literal_type_node
3647 : widest_integer_literal_type_node;
3649 if (mode == QImode)
3650 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3652 if (mode == HImode)
3653 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3655 if (mode == SImode)
3656 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3658 if (mode == DImode)
3659 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3661 #if HOST_BITS_PER_WIDE_INT >= 64
3662 if (mode == TYPE_MODE (intTI_type_node))
3663 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3664 #endif
3666 if (mode == TYPE_MODE (float_type_node))
3667 return float_type_node;
3669 if (mode == TYPE_MODE (double_type_node))
3670 return double_type_node;
3672 if (mode == TYPE_MODE (long_double_type_node))
3673 return long_double_type_node;
3675 if (mode == TYPE_MODE (void_type_node))
3676 return void_type_node;
3678 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3679 return (unsignedp
3680 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3681 : make_signed_type (GET_MODE_PRECISION (mode)));
3683 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3684 return (unsignedp
3685 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3686 : make_signed_type (GET_MODE_PRECISION (mode)));
3688 if (COMPLEX_MODE_P (mode))
3690 machine_mode inner_mode;
3691 tree inner_type;
3693 if (mode == TYPE_MODE (complex_float_type_node))
3694 return complex_float_type_node;
3695 if (mode == TYPE_MODE (complex_double_type_node))
3696 return complex_double_type_node;
3697 if (mode == TYPE_MODE (complex_long_double_type_node))
3698 return complex_long_double_type_node;
3700 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3701 return complex_integer_type_node;
3703 inner_mode = GET_MODE_INNER (mode);
3704 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3705 if (inner_type != NULL_TREE)
3706 return build_complex_type (inner_type);
3708 else if (VECTOR_MODE_P (mode))
3710 machine_mode inner_mode = GET_MODE_INNER (mode);
3711 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3712 if (inner_type != NULL_TREE)
3713 return build_vector_type_for_mode (inner_type, mode);
3716 if (mode == TYPE_MODE (dfloat32_type_node))
3717 return dfloat32_type_node;
3718 if (mode == TYPE_MODE (dfloat64_type_node))
3719 return dfloat64_type_node;
3720 if (mode == TYPE_MODE (dfloat128_type_node))
3721 return dfloat128_type_node;
3723 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3725 if (mode == TYPE_MODE (short_fract_type_node))
3726 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3727 if (mode == TYPE_MODE (fract_type_node))
3728 return unsignedp ? sat_fract_type_node : fract_type_node;
3729 if (mode == TYPE_MODE (long_fract_type_node))
3730 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3731 if (mode == TYPE_MODE (long_long_fract_type_node))
3732 return unsignedp ? sat_long_long_fract_type_node
3733 : long_long_fract_type_node;
3735 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3736 return unsignedp ? sat_unsigned_short_fract_type_node
3737 : unsigned_short_fract_type_node;
3738 if (mode == TYPE_MODE (unsigned_fract_type_node))
3739 return unsignedp ? sat_unsigned_fract_type_node
3740 : unsigned_fract_type_node;
3741 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3742 return unsignedp ? sat_unsigned_long_fract_type_node
3743 : unsigned_long_fract_type_node;
3744 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3745 return unsignedp ? sat_unsigned_long_long_fract_type_node
3746 : unsigned_long_long_fract_type_node;
3748 if (mode == TYPE_MODE (short_accum_type_node))
3749 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3750 if (mode == TYPE_MODE (accum_type_node))
3751 return unsignedp ? sat_accum_type_node : accum_type_node;
3752 if (mode == TYPE_MODE (long_accum_type_node))
3753 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3754 if (mode == TYPE_MODE (long_long_accum_type_node))
3755 return unsignedp ? sat_long_long_accum_type_node
3756 : long_long_accum_type_node;
3758 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3759 return unsignedp ? sat_unsigned_short_accum_type_node
3760 : unsigned_short_accum_type_node;
3761 if (mode == TYPE_MODE (unsigned_accum_type_node))
3762 return unsignedp ? sat_unsigned_accum_type_node
3763 : unsigned_accum_type_node;
3764 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3765 return unsignedp ? sat_unsigned_long_accum_type_node
3766 : unsigned_long_accum_type_node;
3767 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3768 return unsignedp ? sat_unsigned_long_long_accum_type_node
3769 : unsigned_long_long_accum_type_node;
3771 if (mode == QQmode)
3772 return unsignedp ? sat_qq_type_node : qq_type_node;
3773 if (mode == HQmode)
3774 return unsignedp ? sat_hq_type_node : hq_type_node;
3775 if (mode == SQmode)
3776 return unsignedp ? sat_sq_type_node : sq_type_node;
3777 if (mode == DQmode)
3778 return unsignedp ? sat_dq_type_node : dq_type_node;
3779 if (mode == TQmode)
3780 return unsignedp ? sat_tq_type_node : tq_type_node;
3782 if (mode == UQQmode)
3783 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3784 if (mode == UHQmode)
3785 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3786 if (mode == USQmode)
3787 return unsignedp ? sat_usq_type_node : usq_type_node;
3788 if (mode == UDQmode)
3789 return unsignedp ? sat_udq_type_node : udq_type_node;
3790 if (mode == UTQmode)
3791 return unsignedp ? sat_utq_type_node : utq_type_node;
3793 if (mode == HAmode)
3794 return unsignedp ? sat_ha_type_node : ha_type_node;
3795 if (mode == SAmode)
3796 return unsignedp ? sat_sa_type_node : sa_type_node;
3797 if (mode == DAmode)
3798 return unsignedp ? sat_da_type_node : da_type_node;
3799 if (mode == TAmode)
3800 return unsignedp ? sat_ta_type_node : ta_type_node;
3802 if (mode == UHAmode)
3803 return unsignedp ? sat_uha_type_node : uha_type_node;
3804 if (mode == USAmode)
3805 return unsignedp ? sat_usa_type_node : usa_type_node;
3806 if (mode == UDAmode)
3807 return unsignedp ? sat_uda_type_node : uda_type_node;
3808 if (mode == UTAmode)
3809 return unsignedp ? sat_uta_type_node : uta_type_node;
3812 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3813 if (TYPE_MODE (TREE_VALUE (t)) == mode
3814 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3815 return TREE_VALUE (t);
3817 return 0;
3820 tree
3821 c_common_unsigned_type (tree type)
3823 return c_common_signed_or_unsigned_type (1, type);
3826 /* Return a signed type the same as TYPE in other respects. */
3828 tree
3829 c_common_signed_type (tree type)
3831 return c_common_signed_or_unsigned_type (0, type);
3834 /* Return a type the same as TYPE except unsigned or
3835 signed according to UNSIGNEDP. */
3837 tree
3838 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3840 tree type1;
3841 int i;
3843 /* This block of code emulates the behavior of the old
3844 c_common_unsigned_type. In particular, it returns
3845 long_unsigned_type_node if passed a long, even when a int would
3846 have the same size. This is necessary for warnings to work
3847 correctly in archs where sizeof(int) == sizeof(long) */
3849 type1 = TYPE_MAIN_VARIANT (type);
3850 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3851 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3852 if (type1 == integer_type_node || type1 == unsigned_type_node)
3853 return unsignedp ? unsigned_type_node : integer_type_node;
3854 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3855 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3856 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3857 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3858 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3859 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3861 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3862 if (int_n_enabled_p[i]
3863 && (type1 == int_n_trees[i].unsigned_type
3864 || type1 == int_n_trees[i].signed_type))
3865 return (unsignedp ? int_n_trees[i].unsigned_type
3866 : int_n_trees[i].signed_type);
3868 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3869 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3870 #if HOST_BITS_PER_WIDE_INT >= 64
3871 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3872 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3873 #endif
3874 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3875 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3876 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3877 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3878 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3879 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3880 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3881 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3883 #define C_COMMON_FIXED_TYPES(NAME) \
3884 if (type1 == short_ ## NAME ## _type_node \
3885 || type1 == unsigned_short_ ## NAME ## _type_node) \
3886 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3887 : short_ ## NAME ## _type_node; \
3888 if (type1 == NAME ## _type_node \
3889 || type1 == unsigned_ ## NAME ## _type_node) \
3890 return unsignedp ? unsigned_ ## NAME ## _type_node \
3891 : NAME ## _type_node; \
3892 if (type1 == long_ ## NAME ## _type_node \
3893 || type1 == unsigned_long_ ## NAME ## _type_node) \
3894 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3895 : long_ ## NAME ## _type_node; \
3896 if (type1 == long_long_ ## NAME ## _type_node \
3897 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3898 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3899 : long_long_ ## NAME ## _type_node;
3901 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3902 if (type1 == NAME ## _type_node \
3903 || type1 == u ## NAME ## _type_node) \
3904 return unsignedp ? u ## NAME ## _type_node \
3905 : NAME ## _type_node;
3907 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3908 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3909 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3910 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3911 : sat_ ## short_ ## NAME ## _type_node; \
3912 if (type1 == sat_ ## NAME ## _type_node \
3913 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3914 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3915 : sat_ ## NAME ## _type_node; \
3916 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3917 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3918 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3919 : sat_ ## long_ ## NAME ## _type_node; \
3920 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3921 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3922 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3923 : sat_ ## long_long_ ## NAME ## _type_node;
3925 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3926 if (type1 == sat_ ## NAME ## _type_node \
3927 || type1 == sat_ ## u ## NAME ## _type_node) \
3928 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3929 : sat_ ## NAME ## _type_node;
3931 C_COMMON_FIXED_TYPES (fract);
3932 C_COMMON_FIXED_TYPES_SAT (fract);
3933 C_COMMON_FIXED_TYPES (accum);
3934 C_COMMON_FIXED_TYPES_SAT (accum);
3936 C_COMMON_FIXED_MODE_TYPES (qq);
3937 C_COMMON_FIXED_MODE_TYPES (hq);
3938 C_COMMON_FIXED_MODE_TYPES (sq);
3939 C_COMMON_FIXED_MODE_TYPES (dq);
3940 C_COMMON_FIXED_MODE_TYPES (tq);
3941 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3942 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3943 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3944 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3945 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3946 C_COMMON_FIXED_MODE_TYPES (ha);
3947 C_COMMON_FIXED_MODE_TYPES (sa);
3948 C_COMMON_FIXED_MODE_TYPES (da);
3949 C_COMMON_FIXED_MODE_TYPES (ta);
3950 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3951 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3952 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3953 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3955 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3956 the precision; they have precision set to match their range, but
3957 may use a wider mode to match an ABI. If we change modes, we may
3958 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3959 the precision as well, so as to yield correct results for
3960 bit-field types. C++ does not have these separate bit-field
3961 types, and producing a signed or unsigned variant of an
3962 ENUMERAL_TYPE may cause other problems as well. */
3964 if (!INTEGRAL_TYPE_P (type)
3965 || TYPE_UNSIGNED (type) == unsignedp)
3966 return type;
3968 #define TYPE_OK(node) \
3969 (TYPE_MODE (type) == TYPE_MODE (node) \
3970 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3971 if (TYPE_OK (signed_char_type_node))
3972 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3973 if (TYPE_OK (integer_type_node))
3974 return unsignedp ? unsigned_type_node : integer_type_node;
3975 if (TYPE_OK (short_integer_type_node))
3976 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3977 if (TYPE_OK (long_integer_type_node))
3978 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3979 if (TYPE_OK (long_long_integer_type_node))
3980 return (unsignedp ? long_long_unsigned_type_node
3981 : long_long_integer_type_node);
3983 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3984 if (int_n_enabled_p[i]
3985 && TYPE_MODE (type) == int_n_data[i].m
3986 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3987 return (unsignedp ? int_n_trees[i].unsigned_type
3988 : int_n_trees[i].signed_type);
3990 if (TYPE_OK (widest_integer_literal_type_node))
3991 return (unsignedp ? widest_unsigned_literal_type_node
3992 : widest_integer_literal_type_node);
3994 #if HOST_BITS_PER_WIDE_INT >= 64
3995 if (TYPE_OK (intTI_type_node))
3996 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3997 #endif
3998 if (TYPE_OK (intDI_type_node))
3999 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4000 if (TYPE_OK (intSI_type_node))
4001 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4002 if (TYPE_OK (intHI_type_node))
4003 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4004 if (TYPE_OK (intQI_type_node))
4005 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4006 #undef TYPE_OK
4008 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4011 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4013 tree
4014 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4016 int i;
4018 /* Extended integer types of the same width as a standard type have
4019 lesser rank, so those of the same width as int promote to int or
4020 unsigned int and are valid for printf formats expecting int or
4021 unsigned int. To avoid such special cases, avoid creating
4022 extended integer types for bit-fields if a standard integer type
4023 is available. */
4024 if (width == TYPE_PRECISION (integer_type_node))
4025 return unsignedp ? unsigned_type_node : integer_type_node;
4026 if (width == TYPE_PRECISION (signed_char_type_node))
4027 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4028 if (width == TYPE_PRECISION (short_integer_type_node))
4029 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4030 if (width == TYPE_PRECISION (long_integer_type_node))
4031 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4032 if (width == TYPE_PRECISION (long_long_integer_type_node))
4033 return (unsignedp ? long_long_unsigned_type_node
4034 : long_long_integer_type_node);
4035 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4036 if (int_n_enabled_p[i]
4037 && width == int_n_data[i].bitsize)
4038 return (unsignedp ? int_n_trees[i].unsigned_type
4039 : int_n_trees[i].signed_type);
4040 return build_nonstandard_integer_type (width, unsignedp);
4043 /* The C version of the register_builtin_type langhook. */
4045 void
4046 c_register_builtin_type (tree type, const char* name)
4048 tree decl;
4050 decl = build_decl (UNKNOWN_LOCATION,
4051 TYPE_DECL, get_identifier (name), type);
4052 DECL_ARTIFICIAL (decl) = 1;
4053 if (!TYPE_NAME (type))
4054 TYPE_NAME (type) = decl;
4055 pushdecl (decl);
4057 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4060 /* Print an error message for invalid operands to arith operation
4061 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4062 LOCATION is the location of the message. */
4064 void
4065 binary_op_error (location_t location, enum tree_code code,
4066 tree type0, tree type1)
4068 const char *opname;
4070 switch (code)
4072 case PLUS_EXPR:
4073 opname = "+"; break;
4074 case MINUS_EXPR:
4075 opname = "-"; break;
4076 case MULT_EXPR:
4077 opname = "*"; break;
4078 case MAX_EXPR:
4079 opname = "max"; break;
4080 case MIN_EXPR:
4081 opname = "min"; break;
4082 case EQ_EXPR:
4083 opname = "=="; break;
4084 case NE_EXPR:
4085 opname = "!="; break;
4086 case LE_EXPR:
4087 opname = "<="; break;
4088 case GE_EXPR:
4089 opname = ">="; break;
4090 case LT_EXPR:
4091 opname = "<"; break;
4092 case GT_EXPR:
4093 opname = ">"; break;
4094 case LSHIFT_EXPR:
4095 opname = "<<"; break;
4096 case RSHIFT_EXPR:
4097 opname = ">>"; break;
4098 case TRUNC_MOD_EXPR:
4099 case FLOOR_MOD_EXPR:
4100 opname = "%"; break;
4101 case TRUNC_DIV_EXPR:
4102 case FLOOR_DIV_EXPR:
4103 opname = "/"; break;
4104 case BIT_AND_EXPR:
4105 opname = "&"; break;
4106 case BIT_IOR_EXPR:
4107 opname = "|"; break;
4108 case TRUTH_ANDIF_EXPR:
4109 opname = "&&"; break;
4110 case TRUTH_ORIF_EXPR:
4111 opname = "||"; break;
4112 case BIT_XOR_EXPR:
4113 opname = "^"; break;
4114 default:
4115 gcc_unreachable ();
4117 error_at (location,
4118 "invalid operands to binary %s (have %qT and %qT)", opname,
4119 type0, type1);
4122 /* Given an expression as a tree, return its original type. Do this
4123 by stripping any conversion that preserves the sign and precision. */
4124 static tree
4125 expr_original_type (tree expr)
4127 STRIP_SIGN_NOPS (expr);
4128 return TREE_TYPE (expr);
4131 /* Subroutine of build_binary_op, used for comparison operations.
4132 See if the operands have both been converted from subword integer types
4133 and, if so, perhaps change them both back to their original type.
4134 This function is also responsible for converting the two operands
4135 to the proper common type for comparison.
4137 The arguments of this function are all pointers to local variables
4138 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4139 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4141 LOC is the location of the comparison.
4143 If this function returns nonzero, it means that the comparison has
4144 a constant value. What this function returns is an expression for
4145 that value. */
4147 tree
4148 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4149 tree *restype_ptr, enum tree_code *rescode_ptr)
4151 tree type;
4152 tree op0 = *op0_ptr;
4153 tree op1 = *op1_ptr;
4154 int unsignedp0, unsignedp1;
4155 int real1, real2;
4156 tree primop0, primop1;
4157 enum tree_code code = *rescode_ptr;
4159 /* Throw away any conversions to wider types
4160 already present in the operands. */
4162 primop0 = c_common_get_narrower (op0, &unsignedp0);
4163 primop1 = c_common_get_narrower (op1, &unsignedp1);
4165 /* If primopN is first sign-extended from primopN's precision to opN's
4166 precision, then zero-extended from opN's precision to
4167 *restype_ptr precision, shortenings might be invalid. */
4168 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4169 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4170 && !unsignedp0
4171 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4172 primop0 = op0;
4173 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4174 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4175 && !unsignedp1
4176 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4177 primop1 = op1;
4179 /* Handle the case that OP0 does not *contain* a conversion
4180 but it *requires* conversion to FINAL_TYPE. */
4182 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4183 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4184 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4185 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4187 /* If one of the operands must be floated, we cannot optimize. */
4188 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4189 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4191 /* If first arg is constant, swap the args (changing operation
4192 so value is preserved), for canonicalization. Don't do this if
4193 the second arg is 0. */
4195 if (TREE_CONSTANT (primop0)
4196 && !integer_zerop (primop1) && !real_zerop (primop1)
4197 && !fixed_zerop (primop1))
4199 tree tem = primop0;
4200 int temi = unsignedp0;
4201 primop0 = primop1;
4202 primop1 = tem;
4203 tem = op0;
4204 op0 = op1;
4205 op1 = tem;
4206 *op0_ptr = op0;
4207 *op1_ptr = op1;
4208 unsignedp0 = unsignedp1;
4209 unsignedp1 = temi;
4210 temi = real1;
4211 real1 = real2;
4212 real2 = temi;
4214 switch (code)
4216 case LT_EXPR:
4217 code = GT_EXPR;
4218 break;
4219 case GT_EXPR:
4220 code = LT_EXPR;
4221 break;
4222 case LE_EXPR:
4223 code = GE_EXPR;
4224 break;
4225 case GE_EXPR:
4226 code = LE_EXPR;
4227 break;
4228 default:
4229 break;
4231 *rescode_ptr = code;
4234 /* If comparing an integer against a constant more bits wide,
4235 maybe we can deduce a value of 1 or 0 independent of the data.
4236 Or else truncate the constant now
4237 rather than extend the variable at run time.
4239 This is only interesting if the constant is the wider arg.
4240 Also, it is not safe if the constant is unsigned and the
4241 variable arg is signed, since in this case the variable
4242 would be sign-extended and then regarded as unsigned.
4243 Our technique fails in this case because the lowest/highest
4244 possible unsigned results don't follow naturally from the
4245 lowest/highest possible values of the variable operand.
4246 For just EQ_EXPR and NE_EXPR there is another technique that
4247 could be used: see if the constant can be faithfully represented
4248 in the other operand's type, by truncating it and reextending it
4249 and see if that preserves the constant's value. */
4251 if (!real1 && !real2
4252 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4253 && TREE_CODE (primop1) == INTEGER_CST
4254 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4256 int min_gt, max_gt, min_lt, max_lt;
4257 tree maxval, minval;
4258 /* 1 if comparison is nominally unsigned. */
4259 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4260 tree val;
4262 type = c_common_signed_or_unsigned_type (unsignedp0,
4263 TREE_TYPE (primop0));
4265 maxval = TYPE_MAX_VALUE (type);
4266 minval = TYPE_MIN_VALUE (type);
4268 if (unsignedp && !unsignedp0)
4269 *restype_ptr = c_common_signed_type (*restype_ptr);
4271 if (TREE_TYPE (primop1) != *restype_ptr)
4273 /* Convert primop1 to target type, but do not introduce
4274 additional overflow. We know primop1 is an int_cst. */
4275 primop1 = force_fit_type (*restype_ptr,
4276 wide_int::from
4277 (primop1,
4278 TYPE_PRECISION (*restype_ptr),
4279 TYPE_SIGN (TREE_TYPE (primop1))),
4280 0, TREE_OVERFLOW (primop1));
4282 if (type != *restype_ptr)
4284 minval = convert (*restype_ptr, minval);
4285 maxval = convert (*restype_ptr, maxval);
4288 min_gt = tree_int_cst_lt (primop1, minval);
4289 max_gt = tree_int_cst_lt (primop1, maxval);
4290 min_lt = tree_int_cst_lt (minval, primop1);
4291 max_lt = tree_int_cst_lt (maxval, primop1);
4293 val = 0;
4294 /* This used to be a switch, but Genix compiler can't handle that. */
4295 if (code == NE_EXPR)
4297 if (max_lt || min_gt)
4298 val = truthvalue_true_node;
4300 else if (code == EQ_EXPR)
4302 if (max_lt || min_gt)
4303 val = truthvalue_false_node;
4305 else if (code == LT_EXPR)
4307 if (max_lt)
4308 val = truthvalue_true_node;
4309 if (!min_lt)
4310 val = truthvalue_false_node;
4312 else if (code == GT_EXPR)
4314 if (min_gt)
4315 val = truthvalue_true_node;
4316 if (!max_gt)
4317 val = truthvalue_false_node;
4319 else if (code == LE_EXPR)
4321 if (!max_gt)
4322 val = truthvalue_true_node;
4323 if (min_gt)
4324 val = truthvalue_false_node;
4326 else if (code == GE_EXPR)
4328 if (!min_lt)
4329 val = truthvalue_true_node;
4330 if (max_lt)
4331 val = truthvalue_false_node;
4334 /* If primop0 was sign-extended and unsigned comparison specd,
4335 we did a signed comparison above using the signed type bounds.
4336 But the comparison we output must be unsigned.
4338 Also, for inequalities, VAL is no good; but if the signed
4339 comparison had *any* fixed result, it follows that the
4340 unsigned comparison just tests the sign in reverse
4341 (positive values are LE, negative ones GE).
4342 So we can generate an unsigned comparison
4343 against an extreme value of the signed type. */
4345 if (unsignedp && !unsignedp0)
4347 if (val != 0)
4348 switch (code)
4350 case LT_EXPR:
4351 case GE_EXPR:
4352 primop1 = TYPE_MIN_VALUE (type);
4353 val = 0;
4354 break;
4356 case LE_EXPR:
4357 case GT_EXPR:
4358 primop1 = TYPE_MAX_VALUE (type);
4359 val = 0;
4360 break;
4362 default:
4363 break;
4365 type = c_common_unsigned_type (type);
4368 if (TREE_CODE (primop0) != INTEGER_CST)
4370 if (val == truthvalue_false_node)
4371 warning_at (loc, OPT_Wtype_limits,
4372 "comparison is always false due to limited range of data type");
4373 if (val == truthvalue_true_node)
4374 warning_at (loc, OPT_Wtype_limits,
4375 "comparison is always true due to limited range of data type");
4378 if (val != 0)
4380 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4381 if (TREE_SIDE_EFFECTS (primop0))
4382 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4383 return val;
4386 /* Value is not predetermined, but do the comparison
4387 in the type of the operand that is not constant.
4388 TYPE is already properly set. */
4391 /* If either arg is decimal float and the other is float, find the
4392 proper common type to use for comparison. */
4393 else if (real1 && real2
4394 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4395 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4396 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4398 /* If either arg is decimal float and the other is float, fail. */
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 return 0;
4404 else if (real1 && real2
4405 && (TYPE_PRECISION (TREE_TYPE (primop0))
4406 == TYPE_PRECISION (TREE_TYPE (primop1))))
4407 type = TREE_TYPE (primop0);
4409 /* If args' natural types are both narrower than nominal type
4410 and both extend in the same manner, compare them
4411 in the type of the wider arg.
4412 Otherwise must actually extend both to the nominal
4413 common type lest different ways of extending
4414 alter the result.
4415 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4417 else if (unsignedp0 == unsignedp1 && real1 == real2
4418 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4419 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4421 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4422 type = c_common_signed_or_unsigned_type (unsignedp0
4423 || TYPE_UNSIGNED (*restype_ptr),
4424 type);
4425 /* Make sure shorter operand is extended the right way
4426 to match the longer operand. */
4427 primop0
4428 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4429 TREE_TYPE (primop0)),
4430 primop0);
4431 primop1
4432 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4433 TREE_TYPE (primop1)),
4434 primop1);
4436 else
4438 /* Here we must do the comparison on the nominal type
4439 using the args exactly as we received them. */
4440 type = *restype_ptr;
4441 primop0 = op0;
4442 primop1 = op1;
4444 if (!real1 && !real2 && integer_zerop (primop1)
4445 && TYPE_UNSIGNED (*restype_ptr))
4447 tree value = 0;
4448 /* All unsigned values are >= 0, so we warn. However,
4449 if OP0 is a constant that is >= 0, the signedness of
4450 the comparison isn't an issue, so suppress the
4451 warning. */
4452 bool warn =
4453 warn_type_limits && !in_system_header_at (loc)
4454 && !(TREE_CODE (primop0) == INTEGER_CST
4455 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4456 primop0)))
4457 /* Do not warn for enumeration types. */
4458 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4460 switch (code)
4462 case GE_EXPR:
4463 if (warn)
4464 warning_at (loc, OPT_Wtype_limits,
4465 "comparison of unsigned expression >= 0 is always true");
4466 value = truthvalue_true_node;
4467 break;
4469 case LT_EXPR:
4470 if (warn)
4471 warning_at (loc, OPT_Wtype_limits,
4472 "comparison of unsigned expression < 0 is always false");
4473 value = truthvalue_false_node;
4474 break;
4476 default:
4477 break;
4480 if (value != 0)
4482 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4483 if (TREE_SIDE_EFFECTS (primop0))
4484 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4485 primop0, value);
4486 return value;
4491 *op0_ptr = convert (type, primop0);
4492 *op1_ptr = convert (type, primop1);
4494 *restype_ptr = truthvalue_type_node;
4496 return 0;
4499 /* Return a tree for the sum or difference (RESULTCODE says which)
4500 of pointer PTROP and integer INTOP. */
4502 tree
4503 pointer_int_sum (location_t loc, enum tree_code resultcode,
4504 tree ptrop, tree intop, bool complain)
4506 tree size_exp, ret;
4508 /* The result is a pointer of the same type that is being added. */
4509 tree result_type = TREE_TYPE (ptrop);
4511 /* If the pointer lives in UPC shared memory, then
4512 drop the 'shared' qualifier. */
4513 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4514 result_type = build_upc_unshared_type (result_type);
4516 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4518 if (complain && warn_pointer_arith)
4519 pedwarn (loc, OPT_Wpointer_arith,
4520 "pointer of type %<void *%> used in arithmetic");
4521 else if (!complain)
4522 return error_mark_node;
4523 size_exp = integer_one_node;
4525 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4527 if (complain && warn_pointer_arith)
4528 pedwarn (loc, OPT_Wpointer_arith,
4529 "pointer to a function used in arithmetic");
4530 else if (!complain)
4531 return error_mark_node;
4532 size_exp = integer_one_node;
4534 else
4535 size_exp = size_in_bytes (TREE_TYPE (result_type));
4537 /* We are manipulating pointer values, so we don't need to warn
4538 about relying on undefined signed overflow. We disable the
4539 warning here because we use integer types so fold won't know that
4540 they are really pointers. */
4541 fold_defer_overflow_warnings ();
4543 /* If what we are about to multiply by the size of the elements
4544 contains a constant term, apply distributive law
4545 and multiply that constant term separately.
4546 This helps produce common subexpressions. */
4547 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4548 && !TREE_CONSTANT (intop)
4549 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4550 && TREE_CONSTANT (size_exp)
4551 /* If the constant comes from pointer subtraction,
4552 skip this optimization--it would cause an error. */
4553 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4554 /* If the constant is unsigned, and smaller than the pointer size,
4555 then we must skip this optimization. This is because it could cause
4556 an overflow error if the constant is negative but INTOP is not. */
4557 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4558 || (TYPE_PRECISION (TREE_TYPE (intop))
4559 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4561 enum tree_code subcode = resultcode;
4562 tree int_type = TREE_TYPE (intop);
4563 if (TREE_CODE (intop) == MINUS_EXPR)
4564 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4565 /* Convert both subexpression types to the type of intop,
4566 because weird cases involving pointer arithmetic
4567 can result in a sum or difference with different type args. */
4568 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4569 subcode, ptrop,
4570 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4571 intop = convert (int_type, TREE_OPERAND (intop, 0));
4574 /* Convert the integer argument to a type the same size as sizetype
4575 so the multiply won't overflow spuriously. */
4576 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4577 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4578 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4579 TYPE_UNSIGNED (sizetype)), intop);
4581 /* Replace the integer argument with a suitable product by the object size.
4582 Do this multiplication as signed, then convert to the appropriate type
4583 for the pointer operation and disregard an overflow that occurred only
4584 because of the sign-extension change in the latter conversion. */
4586 tree t = build_binary_op (loc,
4587 MULT_EXPR, intop,
4588 convert (TREE_TYPE (intop), size_exp), 1);
4589 intop = convert (sizetype, t);
4590 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4591 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4594 /* Create the sum or difference. */
4595 if (resultcode == MINUS_EXPR)
4596 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4598 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4600 fold_undefer_and_ignore_overflow_warnings ();
4602 return ret;
4605 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4606 and if NON_CONST is known not to be permitted in an evaluated part
4607 of a constant expression. */
4609 tree
4610 c_wrap_maybe_const (tree expr, bool non_const)
4612 bool nowarning = TREE_NO_WARNING (expr);
4613 location_t loc = EXPR_LOCATION (expr);
4615 /* This should never be called for C++. */
4616 if (c_dialect_cxx ())
4617 gcc_unreachable ();
4619 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4620 STRIP_TYPE_NOPS (expr);
4621 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4622 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4623 if (nowarning)
4624 TREE_NO_WARNING (expr) = 1;
4625 protected_set_expr_location (expr, loc);
4627 return expr;
4630 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4631 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4632 around the SAVE_EXPR if needed so that c_fully_fold does not need
4633 to look inside SAVE_EXPRs. */
4635 tree
4636 c_save_expr (tree expr)
4638 bool maybe_const = true;
4639 if (c_dialect_cxx ())
4640 return save_expr (expr);
4641 expr = c_fully_fold (expr, false, &maybe_const);
4642 expr = save_expr (expr);
4643 if (!maybe_const)
4644 expr = c_wrap_maybe_const (expr, true);
4645 return expr;
4648 /* Return whether EXPR is a declaration whose address can never be
4649 NULL. */
4651 bool
4652 decl_with_nonnull_addr_p (const_tree expr)
4654 return (DECL_P (expr)
4655 && (TREE_CODE (expr) == PARM_DECL
4656 || TREE_CODE (expr) == LABEL_DECL
4657 || !DECL_WEAK (expr)));
4660 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4661 or for an `if' or `while' statement or ?..: exp. It should already
4662 have been validated to be of suitable type; otherwise, a bad
4663 diagnostic may result.
4665 The EXPR is located at LOCATION.
4667 This preparation consists of taking the ordinary
4668 representation of an expression expr and producing a valid tree
4669 boolean expression describing whether expr is nonzero. We could
4670 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4671 but we optimize comparisons, &&, ||, and !.
4673 The resulting type should always be `truthvalue_type_node'. */
4675 tree
4676 c_common_truthvalue_conversion (location_t location, tree expr)
4678 switch (TREE_CODE (expr))
4680 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4681 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4682 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4683 case ORDERED_EXPR: case UNORDERED_EXPR:
4684 if (TREE_TYPE (expr) == truthvalue_type_node)
4685 return expr;
4686 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4687 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4688 goto ret;
4690 case TRUTH_ANDIF_EXPR:
4691 case TRUTH_ORIF_EXPR:
4692 case TRUTH_AND_EXPR:
4693 case TRUTH_OR_EXPR:
4694 case TRUTH_XOR_EXPR:
4695 if (TREE_TYPE (expr) == truthvalue_type_node)
4696 return expr;
4697 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4698 c_common_truthvalue_conversion (location,
4699 TREE_OPERAND (expr, 0)),
4700 c_common_truthvalue_conversion (location,
4701 TREE_OPERAND (expr, 1)));
4702 goto ret;
4704 case TRUTH_NOT_EXPR:
4705 if (TREE_TYPE (expr) == truthvalue_type_node)
4706 return expr;
4707 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4708 c_common_truthvalue_conversion (location,
4709 TREE_OPERAND (expr, 0)));
4710 goto ret;
4712 case ERROR_MARK:
4713 return expr;
4715 case INTEGER_CST:
4716 return integer_zerop (expr) ? truthvalue_false_node
4717 : truthvalue_true_node;
4719 case REAL_CST:
4720 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4721 ? truthvalue_true_node
4722 : truthvalue_false_node;
4724 case FIXED_CST:
4725 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4726 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4727 ? truthvalue_true_node
4728 : truthvalue_false_node;
4730 case FUNCTION_DECL:
4731 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4732 /* Fall through. */
4734 case ADDR_EXPR:
4736 tree inner = TREE_OPERAND (expr, 0);
4737 if (decl_with_nonnull_addr_p (inner))
4739 /* Common Ada/Pascal programmer's mistake. */
4740 warning_at (location,
4741 OPT_Waddress,
4742 "the address of %qD will always evaluate as %<true%>",
4743 inner);
4744 return truthvalue_true_node;
4746 break;
4749 case COMPLEX_EXPR:
4750 expr = build_binary_op (EXPR_LOCATION (expr),
4751 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4752 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4753 c_common_truthvalue_conversion (location,
4754 TREE_OPERAND (expr, 0)),
4755 c_common_truthvalue_conversion (location,
4756 TREE_OPERAND (expr, 1)),
4758 goto ret;
4760 case NEGATE_EXPR:
4761 case ABS_EXPR:
4762 case FLOAT_EXPR:
4763 case EXCESS_PRECISION_EXPR:
4764 /* These don't change whether an object is nonzero or zero. */
4765 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4767 case LROTATE_EXPR:
4768 case RROTATE_EXPR:
4769 /* These don't change whether an object is zero or nonzero, but
4770 we can't ignore them if their second arg has side-effects. */
4771 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4773 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4774 TREE_OPERAND (expr, 1),
4775 c_common_truthvalue_conversion
4776 (location, TREE_OPERAND (expr, 0)));
4777 goto ret;
4779 else
4780 return c_common_truthvalue_conversion (location,
4781 TREE_OPERAND (expr, 0));
4783 case COND_EXPR:
4784 /* Distribute the conversion into the arms of a COND_EXPR. */
4785 if (c_dialect_cxx ())
4787 tree op1 = TREE_OPERAND (expr, 1);
4788 tree op2 = TREE_OPERAND (expr, 2);
4789 /* In C++ one of the arms might have void type if it is throw. */
4790 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4791 op1 = c_common_truthvalue_conversion (location, op1);
4792 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4793 op2 = c_common_truthvalue_conversion (location, op2);
4794 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4795 TREE_OPERAND (expr, 0), op1, op2);
4796 goto ret;
4798 else
4800 /* Folding will happen later for C. */
4801 expr = build3 (COND_EXPR, truthvalue_type_node,
4802 TREE_OPERAND (expr, 0),
4803 c_common_truthvalue_conversion (location,
4804 TREE_OPERAND (expr, 1)),
4805 c_common_truthvalue_conversion (location,
4806 TREE_OPERAND (expr, 2)));
4807 goto ret;
4810 CASE_CONVERT:
4812 tree totype = TREE_TYPE (expr);
4813 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4815 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4816 since that affects how `default_conversion' will behave. */
4817 if (TREE_CODE (totype) == REFERENCE_TYPE
4818 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4819 break;
4820 /* Don't strip a conversion from C++0x scoped enum, since they
4821 don't implicitly convert to other types. */
4822 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4823 && ENUM_IS_SCOPED (fromtype))
4824 break;
4825 /* If this isn't narrowing the argument, we can ignore it. */
4826 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4827 return c_common_truthvalue_conversion (location,
4828 TREE_OPERAND (expr, 0));
4830 break;
4832 case MODIFY_EXPR:
4833 if (!TREE_NO_WARNING (expr)
4834 && warn_parentheses)
4836 warning (OPT_Wparentheses,
4837 "suggest parentheses around assignment used as truth value");
4838 TREE_NO_WARNING (expr) = 1;
4840 break;
4842 default:
4843 break;
4846 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4848 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4849 expr = (build_binary_op
4850 (EXPR_LOCATION (expr),
4851 (TREE_SIDE_EFFECTS (expr)
4852 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4853 c_common_truthvalue_conversion
4854 (location,
4855 build_unary_op (location, REALPART_EXPR, t, 0)),
4856 c_common_truthvalue_conversion
4857 (location,
4858 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4859 0));
4860 goto ret;
4863 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4865 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4866 FCONST0 (TYPE_MODE
4867 (TREE_TYPE (expr))));
4868 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4870 else
4871 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4873 ret:
4874 protected_set_expr_location (expr, location);
4875 return expr;
4878 static void def_builtin_1 (enum built_in_function fncode,
4879 const char *name,
4880 enum built_in_class fnclass,
4881 tree fntype, tree libtype,
4882 bool both_p, bool fallback_p, bool nonansi_p,
4883 tree fnattrs, bool implicit_p);
4886 /* Apply the TYPE_QUALS to the new DECL. */
4888 void
4889 c_apply_type_quals_to_decl (int type_quals, tree decl)
4891 tree type = TREE_TYPE (decl);
4893 if (type == error_mark_node)
4894 return;
4896 if ((type_quals & TYPE_QUAL_CONST)
4897 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4898 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4899 constructor can produce constant init, so rely on cp_finish_decl to
4900 clear TREE_READONLY if the variable has non-constant init. */
4901 TREE_READONLY (decl) = 1;
4902 if (type_quals & TYPE_QUAL_VOLATILE)
4904 TREE_SIDE_EFFECTS (decl) = 1;
4905 TREE_THIS_VOLATILE (decl) = 1;
4907 if (type_quals & TYPE_QUAL_RESTRICT)
4909 while (type && TREE_CODE (type) == ARRAY_TYPE)
4910 /* Allow 'restrict' on arrays of pointers.
4911 FIXME currently we just ignore it. */
4912 type = TREE_TYPE (type);
4913 if (!type
4914 || !POINTER_TYPE_P (type)
4915 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4916 error ("invalid use of %<restrict%>");
4918 if (type_quals & TYPE_QUAL_UPC_SHARED)
4920 TREE_SHARED (decl) = 1;
4921 if (type_quals & TYPE_QUAL_UPC_STRICT)
4922 TREE_STRICT(decl) = 1;
4923 else if (type_quals & TYPE_QUAL_UPC_RELAXED)
4924 TREE_RELAXED(decl) = 1;
4925 /* The declaration's type should have been previously defined
4926 as a UPC shared type. */
4927 gcc_assert (upc_shared_type_p (type));
4931 struct c_type_hasher : ggc_hasher<tree>
4933 static hashval_t hash (tree);
4934 static bool equal (tree, tree);
4937 /* Hash function for the problem of multiple type definitions in
4938 different files. This must hash all types that will compare
4939 equal via comptypes to the same value. In practice it hashes
4940 on some of the simple stuff and leaves the details to comptypes. */
4942 hashval_t
4943 c_type_hasher::hash (tree t)
4945 int n_elements;
4946 int shift, size;
4947 tree t2;
4948 switch (TREE_CODE (t))
4950 /* For pointers, hash on pointee type plus some swizzling. */
4951 case POINTER_TYPE:
4952 return hash (TREE_TYPE (t)) ^ 0x3003003;
4953 /* Hash on number of elements and total size. */
4954 case ENUMERAL_TYPE:
4955 shift = 3;
4956 t2 = TYPE_VALUES (t);
4957 break;
4958 case RECORD_TYPE:
4959 shift = 0;
4960 t2 = TYPE_FIELDS (t);
4961 break;
4962 case QUAL_UNION_TYPE:
4963 shift = 1;
4964 t2 = TYPE_FIELDS (t);
4965 break;
4966 case UNION_TYPE:
4967 shift = 2;
4968 t2 = TYPE_FIELDS (t);
4969 break;
4970 default:
4971 gcc_unreachable ();
4973 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4974 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4975 n_elements = list_length (t2);
4976 /* We might have a VLA here. */
4977 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4978 size = 0;
4979 else
4980 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4981 return ((size << 24) | (n_elements << shift));
4984 bool
4985 c_type_hasher::equal (tree t1, tree t2)
4987 return lang_hooks.types_compatible_p (t1, t2);
4990 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4992 /* Return the typed-based alias set for T, which may be an expression
4993 or a type. Return -1 if we don't do anything special. */
4995 alias_set_type
4996 c_common_get_alias_set (tree t)
4998 tree u;
5000 /* For VLAs, use the alias set of the element type rather than the
5001 default of alias set 0 for types compared structurally. */
5002 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5004 if (TREE_CODE (t) == ARRAY_TYPE)
5005 return get_alias_set (TREE_TYPE (t));
5006 return -1;
5009 /* Permit type-punning when accessing a union, provided the access
5010 is directly through the union. For example, this code does not
5011 permit taking the address of a union member and then storing
5012 through it. Even the type-punning allowed here is a GCC
5013 extension, albeit a common and useful one; the C standard says
5014 that such accesses have implementation-defined behavior. */
5015 for (u = t;
5016 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5017 u = TREE_OPERAND (u, 0))
5018 if (TREE_CODE (u) == COMPONENT_REF
5019 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5020 return 0;
5022 /* That's all the expressions we handle specially. */
5023 if (!TYPE_P (t))
5024 return -1;
5026 /* The C standard guarantees that any object may be accessed via an
5027 lvalue that has character type. */
5028 if (t == char_type_node
5029 || t == signed_char_type_node
5030 || t == unsigned_char_type_node)
5031 return 0;
5033 /* The C standard specifically allows aliasing between signed and
5034 unsigned variants of the same type. We treat the signed
5035 variant as canonical. */
5036 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
5038 tree t1 = c_common_signed_type (t);
5040 /* t1 == t can happen for boolean nodes which are always unsigned. */
5041 if (t1 != t)
5042 return get_alias_set (t1);
5045 /* For the time being, make UPC pointers-to-shared conflict
5046 with everything else. Ideally, UPC pointers-to-shared should
5047 only conflict with the internal type used to represent
5048 the UPC pointer-to-shared (i.e., upc_pts_rep_type_node). */
5050 if (TYPE_P (t) ? (TREE_CODE (t) == POINTER_TYPE
5051 && upc_shared_type_p (TREE_TYPE (t)))
5052 : (TREE_TYPE(t)
5053 && TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
5054 && upc_shared_type_p (TREE_TYPE (TREE_TYPE (t)))))
5055 return 0;
5057 /* Handle the case of multiple type nodes referring to "the same" type,
5058 which occurs with IMA. These share an alias set. FIXME: Currently only
5059 C90 is handled. (In C99 type compatibility is not transitive, which
5060 complicates things mightily. The alias set splay trees can theoretically
5061 represent this, but insertion is tricky when you consider all the
5062 different orders things might arrive in.) */
5064 if (c_language != clk_c || flag_isoc99)
5065 return -1;
5067 /* Save time if there's only one input file. */
5068 if (num_in_fnames == 1)
5069 return -1;
5071 /* Pointers need special handling if they point to any type that
5072 needs special handling (below). */
5073 if (TREE_CODE (t) == POINTER_TYPE)
5075 tree t2;
5076 /* Find bottom type under any nested POINTERs. */
5077 for (t2 = TREE_TYPE (t);
5078 TREE_CODE (t2) == POINTER_TYPE;
5079 t2 = TREE_TYPE (t2))
5081 if (TREE_CODE (t2) != RECORD_TYPE
5082 && TREE_CODE (t2) != ENUMERAL_TYPE
5083 && TREE_CODE (t2) != QUAL_UNION_TYPE
5084 && TREE_CODE (t2) != UNION_TYPE)
5085 return -1;
5086 if (TYPE_SIZE (t2) == 0)
5087 return -1;
5089 /* These are the only cases that need special handling. */
5090 if (TREE_CODE (t) != RECORD_TYPE
5091 && TREE_CODE (t) != ENUMERAL_TYPE
5092 && TREE_CODE (t) != QUAL_UNION_TYPE
5093 && TREE_CODE (t) != UNION_TYPE
5094 && TREE_CODE (t) != POINTER_TYPE)
5095 return -1;
5096 /* Undefined? */
5097 if (TYPE_SIZE (t) == 0)
5098 return -1;
5100 /* Look up t in hash table. Only one of the compatible types within each
5101 alias set is recorded in the table. */
5102 if (!type_hash_table)
5103 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5104 tree *slot = type_hash_table->find_slot (t, INSERT);
5105 if (*slot != NULL)
5107 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5108 return TYPE_ALIAS_SET ((tree)*slot);
5110 else
5111 /* Our caller will assign and record (in t) a new alias set; all we need
5112 to do is remember t in the hash table. */
5113 *slot = t;
5115 return -1;
5118 /* Return the value of THREADS.
5120 UPC defines a reserved variable, THREADS, which returns the
5121 number of threads that will be created when the UPC program
5122 executes. The value of threads can be specified at runtime via
5123 the -fupc-threads=N switch, where N is an integer specifying
5124 the number of threads. When the value of THREADS is specified
5125 at compile-time, this is called the "static threads compilation
5126 environment".
5128 In the static threads compilation environment, THREADS is a
5129 pre-defined preprocessor macro with the value, N.
5131 If no value for threads is given at compile-time, then the value
5132 must be specified when the application program is executed.
5133 This method of establishing the value of THREADS is called
5134 the "dynamic threads compilation environment". */
5136 tree
5137 upc_num_threads (void)
5139 tree n;
5140 gcc_assert (flag_upc);
5141 n = flag_upc_threads ? ssize_int (flag_upc_threads)
5142 : lookup_name (get_identifier ("THREADS"));
5143 if (!n)
5145 error ("the UPC-required THREADS variable is undefined; "
5146 "when compiling pre-processed source, "
5147 "all -fupc-* switches must be passed on the command line, "
5148 "asserting the same values as supplied when the "
5149 "original source file was preprocessed");
5150 abort ();
5153 return n;
5156 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5157 the IS_SIZEOF parameter indicates which operator is being applied.
5158 The COMPLAIN flag controls whether we should diagnose possibly
5159 ill-formed constructs or not. LOC is the location of the SIZEOF or
5160 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5161 a type in any context should be returned, rather than the normal
5162 alignment for that type. */
5164 tree
5165 c_sizeof_or_alignof_type (location_t loc,
5166 tree type, bool is_sizeof, bool min_alignof,
5167 int complain)
5169 const char *op_name;
5170 tree value = NULL;
5171 enum tree_code type_code = TREE_CODE (type);
5173 op_name = is_sizeof ? "sizeof" : "__alignof__";
5175 if (type_code == FUNCTION_TYPE)
5177 if (is_sizeof)
5179 if (complain && warn_pointer_arith)
5180 pedwarn (loc, OPT_Wpointer_arith,
5181 "invalid application of %<sizeof%> to a function type");
5182 else if (!complain)
5183 return error_mark_node;
5184 value = size_one_node;
5186 else
5188 if (complain)
5190 if (c_dialect_cxx ())
5191 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5192 "%<alignof%> applied to a function type");
5193 else
5194 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5195 "%<_Alignof%> applied to a function type");
5197 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5200 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5202 if (complain)
5204 if (type_code == VOID_TYPE && upc_shared_type_p (type))
5205 error_at (loc, "invalid application of %qs"
5206 " to %<shared void%> type", op_name);
5207 else if (type_code == VOID_TYPE && warn_pointer_arith)
5208 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
5209 "invalid application of %qs to a void type", op_name);
5211 else
5212 return error_mark_node;
5213 value = size_one_node;
5215 else if (!COMPLETE_TYPE_P (type)
5216 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5218 if (complain)
5219 error_at (loc, "invalid application of %qs to incomplete type %qT",
5220 op_name, type);
5221 return error_mark_node;
5223 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5224 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5226 if (complain)
5227 error_at (loc, "invalid application of %qs to array type %qT of "
5228 "incomplete element type", op_name, type);
5229 return error_mark_node;
5231 else
5233 if (is_sizeof)
5234 /* Convert in case a char is more than one unit. */
5235 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5236 size_int (TYPE_PRECISION (char_type_node)
5237 / BITS_PER_UNIT));
5238 else if (min_alignof)
5239 value = size_int (min_align_of_type (type));
5240 else
5241 value = size_int (TYPE_ALIGN_UNIT (type));
5244 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
5245 && upc_shared_type_p (type)
5246 && TYPE_HAS_THREADS_FACTOR (type))
5248 const tree n_threads = convert (sizetype, upc_num_threads ());
5249 value = size_binop (MULT_EXPR, value, n_threads);
5252 /* VALUE will have the middle-end integer type sizetype.
5253 However, we should really return a value of type `size_t',
5254 which is just a typedef for an ordinary integer type. */
5255 value = fold_convert_loc (loc, size_type_node, value);
5257 return value;
5260 /* Implement the __alignof keyword: Return the minimum required
5261 alignment of EXPR, measured in bytes. For VAR_DECLs,
5262 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5263 from an "aligned" __attribute__ specification). LOC is the
5264 location of the ALIGNOF operator. */
5266 tree
5267 c_alignof_expr (location_t loc, tree expr)
5269 tree t;
5271 if (VAR_OR_FUNCTION_DECL_P (expr))
5272 t = size_int (DECL_ALIGN_UNIT (expr));
5274 else if (TREE_CODE (expr) == COMPONENT_REF
5275 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5277 error_at (loc, "%<__alignof%> applied to a bit-field");
5278 t = size_one_node;
5280 else if (TREE_CODE (expr) == COMPONENT_REF
5281 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5282 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5284 else if (TREE_CODE (expr) == INDIRECT_REF)
5286 tree t = TREE_OPERAND (expr, 0);
5287 tree best = t;
5288 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5290 while (CONVERT_EXPR_P (t)
5291 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5293 int thisalign;
5295 t = TREE_OPERAND (t, 0);
5296 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5297 if (thisalign > bestalign)
5298 best = t, bestalign = thisalign;
5300 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5302 else
5303 return c_alignof (loc, TREE_TYPE (expr));
5305 return fold_convert_loc (loc, size_type_node, t);
5308 /* Handle C and C++ default attributes. */
5310 enum built_in_attribute
5312 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5313 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5314 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5315 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5316 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5317 #include "builtin-attrs.def"
5318 #undef DEF_ATTR_NULL_TREE
5319 #undef DEF_ATTR_INT
5320 #undef DEF_ATTR_STRING
5321 #undef DEF_ATTR_IDENT
5322 #undef DEF_ATTR_TREE_LIST
5323 ATTR_LAST
5326 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5328 static void c_init_attributes (void);
5330 enum c_builtin_type
5332 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5333 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5334 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5335 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5336 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5337 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5338 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5339 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5340 ARG6) NAME,
5341 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5342 ARG6, ARG7) NAME,
5343 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5344 ARG6, ARG7, ARG8) NAME,
5345 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5346 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5347 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5348 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5349 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5350 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5351 NAME,
5352 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5353 ARG6, ARG7) NAME,
5354 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5355 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5356 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5357 #include "builtin-types.def"
5358 #undef DEF_PRIMITIVE_TYPE
5359 #undef DEF_FUNCTION_TYPE_0
5360 #undef DEF_FUNCTION_TYPE_1
5361 #undef DEF_FUNCTION_TYPE_2
5362 #undef DEF_FUNCTION_TYPE_3
5363 #undef DEF_FUNCTION_TYPE_4
5364 #undef DEF_FUNCTION_TYPE_5
5365 #undef DEF_FUNCTION_TYPE_6
5366 #undef DEF_FUNCTION_TYPE_7
5367 #undef DEF_FUNCTION_TYPE_8
5368 #undef DEF_FUNCTION_TYPE_VAR_0
5369 #undef DEF_FUNCTION_TYPE_VAR_1
5370 #undef DEF_FUNCTION_TYPE_VAR_2
5371 #undef DEF_FUNCTION_TYPE_VAR_3
5372 #undef DEF_FUNCTION_TYPE_VAR_4
5373 #undef DEF_FUNCTION_TYPE_VAR_5
5374 #undef DEF_FUNCTION_TYPE_VAR_7
5375 #undef DEF_FUNCTION_TYPE_VAR_11
5376 #undef DEF_POINTER_TYPE
5377 BT_LAST
5380 typedef enum c_builtin_type builtin_type;
5382 /* A temporary array for c_common_nodes_and_builtins. Used in
5383 communication with def_fn_type. */
5384 static tree builtin_types[(int) BT_LAST + 1];
5386 /* A helper function for c_common_nodes_and_builtins. Build function type
5387 for DEF with return type RET and N arguments. If VAR is true, then the
5388 function should be variadic after those N arguments.
5390 Takes special care not to ICE if any of the types involved are
5391 error_mark_node, which indicates that said type is not in fact available
5392 (see builtin_type_for_size). In which case the function type as a whole
5393 should be error_mark_node. */
5395 static void
5396 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5398 tree t;
5399 tree *args = XALLOCAVEC (tree, n);
5400 va_list list;
5401 int i;
5403 va_start (list, n);
5404 for (i = 0; i < n; ++i)
5406 builtin_type a = (builtin_type) va_arg (list, int);
5407 t = builtin_types[a];
5408 if (t == error_mark_node)
5409 goto egress;
5410 args[i] = t;
5413 t = builtin_types[ret];
5414 if (t == error_mark_node)
5415 goto egress;
5416 if (var)
5417 t = build_varargs_function_type_array (t, n, args);
5418 else
5419 t = build_function_type_array (t, n, args);
5421 egress:
5422 builtin_types[def] = t;
5423 va_end (list);
5426 /* Build builtin functions common to both C and C++ language
5427 frontends. */
5429 static void
5430 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5432 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5433 builtin_types[ENUM] = VALUE;
5434 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5435 def_fn_type (ENUM, RETURN, 0, 0);
5436 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5437 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5438 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5439 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5440 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5441 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5442 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5443 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5444 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5445 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5446 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5447 ARG6) \
5448 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5449 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5450 ARG6, ARG7) \
5451 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5452 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5453 ARG6, ARG7, ARG8) \
5454 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5455 ARG7, ARG8);
5456 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5457 def_fn_type (ENUM, RETURN, 1, 0);
5458 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5459 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5460 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5461 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5462 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5463 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5464 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5465 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5466 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5467 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5468 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5469 ARG6, ARG7) \
5470 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5471 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5472 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5473 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5474 ARG7, ARG8, ARG9, ARG10, ARG11);
5475 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5476 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5478 #include "builtin-types.def"
5480 #undef DEF_PRIMITIVE_TYPE
5481 #undef DEF_FUNCTION_TYPE_0
5482 #undef DEF_FUNCTION_TYPE_1
5483 #undef DEF_FUNCTION_TYPE_2
5484 #undef DEF_FUNCTION_TYPE_3
5485 #undef DEF_FUNCTION_TYPE_4
5486 #undef DEF_FUNCTION_TYPE_5
5487 #undef DEF_FUNCTION_TYPE_6
5488 #undef DEF_FUNCTION_TYPE_7
5489 #undef DEF_FUNCTION_TYPE_8
5490 #undef DEF_FUNCTION_TYPE_VAR_0
5491 #undef DEF_FUNCTION_TYPE_VAR_1
5492 #undef DEF_FUNCTION_TYPE_VAR_2
5493 #undef DEF_FUNCTION_TYPE_VAR_3
5494 #undef DEF_FUNCTION_TYPE_VAR_4
5495 #undef DEF_FUNCTION_TYPE_VAR_5
5496 #undef DEF_FUNCTION_TYPE_VAR_7
5497 #undef DEF_FUNCTION_TYPE_VAR_11
5498 #undef DEF_POINTER_TYPE
5499 builtin_types[(int) BT_LAST] = NULL_TREE;
5501 c_init_attributes ();
5503 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5504 NONANSI_P, ATTRS, IMPLICIT, COND) \
5505 if (NAME && COND) \
5506 def_builtin_1 (ENUM, NAME, CLASS, \
5507 builtin_types[(int) TYPE], \
5508 builtin_types[(int) LIBTYPE], \
5509 BOTH_P, FALLBACK_P, NONANSI_P, \
5510 built_in_attributes[(int) ATTRS], IMPLICIT);
5511 #include "builtins.def"
5512 #undef DEF_BUILTIN
5514 targetm.init_builtins ();
5516 build_common_builtin_nodes ();
5518 if (flag_cilkplus)
5519 cilk_init_builtins ();
5522 /* Like get_identifier, but avoid warnings about null arguments when
5523 the argument may be NULL for targets where GCC lacks stdint.h type
5524 information. */
5526 static inline tree
5527 c_get_ident (const char *id)
5529 return get_identifier (id);
5532 /* Build tree nodes and builtin functions common to both C and C++ language
5533 frontends. */
5535 void
5536 c_common_nodes_and_builtins (void)
5538 int char16_type_size;
5539 int char32_type_size;
5540 int wchar_type_size;
5541 tree array_domain_type;
5542 tree va_list_ref_type_node;
5543 tree va_list_arg_type_node;
5544 int i;
5546 build_common_tree_nodes (flag_signed_char, flag_short_double);
5548 /* Define `int' and `char' first so that dbx will output them first. */
5549 record_builtin_type (RID_INT, NULL, integer_type_node);
5550 record_builtin_type (RID_CHAR, "char", char_type_node);
5552 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5553 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5554 but not C. Are the conditionals here needed? */
5555 if (c_dialect_cxx ())
5556 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5557 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5558 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5559 record_builtin_type (RID_MAX, "long unsigned int",
5560 long_unsigned_type_node);
5562 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5564 char name[25];
5566 sprintf (name, "__int%d", int_n_data[i].bitsize);
5567 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), xstrdup (name),
5568 int_n_trees[i].signed_type);
5569 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5570 record_builtin_type (RID_MAX, xstrdup (name),
5571 int_n_trees[i].unsigned_type);
5574 if (c_dialect_cxx ())
5575 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5576 record_builtin_type (RID_MAX, "long long int",
5577 long_long_integer_type_node);
5578 record_builtin_type (RID_MAX, "long long unsigned int",
5579 long_long_unsigned_type_node);
5580 if (c_dialect_cxx ())
5581 record_builtin_type (RID_MAX, "long long unsigned",
5582 long_long_unsigned_type_node);
5583 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5584 record_builtin_type (RID_MAX, "short unsigned int",
5585 short_unsigned_type_node);
5586 if (c_dialect_cxx ())
5587 record_builtin_type (RID_MAX, "unsigned short",
5588 short_unsigned_type_node);
5590 /* Define both `signed char' and `unsigned char'. */
5591 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5592 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5594 /* These are types that c_common_type_for_size and
5595 c_common_type_for_mode use. */
5596 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5597 TYPE_DECL, NULL_TREE,
5598 intQI_type_node));
5599 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5600 TYPE_DECL, NULL_TREE,
5601 intHI_type_node));
5602 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5603 TYPE_DECL, NULL_TREE,
5604 intSI_type_node));
5605 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5606 TYPE_DECL, NULL_TREE,
5607 intDI_type_node));
5608 #if HOST_BITS_PER_WIDE_INT >= 64
5609 /* Note that this is different than the __int128 type that's part of
5610 the generic __intN support. */
5611 if (targetm.scalar_mode_supported_p (TImode))
5612 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5613 TYPE_DECL,
5614 get_identifier ("__int128_t"),
5615 intTI_type_node));
5616 #endif
5617 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5618 TYPE_DECL, NULL_TREE,
5619 unsigned_intQI_type_node));
5620 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5621 TYPE_DECL, NULL_TREE,
5622 unsigned_intHI_type_node));
5623 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5624 TYPE_DECL, NULL_TREE,
5625 unsigned_intSI_type_node));
5626 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5627 TYPE_DECL, NULL_TREE,
5628 unsigned_intDI_type_node));
5629 #if HOST_BITS_PER_WIDE_INT >= 64
5630 if (targetm.scalar_mode_supported_p (TImode))
5631 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5632 TYPE_DECL,
5633 get_identifier ("__uint128_t"),
5634 unsigned_intTI_type_node));
5635 #endif
5637 /* Create the widest literal types. */
5638 widest_integer_literal_type_node
5639 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5640 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5641 TYPE_DECL, NULL_TREE,
5642 widest_integer_literal_type_node));
5644 widest_unsigned_literal_type_node
5645 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5646 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5647 TYPE_DECL, NULL_TREE,
5648 widest_unsigned_literal_type_node));
5650 signed_size_type_node = c_common_signed_type (size_type_node);
5652 pid_type_node =
5653 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5655 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5656 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5657 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5659 /* Only supported decimal floating point extension if the target
5660 actually supports underlying modes. */
5661 if (targetm.scalar_mode_supported_p (SDmode)
5662 && targetm.scalar_mode_supported_p (DDmode)
5663 && targetm.scalar_mode_supported_p (TDmode))
5665 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5666 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5667 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5670 if (targetm.fixed_point_supported_p ())
5672 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5673 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5674 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5675 record_builtin_type (RID_MAX, "long long _Fract",
5676 long_long_fract_type_node);
5677 record_builtin_type (RID_MAX, "unsigned short _Fract",
5678 unsigned_short_fract_type_node);
5679 record_builtin_type (RID_MAX, "unsigned _Fract",
5680 unsigned_fract_type_node);
5681 record_builtin_type (RID_MAX, "unsigned long _Fract",
5682 unsigned_long_fract_type_node);
5683 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5684 unsigned_long_long_fract_type_node);
5685 record_builtin_type (RID_MAX, "_Sat short _Fract",
5686 sat_short_fract_type_node);
5687 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5688 record_builtin_type (RID_MAX, "_Sat long _Fract",
5689 sat_long_fract_type_node);
5690 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5691 sat_long_long_fract_type_node);
5692 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5693 sat_unsigned_short_fract_type_node);
5694 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5695 sat_unsigned_fract_type_node);
5696 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5697 sat_unsigned_long_fract_type_node);
5698 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5699 sat_unsigned_long_long_fract_type_node);
5700 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5701 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5702 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5703 record_builtin_type (RID_MAX, "long long _Accum",
5704 long_long_accum_type_node);
5705 record_builtin_type (RID_MAX, "unsigned short _Accum",
5706 unsigned_short_accum_type_node);
5707 record_builtin_type (RID_MAX, "unsigned _Accum",
5708 unsigned_accum_type_node);
5709 record_builtin_type (RID_MAX, "unsigned long _Accum",
5710 unsigned_long_accum_type_node);
5711 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5712 unsigned_long_long_accum_type_node);
5713 record_builtin_type (RID_MAX, "_Sat short _Accum",
5714 sat_short_accum_type_node);
5715 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5716 record_builtin_type (RID_MAX, "_Sat long _Accum",
5717 sat_long_accum_type_node);
5718 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5719 sat_long_long_accum_type_node);
5720 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5721 sat_unsigned_short_accum_type_node);
5722 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5723 sat_unsigned_accum_type_node);
5724 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5725 sat_unsigned_long_accum_type_node);
5726 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5727 sat_unsigned_long_long_accum_type_node);
5731 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5732 TYPE_DECL,
5733 get_identifier ("complex int"),
5734 complex_integer_type_node));
5735 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5736 TYPE_DECL,
5737 get_identifier ("complex float"),
5738 complex_float_type_node));
5739 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5740 TYPE_DECL,
5741 get_identifier ("complex double"),
5742 complex_double_type_node));
5743 lang_hooks.decls.pushdecl
5744 (build_decl (UNKNOWN_LOCATION,
5745 TYPE_DECL, get_identifier ("complex long double"),
5746 complex_long_double_type_node));
5748 if (c_dialect_cxx ())
5749 /* For C++, make fileptr_type_node a distinct void * type until
5750 FILE type is defined. */
5751 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5753 record_builtin_type (RID_VOID, NULL, void_type_node);
5755 /* Set the TYPE_NAME for any variants that were built before
5756 record_builtin_type gave names to the built-in types. */
5758 tree void_name = TYPE_NAME (void_type_node);
5759 TYPE_NAME (void_type_node) = NULL_TREE;
5760 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5761 = void_name;
5762 TYPE_NAME (void_type_node) = void_name;
5765 void_list_node = build_void_list_node ();
5767 /* Make a type to be the domain of a few array types
5768 whose domains don't really matter.
5769 200 is small enough that it always fits in size_t
5770 and large enough that it can hold most function names for the
5771 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5772 array_domain_type = build_index_type (size_int (200));
5774 /* Make a type for arrays of characters.
5775 With luck nothing will ever really depend on the length of this
5776 array type. */
5777 char_array_type_node
5778 = build_array_type (char_type_node, array_domain_type);
5780 string_type_node = build_pointer_type (char_type_node);
5781 const_string_type_node
5782 = build_pointer_type (build_qualified_type
5783 (char_type_node, TYPE_QUAL_CONST));
5785 /* This is special for C++ so functions can be overloaded. */
5786 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5787 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5788 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5789 underlying_wchar_type_node = wchar_type_node;
5790 if (c_dialect_cxx ())
5792 if (TYPE_UNSIGNED (wchar_type_node))
5793 wchar_type_node = make_unsigned_type (wchar_type_size);
5794 else
5795 wchar_type_node = make_signed_type (wchar_type_size);
5796 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5799 /* This is for wide string constants. */
5800 wchar_array_type_node
5801 = build_array_type (wchar_type_node, array_domain_type);
5803 /* Define 'char16_t'. */
5804 char16_type_node = get_identifier (CHAR16_TYPE);
5805 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5806 char16_type_size = TYPE_PRECISION (char16_type_node);
5807 if (c_dialect_cxx ())
5809 char16_type_node = make_unsigned_type (char16_type_size);
5811 if (cxx_dialect >= cxx11)
5812 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5815 /* This is for UTF-16 string constants. */
5816 char16_array_type_node
5817 = build_array_type (char16_type_node, array_domain_type);
5819 /* Define 'char32_t'. */
5820 char32_type_node = get_identifier (CHAR32_TYPE);
5821 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5822 char32_type_size = TYPE_PRECISION (char32_type_node);
5823 if (c_dialect_cxx ())
5825 char32_type_node = make_unsigned_type (char32_type_size);
5827 if (cxx_dialect >= cxx11)
5828 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5831 /* This is for UTF-32 string constants. */
5832 char32_array_type_node
5833 = build_array_type (char32_type_node, array_domain_type);
5835 wint_type_node =
5836 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5838 intmax_type_node =
5839 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5840 uintmax_type_node =
5841 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5843 if (SIG_ATOMIC_TYPE)
5844 sig_atomic_type_node =
5845 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5846 if (INT8_TYPE)
5847 int8_type_node =
5848 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5849 if (INT16_TYPE)
5850 int16_type_node =
5851 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5852 if (INT32_TYPE)
5853 int32_type_node =
5854 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5855 if (INT64_TYPE)
5856 int64_type_node =
5857 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5858 if (UINT8_TYPE)
5859 uint8_type_node =
5860 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5861 if (UINT16_TYPE)
5862 c_uint16_type_node = uint16_type_node =
5863 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5864 if (UINT32_TYPE)
5865 c_uint32_type_node = uint32_type_node =
5866 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5867 if (UINT64_TYPE)
5868 c_uint64_type_node = uint64_type_node =
5869 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5870 if (INT_LEAST8_TYPE)
5871 int_least8_type_node =
5872 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5873 if (INT_LEAST16_TYPE)
5874 int_least16_type_node =
5875 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5876 if (INT_LEAST32_TYPE)
5877 int_least32_type_node =
5878 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5879 if (INT_LEAST64_TYPE)
5880 int_least64_type_node =
5881 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5882 if (UINT_LEAST8_TYPE)
5883 uint_least8_type_node =
5884 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5885 if (UINT_LEAST16_TYPE)
5886 uint_least16_type_node =
5887 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5888 if (UINT_LEAST32_TYPE)
5889 uint_least32_type_node =
5890 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5891 if (UINT_LEAST64_TYPE)
5892 uint_least64_type_node =
5893 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5894 if (INT_FAST8_TYPE)
5895 int_fast8_type_node =
5896 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5897 if (INT_FAST16_TYPE)
5898 int_fast16_type_node =
5899 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5900 if (INT_FAST32_TYPE)
5901 int_fast32_type_node =
5902 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5903 if (INT_FAST64_TYPE)
5904 int_fast64_type_node =
5905 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5906 if (UINT_FAST8_TYPE)
5907 uint_fast8_type_node =
5908 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5909 if (UINT_FAST16_TYPE)
5910 uint_fast16_type_node =
5911 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5912 if (UINT_FAST32_TYPE)
5913 uint_fast32_type_node =
5914 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5915 if (UINT_FAST64_TYPE)
5916 uint_fast64_type_node =
5917 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5918 if (INTPTR_TYPE)
5919 intptr_type_node =
5920 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5921 if (UINTPTR_TYPE)
5922 uintptr_type_node =
5923 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5925 default_function_type
5926 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5927 ptrdiff_type_node
5928 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5929 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5931 lang_hooks.decls.pushdecl
5932 (build_decl (UNKNOWN_LOCATION,
5933 TYPE_DECL, get_identifier ("__builtin_va_list"),
5934 va_list_type_node));
5935 if (targetm.enum_va_list_p)
5937 int l;
5938 const char *pname;
5939 tree ptype;
5941 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5943 lang_hooks.decls.pushdecl
5944 (build_decl (UNKNOWN_LOCATION,
5945 TYPE_DECL, get_identifier (pname),
5946 ptype));
5951 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5953 va_list_arg_type_node = va_list_ref_type_node =
5954 build_pointer_type (TREE_TYPE (va_list_type_node));
5956 else
5958 va_list_arg_type_node = va_list_type_node;
5959 va_list_ref_type_node = build_reference_type (va_list_type_node);
5962 if (!flag_preprocess_only)
5963 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5965 main_identifier_node = get_identifier ("main");
5967 /* Create the built-in __null node. It is important that this is
5968 not shared. */
5969 null_node = make_int_cst (1, 1);
5970 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5972 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5973 memset (builtin_types, 0, sizeof (builtin_types));
5976 /* The number of named compound-literals generated thus far. */
5977 static GTY(()) int compound_literal_number;
5979 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5981 void
5982 set_compound_literal_name (tree decl)
5984 char *name;
5985 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5986 compound_literal_number);
5987 compound_literal_number++;
5988 DECL_NAME (decl) = get_identifier (name);
5991 tree
5992 build_va_arg (location_t loc, tree expr, tree type)
5994 expr = build1 (VA_ARG_EXPR, type, expr);
5995 SET_EXPR_LOCATION (expr, loc);
5996 return expr;
6000 /* Linked list of disabled built-in functions. */
6002 typedef struct disabled_builtin
6004 const char *name;
6005 struct disabled_builtin *next;
6006 } disabled_builtin;
6007 static disabled_builtin *disabled_builtins = NULL;
6009 static bool builtin_function_disabled_p (const char *);
6011 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6012 begins with "__builtin_", give an error. */
6014 void
6015 disable_builtin_function (const char *name)
6017 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
6018 error ("cannot disable built-in function %qs", name);
6019 else
6021 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6022 new_disabled_builtin->name = name;
6023 new_disabled_builtin->next = disabled_builtins;
6024 disabled_builtins = new_disabled_builtin;
6029 /* Return true if the built-in function NAME has been disabled, false
6030 otherwise. */
6032 static bool
6033 builtin_function_disabled_p (const char *name)
6035 disabled_builtin *p;
6036 for (p = disabled_builtins; p != NULL; p = p->next)
6038 if (strcmp (name, p->name) == 0)
6039 return true;
6041 return false;
6045 /* Worker for DEF_BUILTIN.
6046 Possibly define a builtin function with one or two names.
6047 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6048 nonansi_p and flag_no_nonansi_builtin. */
6050 static void
6051 def_builtin_1 (enum built_in_function fncode,
6052 const char *name,
6053 enum built_in_class fnclass,
6054 tree fntype, tree libtype,
6055 bool both_p, bool fallback_p, bool nonansi_p,
6056 tree fnattrs, bool implicit_p)
6058 tree decl;
6059 const char *libname;
6061 if (fntype == error_mark_node)
6062 return;
6064 gcc_assert ((!both_p && !fallback_p)
6065 || !strncmp (name, "__builtin_",
6066 strlen ("__builtin_")));
6068 libname = name + strlen ("__builtin_");
6069 decl = add_builtin_function (name, fntype, fncode, fnclass,
6070 (fallback_p ? libname : NULL),
6071 fnattrs);
6073 set_builtin_decl (fncode, decl, implicit_p);
6075 if (both_p
6076 && !flag_no_builtin && !builtin_function_disabled_p (libname)
6077 && !(nonansi_p && flag_no_nonansi_builtin))
6078 add_builtin_function (libname, libtype, fncode, fnclass,
6079 NULL, fnattrs);
6082 /* Nonzero if the type T promotes to int. This is (nearly) the
6083 integral promotions defined in ISO C99 6.3.1.1/2. */
6085 bool
6086 c_promoting_integer_type_p (const_tree t)
6088 switch (TREE_CODE (t))
6090 case INTEGER_TYPE:
6091 return (TYPE_MAIN_VARIANT (t) == char_type_node
6092 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6093 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6094 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
6095 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6096 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
6098 case ENUMERAL_TYPE:
6099 /* ??? Technically all enumerations not larger than an int
6100 promote to an int. But this is used along code paths
6101 that only want to notice a size change. */
6102 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6104 case BOOLEAN_TYPE:
6105 return 1;
6107 default:
6108 return 0;
6112 /* Return 1 if PARMS specifies a fixed number of parameters
6113 and none of their types is affected by default promotions. */
6116 self_promoting_args_p (const_tree parms)
6118 const_tree t;
6119 for (t = parms; t; t = TREE_CHAIN (t))
6121 tree type = TREE_VALUE (t);
6123 if (type == error_mark_node)
6124 continue;
6126 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6127 return 0;
6129 if (type == 0)
6130 return 0;
6132 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6133 return 0;
6135 if (c_promoting_integer_type_p (type))
6136 return 0;
6138 return 1;
6141 /* Recursively remove any '*' or '&' operator from TYPE. */
6142 tree
6143 strip_pointer_operator (tree t)
6145 while (POINTER_TYPE_P (t))
6146 t = TREE_TYPE (t);
6147 return t;
6150 /* Recursively remove pointer or array type from TYPE. */
6151 tree
6152 strip_pointer_or_array_types (tree t)
6154 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6155 t = TREE_TYPE (t);
6156 return t;
6159 /* Used to compare case labels. K1 and K2 are actually tree nodes
6160 representing case labels, or NULL_TREE for a `default' label.
6161 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6162 K2, and 0 if K1 and K2 are equal. */
6165 case_compare (splay_tree_key k1, splay_tree_key k2)
6167 /* Consider a NULL key (such as arises with a `default' label) to be
6168 smaller than anything else. */
6169 if (!k1)
6170 return k2 ? -1 : 0;
6171 else if (!k2)
6172 return k1 ? 1 : 0;
6174 return tree_int_cst_compare ((tree) k1, (tree) k2);
6177 /* Process a case label, located at LOC, for the range LOW_VALUE
6178 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6179 then this case label is actually a `default' label. If only
6180 HIGH_VALUE is NULL_TREE, then case label was declared using the
6181 usual C/C++ syntax, rather than the GNU case range extension.
6182 CASES is a tree containing all the case ranges processed so far;
6183 COND is the condition for the switch-statement itself. Returns the
6184 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6185 is created. */
6187 tree
6188 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6189 tree low_value, tree high_value)
6191 tree type;
6192 tree label;
6193 tree case_label;
6194 splay_tree_node node;
6196 /* Create the LABEL_DECL itself. */
6197 label = create_artificial_label (loc);
6199 /* If there was an error processing the switch condition, bail now
6200 before we get more confused. */
6201 if (!cond || cond == error_mark_node)
6202 goto error_out;
6204 if ((low_value && TREE_TYPE (low_value)
6205 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6206 || (high_value && TREE_TYPE (high_value)
6207 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6209 error_at (loc, "pointers are not permitted as case values");
6210 goto error_out;
6213 /* Case ranges are a GNU extension. */
6214 if (high_value)
6215 pedwarn (loc, OPT_Wpedantic,
6216 "range expressions in switch statements are non-standard");
6218 type = TREE_TYPE (cond);
6219 if (low_value)
6221 low_value = check_case_value (loc, low_value);
6222 low_value = convert_and_check (loc, type, low_value);
6223 if (low_value == error_mark_node)
6224 goto error_out;
6226 if (high_value)
6228 high_value = check_case_value (loc, high_value);
6229 high_value = convert_and_check (loc, type, high_value);
6230 if (high_value == error_mark_node)
6231 goto error_out;
6234 if (low_value && high_value)
6236 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6237 really a case range, even though it was written that way.
6238 Remove the HIGH_VALUE to simplify later processing. */
6239 if (tree_int_cst_equal (low_value, high_value))
6240 high_value = NULL_TREE;
6241 else if (!tree_int_cst_lt (low_value, high_value))
6242 warning_at (loc, 0, "empty range specified");
6245 /* See if the case is in range of the type of the original testing
6246 expression. If both low_value and high_value are out of range,
6247 don't insert the case label and return NULL_TREE. */
6248 if (low_value
6249 && !check_case_bounds (loc, type, orig_type,
6250 &low_value, high_value ? &high_value : NULL))
6251 return NULL_TREE;
6253 /* Look up the LOW_VALUE in the table of case labels we already
6254 have. */
6255 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6256 /* If there was not an exact match, check for overlapping ranges.
6257 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6258 that's a `default' label and the only overlap is an exact match. */
6259 if (!node && (low_value || high_value))
6261 splay_tree_node low_bound;
6262 splay_tree_node high_bound;
6264 /* Even though there wasn't an exact match, there might be an
6265 overlap between this case range and another case range.
6266 Since we've (inductively) not allowed any overlapping case
6267 ranges, we simply need to find the greatest low case label
6268 that is smaller that LOW_VALUE, and the smallest low case
6269 label that is greater than LOW_VALUE. If there is an overlap
6270 it will occur in one of these two ranges. */
6271 low_bound = splay_tree_predecessor (cases,
6272 (splay_tree_key) low_value);
6273 high_bound = splay_tree_successor (cases,
6274 (splay_tree_key) low_value);
6276 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6277 the LOW_VALUE, so there is no need to check unless the
6278 LOW_BOUND is in fact itself a case range. */
6279 if (low_bound
6280 && CASE_HIGH ((tree) low_bound->value)
6281 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6282 low_value) >= 0)
6283 node = low_bound;
6284 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6285 range is bigger than the low end of the current range, so we
6286 are only interested if the current range is a real range, and
6287 not an ordinary case label. */
6288 else if (high_bound
6289 && high_value
6290 && (tree_int_cst_compare ((tree) high_bound->key,
6291 high_value)
6292 <= 0))
6293 node = high_bound;
6295 /* If there was an overlap, issue an error. */
6296 if (node)
6298 tree duplicate = CASE_LABEL ((tree) node->value);
6300 if (high_value)
6302 error_at (loc, "duplicate (or overlapping) case value");
6303 error_at (DECL_SOURCE_LOCATION (duplicate),
6304 "this is the first entry overlapping that value");
6306 else if (low_value)
6308 error_at (loc, "duplicate case value") ;
6309 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6311 else
6313 error_at (loc, "multiple default labels in one switch");
6314 error_at (DECL_SOURCE_LOCATION (duplicate),
6315 "this is the first default label");
6317 goto error_out;
6320 /* Add a CASE_LABEL to the statement-tree. */
6321 case_label = add_stmt (build_case_label (low_value, high_value, label));
6322 /* Register this case label in the splay tree. */
6323 splay_tree_insert (cases,
6324 (splay_tree_key) low_value,
6325 (splay_tree_value) case_label);
6327 return case_label;
6329 error_out:
6330 /* Add a label so that the back-end doesn't think that the beginning of
6331 the switch is unreachable. Note that we do not add a case label, as
6332 that just leads to duplicates and thence to failure later on. */
6333 if (!cases->root)
6335 tree t = create_artificial_label (loc);
6336 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6338 return error_mark_node;
6341 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6342 Used to verify that case values match up with enumerator values. */
6344 static void
6345 match_case_to_enum_1 (tree key, tree type, tree label)
6347 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6349 if (tree_fits_uhwi_p (key))
6350 print_dec (key, buf, UNSIGNED);
6351 else if (tree_fits_shwi_p (key))
6352 print_dec (key, buf, SIGNED);
6353 else
6354 print_hex (key, buf);
6356 if (TYPE_NAME (type) == 0)
6357 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6358 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6359 "case value %qs not in enumerated type",
6360 buf);
6361 else
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 %qT",
6365 buf, type);
6368 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6369 Used to verify that case values match up with enumerator values. */
6371 static int
6372 match_case_to_enum (splay_tree_node node, void *data)
6374 tree label = (tree) node->value;
6375 tree type = (tree) data;
6377 /* Skip default case. */
6378 if (!CASE_LOW (label))
6379 return 0;
6381 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6382 when we did our enum->case scan. Reset our scratch bit after. */
6383 if (!CASE_LOW_SEEN (label))
6384 match_case_to_enum_1 (CASE_LOW (label), type, label);
6385 else
6386 CASE_LOW_SEEN (label) = 0;
6388 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6389 not set, that means that CASE_HIGH did not appear when we did our
6390 enum->case scan. Reset our scratch bit after. */
6391 if (CASE_HIGH (label))
6393 if (!CASE_HIGH_SEEN (label))
6394 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6395 else
6396 CASE_HIGH_SEEN (label) = 0;
6399 return 0;
6402 /* Handle -Wswitch*. Called from the front end after parsing the
6403 switch construct. */
6404 /* ??? Should probably be somewhere generic, since other languages
6405 besides C and C++ would want this. At the moment, however, C/C++
6406 are the only tree-ssa languages that support enumerations at all,
6407 so the point is moot. */
6409 void
6410 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6411 tree type, tree cond)
6413 splay_tree_node default_node;
6414 splay_tree_node node;
6415 tree chain;
6417 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6418 return;
6420 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6421 if (!default_node)
6422 warning_at (switch_location, OPT_Wswitch_default,
6423 "switch missing default case");
6425 /* From here on, we only care about about enumerated types. */
6426 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6427 return;
6429 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6430 if (!warn_switch_enum && !warn_switch)
6431 return;
6433 /* Check the cases. Warn about case values which are not members of
6434 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6435 there is no default case, check that exactly all enumeration
6436 literals are covered by the cases. */
6438 /* Clearing COND if it is not an integer constant simplifies
6439 the tests inside the loop below. */
6440 if (TREE_CODE (cond) != INTEGER_CST)
6441 cond = NULL_TREE;
6443 /* The time complexity here is O(N*lg(N)) worst case, but for the
6444 common case of monotonically increasing enumerators, it is
6445 O(N), since the nature of the splay tree will keep the next
6446 element adjacent to the root at all times. */
6448 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6450 tree value = TREE_VALUE (chain);
6451 if (TREE_CODE (value) == CONST_DECL)
6452 value = DECL_INITIAL (value);
6453 node = splay_tree_lookup (cases, (splay_tree_key) value);
6454 if (node)
6456 /* Mark the CASE_LOW part of the case entry as seen. */
6457 tree label = (tree) node->value;
6458 CASE_LOW_SEEN (label) = 1;
6459 continue;
6462 /* Even though there wasn't an exact match, there might be a
6463 case range which includes the enumerator's value. */
6464 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6465 if (node && CASE_HIGH ((tree) node->value))
6467 tree label = (tree) node->value;
6468 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6469 if (cmp >= 0)
6471 /* If we match the upper bound exactly, mark the CASE_HIGH
6472 part of the case entry as seen. */
6473 if (cmp == 0)
6474 CASE_HIGH_SEEN (label) = 1;
6475 continue;
6479 /* We've now determined that this enumerated literal isn't
6480 handled by the case labels of the switch statement. */
6482 /* If the switch expression is a constant, we only really care
6483 about whether that constant is handled by the switch. */
6484 if (cond && tree_int_cst_compare (cond, value))
6485 continue;
6487 /* If there is a default_node, the only relevant option is
6488 Wswitch-enum. Otherwise, if both are enabled then we prefer
6489 to warn using -Wswitch because -Wswitch is enabled by -Wall
6490 while -Wswitch-enum is explicit. */
6491 warning_at (switch_location,
6492 (default_node || !warn_switch
6493 ? OPT_Wswitch_enum
6494 : OPT_Wswitch),
6495 "enumeration value %qE not handled in switch",
6496 TREE_PURPOSE (chain));
6499 /* Warn if there are case expressions that don't correspond to
6500 enumerators. This can occur since C and C++ don't enforce
6501 type-checking of assignments to enumeration variables.
6503 The time complexity here is now always O(N) worst case, since
6504 we should have marked both the lower bound and upper bound of
6505 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6506 above. This scan also resets those fields. */
6508 splay_tree_foreach (cases, match_case_to_enum, type);
6511 /* Finish an expression taking the address of LABEL (an
6512 IDENTIFIER_NODE). Returns an expression for the address.
6514 LOC is the location for the expression returned. */
6516 tree
6517 finish_label_address_expr (tree label, location_t loc)
6519 tree result;
6521 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6523 if (label == error_mark_node)
6524 return error_mark_node;
6526 label = lookup_label (label);
6527 if (label == NULL_TREE)
6528 result = null_pointer_node;
6529 else
6531 TREE_USED (label) = 1;
6532 result = build1 (ADDR_EXPR, ptr_type_node, label);
6533 /* The current function is not necessarily uninlinable.
6534 Computed gotos are incompatible with inlining, but the value
6535 here could be used only in a diagnostic, for example. */
6536 protected_set_expr_location (result, loc);
6539 return result;
6543 /* Given a boolean expression ARG, return a tree representing an increment
6544 or decrement (as indicated by CODE) of ARG. The front end must check for
6545 invalid cases (e.g., decrement in C++). */
6546 tree
6547 boolean_increment (enum tree_code code, tree arg)
6549 tree val;
6550 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6552 arg = stabilize_reference (arg);
6553 switch (code)
6555 case PREINCREMENT_EXPR:
6556 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6557 break;
6558 case POSTINCREMENT_EXPR:
6559 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6560 arg = save_expr (arg);
6561 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6562 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6563 break;
6564 case PREDECREMENT_EXPR:
6565 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6566 invert_truthvalue_loc (input_location, arg));
6567 break;
6568 case POSTDECREMENT_EXPR:
6569 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6570 invert_truthvalue_loc (input_location, arg));
6571 arg = save_expr (arg);
6572 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6573 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6574 break;
6575 default:
6576 gcc_unreachable ();
6578 TREE_SIDE_EFFECTS (val) = 1;
6579 return val;
6582 /* Built-in macros for stddef.h and stdint.h, that require macros
6583 defined in this file. */
6584 void
6585 c_stddef_cpp_builtins(void)
6587 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6588 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6589 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6590 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6591 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6592 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6593 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6594 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6595 if (SIG_ATOMIC_TYPE)
6596 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6597 if (INT8_TYPE)
6598 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6599 if (INT16_TYPE)
6600 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6601 if (INT32_TYPE)
6602 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6603 if (INT64_TYPE)
6604 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6605 if (UINT8_TYPE)
6606 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6607 if (UINT16_TYPE)
6608 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6609 if (UINT32_TYPE)
6610 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6611 if (UINT64_TYPE)
6612 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6613 if (INT_LEAST8_TYPE)
6614 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6615 if (INT_LEAST16_TYPE)
6616 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6617 if (INT_LEAST32_TYPE)
6618 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6619 if (INT_LEAST64_TYPE)
6620 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6621 if (UINT_LEAST8_TYPE)
6622 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6623 if (UINT_LEAST16_TYPE)
6624 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6625 if (UINT_LEAST32_TYPE)
6626 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6627 if (UINT_LEAST64_TYPE)
6628 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6629 if (INT_FAST8_TYPE)
6630 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6631 if (INT_FAST16_TYPE)
6632 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6633 if (INT_FAST32_TYPE)
6634 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6635 if (INT_FAST64_TYPE)
6636 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6637 if (UINT_FAST8_TYPE)
6638 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6639 if (UINT_FAST16_TYPE)
6640 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6641 if (UINT_FAST32_TYPE)
6642 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6643 if (UINT_FAST64_TYPE)
6644 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6645 if (INTPTR_TYPE)
6646 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6647 if (UINTPTR_TYPE)
6648 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6651 static void
6652 c_init_attributes (void)
6654 /* Fill in the built_in_attributes array. */
6655 #define DEF_ATTR_NULL_TREE(ENUM) \
6656 built_in_attributes[(int) ENUM] = NULL_TREE;
6657 #define DEF_ATTR_INT(ENUM, VALUE) \
6658 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6659 #define DEF_ATTR_STRING(ENUM, VALUE) \
6660 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6661 #define DEF_ATTR_IDENT(ENUM, STRING) \
6662 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6663 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6664 built_in_attributes[(int) ENUM] \
6665 = tree_cons (built_in_attributes[(int) PURPOSE], \
6666 built_in_attributes[(int) VALUE], \
6667 built_in_attributes[(int) CHAIN]);
6668 #include "builtin-attrs.def"
6669 #undef DEF_ATTR_NULL_TREE
6670 #undef DEF_ATTR_INT
6671 #undef DEF_ATTR_IDENT
6672 #undef DEF_ATTR_TREE_LIST
6675 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6676 identifier as an argument, so the front end shouldn't look it up. */
6678 bool
6679 attribute_takes_identifier_p (const_tree attr_id)
6681 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6682 if (spec == NULL)
6683 /* Unknown attribute that we'll end up ignoring, return true so we
6684 don't complain about an identifier argument. */
6685 return true;
6686 else if (!strcmp ("mode", spec->name)
6687 || !strcmp ("format", spec->name)
6688 || !strcmp ("cleanup", spec->name))
6689 return true;
6690 else
6691 return targetm.attribute_takes_identifier_p (attr_id);
6694 /* Attribute handlers common to C front ends. */
6696 /* Handle a "packed" attribute; arguments as in
6697 struct attribute_spec.handler. */
6699 static tree
6700 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6701 int flags, bool *no_add_attrs)
6703 if (TYPE_P (*node))
6705 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6706 *node = build_variant_type_copy (*node);
6707 TYPE_PACKED (*node) = 1;
6709 else if (TREE_CODE (*node) == FIELD_DECL)
6711 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6712 /* Still pack bitfields. */
6713 && ! DECL_INITIAL (*node))
6714 warning (OPT_Wattributes,
6715 "%qE attribute ignored for field of type %qT",
6716 name, TREE_TYPE (*node));
6717 else
6718 DECL_PACKED (*node) = 1;
6720 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6721 used for DECL_REGISTER. It wouldn't mean anything anyway.
6722 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6723 that changes what the typedef is typing. */
6724 else
6726 warning (OPT_Wattributes, "%qE attribute ignored", name);
6727 *no_add_attrs = true;
6730 return NULL_TREE;
6733 /* Handle a "nocommon" attribute; arguments as in
6734 struct attribute_spec.handler. */
6736 static tree
6737 handle_nocommon_attribute (tree *node, tree name,
6738 tree ARG_UNUSED (args),
6739 int ARG_UNUSED (flags), bool *no_add_attrs)
6741 if (TREE_CODE (*node) == VAR_DECL)
6742 DECL_COMMON (*node) = 0;
6743 else
6745 warning (OPT_Wattributes, "%qE attribute ignored", name);
6746 *no_add_attrs = true;
6749 return NULL_TREE;
6752 /* Handle a "common" attribute; arguments as in
6753 struct attribute_spec.handler. */
6755 static tree
6756 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6757 int ARG_UNUSED (flags), bool *no_add_attrs)
6759 if (TREE_CODE (*node) == VAR_DECL)
6760 DECL_COMMON (*node) = 1;
6761 else
6763 warning (OPT_Wattributes, "%qE attribute ignored", name);
6764 *no_add_attrs = true;
6767 return NULL_TREE;
6770 /* Handle a "noreturn" attribute; arguments as in
6771 struct attribute_spec.handler. */
6773 static tree
6774 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6775 int ARG_UNUSED (flags), bool *no_add_attrs)
6777 tree type = TREE_TYPE (*node);
6779 /* See FIXME comment in c_common_attribute_table. */
6780 if (TREE_CODE (*node) == FUNCTION_DECL
6781 || objc_method_decl (TREE_CODE (*node)))
6782 TREE_THIS_VOLATILE (*node) = 1;
6783 else if (TREE_CODE (type) == POINTER_TYPE
6784 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6785 TREE_TYPE (*node)
6786 = (build_qualified_type
6787 (build_pointer_type
6788 (build_type_variant (TREE_TYPE (type),
6789 TYPE_READONLY (TREE_TYPE (type)), 1)),
6790 TYPE_QUALS (type)));
6791 else
6793 warning (OPT_Wattributes, "%qE attribute ignored", name);
6794 *no_add_attrs = true;
6797 return NULL_TREE;
6800 /* Handle a "hot" and attribute; arguments as in
6801 struct attribute_spec.handler. */
6803 static tree
6804 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6805 int ARG_UNUSED (flags), bool *no_add_attrs)
6807 if (TREE_CODE (*node) == FUNCTION_DECL
6808 || TREE_CODE (*node) == LABEL_DECL)
6810 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6812 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6813 "with attribute %qs", name, "cold");
6814 *no_add_attrs = true;
6816 /* Most of the rest of the hot processing is done later with
6817 lookup_attribute. */
6819 else
6821 warning (OPT_Wattributes, "%qE attribute ignored", name);
6822 *no_add_attrs = true;
6825 return NULL_TREE;
6828 /* Handle a "cold" and attribute; arguments as in
6829 struct attribute_spec.handler. */
6831 static tree
6832 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6833 int ARG_UNUSED (flags), bool *no_add_attrs)
6835 if (TREE_CODE (*node) == FUNCTION_DECL
6836 || TREE_CODE (*node) == LABEL_DECL)
6838 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6840 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6841 "with attribute %qs", name, "hot");
6842 *no_add_attrs = true;
6844 /* Most of the rest of the cold processing is done later with
6845 lookup_attribute. */
6847 else
6849 warning (OPT_Wattributes, "%qE attribute ignored", name);
6850 *no_add_attrs = true;
6853 return NULL_TREE;
6856 /* Handle a "no_sanitize_address" attribute; arguments as in
6857 struct attribute_spec.handler. */
6859 static tree
6860 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6861 bool *no_add_attrs)
6863 if (TREE_CODE (*node) != FUNCTION_DECL)
6865 warning (OPT_Wattributes, "%qE attribute ignored", name);
6866 *no_add_attrs = true;
6869 return NULL_TREE;
6872 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6873 struct attribute_spec.handler. */
6875 static tree
6876 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6877 bool *no_add_attrs)
6879 if (TREE_CODE (*node) != FUNCTION_DECL)
6880 warning (OPT_Wattributes, "%qE attribute ignored", name);
6881 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6882 DECL_ATTRIBUTES (*node)
6883 = tree_cons (get_identifier ("no_sanitize_address"),
6884 NULL_TREE, DECL_ATTRIBUTES (*node));
6885 *no_add_attrs = true;
6886 return NULL_TREE;
6889 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6890 struct attribute_spec.handler. */
6892 static tree
6893 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6894 bool *no_add_attrs)
6896 if (TREE_CODE (*node) != FUNCTION_DECL)
6898 warning (OPT_Wattributes, "%qE attribute ignored", name);
6899 *no_add_attrs = true;
6902 return NULL_TREE;
6905 /* Handle a "stack_protect" attribute; arguments as in
6906 struct attribute_spec.handler. */
6907 static tree
6908 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6909 bool *no_add_attrs)
6911 if (TREE_CODE (*node) != FUNCTION_DECL)
6913 warning (OPT_Wattributes, "%qE attribute ignored", name);
6914 *no_add_attrs = true;
6916 else
6917 DECL_ATTRIBUTES (*node)
6918 = tree_cons (get_identifier ("stack_protect"),
6919 NULL_TREE, DECL_ATTRIBUTES (*node));
6921 return NULL_TREE;
6924 /* Handle a "noinline" attribute; arguments as in
6925 struct attribute_spec.handler. */
6927 static tree
6928 handle_noinline_attribute (tree *node, tree name,
6929 tree ARG_UNUSED (args),
6930 int ARG_UNUSED (flags), bool *no_add_attrs)
6932 if (TREE_CODE (*node) == FUNCTION_DECL)
6934 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6936 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6937 "with attribute %qs", name, "always_inline");
6938 *no_add_attrs = true;
6940 else
6941 DECL_UNINLINABLE (*node) = 1;
6943 else
6945 warning (OPT_Wattributes, "%qE attribute ignored", name);
6946 *no_add_attrs = true;
6949 return NULL_TREE;
6952 /* Handle a "noclone" attribute; arguments as in
6953 struct attribute_spec.handler. */
6955 static tree
6956 handle_noclone_attribute (tree *node, tree name,
6957 tree ARG_UNUSED (args),
6958 int ARG_UNUSED (flags), bool *no_add_attrs)
6960 if (TREE_CODE (*node) != FUNCTION_DECL)
6962 warning (OPT_Wattributes, "%qE attribute ignored", name);
6963 *no_add_attrs = true;
6966 return NULL_TREE;
6969 /* Handle a "no_icf" attribute; arguments as in
6970 struct attribute_spec.handler. */
6972 static tree
6973 handle_noicf_attribute (tree *node, tree name,
6974 tree ARG_UNUSED (args),
6975 int ARG_UNUSED (flags), bool *no_add_attrs)
6977 if (TREE_CODE (*node) != FUNCTION_DECL)
6979 warning (OPT_Wattributes, "%qE attribute ignored", name);
6980 *no_add_attrs = true;
6983 return NULL_TREE;
6987 /* Handle a "always_inline" attribute; arguments as in
6988 struct attribute_spec.handler. */
6990 static tree
6991 handle_always_inline_attribute (tree *node, tree name,
6992 tree ARG_UNUSED (args),
6993 int ARG_UNUSED (flags),
6994 bool *no_add_attrs)
6996 if (TREE_CODE (*node) == FUNCTION_DECL)
6998 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7000 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7001 "with %qs attribute", name, "noinline");
7002 *no_add_attrs = true;
7004 else
7005 /* Set the attribute and mark it for disregarding inline
7006 limits. */
7007 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
7009 else
7011 warning (OPT_Wattributes, "%qE attribute ignored", name);
7012 *no_add_attrs = true;
7015 return NULL_TREE;
7018 /* Handle a "gnu_inline" attribute; arguments as in
7019 struct attribute_spec.handler. */
7021 static tree
7022 handle_gnu_inline_attribute (tree *node, tree name,
7023 tree ARG_UNUSED (args),
7024 int ARG_UNUSED (flags),
7025 bool *no_add_attrs)
7027 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7029 /* Do nothing else, just set the attribute. We'll get at
7030 it later with lookup_attribute. */
7032 else
7034 warning (OPT_Wattributes, "%qE attribute ignored", name);
7035 *no_add_attrs = true;
7038 return NULL_TREE;
7041 /* Handle a "leaf" attribute; arguments as in
7042 struct attribute_spec.handler. */
7044 static tree
7045 handle_leaf_attribute (tree *node, tree name,
7046 tree ARG_UNUSED (args),
7047 int ARG_UNUSED (flags), bool *no_add_attrs)
7049 if (TREE_CODE (*node) != FUNCTION_DECL)
7051 warning (OPT_Wattributes, "%qE attribute ignored", name);
7052 *no_add_attrs = true;
7054 if (!TREE_PUBLIC (*node))
7056 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7057 *no_add_attrs = true;
7060 return NULL_TREE;
7063 /* Handle an "artificial" attribute; arguments as in
7064 struct attribute_spec.handler. */
7066 static tree
7067 handle_artificial_attribute (tree *node, tree name,
7068 tree ARG_UNUSED (args),
7069 int ARG_UNUSED (flags),
7070 bool *no_add_attrs)
7072 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7074 /* Do nothing else, just set the attribute. We'll get at
7075 it later with lookup_attribute. */
7077 else
7079 warning (OPT_Wattributes, "%qE attribute ignored", name);
7080 *no_add_attrs = true;
7083 return NULL_TREE;
7086 /* Handle a "flatten" attribute; arguments as in
7087 struct attribute_spec.handler. */
7089 static tree
7090 handle_flatten_attribute (tree *node, tree name,
7091 tree args ATTRIBUTE_UNUSED,
7092 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7094 if (TREE_CODE (*node) == FUNCTION_DECL)
7095 /* Do nothing else, just set the attribute. We'll get at
7096 it later with lookup_attribute. */
7098 else
7100 warning (OPT_Wattributes, "%qE attribute ignored", name);
7101 *no_add_attrs = true;
7104 return NULL_TREE;
7107 /* Handle a "warning" or "error" attribute; arguments as in
7108 struct attribute_spec.handler. */
7110 static tree
7111 handle_error_attribute (tree *node, tree name, tree args,
7112 int ARG_UNUSED (flags), bool *no_add_attrs)
7114 if (TREE_CODE (*node) == FUNCTION_DECL
7115 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7116 /* Do nothing else, just set the attribute. We'll get at
7117 it later with lookup_attribute. */
7119 else
7121 warning (OPT_Wattributes, "%qE attribute ignored", name);
7122 *no_add_attrs = true;
7125 return NULL_TREE;
7128 /* Handle a "used" attribute; arguments as in
7129 struct attribute_spec.handler. */
7131 static tree
7132 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7133 int ARG_UNUSED (flags), bool *no_add_attrs)
7135 tree node = *pnode;
7137 if (TREE_CODE (node) == FUNCTION_DECL
7138 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7139 || (TREE_CODE (node) == TYPE_DECL))
7141 TREE_USED (node) = 1;
7142 DECL_PRESERVE_P (node) = 1;
7143 if (TREE_CODE (node) == VAR_DECL)
7144 DECL_READ_P (node) = 1;
7146 else
7148 warning (OPT_Wattributes, "%qE attribute ignored", name);
7149 *no_add_attrs = true;
7152 return NULL_TREE;
7155 /* Handle a "unused" attribute; arguments as in
7156 struct attribute_spec.handler. */
7158 static tree
7159 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7160 int flags, bool *no_add_attrs)
7162 if (DECL_P (*node))
7164 tree decl = *node;
7166 if (TREE_CODE (decl) == PARM_DECL
7167 || TREE_CODE (decl) == VAR_DECL
7168 || TREE_CODE (decl) == FUNCTION_DECL
7169 || TREE_CODE (decl) == LABEL_DECL
7170 || TREE_CODE (decl) == TYPE_DECL)
7172 TREE_USED (decl) = 1;
7173 if (TREE_CODE (decl) == VAR_DECL
7174 || TREE_CODE (decl) == PARM_DECL)
7175 DECL_READ_P (decl) = 1;
7177 else
7179 warning (OPT_Wattributes, "%qE attribute ignored", name);
7180 *no_add_attrs = true;
7183 else
7185 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7186 *node = build_variant_type_copy (*node);
7187 TREE_USED (*node) = 1;
7190 return NULL_TREE;
7193 /* Handle a "externally_visible" attribute; arguments as in
7194 struct attribute_spec.handler. */
7196 static tree
7197 handle_externally_visible_attribute (tree *pnode, tree name,
7198 tree ARG_UNUSED (args),
7199 int ARG_UNUSED (flags),
7200 bool *no_add_attrs)
7202 tree node = *pnode;
7204 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7206 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7207 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7209 warning (OPT_Wattributes,
7210 "%qE attribute have effect only on public objects", name);
7211 *no_add_attrs = true;
7214 else
7216 warning (OPT_Wattributes, "%qE attribute ignored", name);
7217 *no_add_attrs = true;
7220 return NULL_TREE;
7223 /* Handle the "no_reorder" attribute. Arguments as in
7224 struct attribute_spec.handler. */
7226 static tree
7227 handle_no_reorder_attribute (tree *pnode,
7228 tree name,
7229 tree,
7230 int,
7231 bool *no_add_attrs)
7233 tree node = *pnode;
7235 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7236 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7238 warning (OPT_Wattributes,
7239 "%qE attribute only affects top level objects",
7240 name);
7241 *no_add_attrs = true;
7244 return NULL_TREE;
7247 /* Handle a "const" attribute; arguments as in
7248 struct attribute_spec.handler. */
7250 static tree
7251 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7252 int ARG_UNUSED (flags), bool *no_add_attrs)
7254 tree type = TREE_TYPE (*node);
7256 /* See FIXME comment on noreturn in c_common_attribute_table. */
7257 if (TREE_CODE (*node) == FUNCTION_DECL)
7258 TREE_READONLY (*node) = 1;
7259 else if (TREE_CODE (type) == POINTER_TYPE
7260 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7261 TREE_TYPE (*node)
7262 = (build_qualified_type
7263 (build_pointer_type
7264 (build_type_variant (TREE_TYPE (type), 1,
7265 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7266 TYPE_QUALS (type)));
7267 else
7269 warning (OPT_Wattributes, "%qE attribute ignored", name);
7270 *no_add_attrs = true;
7273 return NULL_TREE;
7276 /* Handle a "transparent_union" attribute; arguments as in
7277 struct attribute_spec.handler. */
7279 static tree
7280 handle_transparent_union_attribute (tree *node, tree name,
7281 tree ARG_UNUSED (args), int flags,
7282 bool *no_add_attrs)
7284 tree type;
7286 *no_add_attrs = true;
7289 if (TREE_CODE (*node) == TYPE_DECL
7290 && ! (flags & ATTR_FLAG_CXX11))
7291 node = &TREE_TYPE (*node);
7292 type = *node;
7294 if (TREE_CODE (type) == UNION_TYPE)
7296 /* Make sure that the first field will work for a transparent union.
7297 If the type isn't complete yet, leave the check to the code in
7298 finish_struct. */
7299 if (TYPE_SIZE (type))
7301 tree first = first_field (type);
7302 if (first == NULL_TREE
7303 || DECL_ARTIFICIAL (first)
7304 || TYPE_MODE (type) != DECL_MODE (first))
7305 goto ignored;
7308 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7310 /* If the type isn't complete yet, setting the flag
7311 on a variant wouldn't ever be checked. */
7312 if (!TYPE_SIZE (type))
7313 goto ignored;
7315 /* build_duplicate_type doesn't work for C++. */
7316 if (c_dialect_cxx ())
7317 goto ignored;
7319 /* A type variant isn't good enough, since we don't a cast
7320 to such a type removed as a no-op. */
7321 *node = type = build_duplicate_type (type);
7324 TYPE_TRANSPARENT_AGGR (type) = 1;
7325 return NULL_TREE;
7328 ignored:
7329 warning (OPT_Wattributes, "%qE attribute ignored", name);
7330 return NULL_TREE;
7333 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7334 get the requested priority for a constructor or destructor,
7335 possibly issuing diagnostics for invalid or reserved
7336 priorities. */
7338 static priority_type
7339 get_priority (tree args, bool is_destructor)
7341 HOST_WIDE_INT pri;
7342 tree arg;
7344 if (!args)
7345 return DEFAULT_INIT_PRIORITY;
7347 if (!SUPPORTS_INIT_PRIORITY)
7349 if (is_destructor)
7350 error ("destructor priorities are not supported");
7351 else
7352 error ("constructor priorities are not supported");
7353 return DEFAULT_INIT_PRIORITY;
7356 arg = TREE_VALUE (args);
7357 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7358 goto invalid;
7359 if (arg == error_mark_node)
7360 return DEFAULT_INIT_PRIORITY;
7361 arg = default_conversion (arg);
7362 if (!tree_fits_shwi_p (arg)
7363 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7364 goto invalid;
7366 pri = tree_to_shwi (arg);
7367 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7368 goto invalid;
7370 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7372 if (is_destructor)
7373 warning (0,
7374 "destructor priorities from 0 to %d are reserved "
7375 "for the implementation",
7376 MAX_RESERVED_INIT_PRIORITY);
7377 else
7378 warning (0,
7379 "constructor priorities from 0 to %d are reserved "
7380 "for the implementation",
7381 MAX_RESERVED_INIT_PRIORITY);
7383 return pri;
7385 invalid:
7386 if (is_destructor)
7387 error ("destructor priorities must be integers from 0 to %d inclusive",
7388 MAX_INIT_PRIORITY);
7389 else
7390 error ("constructor priorities must be integers from 0 to %d inclusive",
7391 MAX_INIT_PRIORITY);
7392 return DEFAULT_INIT_PRIORITY;
7395 /* Handle a "constructor" attribute; arguments as in
7396 struct attribute_spec.handler. */
7398 static tree
7399 handle_constructor_attribute (tree *node, tree name, tree args,
7400 int ARG_UNUSED (flags),
7401 bool *no_add_attrs)
7403 tree decl = *node;
7404 tree type = TREE_TYPE (decl);
7406 if (TREE_CODE (decl) == FUNCTION_DECL
7407 && TREE_CODE (type) == FUNCTION_TYPE
7408 && decl_function_context (decl) == 0)
7410 priority_type priority;
7411 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7412 priority = get_priority (args, /*is_destructor=*/false);
7413 SET_DECL_INIT_PRIORITY (decl, priority);
7414 TREE_USED (decl) = 1;
7416 else
7418 warning (OPT_Wattributes, "%qE attribute ignored", name);
7419 *no_add_attrs = true;
7422 return NULL_TREE;
7425 /* Handle a "destructor" attribute; arguments as in
7426 struct attribute_spec.handler. */
7428 static tree
7429 handle_destructor_attribute (tree *node, tree name, tree args,
7430 int ARG_UNUSED (flags),
7431 bool *no_add_attrs)
7433 tree decl = *node;
7434 tree type = TREE_TYPE (decl);
7436 if (TREE_CODE (decl) == FUNCTION_DECL
7437 && TREE_CODE (type) == FUNCTION_TYPE
7438 && decl_function_context (decl) == 0)
7440 priority_type priority;
7441 DECL_STATIC_DESTRUCTOR (decl) = 1;
7442 priority = get_priority (args, /*is_destructor=*/true);
7443 SET_DECL_FINI_PRIORITY (decl, priority);
7444 TREE_USED (decl) = 1;
7446 else
7448 warning (OPT_Wattributes, "%qE attribute ignored", name);
7449 *no_add_attrs = true;
7452 return NULL_TREE;
7455 /* Nonzero if the mode is a valid vector mode for this architecture.
7456 This returns nonzero even if there is no hardware support for the
7457 vector mode, but we can emulate with narrower modes. */
7459 static int
7460 vector_mode_valid_p (machine_mode mode)
7462 enum mode_class mclass = GET_MODE_CLASS (mode);
7463 machine_mode innermode;
7465 /* Doh! What's going on? */
7466 if (mclass != MODE_VECTOR_INT
7467 && mclass != MODE_VECTOR_FLOAT
7468 && mclass != MODE_VECTOR_FRACT
7469 && mclass != MODE_VECTOR_UFRACT
7470 && mclass != MODE_VECTOR_ACCUM
7471 && mclass != MODE_VECTOR_UACCUM)
7472 return 0;
7474 /* Hardware support. Woo hoo! */
7475 if (targetm.vector_mode_supported_p (mode))
7476 return 1;
7478 innermode = GET_MODE_INNER (mode);
7480 /* We should probably return 1 if requesting V4DI and we have no DI,
7481 but we have V2DI, but this is probably very unlikely. */
7483 /* If we have support for the inner mode, we can safely emulate it.
7484 We may not have V2DI, but me can emulate with a pair of DIs. */
7485 return targetm.scalar_mode_supported_p (innermode);
7489 /* Handle a "mode" attribute; arguments as in
7490 struct attribute_spec.handler. */
7492 static tree
7493 handle_mode_attribute (tree *node, tree name, tree args,
7494 int ARG_UNUSED (flags), bool *no_add_attrs)
7496 tree type = *node;
7497 tree ident = TREE_VALUE (args);
7499 *no_add_attrs = true;
7501 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7502 warning (OPT_Wattributes, "%qE attribute ignored", name);
7503 else
7505 int j;
7506 const char *p = IDENTIFIER_POINTER (ident);
7507 int len = strlen (p);
7508 machine_mode mode = VOIDmode;
7509 tree typefm;
7510 bool valid_mode;
7512 if (len > 4 && p[0] == '_' && p[1] == '_'
7513 && p[len - 1] == '_' && p[len - 2] == '_')
7515 char *newp = (char *) alloca (len - 1);
7517 strcpy (newp, &p[2]);
7518 newp[len - 4] = '\0';
7519 p = newp;
7522 /* Change this type to have a type with the specified mode.
7523 First check for the special modes. */
7524 if (!strcmp (p, "byte"))
7525 mode = byte_mode;
7526 else if (!strcmp (p, "word"))
7527 mode = word_mode;
7528 else if (!strcmp (p, "pointer"))
7529 mode = ptr_mode;
7530 else if (!strcmp (p, "libgcc_cmp_return"))
7531 mode = targetm.libgcc_cmp_return_mode ();
7532 else if (!strcmp (p, "libgcc_shift_count"))
7533 mode = targetm.libgcc_shift_count_mode ();
7534 else if (!strcmp (p, "unwind_word"))
7535 mode = targetm.unwind_word_mode ();
7536 else
7537 for (j = 0; j < NUM_MACHINE_MODES; j++)
7538 if (!strcmp (p, GET_MODE_NAME (j)))
7540 mode = (machine_mode) j;
7541 break;
7544 if (mode == VOIDmode)
7546 error ("unknown machine mode %qE", ident);
7547 return NULL_TREE;
7550 valid_mode = false;
7551 switch (GET_MODE_CLASS (mode))
7553 case MODE_INT:
7554 case MODE_PARTIAL_INT:
7555 case MODE_FLOAT:
7556 case MODE_DECIMAL_FLOAT:
7557 case MODE_FRACT:
7558 case MODE_UFRACT:
7559 case MODE_ACCUM:
7560 case MODE_UACCUM:
7561 valid_mode = targetm.scalar_mode_supported_p (mode);
7562 break;
7564 case MODE_COMPLEX_INT:
7565 case MODE_COMPLEX_FLOAT:
7566 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7567 break;
7569 case MODE_VECTOR_INT:
7570 case MODE_VECTOR_FLOAT:
7571 case MODE_VECTOR_FRACT:
7572 case MODE_VECTOR_UFRACT:
7573 case MODE_VECTOR_ACCUM:
7574 case MODE_VECTOR_UACCUM:
7575 warning (OPT_Wattributes, "specifying vector types with "
7576 "__attribute__ ((mode)) is deprecated");
7577 warning (OPT_Wattributes,
7578 "use __attribute__ ((vector_size)) instead");
7579 valid_mode = vector_mode_valid_p (mode);
7580 break;
7582 default:
7583 break;
7585 if (!valid_mode)
7587 error ("unable to emulate %qs", p);
7588 return NULL_TREE;
7591 if (POINTER_TYPE_P (type))
7593 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7594 tree (*fn)(tree, machine_mode, bool);
7596 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7598 error ("invalid pointer mode %qs", p);
7599 return NULL_TREE;
7602 if (TREE_CODE (type) == POINTER_TYPE)
7603 fn = build_pointer_type_for_mode;
7604 else
7605 fn = build_reference_type_for_mode;
7606 typefm = fn (TREE_TYPE (type), mode, false);
7608 else
7610 /* For fixed-point modes, we need to test if the signness of type
7611 and the machine mode are consistent. */
7612 if (ALL_FIXED_POINT_MODE_P (mode)
7613 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7615 error ("signedness of type and machine mode %qs don%'t match", p);
7616 return NULL_TREE;
7618 /* For fixed-point modes, we need to pass saturating info. */
7619 typefm = lang_hooks.types.type_for_mode (mode,
7620 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7621 : TYPE_UNSIGNED (type));
7624 if (typefm == NULL_TREE)
7626 error ("no data type for mode %qs", p);
7627 return NULL_TREE;
7629 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7631 /* For enumeral types, copy the precision from the integer
7632 type returned above. If not an INTEGER_TYPE, we can't use
7633 this mode for this type. */
7634 if (TREE_CODE (typefm) != INTEGER_TYPE)
7636 error ("cannot use mode %qs for enumeral types", p);
7637 return NULL_TREE;
7640 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7642 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7643 typefm = type;
7645 else
7647 /* We cannot build a type variant, as there's code that assumes
7648 that TYPE_MAIN_VARIANT has the same mode. This includes the
7649 debug generators. Instead, create a subrange type. This
7650 results in all of the enumeral values being emitted only once
7651 in the original, and the subtype gets them by reference. */
7652 if (TYPE_UNSIGNED (type))
7653 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7654 else
7655 typefm = make_signed_type (TYPE_PRECISION (typefm));
7656 TREE_TYPE (typefm) = type;
7659 else if (VECTOR_MODE_P (mode)
7660 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7661 : TREE_CODE (type) != TREE_CODE (typefm))
7663 error ("mode %qs applied to inappropriate type", p);
7664 return NULL_TREE;
7667 *node = typefm;
7670 return NULL_TREE;
7673 /* Handle a "section" attribute; arguments as in
7674 struct attribute_spec.handler. */
7676 static tree
7677 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7678 int ARG_UNUSED (flags), bool *no_add_attrs)
7680 tree decl = *node;
7682 if (targetm_common.have_named_sections)
7684 user_defined_section_attribute = true;
7686 if ((TREE_CODE (decl) == FUNCTION_DECL
7687 || TREE_CODE (decl) == VAR_DECL)
7688 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7690 if (TREE_CODE (decl) == VAR_DECL
7691 && current_function_decl != NULL_TREE
7692 && !TREE_STATIC (decl))
7694 error_at (DECL_SOURCE_LOCATION (decl),
7695 "section attribute cannot be specified for "
7696 "local variables");
7697 *no_add_attrs = true;
7700 /* The decl may have already been given a section attribute
7701 from a previous declaration. Ensure they match. */
7702 else if (DECL_SECTION_NAME (decl) != NULL
7703 && strcmp (DECL_SECTION_NAME (decl),
7704 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7706 error ("section of %q+D conflicts with previous declaration",
7707 *node);
7708 *no_add_attrs = true;
7710 else if (TREE_CODE (decl) == VAR_DECL
7711 && !targetm.have_tls && targetm.emutls.tmpl_section
7712 && DECL_THREAD_LOCAL_P (decl))
7714 error ("section of %q+D cannot be overridden", *node);
7715 *no_add_attrs = true;
7717 else
7718 set_decl_section_name (decl,
7719 TREE_STRING_POINTER (TREE_VALUE (args)));
7721 else
7723 error ("section attribute not allowed for %q+D", *node);
7724 *no_add_attrs = true;
7727 else
7729 error_at (DECL_SOURCE_LOCATION (*node),
7730 "section attributes are not supported for this target");
7731 *no_add_attrs = true;
7734 return NULL_TREE;
7737 /* Check whether ALIGN is a valid user-specified alignment. If so,
7738 return its base-2 log; if not, output an error and return -1. If
7739 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7740 no error. */
7742 check_user_alignment (const_tree align, bool allow_zero)
7744 int i;
7746 if (error_operand_p (align))
7747 return -1;
7748 if (TREE_CODE (align) != INTEGER_CST
7749 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7751 error ("requested alignment is not an integer constant");
7752 return -1;
7754 else if (allow_zero && integer_zerop (align))
7755 return -1;
7756 else if (tree_int_cst_sgn (align) == -1
7757 || (i = tree_log2 (align)) == -1)
7759 error ("requested alignment is not a positive power of 2");
7760 return -1;
7762 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7764 error ("requested alignment is too large");
7765 return -1;
7767 return i;
7771 If in c++-11, check if the c++-11 alignment constraint with respect
7772 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7773 c++-11 mode, does nothing.
7775 [dcl.align]2/ says:
7777 [* if the constant expression evaluates to a fundamental alignment,
7778 the alignment requirement of the declared entity shall be the
7779 specified fundamental alignment.
7781 * if the constant expression evaluates to an extended alignment
7782 and the implementation supports that alignment in the context
7783 of the declaration, the alignment of the declared entity shall
7784 be that alignment
7786 * if the constant expression evaluates to an extended alignment
7787 and the implementation does not support that alignment in the
7788 context of the declaration, the program is ill-formed]. */
7790 static bool
7791 check_cxx_fundamental_alignment_constraints (tree node,
7792 unsigned align_log,
7793 int flags)
7795 bool alignment_too_large_p = false;
7796 unsigned requested_alignment = 1U << align_log;
7797 unsigned max_align = 0;
7799 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7800 || (node == NULL_TREE || node == error_mark_node))
7801 return true;
7803 if (cxx_fundamental_alignment_p (requested_alignment))
7804 return true;
7806 if (DECL_P (node))
7808 if (TREE_STATIC (node))
7810 /* For file scope variables and static members, the target
7811 supports alignments that are at most
7812 MAX_OFILE_ALIGNMENT. */
7813 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7814 alignment_too_large_p = true;
7816 else
7818 #ifdef BIGGEST_FIELD_ALIGNMENT
7819 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7820 #else
7821 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7822 #endif
7823 /* For non-static members, the target supports either
7824 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7825 if it is defined or BIGGEST_ALIGNMENT. */
7826 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7827 if (TREE_CODE (node) == FIELD_DECL
7828 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7829 alignment_too_large_p = true;
7830 #undef MAX_TARGET_FIELD_ALIGNMENT
7831 /* For stack variables, the target supports at most
7832 MAX_STACK_ALIGNMENT. */
7833 else if (decl_function_context (node) != NULL
7834 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7835 alignment_too_large_p = true;
7838 else if (TYPE_P (node))
7840 /* Let's be liberal for types. */
7841 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7842 alignment_too_large_p = true;
7845 if (alignment_too_large_p)
7846 pedwarn (input_location, OPT_Wattributes,
7847 "requested alignment %d is larger than %d",
7848 requested_alignment, max_align);
7850 return !alignment_too_large_p;
7853 /* Handle a "aligned" attribute; arguments as in
7854 struct attribute_spec.handler. */
7856 static tree
7857 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7858 int flags, bool *no_add_attrs)
7860 tree decl = NULL_TREE;
7861 tree *type = NULL;
7862 int is_type = 0;
7863 tree align_expr;
7864 int i;
7866 if (args)
7868 align_expr = TREE_VALUE (args);
7869 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7870 && TREE_CODE (align_expr) != FUNCTION_DECL)
7871 align_expr = default_conversion (align_expr);
7873 else
7874 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7876 if (DECL_P (*node))
7878 decl = *node;
7879 type = &TREE_TYPE (decl);
7880 is_type = TREE_CODE (*node) == TYPE_DECL;
7882 else if (TYPE_P (*node))
7883 type = node, is_type = 1;
7885 if ((i = check_user_alignment (align_expr, false)) == -1
7886 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7887 *no_add_attrs = true;
7888 else if (is_type)
7890 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7891 /* OK, modify the type in place. */;
7892 /* If we have a TYPE_DECL, then copy the type, so that we
7893 don't accidentally modify a builtin type. See pushdecl. */
7894 else if (decl && TREE_TYPE (decl) != error_mark_node
7895 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7897 tree tt = TREE_TYPE (decl);
7898 *type = build_variant_type_copy (*type);
7899 DECL_ORIGINAL_TYPE (decl) = tt;
7900 TYPE_NAME (*type) = decl;
7901 TREE_USED (*type) = TREE_USED (decl);
7902 TREE_TYPE (decl) = *type;
7904 else
7905 *type = build_variant_type_copy (*type);
7907 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7908 TYPE_USER_ALIGN (*type) = 1;
7910 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7911 && TREE_CODE (decl) != FIELD_DECL)
7913 error ("alignment may not be specified for %q+D", decl);
7914 *no_add_attrs = true;
7916 else if (DECL_USER_ALIGN (decl)
7917 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7918 /* C++-11 [dcl.align/4]:
7920 When multiple alignment-specifiers are specified for an
7921 entity, the alignment requirement shall be set to the
7922 strictest specified alignment.
7924 This formally comes from the c++11 specification but we are
7925 doing it for the GNU attribute syntax as well. */
7926 *no_add_attrs = true;
7927 else if (TREE_CODE (decl) == FUNCTION_DECL
7928 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7930 if (DECL_USER_ALIGN (decl))
7931 error ("alignment for %q+D was previously specified as %d "
7932 "and may not be decreased", decl,
7933 DECL_ALIGN (decl) / BITS_PER_UNIT);
7934 else
7935 error ("alignment for %q+D must be at least %d", decl,
7936 DECL_ALIGN (decl) / BITS_PER_UNIT);
7937 *no_add_attrs = true;
7939 else
7941 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7942 DECL_USER_ALIGN (decl) = 1;
7945 return NULL_TREE;
7948 /* Handle a "weak" attribute; arguments as in
7949 struct attribute_spec.handler. */
7951 static tree
7952 handle_weak_attribute (tree *node, tree name,
7953 tree ARG_UNUSED (args),
7954 int ARG_UNUSED (flags),
7955 bool * ARG_UNUSED (no_add_attrs))
7957 if (TREE_CODE (*node) == FUNCTION_DECL
7958 && DECL_DECLARED_INLINE_P (*node))
7960 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7961 *no_add_attrs = true;
7963 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7965 error ("indirect function %q+D cannot be declared weak", *node);
7966 *no_add_attrs = true;
7967 return NULL_TREE;
7969 else if (TREE_CODE (*node) == FUNCTION_DECL
7970 || TREE_CODE (*node) == VAR_DECL)
7972 struct symtab_node *n = symtab_node::get (*node);
7973 if (n && n->refuse_visibility_changes)
7974 error ("%+D declared weak after being used", *node);
7975 declare_weak (*node);
7977 else
7978 warning (OPT_Wattributes, "%qE attribute ignored", name);
7980 return NULL_TREE;
7983 /* Handle an "alias" or "ifunc" attribute; arguments as in
7984 struct attribute_spec.handler, except that IS_ALIAS tells us
7985 whether this is an alias as opposed to ifunc attribute. */
7987 static tree
7988 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7989 bool *no_add_attrs)
7991 tree decl = *node;
7993 if (TREE_CODE (decl) != FUNCTION_DECL
7994 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7996 warning (OPT_Wattributes, "%qE attribute ignored", name);
7997 *no_add_attrs = true;
7999 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
8000 || (TREE_CODE (decl) != FUNCTION_DECL
8001 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8002 /* A static variable declaration is always a tentative definition,
8003 but the alias is a non-tentative definition which overrides. */
8004 || (TREE_CODE (decl) != FUNCTION_DECL
8005 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
8007 error ("%q+D defined both normally and as %qE attribute", decl, name);
8008 *no_add_attrs = true;
8009 return NULL_TREE;
8011 else if (!is_alias
8012 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8013 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8015 error ("weak %q+D cannot be defined %qE", decl, name);
8016 *no_add_attrs = true;
8017 return NULL_TREE;
8020 /* Note that the very first time we process a nested declaration,
8021 decl_function_context will not be set. Indeed, *would* never
8022 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8023 we do below. After such frobbery, pushdecl would set the context.
8024 In any case, this is never what we want. */
8025 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
8027 tree id;
8029 id = TREE_VALUE (args);
8030 if (TREE_CODE (id) != STRING_CST)
8032 error ("attribute %qE argument not a string", name);
8033 *no_add_attrs = true;
8034 return NULL_TREE;
8036 id = get_identifier (TREE_STRING_POINTER (id));
8037 /* This counts as a use of the object pointed to. */
8038 TREE_USED (id) = 1;
8040 if (TREE_CODE (decl) == FUNCTION_DECL)
8041 DECL_INITIAL (decl) = error_mark_node;
8042 else
8043 TREE_STATIC (decl) = 1;
8045 if (!is_alias)
8046 /* ifuncs are also aliases, so set that attribute too. */
8047 DECL_ATTRIBUTES (decl)
8048 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8050 else
8052 warning (OPT_Wattributes, "%qE attribute ignored", name);
8053 *no_add_attrs = true;
8056 if (decl_in_symtab_p (*node))
8058 struct symtab_node *n = symtab_node::get (decl);
8059 if (n && n->refuse_visibility_changes)
8061 if (is_alias)
8062 error ("%+D declared alias after being used", decl);
8063 else
8064 error ("%+D declared ifunc after being used", decl);
8069 return NULL_TREE;
8072 /* Handle an "alias" or "ifunc" attribute; arguments as in
8073 struct attribute_spec.handler. */
8075 static tree
8076 handle_ifunc_attribute (tree *node, tree name, tree args,
8077 int ARG_UNUSED (flags), bool *no_add_attrs)
8079 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8082 /* Handle an "alias" or "ifunc" attribute; arguments as in
8083 struct attribute_spec.handler. */
8085 static tree
8086 handle_alias_attribute (tree *node, tree name, tree args,
8087 int ARG_UNUSED (flags), bool *no_add_attrs)
8089 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8092 /* Handle a "weakref" attribute; arguments as in struct
8093 attribute_spec.handler. */
8095 static tree
8096 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8097 int flags, bool *no_add_attrs)
8099 tree attr = NULL_TREE;
8101 /* We must ignore the attribute when it is associated with
8102 local-scoped decls, since attribute alias is ignored and many
8103 such symbols do not even have a DECL_WEAK field. */
8104 if (decl_function_context (*node)
8105 || current_function_decl
8106 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
8108 warning (OPT_Wattributes, "%qE attribute ignored", name);
8109 *no_add_attrs = true;
8110 return NULL_TREE;
8113 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8115 error ("indirect function %q+D cannot be declared weakref", *node);
8116 *no_add_attrs = true;
8117 return NULL_TREE;
8120 /* The idea here is that `weakref("name")' mutates into `weakref,
8121 alias("name")', and weakref without arguments, in turn,
8122 implicitly adds weak. */
8124 if (args)
8126 attr = tree_cons (get_identifier ("alias"), args, attr);
8127 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8129 *no_add_attrs = true;
8131 decl_attributes (node, attr, flags);
8133 else
8135 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8136 error_at (DECL_SOURCE_LOCATION (*node),
8137 "weakref attribute must appear before alias attribute");
8139 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8140 and that isn't supported; and because it wants to add it to
8141 the list of weak decls, which isn't helpful. */
8142 DECL_WEAK (*node) = 1;
8145 if (decl_in_symtab_p (*node))
8147 struct symtab_node *n = symtab_node::get (*node);
8148 if (n && n->refuse_visibility_changes)
8149 error ("%+D declared weakref after being used", *node);
8152 return NULL_TREE;
8155 /* Handle an "visibility" attribute; arguments as in
8156 struct attribute_spec.handler. */
8158 static tree
8159 handle_visibility_attribute (tree *node, tree name, tree args,
8160 int ARG_UNUSED (flags),
8161 bool *ARG_UNUSED (no_add_attrs))
8163 tree decl = *node;
8164 tree id = TREE_VALUE (args);
8165 enum symbol_visibility vis;
8167 if (TYPE_P (*node))
8169 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8170 /* OK */;
8171 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8173 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8174 name);
8175 return NULL_TREE;
8177 else if (TYPE_FIELDS (*node))
8179 error ("%qE attribute ignored because %qT is already defined",
8180 name, *node);
8181 return NULL_TREE;
8184 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8186 warning (OPT_Wattributes, "%qE attribute ignored", name);
8187 return NULL_TREE;
8190 if (TREE_CODE (id) != STRING_CST)
8192 error ("visibility argument not a string");
8193 return NULL_TREE;
8196 /* If this is a type, set the visibility on the type decl. */
8197 if (TYPE_P (decl))
8199 decl = TYPE_NAME (decl);
8200 if (!decl)
8201 return NULL_TREE;
8202 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8204 warning (OPT_Wattributes, "%qE attribute ignored on types",
8205 name);
8206 return NULL_TREE;
8210 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8211 vis = VISIBILITY_DEFAULT;
8212 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8213 vis = VISIBILITY_INTERNAL;
8214 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8215 vis = VISIBILITY_HIDDEN;
8216 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8217 vis = VISIBILITY_PROTECTED;
8218 else
8220 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8221 vis = VISIBILITY_DEFAULT;
8224 if (DECL_VISIBILITY_SPECIFIED (decl)
8225 && vis != DECL_VISIBILITY (decl))
8227 tree attributes = (TYPE_P (*node)
8228 ? TYPE_ATTRIBUTES (*node)
8229 : DECL_ATTRIBUTES (decl));
8230 if (lookup_attribute ("visibility", attributes))
8231 error ("%qD redeclared with different visibility", decl);
8232 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8233 && lookup_attribute ("dllimport", attributes))
8234 error ("%qD was declared %qs which implies default visibility",
8235 decl, "dllimport");
8236 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8237 && lookup_attribute ("dllexport", attributes))
8238 error ("%qD was declared %qs which implies default visibility",
8239 decl, "dllexport");
8242 DECL_VISIBILITY (decl) = vis;
8243 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8245 /* Go ahead and attach the attribute to the node as well. This is needed
8246 so we can determine whether we have VISIBILITY_DEFAULT because the
8247 visibility was not specified, or because it was explicitly overridden
8248 from the containing scope. */
8250 return NULL_TREE;
8253 /* Determine the ELF symbol visibility for DECL, which is either a
8254 variable or a function. It is an error to use this function if a
8255 definition of DECL is not available in this translation unit.
8256 Returns true if the final visibility has been determined by this
8257 function; false if the caller is free to make additional
8258 modifications. */
8260 bool
8261 c_determine_visibility (tree decl)
8263 gcc_assert (TREE_CODE (decl) == VAR_DECL
8264 || TREE_CODE (decl) == FUNCTION_DECL);
8266 /* If the user explicitly specified the visibility with an
8267 attribute, honor that. DECL_VISIBILITY will have been set during
8268 the processing of the attribute. We check for an explicit
8269 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8270 to distinguish the use of an attribute from the use of a "#pragma
8271 GCC visibility push(...)"; in the latter case we still want other
8272 considerations to be able to overrule the #pragma. */
8273 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8274 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8275 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8276 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8277 return true;
8279 /* Set default visibility to whatever the user supplied with
8280 visibility_specified depending on #pragma GCC visibility. */
8281 if (!DECL_VISIBILITY_SPECIFIED (decl))
8283 if (visibility_options.inpragma
8284 || DECL_VISIBILITY (decl) != default_visibility)
8286 DECL_VISIBILITY (decl) = default_visibility;
8287 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8288 /* If visibility changed and DECL already has DECL_RTL, ensure
8289 symbol flags are updated. */
8290 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8291 || TREE_CODE (decl) == FUNCTION_DECL)
8292 && DECL_RTL_SET_P (decl))
8293 make_decl_rtl (decl);
8296 return false;
8299 /* Handle an "tls_model" attribute; arguments as in
8300 struct attribute_spec.handler. */
8302 static tree
8303 handle_tls_model_attribute (tree *node, tree name, tree args,
8304 int ARG_UNUSED (flags), bool *no_add_attrs)
8306 tree id;
8307 tree decl = *node;
8308 enum tls_model kind;
8310 *no_add_attrs = true;
8312 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8314 warning (OPT_Wattributes, "%qE attribute ignored", name);
8315 return NULL_TREE;
8318 kind = DECL_TLS_MODEL (decl);
8319 id = TREE_VALUE (args);
8320 if (TREE_CODE (id) != STRING_CST)
8322 error ("tls_model argument not a string");
8323 return NULL_TREE;
8326 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8327 kind = TLS_MODEL_LOCAL_EXEC;
8328 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8329 kind = TLS_MODEL_INITIAL_EXEC;
8330 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8331 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8332 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8333 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8334 else
8335 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8337 set_decl_tls_model (decl, kind);
8338 return NULL_TREE;
8341 /* Handle a "no_instrument_function" attribute; arguments as in
8342 struct attribute_spec.handler. */
8344 static tree
8345 handle_no_instrument_function_attribute (tree *node, tree name,
8346 tree ARG_UNUSED (args),
8347 int ARG_UNUSED (flags),
8348 bool *no_add_attrs)
8350 tree decl = *node;
8352 if (TREE_CODE (decl) != FUNCTION_DECL)
8354 error_at (DECL_SOURCE_LOCATION (decl),
8355 "%qE attribute applies only to functions", name);
8356 *no_add_attrs = true;
8358 else
8359 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8361 return NULL_TREE;
8364 /* Handle a "malloc" attribute; arguments as in
8365 struct attribute_spec.handler. */
8367 static tree
8368 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8369 int ARG_UNUSED (flags), bool *no_add_attrs)
8371 if (TREE_CODE (*node) == FUNCTION_DECL
8372 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8373 DECL_IS_MALLOC (*node) = 1;
8374 else
8376 warning (OPT_Wattributes, "%qE attribute ignored", name);
8377 *no_add_attrs = true;
8380 return NULL_TREE;
8383 /* Handle a "alloc_size" attribute; arguments as in
8384 struct attribute_spec.handler. */
8386 static tree
8387 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8388 int ARG_UNUSED (flags), bool *no_add_attrs)
8390 unsigned arg_count = type_num_arguments (*node);
8391 for (; args; args = TREE_CHAIN (args))
8393 tree position = TREE_VALUE (args);
8394 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8395 && TREE_CODE (position) != FUNCTION_DECL)
8396 position = default_conversion (position);
8398 if (!tree_fits_uhwi_p (position)
8399 || !arg_count
8400 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8402 warning (OPT_Wattributes,
8403 "alloc_size parameter outside range");
8404 *no_add_attrs = true;
8405 return NULL_TREE;
8408 return NULL_TREE;
8411 /* Handle a "alloc_align" attribute; arguments as in
8412 struct attribute_spec.handler. */
8414 static tree
8415 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8416 bool *no_add_attrs)
8418 unsigned arg_count = type_num_arguments (*node);
8419 tree position = TREE_VALUE (args);
8420 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8421 position = default_conversion (position);
8423 if (!tree_fits_uhwi_p (position)
8424 || !arg_count
8425 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8427 warning (OPT_Wattributes,
8428 "alloc_align parameter outside range");
8429 *no_add_attrs = true;
8430 return NULL_TREE;
8432 return NULL_TREE;
8435 /* Handle a "assume_aligned" attribute; arguments as in
8436 struct attribute_spec.handler. */
8438 static tree
8439 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8440 bool *no_add_attrs)
8442 for (; args; args = TREE_CHAIN (args))
8444 tree position = TREE_VALUE (args);
8445 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8446 && TREE_CODE (position) != FUNCTION_DECL)
8447 position = default_conversion (position);
8449 if (TREE_CODE (position) != INTEGER_CST)
8451 warning (OPT_Wattributes,
8452 "assume_aligned parameter not integer constant");
8453 *no_add_attrs = true;
8454 return NULL_TREE;
8457 return NULL_TREE;
8460 /* Handle a "fn spec" attribute; arguments as in
8461 struct attribute_spec.handler. */
8463 static tree
8464 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8465 tree args, int ARG_UNUSED (flags),
8466 bool *no_add_attrs ATTRIBUTE_UNUSED)
8468 gcc_assert (args
8469 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8470 && !TREE_CHAIN (args));
8471 return NULL_TREE;
8474 /* Handle a "bnd_variable_size" attribute; arguments as in
8475 struct attribute_spec.handler. */
8477 static tree
8478 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8479 int ARG_UNUSED (flags), bool *no_add_attrs)
8481 if (TREE_CODE (*node) != FIELD_DECL)
8483 warning (OPT_Wattributes, "%qE attribute ignored", name);
8484 *no_add_attrs = true;
8487 return NULL_TREE;
8490 /* Handle a "bnd_legacy" attribute; arguments as in
8491 struct attribute_spec.handler. */
8493 static tree
8494 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8495 int ARG_UNUSED (flags), bool *no_add_attrs)
8497 if (TREE_CODE (*node) != FUNCTION_DECL)
8499 warning (OPT_Wattributes, "%qE attribute ignored", name);
8500 *no_add_attrs = true;
8503 return NULL_TREE;
8506 /* Handle a "bnd_instrument" attribute; arguments as in
8507 struct attribute_spec.handler. */
8509 static tree
8510 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8511 int ARG_UNUSED (flags), bool *no_add_attrs)
8513 if (TREE_CODE (*node) != FUNCTION_DECL)
8515 warning (OPT_Wattributes, "%qE attribute ignored", name);
8516 *no_add_attrs = true;
8519 return NULL_TREE;
8522 /* Handle a "warn_unused" attribute; arguments as in
8523 struct attribute_spec.handler. */
8525 static tree
8526 handle_warn_unused_attribute (tree *node, tree name,
8527 tree args ATTRIBUTE_UNUSED,
8528 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8530 if (TYPE_P (*node))
8531 /* Do nothing else, just set the attribute. We'll get at
8532 it later with lookup_attribute. */
8534 else
8536 warning (OPT_Wattributes, "%qE attribute ignored", name);
8537 *no_add_attrs = true;
8540 return NULL_TREE;
8543 /* Handle an "omp declare simd" attribute; arguments as in
8544 struct attribute_spec.handler. */
8546 static tree
8547 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8549 return NULL_TREE;
8552 /* Handle an "omp declare target" attribute; arguments as in
8553 struct attribute_spec.handler. */
8555 static tree
8556 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8558 return NULL_TREE;
8561 /* Handle a "returns_twice" attribute; arguments as in
8562 struct attribute_spec.handler. */
8564 static tree
8565 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8566 int ARG_UNUSED (flags), bool *no_add_attrs)
8568 if (TREE_CODE (*node) == FUNCTION_DECL)
8569 DECL_IS_RETURNS_TWICE (*node) = 1;
8570 else
8572 warning (OPT_Wattributes, "%qE attribute ignored", name);
8573 *no_add_attrs = true;
8576 return NULL_TREE;
8579 /* Handle a "no_limit_stack" attribute; arguments as in
8580 struct attribute_spec.handler. */
8582 static tree
8583 handle_no_limit_stack_attribute (tree *node, tree name,
8584 tree ARG_UNUSED (args),
8585 int ARG_UNUSED (flags),
8586 bool *no_add_attrs)
8588 tree decl = *node;
8590 if (TREE_CODE (decl) != FUNCTION_DECL)
8592 error_at (DECL_SOURCE_LOCATION (decl),
8593 "%qE attribute applies only to functions", name);
8594 *no_add_attrs = true;
8596 else if (DECL_INITIAL (decl))
8598 error_at (DECL_SOURCE_LOCATION (decl),
8599 "can%'t set %qE attribute after definition", name);
8600 *no_add_attrs = true;
8602 else
8603 DECL_NO_LIMIT_STACK (decl) = 1;
8605 return NULL_TREE;
8608 /* Handle a "pure" attribute; arguments as in
8609 struct attribute_spec.handler. */
8611 static tree
8612 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8613 int ARG_UNUSED (flags), bool *no_add_attrs)
8615 if (TREE_CODE (*node) == FUNCTION_DECL)
8616 DECL_PURE_P (*node) = 1;
8617 /* ??? TODO: Support types. */
8618 else
8620 warning (OPT_Wattributes, "%qE attribute ignored", name);
8621 *no_add_attrs = true;
8624 return NULL_TREE;
8627 /* Digest an attribute list destined for a transactional memory statement.
8628 ALLOWED is the set of attributes that are allowed for this statement;
8629 return the attribute we parsed. Multiple attributes are never allowed. */
8632 parse_tm_stmt_attr (tree attrs, int allowed)
8634 tree a_seen = NULL;
8635 int m_seen = 0;
8637 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8639 tree a = TREE_PURPOSE (attrs);
8640 int m = 0;
8642 if (is_attribute_p ("outer", a))
8643 m = TM_STMT_ATTR_OUTER;
8645 if ((m & allowed) == 0)
8647 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8648 continue;
8651 if (m_seen == 0)
8653 a_seen = a;
8654 m_seen = m;
8656 else if (m_seen == m)
8657 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8658 else
8659 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8662 return m_seen;
8665 /* Transform a TM attribute name into a maskable integer and back.
8666 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8667 to how the lack of an attribute is treated. */
8670 tm_attr_to_mask (tree attr)
8672 if (attr == NULL)
8673 return 0;
8674 if (is_attribute_p ("transaction_safe", attr))
8675 return TM_ATTR_SAFE;
8676 if (is_attribute_p ("transaction_callable", attr))
8677 return TM_ATTR_CALLABLE;
8678 if (is_attribute_p ("transaction_pure", attr))
8679 return TM_ATTR_PURE;
8680 if (is_attribute_p ("transaction_unsafe", attr))
8681 return TM_ATTR_IRREVOCABLE;
8682 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8683 return TM_ATTR_MAY_CANCEL_OUTER;
8684 return 0;
8687 tree
8688 tm_mask_to_attr (int mask)
8690 const char *str;
8691 switch (mask)
8693 case TM_ATTR_SAFE:
8694 str = "transaction_safe";
8695 break;
8696 case TM_ATTR_CALLABLE:
8697 str = "transaction_callable";
8698 break;
8699 case TM_ATTR_PURE:
8700 str = "transaction_pure";
8701 break;
8702 case TM_ATTR_IRREVOCABLE:
8703 str = "transaction_unsafe";
8704 break;
8705 case TM_ATTR_MAY_CANCEL_OUTER:
8706 str = "transaction_may_cancel_outer";
8707 break;
8708 default:
8709 gcc_unreachable ();
8711 return get_identifier (str);
8714 /* Return the first TM attribute seen in LIST. */
8716 tree
8717 find_tm_attribute (tree list)
8719 for (; list ; list = TREE_CHAIN (list))
8721 tree name = TREE_PURPOSE (list);
8722 if (tm_attr_to_mask (name) != 0)
8723 return name;
8725 return NULL_TREE;
8728 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8729 Here we accept only function types, and verify that none of the other
8730 function TM attributes are also applied. */
8731 /* ??? We need to accept class types for C++, but not C. This greatly
8732 complicates this function, since we can no longer rely on the extra
8733 processing given by function_type_required. */
8735 static tree
8736 handle_tm_attribute (tree *node, tree name, tree args,
8737 int flags, bool *no_add_attrs)
8739 /* Only one path adds the attribute; others don't. */
8740 *no_add_attrs = true;
8742 switch (TREE_CODE (*node))
8744 case RECORD_TYPE:
8745 case UNION_TYPE:
8746 /* Only tm_callable and tm_safe apply to classes. */
8747 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8748 goto ignored;
8749 /* FALLTHRU */
8751 case FUNCTION_TYPE:
8752 case METHOD_TYPE:
8754 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8755 if (old_name == name)
8757 else if (old_name != NULL_TREE)
8758 error ("type was previously declared %qE", old_name);
8759 else
8760 *no_add_attrs = false;
8762 break;
8764 case POINTER_TYPE:
8766 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8767 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8769 tree fn_tmp = TREE_TYPE (*node);
8770 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8771 *node = build_pointer_type (fn_tmp);
8772 break;
8775 /* FALLTHRU */
8777 default:
8778 /* If a function is next, pass it on to be tried next. */
8779 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8780 return tree_cons (name, args, NULL);
8782 ignored:
8783 warning (OPT_Wattributes, "%qE attribute ignored", name);
8784 break;
8787 return NULL_TREE;
8790 /* Handle the TM_WRAP attribute; arguments as in
8791 struct attribute_spec.handler. */
8793 static tree
8794 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8795 int ARG_UNUSED (flags), bool *no_add_attrs)
8797 tree decl = *node;
8799 /* We don't need the attribute even on success, since we
8800 record the entry in an external table. */
8801 *no_add_attrs = true;
8803 if (TREE_CODE (decl) != FUNCTION_DECL)
8804 warning (OPT_Wattributes, "%qE attribute ignored", name);
8805 else
8807 tree wrap_decl = TREE_VALUE (args);
8808 if (error_operand_p (wrap_decl))
8810 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8811 && TREE_CODE (wrap_decl) != VAR_DECL
8812 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8813 error ("%qE argument not an identifier", name);
8814 else
8816 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8817 wrap_decl = lookup_name (wrap_decl);
8818 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8820 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8821 TREE_TYPE (wrap_decl)))
8822 record_tm_replacement (wrap_decl, decl);
8823 else
8824 error ("%qD is not compatible with %qD", wrap_decl, decl);
8826 else
8827 error ("%qE argument is not a function", name);
8831 return NULL_TREE;
8834 /* Ignore the given attribute. Used when this attribute may be usefully
8835 overridden by the target, but is not used generically. */
8837 static tree
8838 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8839 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8840 bool *no_add_attrs)
8842 *no_add_attrs = true;
8843 return NULL_TREE;
8846 /* Handle a "no vops" attribute; arguments as in
8847 struct attribute_spec.handler. */
8849 static tree
8850 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8851 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8852 bool *ARG_UNUSED (no_add_attrs))
8854 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8855 DECL_IS_NOVOPS (*node) = 1;
8856 return NULL_TREE;
8859 /* Handle a "deprecated" attribute; arguments as in
8860 struct attribute_spec.handler. */
8862 static tree
8863 handle_deprecated_attribute (tree *node, tree name,
8864 tree args, int flags,
8865 bool *no_add_attrs)
8867 tree type = NULL_TREE;
8868 int warn = 0;
8869 tree what = NULL_TREE;
8871 if (!args)
8872 *no_add_attrs = true;
8873 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8875 error ("deprecated message is not a string");
8876 *no_add_attrs = true;
8879 if (DECL_P (*node))
8881 tree decl = *node;
8882 type = TREE_TYPE (decl);
8884 if (TREE_CODE (decl) == TYPE_DECL
8885 || TREE_CODE (decl) == PARM_DECL
8886 || TREE_CODE (decl) == VAR_DECL
8887 || TREE_CODE (decl) == FUNCTION_DECL
8888 || TREE_CODE (decl) == FIELD_DECL
8889 || objc_method_decl (TREE_CODE (decl)))
8890 TREE_DEPRECATED (decl) = 1;
8891 else
8892 warn = 1;
8894 else if (TYPE_P (*node))
8896 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8897 *node = build_variant_type_copy (*node);
8898 TREE_DEPRECATED (*node) = 1;
8899 type = *node;
8901 else
8902 warn = 1;
8904 if (warn)
8906 *no_add_attrs = true;
8907 if (type && TYPE_NAME (type))
8909 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8910 what = TYPE_NAME (*node);
8911 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8912 && DECL_NAME (TYPE_NAME (type)))
8913 what = DECL_NAME (TYPE_NAME (type));
8915 if (what)
8916 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8917 else
8918 warning (OPT_Wattributes, "%qE attribute ignored", name);
8921 return NULL_TREE;
8924 /* Handle a "vector_size" attribute; arguments as in
8925 struct attribute_spec.handler. */
8927 static tree
8928 handle_vector_size_attribute (tree *node, tree name, tree args,
8929 int ARG_UNUSED (flags),
8930 bool *no_add_attrs)
8932 unsigned HOST_WIDE_INT vecsize, nunits;
8933 machine_mode orig_mode;
8934 tree type = *node, new_type, size;
8936 *no_add_attrs = true;
8938 size = TREE_VALUE (args);
8939 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8940 && TREE_CODE (size) != FUNCTION_DECL)
8941 size = default_conversion (size);
8943 if (!tree_fits_uhwi_p (size))
8945 warning (OPT_Wattributes, "%qE attribute ignored", name);
8946 return NULL_TREE;
8949 /* Get the vector size (in bytes). */
8950 vecsize = tree_to_uhwi (size);
8952 /* We need to provide for vector pointers, vector arrays, and
8953 functions returning vectors. For example:
8955 __attribute__((vector_size(16))) short *foo;
8957 In this case, the mode is SI, but the type being modified is
8958 HI, so we need to look further. */
8960 while (POINTER_TYPE_P (type)
8961 || TREE_CODE (type) == FUNCTION_TYPE
8962 || TREE_CODE (type) == METHOD_TYPE
8963 || TREE_CODE (type) == ARRAY_TYPE
8964 || TREE_CODE (type) == OFFSET_TYPE)
8965 type = TREE_TYPE (type);
8967 /* Get the mode of the type being modified. */
8968 orig_mode = TYPE_MODE (type);
8970 if ((!INTEGRAL_TYPE_P (type)
8971 && !SCALAR_FLOAT_TYPE_P (type)
8972 && !FIXED_POINT_TYPE_P (type))
8973 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8974 && GET_MODE_CLASS (orig_mode) != MODE_INT
8975 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8976 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8977 || TREE_CODE (type) == BOOLEAN_TYPE)
8979 error ("invalid vector type for attribute %qE", name);
8980 return NULL_TREE;
8983 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8985 error ("vector size not an integral multiple of component size");
8986 return NULL;
8989 if (vecsize == 0)
8991 error ("zero vector size");
8992 return NULL;
8995 /* Calculate how many units fit in the vector. */
8996 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8997 if (nunits & (nunits - 1))
8999 error ("number of components of the vector not a power of two");
9000 return NULL_TREE;
9003 new_type = build_vector_type (type, nunits);
9005 /* Build back pointers if needed. */
9006 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9008 return NULL_TREE;
9011 /* Handle the "nonnull" attribute. */
9012 static tree
9013 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9014 tree args, int ARG_UNUSED (flags),
9015 bool *no_add_attrs)
9017 tree type = *node;
9018 unsigned HOST_WIDE_INT attr_arg_num;
9020 /* If no arguments are specified, all pointer arguments should be
9021 non-null. Verify a full prototype is given so that the arguments
9022 will have the correct types when we actually check them later. */
9023 if (!args)
9025 if (!prototype_p (type))
9027 error ("nonnull attribute without arguments on a non-prototype");
9028 *no_add_attrs = true;
9030 return NULL_TREE;
9033 /* Argument list specified. Verify that each argument number references
9034 a pointer argument. */
9035 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9037 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9039 tree arg = TREE_VALUE (args);
9040 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9041 && TREE_CODE (arg) != FUNCTION_DECL)
9042 arg = default_conversion (arg);
9044 if (!get_nonnull_operand (arg, &arg_num))
9046 error ("nonnull argument has invalid operand number (argument %lu)",
9047 (unsigned long) attr_arg_num);
9048 *no_add_attrs = true;
9049 return NULL_TREE;
9052 if (prototype_p (type))
9054 function_args_iterator iter;
9055 tree argument;
9057 function_args_iter_init (&iter, type);
9058 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9060 argument = function_args_iter_cond (&iter);
9061 if (argument == NULL_TREE || ck_num == arg_num)
9062 break;
9065 if (!argument
9066 || TREE_CODE (argument) == VOID_TYPE)
9068 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9069 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9070 *no_add_attrs = true;
9071 return NULL_TREE;
9074 if (TREE_CODE (argument) != POINTER_TYPE)
9076 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9077 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9078 *no_add_attrs = true;
9079 return NULL_TREE;
9084 return NULL_TREE;
9087 /* Check the argument list of a function call for null in argument slots
9088 that are marked as requiring a non-null pointer argument. The NARGS
9089 arguments are passed in the array ARGARRAY.
9092 static void
9093 check_function_nonnull (tree attrs, int nargs, tree *argarray)
9095 tree a;
9096 int i;
9098 attrs = lookup_attribute ("nonnull", attrs);
9099 if (attrs == NULL_TREE)
9100 return;
9102 a = attrs;
9103 /* See if any of the nonnull attributes has no arguments. If so,
9104 then every pointer argument is checked (in which case the check
9105 for pointer type is done in check_nonnull_arg). */
9106 if (TREE_VALUE (a) != NULL_TREE)
9108 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9109 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9111 if (a != NULL_TREE)
9112 for (i = 0; i < nargs; i++)
9113 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9114 i + 1);
9115 else
9117 /* Walk the argument list. If we encounter an argument number we
9118 should check for non-null, do it. */
9119 for (i = 0; i < nargs; i++)
9121 for (a = attrs; ; a = TREE_CHAIN (a))
9123 a = lookup_attribute ("nonnull", a);
9124 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9125 break;
9128 if (a != NULL_TREE)
9129 check_function_arguments_recurse (check_nonnull_arg, NULL,
9130 argarray[i], i + 1);
9135 /* Check that the Nth argument of a function call (counting backwards
9136 from the end) is a (pointer)0. The NARGS arguments are passed in the
9137 array ARGARRAY. */
9139 static void
9140 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9142 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9144 if (attr)
9146 int len = 0;
9147 int pos = 0;
9148 tree sentinel;
9149 function_args_iterator iter;
9150 tree t;
9152 /* Skip over the named arguments. */
9153 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9155 if (len == nargs)
9156 break;
9157 len++;
9160 if (TREE_VALUE (attr))
9162 tree p = TREE_VALUE (TREE_VALUE (attr));
9163 pos = TREE_INT_CST_LOW (p);
9166 /* The sentinel must be one of the varargs, i.e.
9167 in position >= the number of fixed arguments. */
9168 if ((nargs - 1 - pos) < len)
9170 warning (OPT_Wformat_,
9171 "not enough variable arguments to fit a sentinel");
9172 return;
9175 /* Validate the sentinel. */
9176 sentinel = argarray[nargs - 1 - pos];
9177 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9178 || !integer_zerop (sentinel))
9179 /* Although __null (in C++) is only an integer we allow it
9180 nevertheless, as we are guaranteed that it's exactly
9181 as wide as a pointer, and we don't want to force
9182 users to cast the NULL they have written there.
9183 We warn with -Wstrict-null-sentinel, though. */
9184 && (warn_strict_null_sentinel || null_node != sentinel))
9185 warning (OPT_Wformat_, "missing sentinel in function call");
9189 /* Helper for check_function_nonnull; given a list of operands which
9190 must be non-null in ARGS, determine if operand PARAM_NUM should be
9191 checked. */
9193 static bool
9194 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9196 unsigned HOST_WIDE_INT arg_num = 0;
9198 for (; args; args = TREE_CHAIN (args))
9200 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9202 gcc_assert (found);
9204 if (arg_num == param_num)
9205 return true;
9207 return false;
9210 /* Check that the function argument PARAM (which is operand number
9211 PARAM_NUM) is non-null. This is called by check_function_nonnull
9212 via check_function_arguments_recurse. */
9214 static void
9215 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9216 unsigned HOST_WIDE_INT param_num)
9218 /* Just skip checking the argument if it's not a pointer. This can
9219 happen if the "nonnull" attribute was given without an operand
9220 list (which means to check every pointer argument). */
9222 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9223 return;
9225 if (integer_zerop (param))
9226 warning (OPT_Wnonnull, "null argument where non-null required "
9227 "(argument %lu)", (unsigned long) param_num);
9230 /* Helper for nonnull attribute handling; fetch the operand number
9231 from the attribute argument list. */
9233 static bool
9234 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9236 /* Verify the arg number is a small constant. */
9237 if (tree_fits_uhwi_p (arg_num_expr))
9239 *valp = TREE_INT_CST_LOW (arg_num_expr);
9240 return true;
9242 else
9243 return false;
9246 /* Handle a "nothrow" attribute; arguments as in
9247 struct attribute_spec.handler. */
9249 static tree
9250 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9251 int ARG_UNUSED (flags), bool *no_add_attrs)
9253 if (TREE_CODE (*node) == FUNCTION_DECL)
9254 TREE_NOTHROW (*node) = 1;
9255 /* ??? TODO: Support types. */
9256 else
9258 warning (OPT_Wattributes, "%qE attribute ignored", name);
9259 *no_add_attrs = true;
9262 return NULL_TREE;
9265 /* Handle a "cleanup" attribute; arguments as in
9266 struct attribute_spec.handler. */
9268 static tree
9269 handle_cleanup_attribute (tree *node, tree name, tree args,
9270 int ARG_UNUSED (flags), bool *no_add_attrs)
9272 tree decl = *node;
9273 tree cleanup_id, cleanup_decl;
9275 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9276 for global destructors in C++. This requires infrastructure that
9277 we don't have generically at the moment. It's also not a feature
9278 we'd be missing too much, since we do have attribute constructor. */
9279 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9281 warning (OPT_Wattributes, "%qE attribute ignored", name);
9282 *no_add_attrs = true;
9283 return NULL_TREE;
9286 /* Verify that the argument is a function in scope. */
9287 /* ??? We could support pointers to functions here as well, if
9288 that was considered desirable. */
9289 cleanup_id = TREE_VALUE (args);
9290 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9292 error ("cleanup argument not an identifier");
9293 *no_add_attrs = true;
9294 return NULL_TREE;
9296 cleanup_decl = lookup_name (cleanup_id);
9297 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9299 error ("cleanup argument not a function");
9300 *no_add_attrs = true;
9301 return NULL_TREE;
9304 /* That the function has proper type is checked with the
9305 eventual call to build_function_call. */
9307 return NULL_TREE;
9310 /* Handle a "warn_unused_result" attribute. No special handling. */
9312 static tree
9313 handle_warn_unused_result_attribute (tree *node, tree name,
9314 tree ARG_UNUSED (args),
9315 int ARG_UNUSED (flags), bool *no_add_attrs)
9317 /* Ignore the attribute for functions not returning any value. */
9318 if (VOID_TYPE_P (TREE_TYPE (*node)))
9320 warning (OPT_Wattributes, "%qE attribute ignored", name);
9321 *no_add_attrs = true;
9324 return NULL_TREE;
9327 /* Handle a "sentinel" attribute. */
9329 static tree
9330 handle_sentinel_attribute (tree *node, tree name, tree args,
9331 int ARG_UNUSED (flags), bool *no_add_attrs)
9333 if (!prototype_p (*node))
9335 warning (OPT_Wattributes,
9336 "%qE attribute requires prototypes with named arguments", name);
9337 *no_add_attrs = true;
9339 else
9341 if (!stdarg_p (*node))
9343 warning (OPT_Wattributes,
9344 "%qE attribute only applies to variadic functions", name);
9345 *no_add_attrs = true;
9349 if (args)
9351 tree position = TREE_VALUE (args);
9352 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9353 && TREE_CODE (position) != FUNCTION_DECL)
9354 position = default_conversion (position);
9356 if (TREE_CODE (position) != INTEGER_CST
9357 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9359 warning (OPT_Wattributes,
9360 "requested position is not an integer constant");
9361 *no_add_attrs = true;
9363 else
9365 if (tree_int_cst_lt (position, integer_zero_node))
9367 warning (OPT_Wattributes,
9368 "requested position is less than zero");
9369 *no_add_attrs = true;
9374 return NULL_TREE;
9377 /* Handle a "type_generic" attribute. */
9379 static tree
9380 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9381 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9382 bool * ARG_UNUSED (no_add_attrs))
9384 /* Ensure we have a function type. */
9385 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9387 /* Ensure we have a variadic function. */
9388 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9390 return NULL_TREE;
9393 /* Handle a "target" attribute. */
9395 static tree
9396 handle_target_attribute (tree *node, tree name, tree args, int flags,
9397 bool *no_add_attrs)
9399 /* Ensure we have a function type. */
9400 if (TREE_CODE (*node) != FUNCTION_DECL)
9402 warning (OPT_Wattributes, "%qE attribute ignored", name);
9403 *no_add_attrs = true;
9405 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9406 flags))
9407 *no_add_attrs = true;
9409 return NULL_TREE;
9412 /* Arguments being collected for optimization. */
9413 typedef const char *const_char_p; /* For DEF_VEC_P. */
9414 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9417 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9418 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9419 false for #pragma GCC optimize. */
9421 bool
9422 parse_optimize_options (tree args, bool attr_p)
9424 bool ret = true;
9425 unsigned opt_argc;
9426 unsigned i;
9427 int saved_flag_strict_aliasing;
9428 const char **opt_argv;
9429 struct cl_decoded_option *decoded_options;
9430 unsigned int decoded_options_count;
9431 tree ap;
9433 /* Build up argv vector. Just in case the string is stored away, use garbage
9434 collected strings. */
9435 vec_safe_truncate (optimize_args, 0);
9436 vec_safe_push (optimize_args, (const char *) NULL);
9438 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9440 tree value = TREE_VALUE (ap);
9442 if (TREE_CODE (value) == INTEGER_CST)
9444 char buffer[20];
9445 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9446 vec_safe_push (optimize_args, ggc_strdup (buffer));
9449 else if (TREE_CODE (value) == STRING_CST)
9451 /* Split string into multiple substrings. */
9452 size_t len = TREE_STRING_LENGTH (value);
9453 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9454 char *end = p + len;
9455 char *comma;
9456 char *next_p = p;
9458 while (next_p != NULL)
9460 size_t len2;
9461 char *q, *r;
9463 p = next_p;
9464 comma = strchr (p, ',');
9465 if (comma)
9467 len2 = comma - p;
9468 *comma = '\0';
9469 next_p = comma+1;
9471 else
9473 len2 = end - p;
9474 next_p = NULL;
9477 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9479 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9480 options. */
9481 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9483 ret = false;
9484 if (attr_p)
9485 warning (OPT_Wattributes,
9486 "bad option %s to optimize attribute", p);
9487 else
9488 warning (OPT_Wpragmas,
9489 "bad option %s to pragma attribute", p);
9490 continue;
9493 if (*p != '-')
9495 *r++ = '-';
9497 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9498 itself is -Os, and any other switch begins with a -f. */
9499 if ((*p >= '0' && *p <= '9')
9500 || (p[0] == 's' && p[1] == '\0'))
9501 *r++ = 'O';
9502 else if (*p != 'O')
9503 *r++ = 'f';
9506 memcpy (r, p, len2);
9507 r[len2] = '\0';
9508 vec_safe_push (optimize_args, (const char *) q);
9514 opt_argc = optimize_args->length ();
9515 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9517 for (i = 1; i < opt_argc; i++)
9518 opt_argv[i] = (*optimize_args)[i];
9520 saved_flag_strict_aliasing = flag_strict_aliasing;
9522 /* Now parse the options. */
9523 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9524 &decoded_options,
9525 &decoded_options_count);
9526 decode_options (&global_options, &global_options_set,
9527 decoded_options, decoded_options_count,
9528 input_location, global_dc);
9530 targetm.override_options_after_change();
9532 /* Don't allow changing -fstrict-aliasing. */
9533 flag_strict_aliasing = saved_flag_strict_aliasing;
9535 optimize_args->truncate (0);
9536 return ret;
9539 /* For handling "optimize" attribute. arguments as in
9540 struct attribute_spec.handler. */
9542 static tree
9543 handle_optimize_attribute (tree *node, tree name, tree args,
9544 int ARG_UNUSED (flags), bool *no_add_attrs)
9546 /* Ensure we have a function type. */
9547 if (TREE_CODE (*node) != FUNCTION_DECL)
9549 warning (OPT_Wattributes, "%qE attribute ignored", name);
9550 *no_add_attrs = true;
9552 else
9554 struct cl_optimization cur_opts;
9555 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9557 /* Save current options. */
9558 cl_optimization_save (&cur_opts, &global_options);
9560 /* If we previously had some optimization options, use them as the
9561 default. */
9562 if (old_opts)
9563 cl_optimization_restore (&global_options,
9564 TREE_OPTIMIZATION (old_opts));
9566 /* Parse options, and update the vector. */
9567 parse_optimize_options (args, true);
9568 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9569 = build_optimization_node (&global_options);
9571 /* Restore current options. */
9572 cl_optimization_restore (&global_options, &cur_opts);
9575 return NULL_TREE;
9578 /* Handle a "no_split_stack" attribute. */
9580 static tree
9581 handle_no_split_stack_attribute (tree *node, tree name,
9582 tree ARG_UNUSED (args),
9583 int ARG_UNUSED (flags),
9584 bool *no_add_attrs)
9586 tree decl = *node;
9588 if (TREE_CODE (decl) != FUNCTION_DECL)
9590 error_at (DECL_SOURCE_LOCATION (decl),
9591 "%qE attribute applies only to functions", name);
9592 *no_add_attrs = true;
9594 else if (DECL_INITIAL (decl))
9596 error_at (DECL_SOURCE_LOCATION (decl),
9597 "can%'t set %qE attribute after definition", name);
9598 *no_add_attrs = true;
9601 return NULL_TREE;
9604 /* Handle a "returns_nonnull" attribute; arguments as in
9605 struct attribute_spec.handler. */
9607 static tree
9608 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9609 bool *no_add_attrs)
9611 // Even without a prototype we still have a return type we can check.
9612 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9614 error ("returns_nonnull attribute on a function not returning a pointer");
9615 *no_add_attrs = true;
9617 return NULL_TREE;
9620 /* Handle a "designated_init" attribute; arguments as in
9621 struct attribute_spec.handler. */
9623 static tree
9624 handle_designated_init_attribute (tree *node, tree name, tree, int,
9625 bool *no_add_attrs)
9627 if (TREE_CODE (*node) != RECORD_TYPE)
9629 error ("%qE attribute is only valid on %<struct%> type", name);
9630 *no_add_attrs = true;
9632 return NULL_TREE;
9636 /* Check for valid arguments being passed to a function with FNTYPE.
9637 There are NARGS arguments in the array ARGARRAY. */
9638 void
9639 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9641 /* Check for null being passed in a pointer argument that must be
9642 non-null. We also need to do this if format checking is enabled. */
9644 if (warn_nonnull)
9645 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9647 /* Check for errors in format strings. */
9649 if (warn_format || warn_suggest_attribute_format)
9650 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9652 if (warn_format)
9653 check_function_sentinel (fntype, nargs, argarray);
9656 /* Generic argument checking recursion routine. PARAM is the argument to
9657 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9658 once the argument is resolved. CTX is context for the callback. */
9659 void
9660 check_function_arguments_recurse (void (*callback)
9661 (void *, tree, unsigned HOST_WIDE_INT),
9662 void *ctx, tree param,
9663 unsigned HOST_WIDE_INT param_num)
9665 if (CONVERT_EXPR_P (param)
9666 && (TYPE_PRECISION (TREE_TYPE (param))
9667 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9669 /* Strip coercion. */
9670 check_function_arguments_recurse (callback, ctx,
9671 TREE_OPERAND (param, 0), param_num);
9672 return;
9675 if (TREE_CODE (param) == CALL_EXPR)
9677 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9678 tree attrs;
9679 bool found_format_arg = false;
9681 /* See if this is a call to a known internationalization function
9682 that modifies a format arg. Such a function may have multiple
9683 format_arg attributes (for example, ngettext). */
9685 for (attrs = TYPE_ATTRIBUTES (type);
9686 attrs;
9687 attrs = TREE_CHAIN (attrs))
9688 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9690 tree inner_arg;
9691 tree format_num_expr;
9692 int format_num;
9693 int i;
9694 call_expr_arg_iterator iter;
9696 /* Extract the argument number, which was previously checked
9697 to be valid. */
9698 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9700 format_num = tree_to_uhwi (format_num_expr);
9702 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9703 inner_arg != 0;
9704 inner_arg = next_call_expr_arg (&iter), i++)
9705 if (i == format_num)
9707 check_function_arguments_recurse (callback, ctx,
9708 inner_arg, param_num);
9709 found_format_arg = true;
9710 break;
9714 /* If we found a format_arg attribute and did a recursive check,
9715 we are done with checking this argument. Otherwise, we continue
9716 and this will be considered a non-literal. */
9717 if (found_format_arg)
9718 return;
9721 if (TREE_CODE (param) == COND_EXPR)
9723 /* Check both halves of the conditional expression. */
9724 check_function_arguments_recurse (callback, ctx,
9725 TREE_OPERAND (param, 1), param_num);
9726 check_function_arguments_recurse (callback, ctx,
9727 TREE_OPERAND (param, 2), param_num);
9728 return;
9731 (*callback) (ctx, param, param_num);
9734 /* Checks for a builtin function FNDECL that the number of arguments
9735 NARGS against the required number REQUIRED and issues an error if
9736 there is a mismatch. Returns true if the number of arguments is
9737 correct, otherwise false. */
9739 static bool
9740 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9742 if (nargs < required)
9744 error_at (input_location,
9745 "not enough arguments to function %qE", fndecl);
9746 return false;
9748 else if (nargs > required)
9750 error_at (input_location,
9751 "too many arguments to function %qE", fndecl);
9752 return false;
9754 return true;
9757 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9758 Returns false if there was an error, otherwise true. */
9760 bool
9761 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9763 if (!DECL_BUILT_IN (fndecl)
9764 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9765 return true;
9767 switch (DECL_FUNCTION_CODE (fndecl))
9769 case BUILT_IN_CONSTANT_P:
9770 return builtin_function_validate_nargs (fndecl, nargs, 1);
9772 case BUILT_IN_ISFINITE:
9773 case BUILT_IN_ISINF:
9774 case BUILT_IN_ISINF_SIGN:
9775 case BUILT_IN_ISNAN:
9776 case BUILT_IN_ISNORMAL:
9777 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9779 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9781 error ("non-floating-point argument in call to "
9782 "function %qE", fndecl);
9783 return false;
9785 return true;
9787 return false;
9789 case BUILT_IN_ISGREATER:
9790 case BUILT_IN_ISGREATEREQUAL:
9791 case BUILT_IN_ISLESS:
9792 case BUILT_IN_ISLESSEQUAL:
9793 case BUILT_IN_ISLESSGREATER:
9794 case BUILT_IN_ISUNORDERED:
9795 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9797 enum tree_code code0, code1;
9798 code0 = TREE_CODE (TREE_TYPE (args[0]));
9799 code1 = TREE_CODE (TREE_TYPE (args[1]));
9800 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9801 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9802 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9804 error ("non-floating-point arguments in call to "
9805 "function %qE", fndecl);
9806 return false;
9808 return true;
9810 return false;
9812 case BUILT_IN_FPCLASSIFY:
9813 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9815 unsigned i;
9817 for (i=0; i<5; i++)
9818 if (TREE_CODE (args[i]) != INTEGER_CST)
9820 error ("non-const integer argument %u in call to function %qE",
9821 i+1, fndecl);
9822 return false;
9825 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9827 error ("non-floating-point argument in call to function %qE",
9828 fndecl);
9829 return false;
9831 return true;
9833 return false;
9835 case BUILT_IN_ASSUME_ALIGNED:
9836 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9838 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9840 error ("non-integer argument 3 in call to function %qE", fndecl);
9841 return false;
9843 return true;
9845 return false;
9847 case BUILT_IN_ADD_OVERFLOW:
9848 case BUILT_IN_SUB_OVERFLOW:
9849 case BUILT_IN_MUL_OVERFLOW:
9850 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9852 unsigned i;
9853 for (i = 0; i < 2; i++)
9854 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9856 error ("argument %u in call to function %qE does not have "
9857 "integral type", i + 1, fndecl);
9858 return false;
9860 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9861 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9863 error ("argument 3 in call to function %qE does not have "
9864 "pointer to integer type", fndecl);
9865 return false;
9867 return true;
9869 return false;
9871 default:
9872 return true;
9876 /* Function to help qsort sort FIELD_DECLs by name order. */
9879 field_decl_cmp (const void *x_p, const void *y_p)
9881 const tree *const x = (const tree *const) x_p;
9882 const tree *const y = (const tree *const) y_p;
9884 if (DECL_NAME (*x) == DECL_NAME (*y))
9885 /* A nontype is "greater" than a type. */
9886 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9887 if (DECL_NAME (*x) == NULL_TREE)
9888 return -1;
9889 if (DECL_NAME (*y) == NULL_TREE)
9890 return 1;
9891 if (DECL_NAME (*x) < DECL_NAME (*y))
9892 return -1;
9893 return 1;
9896 static struct {
9897 gt_pointer_operator new_value;
9898 void *cookie;
9899 } resort_data;
9901 /* This routine compares two fields like field_decl_cmp but using the
9902 pointer operator in resort_data. */
9904 static int
9905 resort_field_decl_cmp (const void *x_p, const void *y_p)
9907 const tree *const x = (const tree *const) x_p;
9908 const tree *const y = (const tree *const) y_p;
9910 if (DECL_NAME (*x) == DECL_NAME (*y))
9911 /* A nontype is "greater" than a type. */
9912 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9913 if (DECL_NAME (*x) == NULL_TREE)
9914 return -1;
9915 if (DECL_NAME (*y) == NULL_TREE)
9916 return 1;
9918 tree d1 = DECL_NAME (*x);
9919 tree d2 = DECL_NAME (*y);
9920 resort_data.new_value (&d1, resort_data.cookie);
9921 resort_data.new_value (&d2, resort_data.cookie);
9922 if (d1 < d2)
9923 return -1;
9925 return 1;
9928 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9930 void
9931 resort_sorted_fields (void *obj,
9932 void * ARG_UNUSED (orig_obj),
9933 gt_pointer_operator new_value,
9934 void *cookie)
9936 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9937 resort_data.new_value = new_value;
9938 resort_data.cookie = cookie;
9939 qsort (&sf->elts[0], sf->len, sizeof (tree),
9940 resort_field_decl_cmp);
9943 /* Subroutine of c_parse_error.
9944 Return the result of concatenating LHS and RHS. RHS is really
9945 a string literal, its first character is indicated by RHS_START and
9946 RHS_SIZE is its length (including the terminating NUL character).
9948 The caller is responsible for deleting the returned pointer. */
9950 static char *
9951 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9953 const int lhs_size = strlen (lhs);
9954 char *result = XNEWVEC (char, lhs_size + rhs_size);
9955 strncpy (result, lhs, lhs_size);
9956 strncpy (result + lhs_size, rhs_start, rhs_size);
9957 return result;
9960 /* Issue the error given by GMSGID, indicating that it occurred before
9961 TOKEN, which had the associated VALUE. */
9963 void
9964 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9965 tree value, unsigned char token_flags)
9967 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9969 char *message = NULL;
9971 if (token_type == CPP_EOF)
9972 message = catenate_messages (gmsgid, " at end of input");
9973 else if (token_type == CPP_CHAR
9974 || token_type == CPP_WCHAR
9975 || token_type == CPP_CHAR16
9976 || token_type == CPP_CHAR32)
9978 unsigned int val = TREE_INT_CST_LOW (value);
9979 const char *prefix;
9981 switch (token_type)
9983 default:
9984 prefix = "";
9985 break;
9986 case CPP_WCHAR:
9987 prefix = "L";
9988 break;
9989 case CPP_CHAR16:
9990 prefix = "u";
9991 break;
9992 case CPP_CHAR32:
9993 prefix = "U";
9994 break;
9997 if (val <= UCHAR_MAX && ISGRAPH (val))
9998 message = catenate_messages (gmsgid, " before %s'%c'");
9999 else
10000 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10002 error (message, prefix, val);
10003 free (message);
10004 message = NULL;
10006 else if (token_type == CPP_CHAR_USERDEF
10007 || token_type == CPP_WCHAR_USERDEF
10008 || token_type == CPP_CHAR16_USERDEF
10009 || token_type == CPP_CHAR32_USERDEF)
10010 message = catenate_messages (gmsgid,
10011 " before user-defined character literal");
10012 else if (token_type == CPP_STRING_USERDEF
10013 || token_type == CPP_WSTRING_USERDEF
10014 || token_type == CPP_STRING16_USERDEF
10015 || token_type == CPP_STRING32_USERDEF
10016 || token_type == CPP_UTF8STRING_USERDEF)
10017 message = catenate_messages (gmsgid, " before user-defined string literal");
10018 else if (token_type == CPP_STRING
10019 || token_type == CPP_WSTRING
10020 || token_type == CPP_STRING16
10021 || token_type == CPP_STRING32
10022 || token_type == CPP_UTF8STRING)
10023 message = catenate_messages (gmsgid, " before string constant");
10024 else if (token_type == CPP_NUMBER)
10025 message = catenate_messages (gmsgid, " before numeric constant");
10026 else if (token_type == CPP_NAME)
10028 message = catenate_messages (gmsgid, " before %qE");
10029 error (message, value);
10030 free (message);
10031 message = NULL;
10033 else if (token_type == CPP_PRAGMA)
10034 message = catenate_messages (gmsgid, " before %<#pragma%>");
10035 else if (token_type == CPP_PRAGMA_EOL)
10036 message = catenate_messages (gmsgid, " before end of line");
10037 else if (token_type == CPP_DECLTYPE)
10038 message = catenate_messages (gmsgid, " before %<decltype%>");
10039 else if (token_type < N_TTYPES)
10041 message = catenate_messages (gmsgid, " before %qs token");
10042 error (message, cpp_type2name (token_type, token_flags));
10043 free (message);
10044 message = NULL;
10046 else
10047 error (gmsgid);
10049 if (message)
10051 error (message);
10052 free (message);
10054 #undef catenate_messages
10057 /* Return the gcc option code associated with the reason for a cpp
10058 message, or 0 if none. */
10060 static int
10061 c_option_controlling_cpp_error (int reason)
10063 const struct cpp_reason_option_codes_t *entry;
10065 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10067 if (entry->reason == reason)
10068 return entry->option_code;
10070 return 0;
10073 /* Callback from cpp_error for PFILE to print diagnostics from the
10074 preprocessor. The diagnostic is of type LEVEL, with REASON set
10075 to the reason code if LEVEL is represents a warning, at location
10076 LOCATION unless this is after lexing and the compiler's location
10077 should be used instead, with column number possibly overridden by
10078 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10079 the arguments. Returns true if a diagnostic was emitted, false
10080 otherwise. */
10082 bool
10083 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10084 location_t location, unsigned int column_override,
10085 const char *msg, va_list *ap)
10087 diagnostic_info diagnostic;
10088 diagnostic_t dlevel;
10089 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10090 bool ret;
10092 switch (level)
10094 case CPP_DL_WARNING_SYSHDR:
10095 if (flag_no_output)
10096 return false;
10097 global_dc->dc_warn_system_headers = 1;
10098 /* Fall through. */
10099 case CPP_DL_WARNING:
10100 if (flag_no_output)
10101 return false;
10102 dlevel = DK_WARNING;
10103 break;
10104 case CPP_DL_PEDWARN:
10105 if (flag_no_output && !flag_pedantic_errors)
10106 return false;
10107 dlevel = DK_PEDWARN;
10108 break;
10109 case CPP_DL_ERROR:
10110 dlevel = DK_ERROR;
10111 break;
10112 case CPP_DL_ICE:
10113 dlevel = DK_ICE;
10114 break;
10115 case CPP_DL_NOTE:
10116 dlevel = DK_NOTE;
10117 break;
10118 case CPP_DL_FATAL:
10119 dlevel = DK_FATAL;
10120 break;
10121 default:
10122 gcc_unreachable ();
10124 if (done_lexing)
10125 location = input_location;
10126 diagnostic_set_info_translated (&diagnostic, msg, ap,
10127 location, dlevel);
10128 if (column_override)
10129 diagnostic_override_column (&diagnostic, column_override);
10130 diagnostic_override_option_index (&diagnostic,
10131 c_option_controlling_cpp_error (reason));
10132 ret = report_diagnostic (&diagnostic);
10133 if (level == CPP_DL_WARNING_SYSHDR)
10134 global_dc->dc_warn_system_headers = save_warn_system_headers;
10135 return ret;
10138 /* Convert a character from the host to the target execution character
10139 set. cpplib handles this, mostly. */
10141 HOST_WIDE_INT
10142 c_common_to_target_charset (HOST_WIDE_INT c)
10144 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10145 zero-extended under -fno-signed-char. cpplib insists that characters
10146 and character constants are always unsigned. Hence we must convert
10147 back and forth. */
10148 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10150 uc = cpp_host_to_exec_charset (parse_in, uc);
10152 if (flag_signed_char)
10153 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10154 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10155 else
10156 return uc;
10159 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10160 references with an INDIRECT_REF of a constant at the bottom; much like the
10161 traditional rendering of offsetof as a macro. Return the folded result. */
10163 tree
10164 fold_offsetof_1 (tree expr)
10166 tree base, off, t;
10168 switch (TREE_CODE (expr))
10170 case ERROR_MARK:
10171 return expr;
10173 case VAR_DECL:
10174 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10175 return error_mark_node;
10177 case CALL_EXPR:
10178 case TARGET_EXPR:
10179 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10180 return error_mark_node;
10182 case NOP_EXPR:
10183 case INDIRECT_REF:
10184 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10186 error ("cannot apply %<offsetof%> to a non constant address");
10187 return error_mark_node;
10189 return TREE_OPERAND (expr, 0);
10191 case COMPONENT_REF:
10192 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10193 if (base == error_mark_node)
10194 return base;
10196 t = TREE_OPERAND (expr, 1);
10197 if (DECL_C_BIT_FIELD (t))
10199 error ("attempt to take address of bit-field structure "
10200 "member %qD", t);
10201 return error_mark_node;
10203 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10204 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10205 / BITS_PER_UNIT));
10206 break;
10208 case ARRAY_REF:
10209 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10210 if (base == error_mark_node)
10211 return base;
10213 t = TREE_OPERAND (expr, 1);
10215 /* Check if the offset goes beyond the upper bound of the array. */
10216 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10218 tree upbound = array_ref_up_bound (expr);
10219 if (upbound != NULL_TREE
10220 && TREE_CODE (upbound) == INTEGER_CST
10221 && !tree_int_cst_equal (upbound,
10222 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10224 upbound = size_binop (PLUS_EXPR, upbound,
10225 build_int_cst (TREE_TYPE (upbound), 1));
10226 if (tree_int_cst_lt (upbound, t))
10228 tree v;
10230 for (v = TREE_OPERAND (expr, 0);
10231 TREE_CODE (v) == COMPONENT_REF;
10232 v = TREE_OPERAND (v, 0))
10233 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10234 == RECORD_TYPE)
10236 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10237 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10238 if (TREE_CODE (fld_chain) == FIELD_DECL)
10239 break;
10241 if (fld_chain)
10242 break;
10244 /* Don't warn if the array might be considered a poor
10245 man's flexible array member with a very permissive
10246 definition thereof. */
10247 if (TREE_CODE (v) == ARRAY_REF
10248 || TREE_CODE (v) == COMPONENT_REF)
10249 warning (OPT_Warray_bounds,
10250 "index %E denotes an offset "
10251 "greater than size of %qT",
10252 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10257 t = convert (sizetype, t);
10258 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10259 break;
10261 case COMPOUND_EXPR:
10262 /* Handle static members of volatile structs. */
10263 t = TREE_OPERAND (expr, 1);
10264 gcc_assert (TREE_CODE (t) == VAR_DECL);
10265 return fold_offsetof_1 (t);
10267 default:
10268 gcc_unreachable ();
10271 return fold_build_pointer_plus (base, off);
10274 /* Likewise, but convert it to the return type of offsetof. */
10276 tree
10277 fold_offsetof (tree expr)
10279 return convert (size_type_node, fold_offsetof_1 (expr));
10282 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10283 expression, because B will always be true. */
10285 void
10286 warn_for_omitted_condop (location_t location, tree cond)
10288 if (truth_value_p (TREE_CODE (cond)))
10289 warning_at (location, OPT_Wparentheses,
10290 "the omitted middle operand in ?: will always be %<true%>, "
10291 "suggest explicit middle operand");
10294 /* Give an error for storing into ARG, which is 'const'. USE indicates
10295 how ARG was being used. */
10297 void
10298 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10300 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10301 || use == lv_asm);
10302 /* Using this macro rather than (for example) arrays of messages
10303 ensures that all the format strings are checked at compile
10304 time. */
10305 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10306 : (use == lv_increment ? (I) \
10307 : (use == lv_decrement ? (D) : (AS))))
10308 if (TREE_CODE (arg) == COMPONENT_REF)
10310 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10311 error_at (loc, READONLY_MSG (G_("assignment of member "
10312 "%qD in read-only object"),
10313 G_("increment of member "
10314 "%qD in read-only object"),
10315 G_("decrement of member "
10316 "%qD in read-only object"),
10317 G_("member %qD in read-only object "
10318 "used as %<asm%> output")),
10319 TREE_OPERAND (arg, 1));
10320 else
10321 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10322 G_("increment of read-only member %qD"),
10323 G_("decrement of read-only member %qD"),
10324 G_("read-only member %qD used as %<asm%> output")),
10325 TREE_OPERAND (arg, 1));
10327 else if (TREE_CODE (arg) == VAR_DECL)
10328 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10329 G_("increment of read-only variable %qD"),
10330 G_("decrement of read-only variable %qD"),
10331 G_("read-only variable %qD used as %<asm%> output")),
10332 arg);
10333 else if (TREE_CODE (arg) == PARM_DECL)
10334 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10335 G_("increment of read-only parameter %qD"),
10336 G_("decrement of read-only parameter %qD"),
10337 G_("read-only parameter %qD use as %<asm%> output")),
10338 arg);
10339 else if (TREE_CODE (arg) == RESULT_DECL)
10341 gcc_assert (c_dialect_cxx ());
10342 error_at (loc, READONLY_MSG (G_("assignment of "
10343 "read-only named return value %qD"),
10344 G_("increment of "
10345 "read-only named return value %qD"),
10346 G_("decrement of "
10347 "read-only named return value %qD"),
10348 G_("read-only named return value %qD "
10349 "used as %<asm%>output")),
10350 arg);
10352 else if (TREE_CODE (arg) == FUNCTION_DECL)
10353 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10354 G_("increment of function %qD"),
10355 G_("decrement of function %qD"),
10356 G_("function %qD used as %<asm%> output")),
10357 arg);
10358 else
10359 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10360 G_("increment of read-only location %qE"),
10361 G_("decrement of read-only location %qE"),
10362 G_("read-only location %qE used as %<asm%> output")),
10363 arg);
10366 /* Print an error message for an invalid lvalue. USE says
10367 how the lvalue is being used and so selects the error message. LOC
10368 is the location for the error. */
10370 void
10371 lvalue_error (location_t loc, enum lvalue_use use)
10373 switch (use)
10375 case lv_assign:
10376 error_at (loc, "lvalue required as left operand of assignment");
10377 break;
10378 case lv_increment:
10379 error_at (loc, "lvalue required as increment operand");
10380 break;
10381 case lv_decrement:
10382 error_at (loc, "lvalue required as decrement operand");
10383 break;
10384 case lv_addressof:
10385 error_at (loc, "lvalue required as unary %<&%> operand");
10386 break;
10387 case lv_asm:
10388 error_at (loc, "lvalue required in asm statement");
10389 break;
10390 default:
10391 gcc_unreachable ();
10395 /* Print an error message for an invalid indirection of type TYPE.
10396 ERRSTRING is the name of the operator for the indirection. */
10398 void
10399 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10401 switch (errstring)
10403 case RO_NULL:
10404 gcc_assert (c_dialect_cxx ());
10405 error_at (loc, "invalid type argument (have %qT)", type);
10406 break;
10407 case RO_ARRAY_INDEXING:
10408 error_at (loc,
10409 "invalid type argument of array indexing (have %qT)",
10410 type);
10411 break;
10412 case RO_UNARY_STAR:
10413 error_at (loc,
10414 "invalid type argument of unary %<*%> (have %qT)",
10415 type);
10416 break;
10417 case RO_ARROW:
10418 error_at (loc,
10419 "invalid type argument of %<->%> (have %qT)",
10420 type);
10421 break;
10422 case RO_ARROW_STAR:
10423 error_at (loc,
10424 "invalid type argument of %<->*%> (have %qT)",
10425 type);
10426 break;
10427 case RO_IMPLICIT_CONVERSION:
10428 error_at (loc,
10429 "invalid type argument of implicit conversion (have %qT)",
10430 type);
10431 break;
10432 default:
10433 gcc_unreachable ();
10437 /* *PTYPE is an incomplete array. Complete it with a domain based on
10438 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10439 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10440 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10443 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10445 tree maxindex, type, main_type, elt, unqual_elt;
10446 int failure = 0, quals;
10447 hashval_t hashcode = 0;
10448 bool overflow_p = false;
10450 maxindex = size_zero_node;
10451 if (initial_value)
10453 if (TREE_CODE (initial_value) == STRING_CST)
10455 int eltsize
10456 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10457 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10459 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10461 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10463 if (vec_safe_is_empty (v))
10465 if (pedantic)
10466 failure = 3;
10467 maxindex = ssize_int (-1);
10469 else
10471 tree curindex;
10472 unsigned HOST_WIDE_INT cnt;
10473 constructor_elt *ce;
10474 bool fold_p = false;
10476 if ((*v)[0].index)
10477 maxindex = (*v)[0].index, fold_p = true;
10479 curindex = maxindex;
10481 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10483 bool curfold_p = false;
10484 if (ce->index)
10485 curindex = ce->index, curfold_p = true;
10486 else
10488 if (fold_p)
10490 /* Since we treat size types now as ordinary
10491 unsigned types, we need an explicit overflow
10492 check. */
10493 tree orig = curindex;
10494 curindex = fold_convert (sizetype, curindex);
10495 overflow_p |= tree_int_cst_lt (curindex, orig);
10497 curindex = size_binop (PLUS_EXPR, curindex,
10498 size_one_node);
10500 if (tree_int_cst_lt (maxindex, curindex))
10501 maxindex = curindex, fold_p = curfold_p;
10503 if (fold_p)
10505 tree orig = maxindex;
10506 maxindex = fold_convert (sizetype, maxindex);
10507 overflow_p |= tree_int_cst_lt (maxindex, orig);
10511 else
10513 /* Make an error message unless that happened already. */
10514 if (initial_value != error_mark_node)
10515 failure = 1;
10518 else
10520 failure = 2;
10521 if (!do_default)
10522 return failure;
10525 type = *ptype;
10526 /* Force an indefinite layout factor. */
10527 if (upc_shared_type_p (type))
10528 type = c_build_qualified_type_1 (type, TYPE_QUAL_UPC_SHARED,
10529 size_zero_node);
10530 elt = TREE_TYPE (type);
10531 quals = TYPE_QUALS (strip_array_types (elt));
10532 if (quals == 0)
10533 unqual_elt = elt;
10534 else
10535 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10537 /* Using build_distinct_type_copy and modifying things afterward instead
10538 of using build_array_type to create a new type preserves all of the
10539 TYPE_LANG_FLAG_? bits that the front end may have set. */
10540 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10541 TREE_TYPE (main_type) = unqual_elt;
10542 TYPE_DOMAIN (main_type)
10543 = build_range_type (TREE_TYPE (maxindex),
10544 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10545 layout_type (main_type);
10547 /* Make sure we have the canonical MAIN_TYPE. */
10548 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10549 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10550 hashcode);
10551 main_type = type_hash_canon (hashcode, main_type);
10553 /* Fix the canonical type. */
10554 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10555 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10556 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10557 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10558 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10559 != TYPE_DOMAIN (main_type)))
10560 TYPE_CANONICAL (main_type)
10561 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10562 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10563 else
10564 TYPE_CANONICAL (main_type) = main_type;
10566 if (quals == 0)
10567 type = main_type;
10568 else
10569 type = c_build_qualified_type (main_type, quals);
10571 if (COMPLETE_TYPE_P (type)
10572 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10573 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10575 error ("size of array is too large");
10576 /* If we proceed with the array type as it is, we'll eventually
10577 crash in tree_to_[su]hwi(). */
10578 type = error_mark_node;
10581 *ptype = type;
10582 return failure;
10585 /* Like c_mark_addressable but don't check register qualifier. */
10586 void
10587 c_common_mark_addressable_vec (tree t)
10589 while (handled_component_p (t))
10590 t = TREE_OPERAND (t, 0);
10591 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10592 return;
10593 TREE_ADDRESSABLE (t) = 1;
10598 /* Used to help initialize the builtin-types.def table. When a type of
10599 the correct size doesn't exist, use error_mark_node instead of NULL.
10600 The later results in segfaults even when a decl using the type doesn't
10601 get invoked. */
10603 tree
10604 builtin_type_for_size (int size, bool unsignedp)
10606 tree type = c_common_type_for_size (size, unsignedp);
10607 return type ? type : error_mark_node;
10610 /* A helper function for resolve_overloaded_builtin in resolving the
10611 overloaded __sync_ builtins. Returns a positive power of 2 if the
10612 first operand of PARAMS is a pointer to a supported data type.
10613 Returns 0 if an error is encountered. */
10615 static int
10616 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10618 tree type;
10619 int size;
10621 if (!params)
10623 error ("too few arguments to function %qE", function);
10624 return 0;
10627 type = TREE_TYPE ((*params)[0]);
10628 if (TREE_CODE (type) == ARRAY_TYPE)
10630 /* Force array-to-pointer decay for C++. */
10631 gcc_assert (c_dialect_cxx());
10632 (*params)[0] = default_conversion ((*params)[0]);
10633 type = TREE_TYPE ((*params)[0]);
10635 if (TREE_CODE (type) != POINTER_TYPE)
10636 goto incompatible;
10638 type = TREE_TYPE (type);
10639 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10640 goto incompatible;
10642 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10643 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10644 return size;
10646 incompatible:
10647 error ("incompatible type for argument %d of %qE", 1, function);
10648 return 0;
10651 /* A helper function for resolve_overloaded_builtin. Adds casts to
10652 PARAMS to make arguments match up with those of FUNCTION. Drops
10653 the variadic arguments at the end. Returns false if some error
10654 was encountered; true on success. */
10656 static bool
10657 sync_resolve_params (location_t loc, tree orig_function, tree function,
10658 vec<tree, va_gc> *params, bool orig_format)
10660 function_args_iterator iter;
10661 tree ptype;
10662 unsigned int parmnum;
10664 function_args_iter_init (&iter, TREE_TYPE (function));
10665 /* We've declared the implementation functions to use "volatile void *"
10666 as the pointer parameter, so we shouldn't get any complaints from the
10667 call to check_function_arguments what ever type the user used. */
10668 function_args_iter_next (&iter);
10669 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10670 ptype = TYPE_MAIN_VARIANT (ptype);
10672 /* For the rest of the values, we need to cast these to FTYPE, so that we
10673 don't get warnings for passing pointer types, etc. */
10674 parmnum = 0;
10675 while (1)
10677 tree val, arg_type;
10679 arg_type = function_args_iter_cond (&iter);
10680 /* XXX void_type_node belies the abstraction. */
10681 if (arg_type == void_type_node)
10682 break;
10684 ++parmnum;
10685 if (params->length () <= parmnum)
10687 error_at (loc, "too few arguments to function %qE", orig_function);
10688 return false;
10691 /* Only convert parameters if arg_type is unsigned integer type with
10692 new format sync routines, i.e. don't attempt to convert pointer
10693 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10694 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10695 kinds). */
10696 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10698 /* Ideally for the first conversion we'd use convert_for_assignment
10699 so that we get warnings for anything that doesn't match the pointer
10700 type. This isn't portable across the C and C++ front ends atm. */
10701 val = (*params)[parmnum];
10702 val = convert (ptype, val);
10703 val = convert (arg_type, val);
10704 (*params)[parmnum] = val;
10707 function_args_iter_next (&iter);
10710 /* __atomic routines are not variadic. */
10711 if (!orig_format && params->length () != parmnum + 1)
10713 error_at (loc, "too many arguments to function %qE", orig_function);
10714 return false;
10717 /* The definition of these primitives is variadic, with the remaining
10718 being "an optional list of variables protected by the memory barrier".
10719 No clue what that's supposed to mean, precisely, but we consider all
10720 call-clobbered variables to be protected so we're safe. */
10721 params->truncate (parmnum + 1);
10723 return true;
10726 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10727 RESULT to make it match the type of the first pointer argument in
10728 PARAMS. */
10730 static tree
10731 sync_resolve_return (tree first_param, tree result, bool orig_format)
10733 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10734 tree rtype = TREE_TYPE (result);
10735 ptype = TYPE_MAIN_VARIANT (ptype);
10737 /* New format doesn't require casting unless the types are the same size. */
10738 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10739 return convert (ptype, result);
10740 else
10741 return result;
10744 /* This function verifies the PARAMS to generic atomic FUNCTION.
10745 It returns the size if all the parameters are the same size, otherwise
10746 0 is returned if the parameters are invalid. */
10748 static int
10749 get_atomic_generic_size (location_t loc, tree function,
10750 vec<tree, va_gc> *params)
10752 unsigned int n_param;
10753 unsigned int n_model;
10754 unsigned int x;
10755 int size_0;
10756 tree type_0;
10758 /* Determine the parameter makeup. */
10759 switch (DECL_FUNCTION_CODE (function))
10761 case BUILT_IN_ATOMIC_EXCHANGE:
10762 n_param = 4;
10763 n_model = 1;
10764 break;
10765 case BUILT_IN_ATOMIC_LOAD:
10766 case BUILT_IN_ATOMIC_STORE:
10767 n_param = 3;
10768 n_model = 1;
10769 break;
10770 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10771 n_param = 6;
10772 n_model = 2;
10773 break;
10774 default:
10775 gcc_unreachable ();
10778 if (vec_safe_length (params) != n_param)
10780 error_at (loc, "incorrect number of arguments to function %qE", function);
10781 return 0;
10784 /* Get type of first parameter, and determine its size. */
10785 type_0 = TREE_TYPE ((*params)[0]);
10786 if (TREE_CODE (type_0) == ARRAY_TYPE)
10788 /* Force array-to-pointer decay for C++. */
10789 gcc_assert (c_dialect_cxx());
10790 (*params)[0] = default_conversion ((*params)[0]);
10791 type_0 = TREE_TYPE ((*params)[0]);
10793 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10795 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10796 function);
10797 return 0;
10800 /* Types must be compile time constant sizes. */
10801 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10803 error_at (loc,
10804 "argument 1 of %qE must be a pointer to a constant size type",
10805 function);
10806 return 0;
10809 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10811 /* Zero size objects are not allowed. */
10812 if (size_0 == 0)
10814 error_at (loc,
10815 "argument 1 of %qE must be a pointer to a nonzero size object",
10816 function);
10817 return 0;
10820 /* Check each other parameter is a pointer and the same size. */
10821 for (x = 0; x < n_param - n_model; x++)
10823 int size;
10824 tree type = TREE_TYPE ((*params)[x]);
10825 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10826 if (n_param == 6 && x == 3)
10827 continue;
10828 if (!POINTER_TYPE_P (type))
10830 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10831 function);
10832 return 0;
10834 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10835 size = type_size ? tree_to_uhwi (type_size) : 0;
10836 if (size != size_0)
10838 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10839 function);
10840 return 0;
10844 /* Check memory model parameters for validity. */
10845 for (x = n_param - n_model ; x < n_param; x++)
10847 tree p = (*params)[x];
10848 if (TREE_CODE (p) == INTEGER_CST)
10850 int i = tree_to_uhwi (p);
10851 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10853 warning_at (loc, OPT_Winvalid_memory_model,
10854 "invalid memory model argument %d of %qE", x + 1,
10855 function);
10858 else
10859 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10861 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10862 function);
10863 return 0;
10867 return size_0;
10871 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10872 at the beginning of the parameter list PARAMS representing the size of the
10873 objects. This is to match the library ABI requirement. LOC is the location
10874 of the function call.
10875 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10876 returned to allow the external call to be constructed. */
10878 static tree
10879 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10880 vec<tree, va_gc> *params)
10882 tree size_node;
10884 /* Insert a SIZE_T parameter as the first param. If there isn't
10885 enough space, allocate a new vector and recursively re-build with that. */
10886 if (!params->space (1))
10888 unsigned int z, len;
10889 vec<tree, va_gc> *v;
10890 tree f;
10892 len = params->length ();
10893 vec_alloc (v, len + 1);
10894 v->quick_push (build_int_cst (size_type_node, n));
10895 for (z = 0; z < len; z++)
10896 v->quick_push ((*params)[z]);
10897 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10898 vec_free (v);
10899 return f;
10902 /* Add the size parameter and leave as a function call for processing. */
10903 size_node = build_int_cst (size_type_node, n);
10904 params->quick_insert (0, size_node);
10905 return NULL_TREE;
10909 /* Return whether atomic operations for naturally aligned N-byte
10910 arguments are supported, whether inline or through libatomic. */
10911 static bool
10912 atomic_size_supported_p (int n)
10914 switch (n)
10916 case 1:
10917 case 2:
10918 case 4:
10919 case 8:
10920 return true;
10922 case 16:
10923 return targetm.scalar_mode_supported_p (TImode);
10925 default:
10926 return false;
10930 /* This will process an __atomic_exchange function call, determine whether it
10931 needs to be mapped to the _N variation, or turned into a library call.
10932 LOC is the location of the builtin call.
10933 FUNCTION is the DECL that has been invoked;
10934 PARAMS is the argument list for the call. The return value is non-null
10935 TRUE is returned if it is translated into the proper format for a call to the
10936 external library, and NEW_RETURN is set the tree for that function.
10937 FALSE is returned if processing for the _N variation is required, and
10938 NEW_RETURN is set to the the return value the result is copied into. */
10939 static bool
10940 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10941 vec<tree, va_gc> *params, tree *new_return)
10943 tree p0, p1, p2, p3;
10944 tree I_type, I_type_ptr;
10945 int n = get_atomic_generic_size (loc, function, params);
10947 /* Size of 0 is an error condition. */
10948 if (n == 0)
10950 *new_return = error_mark_node;
10951 return true;
10954 /* If not a lock-free size, change to the library generic format. */
10955 if (!atomic_size_supported_p (n))
10957 *new_return = add_atomic_size_parameter (n, loc, function, params);
10958 return true;
10961 /* Otherwise there is a lockfree match, transform the call from:
10962 void fn(T* mem, T* desired, T* return, model)
10963 into
10964 *return = (T) (fn (In* mem, (In) *desired, model)) */
10966 p0 = (*params)[0];
10967 p1 = (*params)[1];
10968 p2 = (*params)[2];
10969 p3 = (*params)[3];
10971 /* Create pointer to appropriate size. */
10972 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10973 I_type_ptr = build_pointer_type (I_type);
10975 /* Convert object pointer to required type. */
10976 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10977 (*params)[0] = p0;
10978 /* Convert new value to required type, and dereference it. */
10979 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10980 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10981 (*params)[1] = p1;
10983 /* Move memory model to the 3rd position, and end param list. */
10984 (*params)[2] = p3;
10985 params->truncate (3);
10987 /* Convert return pointer and dereference it for later assignment. */
10988 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10990 return false;
10994 /* This will process an __atomic_compare_exchange function call, determine
10995 whether it needs to be mapped to the _N variation, or turned into a lib call.
10996 LOC is the location of the builtin call.
10997 FUNCTION is the DECL that has been invoked;
10998 PARAMS is the argument list for the call. The return value is non-null
10999 TRUE is returned if it is translated into the proper format for a call to the
11000 external library, and NEW_RETURN is set the tree for that function.
11001 FALSE is returned if processing for the _N variation is required. */
11003 static bool
11004 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11005 vec<tree, va_gc> *params,
11006 tree *new_return)
11008 tree p0, p1, p2;
11009 tree I_type, I_type_ptr;
11010 int n = get_atomic_generic_size (loc, function, params);
11012 /* Size of 0 is an error condition. */
11013 if (n == 0)
11015 *new_return = error_mark_node;
11016 return true;
11019 /* If not a lock-free size, change to the library generic format. */
11020 if (!atomic_size_supported_p (n))
11022 /* The library generic format does not have the weak parameter, so
11023 remove it from the param list. Since a parameter has been removed,
11024 we can be sure that there is room for the SIZE_T parameter, meaning
11025 there will not be a recursive rebuilding of the parameter list, so
11026 there is no danger this will be done twice. */
11027 if (n > 0)
11029 (*params)[3] = (*params)[4];
11030 (*params)[4] = (*params)[5];
11031 params->truncate (5);
11033 *new_return = add_atomic_size_parameter (n, loc, function, params);
11034 return true;
11037 /* Otherwise, there is a match, so the call needs to be transformed from:
11038 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11039 into
11040 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11042 p0 = (*params)[0];
11043 p1 = (*params)[1];
11044 p2 = (*params)[2];
11046 /* Create pointer to appropriate size. */
11047 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11048 I_type_ptr = build_pointer_type (I_type);
11050 /* Convert object pointer to required type. */
11051 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11052 (*params)[0] = p0;
11054 /* Convert expected pointer to required type. */
11055 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11056 (*params)[1] = p1;
11058 /* Convert desired value to required type, and dereference it. */
11059 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11060 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11061 (*params)[2] = p2;
11063 /* The rest of the parameters are fine. NULL means no special return value
11064 processing.*/
11065 *new_return = NULL;
11066 return false;
11070 /* This will process an __atomic_load function call, determine whether it
11071 needs to be mapped to the _N variation, or turned into a library call.
11072 LOC is the location of the builtin call.
11073 FUNCTION is the DECL that has been invoked;
11074 PARAMS is the argument list for the call. The return value is non-null
11075 TRUE is returned if it is translated into the proper format for a call to the
11076 external library, and NEW_RETURN is set the tree for that function.
11077 FALSE is returned if processing for the _N variation is required, and
11078 NEW_RETURN is set to the the return value the result is copied into. */
11080 static bool
11081 resolve_overloaded_atomic_load (location_t loc, tree function,
11082 vec<tree, va_gc> *params, tree *new_return)
11084 tree p0, p1, p2;
11085 tree I_type, I_type_ptr;
11086 int n = get_atomic_generic_size (loc, function, params);
11088 /* Size of 0 is an error condition. */
11089 if (n == 0)
11091 *new_return = error_mark_node;
11092 return true;
11095 /* If not a lock-free size, change to the library generic format. */
11096 if (!atomic_size_supported_p (n))
11098 *new_return = add_atomic_size_parameter (n, loc, function, params);
11099 return true;
11102 /* Otherwise, there is a match, so the call needs to be transformed from:
11103 void fn(T* mem, T* return, model)
11104 into
11105 *return = (T) (fn ((In *) mem, model)) */
11107 p0 = (*params)[0];
11108 p1 = (*params)[1];
11109 p2 = (*params)[2];
11111 /* Create pointer to appropriate size. */
11112 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11113 I_type_ptr = build_pointer_type (I_type);
11115 /* Convert object pointer to required type. */
11116 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11117 (*params)[0] = p0;
11119 /* Move memory model to the 2nd position, and end param list. */
11120 (*params)[1] = p2;
11121 params->truncate (2);
11123 /* Convert return pointer and dereference it for later assignment. */
11124 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11126 return false;
11130 /* This will process an __atomic_store function call, determine whether it
11131 needs to be mapped to the _N variation, or turned into a library call.
11132 LOC is the location of the builtin call.
11133 FUNCTION is the DECL that has been invoked;
11134 PARAMS is the argument list for the call. The return value is non-null
11135 TRUE is returned if it is translated into the proper format for a call to the
11136 external library, and NEW_RETURN is set the tree for that function.
11137 FALSE is returned if processing for the _N variation is required, and
11138 NEW_RETURN is set to the the return value the result is copied into. */
11140 static bool
11141 resolve_overloaded_atomic_store (location_t loc, tree function,
11142 vec<tree, va_gc> *params, tree *new_return)
11144 tree p0, p1;
11145 tree I_type, I_type_ptr;
11146 int n = get_atomic_generic_size (loc, function, params);
11148 /* Size of 0 is an error condition. */
11149 if (n == 0)
11151 *new_return = error_mark_node;
11152 return true;
11155 /* If not a lock-free size, change to the library generic format. */
11156 if (!atomic_size_supported_p (n))
11158 *new_return = add_atomic_size_parameter (n, loc, function, params);
11159 return true;
11162 /* Otherwise, there is a match, so the call needs to be transformed from:
11163 void fn(T* mem, T* value, model)
11164 into
11165 fn ((In *) mem, (In) *value, model) */
11167 p0 = (*params)[0];
11168 p1 = (*params)[1];
11170 /* Create pointer to appropriate size. */
11171 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11172 I_type_ptr = build_pointer_type (I_type);
11174 /* Convert object pointer to required type. */
11175 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11176 (*params)[0] = p0;
11178 /* Convert new value to required type, and dereference it. */
11179 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11180 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11181 (*params)[1] = p1;
11183 /* The memory model is in the right spot already. Return is void. */
11184 *new_return = NULL_TREE;
11186 return false;
11190 /* Some builtin functions are placeholders for other expressions. This
11191 function should be called immediately after parsing the call expression
11192 before surrounding code has committed to the type of the expression.
11194 LOC is the location of the builtin call.
11196 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11197 PARAMS is the argument list for the call. The return value is non-null
11198 when expansion is complete, and null if normal processing should
11199 continue. */
11201 tree
11202 resolve_overloaded_builtin (location_t loc, tree function,
11203 vec<tree, va_gc> *params)
11205 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11206 bool orig_format = true;
11207 tree new_return = NULL_TREE;
11209 switch (DECL_BUILT_IN_CLASS (function))
11211 case BUILT_IN_NORMAL:
11212 break;
11213 case BUILT_IN_MD:
11214 if (targetm.resolve_overloaded_builtin)
11215 return targetm.resolve_overloaded_builtin (loc, function, params);
11216 else
11217 return NULL_TREE;
11218 default:
11219 return NULL_TREE;
11222 /* Handle BUILT_IN_NORMAL here. */
11223 switch (orig_code)
11225 case BUILT_IN_ATOMIC_EXCHANGE:
11226 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11227 case BUILT_IN_ATOMIC_LOAD:
11228 case BUILT_IN_ATOMIC_STORE:
11230 /* Handle these 4 together so that they can fall through to the next
11231 case if the call is transformed to an _N variant. */
11232 switch (orig_code)
11234 case BUILT_IN_ATOMIC_EXCHANGE:
11236 if (resolve_overloaded_atomic_exchange (loc, function, params,
11237 &new_return))
11238 return new_return;
11239 /* Change to the _N variant. */
11240 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11241 break;
11244 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11246 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11247 params,
11248 &new_return))
11249 return new_return;
11250 /* Change to the _N variant. */
11251 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11252 break;
11254 case BUILT_IN_ATOMIC_LOAD:
11256 if (resolve_overloaded_atomic_load (loc, function, params,
11257 &new_return))
11258 return new_return;
11259 /* Change to the _N variant. */
11260 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11261 break;
11263 case BUILT_IN_ATOMIC_STORE:
11265 if (resolve_overloaded_atomic_store (loc, function, params,
11266 &new_return))
11267 return new_return;
11268 /* Change to the _N variant. */
11269 orig_code = BUILT_IN_ATOMIC_STORE_N;
11270 break;
11272 default:
11273 gcc_unreachable ();
11275 /* Fallthrough to the normal processing. */
11277 case BUILT_IN_ATOMIC_EXCHANGE_N:
11278 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11279 case BUILT_IN_ATOMIC_LOAD_N:
11280 case BUILT_IN_ATOMIC_STORE_N:
11281 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11282 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11283 case BUILT_IN_ATOMIC_AND_FETCH_N:
11284 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11285 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11286 case BUILT_IN_ATOMIC_OR_FETCH_N:
11287 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11288 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11289 case BUILT_IN_ATOMIC_FETCH_AND_N:
11290 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11291 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11292 case BUILT_IN_ATOMIC_FETCH_OR_N:
11294 orig_format = false;
11295 /* Fallthru for parameter processing. */
11297 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11298 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11299 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11300 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11301 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11302 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11303 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11304 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11305 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11306 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11307 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11308 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11309 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11310 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11311 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11312 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11314 int n = sync_resolve_size (function, params);
11315 tree new_function, first_param, result;
11316 enum built_in_function fncode;
11318 if (n == 0)
11319 return error_mark_node;
11321 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11322 new_function = builtin_decl_explicit (fncode);
11323 if (!sync_resolve_params (loc, function, new_function, params,
11324 orig_format))
11325 return error_mark_node;
11327 first_param = (*params)[0];
11328 result = build_function_call_vec (loc, vNULL, new_function, params,
11329 NULL);
11330 if (result == error_mark_node)
11331 return result;
11332 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11333 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11334 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11335 result = sync_resolve_return (first_param, result, orig_format);
11337 /* If new_return is set, assign function to that expr and cast the
11338 result to void since the generic interface returned void. */
11339 if (new_return)
11341 /* Cast function result from I{1,2,4,8,16} to the required type. */
11342 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11343 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11344 result);
11345 TREE_SIDE_EFFECTS (result) = 1;
11346 protected_set_expr_location (result, loc);
11347 result = convert (void_type_node, result);
11349 return result;
11352 default:
11353 return NULL_TREE;
11357 /* vector_types_compatible_elements_p is used in type checks of vectors
11358 values used as operands of binary operators. Where it returns true, and
11359 the other checks of the caller succeed (being vector types in he first
11360 place, and matching number of elements), we can just treat the types
11361 as essentially the same.
11362 Contrast with vector_targets_convertible_p, which is used for vector
11363 pointer types, and vector_types_convertible_p, which will allow
11364 language-specific matches under the control of flag_lax_vector_conversions,
11365 and might still require a conversion. */
11366 /* True if vector types T1 and T2 can be inputs to the same binary
11367 operator without conversion.
11368 We don't check the overall vector size here because some of our callers
11369 want to give different error messages when the vectors are compatible
11370 except for the element count. */
11372 bool
11373 vector_types_compatible_elements_p (tree t1, tree t2)
11375 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11376 t1 = TREE_TYPE (t1);
11377 t2 = TREE_TYPE (t2);
11379 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11381 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11382 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11383 || c2 == FIXED_POINT_TYPE));
11385 t1 = c_common_signed_type (t1);
11386 t2 = c_common_signed_type (t2);
11387 /* Equality works here because c_common_signed_type uses
11388 TYPE_MAIN_VARIANT. */
11389 if (t1 == t2)
11390 return true;
11391 if (opaque && c1 == c2
11392 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11393 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11394 return true;
11395 return false;
11398 /* Check for missing format attributes on function pointers. LTYPE is
11399 the new type or left-hand side type. RTYPE is the old type or
11400 right-hand side type. Returns TRUE if LTYPE is missing the desired
11401 attribute. */
11403 bool
11404 check_missing_format_attribute (tree ltype, tree rtype)
11406 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11407 tree ra;
11409 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11410 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11411 break;
11412 if (ra)
11414 tree la;
11415 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11416 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11417 break;
11418 return !la;
11420 else
11421 return false;
11424 /* Subscripting with type char is likely to lose on a machine where
11425 chars are signed. So warn on any machine, but optionally. Don't
11426 warn for unsigned char since that type is safe. Don't warn for
11427 signed char because anyone who uses that must have done so
11428 deliberately. Furthermore, we reduce the false positive load by
11429 warning only for non-constant value of type char. */
11431 void
11432 warn_array_subscript_with_type_char (location_t loc, tree index)
11434 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11435 && TREE_CODE (index) != INTEGER_CST)
11436 warning_at (loc, OPT_Wchar_subscripts,
11437 "array subscript has type %<char%>");
11440 /* Implement -Wparentheses for the unexpected C precedence rules, to
11441 cover cases like x + y << z which readers are likely to
11442 misinterpret. We have seen an expression in which CODE is a binary
11443 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11444 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11445 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11446 expression was not formed using a binary or unary operator, or it
11447 was enclosed in parentheses. */
11449 void
11450 warn_about_parentheses (location_t loc, enum tree_code code,
11451 enum tree_code code_left, tree arg_left,
11452 enum tree_code code_right, tree arg_right)
11454 if (!warn_parentheses)
11455 return;
11457 /* This macro tests that the expression ARG with original tree code
11458 CODE appears to be a boolean expression. or the result of folding a
11459 boolean expression. */
11460 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11461 (truth_value_p (TREE_CODE (ARG)) \
11462 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11463 /* Folding may create 0 or 1 integers from other expressions. */ \
11464 || ((CODE) != INTEGER_CST \
11465 && (integer_onep (ARG) || integer_zerop (ARG))))
11467 switch (code)
11469 case LSHIFT_EXPR:
11470 if (code_left == PLUS_EXPR)
11471 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11472 "suggest parentheses around %<+%> inside %<<<%>");
11473 else if (code_right == PLUS_EXPR)
11474 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11475 "suggest parentheses around %<+%> inside %<<<%>");
11476 else if (code_left == MINUS_EXPR)
11477 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11478 "suggest parentheses around %<-%> inside %<<<%>");
11479 else if (code_right == MINUS_EXPR)
11480 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11481 "suggest parentheses around %<-%> inside %<<<%>");
11482 return;
11484 case RSHIFT_EXPR:
11485 if (code_left == PLUS_EXPR)
11486 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11487 "suggest parentheses around %<+%> inside %<>>%>");
11488 else if (code_right == PLUS_EXPR)
11489 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11490 "suggest parentheses around %<+%> inside %<>>%>");
11491 else if (code_left == MINUS_EXPR)
11492 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11493 "suggest parentheses around %<-%> inside %<>>%>");
11494 else if (code_right == MINUS_EXPR)
11495 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11496 "suggest parentheses around %<-%> inside %<>>%>");
11497 return;
11499 case TRUTH_ORIF_EXPR:
11500 if (code_left == TRUTH_ANDIF_EXPR)
11501 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11502 "suggest parentheses around %<&&%> within %<||%>");
11503 else if (code_right == TRUTH_ANDIF_EXPR)
11504 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11505 "suggest parentheses around %<&&%> within %<||%>");
11506 return;
11508 case BIT_IOR_EXPR:
11509 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11510 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11511 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11512 "suggest parentheses around arithmetic in operand of %<|%>");
11513 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11514 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11515 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11516 "suggest parentheses around arithmetic in operand of %<|%>");
11517 /* Check cases like x|y==z */
11518 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11519 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11520 "suggest parentheses around comparison in operand of %<|%>");
11521 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11522 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11523 "suggest parentheses around comparison in operand of %<|%>");
11524 /* Check cases like !x | y */
11525 else if (code_left == TRUTH_NOT_EXPR
11526 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11527 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11528 "suggest parentheses around operand of "
11529 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11530 return;
11532 case BIT_XOR_EXPR:
11533 if (code_left == BIT_AND_EXPR
11534 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11535 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11536 "suggest parentheses around arithmetic in operand of %<^%>");
11537 else if (code_right == BIT_AND_EXPR
11538 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11539 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11540 "suggest parentheses around arithmetic in operand of %<^%>");
11541 /* Check cases like x^y==z */
11542 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11543 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11544 "suggest parentheses around comparison in operand of %<^%>");
11545 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11546 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11547 "suggest parentheses around comparison in operand of %<^%>");
11548 return;
11550 case BIT_AND_EXPR:
11551 if (code_left == PLUS_EXPR)
11552 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11553 "suggest parentheses around %<+%> in operand of %<&%>");
11554 else if (code_right == PLUS_EXPR)
11555 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11556 "suggest parentheses around %<+%> in operand of %<&%>");
11557 else if (code_left == MINUS_EXPR)
11558 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11559 "suggest parentheses around %<-%> in operand of %<&%>");
11560 else if (code_right == MINUS_EXPR)
11561 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11562 "suggest parentheses around %<-%> in operand of %<&%>");
11563 /* Check cases like x&y==z */
11564 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11565 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11566 "suggest parentheses around comparison in operand of %<&%>");
11567 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11568 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11569 "suggest parentheses around comparison in operand of %<&%>");
11570 /* Check cases like !x & y */
11571 else if (code_left == TRUTH_NOT_EXPR
11572 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11573 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11574 "suggest parentheses around operand of "
11575 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11576 return;
11578 case EQ_EXPR:
11579 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11580 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11581 "suggest parentheses around comparison in operand of %<==%>");
11582 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11583 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11584 "suggest parentheses around comparison in operand of %<==%>");
11585 return;
11586 case NE_EXPR:
11587 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11588 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11589 "suggest parentheses around comparison in operand of %<!=%>");
11590 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11591 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11592 "suggest parentheses around comparison in operand of %<!=%>");
11593 return;
11595 default:
11596 if (TREE_CODE_CLASS (code) == tcc_comparison)
11598 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11599 && code_left != NE_EXPR && code_left != EQ_EXPR
11600 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11601 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11602 "comparisons like %<X<=Y<=Z%> do not "
11603 "have their mathematical meaning");
11604 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11605 && code_right != NE_EXPR && code_right != EQ_EXPR
11606 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11607 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11608 "comparisons like %<X<=Y<=Z%> do not "
11609 "have their mathematical meaning");
11611 return;
11613 #undef NOT_A_BOOLEAN_EXPR_P
11616 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11618 void
11619 warn_for_unused_label (tree label)
11621 if (!TREE_USED (label))
11623 if (DECL_INITIAL (label))
11624 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11625 else
11626 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11630 /* Warn for division by zero according to the value of DIVISOR. LOC
11631 is the location of the division operator. */
11633 void
11634 warn_for_div_by_zero (location_t loc, tree divisor)
11636 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11637 about division by zero. Do not issue a warning if DIVISOR has a
11638 floating-point type, since we consider 0.0/0.0 a valid way of
11639 generating a NaN. */
11640 if (c_inhibit_evaluation_warnings == 0
11641 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11642 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11645 /* Subroutine of build_binary_op. Give warnings for comparisons
11646 between signed and unsigned quantities that may fail. Do the
11647 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11648 so that casts will be considered, but default promotions won't
11651 LOCATION is the location of the comparison operator.
11653 The arguments of this function map directly to local variables
11654 of build_binary_op. */
11656 void
11657 warn_for_sign_compare (location_t location,
11658 tree orig_op0, tree orig_op1,
11659 tree op0, tree op1,
11660 tree result_type, enum tree_code resultcode)
11662 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11663 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11664 int unsignedp0, unsignedp1;
11666 /* In C++, check for comparison of different enum types. */
11667 if (c_dialect_cxx()
11668 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11669 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11670 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11671 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11673 warning_at (location,
11674 OPT_Wsign_compare, "comparison between types %qT and %qT",
11675 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11678 /* Do not warn if the comparison is being done in a signed type,
11679 since the signed type will only be chosen if it can represent
11680 all the values of the unsigned type. */
11681 if (!TYPE_UNSIGNED (result_type))
11682 /* OK */;
11683 /* Do not warn if both operands are unsigned. */
11684 else if (op0_signed == op1_signed)
11685 /* OK */;
11686 else
11688 tree sop, uop, base_type;
11689 bool ovf;
11691 if (op0_signed)
11692 sop = orig_op0, uop = orig_op1;
11693 else
11694 sop = orig_op1, uop = orig_op0;
11696 STRIP_TYPE_NOPS (sop);
11697 STRIP_TYPE_NOPS (uop);
11698 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11699 ? TREE_TYPE (result_type) : result_type);
11701 /* Do not warn if the signed quantity is an unsuffixed integer
11702 literal (or some static constant expression involving such
11703 literals or a conditional expression involving such literals)
11704 and it is non-negative. */
11705 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11706 /* OK */;
11707 /* Do not warn if the comparison is an equality operation, the
11708 unsigned quantity is an integral constant, and it would fit
11709 in the result if the result were signed. */
11710 else if (TREE_CODE (uop) == INTEGER_CST
11711 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11712 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11713 /* OK */;
11714 /* In C, do not warn if the unsigned quantity is an enumeration
11715 constant and its maximum value would fit in the result if the
11716 result were signed. */
11717 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11718 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11719 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11720 c_common_signed_type (base_type)))
11721 /* OK */;
11722 else
11723 warning_at (location,
11724 OPT_Wsign_compare,
11725 "comparison between signed and unsigned integer expressions");
11728 /* Warn if two unsigned values are being compared in a size larger
11729 than their original size, and one (and only one) is the result of
11730 a `~' operator. This comparison will always fail.
11732 Also warn if one operand is a constant, and the constant does not
11733 have all bits set that are set in the ~ operand when it is
11734 extended. */
11736 op0 = c_common_get_narrower (op0, &unsignedp0);
11737 op1 = c_common_get_narrower (op1, &unsignedp1);
11739 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11740 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11742 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11743 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11744 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11745 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11747 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11749 tree primop;
11750 HOST_WIDE_INT constant, mask;
11751 int unsignedp;
11752 unsigned int bits;
11754 if (tree_fits_shwi_p (op0))
11756 primop = op1;
11757 unsignedp = unsignedp1;
11758 constant = tree_to_shwi (op0);
11760 else
11762 primop = op0;
11763 unsignedp = unsignedp0;
11764 constant = tree_to_shwi (op1);
11767 bits = TYPE_PRECISION (TREE_TYPE (primop));
11768 if (bits < TYPE_PRECISION (result_type)
11769 && bits < HOST_BITS_PER_LONG && unsignedp)
11771 mask = (~ (HOST_WIDE_INT) 0) << bits;
11772 if ((mask & constant) != mask)
11774 if (constant == 0)
11775 warning_at (location, OPT_Wsign_compare,
11776 "promoted ~unsigned is always non-zero");
11777 else
11778 warning_at (location, OPT_Wsign_compare,
11779 "comparison of promoted ~unsigned with constant");
11783 else if (unsignedp0 && unsignedp1
11784 && (TYPE_PRECISION (TREE_TYPE (op0))
11785 < TYPE_PRECISION (result_type))
11786 && (TYPE_PRECISION (TREE_TYPE (op1))
11787 < TYPE_PRECISION (result_type)))
11788 warning_at (location, OPT_Wsign_compare,
11789 "comparison of promoted ~unsigned with unsigned");
11793 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11794 type via c_common_type. If -Wdouble-promotion is in use, and the
11795 conditions for warning have been met, issue a warning. GMSGID is
11796 the warning message. It must have two %T specifiers for the type
11797 that was converted (generally "float") and the type to which it was
11798 converted (generally "double), respectively. LOC is the location
11799 to which the awrning should refer. */
11801 void
11802 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11803 const char *gmsgid, location_t loc)
11805 tree source_type;
11807 if (!warn_double_promotion)
11808 return;
11809 /* If the conversion will not occur at run-time, there is no need to
11810 warn about it. */
11811 if (c_inhibit_evaluation_warnings)
11812 return;
11813 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11814 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11815 return;
11816 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11817 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11818 source_type = type1;
11819 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11820 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11821 source_type = type2;
11822 else
11823 return;
11824 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11827 /* Setup a TYPE_DECL node as a typedef representation.
11829 X is a TYPE_DECL for a typedef statement. Create a brand new
11830 ..._TYPE node (which will be just a variant of the existing
11831 ..._TYPE node with identical properties) and then install X
11832 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11834 The whole point here is to end up with a situation where each
11835 and every ..._TYPE node the compiler creates will be uniquely
11836 associated with AT MOST one node representing a typedef name.
11837 This way, even though the compiler substitutes corresponding
11838 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11839 early on, later parts of the compiler can always do the reverse
11840 translation and get back the corresponding typedef name. For
11841 example, given:
11843 typedef struct S MY_TYPE;
11844 MY_TYPE object;
11846 Later parts of the compiler might only know that `object' was of
11847 type `struct S' if it were not for code just below. With this
11848 code however, later parts of the compiler see something like:
11850 struct S' == struct S
11851 typedef struct S' MY_TYPE;
11852 struct S' object;
11854 And they can then deduce (from the node for type struct S') that
11855 the original object declaration was:
11857 MY_TYPE object;
11859 Being able to do this is important for proper support of protoize,
11860 and also for generating precise symbolic debugging information
11861 which takes full account of the programmer's (typedef) vocabulary.
11863 Obviously, we don't want to generate a duplicate ..._TYPE node if
11864 the TYPE_DECL node that we are now processing really represents a
11865 standard built-in type. */
11867 void
11868 set_underlying_type (tree x)
11870 if (x == error_mark_node)
11871 return;
11872 if (DECL_IS_BUILTIN (x))
11874 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11875 TYPE_NAME (TREE_TYPE (x)) = x;
11877 else if (TREE_TYPE (x) != error_mark_node
11878 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11880 tree tt = TREE_TYPE (x);
11881 DECL_ORIGINAL_TYPE (x) = tt;
11882 tt = build_variant_type_copy (tt);
11883 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11884 TYPE_NAME (tt) = x;
11885 TREE_USED (tt) = TREE_USED (x);
11886 TREE_TYPE (x) = tt;
11890 /* Record the types used by the current global variable declaration
11891 being parsed, so that we can decide later to emit their debug info.
11892 Those types are in types_used_by_cur_var_decl, and we are going to
11893 store them in the types_used_by_vars_hash hash table.
11894 DECL is the declaration of the global variable that has been parsed. */
11896 void
11897 record_types_used_by_current_var_decl (tree decl)
11899 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11901 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11903 tree type = types_used_by_cur_var_decl->pop ();
11904 types_used_by_var_decl_insert (type, decl);
11908 /* If DECL is a typedef that is declared in the current function,
11909 record it for the purpose of -Wunused-local-typedefs. */
11911 void
11912 record_locally_defined_typedef (tree decl)
11914 struct c_language_function *l;
11916 if (!warn_unused_local_typedefs
11917 || cfun == NULL
11918 /* if this is not a locally defined typedef then we are not
11919 interested. */
11920 || !is_typedef_decl (decl)
11921 || !decl_function_context (decl))
11922 return;
11924 l = (struct c_language_function *) cfun->language;
11925 vec_safe_push (l->local_typedefs, decl);
11928 /* If T is a TYPE_DECL declared locally, mark it as used. */
11930 void
11931 maybe_record_typedef_use (tree t)
11933 if (!is_typedef_decl (t))
11934 return;
11936 TREE_USED (t) = true;
11939 /* Warn if there are some unused locally defined typedefs in the
11940 current function. */
11942 void
11943 maybe_warn_unused_local_typedefs (void)
11945 int i;
11946 tree decl;
11947 /* The number of times we have emitted -Wunused-local-typedefs
11948 warnings. If this is different from errorcount, that means some
11949 unrelated errors have been issued. In which case, we'll avoid
11950 emitting "unused-local-typedefs" warnings. */
11951 static int unused_local_typedefs_warn_count;
11952 struct c_language_function *l;
11954 if (cfun == NULL)
11955 return;
11957 if ((l = (struct c_language_function *) cfun->language) == NULL)
11958 return;
11960 if (warn_unused_local_typedefs
11961 && errorcount == unused_local_typedefs_warn_count)
11963 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11964 if (!TREE_USED (decl))
11965 warning_at (DECL_SOURCE_LOCATION (decl),
11966 OPT_Wunused_local_typedefs,
11967 "typedef %qD locally defined but not used", decl);
11968 unused_local_typedefs_warn_count = errorcount;
11971 vec_free (l->local_typedefs);
11974 /* Warn about boolean expression compared with an integer value different
11975 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11976 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11977 are the operands of the comparison. The caller must ensure that
11978 either operand is a boolean expression. */
11980 void
11981 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11982 tree op1)
11984 if (TREE_CODE_CLASS (code) != tcc_comparison)
11985 return;
11987 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11988 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11989 if (!cst)
11990 return;
11992 if (!integer_zerop (cst) && !integer_onep (cst))
11994 int sign = (TREE_CODE (op0) == INTEGER_CST)
11995 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
11996 if (code == EQ_EXPR
11997 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11998 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11999 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12000 "with boolean expression is always false", cst);
12001 else
12002 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12003 "with boolean expression is always true", cst);
12007 /* The C and C++ parsers both use vectors to hold function arguments.
12008 For efficiency, we keep a cache of unused vectors. This is the
12009 cache. */
12011 typedef vec<tree, va_gc> *tree_gc_vec;
12012 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12014 /* Return a new vector from the cache. If the cache is empty,
12015 allocate a new vector. These vectors are GC'ed, so it is OK if the
12016 pointer is not released.. */
12018 vec<tree, va_gc> *
12019 make_tree_vector (void)
12021 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12022 return tree_vector_cache->pop ();
12023 else
12025 /* Passing 0 to vec::alloc returns NULL, and our callers require
12026 that we always return a non-NULL value. The vector code uses
12027 4 when growing a NULL vector, so we do too. */
12028 vec<tree, va_gc> *v;
12029 vec_alloc (v, 4);
12030 return v;
12034 /* Release a vector of trees back to the cache. */
12036 void
12037 release_tree_vector (vec<tree, va_gc> *vec)
12039 if (vec != NULL)
12041 vec->truncate (0);
12042 vec_safe_push (tree_vector_cache, vec);
12046 /* Get a new tree vector holding a single tree. */
12048 vec<tree, va_gc> *
12049 make_tree_vector_single (tree t)
12051 vec<tree, va_gc> *ret = make_tree_vector ();
12052 ret->quick_push (t);
12053 return ret;
12056 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12058 vec<tree, va_gc> *
12059 make_tree_vector_from_list (tree list)
12061 vec<tree, va_gc> *ret = make_tree_vector ();
12062 for (; list; list = TREE_CHAIN (list))
12063 vec_safe_push (ret, TREE_VALUE (list));
12064 return ret;
12067 /* Get a new tree vector which is a copy of an existing one. */
12069 vec<tree, va_gc> *
12070 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12072 vec<tree, va_gc> *ret;
12073 unsigned int ix;
12074 tree t;
12076 ret = make_tree_vector ();
12077 vec_safe_reserve (ret, vec_safe_length (orig));
12078 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12079 ret->quick_push (t);
12080 return ret;
12083 /* Return true if KEYWORD starts a type specifier. */
12085 bool
12086 keyword_begins_type_specifier (enum rid keyword)
12088 switch (keyword)
12090 case RID_AUTO_TYPE:
12091 case RID_INT:
12092 case RID_CHAR:
12093 case RID_FLOAT:
12094 case RID_DOUBLE:
12095 case RID_VOID:
12096 case RID_UNSIGNED:
12097 case RID_LONG:
12098 case RID_SHORT:
12099 case RID_SIGNED:
12100 case RID_DFLOAT32:
12101 case RID_DFLOAT64:
12102 case RID_DFLOAT128:
12103 case RID_FRACT:
12104 case RID_ACCUM:
12105 case RID_BOOL:
12106 case RID_WCHAR:
12107 case RID_CHAR16:
12108 case RID_CHAR32:
12109 case RID_SAT:
12110 case RID_COMPLEX:
12111 case RID_TYPEOF:
12112 case RID_STRUCT:
12113 case RID_CLASS:
12114 case RID_UNION:
12115 case RID_ENUM:
12116 return true;
12117 default:
12118 if (keyword >= RID_FIRST_INT_N
12119 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12120 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12121 return true;
12122 return false;
12126 /* Return true if KEYWORD names a type qualifier. */
12128 bool
12129 keyword_is_type_qualifier (enum rid keyword)
12131 switch (keyword)
12133 case RID_CONST:
12134 case RID_VOLATILE:
12135 case RID_RESTRICT:
12136 case RID_ATOMIC:
12137 return true;
12138 default:
12139 return false;
12143 /* Return true if KEYWORD names a storage class specifier.
12145 RID_TYPEDEF is not included in this list despite `typedef' being
12146 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12147 such for syntactic convenience only. */
12149 bool
12150 keyword_is_storage_class_specifier (enum rid keyword)
12152 switch (keyword)
12154 case RID_STATIC:
12155 case RID_EXTERN:
12156 case RID_REGISTER:
12157 case RID_AUTO:
12158 case RID_MUTABLE:
12159 case RID_THREAD:
12160 return true;
12161 default:
12162 return false;
12166 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12168 static bool
12169 keyword_is_function_specifier (enum rid keyword)
12171 switch (keyword)
12173 case RID_INLINE:
12174 case RID_NORETURN:
12175 case RID_VIRTUAL:
12176 case RID_EXPLICIT:
12177 return true;
12178 default:
12179 return false;
12183 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12184 declaration-specifier (C99 6.7). */
12186 bool
12187 keyword_is_decl_specifier (enum rid keyword)
12189 if (keyword_is_storage_class_specifier (keyword)
12190 || keyword_is_type_qualifier (keyword)
12191 || keyword_is_function_specifier (keyword))
12192 return true;
12194 switch (keyword)
12196 case RID_TYPEDEF:
12197 case RID_FRIEND:
12198 case RID_CONSTEXPR:
12199 return true;
12200 default:
12201 return false;
12205 /* Initialize language-specific-bits of tree_contains_struct. */
12207 void
12208 c_common_init_ts (void)
12210 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12211 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12212 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12213 MARK_TS_COMMON (UPC_FORALL_STMT);
12214 MARK_TS_COMMON (UPC_SYNC_STMT);
12217 /* Build a user-defined numeric literal out of an integer constant type VALUE
12218 with identifier SUFFIX. */
12220 tree
12221 build_userdef_literal (tree suffix_id, tree value,
12222 enum overflow_type overflow, tree num_string)
12224 tree literal = make_node (USERDEF_LITERAL);
12225 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12226 USERDEF_LITERAL_VALUE (literal) = value;
12227 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12228 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12229 return literal;
12232 /* For vector[index], convert the vector to a
12233 pointer of the underlying type. Return true if the resulting
12234 ARRAY_REF should not be an lvalue. */
12236 bool
12237 convert_vector_to_pointer_for_subscript (location_t loc,
12238 tree *vecp, tree index)
12240 bool ret = false;
12241 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12243 tree type = TREE_TYPE (*vecp);
12244 tree type1;
12246 ret = !lvalue_p (*vecp);
12247 if (TREE_CODE (index) == INTEGER_CST)
12248 if (!tree_fits_uhwi_p (index)
12249 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12250 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12252 if (ret)
12254 tree tmp = create_tmp_var_raw (type);
12255 DECL_SOURCE_LOCATION (tmp) = loc;
12256 *vecp = c_save_expr (*vecp);
12257 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12259 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12260 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12261 *vecp
12262 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12263 *vecp, NULL_TREE, NULL_TREE),
12264 non_const);
12266 else
12267 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12268 NULL_TREE, NULL_TREE);
12269 SET_EXPR_LOCATION (*vecp, loc);
12270 c_common_mark_addressable_vec (tmp);
12272 else
12273 c_common_mark_addressable_vec (*vecp);
12274 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12275 type1 = build_pointer_type (TREE_TYPE (*vecp));
12276 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12277 if (!ref_all
12278 && !DECL_P (*vecp))
12280 /* If the original vector isn't declared may_alias and it
12281 isn't a bare vector look if the subscripting would
12282 alias the vector we subscript, and if not, force ref-all. */
12283 alias_set_type vecset = get_alias_set (*vecp);
12284 alias_set_type sset = get_alias_set (type);
12285 if (!alias_sets_must_conflict_p (sset, vecset)
12286 && !alias_set_subset_of (sset, vecset))
12287 ref_all = true;
12289 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12290 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12291 *vecp = convert (type, *vecp);
12293 return ret;
12296 /* Determine which of the operands, if any, is a scalar that needs to be
12297 converted to a vector, for the range of operations. */
12298 enum stv_conv
12299 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12300 bool complain)
12302 tree type0 = TREE_TYPE (op0);
12303 tree type1 = TREE_TYPE (op1);
12304 bool integer_only_op = false;
12305 enum stv_conv ret = stv_firstarg;
12307 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12308 || TREE_CODE (type1) == VECTOR_TYPE);
12309 switch (code)
12311 /* Most GENERIC binary expressions require homogeneous arguments.
12312 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12313 argument that is a vector and a second one that is a scalar, so
12314 we never return stv_secondarg for them. */
12315 case RSHIFT_EXPR:
12316 case LSHIFT_EXPR:
12317 if (TREE_CODE (type0) == INTEGER_TYPE
12318 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12320 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12322 if (complain)
12323 error_at (loc, "conversion of scalar %qT to vector %qT "
12324 "involves truncation", type0, type1);
12325 return stv_error;
12327 else
12328 return stv_firstarg;
12330 break;
12332 case BIT_IOR_EXPR:
12333 case BIT_XOR_EXPR:
12334 case BIT_AND_EXPR:
12335 integer_only_op = true;
12336 /* ... fall through ... */
12338 case VEC_COND_EXPR:
12340 case PLUS_EXPR:
12341 case MINUS_EXPR:
12342 case MULT_EXPR:
12343 case TRUNC_DIV_EXPR:
12344 case CEIL_DIV_EXPR:
12345 case FLOOR_DIV_EXPR:
12346 case ROUND_DIV_EXPR:
12347 case EXACT_DIV_EXPR:
12348 case TRUNC_MOD_EXPR:
12349 case FLOOR_MOD_EXPR:
12350 case RDIV_EXPR:
12351 case EQ_EXPR:
12352 case NE_EXPR:
12353 case LE_EXPR:
12354 case GE_EXPR:
12355 case LT_EXPR:
12356 case GT_EXPR:
12357 /* What about UNLT_EXPR? */
12358 if (TREE_CODE (type0) == VECTOR_TYPE)
12360 tree tmp;
12361 ret = stv_secondarg;
12362 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12363 tmp = type0; type0 = type1; type1 = tmp;
12364 tmp = op0; op0 = op1; op1 = tmp;
12367 if (TREE_CODE (type0) == INTEGER_TYPE
12368 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12370 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12372 if (complain)
12373 error_at (loc, "conversion of scalar %qT to vector %qT "
12374 "involves truncation", type0, type1);
12375 return stv_error;
12377 return ret;
12379 else if (!integer_only_op
12380 /* Allow integer --> real conversion if safe. */
12381 && (TREE_CODE (type0) == REAL_TYPE
12382 || TREE_CODE (type0) == INTEGER_TYPE)
12383 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12385 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12387 if (complain)
12388 error_at (loc, "conversion of scalar %qT to vector %qT "
12389 "involves truncation", type0, type1);
12390 return stv_error;
12392 return ret;
12394 default:
12395 break;
12398 return stv_nothing;
12401 /* Return true iff ALIGN is an integral constant that is a fundamental
12402 alignment, as defined by [basic.align] in the c++-11
12403 specifications.
12405 That is:
12407 [A fundamental alignment is represented by an alignment less than or
12408 equal to the greatest alignment supported by the implementation
12409 in all contexts, which is equal to
12410 alignof(max_align_t)]. */
12412 bool
12413 cxx_fundamental_alignment_p (unsigned align)
12415 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12416 TYPE_ALIGN (long_double_type_node)));
12419 /* Return true if T is a pointer to a zero-sized aggregate. */
12421 bool
12422 pointer_to_zero_sized_aggr_p (tree t)
12424 if (!POINTER_TYPE_P (t))
12425 return false;
12426 t = TREE_TYPE (t);
12427 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12430 #include "gt-c-family-c-common.h"