gcc:
[official-gcc.git] / gcc / c-family / c-common.c
blobf89deb32889f649a9554a8676fe6c2599e59cbf0
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 "target.h"
26 #include "function.h"
27 #include "tree.h"
28 #include "c-common.h"
29 #include "gimple-expr.h"
30 #include "tm_p.h"
31 #include "stringpool.h"
32 #include "cgraph.h"
33 #include "diagnostic.h"
34 #include "intl.h"
35 #include "stor-layout.h"
36 #include "calls.h"
37 #include "attribs.h"
38 #include "varasm.h"
39 #include "trans-mem.h"
40 #include "c-objc.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "tree-iterator.h"
46 #include "opts.h"
47 #include "gimplify.h"
49 cpp_reader *parse_in; /* Declared in c-pragma.h. */
51 /* Mode used to build pointers (VOIDmode means ptr_mode). */
53 machine_mode c_default_pointer_mode = VOIDmode;
55 /* The following symbols are subsumed in the c_global_trees array, and
56 listed here individually for documentation purposes.
58 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
60 tree short_integer_type_node;
61 tree long_integer_type_node;
62 tree long_long_integer_type_node;
64 tree short_unsigned_type_node;
65 tree long_unsigned_type_node;
66 tree long_long_unsigned_type_node;
68 tree truthvalue_type_node;
69 tree truthvalue_false_node;
70 tree truthvalue_true_node;
72 tree ptrdiff_type_node;
74 tree unsigned_char_type_node;
75 tree signed_char_type_node;
76 tree wchar_type_node;
78 tree char16_type_node;
79 tree char32_type_node;
81 tree float_type_node;
82 tree double_type_node;
83 tree long_double_type_node;
85 tree complex_integer_type_node;
86 tree complex_float_type_node;
87 tree complex_double_type_node;
88 tree complex_long_double_type_node;
90 tree dfloat32_type_node;
91 tree dfloat64_type_node;
92 tree_dfloat128_type_node;
94 tree intQI_type_node;
95 tree intHI_type_node;
96 tree intSI_type_node;
97 tree intDI_type_node;
98 tree intTI_type_node;
100 tree unsigned_intQI_type_node;
101 tree unsigned_intHI_type_node;
102 tree unsigned_intSI_type_node;
103 tree unsigned_intDI_type_node;
104 tree unsigned_intTI_type_node;
106 tree widest_integer_literal_type_node;
107 tree widest_unsigned_literal_type_node;
109 Nodes for types `void *' and `const void *'.
111 tree ptr_type_node, const_ptr_type_node;
113 Nodes for types `char *' and `const char *'.
115 tree string_type_node, const_string_type_node;
117 Type `char[SOMENUMBER]'.
118 Used when an array of char is needed and the size is irrelevant.
120 tree char_array_type_node;
122 Type `wchar_t[SOMENUMBER]' or something like it.
123 Used when a wide string literal is created.
125 tree wchar_array_type_node;
127 Type `char16_t[SOMENUMBER]' or something like it.
128 Used when a UTF-16 string literal is created.
130 tree char16_array_type_node;
132 Type `char32_t[SOMENUMBER]' or something like it.
133 Used when a UTF-32 string literal is created.
135 tree char32_array_type_node;
137 Type `int ()' -- used for implicit declaration of functions.
139 tree default_function_type;
141 A VOID_TYPE node, packaged in a TREE_LIST.
143 tree void_list_node;
145 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
146 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
147 VAR_DECLS, but C++ does.)
149 tree function_name_decl_node;
150 tree pretty_function_name_decl_node;
151 tree c99_function_name_decl_node;
153 Stack of nested function name VAR_DECLs.
155 tree saved_function_name_decls;
159 tree c_global_trees[CTI_MAX];
161 /* Switches common to the C front ends. */
163 /* Nonzero means don't output line number information. */
165 char flag_no_line_commands;
167 /* Nonzero causes -E output not to be done, but directives such as
168 #define that have side effects are still obeyed. */
170 char flag_no_output;
172 /* Nonzero means dump macros in some fashion. */
174 char flag_dump_macros;
176 /* Nonzero means pass #include lines through to the output. */
178 char flag_dump_includes;
180 /* Nonzero means process PCH files while preprocessing. */
182 bool flag_pch_preprocess;
184 /* The file name to which we should write a precompiled header, or
185 NULL if no header will be written in this compile. */
187 const char *pch_file;
189 /* Nonzero if an ISO standard was selected. It rejects macros in the
190 user's namespace. */
191 int flag_iso;
193 /* C/ObjC language option variables. */
196 /* Nonzero means allow type mismatches in conditional expressions;
197 just make their values `void'. */
199 int flag_cond_mismatch;
201 /* Nonzero means enable C89 Amendment 1 features. */
203 int flag_isoc94;
205 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
207 int flag_isoc99;
209 /* Nonzero means use the ISO C11 dialect of C. */
211 int flag_isoc11;
213 /* Nonzero means that we have builtin functions, and main is an int. */
215 int flag_hosted = 1;
218 /* ObjC language option variables. */
221 /* Tells the compiler that this is a special run. Do not perform any
222 compiling, instead we are to test some platform dependent features
223 and output a C header file with appropriate definitions. */
225 int print_struct_values;
227 /* Tells the compiler what is the constant string class for ObjC. */
229 const char *constant_string_class_name;
232 /* C++ language option variables. */
234 /* The reference version of the ABI for -Wabi. */
236 int warn_abi_version = -1;
238 /* Nonzero means generate separate instantiation control files and
239 juggle them at link time. */
241 int flag_use_repository;
243 /* The C++ dialect being used. Default set in c_common_post_options. */
245 enum cxx_dialect cxx_dialect = cxx_unset;
247 /* Maximum template instantiation depth. This limit exists to limit the
248 time it takes to notice excessively recursive template instantiations.
250 The default is lower than the 1024 recommended by the C++0x standard
251 because G++ runs out of stack before 1024 with highly recursive template
252 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
254 int max_tinst_depth = 900;
256 /* The elements of `ridpointers' are identifier nodes for the reserved
257 type names and storage classes. It is indexed by a RID_... value. */
258 tree *ridpointers;
260 tree (*make_fname_decl) (location_t, tree, int);
262 /* Nonzero means don't warn about problems that occur when the code is
263 executed. */
264 int c_inhibit_evaluation_warnings;
266 /* Whether we are building a boolean conversion inside
267 convert_for_assignment, or some other late binary operation. If
268 build_binary_op is called for C (from code shared by C and C++) in
269 this case, then the operands have already been folded and the
270 result will not be folded again, so C_MAYBE_CONST_EXPR should not
271 be generated. */
272 bool in_late_binary_op;
274 /* Whether lexing has been completed, so subsequent preprocessor
275 errors should use the compiler's input_location. */
276 bool done_lexing = false;
278 /* Information about how a function name is generated. */
279 struct fname_var_t
281 tree *const decl; /* pointer to the VAR_DECL. */
282 const unsigned rid; /* RID number for the identifier. */
283 const int pretty; /* How pretty is it? */
286 /* The three ways of getting then name of the current function. */
288 const struct fname_var_t fname_vars[] =
290 /* C99 compliant __func__, must be first. */
291 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
292 /* GCC __FUNCTION__ compliant. */
293 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
294 /* GCC __PRETTY_FUNCTION__ compliant. */
295 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
296 {NULL, 0, 0},
299 /* Global visibility options. */
300 struct visibility_flags visibility_options;
302 static tree check_case_value (location_t, tree);
303 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
304 bool *);
306 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
312 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
313 int, bool *);
314 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
315 int, bool *);
316 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
317 bool *);
318 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_always_inline_attribute (tree *, tree, tree, int,
324 bool *);
325 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
332 bool *);
333 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
334 bool *);
335 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_transparent_union_attribute (tree *, tree, tree,
337 int, bool *);
338 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
339 int, bool *);
340 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
346 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
347 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
348 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
351 static tree handle_visibility_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_tls_model_attribute (tree *, tree, tree, int,
354 bool *);
355 static tree handle_no_instrument_function_attribute (tree *, tree,
356 tree, int, bool *);
357 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
360 bool *);
361 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_deprecated_attribute (tree *, tree, tree, int,
366 bool *);
367 static tree handle_vector_size_attribute (tree *, tree, tree, int,
368 bool *);
369 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
373 bool *);
374 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
382 static tree ignore_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
388 bool *);
389 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
391 bool *);
392 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
394 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
395 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
397 static void check_function_nonnull (tree, int, tree *);
398 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
399 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
400 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
401 static int resort_field_decl_cmp (const void *, const void *);
403 /* Reserved words. The third field is a mask: keywords are disabled
404 if they match the mask.
406 Masks for languages:
407 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
408 C --std=c99: D_CXXONLY | D_OBJC
409 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
410 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
411 C++ --std=c0x: D_CONLY | D_OBJC
412 ObjC++ is like C++ except that D_OBJC is not set
414 If -fno-asm is used, D_ASM is added to the mask. If
415 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
416 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
417 In C with -Wc++-compat, we warn if D_CXXWARN is set.
419 Note the complication of the D_CXX_OBJC keywords. These are
420 reserved words such as 'class'. In C++, 'class' is a reserved
421 word. In Objective-C++ it is too. In Objective-C, it is a
422 reserved word too, but only if it follows an '@' sign.
424 const struct c_common_resword c_common_reswords[] =
426 { "_Alignas", RID_ALIGNAS, D_CONLY },
427 { "_Alignof", RID_ALIGNOF, D_CONLY },
428 { "_Atomic", RID_ATOMIC, D_CONLY },
429 { "_Bool", RID_BOOL, D_CONLY },
430 { "_Complex", RID_COMPLEX, 0 },
431 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
432 { "_Cilk_sync", RID_CILK_SYNC, 0 },
433 { "_Cilk_for", RID_CILK_FOR, 0 },
434 { "_Imaginary", RID_IMAGINARY, D_CONLY },
435 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
436 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
437 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
438 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
439 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
440 { "_Sat", RID_SAT, D_CONLY | D_EXT },
441 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
442 { "_Noreturn", RID_NORETURN, D_CONLY },
443 { "_Generic", RID_GENERIC, D_CONLY },
444 { "_Thread_local", RID_THREAD, D_CONLY },
445 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
446 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
447 { "__alignof", RID_ALIGNOF, 0 },
448 { "__alignof__", RID_ALIGNOF, 0 },
449 { "__asm", RID_ASM, 0 },
450 { "__asm__", RID_ASM, 0 },
451 { "__attribute", RID_ATTRIBUTE, 0 },
452 { "__attribute__", RID_ATTRIBUTE, 0 },
453 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
454 { "__bases", RID_BASES, D_CXXONLY },
455 { "__builtin_call_with_static_chain",
456 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
457 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
458 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
459 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
460 { "__builtin_offsetof", RID_OFFSETOF, 0 },
461 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
462 { "__builtin_va_arg", RID_VA_ARG, 0 },
463 { "__complex", RID_COMPLEX, 0 },
464 { "__complex__", RID_COMPLEX, 0 },
465 { "__const", RID_CONST, 0 },
466 { "__const__", RID_CONST, 0 },
467 { "__decltype", RID_DECLTYPE, D_CXXONLY },
468 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
469 { "__extension__", RID_EXTENSION, 0 },
470 { "__func__", RID_C99_FUNCTION_NAME, 0 },
471 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
472 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
473 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
474 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
475 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
476 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
477 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
478 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
479 { "__imag", RID_IMAGPART, 0 },
480 { "__imag__", RID_IMAGPART, 0 },
481 { "__inline", RID_INLINE, 0 },
482 { "__inline__", RID_INLINE, 0 },
483 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
484 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
485 { "__is_class", RID_IS_CLASS, D_CXXONLY },
486 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
487 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
488 { "__is_final", RID_IS_FINAL, D_CXXONLY },
489 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
490 { "__is_pod", RID_IS_POD, D_CXXONLY },
491 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
492 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
493 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
494 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
495 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
496 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
497 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
498 { "__is_union", RID_IS_UNION, D_CXXONLY },
499 { "__label__", RID_LABEL, 0 },
500 { "__null", RID_NULL, 0 },
501 { "__real", RID_REALPART, 0 },
502 { "__real__", RID_REALPART, 0 },
503 { "__restrict", RID_RESTRICT, 0 },
504 { "__restrict__", RID_RESTRICT, 0 },
505 { "__signed", RID_SIGNED, 0 },
506 { "__signed__", RID_SIGNED, 0 },
507 { "__thread", RID_THREAD, 0 },
508 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
509 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
510 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
511 { "__typeof", RID_TYPEOF, 0 },
512 { "__typeof__", RID_TYPEOF, 0 },
513 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
514 { "__volatile", RID_VOLATILE, 0 },
515 { "__volatile__", RID_VOLATILE, 0 },
516 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
517 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
518 { "asm", RID_ASM, D_ASM },
519 { "auto", RID_AUTO, 0 },
520 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
521 { "break", RID_BREAK, 0 },
522 { "case", RID_CASE, 0 },
523 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
524 { "char", RID_CHAR, 0 },
525 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
526 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
527 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
528 { "const", RID_CONST, 0 },
529 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
530 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
531 { "continue", RID_CONTINUE, 0 },
532 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
533 { "default", RID_DEFAULT, 0 },
534 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
535 { "do", RID_DO, 0 },
536 { "double", RID_DOUBLE, 0 },
537 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
538 { "else", RID_ELSE, 0 },
539 { "enum", RID_ENUM, 0 },
540 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
541 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
542 { "extern", RID_EXTERN, 0 },
543 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
544 { "float", RID_FLOAT, 0 },
545 { "for", RID_FOR, 0 },
546 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
547 { "goto", RID_GOTO, 0 },
548 { "if", RID_IF, 0 },
549 { "inline", RID_INLINE, D_EXT89 },
550 { "int", RID_INT, 0 },
551 { "long", RID_LONG, 0 },
552 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
553 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
554 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
555 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
556 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
557 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
558 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
559 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
560 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
561 { "register", RID_REGISTER, 0 },
562 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
563 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
564 { "return", RID_RETURN, 0 },
565 { "short", RID_SHORT, 0 },
566 { "signed", RID_SIGNED, 0 },
567 { "sizeof", RID_SIZEOF, 0 },
568 { "static", RID_STATIC, 0 },
569 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
570 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
571 { "struct", RID_STRUCT, 0 },
572 { "switch", RID_SWITCH, 0 },
573 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
574 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
575 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
576 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
577 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
578 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
579 { "typedef", RID_TYPEDEF, 0 },
580 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
581 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
582 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
583 { "union", RID_UNION, 0 },
584 { "unsigned", RID_UNSIGNED, 0 },
585 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
586 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
587 { "void", RID_VOID, 0 },
588 { "volatile", RID_VOLATILE, 0 },
589 { "wchar_t", RID_WCHAR, D_CXXONLY },
590 { "while", RID_WHILE, 0 },
592 /* C++ transactional memory. */
593 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
594 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
595 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
596 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
598 /* Concepts-related keywords */
599 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
600 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
602 /* These Objective-C keywords are recognized only immediately after
603 an '@'. */
604 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
605 { "defs", RID_AT_DEFS, D_OBJC },
606 { "encode", RID_AT_ENCODE, D_OBJC },
607 { "end", RID_AT_END, D_OBJC },
608 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
609 { "interface", RID_AT_INTERFACE, D_OBJC },
610 { "protocol", RID_AT_PROTOCOL, D_OBJC },
611 { "selector", RID_AT_SELECTOR, D_OBJC },
612 { "finally", RID_AT_FINALLY, D_OBJC },
613 { "optional", RID_AT_OPTIONAL, D_OBJC },
614 { "required", RID_AT_REQUIRED, D_OBJC },
615 { "property", RID_AT_PROPERTY, D_OBJC },
616 { "package", RID_AT_PACKAGE, D_OBJC },
617 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
618 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
619 /* These are recognized only in protocol-qualifier context
620 (see above) */
621 { "bycopy", RID_BYCOPY, D_OBJC },
622 { "byref", RID_BYREF, D_OBJC },
623 { "in", RID_IN, D_OBJC },
624 { "inout", RID_INOUT, D_OBJC },
625 { "oneway", RID_ONEWAY, D_OBJC },
626 { "out", RID_OUT, D_OBJC },
627 /* These are recognized inside a property attribute list */
628 { "assign", RID_ASSIGN, D_OBJC },
629 { "copy", RID_COPY, D_OBJC },
630 { "getter", RID_GETTER, D_OBJC },
631 { "nonatomic", RID_NONATOMIC, D_OBJC },
632 { "readonly", RID_READONLY, D_OBJC },
633 { "readwrite", RID_READWRITE, D_OBJC },
634 { "retain", RID_RETAIN, D_OBJC },
635 { "setter", RID_SETTER, D_OBJC },
638 const unsigned int num_c_common_reswords =
639 sizeof c_common_reswords / sizeof (struct c_common_resword);
641 /* Table of machine-independent attributes common to all C-like languages.
643 All attributes referencing arguments should be additionally processed
644 in chkp_copy_function_type_adding_bounds for correct instrumentation
645 by Pointer Bounds Checker.
646 Current list of processed common attributes: nonnull. */
647 const struct attribute_spec c_common_attribute_table[] =
649 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
650 affects_type_identity } */
651 { "packed", 0, 0, false, false, false,
652 handle_packed_attribute , false},
653 { "nocommon", 0, 0, true, false, false,
654 handle_nocommon_attribute, false},
655 { "common", 0, 0, true, false, false,
656 handle_common_attribute, false },
657 /* FIXME: logically, noreturn attributes should be listed as
658 "false, true, true" and apply to function types. But implementing this
659 would require all the places in the compiler that use TREE_THIS_VOLATILE
660 on a decl to identify non-returning functions to be located and fixed
661 to check the function type instead. */
662 { "noreturn", 0, 0, true, false, false,
663 handle_noreturn_attribute, false },
664 { "volatile", 0, 0, true, false, false,
665 handle_noreturn_attribute, false },
666 { "stack_protect", 0, 0, true, false, false,
667 handle_stack_protect_attribute, false },
668 { "noinline", 0, 0, true, false, false,
669 handle_noinline_attribute, false },
670 { "noclone", 0, 0, true, false, false,
671 handle_noclone_attribute, false },
672 { "no_icf", 0, 0, true, false, false,
673 handle_noicf_attribute, false },
674 { "leaf", 0, 0, true, false, false,
675 handle_leaf_attribute, false },
676 { "always_inline", 0, 0, true, false, false,
677 handle_always_inline_attribute, false },
678 { "gnu_inline", 0, 0, true, false, false,
679 handle_gnu_inline_attribute, false },
680 { "artificial", 0, 0, true, false, false,
681 handle_artificial_attribute, false },
682 { "flatten", 0, 0, true, false, false,
683 handle_flatten_attribute, false },
684 { "used", 0, 0, true, false, false,
685 handle_used_attribute, false },
686 { "unused", 0, 0, false, false, false,
687 handle_unused_attribute, false },
688 { "externally_visible", 0, 0, true, false, false,
689 handle_externally_visible_attribute, false },
690 { "no_reorder", 0, 0, true, false, false,
691 handle_no_reorder_attribute, false },
692 /* The same comments as for noreturn attributes apply to const ones. */
693 { "const", 0, 0, true, false, false,
694 handle_const_attribute, false },
695 { "scalar_storage_order", 1, 1, false, false, false,
696 handle_scalar_storage_order_attribute, false },
697 { "transparent_union", 0, 0, false, false, false,
698 handle_transparent_union_attribute, false },
699 { "constructor", 0, 1, true, false, false,
700 handle_constructor_attribute, false },
701 { "destructor", 0, 1, true, false, false,
702 handle_destructor_attribute, false },
703 { "mode", 1, 1, false, true, false,
704 handle_mode_attribute, false },
705 { "section", 1, 1, true, false, false,
706 handle_section_attribute, false },
707 { "aligned", 0, 1, false, false, false,
708 handle_aligned_attribute, false },
709 { "weak", 0, 0, true, false, false,
710 handle_weak_attribute, false },
711 { "noplt", 0, 0, true, false, false,
712 handle_noplt_attribute, false },
713 { "ifunc", 1, 1, true, false, false,
714 handle_ifunc_attribute, false },
715 { "alias", 1, 1, true, false, false,
716 handle_alias_attribute, false },
717 { "weakref", 0, 1, true, false, false,
718 handle_weakref_attribute, false },
719 { "no_instrument_function", 0, 0, true, false, false,
720 handle_no_instrument_function_attribute,
721 false },
722 { "malloc", 0, 0, true, false, false,
723 handle_malloc_attribute, false },
724 { "returns_twice", 0, 0, true, false, false,
725 handle_returns_twice_attribute, false },
726 { "no_stack_limit", 0, 0, true, false, false,
727 handle_no_limit_stack_attribute, false },
728 { "pure", 0, 0, true, false, false,
729 handle_pure_attribute, false },
730 { "transaction_callable", 0, 0, false, true, false,
731 handle_tm_attribute, false },
732 { "transaction_unsafe", 0, 0, false, true, false,
733 handle_tm_attribute, true },
734 { "transaction_safe", 0, 0, false, true, false,
735 handle_tm_attribute, true },
736 { "transaction_safe_dynamic", 0, 0, true, false, false,
737 handle_tm_attribute, false },
738 { "transaction_may_cancel_outer", 0, 0, false, true, false,
739 handle_tm_attribute, false },
740 /* ??? These two attributes didn't make the transition from the
741 Intel language document to the multi-vendor language document. */
742 { "transaction_pure", 0, 0, false, true, false,
743 handle_tm_attribute, false },
744 { "transaction_wrap", 1, 1, true, false, false,
745 handle_tm_wrap_attribute, false },
746 /* For internal use (marking of builtins) only. The name contains space
747 to prevent its usage in source code. */
748 { "no vops", 0, 0, true, false, false,
749 handle_novops_attribute, false },
750 { "deprecated", 0, 1, false, false, false,
751 handle_deprecated_attribute, false },
752 { "vector_size", 1, 1, false, true, false,
753 handle_vector_size_attribute, false },
754 { "visibility", 1, 1, false, false, false,
755 handle_visibility_attribute, false },
756 { "tls_model", 1, 1, true, false, false,
757 handle_tls_model_attribute, false },
758 { "nonnull", 0, -1, false, true, true,
759 handle_nonnull_attribute, false },
760 { "nothrow", 0, 0, true, false, false,
761 handle_nothrow_attribute, false },
762 { "may_alias", 0, 0, false, true, false, NULL, false },
763 { "cleanup", 1, 1, true, false, false,
764 handle_cleanup_attribute, false },
765 { "warn_unused_result", 0, 0, false, true, true,
766 handle_warn_unused_result_attribute, false },
767 { "sentinel", 0, 1, false, true, true,
768 handle_sentinel_attribute, false },
769 /* For internal use (marking of builtins) only. The name contains space
770 to prevent its usage in source code. */
771 { "type generic", 0, 0, false, true, true,
772 handle_type_generic_attribute, false },
773 { "alloc_size", 1, 2, false, true, true,
774 handle_alloc_size_attribute, false },
775 { "cold", 0, 0, true, false, false,
776 handle_cold_attribute, false },
777 { "hot", 0, 0, true, false, false,
778 handle_hot_attribute, false },
779 { "no_address_safety_analysis",
780 0, 0, true, false, false,
781 handle_no_address_safety_analysis_attribute,
782 false },
783 { "no_sanitize_address", 0, 0, true, false, false,
784 handle_no_sanitize_address_attribute,
785 false },
786 { "no_sanitize_thread", 0, 0, true, false, false,
787 handle_no_sanitize_address_attribute,
788 false },
789 { "no_sanitize_undefined", 0, 0, true, false, false,
790 handle_no_sanitize_undefined_attribute,
791 false },
792 { "warning", 1, 1, true, false, false,
793 handle_error_attribute, false },
794 { "error", 1, 1, true, false, false,
795 handle_error_attribute, false },
796 { "target", 1, -1, true, false, false,
797 handle_target_attribute, false },
798 { "target_clones", 1, -1, true, false, false,
799 handle_target_clones_attribute, false },
800 { "optimize", 1, -1, true, false, false,
801 handle_optimize_attribute, false },
802 /* For internal use only. The leading '*' both prevents its usage in
803 source code and signals that it may be overridden by machine tables. */
804 { "*tm regparm", 0, 0, false, true, true,
805 ignore_attribute, false },
806 { "no_split_stack", 0, 0, true, false, false,
807 handle_no_split_stack_attribute, false },
808 /* For internal use (marking of builtins and runtime functions) only.
809 The name contains space to prevent its usage in source code. */
810 { "fn spec", 1, 1, false, true, true,
811 handle_fnspec_attribute, false },
812 { "warn_unused", 0, 0, false, false, false,
813 handle_warn_unused_attribute, false },
814 { "returns_nonnull", 0, 0, false, true, true,
815 handle_returns_nonnull_attribute, false },
816 { "omp declare simd", 0, -1, true, false, false,
817 handle_omp_declare_simd_attribute, false },
818 { "cilk simd function", 0, -1, true, false, false,
819 handle_omp_declare_simd_attribute, false },
820 { "simd", 0, 1, true, false, false,
821 handle_simd_attribute, false },
822 { "omp declare target", 0, 0, true, false, false,
823 handle_omp_declare_target_attribute, false },
824 { "alloc_align", 1, 1, false, true, true,
825 handle_alloc_align_attribute, false },
826 { "assume_aligned", 1, 2, false, true, true,
827 handle_assume_aligned_attribute, false },
828 { "designated_init", 0, 0, false, true, false,
829 handle_designated_init_attribute, false },
830 { "bnd_variable_size", 0, 0, true, false, false,
831 handle_bnd_variable_size_attribute, false },
832 { "bnd_legacy", 0, 0, true, false, false,
833 handle_bnd_legacy, false },
834 { "bnd_instrument", 0, 0, true, false, false,
835 handle_bnd_instrument, false },
836 { NULL, 0, 0, false, false, false, NULL, false }
839 /* Give the specifications for the format attributes, used by C and all
840 descendants.
842 All attributes referencing arguments should be additionally processed
843 in chkp_copy_function_type_adding_bounds for correct instrumentation
844 by Pointer Bounds Checker.
845 Current list of processed format attributes: format, format_arg. */
846 const struct attribute_spec c_common_format_attribute_table[] =
848 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
849 affects_type_identity } */
850 { "format", 3, 3, false, true, true,
851 handle_format_attribute, false },
852 { "format_arg", 1, 1, false, true, true,
853 handle_format_arg_attribute, false },
854 { NULL, 0, 0, false, false, false, NULL, false }
857 /* Return identifier for address space AS. */
859 const char *
860 c_addr_space_name (addr_space_t as)
862 int rid = RID_FIRST_ADDR_SPACE + as;
863 gcc_assert (ridpointers [rid]);
864 return IDENTIFIER_POINTER (ridpointers [rid]);
867 /* Push current bindings for the function name VAR_DECLS. */
869 void
870 start_fname_decls (void)
872 unsigned ix;
873 tree saved = NULL_TREE;
875 for (ix = 0; fname_vars[ix].decl; ix++)
877 tree decl = *fname_vars[ix].decl;
879 if (decl)
881 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
882 saved);
883 *fname_vars[ix].decl = NULL_TREE;
886 if (saved || saved_function_name_decls)
887 /* Normally they'll have been NULL, so only push if we've got a
888 stack, or they are non-NULL. */
889 saved_function_name_decls = tree_cons (saved, NULL_TREE,
890 saved_function_name_decls);
893 /* Finish up the current bindings, adding them into the current function's
894 statement tree. This must be done _before_ finish_stmt_tree is called.
895 If there is no current function, we must be at file scope and no statements
896 are involved. Pop the previous bindings. */
898 void
899 finish_fname_decls (void)
901 unsigned ix;
902 tree stmts = NULL_TREE;
903 tree stack = saved_function_name_decls;
905 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
906 append_to_statement_list (TREE_VALUE (stack), &stmts);
908 if (stmts)
910 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
912 if (TREE_CODE (*bodyp) == BIND_EXPR)
913 bodyp = &BIND_EXPR_BODY (*bodyp);
915 append_to_statement_list_force (*bodyp, &stmts);
916 *bodyp = stmts;
919 for (ix = 0; fname_vars[ix].decl; ix++)
920 *fname_vars[ix].decl = NULL_TREE;
922 if (stack)
924 /* We had saved values, restore them. */
925 tree saved;
927 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
929 tree decl = TREE_PURPOSE (saved);
930 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
932 *fname_vars[ix].decl = decl;
934 stack = TREE_CHAIN (stack);
936 saved_function_name_decls = stack;
939 /* Return the text name of the current function, suitably prettified
940 by PRETTY_P. Return string must be freed by caller. */
942 const char *
943 fname_as_string (int pretty_p)
945 const char *name = "top level";
946 char *namep;
947 int vrb = 2, len;
948 cpp_string cstr = { 0, 0 }, strname;
950 if (!pretty_p)
952 name = "";
953 vrb = 0;
956 if (current_function_decl)
957 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
959 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
961 namep = XNEWVEC (char, len);
962 snprintf (namep, len, "\"%s\"", name);
963 strname.text = (unsigned char *) namep;
964 strname.len = len - 1;
966 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
968 XDELETEVEC (namep);
969 return (const char *) cstr.text;
972 return namep;
975 /* Return the VAR_DECL for a const char array naming the current
976 function. If the VAR_DECL has not yet been created, create it
977 now. RID indicates how it should be formatted and IDENTIFIER_NODE
978 ID is its name (unfortunately C and C++ hold the RID values of
979 keywords in different places, so we can't derive RID from ID in
980 this language independent code. LOC is the location of the
981 function. */
983 tree
984 fname_decl (location_t loc, unsigned int rid, tree id)
986 unsigned ix;
987 tree decl = NULL_TREE;
989 for (ix = 0; fname_vars[ix].decl; ix++)
990 if (fname_vars[ix].rid == rid)
991 break;
993 decl = *fname_vars[ix].decl;
994 if (!decl)
996 /* If a tree is built here, it would normally have the lineno of
997 the current statement. Later this tree will be moved to the
998 beginning of the function and this line number will be wrong.
999 To avoid this problem set the lineno to 0 here; that prevents
1000 it from appearing in the RTL. */
1001 tree stmts;
1002 location_t saved_location = input_location;
1003 input_location = UNKNOWN_LOCATION;
1005 stmts = push_stmt_list ();
1006 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1007 stmts = pop_stmt_list (stmts);
1008 if (!IS_EMPTY_STMT (stmts))
1009 saved_function_name_decls
1010 = tree_cons (decl, stmts, saved_function_name_decls);
1011 *fname_vars[ix].decl = decl;
1012 input_location = saved_location;
1014 if (!ix && !current_function_decl)
1015 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1017 return decl;
1020 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1022 tree
1023 fix_string_type (tree value)
1025 int length = TREE_STRING_LENGTH (value);
1026 int nchars;
1027 tree e_type, i_type, a_type;
1029 /* Compute the number of elements, for the array type. */
1030 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1032 nchars = length;
1033 e_type = char_type_node;
1035 else if (TREE_TYPE (value) == char16_array_type_node)
1037 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1038 e_type = char16_type_node;
1040 else if (TREE_TYPE (value) == char32_array_type_node)
1042 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1043 e_type = char32_type_node;
1045 else
1047 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1048 e_type = wchar_type_node;
1051 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1052 limit in C++98 Annex B is very large (65536) and is not normative,
1053 so we do not diagnose it (warn_overlength_strings is forced off
1054 in c_common_post_options). */
1055 if (warn_overlength_strings)
1057 const int nchars_max = flag_isoc99 ? 4095 : 509;
1058 const int relevant_std = flag_isoc99 ? 99 : 90;
1059 if (nchars - 1 > nchars_max)
1060 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1061 separate the %d from the 'C'. 'ISO' should not be
1062 translated, but it may be moved after 'C%d' in languages
1063 where modifiers follow nouns. */
1064 pedwarn (input_location, OPT_Woverlength_strings,
1065 "string length %qd is greater than the length %qd "
1066 "ISO C%d compilers are required to support",
1067 nchars - 1, nchars_max, relevant_std);
1070 /* Create the array type for the string constant. The ISO C++
1071 standard says that a string literal has type `const char[N]' or
1072 `const wchar_t[N]'. We use the same logic when invoked as a C
1073 front-end with -Wwrite-strings.
1074 ??? We should change the type of an expression depending on the
1075 state of a warning flag. We should just be warning -- see how
1076 this is handled in the C++ front-end for the deprecated implicit
1077 conversion from string literals to `char*' or `wchar_t*'.
1079 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1080 array type being the unqualified version of that type.
1081 Therefore, if we are constructing an array of const char, we must
1082 construct the matching unqualified array type first. The C front
1083 end does not require this, but it does no harm, so we do it
1084 unconditionally. */
1085 i_type = build_index_type (size_int (nchars - 1));
1086 a_type = build_array_type (e_type, i_type);
1087 if (c_dialect_cxx() || warn_write_strings)
1088 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1090 TREE_TYPE (value) = a_type;
1091 TREE_CONSTANT (value) = 1;
1092 TREE_READONLY (value) = 1;
1093 TREE_STATIC (value) = 1;
1094 return value;
1097 /* Fold X for consideration by one of the warning functions when checking
1098 whether an expression has a constant value. */
1100 static tree
1101 fold_for_warn (tree x)
1103 if (c_dialect_cxx ())
1104 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1105 else
1106 /* The C front-end has already folded X appropriately. */
1107 return x;
1110 /* Print a warning if a constant expression had overflow in folding.
1111 Invoke this function on every expression that the language
1112 requires to be a constant expression.
1113 Note the ANSI C standard says it is erroneous for a
1114 constant expression to overflow. */
1116 void
1117 constant_expression_warning (tree value)
1119 if (warn_overflow && pedantic
1120 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1121 || TREE_CODE (value) == FIXED_CST
1122 || TREE_CODE (value) == VECTOR_CST
1123 || TREE_CODE (value) == COMPLEX_CST)
1124 && TREE_OVERFLOW (value))
1125 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1128 /* The same as above but print an unconditional error. */
1129 void
1130 constant_expression_error (tree value)
1132 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1133 || TREE_CODE (value) == FIXED_CST
1134 || TREE_CODE (value) == VECTOR_CST
1135 || TREE_CODE (value) == COMPLEX_CST)
1136 && TREE_OVERFLOW (value))
1137 error ("overflow in constant expression");
1140 /* Print a warning if an expression had overflow in folding and its
1141 operands hadn't.
1143 Invoke this function on every expression that
1144 (1) appears in the source code, and
1145 (2) is a constant expression that overflowed, and
1146 (3) is not already checked by convert_and_check;
1147 however, do not invoke this function on operands of explicit casts
1148 or when the expression is the result of an operator and any operand
1149 already overflowed. */
1151 void
1152 overflow_warning (location_t loc, tree value)
1154 if (c_inhibit_evaluation_warnings != 0)
1155 return;
1157 switch (TREE_CODE (value))
1159 case INTEGER_CST:
1160 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1161 break;
1163 case REAL_CST:
1164 warning_at (loc, OPT_Woverflow,
1165 "floating point overflow in expression");
1166 break;
1168 case FIXED_CST:
1169 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1170 break;
1172 case VECTOR_CST:
1173 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1174 break;
1176 case COMPLEX_CST:
1177 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1178 warning_at (loc, OPT_Woverflow,
1179 "complex integer overflow in expression");
1180 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1181 warning_at (loc, OPT_Woverflow,
1182 "complex floating point overflow in expression");
1183 break;
1185 default:
1186 break;
1190 /* Warn about uses of logical || / && operator in a context where it
1191 is likely that the bitwise equivalent was intended by the
1192 programmer. We have seen an expression in which CODE is a binary
1193 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1194 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1195 void
1196 warn_logical_operator (location_t location, enum tree_code code, tree type,
1197 enum tree_code code_left, tree op_left,
1198 enum tree_code ARG_UNUSED (code_right), tree op_right)
1200 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1201 int in0_p, in1_p, in_p;
1202 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1203 bool strict_overflow_p = false;
1205 if (code != TRUTH_ANDIF_EXPR
1206 && code != TRUTH_AND_EXPR
1207 && code != TRUTH_ORIF_EXPR
1208 && code != TRUTH_OR_EXPR)
1209 return;
1211 /* We don't want to warn if either operand comes from a macro
1212 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1213 see PR61534. */
1214 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1215 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1216 return;
1218 /* Warn if &&/|| are being used in a context where it is
1219 likely that the bitwise equivalent was intended by the
1220 programmer. That is, an expression such as op && MASK
1221 where op should not be any boolean expression, nor a
1222 constant, and mask seems to be a non-boolean integer constant. */
1223 if (TREE_CODE (op_right) == CONST_DECL)
1224 /* An enumerator counts as a constant. */
1225 op_right = DECL_INITIAL (op_right);
1226 if (!truth_value_p (code_left)
1227 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1228 && !CONSTANT_CLASS_P (op_left)
1229 && !TREE_NO_WARNING (op_left)
1230 && TREE_CODE (op_right) == INTEGER_CST
1231 && !integer_zerop (op_right)
1232 && !integer_onep (op_right))
1234 if (or_op)
1235 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1236 " applied to non-boolean constant");
1237 else
1238 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1239 " applied to non-boolean constant");
1240 TREE_NO_WARNING (op_left) = true;
1241 return;
1244 /* We do not warn for constants because they are typical of macro
1245 expansions that test for features. */
1246 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1247 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
1248 return;
1250 /* This warning only makes sense with logical operands. */
1251 if (!(truth_value_p (TREE_CODE (op_left))
1252 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1253 || !(truth_value_p (TREE_CODE (op_right))
1254 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1255 return;
1257 /* The range computations only work with scalars. */
1258 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1259 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1260 return;
1262 /* We first test whether either side separately is trivially true
1263 (with OR) or trivially false (with AND). If so, do not warn.
1264 This is a common idiom for testing ranges of data types in
1265 portable code. */
1266 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1267 if (!lhs)
1268 return;
1269 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1270 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1272 /* If this is an OR operation, invert both sides; now, the result
1273 should be always false to get a warning. */
1274 if (or_op)
1275 in0_p = !in0_p;
1277 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1278 if (tem && integer_zerop (tem))
1279 return;
1281 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1282 if (!rhs)
1283 return;
1284 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1285 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1287 /* If this is an OR operation, invert both sides; now, the result
1288 should be always false to get a warning. */
1289 if (or_op)
1290 in1_p = !in1_p;
1292 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1293 if (tem && integer_zerop (tem))
1294 return;
1296 /* If both expressions have the same operand, if we can merge the
1297 ranges, ... */
1298 if (operand_equal_p (lhs, rhs, 0)
1299 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1300 in1_p, low1, high1))
1302 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1303 /* ... and if the range test is always false, then warn. */
1304 if (tem && integer_zerop (tem))
1306 if (or_op)
1307 warning_at (location, OPT_Wlogical_op,
1308 "logical %<or%> of collectively exhaustive tests is "
1309 "always true");
1310 else
1311 warning_at (location, OPT_Wlogical_op,
1312 "logical %<and%> of mutually exclusive tests is "
1313 "always false");
1315 /* Or warn if the operands have exactly the same range, e.g.
1316 A > 0 && A > 0. */
1317 else if (tree_int_cst_equal (low0, low1)
1318 && tree_int_cst_equal (high0, high1))
1320 if (or_op)
1321 warning_at (location, OPT_Wlogical_op,
1322 "logical %<or%> of equal expressions");
1323 else
1324 warning_at (location, OPT_Wlogical_op,
1325 "logical %<and%> of equal expressions");
1330 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1331 with constant indices. */
1333 static tree
1334 find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1336 tree expr = *expr_p;
1338 if ((TREE_CODE (expr) == ARRAY_REF
1339 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1340 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1342 *(bool *) data = true;
1343 *walk_subtrees = 0;
1346 return NULL_TREE;
1349 /* Warn if a self-comparison always evaluates to true or false. LOC
1350 is the location of the comparison with code CODE, LHS and RHS are
1351 operands of the comparison. */
1353 void
1354 warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1356 if (TREE_CODE_CLASS (code) != tcc_comparison)
1357 return;
1359 /* Don't warn for various macro expansions. */
1360 if (from_macro_expansion_at (loc)
1361 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1362 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1363 return;
1365 /* We do not warn for constants because they are typical of macro
1366 expansions that test for features, sizeof, and similar. */
1367 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1368 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
1369 return;
1371 /* Don't warn for e.g.
1372 HOST_WIDE_INT n;
1374 if (n == (long) n) ...
1376 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1377 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1378 return;
1380 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1381 It could be a NaN, and NaN never compares equal to anything, even
1382 itself. */
1383 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1384 return;
1386 if (operand_equal_p (lhs, rhs, 0))
1388 /* Don't warn about array references with constant indices;
1389 these are likely to come from a macro. */
1390 bool found = false;
1391 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1392 &found);
1393 if (found)
1394 return;
1395 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1396 || code == GE_EXPR || code == UNLE_EXPR
1397 || code == UNGE_EXPR || code == UNEQ_EXPR);
1398 if (always_true)
1399 warning_at (loc, OPT_Wtautological_compare,
1400 "self-comparison always evaluates to true");
1401 else
1402 warning_at (loc, OPT_Wtautological_compare,
1403 "self-comparison always evaluates to false");
1407 /* Warn about logical not used on the left hand side operand of a comparison.
1408 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1409 Do not warn if RHS is of a boolean type. */
1411 void
1412 warn_logical_not_parentheses (location_t location, enum tree_code code,
1413 tree rhs)
1415 if (TREE_CODE_CLASS (code) != tcc_comparison
1416 || TREE_TYPE (rhs) == NULL_TREE
1417 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1418 return;
1420 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1421 !(x == 0) or !(y != 0). */
1422 if ((code == EQ_EXPR || code == NE_EXPR)
1423 && integer_zerop (rhs))
1424 return;
1426 warning_at (location, OPT_Wlogical_not_parentheses,
1427 "logical not is only applied to the left hand side of "
1428 "comparison");
1431 /* Warn if EXP contains any computations whose results are not used.
1432 Return true if a warning is printed; false otherwise. LOCUS is the
1433 (potential) location of the expression. */
1435 bool
1436 warn_if_unused_value (const_tree exp, location_t locus)
1438 restart:
1439 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1440 return false;
1442 /* Don't warn about void constructs. This includes casting to void,
1443 void function calls, and statement expressions with a final cast
1444 to void. */
1445 if (VOID_TYPE_P (TREE_TYPE (exp)))
1446 return false;
1448 if (EXPR_HAS_LOCATION (exp))
1449 locus = EXPR_LOCATION (exp);
1451 switch (TREE_CODE (exp))
1453 case PREINCREMENT_EXPR:
1454 case POSTINCREMENT_EXPR:
1455 case PREDECREMENT_EXPR:
1456 case POSTDECREMENT_EXPR:
1457 case MODIFY_EXPR:
1458 case INIT_EXPR:
1459 case TARGET_EXPR:
1460 case CALL_EXPR:
1461 case TRY_CATCH_EXPR:
1462 case WITH_CLEANUP_EXPR:
1463 case EXIT_EXPR:
1464 case VA_ARG_EXPR:
1465 return false;
1467 case BIND_EXPR:
1468 /* For a binding, warn if no side effect within it. */
1469 exp = BIND_EXPR_BODY (exp);
1470 goto restart;
1472 case SAVE_EXPR:
1473 case NON_LVALUE_EXPR:
1474 case NOP_EXPR:
1475 exp = TREE_OPERAND (exp, 0);
1476 goto restart;
1478 case TRUTH_ORIF_EXPR:
1479 case TRUTH_ANDIF_EXPR:
1480 /* In && or ||, warn if 2nd operand has no side effect. */
1481 exp = TREE_OPERAND (exp, 1);
1482 goto restart;
1484 case COMPOUND_EXPR:
1485 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1486 return true;
1487 /* Let people do `(foo (), 0)' without a warning. */
1488 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1489 return false;
1490 exp = TREE_OPERAND (exp, 1);
1491 goto restart;
1493 case COND_EXPR:
1494 /* If this is an expression with side effects, don't warn; this
1495 case commonly appears in macro expansions. */
1496 if (TREE_SIDE_EFFECTS (exp))
1497 return false;
1498 goto warn;
1500 case INDIRECT_REF:
1501 /* Don't warn about automatic dereferencing of references, since
1502 the user cannot control it. */
1503 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1505 exp = TREE_OPERAND (exp, 0);
1506 goto restart;
1508 /* Fall through. */
1510 default:
1511 /* Referencing a volatile value is a side effect, so don't warn. */
1512 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1513 && TREE_THIS_VOLATILE (exp))
1514 return false;
1516 /* If this is an expression which has no operands, there is no value
1517 to be unused. There are no such language-independent codes,
1518 but front ends may define such. */
1519 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1520 return false;
1522 warn:
1523 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1528 /* Print a warning about casts that might indicate violation
1529 of strict aliasing rules if -Wstrict-aliasing is used and
1530 strict aliasing mode is in effect. OTYPE is the original
1531 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1533 bool
1534 strict_aliasing_warning (tree otype, tree type, tree expr)
1536 /* Strip pointer conversion chains and get to the correct original type. */
1537 STRIP_NOPS (expr);
1538 otype = TREE_TYPE (expr);
1540 if (!(flag_strict_aliasing
1541 && POINTER_TYPE_P (type)
1542 && POINTER_TYPE_P (otype)
1543 && !VOID_TYPE_P (TREE_TYPE (type)))
1544 /* If the type we are casting to is a ref-all pointer
1545 dereferencing it is always valid. */
1546 || TYPE_REF_CAN_ALIAS_ALL (type))
1547 return false;
1549 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1550 && (DECL_P (TREE_OPERAND (expr, 0))
1551 || handled_component_p (TREE_OPERAND (expr, 0))))
1553 /* Casting the address of an object to non void pointer. Warn
1554 if the cast breaks type based aliasing. */
1555 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1557 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1558 "might break strict-aliasing rules");
1559 return true;
1561 else
1563 /* warn_strict_aliasing >= 3. This includes the default (3).
1564 Only warn if the cast is dereferenced immediately. */
1565 alias_set_type set1 =
1566 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1567 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1569 if (set1 != set2 && set2 != 0
1570 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1572 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1573 "pointer will break strict-aliasing rules");
1574 return true;
1576 else if (warn_strict_aliasing == 2
1577 && !alias_sets_must_conflict_p (set1, set2))
1579 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1580 "pointer might break strict-aliasing rules");
1581 return true;
1585 else
1586 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1588 /* At this level, warn for any conversions, even if an address is
1589 not taken in the same statement. This will likely produce many
1590 false positives, but could be useful to pinpoint problems that
1591 are not revealed at higher levels. */
1592 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1593 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1594 if (!COMPLETE_TYPE_P (type)
1595 || !alias_sets_must_conflict_p (set1, set2))
1597 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1598 "pointer might break strict-aliasing rules");
1599 return true;
1603 return false;
1606 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1607 sizeof as last operand of certain builtins. */
1609 void
1610 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1611 vec<tree, va_gc> *params, tree *sizeof_arg,
1612 bool (*comp_types) (tree, tree))
1614 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1615 bool strop = false, cmp = false;
1616 unsigned int idx = ~0;
1617 location_t loc;
1619 if (TREE_CODE (callee) != FUNCTION_DECL
1620 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1621 || vec_safe_length (params) <= 1)
1622 return;
1624 switch (DECL_FUNCTION_CODE (callee))
1626 case BUILT_IN_STRNCMP:
1627 case BUILT_IN_STRNCASECMP:
1628 cmp = true;
1629 /* FALLTHRU */
1630 case BUILT_IN_STRNCPY:
1631 case BUILT_IN_STRNCPY_CHK:
1632 case BUILT_IN_STRNCAT:
1633 case BUILT_IN_STRNCAT_CHK:
1634 case BUILT_IN_STPNCPY:
1635 case BUILT_IN_STPNCPY_CHK:
1636 strop = true;
1637 /* FALLTHRU */
1638 case BUILT_IN_MEMCPY:
1639 case BUILT_IN_MEMCPY_CHK:
1640 case BUILT_IN_MEMMOVE:
1641 case BUILT_IN_MEMMOVE_CHK:
1642 if (params->length () < 3)
1643 return;
1644 src = (*params)[1];
1645 dest = (*params)[0];
1646 idx = 2;
1647 break;
1648 case BUILT_IN_BCOPY:
1649 if (params->length () < 3)
1650 return;
1651 src = (*params)[0];
1652 dest = (*params)[1];
1653 idx = 2;
1654 break;
1655 case BUILT_IN_MEMCMP:
1656 case BUILT_IN_BCMP:
1657 if (params->length () < 3)
1658 return;
1659 src = (*params)[1];
1660 dest = (*params)[0];
1661 idx = 2;
1662 cmp = true;
1663 break;
1664 case BUILT_IN_MEMSET:
1665 case BUILT_IN_MEMSET_CHK:
1666 if (params->length () < 3)
1667 return;
1668 dest = (*params)[0];
1669 idx = 2;
1670 break;
1671 case BUILT_IN_BZERO:
1672 dest = (*params)[0];
1673 idx = 1;
1674 break;
1675 case BUILT_IN_STRNDUP:
1676 src = (*params)[0];
1677 strop = true;
1678 idx = 1;
1679 break;
1680 case BUILT_IN_MEMCHR:
1681 if (params->length () < 3)
1682 return;
1683 src = (*params)[0];
1684 idx = 2;
1685 break;
1686 case BUILT_IN_SNPRINTF:
1687 case BUILT_IN_SNPRINTF_CHK:
1688 case BUILT_IN_VSNPRINTF:
1689 case BUILT_IN_VSNPRINTF_CHK:
1690 dest = (*params)[0];
1691 idx = 1;
1692 strop = true;
1693 break;
1694 default:
1695 break;
1698 if (idx >= 3)
1699 return;
1701 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1702 return;
1704 type = TYPE_P (sizeof_arg[idx])
1705 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1706 if (!POINTER_TYPE_P (type))
1707 return;
1709 if (dest
1710 && (tem = tree_strip_nop_conversions (dest))
1711 && POINTER_TYPE_P (TREE_TYPE (tem))
1712 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1713 return;
1715 if (src
1716 && (tem = tree_strip_nop_conversions (src))
1717 && POINTER_TYPE_P (TREE_TYPE (tem))
1718 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1719 return;
1721 loc = sizeof_arg_loc[idx];
1723 if (dest && !cmp)
1725 if (!TYPE_P (sizeof_arg[idx])
1726 && operand_equal_p (dest, sizeof_arg[idx], 0)
1727 && comp_types (TREE_TYPE (dest), type))
1729 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1730 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1731 "argument to %<sizeof%> in %qD call is the same "
1732 "expression as the destination; did you mean to "
1733 "remove the addressof?", callee);
1734 else if ((TYPE_PRECISION (TREE_TYPE (type))
1735 == TYPE_PRECISION (char_type_node))
1736 || strop)
1737 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1738 "argument to %<sizeof%> in %qD call is the same "
1739 "expression as the destination; did you mean to "
1740 "provide an explicit length?", callee);
1741 else
1742 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1743 "argument to %<sizeof%> in %qD call is the same "
1744 "expression as the destination; did you mean to "
1745 "dereference it?", callee);
1746 return;
1749 if (POINTER_TYPE_P (TREE_TYPE (dest))
1750 && !strop
1751 && comp_types (TREE_TYPE (dest), type)
1752 && !VOID_TYPE_P (TREE_TYPE (type)))
1754 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1755 "argument to %<sizeof%> in %qD call is the same "
1756 "pointer type %qT as the destination; expected %qT "
1757 "or an explicit length", callee, TREE_TYPE (dest),
1758 TREE_TYPE (TREE_TYPE (dest)));
1759 return;
1763 if (src && !cmp)
1765 if (!TYPE_P (sizeof_arg[idx])
1766 && operand_equal_p (src, sizeof_arg[idx], 0)
1767 && comp_types (TREE_TYPE (src), type))
1769 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1770 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1771 "argument to %<sizeof%> in %qD call is the same "
1772 "expression as the source; did you mean to "
1773 "remove the addressof?", callee);
1774 else if ((TYPE_PRECISION (TREE_TYPE (type))
1775 == TYPE_PRECISION (char_type_node))
1776 || strop)
1777 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1778 "argument to %<sizeof%> in %qD call is the same "
1779 "expression as the source; did you mean to "
1780 "provide an explicit length?", callee);
1781 else
1782 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1783 "argument to %<sizeof%> in %qD call is the same "
1784 "expression as the source; did you mean to "
1785 "dereference it?", callee);
1786 return;
1789 if (POINTER_TYPE_P (TREE_TYPE (src))
1790 && !strop
1791 && comp_types (TREE_TYPE (src), type)
1792 && !VOID_TYPE_P (TREE_TYPE (type)))
1794 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1795 "argument to %<sizeof%> in %qD call is the same "
1796 "pointer type %qT as the source; expected %qT "
1797 "or an explicit length", callee, TREE_TYPE (src),
1798 TREE_TYPE (TREE_TYPE (src)));
1799 return;
1803 if (dest)
1805 if (!TYPE_P (sizeof_arg[idx])
1806 && operand_equal_p (dest, sizeof_arg[idx], 0)
1807 && comp_types (TREE_TYPE (dest), type))
1809 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1810 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1811 "argument to %<sizeof%> in %qD call is the same "
1812 "expression as the first source; did you mean to "
1813 "remove the addressof?", callee);
1814 else if ((TYPE_PRECISION (TREE_TYPE (type))
1815 == TYPE_PRECISION (char_type_node))
1816 || strop)
1817 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1818 "argument to %<sizeof%> in %qD call is the same "
1819 "expression as the first source; did you mean to "
1820 "provide an explicit length?", callee);
1821 else
1822 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1823 "argument to %<sizeof%> in %qD call is the same "
1824 "expression as the first source; did you mean to "
1825 "dereference it?", callee);
1826 return;
1829 if (POINTER_TYPE_P (TREE_TYPE (dest))
1830 && !strop
1831 && comp_types (TREE_TYPE (dest), type)
1832 && !VOID_TYPE_P (TREE_TYPE (type)))
1834 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1835 "argument to %<sizeof%> in %qD call is the same "
1836 "pointer type %qT as the first source; expected %qT "
1837 "or an explicit length", callee, TREE_TYPE (dest),
1838 TREE_TYPE (TREE_TYPE (dest)));
1839 return;
1843 if (src)
1845 if (!TYPE_P (sizeof_arg[idx])
1846 && operand_equal_p (src, sizeof_arg[idx], 0)
1847 && comp_types (TREE_TYPE (src), type))
1849 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1850 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1851 "argument to %<sizeof%> in %qD call is the same "
1852 "expression as the second source; did you mean to "
1853 "remove the addressof?", callee);
1854 else if ((TYPE_PRECISION (TREE_TYPE (type))
1855 == TYPE_PRECISION (char_type_node))
1856 || strop)
1857 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1858 "argument to %<sizeof%> in %qD call is the same "
1859 "expression as the second source; did you mean to "
1860 "provide an explicit length?", callee);
1861 else
1862 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1863 "argument to %<sizeof%> in %qD call is the same "
1864 "expression as the second source; did you mean to "
1865 "dereference it?", callee);
1866 return;
1869 if (POINTER_TYPE_P (TREE_TYPE (src))
1870 && !strop
1871 && comp_types (TREE_TYPE (src), type)
1872 && !VOID_TYPE_P (TREE_TYPE (type)))
1874 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1875 "argument to %<sizeof%> in %qD call is the same "
1876 "pointer type %qT as the second source; expected %qT "
1877 "or an explicit length", callee, TREE_TYPE (src),
1878 TREE_TYPE (TREE_TYPE (src)));
1879 return;
1885 /* Warn for unlikely, improbable, or stupid DECL declarations
1886 of `main'. */
1888 void
1889 check_main_parameter_types (tree decl)
1891 function_args_iterator iter;
1892 tree type;
1893 int argct = 0;
1895 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1897 /* XXX void_type_node belies the abstraction. */
1898 if (type == void_type_node || type == error_mark_node )
1899 break;
1901 tree t = type;
1902 if (TYPE_ATOMIC (t))
1903 pedwarn (input_location, OPT_Wmain,
1904 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1905 type, decl);
1906 while (POINTER_TYPE_P (t))
1908 t = TREE_TYPE (t);
1909 if (TYPE_ATOMIC (t))
1910 pedwarn (input_location, OPT_Wmain,
1911 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1912 type, decl);
1915 ++argct;
1916 switch (argct)
1918 case 1:
1919 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1920 pedwarn (input_location, OPT_Wmain,
1921 "first argument of %q+D should be %<int%>", decl);
1922 break;
1924 case 2:
1925 if (TREE_CODE (type) != POINTER_TYPE
1926 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1927 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1928 != char_type_node))
1929 pedwarn (input_location, OPT_Wmain,
1930 "second argument of %q+D should be %<char **%>", decl);
1931 break;
1933 case 3:
1934 if (TREE_CODE (type) != POINTER_TYPE
1935 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1936 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1937 != char_type_node))
1938 pedwarn (input_location, OPT_Wmain,
1939 "third argument of %q+D should probably be "
1940 "%<char **%>", decl);
1941 break;
1945 /* It is intentional that this message does not mention the third
1946 argument because it's only mentioned in an appendix of the
1947 standard. */
1948 if (argct > 0 && (argct < 2 || argct > 3))
1949 pedwarn (input_location, OPT_Wmain,
1950 "%q+D takes only zero or two arguments", decl);
1952 if (stdarg_p (TREE_TYPE (decl)))
1953 pedwarn (input_location, OPT_Wmain,
1954 "%q+D declared as variadic function", decl);
1957 /* vector_targets_convertible_p is used for vector pointer types. The
1958 callers perform various checks that the qualifiers are satisfactory,
1959 while OTOH vector_targets_convertible_p ignores the number of elements
1960 in the vectors. That's fine with vector pointers as we can consider,
1961 say, a vector of 8 elements as two consecutive vectors of 4 elements,
1962 and that does not require and conversion of the pointer values.
1963 In contrast, vector_types_convertible_p and
1964 vector_types_compatible_elements_p are used for vector value types. */
1965 /* True if pointers to distinct types T1 and T2 can be converted to
1966 each other without an explicit cast. Only returns true for opaque
1967 vector types. */
1968 bool
1969 vector_targets_convertible_p (const_tree t1, const_tree t2)
1971 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
1972 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1973 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1974 return true;
1976 return false;
1979 /* vector_types_convertible_p is used for vector value types.
1980 It could in principle call vector_targets_convertible_p as a subroutine,
1981 but then the check for vector type would be duplicated with its callers,
1982 and also the purpose of vector_targets_convertible_p would become
1983 muddled.
1984 Where vector_types_convertible_p returns true, a conversion might still be
1985 needed to make the types match.
1986 In contrast, vector_targets_convertible_p is used for vector pointer
1987 values, and vector_types_compatible_elements_p is used specifically
1988 in the context for binary operators, as a check if use is possible without
1989 conversion. */
1990 /* True if vector types T1 and T2 can be converted to each other
1991 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1992 can only be converted with -flax-vector-conversions yet that is not
1993 in effect, emit a note telling the user about that option if such
1994 a note has not previously been emitted. */
1995 bool
1996 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1998 static bool emitted_lax_note = false;
1999 bool convertible_lax;
2001 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2002 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2003 return true;
2005 convertible_lax =
2006 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2007 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2008 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2009 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2010 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2012 if (!convertible_lax || flag_lax_vector_conversions)
2013 return convertible_lax;
2015 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2016 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2017 return true;
2019 if (emit_lax_note && !emitted_lax_note)
2021 emitted_lax_note = true;
2022 inform (input_location, "use -flax-vector-conversions to permit "
2023 "conversions between vectors with differing "
2024 "element types or numbers of subparts");
2027 return false;
2030 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2031 and have vector types, V0 has the same type as V1, and the number of
2032 elements of V0, V1, MASK is the same.
2034 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2035 called with two arguments. In this case implementation passes the
2036 first argument twice in order to share the same tree code. This fact
2037 could enable the mask-values being twice the vector length. This is
2038 an implementation accident and this semantics is not guaranteed to
2039 the user. */
2040 tree
2041 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2042 bool complain)
2044 tree ret;
2045 bool wrap = true;
2046 bool maybe_const = false;
2047 bool two_arguments = false;
2049 if (v1 == NULL_TREE)
2051 two_arguments = true;
2052 v1 = v0;
2055 if (v0 == error_mark_node || v1 == error_mark_node
2056 || mask == error_mark_node)
2057 return error_mark_node;
2059 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
2061 if (complain)
2062 error_at (loc, "__builtin_shuffle last argument must "
2063 "be an integer vector");
2064 return error_mark_node;
2067 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2068 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
2070 if (complain)
2071 error_at (loc, "__builtin_shuffle arguments must be vectors");
2072 return error_mark_node;
2075 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2077 if (complain)
2078 error_at (loc, "__builtin_shuffle argument vectors must be of "
2079 "the same type");
2080 return error_mark_node;
2083 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2084 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2085 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2086 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2088 if (complain)
2089 error_at (loc, "__builtin_shuffle number of elements of the "
2090 "argument vector(s) and the mask vector should "
2091 "be the same");
2092 return error_mark_node;
2095 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2096 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2098 if (complain)
2099 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2100 "must have the same size as inner type of the mask");
2101 return error_mark_node;
2104 if (!c_dialect_cxx ())
2106 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2107 v0 = c_fully_fold (v0, false, &maybe_const);
2108 wrap &= maybe_const;
2110 if (two_arguments)
2111 v1 = v0 = save_expr (v0);
2112 else
2114 v1 = c_fully_fold (v1, false, &maybe_const);
2115 wrap &= maybe_const;
2118 mask = c_fully_fold (mask, false, &maybe_const);
2119 wrap &= maybe_const;
2121 else if (two_arguments)
2122 v1 = v0 = save_expr (v0);
2124 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2126 if (!c_dialect_cxx () && !wrap)
2127 ret = c_wrap_maybe_const (ret, true);
2129 return ret;
2132 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2133 to integral type. */
2135 static tree
2136 c_common_get_narrower (tree op, int *unsignedp_ptr)
2138 op = get_narrower (op, unsignedp_ptr);
2140 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2141 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2143 /* C++0x scoped enumerations don't implicitly convert to integral
2144 type; if we stripped an explicit conversion to a larger type we
2145 need to replace it so common_type will still work. */
2146 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2147 TYPE_UNSIGNED (TREE_TYPE (op)));
2148 op = fold_convert (type, op);
2150 return op;
2153 /* This is a helper function of build_binary_op.
2155 For certain operations if both args were extended from the same
2156 smaller type, do the arithmetic in that type and then extend.
2158 BITWISE indicates a bitwise operation.
2159 For them, this optimization is safe only if
2160 both args are zero-extended or both are sign-extended.
2161 Otherwise, we might change the result.
2162 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2163 but calculated in (unsigned short) it would be (unsigned short)-1.
2165 tree
2166 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2168 int unsigned0, unsigned1;
2169 tree arg0, arg1;
2170 int uns;
2171 tree type;
2173 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2174 excessive narrowing when we call get_narrower below. For
2175 example, suppose that OP0 is of unsigned int extended
2176 from signed char and that RESULT_TYPE is long long int.
2177 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2178 like
2180 (long long int) (unsigned int) signed_char
2182 which get_narrower would narrow down to
2184 (unsigned int) signed char
2186 If we do not cast OP0 first, get_narrower would return
2187 signed_char, which is inconsistent with the case of the
2188 explicit cast. */
2189 op0 = convert (result_type, op0);
2190 op1 = convert (result_type, op1);
2192 arg0 = c_common_get_narrower (op0, &unsigned0);
2193 arg1 = c_common_get_narrower (op1, &unsigned1);
2195 /* UNS is 1 if the operation to be done is an unsigned one. */
2196 uns = TYPE_UNSIGNED (result_type);
2198 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2199 but it *requires* conversion to FINAL_TYPE. */
2201 if ((TYPE_PRECISION (TREE_TYPE (op0))
2202 == TYPE_PRECISION (TREE_TYPE (arg0)))
2203 && TREE_TYPE (op0) != result_type)
2204 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2205 if ((TYPE_PRECISION (TREE_TYPE (op1))
2206 == TYPE_PRECISION (TREE_TYPE (arg1)))
2207 && TREE_TYPE (op1) != result_type)
2208 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2210 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2212 /* For bitwise operations, signedness of nominal type
2213 does not matter. Consider only how operands were extended. */
2214 if (bitwise)
2215 uns = unsigned0;
2217 /* Note that in all three cases below we refrain from optimizing
2218 an unsigned operation on sign-extended args.
2219 That would not be valid. */
2221 /* Both args variable: if both extended in same way
2222 from same width, do it in that width.
2223 Do it unsigned if args were zero-extended. */
2224 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2225 < TYPE_PRECISION (result_type))
2226 && (TYPE_PRECISION (TREE_TYPE (arg1))
2227 == TYPE_PRECISION (TREE_TYPE (arg0)))
2228 && unsigned0 == unsigned1
2229 && (unsigned0 || !uns))
2230 return c_common_signed_or_unsigned_type
2231 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2233 else if (TREE_CODE (arg0) == INTEGER_CST
2234 && (unsigned1 || !uns)
2235 && (TYPE_PRECISION (TREE_TYPE (arg1))
2236 < TYPE_PRECISION (result_type))
2237 && (type
2238 = c_common_signed_or_unsigned_type (unsigned1,
2239 TREE_TYPE (arg1)))
2240 && !POINTER_TYPE_P (type)
2241 && int_fits_type_p (arg0, type))
2242 return type;
2244 else if (TREE_CODE (arg1) == INTEGER_CST
2245 && (unsigned0 || !uns)
2246 && (TYPE_PRECISION (TREE_TYPE (arg0))
2247 < TYPE_PRECISION (result_type))
2248 && (type
2249 = c_common_signed_or_unsigned_type (unsigned0,
2250 TREE_TYPE (arg0)))
2251 && !POINTER_TYPE_P (type)
2252 && int_fits_type_p (arg1, type))
2253 return type;
2255 return result_type;
2258 /* Returns true iff any integer value of type FROM_TYPE can be represented as
2259 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2261 static bool
2262 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2264 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2265 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2266 REAL_VALUE_TYPE real_low_bound =
2267 real_value_from_int_cst (0, type_low_bound);
2268 REAL_VALUE_TYPE real_high_bound =
2269 real_value_from_int_cst (0, type_high_bound);
2271 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2272 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2275 /* Checks if expression EXPR of complex/real/integer type cannot be converted
2276 to the complex/real/integer type TYPE. Function returns non-zero when:
2277 * EXPR is a constant which cannot be exactly converted to TYPE.
2278 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2279 for EXPR type and TYPE being both integers or both real, or both
2280 complex.
2281 * EXPR is not a constant of complex type and TYPE is a real or
2282 an integer.
2283 * EXPR is not a constant of real type and TYPE is an integer.
2284 * EXPR is not a constant of integer type which cannot be
2285 exactly converted to real type.
2287 Function allows conversions between types of different signedness and
2288 can return SAFE_CONVERSION (zero) in that case. Function can produce
2289 signedness warnings if PRODUCE_WARNS is true.
2291 Function allows conversions from complex constants to non-complex types,
2292 provided that imaginary part is zero and real part can be safely converted
2293 to TYPE. */
2295 enum conversion_safety
2296 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2298 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2299 tree expr_type = TREE_TYPE (expr);
2300 loc = expansion_point_location_if_in_system_header (loc);
2302 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2304 /* If type is complex, we are interested in compatibility with
2305 underlying type. */
2306 if (TREE_CODE (type) == COMPLEX_TYPE)
2307 type = TREE_TYPE (type);
2309 /* Warn for real constant that is not an exact integer converted
2310 to integer type. */
2311 if (TREE_CODE (expr_type) == REAL_TYPE
2312 && TREE_CODE (type) == INTEGER_TYPE)
2314 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2315 give_warning = UNSAFE_REAL;
2317 /* Warn for an integer constant that does not fit into integer type. */
2318 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2319 && TREE_CODE (type) == INTEGER_TYPE
2320 && !int_fits_type_p (expr, type))
2322 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2323 && tree_int_cst_sgn (expr) < 0)
2325 if (produce_warns)
2326 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2327 " implicitly converted to unsigned type");
2329 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2331 if (produce_warns)
2332 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2333 " constant value to negative integer");
2335 else
2336 give_warning = UNSAFE_OTHER;
2338 else if (TREE_CODE (type) == REAL_TYPE)
2340 /* Warn for an integer constant that does not fit into real type. */
2341 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2343 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2344 if (!exact_real_truncate (TYPE_MODE (type), &a))
2345 give_warning = UNSAFE_REAL;
2347 /* Warn for a real constant that does not fit into a smaller
2348 real type. */
2349 else if (TREE_CODE (expr_type) == REAL_TYPE
2350 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2352 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2353 if (!exact_real_truncate (TYPE_MODE (type), &a))
2354 give_warning = UNSAFE_REAL;
2359 else if (TREE_CODE (expr) == COMPLEX_CST)
2361 tree imag_part = TREE_IMAGPART (expr);
2362 /* Conversion from complex constant with zero imaginary part,
2363 perform check for conversion of real part. */
2364 if ((TREE_CODE (imag_part) == REAL_CST
2365 && real_zerop (imag_part))
2366 || (TREE_CODE (imag_part) == INTEGER_CST
2367 && integer_zerop (imag_part)))
2368 /* Note: in this branch we use recursive call to unsafe_conversion_p
2369 with different type of EXPR, but it is still safe, because when EXPR
2370 is a constant, it's type is not used in text of generated warnings
2371 (otherwise they could sound misleading). */
2372 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2373 produce_warns);
2374 /* Conversion from complex constant with non-zero imaginary part. */
2375 else
2377 /* Conversion to complex type.
2378 Perform checks for both real and imaginary parts. */
2379 if (TREE_CODE (type) == COMPLEX_TYPE)
2381 /* Unfortunately, produce_warns must be false in two subsequent
2382 calls of unsafe_conversion_p, because otherwise we could
2383 produce strange "double" warnings, if both real and imaginary
2384 parts have conversion problems related to signedness.
2386 For example:
2387 int32_t _Complex a = 0x80000000 + 0x80000000i;
2389 Possible solution: add a separate function for checking
2390 constants and combine result of two calls appropriately. */
2391 enum conversion_safety re_safety =
2392 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2393 enum conversion_safety im_safety =
2394 unsafe_conversion_p (loc, type, imag_part, false);
2396 /* Merge the results into appropriate single warning. */
2398 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2399 if (re_safety == im_safety)
2400 give_warning = re_safety;
2401 else if (!re_safety && im_safety)
2402 give_warning = im_safety;
2403 else if (re_safety && !im_safety)
2404 give_warning = re_safety;
2405 else
2406 give_warning = UNSAFE_OTHER;
2408 /* Warn about conversion from complex to real or integer type. */
2409 else
2410 give_warning = UNSAFE_IMAGINARY;
2414 /* Checks for remaining case: EXPR is not constant. */
2415 else
2417 /* Warn for real types converted to integer types. */
2418 if (TREE_CODE (expr_type) == REAL_TYPE
2419 && TREE_CODE (type) == INTEGER_TYPE)
2420 give_warning = UNSAFE_REAL;
2422 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2423 && TREE_CODE (type) == INTEGER_TYPE)
2425 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2426 expr = get_unwidened (expr, 0);
2427 expr_type = TREE_TYPE (expr);
2429 /* Don't warn for short y; short x = ((int)y & 0xff); */
2430 if (TREE_CODE (expr) == BIT_AND_EXPR
2431 || TREE_CODE (expr) == BIT_IOR_EXPR
2432 || TREE_CODE (expr) == BIT_XOR_EXPR)
2434 /* If both args were extended from a shortest type,
2435 use that type if that is safe. */
2436 expr_type = shorten_binary_op (expr_type,
2437 TREE_OPERAND (expr, 0),
2438 TREE_OPERAND (expr, 1),
2439 /* bitwise */1);
2441 if (TREE_CODE (expr) == BIT_AND_EXPR)
2443 tree op0 = TREE_OPERAND (expr, 0);
2444 tree op1 = TREE_OPERAND (expr, 1);
2445 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2446 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2448 /* If one of the operands is a non-negative constant
2449 that fits in the target type, then the type of the
2450 other operand does not matter. */
2451 if ((TREE_CODE (op0) == INTEGER_CST
2452 && int_fits_type_p (op0, c_common_signed_type (type))
2453 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2454 || (TREE_CODE (op1) == INTEGER_CST
2455 && int_fits_type_p (op1, c_common_signed_type (type))
2456 && int_fits_type_p (op1,
2457 c_common_unsigned_type (type))))
2458 return SAFE_CONVERSION;
2459 /* If constant is unsigned and fits in the target
2460 type, then the result will also fit. */
2461 else if ((TREE_CODE (op0) == INTEGER_CST
2462 && unsigned0
2463 && int_fits_type_p (op0, type))
2464 || (TREE_CODE (op1) == INTEGER_CST
2465 && unsigned1
2466 && int_fits_type_p (op1, type)))
2467 return SAFE_CONVERSION;
2470 /* Warn for integer types converted to smaller integer types. */
2471 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2472 give_warning = UNSAFE_OTHER;
2474 /* When they are the same width but different signedness,
2475 then the value may change. */
2476 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2477 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2478 /* Even when converted to a bigger type, if the type is
2479 unsigned but expr is signed, then negative values
2480 will be changed. */
2481 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2482 && produce_warns)
2483 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2484 "may change the sign of the result",
2485 type, expr_type);
2488 /* Warn for integer types converted to real types if and only if
2489 all the range of values of the integer type cannot be
2490 represented by the real type. */
2491 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2492 && TREE_CODE (type) == REAL_TYPE)
2494 /* Don't warn about char y = 0xff; float x = (int) y; */
2495 expr = get_unwidened (expr, 0);
2496 expr_type = TREE_TYPE (expr);
2498 if (!int_safely_convertible_to_real_p (expr_type, type))
2499 give_warning = UNSAFE_OTHER;
2502 /* Warn for real types converted to smaller real types. */
2503 else if (TREE_CODE (expr_type) == REAL_TYPE
2504 && TREE_CODE (type) == REAL_TYPE
2505 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2506 give_warning = UNSAFE_REAL;
2508 /* Check conversion between two complex types. */
2509 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2510 && TREE_CODE (type) == COMPLEX_TYPE)
2512 /* Extract underlying types (i.e., type of real and imaginary
2513 parts) of expr_type and type. */
2514 tree from_type = TREE_TYPE (expr_type);
2515 tree to_type = TREE_TYPE (type);
2517 /* Warn for real types converted to integer types. */
2518 if (TREE_CODE (from_type) == REAL_TYPE
2519 && TREE_CODE (to_type) == INTEGER_TYPE)
2520 give_warning = UNSAFE_REAL;
2522 /* Warn for real types converted to smaller real types. */
2523 else if (TREE_CODE (from_type) == REAL_TYPE
2524 && TREE_CODE (to_type) == REAL_TYPE
2525 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2526 give_warning = UNSAFE_REAL;
2528 /* Check conversion for complex integer types. Here implementation
2529 is simpler than for real-domain integers because it does not
2530 involve sophisticated cases, such as bitmasks, casts, etc. */
2531 else if (TREE_CODE (from_type) == INTEGER_TYPE
2532 && TREE_CODE (to_type) == INTEGER_TYPE)
2534 /* Warn for integer types converted to smaller integer types. */
2535 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2536 give_warning = UNSAFE_OTHER;
2538 /* Check for different signedness, see case for real-domain
2539 integers (above) for a more detailed comment. */
2540 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2541 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2542 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2543 && produce_warns)
2544 warning_at (loc, OPT_Wsign_conversion,
2545 "conversion to %qT from %qT "
2546 "may change the sign of the result",
2547 type, expr_type);
2549 else if (TREE_CODE (from_type) == INTEGER_TYPE
2550 && TREE_CODE (to_type) == REAL_TYPE
2551 && !int_safely_convertible_to_real_p (from_type, to_type))
2552 give_warning = UNSAFE_OTHER;
2555 /* Warn for complex types converted to real or integer types. */
2556 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2557 && TREE_CODE (type) != COMPLEX_TYPE)
2558 give_warning = UNSAFE_IMAGINARY;
2561 return give_warning;
2564 /* Warns if the conversion of EXPR to TYPE may alter a value.
2565 This is a helper function for warnings_for_convert_and_check. */
2567 static void
2568 conversion_warning (location_t loc, tree type, tree expr)
2570 tree expr_type = TREE_TYPE (expr);
2571 enum conversion_safety conversion_kind;
2573 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2574 return;
2576 /* This may happen, because for LHS op= RHS we preevaluate
2577 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2578 means we could no longer see the code of the EXPR. */
2579 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2580 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2581 if (TREE_CODE (expr) == SAVE_EXPR)
2582 expr = TREE_OPERAND (expr, 0);
2584 switch (TREE_CODE (expr))
2586 case EQ_EXPR:
2587 case NE_EXPR:
2588 case LE_EXPR:
2589 case GE_EXPR:
2590 case LT_EXPR:
2591 case GT_EXPR:
2592 case TRUTH_ANDIF_EXPR:
2593 case TRUTH_ORIF_EXPR:
2594 case TRUTH_AND_EXPR:
2595 case TRUTH_OR_EXPR:
2596 case TRUTH_XOR_EXPR:
2597 case TRUTH_NOT_EXPR:
2598 /* Conversion from boolean to a signed:1 bit-field (which only
2599 can hold the values 0 and -1) doesn't lose information - but
2600 it does change the value. */
2601 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2602 warning_at (loc, OPT_Wconversion,
2603 "conversion to %qT from boolean expression", type);
2604 return;
2606 case REAL_CST:
2607 case INTEGER_CST:
2608 case COMPLEX_CST:
2609 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2610 if (conversion_kind == UNSAFE_REAL)
2611 warning_at (loc, OPT_Wfloat_conversion,
2612 "conversion to %qT alters %qT constant value",
2613 type, expr_type);
2614 else if (conversion_kind)
2615 warning_at (loc, OPT_Wconversion,
2616 "conversion to %qT alters %qT constant value",
2617 type, expr_type);
2618 return;
2620 case COND_EXPR:
2622 /* In case of COND_EXPR, we do not care about the type of
2623 COND_EXPR, only about the conversion of each operand. */
2624 tree op1 = TREE_OPERAND (expr, 1);
2625 tree op2 = TREE_OPERAND (expr, 2);
2627 conversion_warning (loc, type, op1);
2628 conversion_warning (loc, type, op2);
2629 return;
2632 default: /* 'expr' is not a constant. */
2633 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2634 if (conversion_kind == UNSAFE_REAL)
2635 warning_at (loc, OPT_Wfloat_conversion,
2636 "conversion to %qT from %qT may alter its value",
2637 type, expr_type);
2638 else if (conversion_kind == UNSAFE_IMAGINARY)
2639 warning_at (loc, OPT_Wconversion,
2640 "conversion to %qT from %qT discards imaginary component",
2641 type, expr_type);
2642 else if (conversion_kind)
2643 warning_at (loc, OPT_Wconversion,
2644 "conversion to %qT from %qT may alter its value",
2645 type, expr_type);
2649 /* Produce warnings after a conversion. RESULT is the result of
2650 converting EXPR to TYPE. This is a helper function for
2651 convert_and_check and cp_convert_and_check. */
2653 void
2654 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2655 tree result)
2657 loc = expansion_point_location_if_in_system_header (loc);
2659 if (TREE_CODE (expr) == INTEGER_CST
2660 && (TREE_CODE (type) == INTEGER_TYPE
2661 || TREE_CODE (type) == ENUMERAL_TYPE)
2662 && !int_fits_type_p (expr, type))
2664 /* Do not diagnose overflow in a constant expression merely
2665 because a conversion overflowed. */
2666 if (TREE_OVERFLOW (result))
2667 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2669 if (TYPE_UNSIGNED (type))
2671 /* This detects cases like converting -129 or 256 to
2672 unsigned char. */
2673 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2674 warning_at (loc, OPT_Woverflow,
2675 "large integer implicitly truncated to unsigned type");
2676 else
2677 conversion_warning (loc, type, expr);
2679 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2680 warning_at (loc, OPT_Woverflow,
2681 "overflow in implicit constant conversion");
2682 /* No warning for converting 0x80000000 to int. */
2683 else if (pedantic
2684 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2685 || TYPE_PRECISION (TREE_TYPE (expr))
2686 != TYPE_PRECISION (type)))
2687 warning_at (loc, OPT_Woverflow,
2688 "overflow in implicit constant conversion");
2690 else
2691 conversion_warning (loc, type, expr);
2693 else if ((TREE_CODE (result) == INTEGER_CST
2694 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2695 warning_at (loc, OPT_Woverflow,
2696 "overflow in implicit constant conversion");
2697 else
2698 conversion_warning (loc, type, expr);
2702 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2703 Invoke this function on every expression that is converted implicitly,
2704 i.e. because of language rules and not because of an explicit cast. */
2706 tree
2707 convert_and_check (location_t loc, tree type, tree expr)
2709 tree result;
2710 tree expr_for_warning;
2712 /* Convert from a value with possible excess precision rather than
2713 via the semantic type, but do not warn about values not fitting
2714 exactly in the semantic type. */
2715 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2717 tree orig_type = TREE_TYPE (expr);
2718 expr = TREE_OPERAND (expr, 0);
2719 expr_for_warning = convert (orig_type, expr);
2720 if (orig_type == type)
2721 return expr_for_warning;
2723 else
2724 expr_for_warning = expr;
2726 if (TREE_TYPE (expr) == type)
2727 return expr;
2729 result = convert (type, expr);
2731 if (c_inhibit_evaluation_warnings == 0
2732 && !TREE_OVERFLOW_P (expr)
2733 && result != error_mark_node)
2734 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2736 return result;
2739 /* A node in a list that describes references to variables (EXPR), which are
2740 either read accesses if WRITER is zero, or write accesses, in which case
2741 WRITER is the parent of EXPR. */
2742 struct tlist
2744 struct tlist *next;
2745 tree expr, writer;
2748 /* Used to implement a cache the results of a call to verify_tree. We only
2749 use this for SAVE_EXPRs. */
2750 struct tlist_cache
2752 struct tlist_cache *next;
2753 struct tlist *cache_before_sp;
2754 struct tlist *cache_after_sp;
2755 tree expr;
2758 /* Obstack to use when allocating tlist structures, and corresponding
2759 firstobj. */
2760 static struct obstack tlist_obstack;
2761 static char *tlist_firstobj = 0;
2763 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2764 warnings. */
2765 static struct tlist *warned_ids;
2766 /* SAVE_EXPRs need special treatment. We process them only once and then
2767 cache the results. */
2768 static struct tlist_cache *save_expr_cache;
2770 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2771 static void merge_tlist (struct tlist **, struct tlist *, int);
2772 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2773 static int warning_candidate_p (tree);
2774 static bool candidate_equal_p (const_tree, const_tree);
2775 static void warn_for_collisions (struct tlist *);
2776 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2777 static struct tlist *new_tlist (struct tlist *, tree, tree);
2779 /* Create a new struct tlist and fill in its fields. */
2780 static struct tlist *
2781 new_tlist (struct tlist *next, tree t, tree writer)
2783 struct tlist *l;
2784 l = XOBNEW (&tlist_obstack, struct tlist);
2785 l->next = next;
2786 l->expr = t;
2787 l->writer = writer;
2788 return l;
2791 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2792 is nonnull, we ignore any node we find which has a writer equal to it. */
2794 static void
2795 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2797 while (add)
2799 struct tlist *next = add->next;
2800 if (!copy)
2801 add->next = *to;
2802 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2803 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2804 add = next;
2808 /* Merge the nodes of ADD into TO. This merging process is done so that for
2809 each variable that already exists in TO, no new node is added; however if
2810 there is a write access recorded in ADD, and an occurrence on TO is only
2811 a read access, then the occurrence in TO will be modified to record the
2812 write. */
2814 static void
2815 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2817 struct tlist **end = to;
2819 while (*end)
2820 end = &(*end)->next;
2822 while (add)
2824 int found = 0;
2825 struct tlist *tmp2;
2826 struct tlist *next = add->next;
2828 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2829 if (candidate_equal_p (tmp2->expr, add->expr))
2831 found = 1;
2832 if (!tmp2->writer)
2833 tmp2->writer = add->writer;
2835 if (!found)
2837 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
2838 end = &(*end)->next;
2839 *end = 0;
2841 add = next;
2845 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2846 references in list LIST conflict with it, excluding reads if ONLY writers
2847 is nonzero. */
2849 static void
2850 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2851 int only_writes)
2853 struct tlist *tmp;
2855 /* Avoid duplicate warnings. */
2856 for (tmp = warned_ids; tmp; tmp = tmp->next)
2857 if (candidate_equal_p (tmp->expr, written))
2858 return;
2860 while (list)
2862 if (candidate_equal_p (list->expr, written)
2863 && !candidate_equal_p (list->writer, writer)
2864 && (!only_writes || list->writer))
2866 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2867 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
2868 OPT_Wsequence_point, "operation on %qE may be undefined",
2869 list->expr);
2871 list = list->next;
2875 /* Given a list LIST of references to variables, find whether any of these
2876 can cause conflicts due to missing sequence points. */
2878 static void
2879 warn_for_collisions (struct tlist *list)
2881 struct tlist *tmp;
2883 for (tmp = list; tmp; tmp = tmp->next)
2885 if (tmp->writer)
2886 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2890 /* Return nonzero if X is a tree that can be verified by the sequence point
2891 warnings. */
2892 static int
2893 warning_candidate_p (tree x)
2895 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2896 return 0;
2898 if (TREE_CODE (x) == BLOCK)
2899 return 0;
2901 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2902 (lvalue_p) crash on TRY/CATCH. */
2903 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2904 return 0;
2906 if (!lvalue_p (x))
2907 return 0;
2909 /* No point to track non-const calls, they will never satisfy
2910 operand_equal_p. */
2911 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2912 return 0;
2914 if (TREE_CODE (x) == STRING_CST)
2915 return 0;
2917 return 1;
2920 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2921 static bool
2922 candidate_equal_p (const_tree x, const_tree y)
2924 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2927 /* Walk the tree X, and record accesses to variables. If X is written by the
2928 parent tree, WRITER is the parent.
2929 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2930 expression or its only operand forces a sequence point, then everything up
2931 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2932 in PNO_SP.
2933 Once we return, we will have emitted warnings if any subexpression before
2934 such a sequence point could be undefined. On a higher level, however, the
2935 sequence point may not be relevant, and we'll merge the two lists.
2937 Example: (b++, a) + b;
2938 The call that processes the COMPOUND_EXPR will store the increment of B
2939 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2940 processes the PLUS_EXPR will need to merge the two lists so that
2941 eventually, all accesses end up on the same list (and we'll warn about the
2942 unordered subexpressions b++ and b.
2944 A note on merging. If we modify the former example so that our expression
2945 becomes
2946 (b++, b) + a
2947 care must be taken not simply to add all three expressions into the final
2948 PNO_SP list. The function merge_tlist takes care of that by merging the
2949 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2950 way, so that no more than one access to B is recorded. */
2952 static void
2953 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2954 tree writer)
2956 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2957 enum tree_code code;
2958 enum tree_code_class cl;
2960 /* X may be NULL if it is the operand of an empty statement expression
2961 ({ }). */
2962 if (x == NULL)
2963 return;
2965 restart:
2966 code = TREE_CODE (x);
2967 cl = TREE_CODE_CLASS (code);
2969 if (warning_candidate_p (x))
2970 *pno_sp = new_tlist (*pno_sp, x, writer);
2972 switch (code)
2974 case CONSTRUCTOR:
2975 case SIZEOF_EXPR:
2976 return;
2978 case COMPOUND_EXPR:
2979 case TRUTH_ANDIF_EXPR:
2980 case TRUTH_ORIF_EXPR:
2981 tmp_before = tmp_nosp = tmp_list3 = 0;
2982 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2983 warn_for_collisions (tmp_nosp);
2984 merge_tlist (pbefore_sp, tmp_before, 0);
2985 merge_tlist (pbefore_sp, tmp_nosp, 0);
2986 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2987 merge_tlist (pbefore_sp, tmp_list3, 0);
2988 return;
2990 case COND_EXPR:
2991 tmp_before = tmp_list2 = 0;
2992 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2993 warn_for_collisions (tmp_list2);
2994 merge_tlist (pbefore_sp, tmp_before, 0);
2995 merge_tlist (pbefore_sp, tmp_list2, 0);
2997 tmp_list3 = tmp_nosp = 0;
2998 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2999 warn_for_collisions (tmp_nosp);
3000 merge_tlist (pbefore_sp, tmp_list3, 0);
3002 tmp_list3 = tmp_list2 = 0;
3003 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3004 warn_for_collisions (tmp_list2);
3005 merge_tlist (pbefore_sp, tmp_list3, 0);
3006 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3007 two first, to avoid warning for (a ? b++ : b++). */
3008 merge_tlist (&tmp_nosp, tmp_list2, 0);
3009 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3010 return;
3012 case PREDECREMENT_EXPR:
3013 case PREINCREMENT_EXPR:
3014 case POSTDECREMENT_EXPR:
3015 case POSTINCREMENT_EXPR:
3016 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3017 return;
3019 case MODIFY_EXPR:
3020 tmp_before = tmp_nosp = tmp_list3 = 0;
3021 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3022 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3023 /* Expressions inside the LHS are not ordered wrt. the sequence points
3024 in the RHS. Example:
3025 *a = (a++, 2)
3026 Despite the fact that the modification of "a" is in the before_sp
3027 list (tmp_before), it conflicts with the use of "a" in the LHS.
3028 We can handle this by adding the contents of tmp_list3
3029 to those of tmp_before, and redoing the collision warnings for that
3030 list. */
3031 add_tlist (&tmp_before, tmp_list3, x, 1);
3032 warn_for_collisions (tmp_before);
3033 /* Exclude the LHS itself here; we first have to merge it into the
3034 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3035 didn't exclude the LHS, we'd get it twice, once as a read and once
3036 as a write. */
3037 add_tlist (pno_sp, tmp_list3, x, 0);
3038 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3040 merge_tlist (pbefore_sp, tmp_before, 0);
3041 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3042 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3043 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3044 return;
3046 case CALL_EXPR:
3047 /* We need to warn about conflicts among arguments and conflicts between
3048 args and the function address. Side effects of the function address,
3049 however, are not ordered by the sequence point of the call. */
3051 call_expr_arg_iterator iter;
3052 tree arg;
3053 tmp_before = tmp_nosp = 0;
3054 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3055 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3057 tmp_list2 = tmp_list3 = 0;
3058 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3059 merge_tlist (&tmp_list3, tmp_list2, 0);
3060 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3062 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3063 warn_for_collisions (tmp_before);
3064 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3065 return;
3068 case TREE_LIST:
3069 /* Scan all the list, e.g. indices of multi dimensional array. */
3070 while (x)
3072 tmp_before = tmp_nosp = 0;
3073 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3074 merge_tlist (&tmp_nosp, tmp_before, 0);
3075 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3076 x = TREE_CHAIN (x);
3078 return;
3080 case SAVE_EXPR:
3082 struct tlist_cache *t;
3083 for (t = save_expr_cache; t; t = t->next)
3084 if (candidate_equal_p (t->expr, x))
3085 break;
3087 if (!t)
3089 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3090 t->next = save_expr_cache;
3091 t->expr = x;
3092 save_expr_cache = t;
3094 tmp_before = tmp_nosp = 0;
3095 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3096 warn_for_collisions (tmp_nosp);
3098 tmp_list3 = 0;
3099 merge_tlist (&tmp_list3, tmp_nosp, 0);
3100 t->cache_before_sp = tmp_before;
3101 t->cache_after_sp = tmp_list3;
3103 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3104 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3105 return;
3108 case ADDR_EXPR:
3109 x = TREE_OPERAND (x, 0);
3110 if (DECL_P (x))
3111 return;
3112 writer = 0;
3113 goto restart;
3115 default:
3116 /* For other expressions, simply recurse on their operands.
3117 Manual tail recursion for unary expressions.
3118 Other non-expressions need not be processed. */
3119 if (cl == tcc_unary)
3121 x = TREE_OPERAND (x, 0);
3122 writer = 0;
3123 goto restart;
3125 else if (IS_EXPR_CODE_CLASS (cl))
3127 int lp;
3128 int max = TREE_OPERAND_LENGTH (x);
3129 for (lp = 0; lp < max; lp++)
3131 tmp_before = tmp_nosp = 0;
3132 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3133 merge_tlist (&tmp_nosp, tmp_before, 0);
3134 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3137 return;
3141 /* Try to warn for undefined behavior in EXPR due to missing sequence
3142 points. */
3144 DEBUG_FUNCTION void
3145 verify_sequence_points (tree expr)
3147 struct tlist *before_sp = 0, *after_sp = 0;
3149 warned_ids = 0;
3150 save_expr_cache = 0;
3151 if (tlist_firstobj == 0)
3153 gcc_obstack_init (&tlist_obstack);
3154 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3157 verify_tree (expr, &before_sp, &after_sp, 0);
3158 warn_for_collisions (after_sp);
3159 obstack_free (&tlist_obstack, tlist_firstobj);
3162 /* Validate the expression after `case' and apply default promotions. */
3164 static tree
3165 check_case_value (location_t loc, tree value)
3167 if (value == NULL_TREE)
3168 return value;
3170 if (TREE_CODE (value) == INTEGER_CST)
3171 /* Promote char or short to int. */
3172 value = perform_integral_promotions (value);
3173 else if (value != error_mark_node)
3175 error_at (loc, "case label does not reduce to an integer constant");
3176 value = error_mark_node;
3179 constant_expression_warning (value);
3181 return value;
3184 /* See if the case values LOW and HIGH are in the range of the original
3185 type (i.e. before the default conversion to int) of the switch testing
3186 expression.
3187 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3188 the type before promoting it. CASE_LOW_P is a pointer to the lower
3189 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3190 if the case is not a case range.
3191 The caller has to make sure that we are not called with NULL for
3192 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3193 was a case value that doesn't fit into the range of the ORIG_TYPE.
3194 Returns true if the case label is in range of ORIG_TYPE (saturated or
3195 untouched) or false if the label is out of range. */
3197 static bool
3198 check_case_bounds (location_t loc, tree type, tree orig_type,
3199 tree *case_low_p, tree *case_high_p,
3200 bool *outside_range_p)
3202 tree min_value, max_value;
3203 tree case_low = *case_low_p;
3204 tree case_high = case_high_p ? *case_high_p : case_low;
3206 /* If there was a problem with the original type, do nothing. */
3207 if (orig_type == error_mark_node)
3208 return true;
3210 min_value = TYPE_MIN_VALUE (orig_type);
3211 max_value = TYPE_MAX_VALUE (orig_type);
3213 /* We'll really need integer constants here. */
3214 case_low = fold (case_low);
3215 case_high = fold (case_high);
3217 /* Case label is less than minimum for type. */
3218 if (tree_int_cst_compare (case_low, min_value) < 0
3219 && tree_int_cst_compare (case_high, min_value) < 0)
3221 warning_at (loc, 0, "case label value is less than minimum value "
3222 "for type");
3223 *outside_range_p = true;
3224 return false;
3227 /* Case value is greater than maximum for type. */
3228 if (tree_int_cst_compare (case_low, max_value) > 0
3229 && tree_int_cst_compare (case_high, max_value) > 0)
3231 warning_at (loc, 0, "case label value exceeds maximum value for type");
3232 *outside_range_p = true;
3233 return false;
3236 /* Saturate lower case label value to minimum. */
3237 if (tree_int_cst_compare (case_high, min_value) >= 0
3238 && tree_int_cst_compare (case_low, min_value) < 0)
3240 warning_at (loc, 0, "lower value in case label range"
3241 " less than minimum value for type");
3242 *outside_range_p = true;
3243 case_low = min_value;
3246 /* Saturate upper case label value to maximum. */
3247 if (tree_int_cst_compare (case_low, max_value) <= 0
3248 && tree_int_cst_compare (case_high, max_value) > 0)
3250 warning_at (loc, 0, "upper value in case label range"
3251 " exceeds maximum value for type");
3252 *outside_range_p = true;
3253 case_high = max_value;
3256 if (*case_low_p != case_low)
3257 *case_low_p = convert (type, case_low);
3258 if (case_high_p && *case_high_p != case_high)
3259 *case_high_p = convert (type, case_high);
3261 return true;
3264 /* Return an integer type with BITS bits of precision,
3265 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3267 tree
3268 c_common_type_for_size (unsigned int bits, int unsignedp)
3270 int i;
3272 if (bits == TYPE_PRECISION (integer_type_node))
3273 return unsignedp ? unsigned_type_node : integer_type_node;
3275 if (bits == TYPE_PRECISION (signed_char_type_node))
3276 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3278 if (bits == TYPE_PRECISION (short_integer_type_node))
3279 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3281 if (bits == TYPE_PRECISION (long_integer_type_node))
3282 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3284 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3285 return (unsignedp ? long_long_unsigned_type_node
3286 : long_long_integer_type_node);
3288 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3289 if (int_n_enabled_p[i]
3290 && bits == int_n_data[i].bitsize)
3291 return (unsignedp ? int_n_trees[i].unsigned_type
3292 : int_n_trees[i].signed_type);
3294 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3295 return (unsignedp ? widest_unsigned_literal_type_node
3296 : widest_integer_literal_type_node);
3298 if (bits <= TYPE_PRECISION (intQI_type_node))
3299 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3301 if (bits <= TYPE_PRECISION (intHI_type_node))
3302 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3304 if (bits <= TYPE_PRECISION (intSI_type_node))
3305 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3307 if (bits <= TYPE_PRECISION (intDI_type_node))
3308 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3310 return 0;
3313 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3314 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3315 and saturating if SATP is nonzero, otherwise not saturating. */
3317 tree
3318 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3319 int unsignedp, int satp)
3321 machine_mode mode;
3322 if (ibit == 0)
3323 mode = unsignedp ? UQQmode : QQmode;
3324 else
3325 mode = unsignedp ? UHAmode : HAmode;
3327 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3328 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3329 break;
3331 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3333 sorry ("GCC cannot support operators with integer types and "
3334 "fixed-point types that have too many integral and "
3335 "fractional bits together");
3336 return 0;
3339 return c_common_type_for_mode (mode, satp);
3342 /* Used for communication between c_common_type_for_mode and
3343 c_register_builtin_type. */
3344 tree registered_builtin_types;
3346 /* Return a data type that has machine mode MODE.
3347 If the mode is an integer,
3348 then UNSIGNEDP selects between signed and unsigned types.
3349 If the mode is a fixed-point mode,
3350 then UNSIGNEDP selects between saturating and nonsaturating types. */
3352 tree
3353 c_common_type_for_mode (machine_mode mode, int unsignedp)
3355 tree t;
3356 int i;
3358 if (mode == TYPE_MODE (integer_type_node))
3359 return unsignedp ? unsigned_type_node : integer_type_node;
3361 if (mode == TYPE_MODE (signed_char_type_node))
3362 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3364 if (mode == TYPE_MODE (short_integer_type_node))
3365 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3367 if (mode == TYPE_MODE (long_integer_type_node))
3368 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3370 if (mode == TYPE_MODE (long_long_integer_type_node))
3371 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3373 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3374 if (int_n_enabled_p[i]
3375 && mode == int_n_data[i].m)
3376 return (unsignedp ? int_n_trees[i].unsigned_type
3377 : int_n_trees[i].signed_type);
3379 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3380 return unsignedp ? widest_unsigned_literal_type_node
3381 : widest_integer_literal_type_node;
3383 if (mode == QImode)
3384 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3386 if (mode == HImode)
3387 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3389 if (mode == SImode)
3390 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3392 if (mode == DImode)
3393 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3395 #if HOST_BITS_PER_WIDE_INT >= 64
3396 if (mode == TYPE_MODE (intTI_type_node))
3397 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3398 #endif
3400 if (mode == TYPE_MODE (float_type_node))
3401 return float_type_node;
3403 if (mode == TYPE_MODE (double_type_node))
3404 return double_type_node;
3406 if (mode == TYPE_MODE (long_double_type_node))
3407 return long_double_type_node;
3409 if (mode == TYPE_MODE (void_type_node))
3410 return void_type_node;
3412 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3413 return (unsignedp
3414 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3415 : make_signed_type (GET_MODE_PRECISION (mode)));
3417 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3418 return (unsignedp
3419 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3420 : make_signed_type (GET_MODE_PRECISION (mode)));
3422 if (COMPLEX_MODE_P (mode))
3424 machine_mode inner_mode;
3425 tree inner_type;
3427 if (mode == TYPE_MODE (complex_float_type_node))
3428 return complex_float_type_node;
3429 if (mode == TYPE_MODE (complex_double_type_node))
3430 return complex_double_type_node;
3431 if (mode == TYPE_MODE (complex_long_double_type_node))
3432 return complex_long_double_type_node;
3434 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3435 return complex_integer_type_node;
3437 inner_mode = GET_MODE_INNER (mode);
3438 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3439 if (inner_type != NULL_TREE)
3440 return build_complex_type (inner_type);
3442 else if (VECTOR_MODE_P (mode))
3444 machine_mode inner_mode = GET_MODE_INNER (mode);
3445 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3446 if (inner_type != NULL_TREE)
3447 return build_vector_type_for_mode (inner_type, mode);
3450 if (mode == TYPE_MODE (dfloat32_type_node))
3451 return dfloat32_type_node;
3452 if (mode == TYPE_MODE (dfloat64_type_node))
3453 return dfloat64_type_node;
3454 if (mode == TYPE_MODE (dfloat128_type_node))
3455 return dfloat128_type_node;
3457 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3459 if (mode == TYPE_MODE (short_fract_type_node))
3460 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3461 if (mode == TYPE_MODE (fract_type_node))
3462 return unsignedp ? sat_fract_type_node : fract_type_node;
3463 if (mode == TYPE_MODE (long_fract_type_node))
3464 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3465 if (mode == TYPE_MODE (long_long_fract_type_node))
3466 return unsignedp ? sat_long_long_fract_type_node
3467 : long_long_fract_type_node;
3469 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3470 return unsignedp ? sat_unsigned_short_fract_type_node
3471 : unsigned_short_fract_type_node;
3472 if (mode == TYPE_MODE (unsigned_fract_type_node))
3473 return unsignedp ? sat_unsigned_fract_type_node
3474 : unsigned_fract_type_node;
3475 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3476 return unsignedp ? sat_unsigned_long_fract_type_node
3477 : unsigned_long_fract_type_node;
3478 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3479 return unsignedp ? sat_unsigned_long_long_fract_type_node
3480 : unsigned_long_long_fract_type_node;
3482 if (mode == TYPE_MODE (short_accum_type_node))
3483 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3484 if (mode == TYPE_MODE (accum_type_node))
3485 return unsignedp ? sat_accum_type_node : accum_type_node;
3486 if (mode == TYPE_MODE (long_accum_type_node))
3487 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3488 if (mode == TYPE_MODE (long_long_accum_type_node))
3489 return unsignedp ? sat_long_long_accum_type_node
3490 : long_long_accum_type_node;
3492 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3493 return unsignedp ? sat_unsigned_short_accum_type_node
3494 : unsigned_short_accum_type_node;
3495 if (mode == TYPE_MODE (unsigned_accum_type_node))
3496 return unsignedp ? sat_unsigned_accum_type_node
3497 : unsigned_accum_type_node;
3498 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3499 return unsignedp ? sat_unsigned_long_accum_type_node
3500 : unsigned_long_accum_type_node;
3501 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3502 return unsignedp ? sat_unsigned_long_long_accum_type_node
3503 : unsigned_long_long_accum_type_node;
3505 if (mode == QQmode)
3506 return unsignedp ? sat_qq_type_node : qq_type_node;
3507 if (mode == HQmode)
3508 return unsignedp ? sat_hq_type_node : hq_type_node;
3509 if (mode == SQmode)
3510 return unsignedp ? sat_sq_type_node : sq_type_node;
3511 if (mode == DQmode)
3512 return unsignedp ? sat_dq_type_node : dq_type_node;
3513 if (mode == TQmode)
3514 return unsignedp ? sat_tq_type_node : tq_type_node;
3516 if (mode == UQQmode)
3517 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3518 if (mode == UHQmode)
3519 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3520 if (mode == USQmode)
3521 return unsignedp ? sat_usq_type_node : usq_type_node;
3522 if (mode == UDQmode)
3523 return unsignedp ? sat_udq_type_node : udq_type_node;
3524 if (mode == UTQmode)
3525 return unsignedp ? sat_utq_type_node : utq_type_node;
3527 if (mode == HAmode)
3528 return unsignedp ? sat_ha_type_node : ha_type_node;
3529 if (mode == SAmode)
3530 return unsignedp ? sat_sa_type_node : sa_type_node;
3531 if (mode == DAmode)
3532 return unsignedp ? sat_da_type_node : da_type_node;
3533 if (mode == TAmode)
3534 return unsignedp ? sat_ta_type_node : ta_type_node;
3536 if (mode == UHAmode)
3537 return unsignedp ? sat_uha_type_node : uha_type_node;
3538 if (mode == USAmode)
3539 return unsignedp ? sat_usa_type_node : usa_type_node;
3540 if (mode == UDAmode)
3541 return unsignedp ? sat_uda_type_node : uda_type_node;
3542 if (mode == UTAmode)
3543 return unsignedp ? sat_uta_type_node : uta_type_node;
3546 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3547 if (TYPE_MODE (TREE_VALUE (t)) == mode
3548 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3549 return TREE_VALUE (t);
3551 return 0;
3554 tree
3555 c_common_unsigned_type (tree type)
3557 return c_common_signed_or_unsigned_type (1, type);
3560 /* Return a signed type the same as TYPE in other respects. */
3562 tree
3563 c_common_signed_type (tree type)
3565 return c_common_signed_or_unsigned_type (0, type);
3568 /* Return a type the same as TYPE except unsigned or
3569 signed according to UNSIGNEDP. */
3571 tree
3572 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3574 tree type1;
3575 int i;
3577 /* This block of code emulates the behavior of the old
3578 c_common_unsigned_type. In particular, it returns
3579 long_unsigned_type_node if passed a long, even when a int would
3580 have the same size. This is necessary for warnings to work
3581 correctly in archs where sizeof(int) == sizeof(long) */
3583 type1 = TYPE_MAIN_VARIANT (type);
3584 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3585 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3586 if (type1 == integer_type_node || type1 == unsigned_type_node)
3587 return unsignedp ? unsigned_type_node : integer_type_node;
3588 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3589 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3590 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3591 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3592 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3593 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3595 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3596 if (int_n_enabled_p[i]
3597 && (type1 == int_n_trees[i].unsigned_type
3598 || type1 == int_n_trees[i].signed_type))
3599 return (unsignedp ? int_n_trees[i].unsigned_type
3600 : int_n_trees[i].signed_type);
3602 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3603 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3604 #if HOST_BITS_PER_WIDE_INT >= 64
3605 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3606 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3607 #endif
3608 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3609 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3610 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3611 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3612 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3613 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3614 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3615 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3617 #define C_COMMON_FIXED_TYPES(NAME) \
3618 if (type1 == short_ ## NAME ## _type_node \
3619 || type1 == unsigned_short_ ## NAME ## _type_node) \
3620 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3621 : short_ ## NAME ## _type_node; \
3622 if (type1 == NAME ## _type_node \
3623 || type1 == unsigned_ ## NAME ## _type_node) \
3624 return unsignedp ? unsigned_ ## NAME ## _type_node \
3625 : NAME ## _type_node; \
3626 if (type1 == long_ ## NAME ## _type_node \
3627 || type1 == unsigned_long_ ## NAME ## _type_node) \
3628 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3629 : long_ ## NAME ## _type_node; \
3630 if (type1 == long_long_ ## NAME ## _type_node \
3631 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3632 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3633 : long_long_ ## NAME ## _type_node;
3635 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3636 if (type1 == NAME ## _type_node \
3637 || type1 == u ## NAME ## _type_node) \
3638 return unsignedp ? u ## NAME ## _type_node \
3639 : NAME ## _type_node;
3641 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3642 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3643 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3644 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3645 : sat_ ## short_ ## NAME ## _type_node; \
3646 if (type1 == sat_ ## NAME ## _type_node \
3647 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3648 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3649 : sat_ ## NAME ## _type_node; \
3650 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3651 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3652 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3653 : sat_ ## long_ ## NAME ## _type_node; \
3654 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3655 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3656 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3657 : sat_ ## long_long_ ## NAME ## _type_node;
3659 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3660 if (type1 == sat_ ## NAME ## _type_node \
3661 || type1 == sat_ ## u ## NAME ## _type_node) \
3662 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3663 : sat_ ## NAME ## _type_node;
3665 C_COMMON_FIXED_TYPES (fract);
3666 C_COMMON_FIXED_TYPES_SAT (fract);
3667 C_COMMON_FIXED_TYPES (accum);
3668 C_COMMON_FIXED_TYPES_SAT (accum);
3670 C_COMMON_FIXED_MODE_TYPES (qq);
3671 C_COMMON_FIXED_MODE_TYPES (hq);
3672 C_COMMON_FIXED_MODE_TYPES (sq);
3673 C_COMMON_FIXED_MODE_TYPES (dq);
3674 C_COMMON_FIXED_MODE_TYPES (tq);
3675 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3676 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3677 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3678 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3679 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3680 C_COMMON_FIXED_MODE_TYPES (ha);
3681 C_COMMON_FIXED_MODE_TYPES (sa);
3682 C_COMMON_FIXED_MODE_TYPES (da);
3683 C_COMMON_FIXED_MODE_TYPES (ta);
3684 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3685 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3686 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3687 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3689 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3690 the precision; they have precision set to match their range, but
3691 may use a wider mode to match an ABI. If we change modes, we may
3692 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3693 the precision as well, so as to yield correct results for
3694 bit-field types. C++ does not have these separate bit-field
3695 types, and producing a signed or unsigned variant of an
3696 ENUMERAL_TYPE may cause other problems as well. */
3698 if (!INTEGRAL_TYPE_P (type)
3699 || TYPE_UNSIGNED (type) == unsignedp)
3700 return type;
3702 #define TYPE_OK(node) \
3703 (TYPE_MODE (type) == TYPE_MODE (node) \
3704 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3705 if (TYPE_OK (signed_char_type_node))
3706 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3707 if (TYPE_OK (integer_type_node))
3708 return unsignedp ? unsigned_type_node : integer_type_node;
3709 if (TYPE_OK (short_integer_type_node))
3710 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3711 if (TYPE_OK (long_integer_type_node))
3712 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3713 if (TYPE_OK (long_long_integer_type_node))
3714 return (unsignedp ? long_long_unsigned_type_node
3715 : long_long_integer_type_node);
3717 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3718 if (int_n_enabled_p[i]
3719 && TYPE_MODE (type) == int_n_data[i].m
3720 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3721 return (unsignedp ? int_n_trees[i].unsigned_type
3722 : int_n_trees[i].signed_type);
3724 if (TYPE_OK (widest_integer_literal_type_node))
3725 return (unsignedp ? widest_unsigned_literal_type_node
3726 : widest_integer_literal_type_node);
3728 #if HOST_BITS_PER_WIDE_INT >= 64
3729 if (TYPE_OK (intTI_type_node))
3730 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3731 #endif
3732 if (TYPE_OK (intDI_type_node))
3733 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3734 if (TYPE_OK (intSI_type_node))
3735 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3736 if (TYPE_OK (intHI_type_node))
3737 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3738 if (TYPE_OK (intQI_type_node))
3739 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3740 #undef TYPE_OK
3742 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3745 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3747 tree
3748 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3750 int i;
3752 /* Extended integer types of the same width as a standard type have
3753 lesser rank, so those of the same width as int promote to int or
3754 unsigned int and are valid for printf formats expecting int or
3755 unsigned int. To avoid such special cases, avoid creating
3756 extended integer types for bit-fields if a standard integer type
3757 is available. */
3758 if (width == TYPE_PRECISION (integer_type_node))
3759 return unsignedp ? unsigned_type_node : integer_type_node;
3760 if (width == TYPE_PRECISION (signed_char_type_node))
3761 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3762 if (width == TYPE_PRECISION (short_integer_type_node))
3763 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3764 if (width == TYPE_PRECISION (long_integer_type_node))
3765 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3766 if (width == TYPE_PRECISION (long_long_integer_type_node))
3767 return (unsignedp ? long_long_unsigned_type_node
3768 : long_long_integer_type_node);
3769 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3770 if (int_n_enabled_p[i]
3771 && width == int_n_data[i].bitsize)
3772 return (unsignedp ? int_n_trees[i].unsigned_type
3773 : int_n_trees[i].signed_type);
3774 return build_nonstandard_integer_type (width, unsignedp);
3777 /* The C version of the register_builtin_type langhook. */
3779 void
3780 c_register_builtin_type (tree type, const char* name)
3782 tree decl;
3784 decl = build_decl (UNKNOWN_LOCATION,
3785 TYPE_DECL, get_identifier (name), type);
3786 DECL_ARTIFICIAL (decl) = 1;
3787 if (!TYPE_NAME (type))
3788 TYPE_NAME (type) = decl;
3789 pushdecl (decl);
3791 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3794 /* Print an error message for invalid operands to arith operation
3795 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3796 LOCATION is the location of the message. */
3798 void
3799 binary_op_error (location_t location, enum tree_code code,
3800 tree type0, tree type1)
3802 const char *opname;
3804 switch (code)
3806 case PLUS_EXPR:
3807 opname = "+"; break;
3808 case MINUS_EXPR:
3809 opname = "-"; break;
3810 case MULT_EXPR:
3811 opname = "*"; break;
3812 case MAX_EXPR:
3813 opname = "max"; break;
3814 case MIN_EXPR:
3815 opname = "min"; break;
3816 case EQ_EXPR:
3817 opname = "=="; break;
3818 case NE_EXPR:
3819 opname = "!="; break;
3820 case LE_EXPR:
3821 opname = "<="; break;
3822 case GE_EXPR:
3823 opname = ">="; break;
3824 case LT_EXPR:
3825 opname = "<"; break;
3826 case GT_EXPR:
3827 opname = ">"; break;
3828 case LSHIFT_EXPR:
3829 opname = "<<"; break;
3830 case RSHIFT_EXPR:
3831 opname = ">>"; break;
3832 case TRUNC_MOD_EXPR:
3833 case FLOOR_MOD_EXPR:
3834 opname = "%"; break;
3835 case TRUNC_DIV_EXPR:
3836 case FLOOR_DIV_EXPR:
3837 opname = "/"; break;
3838 case BIT_AND_EXPR:
3839 opname = "&"; break;
3840 case BIT_IOR_EXPR:
3841 opname = "|"; break;
3842 case TRUTH_ANDIF_EXPR:
3843 opname = "&&"; break;
3844 case TRUTH_ORIF_EXPR:
3845 opname = "||"; break;
3846 case BIT_XOR_EXPR:
3847 opname = "^"; break;
3848 default:
3849 gcc_unreachable ();
3851 error_at (location,
3852 "invalid operands to binary %s (have %qT and %qT)", opname,
3853 type0, type1);
3856 /* Given an expression as a tree, return its original type. Do this
3857 by stripping any conversion that preserves the sign and precision. */
3858 static tree
3859 expr_original_type (tree expr)
3861 STRIP_SIGN_NOPS (expr);
3862 return TREE_TYPE (expr);
3865 /* Subroutine of build_binary_op, used for comparison operations.
3866 See if the operands have both been converted from subword integer types
3867 and, if so, perhaps change them both back to their original type.
3868 This function is also responsible for converting the two operands
3869 to the proper common type for comparison.
3871 The arguments of this function are all pointers to local variables
3872 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3873 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3875 LOC is the location of the comparison.
3877 If this function returns nonzero, it means that the comparison has
3878 a constant value. What this function returns is an expression for
3879 that value. */
3881 tree
3882 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3883 tree *restype_ptr, enum tree_code *rescode_ptr)
3885 tree type;
3886 tree op0 = *op0_ptr;
3887 tree op1 = *op1_ptr;
3888 int unsignedp0, unsignedp1;
3889 int real1, real2;
3890 tree primop0, primop1;
3891 enum tree_code code = *rescode_ptr;
3893 /* Throw away any conversions to wider types
3894 already present in the operands. */
3896 primop0 = c_common_get_narrower (op0, &unsignedp0);
3897 primop1 = c_common_get_narrower (op1, &unsignedp1);
3899 /* If primopN is first sign-extended from primopN's precision to opN's
3900 precision, then zero-extended from opN's precision to
3901 *restype_ptr precision, shortenings might be invalid. */
3902 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3903 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3904 && !unsignedp0
3905 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3906 primop0 = op0;
3907 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3908 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3909 && !unsignedp1
3910 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3911 primop1 = op1;
3913 /* Handle the case that OP0 does not *contain* a conversion
3914 but it *requires* conversion to FINAL_TYPE. */
3916 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3917 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3918 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3919 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3921 /* If one of the operands must be floated, we cannot optimize. */
3922 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3923 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3925 /* If first arg is constant, swap the args (changing operation
3926 so value is preserved), for canonicalization. Don't do this if
3927 the second arg is 0. */
3929 if (TREE_CONSTANT (primop0)
3930 && !integer_zerop (primop1) && !real_zerop (primop1)
3931 && !fixed_zerop (primop1))
3933 std::swap (primop0, primop1);
3934 std::swap (op0, op1);
3935 *op0_ptr = op0;
3936 *op1_ptr = op1;
3937 std::swap (unsignedp0, unsignedp1);
3938 std::swap (real1, real2);
3940 switch (code)
3942 case LT_EXPR:
3943 code = GT_EXPR;
3944 break;
3945 case GT_EXPR:
3946 code = LT_EXPR;
3947 break;
3948 case LE_EXPR:
3949 code = GE_EXPR;
3950 break;
3951 case GE_EXPR:
3952 code = LE_EXPR;
3953 break;
3954 default:
3955 break;
3957 *rescode_ptr = code;
3960 /* If comparing an integer against a constant more bits wide,
3961 maybe we can deduce a value of 1 or 0 independent of the data.
3962 Or else truncate the constant now
3963 rather than extend the variable at run time.
3965 This is only interesting if the constant is the wider arg.
3966 Also, it is not safe if the constant is unsigned and the
3967 variable arg is signed, since in this case the variable
3968 would be sign-extended and then regarded as unsigned.
3969 Our technique fails in this case because the lowest/highest
3970 possible unsigned results don't follow naturally from the
3971 lowest/highest possible values of the variable operand.
3972 For just EQ_EXPR and NE_EXPR there is another technique that
3973 could be used: see if the constant can be faithfully represented
3974 in the other operand's type, by truncating it and reextending it
3975 and see if that preserves the constant's value. */
3977 if (!real1 && !real2
3978 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3979 && TREE_CODE (primop1) == INTEGER_CST
3980 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3982 int min_gt, max_gt, min_lt, max_lt;
3983 tree maxval, minval;
3984 /* 1 if comparison is nominally unsigned. */
3985 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3986 tree val;
3988 type = c_common_signed_or_unsigned_type (unsignedp0,
3989 TREE_TYPE (primop0));
3991 maxval = TYPE_MAX_VALUE (type);
3992 minval = TYPE_MIN_VALUE (type);
3994 if (unsignedp && !unsignedp0)
3995 *restype_ptr = c_common_signed_type (*restype_ptr);
3997 if (TREE_TYPE (primop1) != *restype_ptr)
3999 /* Convert primop1 to target type, but do not introduce
4000 additional overflow. We know primop1 is an int_cst. */
4001 primop1 = force_fit_type (*restype_ptr,
4002 wide_int::from
4003 (primop1,
4004 TYPE_PRECISION (*restype_ptr),
4005 TYPE_SIGN (TREE_TYPE (primop1))),
4006 0, TREE_OVERFLOW (primop1));
4008 if (type != *restype_ptr)
4010 minval = convert (*restype_ptr, minval);
4011 maxval = convert (*restype_ptr, maxval);
4014 min_gt = tree_int_cst_lt (primop1, minval);
4015 max_gt = tree_int_cst_lt (primop1, maxval);
4016 min_lt = tree_int_cst_lt (minval, primop1);
4017 max_lt = tree_int_cst_lt (maxval, primop1);
4019 val = 0;
4020 /* This used to be a switch, but Genix compiler can't handle that. */
4021 if (code == NE_EXPR)
4023 if (max_lt || min_gt)
4024 val = truthvalue_true_node;
4026 else if (code == EQ_EXPR)
4028 if (max_lt || min_gt)
4029 val = truthvalue_false_node;
4031 else if (code == LT_EXPR)
4033 if (max_lt)
4034 val = truthvalue_true_node;
4035 if (!min_lt)
4036 val = truthvalue_false_node;
4038 else if (code == GT_EXPR)
4040 if (min_gt)
4041 val = truthvalue_true_node;
4042 if (!max_gt)
4043 val = truthvalue_false_node;
4045 else if (code == LE_EXPR)
4047 if (!max_gt)
4048 val = truthvalue_true_node;
4049 if (min_gt)
4050 val = truthvalue_false_node;
4052 else if (code == GE_EXPR)
4054 if (!min_lt)
4055 val = truthvalue_true_node;
4056 if (max_lt)
4057 val = truthvalue_false_node;
4060 /* If primop0 was sign-extended and unsigned comparison specd,
4061 we did a signed comparison above using the signed type bounds.
4062 But the comparison we output must be unsigned.
4064 Also, for inequalities, VAL is no good; but if the signed
4065 comparison had *any* fixed result, it follows that the
4066 unsigned comparison just tests the sign in reverse
4067 (positive values are LE, negative ones GE).
4068 So we can generate an unsigned comparison
4069 against an extreme value of the signed type. */
4071 if (unsignedp && !unsignedp0)
4073 if (val != 0)
4074 switch (code)
4076 case LT_EXPR:
4077 case GE_EXPR:
4078 primop1 = TYPE_MIN_VALUE (type);
4079 val = 0;
4080 break;
4082 case LE_EXPR:
4083 case GT_EXPR:
4084 primop1 = TYPE_MAX_VALUE (type);
4085 val = 0;
4086 break;
4088 default:
4089 break;
4091 type = c_common_unsigned_type (type);
4094 if (TREE_CODE (primop0) != INTEGER_CST
4095 /* Don't warn if it's from a (non-system) macro. */
4096 && !(from_macro_expansion_at
4097 (expansion_point_location_if_in_system_header
4098 (EXPR_LOCATION (primop0)))))
4100 if (val == truthvalue_false_node)
4101 warning_at (loc, OPT_Wtype_limits,
4102 "comparison is always false due to limited range of data type");
4103 if (val == truthvalue_true_node)
4104 warning_at (loc, OPT_Wtype_limits,
4105 "comparison is always true due to limited range of data type");
4108 if (val != 0)
4110 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4111 if (TREE_SIDE_EFFECTS (primop0))
4112 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4113 return val;
4116 /* Value is not predetermined, but do the comparison
4117 in the type of the operand that is not constant.
4118 TYPE is already properly set. */
4121 /* If either arg is decimal float and the other is float, find the
4122 proper common type to use for comparison. */
4123 else if (real1 && real2
4124 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4125 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4126 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4128 /* If either arg is decimal float and the other is float, fail. */
4129 else if (real1 && real2
4130 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4131 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4132 return 0;
4134 else if (real1 && real2
4135 && (TYPE_PRECISION (TREE_TYPE (primop0))
4136 == TYPE_PRECISION (TREE_TYPE (primop1))))
4137 type = TREE_TYPE (primop0);
4139 /* If args' natural types are both narrower than nominal type
4140 and both extend in the same manner, compare them
4141 in the type of the wider arg.
4142 Otherwise must actually extend both to the nominal
4143 common type lest different ways of extending
4144 alter the result.
4145 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4147 else if (unsignedp0 == unsignedp1 && real1 == real2
4148 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4149 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4151 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4152 type = c_common_signed_or_unsigned_type (unsignedp0
4153 || TYPE_UNSIGNED (*restype_ptr),
4154 type);
4155 /* Make sure shorter operand is extended the right way
4156 to match the longer operand. */
4157 primop0
4158 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4159 TREE_TYPE (primop0)),
4160 primop0);
4161 primop1
4162 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4163 TREE_TYPE (primop1)),
4164 primop1);
4166 else
4168 /* Here we must do the comparison on the nominal type
4169 using the args exactly as we received them. */
4170 type = *restype_ptr;
4171 primop0 = op0;
4172 primop1 = op1;
4174 if (!real1 && !real2 && integer_zerop (primop1)
4175 && TYPE_UNSIGNED (*restype_ptr))
4177 tree value = 0;
4178 /* All unsigned values are >= 0, so we warn. However,
4179 if OP0 is a constant that is >= 0, the signedness of
4180 the comparison isn't an issue, so suppress the
4181 warning. */
4182 bool warn =
4183 warn_type_limits && !in_system_header_at (loc)
4184 && !(TREE_CODE (primop0) == INTEGER_CST
4185 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4186 primop0)))
4187 /* Do not warn for enumeration types. */
4188 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4190 switch (code)
4192 case GE_EXPR:
4193 if (warn)
4194 warning_at (loc, OPT_Wtype_limits,
4195 "comparison of unsigned expression >= 0 is always true");
4196 value = truthvalue_true_node;
4197 break;
4199 case LT_EXPR:
4200 if (warn)
4201 warning_at (loc, OPT_Wtype_limits,
4202 "comparison of unsigned expression < 0 is always false");
4203 value = truthvalue_false_node;
4204 break;
4206 default:
4207 break;
4210 if (value != 0)
4212 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4213 if (TREE_SIDE_EFFECTS (primop0))
4214 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4215 primop0, value);
4216 return value;
4221 *op0_ptr = convert (type, primop0);
4222 *op1_ptr = convert (type, primop1);
4224 *restype_ptr = truthvalue_type_node;
4226 return 0;
4229 /* Return a tree for the sum or difference (RESULTCODE says which)
4230 of pointer PTROP and integer INTOP. */
4232 tree
4233 pointer_int_sum (location_t loc, enum tree_code resultcode,
4234 tree ptrop, tree intop, bool complain)
4236 tree size_exp, ret;
4238 /* The result is a pointer of the same type that is being added. */
4239 tree result_type = TREE_TYPE (ptrop);
4241 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4243 if (complain && warn_pointer_arith)
4244 pedwarn (loc, OPT_Wpointer_arith,
4245 "pointer of type %<void *%> used in arithmetic");
4246 else if (!complain)
4247 return error_mark_node;
4248 size_exp = integer_one_node;
4250 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4252 if (complain && warn_pointer_arith)
4253 pedwarn (loc, OPT_Wpointer_arith,
4254 "pointer to a function used in arithmetic");
4255 else if (!complain)
4256 return error_mark_node;
4257 size_exp = integer_one_node;
4259 else
4260 size_exp = size_in_bytes (TREE_TYPE (result_type));
4262 /* We are manipulating pointer values, so we don't need to warn
4263 about relying on undefined signed overflow. We disable the
4264 warning here because we use integer types so fold won't know that
4265 they are really pointers. */
4266 fold_defer_overflow_warnings ();
4268 /* If what we are about to multiply by the size of the elements
4269 contains a constant term, apply distributive law
4270 and multiply that constant term separately.
4271 This helps produce common subexpressions. */
4272 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4273 && !TREE_CONSTANT (intop)
4274 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4275 && TREE_CONSTANT (size_exp)
4276 /* If the constant comes from pointer subtraction,
4277 skip this optimization--it would cause an error. */
4278 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4279 /* If the constant is unsigned, and smaller than the pointer size,
4280 then we must skip this optimization. This is because it could cause
4281 an overflow error if the constant is negative but INTOP is not. */
4282 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4283 || (TYPE_PRECISION (TREE_TYPE (intop))
4284 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4286 enum tree_code subcode = resultcode;
4287 tree int_type = TREE_TYPE (intop);
4288 if (TREE_CODE (intop) == MINUS_EXPR)
4289 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4290 /* Convert both subexpression types to the type of intop,
4291 because weird cases involving pointer arithmetic
4292 can result in a sum or difference with different type args. */
4293 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4294 subcode, ptrop,
4295 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4296 intop = convert (int_type, TREE_OPERAND (intop, 0));
4299 /* Convert the integer argument to a type the same size as sizetype
4300 so the multiply won't overflow spuriously. */
4301 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4302 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4303 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4304 TYPE_UNSIGNED (sizetype)), intop);
4306 /* Replace the integer argument with a suitable product by the object size.
4307 Do this multiplication as signed, then convert to the appropriate type
4308 for the pointer operation and disregard an overflow that occurred only
4309 because of the sign-extension change in the latter conversion. */
4311 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4312 convert (TREE_TYPE (intop), size_exp));
4313 intop = convert (sizetype, t);
4314 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4315 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4318 /* Create the sum or difference. */
4319 if (resultcode == MINUS_EXPR)
4320 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4322 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4324 fold_undefer_and_ignore_overflow_warnings ();
4326 return ret;
4329 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4330 and if NON_CONST is known not to be permitted in an evaluated part
4331 of a constant expression. */
4333 tree
4334 c_wrap_maybe_const (tree expr, bool non_const)
4336 bool nowarning = TREE_NO_WARNING (expr);
4337 location_t loc = EXPR_LOCATION (expr);
4339 /* This should never be called for C++. */
4340 if (c_dialect_cxx ())
4341 gcc_unreachable ();
4343 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4344 STRIP_TYPE_NOPS (expr);
4345 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4346 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4347 if (nowarning)
4348 TREE_NO_WARNING (expr) = 1;
4349 protected_set_expr_location (expr, loc);
4351 return expr;
4354 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4355 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4356 around the SAVE_EXPR if needed so that c_fully_fold does not need
4357 to look inside SAVE_EXPRs. */
4359 tree
4360 c_save_expr (tree expr)
4362 bool maybe_const = true;
4363 if (c_dialect_cxx ())
4364 return save_expr (expr);
4365 expr = c_fully_fold (expr, false, &maybe_const);
4366 expr = save_expr (expr);
4367 if (!maybe_const)
4368 expr = c_wrap_maybe_const (expr, true);
4369 return expr;
4372 /* Return whether EXPR is a declaration whose address can never be
4373 NULL. */
4375 bool
4376 decl_with_nonnull_addr_p (const_tree expr)
4378 return (DECL_P (expr)
4379 && (TREE_CODE (expr) == PARM_DECL
4380 || TREE_CODE (expr) == LABEL_DECL
4381 || !DECL_WEAK (expr)));
4384 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4385 or for an `if' or `while' statement or ?..: exp. It should already
4386 have been validated to be of suitable type; otherwise, a bad
4387 diagnostic may result.
4389 The EXPR is located at LOCATION.
4391 This preparation consists of taking the ordinary
4392 representation of an expression expr and producing a valid tree
4393 boolean expression describing whether expr is nonzero. We could
4394 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4395 but we optimize comparisons, &&, ||, and !.
4397 The resulting type should always be `truthvalue_type_node'. */
4399 tree
4400 c_common_truthvalue_conversion (location_t location, tree expr)
4402 switch (TREE_CODE (expr))
4404 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4405 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4406 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4407 case ORDERED_EXPR: case UNORDERED_EXPR:
4408 if (TREE_TYPE (expr) == truthvalue_type_node)
4409 return expr;
4410 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4411 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4412 goto ret;
4414 case TRUTH_ANDIF_EXPR:
4415 case TRUTH_ORIF_EXPR:
4416 case TRUTH_AND_EXPR:
4417 case TRUTH_OR_EXPR:
4418 case TRUTH_XOR_EXPR:
4419 if (TREE_TYPE (expr) == truthvalue_type_node)
4420 return expr;
4421 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4422 c_common_truthvalue_conversion (location,
4423 TREE_OPERAND (expr, 0)),
4424 c_common_truthvalue_conversion (location,
4425 TREE_OPERAND (expr, 1)));
4426 goto ret;
4428 case TRUTH_NOT_EXPR:
4429 if (TREE_TYPE (expr) == truthvalue_type_node)
4430 return expr;
4431 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4432 c_common_truthvalue_conversion (location,
4433 TREE_OPERAND (expr, 0)));
4434 goto ret;
4436 case ERROR_MARK:
4437 return expr;
4439 case INTEGER_CST:
4440 return integer_zerop (expr) ? truthvalue_false_node
4441 : truthvalue_true_node;
4443 case REAL_CST:
4444 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4445 ? truthvalue_true_node
4446 : truthvalue_false_node;
4448 case FIXED_CST:
4449 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4450 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4451 ? truthvalue_true_node
4452 : truthvalue_false_node;
4454 case FUNCTION_DECL:
4455 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4456 /* Fall through. */
4458 case ADDR_EXPR:
4460 tree inner = TREE_OPERAND (expr, 0);
4461 if (decl_with_nonnull_addr_p (inner))
4463 /* Common Ada/Pascal programmer's mistake. */
4464 warning_at (location,
4465 OPT_Waddress,
4466 "the address of %qD will always evaluate as %<true%>",
4467 inner);
4468 return truthvalue_true_node;
4470 break;
4473 case COMPLEX_EXPR:
4474 expr = build_binary_op (EXPR_LOCATION (expr),
4475 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4476 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4477 c_common_truthvalue_conversion (location,
4478 TREE_OPERAND (expr, 0)),
4479 c_common_truthvalue_conversion (location,
4480 TREE_OPERAND (expr, 1)),
4482 goto ret;
4484 case NEGATE_EXPR:
4485 case ABS_EXPR:
4486 case FLOAT_EXPR:
4487 case EXCESS_PRECISION_EXPR:
4488 /* These don't change whether an object is nonzero or zero. */
4489 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4491 case LROTATE_EXPR:
4492 case RROTATE_EXPR:
4493 /* These don't change whether an object is zero or nonzero, but
4494 we can't ignore them if their second arg has side-effects. */
4495 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4497 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4498 TREE_OPERAND (expr, 1),
4499 c_common_truthvalue_conversion
4500 (location, TREE_OPERAND (expr, 0)));
4501 goto ret;
4503 else
4504 return c_common_truthvalue_conversion (location,
4505 TREE_OPERAND (expr, 0));
4507 case COND_EXPR:
4508 /* Distribute the conversion into the arms of a COND_EXPR. */
4509 if (c_dialect_cxx ())
4511 tree op1 = TREE_OPERAND (expr, 1);
4512 tree op2 = TREE_OPERAND (expr, 2);
4513 /* In C++ one of the arms might have void type if it is throw. */
4514 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4515 op1 = c_common_truthvalue_conversion (location, op1);
4516 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4517 op2 = c_common_truthvalue_conversion (location, op2);
4518 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4519 TREE_OPERAND (expr, 0), op1, op2);
4520 goto ret;
4522 else
4524 /* Folding will happen later for C. */
4525 expr = build3 (COND_EXPR, truthvalue_type_node,
4526 TREE_OPERAND (expr, 0),
4527 c_common_truthvalue_conversion (location,
4528 TREE_OPERAND (expr, 1)),
4529 c_common_truthvalue_conversion (location,
4530 TREE_OPERAND (expr, 2)));
4531 goto ret;
4534 CASE_CONVERT:
4536 tree totype = TREE_TYPE (expr);
4537 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4539 if (POINTER_TYPE_P (totype)
4540 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4542 tree inner = expr;
4543 STRIP_NOPS (inner);
4545 if (DECL_P (inner))
4546 warning_at (location,
4547 OPT_Waddress,
4548 "the compiler can assume that the address of "
4549 "%qD will always evaluate to %<true%>",
4550 inner);
4553 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4554 since that affects how `default_conversion' will behave. */
4555 if (TREE_CODE (totype) == REFERENCE_TYPE
4556 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4557 break;
4558 /* Don't strip a conversion from C++0x scoped enum, since they
4559 don't implicitly convert to other types. */
4560 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4561 && ENUM_IS_SCOPED (fromtype))
4562 break;
4563 /* If this isn't narrowing the argument, we can ignore it. */
4564 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4565 return c_common_truthvalue_conversion (location,
4566 TREE_OPERAND (expr, 0));
4568 break;
4570 case MODIFY_EXPR:
4571 if (!TREE_NO_WARNING (expr)
4572 && warn_parentheses)
4574 warning (OPT_Wparentheses,
4575 "suggest parentheses around assignment used as truth value");
4576 TREE_NO_WARNING (expr) = 1;
4578 break;
4580 default:
4581 break;
4584 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4586 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4587 expr = (build_binary_op
4588 (EXPR_LOCATION (expr),
4589 (TREE_SIDE_EFFECTS (expr)
4590 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4591 c_common_truthvalue_conversion
4592 (location,
4593 build_unary_op (location, REALPART_EXPR, t, 0)),
4594 c_common_truthvalue_conversion
4595 (location,
4596 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4597 0));
4598 goto ret;
4601 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4603 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4604 FCONST0 (TYPE_MODE
4605 (TREE_TYPE (expr))));
4606 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4608 else
4609 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4611 ret:
4612 protected_set_expr_location (expr, location);
4613 return expr;
4616 static void def_builtin_1 (enum built_in_function fncode,
4617 const char *name,
4618 enum built_in_class fnclass,
4619 tree fntype, tree libtype,
4620 bool both_p, bool fallback_p, bool nonansi_p,
4621 tree fnattrs, bool implicit_p);
4624 /* Apply the TYPE_QUALS to the new DECL. */
4626 void
4627 c_apply_type_quals_to_decl (int type_quals, tree decl)
4629 tree type = TREE_TYPE (decl);
4631 if (type == error_mark_node)
4632 return;
4634 if ((type_quals & TYPE_QUAL_CONST)
4635 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4636 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4637 constructor can produce constant init, so rely on cp_finish_decl to
4638 clear TREE_READONLY if the variable has non-constant init. */
4639 TREE_READONLY (decl) = 1;
4640 if (type_quals & TYPE_QUAL_VOLATILE)
4642 TREE_SIDE_EFFECTS (decl) = 1;
4643 TREE_THIS_VOLATILE (decl) = 1;
4645 if (type_quals & TYPE_QUAL_RESTRICT)
4647 while (type && TREE_CODE (type) == ARRAY_TYPE)
4648 /* Allow 'restrict' on arrays of pointers.
4649 FIXME currently we just ignore it. */
4650 type = TREE_TYPE (type);
4651 if (!type
4652 || !POINTER_TYPE_P (type)
4653 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4654 error ("invalid use of %<restrict%>");
4658 struct c_type_hasher : ggc_ptr_hash<tree_node>
4660 static hashval_t hash (tree);
4661 static bool equal (tree, tree);
4664 /* Hash function for the problem of multiple type definitions in
4665 different files. This must hash all types that will compare
4666 equal via comptypes to the same value. In practice it hashes
4667 on some of the simple stuff and leaves the details to comptypes. */
4669 hashval_t
4670 c_type_hasher::hash (tree t)
4672 int n_elements;
4673 int shift, size;
4674 tree t2;
4675 switch (TREE_CODE (t))
4677 /* For pointers, hash on pointee type plus some swizzling. */
4678 case POINTER_TYPE:
4679 return hash (TREE_TYPE (t)) ^ 0x3003003;
4680 /* Hash on number of elements and total size. */
4681 case ENUMERAL_TYPE:
4682 shift = 3;
4683 t2 = TYPE_VALUES (t);
4684 break;
4685 case RECORD_TYPE:
4686 shift = 0;
4687 t2 = TYPE_FIELDS (t);
4688 break;
4689 case QUAL_UNION_TYPE:
4690 shift = 1;
4691 t2 = TYPE_FIELDS (t);
4692 break;
4693 case UNION_TYPE:
4694 shift = 2;
4695 t2 = TYPE_FIELDS (t);
4696 break;
4697 default:
4698 gcc_unreachable ();
4700 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4701 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4702 n_elements = list_length (t2);
4703 /* We might have a VLA here. */
4704 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4705 size = 0;
4706 else
4707 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4708 return ((size << 24) | (n_elements << shift));
4711 bool
4712 c_type_hasher::equal (tree t1, tree t2)
4714 return lang_hooks.types_compatible_p (t1, t2);
4717 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4719 /* Return the typed-based alias set for T, which may be an expression
4720 or a type. Return -1 if we don't do anything special. */
4722 alias_set_type
4723 c_common_get_alias_set (tree t)
4725 tree u;
4727 /* For VLAs, use the alias set of the element type rather than the
4728 default of alias set 0 for types compared structurally. */
4729 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4731 if (TREE_CODE (t) == ARRAY_TYPE)
4732 return get_alias_set (TREE_TYPE (t));
4733 return -1;
4736 /* Permit type-punning when accessing a union, provided the access
4737 is directly through the union. For example, this code does not
4738 permit taking the address of a union member and then storing
4739 through it. Even the type-punning allowed here is a GCC
4740 extension, albeit a common and useful one; the C standard says
4741 that such accesses have implementation-defined behavior. */
4742 for (u = t;
4743 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4744 u = TREE_OPERAND (u, 0))
4745 if (TREE_CODE (u) == COMPONENT_REF
4746 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4747 return 0;
4749 /* That's all the expressions we handle specially. */
4750 if (!TYPE_P (t))
4751 return -1;
4753 /* The C standard guarantees that any object may be accessed via an
4754 lvalue that has character type. */
4755 if (t == char_type_node
4756 || t == signed_char_type_node
4757 || t == unsigned_char_type_node)
4758 return 0;
4760 /* The C standard specifically allows aliasing between signed and
4761 unsigned variants of the same type. We treat the signed
4762 variant as canonical. */
4763 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4765 tree t1 = c_common_signed_type (t);
4767 /* t1 == t can happen for boolean nodes which are always unsigned. */
4768 if (t1 != t)
4769 return get_alias_set (t1);
4772 /* Handle the case of multiple type nodes referring to "the same" type,
4773 which occurs with IMA. These share an alias set. FIXME: Currently only
4774 C90 is handled. (In C99 type compatibility is not transitive, which
4775 complicates things mightily. The alias set splay trees can theoretically
4776 represent this, but insertion is tricky when you consider all the
4777 different orders things might arrive in.) */
4779 if (c_language != clk_c || flag_isoc99)
4780 return -1;
4782 /* Save time if there's only one input file. */
4783 if (num_in_fnames == 1)
4784 return -1;
4786 /* Pointers need special handling if they point to any type that
4787 needs special handling (below). */
4788 if (TREE_CODE (t) == POINTER_TYPE)
4790 tree t2;
4791 /* Find bottom type under any nested POINTERs. */
4792 for (t2 = TREE_TYPE (t);
4793 TREE_CODE (t2) == POINTER_TYPE;
4794 t2 = TREE_TYPE (t2))
4796 if (!RECORD_OR_UNION_TYPE_P (t2)
4797 && TREE_CODE (t2) != ENUMERAL_TYPE)
4798 return -1;
4799 if (TYPE_SIZE (t2) == 0)
4800 return -1;
4802 /* These are the only cases that need special handling. */
4803 if (!RECORD_OR_UNION_TYPE_P (t)
4804 && TREE_CODE (t) != ENUMERAL_TYPE
4805 && TREE_CODE (t) != POINTER_TYPE)
4806 return -1;
4807 /* Undefined? */
4808 if (TYPE_SIZE (t) == 0)
4809 return -1;
4811 /* Look up t in hash table. Only one of the compatible types within each
4812 alias set is recorded in the table. */
4813 if (!type_hash_table)
4814 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4815 tree *slot = type_hash_table->find_slot (t, INSERT);
4816 if (*slot != NULL)
4818 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4819 return TYPE_ALIAS_SET ((tree)*slot);
4821 else
4822 /* Our caller will assign and record (in t) a new alias set; all we need
4823 to do is remember t in the hash table. */
4824 *slot = t;
4826 return -1;
4829 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4830 the IS_SIZEOF parameter indicates which operator is being applied.
4831 The COMPLAIN flag controls whether we should diagnose possibly
4832 ill-formed constructs or not. LOC is the location of the SIZEOF or
4833 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4834 a type in any context should be returned, rather than the normal
4835 alignment for that type. */
4837 tree
4838 c_sizeof_or_alignof_type (location_t loc,
4839 tree type, bool is_sizeof, bool min_alignof,
4840 int complain)
4842 const char *op_name;
4843 tree value = NULL;
4844 enum tree_code type_code = TREE_CODE (type);
4846 op_name = is_sizeof ? "sizeof" : "__alignof__";
4848 if (type_code == FUNCTION_TYPE)
4850 if (is_sizeof)
4852 if (complain && warn_pointer_arith)
4853 pedwarn (loc, OPT_Wpointer_arith,
4854 "invalid application of %<sizeof%> to a function type");
4855 else if (!complain)
4856 return error_mark_node;
4857 value = size_one_node;
4859 else
4861 if (complain)
4863 if (c_dialect_cxx ())
4864 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4865 "%<alignof%> applied to a function type");
4866 else
4867 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4868 "%<_Alignof%> applied to a function type");
4870 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4873 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4875 if (type_code == VOID_TYPE
4876 && complain && warn_pointer_arith)
4877 pedwarn (loc, OPT_Wpointer_arith,
4878 "invalid application of %qs to a void type", op_name);
4879 else if (!complain)
4880 return error_mark_node;
4881 value = size_one_node;
4883 else if (!COMPLETE_TYPE_P (type)
4884 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4886 if (complain)
4887 error_at (loc, "invalid application of %qs to incomplete type %qT",
4888 op_name, type);
4889 return error_mark_node;
4891 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4892 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4894 if (complain)
4895 error_at (loc, "invalid application of %qs to array type %qT of "
4896 "incomplete element type", op_name, type);
4897 return error_mark_node;
4899 else
4901 if (is_sizeof)
4902 /* Convert in case a char is more than one unit. */
4903 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4904 size_int (TYPE_PRECISION (char_type_node)
4905 / BITS_PER_UNIT));
4906 else if (min_alignof)
4907 value = size_int (min_align_of_type (type));
4908 else
4909 value = size_int (TYPE_ALIGN_UNIT (type));
4912 /* VALUE will have the middle-end integer type sizetype.
4913 However, we should really return a value of type `size_t',
4914 which is just a typedef for an ordinary integer type. */
4915 value = fold_convert_loc (loc, size_type_node, value);
4917 return value;
4920 /* Implement the __alignof keyword: Return the minimum required
4921 alignment of EXPR, measured in bytes. For VAR_DECLs,
4922 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4923 from an "aligned" __attribute__ specification). LOC is the
4924 location of the ALIGNOF operator. */
4926 tree
4927 c_alignof_expr (location_t loc, tree expr)
4929 tree t;
4931 if (VAR_OR_FUNCTION_DECL_P (expr))
4932 t = size_int (DECL_ALIGN_UNIT (expr));
4934 else if (TREE_CODE (expr) == COMPONENT_REF
4935 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4937 error_at (loc, "%<__alignof%> applied to a bit-field");
4938 t = size_one_node;
4940 else if (TREE_CODE (expr) == COMPONENT_REF
4941 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4942 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4944 else if (INDIRECT_REF_P (expr))
4946 tree t = TREE_OPERAND (expr, 0);
4947 tree best = t;
4948 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4950 while (CONVERT_EXPR_P (t)
4951 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4953 int thisalign;
4955 t = TREE_OPERAND (t, 0);
4956 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4957 if (thisalign > bestalign)
4958 best = t, bestalign = thisalign;
4960 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4962 else
4963 return c_alignof (loc, TREE_TYPE (expr));
4965 return fold_convert_loc (loc, size_type_node, t);
4968 /* Handle C and C++ default attributes. */
4970 enum built_in_attribute
4972 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4973 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4974 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
4975 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4976 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4977 #include "builtin-attrs.def"
4978 #undef DEF_ATTR_NULL_TREE
4979 #undef DEF_ATTR_INT
4980 #undef DEF_ATTR_STRING
4981 #undef DEF_ATTR_IDENT
4982 #undef DEF_ATTR_TREE_LIST
4983 ATTR_LAST
4986 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4988 static void c_init_attributes (void);
4990 enum c_builtin_type
4992 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4993 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4994 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4995 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4996 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4997 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4998 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4999 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5000 ARG6) NAME,
5001 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5002 ARG6, ARG7) NAME,
5003 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5004 ARG6, ARG7, ARG8) NAME,
5005 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5006 ARG6, ARG7, ARG8, ARG9) NAME,
5007 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5008 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5009 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5010 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5011 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5012 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5013 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5014 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5015 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5016 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5017 NAME,
5018 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5019 ARG6) NAME,
5020 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5021 ARG6, ARG7) NAME,
5022 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5023 #include "builtin-types.def"
5024 #undef DEF_PRIMITIVE_TYPE
5025 #undef DEF_FUNCTION_TYPE_0
5026 #undef DEF_FUNCTION_TYPE_1
5027 #undef DEF_FUNCTION_TYPE_2
5028 #undef DEF_FUNCTION_TYPE_3
5029 #undef DEF_FUNCTION_TYPE_4
5030 #undef DEF_FUNCTION_TYPE_5
5031 #undef DEF_FUNCTION_TYPE_6
5032 #undef DEF_FUNCTION_TYPE_7
5033 #undef DEF_FUNCTION_TYPE_8
5034 #undef DEF_FUNCTION_TYPE_9
5035 #undef DEF_FUNCTION_TYPE_10
5036 #undef DEF_FUNCTION_TYPE_11
5037 #undef DEF_FUNCTION_TYPE_VAR_0
5038 #undef DEF_FUNCTION_TYPE_VAR_1
5039 #undef DEF_FUNCTION_TYPE_VAR_2
5040 #undef DEF_FUNCTION_TYPE_VAR_3
5041 #undef DEF_FUNCTION_TYPE_VAR_4
5042 #undef DEF_FUNCTION_TYPE_VAR_5
5043 #undef DEF_FUNCTION_TYPE_VAR_6
5044 #undef DEF_FUNCTION_TYPE_VAR_7
5045 #undef DEF_POINTER_TYPE
5046 BT_LAST
5049 typedef enum c_builtin_type builtin_type;
5051 /* A temporary array for c_common_nodes_and_builtins. Used in
5052 communication with def_fn_type. */
5053 static tree builtin_types[(int) BT_LAST + 1];
5055 /* A helper function for c_common_nodes_and_builtins. Build function type
5056 for DEF with return type RET and N arguments. If VAR is true, then the
5057 function should be variadic after those N arguments.
5059 Takes special care not to ICE if any of the types involved are
5060 error_mark_node, which indicates that said type is not in fact available
5061 (see builtin_type_for_size). In which case the function type as a whole
5062 should be error_mark_node. */
5064 static void
5065 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5067 tree t;
5068 tree *args = XALLOCAVEC (tree, n);
5069 va_list list;
5070 int i;
5072 va_start (list, n);
5073 for (i = 0; i < n; ++i)
5075 builtin_type a = (builtin_type) va_arg (list, int);
5076 t = builtin_types[a];
5077 if (t == error_mark_node)
5078 goto egress;
5079 args[i] = t;
5082 t = builtin_types[ret];
5083 if (t == error_mark_node)
5084 goto egress;
5085 if (var)
5086 t = build_varargs_function_type_array (t, n, args);
5087 else
5088 t = build_function_type_array (t, n, args);
5090 egress:
5091 builtin_types[def] = t;
5092 va_end (list);
5095 /* Build builtin functions common to both C and C++ language
5096 frontends. */
5098 static void
5099 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5101 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5102 builtin_types[ENUM] = VALUE;
5103 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5104 def_fn_type (ENUM, RETURN, 0, 0);
5105 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5106 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5107 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5108 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5109 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5110 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5111 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5112 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5113 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5114 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5115 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5116 ARG6) \
5117 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5118 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5119 ARG6, ARG7) \
5120 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5121 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5122 ARG6, ARG7, ARG8) \
5123 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5124 ARG7, ARG8);
5125 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5126 ARG6, ARG7, ARG8, ARG9) \
5127 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5128 ARG7, ARG8, ARG9);
5129 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5130 ARG6, ARG7, ARG8, ARG9, ARG10) \
5131 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5132 ARG7, ARG8, ARG9, ARG10);
5133 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5134 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5135 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5136 ARG7, ARG8, ARG9, ARG10, ARG11);
5137 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5138 def_fn_type (ENUM, RETURN, 1, 0);
5139 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5140 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5141 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5142 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5143 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5144 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5145 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5146 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5147 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5148 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5149 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5150 ARG6) \
5151 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5152 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5153 ARG6, ARG7) \
5154 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5155 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5156 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5158 #include "builtin-types.def"
5160 #undef DEF_PRIMITIVE_TYPE
5161 #undef DEF_FUNCTION_TYPE_0
5162 #undef DEF_FUNCTION_TYPE_1
5163 #undef DEF_FUNCTION_TYPE_2
5164 #undef DEF_FUNCTION_TYPE_3
5165 #undef DEF_FUNCTION_TYPE_4
5166 #undef DEF_FUNCTION_TYPE_5
5167 #undef DEF_FUNCTION_TYPE_6
5168 #undef DEF_FUNCTION_TYPE_7
5169 #undef DEF_FUNCTION_TYPE_8
5170 #undef DEF_FUNCTION_TYPE_9
5171 #undef DEF_FUNCTION_TYPE_10
5172 #undef DEF_FUNCTION_TYPE_11
5173 #undef DEF_FUNCTION_TYPE_VAR_0
5174 #undef DEF_FUNCTION_TYPE_VAR_1
5175 #undef DEF_FUNCTION_TYPE_VAR_2
5176 #undef DEF_FUNCTION_TYPE_VAR_3
5177 #undef DEF_FUNCTION_TYPE_VAR_4
5178 #undef DEF_FUNCTION_TYPE_VAR_5
5179 #undef DEF_FUNCTION_TYPE_VAR_6
5180 #undef DEF_FUNCTION_TYPE_VAR_7
5181 #undef DEF_POINTER_TYPE
5182 builtin_types[(int) BT_LAST] = NULL_TREE;
5184 c_init_attributes ();
5186 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5187 NONANSI_P, ATTRS, IMPLICIT, COND) \
5188 if (NAME && COND) \
5189 def_builtin_1 (ENUM, NAME, CLASS, \
5190 builtin_types[(int) TYPE], \
5191 builtin_types[(int) LIBTYPE], \
5192 BOTH_P, FALLBACK_P, NONANSI_P, \
5193 built_in_attributes[(int) ATTRS], IMPLICIT);
5194 #include "builtins.def"
5196 targetm.init_builtins ();
5198 build_common_builtin_nodes ();
5200 if (flag_cilkplus)
5201 cilk_init_builtins ();
5204 /* Like get_identifier, but avoid warnings about null arguments when
5205 the argument may be NULL for targets where GCC lacks stdint.h type
5206 information. */
5208 static inline tree
5209 c_get_ident (const char *id)
5211 return get_identifier (id);
5214 /* Build tree nodes and builtin functions common to both C and C++ language
5215 frontends. */
5217 void
5218 c_common_nodes_and_builtins (void)
5220 int char16_type_size;
5221 int char32_type_size;
5222 int wchar_type_size;
5223 tree array_domain_type;
5224 tree va_list_ref_type_node;
5225 tree va_list_arg_type_node;
5226 int i;
5228 build_common_tree_nodes (flag_signed_char, flag_short_double);
5230 /* Define `int' and `char' first so that dbx will output them first. */
5231 record_builtin_type (RID_INT, NULL, integer_type_node);
5232 record_builtin_type (RID_CHAR, "char", char_type_node);
5234 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5235 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5236 but not C. Are the conditionals here needed? */
5237 if (c_dialect_cxx ())
5238 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5239 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5240 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5241 record_builtin_type (RID_MAX, "long unsigned int",
5242 long_unsigned_type_node);
5244 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5246 char name[25];
5248 sprintf (name, "__int%d", int_n_data[i].bitsize);
5249 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5250 int_n_trees[i].signed_type);
5251 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5252 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5255 if (c_dialect_cxx ())
5256 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5257 record_builtin_type (RID_MAX, "long long int",
5258 long_long_integer_type_node);
5259 record_builtin_type (RID_MAX, "long long unsigned int",
5260 long_long_unsigned_type_node);
5261 if (c_dialect_cxx ())
5262 record_builtin_type (RID_MAX, "long long unsigned",
5263 long_long_unsigned_type_node);
5264 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5265 record_builtin_type (RID_MAX, "short unsigned int",
5266 short_unsigned_type_node);
5267 if (c_dialect_cxx ())
5268 record_builtin_type (RID_MAX, "unsigned short",
5269 short_unsigned_type_node);
5271 /* Define both `signed char' and `unsigned char'. */
5272 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5273 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5275 /* These are types that c_common_type_for_size and
5276 c_common_type_for_mode use. */
5277 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5278 TYPE_DECL, NULL_TREE,
5279 intQI_type_node));
5280 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5281 TYPE_DECL, NULL_TREE,
5282 intHI_type_node));
5283 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5284 TYPE_DECL, NULL_TREE,
5285 intSI_type_node));
5286 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5287 TYPE_DECL, NULL_TREE,
5288 intDI_type_node));
5289 #if HOST_BITS_PER_WIDE_INT >= 64
5290 /* Note that this is different than the __int128 type that's part of
5291 the generic __intN support. */
5292 if (targetm.scalar_mode_supported_p (TImode))
5293 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5294 TYPE_DECL,
5295 get_identifier ("__int128_t"),
5296 intTI_type_node));
5297 #endif
5298 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5299 TYPE_DECL, NULL_TREE,
5300 unsigned_intQI_type_node));
5301 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5302 TYPE_DECL, NULL_TREE,
5303 unsigned_intHI_type_node));
5304 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5305 TYPE_DECL, NULL_TREE,
5306 unsigned_intSI_type_node));
5307 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5308 TYPE_DECL, NULL_TREE,
5309 unsigned_intDI_type_node));
5310 #if HOST_BITS_PER_WIDE_INT >= 64
5311 if (targetm.scalar_mode_supported_p (TImode))
5312 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5313 TYPE_DECL,
5314 get_identifier ("__uint128_t"),
5315 unsigned_intTI_type_node));
5316 #endif
5318 /* Create the widest literal types. */
5319 widest_integer_literal_type_node
5320 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5321 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5322 TYPE_DECL, NULL_TREE,
5323 widest_integer_literal_type_node));
5325 widest_unsigned_literal_type_node
5326 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5327 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5328 TYPE_DECL, NULL_TREE,
5329 widest_unsigned_literal_type_node));
5331 signed_size_type_node = c_common_signed_type (size_type_node);
5333 pid_type_node =
5334 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5336 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5337 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5338 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5340 /* Only supported decimal floating point extension if the target
5341 actually supports underlying modes. */
5342 if (targetm.scalar_mode_supported_p (SDmode)
5343 && targetm.scalar_mode_supported_p (DDmode)
5344 && targetm.scalar_mode_supported_p (TDmode))
5346 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5347 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5348 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5351 if (targetm.fixed_point_supported_p ())
5353 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5354 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5355 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5356 record_builtin_type (RID_MAX, "long long _Fract",
5357 long_long_fract_type_node);
5358 record_builtin_type (RID_MAX, "unsigned short _Fract",
5359 unsigned_short_fract_type_node);
5360 record_builtin_type (RID_MAX, "unsigned _Fract",
5361 unsigned_fract_type_node);
5362 record_builtin_type (RID_MAX, "unsigned long _Fract",
5363 unsigned_long_fract_type_node);
5364 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5365 unsigned_long_long_fract_type_node);
5366 record_builtin_type (RID_MAX, "_Sat short _Fract",
5367 sat_short_fract_type_node);
5368 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5369 record_builtin_type (RID_MAX, "_Sat long _Fract",
5370 sat_long_fract_type_node);
5371 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5372 sat_long_long_fract_type_node);
5373 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5374 sat_unsigned_short_fract_type_node);
5375 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5376 sat_unsigned_fract_type_node);
5377 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5378 sat_unsigned_long_fract_type_node);
5379 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5380 sat_unsigned_long_long_fract_type_node);
5381 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5382 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5383 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5384 record_builtin_type (RID_MAX, "long long _Accum",
5385 long_long_accum_type_node);
5386 record_builtin_type (RID_MAX, "unsigned short _Accum",
5387 unsigned_short_accum_type_node);
5388 record_builtin_type (RID_MAX, "unsigned _Accum",
5389 unsigned_accum_type_node);
5390 record_builtin_type (RID_MAX, "unsigned long _Accum",
5391 unsigned_long_accum_type_node);
5392 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5393 unsigned_long_long_accum_type_node);
5394 record_builtin_type (RID_MAX, "_Sat short _Accum",
5395 sat_short_accum_type_node);
5396 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5397 record_builtin_type (RID_MAX, "_Sat long _Accum",
5398 sat_long_accum_type_node);
5399 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5400 sat_long_long_accum_type_node);
5401 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5402 sat_unsigned_short_accum_type_node);
5403 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5404 sat_unsigned_accum_type_node);
5405 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5406 sat_unsigned_long_accum_type_node);
5407 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5408 sat_unsigned_long_long_accum_type_node);
5412 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5413 TYPE_DECL,
5414 get_identifier ("complex int"),
5415 complex_integer_type_node));
5416 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5417 TYPE_DECL,
5418 get_identifier ("complex float"),
5419 complex_float_type_node));
5420 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5421 TYPE_DECL,
5422 get_identifier ("complex double"),
5423 complex_double_type_node));
5424 lang_hooks.decls.pushdecl
5425 (build_decl (UNKNOWN_LOCATION,
5426 TYPE_DECL, get_identifier ("complex long double"),
5427 complex_long_double_type_node));
5429 if (c_dialect_cxx ())
5430 /* For C++, make fileptr_type_node a distinct void * type until
5431 FILE type is defined. */
5432 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5434 record_builtin_type (RID_VOID, NULL, void_type_node);
5436 /* Set the TYPE_NAME for any variants that were built before
5437 record_builtin_type gave names to the built-in types. */
5439 tree void_name = TYPE_NAME (void_type_node);
5440 TYPE_NAME (void_type_node) = NULL_TREE;
5441 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5442 = void_name;
5443 TYPE_NAME (void_type_node) = void_name;
5446 void_list_node = build_void_list_node ();
5448 /* Make a type to be the domain of a few array types
5449 whose domains don't really matter.
5450 200 is small enough that it always fits in size_t
5451 and large enough that it can hold most function names for the
5452 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5453 array_domain_type = build_index_type (size_int (200));
5455 /* Make a type for arrays of characters.
5456 With luck nothing will ever really depend on the length of this
5457 array type. */
5458 char_array_type_node
5459 = build_array_type (char_type_node, array_domain_type);
5461 string_type_node = build_pointer_type (char_type_node);
5462 const_string_type_node
5463 = build_pointer_type (build_qualified_type
5464 (char_type_node, TYPE_QUAL_CONST));
5466 /* This is special for C++ so functions can be overloaded. */
5467 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5468 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5469 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5470 underlying_wchar_type_node = wchar_type_node;
5471 if (c_dialect_cxx ())
5473 if (TYPE_UNSIGNED (wchar_type_node))
5474 wchar_type_node = make_unsigned_type (wchar_type_size);
5475 else
5476 wchar_type_node = make_signed_type (wchar_type_size);
5477 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5480 /* This is for wide string constants. */
5481 wchar_array_type_node
5482 = build_array_type (wchar_type_node, array_domain_type);
5484 /* Define 'char16_t'. */
5485 char16_type_node = get_identifier (CHAR16_TYPE);
5486 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5487 char16_type_size = TYPE_PRECISION (char16_type_node);
5488 if (c_dialect_cxx ())
5490 char16_type_node = make_unsigned_type (char16_type_size);
5492 if (cxx_dialect >= cxx11)
5493 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5496 /* This is for UTF-16 string constants. */
5497 char16_array_type_node
5498 = build_array_type (char16_type_node, array_domain_type);
5500 /* Define 'char32_t'. */
5501 char32_type_node = get_identifier (CHAR32_TYPE);
5502 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5503 char32_type_size = TYPE_PRECISION (char32_type_node);
5504 if (c_dialect_cxx ())
5506 char32_type_node = make_unsigned_type (char32_type_size);
5508 if (cxx_dialect >= cxx11)
5509 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5512 /* This is for UTF-32 string constants. */
5513 char32_array_type_node
5514 = build_array_type (char32_type_node, array_domain_type);
5516 wint_type_node =
5517 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5519 intmax_type_node =
5520 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5521 uintmax_type_node =
5522 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5524 if (SIG_ATOMIC_TYPE)
5525 sig_atomic_type_node =
5526 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5527 if (INT8_TYPE)
5528 int8_type_node =
5529 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5530 if (INT16_TYPE)
5531 int16_type_node =
5532 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5533 if (INT32_TYPE)
5534 int32_type_node =
5535 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5536 if (INT64_TYPE)
5537 int64_type_node =
5538 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5539 if (UINT8_TYPE)
5540 uint8_type_node =
5541 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5542 if (UINT16_TYPE)
5543 c_uint16_type_node = uint16_type_node =
5544 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5545 if (UINT32_TYPE)
5546 c_uint32_type_node = uint32_type_node =
5547 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5548 if (UINT64_TYPE)
5549 c_uint64_type_node = uint64_type_node =
5550 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5551 if (INT_LEAST8_TYPE)
5552 int_least8_type_node =
5553 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5554 if (INT_LEAST16_TYPE)
5555 int_least16_type_node =
5556 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5557 if (INT_LEAST32_TYPE)
5558 int_least32_type_node =
5559 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5560 if (INT_LEAST64_TYPE)
5561 int_least64_type_node =
5562 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5563 if (UINT_LEAST8_TYPE)
5564 uint_least8_type_node =
5565 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5566 if (UINT_LEAST16_TYPE)
5567 uint_least16_type_node =
5568 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5569 if (UINT_LEAST32_TYPE)
5570 uint_least32_type_node =
5571 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5572 if (UINT_LEAST64_TYPE)
5573 uint_least64_type_node =
5574 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5575 if (INT_FAST8_TYPE)
5576 int_fast8_type_node =
5577 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5578 if (INT_FAST16_TYPE)
5579 int_fast16_type_node =
5580 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5581 if (INT_FAST32_TYPE)
5582 int_fast32_type_node =
5583 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5584 if (INT_FAST64_TYPE)
5585 int_fast64_type_node =
5586 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5587 if (UINT_FAST8_TYPE)
5588 uint_fast8_type_node =
5589 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5590 if (UINT_FAST16_TYPE)
5591 uint_fast16_type_node =
5592 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5593 if (UINT_FAST32_TYPE)
5594 uint_fast32_type_node =
5595 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5596 if (UINT_FAST64_TYPE)
5597 uint_fast64_type_node =
5598 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5599 if (INTPTR_TYPE)
5600 intptr_type_node =
5601 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5602 if (UINTPTR_TYPE)
5603 uintptr_type_node =
5604 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5606 default_function_type
5607 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5608 ptrdiff_type_node
5609 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5610 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5612 lang_hooks.decls.pushdecl
5613 (build_decl (UNKNOWN_LOCATION,
5614 TYPE_DECL, get_identifier ("__builtin_va_list"),
5615 va_list_type_node));
5616 if (targetm.enum_va_list_p)
5618 int l;
5619 const char *pname;
5620 tree ptype;
5622 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5624 lang_hooks.decls.pushdecl
5625 (build_decl (UNKNOWN_LOCATION,
5626 TYPE_DECL, get_identifier (pname),
5627 ptype));
5632 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5634 va_list_arg_type_node = va_list_ref_type_node =
5635 build_pointer_type (TREE_TYPE (va_list_type_node));
5637 else
5639 va_list_arg_type_node = va_list_type_node;
5640 va_list_ref_type_node = build_reference_type (va_list_type_node);
5643 if (!flag_preprocess_only)
5644 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5646 main_identifier_node = get_identifier ("main");
5648 /* Create the built-in __null node. It is important that this is
5649 not shared. */
5650 null_node = make_int_cst (1, 1);
5651 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5653 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5654 memset (builtin_types, 0, sizeof (builtin_types));
5657 /* The number of named compound-literals generated thus far. */
5658 static GTY(()) int compound_literal_number;
5660 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5662 void
5663 set_compound_literal_name (tree decl)
5665 char *name;
5666 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5667 compound_literal_number);
5668 compound_literal_number++;
5669 DECL_NAME (decl) = get_identifier (name);
5672 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5673 TYPE and operand OP. */
5675 static tree
5676 build_va_arg_1 (location_t loc, tree type, tree op)
5678 tree expr = build1 (VA_ARG_EXPR, type, op);
5679 SET_EXPR_LOCATION (expr, loc);
5680 return expr;
5683 /* Return a VA_ARG_EXPR corresponding to a source-level expression
5684 va_arg (EXPR, TYPE) at source location LOC. */
5686 tree
5687 build_va_arg (location_t loc, tree expr, tree type)
5689 tree va_type = TREE_TYPE (expr);
5690 tree canon_va_type = (va_type == error_mark_node
5691 ? NULL_TREE
5692 : targetm.canonical_va_list_type (va_type));
5694 if (va_type == error_mark_node
5695 || canon_va_type == NULL_TREE)
5697 /* Let's handle things neutrallly, if expr:
5698 - has undeclared type, or
5699 - is not an va_list type. */
5700 return build_va_arg_1 (loc, type, expr);
5703 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5705 /* Case 1: Not an array type. */
5707 /* Take the address, to get '&ap'. */
5708 mark_addressable (expr);
5709 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5711 /* Verify that &ap is still recognized as having va_list type. */
5712 tree canon_expr_type
5713 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5714 gcc_assert (canon_expr_type != NULL_TREE);
5716 return build_va_arg_1 (loc, type, expr);
5719 /* Case 2: Array type.
5721 Background:
5723 For contrast, let's start with the simple case (case 1). If
5724 canon_va_type is not an array type, but say a char *, then when
5725 passing-by-value a va_list, the type of the va_list param decl is
5726 the same as for another va_list decl (all ap's are char *):
5728 f2_1 (char * ap)
5729 D.1815 = VA_ARG (&ap, 0B, 1);
5730 return D.1815;
5732 f2 (int i)
5733 char * ap.0;
5734 char * ap;
5735 __builtin_va_start (&ap, 0);
5736 ap.0 = ap;
5737 res = f2_1 (ap.0);
5738 __builtin_va_end (&ap);
5739 D.1812 = res;
5740 return D.1812;
5742 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5743 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5744 the same as for another va_list decl (case 2a, struct ap[1]).
5746 f2_1 (struct * ap)
5747 D.1844 = VA_ARG (ap, 0B, 0);
5748 return D.1844;
5750 f2 (int i)
5751 struct ap[1];
5752 __builtin_va_start (&ap, 0);
5753 res = f2_1 (&ap);
5754 __builtin_va_end (&ap);
5755 D.1841 = res;
5756 return D.1841;
5758 Case 2b is different because:
5759 - on the callee side, the parm decl has declared type va_list, but
5760 grokdeclarator changes the type of the parm decl to a pointer to the
5761 array elem type.
5762 - on the caller side, the pass-by-value uses &ap.
5764 We unify these two cases (case 2a: va_list is array type,
5765 case 2b: va_list is pointer to array elem type), by adding '&' for the
5766 array type case, such that we have a pointer to array elem in both
5767 cases. */
5769 if (TREE_CODE (va_type) == ARRAY_TYPE)
5771 /* Case 2a: va_list is array type. */
5773 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5774 elem type. */
5775 mark_addressable (expr);
5776 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5777 expr);
5779 /* Verify that &ap is still recognized as having va_list type. */
5780 tree canon_expr_type
5781 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5782 gcc_assert (canon_expr_type != NULL_TREE);
5784 else
5786 /* Case 2b: va_list is pointer to array elem type. */
5787 gcc_assert (POINTER_TYPE_P (va_type));
5788 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
5790 /* Don't take the address. We've already got '&ap'. */
5794 return build_va_arg_1 (loc, type, expr);
5798 /* Linked list of disabled built-in functions. */
5800 struct disabled_builtin
5802 const char *name;
5803 struct disabled_builtin *next;
5805 static disabled_builtin *disabled_builtins = NULL;
5807 static bool builtin_function_disabled_p (const char *);
5809 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5810 begins with "__builtin_", give an error. */
5812 void
5813 disable_builtin_function (const char *name)
5815 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5816 error ("cannot disable built-in function %qs", name);
5817 else
5819 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5820 new_disabled_builtin->name = name;
5821 new_disabled_builtin->next = disabled_builtins;
5822 disabled_builtins = new_disabled_builtin;
5827 /* Return true if the built-in function NAME has been disabled, false
5828 otherwise. */
5830 static bool
5831 builtin_function_disabled_p (const char *name)
5833 disabled_builtin *p;
5834 for (p = disabled_builtins; p != NULL; p = p->next)
5836 if (strcmp (name, p->name) == 0)
5837 return true;
5839 return false;
5843 /* Worker for DEF_BUILTIN.
5844 Possibly define a builtin function with one or two names.
5845 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5846 nonansi_p and flag_no_nonansi_builtin. */
5848 static void
5849 def_builtin_1 (enum built_in_function fncode,
5850 const char *name,
5851 enum built_in_class fnclass,
5852 tree fntype, tree libtype,
5853 bool both_p, bool fallback_p, bool nonansi_p,
5854 tree fnattrs, bool implicit_p)
5856 tree decl;
5857 const char *libname;
5859 if (fntype == error_mark_node)
5860 return;
5862 gcc_assert ((!both_p && !fallback_p)
5863 || !strncmp (name, "__builtin_",
5864 strlen ("__builtin_")));
5866 libname = name + strlen ("__builtin_");
5867 decl = add_builtin_function (name, fntype, fncode, fnclass,
5868 (fallback_p ? libname : NULL),
5869 fnattrs);
5871 set_builtin_decl (fncode, decl, implicit_p);
5873 if (both_p
5874 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5875 && !(nonansi_p && flag_no_nonansi_builtin))
5876 add_builtin_function (libname, libtype, fncode, fnclass,
5877 NULL, fnattrs);
5880 /* Nonzero if the type T promotes to int. This is (nearly) the
5881 integral promotions defined in ISO C99 6.3.1.1/2. */
5883 bool
5884 c_promoting_integer_type_p (const_tree t)
5886 switch (TREE_CODE (t))
5888 case INTEGER_TYPE:
5889 return (TYPE_MAIN_VARIANT (t) == char_type_node
5890 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5891 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5892 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5893 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5894 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5896 case ENUMERAL_TYPE:
5897 /* ??? Technically all enumerations not larger than an int
5898 promote to an int. But this is used along code paths
5899 that only want to notice a size change. */
5900 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5902 case BOOLEAN_TYPE:
5903 return 1;
5905 default:
5906 return 0;
5910 /* Return 1 if PARMS specifies a fixed number of parameters
5911 and none of their types is affected by default promotions. */
5914 self_promoting_args_p (const_tree parms)
5916 const_tree t;
5917 for (t = parms; t; t = TREE_CHAIN (t))
5919 tree type = TREE_VALUE (t);
5921 if (type == error_mark_node)
5922 continue;
5924 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5925 return 0;
5927 if (type == 0)
5928 return 0;
5930 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5931 return 0;
5933 if (c_promoting_integer_type_p (type))
5934 return 0;
5936 return 1;
5939 /* Recursively remove any '*' or '&' operator from TYPE. */
5940 tree
5941 strip_pointer_operator (tree t)
5943 while (POINTER_TYPE_P (t))
5944 t = TREE_TYPE (t);
5945 return t;
5948 /* Recursively remove pointer or array type from TYPE. */
5949 tree
5950 strip_pointer_or_array_types (tree t)
5952 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5953 t = TREE_TYPE (t);
5954 return t;
5957 /* Used to compare case labels. K1 and K2 are actually tree nodes
5958 representing case labels, or NULL_TREE for a `default' label.
5959 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5960 K2, and 0 if K1 and K2 are equal. */
5963 case_compare (splay_tree_key k1, splay_tree_key k2)
5965 /* Consider a NULL key (such as arises with a `default' label) to be
5966 smaller than anything else. */
5967 if (!k1)
5968 return k2 ? -1 : 0;
5969 else if (!k2)
5970 return k1 ? 1 : 0;
5972 return tree_int_cst_compare ((tree) k1, (tree) k2);
5975 /* Process a case label, located at LOC, for the range LOW_VALUE
5976 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5977 then this case label is actually a `default' label. If only
5978 HIGH_VALUE is NULL_TREE, then case label was declared using the
5979 usual C/C++ syntax, rather than the GNU case range extension.
5980 CASES is a tree containing all the case ranges processed so far;
5981 COND is the condition for the switch-statement itself.
5982 OUTSIDE_RANGE_P says whether there was a case value that doesn't
5983 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
5984 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
5986 tree
5987 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5988 tree low_value, tree high_value, bool *outside_range_p)
5990 tree type;
5991 tree label;
5992 tree case_label;
5993 splay_tree_node node;
5995 /* Create the LABEL_DECL itself. */
5996 label = create_artificial_label (loc);
5998 /* If there was an error processing the switch condition, bail now
5999 before we get more confused. */
6000 if (!cond || cond == error_mark_node)
6001 goto error_out;
6003 if ((low_value && TREE_TYPE (low_value)
6004 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6005 || (high_value && TREE_TYPE (high_value)
6006 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6008 error_at (loc, "pointers are not permitted as case values");
6009 goto error_out;
6012 /* Case ranges are a GNU extension. */
6013 if (high_value)
6014 pedwarn (loc, OPT_Wpedantic,
6015 "range expressions in switch statements are non-standard");
6017 type = TREE_TYPE (cond);
6018 if (low_value)
6020 low_value = check_case_value (loc, low_value);
6021 low_value = convert_and_check (loc, type, low_value);
6022 if (low_value == error_mark_node)
6023 goto error_out;
6025 if (high_value)
6027 high_value = check_case_value (loc, high_value);
6028 high_value = convert_and_check (loc, type, high_value);
6029 if (high_value == error_mark_node)
6030 goto error_out;
6033 if (low_value && high_value)
6035 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6036 really a case range, even though it was written that way.
6037 Remove the HIGH_VALUE to simplify later processing. */
6038 if (tree_int_cst_equal (low_value, high_value))
6039 high_value = NULL_TREE;
6040 else if (!tree_int_cst_lt (low_value, high_value))
6041 warning_at (loc, 0, "empty range specified");
6044 /* See if the case is in range of the type of the original testing
6045 expression. If both low_value and high_value are out of range,
6046 don't insert the case label and return NULL_TREE. */
6047 if (low_value
6048 && !check_case_bounds (loc, type, orig_type,
6049 &low_value, high_value ? &high_value : NULL,
6050 outside_range_p))
6051 return NULL_TREE;
6053 /* Look up the LOW_VALUE in the table of case labels we already
6054 have. */
6055 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6056 /* If there was not an exact match, check for overlapping ranges.
6057 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6058 that's a `default' label and the only overlap is an exact match. */
6059 if (!node && (low_value || high_value))
6061 splay_tree_node low_bound;
6062 splay_tree_node high_bound;
6064 /* Even though there wasn't an exact match, there might be an
6065 overlap between this case range and another case range.
6066 Since we've (inductively) not allowed any overlapping case
6067 ranges, we simply need to find the greatest low case label
6068 that is smaller that LOW_VALUE, and the smallest low case
6069 label that is greater than LOW_VALUE. If there is an overlap
6070 it will occur in one of these two ranges. */
6071 low_bound = splay_tree_predecessor (cases,
6072 (splay_tree_key) low_value);
6073 high_bound = splay_tree_successor (cases,
6074 (splay_tree_key) low_value);
6076 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6077 the LOW_VALUE, so there is no need to check unless the
6078 LOW_BOUND is in fact itself a case range. */
6079 if (low_bound
6080 && CASE_HIGH ((tree) low_bound->value)
6081 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6082 low_value) >= 0)
6083 node = low_bound;
6084 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6085 range is bigger than the low end of the current range, so we
6086 are only interested if the current range is a real range, and
6087 not an ordinary case label. */
6088 else if (high_bound
6089 && high_value
6090 && (tree_int_cst_compare ((tree) high_bound->key,
6091 high_value)
6092 <= 0))
6093 node = high_bound;
6095 /* If there was an overlap, issue an error. */
6096 if (node)
6098 tree duplicate = CASE_LABEL ((tree) node->value);
6100 if (high_value)
6102 error_at (loc, "duplicate (or overlapping) case value");
6103 error_at (DECL_SOURCE_LOCATION (duplicate),
6104 "this is the first entry overlapping that value");
6106 else if (low_value)
6108 error_at (loc, "duplicate case value") ;
6109 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6111 else
6113 error_at (loc, "multiple default labels in one switch");
6114 error_at (DECL_SOURCE_LOCATION (duplicate),
6115 "this is the first default label");
6117 goto error_out;
6120 /* Add a CASE_LABEL to the statement-tree. */
6121 case_label = add_stmt (build_case_label (low_value, high_value, label));
6122 /* Register this case label in the splay tree. */
6123 splay_tree_insert (cases,
6124 (splay_tree_key) low_value,
6125 (splay_tree_value) case_label);
6127 return case_label;
6129 error_out:
6130 /* Add a label so that the back-end doesn't think that the beginning of
6131 the switch is unreachable. Note that we do not add a case label, as
6132 that just leads to duplicates and thence to failure later on. */
6133 if (!cases->root)
6135 tree t = create_artificial_label (loc);
6136 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6138 return error_mark_node;
6141 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6142 Used to verify that case values match up with enumerator values. */
6144 static void
6145 match_case_to_enum_1 (tree key, tree type, tree label)
6147 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6149 if (tree_fits_uhwi_p (key))
6150 print_dec (key, buf, UNSIGNED);
6151 else if (tree_fits_shwi_p (key))
6152 print_dec (key, buf, SIGNED);
6153 else
6154 print_hex (key, buf);
6156 if (TYPE_NAME (type) == 0)
6157 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6158 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6159 "case value %qs not in enumerated type",
6160 buf);
6161 else
6162 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6163 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6164 "case value %qs not in enumerated type %qT",
6165 buf, type);
6168 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6169 Used to verify that case values match up with enumerator values. */
6171 static int
6172 match_case_to_enum (splay_tree_node node, void *data)
6174 tree label = (tree) node->value;
6175 tree type = (tree) data;
6177 /* Skip default case. */
6178 if (!CASE_LOW (label))
6179 return 0;
6181 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6182 when we did our enum->case scan. Reset our scratch bit after. */
6183 if (!CASE_LOW_SEEN (label))
6184 match_case_to_enum_1 (CASE_LOW (label), type, label);
6185 else
6186 CASE_LOW_SEEN (label) = 0;
6188 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6189 not set, that means that CASE_HIGH did not appear when we did our
6190 enum->case scan. Reset our scratch bit after. */
6191 if (CASE_HIGH (label))
6193 if (!CASE_HIGH_SEEN (label))
6194 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6195 else
6196 CASE_HIGH_SEEN (label) = 0;
6199 return 0;
6202 /* Handle -Wswitch*. Called from the front end after parsing the
6203 switch construct. */
6204 /* ??? Should probably be somewhere generic, since other languages
6205 besides C and C++ would want this. At the moment, however, C/C++
6206 are the only tree-ssa languages that support enumerations at all,
6207 so the point is moot. */
6209 void
6210 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6211 tree type, tree cond, bool bool_cond_p,
6212 bool outside_range_p)
6214 splay_tree_node default_node;
6215 splay_tree_node node;
6216 tree chain;
6218 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6219 && !warn_switch_bool)
6220 return;
6222 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6223 if (!default_node)
6224 warning_at (switch_location, OPT_Wswitch_default,
6225 "switch missing default case");
6227 /* There are certain cases where -Wswitch-bool warnings aren't
6228 desirable, such as
6229 switch (boolean)
6231 case true: ...
6232 case false: ...
6234 so be careful here. */
6235 if (warn_switch_bool && bool_cond_p)
6237 splay_tree_node min_node;
6238 /* If there's a default node, it's also the value with the minimal
6239 key. So look at the penultimate key (if any). */
6240 if (default_node)
6241 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6242 else
6243 min_node = splay_tree_min (cases);
6244 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6246 splay_tree_node max_node = splay_tree_max (cases);
6247 /* This might be a case range, so look at the value with the
6248 maximal key and then check CASE_HIGH. */
6249 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6250 if (max)
6251 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6253 /* If there's a case value > 1 or < 0, that is outside bool
6254 range, warn. */
6255 if (outside_range_p
6256 || (max && wi::gts_p (max, 1))
6257 || (min && wi::lts_p (min, 0))
6258 /* And handle the
6259 switch (boolean)
6261 case true: ...
6262 case false: ...
6263 default: ...
6265 case, where we want to warn. */
6266 || (default_node
6267 && max && wi::eq_p (max, 1)
6268 && min && wi::eq_p (min, 0)))
6269 warning_at (switch_location, OPT_Wswitch_bool,
6270 "switch condition has boolean value");
6273 /* From here on, we only care about enumerated types. */
6274 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6275 return;
6277 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6278 if (!warn_switch_enum && !warn_switch)
6279 return;
6281 /* Check the cases. Warn about case values which are not members of
6282 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6283 there is no default case, check that exactly all enumeration
6284 literals are covered by the cases. */
6286 /* Clearing COND if it is not an integer constant simplifies
6287 the tests inside the loop below. */
6288 if (TREE_CODE (cond) != INTEGER_CST)
6289 cond = NULL_TREE;
6291 /* The time complexity here is O(N*lg(N)) worst case, but for the
6292 common case of monotonically increasing enumerators, it is
6293 O(N), since the nature of the splay tree will keep the next
6294 element adjacent to the root at all times. */
6296 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6298 tree value = TREE_VALUE (chain);
6299 if (TREE_CODE (value) == CONST_DECL)
6300 value = DECL_INITIAL (value);
6301 node = splay_tree_lookup (cases, (splay_tree_key) value);
6302 if (node)
6304 /* Mark the CASE_LOW part of the case entry as seen. */
6305 tree label = (tree) node->value;
6306 CASE_LOW_SEEN (label) = 1;
6307 continue;
6310 /* Even though there wasn't an exact match, there might be a
6311 case range which includes the enumerator's value. */
6312 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6313 if (node && CASE_HIGH ((tree) node->value))
6315 tree label = (tree) node->value;
6316 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6317 if (cmp >= 0)
6319 /* If we match the upper bound exactly, mark the CASE_HIGH
6320 part of the case entry as seen. */
6321 if (cmp == 0)
6322 CASE_HIGH_SEEN (label) = 1;
6323 continue;
6327 /* We've now determined that this enumerated literal isn't
6328 handled by the case labels of the switch statement. */
6330 /* If the switch expression is a constant, we only really care
6331 about whether that constant is handled by the switch. */
6332 if (cond && tree_int_cst_compare (cond, value))
6333 continue;
6335 /* If there is a default_node, the only relevant option is
6336 Wswitch-enum. Otherwise, if both are enabled then we prefer
6337 to warn using -Wswitch because -Wswitch is enabled by -Wall
6338 while -Wswitch-enum is explicit. */
6339 warning_at (switch_location,
6340 (default_node || !warn_switch
6341 ? OPT_Wswitch_enum
6342 : OPT_Wswitch),
6343 "enumeration value %qE not handled in switch",
6344 TREE_PURPOSE (chain));
6347 /* Warn if there are case expressions that don't correspond to
6348 enumerators. This can occur since C and C++ don't enforce
6349 type-checking of assignments to enumeration variables.
6351 The time complexity here is now always O(N) worst case, since
6352 we should have marked both the lower bound and upper bound of
6353 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6354 above. This scan also resets those fields. */
6356 splay_tree_foreach (cases, match_case_to_enum, type);
6359 /* Finish an expression taking the address of LABEL (an
6360 IDENTIFIER_NODE). Returns an expression for the address.
6362 LOC is the location for the expression returned. */
6364 tree
6365 finish_label_address_expr (tree label, location_t loc)
6367 tree result;
6369 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6371 if (label == error_mark_node)
6372 return error_mark_node;
6374 label = lookup_label (label);
6375 if (label == NULL_TREE)
6376 result = null_pointer_node;
6377 else
6379 TREE_USED (label) = 1;
6380 result = build1 (ADDR_EXPR, ptr_type_node, label);
6381 /* The current function is not necessarily uninlinable.
6382 Computed gotos are incompatible with inlining, but the value
6383 here could be used only in a diagnostic, for example. */
6384 protected_set_expr_location (result, loc);
6387 return result;
6391 /* Given a boolean expression ARG, return a tree representing an increment
6392 or decrement (as indicated by CODE) of ARG. The front end must check for
6393 invalid cases (e.g., decrement in C++). */
6394 tree
6395 boolean_increment (enum tree_code code, tree arg)
6397 tree val;
6398 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6400 arg = stabilize_reference (arg);
6401 switch (code)
6403 case PREINCREMENT_EXPR:
6404 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6405 break;
6406 case POSTINCREMENT_EXPR:
6407 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6408 arg = save_expr (arg);
6409 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6410 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6411 break;
6412 case PREDECREMENT_EXPR:
6413 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6414 invert_truthvalue_loc (input_location, arg));
6415 break;
6416 case POSTDECREMENT_EXPR:
6417 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6418 invert_truthvalue_loc (input_location, arg));
6419 arg = save_expr (arg);
6420 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6421 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6422 break;
6423 default:
6424 gcc_unreachable ();
6426 TREE_SIDE_EFFECTS (val) = 1;
6427 return val;
6430 /* Built-in macros for stddef.h and stdint.h, that require macros
6431 defined in this file. */
6432 void
6433 c_stddef_cpp_builtins(void)
6435 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6436 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6437 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6438 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6439 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6440 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6441 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6442 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6443 if (SIG_ATOMIC_TYPE)
6444 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6445 if (INT8_TYPE)
6446 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6447 if (INT16_TYPE)
6448 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6449 if (INT32_TYPE)
6450 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6451 if (INT64_TYPE)
6452 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6453 if (UINT8_TYPE)
6454 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6455 if (UINT16_TYPE)
6456 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6457 if (UINT32_TYPE)
6458 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6459 if (UINT64_TYPE)
6460 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6461 if (INT_LEAST8_TYPE)
6462 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6463 if (INT_LEAST16_TYPE)
6464 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6465 if (INT_LEAST32_TYPE)
6466 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6467 if (INT_LEAST64_TYPE)
6468 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6469 if (UINT_LEAST8_TYPE)
6470 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6471 if (UINT_LEAST16_TYPE)
6472 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6473 if (UINT_LEAST32_TYPE)
6474 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6475 if (UINT_LEAST64_TYPE)
6476 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6477 if (INT_FAST8_TYPE)
6478 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6479 if (INT_FAST16_TYPE)
6480 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6481 if (INT_FAST32_TYPE)
6482 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6483 if (INT_FAST64_TYPE)
6484 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6485 if (UINT_FAST8_TYPE)
6486 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6487 if (UINT_FAST16_TYPE)
6488 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6489 if (UINT_FAST32_TYPE)
6490 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6491 if (UINT_FAST64_TYPE)
6492 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6493 if (INTPTR_TYPE)
6494 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6495 if (UINTPTR_TYPE)
6496 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6499 static void
6500 c_init_attributes (void)
6502 /* Fill in the built_in_attributes array. */
6503 #define DEF_ATTR_NULL_TREE(ENUM) \
6504 built_in_attributes[(int) ENUM] = NULL_TREE;
6505 #define DEF_ATTR_INT(ENUM, VALUE) \
6506 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6507 #define DEF_ATTR_STRING(ENUM, VALUE) \
6508 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6509 #define DEF_ATTR_IDENT(ENUM, STRING) \
6510 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6511 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6512 built_in_attributes[(int) ENUM] \
6513 = tree_cons (built_in_attributes[(int) PURPOSE], \
6514 built_in_attributes[(int) VALUE], \
6515 built_in_attributes[(int) CHAIN]);
6516 #include "builtin-attrs.def"
6517 #undef DEF_ATTR_NULL_TREE
6518 #undef DEF_ATTR_INT
6519 #undef DEF_ATTR_IDENT
6520 #undef DEF_ATTR_TREE_LIST
6523 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6524 identifier as an argument, so the front end shouldn't look it up. */
6526 bool
6527 attribute_takes_identifier_p (const_tree attr_id)
6529 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6530 if (spec == NULL)
6531 /* Unknown attribute that we'll end up ignoring, return true so we
6532 don't complain about an identifier argument. */
6533 return true;
6534 else if (!strcmp ("mode", spec->name)
6535 || !strcmp ("format", spec->name)
6536 || !strcmp ("cleanup", spec->name))
6537 return true;
6538 else
6539 return targetm.attribute_takes_identifier_p (attr_id);
6542 /* Attribute handlers common to C front ends. */
6544 /* Handle a "packed" attribute; arguments as in
6545 struct attribute_spec.handler. */
6547 static tree
6548 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6549 int flags, bool *no_add_attrs)
6551 if (TYPE_P (*node))
6553 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6554 *node = build_variant_type_copy (*node);
6555 TYPE_PACKED (*node) = 1;
6557 else if (TREE_CODE (*node) == FIELD_DECL)
6559 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6560 /* Still pack bitfields. */
6561 && ! DECL_INITIAL (*node))
6562 warning (OPT_Wattributes,
6563 "%qE attribute ignored for field of type %qT",
6564 name, TREE_TYPE (*node));
6565 else
6566 DECL_PACKED (*node) = 1;
6568 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6569 used for DECL_REGISTER. It wouldn't mean anything anyway.
6570 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6571 that changes what the typedef is typing. */
6572 else
6574 warning (OPT_Wattributes, "%qE attribute ignored", name);
6575 *no_add_attrs = true;
6578 return NULL_TREE;
6581 /* Handle a "nocommon" attribute; arguments as in
6582 struct attribute_spec.handler. */
6584 static tree
6585 handle_nocommon_attribute (tree *node, tree name,
6586 tree ARG_UNUSED (args),
6587 int ARG_UNUSED (flags), bool *no_add_attrs)
6589 if (VAR_P (*node))
6590 DECL_COMMON (*node) = 0;
6591 else
6593 warning (OPT_Wattributes, "%qE attribute ignored", name);
6594 *no_add_attrs = true;
6597 return NULL_TREE;
6600 /* Handle a "common" attribute; arguments as in
6601 struct attribute_spec.handler. */
6603 static tree
6604 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6605 int ARG_UNUSED (flags), bool *no_add_attrs)
6607 if (VAR_P (*node))
6608 DECL_COMMON (*node) = 1;
6609 else
6611 warning (OPT_Wattributes, "%qE attribute ignored", name);
6612 *no_add_attrs = true;
6615 return NULL_TREE;
6618 /* Handle a "noreturn" attribute; arguments as in
6619 struct attribute_spec.handler. */
6621 static tree
6622 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6623 int ARG_UNUSED (flags), bool *no_add_attrs)
6625 tree type = TREE_TYPE (*node);
6627 /* See FIXME comment in c_common_attribute_table. */
6628 if (TREE_CODE (*node) == FUNCTION_DECL
6629 || objc_method_decl (TREE_CODE (*node)))
6630 TREE_THIS_VOLATILE (*node) = 1;
6631 else if (TREE_CODE (type) == POINTER_TYPE
6632 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6633 TREE_TYPE (*node)
6634 = (build_qualified_type
6635 (build_pointer_type
6636 (build_type_variant (TREE_TYPE (type),
6637 TYPE_READONLY (TREE_TYPE (type)), 1)),
6638 TYPE_QUALS (type)));
6639 else
6641 warning (OPT_Wattributes, "%qE attribute ignored", name);
6642 *no_add_attrs = true;
6645 return NULL_TREE;
6648 /* Handle a "hot" and attribute; arguments as in
6649 struct attribute_spec.handler. */
6651 static tree
6652 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6653 int ARG_UNUSED (flags), bool *no_add_attrs)
6655 if (TREE_CODE (*node) == FUNCTION_DECL
6656 || TREE_CODE (*node) == LABEL_DECL)
6658 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6660 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6661 "with attribute %qs", name, "cold");
6662 *no_add_attrs = true;
6664 /* Most of the rest of the hot processing is done later with
6665 lookup_attribute. */
6667 else
6669 warning (OPT_Wattributes, "%qE attribute ignored", name);
6670 *no_add_attrs = true;
6673 return NULL_TREE;
6676 /* Handle a "cold" and attribute; arguments as in
6677 struct attribute_spec.handler. */
6679 static tree
6680 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6681 int ARG_UNUSED (flags), bool *no_add_attrs)
6683 if (TREE_CODE (*node) == FUNCTION_DECL
6684 || TREE_CODE (*node) == LABEL_DECL)
6686 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6688 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6689 "with attribute %qs", name, "hot");
6690 *no_add_attrs = true;
6692 /* Most of the rest of the cold processing is done later with
6693 lookup_attribute. */
6695 else
6697 warning (OPT_Wattributes, "%qE attribute ignored", name);
6698 *no_add_attrs = true;
6701 return NULL_TREE;
6704 /* Handle a "no_sanitize_address" attribute; arguments as in
6705 struct attribute_spec.handler. */
6707 static tree
6708 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6709 bool *no_add_attrs)
6711 if (TREE_CODE (*node) != FUNCTION_DECL)
6713 warning (OPT_Wattributes, "%qE attribute ignored", name);
6714 *no_add_attrs = true;
6717 return NULL_TREE;
6720 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6721 struct attribute_spec.handler. */
6723 static tree
6724 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6725 bool *no_add_attrs)
6727 if (TREE_CODE (*node) != FUNCTION_DECL)
6728 warning (OPT_Wattributes, "%qE attribute ignored", name);
6729 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6730 DECL_ATTRIBUTES (*node)
6731 = tree_cons (get_identifier ("no_sanitize_address"),
6732 NULL_TREE, DECL_ATTRIBUTES (*node));
6733 *no_add_attrs = true;
6734 return NULL_TREE;
6737 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6738 struct attribute_spec.handler. */
6740 static tree
6741 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6742 bool *no_add_attrs)
6744 if (TREE_CODE (*node) != FUNCTION_DECL)
6746 warning (OPT_Wattributes, "%qE attribute ignored", name);
6747 *no_add_attrs = true;
6750 return NULL_TREE;
6753 /* Handle a "stack_protect" attribute; arguments as in
6754 struct attribute_spec.handler. */
6755 static tree
6756 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6757 bool *no_add_attrs)
6759 if (TREE_CODE (*node) != FUNCTION_DECL)
6761 warning (OPT_Wattributes, "%qE attribute ignored", name);
6762 *no_add_attrs = true;
6764 else
6765 DECL_ATTRIBUTES (*node)
6766 = tree_cons (get_identifier ("stack_protect"),
6767 NULL_TREE, DECL_ATTRIBUTES (*node));
6769 return NULL_TREE;
6772 /* Handle a "noinline" attribute; arguments as in
6773 struct attribute_spec.handler. */
6775 static tree
6776 handle_noinline_attribute (tree *node, tree name,
6777 tree ARG_UNUSED (args),
6778 int ARG_UNUSED (flags), bool *no_add_attrs)
6780 if (TREE_CODE (*node) == FUNCTION_DECL)
6782 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6784 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6785 "with attribute %qs", name, "always_inline");
6786 *no_add_attrs = true;
6788 else
6789 DECL_UNINLINABLE (*node) = 1;
6791 else
6793 warning (OPT_Wattributes, "%qE attribute ignored", name);
6794 *no_add_attrs = true;
6797 return NULL_TREE;
6800 /* Handle a "noclone" attribute; arguments as in
6801 struct attribute_spec.handler. */
6803 static tree
6804 handle_noclone_attribute (tree *node, tree name,
6805 tree ARG_UNUSED (args),
6806 int ARG_UNUSED (flags), bool *no_add_attrs)
6808 if (TREE_CODE (*node) != FUNCTION_DECL)
6810 warning (OPT_Wattributes, "%qE attribute ignored", name);
6811 *no_add_attrs = true;
6814 return NULL_TREE;
6817 /* Handle a "no_icf" attribute; arguments as in
6818 struct attribute_spec.handler. */
6820 static tree
6821 handle_noicf_attribute (tree *node, tree name,
6822 tree ARG_UNUSED (args),
6823 int ARG_UNUSED (flags), bool *no_add_attrs)
6825 if (TREE_CODE (*node) != FUNCTION_DECL)
6827 warning (OPT_Wattributes, "%qE attribute ignored", name);
6828 *no_add_attrs = true;
6831 return NULL_TREE;
6835 /* Handle a "always_inline" attribute; arguments as in
6836 struct attribute_spec.handler. */
6838 static tree
6839 handle_always_inline_attribute (tree *node, tree name,
6840 tree ARG_UNUSED (args),
6841 int ARG_UNUSED (flags),
6842 bool *no_add_attrs)
6844 if (TREE_CODE (*node) == FUNCTION_DECL)
6846 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6848 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6849 "with %qs attribute", name, "noinline");
6850 *no_add_attrs = true;
6852 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
6854 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6855 "with %qs attribute", name, "target_clones");
6856 *no_add_attrs = true;
6858 else
6859 /* Set the attribute and mark it for disregarding inline
6860 limits. */
6861 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6863 else
6865 warning (OPT_Wattributes, "%qE attribute ignored", name);
6866 *no_add_attrs = true;
6869 return NULL_TREE;
6872 /* Handle a "gnu_inline" attribute; arguments as in
6873 struct attribute_spec.handler. */
6875 static tree
6876 handle_gnu_inline_attribute (tree *node, tree name,
6877 tree ARG_UNUSED (args),
6878 int ARG_UNUSED (flags),
6879 bool *no_add_attrs)
6881 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6883 /* Do nothing else, just set the attribute. We'll get at
6884 it later with lookup_attribute. */
6886 else
6888 warning (OPT_Wattributes, "%qE attribute ignored", name);
6889 *no_add_attrs = true;
6892 return NULL_TREE;
6895 /* Handle a "leaf" attribute; arguments as in
6896 struct attribute_spec.handler. */
6898 static tree
6899 handle_leaf_attribute (tree *node, tree name,
6900 tree ARG_UNUSED (args),
6901 int ARG_UNUSED (flags), bool *no_add_attrs)
6903 if (TREE_CODE (*node) != FUNCTION_DECL)
6905 warning (OPT_Wattributes, "%qE attribute ignored", name);
6906 *no_add_attrs = true;
6908 if (!TREE_PUBLIC (*node))
6910 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6911 *no_add_attrs = true;
6914 return NULL_TREE;
6917 /* Handle an "artificial" attribute; arguments as in
6918 struct attribute_spec.handler. */
6920 static tree
6921 handle_artificial_attribute (tree *node, tree name,
6922 tree ARG_UNUSED (args),
6923 int ARG_UNUSED (flags),
6924 bool *no_add_attrs)
6926 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6928 /* Do nothing else, just set the attribute. We'll get at
6929 it later with lookup_attribute. */
6931 else
6933 warning (OPT_Wattributes, "%qE attribute ignored", name);
6934 *no_add_attrs = true;
6937 return NULL_TREE;
6940 /* Handle a "flatten" attribute; arguments as in
6941 struct attribute_spec.handler. */
6943 static tree
6944 handle_flatten_attribute (tree *node, tree name,
6945 tree args ATTRIBUTE_UNUSED,
6946 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6948 if (TREE_CODE (*node) == FUNCTION_DECL)
6949 /* Do nothing else, just set the attribute. We'll get at
6950 it later with lookup_attribute. */
6952 else
6954 warning (OPT_Wattributes, "%qE attribute ignored", name);
6955 *no_add_attrs = true;
6958 return NULL_TREE;
6961 /* Handle a "warning" or "error" attribute; arguments as in
6962 struct attribute_spec.handler. */
6964 static tree
6965 handle_error_attribute (tree *node, tree name, tree args,
6966 int ARG_UNUSED (flags), bool *no_add_attrs)
6968 if (TREE_CODE (*node) == FUNCTION_DECL
6969 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6970 /* Do nothing else, just set the attribute. We'll get at
6971 it later with lookup_attribute. */
6973 else
6975 warning (OPT_Wattributes, "%qE attribute ignored", name);
6976 *no_add_attrs = true;
6979 return NULL_TREE;
6982 /* Handle a "used" attribute; arguments as in
6983 struct attribute_spec.handler. */
6985 static tree
6986 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6987 int ARG_UNUSED (flags), bool *no_add_attrs)
6989 tree node = *pnode;
6991 if (TREE_CODE (node) == FUNCTION_DECL
6992 || (VAR_P (node) && TREE_STATIC (node))
6993 || (TREE_CODE (node) == TYPE_DECL))
6995 TREE_USED (node) = 1;
6996 DECL_PRESERVE_P (node) = 1;
6997 if (VAR_P (node))
6998 DECL_READ_P (node) = 1;
7000 else
7002 warning (OPT_Wattributes, "%qE attribute ignored", name);
7003 *no_add_attrs = true;
7006 return NULL_TREE;
7009 /* Handle a "unused" attribute; arguments as in
7010 struct attribute_spec.handler. */
7012 static tree
7013 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7014 int flags, bool *no_add_attrs)
7016 if (DECL_P (*node))
7018 tree decl = *node;
7020 if (TREE_CODE (decl) == PARM_DECL
7021 || VAR_OR_FUNCTION_DECL_P (decl)
7022 || TREE_CODE (decl) == LABEL_DECL
7023 || TREE_CODE (decl) == TYPE_DECL)
7025 TREE_USED (decl) = 1;
7026 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
7027 DECL_READ_P (decl) = 1;
7029 else
7031 warning (OPT_Wattributes, "%qE attribute ignored", name);
7032 *no_add_attrs = true;
7035 else
7037 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7038 *node = build_variant_type_copy (*node);
7039 TREE_USED (*node) = 1;
7042 return NULL_TREE;
7045 /* Handle a "externally_visible" attribute; arguments as in
7046 struct attribute_spec.handler. */
7048 static tree
7049 handle_externally_visible_attribute (tree *pnode, tree name,
7050 tree ARG_UNUSED (args),
7051 int ARG_UNUSED (flags),
7052 bool *no_add_attrs)
7054 tree node = *pnode;
7056 if (VAR_OR_FUNCTION_DECL_P (node))
7058 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7059 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7061 warning (OPT_Wattributes,
7062 "%qE attribute have effect only on public objects", name);
7063 *no_add_attrs = true;
7066 else
7068 warning (OPT_Wattributes, "%qE attribute ignored", name);
7069 *no_add_attrs = true;
7072 return NULL_TREE;
7075 /* Handle the "no_reorder" attribute. Arguments as in
7076 struct attribute_spec.handler. */
7078 static tree
7079 handle_no_reorder_attribute (tree *pnode,
7080 tree name,
7081 tree,
7082 int,
7083 bool *no_add_attrs)
7085 tree node = *pnode;
7087 if (!VAR_OR_FUNCTION_DECL_P (node)
7088 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7090 warning (OPT_Wattributes,
7091 "%qE attribute only affects top level objects",
7092 name);
7093 *no_add_attrs = true;
7096 return NULL_TREE;
7099 /* Handle a "const" attribute; arguments as in
7100 struct attribute_spec.handler. */
7102 static tree
7103 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7104 int ARG_UNUSED (flags), bool *no_add_attrs)
7106 tree type = TREE_TYPE (*node);
7108 /* See FIXME comment on noreturn in c_common_attribute_table. */
7109 if (TREE_CODE (*node) == FUNCTION_DECL)
7110 TREE_READONLY (*node) = 1;
7111 else if (TREE_CODE (type) == POINTER_TYPE
7112 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7113 TREE_TYPE (*node)
7114 = (build_qualified_type
7115 (build_pointer_type
7116 (build_type_variant (TREE_TYPE (type), 1,
7117 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7118 TYPE_QUALS (type)));
7119 else
7121 warning (OPT_Wattributes, "%qE attribute ignored", name);
7122 *no_add_attrs = true;
7125 return NULL_TREE;
7128 /* Handle a "scalar_storage_order" attribute; arguments as in
7129 struct attribute_spec.handler. */
7131 static tree
7132 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7133 int flags, bool *no_add_attrs)
7135 tree id = TREE_VALUE (args);
7136 tree type;
7138 if (TREE_CODE (*node) == TYPE_DECL
7139 && ! (flags & ATTR_FLAG_CXX11))
7140 node = &TREE_TYPE (*node);
7141 type = *node;
7143 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7145 error ("scalar_storage_order is not supported because endianness "
7146 "is not uniform");
7147 return NULL_TREE;
7150 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7152 bool reverse = false;
7154 if (TREE_CODE (id) == STRING_CST
7155 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7156 reverse = !BYTES_BIG_ENDIAN;
7157 else if (TREE_CODE (id) == STRING_CST
7158 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7159 reverse = BYTES_BIG_ENDIAN;
7160 else
7162 error ("scalar_storage_order argument must be one of \"big-endian\""
7163 " or \"little-endian\"");
7164 return NULL_TREE;
7167 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7169 if (reverse)
7170 /* A type variant isn't good enough, since we don't want a cast
7171 to such a type to be removed as a no-op. */
7172 *node = type = build_duplicate_type (type);
7175 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7176 return NULL_TREE;
7179 warning (OPT_Wattributes, "%qE attribute ignored", name);
7180 *no_add_attrs = true;
7181 return NULL_TREE;
7184 /* Handle a "transparent_union" attribute; arguments as in
7185 struct attribute_spec.handler. */
7187 static tree
7188 handle_transparent_union_attribute (tree *node, tree name,
7189 tree ARG_UNUSED (args), int flags,
7190 bool *no_add_attrs)
7192 tree type;
7194 *no_add_attrs = true;
7196 if (TREE_CODE (*node) == TYPE_DECL
7197 && ! (flags & ATTR_FLAG_CXX11))
7198 node = &TREE_TYPE (*node);
7199 type = *node;
7201 if (TREE_CODE (type) == UNION_TYPE)
7203 /* Make sure that the first field will work for a transparent union.
7204 If the type isn't complete yet, leave the check to the code in
7205 finish_struct. */
7206 if (TYPE_SIZE (type))
7208 tree first = first_field (type);
7209 if (first == NULL_TREE
7210 || DECL_ARTIFICIAL (first)
7211 || TYPE_MODE (type) != DECL_MODE (first))
7212 goto ignored;
7215 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7217 /* If the type isn't complete yet, setting the flag
7218 on a variant wouldn't ever be checked. */
7219 if (!TYPE_SIZE (type))
7220 goto ignored;
7222 /* build_duplicate_type doesn't work for C++. */
7223 if (c_dialect_cxx ())
7224 goto ignored;
7226 /* A type variant isn't good enough, since we don't want a cast
7227 to such a type to be removed as a no-op. */
7228 *node = type = build_duplicate_type (type);
7231 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7232 TYPE_TRANSPARENT_AGGR (t) = 1;
7233 return NULL_TREE;
7236 ignored:
7237 warning (OPT_Wattributes, "%qE attribute ignored", name);
7238 return NULL_TREE;
7241 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7242 get the requested priority for a constructor or destructor,
7243 possibly issuing diagnostics for invalid or reserved
7244 priorities. */
7246 static priority_type
7247 get_priority (tree args, bool is_destructor)
7249 HOST_WIDE_INT pri;
7250 tree arg;
7252 if (!args)
7253 return DEFAULT_INIT_PRIORITY;
7255 if (!SUPPORTS_INIT_PRIORITY)
7257 if (is_destructor)
7258 error ("destructor priorities are not supported");
7259 else
7260 error ("constructor priorities are not supported");
7261 return DEFAULT_INIT_PRIORITY;
7264 arg = TREE_VALUE (args);
7265 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7266 goto invalid;
7267 if (arg == error_mark_node)
7268 return DEFAULT_INIT_PRIORITY;
7269 arg = default_conversion (arg);
7270 if (!tree_fits_shwi_p (arg)
7271 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7272 goto invalid;
7274 pri = tree_to_shwi (arg);
7275 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7276 goto invalid;
7278 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7280 if (is_destructor)
7281 warning (0,
7282 "destructor priorities from 0 to %d are reserved "
7283 "for the implementation",
7284 MAX_RESERVED_INIT_PRIORITY);
7285 else
7286 warning (0,
7287 "constructor priorities from 0 to %d are reserved "
7288 "for the implementation",
7289 MAX_RESERVED_INIT_PRIORITY);
7291 return pri;
7293 invalid:
7294 if (is_destructor)
7295 error ("destructor priorities must be integers from 0 to %d inclusive",
7296 MAX_INIT_PRIORITY);
7297 else
7298 error ("constructor priorities must be integers from 0 to %d inclusive",
7299 MAX_INIT_PRIORITY);
7300 return DEFAULT_INIT_PRIORITY;
7303 /* Handle a "constructor" attribute; arguments as in
7304 struct attribute_spec.handler. */
7306 static tree
7307 handle_constructor_attribute (tree *node, tree name, tree args,
7308 int ARG_UNUSED (flags),
7309 bool *no_add_attrs)
7311 tree decl = *node;
7312 tree type = TREE_TYPE (decl);
7314 if (TREE_CODE (decl) == FUNCTION_DECL
7315 && TREE_CODE (type) == FUNCTION_TYPE
7316 && decl_function_context (decl) == 0)
7318 priority_type priority;
7319 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7320 priority = get_priority (args, /*is_destructor=*/false);
7321 SET_DECL_INIT_PRIORITY (decl, priority);
7322 TREE_USED (decl) = 1;
7324 else
7326 warning (OPT_Wattributes, "%qE attribute ignored", name);
7327 *no_add_attrs = true;
7330 return NULL_TREE;
7333 /* Handle a "destructor" attribute; arguments as in
7334 struct attribute_spec.handler. */
7336 static tree
7337 handle_destructor_attribute (tree *node, tree name, tree args,
7338 int ARG_UNUSED (flags),
7339 bool *no_add_attrs)
7341 tree decl = *node;
7342 tree type = TREE_TYPE (decl);
7344 if (TREE_CODE (decl) == FUNCTION_DECL
7345 && TREE_CODE (type) == FUNCTION_TYPE
7346 && decl_function_context (decl) == 0)
7348 priority_type priority;
7349 DECL_STATIC_DESTRUCTOR (decl) = 1;
7350 priority = get_priority (args, /*is_destructor=*/true);
7351 SET_DECL_FINI_PRIORITY (decl, priority);
7352 TREE_USED (decl) = 1;
7354 else
7356 warning (OPT_Wattributes, "%qE attribute ignored", name);
7357 *no_add_attrs = true;
7360 return NULL_TREE;
7363 /* Nonzero if the mode is a valid vector mode for this architecture.
7364 This returns nonzero even if there is no hardware support for the
7365 vector mode, but we can emulate with narrower modes. */
7367 static int
7368 vector_mode_valid_p (machine_mode mode)
7370 enum mode_class mclass = GET_MODE_CLASS (mode);
7371 machine_mode innermode;
7373 /* Doh! What's going on? */
7374 if (mclass != MODE_VECTOR_INT
7375 && mclass != MODE_VECTOR_FLOAT
7376 && mclass != MODE_VECTOR_FRACT
7377 && mclass != MODE_VECTOR_UFRACT
7378 && mclass != MODE_VECTOR_ACCUM
7379 && mclass != MODE_VECTOR_UACCUM)
7380 return 0;
7382 /* Hardware support. Woo hoo! */
7383 if (targetm.vector_mode_supported_p (mode))
7384 return 1;
7386 innermode = GET_MODE_INNER (mode);
7388 /* We should probably return 1 if requesting V4DI and we have no DI,
7389 but we have V2DI, but this is probably very unlikely. */
7391 /* If we have support for the inner mode, we can safely emulate it.
7392 We may not have V2DI, but me can emulate with a pair of DIs. */
7393 return targetm.scalar_mode_supported_p (innermode);
7397 /* Handle a "mode" attribute; arguments as in
7398 struct attribute_spec.handler. */
7400 static tree
7401 handle_mode_attribute (tree *node, tree name, tree args,
7402 int ARG_UNUSED (flags), bool *no_add_attrs)
7404 tree type = *node;
7405 tree ident = TREE_VALUE (args);
7407 *no_add_attrs = true;
7409 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7410 warning (OPT_Wattributes, "%qE attribute ignored", name);
7411 else
7413 int j;
7414 const char *p = IDENTIFIER_POINTER (ident);
7415 int len = strlen (p);
7416 machine_mode mode = VOIDmode;
7417 tree typefm;
7418 bool valid_mode;
7420 if (len > 4 && p[0] == '_' && p[1] == '_'
7421 && p[len - 1] == '_' && p[len - 2] == '_')
7423 char *newp = (char *) alloca (len - 1);
7425 strcpy (newp, &p[2]);
7426 newp[len - 4] = '\0';
7427 p = newp;
7430 /* Change this type to have a type with the specified mode.
7431 First check for the special modes. */
7432 if (!strcmp (p, "byte"))
7433 mode = byte_mode;
7434 else if (!strcmp (p, "word"))
7435 mode = word_mode;
7436 else if (!strcmp (p, "pointer"))
7437 mode = ptr_mode;
7438 else if (!strcmp (p, "libgcc_cmp_return"))
7439 mode = targetm.libgcc_cmp_return_mode ();
7440 else if (!strcmp (p, "libgcc_shift_count"))
7441 mode = targetm.libgcc_shift_count_mode ();
7442 else if (!strcmp (p, "unwind_word"))
7443 mode = targetm.unwind_word_mode ();
7444 else
7445 for (j = 0; j < NUM_MACHINE_MODES; j++)
7446 if (!strcmp (p, GET_MODE_NAME (j)))
7448 mode = (machine_mode) j;
7449 break;
7452 if (mode == VOIDmode)
7454 error ("unknown machine mode %qE", ident);
7455 return NULL_TREE;
7458 valid_mode = false;
7459 switch (GET_MODE_CLASS (mode))
7461 case MODE_INT:
7462 case MODE_PARTIAL_INT:
7463 case MODE_FLOAT:
7464 case MODE_DECIMAL_FLOAT:
7465 case MODE_FRACT:
7466 case MODE_UFRACT:
7467 case MODE_ACCUM:
7468 case MODE_UACCUM:
7469 valid_mode = targetm.scalar_mode_supported_p (mode);
7470 break;
7472 case MODE_COMPLEX_INT:
7473 case MODE_COMPLEX_FLOAT:
7474 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7475 break;
7477 case MODE_VECTOR_INT:
7478 case MODE_VECTOR_FLOAT:
7479 case MODE_VECTOR_FRACT:
7480 case MODE_VECTOR_UFRACT:
7481 case MODE_VECTOR_ACCUM:
7482 case MODE_VECTOR_UACCUM:
7483 warning (OPT_Wattributes, "specifying vector types with "
7484 "__attribute__ ((mode)) is deprecated");
7485 warning (OPT_Wattributes,
7486 "use __attribute__ ((vector_size)) instead");
7487 valid_mode = vector_mode_valid_p (mode);
7488 break;
7490 default:
7491 break;
7493 if (!valid_mode)
7495 error ("unable to emulate %qs", p);
7496 return NULL_TREE;
7499 if (POINTER_TYPE_P (type))
7501 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7502 tree (*fn)(tree, machine_mode, bool);
7504 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7506 error ("invalid pointer mode %qs", p);
7507 return NULL_TREE;
7510 if (TREE_CODE (type) == POINTER_TYPE)
7511 fn = build_pointer_type_for_mode;
7512 else
7513 fn = build_reference_type_for_mode;
7514 typefm = fn (TREE_TYPE (type), mode, false);
7516 else
7518 /* For fixed-point modes, we need to test if the signness of type
7519 and the machine mode are consistent. */
7520 if (ALL_FIXED_POINT_MODE_P (mode)
7521 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7523 error ("signedness of type and machine mode %qs don%'t match", p);
7524 return NULL_TREE;
7526 /* For fixed-point modes, we need to pass saturating info. */
7527 typefm = lang_hooks.types.type_for_mode (mode,
7528 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7529 : TYPE_UNSIGNED (type));
7532 if (typefm == NULL_TREE)
7534 error ("no data type for mode %qs", p);
7535 return NULL_TREE;
7537 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7539 /* For enumeral types, copy the precision from the integer
7540 type returned above. If not an INTEGER_TYPE, we can't use
7541 this mode for this type. */
7542 if (TREE_CODE (typefm) != INTEGER_TYPE)
7544 error ("cannot use mode %qs for enumeral types", p);
7545 return NULL_TREE;
7548 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7550 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7551 typefm = type;
7553 else
7555 /* We cannot build a type variant, as there's code that assumes
7556 that TYPE_MAIN_VARIANT has the same mode. This includes the
7557 debug generators. Instead, create a subrange type. This
7558 results in all of the enumeral values being emitted only once
7559 in the original, and the subtype gets them by reference. */
7560 if (TYPE_UNSIGNED (type))
7561 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7562 else
7563 typefm = make_signed_type (TYPE_PRECISION (typefm));
7564 TREE_TYPE (typefm) = type;
7567 else if (VECTOR_MODE_P (mode)
7568 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7569 : TREE_CODE (type) != TREE_CODE (typefm))
7571 error ("mode %qs applied to inappropriate type", p);
7572 return NULL_TREE;
7575 *node = typefm;
7578 return NULL_TREE;
7581 /* Handle a "section" attribute; arguments as in
7582 struct attribute_spec.handler. */
7584 static tree
7585 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7586 int ARG_UNUSED (flags), bool *no_add_attrs)
7588 tree decl = *node;
7590 if (!targetm_common.have_named_sections)
7592 error_at (DECL_SOURCE_LOCATION (*node),
7593 "section attributes are not supported for this target");
7594 goto fail;
7597 user_defined_section_attribute = true;
7599 if (!VAR_OR_FUNCTION_DECL_P (decl))
7601 error ("section attribute not allowed for %q+D", *node);
7602 goto fail;
7605 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7607 error ("section attribute argument not a string constant");
7608 goto fail;
7611 if (VAR_P (decl)
7612 && current_function_decl != NULL_TREE
7613 && !TREE_STATIC (decl))
7615 error_at (DECL_SOURCE_LOCATION (decl),
7616 "section attribute cannot be specified for local variables");
7617 goto fail;
7620 /* The decl may have already been given a section attribute
7621 from a previous declaration. Ensure they match. */
7622 if (DECL_SECTION_NAME (decl) != NULL
7623 && strcmp (DECL_SECTION_NAME (decl),
7624 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7626 error ("section of %q+D conflicts with previous declaration", *node);
7627 goto fail;
7630 if (VAR_P (decl)
7631 && !targetm.have_tls && targetm.emutls.tmpl_section
7632 && DECL_THREAD_LOCAL_P (decl))
7634 error ("section of %q+D cannot be overridden", *node);
7635 goto fail;
7638 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7639 return NULL_TREE;
7641 fail:
7642 *no_add_attrs = true;
7643 return NULL_TREE;
7646 /* Check whether ALIGN is a valid user-specified alignment. If so,
7647 return its base-2 log; if not, output an error and return -1. If
7648 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7649 no error. */
7651 check_user_alignment (const_tree align, bool allow_zero)
7653 int i;
7655 if (error_operand_p (align))
7656 return -1;
7657 if (TREE_CODE (align) != INTEGER_CST
7658 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7660 error ("requested alignment is not an integer constant");
7661 return -1;
7663 else if (allow_zero && integer_zerop (align))
7664 return -1;
7665 else if (tree_int_cst_sgn (align) == -1
7666 || (i = tree_log2 (align)) == -1)
7668 error ("requested alignment is not a positive power of 2");
7669 return -1;
7671 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7673 error ("requested alignment is too large");
7674 return -1;
7676 return i;
7680 If in c++-11, check if the c++-11 alignment constraint with respect
7681 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7682 c++-11 mode, does nothing.
7684 [dcl.align]2/ says:
7686 [* if the constant expression evaluates to a fundamental alignment,
7687 the alignment requirement of the declared entity shall be the
7688 specified fundamental alignment.
7690 * if the constant expression evaluates to an extended alignment
7691 and the implementation supports that alignment in the context
7692 of the declaration, the alignment of the declared entity shall
7693 be that alignment
7695 * if the constant expression evaluates to an extended alignment
7696 and the implementation does not support that alignment in the
7697 context of the declaration, the program is ill-formed]. */
7699 static bool
7700 check_cxx_fundamental_alignment_constraints (tree node,
7701 unsigned align_log,
7702 int flags)
7704 bool alignment_too_large_p = false;
7705 unsigned requested_alignment = 1U << align_log;
7706 unsigned max_align = 0;
7708 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7709 || (node == NULL_TREE || node == error_mark_node))
7710 return true;
7712 if (cxx_fundamental_alignment_p (requested_alignment))
7713 return true;
7715 if (DECL_P (node))
7717 if (TREE_STATIC (node))
7719 /* For file scope variables and static members, the target
7720 supports alignments that are at most
7721 MAX_OFILE_ALIGNMENT. */
7722 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7723 alignment_too_large_p = true;
7725 else
7727 #ifdef BIGGEST_FIELD_ALIGNMENT
7728 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7729 #else
7730 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7731 #endif
7732 /* For non-static members, the target supports either
7733 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7734 if it is defined or BIGGEST_ALIGNMENT. */
7735 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7736 if (TREE_CODE (node) == FIELD_DECL
7737 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7738 alignment_too_large_p = true;
7739 #undef MAX_TARGET_FIELD_ALIGNMENT
7740 /* For stack variables, the target supports at most
7741 MAX_STACK_ALIGNMENT. */
7742 else if (decl_function_context (node) != NULL
7743 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7744 alignment_too_large_p = true;
7747 else if (TYPE_P (node))
7749 /* Let's be liberal for types. */
7750 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7751 alignment_too_large_p = true;
7754 if (alignment_too_large_p)
7755 pedwarn (input_location, OPT_Wattributes,
7756 "requested alignment %d is larger than %d",
7757 requested_alignment, max_align);
7759 return !alignment_too_large_p;
7762 /* Handle a "aligned" attribute; arguments as in
7763 struct attribute_spec.handler. */
7765 static tree
7766 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7767 int flags, bool *no_add_attrs)
7769 tree decl = NULL_TREE;
7770 tree *type = NULL;
7771 int is_type = 0;
7772 tree align_expr;
7773 int i;
7775 if (args)
7777 align_expr = TREE_VALUE (args);
7778 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7779 && TREE_CODE (align_expr) != FUNCTION_DECL)
7780 align_expr = default_conversion (align_expr);
7782 else
7783 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7785 if (DECL_P (*node))
7787 decl = *node;
7788 type = &TREE_TYPE (decl);
7789 is_type = TREE_CODE (*node) == TYPE_DECL;
7791 else if (TYPE_P (*node))
7792 type = node, is_type = 1;
7794 if ((i = check_user_alignment (align_expr, false)) == -1
7795 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7796 *no_add_attrs = true;
7797 else if (is_type)
7799 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7800 /* OK, modify the type in place. */;
7801 /* If we have a TYPE_DECL, then copy the type, so that we
7802 don't accidentally modify a builtin type. See pushdecl. */
7803 else if (decl && TREE_TYPE (decl) != error_mark_node
7804 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7806 tree tt = TREE_TYPE (decl);
7807 *type = build_variant_type_copy (*type);
7808 DECL_ORIGINAL_TYPE (decl) = tt;
7809 TYPE_NAME (*type) = decl;
7810 TREE_USED (*type) = TREE_USED (decl);
7811 TREE_TYPE (decl) = *type;
7813 else
7814 *type = build_variant_type_copy (*type);
7816 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7817 TYPE_USER_ALIGN (*type) = 1;
7819 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7820 && TREE_CODE (decl) != FIELD_DECL)
7822 error ("alignment may not be specified for %q+D", decl);
7823 *no_add_attrs = true;
7825 else if (DECL_USER_ALIGN (decl)
7826 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7827 /* C++-11 [dcl.align/4]:
7829 When multiple alignment-specifiers are specified for an
7830 entity, the alignment requirement shall be set to the
7831 strictest specified alignment.
7833 This formally comes from the c++11 specification but we are
7834 doing it for the GNU attribute syntax as well. */
7835 *no_add_attrs = true;
7836 else if (TREE_CODE (decl) == FUNCTION_DECL
7837 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7839 if (DECL_USER_ALIGN (decl))
7840 error ("alignment for %q+D was previously specified as %d "
7841 "and may not be decreased", decl,
7842 DECL_ALIGN (decl) / BITS_PER_UNIT);
7843 else
7844 error ("alignment for %q+D must be at least %d", decl,
7845 DECL_ALIGN (decl) / BITS_PER_UNIT);
7846 *no_add_attrs = true;
7848 else
7850 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7851 DECL_USER_ALIGN (decl) = 1;
7854 return NULL_TREE;
7857 /* Handle a "weak" attribute; arguments as in
7858 struct attribute_spec.handler. */
7860 static tree
7861 handle_weak_attribute (tree *node, tree name,
7862 tree ARG_UNUSED (args),
7863 int ARG_UNUSED (flags),
7864 bool * ARG_UNUSED (no_add_attrs))
7866 if (TREE_CODE (*node) == FUNCTION_DECL
7867 && DECL_DECLARED_INLINE_P (*node))
7869 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7870 *no_add_attrs = true;
7872 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7874 error ("indirect function %q+D cannot be declared weak", *node);
7875 *no_add_attrs = true;
7876 return NULL_TREE;
7878 else if (VAR_OR_FUNCTION_DECL_P (*node))
7879 declare_weak (*node);
7880 else
7881 warning (OPT_Wattributes, "%qE attribute ignored", name);
7883 return NULL_TREE;
7886 /* Handle a "noplt" attribute; arguments as in
7887 struct attribute_spec.handler. */
7889 static tree
7890 handle_noplt_attribute (tree *node, tree name,
7891 tree ARG_UNUSED (args),
7892 int ARG_UNUSED (flags),
7893 bool * ARG_UNUSED (no_add_attrs))
7895 if (TREE_CODE (*node) != FUNCTION_DECL)
7897 warning (OPT_Wattributes,
7898 "%qE attribute is only applicable on functions", name);
7899 *no_add_attrs = true;
7900 return NULL_TREE;
7902 return NULL_TREE;
7905 /* Handle an "alias" or "ifunc" attribute; arguments as in
7906 struct attribute_spec.handler, except that IS_ALIAS tells us
7907 whether this is an alias as opposed to ifunc attribute. */
7909 static tree
7910 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7911 bool *no_add_attrs)
7913 tree decl = *node;
7915 if (TREE_CODE (decl) != FUNCTION_DECL
7916 && (!is_alias || !VAR_P (decl)))
7918 warning (OPT_Wattributes, "%qE attribute ignored", name);
7919 *no_add_attrs = true;
7921 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7922 || (TREE_CODE (decl) != FUNCTION_DECL
7923 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7924 /* A static variable declaration is always a tentative definition,
7925 but the alias is a non-tentative definition which overrides. */
7926 || (TREE_CODE (decl) != FUNCTION_DECL
7927 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7929 error ("%q+D defined both normally and as %qE attribute", decl, name);
7930 *no_add_attrs = true;
7931 return NULL_TREE;
7933 else if (!is_alias
7934 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7935 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7937 error ("weak %q+D cannot be defined %qE", decl, name);
7938 *no_add_attrs = true;
7939 return NULL_TREE;
7942 /* Note that the very first time we process a nested declaration,
7943 decl_function_context will not be set. Indeed, *would* never
7944 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7945 we do below. After such frobbery, pushdecl would set the context.
7946 In any case, this is never what we want. */
7947 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7949 tree id;
7951 id = TREE_VALUE (args);
7952 if (TREE_CODE (id) != STRING_CST)
7954 error ("attribute %qE argument not a string", name);
7955 *no_add_attrs = true;
7956 return NULL_TREE;
7958 id = get_identifier (TREE_STRING_POINTER (id));
7959 /* This counts as a use of the object pointed to. */
7960 TREE_USED (id) = 1;
7962 if (TREE_CODE (decl) == FUNCTION_DECL)
7963 DECL_INITIAL (decl) = error_mark_node;
7964 else
7965 TREE_STATIC (decl) = 1;
7967 if (!is_alias)
7968 /* ifuncs are also aliases, so set that attribute too. */
7969 DECL_ATTRIBUTES (decl)
7970 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7972 else
7974 warning (OPT_Wattributes, "%qE attribute ignored", name);
7975 *no_add_attrs = true;
7978 if (decl_in_symtab_p (*node))
7980 struct symtab_node *n = symtab_node::get (decl);
7981 if (n && n->refuse_visibility_changes)
7983 if (is_alias)
7984 error ("%+D declared alias after being used", decl);
7985 else
7986 error ("%+D declared ifunc after being used", decl);
7991 return NULL_TREE;
7994 /* Handle an "alias" or "ifunc" attribute; arguments as in
7995 struct attribute_spec.handler. */
7997 static tree
7998 handle_ifunc_attribute (tree *node, tree name, tree args,
7999 int ARG_UNUSED (flags), bool *no_add_attrs)
8001 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8004 /* Handle an "alias" or "ifunc" attribute; arguments as in
8005 struct attribute_spec.handler. */
8007 static tree
8008 handle_alias_attribute (tree *node, tree name, tree args,
8009 int ARG_UNUSED (flags), bool *no_add_attrs)
8011 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8014 /* Handle a "weakref" attribute; arguments as in struct
8015 attribute_spec.handler. */
8017 static tree
8018 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8019 int flags, bool *no_add_attrs)
8021 tree attr = NULL_TREE;
8023 /* We must ignore the attribute when it is associated with
8024 local-scoped decls, since attribute alias is ignored and many
8025 such symbols do not even have a DECL_WEAK field. */
8026 if (decl_function_context (*node)
8027 || current_function_decl
8028 || !VAR_OR_FUNCTION_DECL_P (*node))
8030 warning (OPT_Wattributes, "%qE attribute ignored", name);
8031 *no_add_attrs = true;
8032 return NULL_TREE;
8035 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8037 error ("indirect function %q+D cannot be declared weakref", *node);
8038 *no_add_attrs = true;
8039 return NULL_TREE;
8042 /* The idea here is that `weakref("name")' mutates into `weakref,
8043 alias("name")', and weakref without arguments, in turn,
8044 implicitly adds weak. */
8046 if (args)
8048 attr = tree_cons (get_identifier ("alias"), args, attr);
8049 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8051 *no_add_attrs = true;
8053 decl_attributes (node, attr, flags);
8055 else
8057 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8058 error_at (DECL_SOURCE_LOCATION (*node),
8059 "weakref attribute must appear before alias attribute");
8061 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8062 and that isn't supported; and because it wants to add it to
8063 the list of weak decls, which isn't helpful. */
8064 DECL_WEAK (*node) = 1;
8067 if (decl_in_symtab_p (*node))
8069 struct symtab_node *n = symtab_node::get (*node);
8070 if (n && n->refuse_visibility_changes)
8071 error ("%+D declared weakref after being used", *node);
8074 return NULL_TREE;
8077 /* Handle an "visibility" attribute; arguments as in
8078 struct attribute_spec.handler. */
8080 static tree
8081 handle_visibility_attribute (tree *node, tree name, tree args,
8082 int ARG_UNUSED (flags),
8083 bool *ARG_UNUSED (no_add_attrs))
8085 tree decl = *node;
8086 tree id = TREE_VALUE (args);
8087 enum symbol_visibility vis;
8089 if (TYPE_P (*node))
8091 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8092 /* OK */;
8093 else if (!RECORD_OR_UNION_TYPE_P (*node))
8095 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8096 name);
8097 return NULL_TREE;
8099 else if (TYPE_FIELDS (*node))
8101 error ("%qE attribute ignored because %qT is already defined",
8102 name, *node);
8103 return NULL_TREE;
8106 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8108 warning (OPT_Wattributes, "%qE attribute ignored", name);
8109 return NULL_TREE;
8112 if (TREE_CODE (id) != STRING_CST)
8114 error ("visibility argument not a string");
8115 return NULL_TREE;
8118 /* If this is a type, set the visibility on the type decl. */
8119 if (TYPE_P (decl))
8121 decl = TYPE_NAME (decl);
8122 if (!decl)
8123 return NULL_TREE;
8124 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8126 warning (OPT_Wattributes, "%qE attribute ignored on types",
8127 name);
8128 return NULL_TREE;
8132 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8133 vis = VISIBILITY_DEFAULT;
8134 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8135 vis = VISIBILITY_INTERNAL;
8136 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8137 vis = VISIBILITY_HIDDEN;
8138 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8139 vis = VISIBILITY_PROTECTED;
8140 else
8142 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8143 vis = VISIBILITY_DEFAULT;
8146 if (DECL_VISIBILITY_SPECIFIED (decl)
8147 && vis != DECL_VISIBILITY (decl))
8149 tree attributes = (TYPE_P (*node)
8150 ? TYPE_ATTRIBUTES (*node)
8151 : DECL_ATTRIBUTES (decl));
8152 if (lookup_attribute ("visibility", attributes))
8153 error ("%qD redeclared with different visibility", decl);
8154 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8155 && lookup_attribute ("dllimport", attributes))
8156 error ("%qD was declared %qs which implies default visibility",
8157 decl, "dllimport");
8158 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8159 && lookup_attribute ("dllexport", attributes))
8160 error ("%qD was declared %qs which implies default visibility",
8161 decl, "dllexport");
8164 DECL_VISIBILITY (decl) = vis;
8165 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8167 /* Go ahead and attach the attribute to the node as well. This is needed
8168 so we can determine whether we have VISIBILITY_DEFAULT because the
8169 visibility was not specified, or because it was explicitly overridden
8170 from the containing scope. */
8172 return NULL_TREE;
8175 /* Determine the ELF symbol visibility for DECL, which is either a
8176 variable or a function. It is an error to use this function if a
8177 definition of DECL is not available in this translation unit.
8178 Returns true if the final visibility has been determined by this
8179 function; false if the caller is free to make additional
8180 modifications. */
8182 bool
8183 c_determine_visibility (tree decl)
8185 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8187 /* If the user explicitly specified the visibility with an
8188 attribute, honor that. DECL_VISIBILITY will have been set during
8189 the processing of the attribute. We check for an explicit
8190 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8191 to distinguish the use of an attribute from the use of a "#pragma
8192 GCC visibility push(...)"; in the latter case we still want other
8193 considerations to be able to overrule the #pragma. */
8194 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8195 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8196 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8197 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8198 return true;
8200 /* Set default visibility to whatever the user supplied with
8201 visibility_specified depending on #pragma GCC visibility. */
8202 if (!DECL_VISIBILITY_SPECIFIED (decl))
8204 if (visibility_options.inpragma
8205 || DECL_VISIBILITY (decl) != default_visibility)
8207 DECL_VISIBILITY (decl) = default_visibility;
8208 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8209 /* If visibility changed and DECL already has DECL_RTL, ensure
8210 symbol flags are updated. */
8211 if (((VAR_P (decl) && TREE_STATIC (decl))
8212 || TREE_CODE (decl) == FUNCTION_DECL)
8213 && DECL_RTL_SET_P (decl))
8214 make_decl_rtl (decl);
8217 return false;
8220 /* Handle an "tls_model" attribute; arguments as in
8221 struct attribute_spec.handler. */
8223 static tree
8224 handle_tls_model_attribute (tree *node, tree name, tree args,
8225 int ARG_UNUSED (flags), bool *no_add_attrs)
8227 tree id;
8228 tree decl = *node;
8229 enum tls_model kind;
8231 *no_add_attrs = true;
8233 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
8235 warning (OPT_Wattributes, "%qE attribute ignored", name);
8236 return NULL_TREE;
8239 kind = DECL_TLS_MODEL (decl);
8240 id = TREE_VALUE (args);
8241 if (TREE_CODE (id) != STRING_CST)
8243 error ("tls_model argument not a string");
8244 return NULL_TREE;
8247 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8248 kind = TLS_MODEL_LOCAL_EXEC;
8249 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8250 kind = TLS_MODEL_INITIAL_EXEC;
8251 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8252 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8253 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8254 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8255 else
8256 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8258 set_decl_tls_model (decl, kind);
8259 return NULL_TREE;
8262 /* Handle a "no_instrument_function" attribute; arguments as in
8263 struct attribute_spec.handler. */
8265 static tree
8266 handle_no_instrument_function_attribute (tree *node, tree name,
8267 tree ARG_UNUSED (args),
8268 int ARG_UNUSED (flags),
8269 bool *no_add_attrs)
8271 tree decl = *node;
8273 if (TREE_CODE (decl) != FUNCTION_DECL)
8275 error_at (DECL_SOURCE_LOCATION (decl),
8276 "%qE attribute applies only to functions", name);
8277 *no_add_attrs = true;
8279 else
8280 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8282 return NULL_TREE;
8285 /* Handle a "malloc" attribute; arguments as in
8286 struct attribute_spec.handler. */
8288 static tree
8289 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8290 int ARG_UNUSED (flags), bool *no_add_attrs)
8292 if (TREE_CODE (*node) == FUNCTION_DECL
8293 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8294 DECL_IS_MALLOC (*node) = 1;
8295 else
8297 warning (OPT_Wattributes, "%qE attribute ignored", name);
8298 *no_add_attrs = true;
8301 return NULL_TREE;
8304 /* Handle a "alloc_size" attribute; arguments as in
8305 struct attribute_spec.handler. */
8307 static tree
8308 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8309 int ARG_UNUSED (flags), bool *no_add_attrs)
8311 unsigned arg_count = type_num_arguments (*node);
8312 for (; args; args = TREE_CHAIN (args))
8314 tree position = TREE_VALUE (args);
8315 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8316 && TREE_CODE (position) != FUNCTION_DECL)
8317 position = default_conversion (position);
8319 if (!tree_fits_uhwi_p (position)
8320 || !arg_count
8321 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8323 warning (OPT_Wattributes,
8324 "alloc_size parameter outside range");
8325 *no_add_attrs = true;
8326 return NULL_TREE;
8329 return NULL_TREE;
8332 /* Handle a "alloc_align" attribute; arguments as in
8333 struct attribute_spec.handler. */
8335 static tree
8336 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8337 bool *no_add_attrs)
8339 unsigned arg_count = type_num_arguments (*node);
8340 tree position = TREE_VALUE (args);
8341 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8342 position = default_conversion (position);
8344 if (!tree_fits_uhwi_p (position)
8345 || !arg_count
8346 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8348 warning (OPT_Wattributes,
8349 "alloc_align parameter outside range");
8350 *no_add_attrs = true;
8351 return NULL_TREE;
8353 return NULL_TREE;
8356 /* Handle a "assume_aligned" attribute; arguments as in
8357 struct attribute_spec.handler. */
8359 static tree
8360 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8361 bool *no_add_attrs)
8363 for (; args; args = TREE_CHAIN (args))
8365 tree position = TREE_VALUE (args);
8366 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8367 && TREE_CODE (position) != FUNCTION_DECL)
8368 position = default_conversion (position);
8370 if (TREE_CODE (position) != INTEGER_CST)
8372 warning (OPT_Wattributes,
8373 "assume_aligned parameter not integer constant");
8374 *no_add_attrs = true;
8375 return NULL_TREE;
8378 return NULL_TREE;
8381 /* Handle a "fn spec" attribute; arguments as in
8382 struct attribute_spec.handler. */
8384 static tree
8385 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8386 tree args, int ARG_UNUSED (flags),
8387 bool *no_add_attrs ATTRIBUTE_UNUSED)
8389 gcc_assert (args
8390 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8391 && !TREE_CHAIN (args));
8392 return NULL_TREE;
8395 /* Handle a "bnd_variable_size" attribute; arguments as in
8396 struct attribute_spec.handler. */
8398 static tree
8399 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8400 int ARG_UNUSED (flags), bool *no_add_attrs)
8402 if (TREE_CODE (*node) != FIELD_DECL)
8404 warning (OPT_Wattributes, "%qE attribute ignored", name);
8405 *no_add_attrs = true;
8408 return NULL_TREE;
8411 /* Handle a "bnd_legacy" attribute; arguments as in
8412 struct attribute_spec.handler. */
8414 static tree
8415 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8416 int ARG_UNUSED (flags), bool *no_add_attrs)
8418 if (TREE_CODE (*node) != FUNCTION_DECL)
8420 warning (OPT_Wattributes, "%qE attribute ignored", name);
8421 *no_add_attrs = true;
8424 return NULL_TREE;
8427 /* Handle a "bnd_instrument" attribute; arguments as in
8428 struct attribute_spec.handler. */
8430 static tree
8431 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8432 int ARG_UNUSED (flags), bool *no_add_attrs)
8434 if (TREE_CODE (*node) != FUNCTION_DECL)
8436 warning (OPT_Wattributes, "%qE attribute ignored", name);
8437 *no_add_attrs = true;
8440 return NULL_TREE;
8443 /* Handle a "warn_unused" attribute; arguments as in
8444 struct attribute_spec.handler. */
8446 static tree
8447 handle_warn_unused_attribute (tree *node, tree name,
8448 tree args ATTRIBUTE_UNUSED,
8449 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8451 if (TYPE_P (*node))
8452 /* Do nothing else, just set the attribute. We'll get at
8453 it later with lookup_attribute. */
8455 else
8457 warning (OPT_Wattributes, "%qE attribute ignored", name);
8458 *no_add_attrs = true;
8461 return NULL_TREE;
8464 /* Handle an "omp declare simd" attribute; arguments as in
8465 struct attribute_spec.handler. */
8467 static tree
8468 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8470 return NULL_TREE;
8473 /* Handle a "simd" attribute. */
8475 static tree
8476 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
8478 if (TREE_CODE (*node) == FUNCTION_DECL)
8480 if (lookup_attribute ("cilk simd function",
8481 DECL_ATTRIBUTES (*node)) != NULL)
8483 error_at (DECL_SOURCE_LOCATION (*node),
8484 "%<__simd__%> attribute cannot be used in the same "
8485 "function marked as a Cilk Plus SIMD-enabled function");
8486 *no_add_attrs = true;
8488 else
8490 tree t = get_identifier ("omp declare simd");
8491 tree attr = NULL_TREE;
8492 if (args)
8494 tree id = TREE_VALUE (args);
8496 if (TREE_CODE (id) != STRING_CST)
8498 error ("attribute %qE argument not a string", name);
8499 *no_add_attrs = true;
8500 return NULL_TREE;
8503 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8504 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8505 OMP_CLAUSE_NOTINBRANCH);
8506 else
8507 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8508 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8509 OMP_CLAUSE_INBRANCH);
8510 else
8512 error ("only %<inbranch%> and %<notinbranch%> flags are "
8513 "allowed for %<__simd__%> attribute");
8514 *no_add_attrs = true;
8515 return NULL_TREE;
8519 DECL_ATTRIBUTES (*node) = tree_cons (t,
8520 build_tree_list (NULL_TREE,
8521 attr),
8522 DECL_ATTRIBUTES (*node));
8525 else
8527 warning (OPT_Wattributes, "%qE attribute ignored", name);
8528 *no_add_attrs = true;
8531 return NULL_TREE;
8534 /* Handle an "omp declare target" attribute; arguments as in
8535 struct attribute_spec.handler. */
8537 static tree
8538 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8540 return NULL_TREE;
8543 /* Handle a "returns_twice" attribute; arguments as in
8544 struct attribute_spec.handler. */
8546 static tree
8547 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8548 int ARG_UNUSED (flags), bool *no_add_attrs)
8550 if (TREE_CODE (*node) == FUNCTION_DECL)
8551 DECL_IS_RETURNS_TWICE (*node) = 1;
8552 else
8554 warning (OPT_Wattributes, "%qE attribute ignored", name);
8555 *no_add_attrs = true;
8558 return NULL_TREE;
8561 /* Handle a "no_limit_stack" attribute; arguments as in
8562 struct attribute_spec.handler. */
8564 static tree
8565 handle_no_limit_stack_attribute (tree *node, tree name,
8566 tree ARG_UNUSED (args),
8567 int ARG_UNUSED (flags),
8568 bool *no_add_attrs)
8570 tree decl = *node;
8572 if (TREE_CODE (decl) != FUNCTION_DECL)
8574 error_at (DECL_SOURCE_LOCATION (decl),
8575 "%qE attribute applies only to functions", name);
8576 *no_add_attrs = true;
8578 else if (DECL_INITIAL (decl))
8580 error_at (DECL_SOURCE_LOCATION (decl),
8581 "can%'t set %qE attribute after definition", name);
8582 *no_add_attrs = true;
8584 else
8585 DECL_NO_LIMIT_STACK (decl) = 1;
8587 return NULL_TREE;
8590 /* Handle a "pure" attribute; arguments as in
8591 struct attribute_spec.handler. */
8593 static tree
8594 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8595 int ARG_UNUSED (flags), bool *no_add_attrs)
8597 if (TREE_CODE (*node) == FUNCTION_DECL)
8598 DECL_PURE_P (*node) = 1;
8599 /* ??? TODO: Support types. */
8600 else
8602 warning (OPT_Wattributes, "%qE attribute ignored", name);
8603 *no_add_attrs = true;
8606 return NULL_TREE;
8609 /* Digest an attribute list destined for a transactional memory statement.
8610 ALLOWED is the set of attributes that are allowed for this statement;
8611 return the attribute we parsed. Multiple attributes are never allowed. */
8614 parse_tm_stmt_attr (tree attrs, int allowed)
8616 tree a_seen = NULL;
8617 int m_seen = 0;
8619 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8621 tree a = TREE_PURPOSE (attrs);
8622 int m = 0;
8624 if (is_attribute_p ("outer", a))
8625 m = TM_STMT_ATTR_OUTER;
8627 if ((m & allowed) == 0)
8629 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8630 continue;
8633 if (m_seen == 0)
8635 a_seen = a;
8636 m_seen = m;
8638 else if (m_seen == m)
8639 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8640 else
8641 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8644 return m_seen;
8647 /* Transform a TM attribute name into a maskable integer and back.
8648 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8649 to how the lack of an attribute is treated. */
8652 tm_attr_to_mask (tree attr)
8654 if (attr == NULL)
8655 return 0;
8656 if (is_attribute_p ("transaction_safe", attr))
8657 return TM_ATTR_SAFE;
8658 if (is_attribute_p ("transaction_callable", attr))
8659 return TM_ATTR_CALLABLE;
8660 if (is_attribute_p ("transaction_pure", attr))
8661 return TM_ATTR_PURE;
8662 if (is_attribute_p ("transaction_unsafe", attr))
8663 return TM_ATTR_IRREVOCABLE;
8664 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8665 return TM_ATTR_MAY_CANCEL_OUTER;
8666 return 0;
8669 tree
8670 tm_mask_to_attr (int mask)
8672 const char *str;
8673 switch (mask)
8675 case TM_ATTR_SAFE:
8676 str = "transaction_safe";
8677 break;
8678 case TM_ATTR_CALLABLE:
8679 str = "transaction_callable";
8680 break;
8681 case TM_ATTR_PURE:
8682 str = "transaction_pure";
8683 break;
8684 case TM_ATTR_IRREVOCABLE:
8685 str = "transaction_unsafe";
8686 break;
8687 case TM_ATTR_MAY_CANCEL_OUTER:
8688 str = "transaction_may_cancel_outer";
8689 break;
8690 default:
8691 gcc_unreachable ();
8693 return get_identifier (str);
8696 /* Return the first TM attribute seen in LIST. */
8698 tree
8699 find_tm_attribute (tree list)
8701 for (; list ; list = TREE_CHAIN (list))
8703 tree name = TREE_PURPOSE (list);
8704 if (tm_attr_to_mask (name) != 0)
8705 return name;
8707 return NULL_TREE;
8710 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8711 Here we accept only function types, and verify that none of the other
8712 function TM attributes are also applied. */
8713 /* ??? We need to accept class types for C++, but not C. This greatly
8714 complicates this function, since we can no longer rely on the extra
8715 processing given by function_type_required. */
8717 static tree
8718 handle_tm_attribute (tree *node, tree name, tree args,
8719 int flags, bool *no_add_attrs)
8721 /* Only one path adds the attribute; others don't. */
8722 *no_add_attrs = true;
8724 switch (TREE_CODE (*node))
8726 case RECORD_TYPE:
8727 case UNION_TYPE:
8728 /* Only tm_callable and tm_safe apply to classes. */
8729 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8730 goto ignored;
8731 /* FALLTHRU */
8733 case FUNCTION_TYPE:
8734 case METHOD_TYPE:
8736 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8737 if (old_name == name)
8739 else if (old_name != NULL_TREE)
8740 error ("type was previously declared %qE", old_name);
8741 else
8742 *no_add_attrs = false;
8744 break;
8746 case FUNCTION_DECL:
8748 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8749 want to set transaction_safe on the type. */
8750 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8751 if (!TYPE_P (DECL_CONTEXT (*node)))
8752 error_at (DECL_SOURCE_LOCATION (*node),
8753 "%<transaction_safe_dynamic%> may only be specified for "
8754 "a virtual function");
8755 *no_add_attrs = false;
8756 decl_attributes (&TREE_TYPE (*node),
8757 build_tree_list (get_identifier ("transaction_safe"),
8758 NULL_TREE),
8760 break;
8763 case POINTER_TYPE:
8765 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8766 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8768 tree fn_tmp = TREE_TYPE (*node);
8769 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8770 *node = build_pointer_type (fn_tmp);
8771 break;
8774 /* FALLTHRU */
8776 default:
8777 /* If a function is next, pass it on to be tried next. */
8778 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8779 return tree_cons (name, args, NULL);
8781 ignored:
8782 warning (OPT_Wattributes, "%qE attribute ignored", name);
8783 break;
8786 return NULL_TREE;
8789 /* Handle the TM_WRAP attribute; arguments as in
8790 struct attribute_spec.handler. */
8792 static tree
8793 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8794 int ARG_UNUSED (flags), bool *no_add_attrs)
8796 tree decl = *node;
8798 /* We don't need the attribute even on success, since we
8799 record the entry in an external table. */
8800 *no_add_attrs = true;
8802 if (TREE_CODE (decl) != FUNCTION_DECL)
8803 warning (OPT_Wattributes, "%qE attribute ignored", name);
8804 else
8806 tree wrap_decl = TREE_VALUE (args);
8807 if (error_operand_p (wrap_decl))
8809 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8810 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
8811 error ("%qE argument not an identifier", name);
8812 else
8814 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8815 wrap_decl = lookup_name (wrap_decl);
8816 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8818 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8819 TREE_TYPE (wrap_decl)))
8820 record_tm_replacement (wrap_decl, decl);
8821 else
8822 error ("%qD is not compatible with %qD", wrap_decl, decl);
8824 else
8825 error ("%qE argument is not a function", name);
8829 return NULL_TREE;
8832 /* Ignore the given attribute. Used when this attribute may be usefully
8833 overridden by the target, but is not used generically. */
8835 static tree
8836 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8837 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8838 bool *no_add_attrs)
8840 *no_add_attrs = true;
8841 return NULL_TREE;
8844 /* Handle a "no vops" attribute; arguments as in
8845 struct attribute_spec.handler. */
8847 static tree
8848 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8849 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8850 bool *ARG_UNUSED (no_add_attrs))
8852 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8853 DECL_IS_NOVOPS (*node) = 1;
8854 return NULL_TREE;
8857 /* Handle a "deprecated" attribute; arguments as in
8858 struct attribute_spec.handler. */
8860 static tree
8861 handle_deprecated_attribute (tree *node, tree name,
8862 tree args, int flags,
8863 bool *no_add_attrs)
8865 tree type = NULL_TREE;
8866 int warn = 0;
8867 tree what = NULL_TREE;
8869 if (!args)
8870 *no_add_attrs = true;
8871 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8873 error ("deprecated message is not a string");
8874 *no_add_attrs = true;
8877 if (DECL_P (*node))
8879 tree decl = *node;
8880 type = TREE_TYPE (decl);
8882 if (TREE_CODE (decl) == TYPE_DECL
8883 || TREE_CODE (decl) == PARM_DECL
8884 || VAR_OR_FUNCTION_DECL_P (decl)
8885 || TREE_CODE (decl) == FIELD_DECL
8886 || TREE_CODE (decl) == CONST_DECL
8887 || objc_method_decl (TREE_CODE (decl)))
8888 TREE_DEPRECATED (decl) = 1;
8889 else
8890 warn = 1;
8892 else if (TYPE_P (*node))
8894 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8895 *node = build_variant_type_copy (*node);
8896 TREE_DEPRECATED (*node) = 1;
8897 type = *node;
8899 else
8900 warn = 1;
8902 if (warn)
8904 *no_add_attrs = true;
8905 if (type && TYPE_NAME (type))
8907 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8908 what = TYPE_NAME (*node);
8909 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8910 && DECL_NAME (TYPE_NAME (type)))
8911 what = DECL_NAME (TYPE_NAME (type));
8913 if (what)
8914 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8915 else
8916 warning (OPT_Wattributes, "%qE attribute ignored", name);
8919 return NULL_TREE;
8922 /* Handle a "vector_size" attribute; arguments as in
8923 struct attribute_spec.handler. */
8925 static tree
8926 handle_vector_size_attribute (tree *node, tree name, tree args,
8927 int ARG_UNUSED (flags),
8928 bool *no_add_attrs)
8930 unsigned HOST_WIDE_INT vecsize, nunits;
8931 machine_mode orig_mode;
8932 tree type = *node, new_type, size;
8934 *no_add_attrs = true;
8936 size = TREE_VALUE (args);
8937 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8938 && TREE_CODE (size) != FUNCTION_DECL)
8939 size = default_conversion (size);
8941 if (!tree_fits_uhwi_p (size))
8943 warning (OPT_Wattributes, "%qE attribute ignored", name);
8944 return NULL_TREE;
8947 /* Get the vector size (in bytes). */
8948 vecsize = tree_to_uhwi (size);
8950 /* We need to provide for vector pointers, vector arrays, and
8951 functions returning vectors. For example:
8953 __attribute__((vector_size(16))) short *foo;
8955 In this case, the mode is SI, but the type being modified is
8956 HI, so we need to look further. */
8958 while (POINTER_TYPE_P (type)
8959 || TREE_CODE (type) == FUNCTION_TYPE
8960 || TREE_CODE (type) == METHOD_TYPE
8961 || TREE_CODE (type) == ARRAY_TYPE
8962 || TREE_CODE (type) == OFFSET_TYPE)
8963 type = TREE_TYPE (type);
8965 /* Get the mode of the type being modified. */
8966 orig_mode = TYPE_MODE (type);
8968 if ((!INTEGRAL_TYPE_P (type)
8969 && !SCALAR_FLOAT_TYPE_P (type)
8970 && !FIXED_POINT_TYPE_P (type))
8971 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8972 && GET_MODE_CLASS (orig_mode) != MODE_INT
8973 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8974 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8975 || TREE_CODE (type) == BOOLEAN_TYPE)
8977 error ("invalid vector type for attribute %qE", name);
8978 return NULL_TREE;
8981 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8983 error ("vector size not an integral multiple of component size");
8984 return NULL;
8987 if (vecsize == 0)
8989 error ("zero vector size");
8990 return NULL;
8993 /* Calculate how many units fit in the vector. */
8994 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8995 if (nunits & (nunits - 1))
8997 error ("number of components of the vector not a power of two");
8998 return NULL_TREE;
9001 new_type = build_vector_type (type, nunits);
9003 /* Build back pointers if needed. */
9004 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9006 return NULL_TREE;
9009 /* Handle the "nonnull" attribute. */
9010 static tree
9011 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9012 tree args, int ARG_UNUSED (flags),
9013 bool *no_add_attrs)
9015 tree type = *node;
9016 unsigned HOST_WIDE_INT attr_arg_num;
9018 /* If no arguments are specified, all pointer arguments should be
9019 non-null. Verify a full prototype is given so that the arguments
9020 will have the correct types when we actually check them later. */
9021 if (!args)
9023 if (!prototype_p (type))
9025 error ("nonnull attribute without arguments on a non-prototype");
9026 *no_add_attrs = true;
9028 return NULL_TREE;
9031 /* Argument list specified. Verify that each argument number references
9032 a pointer argument. */
9033 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9035 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9037 tree arg = TREE_VALUE (args);
9038 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9039 && TREE_CODE (arg) != FUNCTION_DECL)
9040 arg = default_conversion (arg);
9042 if (!get_nonnull_operand (arg, &arg_num))
9044 error ("nonnull argument has invalid operand number (argument %lu)",
9045 (unsigned long) attr_arg_num);
9046 *no_add_attrs = true;
9047 return NULL_TREE;
9050 if (prototype_p (type))
9052 function_args_iterator iter;
9053 tree argument;
9055 function_args_iter_init (&iter, type);
9056 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9058 argument = function_args_iter_cond (&iter);
9059 if (argument == NULL_TREE || ck_num == arg_num)
9060 break;
9063 if (!argument
9064 || TREE_CODE (argument) == VOID_TYPE)
9066 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9067 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9068 *no_add_attrs = true;
9069 return NULL_TREE;
9072 if (TREE_CODE (argument) != POINTER_TYPE)
9074 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9075 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9076 *no_add_attrs = true;
9077 return NULL_TREE;
9082 return NULL_TREE;
9085 /* Check the argument list of a function call for null in argument slots
9086 that are marked as requiring a non-null pointer argument. The NARGS
9087 arguments are passed in the array ARGARRAY.
9090 static void
9091 check_function_nonnull (tree attrs, int nargs, tree *argarray)
9093 tree a;
9094 int i;
9096 attrs = lookup_attribute ("nonnull", attrs);
9097 if (attrs == NULL_TREE)
9098 return;
9100 a = attrs;
9101 /* See if any of the nonnull attributes has no arguments. If so,
9102 then every pointer argument is checked (in which case the check
9103 for pointer type is done in check_nonnull_arg). */
9104 if (TREE_VALUE (a) != NULL_TREE)
9106 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9107 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9109 if (a != NULL_TREE)
9110 for (i = 0; i < nargs; i++)
9111 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9112 i + 1);
9113 else
9115 /* Walk the argument list. If we encounter an argument number we
9116 should check for non-null, do it. */
9117 for (i = 0; i < nargs; i++)
9119 for (a = attrs; ; a = TREE_CHAIN (a))
9121 a = lookup_attribute ("nonnull", a);
9122 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9123 break;
9126 if (a != NULL_TREE)
9127 check_function_arguments_recurse (check_nonnull_arg, NULL,
9128 argarray[i], i + 1);
9133 /* Check that the Nth argument of a function call (counting backwards
9134 from the end) is a (pointer)0. The NARGS arguments are passed in the
9135 array ARGARRAY. */
9137 static void
9138 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9140 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9142 if (attr)
9144 int len = 0;
9145 int pos = 0;
9146 tree sentinel;
9147 function_args_iterator iter;
9148 tree t;
9150 /* Skip over the named arguments. */
9151 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9153 if (len == nargs)
9154 break;
9155 len++;
9158 if (TREE_VALUE (attr))
9160 tree p = TREE_VALUE (TREE_VALUE (attr));
9161 pos = TREE_INT_CST_LOW (p);
9164 /* The sentinel must be one of the varargs, i.e.
9165 in position >= the number of fixed arguments. */
9166 if ((nargs - 1 - pos) < len)
9168 warning (OPT_Wformat_,
9169 "not enough variable arguments to fit a sentinel");
9170 return;
9173 /* Validate the sentinel. */
9174 sentinel = argarray[nargs - 1 - pos];
9175 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9176 || !integer_zerop (sentinel))
9177 /* Although __null (in C++) is only an integer we allow it
9178 nevertheless, as we are guaranteed that it's exactly
9179 as wide as a pointer, and we don't want to force
9180 users to cast the NULL they have written there.
9181 We warn with -Wstrict-null-sentinel, though. */
9182 && (warn_strict_null_sentinel || null_node != sentinel))
9183 warning (OPT_Wformat_, "missing sentinel in function call");
9187 /* Helper for check_function_nonnull; given a list of operands which
9188 must be non-null in ARGS, determine if operand PARAM_NUM should be
9189 checked. */
9191 static bool
9192 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9194 unsigned HOST_WIDE_INT arg_num = 0;
9196 for (; args; args = TREE_CHAIN (args))
9198 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9200 gcc_assert (found);
9202 if (arg_num == param_num)
9203 return true;
9205 return false;
9208 /* Check that the function argument PARAM (which is operand number
9209 PARAM_NUM) is non-null. This is called by check_function_nonnull
9210 via check_function_arguments_recurse. */
9212 static void
9213 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9214 unsigned HOST_WIDE_INT param_num)
9216 /* Just skip checking the argument if it's not a pointer. This can
9217 happen if the "nonnull" attribute was given without an operand
9218 list (which means to check every pointer argument). */
9220 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9221 return;
9223 if (integer_zerop (param))
9224 warning (OPT_Wnonnull, "null argument where non-null required "
9225 "(argument %lu)", (unsigned long) param_num);
9228 /* Helper for nonnull attribute handling; fetch the operand number
9229 from the attribute argument list. */
9231 static bool
9232 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9234 /* Verify the arg number is a small constant. */
9235 if (tree_fits_uhwi_p (arg_num_expr))
9237 *valp = TREE_INT_CST_LOW (arg_num_expr);
9238 return true;
9240 else
9241 return false;
9244 /* Handle a "nothrow" attribute; arguments as in
9245 struct attribute_spec.handler. */
9247 static tree
9248 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9249 int ARG_UNUSED (flags), bool *no_add_attrs)
9251 if (TREE_CODE (*node) == FUNCTION_DECL)
9252 TREE_NOTHROW (*node) = 1;
9253 /* ??? TODO: Support types. */
9254 else
9256 warning (OPT_Wattributes, "%qE attribute ignored", name);
9257 *no_add_attrs = true;
9260 return NULL_TREE;
9263 /* Handle a "cleanup" attribute; arguments as in
9264 struct attribute_spec.handler. */
9266 static tree
9267 handle_cleanup_attribute (tree *node, tree name, tree args,
9268 int ARG_UNUSED (flags), bool *no_add_attrs)
9270 tree decl = *node;
9271 tree cleanup_id, cleanup_decl;
9273 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9274 for global destructors in C++. This requires infrastructure that
9275 we don't have generically at the moment. It's also not a feature
9276 we'd be missing too much, since we do have attribute constructor. */
9277 if (!VAR_P (decl) || TREE_STATIC (decl))
9279 warning (OPT_Wattributes, "%qE attribute ignored", name);
9280 *no_add_attrs = true;
9281 return NULL_TREE;
9284 /* Verify that the argument is a function in scope. */
9285 /* ??? We could support pointers to functions here as well, if
9286 that was considered desirable. */
9287 cleanup_id = TREE_VALUE (args);
9288 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9290 error ("cleanup argument not an identifier");
9291 *no_add_attrs = true;
9292 return NULL_TREE;
9294 cleanup_decl = lookup_name (cleanup_id);
9295 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9297 error ("cleanup argument not a function");
9298 *no_add_attrs = true;
9299 return NULL_TREE;
9302 /* That the function has proper type is checked with the
9303 eventual call to build_function_call. */
9305 return NULL_TREE;
9308 /* Handle a "warn_unused_result" attribute. No special handling. */
9310 static tree
9311 handle_warn_unused_result_attribute (tree *node, tree name,
9312 tree ARG_UNUSED (args),
9313 int ARG_UNUSED (flags), bool *no_add_attrs)
9315 /* Ignore the attribute for functions not returning any value. */
9316 if (VOID_TYPE_P (TREE_TYPE (*node)))
9318 warning (OPT_Wattributes, "%qE attribute ignored", name);
9319 *no_add_attrs = true;
9322 return NULL_TREE;
9325 /* Handle a "sentinel" attribute. */
9327 static tree
9328 handle_sentinel_attribute (tree *node, tree name, tree args,
9329 int ARG_UNUSED (flags), bool *no_add_attrs)
9331 if (!prototype_p (*node))
9333 warning (OPT_Wattributes,
9334 "%qE attribute requires prototypes with named arguments", name);
9335 *no_add_attrs = true;
9337 else
9339 if (!stdarg_p (*node))
9341 warning (OPT_Wattributes,
9342 "%qE attribute only applies to variadic functions", name);
9343 *no_add_attrs = true;
9347 if (args)
9349 tree position = TREE_VALUE (args);
9350 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9351 && TREE_CODE (position) != FUNCTION_DECL)
9352 position = default_conversion (position);
9354 if (TREE_CODE (position) != INTEGER_CST
9355 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9357 warning (OPT_Wattributes,
9358 "requested position is not an integer constant");
9359 *no_add_attrs = true;
9361 else
9363 if (tree_int_cst_lt (position, integer_zero_node))
9365 warning (OPT_Wattributes,
9366 "requested position is less than zero");
9367 *no_add_attrs = true;
9372 return NULL_TREE;
9375 /* Handle a "type_generic" attribute. */
9377 static tree
9378 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9379 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9380 bool * ARG_UNUSED (no_add_attrs))
9382 /* Ensure we have a function type. */
9383 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9385 /* Ensure we have a variadic function. */
9386 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9388 return NULL_TREE;
9391 /* Handle a "target" attribute. */
9393 static tree
9394 handle_target_attribute (tree *node, tree name, tree args, int flags,
9395 bool *no_add_attrs)
9397 /* Ensure we have a function type. */
9398 if (TREE_CODE (*node) != FUNCTION_DECL)
9400 warning (OPT_Wattributes, "%qE attribute ignored", name);
9401 *no_add_attrs = true;
9403 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9405 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9406 "with %qs attribute", name, "target_clones");
9407 *no_add_attrs = true;
9409 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9410 flags))
9411 *no_add_attrs = true;
9413 return NULL_TREE;
9416 /* Handle a "target_clones" attribute. */
9418 static tree
9419 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9420 int ARG_UNUSED (flags), bool *no_add_attrs)
9422 /* Ensure we have a function type. */
9423 if (TREE_CODE (*node) == FUNCTION_DECL)
9425 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9427 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9428 "with %qs attribute", name, "always_inline");
9429 *no_add_attrs = true;
9431 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9433 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9434 "with %qs attribute", name, "target");
9435 *no_add_attrs = true;
9437 else
9438 /* Do not inline functions with multiple clone targets. */
9439 DECL_UNINLINABLE (*node) = 1;
9441 else
9443 warning (OPT_Wattributes, "%qE attribute ignored", name);
9444 *no_add_attrs = true;
9446 return NULL_TREE;
9449 /* Arguments being collected for optimization. */
9450 typedef const char *const_char_p; /* For DEF_VEC_P. */
9451 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9454 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9455 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9456 false for #pragma GCC optimize. */
9458 bool
9459 parse_optimize_options (tree args, bool attr_p)
9461 bool ret = true;
9462 unsigned opt_argc;
9463 unsigned i;
9464 const char **opt_argv;
9465 struct cl_decoded_option *decoded_options;
9466 unsigned int decoded_options_count;
9467 tree ap;
9469 /* Build up argv vector. Just in case the string is stored away, use garbage
9470 collected strings. */
9471 vec_safe_truncate (optimize_args, 0);
9472 vec_safe_push (optimize_args, (const char *) NULL);
9474 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9476 tree value = TREE_VALUE (ap);
9478 if (TREE_CODE (value) == INTEGER_CST)
9480 char buffer[20];
9481 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9482 vec_safe_push (optimize_args, ggc_strdup (buffer));
9485 else if (TREE_CODE (value) == STRING_CST)
9487 /* Split string into multiple substrings. */
9488 size_t len = TREE_STRING_LENGTH (value);
9489 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9490 char *end = p + len;
9491 char *comma;
9492 char *next_p = p;
9494 while (next_p != NULL)
9496 size_t len2;
9497 char *q, *r;
9499 p = next_p;
9500 comma = strchr (p, ',');
9501 if (comma)
9503 len2 = comma - p;
9504 *comma = '\0';
9505 next_p = comma+1;
9507 else
9509 len2 = end - p;
9510 next_p = NULL;
9513 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9515 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9516 options. */
9517 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9519 ret = false;
9520 if (attr_p)
9521 warning (OPT_Wattributes,
9522 "bad option %s to optimize attribute", p);
9523 else
9524 warning (OPT_Wpragmas,
9525 "bad option %s to pragma attribute", p);
9526 continue;
9529 if (*p != '-')
9531 *r++ = '-';
9533 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9534 itself is -Os, and any other switch begins with a -f. */
9535 if ((*p >= '0' && *p <= '9')
9536 || (p[0] == 's' && p[1] == '\0'))
9537 *r++ = 'O';
9538 else if (*p != 'O')
9539 *r++ = 'f';
9542 memcpy (r, p, len2);
9543 r[len2] = '\0';
9544 vec_safe_push (optimize_args, (const char *) q);
9550 opt_argc = optimize_args->length ();
9551 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9553 for (i = 1; i < opt_argc; i++)
9554 opt_argv[i] = (*optimize_args)[i];
9556 /* Now parse the options. */
9557 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9558 &decoded_options,
9559 &decoded_options_count);
9560 decode_options (&global_options, &global_options_set,
9561 decoded_options, decoded_options_count,
9562 input_location, global_dc);
9564 targetm.override_options_after_change();
9566 optimize_args->truncate (0);
9567 return ret;
9570 /* For handling "optimize" attribute. arguments as in
9571 struct attribute_spec.handler. */
9573 static tree
9574 handle_optimize_attribute (tree *node, tree name, tree args,
9575 int ARG_UNUSED (flags), bool *no_add_attrs)
9577 /* Ensure we have a function type. */
9578 if (TREE_CODE (*node) != FUNCTION_DECL)
9580 warning (OPT_Wattributes, "%qE attribute ignored", name);
9581 *no_add_attrs = true;
9583 else
9585 struct cl_optimization cur_opts;
9586 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9588 /* Save current options. */
9589 cl_optimization_save (&cur_opts, &global_options);
9591 /* If we previously had some optimization options, use them as the
9592 default. */
9593 if (old_opts)
9594 cl_optimization_restore (&global_options,
9595 TREE_OPTIMIZATION (old_opts));
9597 /* Parse options, and update the vector. */
9598 parse_optimize_options (args, true);
9599 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9600 = build_optimization_node (&global_options);
9602 /* Restore current options. */
9603 cl_optimization_restore (&global_options, &cur_opts);
9606 return NULL_TREE;
9609 /* Handle a "no_split_stack" attribute. */
9611 static tree
9612 handle_no_split_stack_attribute (tree *node, tree name,
9613 tree ARG_UNUSED (args),
9614 int ARG_UNUSED (flags),
9615 bool *no_add_attrs)
9617 tree decl = *node;
9619 if (TREE_CODE (decl) != FUNCTION_DECL)
9621 error_at (DECL_SOURCE_LOCATION (decl),
9622 "%qE attribute applies only to functions", name);
9623 *no_add_attrs = true;
9625 else if (DECL_INITIAL (decl))
9627 error_at (DECL_SOURCE_LOCATION (decl),
9628 "can%'t set %qE attribute after definition", name);
9629 *no_add_attrs = true;
9632 return NULL_TREE;
9635 /* Handle a "returns_nonnull" attribute; arguments as in
9636 struct attribute_spec.handler. */
9638 static tree
9639 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9640 bool *no_add_attrs)
9642 // Even without a prototype we still have a return type we can check.
9643 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9645 error ("returns_nonnull attribute on a function not returning a pointer");
9646 *no_add_attrs = true;
9648 return NULL_TREE;
9651 /* Handle a "designated_init" attribute; arguments as in
9652 struct attribute_spec.handler. */
9654 static tree
9655 handle_designated_init_attribute (tree *node, tree name, tree, int,
9656 bool *no_add_attrs)
9658 if (TREE_CODE (*node) != RECORD_TYPE)
9660 error ("%qE attribute is only valid on %<struct%> type", name);
9661 *no_add_attrs = true;
9663 return NULL_TREE;
9667 /* Check for valid arguments being passed to a function with FNTYPE.
9668 There are NARGS arguments in the array ARGARRAY. */
9669 void
9670 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9672 /* Check for null being passed in a pointer argument that must be
9673 non-null. We also need to do this if format checking is enabled. */
9675 if (warn_nonnull)
9676 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9678 /* Check for errors in format strings. */
9680 if (warn_format || warn_suggest_attribute_format)
9681 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9683 if (warn_format)
9684 check_function_sentinel (fntype, nargs, argarray);
9687 /* Generic argument checking recursion routine. PARAM is the argument to
9688 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9689 once the argument is resolved. CTX is context for the callback. */
9690 void
9691 check_function_arguments_recurse (void (*callback)
9692 (void *, tree, unsigned HOST_WIDE_INT),
9693 void *ctx, tree param,
9694 unsigned HOST_WIDE_INT param_num)
9696 if (CONVERT_EXPR_P (param)
9697 && (TYPE_PRECISION (TREE_TYPE (param))
9698 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9700 /* Strip coercion. */
9701 check_function_arguments_recurse (callback, ctx,
9702 TREE_OPERAND (param, 0), param_num);
9703 return;
9706 if (TREE_CODE (param) == CALL_EXPR)
9708 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9709 tree attrs;
9710 bool found_format_arg = false;
9712 /* See if this is a call to a known internationalization function
9713 that modifies a format arg. Such a function may have multiple
9714 format_arg attributes (for example, ngettext). */
9716 for (attrs = TYPE_ATTRIBUTES (type);
9717 attrs;
9718 attrs = TREE_CHAIN (attrs))
9719 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9721 tree inner_arg;
9722 tree format_num_expr;
9723 int format_num;
9724 int i;
9725 call_expr_arg_iterator iter;
9727 /* Extract the argument number, which was previously checked
9728 to be valid. */
9729 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9731 format_num = tree_to_uhwi (format_num_expr);
9733 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9734 inner_arg != 0;
9735 inner_arg = next_call_expr_arg (&iter), i++)
9736 if (i == format_num)
9738 check_function_arguments_recurse (callback, ctx,
9739 inner_arg, param_num);
9740 found_format_arg = true;
9741 break;
9745 /* If we found a format_arg attribute and did a recursive check,
9746 we are done with checking this argument. Otherwise, we continue
9747 and this will be considered a non-literal. */
9748 if (found_format_arg)
9749 return;
9752 if (TREE_CODE (param) == COND_EXPR)
9754 tree cond = fold_for_warn (TREE_OPERAND (param, 0));
9755 /* Check both halves of the conditional expression. */
9756 if (!integer_zerop (cond))
9757 check_function_arguments_recurse (callback, ctx,
9758 TREE_OPERAND (param, 1), param_num);
9759 if (!integer_nonzerop (cond))
9760 check_function_arguments_recurse (callback, ctx,
9761 TREE_OPERAND (param, 2), param_num);
9762 return;
9765 (*callback) (ctx, param, param_num);
9768 /* Checks for a builtin function FNDECL that the number of arguments
9769 NARGS against the required number REQUIRED and issues an error if
9770 there is a mismatch. Returns true if the number of arguments is
9771 correct, otherwise false. */
9773 static bool
9774 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9776 if (nargs < required)
9778 error_at (input_location,
9779 "not enough arguments to function %qE", fndecl);
9780 return false;
9782 else if (nargs > required)
9784 error_at (input_location,
9785 "too many arguments to function %qE", fndecl);
9786 return false;
9788 return true;
9791 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9792 Returns false if there was an error, otherwise true. */
9794 bool
9795 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9797 if (!DECL_BUILT_IN (fndecl)
9798 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9799 return true;
9801 switch (DECL_FUNCTION_CODE (fndecl))
9803 case BUILT_IN_CONSTANT_P:
9804 return builtin_function_validate_nargs (fndecl, nargs, 1);
9806 case BUILT_IN_ISFINITE:
9807 case BUILT_IN_ISINF:
9808 case BUILT_IN_ISINF_SIGN:
9809 case BUILT_IN_ISNAN:
9810 case BUILT_IN_ISNORMAL:
9811 case BUILT_IN_SIGNBIT:
9812 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9814 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9816 error ("non-floating-point argument in call to "
9817 "function %qE", fndecl);
9818 return false;
9820 return true;
9822 return false;
9824 case BUILT_IN_ISGREATER:
9825 case BUILT_IN_ISGREATEREQUAL:
9826 case BUILT_IN_ISLESS:
9827 case BUILT_IN_ISLESSEQUAL:
9828 case BUILT_IN_ISLESSGREATER:
9829 case BUILT_IN_ISUNORDERED:
9830 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9832 enum tree_code code0, code1;
9833 code0 = TREE_CODE (TREE_TYPE (args[0]));
9834 code1 = TREE_CODE (TREE_TYPE (args[1]));
9835 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9836 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9837 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9839 error ("non-floating-point arguments in call to "
9840 "function %qE", fndecl);
9841 return false;
9843 return true;
9845 return false;
9847 case BUILT_IN_FPCLASSIFY:
9848 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9850 unsigned i;
9852 for (i=0; i<5; i++)
9853 if (TREE_CODE (args[i]) != INTEGER_CST)
9855 error ("non-const integer argument %u in call to function %qE",
9856 i+1, fndecl);
9857 return false;
9860 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9862 error ("non-floating-point argument in call to function %qE",
9863 fndecl);
9864 return false;
9866 return true;
9868 return false;
9870 case BUILT_IN_ASSUME_ALIGNED:
9871 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9873 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9875 error ("non-integer argument 3 in call to function %qE", fndecl);
9876 return false;
9878 return true;
9880 return false;
9882 case BUILT_IN_ADD_OVERFLOW:
9883 case BUILT_IN_SUB_OVERFLOW:
9884 case BUILT_IN_MUL_OVERFLOW:
9885 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9887 unsigned i;
9888 for (i = 0; i < 2; i++)
9889 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9891 error ("argument %u in call to function %qE does not have "
9892 "integral type", i + 1, fndecl);
9893 return false;
9895 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9896 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9898 error ("argument 3 in call to function %qE does not have "
9899 "pointer to integer type", fndecl);
9900 return false;
9902 return true;
9904 return false;
9906 default:
9907 return true;
9911 /* Function to help qsort sort FIELD_DECLs by name order. */
9914 field_decl_cmp (const void *x_p, const void *y_p)
9916 const tree *const x = (const tree *const) x_p;
9917 const tree *const y = (const tree *const) y_p;
9919 if (DECL_NAME (*x) == DECL_NAME (*y))
9920 /* A nontype is "greater" than a type. */
9921 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9922 if (DECL_NAME (*x) == NULL_TREE)
9923 return -1;
9924 if (DECL_NAME (*y) == NULL_TREE)
9925 return 1;
9926 if (DECL_NAME (*x) < DECL_NAME (*y))
9927 return -1;
9928 return 1;
9931 static struct {
9932 gt_pointer_operator new_value;
9933 void *cookie;
9934 } resort_data;
9936 /* This routine compares two fields like field_decl_cmp but using the
9937 pointer operator in resort_data. */
9939 static int
9940 resort_field_decl_cmp (const void *x_p, const void *y_p)
9942 const tree *const x = (const tree *const) x_p;
9943 const tree *const y = (const tree *const) y_p;
9945 if (DECL_NAME (*x) == DECL_NAME (*y))
9946 /* A nontype is "greater" than a type. */
9947 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9948 if (DECL_NAME (*x) == NULL_TREE)
9949 return -1;
9950 if (DECL_NAME (*y) == NULL_TREE)
9951 return 1;
9953 tree d1 = DECL_NAME (*x);
9954 tree d2 = DECL_NAME (*y);
9955 resort_data.new_value (&d1, resort_data.cookie);
9956 resort_data.new_value (&d2, resort_data.cookie);
9957 if (d1 < d2)
9958 return -1;
9960 return 1;
9963 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9965 void
9966 resort_sorted_fields (void *obj,
9967 void * ARG_UNUSED (orig_obj),
9968 gt_pointer_operator new_value,
9969 void *cookie)
9971 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9972 resort_data.new_value = new_value;
9973 resort_data.cookie = cookie;
9974 qsort (&sf->elts[0], sf->len, sizeof (tree),
9975 resort_field_decl_cmp);
9978 /* Subroutine of c_parse_error.
9979 Return the result of concatenating LHS and RHS. RHS is really
9980 a string literal, its first character is indicated by RHS_START and
9981 RHS_SIZE is its length (including the terminating NUL character).
9983 The caller is responsible for deleting the returned pointer. */
9985 static char *
9986 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9988 const int lhs_size = strlen (lhs);
9989 char *result = XNEWVEC (char, lhs_size + rhs_size);
9990 strncpy (result, lhs, lhs_size);
9991 strncpy (result + lhs_size, rhs_start, rhs_size);
9992 return result;
9995 /* Issue the error given by GMSGID, indicating that it occurred before
9996 TOKEN, which had the associated VALUE. */
9998 void
9999 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
10000 tree value, unsigned char token_flags)
10002 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10004 char *message = NULL;
10006 if (token_type == CPP_EOF)
10007 message = catenate_messages (gmsgid, " at end of input");
10008 else if (token_type == CPP_CHAR
10009 || token_type == CPP_WCHAR
10010 || token_type == CPP_CHAR16
10011 || token_type == CPP_CHAR32
10012 || token_type == CPP_UTF8CHAR)
10014 unsigned int val = TREE_INT_CST_LOW (value);
10015 const char *prefix;
10017 switch (token_type)
10019 default:
10020 prefix = "";
10021 break;
10022 case CPP_WCHAR:
10023 prefix = "L";
10024 break;
10025 case CPP_CHAR16:
10026 prefix = "u";
10027 break;
10028 case CPP_CHAR32:
10029 prefix = "U";
10030 break;
10031 case CPP_UTF8CHAR:
10032 prefix = "u8";
10033 break;
10036 if (val <= UCHAR_MAX && ISGRAPH (val))
10037 message = catenate_messages (gmsgid, " before %s'%c'");
10038 else
10039 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10041 error (message, prefix, val);
10042 free (message);
10043 message = NULL;
10045 else if (token_type == CPP_CHAR_USERDEF
10046 || token_type == CPP_WCHAR_USERDEF
10047 || token_type == CPP_CHAR16_USERDEF
10048 || token_type == CPP_CHAR32_USERDEF
10049 || token_type == CPP_UTF8CHAR_USERDEF)
10050 message = catenate_messages (gmsgid,
10051 " before user-defined character literal");
10052 else if (token_type == CPP_STRING_USERDEF
10053 || token_type == CPP_WSTRING_USERDEF
10054 || token_type == CPP_STRING16_USERDEF
10055 || token_type == CPP_STRING32_USERDEF
10056 || token_type == CPP_UTF8STRING_USERDEF)
10057 message = catenate_messages (gmsgid, " before user-defined string literal");
10058 else if (token_type == CPP_STRING
10059 || token_type == CPP_WSTRING
10060 || token_type == CPP_STRING16
10061 || token_type == CPP_STRING32
10062 || token_type == CPP_UTF8STRING)
10063 message = catenate_messages (gmsgid, " before string constant");
10064 else if (token_type == CPP_NUMBER)
10065 message = catenate_messages (gmsgid, " before numeric constant");
10066 else if (token_type == CPP_NAME)
10068 message = catenate_messages (gmsgid, " before %qE");
10069 error (message, value);
10070 free (message);
10071 message = NULL;
10073 else if (token_type == CPP_PRAGMA)
10074 message = catenate_messages (gmsgid, " before %<#pragma%>");
10075 else if (token_type == CPP_PRAGMA_EOL)
10076 message = catenate_messages (gmsgid, " before end of line");
10077 else if (token_type == CPP_DECLTYPE)
10078 message = catenate_messages (gmsgid, " before %<decltype%>");
10079 else if (token_type < N_TTYPES)
10081 message = catenate_messages (gmsgid, " before %qs token");
10082 error (message, cpp_type2name (token_type, token_flags));
10083 free (message);
10084 message = NULL;
10086 else
10087 error (gmsgid);
10089 if (message)
10091 error (message);
10092 free (message);
10094 #undef catenate_messages
10097 /* Return the gcc option code associated with the reason for a cpp
10098 message, or 0 if none. */
10100 static int
10101 c_option_controlling_cpp_error (int reason)
10103 const struct cpp_reason_option_codes_t *entry;
10105 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10107 if (entry->reason == reason)
10108 return entry->option_code;
10110 return 0;
10113 /* Callback from cpp_error for PFILE to print diagnostics from the
10114 preprocessor. The diagnostic is of type LEVEL, with REASON set
10115 to the reason code if LEVEL is represents a warning, at location
10116 RICHLOC unless this is after lexing and the compiler's location
10117 should be used instead; MSG is the translated message and AP
10118 the arguments. Returns true if a diagnostic was emitted, false
10119 otherwise. */
10121 bool
10122 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10123 rich_location *richloc,
10124 const char *msg, va_list *ap)
10126 diagnostic_info diagnostic;
10127 diagnostic_t dlevel;
10128 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10129 bool ret;
10131 switch (level)
10133 case CPP_DL_WARNING_SYSHDR:
10134 if (flag_no_output)
10135 return false;
10136 global_dc->dc_warn_system_headers = 1;
10137 /* Fall through. */
10138 case CPP_DL_WARNING:
10139 if (flag_no_output)
10140 return false;
10141 dlevel = DK_WARNING;
10142 break;
10143 case CPP_DL_PEDWARN:
10144 if (flag_no_output && !flag_pedantic_errors)
10145 return false;
10146 dlevel = DK_PEDWARN;
10147 break;
10148 case CPP_DL_ERROR:
10149 dlevel = DK_ERROR;
10150 break;
10151 case CPP_DL_ICE:
10152 dlevel = DK_ICE;
10153 break;
10154 case CPP_DL_NOTE:
10155 dlevel = DK_NOTE;
10156 break;
10157 case CPP_DL_FATAL:
10158 dlevel = DK_FATAL;
10159 break;
10160 default:
10161 gcc_unreachable ();
10163 if (done_lexing)
10164 richloc->set_range (0,
10165 source_range::from_location (input_location),
10166 true, true);
10167 diagnostic_set_info_translated (&diagnostic, msg, ap,
10168 richloc, dlevel);
10169 diagnostic_override_option_index (&diagnostic,
10170 c_option_controlling_cpp_error (reason));
10171 ret = report_diagnostic (&diagnostic);
10172 if (level == CPP_DL_WARNING_SYSHDR)
10173 global_dc->dc_warn_system_headers = save_warn_system_headers;
10174 return ret;
10177 /* Convert a character from the host to the target execution character
10178 set. cpplib handles this, mostly. */
10180 HOST_WIDE_INT
10181 c_common_to_target_charset (HOST_WIDE_INT c)
10183 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10184 zero-extended under -fno-signed-char. cpplib insists that characters
10185 and character constants are always unsigned. Hence we must convert
10186 back and forth. */
10187 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10189 uc = cpp_host_to_exec_charset (parse_in, uc);
10191 if (flag_signed_char)
10192 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10193 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10194 else
10195 return uc;
10198 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10199 references with an INDIRECT_REF of a constant at the bottom; much like the
10200 traditional rendering of offsetof as a macro. Return the folded result. */
10202 tree
10203 fold_offsetof_1 (tree expr, enum tree_code ctx)
10205 tree base, off, t;
10206 tree_code code = TREE_CODE (expr);
10207 switch (code)
10209 case ERROR_MARK:
10210 return expr;
10212 case VAR_DECL:
10213 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10214 return error_mark_node;
10216 case CALL_EXPR:
10217 case TARGET_EXPR:
10218 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10219 return error_mark_node;
10221 case NOP_EXPR:
10222 case INDIRECT_REF:
10223 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10225 error ("cannot apply %<offsetof%> to a non constant address");
10226 return error_mark_node;
10228 return TREE_OPERAND (expr, 0);
10230 case COMPONENT_REF:
10231 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10232 if (base == error_mark_node)
10233 return base;
10235 t = TREE_OPERAND (expr, 1);
10236 if (DECL_C_BIT_FIELD (t))
10238 error ("attempt to take address of bit-field structure "
10239 "member %qD", t);
10240 return error_mark_node;
10242 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10243 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10244 / BITS_PER_UNIT));
10245 break;
10247 case ARRAY_REF:
10248 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10249 if (base == error_mark_node)
10250 return base;
10252 t = TREE_OPERAND (expr, 1);
10254 /* Check if the offset goes beyond the upper bound of the array. */
10255 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10257 tree upbound = array_ref_up_bound (expr);
10258 if (upbound != NULL_TREE
10259 && TREE_CODE (upbound) == INTEGER_CST
10260 && !tree_int_cst_equal (upbound,
10261 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10263 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10264 upbound = size_binop (PLUS_EXPR, upbound,
10265 build_int_cst (TREE_TYPE (upbound), 1));
10266 if (tree_int_cst_lt (upbound, t))
10268 tree v;
10270 for (v = TREE_OPERAND (expr, 0);
10271 TREE_CODE (v) == COMPONENT_REF;
10272 v = TREE_OPERAND (v, 0))
10273 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10274 == RECORD_TYPE)
10276 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10277 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10278 if (TREE_CODE (fld_chain) == FIELD_DECL)
10279 break;
10281 if (fld_chain)
10282 break;
10284 /* Don't warn if the array might be considered a poor
10285 man's flexible array member with a very permissive
10286 definition thereof. */
10287 if (TREE_CODE (v) == ARRAY_REF
10288 || TREE_CODE (v) == COMPONENT_REF)
10289 warning (OPT_Warray_bounds,
10290 "index %E denotes an offset "
10291 "greater than size of %qT",
10292 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10297 t = convert (sizetype, t);
10298 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10299 break;
10301 case COMPOUND_EXPR:
10302 /* Handle static members of volatile structs. */
10303 t = TREE_OPERAND (expr, 1);
10304 gcc_assert (VAR_P (t));
10305 return fold_offsetof_1 (t);
10307 default:
10308 gcc_unreachable ();
10311 return fold_build_pointer_plus (base, off);
10314 /* Likewise, but convert it to the return type of offsetof. */
10316 tree
10317 fold_offsetof (tree expr)
10319 return convert (size_type_node, fold_offsetof_1 (expr));
10322 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10323 expression, because B will always be true. */
10325 void
10326 warn_for_omitted_condop (location_t location, tree cond)
10328 if (truth_value_p (TREE_CODE (cond)))
10329 warning_at (location, OPT_Wparentheses,
10330 "the omitted middle operand in ?: will always be %<true%>, "
10331 "suggest explicit middle operand");
10334 /* Give an error for storing into ARG, which is 'const'. USE indicates
10335 how ARG was being used. */
10337 void
10338 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10340 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10341 || use == lv_asm);
10342 /* Using this macro rather than (for example) arrays of messages
10343 ensures that all the format strings are checked at compile
10344 time. */
10345 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10346 : (use == lv_increment ? (I) \
10347 : (use == lv_decrement ? (D) : (AS))))
10348 if (TREE_CODE (arg) == COMPONENT_REF)
10350 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10351 error_at (loc, READONLY_MSG (G_("assignment of member "
10352 "%qD in read-only object"),
10353 G_("increment of member "
10354 "%qD in read-only object"),
10355 G_("decrement of member "
10356 "%qD in read-only object"),
10357 G_("member %qD in read-only object "
10358 "used as %<asm%> output")),
10359 TREE_OPERAND (arg, 1));
10360 else
10361 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10362 G_("increment of read-only member %qD"),
10363 G_("decrement of read-only member %qD"),
10364 G_("read-only member %qD used as %<asm%> output")),
10365 TREE_OPERAND (arg, 1));
10367 else if (VAR_P (arg))
10368 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10369 G_("increment of read-only variable %qD"),
10370 G_("decrement of read-only variable %qD"),
10371 G_("read-only variable %qD used as %<asm%> output")),
10372 arg);
10373 else if (TREE_CODE (arg) == PARM_DECL)
10374 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10375 G_("increment of read-only parameter %qD"),
10376 G_("decrement of read-only parameter %qD"),
10377 G_("read-only parameter %qD use as %<asm%> output")),
10378 arg);
10379 else if (TREE_CODE (arg) == RESULT_DECL)
10381 gcc_assert (c_dialect_cxx ());
10382 error_at (loc, READONLY_MSG (G_("assignment of "
10383 "read-only named return value %qD"),
10384 G_("increment of "
10385 "read-only named return value %qD"),
10386 G_("decrement of "
10387 "read-only named return value %qD"),
10388 G_("read-only named return value %qD "
10389 "used as %<asm%>output")),
10390 arg);
10392 else if (TREE_CODE (arg) == FUNCTION_DECL)
10393 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10394 G_("increment of function %qD"),
10395 G_("decrement of function %qD"),
10396 G_("function %qD used as %<asm%> output")),
10397 arg);
10398 else
10399 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10400 G_("increment of read-only location %qE"),
10401 G_("decrement of read-only location %qE"),
10402 G_("read-only location %qE used as %<asm%> output")),
10403 arg);
10406 /* Print an error message for an invalid lvalue. USE says
10407 how the lvalue is being used and so selects the error message. LOC
10408 is the location for the error. */
10410 void
10411 lvalue_error (location_t loc, enum lvalue_use use)
10413 switch (use)
10415 case lv_assign:
10416 error_at (loc, "lvalue required as left operand of assignment");
10417 break;
10418 case lv_increment:
10419 error_at (loc, "lvalue required as increment operand");
10420 break;
10421 case lv_decrement:
10422 error_at (loc, "lvalue required as decrement operand");
10423 break;
10424 case lv_addressof:
10425 error_at (loc, "lvalue required as unary %<&%> operand");
10426 break;
10427 case lv_asm:
10428 error_at (loc, "lvalue required in asm statement");
10429 break;
10430 default:
10431 gcc_unreachable ();
10435 /* Print an error message for an invalid indirection of type TYPE.
10436 ERRSTRING is the name of the operator for the indirection. */
10438 void
10439 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10441 switch (errstring)
10443 case RO_NULL:
10444 gcc_assert (c_dialect_cxx ());
10445 error_at (loc, "invalid type argument (have %qT)", type);
10446 break;
10447 case RO_ARRAY_INDEXING:
10448 error_at (loc,
10449 "invalid type argument of array indexing (have %qT)",
10450 type);
10451 break;
10452 case RO_UNARY_STAR:
10453 error_at (loc,
10454 "invalid type argument of unary %<*%> (have %qT)",
10455 type);
10456 break;
10457 case RO_ARROW:
10458 error_at (loc,
10459 "invalid type argument of %<->%> (have %qT)",
10460 type);
10461 break;
10462 case RO_ARROW_STAR:
10463 error_at (loc,
10464 "invalid type argument of %<->*%> (have %qT)",
10465 type);
10466 break;
10467 case RO_IMPLICIT_CONVERSION:
10468 error_at (loc,
10469 "invalid type argument of implicit conversion (have %qT)",
10470 type);
10471 break;
10472 default:
10473 gcc_unreachable ();
10477 /* *PTYPE is an incomplete array. Complete it with a domain based on
10478 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10479 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10480 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10483 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10485 tree maxindex, type, main_type, elt, unqual_elt;
10486 int failure = 0, quals;
10487 hashval_t hashcode = 0;
10488 bool overflow_p = false;
10490 maxindex = size_zero_node;
10491 if (initial_value)
10493 if (TREE_CODE (initial_value) == STRING_CST)
10495 int eltsize
10496 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10497 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10499 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10501 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10503 if (vec_safe_is_empty (v))
10505 if (pedantic)
10506 failure = 3;
10507 maxindex = ssize_int (-1);
10509 else
10511 tree curindex;
10512 unsigned HOST_WIDE_INT cnt;
10513 constructor_elt *ce;
10514 bool fold_p = false;
10516 if ((*v)[0].index)
10517 maxindex = (*v)[0].index, fold_p = true;
10519 curindex = maxindex;
10521 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10523 bool curfold_p = false;
10524 if (ce->index)
10525 curindex = ce->index, curfold_p = true;
10526 else
10528 if (fold_p)
10530 /* Since we treat size types now as ordinary
10531 unsigned types, we need an explicit overflow
10532 check. */
10533 tree orig = curindex;
10534 curindex = fold_convert (sizetype, curindex);
10535 overflow_p |= tree_int_cst_lt (curindex, orig);
10537 curindex = size_binop (PLUS_EXPR, curindex,
10538 size_one_node);
10540 if (tree_int_cst_lt (maxindex, curindex))
10541 maxindex = curindex, fold_p = curfold_p;
10543 if (fold_p)
10545 tree orig = maxindex;
10546 maxindex = fold_convert (sizetype, maxindex);
10547 overflow_p |= tree_int_cst_lt (maxindex, orig);
10551 else
10553 /* Make an error message unless that happened already. */
10554 if (initial_value != error_mark_node)
10555 failure = 1;
10558 else
10560 failure = 2;
10561 if (!do_default)
10562 return failure;
10565 type = *ptype;
10566 elt = TREE_TYPE (type);
10567 quals = TYPE_QUALS (strip_array_types (elt));
10568 if (quals == 0)
10569 unqual_elt = elt;
10570 else
10571 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10573 /* Using build_distinct_type_copy and modifying things afterward instead
10574 of using build_array_type to create a new type preserves all of the
10575 TYPE_LANG_FLAG_? bits that the front end may have set. */
10576 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10577 TREE_TYPE (main_type) = unqual_elt;
10578 TYPE_DOMAIN (main_type)
10579 = build_range_type (TREE_TYPE (maxindex),
10580 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10581 layout_type (main_type);
10583 /* Make sure we have the canonical MAIN_TYPE. */
10584 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10585 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10586 hashcode);
10587 main_type = type_hash_canon (hashcode, main_type);
10589 /* Fix the canonical type. */
10590 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10591 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10592 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10593 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10594 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10595 != TYPE_DOMAIN (main_type)))
10596 TYPE_CANONICAL (main_type)
10597 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10598 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10599 else
10600 TYPE_CANONICAL (main_type) = main_type;
10602 if (quals == 0)
10603 type = main_type;
10604 else
10605 type = c_build_qualified_type (main_type, quals);
10607 if (COMPLETE_TYPE_P (type)
10608 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10609 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10611 error ("size of array is too large");
10612 /* If we proceed with the array type as it is, we'll eventually
10613 crash in tree_to_[su]hwi(). */
10614 type = error_mark_node;
10617 *ptype = type;
10618 return failure;
10621 /* Like c_mark_addressable but don't check register qualifier. */
10622 void
10623 c_common_mark_addressable_vec (tree t)
10625 while (handled_component_p (t))
10626 t = TREE_OPERAND (t, 0);
10627 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
10628 return;
10629 TREE_ADDRESSABLE (t) = 1;
10634 /* Used to help initialize the builtin-types.def table. When a type of
10635 the correct size doesn't exist, use error_mark_node instead of NULL.
10636 The later results in segfaults even when a decl using the type doesn't
10637 get invoked. */
10639 tree
10640 builtin_type_for_size (int size, bool unsignedp)
10642 tree type = c_common_type_for_size (size, unsignedp);
10643 return type ? type : error_mark_node;
10646 /* A helper function for resolve_overloaded_builtin in resolving the
10647 overloaded __sync_ builtins. Returns a positive power of 2 if the
10648 first operand of PARAMS is a pointer to a supported data type.
10649 Returns 0 if an error is encountered. */
10651 static int
10652 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10654 tree type;
10655 int size;
10657 if (!params)
10659 error ("too few arguments to function %qE", function);
10660 return 0;
10663 type = TREE_TYPE ((*params)[0]);
10664 if (TREE_CODE (type) == ARRAY_TYPE)
10666 /* Force array-to-pointer decay for C++. */
10667 gcc_assert (c_dialect_cxx());
10668 (*params)[0] = default_conversion ((*params)[0]);
10669 type = TREE_TYPE ((*params)[0]);
10671 if (TREE_CODE (type) != POINTER_TYPE)
10672 goto incompatible;
10674 type = TREE_TYPE (type);
10675 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10676 goto incompatible;
10678 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10679 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10680 return size;
10682 incompatible:
10683 error ("incompatible type for argument %d of %qE", 1, function);
10684 return 0;
10687 /* A helper function for resolve_overloaded_builtin. Adds casts to
10688 PARAMS to make arguments match up with those of FUNCTION. Drops
10689 the variadic arguments at the end. Returns false if some error
10690 was encountered; true on success. */
10692 static bool
10693 sync_resolve_params (location_t loc, tree orig_function, tree function,
10694 vec<tree, va_gc> *params, bool orig_format)
10696 function_args_iterator iter;
10697 tree ptype;
10698 unsigned int parmnum;
10700 function_args_iter_init (&iter, TREE_TYPE (function));
10701 /* We've declared the implementation functions to use "volatile void *"
10702 as the pointer parameter, so we shouldn't get any complaints from the
10703 call to check_function_arguments what ever type the user used. */
10704 function_args_iter_next (&iter);
10705 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10706 ptype = TYPE_MAIN_VARIANT (ptype);
10708 /* For the rest of the values, we need to cast these to FTYPE, so that we
10709 don't get warnings for passing pointer types, etc. */
10710 parmnum = 0;
10711 while (1)
10713 tree val, arg_type;
10715 arg_type = function_args_iter_cond (&iter);
10716 /* XXX void_type_node belies the abstraction. */
10717 if (arg_type == void_type_node)
10718 break;
10720 ++parmnum;
10721 if (params->length () <= parmnum)
10723 error_at (loc, "too few arguments to function %qE", orig_function);
10724 return false;
10727 /* Only convert parameters if arg_type is unsigned integer type with
10728 new format sync routines, i.e. don't attempt to convert pointer
10729 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10730 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10731 kinds). */
10732 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10734 /* Ideally for the first conversion we'd use convert_for_assignment
10735 so that we get warnings for anything that doesn't match the pointer
10736 type. This isn't portable across the C and C++ front ends atm. */
10737 val = (*params)[parmnum];
10738 val = convert (ptype, val);
10739 val = convert (arg_type, val);
10740 (*params)[parmnum] = val;
10743 function_args_iter_next (&iter);
10746 /* __atomic routines are not variadic. */
10747 if (!orig_format && params->length () != parmnum + 1)
10749 error_at (loc, "too many arguments to function %qE", orig_function);
10750 return false;
10753 /* The definition of these primitives is variadic, with the remaining
10754 being "an optional list of variables protected by the memory barrier".
10755 No clue what that's supposed to mean, precisely, but we consider all
10756 call-clobbered variables to be protected so we're safe. */
10757 params->truncate (parmnum + 1);
10759 return true;
10762 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10763 RESULT to make it match the type of the first pointer argument in
10764 PARAMS. */
10766 static tree
10767 sync_resolve_return (tree first_param, tree result, bool orig_format)
10769 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10770 tree rtype = TREE_TYPE (result);
10771 ptype = TYPE_MAIN_VARIANT (ptype);
10773 /* New format doesn't require casting unless the types are the same size. */
10774 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10775 return convert (ptype, result);
10776 else
10777 return result;
10780 /* This function verifies the PARAMS to generic atomic FUNCTION.
10781 It returns the size if all the parameters are the same size, otherwise
10782 0 is returned if the parameters are invalid. */
10784 static int
10785 get_atomic_generic_size (location_t loc, tree function,
10786 vec<tree, va_gc> *params)
10788 unsigned int n_param;
10789 unsigned int n_model;
10790 unsigned int x;
10791 int size_0;
10792 tree type_0;
10794 /* Determine the parameter makeup. */
10795 switch (DECL_FUNCTION_CODE (function))
10797 case BUILT_IN_ATOMIC_EXCHANGE:
10798 n_param = 4;
10799 n_model = 1;
10800 break;
10801 case BUILT_IN_ATOMIC_LOAD:
10802 case BUILT_IN_ATOMIC_STORE:
10803 n_param = 3;
10804 n_model = 1;
10805 break;
10806 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10807 n_param = 6;
10808 n_model = 2;
10809 break;
10810 default:
10811 gcc_unreachable ();
10814 if (vec_safe_length (params) != n_param)
10816 error_at (loc, "incorrect number of arguments to function %qE", function);
10817 return 0;
10820 /* Get type of first parameter, and determine its size. */
10821 type_0 = TREE_TYPE ((*params)[0]);
10822 if (TREE_CODE (type_0) == ARRAY_TYPE)
10824 /* Force array-to-pointer decay for C++. */
10825 gcc_assert (c_dialect_cxx());
10826 (*params)[0] = default_conversion ((*params)[0]);
10827 type_0 = TREE_TYPE ((*params)[0]);
10829 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10831 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10832 function);
10833 return 0;
10836 /* Types must be compile time constant sizes. */
10837 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10839 error_at (loc,
10840 "argument 1 of %qE must be a pointer to a constant size type",
10841 function);
10842 return 0;
10845 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10847 /* Zero size objects are not allowed. */
10848 if (size_0 == 0)
10850 error_at (loc,
10851 "argument 1 of %qE must be a pointer to a nonzero size object",
10852 function);
10853 return 0;
10856 /* Check each other parameter is a pointer and the same size. */
10857 for (x = 0; x < n_param - n_model; x++)
10859 int size;
10860 tree type = TREE_TYPE ((*params)[x]);
10861 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10862 if (n_param == 6 && x == 3)
10863 continue;
10864 if (!POINTER_TYPE_P (type))
10866 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10867 function);
10868 return 0;
10870 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10871 size = type_size ? tree_to_uhwi (type_size) : 0;
10872 if (size != size_0)
10874 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10875 function);
10876 return 0;
10880 /* Check memory model parameters for validity. */
10881 for (x = n_param - n_model ; x < n_param; x++)
10883 tree p = (*params)[x];
10884 if (TREE_CODE (p) == INTEGER_CST)
10886 int i = tree_to_uhwi (p);
10887 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
10889 warning_at (loc, OPT_Winvalid_memory_model,
10890 "invalid memory model argument %d of %qE", x + 1,
10891 function);
10894 else
10895 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10897 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10898 function);
10899 return 0;
10903 return size_0;
10907 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10908 at the beginning of the parameter list PARAMS representing the size of the
10909 objects. This is to match the library ABI requirement. LOC is the location
10910 of the function call.
10911 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10912 returned to allow the external call to be constructed. */
10914 static tree
10915 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10916 vec<tree, va_gc> *params)
10918 tree size_node;
10920 /* Insert a SIZE_T parameter as the first param. If there isn't
10921 enough space, allocate a new vector and recursively re-build with that. */
10922 if (!params->space (1))
10924 unsigned int z, len;
10925 vec<tree, va_gc> *v;
10926 tree f;
10928 len = params->length ();
10929 vec_alloc (v, len + 1);
10930 v->quick_push (build_int_cst (size_type_node, n));
10931 for (z = 0; z < len; z++)
10932 v->quick_push ((*params)[z]);
10933 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10934 vec_free (v);
10935 return f;
10938 /* Add the size parameter and leave as a function call for processing. */
10939 size_node = build_int_cst (size_type_node, n);
10940 params->quick_insert (0, size_node);
10941 return NULL_TREE;
10945 /* Return whether atomic operations for naturally aligned N-byte
10946 arguments are supported, whether inline or through libatomic. */
10947 static bool
10948 atomic_size_supported_p (int n)
10950 switch (n)
10952 case 1:
10953 case 2:
10954 case 4:
10955 case 8:
10956 return true;
10958 case 16:
10959 return targetm.scalar_mode_supported_p (TImode);
10961 default:
10962 return false;
10966 /* This will process an __atomic_exchange function call, determine whether it
10967 needs to be mapped to the _N variation, or turned into a library call.
10968 LOC is the location of the builtin call.
10969 FUNCTION is the DECL that has been invoked;
10970 PARAMS is the argument list for the call. The return value is non-null
10971 TRUE is returned if it is translated into the proper format for a call to the
10972 external library, and NEW_RETURN is set the tree for that function.
10973 FALSE is returned if processing for the _N variation is required, and
10974 NEW_RETURN is set to the return value the result is copied into. */
10975 static bool
10976 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10977 vec<tree, va_gc> *params, tree *new_return)
10979 tree p0, p1, p2, p3;
10980 tree I_type, I_type_ptr;
10981 int n = get_atomic_generic_size (loc, function, params);
10983 /* Size of 0 is an error condition. */
10984 if (n == 0)
10986 *new_return = error_mark_node;
10987 return true;
10990 /* If not a lock-free size, change to the library generic format. */
10991 if (!atomic_size_supported_p (n))
10993 *new_return = add_atomic_size_parameter (n, loc, function, params);
10994 return true;
10997 /* Otherwise there is a lockfree match, transform the call from:
10998 void fn(T* mem, T* desired, T* return, model)
10999 into
11000 *return = (T) (fn (In* mem, (In) *desired, model)) */
11002 p0 = (*params)[0];
11003 p1 = (*params)[1];
11004 p2 = (*params)[2];
11005 p3 = (*params)[3];
11007 /* Create pointer to appropriate size. */
11008 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11009 I_type_ptr = build_pointer_type (I_type);
11011 /* Convert object pointer to required type. */
11012 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11013 (*params)[0] = p0;
11014 /* Convert new value to required type, and dereference it. */
11015 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11016 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11017 (*params)[1] = p1;
11019 /* Move memory model to the 3rd position, and end param list. */
11020 (*params)[2] = p3;
11021 params->truncate (3);
11023 /* Convert return pointer and dereference it for later assignment. */
11024 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11026 return false;
11030 /* This will process an __atomic_compare_exchange function call, determine
11031 whether it needs to be mapped to the _N variation, or turned into a lib call.
11032 LOC is the location of the builtin call.
11033 FUNCTION is the DECL that has been invoked;
11034 PARAMS is the argument list for the call. The return value is non-null
11035 TRUE is returned if it is translated into the proper format for a call to the
11036 external library, and NEW_RETURN is set the tree for that function.
11037 FALSE is returned if processing for the _N variation is required. */
11039 static bool
11040 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11041 vec<tree, va_gc> *params,
11042 tree *new_return)
11044 tree p0, p1, p2;
11045 tree I_type, I_type_ptr;
11046 int n = get_atomic_generic_size (loc, function, params);
11048 /* Size of 0 is an error condition. */
11049 if (n == 0)
11051 *new_return = error_mark_node;
11052 return true;
11055 /* If not a lock-free size, change to the library generic format. */
11056 if (!atomic_size_supported_p (n))
11058 /* The library generic format does not have the weak parameter, so
11059 remove it from the param list. Since a parameter has been removed,
11060 we can be sure that there is room for the SIZE_T parameter, meaning
11061 there will not be a recursive rebuilding of the parameter list, so
11062 there is no danger this will be done twice. */
11063 if (n > 0)
11065 (*params)[3] = (*params)[4];
11066 (*params)[4] = (*params)[5];
11067 params->truncate (5);
11069 *new_return = add_atomic_size_parameter (n, loc, function, params);
11070 return true;
11073 /* Otherwise, there is a match, so the call needs to be transformed from:
11074 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11075 into
11076 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11078 p0 = (*params)[0];
11079 p1 = (*params)[1];
11080 p2 = (*params)[2];
11082 /* Create pointer to appropriate size. */
11083 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11084 I_type_ptr = build_pointer_type (I_type);
11086 /* Convert object pointer to required type. */
11087 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11088 (*params)[0] = p0;
11090 /* Convert expected pointer to required type. */
11091 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11092 (*params)[1] = p1;
11094 /* Convert desired value to required type, and dereference it. */
11095 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11096 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11097 (*params)[2] = p2;
11099 /* The rest of the parameters are fine. NULL means no special return value
11100 processing.*/
11101 *new_return = NULL;
11102 return false;
11106 /* This will process an __atomic_load function call, determine whether it
11107 needs to be mapped to the _N variation, or turned into a library call.
11108 LOC is the location of the builtin call.
11109 FUNCTION is the DECL that has been invoked;
11110 PARAMS is the argument list for the call. The return value is non-null
11111 TRUE is returned if it is translated into the proper format for a call to the
11112 external library, and NEW_RETURN is set the tree for that function.
11113 FALSE is returned if processing for the _N variation is required, and
11114 NEW_RETURN is set to the return value the result is copied into. */
11116 static bool
11117 resolve_overloaded_atomic_load (location_t loc, tree function,
11118 vec<tree, va_gc> *params, tree *new_return)
11120 tree p0, p1, p2;
11121 tree I_type, I_type_ptr;
11122 int n = get_atomic_generic_size (loc, function, params);
11124 /* Size of 0 is an error condition. */
11125 if (n == 0)
11127 *new_return = error_mark_node;
11128 return true;
11131 /* If not a lock-free size, change to the library generic format. */
11132 if (!atomic_size_supported_p (n))
11134 *new_return = add_atomic_size_parameter (n, loc, function, params);
11135 return true;
11138 /* Otherwise, there is a match, so the call needs to be transformed from:
11139 void fn(T* mem, T* return, model)
11140 into
11141 *return = (T) (fn ((In *) mem, model)) */
11143 p0 = (*params)[0];
11144 p1 = (*params)[1];
11145 p2 = (*params)[2];
11147 /* Create pointer to appropriate size. */
11148 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11149 I_type_ptr = build_pointer_type (I_type);
11151 /* Convert object pointer to required type. */
11152 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11153 (*params)[0] = p0;
11155 /* Move memory model to the 2nd position, and end param list. */
11156 (*params)[1] = p2;
11157 params->truncate (2);
11159 /* Convert return pointer and dereference it for later assignment. */
11160 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11162 return false;
11166 /* This will process an __atomic_store function call, determine whether it
11167 needs to be mapped to the _N variation, or turned into a library call.
11168 LOC is the location of the builtin call.
11169 FUNCTION is the DECL that has been invoked;
11170 PARAMS is the argument list for the call. The return value is non-null
11171 TRUE is returned if it is translated into the proper format for a call to the
11172 external library, and NEW_RETURN is set the tree for that function.
11173 FALSE is returned if processing for the _N variation is required, and
11174 NEW_RETURN is set to the return value the result is copied into. */
11176 static bool
11177 resolve_overloaded_atomic_store (location_t loc, tree function,
11178 vec<tree, va_gc> *params, tree *new_return)
11180 tree p0, p1;
11181 tree I_type, I_type_ptr;
11182 int n = get_atomic_generic_size (loc, function, params);
11184 /* Size of 0 is an error condition. */
11185 if (n == 0)
11187 *new_return = error_mark_node;
11188 return true;
11191 /* If not a lock-free size, change to the library generic format. */
11192 if (!atomic_size_supported_p (n))
11194 *new_return = add_atomic_size_parameter (n, loc, function, params);
11195 return true;
11198 /* Otherwise, there is a match, so the call needs to be transformed from:
11199 void fn(T* mem, T* value, model)
11200 into
11201 fn ((In *) mem, (In) *value, model) */
11203 p0 = (*params)[0];
11204 p1 = (*params)[1];
11206 /* Create pointer to appropriate size. */
11207 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11208 I_type_ptr = build_pointer_type (I_type);
11210 /* Convert object pointer to required type. */
11211 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11212 (*params)[0] = p0;
11214 /* Convert new value to required type, and dereference it. */
11215 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11216 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11217 (*params)[1] = p1;
11219 /* The memory model is in the right spot already. Return is void. */
11220 *new_return = NULL_TREE;
11222 return false;
11226 /* Some builtin functions are placeholders for other expressions. This
11227 function should be called immediately after parsing the call expression
11228 before surrounding code has committed to the type of the expression.
11230 LOC is the location of the builtin call.
11232 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11233 PARAMS is the argument list for the call. The return value is non-null
11234 when expansion is complete, and null if normal processing should
11235 continue. */
11237 tree
11238 resolve_overloaded_builtin (location_t loc, tree function,
11239 vec<tree, va_gc> *params)
11241 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11242 bool orig_format = true;
11243 tree new_return = NULL_TREE;
11245 switch (DECL_BUILT_IN_CLASS (function))
11247 case BUILT_IN_NORMAL:
11248 break;
11249 case BUILT_IN_MD:
11250 if (targetm.resolve_overloaded_builtin)
11251 return targetm.resolve_overloaded_builtin (loc, function, params);
11252 else
11253 return NULL_TREE;
11254 default:
11255 return NULL_TREE;
11258 /* Handle BUILT_IN_NORMAL here. */
11259 switch (orig_code)
11261 case BUILT_IN_ATOMIC_EXCHANGE:
11262 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11263 case BUILT_IN_ATOMIC_LOAD:
11264 case BUILT_IN_ATOMIC_STORE:
11266 /* Handle these 4 together so that they can fall through to the next
11267 case if the call is transformed to an _N variant. */
11268 switch (orig_code)
11270 case BUILT_IN_ATOMIC_EXCHANGE:
11272 if (resolve_overloaded_atomic_exchange (loc, function, params,
11273 &new_return))
11274 return new_return;
11275 /* Change to the _N variant. */
11276 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11277 break;
11280 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11282 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11283 params,
11284 &new_return))
11285 return new_return;
11286 /* Change to the _N variant. */
11287 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11288 break;
11290 case BUILT_IN_ATOMIC_LOAD:
11292 if (resolve_overloaded_atomic_load (loc, function, params,
11293 &new_return))
11294 return new_return;
11295 /* Change to the _N variant. */
11296 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11297 break;
11299 case BUILT_IN_ATOMIC_STORE:
11301 if (resolve_overloaded_atomic_store (loc, function, params,
11302 &new_return))
11303 return new_return;
11304 /* Change to the _N variant. */
11305 orig_code = BUILT_IN_ATOMIC_STORE_N;
11306 break;
11308 default:
11309 gcc_unreachable ();
11311 /* Fallthrough to the normal processing. */
11313 case BUILT_IN_ATOMIC_EXCHANGE_N:
11314 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11315 case BUILT_IN_ATOMIC_LOAD_N:
11316 case BUILT_IN_ATOMIC_STORE_N:
11317 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11318 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11319 case BUILT_IN_ATOMIC_AND_FETCH_N:
11320 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11321 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11322 case BUILT_IN_ATOMIC_OR_FETCH_N:
11323 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11324 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11325 case BUILT_IN_ATOMIC_FETCH_AND_N:
11326 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11327 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11328 case BUILT_IN_ATOMIC_FETCH_OR_N:
11330 orig_format = false;
11331 /* Fallthru for parameter processing. */
11333 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11334 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11335 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11336 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11337 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11338 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11339 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11340 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11341 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11342 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11343 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11344 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11345 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11346 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11347 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11348 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11350 int n = sync_resolve_size (function, params);
11351 tree new_function, first_param, result;
11352 enum built_in_function fncode;
11354 if (n == 0)
11355 return error_mark_node;
11357 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11358 new_function = builtin_decl_explicit (fncode);
11359 if (!sync_resolve_params (loc, function, new_function, params,
11360 orig_format))
11361 return error_mark_node;
11363 first_param = (*params)[0];
11364 result = build_function_call_vec (loc, vNULL, new_function, params,
11365 NULL);
11366 if (result == error_mark_node)
11367 return result;
11368 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11369 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11370 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11371 result = sync_resolve_return (first_param, result, orig_format);
11373 /* If new_return is set, assign function to that expr and cast the
11374 result to void since the generic interface returned void. */
11375 if (new_return)
11377 /* Cast function result from I{1,2,4,8,16} to the required type. */
11378 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11379 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11380 result);
11381 TREE_SIDE_EFFECTS (result) = 1;
11382 protected_set_expr_location (result, loc);
11383 result = convert (void_type_node, result);
11385 return result;
11388 default:
11389 return NULL_TREE;
11393 /* vector_types_compatible_elements_p is used in type checks of vectors
11394 values used as operands of binary operators. Where it returns true, and
11395 the other checks of the caller succeed (being vector types in he first
11396 place, and matching number of elements), we can just treat the types
11397 as essentially the same.
11398 Contrast with vector_targets_convertible_p, which is used for vector
11399 pointer types, and vector_types_convertible_p, which will allow
11400 language-specific matches under the control of flag_lax_vector_conversions,
11401 and might still require a conversion. */
11402 /* True if vector types T1 and T2 can be inputs to the same binary
11403 operator without conversion.
11404 We don't check the overall vector size here because some of our callers
11405 want to give different error messages when the vectors are compatible
11406 except for the element count. */
11408 bool
11409 vector_types_compatible_elements_p (tree t1, tree t2)
11411 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11412 t1 = TREE_TYPE (t1);
11413 t2 = TREE_TYPE (t2);
11415 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11417 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11418 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11419 || c2 == FIXED_POINT_TYPE));
11421 t1 = c_common_signed_type (t1);
11422 t2 = c_common_signed_type (t2);
11423 /* Equality works here because c_common_signed_type uses
11424 TYPE_MAIN_VARIANT. */
11425 if (t1 == t2)
11426 return true;
11427 if (opaque && c1 == c2
11428 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11429 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11430 return true;
11431 return false;
11434 /* Check for missing format attributes on function pointers. LTYPE is
11435 the new type or left-hand side type. RTYPE is the old type or
11436 right-hand side type. Returns TRUE if LTYPE is missing the desired
11437 attribute. */
11439 bool
11440 check_missing_format_attribute (tree ltype, tree rtype)
11442 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11443 tree ra;
11445 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11446 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11447 break;
11448 if (ra)
11450 tree la;
11451 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11452 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11453 break;
11454 return !la;
11456 else
11457 return false;
11460 /* Subscripting with type char is likely to lose on a machine where
11461 chars are signed. So warn on any machine, but optionally. Don't
11462 warn for unsigned char since that type is safe. Don't warn for
11463 signed char because anyone who uses that must have done so
11464 deliberately. Furthermore, we reduce the false positive load by
11465 warning only for non-constant value of type char. */
11467 void
11468 warn_array_subscript_with_type_char (location_t loc, tree index)
11470 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11471 && TREE_CODE (index) != INTEGER_CST)
11472 warning_at (loc, OPT_Wchar_subscripts,
11473 "array subscript has type %<char%>");
11476 /* Implement -Wparentheses for the unexpected C precedence rules, to
11477 cover cases like x + y << z which readers are likely to
11478 misinterpret. We have seen an expression in which CODE is a binary
11479 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11480 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11481 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11482 expression was not formed using a binary or unary operator, or it
11483 was enclosed in parentheses. */
11485 void
11486 warn_about_parentheses (location_t loc, enum tree_code code,
11487 enum tree_code code_left, tree arg_left,
11488 enum tree_code code_right, tree arg_right)
11490 if (!warn_parentheses)
11491 return;
11493 /* This macro tests that the expression ARG with original tree code
11494 CODE appears to be a boolean expression. or the result of folding a
11495 boolean expression. */
11496 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11497 (truth_value_p (TREE_CODE (ARG)) \
11498 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11499 /* Folding may create 0 or 1 integers from other expressions. */ \
11500 || ((CODE) != INTEGER_CST \
11501 && (integer_onep (ARG) || integer_zerop (ARG))))
11503 switch (code)
11505 case LSHIFT_EXPR:
11506 if (code_left == PLUS_EXPR)
11507 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11508 "suggest parentheses around %<+%> inside %<<<%>");
11509 else if (code_right == PLUS_EXPR)
11510 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11511 "suggest parentheses around %<+%> inside %<<<%>");
11512 else if (code_left == MINUS_EXPR)
11513 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11514 "suggest parentheses around %<-%> inside %<<<%>");
11515 else if (code_right == MINUS_EXPR)
11516 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11517 "suggest parentheses around %<-%> inside %<<<%>");
11518 return;
11520 case RSHIFT_EXPR:
11521 if (code_left == PLUS_EXPR)
11522 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11523 "suggest parentheses around %<+%> inside %<>>%>");
11524 else if (code_right == PLUS_EXPR)
11525 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11526 "suggest parentheses around %<+%> inside %<>>%>");
11527 else if (code_left == MINUS_EXPR)
11528 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11529 "suggest parentheses around %<-%> inside %<>>%>");
11530 else if (code_right == MINUS_EXPR)
11531 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11532 "suggest parentheses around %<-%> inside %<>>%>");
11533 return;
11535 case TRUTH_ORIF_EXPR:
11536 if (code_left == TRUTH_ANDIF_EXPR)
11537 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11538 "suggest parentheses around %<&&%> within %<||%>");
11539 else if (code_right == TRUTH_ANDIF_EXPR)
11540 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11541 "suggest parentheses around %<&&%> within %<||%>");
11542 return;
11544 case BIT_IOR_EXPR:
11545 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11546 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11547 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11548 "suggest parentheses around arithmetic in operand of %<|%>");
11549 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11550 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11551 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11552 "suggest parentheses around arithmetic in operand of %<|%>");
11553 /* Check cases like x|y==z */
11554 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11555 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11556 "suggest parentheses around comparison in operand of %<|%>");
11557 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11558 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11559 "suggest parentheses around comparison in operand of %<|%>");
11560 /* Check cases like !x | y */
11561 else if (code_left == TRUTH_NOT_EXPR
11562 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11563 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11564 "suggest parentheses around operand of "
11565 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11566 return;
11568 case BIT_XOR_EXPR:
11569 if (code_left == BIT_AND_EXPR
11570 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11571 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11572 "suggest parentheses around arithmetic in operand of %<^%>");
11573 else if (code_right == BIT_AND_EXPR
11574 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11575 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11576 "suggest parentheses around arithmetic in operand of %<^%>");
11577 /* Check cases like x^y==z */
11578 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11579 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11580 "suggest parentheses around comparison in operand of %<^%>");
11581 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11582 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11583 "suggest parentheses around comparison in operand of %<^%>");
11584 return;
11586 case BIT_AND_EXPR:
11587 if (code_left == PLUS_EXPR)
11588 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11589 "suggest parentheses around %<+%> in operand of %<&%>");
11590 else if (code_right == PLUS_EXPR)
11591 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11592 "suggest parentheses around %<+%> in operand of %<&%>");
11593 else if (code_left == MINUS_EXPR)
11594 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11595 "suggest parentheses around %<-%> in operand of %<&%>");
11596 else if (code_right == MINUS_EXPR)
11597 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11598 "suggest parentheses around %<-%> in operand of %<&%>");
11599 /* Check cases like x&y==z */
11600 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11601 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11602 "suggest parentheses around comparison in operand of %<&%>");
11603 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11604 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11605 "suggest parentheses around comparison in operand of %<&%>");
11606 /* Check cases like !x & y */
11607 else if (code_left == TRUTH_NOT_EXPR
11608 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11609 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11610 "suggest parentheses around operand of "
11611 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11612 return;
11614 case EQ_EXPR:
11615 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11616 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11617 "suggest parentheses around comparison in operand of %<==%>");
11618 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11619 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11620 "suggest parentheses around comparison in operand of %<==%>");
11621 return;
11622 case NE_EXPR:
11623 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11624 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11625 "suggest parentheses around comparison in operand of %<!=%>");
11626 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11627 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11628 "suggest parentheses around comparison in operand of %<!=%>");
11629 return;
11631 default:
11632 if (TREE_CODE_CLASS (code) == tcc_comparison)
11634 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11635 && code_left != NE_EXPR && code_left != EQ_EXPR
11636 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11637 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11638 "comparisons like %<X<=Y<=Z%> do not "
11639 "have their mathematical meaning");
11640 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11641 && code_right != NE_EXPR && code_right != EQ_EXPR
11642 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11643 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11644 "comparisons like %<X<=Y<=Z%> do not "
11645 "have their mathematical meaning");
11647 return;
11649 #undef NOT_A_BOOLEAN_EXPR_P
11652 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11654 void
11655 warn_for_unused_label (tree label)
11657 if (!TREE_USED (label))
11659 if (DECL_INITIAL (label))
11660 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11661 else
11662 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11666 /* Warn for division by zero according to the value of DIVISOR. LOC
11667 is the location of the division operator. */
11669 void
11670 warn_for_div_by_zero (location_t loc, tree divisor)
11672 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11673 about division by zero. Do not issue a warning if DIVISOR has a
11674 floating-point type, since we consider 0.0/0.0 a valid way of
11675 generating a NaN. */
11676 if (c_inhibit_evaluation_warnings == 0
11677 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11678 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11681 /* Subroutine of build_binary_op. Give warnings for comparisons
11682 between signed and unsigned quantities that may fail. Do the
11683 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11684 so that casts will be considered, but default promotions won't
11687 LOCATION is the location of the comparison operator.
11689 The arguments of this function map directly to local variables
11690 of build_binary_op. */
11692 void
11693 warn_for_sign_compare (location_t location,
11694 tree orig_op0, tree orig_op1,
11695 tree op0, tree op1,
11696 tree result_type, enum tree_code resultcode)
11698 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11699 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11700 int unsignedp0, unsignedp1;
11702 /* In C++, check for comparison of different enum types. */
11703 if (c_dialect_cxx()
11704 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11705 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11706 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11707 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11709 warning_at (location,
11710 OPT_Wsign_compare, "comparison between types %qT and %qT",
11711 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11714 /* Do not warn if the comparison is being done in a signed type,
11715 since the signed type will only be chosen if it can represent
11716 all the values of the unsigned type. */
11717 if (!TYPE_UNSIGNED (result_type))
11718 /* OK */;
11719 /* Do not warn if both operands are unsigned. */
11720 else if (op0_signed == op1_signed)
11721 /* OK */;
11722 else
11724 tree sop, uop, base_type;
11725 bool ovf;
11727 if (op0_signed)
11728 sop = orig_op0, uop = orig_op1;
11729 else
11730 sop = orig_op1, uop = orig_op0;
11732 STRIP_TYPE_NOPS (sop);
11733 STRIP_TYPE_NOPS (uop);
11734 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11735 ? TREE_TYPE (result_type) : result_type);
11737 /* Do not warn if the signed quantity is an unsuffixed integer
11738 literal (or some static constant expression involving such
11739 literals or a conditional expression involving such literals)
11740 and it is non-negative. */
11741 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11742 /* OK */;
11743 /* Do not warn if the comparison is an equality operation, the
11744 unsigned quantity is an integral constant, and it would fit
11745 in the result if the result were signed. */
11746 else if (TREE_CODE (uop) == INTEGER_CST
11747 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11748 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11749 /* OK */;
11750 /* In C, do not warn if the unsigned quantity is an enumeration
11751 constant and its maximum value would fit in the result if the
11752 result were signed. */
11753 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11754 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11755 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11756 c_common_signed_type (base_type)))
11757 /* OK */;
11758 else
11759 warning_at (location,
11760 OPT_Wsign_compare,
11761 "comparison between signed and unsigned integer expressions");
11764 /* Warn if two unsigned values are being compared in a size larger
11765 than their original size, and one (and only one) is the result of
11766 a `~' operator. This comparison will always fail.
11768 Also warn if one operand is a constant, and the constant does not
11769 have all bits set that are set in the ~ operand when it is
11770 extended. */
11772 op0 = c_common_get_narrower (op0, &unsignedp0);
11773 op1 = c_common_get_narrower (op1, &unsignedp1);
11775 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11776 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11778 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11779 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11780 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11781 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11783 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11785 tree primop;
11786 HOST_WIDE_INT constant, mask;
11787 int unsignedp;
11788 unsigned int bits;
11790 if (tree_fits_shwi_p (op0))
11792 primop = op1;
11793 unsignedp = unsignedp1;
11794 constant = tree_to_shwi (op0);
11796 else
11798 primop = op0;
11799 unsignedp = unsignedp0;
11800 constant = tree_to_shwi (op1);
11803 bits = TYPE_PRECISION (TREE_TYPE (primop));
11804 if (bits < TYPE_PRECISION (result_type)
11805 && bits < HOST_BITS_PER_LONG && unsignedp)
11807 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
11808 if ((mask & constant) != mask)
11810 if (constant == 0)
11811 warning_at (location, OPT_Wsign_compare,
11812 "promoted ~unsigned is always non-zero");
11813 else
11814 warning_at (location, OPT_Wsign_compare,
11815 "comparison of promoted ~unsigned with constant");
11819 else if (unsignedp0 && unsignedp1
11820 && (TYPE_PRECISION (TREE_TYPE (op0))
11821 < TYPE_PRECISION (result_type))
11822 && (TYPE_PRECISION (TREE_TYPE (op1))
11823 < TYPE_PRECISION (result_type)))
11824 warning_at (location, OPT_Wsign_compare,
11825 "comparison of promoted ~unsigned with unsigned");
11829 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11830 type via c_common_type. If -Wdouble-promotion is in use, and the
11831 conditions for warning have been met, issue a warning. GMSGID is
11832 the warning message. It must have two %T specifiers for the type
11833 that was converted (generally "float") and the type to which it was
11834 converted (generally "double), respectively. LOC is the location
11835 to which the awrning should refer. */
11837 void
11838 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11839 const char *gmsgid, location_t loc)
11841 tree source_type;
11843 if (!warn_double_promotion)
11844 return;
11845 /* If the conversion will not occur at run-time, there is no need to
11846 warn about it. */
11847 if (c_inhibit_evaluation_warnings)
11848 return;
11849 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11850 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11851 return;
11852 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11853 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11854 source_type = type1;
11855 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11856 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11857 source_type = type2;
11858 else
11859 return;
11860 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11863 /* Possibly warn about unused parameters. */
11865 void
11866 do_warn_unused_parameter (tree fn)
11868 tree decl;
11870 for (decl = DECL_ARGUMENTS (fn);
11871 decl; decl = DECL_CHAIN (decl))
11872 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
11873 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
11874 && !TREE_NO_WARNING (decl))
11875 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
11876 "unused parameter %qD", decl);
11880 /* Setup a TYPE_DECL node as a typedef representation.
11882 X is a TYPE_DECL for a typedef statement. Create a brand new
11883 ..._TYPE node (which will be just a variant of the existing
11884 ..._TYPE node with identical properties) and then install X
11885 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11887 The whole point here is to end up with a situation where each
11888 and every ..._TYPE node the compiler creates will be uniquely
11889 associated with AT MOST one node representing a typedef name.
11890 This way, even though the compiler substitutes corresponding
11891 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11892 early on, later parts of the compiler can always do the reverse
11893 translation and get back the corresponding typedef name. For
11894 example, given:
11896 typedef struct S MY_TYPE;
11897 MY_TYPE object;
11899 Later parts of the compiler might only know that `object' was of
11900 type `struct S' if it were not for code just below. With this
11901 code however, later parts of the compiler see something like:
11903 struct S' == struct S
11904 typedef struct S' MY_TYPE;
11905 struct S' object;
11907 And they can then deduce (from the node for type struct S') that
11908 the original object declaration was:
11910 MY_TYPE object;
11912 Being able to do this is important for proper support of protoize,
11913 and also for generating precise symbolic debugging information
11914 which takes full account of the programmer's (typedef) vocabulary.
11916 Obviously, we don't want to generate a duplicate ..._TYPE node if
11917 the TYPE_DECL node that we are now processing really represents a
11918 standard built-in type. */
11920 void
11921 set_underlying_type (tree x)
11923 if (x == error_mark_node)
11924 return;
11925 if (DECL_IS_BUILTIN (x))
11927 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11928 TYPE_NAME (TREE_TYPE (x)) = x;
11930 else if (TREE_TYPE (x) != error_mark_node
11931 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11933 tree tt = TREE_TYPE (x);
11934 DECL_ORIGINAL_TYPE (x) = tt;
11935 tt = build_variant_type_copy (tt);
11936 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11937 TYPE_NAME (tt) = x;
11938 TREE_USED (tt) = TREE_USED (x);
11939 TREE_TYPE (x) = tt;
11943 /* Record the types used by the current global variable declaration
11944 being parsed, so that we can decide later to emit their debug info.
11945 Those types are in types_used_by_cur_var_decl, and we are going to
11946 store them in the types_used_by_vars_hash hash table.
11947 DECL is the declaration of the global variable that has been parsed. */
11949 void
11950 record_types_used_by_current_var_decl (tree decl)
11952 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11954 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11956 tree type = types_used_by_cur_var_decl->pop ();
11957 types_used_by_var_decl_insert (type, decl);
11961 /* If DECL is a typedef that is declared in the current function,
11962 record it for the purpose of -Wunused-local-typedefs. */
11964 void
11965 record_locally_defined_typedef (tree decl)
11967 struct c_language_function *l;
11969 if (!warn_unused_local_typedefs
11970 || cfun == NULL
11971 /* if this is not a locally defined typedef then we are not
11972 interested. */
11973 || !is_typedef_decl (decl)
11974 || !decl_function_context (decl))
11975 return;
11977 l = (struct c_language_function *) cfun->language;
11978 vec_safe_push (l->local_typedefs, decl);
11981 /* If T is a TYPE_DECL declared locally, mark it as used. */
11983 void
11984 maybe_record_typedef_use (tree t)
11986 if (!is_typedef_decl (t))
11987 return;
11989 TREE_USED (t) = true;
11992 /* Warn if there are some unused locally defined typedefs in the
11993 current function. */
11995 void
11996 maybe_warn_unused_local_typedefs (void)
11998 int i;
11999 tree decl;
12000 /* The number of times we have emitted -Wunused-local-typedefs
12001 warnings. If this is different from errorcount, that means some
12002 unrelated errors have been issued. In which case, we'll avoid
12003 emitting "unused-local-typedefs" warnings. */
12004 static int unused_local_typedefs_warn_count;
12005 struct c_language_function *l;
12007 if (cfun == NULL)
12008 return;
12010 if ((l = (struct c_language_function *) cfun->language) == NULL)
12011 return;
12013 if (warn_unused_local_typedefs
12014 && errorcount == unused_local_typedefs_warn_count)
12016 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
12017 if (!TREE_USED (decl))
12018 warning_at (DECL_SOURCE_LOCATION (decl),
12019 OPT_Wunused_local_typedefs,
12020 "typedef %qD locally defined but not used", decl);
12021 unused_local_typedefs_warn_count = errorcount;
12024 vec_free (l->local_typedefs);
12027 /* Warn about boolean expression compared with an integer value different
12028 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12029 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12030 are the operands of the comparison. The caller must ensure that
12031 either operand is a boolean expression. */
12033 void
12034 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12035 tree op1)
12037 if (TREE_CODE_CLASS (code) != tcc_comparison)
12038 return;
12040 tree f, cst;
12041 if (f = fold_for_warn (op0),
12042 TREE_CODE (f) == INTEGER_CST)
12043 cst = op0 = f;
12044 else if (f = fold_for_warn (op1),
12045 TREE_CODE (f) == INTEGER_CST)
12046 cst = op1 = f;
12047 else
12048 return;
12050 if (!integer_zerop (cst) && !integer_onep (cst))
12052 int sign = (TREE_CODE (op0) == INTEGER_CST
12053 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
12054 if (code == EQ_EXPR
12055 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12056 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12057 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12058 "with boolean expression is always false", cst);
12059 else
12060 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12061 "with boolean expression is always true", cst);
12063 else if (integer_zerop (cst) || integer_onep (cst))
12065 /* If the non-constant operand isn't of a boolean type, we
12066 don't want to warn here. */
12067 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12068 /* Handle booleans promoted to integers. */
12069 if (CONVERT_EXPR_P (noncst)
12070 && TREE_TYPE (noncst) == integer_type_node
12071 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12072 /* Warn. */;
12073 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12074 && !truth_value_p (TREE_CODE (noncst)))
12075 return;
12076 /* Do some magic to get the right diagnostics. */
12077 bool flag = TREE_CODE (op0) == INTEGER_CST;
12078 flag = integer_zerop (cst) ? flag : !flag;
12079 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12080 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12081 "with boolean expression is always true", cst);
12082 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12083 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12084 "with boolean expression is always false", cst);
12088 /* Warn if signed left shift overflows. We don't warn
12089 about left-shifting 1 into the sign bit in C++14; cf.
12090 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12091 LOC is a location of the shift; OP0 and OP1 are the operands.
12092 Return true if an overflow is detected, false otherwise. */
12094 bool
12095 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12097 if (TREE_CODE (op0) != INTEGER_CST
12098 || TREE_CODE (op1) != INTEGER_CST)
12099 return false;
12101 tree type0 = TREE_TYPE (op0);
12102 unsigned int prec0 = TYPE_PRECISION (type0);
12104 /* Left-hand operand must be signed. */
12105 if (TYPE_UNSIGNED (type0))
12106 return false;
12108 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12109 + TREE_INT_CST_LOW (op1));
12110 /* Handle the case of left-shifting 1 into the sign bit.
12111 * However, shifting 1 _out_ of the sign bit, as in
12112 * INT_MIN << 1, is considered an overflow.
12114 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
12116 /* Never warn for C++14 onwards. */
12117 if (cxx_dialect >= cxx14)
12118 return false;
12119 /* Otherwise only if -Wshift-overflow=2. But return
12120 true to signal an overflow for the sake of integer
12121 constant expressions. */
12122 if (warn_shift_overflow < 2)
12123 return true;
12126 bool overflowed = min_prec > prec0;
12127 if (overflowed && c_inhibit_evaluation_warnings == 0)
12128 warning_at (loc, OPT_Wshift_overflow_,
12129 "result of %qE requires %u bits to represent, "
12130 "but %qT only has %u bits",
12131 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12132 min_prec, type0, prec0);
12134 return overflowed;
12137 /* The C and C++ parsers both use vectors to hold function arguments.
12138 For efficiency, we keep a cache of unused vectors. This is the
12139 cache. */
12141 typedef vec<tree, va_gc> *tree_gc_vec;
12142 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12144 /* Return a new vector from the cache. If the cache is empty,
12145 allocate a new vector. These vectors are GC'ed, so it is OK if the
12146 pointer is not released.. */
12148 vec<tree, va_gc> *
12149 make_tree_vector (void)
12151 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12152 return tree_vector_cache->pop ();
12153 else
12155 /* Passing 0 to vec::alloc returns NULL, and our callers require
12156 that we always return a non-NULL value. The vector code uses
12157 4 when growing a NULL vector, so we do too. */
12158 vec<tree, va_gc> *v;
12159 vec_alloc (v, 4);
12160 return v;
12164 /* Release a vector of trees back to the cache. */
12166 void
12167 release_tree_vector (vec<tree, va_gc> *vec)
12169 if (vec != NULL)
12171 vec->truncate (0);
12172 vec_safe_push (tree_vector_cache, vec);
12176 /* Get a new tree vector holding a single tree. */
12178 vec<tree, va_gc> *
12179 make_tree_vector_single (tree t)
12181 vec<tree, va_gc> *ret = make_tree_vector ();
12182 ret->quick_push (t);
12183 return ret;
12186 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12188 vec<tree, va_gc> *
12189 make_tree_vector_from_list (tree list)
12191 vec<tree, va_gc> *ret = make_tree_vector ();
12192 for (; list; list = TREE_CHAIN (list))
12193 vec_safe_push (ret, TREE_VALUE (list));
12194 return ret;
12197 /* Get a new tree vector which is a copy of an existing one. */
12199 vec<tree, va_gc> *
12200 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12202 vec<tree, va_gc> *ret;
12203 unsigned int ix;
12204 tree t;
12206 ret = make_tree_vector ();
12207 vec_safe_reserve (ret, vec_safe_length (orig));
12208 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12209 ret->quick_push (t);
12210 return ret;
12213 /* Return true if KEYWORD starts a type specifier. */
12215 bool
12216 keyword_begins_type_specifier (enum rid keyword)
12218 switch (keyword)
12220 case RID_AUTO_TYPE:
12221 case RID_INT:
12222 case RID_CHAR:
12223 case RID_FLOAT:
12224 case RID_DOUBLE:
12225 case RID_VOID:
12226 case RID_UNSIGNED:
12227 case RID_LONG:
12228 case RID_SHORT:
12229 case RID_SIGNED:
12230 case RID_DFLOAT32:
12231 case RID_DFLOAT64:
12232 case RID_DFLOAT128:
12233 case RID_FRACT:
12234 case RID_ACCUM:
12235 case RID_BOOL:
12236 case RID_WCHAR:
12237 case RID_CHAR16:
12238 case RID_CHAR32:
12239 case RID_SAT:
12240 case RID_COMPLEX:
12241 case RID_TYPEOF:
12242 case RID_STRUCT:
12243 case RID_CLASS:
12244 case RID_UNION:
12245 case RID_ENUM:
12246 return true;
12247 default:
12248 if (keyword >= RID_FIRST_INT_N
12249 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12250 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12251 return true;
12252 return false;
12256 /* Return true if KEYWORD names a type qualifier. */
12258 bool
12259 keyword_is_type_qualifier (enum rid keyword)
12261 switch (keyword)
12263 case RID_CONST:
12264 case RID_VOLATILE:
12265 case RID_RESTRICT:
12266 case RID_ATOMIC:
12267 return true;
12268 default:
12269 return false;
12273 /* Return true if KEYWORD names a storage class specifier.
12275 RID_TYPEDEF is not included in this list despite `typedef' being
12276 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12277 such for syntactic convenience only. */
12279 bool
12280 keyword_is_storage_class_specifier (enum rid keyword)
12282 switch (keyword)
12284 case RID_STATIC:
12285 case RID_EXTERN:
12286 case RID_REGISTER:
12287 case RID_AUTO:
12288 case RID_MUTABLE:
12289 case RID_THREAD:
12290 return true;
12291 default:
12292 return false;
12296 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12298 static bool
12299 keyword_is_function_specifier (enum rid keyword)
12301 switch (keyword)
12303 case RID_INLINE:
12304 case RID_NORETURN:
12305 case RID_VIRTUAL:
12306 case RID_EXPLICIT:
12307 return true;
12308 default:
12309 return false;
12313 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12314 declaration-specifier (C99 6.7). */
12316 bool
12317 keyword_is_decl_specifier (enum rid keyword)
12319 if (keyword_is_storage_class_specifier (keyword)
12320 || keyword_is_type_qualifier (keyword)
12321 || keyword_is_function_specifier (keyword))
12322 return true;
12324 switch (keyword)
12326 case RID_TYPEDEF:
12327 case RID_FRIEND:
12328 case RID_CONSTEXPR:
12329 return true;
12330 default:
12331 return false;
12335 /* Initialize language-specific-bits of tree_contains_struct. */
12337 void
12338 c_common_init_ts (void)
12340 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12341 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12342 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12345 /* Build a user-defined numeric literal out of an integer constant type VALUE
12346 with identifier SUFFIX. */
12348 tree
12349 build_userdef_literal (tree suffix_id, tree value,
12350 enum overflow_type overflow, tree num_string)
12352 tree literal = make_node (USERDEF_LITERAL);
12353 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12354 USERDEF_LITERAL_VALUE (literal) = value;
12355 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12356 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12357 return literal;
12360 /* For vector[index], convert the vector to a
12361 pointer of the underlying type. Return true if the resulting
12362 ARRAY_REF should not be an lvalue. */
12364 bool
12365 convert_vector_to_pointer_for_subscript (location_t loc,
12366 tree *vecp, tree index)
12368 bool ret = false;
12369 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
12371 tree type = TREE_TYPE (*vecp);
12372 tree type1;
12374 ret = !lvalue_p (*vecp);
12375 if (TREE_CODE (index) == INTEGER_CST)
12376 if (!tree_fits_uhwi_p (index)
12377 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12378 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12380 if (ret)
12382 tree tmp = create_tmp_var_raw (type);
12383 DECL_SOURCE_LOCATION (tmp) = loc;
12384 *vecp = c_save_expr (*vecp);
12385 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12387 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12388 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12389 *vecp
12390 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12391 *vecp, NULL_TREE, NULL_TREE),
12392 non_const);
12394 else
12395 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12396 NULL_TREE, NULL_TREE);
12397 SET_EXPR_LOCATION (*vecp, loc);
12398 c_common_mark_addressable_vec (tmp);
12400 else
12401 c_common_mark_addressable_vec (*vecp);
12402 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12403 type1 = build_pointer_type (TREE_TYPE (*vecp));
12404 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12405 if (!ref_all
12406 && !DECL_P (*vecp))
12408 /* If the original vector isn't declared may_alias and it
12409 isn't a bare vector look if the subscripting would
12410 alias the vector we subscript, and if not, force ref-all. */
12411 alias_set_type vecset = get_alias_set (*vecp);
12412 alias_set_type sset = get_alias_set (type);
12413 if (!alias_sets_must_conflict_p (sset, vecset)
12414 && !alias_set_subset_of (sset, vecset))
12415 ref_all = true;
12417 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12418 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12419 *vecp = convert (type, *vecp);
12421 return ret;
12424 /* Determine which of the operands, if any, is a scalar that needs to be
12425 converted to a vector, for the range of operations. */
12426 enum stv_conv
12427 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12428 bool complain)
12430 tree type0 = TREE_TYPE (op0);
12431 tree type1 = TREE_TYPE (op1);
12432 bool integer_only_op = false;
12433 enum stv_conv ret = stv_firstarg;
12435 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
12436 switch (code)
12438 /* Most GENERIC binary expressions require homogeneous arguments.
12439 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12440 argument that is a vector and a second one that is a scalar, so
12441 we never return stv_secondarg for them. */
12442 case RSHIFT_EXPR:
12443 case LSHIFT_EXPR:
12444 if (TREE_CODE (type0) == INTEGER_TYPE
12445 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12447 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12449 if (complain)
12450 error_at (loc, "conversion of scalar %qT to vector %qT "
12451 "involves truncation", type0, type1);
12452 return stv_error;
12454 else
12455 return stv_firstarg;
12457 break;
12459 case BIT_IOR_EXPR:
12460 case BIT_XOR_EXPR:
12461 case BIT_AND_EXPR:
12462 integer_only_op = true;
12463 /* ... fall through ... */
12465 case VEC_COND_EXPR:
12467 case PLUS_EXPR:
12468 case MINUS_EXPR:
12469 case MULT_EXPR:
12470 case TRUNC_DIV_EXPR:
12471 case CEIL_DIV_EXPR:
12472 case FLOOR_DIV_EXPR:
12473 case ROUND_DIV_EXPR:
12474 case EXACT_DIV_EXPR:
12475 case TRUNC_MOD_EXPR:
12476 case FLOOR_MOD_EXPR:
12477 case RDIV_EXPR:
12478 case EQ_EXPR:
12479 case NE_EXPR:
12480 case LE_EXPR:
12481 case GE_EXPR:
12482 case LT_EXPR:
12483 case GT_EXPR:
12484 /* What about UNLT_EXPR? */
12485 if (VECTOR_TYPE_P (type0))
12487 ret = stv_secondarg;
12488 std::swap (type0, type1);
12489 std::swap (op0, op1);
12492 if (TREE_CODE (type0) == INTEGER_TYPE
12493 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12495 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12497 if (complain)
12498 error_at (loc, "conversion of scalar %qT to vector %qT "
12499 "involves truncation", type0, type1);
12500 return stv_error;
12502 return ret;
12504 else if (!integer_only_op
12505 /* Allow integer --> real conversion if safe. */
12506 && (TREE_CODE (type0) == REAL_TYPE
12507 || TREE_CODE (type0) == INTEGER_TYPE)
12508 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12510 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12512 if (complain)
12513 error_at (loc, "conversion of scalar %qT to vector %qT "
12514 "involves truncation", type0, type1);
12515 return stv_error;
12517 return ret;
12519 default:
12520 break;
12523 return stv_nothing;
12526 /* Return true iff ALIGN is an integral constant that is a fundamental
12527 alignment, as defined by [basic.align] in the c++-11
12528 specifications.
12530 That is:
12532 [A fundamental alignment is represented by an alignment less than or
12533 equal to the greatest alignment supported by the implementation
12534 in all contexts, which is equal to
12535 alignof(max_align_t)]. */
12537 bool
12538 cxx_fundamental_alignment_p (unsigned align)
12540 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12541 TYPE_ALIGN (long_double_type_node)));
12544 /* Return true if T is a pointer to a zero-sized aggregate. */
12546 bool
12547 pointer_to_zero_sized_aggr_p (tree t)
12549 if (!POINTER_TYPE_P (t))
12550 return false;
12551 t = TREE_TYPE (t);
12552 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12555 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12556 with no library fallback or for an ADDR_EXPR whose operand is such type
12557 issues an error pointing to the location LOC.
12558 Returns true when the expression has been diagnosed and false
12559 otherwise. */
12560 bool
12561 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12563 if (TREE_CODE (expr) == ADDR_EXPR)
12564 expr = TREE_OPERAND (expr, 0);
12566 if (TREE_TYPE (expr)
12567 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12568 && DECL_P (expr)
12569 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12570 false positives for user-declared built-ins such as abs or
12571 strlen, and for C++ operators new and delete.
12572 The c_decl_implicit() test avoids false positives for implicitly
12573 declared built-ins with library fallbacks (such as abs). */
12574 && DECL_BUILT_IN (expr)
12575 && DECL_IS_BUILTIN (expr)
12576 && !c_decl_implicit (expr)
12577 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12579 if (loc == UNKNOWN_LOCATION)
12580 loc = EXPR_LOC_OR_LOC (expr, input_location);
12582 /* Reject arguments that are built-in functions with
12583 no library fallback. */
12584 error_at (loc, "built-in function %qE must be directly called", expr);
12586 return true;
12589 return false;
12592 /* If we're creating an if-else-if condition chain, first see if we
12593 already have this COND in the CHAIN. If so, warn and don't add COND
12594 into the vector, otherwise add the COND there. LOC is the location
12595 of COND. */
12597 void
12598 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12600 /* No chain has been created yet. Do nothing. */
12601 if (*chain == NULL)
12602 return;
12604 if (TREE_SIDE_EFFECTS (cond))
12606 /* Uh-oh! This condition has a side-effect, thus invalidates
12607 the whole chain. */
12608 delete *chain;
12609 *chain = NULL;
12610 return;
12613 unsigned int ix;
12614 tree t;
12615 bool found = false;
12616 FOR_EACH_VEC_ELT (**chain, ix, t)
12617 if (operand_equal_p (cond, t, 0))
12619 if (warning_at (loc, OPT_Wduplicated_cond,
12620 "duplicated %<if%> condition"))
12621 inform (EXPR_LOCATION (t), "previously used here");
12622 found = true;
12623 break;
12626 if (!found
12627 && !CONSTANT_CLASS_P (cond)
12628 /* Don't infinitely grow the chain. */
12629 && (*chain)->length () < 512)
12630 (*chain)->safe_push (cond);
12633 /* Check if array size calculations overflow or if the array covers more
12634 than half of the address space. Return true if the size of the array
12635 is valid, false otherwise. TYPE is the type of the array and NAME is
12636 the name of the array, or NULL_TREE for unnamed arrays. */
12638 bool
12639 valid_array_size_p (location_t loc, tree type, tree name)
12641 if (type != error_mark_node
12642 && COMPLETE_TYPE_P (type)
12643 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12644 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
12646 if (name)
12647 error_at (loc, "size of array %qE is too large", name);
12648 else
12649 error_at (loc, "size of unnamed array is too large");
12650 return false;
12652 return true;
12655 #include "gt-c-family-c-common.h"