2013-11-12 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / c-family / c-common.c
blob835d22c6cf90028b7dad2669c96254bdfe13d801
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2013 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 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "intl.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "c-pragma.h"
28 #include "ggc.h"
29 #include "c-common.h"
30 #include "c-objc.h"
31 #include "tm_p.h"
32 #include "obstack.h"
33 #include "cpplib.h"
34 #include "target.h"
35 #include "common/common-target.h"
36 #include "langhooks.h"
37 #include "tree-inline.h"
38 #include "toplev.h"
39 #include "diagnostic.h"
40 #include "tree-iterator.h"
41 #include "hashtab.h"
42 #include "opts.h"
43 #include "cgraph.h"
44 #include "target-def.h"
45 #include "gimplify.h"
47 cpp_reader *parse_in; /* Declared in c-pragma.h. */
49 /* The following symbols are subsumed in the c_global_trees array, and
50 listed here individually for documentation purposes.
52 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
54 tree short_integer_type_node;
55 tree long_integer_type_node;
56 tree long_long_integer_type_node;
57 tree int128_integer_type_node;
59 tree short_unsigned_type_node;
60 tree long_unsigned_type_node;
61 tree long_long_unsigned_type_node;
62 tree int128_unsigned_type_node;
64 tree truthvalue_type_node;
65 tree truthvalue_false_node;
66 tree truthvalue_true_node;
68 tree ptrdiff_type_node;
70 tree unsigned_char_type_node;
71 tree signed_char_type_node;
72 tree wchar_type_node;
74 tree char16_type_node;
75 tree char32_type_node;
77 tree float_type_node;
78 tree double_type_node;
79 tree long_double_type_node;
81 tree complex_integer_type_node;
82 tree complex_float_type_node;
83 tree complex_double_type_node;
84 tree complex_long_double_type_node;
86 tree dfloat32_type_node;
87 tree dfloat64_type_node;
88 tree_dfloat128_type_node;
90 tree intQI_type_node;
91 tree intHI_type_node;
92 tree intSI_type_node;
93 tree intDI_type_node;
94 tree intTI_type_node;
96 tree unsigned_intQI_type_node;
97 tree unsigned_intHI_type_node;
98 tree unsigned_intSI_type_node;
99 tree unsigned_intDI_type_node;
100 tree unsigned_intTI_type_node;
102 tree widest_integer_literal_type_node;
103 tree widest_unsigned_literal_type_node;
105 Nodes for types `void *' and `const void *'.
107 tree ptr_type_node, const_ptr_type_node;
109 Nodes for types `char *' and `const char *'.
111 tree string_type_node, const_string_type_node;
113 Type `char[SOMENUMBER]'.
114 Used when an array of char is needed and the size is irrelevant.
116 tree char_array_type_node;
118 Type `int[SOMENUMBER]' or something like it.
119 Used when an array of int needed and the size is irrelevant.
121 tree int_array_type_node;
123 Type `wchar_t[SOMENUMBER]' or something like it.
124 Used when a wide string literal is created.
126 tree wchar_array_type_node;
128 Type `char16_t[SOMENUMBER]' or something like it.
129 Used when a UTF-16 string literal is created.
131 tree char16_array_type_node;
133 Type `char32_t[SOMENUMBER]' or something like it.
134 Used when a UTF-32 string literal is created.
136 tree char32_array_type_node;
138 Type `int ()' -- used for implicit declaration of functions.
140 tree default_function_type;
142 A VOID_TYPE node, packaged in a TREE_LIST.
144 tree void_list_node;
146 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
147 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
148 VAR_DECLS, but C++ does.)
150 tree function_name_decl_node;
151 tree pretty_function_name_decl_node;
152 tree c99_function_name_decl_node;
154 Stack of nested function name VAR_DECLs.
156 tree saved_function_name_decls;
160 tree c_global_trees[CTI_MAX];
162 /* Switches common to the C front ends. */
164 /* Nonzero means don't output line number information. */
166 char flag_no_line_commands;
168 /* Nonzero causes -E output not to be done, but directives such as
169 #define that have side effects are still obeyed. */
171 char flag_no_output;
173 /* Nonzero means dump macros in some fashion. */
175 char flag_dump_macros;
177 /* Nonzero means pass #include lines through to the output. */
179 char flag_dump_includes;
181 /* Nonzero means process PCH files while preprocessing. */
183 bool flag_pch_preprocess;
185 /* The file name to which we should write a precompiled header, or
186 NULL if no header will be written in this compile. */
188 const char *pch_file;
190 /* Nonzero if an ISO standard was selected. It rejects macros in the
191 user's namespace. */
192 int flag_iso;
194 /* C/ObjC language option variables. */
197 /* Nonzero means allow type mismatches in conditional expressions;
198 just make their values `void'. */
200 int flag_cond_mismatch;
202 /* Nonzero means enable C89 Amendment 1 features. */
204 int flag_isoc94;
206 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
208 int flag_isoc99;
210 /* Nonzero means use the ISO C11 dialect of C. */
212 int flag_isoc11;
214 /* Nonzero means that we have builtin functions, and main is an int. */
216 int flag_hosted = 1;
219 /* ObjC language option variables. */
222 /* Tells the compiler that this is a special run. Do not perform any
223 compiling, instead we are to test some platform dependent features
224 and output a C header file with appropriate definitions. */
226 int print_struct_values;
228 /* Tells the compiler what is the constant string class for ObjC. */
230 const char *constant_string_class_name;
233 /* C++ language option variables. */
236 /* Nonzero means generate separate instantiation control files and
237 juggle them at link time. */
239 int flag_use_repository;
241 /* The C++ dialect being used. C++98 is the default. */
243 enum cxx_dialect cxx_dialect = cxx98;
245 /* Maximum template instantiation depth. This limit exists to limit the
246 time it takes to notice excessively recursive template instantiations.
248 The default is lower than the 1024 recommended by the C++0x standard
249 because G++ runs out of stack before 1024 with highly recursive template
250 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
252 int max_tinst_depth = 900;
254 /* The elements of `ridpointers' are identifier nodes for the reserved
255 type names and storage classes. It is indexed by a RID_... value. */
256 tree *ridpointers;
258 tree (*make_fname_decl) (location_t, tree, int);
260 /* Nonzero means don't warn about problems that occur when the code is
261 executed. */
262 int c_inhibit_evaluation_warnings;
264 /* Whether we are building a boolean conversion inside
265 convert_for_assignment, or some other late binary operation. If
266 build_binary_op is called for C (from code shared by C and C++) in
267 this case, then the operands have already been folded and the
268 result will not be folded again, so C_MAYBE_CONST_EXPR should not
269 be generated. */
270 bool in_late_binary_op;
272 /* Whether lexing has been completed, so subsequent preprocessor
273 errors should use the compiler's input_location. */
274 bool done_lexing = false;
276 /* Information about how a function name is generated. */
277 struct fname_var_t
279 tree *const decl; /* pointer to the VAR_DECL. */
280 const unsigned rid; /* RID number for the identifier. */
281 const int pretty; /* How pretty is it? */
284 /* The three ways of getting then name of the current function. */
286 const struct fname_var_t fname_vars[] =
288 /* C99 compliant __func__, must be first. */
289 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
290 /* GCC __FUNCTION__ compliant. */
291 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
292 /* GCC __PRETTY_FUNCTION__ compliant. */
293 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
294 {NULL, 0, 0},
297 /* Global visibility options. */
298 struct visibility_flags visibility_options;
300 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
301 static tree check_case_value (tree);
302 static bool check_case_bounds (tree, tree, tree *, tree *);
304 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
305 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
306 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
311 int, bool *);
312 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
313 int, bool *);
314 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
315 bool *);
316 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_always_inline_attribute (tree *, tree, tree, int,
320 bool *);
321 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
328 bool *);
329 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_transparent_union_attribute (tree *, tree, tree,
331 int, bool *);
332 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
338 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
339 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
342 static tree handle_visibility_attribute (tree *, tree, tree, int,
343 bool *);
344 static tree handle_tls_model_attribute (tree *, tree, tree, int,
345 bool *);
346 static tree handle_no_instrument_function_attribute (tree *, tree,
347 tree, int, bool *);
348 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
351 bool *);
352 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_deprecated_attribute (tree *, tree, tree, int,
357 bool *);
358 static tree handle_vector_size_attribute (tree *, tree, tree, int,
359 bool *);
360 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
370 static tree ignore_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
376 bool *);
377 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
378 bool *);
379 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
382 static void check_function_nonnull (tree, int, tree *);
383 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
384 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
385 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
386 static int resort_field_decl_cmp (const void *, const void *);
388 /* Reserved words. The third field is a mask: keywords are disabled
389 if they match the mask.
391 Masks for languages:
392 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
393 C --std=c99: D_CXXONLY | D_OBJC
394 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
395 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
396 C++ --std=c0x: D_CONLY | D_OBJC
397 ObjC++ is like C++ except that D_OBJC is not set
399 If -fno-asm is used, D_ASM is added to the mask. If
400 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
401 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
402 In C with -Wc++-compat, we warn if D_CXXWARN is set.
404 Note the complication of the D_CXX_OBJC keywords. These are
405 reserved words such as 'class'. In C++, 'class' is a reserved
406 word. In Objective-C++ it is too. In Objective-C, it is a
407 reserved word too, but only if it follows an '@' sign.
409 const struct c_common_resword c_common_reswords[] =
411 { "_Alignas", RID_ALIGNAS, D_CONLY },
412 { "_Alignof", RID_ALIGNOF, D_CONLY },
413 { "_Atomic", RID_ATOMIC, D_CONLY },
414 { "_Bool", RID_BOOL, D_CONLY },
415 { "_Complex", RID_COMPLEX, 0 },
416 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
417 { "_Cilk_sync", RID_CILK_SYNC, 0 },
418 { "_Imaginary", RID_IMAGINARY, D_CONLY },
419 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
420 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
421 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
422 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
423 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
424 { "_Sat", RID_SAT, D_CONLY | D_EXT },
425 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
426 { "_Noreturn", RID_NORETURN, D_CONLY },
427 { "_Generic", RID_GENERIC, D_CONLY },
428 { "_Thread_local", RID_THREAD, D_CONLY },
429 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
430 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
431 { "__alignof", RID_ALIGNOF, 0 },
432 { "__alignof__", RID_ALIGNOF, 0 },
433 { "__asm", RID_ASM, 0 },
434 { "__asm__", RID_ASM, 0 },
435 { "__attribute", RID_ATTRIBUTE, 0 },
436 { "__attribute__", RID_ATTRIBUTE, 0 },
437 { "__bases", RID_BASES, D_CXXONLY },
438 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
439 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
440 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
441 { "__builtin_offsetof", RID_OFFSETOF, 0 },
442 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
443 { "__builtin_va_arg", RID_VA_ARG, 0 },
444 { "__complex", RID_COMPLEX, 0 },
445 { "__complex__", RID_COMPLEX, 0 },
446 { "__const", RID_CONST, 0 },
447 { "__const__", RID_CONST, 0 },
448 { "__decltype", RID_DECLTYPE, D_CXXONLY },
449 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
450 { "__extension__", RID_EXTENSION, 0 },
451 { "__func__", RID_C99_FUNCTION_NAME, 0 },
452 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
453 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
454 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
455 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
456 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
457 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
458 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
459 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
460 { "__imag", RID_IMAGPART, 0 },
461 { "__imag__", RID_IMAGPART, 0 },
462 { "__inline", RID_INLINE, 0 },
463 { "__inline__", RID_INLINE, 0 },
464 { "__int128", RID_INT128, 0 },
465 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
466 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
467 { "__is_class", RID_IS_CLASS, D_CXXONLY },
468 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
469 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
470 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
471 { "__is_final", RID_IS_FINAL, D_CXXONLY },
472 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
473 { "__is_pod", RID_IS_POD, D_CXXONLY },
474 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
475 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
476 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
477 { "__is_union", RID_IS_UNION, D_CXXONLY },
478 { "__label__", RID_LABEL, 0 },
479 { "__null", RID_NULL, 0 },
480 { "__real", RID_REALPART, 0 },
481 { "__real__", RID_REALPART, 0 },
482 { "__restrict", RID_RESTRICT, 0 },
483 { "__restrict__", RID_RESTRICT, 0 },
484 { "__signed", RID_SIGNED, 0 },
485 { "__signed__", RID_SIGNED, 0 },
486 { "__thread", RID_THREAD, 0 },
487 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
488 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
489 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
490 { "__typeof", RID_TYPEOF, 0 },
491 { "__typeof__", RID_TYPEOF, 0 },
492 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
493 { "__volatile", RID_VOLATILE, 0 },
494 { "__volatile__", RID_VOLATILE, 0 },
495 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
496 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
497 { "asm", RID_ASM, D_ASM },
498 { "auto", RID_AUTO, 0 },
499 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
500 { "break", RID_BREAK, 0 },
501 { "case", RID_CASE, 0 },
502 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
503 { "char", RID_CHAR, 0 },
504 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
505 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
506 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
507 { "const", RID_CONST, 0 },
508 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
509 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
510 { "continue", RID_CONTINUE, 0 },
511 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
512 { "default", RID_DEFAULT, 0 },
513 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
514 { "do", RID_DO, 0 },
515 { "double", RID_DOUBLE, 0 },
516 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
517 { "else", RID_ELSE, 0 },
518 { "enum", RID_ENUM, 0 },
519 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
520 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
521 { "extern", RID_EXTERN, 0 },
522 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
523 { "float", RID_FLOAT, 0 },
524 { "for", RID_FOR, 0 },
525 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
526 { "goto", RID_GOTO, 0 },
527 { "if", RID_IF, 0 },
528 { "inline", RID_INLINE, D_EXT89 },
529 { "int", RID_INT, 0 },
530 { "long", RID_LONG, 0 },
531 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
532 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
533 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
534 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
535 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
536 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
537 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
538 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
539 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
540 { "register", RID_REGISTER, 0 },
541 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
542 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
543 { "return", RID_RETURN, 0 },
544 { "short", RID_SHORT, 0 },
545 { "signed", RID_SIGNED, 0 },
546 { "sizeof", RID_SIZEOF, 0 },
547 { "static", RID_STATIC, 0 },
548 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
549 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
550 { "struct", RID_STRUCT, 0 },
551 { "switch", RID_SWITCH, 0 },
552 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
553 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
554 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
555 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
556 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
557 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
558 { "typedef", RID_TYPEDEF, 0 },
559 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
560 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
561 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
562 { "union", RID_UNION, 0 },
563 { "unsigned", RID_UNSIGNED, 0 },
564 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
565 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
566 { "void", RID_VOID, 0 },
567 { "volatile", RID_VOLATILE, 0 },
568 { "wchar_t", RID_WCHAR, D_CXXONLY },
569 { "while", RID_WHILE, 0 },
570 /* These Objective-C keywords are recognized only immediately after
571 an '@'. */
572 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
573 { "defs", RID_AT_DEFS, D_OBJC },
574 { "encode", RID_AT_ENCODE, D_OBJC },
575 { "end", RID_AT_END, D_OBJC },
576 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
577 { "interface", RID_AT_INTERFACE, D_OBJC },
578 { "protocol", RID_AT_PROTOCOL, D_OBJC },
579 { "selector", RID_AT_SELECTOR, D_OBJC },
580 { "finally", RID_AT_FINALLY, D_OBJC },
581 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
582 { "optional", RID_AT_OPTIONAL, D_OBJC },
583 { "required", RID_AT_REQUIRED, D_OBJC },
584 { "property", RID_AT_PROPERTY, D_OBJC },
585 { "package", RID_AT_PACKAGE, D_OBJC },
586 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
587 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
588 /* These are recognized only in protocol-qualifier context
589 (see above) */
590 { "bycopy", RID_BYCOPY, D_OBJC },
591 { "byref", RID_BYREF, D_OBJC },
592 { "in", RID_IN, D_OBJC },
593 { "inout", RID_INOUT, D_OBJC },
594 { "oneway", RID_ONEWAY, D_OBJC },
595 { "out", RID_OUT, D_OBJC },
596 /* These are recognized inside a property attribute list */
597 { "assign", RID_ASSIGN, D_OBJC },
598 { "copy", RID_COPY, D_OBJC },
599 { "getter", RID_GETTER, D_OBJC },
600 { "nonatomic", RID_NONATOMIC, D_OBJC },
601 { "readonly", RID_READONLY, D_OBJC },
602 { "readwrite", RID_READWRITE, D_OBJC },
603 { "retain", RID_RETAIN, D_OBJC },
604 { "setter", RID_SETTER, D_OBJC },
607 const unsigned int num_c_common_reswords =
608 sizeof c_common_reswords / sizeof (struct c_common_resword);
610 /* Table of machine-independent attributes common to all C-like languages. */
611 const struct attribute_spec c_common_attribute_table[] =
613 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
614 affects_type_identity } */
615 { "packed", 0, 0, false, false, false,
616 handle_packed_attribute , false},
617 { "nocommon", 0, 0, true, false, false,
618 handle_nocommon_attribute, false},
619 { "common", 0, 0, true, false, false,
620 handle_common_attribute, false },
621 /* FIXME: logically, noreturn attributes should be listed as
622 "false, true, true" and apply to function types. But implementing this
623 would require all the places in the compiler that use TREE_THIS_VOLATILE
624 on a decl to identify non-returning functions to be located and fixed
625 to check the function type instead. */
626 { "noreturn", 0, 0, true, false, false,
627 handle_noreturn_attribute, false },
628 { "volatile", 0, 0, true, false, false,
629 handle_noreturn_attribute, false },
630 { "noinline", 0, 0, true, false, false,
631 handle_noinline_attribute, false },
632 { "noclone", 0, 0, true, false, false,
633 handle_noclone_attribute, false },
634 { "leaf", 0, 0, true, false, false,
635 handle_leaf_attribute, false },
636 { "always_inline", 0, 0, true, false, false,
637 handle_always_inline_attribute, false },
638 { "gnu_inline", 0, 0, true, false, false,
639 handle_gnu_inline_attribute, false },
640 { "artificial", 0, 0, true, false, false,
641 handle_artificial_attribute, false },
642 { "flatten", 0, 0, true, false, false,
643 handle_flatten_attribute, false },
644 { "used", 0, 0, true, false, false,
645 handle_used_attribute, false },
646 { "unused", 0, 0, false, false, false,
647 handle_unused_attribute, false },
648 { "externally_visible", 0, 0, true, false, false,
649 handle_externally_visible_attribute, false },
650 /* The same comments as for noreturn attributes apply to const ones. */
651 { "const", 0, 0, true, false, false,
652 handle_const_attribute, false },
653 { "transparent_union", 0, 0, false, false, false,
654 handle_transparent_union_attribute, false },
655 { "constructor", 0, 1, true, false, false,
656 handle_constructor_attribute, false },
657 { "destructor", 0, 1, true, false, false,
658 handle_destructor_attribute, false },
659 { "mode", 1, 1, false, true, false,
660 handle_mode_attribute, false },
661 { "section", 1, 1, true, false, false,
662 handle_section_attribute, false },
663 { "aligned", 0, 1, false, false, false,
664 handle_aligned_attribute, false },
665 { "weak", 0, 0, true, false, false,
666 handle_weak_attribute, false },
667 { "ifunc", 1, 1, true, false, false,
668 handle_ifunc_attribute, false },
669 { "alias", 1, 1, true, false, false,
670 handle_alias_attribute, false },
671 { "weakref", 0, 1, true, false, false,
672 handle_weakref_attribute, false },
673 { "no_instrument_function", 0, 0, true, false, false,
674 handle_no_instrument_function_attribute,
675 false },
676 { "malloc", 0, 0, true, false, false,
677 handle_malloc_attribute, false },
678 { "returns_twice", 0, 0, true, false, false,
679 handle_returns_twice_attribute, false },
680 { "no_stack_limit", 0, 0, true, false, false,
681 handle_no_limit_stack_attribute, false },
682 { "pure", 0, 0, true, false, false,
683 handle_pure_attribute, false },
684 { "transaction_callable", 0, 0, false, true, false,
685 handle_tm_attribute, false },
686 { "transaction_unsafe", 0, 0, false, true, false,
687 handle_tm_attribute, false },
688 { "transaction_safe", 0, 0, false, true, false,
689 handle_tm_attribute, false },
690 { "transaction_may_cancel_outer", 0, 0, false, true, false,
691 handle_tm_attribute, false },
692 /* ??? These two attributes didn't make the transition from the
693 Intel language document to the multi-vendor language document. */
694 { "transaction_pure", 0, 0, false, true, false,
695 handle_tm_attribute, false },
696 { "transaction_wrap", 1, 1, true, false, false,
697 handle_tm_wrap_attribute, false },
698 /* For internal use (marking of builtins) only. The name contains space
699 to prevent its usage in source code. */
700 { "no vops", 0, 0, true, false, false,
701 handle_novops_attribute, false },
702 { "deprecated", 0, 1, false, false, false,
703 handle_deprecated_attribute, false },
704 { "vector_size", 1, 1, false, true, false,
705 handle_vector_size_attribute, false },
706 { "visibility", 1, 1, false, false, false,
707 handle_visibility_attribute, false },
708 { "tls_model", 1, 1, true, false, false,
709 handle_tls_model_attribute, false },
710 { "nonnull", 0, -1, false, true, true,
711 handle_nonnull_attribute, false },
712 { "nothrow", 0, 0, true, false, false,
713 handle_nothrow_attribute, false },
714 { "may_alias", 0, 0, false, true, false, NULL, false },
715 { "cleanup", 1, 1, true, false, false,
716 handle_cleanup_attribute, false },
717 { "warn_unused_result", 0, 0, false, true, true,
718 handle_warn_unused_result_attribute, false },
719 { "sentinel", 0, 1, false, true, true,
720 handle_sentinel_attribute, false },
721 /* For internal use (marking of builtins) only. The name contains space
722 to prevent its usage in source code. */
723 { "type generic", 0, 0, false, true, true,
724 handle_type_generic_attribute, false },
725 { "alloc_size", 1, 2, false, true, true,
726 handle_alloc_size_attribute, false },
727 { "cold", 0, 0, true, false, false,
728 handle_cold_attribute, false },
729 { "hot", 0, 0, true, false, false,
730 handle_hot_attribute, false },
731 { "no_address_safety_analysis",
732 0, 0, true, false, false,
733 handle_no_address_safety_analysis_attribute,
734 false },
735 { "no_sanitize_address", 0, 0, true, false, false,
736 handle_no_sanitize_address_attribute,
737 false },
738 { "no_sanitize_undefined", 0, 0, true, false, false,
739 handle_no_sanitize_undefined_attribute,
740 false },
741 { "warning", 1, 1, true, false, false,
742 handle_error_attribute, false },
743 { "error", 1, 1, true, false, false,
744 handle_error_attribute, false },
745 { "target", 1, -1, true, false, false,
746 handle_target_attribute, false },
747 { "optimize", 1, -1, true, false, false,
748 handle_optimize_attribute, false },
749 /* For internal use only. The leading '*' both prevents its usage in
750 source code and signals that it may be overridden by machine tables. */
751 { "*tm regparm", 0, 0, false, true, true,
752 ignore_attribute, false },
753 { "no_split_stack", 0, 0, true, false, false,
754 handle_no_split_stack_attribute, false },
755 /* For internal use (marking of builtins and runtime functions) only.
756 The name contains space to prevent its usage in source code. */
757 { "fn spec", 1, 1, false, true, true,
758 handle_fnspec_attribute, false },
759 { "warn_unused", 0, 0, false, false, false,
760 handle_warn_unused_attribute, false },
761 { "returns_nonnull", 0, 0, false, true, true,
762 handle_returns_nonnull_attribute, false },
763 { "omp declare simd", 0, -1, true, false, false,
764 handle_omp_declare_simd_attribute, false },
765 { "omp declare target", 0, 0, true, false, false,
766 handle_omp_declare_target_attribute, false },
767 { "bnd_variable_size", 0, 0, true, false, false,
768 handle_bnd_variable_size_attribute, false },
769 { "bnd_legacy", 0, 0, true, false, false,
770 handle_bnd_legacy, false },
771 { NULL, 0, 0, false, false, false, NULL, false }
774 /* Give the specifications for the format attributes, used by C and all
775 descendants. */
777 const struct attribute_spec c_common_format_attribute_table[] =
779 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
780 affects_type_identity } */
781 { "format", 3, 3, false, true, true,
782 handle_format_attribute, false },
783 { "format_arg", 1, 1, false, true, true,
784 handle_format_arg_attribute, false },
785 { NULL, 0, 0, false, false, false, NULL, false }
788 /* Return identifier for address space AS. */
790 const char *
791 c_addr_space_name (addr_space_t as)
793 int rid = RID_FIRST_ADDR_SPACE + as;
794 gcc_assert (ridpointers [rid]);
795 return IDENTIFIER_POINTER (ridpointers [rid]);
798 /* Push current bindings for the function name VAR_DECLS. */
800 void
801 start_fname_decls (void)
803 unsigned ix;
804 tree saved = NULL_TREE;
806 for (ix = 0; fname_vars[ix].decl; ix++)
808 tree decl = *fname_vars[ix].decl;
810 if (decl)
812 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
813 saved);
814 *fname_vars[ix].decl = NULL_TREE;
817 if (saved || saved_function_name_decls)
818 /* Normally they'll have been NULL, so only push if we've got a
819 stack, or they are non-NULL. */
820 saved_function_name_decls = tree_cons (saved, NULL_TREE,
821 saved_function_name_decls);
824 /* Finish up the current bindings, adding them into the current function's
825 statement tree. This must be done _before_ finish_stmt_tree is called.
826 If there is no current function, we must be at file scope and no statements
827 are involved. Pop the previous bindings. */
829 void
830 finish_fname_decls (void)
832 unsigned ix;
833 tree stmts = NULL_TREE;
834 tree stack = saved_function_name_decls;
836 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
837 append_to_statement_list (TREE_VALUE (stack), &stmts);
839 if (stmts)
841 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
843 if (TREE_CODE (*bodyp) == BIND_EXPR)
844 bodyp = &BIND_EXPR_BODY (*bodyp);
846 append_to_statement_list_force (*bodyp, &stmts);
847 *bodyp = stmts;
850 for (ix = 0; fname_vars[ix].decl; ix++)
851 *fname_vars[ix].decl = NULL_TREE;
853 if (stack)
855 /* We had saved values, restore them. */
856 tree saved;
858 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
860 tree decl = TREE_PURPOSE (saved);
861 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
863 *fname_vars[ix].decl = decl;
865 stack = TREE_CHAIN (stack);
867 saved_function_name_decls = stack;
870 /* Return the text name of the current function, suitably prettified
871 by PRETTY_P. Return string must be freed by caller. */
873 const char *
874 fname_as_string (int pretty_p)
876 const char *name = "top level";
877 char *namep;
878 int vrb = 2, len;
879 cpp_string cstr = { 0, 0 }, strname;
881 if (!pretty_p)
883 name = "";
884 vrb = 0;
887 if (current_function_decl)
888 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
890 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
892 namep = XNEWVEC (char, len);
893 snprintf (namep, len, "\"%s\"", name);
894 strname.text = (unsigned char *) namep;
895 strname.len = len - 1;
897 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
899 XDELETEVEC (namep);
900 return (const char *) cstr.text;
903 return namep;
906 /* Return the VAR_DECL for a const char array naming the current
907 function. If the VAR_DECL has not yet been created, create it
908 now. RID indicates how it should be formatted and IDENTIFIER_NODE
909 ID is its name (unfortunately C and C++ hold the RID values of
910 keywords in different places, so we can't derive RID from ID in
911 this language independent code. LOC is the location of the
912 function. */
914 tree
915 fname_decl (location_t loc, unsigned int rid, tree id)
917 unsigned ix;
918 tree decl = NULL_TREE;
920 for (ix = 0; fname_vars[ix].decl; ix++)
921 if (fname_vars[ix].rid == rid)
922 break;
924 decl = *fname_vars[ix].decl;
925 if (!decl)
927 /* If a tree is built here, it would normally have the lineno of
928 the current statement. Later this tree will be moved to the
929 beginning of the function and this line number will be wrong.
930 To avoid this problem set the lineno to 0 here; that prevents
931 it from appearing in the RTL. */
932 tree stmts;
933 location_t saved_location = input_location;
934 input_location = UNKNOWN_LOCATION;
936 stmts = push_stmt_list ();
937 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
938 stmts = pop_stmt_list (stmts);
939 if (!IS_EMPTY_STMT (stmts))
940 saved_function_name_decls
941 = tree_cons (decl, stmts, saved_function_name_decls);
942 *fname_vars[ix].decl = decl;
943 input_location = saved_location;
945 if (!ix && !current_function_decl)
946 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
948 return decl;
951 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
953 tree
954 fix_string_type (tree value)
956 int length = TREE_STRING_LENGTH (value);
957 int nchars;
958 tree e_type, i_type, a_type;
960 /* Compute the number of elements, for the array type. */
961 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
963 nchars = length;
964 e_type = char_type_node;
966 else if (TREE_TYPE (value) == char16_array_type_node)
968 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
969 e_type = char16_type_node;
971 else if (TREE_TYPE (value) == char32_array_type_node)
973 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
974 e_type = char32_type_node;
976 else
978 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
979 e_type = wchar_type_node;
982 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
983 limit in C++98 Annex B is very large (65536) and is not normative,
984 so we do not diagnose it (warn_overlength_strings is forced off
985 in c_common_post_options). */
986 if (warn_overlength_strings)
988 const int nchars_max = flag_isoc99 ? 4095 : 509;
989 const int relevant_std = flag_isoc99 ? 99 : 90;
990 if (nchars - 1 > nchars_max)
991 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
992 separate the %d from the 'C'. 'ISO' should not be
993 translated, but it may be moved after 'C%d' in languages
994 where modifiers follow nouns. */
995 pedwarn (input_location, OPT_Woverlength_strings,
996 "string length %qd is greater than the length %qd "
997 "ISO C%d compilers are required to support",
998 nchars - 1, nchars_max, relevant_std);
1001 /* Create the array type for the string constant. The ISO C++
1002 standard says that a string literal has type `const char[N]' or
1003 `const wchar_t[N]'. We use the same logic when invoked as a C
1004 front-end with -Wwrite-strings.
1005 ??? We should change the type of an expression depending on the
1006 state of a warning flag. We should just be warning -- see how
1007 this is handled in the C++ front-end for the deprecated implicit
1008 conversion from string literals to `char*' or `wchar_t*'.
1010 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1011 array type being the unqualified version of that type.
1012 Therefore, if we are constructing an array of const char, we must
1013 construct the matching unqualified array type first. The C front
1014 end does not require this, but it does no harm, so we do it
1015 unconditionally. */
1016 i_type = build_index_type (size_int (nchars - 1));
1017 a_type = build_array_type (e_type, i_type);
1018 if (c_dialect_cxx() || warn_write_strings)
1019 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1021 TREE_TYPE (value) = a_type;
1022 TREE_CONSTANT (value) = 1;
1023 TREE_READONLY (value) = 1;
1024 TREE_STATIC (value) = 1;
1025 return value;
1028 /* If DISABLE is true, stop issuing warnings. This is used when
1029 parsing code that we know will not be executed. This function may
1030 be called multiple times, and works as a stack. */
1032 static void
1033 c_disable_warnings (bool disable)
1035 if (disable)
1037 ++c_inhibit_evaluation_warnings;
1038 fold_defer_overflow_warnings ();
1042 /* If ENABLE is true, reenable issuing warnings. */
1044 static void
1045 c_enable_warnings (bool enable)
1047 if (enable)
1049 --c_inhibit_evaluation_warnings;
1050 fold_undefer_and_ignore_overflow_warnings ();
1054 /* Fully fold EXPR, an expression that was not folded (beyond integer
1055 constant expressions and null pointer constants) when being built
1056 up. If IN_INIT, this is in a static initializer and certain
1057 changes are made to the folding done. Clear *MAYBE_CONST if
1058 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1059 expression because it contains an evaluated operator (in C99) or an
1060 operator outside of sizeof returning an integer constant (in C90)
1061 not permitted in constant expressions, or because it contains an
1062 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1063 set to true by callers before calling this function.) Return the
1064 folded expression. Function arguments have already been folded
1065 before calling this function, as have the contents of SAVE_EXPR,
1066 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1067 C_MAYBE_CONST_EXPR. */
1069 tree
1070 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1072 tree ret;
1073 tree eptype = NULL_TREE;
1074 bool dummy = true;
1075 bool maybe_const_itself = true;
1076 location_t loc = EXPR_LOCATION (expr);
1078 /* This function is not relevant to C++ because C++ folds while
1079 parsing, and may need changes to be correct for C++ when C++
1080 stops folding while parsing. */
1081 if (c_dialect_cxx ())
1082 gcc_unreachable ();
1084 if (!maybe_const)
1085 maybe_const = &dummy;
1086 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1088 eptype = TREE_TYPE (expr);
1089 expr = TREE_OPERAND (expr, 0);
1091 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1092 &maybe_const_itself);
1093 if (eptype)
1094 ret = fold_convert_loc (loc, eptype, ret);
1095 *maybe_const &= maybe_const_itself;
1096 return ret;
1099 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1100 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1101 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1102 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1103 both evaluated and unevaluated subexpressions while
1104 *MAYBE_CONST_ITSELF is carried from only evaluated
1105 subexpressions). */
1107 static tree
1108 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1109 bool *maybe_const_itself)
1111 tree ret = expr;
1112 enum tree_code code = TREE_CODE (expr);
1113 enum tree_code_class kind = TREE_CODE_CLASS (code);
1114 location_t loc = EXPR_LOCATION (expr);
1115 tree op0, op1, op2, op3;
1116 tree orig_op0, orig_op1, orig_op2;
1117 bool op0_const = true, op1_const = true, op2_const = true;
1118 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1119 bool nowarning = TREE_NO_WARNING (expr);
1120 bool unused_p;
1122 /* This function is not relevant to C++ because C++ folds while
1123 parsing, and may need changes to be correct for C++ when C++
1124 stops folding while parsing. */
1125 if (c_dialect_cxx ())
1126 gcc_unreachable ();
1128 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1129 anything else not counted as an expression cannot usefully be
1130 folded further at this point. */
1131 if (!IS_EXPR_CODE_CLASS (kind)
1132 || kind == tcc_statement
1133 || code == SAVE_EXPR)
1134 return expr;
1136 /* Operands of variable-length expressions (function calls) have
1137 already been folded, as have __builtin_* function calls, and such
1138 expressions cannot occur in constant expressions. */
1139 if (kind == tcc_vl_exp)
1141 *maybe_const_operands = false;
1142 ret = fold (expr);
1143 goto out;
1146 if (code == C_MAYBE_CONST_EXPR)
1148 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1149 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1150 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1151 *maybe_const_operands = false;
1152 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1153 *maybe_const_itself = false;
1154 if (pre && !in_init)
1155 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1156 else
1157 ret = inner;
1158 goto out;
1161 /* Assignment, increment, decrement, function call and comma
1162 operators, and statement expressions, cannot occur in constant
1163 expressions if evaluated / outside of sizeof. (Function calls
1164 were handled above, though VA_ARG_EXPR is treated like a function
1165 call here, and statement expressions are handled through
1166 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1167 switch (code)
1169 case MODIFY_EXPR:
1170 case PREDECREMENT_EXPR:
1171 case PREINCREMENT_EXPR:
1172 case POSTDECREMENT_EXPR:
1173 case POSTINCREMENT_EXPR:
1174 case COMPOUND_EXPR:
1175 *maybe_const_operands = false;
1176 break;
1178 case VA_ARG_EXPR:
1179 case TARGET_EXPR:
1180 case BIND_EXPR:
1181 case OBJ_TYPE_REF:
1182 *maybe_const_operands = false;
1183 ret = fold (expr);
1184 goto out;
1186 default:
1187 break;
1190 /* Fold individual tree codes as appropriate. */
1191 switch (code)
1193 case COMPOUND_LITERAL_EXPR:
1194 /* Any non-constancy will have been marked in a containing
1195 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1196 goto out;
1198 case COMPONENT_REF:
1199 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1200 op1 = TREE_OPERAND (expr, 1);
1201 op2 = TREE_OPERAND (expr, 2);
1202 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1203 maybe_const_itself);
1204 STRIP_TYPE_NOPS (op0);
1205 if (op0 != orig_op0)
1206 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1207 if (ret != expr)
1209 TREE_READONLY (ret) = TREE_READONLY (expr);
1210 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1212 goto out;
1214 case ARRAY_REF:
1215 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1216 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1217 op2 = TREE_OPERAND (expr, 2);
1218 op3 = TREE_OPERAND (expr, 3);
1219 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1220 maybe_const_itself);
1221 STRIP_TYPE_NOPS (op0);
1222 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1223 maybe_const_itself);
1224 STRIP_TYPE_NOPS (op1);
1225 op1 = decl_constant_value_for_optimization (op1);
1226 if (op0 != orig_op0 || op1 != orig_op1)
1227 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1228 if (ret != expr)
1230 TREE_READONLY (ret) = TREE_READONLY (expr);
1231 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1232 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1234 ret = fold (ret);
1235 goto out;
1237 case COMPOUND_EXPR:
1238 case MODIFY_EXPR:
1239 case PREDECREMENT_EXPR:
1240 case PREINCREMENT_EXPR:
1241 case POSTDECREMENT_EXPR:
1242 case POSTINCREMENT_EXPR:
1243 case PLUS_EXPR:
1244 case MINUS_EXPR:
1245 case MULT_EXPR:
1246 case POINTER_PLUS_EXPR:
1247 case TRUNC_DIV_EXPR:
1248 case CEIL_DIV_EXPR:
1249 case FLOOR_DIV_EXPR:
1250 case TRUNC_MOD_EXPR:
1251 case RDIV_EXPR:
1252 case EXACT_DIV_EXPR:
1253 case LSHIFT_EXPR:
1254 case RSHIFT_EXPR:
1255 case BIT_IOR_EXPR:
1256 case BIT_XOR_EXPR:
1257 case BIT_AND_EXPR:
1258 case LT_EXPR:
1259 case LE_EXPR:
1260 case GT_EXPR:
1261 case GE_EXPR:
1262 case EQ_EXPR:
1263 case NE_EXPR:
1264 case COMPLEX_EXPR:
1265 case TRUTH_AND_EXPR:
1266 case TRUTH_OR_EXPR:
1267 case TRUTH_XOR_EXPR:
1268 case UNORDERED_EXPR:
1269 case ORDERED_EXPR:
1270 case UNLT_EXPR:
1271 case UNLE_EXPR:
1272 case UNGT_EXPR:
1273 case UNGE_EXPR:
1274 case UNEQ_EXPR:
1275 /* Binary operations evaluating both arguments (increment and
1276 decrement are binary internally in GCC). */
1277 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1278 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1279 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1280 maybe_const_itself);
1281 STRIP_TYPE_NOPS (op0);
1282 if (code != MODIFY_EXPR
1283 && code != PREDECREMENT_EXPR
1284 && code != PREINCREMENT_EXPR
1285 && code != POSTDECREMENT_EXPR
1286 && code != POSTINCREMENT_EXPR)
1287 op0 = decl_constant_value_for_optimization (op0);
1288 /* The RHS of a MODIFY_EXPR was fully folded when building that
1289 expression for the sake of conversion warnings. */
1290 if (code != MODIFY_EXPR)
1291 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1292 maybe_const_itself);
1293 STRIP_TYPE_NOPS (op1);
1294 op1 = decl_constant_value_for_optimization (op1);
1295 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1296 ret = in_init
1297 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1298 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1299 else
1300 ret = fold (expr);
1301 if (TREE_OVERFLOW_P (ret)
1302 && !TREE_OVERFLOW_P (op0)
1303 && !TREE_OVERFLOW_P (op1))
1304 overflow_warning (EXPR_LOCATION (expr), ret);
1305 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1306 && TREE_CODE (orig_op1) != INTEGER_CST
1307 && TREE_CODE (op1) == INTEGER_CST
1308 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1309 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1310 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1311 && c_inhibit_evaluation_warnings == 0)
1313 if (tree_int_cst_sgn (op1) < 0)
1314 warning_at (loc, 0, (code == LSHIFT_EXPR
1315 ? G_("left shift count is negative")
1316 : G_("right shift count is negative")));
1317 else if (compare_tree_int (op1,
1318 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1319 >= 0)
1320 warning_at (loc, 0, (code == LSHIFT_EXPR
1321 ? G_("left shift count >= width of type")
1322 : G_("right shift count >= width of type")));
1324 goto out;
1326 case INDIRECT_REF:
1327 case FIX_TRUNC_EXPR:
1328 case FLOAT_EXPR:
1329 CASE_CONVERT:
1330 case VIEW_CONVERT_EXPR:
1331 case NON_LVALUE_EXPR:
1332 case NEGATE_EXPR:
1333 case BIT_NOT_EXPR:
1334 case TRUTH_NOT_EXPR:
1335 case ADDR_EXPR:
1336 case CONJ_EXPR:
1337 case REALPART_EXPR:
1338 case IMAGPART_EXPR:
1339 /* Unary operations. */
1340 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1341 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1342 maybe_const_itself);
1343 STRIP_TYPE_NOPS (op0);
1344 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1345 op0 = decl_constant_value_for_optimization (op0);
1346 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1347 not prepared to deal with them if they occur in initializers. */
1348 if (op0 != orig_op0
1349 && code == ADDR_EXPR
1350 && (op1 = get_base_address (op0)) != NULL_TREE
1351 && TREE_CODE (op1) == INDIRECT_REF
1352 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1353 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1354 else if (op0 != orig_op0 || in_init)
1355 ret = in_init
1356 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1357 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1358 else
1359 ret = fold (expr);
1360 if (code == INDIRECT_REF
1361 && ret != expr
1362 && TREE_CODE (ret) == INDIRECT_REF)
1364 TREE_READONLY (ret) = TREE_READONLY (expr);
1365 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1366 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1368 switch (code)
1370 case FIX_TRUNC_EXPR:
1371 case FLOAT_EXPR:
1372 CASE_CONVERT:
1373 /* Don't warn about explicit conversions. We will already
1374 have warned about suspect implicit conversions. */
1375 break;
1377 default:
1378 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1379 overflow_warning (EXPR_LOCATION (expr), ret);
1380 break;
1382 goto out;
1384 case TRUTH_ANDIF_EXPR:
1385 case TRUTH_ORIF_EXPR:
1386 /* Binary operations not necessarily evaluating both
1387 arguments. */
1388 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1389 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1390 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1391 STRIP_TYPE_NOPS (op0);
1393 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1394 ? truthvalue_false_node
1395 : truthvalue_true_node));
1396 c_disable_warnings (unused_p);
1397 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1398 STRIP_TYPE_NOPS (op1);
1399 c_enable_warnings (unused_p);
1401 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1402 ret = in_init
1403 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1404 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1405 else
1406 ret = fold (expr);
1407 *maybe_const_operands &= op0_const;
1408 *maybe_const_itself &= op0_const_self;
1409 if (!(flag_isoc99
1410 && op0_const
1411 && op0_const_self
1412 && (code == TRUTH_ANDIF_EXPR
1413 ? op0 == truthvalue_false_node
1414 : op0 == truthvalue_true_node)))
1415 *maybe_const_operands &= op1_const;
1416 if (!(op0_const
1417 && op0_const_self
1418 && (code == TRUTH_ANDIF_EXPR
1419 ? op0 == truthvalue_false_node
1420 : op0 == truthvalue_true_node)))
1421 *maybe_const_itself &= op1_const_self;
1422 goto out;
1424 case COND_EXPR:
1425 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1426 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1427 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1428 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1430 STRIP_TYPE_NOPS (op0);
1431 c_disable_warnings (op0 == truthvalue_false_node);
1432 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1433 STRIP_TYPE_NOPS (op1);
1434 c_enable_warnings (op0 == truthvalue_false_node);
1436 c_disable_warnings (op0 == truthvalue_true_node);
1437 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1438 STRIP_TYPE_NOPS (op2);
1439 c_enable_warnings (op0 == truthvalue_true_node);
1441 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1442 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1443 else
1444 ret = fold (expr);
1445 *maybe_const_operands &= op0_const;
1446 *maybe_const_itself &= op0_const_self;
1447 if (!(flag_isoc99
1448 && op0_const
1449 && op0_const_self
1450 && op0 == truthvalue_false_node))
1451 *maybe_const_operands &= op1_const;
1452 if (!(op0_const
1453 && op0_const_self
1454 && op0 == truthvalue_false_node))
1455 *maybe_const_itself &= op1_const_self;
1456 if (!(flag_isoc99
1457 && op0_const
1458 && op0_const_self
1459 && op0 == truthvalue_true_node))
1460 *maybe_const_operands &= op2_const;
1461 if (!(op0_const
1462 && op0_const_self
1463 && op0 == truthvalue_true_node))
1464 *maybe_const_itself &= op2_const_self;
1465 goto out;
1467 case EXCESS_PRECISION_EXPR:
1468 /* Each case where an operand with excess precision may be
1469 encountered must remove the EXCESS_PRECISION_EXPR around
1470 inner operands and possibly put one around the whole
1471 expression or possibly convert to the semantic type (which
1472 c_fully_fold does); we cannot tell at this stage which is
1473 appropriate in any particular case. */
1474 gcc_unreachable ();
1476 default:
1477 /* Various codes may appear through folding built-in functions
1478 and their arguments. */
1479 goto out;
1482 out:
1483 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1484 have been done by this point, so remove them again. */
1485 nowarning |= TREE_NO_WARNING (ret);
1486 STRIP_TYPE_NOPS (ret);
1487 if (nowarning && !TREE_NO_WARNING (ret))
1489 if (!CAN_HAVE_LOCATION_P (ret))
1490 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1491 TREE_NO_WARNING (ret) = 1;
1493 if (ret != expr)
1494 protected_set_expr_location (ret, loc);
1495 return ret;
1498 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1499 return EXP. Otherwise, return either EXP or its known constant
1500 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1501 Is the BLKmode test appropriate? */
1503 tree
1504 decl_constant_value_for_optimization (tree exp)
1506 tree ret;
1508 /* This function is only used by C, for c_fully_fold and other
1509 optimization, and may not be correct for C++. */
1510 if (c_dialect_cxx ())
1511 gcc_unreachable ();
1513 if (!optimize
1514 || TREE_CODE (exp) != VAR_DECL
1515 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1516 || DECL_MODE (exp) == BLKmode)
1517 return exp;
1519 ret = decl_constant_value (exp);
1520 /* Avoid unwanted tree sharing between the initializer and current
1521 function's body where the tree can be modified e.g. by the
1522 gimplifier. */
1523 if (ret != exp && TREE_STATIC (exp))
1524 ret = unshare_expr (ret);
1525 return ret;
1528 /* Print a warning if a constant expression had overflow in folding.
1529 Invoke this function on every expression that the language
1530 requires to be a constant expression.
1531 Note the ANSI C standard says it is erroneous for a
1532 constant expression to overflow. */
1534 void
1535 constant_expression_warning (tree value)
1537 if (warn_overflow && pedantic
1538 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1539 || TREE_CODE (value) == FIXED_CST
1540 || TREE_CODE (value) == VECTOR_CST
1541 || TREE_CODE (value) == COMPLEX_CST)
1542 && TREE_OVERFLOW (value))
1543 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1546 /* The same as above but print an unconditional error. */
1547 void
1548 constant_expression_error (tree value)
1550 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1551 || TREE_CODE (value) == FIXED_CST
1552 || TREE_CODE (value) == VECTOR_CST
1553 || TREE_CODE (value) == COMPLEX_CST)
1554 && TREE_OVERFLOW (value))
1555 error ("overflow in constant expression");
1558 /* Print a warning if an expression had overflow in folding and its
1559 operands hadn't.
1561 Invoke this function on every expression that
1562 (1) appears in the source code, and
1563 (2) is a constant expression that overflowed, and
1564 (3) is not already checked by convert_and_check;
1565 however, do not invoke this function on operands of explicit casts
1566 or when the expression is the result of an operator and any operand
1567 already overflowed. */
1569 void
1570 overflow_warning (location_t loc, tree value)
1572 if (c_inhibit_evaluation_warnings != 0)
1573 return;
1575 switch (TREE_CODE (value))
1577 case INTEGER_CST:
1578 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1579 break;
1581 case REAL_CST:
1582 warning_at (loc, OPT_Woverflow,
1583 "floating point overflow in expression");
1584 break;
1586 case FIXED_CST:
1587 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1588 break;
1590 case VECTOR_CST:
1591 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1592 break;
1594 case COMPLEX_CST:
1595 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1596 warning_at (loc, OPT_Woverflow,
1597 "complex integer overflow in expression");
1598 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1599 warning_at (loc, OPT_Woverflow,
1600 "complex floating point overflow in expression");
1601 break;
1603 default:
1604 break;
1608 /* Warn about uses of logical || / && operator in a context where it
1609 is likely that the bitwise equivalent was intended by the
1610 programmer. We have seen an expression in which CODE is a binary
1611 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1612 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1613 void
1614 warn_logical_operator (location_t location, enum tree_code code, tree type,
1615 enum tree_code code_left, tree op_left,
1616 enum tree_code ARG_UNUSED (code_right), tree op_right)
1618 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1619 int in0_p, in1_p, in_p;
1620 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1621 bool strict_overflow_p = false;
1623 if (code != TRUTH_ANDIF_EXPR
1624 && code != TRUTH_AND_EXPR
1625 && code != TRUTH_ORIF_EXPR
1626 && code != TRUTH_OR_EXPR)
1627 return;
1629 /* Warn if &&/|| are being used in a context where it is
1630 likely that the bitwise equivalent was intended by the
1631 programmer. That is, an expression such as op && MASK
1632 where op should not be any boolean expression, nor a
1633 constant, and mask seems to be a non-boolean integer constant. */
1634 if (!truth_value_p (code_left)
1635 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1636 && !CONSTANT_CLASS_P (op_left)
1637 && !TREE_NO_WARNING (op_left)
1638 && TREE_CODE (op_right) == INTEGER_CST
1639 && !integer_zerop (op_right)
1640 && !integer_onep (op_right))
1642 if (or_op)
1643 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1644 " applied to non-boolean constant");
1645 else
1646 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1647 " applied to non-boolean constant");
1648 TREE_NO_WARNING (op_left) = true;
1649 return;
1652 /* We do not warn for constants because they are typical of macro
1653 expansions that test for features. */
1654 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1655 return;
1657 /* This warning only makes sense with logical operands. */
1658 if (!(truth_value_p (TREE_CODE (op_left))
1659 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1660 || !(truth_value_p (TREE_CODE (op_right))
1661 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1662 return;
1665 /* We first test whether either side separately is trivially true
1666 (with OR) or trivially false (with AND). If so, do not warn.
1667 This is a common idiom for testing ranges of data types in
1668 portable code. */
1669 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1670 if (!lhs)
1671 return;
1672 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1673 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1675 /* If this is an OR operation, invert both sides; now, the result
1676 should be always false to get a warning. */
1677 if (or_op)
1678 in0_p = !in0_p;
1680 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1681 if (tem && integer_zerop (tem))
1682 return;
1684 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1685 if (!rhs)
1686 return;
1687 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1688 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1690 /* If this is an OR operation, invert both sides; now, the result
1691 should be always false to get a warning. */
1692 if (or_op)
1693 in1_p = !in1_p;
1695 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1696 if (tem && integer_zerop (tem))
1697 return;
1699 /* If both expressions have the same operand, if we can merge the
1700 ranges, and if the range test is always false, then warn. */
1701 if (operand_equal_p (lhs, rhs, 0)
1702 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1703 in1_p, low1, high1)
1704 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1705 type, lhs, in_p, low, high))
1706 && integer_zerop (tem))
1708 if (or_op)
1709 warning_at (location, OPT_Wlogical_op,
1710 "logical %<or%> "
1711 "of collectively exhaustive tests is always true");
1712 else
1713 warning_at (location, OPT_Wlogical_op,
1714 "logical %<and%> "
1715 "of mutually exclusive tests is always false");
1720 /* Warn if EXP contains any computations whose results are not used.
1721 Return true if a warning is printed; false otherwise. LOCUS is the
1722 (potential) location of the expression. */
1724 bool
1725 warn_if_unused_value (const_tree exp, location_t locus)
1727 restart:
1728 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1729 return false;
1731 /* Don't warn about void constructs. This includes casting to void,
1732 void function calls, and statement expressions with a final cast
1733 to void. */
1734 if (VOID_TYPE_P (TREE_TYPE (exp)))
1735 return false;
1737 if (EXPR_HAS_LOCATION (exp))
1738 locus = EXPR_LOCATION (exp);
1740 switch (TREE_CODE (exp))
1742 case PREINCREMENT_EXPR:
1743 case POSTINCREMENT_EXPR:
1744 case PREDECREMENT_EXPR:
1745 case POSTDECREMENT_EXPR:
1746 case MODIFY_EXPR:
1747 case INIT_EXPR:
1748 case TARGET_EXPR:
1749 case CALL_EXPR:
1750 case TRY_CATCH_EXPR:
1751 case WITH_CLEANUP_EXPR:
1752 case EXIT_EXPR:
1753 case VA_ARG_EXPR:
1754 return false;
1756 case BIND_EXPR:
1757 /* For a binding, warn if no side effect within it. */
1758 exp = BIND_EXPR_BODY (exp);
1759 goto restart;
1761 case SAVE_EXPR:
1762 case NON_LVALUE_EXPR:
1763 case NOP_EXPR:
1764 exp = TREE_OPERAND (exp, 0);
1765 goto restart;
1767 case TRUTH_ORIF_EXPR:
1768 case TRUTH_ANDIF_EXPR:
1769 /* In && or ||, warn if 2nd operand has no side effect. */
1770 exp = TREE_OPERAND (exp, 1);
1771 goto restart;
1773 case COMPOUND_EXPR:
1774 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1775 return true;
1776 /* Let people do `(foo (), 0)' without a warning. */
1777 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1778 return false;
1779 exp = TREE_OPERAND (exp, 1);
1780 goto restart;
1782 case COND_EXPR:
1783 /* If this is an expression with side effects, don't warn; this
1784 case commonly appears in macro expansions. */
1785 if (TREE_SIDE_EFFECTS (exp))
1786 return false;
1787 goto warn;
1789 case INDIRECT_REF:
1790 /* Don't warn about automatic dereferencing of references, since
1791 the user cannot control it. */
1792 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1794 exp = TREE_OPERAND (exp, 0);
1795 goto restart;
1797 /* Fall through. */
1799 default:
1800 /* Referencing a volatile value is a side effect, so don't warn. */
1801 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1802 && TREE_THIS_VOLATILE (exp))
1803 return false;
1805 /* If this is an expression which has no operands, there is no value
1806 to be unused. There are no such language-independent codes,
1807 but front ends may define such. */
1808 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1809 return false;
1811 warn:
1812 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1817 /* Print a warning about casts that might indicate violation
1818 of strict aliasing rules if -Wstrict-aliasing is used and
1819 strict aliasing mode is in effect. OTYPE is the original
1820 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1822 bool
1823 strict_aliasing_warning (tree otype, tree type, tree expr)
1825 /* Strip pointer conversion chains and get to the correct original type. */
1826 STRIP_NOPS (expr);
1827 otype = TREE_TYPE (expr);
1829 if (!(flag_strict_aliasing
1830 && POINTER_TYPE_P (type)
1831 && POINTER_TYPE_P (otype)
1832 && !VOID_TYPE_P (TREE_TYPE (type)))
1833 /* If the type we are casting to is a ref-all pointer
1834 dereferencing it is always valid. */
1835 || TYPE_REF_CAN_ALIAS_ALL (type))
1836 return false;
1838 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1839 && (DECL_P (TREE_OPERAND (expr, 0))
1840 || handled_component_p (TREE_OPERAND (expr, 0))))
1842 /* Casting the address of an object to non void pointer. Warn
1843 if the cast breaks type based aliasing. */
1844 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1846 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1847 "might break strict-aliasing rules");
1848 return true;
1850 else
1852 /* warn_strict_aliasing >= 3. This includes the default (3).
1853 Only warn if the cast is dereferenced immediately. */
1854 alias_set_type set1 =
1855 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1856 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1858 if (set1 != set2 && set2 != 0
1859 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1861 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1862 "pointer will break strict-aliasing rules");
1863 return true;
1865 else if (warn_strict_aliasing == 2
1866 && !alias_sets_must_conflict_p (set1, set2))
1868 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1869 "pointer might break strict-aliasing rules");
1870 return true;
1874 else
1875 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1877 /* At this level, warn for any conversions, even if an address is
1878 not taken in the same statement. This will likely produce many
1879 false positives, but could be useful to pinpoint problems that
1880 are not revealed at higher levels. */
1881 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1882 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1883 if (!COMPLETE_TYPE_P (type)
1884 || !alias_sets_must_conflict_p (set1, set2))
1886 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1887 "pointer might break strict-aliasing rules");
1888 return true;
1892 return false;
1895 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1896 sizeof as last operand of certain builtins. */
1898 void
1899 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1900 vec<tree, va_gc> *params, tree *sizeof_arg,
1901 bool (*comp_types) (tree, tree))
1903 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1904 bool strop = false, cmp = false;
1905 unsigned int idx = ~0;
1906 location_t loc;
1908 if (TREE_CODE (callee) != FUNCTION_DECL
1909 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1910 || vec_safe_length (params) <= 1)
1911 return;
1913 switch (DECL_FUNCTION_CODE (callee))
1915 case BUILT_IN_STRNCMP:
1916 case BUILT_IN_STRNCASECMP:
1917 cmp = true;
1918 /* FALLTHRU */
1919 case BUILT_IN_STRNCPY:
1920 case BUILT_IN_STRNCPY_CHK:
1921 case BUILT_IN_STRNCAT:
1922 case BUILT_IN_STRNCAT_CHK:
1923 case BUILT_IN_STPNCPY:
1924 case BUILT_IN_STPNCPY_CHK:
1925 strop = true;
1926 /* FALLTHRU */
1927 case BUILT_IN_MEMCPY:
1928 case BUILT_IN_MEMCPY_CHK:
1929 case BUILT_IN_MEMMOVE:
1930 case BUILT_IN_MEMMOVE_CHK:
1931 if (params->length () < 3)
1932 return;
1933 src = (*params)[1];
1934 dest = (*params)[0];
1935 idx = 2;
1936 break;
1937 case BUILT_IN_BCOPY:
1938 if (params->length () < 3)
1939 return;
1940 src = (*params)[0];
1941 dest = (*params)[1];
1942 idx = 2;
1943 break;
1944 case BUILT_IN_MEMCMP:
1945 case BUILT_IN_BCMP:
1946 if (params->length () < 3)
1947 return;
1948 src = (*params)[1];
1949 dest = (*params)[0];
1950 idx = 2;
1951 cmp = true;
1952 break;
1953 case BUILT_IN_MEMSET:
1954 case BUILT_IN_MEMSET_CHK:
1955 if (params->length () < 3)
1956 return;
1957 dest = (*params)[0];
1958 idx = 2;
1959 break;
1960 case BUILT_IN_BZERO:
1961 dest = (*params)[0];
1962 idx = 1;
1963 break;
1964 case BUILT_IN_STRNDUP:
1965 src = (*params)[0];
1966 strop = true;
1967 idx = 1;
1968 break;
1969 case BUILT_IN_MEMCHR:
1970 if (params->length () < 3)
1971 return;
1972 src = (*params)[0];
1973 idx = 2;
1974 break;
1975 case BUILT_IN_SNPRINTF:
1976 case BUILT_IN_SNPRINTF_CHK:
1977 case BUILT_IN_VSNPRINTF:
1978 case BUILT_IN_VSNPRINTF_CHK:
1979 dest = (*params)[0];
1980 idx = 1;
1981 strop = true;
1982 break;
1983 default:
1984 break;
1987 if (idx >= 3)
1988 return;
1990 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1991 return;
1993 type = TYPE_P (sizeof_arg[idx])
1994 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1995 if (!POINTER_TYPE_P (type))
1996 return;
1998 if (dest
1999 && (tem = tree_strip_nop_conversions (dest))
2000 && POINTER_TYPE_P (TREE_TYPE (tem))
2001 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2002 return;
2004 if (src
2005 && (tem = tree_strip_nop_conversions (src))
2006 && POINTER_TYPE_P (TREE_TYPE (tem))
2007 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2008 return;
2010 loc = sizeof_arg_loc[idx];
2012 if (dest && !cmp)
2014 if (!TYPE_P (sizeof_arg[idx])
2015 && operand_equal_p (dest, sizeof_arg[idx], 0)
2016 && comp_types (TREE_TYPE (dest), type))
2018 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2019 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2020 "argument to %<sizeof%> in %qD call is the same "
2021 "expression as the destination; did you mean to "
2022 "remove the addressof?", callee);
2023 else if ((TYPE_PRECISION (TREE_TYPE (type))
2024 == TYPE_PRECISION (char_type_node))
2025 || strop)
2026 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2027 "argument to %<sizeof%> in %qD call is the same "
2028 "expression as the destination; did you mean to "
2029 "provide an explicit length?", callee);
2030 else
2031 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2032 "argument to %<sizeof%> in %qD call is the same "
2033 "expression as the destination; did you mean to "
2034 "dereference it?", callee);
2035 return;
2038 if (POINTER_TYPE_P (TREE_TYPE (dest))
2039 && !strop
2040 && comp_types (TREE_TYPE (dest), type)
2041 && !VOID_TYPE_P (TREE_TYPE (type)))
2043 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2044 "argument to %<sizeof%> in %qD call is the same "
2045 "pointer type %qT as the destination; expected %qT "
2046 "or an explicit length", callee, TREE_TYPE (dest),
2047 TREE_TYPE (TREE_TYPE (dest)));
2048 return;
2052 if (src && !cmp)
2054 if (!TYPE_P (sizeof_arg[idx])
2055 && operand_equal_p (src, sizeof_arg[idx], 0)
2056 && comp_types (TREE_TYPE (src), type))
2058 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2059 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2060 "argument to %<sizeof%> in %qD call is the same "
2061 "expression as the source; did you mean to "
2062 "remove the addressof?", callee);
2063 else if ((TYPE_PRECISION (TREE_TYPE (type))
2064 == TYPE_PRECISION (char_type_node))
2065 || strop)
2066 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2067 "argument to %<sizeof%> in %qD call is the same "
2068 "expression as the source; did you mean to "
2069 "provide an explicit length?", callee);
2070 else
2071 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2072 "argument to %<sizeof%> in %qD call is the same "
2073 "expression as the source; did you mean to "
2074 "dereference it?", callee);
2075 return;
2078 if (POINTER_TYPE_P (TREE_TYPE (src))
2079 && !strop
2080 && comp_types (TREE_TYPE (src), type)
2081 && !VOID_TYPE_P (TREE_TYPE (type)))
2083 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2084 "argument to %<sizeof%> in %qD call is the same "
2085 "pointer type %qT as the source; expected %qT "
2086 "or an explicit length", callee, TREE_TYPE (src),
2087 TREE_TYPE (TREE_TYPE (src)));
2088 return;
2092 if (dest)
2094 if (!TYPE_P (sizeof_arg[idx])
2095 && operand_equal_p (dest, sizeof_arg[idx], 0)
2096 && comp_types (TREE_TYPE (dest), type))
2098 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2099 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2100 "argument to %<sizeof%> in %qD call is the same "
2101 "expression as the first source; did you mean to "
2102 "remove the addressof?", callee);
2103 else if ((TYPE_PRECISION (TREE_TYPE (type))
2104 == TYPE_PRECISION (char_type_node))
2105 || strop)
2106 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2107 "argument to %<sizeof%> in %qD call is the same "
2108 "expression as the first source; did you mean to "
2109 "provide an explicit length?", callee);
2110 else
2111 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2112 "argument to %<sizeof%> in %qD call is the same "
2113 "expression as the first source; did you mean to "
2114 "dereference it?", callee);
2115 return;
2118 if (POINTER_TYPE_P (TREE_TYPE (dest))
2119 && !strop
2120 && comp_types (TREE_TYPE (dest), type)
2121 && !VOID_TYPE_P (TREE_TYPE (type)))
2123 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2124 "argument to %<sizeof%> in %qD call is the same "
2125 "pointer type %qT as the first source; expected %qT "
2126 "or an explicit length", callee, TREE_TYPE (dest),
2127 TREE_TYPE (TREE_TYPE (dest)));
2128 return;
2132 if (src)
2134 if (!TYPE_P (sizeof_arg[idx])
2135 && operand_equal_p (src, sizeof_arg[idx], 0)
2136 && comp_types (TREE_TYPE (src), type))
2138 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2139 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2140 "argument to %<sizeof%> in %qD call is the same "
2141 "expression as the second source; did you mean to "
2142 "remove the addressof?", callee);
2143 else if ((TYPE_PRECISION (TREE_TYPE (type))
2144 == TYPE_PRECISION (char_type_node))
2145 || strop)
2146 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2147 "argument to %<sizeof%> in %qD call is the same "
2148 "expression as the second source; did you mean to "
2149 "provide an explicit length?", callee);
2150 else
2151 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2152 "argument to %<sizeof%> in %qD call is the same "
2153 "expression as the second source; did you mean to "
2154 "dereference it?", callee);
2155 return;
2158 if (POINTER_TYPE_P (TREE_TYPE (src))
2159 && !strop
2160 && comp_types (TREE_TYPE (src), type)
2161 && !VOID_TYPE_P (TREE_TYPE (type)))
2163 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2164 "argument to %<sizeof%> in %qD call is the same "
2165 "pointer type %qT as the second source; expected %qT "
2166 "or an explicit length", callee, TREE_TYPE (src),
2167 TREE_TYPE (TREE_TYPE (src)));
2168 return;
2174 /* Warn for unlikely, improbable, or stupid DECL declarations
2175 of `main'. */
2177 void
2178 check_main_parameter_types (tree decl)
2180 function_args_iterator iter;
2181 tree type;
2182 int argct = 0;
2184 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2186 /* XXX void_type_node belies the abstraction. */
2187 if (type == void_type_node || type == error_mark_node )
2188 break;
2190 ++argct;
2191 switch (argct)
2193 case 1:
2194 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2195 pedwarn (input_location, OPT_Wmain,
2196 "first argument of %q+D should be %<int%>", decl);
2197 break;
2199 case 2:
2200 if (TREE_CODE (type) != POINTER_TYPE
2201 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2202 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2203 != char_type_node))
2204 pedwarn (input_location, OPT_Wmain,
2205 "second argument of %q+D should be %<char **%>", decl);
2206 break;
2208 case 3:
2209 if (TREE_CODE (type) != POINTER_TYPE
2210 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2211 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2212 != char_type_node))
2213 pedwarn (input_location, OPT_Wmain,
2214 "third argument of %q+D should probably be "
2215 "%<char **%>", decl);
2216 break;
2220 /* It is intentional that this message does not mention the third
2221 argument because it's only mentioned in an appendix of the
2222 standard. */
2223 if (argct > 0 && (argct < 2 || argct > 3))
2224 pedwarn (input_location, OPT_Wmain,
2225 "%q+D takes only zero or two arguments", decl);
2228 /* vector_targets_convertible_p is used for vector pointer types. The
2229 callers perform various checks that the qualifiers are satisfactory,
2230 while OTOH vector_targets_convertible_p ignores the number of elements
2231 in the vectors. That's fine with vector pointers as we can consider,
2232 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2233 and that does not require and conversion of the pointer values.
2234 In contrast, vector_types_convertible_p and
2235 vector_types_compatible_elements_p are used for vector value types. */
2236 /* True if pointers to distinct types T1 and T2 can be converted to
2237 each other without an explicit cast. Only returns true for opaque
2238 vector types. */
2239 bool
2240 vector_targets_convertible_p (const_tree t1, const_tree t2)
2242 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2243 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2244 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2245 return true;
2247 return false;
2250 /* vector_types_convertible_p is used for vector value types.
2251 It could in principle call vector_targets_convertible_p as a subroutine,
2252 but then the check for vector type would be duplicated with its callers,
2253 and also the purpose of vector_targets_convertible_p would become
2254 muddled.
2255 Where vector_types_convertible_p returns true, a conversion might still be
2256 needed to make the types match.
2257 In contrast, vector_targets_convertible_p is used for vector pointer
2258 values, and vector_types_compatible_elements_p is used specifically
2259 in the context for binary operators, as a check if use is possible without
2260 conversion. */
2261 /* True if vector types T1 and T2 can be converted to each other
2262 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2263 can only be converted with -flax-vector-conversions yet that is not
2264 in effect, emit a note telling the user about that option if such
2265 a note has not previously been emitted. */
2266 bool
2267 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2269 static bool emitted_lax_note = false;
2270 bool convertible_lax;
2272 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2273 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2274 return true;
2276 convertible_lax =
2277 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2278 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2279 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2280 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2281 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2283 if (!convertible_lax || flag_lax_vector_conversions)
2284 return convertible_lax;
2286 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2287 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2288 return true;
2290 if (emit_lax_note && !emitted_lax_note)
2292 emitted_lax_note = true;
2293 inform (input_location, "use -flax-vector-conversions to permit "
2294 "conversions between vectors with differing "
2295 "element types or numbers of subparts");
2298 return false;
2301 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2302 and have vector types, V0 has the same type as V1, and the number of
2303 elements of V0, V1, MASK is the same.
2305 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2306 called with two arguments. In this case implementation passes the
2307 first argument twice in order to share the same tree code. This fact
2308 could enable the mask-values being twice the vector length. This is
2309 an implementation accident and this semantics is not guaranteed to
2310 the user. */
2311 tree
2312 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2313 bool complain)
2315 tree ret;
2316 bool wrap = true;
2317 bool maybe_const = false;
2318 bool two_arguments = false;
2320 if (v1 == NULL_TREE)
2322 two_arguments = true;
2323 v1 = v0;
2326 if (v0 == error_mark_node || v1 == error_mark_node
2327 || mask == error_mark_node)
2328 return error_mark_node;
2330 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2331 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2333 if (complain)
2334 error_at (loc, "__builtin_shuffle last argument must "
2335 "be an integer vector");
2336 return error_mark_node;
2339 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2340 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2342 if (complain)
2343 error_at (loc, "__builtin_shuffle arguments must be vectors");
2344 return error_mark_node;
2347 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2349 if (complain)
2350 error_at (loc, "__builtin_shuffle argument vectors must be of "
2351 "the same type");
2352 return error_mark_node;
2355 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2356 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2357 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2358 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2360 if (complain)
2361 error_at (loc, "__builtin_shuffle number of elements of the "
2362 "argument vector(s) and the mask vector should "
2363 "be the same");
2364 return error_mark_node;
2367 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2368 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2370 if (complain)
2371 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2372 "must have the same size as inner type of the mask");
2373 return error_mark_node;
2376 if (!c_dialect_cxx ())
2378 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2379 v0 = c_fully_fold (v0, false, &maybe_const);
2380 wrap &= maybe_const;
2382 if (two_arguments)
2383 v1 = v0 = save_expr (v0);
2384 else
2386 v1 = c_fully_fold (v1, false, &maybe_const);
2387 wrap &= maybe_const;
2390 mask = c_fully_fold (mask, false, &maybe_const);
2391 wrap &= maybe_const;
2393 else if (two_arguments)
2394 v1 = v0 = save_expr (v0);
2396 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2398 if (!c_dialect_cxx () && !wrap)
2399 ret = c_wrap_maybe_const (ret, true);
2401 return ret;
2404 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2405 to integral type. */
2407 static tree
2408 c_common_get_narrower (tree op, int *unsignedp_ptr)
2410 op = get_narrower (op, unsignedp_ptr);
2412 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2413 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2415 /* C++0x scoped enumerations don't implicitly convert to integral
2416 type; if we stripped an explicit conversion to a larger type we
2417 need to replace it so common_type will still work. */
2418 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2419 TYPE_UNSIGNED (TREE_TYPE (op)));
2420 op = fold_convert (type, op);
2422 return op;
2425 /* This is a helper function of build_binary_op.
2427 For certain operations if both args were extended from the same
2428 smaller type, do the arithmetic in that type and then extend.
2430 BITWISE indicates a bitwise operation.
2431 For them, this optimization is safe only if
2432 both args are zero-extended or both are sign-extended.
2433 Otherwise, we might change the result.
2434 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2435 but calculated in (unsigned short) it would be (unsigned short)-1.
2437 tree
2438 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2440 int unsigned0, unsigned1;
2441 tree arg0, arg1;
2442 int uns;
2443 tree type;
2445 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2446 excessive narrowing when we call get_narrower below. For
2447 example, suppose that OP0 is of unsigned int extended
2448 from signed char and that RESULT_TYPE is long long int.
2449 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2450 like
2452 (long long int) (unsigned int) signed_char
2454 which get_narrower would narrow down to
2456 (unsigned int) signed char
2458 If we do not cast OP0 first, get_narrower would return
2459 signed_char, which is inconsistent with the case of the
2460 explicit cast. */
2461 op0 = convert (result_type, op0);
2462 op1 = convert (result_type, op1);
2464 arg0 = c_common_get_narrower (op0, &unsigned0);
2465 arg1 = c_common_get_narrower (op1, &unsigned1);
2467 /* UNS is 1 if the operation to be done is an unsigned one. */
2468 uns = TYPE_UNSIGNED (result_type);
2470 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2471 but it *requires* conversion to FINAL_TYPE. */
2473 if ((TYPE_PRECISION (TREE_TYPE (op0))
2474 == TYPE_PRECISION (TREE_TYPE (arg0)))
2475 && TREE_TYPE (op0) != result_type)
2476 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2477 if ((TYPE_PRECISION (TREE_TYPE (op1))
2478 == TYPE_PRECISION (TREE_TYPE (arg1)))
2479 && TREE_TYPE (op1) != result_type)
2480 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2482 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2484 /* For bitwise operations, signedness of nominal type
2485 does not matter. Consider only how operands were extended. */
2486 if (bitwise)
2487 uns = unsigned0;
2489 /* Note that in all three cases below we refrain from optimizing
2490 an unsigned operation on sign-extended args.
2491 That would not be valid. */
2493 /* Both args variable: if both extended in same way
2494 from same width, do it in that width.
2495 Do it unsigned if args were zero-extended. */
2496 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2497 < TYPE_PRECISION (result_type))
2498 && (TYPE_PRECISION (TREE_TYPE (arg1))
2499 == TYPE_PRECISION (TREE_TYPE (arg0)))
2500 && unsigned0 == unsigned1
2501 && (unsigned0 || !uns))
2502 return c_common_signed_or_unsigned_type
2503 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2505 else if (TREE_CODE (arg0) == INTEGER_CST
2506 && (unsigned1 || !uns)
2507 && (TYPE_PRECISION (TREE_TYPE (arg1))
2508 < TYPE_PRECISION (result_type))
2509 && (type
2510 = c_common_signed_or_unsigned_type (unsigned1,
2511 TREE_TYPE (arg1)))
2512 && !POINTER_TYPE_P (type)
2513 && int_fits_type_p (arg0, type))
2514 return type;
2516 else if (TREE_CODE (arg1) == INTEGER_CST
2517 && (unsigned0 || !uns)
2518 && (TYPE_PRECISION (TREE_TYPE (arg0))
2519 < TYPE_PRECISION (result_type))
2520 && (type
2521 = c_common_signed_or_unsigned_type (unsigned0,
2522 TREE_TYPE (arg0)))
2523 && !POINTER_TYPE_P (type)
2524 && int_fits_type_p (arg1, type))
2525 return type;
2527 return result_type;
2530 /* Checks if expression EXPR of real/integer type cannot be converted
2531 to the real/integer type TYPE. Function returns true when:
2532 * EXPR is a constant which cannot be exactly converted to TYPE
2533 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2534 for EXPR type and TYPE being both integers or both real.
2535 * EXPR is not a constant of real type and TYPE is an integer.
2536 * EXPR is not a constant of integer type which cannot be
2537 exactly converted to real type.
2538 Function allows conversions between types of different signedness and
2539 does not return true in that case. Function can produce signedness
2540 warnings if PRODUCE_WARNS is true. */
2541 bool
2542 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2544 bool give_warning = false;
2545 tree expr_type = TREE_TYPE (expr);
2546 location_t loc = EXPR_LOC_OR_HERE (expr);
2548 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2550 /* Warn for real constant that is not an exact integer converted
2551 to integer type. */
2552 if (TREE_CODE (expr_type) == REAL_TYPE
2553 && TREE_CODE (type) == INTEGER_TYPE)
2555 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2556 give_warning = true;
2558 /* Warn for an integer constant that does not fit into integer type. */
2559 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2560 && TREE_CODE (type) == INTEGER_TYPE
2561 && !int_fits_type_p (expr, type))
2563 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2564 && tree_int_cst_sgn (expr) < 0)
2566 if (produce_warns)
2567 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2568 " implicitly converted to unsigned type");
2570 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2572 if (produce_warns)
2573 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2574 " constant value to negative integer");
2576 else
2577 give_warning = true;
2579 else if (TREE_CODE (type) == REAL_TYPE)
2581 /* Warn for an integer constant that does not fit into real type. */
2582 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2584 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2585 if (!exact_real_truncate (TYPE_MODE (type), &a))
2586 give_warning = true;
2588 /* Warn for a real constant that does not fit into a smaller
2589 real type. */
2590 else if (TREE_CODE (expr_type) == REAL_TYPE
2591 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2593 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2594 if (!exact_real_truncate (TYPE_MODE (type), &a))
2595 give_warning = true;
2599 else
2601 /* Warn for real types converted to integer types. */
2602 if (TREE_CODE (expr_type) == REAL_TYPE
2603 && TREE_CODE (type) == INTEGER_TYPE)
2604 give_warning = true;
2606 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2607 && TREE_CODE (type) == INTEGER_TYPE)
2609 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2610 expr = get_unwidened (expr, 0);
2611 expr_type = TREE_TYPE (expr);
2613 /* Don't warn for short y; short x = ((int)y & 0xff); */
2614 if (TREE_CODE (expr) == BIT_AND_EXPR
2615 || TREE_CODE (expr) == BIT_IOR_EXPR
2616 || TREE_CODE (expr) == BIT_XOR_EXPR)
2618 /* If both args were extended from a shortest type,
2619 use that type if that is safe. */
2620 expr_type = shorten_binary_op (expr_type,
2621 TREE_OPERAND (expr, 0),
2622 TREE_OPERAND (expr, 1),
2623 /* bitwise */1);
2625 if (TREE_CODE (expr) == BIT_AND_EXPR)
2627 tree op0 = TREE_OPERAND (expr, 0);
2628 tree op1 = TREE_OPERAND (expr, 1);
2629 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2630 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2632 /* If one of the operands is a non-negative constant
2633 that fits in the target type, then the type of the
2634 other operand does not matter. */
2635 if ((TREE_CODE (op0) == INTEGER_CST
2636 && int_fits_type_p (op0, c_common_signed_type (type))
2637 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2638 || (TREE_CODE (op1) == INTEGER_CST
2639 && int_fits_type_p (op1, c_common_signed_type (type))
2640 && int_fits_type_p (op1,
2641 c_common_unsigned_type (type))))
2642 return false;
2643 /* If constant is unsigned and fits in the target
2644 type, then the result will also fit. */
2645 else if ((TREE_CODE (op0) == INTEGER_CST
2646 && unsigned0
2647 && int_fits_type_p (op0, type))
2648 || (TREE_CODE (op1) == INTEGER_CST
2649 && unsigned1
2650 && int_fits_type_p (op1, type)))
2651 return false;
2654 /* Warn for integer types converted to smaller integer types. */
2655 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2656 give_warning = true;
2658 /* When they are the same width but different signedness,
2659 then the value may change. */
2660 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2661 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2662 /* Even when converted to a bigger type, if the type is
2663 unsigned but expr is signed, then negative values
2664 will be changed. */
2665 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2666 && produce_warns)
2667 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2668 "may change the sign of the result",
2669 type, expr_type);
2672 /* Warn for integer types converted to real types if and only if
2673 all the range of values of the integer type cannot be
2674 represented by the real type. */
2675 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2676 && TREE_CODE (type) == REAL_TYPE)
2678 tree type_low_bound, type_high_bound;
2679 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2681 /* Don't warn about char y = 0xff; float x = (int) y; */
2682 expr = get_unwidened (expr, 0);
2683 expr_type = TREE_TYPE (expr);
2685 type_low_bound = TYPE_MIN_VALUE (expr_type);
2686 type_high_bound = TYPE_MAX_VALUE (expr_type);
2687 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2688 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2690 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2691 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2692 give_warning = true;
2695 /* Warn for real types converted to smaller real types. */
2696 else if (TREE_CODE (expr_type) == REAL_TYPE
2697 && TREE_CODE (type) == REAL_TYPE
2698 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2699 give_warning = true;
2702 return give_warning;
2705 /* Warns if the conversion of EXPR to TYPE may alter a value.
2706 This is a helper function for warnings_for_convert_and_check. */
2708 static void
2709 conversion_warning (tree type, tree expr)
2711 tree expr_type = TREE_TYPE (expr);
2712 location_t loc = EXPR_LOC_OR_HERE (expr);
2714 if (!warn_conversion && !warn_sign_conversion)
2715 return;
2717 switch (TREE_CODE (expr))
2719 case EQ_EXPR:
2720 case NE_EXPR:
2721 case LE_EXPR:
2722 case GE_EXPR:
2723 case LT_EXPR:
2724 case GT_EXPR:
2725 case TRUTH_ANDIF_EXPR:
2726 case TRUTH_ORIF_EXPR:
2727 case TRUTH_AND_EXPR:
2728 case TRUTH_OR_EXPR:
2729 case TRUTH_XOR_EXPR:
2730 case TRUTH_NOT_EXPR:
2731 /* Conversion from boolean to a signed:1 bit-field (which only
2732 can hold the values 0 and -1) doesn't lose information - but
2733 it does change the value. */
2734 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2735 warning_at (loc, OPT_Wconversion,
2736 "conversion to %qT from boolean expression", type);
2737 return;
2739 case REAL_CST:
2740 case INTEGER_CST:
2741 if (unsafe_conversion_p (type, expr, true))
2742 warning_at (loc, OPT_Wconversion,
2743 "conversion to %qT alters %qT constant value",
2744 type, expr_type);
2745 return;
2747 case COND_EXPR:
2749 /* In case of COND_EXPR, we do not care about the type of
2750 COND_EXPR, only about the conversion of each operand. */
2751 tree op1 = TREE_OPERAND (expr, 1);
2752 tree op2 = TREE_OPERAND (expr, 2);
2754 conversion_warning (type, op1);
2755 conversion_warning (type, op2);
2756 return;
2759 default: /* 'expr' is not a constant. */
2760 if (unsafe_conversion_p (type, expr, true))
2761 warning_at (loc, OPT_Wconversion,
2762 "conversion to %qT from %qT may alter its value",
2763 type, expr_type);
2767 /* Produce warnings after a conversion. RESULT is the result of
2768 converting EXPR to TYPE. This is a helper function for
2769 convert_and_check and cp_convert_and_check. */
2771 void
2772 warnings_for_convert_and_check (tree type, tree expr, tree result)
2774 location_t loc = EXPR_LOC_OR_HERE (expr);
2776 if (TREE_CODE (expr) == INTEGER_CST
2777 && (TREE_CODE (type) == INTEGER_TYPE
2778 || TREE_CODE (type) == ENUMERAL_TYPE)
2779 && !int_fits_type_p (expr, type))
2781 /* Do not diagnose overflow in a constant expression merely
2782 because a conversion overflowed. */
2783 if (TREE_OVERFLOW (result))
2784 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2786 if (TYPE_UNSIGNED (type))
2788 /* This detects cases like converting -129 or 256 to
2789 unsigned char. */
2790 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2791 warning_at (loc, OPT_Woverflow,
2792 "large integer implicitly truncated to unsigned type");
2793 else
2794 conversion_warning (type, expr);
2796 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2797 warning (OPT_Woverflow,
2798 "overflow in implicit constant conversion");
2799 /* No warning for converting 0x80000000 to int. */
2800 else if (pedantic
2801 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2802 || TYPE_PRECISION (TREE_TYPE (expr))
2803 != TYPE_PRECISION (type)))
2804 warning_at (loc, OPT_Woverflow,
2805 "overflow in implicit constant conversion");
2807 else
2808 conversion_warning (type, expr);
2810 else if ((TREE_CODE (result) == INTEGER_CST
2811 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2812 warning_at (loc, OPT_Woverflow,
2813 "overflow in implicit constant conversion");
2814 else
2815 conversion_warning (type, expr);
2819 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2820 Invoke this function on every expression that is converted implicitly,
2821 i.e. because of language rules and not because of an explicit cast. */
2823 tree
2824 convert_and_check (tree type, tree expr)
2826 tree result;
2827 tree expr_for_warning;
2829 /* Convert from a value with possible excess precision rather than
2830 via the semantic type, but do not warn about values not fitting
2831 exactly in the semantic type. */
2832 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2834 tree orig_type = TREE_TYPE (expr);
2835 expr = TREE_OPERAND (expr, 0);
2836 expr_for_warning = convert (orig_type, expr);
2837 if (orig_type == type)
2838 return expr_for_warning;
2840 else
2841 expr_for_warning = expr;
2843 if (TREE_TYPE (expr) == type)
2844 return expr;
2846 result = convert (type, expr);
2848 if (c_inhibit_evaluation_warnings == 0
2849 && !TREE_OVERFLOW_P (expr)
2850 && result != error_mark_node)
2851 warnings_for_convert_and_check (type, expr_for_warning, result);
2853 return result;
2856 /* A node in a list that describes references to variables (EXPR), which are
2857 either read accesses if WRITER is zero, or write accesses, in which case
2858 WRITER is the parent of EXPR. */
2859 struct tlist
2861 struct tlist *next;
2862 tree expr, writer;
2865 /* Used to implement a cache the results of a call to verify_tree. We only
2866 use this for SAVE_EXPRs. */
2867 struct tlist_cache
2869 struct tlist_cache *next;
2870 struct tlist *cache_before_sp;
2871 struct tlist *cache_after_sp;
2872 tree expr;
2875 /* Obstack to use when allocating tlist structures, and corresponding
2876 firstobj. */
2877 static struct obstack tlist_obstack;
2878 static char *tlist_firstobj = 0;
2880 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2881 warnings. */
2882 static struct tlist *warned_ids;
2883 /* SAVE_EXPRs need special treatment. We process them only once and then
2884 cache the results. */
2885 static struct tlist_cache *save_expr_cache;
2887 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2888 static void merge_tlist (struct tlist **, struct tlist *, int);
2889 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2890 static int warning_candidate_p (tree);
2891 static bool candidate_equal_p (const_tree, const_tree);
2892 static void warn_for_collisions (struct tlist *);
2893 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2894 static struct tlist *new_tlist (struct tlist *, tree, tree);
2896 /* Create a new struct tlist and fill in its fields. */
2897 static struct tlist *
2898 new_tlist (struct tlist *next, tree t, tree writer)
2900 struct tlist *l;
2901 l = XOBNEW (&tlist_obstack, struct tlist);
2902 l->next = next;
2903 l->expr = t;
2904 l->writer = writer;
2905 return l;
2908 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2909 is nonnull, we ignore any node we find which has a writer equal to it. */
2911 static void
2912 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2914 while (add)
2916 struct tlist *next = add->next;
2917 if (!copy)
2918 add->next = *to;
2919 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2920 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2921 add = next;
2925 /* Merge the nodes of ADD into TO. This merging process is done so that for
2926 each variable that already exists in TO, no new node is added; however if
2927 there is a write access recorded in ADD, and an occurrence on TO is only
2928 a read access, then the occurrence in TO will be modified to record the
2929 write. */
2931 static void
2932 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2934 struct tlist **end = to;
2936 while (*end)
2937 end = &(*end)->next;
2939 while (add)
2941 int found = 0;
2942 struct tlist *tmp2;
2943 struct tlist *next = add->next;
2945 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2946 if (candidate_equal_p (tmp2->expr, add->expr))
2948 found = 1;
2949 if (!tmp2->writer)
2950 tmp2->writer = add->writer;
2952 if (!found)
2954 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2955 end = &(*end)->next;
2956 *end = 0;
2958 add = next;
2962 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2963 references in list LIST conflict with it, excluding reads if ONLY writers
2964 is nonzero. */
2966 static void
2967 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2968 int only_writes)
2970 struct tlist *tmp;
2972 /* Avoid duplicate warnings. */
2973 for (tmp = warned_ids; tmp; tmp = tmp->next)
2974 if (candidate_equal_p (tmp->expr, written))
2975 return;
2977 while (list)
2979 if (candidate_equal_p (list->expr, written)
2980 && !candidate_equal_p (list->writer, writer)
2981 && (!only_writes || list->writer))
2983 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2984 warning_at (EXPR_LOC_OR_HERE (writer),
2985 OPT_Wsequence_point, "operation on %qE may be undefined",
2986 list->expr);
2988 list = list->next;
2992 /* Given a list LIST of references to variables, find whether any of these
2993 can cause conflicts due to missing sequence points. */
2995 static void
2996 warn_for_collisions (struct tlist *list)
2998 struct tlist *tmp;
3000 for (tmp = list; tmp; tmp = tmp->next)
3002 if (tmp->writer)
3003 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3007 /* Return nonzero if X is a tree that can be verified by the sequence point
3008 warnings. */
3009 static int
3010 warning_candidate_p (tree x)
3012 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3013 return 0;
3015 if (TREE_CODE (x) == BLOCK)
3016 return 0;
3018 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3019 (lvalue_p) crash on TRY/CATCH. */
3020 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3021 return 0;
3023 if (!lvalue_p (x))
3024 return 0;
3026 /* No point to track non-const calls, they will never satisfy
3027 operand_equal_p. */
3028 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3029 return 0;
3031 if (TREE_CODE (x) == STRING_CST)
3032 return 0;
3034 return 1;
3037 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3038 static bool
3039 candidate_equal_p (const_tree x, const_tree y)
3041 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3044 /* Walk the tree X, and record accesses to variables. If X is written by the
3045 parent tree, WRITER is the parent.
3046 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3047 expression or its only operand forces a sequence point, then everything up
3048 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3049 in PNO_SP.
3050 Once we return, we will have emitted warnings if any subexpression before
3051 such a sequence point could be undefined. On a higher level, however, the
3052 sequence point may not be relevant, and we'll merge the two lists.
3054 Example: (b++, a) + b;
3055 The call that processes the COMPOUND_EXPR will store the increment of B
3056 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3057 processes the PLUS_EXPR will need to merge the two lists so that
3058 eventually, all accesses end up on the same list (and we'll warn about the
3059 unordered subexpressions b++ and b.
3061 A note on merging. If we modify the former example so that our expression
3062 becomes
3063 (b++, b) + a
3064 care must be taken not simply to add all three expressions into the final
3065 PNO_SP list. The function merge_tlist takes care of that by merging the
3066 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3067 way, so that no more than one access to B is recorded. */
3069 static void
3070 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3071 tree writer)
3073 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3074 enum tree_code code;
3075 enum tree_code_class cl;
3077 /* X may be NULL if it is the operand of an empty statement expression
3078 ({ }). */
3079 if (x == NULL)
3080 return;
3082 restart:
3083 code = TREE_CODE (x);
3084 cl = TREE_CODE_CLASS (code);
3086 if (warning_candidate_p (x))
3087 *pno_sp = new_tlist (*pno_sp, x, writer);
3089 switch (code)
3091 case CONSTRUCTOR:
3092 case SIZEOF_EXPR:
3093 return;
3095 case COMPOUND_EXPR:
3096 case TRUTH_ANDIF_EXPR:
3097 case TRUTH_ORIF_EXPR:
3098 tmp_before = tmp_nosp = tmp_list3 = 0;
3099 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3100 warn_for_collisions (tmp_nosp);
3101 merge_tlist (pbefore_sp, tmp_before, 0);
3102 merge_tlist (pbefore_sp, tmp_nosp, 0);
3103 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3104 merge_tlist (pbefore_sp, tmp_list3, 0);
3105 return;
3107 case COND_EXPR:
3108 tmp_before = tmp_list2 = 0;
3109 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3110 warn_for_collisions (tmp_list2);
3111 merge_tlist (pbefore_sp, tmp_before, 0);
3112 merge_tlist (pbefore_sp, tmp_list2, 1);
3114 tmp_list3 = tmp_nosp = 0;
3115 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3116 warn_for_collisions (tmp_nosp);
3117 merge_tlist (pbefore_sp, tmp_list3, 0);
3119 tmp_list3 = tmp_list2 = 0;
3120 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3121 warn_for_collisions (tmp_list2);
3122 merge_tlist (pbefore_sp, tmp_list3, 0);
3123 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3124 two first, to avoid warning for (a ? b++ : b++). */
3125 merge_tlist (&tmp_nosp, tmp_list2, 0);
3126 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3127 return;
3129 case PREDECREMENT_EXPR:
3130 case PREINCREMENT_EXPR:
3131 case POSTDECREMENT_EXPR:
3132 case POSTINCREMENT_EXPR:
3133 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3134 return;
3136 case MODIFY_EXPR:
3137 tmp_before = tmp_nosp = tmp_list3 = 0;
3138 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3139 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3140 /* Expressions inside the LHS are not ordered wrt. the sequence points
3141 in the RHS. Example:
3142 *a = (a++, 2)
3143 Despite the fact that the modification of "a" is in the before_sp
3144 list (tmp_before), it conflicts with the use of "a" in the LHS.
3145 We can handle this by adding the contents of tmp_list3
3146 to those of tmp_before, and redoing the collision warnings for that
3147 list. */
3148 add_tlist (&tmp_before, tmp_list3, x, 1);
3149 warn_for_collisions (tmp_before);
3150 /* Exclude the LHS itself here; we first have to merge it into the
3151 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3152 didn't exclude the LHS, we'd get it twice, once as a read and once
3153 as a write. */
3154 add_tlist (pno_sp, tmp_list3, x, 0);
3155 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3157 merge_tlist (pbefore_sp, tmp_before, 0);
3158 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3159 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3160 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3161 return;
3163 case CALL_EXPR:
3164 /* We need to warn about conflicts among arguments and conflicts between
3165 args and the function address. Side effects of the function address,
3166 however, are not ordered by the sequence point of the call. */
3168 call_expr_arg_iterator iter;
3169 tree arg;
3170 tmp_before = tmp_nosp = 0;
3171 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3172 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3174 tmp_list2 = tmp_list3 = 0;
3175 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3176 merge_tlist (&tmp_list3, tmp_list2, 0);
3177 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3179 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3180 warn_for_collisions (tmp_before);
3181 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3182 return;
3185 case TREE_LIST:
3186 /* Scan all the list, e.g. indices of multi dimensional array. */
3187 while (x)
3189 tmp_before = tmp_nosp = 0;
3190 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3191 merge_tlist (&tmp_nosp, tmp_before, 0);
3192 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3193 x = TREE_CHAIN (x);
3195 return;
3197 case SAVE_EXPR:
3199 struct tlist_cache *t;
3200 for (t = save_expr_cache; t; t = t->next)
3201 if (candidate_equal_p (t->expr, x))
3202 break;
3204 if (!t)
3206 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3207 t->next = save_expr_cache;
3208 t->expr = x;
3209 save_expr_cache = t;
3211 tmp_before = tmp_nosp = 0;
3212 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3213 warn_for_collisions (tmp_nosp);
3215 tmp_list3 = 0;
3216 while (tmp_nosp)
3218 struct tlist *t = tmp_nosp;
3219 tmp_nosp = t->next;
3220 merge_tlist (&tmp_list3, t, 0);
3222 t->cache_before_sp = tmp_before;
3223 t->cache_after_sp = tmp_list3;
3225 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3226 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3227 return;
3230 case ADDR_EXPR:
3231 x = TREE_OPERAND (x, 0);
3232 if (DECL_P (x))
3233 return;
3234 writer = 0;
3235 goto restart;
3237 default:
3238 /* For other expressions, simply recurse on their operands.
3239 Manual tail recursion for unary expressions.
3240 Other non-expressions need not be processed. */
3241 if (cl == tcc_unary)
3243 x = TREE_OPERAND (x, 0);
3244 writer = 0;
3245 goto restart;
3247 else if (IS_EXPR_CODE_CLASS (cl))
3249 int lp;
3250 int max = TREE_OPERAND_LENGTH (x);
3251 for (lp = 0; lp < max; lp++)
3253 tmp_before = tmp_nosp = 0;
3254 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3255 merge_tlist (&tmp_nosp, tmp_before, 0);
3256 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3259 return;
3263 /* Try to warn for undefined behavior in EXPR due to missing sequence
3264 points. */
3266 DEBUG_FUNCTION void
3267 verify_sequence_points (tree expr)
3269 struct tlist *before_sp = 0, *after_sp = 0;
3271 warned_ids = 0;
3272 save_expr_cache = 0;
3273 if (tlist_firstobj == 0)
3275 gcc_obstack_init (&tlist_obstack);
3276 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3279 verify_tree (expr, &before_sp, &after_sp, 0);
3280 warn_for_collisions (after_sp);
3281 obstack_free (&tlist_obstack, tlist_firstobj);
3284 /* Validate the expression after `case' and apply default promotions. */
3286 static tree
3287 check_case_value (tree value)
3289 if (value == NULL_TREE)
3290 return value;
3292 if (TREE_CODE (value) == INTEGER_CST)
3293 /* Promote char or short to int. */
3294 value = perform_integral_promotions (value);
3295 else if (value != error_mark_node)
3297 error ("case label does not reduce to an integer constant");
3298 value = error_mark_node;
3301 constant_expression_warning (value);
3303 return value;
3306 /* See if the case values LOW and HIGH are in the range of the original
3307 type (i.e. before the default conversion to int) of the switch testing
3308 expression.
3309 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3310 the type before promoting it. CASE_LOW_P is a pointer to the lower
3311 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3312 if the case is not a case range.
3313 The caller has to make sure that we are not called with NULL for
3314 CASE_LOW_P (i.e. the default case).
3315 Returns true if the case label is in range of ORIG_TYPE (saturated or
3316 untouched) or false if the label is out of range. */
3318 static bool
3319 check_case_bounds (tree type, tree orig_type,
3320 tree *case_low_p, tree *case_high_p)
3322 tree min_value, max_value;
3323 tree case_low = *case_low_p;
3324 tree case_high = case_high_p ? *case_high_p : case_low;
3326 /* If there was a problem with the original type, do nothing. */
3327 if (orig_type == error_mark_node)
3328 return true;
3330 min_value = TYPE_MIN_VALUE (orig_type);
3331 max_value = TYPE_MAX_VALUE (orig_type);
3333 /* Case label is less than minimum for type. */
3334 if (tree_int_cst_compare (case_low, min_value) < 0
3335 && tree_int_cst_compare (case_high, min_value) < 0)
3337 warning (0, "case label value is less than minimum value for type");
3338 return false;
3341 /* Case value is greater than maximum for type. */
3342 if (tree_int_cst_compare (case_low, max_value) > 0
3343 && tree_int_cst_compare (case_high, max_value) > 0)
3345 warning (0, "case label value exceeds maximum value for type");
3346 return false;
3349 /* Saturate lower case label value to minimum. */
3350 if (tree_int_cst_compare (case_high, min_value) >= 0
3351 && tree_int_cst_compare (case_low, min_value) < 0)
3353 warning (0, "lower value in case label range"
3354 " less than minimum value for type");
3355 case_low = min_value;
3358 /* Saturate upper case label value to maximum. */
3359 if (tree_int_cst_compare (case_low, max_value) <= 0
3360 && tree_int_cst_compare (case_high, max_value) > 0)
3362 warning (0, "upper value in case label range"
3363 " exceeds maximum value for type");
3364 case_high = max_value;
3367 if (*case_low_p != case_low)
3368 *case_low_p = convert (type, case_low);
3369 if (case_high_p && *case_high_p != case_high)
3370 *case_high_p = convert (type, case_high);
3372 return true;
3375 /* Return an integer type with BITS bits of precision,
3376 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3378 tree
3379 c_common_type_for_size (unsigned int bits, int unsignedp)
3381 if (bits == TYPE_PRECISION (integer_type_node))
3382 return unsignedp ? unsigned_type_node : integer_type_node;
3384 if (bits == TYPE_PRECISION (signed_char_type_node))
3385 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3387 if (bits == TYPE_PRECISION (short_integer_type_node))
3388 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3390 if (bits == TYPE_PRECISION (long_integer_type_node))
3391 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3393 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3394 return (unsignedp ? long_long_unsigned_type_node
3395 : long_long_integer_type_node);
3397 if (int128_integer_type_node
3398 && bits == TYPE_PRECISION (int128_integer_type_node))
3399 return (unsignedp ? int128_unsigned_type_node
3400 : int128_integer_type_node);
3402 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3403 return (unsignedp ? widest_unsigned_literal_type_node
3404 : widest_integer_literal_type_node);
3406 if (bits <= TYPE_PRECISION (intQI_type_node))
3407 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3409 if (bits <= TYPE_PRECISION (intHI_type_node))
3410 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3412 if (bits <= TYPE_PRECISION (intSI_type_node))
3413 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3415 if (bits <= TYPE_PRECISION (intDI_type_node))
3416 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3418 return 0;
3421 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3422 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3423 and saturating if SATP is nonzero, otherwise not saturating. */
3425 tree
3426 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3427 int unsignedp, int satp)
3429 enum machine_mode mode;
3430 if (ibit == 0)
3431 mode = unsignedp ? UQQmode : QQmode;
3432 else
3433 mode = unsignedp ? UHAmode : HAmode;
3435 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3436 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3437 break;
3439 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3441 sorry ("GCC cannot support operators with integer types and "
3442 "fixed-point types that have too many integral and "
3443 "fractional bits together");
3444 return 0;
3447 return c_common_type_for_mode (mode, satp);
3450 /* Used for communication between c_common_type_for_mode and
3451 c_register_builtin_type. */
3452 static GTY(()) tree registered_builtin_types;
3454 /* Return a data type that has machine mode MODE.
3455 If the mode is an integer,
3456 then UNSIGNEDP selects between signed and unsigned types.
3457 If the mode is a fixed-point mode,
3458 then UNSIGNEDP selects between saturating and nonsaturating types. */
3460 tree
3461 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3463 tree t;
3465 if (mode == TYPE_MODE (integer_type_node))
3466 return unsignedp ? unsigned_type_node : integer_type_node;
3468 if (mode == TYPE_MODE (signed_char_type_node))
3469 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3471 if (mode == TYPE_MODE (short_integer_type_node))
3472 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3474 if (mode == TYPE_MODE (long_integer_type_node))
3475 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3477 if (mode == TYPE_MODE (long_long_integer_type_node))
3478 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3480 if (int128_integer_type_node
3481 && mode == TYPE_MODE (int128_integer_type_node))
3482 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3484 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3485 return unsignedp ? widest_unsigned_literal_type_node
3486 : widest_integer_literal_type_node;
3488 if (mode == QImode)
3489 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3491 if (mode == HImode)
3492 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3494 if (mode == SImode)
3495 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3497 if (mode == DImode)
3498 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3500 #if HOST_BITS_PER_WIDE_INT >= 64
3501 if (mode == TYPE_MODE (intTI_type_node))
3502 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3503 #endif
3505 if (mode == TYPE_MODE (float_type_node))
3506 return float_type_node;
3508 if (mode == TYPE_MODE (double_type_node))
3509 return double_type_node;
3511 if (mode == TYPE_MODE (long_double_type_node))
3512 return long_double_type_node;
3514 if (mode == TYPE_MODE (void_type_node))
3515 return void_type_node;
3517 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3518 return (unsignedp
3519 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3520 : make_signed_type (GET_MODE_PRECISION (mode)));
3522 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3523 return (unsignedp
3524 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3525 : make_signed_type (GET_MODE_PRECISION (mode)));
3527 if (COMPLEX_MODE_P (mode))
3529 enum machine_mode inner_mode;
3530 tree inner_type;
3532 if (mode == TYPE_MODE (complex_float_type_node))
3533 return complex_float_type_node;
3534 if (mode == TYPE_MODE (complex_double_type_node))
3535 return complex_double_type_node;
3536 if (mode == TYPE_MODE (complex_long_double_type_node))
3537 return complex_long_double_type_node;
3539 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3540 return complex_integer_type_node;
3542 inner_mode = GET_MODE_INNER (mode);
3543 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3544 if (inner_type != NULL_TREE)
3545 return build_complex_type (inner_type);
3547 else if (VECTOR_MODE_P (mode))
3549 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3550 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3551 if (inner_type != NULL_TREE)
3552 return build_vector_type_for_mode (inner_type, mode);
3555 if (mode == TYPE_MODE (dfloat32_type_node))
3556 return dfloat32_type_node;
3557 if (mode == TYPE_MODE (dfloat64_type_node))
3558 return dfloat64_type_node;
3559 if (mode == TYPE_MODE (dfloat128_type_node))
3560 return dfloat128_type_node;
3562 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3564 if (mode == TYPE_MODE (short_fract_type_node))
3565 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3566 if (mode == TYPE_MODE (fract_type_node))
3567 return unsignedp ? sat_fract_type_node : fract_type_node;
3568 if (mode == TYPE_MODE (long_fract_type_node))
3569 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3570 if (mode == TYPE_MODE (long_long_fract_type_node))
3571 return unsignedp ? sat_long_long_fract_type_node
3572 : long_long_fract_type_node;
3574 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3575 return unsignedp ? sat_unsigned_short_fract_type_node
3576 : unsigned_short_fract_type_node;
3577 if (mode == TYPE_MODE (unsigned_fract_type_node))
3578 return unsignedp ? sat_unsigned_fract_type_node
3579 : unsigned_fract_type_node;
3580 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3581 return unsignedp ? sat_unsigned_long_fract_type_node
3582 : unsigned_long_fract_type_node;
3583 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3584 return unsignedp ? sat_unsigned_long_long_fract_type_node
3585 : unsigned_long_long_fract_type_node;
3587 if (mode == TYPE_MODE (short_accum_type_node))
3588 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3589 if (mode == TYPE_MODE (accum_type_node))
3590 return unsignedp ? sat_accum_type_node : accum_type_node;
3591 if (mode == TYPE_MODE (long_accum_type_node))
3592 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3593 if (mode == TYPE_MODE (long_long_accum_type_node))
3594 return unsignedp ? sat_long_long_accum_type_node
3595 : long_long_accum_type_node;
3597 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3598 return unsignedp ? sat_unsigned_short_accum_type_node
3599 : unsigned_short_accum_type_node;
3600 if (mode == TYPE_MODE (unsigned_accum_type_node))
3601 return unsignedp ? sat_unsigned_accum_type_node
3602 : unsigned_accum_type_node;
3603 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3604 return unsignedp ? sat_unsigned_long_accum_type_node
3605 : unsigned_long_accum_type_node;
3606 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3607 return unsignedp ? sat_unsigned_long_long_accum_type_node
3608 : unsigned_long_long_accum_type_node;
3610 if (mode == QQmode)
3611 return unsignedp ? sat_qq_type_node : qq_type_node;
3612 if (mode == HQmode)
3613 return unsignedp ? sat_hq_type_node : hq_type_node;
3614 if (mode == SQmode)
3615 return unsignedp ? sat_sq_type_node : sq_type_node;
3616 if (mode == DQmode)
3617 return unsignedp ? sat_dq_type_node : dq_type_node;
3618 if (mode == TQmode)
3619 return unsignedp ? sat_tq_type_node : tq_type_node;
3621 if (mode == UQQmode)
3622 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3623 if (mode == UHQmode)
3624 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3625 if (mode == USQmode)
3626 return unsignedp ? sat_usq_type_node : usq_type_node;
3627 if (mode == UDQmode)
3628 return unsignedp ? sat_udq_type_node : udq_type_node;
3629 if (mode == UTQmode)
3630 return unsignedp ? sat_utq_type_node : utq_type_node;
3632 if (mode == HAmode)
3633 return unsignedp ? sat_ha_type_node : ha_type_node;
3634 if (mode == SAmode)
3635 return unsignedp ? sat_sa_type_node : sa_type_node;
3636 if (mode == DAmode)
3637 return unsignedp ? sat_da_type_node : da_type_node;
3638 if (mode == TAmode)
3639 return unsignedp ? sat_ta_type_node : ta_type_node;
3641 if (mode == UHAmode)
3642 return unsignedp ? sat_uha_type_node : uha_type_node;
3643 if (mode == USAmode)
3644 return unsignedp ? sat_usa_type_node : usa_type_node;
3645 if (mode == UDAmode)
3646 return unsignedp ? sat_uda_type_node : uda_type_node;
3647 if (mode == UTAmode)
3648 return unsignedp ? sat_uta_type_node : uta_type_node;
3651 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3652 if (TYPE_MODE (TREE_VALUE (t)) == mode
3653 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3654 return TREE_VALUE (t);
3656 return 0;
3659 tree
3660 c_common_unsigned_type (tree type)
3662 return c_common_signed_or_unsigned_type (1, type);
3665 /* Return a signed type the same as TYPE in other respects. */
3667 tree
3668 c_common_signed_type (tree type)
3670 return c_common_signed_or_unsigned_type (0, type);
3673 /* Return a type the same as TYPE except unsigned or
3674 signed according to UNSIGNEDP. */
3676 tree
3677 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3679 tree type1;
3681 /* This block of code emulates the behavior of the old
3682 c_common_unsigned_type. In particular, it returns
3683 long_unsigned_type_node if passed a long, even when a int would
3684 have the same size. This is necessary for warnings to work
3685 correctly in archs where sizeof(int) == sizeof(long) */
3687 type1 = TYPE_MAIN_VARIANT (type);
3688 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3689 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3690 if (type1 == integer_type_node || type1 == unsigned_type_node)
3691 return unsignedp ? unsigned_type_node : integer_type_node;
3692 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3693 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3694 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3695 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3696 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3697 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3698 if (int128_integer_type_node
3699 && (type1 == int128_integer_type_node
3700 || type1 == int128_unsigned_type_node))
3701 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3702 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3703 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3704 #if HOST_BITS_PER_WIDE_INT >= 64
3705 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3706 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3707 #endif
3708 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3709 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3710 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3711 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3712 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3713 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3714 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3715 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3717 #define C_COMMON_FIXED_TYPES(NAME) \
3718 if (type1 == short_ ## NAME ## _type_node \
3719 || type1 == unsigned_short_ ## NAME ## _type_node) \
3720 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3721 : short_ ## NAME ## _type_node; \
3722 if (type1 == NAME ## _type_node \
3723 || type1 == unsigned_ ## NAME ## _type_node) \
3724 return unsignedp ? unsigned_ ## NAME ## _type_node \
3725 : NAME ## _type_node; \
3726 if (type1 == long_ ## NAME ## _type_node \
3727 || type1 == unsigned_long_ ## NAME ## _type_node) \
3728 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3729 : long_ ## NAME ## _type_node; \
3730 if (type1 == long_long_ ## NAME ## _type_node \
3731 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3732 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3733 : long_long_ ## NAME ## _type_node;
3735 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3736 if (type1 == NAME ## _type_node \
3737 || type1 == u ## NAME ## _type_node) \
3738 return unsignedp ? u ## NAME ## _type_node \
3739 : NAME ## _type_node;
3741 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3742 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3743 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3744 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3745 : sat_ ## short_ ## NAME ## _type_node; \
3746 if (type1 == sat_ ## NAME ## _type_node \
3747 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3748 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3749 : sat_ ## NAME ## _type_node; \
3750 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3751 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3752 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3753 : sat_ ## long_ ## NAME ## _type_node; \
3754 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3755 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3756 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3757 : sat_ ## long_long_ ## NAME ## _type_node;
3759 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3760 if (type1 == sat_ ## NAME ## _type_node \
3761 || type1 == sat_ ## u ## NAME ## _type_node) \
3762 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3763 : sat_ ## NAME ## _type_node;
3765 C_COMMON_FIXED_TYPES (fract);
3766 C_COMMON_FIXED_TYPES_SAT (fract);
3767 C_COMMON_FIXED_TYPES (accum);
3768 C_COMMON_FIXED_TYPES_SAT (accum);
3770 C_COMMON_FIXED_MODE_TYPES (qq);
3771 C_COMMON_FIXED_MODE_TYPES (hq);
3772 C_COMMON_FIXED_MODE_TYPES (sq);
3773 C_COMMON_FIXED_MODE_TYPES (dq);
3774 C_COMMON_FIXED_MODE_TYPES (tq);
3775 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3776 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3777 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3778 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3779 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3780 C_COMMON_FIXED_MODE_TYPES (ha);
3781 C_COMMON_FIXED_MODE_TYPES (sa);
3782 C_COMMON_FIXED_MODE_TYPES (da);
3783 C_COMMON_FIXED_MODE_TYPES (ta);
3784 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3785 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3786 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3787 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3789 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3790 the precision; they have precision set to match their range, but
3791 may use a wider mode to match an ABI. If we change modes, we may
3792 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3793 the precision as well, so as to yield correct results for
3794 bit-field types. C++ does not have these separate bit-field
3795 types, and producing a signed or unsigned variant of an
3796 ENUMERAL_TYPE may cause other problems as well. */
3798 if (!INTEGRAL_TYPE_P (type)
3799 || TYPE_UNSIGNED (type) == unsignedp)
3800 return type;
3802 #define TYPE_OK(node) \
3803 (TYPE_MODE (type) == TYPE_MODE (node) \
3804 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3805 if (TYPE_OK (signed_char_type_node))
3806 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3807 if (TYPE_OK (integer_type_node))
3808 return unsignedp ? unsigned_type_node : integer_type_node;
3809 if (TYPE_OK (short_integer_type_node))
3810 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3811 if (TYPE_OK (long_integer_type_node))
3812 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3813 if (TYPE_OK (long_long_integer_type_node))
3814 return (unsignedp ? long_long_unsigned_type_node
3815 : long_long_integer_type_node);
3816 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3817 return (unsignedp ? int128_unsigned_type_node
3818 : int128_integer_type_node);
3819 if (TYPE_OK (widest_integer_literal_type_node))
3820 return (unsignedp ? widest_unsigned_literal_type_node
3821 : widest_integer_literal_type_node);
3823 #if HOST_BITS_PER_WIDE_INT >= 64
3824 if (TYPE_OK (intTI_type_node))
3825 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3826 #endif
3827 if (TYPE_OK (intDI_type_node))
3828 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3829 if (TYPE_OK (intSI_type_node))
3830 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3831 if (TYPE_OK (intHI_type_node))
3832 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3833 if (TYPE_OK (intQI_type_node))
3834 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3835 #undef TYPE_OK
3837 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3840 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3842 tree
3843 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3845 /* Extended integer types of the same width as a standard type have
3846 lesser rank, so those of the same width as int promote to int or
3847 unsigned int and are valid for printf formats expecting int or
3848 unsigned int. To avoid such special cases, avoid creating
3849 extended integer types for bit-fields if a standard integer type
3850 is available. */
3851 if (width == TYPE_PRECISION (integer_type_node))
3852 return unsignedp ? unsigned_type_node : integer_type_node;
3853 if (width == TYPE_PRECISION (signed_char_type_node))
3854 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3855 if (width == TYPE_PRECISION (short_integer_type_node))
3856 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3857 if (width == TYPE_PRECISION (long_integer_type_node))
3858 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3859 if (width == TYPE_PRECISION (long_long_integer_type_node))
3860 return (unsignedp ? long_long_unsigned_type_node
3861 : long_long_integer_type_node);
3862 if (int128_integer_type_node
3863 && width == TYPE_PRECISION (int128_integer_type_node))
3864 return (unsignedp ? int128_unsigned_type_node
3865 : int128_integer_type_node);
3866 return build_nonstandard_integer_type (width, unsignedp);
3869 /* The C version of the register_builtin_type langhook. */
3871 void
3872 c_register_builtin_type (tree type, const char* name)
3874 tree decl;
3876 decl = build_decl (UNKNOWN_LOCATION,
3877 TYPE_DECL, get_identifier (name), type);
3878 DECL_ARTIFICIAL (decl) = 1;
3879 if (!TYPE_NAME (type))
3880 TYPE_NAME (type) = decl;
3881 pushdecl (decl);
3883 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3886 /* Print an error message for invalid operands to arith operation
3887 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3888 LOCATION is the location of the message. */
3890 void
3891 binary_op_error (location_t location, enum tree_code code,
3892 tree type0, tree type1)
3894 const char *opname;
3896 switch (code)
3898 case PLUS_EXPR:
3899 opname = "+"; break;
3900 case MINUS_EXPR:
3901 opname = "-"; break;
3902 case MULT_EXPR:
3903 opname = "*"; break;
3904 case MAX_EXPR:
3905 opname = "max"; break;
3906 case MIN_EXPR:
3907 opname = "min"; break;
3908 case EQ_EXPR:
3909 opname = "=="; break;
3910 case NE_EXPR:
3911 opname = "!="; break;
3912 case LE_EXPR:
3913 opname = "<="; break;
3914 case GE_EXPR:
3915 opname = ">="; break;
3916 case LT_EXPR:
3917 opname = "<"; break;
3918 case GT_EXPR:
3919 opname = ">"; break;
3920 case LSHIFT_EXPR:
3921 opname = "<<"; break;
3922 case RSHIFT_EXPR:
3923 opname = ">>"; break;
3924 case TRUNC_MOD_EXPR:
3925 case FLOOR_MOD_EXPR:
3926 opname = "%"; break;
3927 case TRUNC_DIV_EXPR:
3928 case FLOOR_DIV_EXPR:
3929 opname = "/"; break;
3930 case BIT_AND_EXPR:
3931 opname = "&"; break;
3932 case BIT_IOR_EXPR:
3933 opname = "|"; break;
3934 case TRUTH_ANDIF_EXPR:
3935 opname = "&&"; break;
3936 case TRUTH_ORIF_EXPR:
3937 opname = "||"; break;
3938 case BIT_XOR_EXPR:
3939 opname = "^"; break;
3940 default:
3941 gcc_unreachable ();
3943 error_at (location,
3944 "invalid operands to binary %s (have %qT and %qT)", opname,
3945 type0, type1);
3948 /* Given an expression as a tree, return its original type. Do this
3949 by stripping any conversion that preserves the sign and precision. */
3950 static tree
3951 expr_original_type (tree expr)
3953 STRIP_SIGN_NOPS (expr);
3954 return TREE_TYPE (expr);
3957 /* Subroutine of build_binary_op, used for comparison operations.
3958 See if the operands have both been converted from subword integer types
3959 and, if so, perhaps change them both back to their original type.
3960 This function is also responsible for converting the two operands
3961 to the proper common type for comparison.
3963 The arguments of this function are all pointers to local variables
3964 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3965 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3967 If this function returns nonzero, it means that the comparison has
3968 a constant value. What this function returns is an expression for
3969 that value. */
3971 tree
3972 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3973 enum tree_code *rescode_ptr)
3975 tree type;
3976 tree op0 = *op0_ptr;
3977 tree op1 = *op1_ptr;
3978 int unsignedp0, unsignedp1;
3979 int real1, real2;
3980 tree primop0, primop1;
3981 enum tree_code code = *rescode_ptr;
3982 location_t loc = EXPR_LOC_OR_HERE (op0);
3984 /* Throw away any conversions to wider types
3985 already present in the operands. */
3987 primop0 = c_common_get_narrower (op0, &unsignedp0);
3988 primop1 = c_common_get_narrower (op1, &unsignedp1);
3990 /* If primopN is first sign-extended from primopN's precision to opN's
3991 precision, then zero-extended from opN's precision to
3992 *restype_ptr precision, shortenings might be invalid. */
3993 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3994 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3995 && !unsignedp0
3996 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3997 primop0 = op0;
3998 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3999 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4000 && !unsignedp1
4001 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4002 primop1 = op1;
4004 /* Handle the case that OP0 does not *contain* a conversion
4005 but it *requires* conversion to FINAL_TYPE. */
4007 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4008 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4009 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4010 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4012 /* If one of the operands must be floated, we cannot optimize. */
4013 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4014 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4016 /* If first arg is constant, swap the args (changing operation
4017 so value is preserved), for canonicalization. Don't do this if
4018 the second arg is 0. */
4020 if (TREE_CONSTANT (primop0)
4021 && !integer_zerop (primop1) && !real_zerop (primop1)
4022 && !fixed_zerop (primop1))
4024 tree tem = primop0;
4025 int temi = unsignedp0;
4026 primop0 = primop1;
4027 primop1 = tem;
4028 tem = op0;
4029 op0 = op1;
4030 op1 = tem;
4031 *op0_ptr = op0;
4032 *op1_ptr = op1;
4033 unsignedp0 = unsignedp1;
4034 unsignedp1 = temi;
4035 temi = real1;
4036 real1 = real2;
4037 real2 = temi;
4039 switch (code)
4041 case LT_EXPR:
4042 code = GT_EXPR;
4043 break;
4044 case GT_EXPR:
4045 code = LT_EXPR;
4046 break;
4047 case LE_EXPR:
4048 code = GE_EXPR;
4049 break;
4050 case GE_EXPR:
4051 code = LE_EXPR;
4052 break;
4053 default:
4054 break;
4056 *rescode_ptr = code;
4059 /* If comparing an integer against a constant more bits wide,
4060 maybe we can deduce a value of 1 or 0 independent of the data.
4061 Or else truncate the constant now
4062 rather than extend the variable at run time.
4064 This is only interesting if the constant is the wider arg.
4065 Also, it is not safe if the constant is unsigned and the
4066 variable arg is signed, since in this case the variable
4067 would be sign-extended and then regarded as unsigned.
4068 Our technique fails in this case because the lowest/highest
4069 possible unsigned results don't follow naturally from the
4070 lowest/highest possible values of the variable operand.
4071 For just EQ_EXPR and NE_EXPR there is another technique that
4072 could be used: see if the constant can be faithfully represented
4073 in the other operand's type, by truncating it and reextending it
4074 and see if that preserves the constant's value. */
4076 if (!real1 && !real2
4077 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4078 && TREE_CODE (primop1) == INTEGER_CST
4079 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4081 int min_gt, max_gt, min_lt, max_lt;
4082 tree maxval, minval;
4083 /* 1 if comparison is nominally unsigned. */
4084 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4085 tree val;
4087 type = c_common_signed_or_unsigned_type (unsignedp0,
4088 TREE_TYPE (primop0));
4090 maxval = TYPE_MAX_VALUE (type);
4091 minval = TYPE_MIN_VALUE (type);
4093 if (unsignedp && !unsignedp0)
4094 *restype_ptr = c_common_signed_type (*restype_ptr);
4096 if (TREE_TYPE (primop1) != *restype_ptr)
4098 /* Convert primop1 to target type, but do not introduce
4099 additional overflow. We know primop1 is an int_cst. */
4100 primop1 = force_fit_type_double (*restype_ptr,
4101 tree_to_double_int (primop1),
4102 0, TREE_OVERFLOW (primop1));
4104 if (type != *restype_ptr)
4106 minval = convert (*restype_ptr, minval);
4107 maxval = convert (*restype_ptr, maxval);
4110 if (unsignedp && unsignedp0)
4112 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4113 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4114 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4115 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4117 else
4119 min_gt = INT_CST_LT (primop1, minval);
4120 max_gt = INT_CST_LT (primop1, maxval);
4121 min_lt = INT_CST_LT (minval, primop1);
4122 max_lt = INT_CST_LT (maxval, primop1);
4125 val = 0;
4126 /* This used to be a switch, but Genix compiler can't handle that. */
4127 if (code == NE_EXPR)
4129 if (max_lt || min_gt)
4130 val = truthvalue_true_node;
4132 else if (code == EQ_EXPR)
4134 if (max_lt || min_gt)
4135 val = truthvalue_false_node;
4137 else if (code == LT_EXPR)
4139 if (max_lt)
4140 val = truthvalue_true_node;
4141 if (!min_lt)
4142 val = truthvalue_false_node;
4144 else if (code == GT_EXPR)
4146 if (min_gt)
4147 val = truthvalue_true_node;
4148 if (!max_gt)
4149 val = truthvalue_false_node;
4151 else if (code == LE_EXPR)
4153 if (!max_gt)
4154 val = truthvalue_true_node;
4155 if (min_gt)
4156 val = truthvalue_false_node;
4158 else if (code == GE_EXPR)
4160 if (!min_lt)
4161 val = truthvalue_true_node;
4162 if (max_lt)
4163 val = truthvalue_false_node;
4166 /* If primop0 was sign-extended and unsigned comparison specd,
4167 we did a signed comparison above using the signed type bounds.
4168 But the comparison we output must be unsigned.
4170 Also, for inequalities, VAL is no good; but if the signed
4171 comparison had *any* fixed result, it follows that the
4172 unsigned comparison just tests the sign in reverse
4173 (positive values are LE, negative ones GE).
4174 So we can generate an unsigned comparison
4175 against an extreme value of the signed type. */
4177 if (unsignedp && !unsignedp0)
4179 if (val != 0)
4180 switch (code)
4182 case LT_EXPR:
4183 case GE_EXPR:
4184 primop1 = TYPE_MIN_VALUE (type);
4185 val = 0;
4186 break;
4188 case LE_EXPR:
4189 case GT_EXPR:
4190 primop1 = TYPE_MAX_VALUE (type);
4191 val = 0;
4192 break;
4194 default:
4195 break;
4197 type = c_common_unsigned_type (type);
4200 if (TREE_CODE (primop0) != INTEGER_CST
4201 && c_inhibit_evaluation_warnings == 0)
4203 if (val == truthvalue_false_node)
4204 warning_at (loc, OPT_Wtype_limits,
4205 "comparison is always false due to limited range of data type");
4206 if (val == truthvalue_true_node)
4207 warning_at (loc, OPT_Wtype_limits,
4208 "comparison is always true due to limited range of data type");
4211 if (val != 0)
4213 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4214 if (TREE_SIDE_EFFECTS (primop0))
4215 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4216 return val;
4219 /* Value is not predetermined, but do the comparison
4220 in the type of the operand that is not constant.
4221 TYPE is already properly set. */
4224 /* If either arg is decimal float and the other is float, find the
4225 proper common type to use for comparison. */
4226 else if (real1 && real2
4227 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4228 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4229 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4231 else if (real1 && real2
4232 && (TYPE_PRECISION (TREE_TYPE (primop0))
4233 == TYPE_PRECISION (TREE_TYPE (primop1))))
4234 type = TREE_TYPE (primop0);
4236 /* If args' natural types are both narrower than nominal type
4237 and both extend in the same manner, compare them
4238 in the type of the wider arg.
4239 Otherwise must actually extend both to the nominal
4240 common type lest different ways of extending
4241 alter the result.
4242 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4244 else if (unsignedp0 == unsignedp1 && real1 == real2
4245 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4246 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4248 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4249 type = c_common_signed_or_unsigned_type (unsignedp0
4250 || TYPE_UNSIGNED (*restype_ptr),
4251 type);
4252 /* Make sure shorter operand is extended the right way
4253 to match the longer operand. */
4254 primop0
4255 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4256 TREE_TYPE (primop0)),
4257 primop0);
4258 primop1
4259 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4260 TREE_TYPE (primop1)),
4261 primop1);
4263 else
4265 /* Here we must do the comparison on the nominal type
4266 using the args exactly as we received them. */
4267 type = *restype_ptr;
4268 primop0 = op0;
4269 primop1 = op1;
4271 if (!real1 && !real2 && integer_zerop (primop1)
4272 && TYPE_UNSIGNED (*restype_ptr))
4274 tree value = 0;
4275 /* All unsigned values are >= 0, so we warn. However,
4276 if OP0 is a constant that is >= 0, the signedness of
4277 the comparison isn't an issue, so suppress the
4278 warning. */
4279 bool warn =
4280 warn_type_limits && !in_system_header
4281 && c_inhibit_evaluation_warnings == 0
4282 && !(TREE_CODE (primop0) == INTEGER_CST
4283 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4284 primop0)))
4285 /* Do not warn for enumeration types. */
4286 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4288 switch (code)
4290 case GE_EXPR:
4291 if (warn)
4292 warning_at (loc, OPT_Wtype_limits,
4293 "comparison of unsigned expression >= 0 is always true");
4294 value = truthvalue_true_node;
4295 break;
4297 case LT_EXPR:
4298 if (warn)
4299 warning_at (loc, OPT_Wtype_limits,
4300 "comparison of unsigned expression < 0 is always false");
4301 value = truthvalue_false_node;
4302 break;
4304 default:
4305 break;
4308 if (value != 0)
4310 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4311 if (TREE_SIDE_EFFECTS (primop0))
4312 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4313 primop0, value);
4314 return value;
4319 *op0_ptr = convert (type, primop0);
4320 *op1_ptr = convert (type, primop1);
4322 *restype_ptr = truthvalue_type_node;
4324 return 0;
4327 /* Return a tree for the sum or difference (RESULTCODE says which)
4328 of pointer PTROP and integer INTOP. */
4330 tree
4331 pointer_int_sum (location_t loc, enum tree_code resultcode,
4332 tree ptrop, tree intop, bool complain)
4334 tree size_exp, ret;
4336 /* The result is a pointer of the same type that is being added. */
4337 tree result_type = TREE_TYPE (ptrop);
4339 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4341 if (complain && warn_pointer_arith)
4342 pedwarn (loc, OPT_Wpointer_arith,
4343 "pointer of type %<void *%> used in arithmetic");
4344 else if (!complain)
4345 return error_mark_node;
4346 size_exp = integer_one_node;
4348 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4350 if (complain && warn_pointer_arith)
4351 pedwarn (loc, OPT_Wpointer_arith,
4352 "pointer to a function used in arithmetic");
4353 else if (!complain)
4354 return error_mark_node;
4355 size_exp = integer_one_node;
4357 else
4358 size_exp = size_in_bytes (TREE_TYPE (result_type));
4360 /* We are manipulating pointer values, so we don't need to warn
4361 about relying on undefined signed overflow. We disable the
4362 warning here because we use integer types so fold won't know that
4363 they are really pointers. */
4364 fold_defer_overflow_warnings ();
4366 /* If what we are about to multiply by the size of the elements
4367 contains a constant term, apply distributive law
4368 and multiply that constant term separately.
4369 This helps produce common subexpressions. */
4370 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4371 && !TREE_CONSTANT (intop)
4372 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4373 && TREE_CONSTANT (size_exp)
4374 /* If the constant comes from pointer subtraction,
4375 skip this optimization--it would cause an error. */
4376 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4377 /* If the constant is unsigned, and smaller than the pointer size,
4378 then we must skip this optimization. This is because it could cause
4379 an overflow error if the constant is negative but INTOP is not. */
4380 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4381 || (TYPE_PRECISION (TREE_TYPE (intop))
4382 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4384 enum tree_code subcode = resultcode;
4385 tree int_type = TREE_TYPE (intop);
4386 if (TREE_CODE (intop) == MINUS_EXPR)
4387 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4388 /* Convert both subexpression types to the type of intop,
4389 because weird cases involving pointer arithmetic
4390 can result in a sum or difference with different type args. */
4391 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4392 subcode, ptrop,
4393 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4394 intop = convert (int_type, TREE_OPERAND (intop, 0));
4397 /* Convert the integer argument to a type the same size as sizetype
4398 so the multiply won't overflow spuriously. */
4399 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4400 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4401 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4402 TYPE_UNSIGNED (sizetype)), intop);
4404 /* Replace the integer argument with a suitable product by the object size.
4405 Do this multiplication as signed, then convert to the appropriate type
4406 for the pointer operation and disregard an overflow that occurred only
4407 because of the sign-extension change in the latter conversion. */
4409 tree t = build_binary_op (loc,
4410 MULT_EXPR, intop,
4411 convert (TREE_TYPE (intop), size_exp), 1);
4412 intop = convert (sizetype, t);
4413 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4414 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4415 TREE_INT_CST_HIGH (intop));
4418 /* Create the sum or difference. */
4419 if (resultcode == MINUS_EXPR)
4420 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4422 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4424 fold_undefer_and_ignore_overflow_warnings ();
4426 return ret;
4429 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4430 and if NON_CONST is known not to be permitted in an evaluated part
4431 of a constant expression. */
4433 tree
4434 c_wrap_maybe_const (tree expr, bool non_const)
4436 bool nowarning = TREE_NO_WARNING (expr);
4437 location_t loc = EXPR_LOCATION (expr);
4439 /* This should never be called for C++. */
4440 if (c_dialect_cxx ())
4441 gcc_unreachable ();
4443 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4444 STRIP_TYPE_NOPS (expr);
4445 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4446 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4447 if (nowarning)
4448 TREE_NO_WARNING (expr) = 1;
4449 protected_set_expr_location (expr, loc);
4451 return expr;
4454 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4455 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4456 around the SAVE_EXPR if needed so that c_fully_fold does not need
4457 to look inside SAVE_EXPRs. */
4459 tree
4460 c_save_expr (tree expr)
4462 bool maybe_const = true;
4463 if (c_dialect_cxx ())
4464 return save_expr (expr);
4465 expr = c_fully_fold (expr, false, &maybe_const);
4466 expr = save_expr (expr);
4467 if (!maybe_const)
4468 expr = c_wrap_maybe_const (expr, true);
4469 return expr;
4472 /* Return whether EXPR is a declaration whose address can never be
4473 NULL. */
4475 bool
4476 decl_with_nonnull_addr_p (const_tree expr)
4478 return (DECL_P (expr)
4479 && (TREE_CODE (expr) == PARM_DECL
4480 || TREE_CODE (expr) == LABEL_DECL
4481 || !DECL_WEAK (expr)));
4484 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4485 or for an `if' or `while' statement or ?..: exp. It should already
4486 have been validated to be of suitable type; otherwise, a bad
4487 diagnostic may result.
4489 The EXPR is located at LOCATION.
4491 This preparation consists of taking the ordinary
4492 representation of an expression expr and producing a valid tree
4493 boolean expression describing whether expr is nonzero. We could
4494 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4495 but we optimize comparisons, &&, ||, and !.
4497 The resulting type should always be `truthvalue_type_node'. */
4499 tree
4500 c_common_truthvalue_conversion (location_t location, tree expr)
4502 switch (TREE_CODE (expr))
4504 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4505 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4506 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4507 case ORDERED_EXPR: case UNORDERED_EXPR:
4508 if (TREE_TYPE (expr) == truthvalue_type_node)
4509 return expr;
4510 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4511 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4512 goto ret;
4514 case TRUTH_ANDIF_EXPR:
4515 case TRUTH_ORIF_EXPR:
4516 case TRUTH_AND_EXPR:
4517 case TRUTH_OR_EXPR:
4518 case TRUTH_XOR_EXPR:
4519 if (TREE_TYPE (expr) == truthvalue_type_node)
4520 return expr;
4521 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4522 c_common_truthvalue_conversion (location,
4523 TREE_OPERAND (expr, 0)),
4524 c_common_truthvalue_conversion (location,
4525 TREE_OPERAND (expr, 1)));
4526 goto ret;
4528 case TRUTH_NOT_EXPR:
4529 if (TREE_TYPE (expr) == truthvalue_type_node)
4530 return expr;
4531 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4532 c_common_truthvalue_conversion (location,
4533 TREE_OPERAND (expr, 0)));
4534 goto ret;
4536 case ERROR_MARK:
4537 return expr;
4539 case INTEGER_CST:
4540 return integer_zerop (expr) ? truthvalue_false_node
4541 : truthvalue_true_node;
4543 case REAL_CST:
4544 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4545 ? truthvalue_true_node
4546 : truthvalue_false_node;
4548 case FIXED_CST:
4549 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4550 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4551 ? truthvalue_true_node
4552 : truthvalue_false_node;
4554 case FUNCTION_DECL:
4555 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4556 /* Fall through. */
4558 case ADDR_EXPR:
4560 tree inner = TREE_OPERAND (expr, 0);
4561 if (decl_with_nonnull_addr_p (inner))
4563 /* Common Ada/Pascal programmer's mistake. */
4564 warning_at (location,
4565 OPT_Waddress,
4566 "the address of %qD will always evaluate as %<true%>",
4567 inner);
4568 return truthvalue_true_node;
4570 break;
4573 case COMPLEX_EXPR:
4574 expr = build_binary_op (EXPR_LOCATION (expr),
4575 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4576 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4577 c_common_truthvalue_conversion (location,
4578 TREE_OPERAND (expr, 0)),
4579 c_common_truthvalue_conversion (location,
4580 TREE_OPERAND (expr, 1)),
4582 goto ret;
4584 case NEGATE_EXPR:
4585 case ABS_EXPR:
4586 case FLOAT_EXPR:
4587 case EXCESS_PRECISION_EXPR:
4588 /* These don't change whether an object is nonzero or zero. */
4589 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4591 case LROTATE_EXPR:
4592 case RROTATE_EXPR:
4593 /* These don't change whether an object is zero or nonzero, but
4594 we can't ignore them if their second arg has side-effects. */
4595 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4597 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4598 TREE_OPERAND (expr, 1),
4599 c_common_truthvalue_conversion
4600 (location, TREE_OPERAND (expr, 0)));
4601 goto ret;
4603 else
4604 return c_common_truthvalue_conversion (location,
4605 TREE_OPERAND (expr, 0));
4607 case COND_EXPR:
4608 /* Distribute the conversion into the arms of a COND_EXPR. */
4609 if (c_dialect_cxx ())
4611 tree op1 = TREE_OPERAND (expr, 1);
4612 tree op2 = TREE_OPERAND (expr, 2);
4613 /* In C++ one of the arms might have void type if it is throw. */
4614 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4615 op1 = c_common_truthvalue_conversion (location, op1);
4616 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4617 op2 = c_common_truthvalue_conversion (location, op2);
4618 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4619 TREE_OPERAND (expr, 0), op1, op2);
4620 goto ret;
4622 else
4624 /* Folding will happen later for C. */
4625 expr = build3 (COND_EXPR, truthvalue_type_node,
4626 TREE_OPERAND (expr, 0),
4627 c_common_truthvalue_conversion (location,
4628 TREE_OPERAND (expr, 1)),
4629 c_common_truthvalue_conversion (location,
4630 TREE_OPERAND (expr, 2)));
4631 goto ret;
4634 CASE_CONVERT:
4636 tree totype = TREE_TYPE (expr);
4637 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4639 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4640 since that affects how `default_conversion' will behave. */
4641 if (TREE_CODE (totype) == REFERENCE_TYPE
4642 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4643 break;
4644 /* Don't strip a conversion from C++0x scoped enum, since they
4645 don't implicitly convert to other types. */
4646 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4647 && ENUM_IS_SCOPED (fromtype))
4648 break;
4649 /* If this isn't narrowing the argument, we can ignore it. */
4650 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4651 return c_common_truthvalue_conversion (location,
4652 TREE_OPERAND (expr, 0));
4654 break;
4656 case MODIFY_EXPR:
4657 if (!TREE_NO_WARNING (expr)
4658 && warn_parentheses)
4660 warning (OPT_Wparentheses,
4661 "suggest parentheses around assignment used as truth value");
4662 TREE_NO_WARNING (expr) = 1;
4664 break;
4666 default:
4667 break;
4670 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4672 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4673 expr = (build_binary_op
4674 (EXPR_LOCATION (expr),
4675 (TREE_SIDE_EFFECTS (expr)
4676 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4677 c_common_truthvalue_conversion
4678 (location,
4679 build_unary_op (location, REALPART_EXPR, t, 0)),
4680 c_common_truthvalue_conversion
4681 (location,
4682 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4683 0));
4684 goto ret;
4687 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4689 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4690 FCONST0 (TYPE_MODE
4691 (TREE_TYPE (expr))));
4692 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4694 else
4695 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4697 ret:
4698 protected_set_expr_location (expr, location);
4699 return expr;
4702 static void def_builtin_1 (enum built_in_function fncode,
4703 const char *name,
4704 enum built_in_class fnclass,
4705 tree fntype, tree libtype,
4706 bool both_p, bool fallback_p, bool nonansi_p,
4707 tree fnattrs, bool implicit_p);
4710 /* Apply the TYPE_QUALS to the new DECL. */
4712 void
4713 c_apply_type_quals_to_decl (int type_quals, tree decl)
4715 tree type = TREE_TYPE (decl);
4717 if (type == error_mark_node)
4718 return;
4720 if ((type_quals & TYPE_QUAL_CONST)
4721 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4722 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4723 constructor can produce constant init, so rely on cp_finish_decl to
4724 clear TREE_READONLY if the variable has non-constant init. */
4725 TREE_READONLY (decl) = 1;
4726 if (type_quals & TYPE_QUAL_VOLATILE)
4728 TREE_SIDE_EFFECTS (decl) = 1;
4729 TREE_THIS_VOLATILE (decl) = 1;
4731 if (type_quals & TYPE_QUAL_RESTRICT)
4733 while (type && TREE_CODE (type) == ARRAY_TYPE)
4734 /* Allow 'restrict' on arrays of pointers.
4735 FIXME currently we just ignore it. */
4736 type = TREE_TYPE (type);
4737 if (!type
4738 || !POINTER_TYPE_P (type)
4739 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4740 error ("invalid use of %<restrict%>");
4744 /* Hash function for the problem of multiple type definitions in
4745 different files. This must hash all types that will compare
4746 equal via comptypes to the same value. In practice it hashes
4747 on some of the simple stuff and leaves the details to comptypes. */
4749 static hashval_t
4750 c_type_hash (const void *p)
4752 int n_elements;
4753 int shift, size;
4754 const_tree const t = (const_tree) p;
4755 tree t2;
4756 switch (TREE_CODE (t))
4758 /* For pointers, hash on pointee type plus some swizzling. */
4759 case POINTER_TYPE:
4760 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4761 /* Hash on number of elements and total size. */
4762 case ENUMERAL_TYPE:
4763 shift = 3;
4764 t2 = TYPE_VALUES (t);
4765 break;
4766 case RECORD_TYPE:
4767 shift = 0;
4768 t2 = TYPE_FIELDS (t);
4769 break;
4770 case QUAL_UNION_TYPE:
4771 shift = 1;
4772 t2 = TYPE_FIELDS (t);
4773 break;
4774 case UNION_TYPE:
4775 shift = 2;
4776 t2 = TYPE_FIELDS (t);
4777 break;
4778 default:
4779 gcc_unreachable ();
4781 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4782 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4783 n_elements = list_length (t2);
4784 /* We might have a VLA here. */
4785 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4786 size = 0;
4787 else
4788 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4789 return ((size << 24) | (n_elements << shift));
4792 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4794 /* Return the typed-based alias set for T, which may be an expression
4795 or a type. Return -1 if we don't do anything special. */
4797 alias_set_type
4798 c_common_get_alias_set (tree t)
4800 tree u;
4801 PTR *slot;
4803 /* For VLAs, use the alias set of the element type rather than the
4804 default of alias set 0 for types compared structurally. */
4805 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4807 if (TREE_CODE (t) == ARRAY_TYPE)
4808 return get_alias_set (TREE_TYPE (t));
4809 return -1;
4812 /* Permit type-punning when accessing a union, provided the access
4813 is directly through the union. For example, this code does not
4814 permit taking the address of a union member and then storing
4815 through it. Even the type-punning allowed here is a GCC
4816 extension, albeit a common and useful one; the C standard says
4817 that such accesses have implementation-defined behavior. */
4818 for (u = t;
4819 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4820 u = TREE_OPERAND (u, 0))
4821 if (TREE_CODE (u) == COMPONENT_REF
4822 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4823 return 0;
4825 /* That's all the expressions we handle specially. */
4826 if (!TYPE_P (t))
4827 return -1;
4829 /* The C standard guarantees that any object may be accessed via an
4830 lvalue that has character type. */
4831 if (t == char_type_node
4832 || t == signed_char_type_node
4833 || t == unsigned_char_type_node)
4834 return 0;
4836 /* The C standard specifically allows aliasing between signed and
4837 unsigned variants of the same type. We treat the signed
4838 variant as canonical. */
4839 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4841 tree t1 = c_common_signed_type (t);
4843 /* t1 == t can happen for boolean nodes which are always unsigned. */
4844 if (t1 != t)
4845 return get_alias_set (t1);
4848 /* Handle the case of multiple type nodes referring to "the same" type,
4849 which occurs with IMA. These share an alias set. FIXME: Currently only
4850 C90 is handled. (In C99 type compatibility is not transitive, which
4851 complicates things mightily. The alias set splay trees can theoretically
4852 represent this, but insertion is tricky when you consider all the
4853 different orders things might arrive in.) */
4855 if (c_language != clk_c || flag_isoc99)
4856 return -1;
4858 /* Save time if there's only one input file. */
4859 if (num_in_fnames == 1)
4860 return -1;
4862 /* Pointers need special handling if they point to any type that
4863 needs special handling (below). */
4864 if (TREE_CODE (t) == POINTER_TYPE)
4866 tree t2;
4867 /* Find bottom type under any nested POINTERs. */
4868 for (t2 = TREE_TYPE (t);
4869 TREE_CODE (t2) == POINTER_TYPE;
4870 t2 = TREE_TYPE (t2))
4872 if (TREE_CODE (t2) != RECORD_TYPE
4873 && TREE_CODE (t2) != ENUMERAL_TYPE
4874 && TREE_CODE (t2) != QUAL_UNION_TYPE
4875 && TREE_CODE (t2) != UNION_TYPE)
4876 return -1;
4877 if (TYPE_SIZE (t2) == 0)
4878 return -1;
4880 /* These are the only cases that need special handling. */
4881 if (TREE_CODE (t) != RECORD_TYPE
4882 && TREE_CODE (t) != ENUMERAL_TYPE
4883 && TREE_CODE (t) != QUAL_UNION_TYPE
4884 && TREE_CODE (t) != UNION_TYPE
4885 && TREE_CODE (t) != POINTER_TYPE)
4886 return -1;
4887 /* Undefined? */
4888 if (TYPE_SIZE (t) == 0)
4889 return -1;
4891 /* Look up t in hash table. Only one of the compatible types within each
4892 alias set is recorded in the table. */
4893 if (!type_hash_table)
4894 type_hash_table = htab_create_ggc (1021, c_type_hash,
4895 (htab_eq) lang_hooks.types_compatible_p,
4896 NULL);
4897 slot = htab_find_slot (type_hash_table, t, INSERT);
4898 if (*slot != NULL)
4900 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4901 return TYPE_ALIAS_SET ((tree)*slot);
4903 else
4904 /* Our caller will assign and record (in t) a new alias set; all we need
4905 to do is remember t in the hash table. */
4906 *slot = t;
4908 return -1;
4911 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4912 the second parameter indicates which OPERATOR is being applied.
4913 The COMPLAIN flag controls whether we should diagnose possibly
4914 ill-formed constructs or not. LOC is the location of the SIZEOF or
4915 TYPEOF operator. */
4917 tree
4918 c_sizeof_or_alignof_type (location_t loc,
4919 tree type, bool is_sizeof, int complain)
4921 const char *op_name;
4922 tree value = NULL;
4923 enum tree_code type_code = TREE_CODE (type);
4925 op_name = is_sizeof ? "sizeof" : "__alignof__";
4927 if (type_code == FUNCTION_TYPE)
4929 if (is_sizeof)
4931 if (complain && warn_pointer_arith)
4932 pedwarn (loc, OPT_Wpointer_arith,
4933 "invalid application of %<sizeof%> to a function type");
4934 else if (!complain)
4935 return error_mark_node;
4936 value = size_one_node;
4938 else
4940 if (complain)
4942 if (c_dialect_cxx ())
4943 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4944 "%<alignof%> applied to a function type");
4945 else
4946 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4947 "%<_Alignof%> applied to a function type");
4949 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4952 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4954 if (type_code == VOID_TYPE
4955 && complain && warn_pointer_arith)
4956 pedwarn (loc, OPT_Wpointer_arith,
4957 "invalid application of %qs to a void type", op_name);
4958 else if (!complain)
4959 return error_mark_node;
4960 value = size_one_node;
4962 else if (!COMPLETE_TYPE_P (type)
4963 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4965 if (complain)
4966 error_at (loc, "invalid application of %qs to incomplete type %qT",
4967 op_name, type);
4968 return error_mark_node;
4970 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4971 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4973 if (complain)
4974 error_at (loc, "invalid application of %qs to array type %qT of "
4975 "incomplete element type", op_name, type);
4976 return error_mark_node;
4978 else
4980 if (is_sizeof)
4981 /* Convert in case a char is more than one unit. */
4982 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4983 size_int (TYPE_PRECISION (char_type_node)
4984 / BITS_PER_UNIT));
4985 else
4986 value = size_int (TYPE_ALIGN_UNIT (type));
4989 /* VALUE will have the middle-end integer type sizetype.
4990 However, we should really return a value of type `size_t',
4991 which is just a typedef for an ordinary integer type. */
4992 value = fold_convert_loc (loc, size_type_node, value);
4994 return value;
4997 /* Implement the __alignof keyword: Return the minimum required
4998 alignment of EXPR, measured in bytes. For VAR_DECLs,
4999 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5000 from an "aligned" __attribute__ specification). LOC is the
5001 location of the ALIGNOF operator. */
5003 tree
5004 c_alignof_expr (location_t loc, tree expr)
5006 tree t;
5008 if (VAR_OR_FUNCTION_DECL_P (expr))
5009 t = size_int (DECL_ALIGN_UNIT (expr));
5011 else if (TREE_CODE (expr) == COMPONENT_REF
5012 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5014 error_at (loc, "%<__alignof%> applied to a bit-field");
5015 t = size_one_node;
5017 else if (TREE_CODE (expr) == COMPONENT_REF
5018 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5019 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5021 else if (TREE_CODE (expr) == INDIRECT_REF)
5023 tree t = TREE_OPERAND (expr, 0);
5024 tree best = t;
5025 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5027 while (CONVERT_EXPR_P (t)
5028 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5030 int thisalign;
5032 t = TREE_OPERAND (t, 0);
5033 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5034 if (thisalign > bestalign)
5035 best = t, bestalign = thisalign;
5037 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5039 else
5040 return c_alignof (loc, TREE_TYPE (expr));
5042 return fold_convert_loc (loc, size_type_node, t);
5045 /* Handle C and C++ default attributes. */
5047 enum built_in_attribute
5049 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5050 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5051 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5052 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5053 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5054 #include "builtin-attrs.def"
5055 #undef DEF_ATTR_NULL_TREE
5056 #undef DEF_ATTR_INT
5057 #undef DEF_ATTR_STRING
5058 #undef DEF_ATTR_IDENT
5059 #undef DEF_ATTR_TREE_LIST
5060 ATTR_LAST
5063 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5065 static void c_init_attributes (void);
5067 enum c_builtin_type
5069 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5070 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5071 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5072 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5073 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5074 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5075 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5076 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5077 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5078 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5079 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5080 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5081 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5082 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5083 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5084 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5085 NAME,
5086 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5087 #include "builtin-types.def"
5088 #undef DEF_PRIMITIVE_TYPE
5089 #undef DEF_FUNCTION_TYPE_0
5090 #undef DEF_FUNCTION_TYPE_1
5091 #undef DEF_FUNCTION_TYPE_2
5092 #undef DEF_FUNCTION_TYPE_3
5093 #undef DEF_FUNCTION_TYPE_4
5094 #undef DEF_FUNCTION_TYPE_5
5095 #undef DEF_FUNCTION_TYPE_6
5096 #undef DEF_FUNCTION_TYPE_7
5097 #undef DEF_FUNCTION_TYPE_8
5098 #undef DEF_FUNCTION_TYPE_VAR_0
5099 #undef DEF_FUNCTION_TYPE_VAR_1
5100 #undef DEF_FUNCTION_TYPE_VAR_2
5101 #undef DEF_FUNCTION_TYPE_VAR_3
5102 #undef DEF_FUNCTION_TYPE_VAR_4
5103 #undef DEF_FUNCTION_TYPE_VAR_5
5104 #undef DEF_POINTER_TYPE
5105 BT_LAST
5108 typedef enum c_builtin_type builtin_type;
5110 /* A temporary array for c_common_nodes_and_builtins. Used in
5111 communication with def_fn_type. */
5112 static tree builtin_types[(int) BT_LAST + 1];
5114 /* A helper function for c_common_nodes_and_builtins. Build function type
5115 for DEF with return type RET and N arguments. If VAR is true, then the
5116 function should be variadic after those N arguments.
5118 Takes special care not to ICE if any of the types involved are
5119 error_mark_node, which indicates that said type is not in fact available
5120 (see builtin_type_for_size). In which case the function type as a whole
5121 should be error_mark_node. */
5123 static void
5124 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5126 tree t;
5127 tree *args = XALLOCAVEC (tree, n);
5128 va_list list;
5129 int i;
5131 va_start (list, n);
5132 for (i = 0; i < n; ++i)
5134 builtin_type a = (builtin_type) va_arg (list, int);
5135 t = builtin_types[a];
5136 if (t == error_mark_node)
5137 goto egress;
5138 args[i] = t;
5141 t = builtin_types[ret];
5142 if (t == error_mark_node)
5143 goto egress;
5144 if (var)
5145 t = build_varargs_function_type_array (t, n, args);
5146 else
5147 t = build_function_type_array (t, n, args);
5149 egress:
5150 builtin_types[def] = t;
5151 va_end (list);
5154 /* Build builtin functions common to both C and C++ language
5155 frontends. */
5157 static void
5158 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5160 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5161 builtin_types[ENUM] = VALUE;
5162 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5163 def_fn_type (ENUM, RETURN, 0, 0);
5164 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5165 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5166 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5167 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5168 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5169 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5170 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5171 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5172 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5173 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5174 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5175 ARG6) \
5176 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5177 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5178 ARG6, ARG7) \
5179 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5180 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5181 ARG6, ARG7, ARG8) \
5182 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5183 ARG7, ARG8);
5184 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5185 def_fn_type (ENUM, RETURN, 1, 0);
5186 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5187 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5188 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5189 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5190 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5191 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5192 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5193 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5194 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5195 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5196 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5197 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5199 #include "builtin-types.def"
5201 #undef DEF_PRIMITIVE_TYPE
5202 #undef DEF_FUNCTION_TYPE_1
5203 #undef DEF_FUNCTION_TYPE_2
5204 #undef DEF_FUNCTION_TYPE_3
5205 #undef DEF_FUNCTION_TYPE_4
5206 #undef DEF_FUNCTION_TYPE_5
5207 #undef DEF_FUNCTION_TYPE_6
5208 #undef DEF_FUNCTION_TYPE_VAR_0
5209 #undef DEF_FUNCTION_TYPE_VAR_1
5210 #undef DEF_FUNCTION_TYPE_VAR_2
5211 #undef DEF_FUNCTION_TYPE_VAR_3
5212 #undef DEF_FUNCTION_TYPE_VAR_4
5213 #undef DEF_FUNCTION_TYPE_VAR_5
5214 #undef DEF_POINTER_TYPE
5215 builtin_types[(int) BT_LAST] = NULL_TREE;
5217 c_init_attributes ();
5219 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5220 NONANSI_P, ATTRS, IMPLICIT, COND) \
5221 if (NAME && COND) \
5222 def_builtin_1 (ENUM, NAME, CLASS, \
5223 builtin_types[(int) TYPE], \
5224 builtin_types[(int) LIBTYPE], \
5225 BOTH_P, FALLBACK_P, NONANSI_P, \
5226 built_in_attributes[(int) ATTRS], IMPLICIT);
5227 #include "builtins.def"
5228 #undef DEF_BUILTIN
5230 targetm.init_builtins ();
5232 build_common_builtin_nodes ();
5234 if (flag_enable_cilkplus)
5235 cilk_init_builtins ();
5238 /* Like get_identifier, but avoid warnings about null arguments when
5239 the argument may be NULL for targets where GCC lacks stdint.h type
5240 information. */
5242 static inline tree
5243 c_get_ident (const char *id)
5245 return get_identifier (id);
5248 /* Build tree nodes and builtin functions common to both C and C++ language
5249 frontends. */
5251 void
5252 c_common_nodes_and_builtins (void)
5254 int char16_type_size;
5255 int char32_type_size;
5256 int wchar_type_size;
5257 tree array_domain_type;
5258 tree va_list_ref_type_node;
5259 tree va_list_arg_type_node;
5261 build_common_tree_nodes (flag_signed_char, flag_short_double);
5263 /* Define `int' and `char' first so that dbx will output them first. */
5264 record_builtin_type (RID_INT, NULL, integer_type_node);
5265 record_builtin_type (RID_CHAR, "char", char_type_node);
5267 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5268 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5269 but not C. Are the conditionals here needed? */
5270 if (c_dialect_cxx ())
5271 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5272 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5273 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5274 record_builtin_type (RID_MAX, "long unsigned int",
5275 long_unsigned_type_node);
5276 if (int128_integer_type_node != NULL_TREE)
5278 record_builtin_type (RID_INT128, "__int128",
5279 int128_integer_type_node);
5280 record_builtin_type (RID_MAX, "__int128 unsigned",
5281 int128_unsigned_type_node);
5283 if (c_dialect_cxx ())
5284 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5285 record_builtin_type (RID_MAX, "long long int",
5286 long_long_integer_type_node);
5287 record_builtin_type (RID_MAX, "long long unsigned int",
5288 long_long_unsigned_type_node);
5289 if (c_dialect_cxx ())
5290 record_builtin_type (RID_MAX, "long long unsigned",
5291 long_long_unsigned_type_node);
5292 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5293 record_builtin_type (RID_MAX, "short unsigned int",
5294 short_unsigned_type_node);
5295 if (c_dialect_cxx ())
5296 record_builtin_type (RID_MAX, "unsigned short",
5297 short_unsigned_type_node);
5299 /* Define both `signed char' and `unsigned char'. */
5300 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5301 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5303 /* These are types that c_common_type_for_size and
5304 c_common_type_for_mode use. */
5305 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5306 TYPE_DECL, NULL_TREE,
5307 intQI_type_node));
5308 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5309 TYPE_DECL, NULL_TREE,
5310 intHI_type_node));
5311 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5312 TYPE_DECL, NULL_TREE,
5313 intSI_type_node));
5314 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5315 TYPE_DECL, NULL_TREE,
5316 intDI_type_node));
5317 #if HOST_BITS_PER_WIDE_INT >= 64
5318 if (targetm.scalar_mode_supported_p (TImode))
5319 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5320 TYPE_DECL,
5321 get_identifier ("__int128_t"),
5322 intTI_type_node));
5323 #endif
5324 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5325 TYPE_DECL, NULL_TREE,
5326 unsigned_intQI_type_node));
5327 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5328 TYPE_DECL, NULL_TREE,
5329 unsigned_intHI_type_node));
5330 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5331 TYPE_DECL, NULL_TREE,
5332 unsigned_intSI_type_node));
5333 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5334 TYPE_DECL, NULL_TREE,
5335 unsigned_intDI_type_node));
5336 #if HOST_BITS_PER_WIDE_INT >= 64
5337 if (targetm.scalar_mode_supported_p (TImode))
5338 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5339 TYPE_DECL,
5340 get_identifier ("__uint128_t"),
5341 unsigned_intTI_type_node));
5342 #endif
5344 /* Create the widest literal types. */
5345 widest_integer_literal_type_node
5346 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5347 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5348 TYPE_DECL, NULL_TREE,
5349 widest_integer_literal_type_node));
5351 widest_unsigned_literal_type_node
5352 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5353 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5354 TYPE_DECL, NULL_TREE,
5355 widest_unsigned_literal_type_node));
5357 signed_size_type_node = c_common_signed_type (size_type_node);
5359 pid_type_node =
5360 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5362 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5363 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5364 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5366 /* Only supported decimal floating point extension if the target
5367 actually supports underlying modes. */
5368 if (targetm.scalar_mode_supported_p (SDmode)
5369 && targetm.scalar_mode_supported_p (DDmode)
5370 && targetm.scalar_mode_supported_p (TDmode))
5372 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5373 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5374 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5377 if (targetm.fixed_point_supported_p ())
5379 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5380 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5381 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5382 record_builtin_type (RID_MAX, "long long _Fract",
5383 long_long_fract_type_node);
5384 record_builtin_type (RID_MAX, "unsigned short _Fract",
5385 unsigned_short_fract_type_node);
5386 record_builtin_type (RID_MAX, "unsigned _Fract",
5387 unsigned_fract_type_node);
5388 record_builtin_type (RID_MAX, "unsigned long _Fract",
5389 unsigned_long_fract_type_node);
5390 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5391 unsigned_long_long_fract_type_node);
5392 record_builtin_type (RID_MAX, "_Sat short _Fract",
5393 sat_short_fract_type_node);
5394 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5395 record_builtin_type (RID_MAX, "_Sat long _Fract",
5396 sat_long_fract_type_node);
5397 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5398 sat_long_long_fract_type_node);
5399 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5400 sat_unsigned_short_fract_type_node);
5401 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5402 sat_unsigned_fract_type_node);
5403 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5404 sat_unsigned_long_fract_type_node);
5405 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5406 sat_unsigned_long_long_fract_type_node);
5407 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5408 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5409 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5410 record_builtin_type (RID_MAX, "long long _Accum",
5411 long_long_accum_type_node);
5412 record_builtin_type (RID_MAX, "unsigned short _Accum",
5413 unsigned_short_accum_type_node);
5414 record_builtin_type (RID_MAX, "unsigned _Accum",
5415 unsigned_accum_type_node);
5416 record_builtin_type (RID_MAX, "unsigned long _Accum",
5417 unsigned_long_accum_type_node);
5418 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5419 unsigned_long_long_accum_type_node);
5420 record_builtin_type (RID_MAX, "_Sat short _Accum",
5421 sat_short_accum_type_node);
5422 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5423 record_builtin_type (RID_MAX, "_Sat long _Accum",
5424 sat_long_accum_type_node);
5425 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5426 sat_long_long_accum_type_node);
5427 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5428 sat_unsigned_short_accum_type_node);
5429 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5430 sat_unsigned_accum_type_node);
5431 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5432 sat_unsigned_long_accum_type_node);
5433 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5434 sat_unsigned_long_long_accum_type_node);
5438 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5439 TYPE_DECL,
5440 get_identifier ("complex int"),
5441 complex_integer_type_node));
5442 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5443 TYPE_DECL,
5444 get_identifier ("complex float"),
5445 complex_float_type_node));
5446 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5447 TYPE_DECL,
5448 get_identifier ("complex double"),
5449 complex_double_type_node));
5450 lang_hooks.decls.pushdecl
5451 (build_decl (UNKNOWN_LOCATION,
5452 TYPE_DECL, get_identifier ("complex long double"),
5453 complex_long_double_type_node));
5455 if (c_dialect_cxx ())
5456 /* For C++, make fileptr_type_node a distinct void * type until
5457 FILE type is defined. */
5458 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5460 record_builtin_type (RID_VOID, NULL, void_type_node);
5462 /* Set the TYPE_NAME for any variants that were built before
5463 record_builtin_type gave names to the built-in types. */
5465 tree void_name = TYPE_NAME (void_type_node);
5466 TYPE_NAME (void_type_node) = NULL_TREE;
5467 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5468 = void_name;
5469 TYPE_NAME (void_type_node) = void_name;
5472 /* This node must not be shared. */
5473 void_zero_node = make_node (INTEGER_CST);
5474 TREE_TYPE (void_zero_node) = void_type_node;
5476 void_list_node = build_void_list_node ();
5478 /* Make a type to be the domain of a few array types
5479 whose domains don't really matter.
5480 200 is small enough that it always fits in size_t
5481 and large enough that it can hold most function names for the
5482 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5483 array_domain_type = build_index_type (size_int (200));
5485 /* Make a type for arrays of characters.
5486 With luck nothing will ever really depend on the length of this
5487 array type. */
5488 char_array_type_node
5489 = build_array_type (char_type_node, array_domain_type);
5491 /* Likewise for arrays of ints. */
5492 int_array_type_node
5493 = build_array_type (integer_type_node, array_domain_type);
5495 string_type_node = build_pointer_type (char_type_node);
5496 const_string_type_node
5497 = build_pointer_type (build_qualified_type
5498 (char_type_node, TYPE_QUAL_CONST));
5500 /* This is special for C++ so functions can be overloaded. */
5501 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5502 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5503 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5504 underlying_wchar_type_node = wchar_type_node;
5505 if (c_dialect_cxx ())
5507 if (TYPE_UNSIGNED (wchar_type_node))
5508 wchar_type_node = make_unsigned_type (wchar_type_size);
5509 else
5510 wchar_type_node = make_signed_type (wchar_type_size);
5511 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5514 /* This is for wide string constants. */
5515 wchar_array_type_node
5516 = build_array_type (wchar_type_node, array_domain_type);
5518 /* Define 'char16_t'. */
5519 char16_type_node = get_identifier (CHAR16_TYPE);
5520 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5521 char16_type_size = TYPE_PRECISION (char16_type_node);
5522 if (c_dialect_cxx ())
5524 char16_type_node = make_unsigned_type (char16_type_size);
5526 if (cxx_dialect >= cxx11)
5527 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5530 /* This is for UTF-16 string constants. */
5531 char16_array_type_node
5532 = build_array_type (char16_type_node, array_domain_type);
5534 /* Define 'char32_t'. */
5535 char32_type_node = get_identifier (CHAR32_TYPE);
5536 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5537 char32_type_size = TYPE_PRECISION (char32_type_node);
5538 if (c_dialect_cxx ())
5540 char32_type_node = make_unsigned_type (char32_type_size);
5542 if (cxx_dialect >= cxx11)
5543 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5546 /* This is for UTF-32 string constants. */
5547 char32_array_type_node
5548 = build_array_type (char32_type_node, array_domain_type);
5550 wint_type_node =
5551 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5553 intmax_type_node =
5554 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5555 uintmax_type_node =
5556 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5558 if (SIG_ATOMIC_TYPE)
5559 sig_atomic_type_node =
5560 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5561 if (INT8_TYPE)
5562 int8_type_node =
5563 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5564 if (INT16_TYPE)
5565 int16_type_node =
5566 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5567 if (INT32_TYPE)
5568 int32_type_node =
5569 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5570 if (INT64_TYPE)
5571 int64_type_node =
5572 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5573 if (UINT8_TYPE)
5574 uint8_type_node =
5575 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5576 if (UINT16_TYPE)
5577 c_uint16_type_node =
5578 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5579 if (UINT32_TYPE)
5580 c_uint32_type_node =
5581 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5582 if (UINT64_TYPE)
5583 c_uint64_type_node =
5584 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5585 if (INT_LEAST8_TYPE)
5586 int_least8_type_node =
5587 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5588 if (INT_LEAST16_TYPE)
5589 int_least16_type_node =
5590 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5591 if (INT_LEAST32_TYPE)
5592 int_least32_type_node =
5593 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5594 if (INT_LEAST64_TYPE)
5595 int_least64_type_node =
5596 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5597 if (UINT_LEAST8_TYPE)
5598 uint_least8_type_node =
5599 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5600 if (UINT_LEAST16_TYPE)
5601 uint_least16_type_node =
5602 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5603 if (UINT_LEAST32_TYPE)
5604 uint_least32_type_node =
5605 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5606 if (UINT_LEAST64_TYPE)
5607 uint_least64_type_node =
5608 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5609 if (INT_FAST8_TYPE)
5610 int_fast8_type_node =
5611 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5612 if (INT_FAST16_TYPE)
5613 int_fast16_type_node =
5614 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5615 if (INT_FAST32_TYPE)
5616 int_fast32_type_node =
5617 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5618 if (INT_FAST64_TYPE)
5619 int_fast64_type_node =
5620 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5621 if (UINT_FAST8_TYPE)
5622 uint_fast8_type_node =
5623 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5624 if (UINT_FAST16_TYPE)
5625 uint_fast16_type_node =
5626 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5627 if (UINT_FAST32_TYPE)
5628 uint_fast32_type_node =
5629 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5630 if (UINT_FAST64_TYPE)
5631 uint_fast64_type_node =
5632 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5633 if (INTPTR_TYPE)
5634 intptr_type_node =
5635 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5636 if (UINTPTR_TYPE)
5637 uintptr_type_node =
5638 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5640 default_function_type
5641 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5642 ptrdiff_type_node
5643 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5644 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5646 lang_hooks.decls.pushdecl
5647 (build_decl (UNKNOWN_LOCATION,
5648 TYPE_DECL, get_identifier ("__builtin_va_list"),
5649 va_list_type_node));
5650 if (targetm.enum_va_list_p)
5652 int l;
5653 const char *pname;
5654 tree ptype;
5656 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5658 lang_hooks.decls.pushdecl
5659 (build_decl (UNKNOWN_LOCATION,
5660 TYPE_DECL, get_identifier (pname),
5661 ptype));
5666 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5668 va_list_arg_type_node = va_list_ref_type_node =
5669 build_pointer_type (TREE_TYPE (va_list_type_node));
5671 else
5673 va_list_arg_type_node = va_list_type_node;
5674 va_list_ref_type_node = build_reference_type (va_list_type_node);
5677 if (!flag_preprocess_only)
5678 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5680 main_identifier_node = get_identifier ("main");
5682 /* Create the built-in __null node. It is important that this is
5683 not shared. */
5684 null_node = make_node (INTEGER_CST);
5685 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5687 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5688 memset (builtin_types, 0, sizeof (builtin_types));
5691 /* The number of named compound-literals generated thus far. */
5692 static GTY(()) int compound_literal_number;
5694 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5696 void
5697 set_compound_literal_name (tree decl)
5699 char *name;
5700 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5701 compound_literal_number);
5702 compound_literal_number++;
5703 DECL_NAME (decl) = get_identifier (name);
5706 tree
5707 build_va_arg (location_t loc, tree expr, tree type)
5709 expr = build1 (VA_ARG_EXPR, type, expr);
5710 SET_EXPR_LOCATION (expr, loc);
5711 return expr;
5715 /* Linked list of disabled built-in functions. */
5717 typedef struct disabled_builtin
5719 const char *name;
5720 struct disabled_builtin *next;
5721 } disabled_builtin;
5722 static disabled_builtin *disabled_builtins = NULL;
5724 static bool builtin_function_disabled_p (const char *);
5726 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5727 begins with "__builtin_", give an error. */
5729 void
5730 disable_builtin_function (const char *name)
5732 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5733 error ("cannot disable built-in function %qs", name);
5734 else
5736 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5737 new_disabled_builtin->name = name;
5738 new_disabled_builtin->next = disabled_builtins;
5739 disabled_builtins = new_disabled_builtin;
5744 /* Return true if the built-in function NAME has been disabled, false
5745 otherwise. */
5747 static bool
5748 builtin_function_disabled_p (const char *name)
5750 disabled_builtin *p;
5751 for (p = disabled_builtins; p != NULL; p = p->next)
5753 if (strcmp (name, p->name) == 0)
5754 return true;
5756 return false;
5760 /* Worker for DEF_BUILTIN.
5761 Possibly define a builtin function with one or two names.
5762 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5763 nonansi_p and flag_no_nonansi_builtin. */
5765 static void
5766 def_builtin_1 (enum built_in_function fncode,
5767 const char *name,
5768 enum built_in_class fnclass,
5769 tree fntype, tree libtype,
5770 bool both_p, bool fallback_p, bool nonansi_p,
5771 tree fnattrs, bool implicit_p)
5773 tree decl;
5774 const char *libname;
5776 if (fntype == error_mark_node)
5777 return;
5779 gcc_assert ((!both_p && !fallback_p)
5780 || !strncmp (name, "__builtin_",
5781 strlen ("__builtin_")));
5783 libname = name + strlen ("__builtin_");
5784 decl = add_builtin_function (name, fntype, fncode, fnclass,
5785 (fallback_p ? libname : NULL),
5786 fnattrs);
5788 set_builtin_decl (fncode, decl, implicit_p);
5790 if (both_p
5791 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5792 && !(nonansi_p && flag_no_nonansi_builtin))
5793 add_builtin_function (libname, libtype, fncode, fnclass,
5794 NULL, fnattrs);
5797 /* Nonzero if the type T promotes to int. This is (nearly) the
5798 integral promotions defined in ISO C99 6.3.1.1/2. */
5800 bool
5801 c_promoting_integer_type_p (const_tree t)
5803 switch (TREE_CODE (t))
5805 case INTEGER_TYPE:
5806 return (TYPE_MAIN_VARIANT (t) == char_type_node
5807 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5808 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5809 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5810 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5811 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5813 case ENUMERAL_TYPE:
5814 /* ??? Technically all enumerations not larger than an int
5815 promote to an int. But this is used along code paths
5816 that only want to notice a size change. */
5817 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5819 case BOOLEAN_TYPE:
5820 return 1;
5822 default:
5823 return 0;
5827 /* Return 1 if PARMS specifies a fixed number of parameters
5828 and none of their types is affected by default promotions. */
5831 self_promoting_args_p (const_tree parms)
5833 const_tree t;
5834 for (t = parms; t; t = TREE_CHAIN (t))
5836 tree type = TREE_VALUE (t);
5838 if (type == error_mark_node)
5839 continue;
5841 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5842 return 0;
5844 if (type == 0)
5845 return 0;
5847 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5848 return 0;
5850 if (c_promoting_integer_type_p (type))
5851 return 0;
5853 return 1;
5856 /* Recursively remove any '*' or '&' operator from TYPE. */
5857 tree
5858 strip_pointer_operator (tree t)
5860 while (POINTER_TYPE_P (t))
5861 t = TREE_TYPE (t);
5862 return t;
5865 /* Recursively remove pointer or array type from TYPE. */
5866 tree
5867 strip_pointer_or_array_types (tree t)
5869 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5870 t = TREE_TYPE (t);
5871 return t;
5874 /* Used to compare case labels. K1 and K2 are actually tree nodes
5875 representing case labels, or NULL_TREE for a `default' label.
5876 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5877 K2, and 0 if K1 and K2 are equal. */
5880 case_compare (splay_tree_key k1, splay_tree_key k2)
5882 /* Consider a NULL key (such as arises with a `default' label) to be
5883 smaller than anything else. */
5884 if (!k1)
5885 return k2 ? -1 : 0;
5886 else if (!k2)
5887 return k1 ? 1 : 0;
5889 return tree_int_cst_compare ((tree) k1, (tree) k2);
5892 /* Process a case label, located at LOC, for the range LOW_VALUE
5893 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5894 then this case label is actually a `default' label. If only
5895 HIGH_VALUE is NULL_TREE, then case label was declared using the
5896 usual C/C++ syntax, rather than the GNU case range extension.
5897 CASES is a tree containing all the case ranges processed so far;
5898 COND is the condition for the switch-statement itself. Returns the
5899 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5900 is created. */
5902 tree
5903 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5904 tree low_value, tree high_value)
5906 tree type;
5907 tree label;
5908 tree case_label;
5909 splay_tree_node node;
5911 /* Create the LABEL_DECL itself. */
5912 label = create_artificial_label (loc);
5914 /* If there was an error processing the switch condition, bail now
5915 before we get more confused. */
5916 if (!cond || cond == error_mark_node)
5917 goto error_out;
5919 if ((low_value && TREE_TYPE (low_value)
5920 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5921 || (high_value && TREE_TYPE (high_value)
5922 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5924 error_at (loc, "pointers are not permitted as case values");
5925 goto error_out;
5928 /* Case ranges are a GNU extension. */
5929 if (high_value)
5930 pedwarn (loc, OPT_Wpedantic,
5931 "range expressions in switch statements are non-standard");
5933 type = TREE_TYPE (cond);
5934 if (low_value)
5936 low_value = check_case_value (low_value);
5937 low_value = convert_and_check (type, low_value);
5938 if (low_value == error_mark_node)
5939 goto error_out;
5941 if (high_value)
5943 high_value = check_case_value (high_value);
5944 high_value = convert_and_check (type, high_value);
5945 if (high_value == error_mark_node)
5946 goto error_out;
5949 if (low_value && high_value)
5951 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5952 really a case range, even though it was written that way.
5953 Remove the HIGH_VALUE to simplify later processing. */
5954 if (tree_int_cst_equal (low_value, high_value))
5955 high_value = NULL_TREE;
5956 else if (!tree_int_cst_lt (low_value, high_value))
5957 warning_at (loc, 0, "empty range specified");
5960 /* See if the case is in range of the type of the original testing
5961 expression. If both low_value and high_value are out of range,
5962 don't insert the case label and return NULL_TREE. */
5963 if (low_value
5964 && !check_case_bounds (type, orig_type,
5965 &low_value, high_value ? &high_value : NULL))
5966 return NULL_TREE;
5968 /* Look up the LOW_VALUE in the table of case labels we already
5969 have. */
5970 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5971 /* If there was not an exact match, check for overlapping ranges.
5972 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5973 that's a `default' label and the only overlap is an exact match. */
5974 if (!node && (low_value || high_value))
5976 splay_tree_node low_bound;
5977 splay_tree_node high_bound;
5979 /* Even though there wasn't an exact match, there might be an
5980 overlap between this case range and another case range.
5981 Since we've (inductively) not allowed any overlapping case
5982 ranges, we simply need to find the greatest low case label
5983 that is smaller that LOW_VALUE, and the smallest low case
5984 label that is greater than LOW_VALUE. If there is an overlap
5985 it will occur in one of these two ranges. */
5986 low_bound = splay_tree_predecessor (cases,
5987 (splay_tree_key) low_value);
5988 high_bound = splay_tree_successor (cases,
5989 (splay_tree_key) low_value);
5991 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5992 the LOW_VALUE, so there is no need to check unless the
5993 LOW_BOUND is in fact itself a case range. */
5994 if (low_bound
5995 && CASE_HIGH ((tree) low_bound->value)
5996 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5997 low_value) >= 0)
5998 node = low_bound;
5999 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6000 range is bigger than the low end of the current range, so we
6001 are only interested if the current range is a real range, and
6002 not an ordinary case label. */
6003 else if (high_bound
6004 && high_value
6005 && (tree_int_cst_compare ((tree) high_bound->key,
6006 high_value)
6007 <= 0))
6008 node = high_bound;
6010 /* If there was an overlap, issue an error. */
6011 if (node)
6013 tree duplicate = CASE_LABEL ((tree) node->value);
6015 if (high_value)
6017 error_at (loc, "duplicate (or overlapping) case value");
6018 error_at (DECL_SOURCE_LOCATION (duplicate),
6019 "this is the first entry overlapping that value");
6021 else if (low_value)
6023 error_at (loc, "duplicate case value") ;
6024 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6026 else
6028 error_at (loc, "multiple default labels in one switch");
6029 error_at (DECL_SOURCE_LOCATION (duplicate),
6030 "this is the first default label");
6032 goto error_out;
6035 /* Add a CASE_LABEL to the statement-tree. */
6036 case_label = add_stmt (build_case_label (low_value, high_value, label));
6037 /* Register this case label in the splay tree. */
6038 splay_tree_insert (cases,
6039 (splay_tree_key) low_value,
6040 (splay_tree_value) case_label);
6042 return case_label;
6044 error_out:
6045 /* Add a label so that the back-end doesn't think that the beginning of
6046 the switch is unreachable. Note that we do not add a case label, as
6047 that just leads to duplicates and thence to failure later on. */
6048 if (!cases->root)
6050 tree t = create_artificial_label (loc);
6051 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6053 return error_mark_node;
6056 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6057 Used to verify that case values match up with enumerator values. */
6059 static void
6060 match_case_to_enum_1 (tree key, tree type, tree label)
6062 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6064 /* ??? Not working too hard to print the double-word value.
6065 Should perhaps be done with %lwd in the diagnostic routines? */
6066 if (TREE_INT_CST_HIGH (key) == 0)
6067 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6068 TREE_INT_CST_LOW (key));
6069 else if (!TYPE_UNSIGNED (type)
6070 && TREE_INT_CST_HIGH (key) == -1
6071 && TREE_INT_CST_LOW (key) != 0)
6072 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6073 -TREE_INT_CST_LOW (key));
6074 else
6075 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6076 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6077 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6079 if (TYPE_NAME (type) == 0)
6080 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6081 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6082 "case value %qs not in enumerated type",
6083 buf);
6084 else
6085 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6086 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6087 "case value %qs not in enumerated type %qT",
6088 buf, type);
6091 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6092 Used to verify that case values match up with enumerator values. */
6094 static int
6095 match_case_to_enum (splay_tree_node node, void *data)
6097 tree label = (tree) node->value;
6098 tree type = (tree) data;
6100 /* Skip default case. */
6101 if (!CASE_LOW (label))
6102 return 0;
6104 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6105 when we did our enum->case scan. Reset our scratch bit after. */
6106 if (!CASE_LOW_SEEN (label))
6107 match_case_to_enum_1 (CASE_LOW (label), type, label);
6108 else
6109 CASE_LOW_SEEN (label) = 0;
6111 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6112 not set, that means that CASE_HIGH did not appear when we did our
6113 enum->case scan. Reset our scratch bit after. */
6114 if (CASE_HIGH (label))
6116 if (!CASE_HIGH_SEEN (label))
6117 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6118 else
6119 CASE_HIGH_SEEN (label) = 0;
6122 return 0;
6125 /* Handle -Wswitch*. Called from the front end after parsing the
6126 switch construct. */
6127 /* ??? Should probably be somewhere generic, since other languages
6128 besides C and C++ would want this. At the moment, however, C/C++
6129 are the only tree-ssa languages that support enumerations at all,
6130 so the point is moot. */
6132 void
6133 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6134 tree type, tree cond)
6136 splay_tree_node default_node;
6137 splay_tree_node node;
6138 tree chain;
6140 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6141 return;
6143 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6144 if (!default_node)
6145 warning_at (switch_location, OPT_Wswitch_default,
6146 "switch missing default case");
6148 /* From here on, we only care about about enumerated types. */
6149 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6150 return;
6152 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6153 if (!warn_switch_enum && !warn_switch)
6154 return;
6156 /* Check the cases. Warn about case values which are not members of
6157 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6158 there is no default case, check that exactly all enumeration
6159 literals are covered by the cases. */
6161 /* Clearing COND if it is not an integer constant simplifies
6162 the tests inside the loop below. */
6163 if (TREE_CODE (cond) != INTEGER_CST)
6164 cond = NULL_TREE;
6166 /* The time complexity here is O(N*lg(N)) worst case, but for the
6167 common case of monotonically increasing enumerators, it is
6168 O(N), since the nature of the splay tree will keep the next
6169 element adjacent to the root at all times. */
6171 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6173 tree value = TREE_VALUE (chain);
6174 if (TREE_CODE (value) == CONST_DECL)
6175 value = DECL_INITIAL (value);
6176 node = splay_tree_lookup (cases, (splay_tree_key) value);
6177 if (node)
6179 /* Mark the CASE_LOW part of the case entry as seen. */
6180 tree label = (tree) node->value;
6181 CASE_LOW_SEEN (label) = 1;
6182 continue;
6185 /* Even though there wasn't an exact match, there might be a
6186 case range which includes the enumerator's value. */
6187 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6188 if (node && CASE_HIGH ((tree) node->value))
6190 tree label = (tree) node->value;
6191 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6192 if (cmp >= 0)
6194 /* If we match the upper bound exactly, mark the CASE_HIGH
6195 part of the case entry as seen. */
6196 if (cmp == 0)
6197 CASE_HIGH_SEEN (label) = 1;
6198 continue;
6202 /* We've now determined that this enumerated literal isn't
6203 handled by the case labels of the switch statement. */
6205 /* If the switch expression is a constant, we only really care
6206 about whether that constant is handled by the switch. */
6207 if (cond && tree_int_cst_compare (cond, value))
6208 continue;
6210 /* If there is a default_node, the only relevant option is
6211 Wswitch-enum. Otherwise, if both are enabled then we prefer
6212 to warn using -Wswitch because -Wswitch is enabled by -Wall
6213 while -Wswitch-enum is explicit. */
6214 warning_at (switch_location,
6215 (default_node || !warn_switch
6216 ? OPT_Wswitch_enum
6217 : OPT_Wswitch),
6218 "enumeration value %qE not handled in switch",
6219 TREE_PURPOSE (chain));
6222 /* Warn if there are case expressions that don't correspond to
6223 enumerators. This can occur since C and C++ don't enforce
6224 type-checking of assignments to enumeration variables.
6226 The time complexity here is now always O(N) worst case, since
6227 we should have marked both the lower bound and upper bound of
6228 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6229 above. This scan also resets those fields. */
6231 splay_tree_foreach (cases, match_case_to_enum, type);
6234 /* Finish an expression taking the address of LABEL (an
6235 IDENTIFIER_NODE). Returns an expression for the address.
6237 LOC is the location for the expression returned. */
6239 tree
6240 finish_label_address_expr (tree label, location_t loc)
6242 tree result;
6244 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6246 if (label == error_mark_node)
6247 return error_mark_node;
6249 label = lookup_label (label);
6250 if (label == NULL_TREE)
6251 result = null_pointer_node;
6252 else
6254 TREE_USED (label) = 1;
6255 result = build1 (ADDR_EXPR, ptr_type_node, label);
6256 /* The current function is not necessarily uninlinable.
6257 Computed gotos are incompatible with inlining, but the value
6258 here could be used only in a diagnostic, for example. */
6259 protected_set_expr_location (result, loc);
6262 return result;
6266 /* Given a boolean expression ARG, return a tree representing an increment
6267 or decrement (as indicated by CODE) of ARG. The front end must check for
6268 invalid cases (e.g., decrement in C++). */
6269 tree
6270 boolean_increment (enum tree_code code, tree arg)
6272 tree val;
6273 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6275 arg = stabilize_reference (arg);
6276 switch (code)
6278 case PREINCREMENT_EXPR:
6279 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6280 break;
6281 case POSTINCREMENT_EXPR:
6282 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6283 arg = save_expr (arg);
6284 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6285 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6286 break;
6287 case PREDECREMENT_EXPR:
6288 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6289 invert_truthvalue_loc (input_location, arg));
6290 break;
6291 case POSTDECREMENT_EXPR:
6292 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6293 invert_truthvalue_loc (input_location, arg));
6294 arg = save_expr (arg);
6295 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6296 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6297 break;
6298 default:
6299 gcc_unreachable ();
6301 TREE_SIDE_EFFECTS (val) = 1;
6302 return val;
6305 /* Built-in macros for stddef.h and stdint.h, that require macros
6306 defined in this file. */
6307 void
6308 c_stddef_cpp_builtins(void)
6310 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6311 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6312 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6313 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6314 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6315 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6316 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6317 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6318 if (SIG_ATOMIC_TYPE)
6319 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6320 if (INT8_TYPE)
6321 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6322 if (INT16_TYPE)
6323 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6324 if (INT32_TYPE)
6325 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6326 if (INT64_TYPE)
6327 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6328 if (UINT8_TYPE)
6329 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6330 if (UINT16_TYPE)
6331 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6332 if (UINT32_TYPE)
6333 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6334 if (UINT64_TYPE)
6335 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6336 if (INT_LEAST8_TYPE)
6337 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6338 if (INT_LEAST16_TYPE)
6339 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6340 if (INT_LEAST32_TYPE)
6341 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6342 if (INT_LEAST64_TYPE)
6343 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6344 if (UINT_LEAST8_TYPE)
6345 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6346 if (UINT_LEAST16_TYPE)
6347 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6348 if (UINT_LEAST32_TYPE)
6349 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6350 if (UINT_LEAST64_TYPE)
6351 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6352 if (INT_FAST8_TYPE)
6353 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6354 if (INT_FAST16_TYPE)
6355 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6356 if (INT_FAST32_TYPE)
6357 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6358 if (INT_FAST64_TYPE)
6359 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6360 if (UINT_FAST8_TYPE)
6361 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6362 if (UINT_FAST16_TYPE)
6363 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6364 if (UINT_FAST32_TYPE)
6365 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6366 if (UINT_FAST64_TYPE)
6367 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6368 if (INTPTR_TYPE)
6369 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6370 if (UINTPTR_TYPE)
6371 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6374 static void
6375 c_init_attributes (void)
6377 /* Fill in the built_in_attributes array. */
6378 #define DEF_ATTR_NULL_TREE(ENUM) \
6379 built_in_attributes[(int) ENUM] = NULL_TREE;
6380 #define DEF_ATTR_INT(ENUM, VALUE) \
6381 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6382 #define DEF_ATTR_STRING(ENUM, VALUE) \
6383 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6384 #define DEF_ATTR_IDENT(ENUM, STRING) \
6385 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6386 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6387 built_in_attributes[(int) ENUM] \
6388 = tree_cons (built_in_attributes[(int) PURPOSE], \
6389 built_in_attributes[(int) VALUE], \
6390 built_in_attributes[(int) CHAIN]);
6391 #include "builtin-attrs.def"
6392 #undef DEF_ATTR_NULL_TREE
6393 #undef DEF_ATTR_INT
6394 #undef DEF_ATTR_IDENT
6395 #undef DEF_ATTR_TREE_LIST
6398 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6399 identifier as an argument, so the front end shouldn't look it up. */
6401 bool
6402 attribute_takes_identifier_p (const_tree attr_id)
6404 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6405 if (spec == NULL)
6406 /* Unknown attribute that we'll end up ignoring, return true so we
6407 don't complain about an identifier argument. */
6408 return true;
6409 else if (!strcmp ("mode", spec->name)
6410 || !strcmp ("format", spec->name)
6411 || !strcmp ("cleanup", spec->name))
6412 return true;
6413 else
6414 return targetm.attribute_takes_identifier_p (attr_id);
6417 /* Attribute handlers common to C front ends. */
6419 /* Handle a "packed" attribute; arguments as in
6420 struct attribute_spec.handler. */
6422 static tree
6423 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6424 int flags, bool *no_add_attrs)
6426 if (TYPE_P (*node))
6428 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6429 *node = build_variant_type_copy (*node);
6430 TYPE_PACKED (*node) = 1;
6432 else if (TREE_CODE (*node) == FIELD_DECL)
6434 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6435 /* Still pack bitfields. */
6436 && ! DECL_INITIAL (*node))
6437 warning (OPT_Wattributes,
6438 "%qE attribute ignored for field of type %qT",
6439 name, TREE_TYPE (*node));
6440 else
6441 DECL_PACKED (*node) = 1;
6443 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6444 used for DECL_REGISTER. It wouldn't mean anything anyway.
6445 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6446 that changes what the typedef is typing. */
6447 else
6449 warning (OPT_Wattributes, "%qE attribute ignored", name);
6450 *no_add_attrs = true;
6453 return NULL_TREE;
6456 /* Handle a "nocommon" attribute; arguments as in
6457 struct attribute_spec.handler. */
6459 static tree
6460 handle_nocommon_attribute (tree *node, tree name,
6461 tree ARG_UNUSED (args),
6462 int ARG_UNUSED (flags), bool *no_add_attrs)
6464 if (TREE_CODE (*node) == VAR_DECL)
6465 DECL_COMMON (*node) = 0;
6466 else
6468 warning (OPT_Wattributes, "%qE attribute ignored", name);
6469 *no_add_attrs = true;
6472 return NULL_TREE;
6475 /* Handle a "common" attribute; arguments as in
6476 struct attribute_spec.handler. */
6478 static tree
6479 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6480 int ARG_UNUSED (flags), bool *no_add_attrs)
6482 if (TREE_CODE (*node) == VAR_DECL)
6483 DECL_COMMON (*node) = 1;
6484 else
6486 warning (OPT_Wattributes, "%qE attribute ignored", name);
6487 *no_add_attrs = true;
6490 return NULL_TREE;
6493 /* Handle a "noreturn" attribute; arguments as in
6494 struct attribute_spec.handler. */
6496 static tree
6497 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6498 int ARG_UNUSED (flags), bool *no_add_attrs)
6500 tree type = TREE_TYPE (*node);
6502 /* See FIXME comment in c_common_attribute_table. */
6503 if (TREE_CODE (*node) == FUNCTION_DECL
6504 || objc_method_decl (TREE_CODE (*node)))
6505 TREE_THIS_VOLATILE (*node) = 1;
6506 else if (TREE_CODE (type) == POINTER_TYPE
6507 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6508 TREE_TYPE (*node)
6509 = build_pointer_type
6510 (build_type_variant (TREE_TYPE (type),
6511 TYPE_READONLY (TREE_TYPE (type)), 1));
6512 else
6514 warning (OPT_Wattributes, "%qE attribute ignored", name);
6515 *no_add_attrs = true;
6518 return NULL_TREE;
6521 /* Handle a "hot" and attribute; arguments as in
6522 struct attribute_spec.handler. */
6524 static tree
6525 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6526 int ARG_UNUSED (flags), bool *no_add_attrs)
6528 if (TREE_CODE (*node) == FUNCTION_DECL
6529 || TREE_CODE (*node) == LABEL_DECL)
6531 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6533 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6534 name, "cold");
6535 *no_add_attrs = true;
6537 /* Most of the rest of the hot processing is done later with
6538 lookup_attribute. */
6540 else
6542 warning (OPT_Wattributes, "%qE attribute ignored", name);
6543 *no_add_attrs = true;
6546 return NULL_TREE;
6549 /* Handle a "cold" and attribute; arguments as in
6550 struct attribute_spec.handler. */
6552 static tree
6553 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6554 int ARG_UNUSED (flags), bool *no_add_attrs)
6556 if (TREE_CODE (*node) == FUNCTION_DECL
6557 || TREE_CODE (*node) == LABEL_DECL)
6559 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6561 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6562 name, "hot");
6563 *no_add_attrs = true;
6565 /* Most of the rest of the cold processing is done later with
6566 lookup_attribute. */
6568 else
6570 warning (OPT_Wattributes, "%qE attribute ignored", name);
6571 *no_add_attrs = true;
6574 return NULL_TREE;
6577 /* Handle a "no_sanitize_address" attribute; arguments as in
6578 struct attribute_spec.handler. */
6580 static tree
6581 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6582 bool *no_add_attrs)
6584 if (TREE_CODE (*node) != FUNCTION_DECL)
6586 warning (OPT_Wattributes, "%qE attribute ignored", name);
6587 *no_add_attrs = true;
6590 return NULL_TREE;
6593 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6594 struct attribute_spec.handler. */
6596 static tree
6597 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6598 bool *no_add_attrs)
6600 if (TREE_CODE (*node) != FUNCTION_DECL)
6601 warning (OPT_Wattributes, "%qE attribute ignored", name);
6602 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6603 DECL_ATTRIBUTES (*node)
6604 = tree_cons (get_identifier ("no_sanitize_address"),
6605 NULL_TREE, DECL_ATTRIBUTES (*node));
6606 *no_add_attrs = true;
6607 return NULL_TREE;
6610 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6611 struct attribute_spec.handler. */
6613 static tree
6614 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6615 bool *no_add_attrs)
6617 if (TREE_CODE (*node) != FUNCTION_DECL)
6619 warning (OPT_Wattributes, "%qE attribute ignored", name);
6620 *no_add_attrs = true;
6623 return NULL_TREE;
6626 /* Handle a "noinline" attribute; arguments as in
6627 struct attribute_spec.handler. */
6629 static tree
6630 handle_noinline_attribute (tree *node, tree name,
6631 tree ARG_UNUSED (args),
6632 int ARG_UNUSED (flags), bool *no_add_attrs)
6634 if (TREE_CODE (*node) == FUNCTION_DECL)
6635 DECL_UNINLINABLE (*node) = 1;
6636 else
6638 warning (OPT_Wattributes, "%qE attribute ignored", name);
6639 *no_add_attrs = true;
6642 return NULL_TREE;
6645 /* Handle a "noclone" attribute; arguments as in
6646 struct attribute_spec.handler. */
6648 static tree
6649 handle_noclone_attribute (tree *node, tree name,
6650 tree ARG_UNUSED (args),
6651 int ARG_UNUSED (flags), bool *no_add_attrs)
6653 if (TREE_CODE (*node) != FUNCTION_DECL)
6655 warning (OPT_Wattributes, "%qE attribute ignored", name);
6656 *no_add_attrs = true;
6659 return NULL_TREE;
6662 /* Handle a "always_inline" attribute; arguments as in
6663 struct attribute_spec.handler. */
6665 static tree
6666 handle_always_inline_attribute (tree *node, tree name,
6667 tree ARG_UNUSED (args),
6668 int ARG_UNUSED (flags),
6669 bool *no_add_attrs)
6671 if (TREE_CODE (*node) == FUNCTION_DECL)
6673 /* Set the attribute and mark it for disregarding inline
6674 limits. */
6675 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6677 else
6679 warning (OPT_Wattributes, "%qE attribute ignored", name);
6680 *no_add_attrs = true;
6683 return NULL_TREE;
6686 /* Handle a "gnu_inline" attribute; arguments as in
6687 struct attribute_spec.handler. */
6689 static tree
6690 handle_gnu_inline_attribute (tree *node, tree name,
6691 tree ARG_UNUSED (args),
6692 int ARG_UNUSED (flags),
6693 bool *no_add_attrs)
6695 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6697 /* Do nothing else, just set the attribute. We'll get at
6698 it later with lookup_attribute. */
6700 else
6702 warning (OPT_Wattributes, "%qE attribute ignored", name);
6703 *no_add_attrs = true;
6706 return NULL_TREE;
6709 /* Handle a "leaf" attribute; arguments as in
6710 struct attribute_spec.handler. */
6712 static tree
6713 handle_leaf_attribute (tree *node, tree name,
6714 tree ARG_UNUSED (args),
6715 int ARG_UNUSED (flags), bool *no_add_attrs)
6717 if (TREE_CODE (*node) != FUNCTION_DECL)
6719 warning (OPT_Wattributes, "%qE attribute ignored", name);
6720 *no_add_attrs = true;
6722 if (!TREE_PUBLIC (*node))
6724 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6725 *no_add_attrs = true;
6728 return NULL_TREE;
6731 /* Handle an "artificial" attribute; arguments as in
6732 struct attribute_spec.handler. */
6734 static tree
6735 handle_artificial_attribute (tree *node, tree name,
6736 tree ARG_UNUSED (args),
6737 int ARG_UNUSED (flags),
6738 bool *no_add_attrs)
6740 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6742 /* Do nothing else, just set the attribute. We'll get at
6743 it later with lookup_attribute. */
6745 else
6747 warning (OPT_Wattributes, "%qE attribute ignored", name);
6748 *no_add_attrs = true;
6751 return NULL_TREE;
6754 /* Handle a "flatten" attribute; arguments as in
6755 struct attribute_spec.handler. */
6757 static tree
6758 handle_flatten_attribute (tree *node, tree name,
6759 tree args ATTRIBUTE_UNUSED,
6760 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6762 if (TREE_CODE (*node) == FUNCTION_DECL)
6763 /* Do nothing else, just set the attribute. We'll get at
6764 it later with lookup_attribute. */
6766 else
6768 warning (OPT_Wattributes, "%qE attribute ignored", name);
6769 *no_add_attrs = true;
6772 return NULL_TREE;
6775 /* Handle a "warning" or "error" attribute; arguments as in
6776 struct attribute_spec.handler. */
6778 static tree
6779 handle_error_attribute (tree *node, tree name, tree args,
6780 int ARG_UNUSED (flags), bool *no_add_attrs)
6782 if (TREE_CODE (*node) == FUNCTION_DECL
6783 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6784 /* Do nothing else, just set the attribute. We'll get at
6785 it later with lookup_attribute. */
6787 else
6789 warning (OPT_Wattributes, "%qE attribute ignored", name);
6790 *no_add_attrs = true;
6793 return NULL_TREE;
6796 /* Handle a "used" attribute; arguments as in
6797 struct attribute_spec.handler. */
6799 static tree
6800 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6801 int ARG_UNUSED (flags), bool *no_add_attrs)
6803 tree node = *pnode;
6805 if (TREE_CODE (node) == FUNCTION_DECL
6806 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6807 || (TREE_CODE (node) == TYPE_DECL))
6809 TREE_USED (node) = 1;
6810 DECL_PRESERVE_P (node) = 1;
6811 if (TREE_CODE (node) == VAR_DECL)
6812 DECL_READ_P (node) = 1;
6814 else
6816 warning (OPT_Wattributes, "%qE attribute ignored", name);
6817 *no_add_attrs = true;
6820 return NULL_TREE;
6823 /* Handle a "unused" attribute; arguments as in
6824 struct attribute_spec.handler. */
6826 static tree
6827 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6828 int flags, bool *no_add_attrs)
6830 if (DECL_P (*node))
6832 tree decl = *node;
6834 if (TREE_CODE (decl) == PARM_DECL
6835 || TREE_CODE (decl) == VAR_DECL
6836 || TREE_CODE (decl) == FUNCTION_DECL
6837 || TREE_CODE (decl) == LABEL_DECL
6838 || TREE_CODE (decl) == TYPE_DECL)
6840 TREE_USED (decl) = 1;
6841 if (TREE_CODE (decl) == VAR_DECL
6842 || TREE_CODE (decl) == PARM_DECL)
6843 DECL_READ_P (decl) = 1;
6845 else
6847 warning (OPT_Wattributes, "%qE attribute ignored", name);
6848 *no_add_attrs = true;
6851 else
6853 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6854 *node = build_variant_type_copy (*node);
6855 TREE_USED (*node) = 1;
6858 return NULL_TREE;
6861 /* Handle a "externally_visible" attribute; arguments as in
6862 struct attribute_spec.handler. */
6864 static tree
6865 handle_externally_visible_attribute (tree *pnode, tree name,
6866 tree ARG_UNUSED (args),
6867 int ARG_UNUSED (flags),
6868 bool *no_add_attrs)
6870 tree node = *pnode;
6872 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6874 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6875 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6877 warning (OPT_Wattributes,
6878 "%qE attribute have effect only on public objects", name);
6879 *no_add_attrs = true;
6882 else
6884 warning (OPT_Wattributes, "%qE attribute ignored", name);
6885 *no_add_attrs = true;
6888 return NULL_TREE;
6891 /* Handle a "const" attribute; arguments as in
6892 struct attribute_spec.handler. */
6894 static tree
6895 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6896 int ARG_UNUSED (flags), bool *no_add_attrs)
6898 tree type = TREE_TYPE (*node);
6900 /* See FIXME comment on noreturn in c_common_attribute_table. */
6901 if (TREE_CODE (*node) == FUNCTION_DECL)
6902 TREE_READONLY (*node) = 1;
6903 else if (TREE_CODE (type) == POINTER_TYPE
6904 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6905 TREE_TYPE (*node)
6906 = build_pointer_type
6907 (build_type_variant (TREE_TYPE (type), 1,
6908 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6909 else
6911 warning (OPT_Wattributes, "%qE attribute ignored", name);
6912 *no_add_attrs = true;
6915 return NULL_TREE;
6918 /* Handle a "transparent_union" attribute; arguments as in
6919 struct attribute_spec.handler. */
6921 static tree
6922 handle_transparent_union_attribute (tree *node, tree name,
6923 tree ARG_UNUSED (args), int flags,
6924 bool *no_add_attrs)
6926 tree type;
6928 *no_add_attrs = true;
6931 if (TREE_CODE (*node) == TYPE_DECL
6932 && ! (flags & ATTR_FLAG_CXX11))
6933 node = &TREE_TYPE (*node);
6934 type = *node;
6936 if (TREE_CODE (type) == UNION_TYPE)
6938 /* Make sure that the first field will work for a transparent union.
6939 If the type isn't complete yet, leave the check to the code in
6940 finish_struct. */
6941 if (TYPE_SIZE (type))
6943 tree first = first_field (type);
6944 if (first == NULL_TREE
6945 || DECL_ARTIFICIAL (first)
6946 || TYPE_MODE (type) != DECL_MODE (first))
6947 goto ignored;
6950 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6952 /* If the type isn't complete yet, setting the flag
6953 on a variant wouldn't ever be checked. */
6954 if (!TYPE_SIZE (type))
6955 goto ignored;
6957 /* build_duplicate_type doesn't work for C++. */
6958 if (c_dialect_cxx ())
6959 goto ignored;
6961 /* A type variant isn't good enough, since we don't a cast
6962 to such a type removed as a no-op. */
6963 *node = type = build_duplicate_type (type);
6966 TYPE_TRANSPARENT_AGGR (type) = 1;
6967 return NULL_TREE;
6970 ignored:
6971 warning (OPT_Wattributes, "%qE attribute ignored", name);
6972 return NULL_TREE;
6975 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6976 get the requested priority for a constructor or destructor,
6977 possibly issuing diagnostics for invalid or reserved
6978 priorities. */
6980 static priority_type
6981 get_priority (tree args, bool is_destructor)
6983 HOST_WIDE_INT pri;
6984 tree arg;
6986 if (!args)
6987 return DEFAULT_INIT_PRIORITY;
6989 if (!SUPPORTS_INIT_PRIORITY)
6991 if (is_destructor)
6992 error ("destructor priorities are not supported");
6993 else
6994 error ("constructor priorities are not supported");
6995 return DEFAULT_INIT_PRIORITY;
6998 arg = TREE_VALUE (args);
6999 arg = default_conversion (arg);
7000 if (!host_integerp (arg, /*pos=*/0)
7001 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7002 goto invalid;
7004 pri = tree_low_cst (arg, /*pos=*/0);
7005 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7006 goto invalid;
7008 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7010 if (is_destructor)
7011 warning (0,
7012 "destructor priorities from 0 to %d are reserved "
7013 "for the implementation",
7014 MAX_RESERVED_INIT_PRIORITY);
7015 else
7016 warning (0,
7017 "constructor priorities from 0 to %d are reserved "
7018 "for the implementation",
7019 MAX_RESERVED_INIT_PRIORITY);
7021 return pri;
7023 invalid:
7024 if (is_destructor)
7025 error ("destructor priorities must be integers from 0 to %d inclusive",
7026 MAX_INIT_PRIORITY);
7027 else
7028 error ("constructor priorities must be integers from 0 to %d inclusive",
7029 MAX_INIT_PRIORITY);
7030 return DEFAULT_INIT_PRIORITY;
7033 /* Handle a "constructor" attribute; arguments as in
7034 struct attribute_spec.handler. */
7036 static tree
7037 handle_constructor_attribute (tree *node, tree name, tree args,
7038 int ARG_UNUSED (flags),
7039 bool *no_add_attrs)
7041 tree decl = *node;
7042 tree type = TREE_TYPE (decl);
7044 if (TREE_CODE (decl) == FUNCTION_DECL
7045 && TREE_CODE (type) == FUNCTION_TYPE
7046 && decl_function_context (decl) == 0)
7048 priority_type priority;
7049 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7050 priority = get_priority (args, /*is_destructor=*/false);
7051 SET_DECL_INIT_PRIORITY (decl, priority);
7052 TREE_USED (decl) = 1;
7054 else
7056 warning (OPT_Wattributes, "%qE attribute ignored", name);
7057 *no_add_attrs = true;
7060 return NULL_TREE;
7063 /* Handle a "destructor" attribute; arguments as in
7064 struct attribute_spec.handler. */
7066 static tree
7067 handle_destructor_attribute (tree *node, tree name, tree args,
7068 int ARG_UNUSED (flags),
7069 bool *no_add_attrs)
7071 tree decl = *node;
7072 tree type = TREE_TYPE (decl);
7074 if (TREE_CODE (decl) == FUNCTION_DECL
7075 && TREE_CODE (type) == FUNCTION_TYPE
7076 && decl_function_context (decl) == 0)
7078 priority_type priority;
7079 DECL_STATIC_DESTRUCTOR (decl) = 1;
7080 priority = get_priority (args, /*is_destructor=*/true);
7081 SET_DECL_FINI_PRIORITY (decl, priority);
7082 TREE_USED (decl) = 1;
7084 else
7086 warning (OPT_Wattributes, "%qE attribute ignored", name);
7087 *no_add_attrs = true;
7090 return NULL_TREE;
7093 /* Nonzero if the mode is a valid vector mode for this architecture.
7094 This returns nonzero even if there is no hardware support for the
7095 vector mode, but we can emulate with narrower modes. */
7097 static int
7098 vector_mode_valid_p (enum machine_mode mode)
7100 enum mode_class mclass = GET_MODE_CLASS (mode);
7101 enum machine_mode innermode;
7103 /* Doh! What's going on? */
7104 if (mclass != MODE_VECTOR_INT
7105 && mclass != MODE_VECTOR_FLOAT
7106 && mclass != MODE_VECTOR_FRACT
7107 && mclass != MODE_VECTOR_UFRACT
7108 && mclass != MODE_VECTOR_ACCUM
7109 && mclass != MODE_VECTOR_UACCUM)
7110 return 0;
7112 /* Hardware support. Woo hoo! */
7113 if (targetm.vector_mode_supported_p (mode))
7114 return 1;
7116 innermode = GET_MODE_INNER (mode);
7118 /* We should probably return 1 if requesting V4DI and we have no DI,
7119 but we have V2DI, but this is probably very unlikely. */
7121 /* If we have support for the inner mode, we can safely emulate it.
7122 We may not have V2DI, but me can emulate with a pair of DIs. */
7123 return targetm.scalar_mode_supported_p (innermode);
7127 /* Handle a "mode" attribute; arguments as in
7128 struct attribute_spec.handler. */
7130 static tree
7131 handle_mode_attribute (tree *node, tree name, tree args,
7132 int ARG_UNUSED (flags), bool *no_add_attrs)
7134 tree type = *node;
7135 tree ident = TREE_VALUE (args);
7137 *no_add_attrs = true;
7139 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7140 warning (OPT_Wattributes, "%qE attribute ignored", name);
7141 else
7143 int j;
7144 const char *p = IDENTIFIER_POINTER (ident);
7145 int len = strlen (p);
7146 enum machine_mode mode = VOIDmode;
7147 tree typefm;
7148 bool valid_mode;
7150 if (len > 4 && p[0] == '_' && p[1] == '_'
7151 && p[len - 1] == '_' && p[len - 2] == '_')
7153 char *newp = (char *) alloca (len - 1);
7155 strcpy (newp, &p[2]);
7156 newp[len - 4] = '\0';
7157 p = newp;
7160 /* Change this type to have a type with the specified mode.
7161 First check for the special modes. */
7162 if (!strcmp (p, "byte"))
7163 mode = byte_mode;
7164 else if (!strcmp (p, "word"))
7165 mode = word_mode;
7166 else if (!strcmp (p, "pointer"))
7167 mode = ptr_mode;
7168 else if (!strcmp (p, "libgcc_cmp_return"))
7169 mode = targetm.libgcc_cmp_return_mode ();
7170 else if (!strcmp (p, "libgcc_shift_count"))
7171 mode = targetm.libgcc_shift_count_mode ();
7172 else if (!strcmp (p, "unwind_word"))
7173 mode = targetm.unwind_word_mode ();
7174 else
7175 for (j = 0; j < NUM_MACHINE_MODES; j++)
7176 if (!strcmp (p, GET_MODE_NAME (j)))
7178 mode = (enum machine_mode) j;
7179 break;
7182 if (mode == VOIDmode)
7184 error ("unknown machine mode %qE", ident);
7185 return NULL_TREE;
7188 valid_mode = false;
7189 switch (GET_MODE_CLASS (mode))
7191 case MODE_INT:
7192 case MODE_PARTIAL_INT:
7193 case MODE_FLOAT:
7194 case MODE_DECIMAL_FLOAT:
7195 case MODE_FRACT:
7196 case MODE_UFRACT:
7197 case MODE_ACCUM:
7198 case MODE_UACCUM:
7199 valid_mode = targetm.scalar_mode_supported_p (mode);
7200 break;
7202 case MODE_COMPLEX_INT:
7203 case MODE_COMPLEX_FLOAT:
7204 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7205 break;
7207 case MODE_VECTOR_INT:
7208 case MODE_VECTOR_FLOAT:
7209 case MODE_VECTOR_FRACT:
7210 case MODE_VECTOR_UFRACT:
7211 case MODE_VECTOR_ACCUM:
7212 case MODE_VECTOR_UACCUM:
7213 warning (OPT_Wattributes, "specifying vector types with "
7214 "__attribute__ ((mode)) is deprecated");
7215 warning (OPT_Wattributes,
7216 "use __attribute__ ((vector_size)) instead");
7217 valid_mode = vector_mode_valid_p (mode);
7218 break;
7220 default:
7221 break;
7223 if (!valid_mode)
7225 error ("unable to emulate %qs", p);
7226 return NULL_TREE;
7229 if (POINTER_TYPE_P (type))
7231 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7232 tree (*fn)(tree, enum machine_mode, bool);
7234 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7236 error ("invalid pointer mode %qs", p);
7237 return NULL_TREE;
7240 if (TREE_CODE (type) == POINTER_TYPE)
7241 fn = build_pointer_type_for_mode;
7242 else
7243 fn = build_reference_type_for_mode;
7244 typefm = fn (TREE_TYPE (type), mode, false);
7246 else
7248 /* For fixed-point modes, we need to test if the signness of type
7249 and the machine mode are consistent. */
7250 if (ALL_FIXED_POINT_MODE_P (mode)
7251 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7253 error ("signedness of type and machine mode %qs don%'t match", p);
7254 return NULL_TREE;
7256 /* For fixed-point modes, we need to pass saturating info. */
7257 typefm = lang_hooks.types.type_for_mode (mode,
7258 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7259 : TYPE_UNSIGNED (type));
7262 if (typefm == NULL_TREE)
7264 error ("no data type for mode %qs", p);
7265 return NULL_TREE;
7267 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7269 /* For enumeral types, copy the precision from the integer
7270 type returned above. If not an INTEGER_TYPE, we can't use
7271 this mode for this type. */
7272 if (TREE_CODE (typefm) != INTEGER_TYPE)
7274 error ("cannot use mode %qs for enumeral types", p);
7275 return NULL_TREE;
7278 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7280 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7281 typefm = type;
7283 else
7285 /* We cannot build a type variant, as there's code that assumes
7286 that TYPE_MAIN_VARIANT has the same mode. This includes the
7287 debug generators. Instead, create a subrange type. This
7288 results in all of the enumeral values being emitted only once
7289 in the original, and the subtype gets them by reference. */
7290 if (TYPE_UNSIGNED (type))
7291 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7292 else
7293 typefm = make_signed_type (TYPE_PRECISION (typefm));
7294 TREE_TYPE (typefm) = type;
7297 else if (VECTOR_MODE_P (mode)
7298 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7299 : TREE_CODE (type) != TREE_CODE (typefm))
7301 error ("mode %qs applied to inappropriate type", p);
7302 return NULL_TREE;
7305 *node = typefm;
7308 return NULL_TREE;
7311 /* Handle a "section" attribute; arguments as in
7312 struct attribute_spec.handler. */
7314 static tree
7315 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7316 int ARG_UNUSED (flags), bool *no_add_attrs)
7318 tree decl = *node;
7320 if (targetm_common.have_named_sections)
7322 user_defined_section_attribute = true;
7324 if ((TREE_CODE (decl) == FUNCTION_DECL
7325 || TREE_CODE (decl) == VAR_DECL)
7326 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7328 if (TREE_CODE (decl) == VAR_DECL
7329 && current_function_decl != NULL_TREE
7330 && !TREE_STATIC (decl))
7332 error_at (DECL_SOURCE_LOCATION (decl),
7333 "section attribute cannot be specified for "
7334 "local variables");
7335 *no_add_attrs = true;
7338 /* The decl may have already been given a section attribute
7339 from a previous declaration. Ensure they match. */
7340 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7341 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7342 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7344 error ("section of %q+D conflicts with previous declaration",
7345 *node);
7346 *no_add_attrs = true;
7348 else if (TREE_CODE (decl) == VAR_DECL
7349 && !targetm.have_tls && targetm.emutls.tmpl_section
7350 && DECL_THREAD_LOCAL_P (decl))
7352 error ("section of %q+D cannot be overridden", *node);
7353 *no_add_attrs = true;
7355 else
7356 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7358 else
7360 error ("section attribute not allowed for %q+D", *node);
7361 *no_add_attrs = true;
7364 else
7366 error_at (DECL_SOURCE_LOCATION (*node),
7367 "section attributes are not supported for this target");
7368 *no_add_attrs = true;
7371 return NULL_TREE;
7374 /* Check whether ALIGN is a valid user-specified alignment. If so,
7375 return its base-2 log; if not, output an error and return -1. If
7376 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7377 no error. */
7379 check_user_alignment (const_tree align, bool allow_zero)
7381 int i;
7383 if (TREE_CODE (align) != INTEGER_CST
7384 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7386 error ("requested alignment is not an integer constant");
7387 return -1;
7389 else if (allow_zero && integer_zerop (align))
7390 return -1;
7391 else if (tree_int_cst_sgn (align) == -1
7392 || (i = tree_log2 (align)) == -1)
7394 error ("requested alignment is not a positive power of 2");
7395 return -1;
7397 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7399 error ("requested alignment is too large");
7400 return -1;
7402 return i;
7406 If in c++-11, check if the c++-11 alignment constraint with respect
7407 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7408 c++-11 mode, does nothing.
7410 [dcl.align]2/ says:
7412 [* if the constant expression evaluates to a fundamental alignment,
7413 the alignment requirement of the declared entity shall be the
7414 specified fundamental alignment.
7416 * if the constant expression evaluates to an extended alignment
7417 and the implementation supports that alignment in the context
7418 of the declaration, the alignment of the declared entity shall
7419 be that alignment
7421 * if the constant expression evaluates to an extended alignment
7422 and the implementation does not support that alignment in the
7423 context of the declaration, the program is ill-formed]. */
7425 static bool
7426 check_cxx_fundamental_alignment_constraints (tree node,
7427 unsigned align_log,
7428 int flags)
7430 bool alignment_too_large_p = false;
7431 unsigned requested_alignment = 1U << align_log;
7432 unsigned max_align = 0;
7434 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7435 || (node == NULL_TREE || node == error_mark_node))
7436 return true;
7438 if (cxx_fundamental_alignment_p (requested_alignment))
7439 return true;
7441 if (DECL_P (node))
7443 if (TREE_STATIC (node))
7445 /* For file scope variables and static members, the target
7446 supports alignments that are at most
7447 MAX_OFILE_ALIGNMENT. */
7448 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7449 alignment_too_large_p = true;
7451 else
7453 #ifdef BIGGEST_FIELD_ALIGNMENT
7454 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7455 #else
7456 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7457 #endif
7458 /* For non-static members, the target supports either
7459 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7460 if it is defined or BIGGEST_ALIGNMENT. */
7461 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7462 if (TREE_CODE (node) == FIELD_DECL
7463 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7464 alignment_too_large_p = true;
7465 #undef MAX_TARGET_FIELD_ALIGNMENT
7466 /* For stack variables, the target supports at most
7467 MAX_STACK_ALIGNMENT. */
7468 else if (decl_function_context (node) != NULL
7469 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7470 alignment_too_large_p = true;
7473 else if (TYPE_P (node))
7475 /* Let's be liberal for types. */
7476 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7477 alignment_too_large_p = true;
7480 if (alignment_too_large_p)
7481 pedwarn (input_location, OPT_Wattributes,
7482 "requested alignment %d is larger than %d",
7483 requested_alignment, max_align);
7485 return !alignment_too_large_p;
7488 /* Handle a "aligned" attribute; arguments as in
7489 struct attribute_spec.handler. */
7491 static tree
7492 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7493 int flags, bool *no_add_attrs)
7495 tree decl = NULL_TREE;
7496 tree *type = NULL;
7497 int is_type = 0;
7498 tree align_expr = (args ? TREE_VALUE (args)
7499 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7500 int i;
7502 if (DECL_P (*node))
7504 decl = *node;
7505 type = &TREE_TYPE (decl);
7506 is_type = TREE_CODE (*node) == TYPE_DECL;
7508 else if (TYPE_P (*node))
7509 type = node, is_type = 1;
7511 if ((i = check_user_alignment (align_expr, false)) == -1
7512 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7513 *no_add_attrs = true;
7514 else if (is_type)
7516 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7517 /* OK, modify the type in place. */;
7518 /* If we have a TYPE_DECL, then copy the type, so that we
7519 don't accidentally modify a builtin type. See pushdecl. */
7520 else if (decl && TREE_TYPE (decl) != error_mark_node
7521 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7523 tree tt = TREE_TYPE (decl);
7524 *type = build_variant_type_copy (*type);
7525 DECL_ORIGINAL_TYPE (decl) = tt;
7526 TYPE_NAME (*type) = decl;
7527 TREE_USED (*type) = TREE_USED (decl);
7528 TREE_TYPE (decl) = *type;
7530 else
7531 *type = build_variant_type_copy (*type);
7533 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7534 TYPE_USER_ALIGN (*type) = 1;
7536 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7537 && TREE_CODE (decl) != FIELD_DECL)
7539 error ("alignment may not be specified for %q+D", decl);
7540 *no_add_attrs = true;
7542 else if (DECL_USER_ALIGN (decl)
7543 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7544 /* C++-11 [dcl.align/4]:
7546 When multiple alignment-specifiers are specified for an
7547 entity, the alignment requirement shall be set to the
7548 strictest specified alignment.
7550 This formally comes from the c++11 specification but we are
7551 doing it for the GNU attribute syntax as well. */
7552 *no_add_attrs = true;
7553 else if (TREE_CODE (decl) == FUNCTION_DECL
7554 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7556 if (DECL_USER_ALIGN (decl))
7557 error ("alignment for %q+D was previously specified as %d "
7558 "and may not be decreased", decl,
7559 DECL_ALIGN (decl) / BITS_PER_UNIT);
7560 else
7561 error ("alignment for %q+D must be at least %d", decl,
7562 DECL_ALIGN (decl) / BITS_PER_UNIT);
7563 *no_add_attrs = true;
7565 else
7567 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7568 DECL_USER_ALIGN (decl) = 1;
7571 return NULL_TREE;
7574 /* Handle a "weak" attribute; arguments as in
7575 struct attribute_spec.handler. */
7577 static tree
7578 handle_weak_attribute (tree *node, tree name,
7579 tree ARG_UNUSED (args),
7580 int ARG_UNUSED (flags),
7581 bool * ARG_UNUSED (no_add_attrs))
7583 if (TREE_CODE (*node) == FUNCTION_DECL
7584 && DECL_DECLARED_INLINE_P (*node))
7586 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7587 *no_add_attrs = true;
7589 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7591 error ("indirect function %q+D cannot be declared weak", *node);
7592 *no_add_attrs = true;
7593 return NULL_TREE;
7595 else if (TREE_CODE (*node) == FUNCTION_DECL
7596 || TREE_CODE (*node) == VAR_DECL)
7597 declare_weak (*node);
7598 else
7599 warning (OPT_Wattributes, "%qE attribute ignored", name);
7601 return NULL_TREE;
7604 /* Handle an "alias" or "ifunc" attribute; arguments as in
7605 struct attribute_spec.handler, except that IS_ALIAS tells us
7606 whether this is an alias as opposed to ifunc attribute. */
7608 static tree
7609 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7610 bool *no_add_attrs)
7612 tree decl = *node;
7614 if (TREE_CODE (decl) != FUNCTION_DECL
7615 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7617 warning (OPT_Wattributes, "%qE attribute ignored", name);
7618 *no_add_attrs = true;
7620 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7621 || (TREE_CODE (decl) != FUNCTION_DECL
7622 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7623 /* A static variable declaration is always a tentative definition,
7624 but the alias is a non-tentative definition which overrides. */
7625 || (TREE_CODE (decl) != FUNCTION_DECL
7626 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7628 error ("%q+D defined both normally and as %qE attribute", decl, name);
7629 *no_add_attrs = true;
7630 return NULL_TREE;
7632 else if (!is_alias
7633 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7634 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7636 error ("weak %q+D cannot be defined %qE", decl, name);
7637 *no_add_attrs = true;
7638 return NULL_TREE;
7641 /* Note that the very first time we process a nested declaration,
7642 decl_function_context will not be set. Indeed, *would* never
7643 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7644 we do below. After such frobbery, pushdecl would set the context.
7645 In any case, this is never what we want. */
7646 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7648 tree id;
7650 id = TREE_VALUE (args);
7651 if (TREE_CODE (id) != STRING_CST)
7653 error ("attribute %qE argument not a string", name);
7654 *no_add_attrs = true;
7655 return NULL_TREE;
7657 id = get_identifier (TREE_STRING_POINTER (id));
7658 /* This counts as a use of the object pointed to. */
7659 TREE_USED (id) = 1;
7661 if (TREE_CODE (decl) == FUNCTION_DECL)
7662 DECL_INITIAL (decl) = error_mark_node;
7663 else
7664 TREE_STATIC (decl) = 1;
7666 if (!is_alias)
7667 /* ifuncs are also aliases, so set that attribute too. */
7668 DECL_ATTRIBUTES (decl)
7669 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7671 else
7673 warning (OPT_Wattributes, "%qE attribute ignored", name);
7674 *no_add_attrs = true;
7677 return NULL_TREE;
7680 /* Handle an "alias" or "ifunc" attribute; arguments as in
7681 struct attribute_spec.handler. */
7683 static tree
7684 handle_ifunc_attribute (tree *node, tree name, tree args,
7685 int ARG_UNUSED (flags), bool *no_add_attrs)
7687 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7690 /* Handle an "alias" or "ifunc" attribute; arguments as in
7691 struct attribute_spec.handler. */
7693 static tree
7694 handle_alias_attribute (tree *node, tree name, tree args,
7695 int ARG_UNUSED (flags), bool *no_add_attrs)
7697 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7700 /* Handle a "weakref" attribute; arguments as in struct
7701 attribute_spec.handler. */
7703 static tree
7704 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7705 int flags, bool *no_add_attrs)
7707 tree attr = NULL_TREE;
7709 /* We must ignore the attribute when it is associated with
7710 local-scoped decls, since attribute alias is ignored and many
7711 such symbols do not even have a DECL_WEAK field. */
7712 if (decl_function_context (*node)
7713 || current_function_decl
7714 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7716 warning (OPT_Wattributes, "%qE attribute ignored", name);
7717 *no_add_attrs = true;
7718 return NULL_TREE;
7721 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7723 error ("indirect function %q+D cannot be declared weakref", *node);
7724 *no_add_attrs = true;
7725 return NULL_TREE;
7728 /* The idea here is that `weakref("name")' mutates into `weakref,
7729 alias("name")', and weakref without arguments, in turn,
7730 implicitly adds weak. */
7732 if (args)
7734 attr = tree_cons (get_identifier ("alias"), args, attr);
7735 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7737 *no_add_attrs = true;
7739 decl_attributes (node, attr, flags);
7741 else
7743 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7744 error_at (DECL_SOURCE_LOCATION (*node),
7745 "weakref attribute must appear before alias attribute");
7747 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7748 and that isn't supported; and because it wants to add it to
7749 the list of weak decls, which isn't helpful. */
7750 DECL_WEAK (*node) = 1;
7753 return NULL_TREE;
7756 /* Handle an "visibility" attribute; arguments as in
7757 struct attribute_spec.handler. */
7759 static tree
7760 handle_visibility_attribute (tree *node, tree name, tree args,
7761 int ARG_UNUSED (flags),
7762 bool *ARG_UNUSED (no_add_attrs))
7764 tree decl = *node;
7765 tree id = TREE_VALUE (args);
7766 enum symbol_visibility vis;
7768 if (TYPE_P (*node))
7770 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7771 /* OK */;
7772 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7774 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7775 name);
7776 return NULL_TREE;
7778 else if (TYPE_FIELDS (*node))
7780 error ("%qE attribute ignored because %qT is already defined",
7781 name, *node);
7782 return NULL_TREE;
7785 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7787 warning (OPT_Wattributes, "%qE attribute ignored", name);
7788 return NULL_TREE;
7791 if (TREE_CODE (id) != STRING_CST)
7793 error ("visibility argument not a string");
7794 return NULL_TREE;
7797 /* If this is a type, set the visibility on the type decl. */
7798 if (TYPE_P (decl))
7800 decl = TYPE_NAME (decl);
7801 if (!decl)
7802 return NULL_TREE;
7803 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7805 warning (OPT_Wattributes, "%qE attribute ignored on types",
7806 name);
7807 return NULL_TREE;
7811 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7812 vis = VISIBILITY_DEFAULT;
7813 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7814 vis = VISIBILITY_INTERNAL;
7815 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7816 vis = VISIBILITY_HIDDEN;
7817 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7818 vis = VISIBILITY_PROTECTED;
7819 else
7821 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7822 vis = VISIBILITY_DEFAULT;
7825 if (DECL_VISIBILITY_SPECIFIED (decl)
7826 && vis != DECL_VISIBILITY (decl))
7828 tree attributes = (TYPE_P (*node)
7829 ? TYPE_ATTRIBUTES (*node)
7830 : DECL_ATTRIBUTES (decl));
7831 if (lookup_attribute ("visibility", attributes))
7832 error ("%qD redeclared with different visibility", decl);
7833 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7834 && lookup_attribute ("dllimport", attributes))
7835 error ("%qD was declared %qs which implies default visibility",
7836 decl, "dllimport");
7837 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7838 && lookup_attribute ("dllexport", attributes))
7839 error ("%qD was declared %qs which implies default visibility",
7840 decl, "dllexport");
7843 DECL_VISIBILITY (decl) = vis;
7844 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7846 /* Go ahead and attach the attribute to the node as well. This is needed
7847 so we can determine whether we have VISIBILITY_DEFAULT because the
7848 visibility was not specified, or because it was explicitly overridden
7849 from the containing scope. */
7851 return NULL_TREE;
7854 /* Determine the ELF symbol visibility for DECL, which is either a
7855 variable or a function. It is an error to use this function if a
7856 definition of DECL is not available in this translation unit.
7857 Returns true if the final visibility has been determined by this
7858 function; false if the caller is free to make additional
7859 modifications. */
7861 bool
7862 c_determine_visibility (tree decl)
7864 gcc_assert (TREE_CODE (decl) == VAR_DECL
7865 || TREE_CODE (decl) == FUNCTION_DECL);
7867 /* If the user explicitly specified the visibility with an
7868 attribute, honor that. DECL_VISIBILITY will have been set during
7869 the processing of the attribute. We check for an explicit
7870 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7871 to distinguish the use of an attribute from the use of a "#pragma
7872 GCC visibility push(...)"; in the latter case we still want other
7873 considerations to be able to overrule the #pragma. */
7874 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7875 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7876 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7877 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7878 return true;
7880 /* Set default visibility to whatever the user supplied with
7881 visibility_specified depending on #pragma GCC visibility. */
7882 if (!DECL_VISIBILITY_SPECIFIED (decl))
7884 if (visibility_options.inpragma
7885 || DECL_VISIBILITY (decl) != default_visibility)
7887 DECL_VISIBILITY (decl) = default_visibility;
7888 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7889 /* If visibility changed and DECL already has DECL_RTL, ensure
7890 symbol flags are updated. */
7891 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7892 || TREE_CODE (decl) == FUNCTION_DECL)
7893 && DECL_RTL_SET_P (decl))
7894 make_decl_rtl (decl);
7897 return false;
7900 /* Handle an "tls_model" attribute; arguments as in
7901 struct attribute_spec.handler. */
7903 static tree
7904 handle_tls_model_attribute (tree *node, tree name, tree args,
7905 int ARG_UNUSED (flags), bool *no_add_attrs)
7907 tree id;
7908 tree decl = *node;
7909 enum tls_model kind;
7911 *no_add_attrs = true;
7913 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7915 warning (OPT_Wattributes, "%qE attribute ignored", name);
7916 return NULL_TREE;
7919 kind = DECL_TLS_MODEL (decl);
7920 id = TREE_VALUE (args);
7921 if (TREE_CODE (id) != STRING_CST)
7923 error ("tls_model argument not a string");
7924 return NULL_TREE;
7927 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7928 kind = TLS_MODEL_LOCAL_EXEC;
7929 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7930 kind = TLS_MODEL_INITIAL_EXEC;
7931 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7932 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7933 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7934 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7935 else
7936 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7938 DECL_TLS_MODEL (decl) = kind;
7939 return NULL_TREE;
7942 /* Handle a "no_instrument_function" attribute; arguments as in
7943 struct attribute_spec.handler. */
7945 static tree
7946 handle_no_instrument_function_attribute (tree *node, tree name,
7947 tree ARG_UNUSED (args),
7948 int ARG_UNUSED (flags),
7949 bool *no_add_attrs)
7951 tree decl = *node;
7953 if (TREE_CODE (decl) != FUNCTION_DECL)
7955 error_at (DECL_SOURCE_LOCATION (decl),
7956 "%qE attribute applies only to functions", name);
7957 *no_add_attrs = true;
7959 else if (DECL_INITIAL (decl))
7961 error_at (DECL_SOURCE_LOCATION (decl),
7962 "can%'t set %qE attribute after definition", name);
7963 *no_add_attrs = true;
7965 else
7966 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7968 return NULL_TREE;
7971 /* Handle a "malloc" attribute; arguments as in
7972 struct attribute_spec.handler. */
7974 static tree
7975 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7976 int ARG_UNUSED (flags), bool *no_add_attrs)
7978 if (TREE_CODE (*node) == FUNCTION_DECL
7979 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7980 DECL_IS_MALLOC (*node) = 1;
7981 else
7983 warning (OPT_Wattributes, "%qE attribute ignored", name);
7984 *no_add_attrs = true;
7987 return NULL_TREE;
7990 /* Handle a "alloc_size" attribute; arguments as in
7991 struct attribute_spec.handler. */
7993 static tree
7994 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7995 int ARG_UNUSED (flags), bool *no_add_attrs)
7997 unsigned arg_count = type_num_arguments (*node);
7998 for (; args; args = TREE_CHAIN (args))
8000 tree position = TREE_VALUE (args);
8002 if (TREE_CODE (position) != INTEGER_CST
8003 || TREE_INT_CST_HIGH (position)
8004 || TREE_INT_CST_LOW (position) < 1
8005 || TREE_INT_CST_LOW (position) > arg_count )
8007 warning (OPT_Wattributes,
8008 "alloc_size parameter outside range");
8009 *no_add_attrs = true;
8010 return NULL_TREE;
8013 return NULL_TREE;
8016 /* Handle a "fn spec" attribute; arguments as in
8017 struct attribute_spec.handler. */
8019 static tree
8020 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8021 tree args, int ARG_UNUSED (flags),
8022 bool *no_add_attrs ATTRIBUTE_UNUSED)
8024 gcc_assert (args
8025 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8026 && !TREE_CHAIN (args));
8027 return NULL_TREE;
8030 /* Handle a "bnd_variable_size" attribute; arguments as in
8031 struct attribute_spec.handler. */
8033 static tree
8034 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8035 int ARG_UNUSED (flags), bool *no_add_attrs)
8037 if (TREE_CODE (*node) != FIELD_DECL)
8039 warning (OPT_Wattributes, "%qE attribute ignored", name);
8040 *no_add_attrs = true;
8043 return NULL_TREE;
8046 /* Handle a "bnd_legacy" attribute; arguments as in
8047 struct attribute_spec.handler. */
8049 static tree
8050 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8051 int ARG_UNUSED (flags), bool *no_add_attrs)
8053 if (TREE_CODE (*node) != FUNCTION_DECL)
8055 warning (OPT_Wattributes, "%qE attribute ignored", name);
8056 *no_add_attrs = true;
8059 return NULL_TREE;
8062 /* Handle a "warn_unused" attribute; arguments as in
8063 struct attribute_spec.handler. */
8065 static tree
8066 handle_warn_unused_attribute (tree *node, tree name,
8067 tree args ATTRIBUTE_UNUSED,
8068 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8070 if (TYPE_P (*node))
8071 /* Do nothing else, just set the attribute. We'll get at
8072 it later with lookup_attribute. */
8074 else
8076 warning (OPT_Wattributes, "%qE attribute ignored", name);
8077 *no_add_attrs = true;
8080 return NULL_TREE;
8083 /* Handle an "omp declare simd" attribute; arguments as in
8084 struct attribute_spec.handler. */
8086 static tree
8087 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8089 return NULL_TREE;
8092 /* Handle an "omp declare target" attribute; arguments as in
8093 struct attribute_spec.handler. */
8095 static tree
8096 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8098 return NULL_TREE;
8101 /* Handle a "returns_twice" attribute; arguments as in
8102 struct attribute_spec.handler. */
8104 static tree
8105 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8106 int ARG_UNUSED (flags), bool *no_add_attrs)
8108 if (TREE_CODE (*node) == FUNCTION_DECL)
8109 DECL_IS_RETURNS_TWICE (*node) = 1;
8110 else
8112 warning (OPT_Wattributes, "%qE attribute ignored", name);
8113 *no_add_attrs = true;
8116 return NULL_TREE;
8119 /* Handle a "no_limit_stack" attribute; arguments as in
8120 struct attribute_spec.handler. */
8122 static tree
8123 handle_no_limit_stack_attribute (tree *node, tree name,
8124 tree ARG_UNUSED (args),
8125 int ARG_UNUSED (flags),
8126 bool *no_add_attrs)
8128 tree decl = *node;
8130 if (TREE_CODE (decl) != FUNCTION_DECL)
8132 error_at (DECL_SOURCE_LOCATION (decl),
8133 "%qE attribute applies only to functions", name);
8134 *no_add_attrs = true;
8136 else if (DECL_INITIAL (decl))
8138 error_at (DECL_SOURCE_LOCATION (decl),
8139 "can%'t set %qE attribute after definition", name);
8140 *no_add_attrs = true;
8142 else
8143 DECL_NO_LIMIT_STACK (decl) = 1;
8145 return NULL_TREE;
8148 /* Handle a "pure" attribute; arguments as in
8149 struct attribute_spec.handler. */
8151 static tree
8152 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8153 int ARG_UNUSED (flags), bool *no_add_attrs)
8155 if (TREE_CODE (*node) == FUNCTION_DECL)
8156 DECL_PURE_P (*node) = 1;
8157 /* ??? TODO: Support types. */
8158 else
8160 warning (OPT_Wattributes, "%qE attribute ignored", name);
8161 *no_add_attrs = true;
8164 return NULL_TREE;
8167 /* Digest an attribute list destined for a transactional memory statement.
8168 ALLOWED is the set of attributes that are allowed for this statement;
8169 return the attribute we parsed. Multiple attributes are never allowed. */
8172 parse_tm_stmt_attr (tree attrs, int allowed)
8174 tree a_seen = NULL;
8175 int m_seen = 0;
8177 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8179 tree a = TREE_PURPOSE (attrs);
8180 int m = 0;
8182 if (is_attribute_p ("outer", a))
8183 m = TM_STMT_ATTR_OUTER;
8185 if ((m & allowed) == 0)
8187 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8188 continue;
8191 if (m_seen == 0)
8193 a_seen = a;
8194 m_seen = m;
8196 else if (m_seen == m)
8197 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8198 else
8199 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8202 return m_seen;
8205 /* Transform a TM attribute name into a maskable integer and back.
8206 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8207 to how the lack of an attribute is treated. */
8210 tm_attr_to_mask (tree attr)
8212 if (attr == NULL)
8213 return 0;
8214 if (is_attribute_p ("transaction_safe", attr))
8215 return TM_ATTR_SAFE;
8216 if (is_attribute_p ("transaction_callable", attr))
8217 return TM_ATTR_CALLABLE;
8218 if (is_attribute_p ("transaction_pure", attr))
8219 return TM_ATTR_PURE;
8220 if (is_attribute_p ("transaction_unsafe", attr))
8221 return TM_ATTR_IRREVOCABLE;
8222 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8223 return TM_ATTR_MAY_CANCEL_OUTER;
8224 return 0;
8227 tree
8228 tm_mask_to_attr (int mask)
8230 const char *str;
8231 switch (mask)
8233 case TM_ATTR_SAFE:
8234 str = "transaction_safe";
8235 break;
8236 case TM_ATTR_CALLABLE:
8237 str = "transaction_callable";
8238 break;
8239 case TM_ATTR_PURE:
8240 str = "transaction_pure";
8241 break;
8242 case TM_ATTR_IRREVOCABLE:
8243 str = "transaction_unsafe";
8244 break;
8245 case TM_ATTR_MAY_CANCEL_OUTER:
8246 str = "transaction_may_cancel_outer";
8247 break;
8248 default:
8249 gcc_unreachable ();
8251 return get_identifier (str);
8254 /* Return the first TM attribute seen in LIST. */
8256 tree
8257 find_tm_attribute (tree list)
8259 for (; list ; list = TREE_CHAIN (list))
8261 tree name = TREE_PURPOSE (list);
8262 if (tm_attr_to_mask (name) != 0)
8263 return name;
8265 return NULL_TREE;
8268 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8269 Here we accept only function types, and verify that none of the other
8270 function TM attributes are also applied. */
8271 /* ??? We need to accept class types for C++, but not C. This greatly
8272 complicates this function, since we can no longer rely on the extra
8273 processing given by function_type_required. */
8275 static tree
8276 handle_tm_attribute (tree *node, tree name, tree args,
8277 int flags, bool *no_add_attrs)
8279 /* Only one path adds the attribute; others don't. */
8280 *no_add_attrs = true;
8282 switch (TREE_CODE (*node))
8284 case RECORD_TYPE:
8285 case UNION_TYPE:
8286 /* Only tm_callable and tm_safe apply to classes. */
8287 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8288 goto ignored;
8289 /* FALLTHRU */
8291 case FUNCTION_TYPE:
8292 case METHOD_TYPE:
8294 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8295 if (old_name == name)
8297 else if (old_name != NULL_TREE)
8298 error ("type was previously declared %qE", old_name);
8299 else
8300 *no_add_attrs = false;
8302 break;
8304 case POINTER_TYPE:
8306 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8307 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8309 tree fn_tmp = TREE_TYPE (*node);
8310 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8311 *node = build_pointer_type (fn_tmp);
8312 break;
8315 /* FALLTHRU */
8317 default:
8318 /* If a function is next, pass it on to be tried next. */
8319 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8320 return tree_cons (name, args, NULL);
8322 ignored:
8323 warning (OPT_Wattributes, "%qE attribute ignored", name);
8324 break;
8327 return NULL_TREE;
8330 /* Handle the TM_WRAP attribute; arguments as in
8331 struct attribute_spec.handler. */
8333 static tree
8334 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8335 int ARG_UNUSED (flags), bool *no_add_attrs)
8337 tree decl = *node;
8339 /* We don't need the attribute even on success, since we
8340 record the entry in an external table. */
8341 *no_add_attrs = true;
8343 if (TREE_CODE (decl) != FUNCTION_DECL)
8344 warning (OPT_Wattributes, "%qE attribute ignored", name);
8345 else
8347 tree wrap_decl = TREE_VALUE (args);
8348 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8349 && TREE_CODE (wrap_decl) != VAR_DECL
8350 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8351 error ("%qE argument not an identifier", name);
8352 else
8354 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8355 wrap_decl = lookup_name (wrap_decl);
8356 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8358 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8359 TREE_TYPE (wrap_decl)))
8360 record_tm_replacement (wrap_decl, decl);
8361 else
8362 error ("%qD is not compatible with %qD", wrap_decl, decl);
8364 else
8365 error ("transaction_wrap argument is not a function");
8369 return NULL_TREE;
8372 /* Ignore the given attribute. Used when this attribute may be usefully
8373 overridden by the target, but is not used generically. */
8375 static tree
8376 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8377 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8378 bool *no_add_attrs)
8380 *no_add_attrs = true;
8381 return NULL_TREE;
8384 /* Handle a "no vops" attribute; arguments as in
8385 struct attribute_spec.handler. */
8387 static tree
8388 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8389 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8390 bool *ARG_UNUSED (no_add_attrs))
8392 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8393 DECL_IS_NOVOPS (*node) = 1;
8394 return NULL_TREE;
8397 /* Handle a "deprecated" attribute; arguments as in
8398 struct attribute_spec.handler. */
8400 static tree
8401 handle_deprecated_attribute (tree *node, tree name,
8402 tree args, int flags,
8403 bool *no_add_attrs)
8405 tree type = NULL_TREE;
8406 int warn = 0;
8407 tree what = NULL_TREE;
8409 if (!args)
8410 *no_add_attrs = true;
8411 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8413 error ("deprecated message is not a string");
8414 *no_add_attrs = true;
8417 if (DECL_P (*node))
8419 tree decl = *node;
8420 type = TREE_TYPE (decl);
8422 if (TREE_CODE (decl) == TYPE_DECL
8423 || TREE_CODE (decl) == PARM_DECL
8424 || TREE_CODE (decl) == VAR_DECL
8425 || TREE_CODE (decl) == FUNCTION_DECL
8426 || TREE_CODE (decl) == FIELD_DECL
8427 || objc_method_decl (TREE_CODE (decl)))
8428 TREE_DEPRECATED (decl) = 1;
8429 else
8430 warn = 1;
8432 else if (TYPE_P (*node))
8434 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8435 *node = build_variant_type_copy (*node);
8436 TREE_DEPRECATED (*node) = 1;
8437 type = *node;
8439 else
8440 warn = 1;
8442 if (warn)
8444 *no_add_attrs = true;
8445 if (type && TYPE_NAME (type))
8447 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8448 what = TYPE_NAME (*node);
8449 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8450 && DECL_NAME (TYPE_NAME (type)))
8451 what = DECL_NAME (TYPE_NAME (type));
8453 if (what)
8454 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8455 else
8456 warning (OPT_Wattributes, "%qE attribute ignored", name);
8459 return NULL_TREE;
8462 /* Handle a "vector_size" attribute; arguments as in
8463 struct attribute_spec.handler. */
8465 static tree
8466 handle_vector_size_attribute (tree *node, tree name, tree args,
8467 int ARG_UNUSED (flags),
8468 bool *no_add_attrs)
8470 unsigned HOST_WIDE_INT vecsize, nunits;
8471 enum machine_mode orig_mode;
8472 tree type = *node, new_type, size;
8474 *no_add_attrs = true;
8476 size = TREE_VALUE (args);
8478 if (!host_integerp (size, 1))
8480 warning (OPT_Wattributes, "%qE attribute ignored", name);
8481 return NULL_TREE;
8484 /* Get the vector size (in bytes). */
8485 vecsize = tree_low_cst (size, 1);
8487 /* We need to provide for vector pointers, vector arrays, and
8488 functions returning vectors. For example:
8490 __attribute__((vector_size(16))) short *foo;
8492 In this case, the mode is SI, but the type being modified is
8493 HI, so we need to look further. */
8495 while (POINTER_TYPE_P (type)
8496 || TREE_CODE (type) == FUNCTION_TYPE
8497 || TREE_CODE (type) == METHOD_TYPE
8498 || TREE_CODE (type) == ARRAY_TYPE
8499 || TREE_CODE (type) == OFFSET_TYPE)
8500 type = TREE_TYPE (type);
8502 /* Get the mode of the type being modified. */
8503 orig_mode = TYPE_MODE (type);
8505 if ((!INTEGRAL_TYPE_P (type)
8506 && !SCALAR_FLOAT_TYPE_P (type)
8507 && !FIXED_POINT_TYPE_P (type))
8508 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8509 && GET_MODE_CLASS (orig_mode) != MODE_INT
8510 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8511 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8512 || TREE_CODE (type) == BOOLEAN_TYPE)
8514 error ("invalid vector type for attribute %qE", name);
8515 return NULL_TREE;
8518 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8520 error ("vector size not an integral multiple of component size");
8521 return NULL;
8524 if (vecsize == 0)
8526 error ("zero vector size");
8527 return NULL;
8530 /* Calculate how many units fit in the vector. */
8531 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8532 if (nunits & (nunits - 1))
8534 error ("number of components of the vector not a power of two");
8535 return NULL_TREE;
8538 new_type = build_vector_type (type, nunits);
8540 /* Build back pointers if needed. */
8541 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8543 return NULL_TREE;
8546 /* Handle the "nonnull" attribute. */
8547 static tree
8548 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8549 tree args, int ARG_UNUSED (flags),
8550 bool *no_add_attrs)
8552 tree type = *node;
8553 unsigned HOST_WIDE_INT attr_arg_num;
8555 /* If no arguments are specified, all pointer arguments should be
8556 non-null. Verify a full prototype is given so that the arguments
8557 will have the correct types when we actually check them later. */
8558 if (!args)
8560 if (!prototype_p (type))
8562 error ("nonnull attribute without arguments on a non-prototype");
8563 *no_add_attrs = true;
8565 return NULL_TREE;
8568 /* Argument list specified. Verify that each argument number references
8569 a pointer argument. */
8570 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8572 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8574 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8576 error ("nonnull argument has invalid operand number (argument %lu)",
8577 (unsigned long) attr_arg_num);
8578 *no_add_attrs = true;
8579 return NULL_TREE;
8582 if (prototype_p (type))
8584 function_args_iterator iter;
8585 tree argument;
8587 function_args_iter_init (&iter, type);
8588 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8590 argument = function_args_iter_cond (&iter);
8591 if (argument == NULL_TREE || ck_num == arg_num)
8592 break;
8595 if (!argument
8596 || TREE_CODE (argument) == VOID_TYPE)
8598 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8599 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8600 *no_add_attrs = true;
8601 return NULL_TREE;
8604 if (TREE_CODE (argument) != POINTER_TYPE)
8606 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8607 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8608 *no_add_attrs = true;
8609 return NULL_TREE;
8614 return NULL_TREE;
8617 /* Check the argument list of a function call for null in argument slots
8618 that are marked as requiring a non-null pointer argument. The NARGS
8619 arguments are passed in the array ARGARRAY.
8622 static void
8623 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8625 tree a;
8626 int i;
8628 attrs = lookup_attribute ("nonnull", attrs);
8629 if (attrs == NULL_TREE)
8630 return;
8632 a = attrs;
8633 /* See if any of the nonnull attributes has no arguments. If so,
8634 then every pointer argument is checked (in which case the check
8635 for pointer type is done in check_nonnull_arg). */
8636 if (TREE_VALUE (a) != NULL_TREE)
8638 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8639 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8641 if (a != NULL_TREE)
8642 for (i = 0; i < nargs; i++)
8643 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8644 i + 1);
8645 else
8647 /* Walk the argument list. If we encounter an argument number we
8648 should check for non-null, do it. */
8649 for (i = 0; i < nargs; i++)
8651 for (a = attrs; ; a = TREE_CHAIN (a))
8653 a = lookup_attribute ("nonnull", a);
8654 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8655 break;
8658 if (a != NULL_TREE)
8659 check_function_arguments_recurse (check_nonnull_arg, NULL,
8660 argarray[i], i + 1);
8665 /* Check that the Nth argument of a function call (counting backwards
8666 from the end) is a (pointer)0. The NARGS arguments are passed in the
8667 array ARGARRAY. */
8669 static void
8670 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8672 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8674 if (attr)
8676 int len = 0;
8677 int pos = 0;
8678 tree sentinel;
8679 function_args_iterator iter;
8680 tree t;
8682 /* Skip over the named arguments. */
8683 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8685 if (len == nargs)
8686 break;
8687 len++;
8690 if (TREE_VALUE (attr))
8692 tree p = TREE_VALUE (TREE_VALUE (attr));
8693 pos = TREE_INT_CST_LOW (p);
8696 /* The sentinel must be one of the varargs, i.e.
8697 in position >= the number of fixed arguments. */
8698 if ((nargs - 1 - pos) < len)
8700 warning (OPT_Wformat_,
8701 "not enough variable arguments to fit a sentinel");
8702 return;
8705 /* Validate the sentinel. */
8706 sentinel = argarray[nargs - 1 - pos];
8707 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8708 || !integer_zerop (sentinel))
8709 /* Although __null (in C++) is only an integer we allow it
8710 nevertheless, as we are guaranteed that it's exactly
8711 as wide as a pointer, and we don't want to force
8712 users to cast the NULL they have written there.
8713 We warn with -Wstrict-null-sentinel, though. */
8714 && (warn_strict_null_sentinel || null_node != sentinel))
8715 warning (OPT_Wformat_, "missing sentinel in function call");
8719 /* Helper for check_function_nonnull; given a list of operands which
8720 must be non-null in ARGS, determine if operand PARAM_NUM should be
8721 checked. */
8723 static bool
8724 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8726 unsigned HOST_WIDE_INT arg_num = 0;
8728 for (; args; args = TREE_CHAIN (args))
8730 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8732 gcc_assert (found);
8734 if (arg_num == param_num)
8735 return true;
8737 return false;
8740 /* Check that the function argument PARAM (which is operand number
8741 PARAM_NUM) is non-null. This is called by check_function_nonnull
8742 via check_function_arguments_recurse. */
8744 static void
8745 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8746 unsigned HOST_WIDE_INT param_num)
8748 /* Just skip checking the argument if it's not a pointer. This can
8749 happen if the "nonnull" attribute was given without an operand
8750 list (which means to check every pointer argument). */
8752 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8753 return;
8755 if (integer_zerop (param))
8756 warning (OPT_Wnonnull, "null argument where non-null required "
8757 "(argument %lu)", (unsigned long) param_num);
8760 /* Helper for nonnull attribute handling; fetch the operand number
8761 from the attribute argument list. */
8763 static bool
8764 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8766 /* Verify the arg number is a constant. */
8767 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8768 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8769 return false;
8771 *valp = TREE_INT_CST_LOW (arg_num_expr);
8772 return true;
8775 /* Handle a "nothrow" attribute; arguments as in
8776 struct attribute_spec.handler. */
8778 static tree
8779 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8780 int ARG_UNUSED (flags), bool *no_add_attrs)
8782 if (TREE_CODE (*node) == FUNCTION_DECL)
8783 TREE_NOTHROW (*node) = 1;
8784 /* ??? TODO: Support types. */
8785 else
8787 warning (OPT_Wattributes, "%qE attribute ignored", name);
8788 *no_add_attrs = true;
8791 return NULL_TREE;
8794 /* Handle a "cleanup" attribute; arguments as in
8795 struct attribute_spec.handler. */
8797 static tree
8798 handle_cleanup_attribute (tree *node, tree name, tree args,
8799 int ARG_UNUSED (flags), bool *no_add_attrs)
8801 tree decl = *node;
8802 tree cleanup_id, cleanup_decl;
8804 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8805 for global destructors in C++. This requires infrastructure that
8806 we don't have generically at the moment. It's also not a feature
8807 we'd be missing too much, since we do have attribute constructor. */
8808 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8810 warning (OPT_Wattributes, "%qE attribute ignored", name);
8811 *no_add_attrs = true;
8812 return NULL_TREE;
8815 /* Verify that the argument is a function in scope. */
8816 /* ??? We could support pointers to functions here as well, if
8817 that was considered desirable. */
8818 cleanup_id = TREE_VALUE (args);
8819 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8821 error ("cleanup argument not an identifier");
8822 *no_add_attrs = true;
8823 return NULL_TREE;
8825 cleanup_decl = lookup_name (cleanup_id);
8826 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8828 error ("cleanup argument not a function");
8829 *no_add_attrs = true;
8830 return NULL_TREE;
8833 /* That the function has proper type is checked with the
8834 eventual call to build_function_call. */
8836 return NULL_TREE;
8839 /* Handle a "warn_unused_result" attribute. No special handling. */
8841 static tree
8842 handle_warn_unused_result_attribute (tree *node, tree name,
8843 tree ARG_UNUSED (args),
8844 int ARG_UNUSED (flags), bool *no_add_attrs)
8846 /* Ignore the attribute for functions not returning any value. */
8847 if (VOID_TYPE_P (TREE_TYPE (*node)))
8849 warning (OPT_Wattributes, "%qE attribute ignored", name);
8850 *no_add_attrs = true;
8853 return NULL_TREE;
8856 /* Handle a "sentinel" attribute. */
8858 static tree
8859 handle_sentinel_attribute (tree *node, tree name, tree args,
8860 int ARG_UNUSED (flags), bool *no_add_attrs)
8862 if (!prototype_p (*node))
8864 warning (OPT_Wattributes,
8865 "%qE attribute requires prototypes with named arguments", name);
8866 *no_add_attrs = true;
8868 else
8870 if (!stdarg_p (*node))
8872 warning (OPT_Wattributes,
8873 "%qE attribute only applies to variadic functions", name);
8874 *no_add_attrs = true;
8878 if (args)
8880 tree position = TREE_VALUE (args);
8882 if (TREE_CODE (position) != INTEGER_CST)
8884 warning (OPT_Wattributes,
8885 "requested position is not an integer constant");
8886 *no_add_attrs = true;
8888 else
8890 if (tree_int_cst_lt (position, integer_zero_node))
8892 warning (OPT_Wattributes,
8893 "requested position is less than zero");
8894 *no_add_attrs = true;
8899 return NULL_TREE;
8902 /* Handle a "type_generic" attribute. */
8904 static tree
8905 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8906 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8907 bool * ARG_UNUSED (no_add_attrs))
8909 /* Ensure we have a function type. */
8910 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8912 /* Ensure we have a variadic function. */
8913 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8915 return NULL_TREE;
8918 /* Handle a "target" attribute. */
8920 static tree
8921 handle_target_attribute (tree *node, tree name, tree args, int flags,
8922 bool *no_add_attrs)
8924 /* Ensure we have a function type. */
8925 if (TREE_CODE (*node) != FUNCTION_DECL)
8927 warning (OPT_Wattributes, "%qE attribute ignored", name);
8928 *no_add_attrs = true;
8930 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8931 flags))
8932 *no_add_attrs = true;
8934 return NULL_TREE;
8937 /* Arguments being collected for optimization. */
8938 typedef const char *const_char_p; /* For DEF_VEC_P. */
8939 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
8942 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8943 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8944 false for #pragma GCC optimize. */
8946 bool
8947 parse_optimize_options (tree args, bool attr_p)
8949 bool ret = true;
8950 unsigned opt_argc;
8951 unsigned i;
8952 int saved_flag_strict_aliasing;
8953 const char **opt_argv;
8954 struct cl_decoded_option *decoded_options;
8955 unsigned int decoded_options_count;
8956 tree ap;
8958 /* Build up argv vector. Just in case the string is stored away, use garbage
8959 collected strings. */
8960 vec_safe_truncate (optimize_args, 0);
8961 vec_safe_push (optimize_args, (const char *) NULL);
8963 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8965 tree value = TREE_VALUE (ap);
8967 if (TREE_CODE (value) == INTEGER_CST)
8969 char buffer[20];
8970 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8971 vec_safe_push (optimize_args, ggc_strdup (buffer));
8974 else if (TREE_CODE (value) == STRING_CST)
8976 /* Split string into multiple substrings. */
8977 size_t len = TREE_STRING_LENGTH (value);
8978 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8979 char *end = p + len;
8980 char *comma;
8981 char *next_p = p;
8983 while (next_p != NULL)
8985 size_t len2;
8986 char *q, *r;
8988 p = next_p;
8989 comma = strchr (p, ',');
8990 if (comma)
8992 len2 = comma - p;
8993 *comma = '\0';
8994 next_p = comma+1;
8996 else
8998 len2 = end - p;
8999 next_p = NULL;
9002 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9004 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9005 options. */
9006 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9008 ret = false;
9009 if (attr_p)
9010 warning (OPT_Wattributes,
9011 "bad option %s to optimize attribute", p);
9012 else
9013 warning (OPT_Wpragmas,
9014 "bad option %s to pragma attribute", p);
9015 continue;
9018 if (*p != '-')
9020 *r++ = '-';
9022 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9023 itself is -Os, and any other switch begins with a -f. */
9024 if ((*p >= '0' && *p <= '9')
9025 || (p[0] == 's' && p[1] == '\0'))
9026 *r++ = 'O';
9027 else if (*p != 'O')
9028 *r++ = 'f';
9031 memcpy (r, p, len2);
9032 r[len2] = '\0';
9033 vec_safe_push (optimize_args, (const char *) q);
9039 opt_argc = optimize_args->length ();
9040 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9042 for (i = 1; i < opt_argc; i++)
9043 opt_argv[i] = (*optimize_args)[i];
9045 saved_flag_strict_aliasing = flag_strict_aliasing;
9047 /* Now parse the options. */
9048 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9049 &decoded_options,
9050 &decoded_options_count);
9051 decode_options (&global_options, &global_options_set,
9052 decoded_options, decoded_options_count,
9053 input_location, global_dc);
9055 targetm.override_options_after_change();
9057 /* Don't allow changing -fstrict-aliasing. */
9058 flag_strict_aliasing = saved_flag_strict_aliasing;
9060 optimize_args->truncate (0);
9061 return ret;
9064 /* For handling "optimize" attribute. arguments as in
9065 struct attribute_spec.handler. */
9067 static tree
9068 handle_optimize_attribute (tree *node, tree name, tree args,
9069 int ARG_UNUSED (flags), bool *no_add_attrs)
9071 /* Ensure we have a function type. */
9072 if (TREE_CODE (*node) != FUNCTION_DECL)
9074 warning (OPT_Wattributes, "%qE attribute ignored", name);
9075 *no_add_attrs = true;
9077 else
9079 struct cl_optimization cur_opts;
9080 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9082 /* Save current options. */
9083 cl_optimization_save (&cur_opts, &global_options);
9085 /* If we previously had some optimization options, use them as the
9086 default. */
9087 if (old_opts)
9088 cl_optimization_restore (&global_options,
9089 TREE_OPTIMIZATION (old_opts));
9091 /* Parse options, and update the vector. */
9092 parse_optimize_options (args, true);
9093 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9094 = build_optimization_node (&global_options);
9096 /* Restore current options. */
9097 cl_optimization_restore (&global_options, &cur_opts);
9100 return NULL_TREE;
9103 /* Handle a "no_split_stack" attribute. */
9105 static tree
9106 handle_no_split_stack_attribute (tree *node, tree name,
9107 tree ARG_UNUSED (args),
9108 int ARG_UNUSED (flags),
9109 bool *no_add_attrs)
9111 tree decl = *node;
9113 if (TREE_CODE (decl) != FUNCTION_DECL)
9115 error_at (DECL_SOURCE_LOCATION (decl),
9116 "%qE attribute applies only to functions", name);
9117 *no_add_attrs = true;
9119 else if (DECL_INITIAL (decl))
9121 error_at (DECL_SOURCE_LOCATION (decl),
9122 "can%'t set %qE attribute after definition", name);
9123 *no_add_attrs = true;
9126 return NULL_TREE;
9129 /* Handle a "returns_nonnull" attribute; arguments as in
9130 struct attribute_spec.handler. */
9132 static tree
9133 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9134 bool *no_add_attrs)
9136 // Even without a prototype we still have a return type we can check.
9137 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9139 error ("returns_nonnull attribute on a function not returning a pointer");
9140 *no_add_attrs = true;
9142 return NULL_TREE;
9146 /* Check for valid arguments being passed to a function with FNTYPE.
9147 There are NARGS arguments in the array ARGARRAY. */
9148 void
9149 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9151 /* Check for null being passed in a pointer argument that must be
9152 non-null. We also need to do this if format checking is enabled. */
9154 if (warn_nonnull)
9155 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9157 /* Check for errors in format strings. */
9159 if (warn_format || warn_suggest_attribute_format)
9160 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9162 if (warn_format)
9163 check_function_sentinel (fntype, nargs, argarray);
9166 /* Generic argument checking recursion routine. PARAM is the argument to
9167 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9168 once the argument is resolved. CTX is context for the callback. */
9169 void
9170 check_function_arguments_recurse (void (*callback)
9171 (void *, tree, unsigned HOST_WIDE_INT),
9172 void *ctx, tree param,
9173 unsigned HOST_WIDE_INT param_num)
9175 if (CONVERT_EXPR_P (param)
9176 && (TYPE_PRECISION (TREE_TYPE (param))
9177 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9179 /* Strip coercion. */
9180 check_function_arguments_recurse (callback, ctx,
9181 TREE_OPERAND (param, 0), param_num);
9182 return;
9185 if (TREE_CODE (param) == CALL_EXPR)
9187 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9188 tree attrs;
9189 bool found_format_arg = false;
9191 /* See if this is a call to a known internationalization function
9192 that modifies a format arg. Such a function may have multiple
9193 format_arg attributes (for example, ngettext). */
9195 for (attrs = TYPE_ATTRIBUTES (type);
9196 attrs;
9197 attrs = TREE_CHAIN (attrs))
9198 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9200 tree inner_arg;
9201 tree format_num_expr;
9202 int format_num;
9203 int i;
9204 call_expr_arg_iterator iter;
9206 /* Extract the argument number, which was previously checked
9207 to be valid. */
9208 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9210 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9211 && !TREE_INT_CST_HIGH (format_num_expr));
9213 format_num = TREE_INT_CST_LOW (format_num_expr);
9215 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9216 inner_arg != 0;
9217 inner_arg = next_call_expr_arg (&iter), i++)
9218 if (i == format_num)
9220 check_function_arguments_recurse (callback, ctx,
9221 inner_arg, param_num);
9222 found_format_arg = true;
9223 break;
9227 /* If we found a format_arg attribute and did a recursive check,
9228 we are done with checking this argument. Otherwise, we continue
9229 and this will be considered a non-literal. */
9230 if (found_format_arg)
9231 return;
9234 if (TREE_CODE (param) == COND_EXPR)
9236 /* Check both halves of the conditional expression. */
9237 check_function_arguments_recurse (callback, ctx,
9238 TREE_OPERAND (param, 1), param_num);
9239 check_function_arguments_recurse (callback, ctx,
9240 TREE_OPERAND (param, 2), param_num);
9241 return;
9244 (*callback) (ctx, param, param_num);
9247 /* Checks for a builtin function FNDECL that the number of arguments
9248 NARGS against the required number REQUIRED and issues an error if
9249 there is a mismatch. Returns true if the number of arguments is
9250 correct, otherwise false. */
9252 static bool
9253 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9255 if (nargs < required)
9257 error_at (input_location,
9258 "not enough arguments to function %qE", fndecl);
9259 return false;
9261 else if (nargs > required)
9263 error_at (input_location,
9264 "too many arguments to function %qE", fndecl);
9265 return false;
9267 return true;
9270 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9271 Returns false if there was an error, otherwise true. */
9273 bool
9274 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9276 if (!DECL_BUILT_IN (fndecl)
9277 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9278 return true;
9280 switch (DECL_FUNCTION_CODE (fndecl))
9282 case BUILT_IN_CONSTANT_P:
9283 return builtin_function_validate_nargs (fndecl, nargs, 1);
9285 case BUILT_IN_ISFINITE:
9286 case BUILT_IN_ISINF:
9287 case BUILT_IN_ISINF_SIGN:
9288 case BUILT_IN_ISNAN:
9289 case BUILT_IN_ISNORMAL:
9290 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9292 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9294 error ("non-floating-point argument in call to "
9295 "function %qE", fndecl);
9296 return false;
9298 return true;
9300 return false;
9302 case BUILT_IN_ISGREATER:
9303 case BUILT_IN_ISGREATEREQUAL:
9304 case BUILT_IN_ISLESS:
9305 case BUILT_IN_ISLESSEQUAL:
9306 case BUILT_IN_ISLESSGREATER:
9307 case BUILT_IN_ISUNORDERED:
9308 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9310 enum tree_code code0, code1;
9311 code0 = TREE_CODE (TREE_TYPE (args[0]));
9312 code1 = TREE_CODE (TREE_TYPE (args[1]));
9313 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9314 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9315 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9317 error ("non-floating-point arguments in call to "
9318 "function %qE", fndecl);
9319 return false;
9321 return true;
9323 return false;
9325 case BUILT_IN_FPCLASSIFY:
9326 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9328 unsigned i;
9330 for (i=0; i<5; i++)
9331 if (TREE_CODE (args[i]) != INTEGER_CST)
9333 error ("non-const integer argument %u in call to function %qE",
9334 i+1, fndecl);
9335 return false;
9338 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9340 error ("non-floating-point argument in call to function %qE",
9341 fndecl);
9342 return false;
9344 return true;
9346 return false;
9348 case BUILT_IN_ASSUME_ALIGNED:
9349 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9351 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9353 error ("non-integer argument 3 in call to function %qE", fndecl);
9354 return false;
9356 return true;
9358 return false;
9360 default:
9361 return true;
9365 /* Function to help qsort sort FIELD_DECLs by name order. */
9368 field_decl_cmp (const void *x_p, const void *y_p)
9370 const tree *const x = (const tree *const) x_p;
9371 const tree *const y = (const tree *const) y_p;
9373 if (DECL_NAME (*x) == DECL_NAME (*y))
9374 /* A nontype is "greater" than a type. */
9375 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9376 if (DECL_NAME (*x) == NULL_TREE)
9377 return -1;
9378 if (DECL_NAME (*y) == NULL_TREE)
9379 return 1;
9380 if (DECL_NAME (*x) < DECL_NAME (*y))
9381 return -1;
9382 return 1;
9385 static struct {
9386 gt_pointer_operator new_value;
9387 void *cookie;
9388 } resort_data;
9390 /* This routine compares two fields like field_decl_cmp but using the
9391 pointer operator in resort_data. */
9393 static int
9394 resort_field_decl_cmp (const void *x_p, const void *y_p)
9396 const tree *const x = (const tree *const) x_p;
9397 const tree *const y = (const tree *const) y_p;
9399 if (DECL_NAME (*x) == DECL_NAME (*y))
9400 /* A nontype is "greater" than a type. */
9401 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9402 if (DECL_NAME (*x) == NULL_TREE)
9403 return -1;
9404 if (DECL_NAME (*y) == NULL_TREE)
9405 return 1;
9407 tree d1 = DECL_NAME (*x);
9408 tree d2 = DECL_NAME (*y);
9409 resort_data.new_value (&d1, resort_data.cookie);
9410 resort_data.new_value (&d2, resort_data.cookie);
9411 if (d1 < d2)
9412 return -1;
9414 return 1;
9417 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9419 void
9420 resort_sorted_fields (void *obj,
9421 void * ARG_UNUSED (orig_obj),
9422 gt_pointer_operator new_value,
9423 void *cookie)
9425 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9426 resort_data.new_value = new_value;
9427 resort_data.cookie = cookie;
9428 qsort (&sf->elts[0], sf->len, sizeof (tree),
9429 resort_field_decl_cmp);
9432 /* Subroutine of c_parse_error.
9433 Return the result of concatenating LHS and RHS. RHS is really
9434 a string literal, its first character is indicated by RHS_START and
9435 RHS_SIZE is its length (including the terminating NUL character).
9437 The caller is responsible for deleting the returned pointer. */
9439 static char *
9440 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9442 const int lhs_size = strlen (lhs);
9443 char *result = XNEWVEC (char, lhs_size + rhs_size);
9444 strncpy (result, lhs, lhs_size);
9445 strncpy (result + lhs_size, rhs_start, rhs_size);
9446 return result;
9449 /* Issue the error given by GMSGID, indicating that it occurred before
9450 TOKEN, which had the associated VALUE. */
9452 void
9453 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9454 tree value, unsigned char token_flags)
9456 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9458 char *message = NULL;
9460 if (token_type == CPP_EOF)
9461 message = catenate_messages (gmsgid, " at end of input");
9462 else if (token_type == CPP_CHAR
9463 || token_type == CPP_WCHAR
9464 || token_type == CPP_CHAR16
9465 || token_type == CPP_CHAR32)
9467 unsigned int val = TREE_INT_CST_LOW (value);
9468 const char *prefix;
9470 switch (token_type)
9472 default:
9473 prefix = "";
9474 break;
9475 case CPP_WCHAR:
9476 prefix = "L";
9477 break;
9478 case CPP_CHAR16:
9479 prefix = "u";
9480 break;
9481 case CPP_CHAR32:
9482 prefix = "U";
9483 break;
9486 if (val <= UCHAR_MAX && ISGRAPH (val))
9487 message = catenate_messages (gmsgid, " before %s'%c'");
9488 else
9489 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9491 error (message, prefix, val);
9492 free (message);
9493 message = NULL;
9495 else if (token_type == CPP_CHAR_USERDEF
9496 || token_type == CPP_WCHAR_USERDEF
9497 || token_type == CPP_CHAR16_USERDEF
9498 || token_type == CPP_CHAR32_USERDEF)
9499 message = catenate_messages (gmsgid,
9500 " before user-defined character literal");
9501 else if (token_type == CPP_STRING_USERDEF
9502 || token_type == CPP_WSTRING_USERDEF
9503 || token_type == CPP_STRING16_USERDEF
9504 || token_type == CPP_STRING32_USERDEF
9505 || token_type == CPP_UTF8STRING_USERDEF)
9506 message = catenate_messages (gmsgid, " before user-defined string literal");
9507 else if (token_type == CPP_STRING
9508 || token_type == CPP_WSTRING
9509 || token_type == CPP_STRING16
9510 || token_type == CPP_STRING32
9511 || token_type == CPP_UTF8STRING)
9512 message = catenate_messages (gmsgid, " before string constant");
9513 else if (token_type == CPP_NUMBER)
9514 message = catenate_messages (gmsgid, " before numeric constant");
9515 else if (token_type == CPP_NAME)
9517 message = catenate_messages (gmsgid, " before %qE");
9518 error (message, value);
9519 free (message);
9520 message = NULL;
9522 else if (token_type == CPP_PRAGMA)
9523 message = catenate_messages (gmsgid, " before %<#pragma%>");
9524 else if (token_type == CPP_PRAGMA_EOL)
9525 message = catenate_messages (gmsgid, " before end of line");
9526 else if (token_type == CPP_DECLTYPE)
9527 message = catenate_messages (gmsgid, " before %<decltype%>");
9528 else if (token_type < N_TTYPES)
9530 message = catenate_messages (gmsgid, " before %qs token");
9531 error (message, cpp_type2name (token_type, token_flags));
9532 free (message);
9533 message = NULL;
9535 else
9536 error (gmsgid);
9538 if (message)
9540 error (message);
9541 free (message);
9543 #undef catenate_messages
9546 /* Mapping for cpp message reasons to the options that enable them. */
9548 struct reason_option_codes_t
9550 const int reason; /* cpplib message reason. */
9551 const int option_code; /* gcc option that controls this message. */
9554 static const struct reason_option_codes_t option_codes[] = {
9555 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9556 {CPP_W_COMMENTS, OPT_Wcomment},
9557 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9558 {CPP_W_MULTICHAR, OPT_Wmultichar},
9559 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9560 {CPP_W_LONG_LONG, OPT_Wlong_long},
9561 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9562 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9563 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9564 {CPP_W_UNDEF, OPT_Wundef},
9565 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9566 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9567 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9568 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9569 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9570 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9571 {CPP_W_DATE_TIME, OPT_Wdate_time},
9572 {CPP_W_NONE, 0}
9575 /* Return the gcc option code associated with the reason for a cpp
9576 message, or 0 if none. */
9578 static int
9579 c_option_controlling_cpp_error (int reason)
9581 const struct reason_option_codes_t *entry;
9583 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9585 if (entry->reason == reason)
9586 return entry->option_code;
9588 return 0;
9591 /* Callback from cpp_error for PFILE to print diagnostics from the
9592 preprocessor. The diagnostic is of type LEVEL, with REASON set
9593 to the reason code if LEVEL is represents a warning, at location
9594 LOCATION unless this is after lexing and the compiler's location
9595 should be used instead, with column number possibly overridden by
9596 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9597 the arguments. Returns true if a diagnostic was emitted, false
9598 otherwise. */
9600 bool
9601 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9602 location_t location, unsigned int column_override,
9603 const char *msg, va_list *ap)
9605 diagnostic_info diagnostic;
9606 diagnostic_t dlevel;
9607 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9608 bool ret;
9610 switch (level)
9612 case CPP_DL_WARNING_SYSHDR:
9613 if (flag_no_output)
9614 return false;
9615 global_dc->dc_warn_system_headers = 1;
9616 /* Fall through. */
9617 case CPP_DL_WARNING:
9618 if (flag_no_output)
9619 return false;
9620 dlevel = DK_WARNING;
9621 break;
9622 case CPP_DL_PEDWARN:
9623 if (flag_no_output && !flag_pedantic_errors)
9624 return false;
9625 dlevel = DK_PEDWARN;
9626 break;
9627 case CPP_DL_ERROR:
9628 dlevel = DK_ERROR;
9629 break;
9630 case CPP_DL_ICE:
9631 dlevel = DK_ICE;
9632 break;
9633 case CPP_DL_NOTE:
9634 dlevel = DK_NOTE;
9635 break;
9636 case CPP_DL_FATAL:
9637 dlevel = DK_FATAL;
9638 break;
9639 default:
9640 gcc_unreachable ();
9642 if (done_lexing)
9643 location = input_location;
9644 diagnostic_set_info_translated (&diagnostic, msg, ap,
9645 location, dlevel);
9646 if (column_override)
9647 diagnostic_override_column (&diagnostic, column_override);
9648 diagnostic_override_option_index (&diagnostic,
9649 c_option_controlling_cpp_error (reason));
9650 ret = report_diagnostic (&diagnostic);
9651 if (level == CPP_DL_WARNING_SYSHDR)
9652 global_dc->dc_warn_system_headers = save_warn_system_headers;
9653 return ret;
9656 /* Convert a character from the host to the target execution character
9657 set. cpplib handles this, mostly. */
9659 HOST_WIDE_INT
9660 c_common_to_target_charset (HOST_WIDE_INT c)
9662 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9663 zero-extended under -fno-signed-char. cpplib insists that characters
9664 and character constants are always unsigned. Hence we must convert
9665 back and forth. */
9666 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9668 uc = cpp_host_to_exec_charset (parse_in, uc);
9670 if (flag_signed_char)
9671 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9672 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9673 else
9674 return uc;
9677 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9678 references with an INDIRECT_REF of a constant at the bottom; much like the
9679 traditional rendering of offsetof as a macro. Return the folded result. */
9681 tree
9682 fold_offsetof_1 (tree expr)
9684 tree base, off, t;
9686 switch (TREE_CODE (expr))
9688 case ERROR_MARK:
9689 return expr;
9691 case VAR_DECL:
9692 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9693 return error_mark_node;
9695 case CALL_EXPR:
9696 case TARGET_EXPR:
9697 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9698 return error_mark_node;
9700 case NOP_EXPR:
9701 case INDIRECT_REF:
9702 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9704 error ("cannot apply %<offsetof%> to a non constant address");
9705 return error_mark_node;
9707 return TREE_OPERAND (expr, 0);
9709 case COMPONENT_REF:
9710 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9711 if (base == error_mark_node)
9712 return base;
9714 t = TREE_OPERAND (expr, 1);
9715 if (DECL_C_BIT_FIELD (t))
9717 error ("attempt to take address of bit-field structure "
9718 "member %qD", t);
9719 return error_mark_node;
9721 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9722 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9724 / BITS_PER_UNIT));
9725 break;
9727 case ARRAY_REF:
9728 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9729 if (base == error_mark_node)
9730 return base;
9732 t = TREE_OPERAND (expr, 1);
9734 /* Check if the offset goes beyond the upper bound of the array. */
9735 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9737 tree upbound = array_ref_up_bound (expr);
9738 if (upbound != NULL_TREE
9739 && TREE_CODE (upbound) == INTEGER_CST
9740 && !tree_int_cst_equal (upbound,
9741 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9743 upbound = size_binop (PLUS_EXPR, upbound,
9744 build_int_cst (TREE_TYPE (upbound), 1));
9745 if (tree_int_cst_lt (upbound, t))
9747 tree v;
9749 for (v = TREE_OPERAND (expr, 0);
9750 TREE_CODE (v) == COMPONENT_REF;
9751 v = TREE_OPERAND (v, 0))
9752 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9753 == RECORD_TYPE)
9755 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9756 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9757 if (TREE_CODE (fld_chain) == FIELD_DECL)
9758 break;
9760 if (fld_chain)
9761 break;
9763 /* Don't warn if the array might be considered a poor
9764 man's flexible array member with a very permissive
9765 definition thereof. */
9766 if (TREE_CODE (v) == ARRAY_REF
9767 || TREE_CODE (v) == COMPONENT_REF)
9768 warning (OPT_Warray_bounds,
9769 "index %E denotes an offset "
9770 "greater than size of %qT",
9771 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9776 t = convert (sizetype, t);
9777 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9778 break;
9780 case COMPOUND_EXPR:
9781 /* Handle static members of volatile structs. */
9782 t = TREE_OPERAND (expr, 1);
9783 gcc_assert (TREE_CODE (t) == VAR_DECL);
9784 return fold_offsetof_1 (t);
9786 default:
9787 gcc_unreachable ();
9790 return fold_build_pointer_plus (base, off);
9793 /* Likewise, but convert it to the return type of offsetof. */
9795 tree
9796 fold_offsetof (tree expr)
9798 return convert (size_type_node, fold_offsetof_1 (expr));
9801 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9802 expression, because B will always be true. */
9804 void
9805 warn_for_omitted_condop (location_t location, tree cond)
9807 if (truth_value_p (TREE_CODE (cond)))
9808 warning_at (location, OPT_Wparentheses,
9809 "the omitted middle operand in ?: will always be %<true%>, "
9810 "suggest explicit middle operand");
9813 /* Give an error for storing into ARG, which is 'const'. USE indicates
9814 how ARG was being used. */
9816 void
9817 readonly_error (tree arg, enum lvalue_use use)
9819 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9820 || use == lv_asm);
9821 /* Using this macro rather than (for example) arrays of messages
9822 ensures that all the format strings are checked at compile
9823 time. */
9824 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9825 : (use == lv_increment ? (I) \
9826 : (use == lv_decrement ? (D) : (AS))))
9827 if (TREE_CODE (arg) == COMPONENT_REF)
9829 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9830 error (READONLY_MSG (G_("assignment of member "
9831 "%qD in read-only object"),
9832 G_("increment of member "
9833 "%qD in read-only object"),
9834 G_("decrement of member "
9835 "%qD in read-only object"),
9836 G_("member %qD in read-only object "
9837 "used as %<asm%> output")),
9838 TREE_OPERAND (arg, 1));
9839 else
9840 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9841 G_("increment of read-only member %qD"),
9842 G_("decrement of read-only member %qD"),
9843 G_("read-only member %qD used as %<asm%> output")),
9844 TREE_OPERAND (arg, 1));
9846 else if (TREE_CODE (arg) == VAR_DECL)
9847 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9848 G_("increment of read-only variable %qD"),
9849 G_("decrement of read-only variable %qD"),
9850 G_("read-only variable %qD used as %<asm%> output")),
9851 arg);
9852 else if (TREE_CODE (arg) == PARM_DECL)
9853 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9854 G_("increment of read-only parameter %qD"),
9855 G_("decrement of read-only parameter %qD"),
9856 G_("read-only parameter %qD use as %<asm%> output")),
9857 arg);
9858 else if (TREE_CODE (arg) == RESULT_DECL)
9860 gcc_assert (c_dialect_cxx ());
9861 error (READONLY_MSG (G_("assignment of "
9862 "read-only named return value %qD"),
9863 G_("increment of "
9864 "read-only named return value %qD"),
9865 G_("decrement of "
9866 "read-only named return value %qD"),
9867 G_("read-only named return value %qD "
9868 "used as %<asm%>output")),
9869 arg);
9871 else if (TREE_CODE (arg) == FUNCTION_DECL)
9872 error (READONLY_MSG (G_("assignment of function %qD"),
9873 G_("increment of function %qD"),
9874 G_("decrement of function %qD"),
9875 G_("function %qD used as %<asm%> output")),
9876 arg);
9877 else
9878 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9879 G_("increment of read-only location %qE"),
9880 G_("decrement of read-only location %qE"),
9881 G_("read-only location %qE used as %<asm%> output")),
9882 arg);
9885 /* Print an error message for an invalid lvalue. USE says
9886 how the lvalue is being used and so selects the error message. LOC
9887 is the location for the error. */
9889 void
9890 lvalue_error (location_t loc, enum lvalue_use use)
9892 switch (use)
9894 case lv_assign:
9895 error_at (loc, "lvalue required as left operand of assignment");
9896 break;
9897 case lv_increment:
9898 error_at (loc, "lvalue required as increment operand");
9899 break;
9900 case lv_decrement:
9901 error_at (loc, "lvalue required as decrement operand");
9902 break;
9903 case lv_addressof:
9904 error_at (loc, "lvalue required as unary %<&%> operand");
9905 break;
9906 case lv_asm:
9907 error_at (loc, "lvalue required in asm statement");
9908 break;
9909 default:
9910 gcc_unreachable ();
9914 /* Print an error message for an invalid indirection of type TYPE.
9915 ERRSTRING is the name of the operator for the indirection. */
9917 void
9918 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9920 switch (errstring)
9922 case RO_NULL:
9923 gcc_assert (c_dialect_cxx ());
9924 error_at (loc, "invalid type argument (have %qT)", type);
9925 break;
9926 case RO_ARRAY_INDEXING:
9927 error_at (loc,
9928 "invalid type argument of array indexing (have %qT)",
9929 type);
9930 break;
9931 case RO_UNARY_STAR:
9932 error_at (loc,
9933 "invalid type argument of unary %<*%> (have %qT)",
9934 type);
9935 break;
9936 case RO_ARROW:
9937 error_at (loc,
9938 "invalid type argument of %<->%> (have %qT)",
9939 type);
9940 break;
9941 case RO_ARROW_STAR:
9942 error_at (loc,
9943 "invalid type argument of %<->*%> (have %qT)",
9944 type);
9945 break;
9946 case RO_IMPLICIT_CONVERSION:
9947 error_at (loc,
9948 "invalid type argument of implicit conversion (have %qT)",
9949 type);
9950 break;
9951 default:
9952 gcc_unreachable ();
9956 /* *PTYPE is an incomplete array. Complete it with a domain based on
9957 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9958 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9959 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9962 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9964 tree maxindex, type, main_type, elt, unqual_elt;
9965 int failure = 0, quals;
9966 hashval_t hashcode = 0;
9967 bool overflow_p = false;
9969 maxindex = size_zero_node;
9970 if (initial_value)
9972 if (TREE_CODE (initial_value) == STRING_CST)
9974 int eltsize
9975 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9976 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9978 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9980 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9982 if (vec_safe_is_empty (v))
9984 if (pedantic)
9985 failure = 3;
9986 maxindex = ssize_int (-1);
9988 else
9990 tree curindex;
9991 unsigned HOST_WIDE_INT cnt;
9992 constructor_elt *ce;
9993 bool fold_p = false;
9995 if ((*v)[0].index)
9996 maxindex = (*v)[0].index, fold_p = true;
9998 curindex = maxindex;
10000 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10002 bool curfold_p = false;
10003 if (ce->index)
10004 curindex = ce->index, curfold_p = true;
10005 else
10007 if (fold_p)
10009 /* Since we treat size types now as ordinary
10010 unsigned types, we need an explicit overflow
10011 check. */
10012 tree orig = curindex;
10013 curindex = fold_convert (sizetype, curindex);
10014 overflow_p |= tree_int_cst_lt (curindex, orig);
10016 curindex = size_binop (PLUS_EXPR, curindex,
10017 size_one_node);
10019 if (tree_int_cst_lt (maxindex, curindex))
10020 maxindex = curindex, fold_p = curfold_p;
10022 if (fold_p)
10024 tree orig = maxindex;
10025 maxindex = fold_convert (sizetype, maxindex);
10026 overflow_p |= tree_int_cst_lt (maxindex, orig);
10030 else
10032 /* Make an error message unless that happened already. */
10033 if (initial_value != error_mark_node)
10034 failure = 1;
10037 else
10039 failure = 2;
10040 if (!do_default)
10041 return failure;
10044 type = *ptype;
10045 elt = TREE_TYPE (type);
10046 quals = TYPE_QUALS (strip_array_types (elt));
10047 if (quals == 0)
10048 unqual_elt = elt;
10049 else
10050 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10052 /* Using build_distinct_type_copy and modifying things afterward instead
10053 of using build_array_type to create a new type preserves all of the
10054 TYPE_LANG_FLAG_? bits that the front end may have set. */
10055 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10056 TREE_TYPE (main_type) = unqual_elt;
10057 TYPE_DOMAIN (main_type)
10058 = build_range_type (TREE_TYPE (maxindex),
10059 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10060 layout_type (main_type);
10062 /* Make sure we have the canonical MAIN_TYPE. */
10063 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10064 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10065 hashcode);
10066 main_type = type_hash_canon (hashcode, main_type);
10068 /* Fix the canonical type. */
10069 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10070 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10071 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10072 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10073 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10074 != TYPE_DOMAIN (main_type)))
10075 TYPE_CANONICAL (main_type)
10076 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10077 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10078 else
10079 TYPE_CANONICAL (main_type) = main_type;
10081 if (quals == 0)
10082 type = main_type;
10083 else
10084 type = c_build_qualified_type (main_type, quals);
10086 if (COMPLETE_TYPE_P (type)
10087 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10088 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10090 error ("size of array is too large");
10091 /* If we proceed with the array type as it is, we'll eventually
10092 crash in tree_low_cst(). */
10093 type = error_mark_node;
10096 *ptype = type;
10097 return failure;
10100 /* Like c_mark_addressable but don't check register qualifier. */
10101 void
10102 c_common_mark_addressable_vec (tree t)
10104 while (handled_component_p (t))
10105 t = TREE_OPERAND (t, 0);
10106 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10107 return;
10108 TREE_ADDRESSABLE (t) = 1;
10113 /* Used to help initialize the builtin-types.def table. When a type of
10114 the correct size doesn't exist, use error_mark_node instead of NULL.
10115 The later results in segfaults even when a decl using the type doesn't
10116 get invoked. */
10118 tree
10119 builtin_type_for_size (int size, bool unsignedp)
10121 tree type = c_common_type_for_size (size, unsignedp);
10122 return type ? type : error_mark_node;
10125 /* A helper function for resolve_overloaded_builtin in resolving the
10126 overloaded __sync_ builtins. Returns a positive power of 2 if the
10127 first operand of PARAMS is a pointer to a supported data type.
10128 Returns 0 if an error is encountered. */
10130 static int
10131 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10133 tree type;
10134 int size;
10136 if (!params)
10138 error ("too few arguments to function %qE", function);
10139 return 0;
10142 type = TREE_TYPE ((*params)[0]);
10143 if (TREE_CODE (type) != POINTER_TYPE)
10144 goto incompatible;
10146 type = TREE_TYPE (type);
10147 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10148 goto incompatible;
10150 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
10151 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10152 return size;
10154 incompatible:
10155 error ("incompatible type for argument %d of %qE", 1, function);
10156 return 0;
10159 /* A helper function for resolve_overloaded_builtin. Adds casts to
10160 PARAMS to make arguments match up with those of FUNCTION. Drops
10161 the variadic arguments at the end. Returns false if some error
10162 was encountered; true on success. */
10164 static bool
10165 sync_resolve_params (location_t loc, tree orig_function, tree function,
10166 vec<tree, va_gc> *params, bool orig_format)
10168 function_args_iterator iter;
10169 tree ptype;
10170 unsigned int parmnum;
10172 function_args_iter_init (&iter, TREE_TYPE (function));
10173 /* We've declared the implementation functions to use "volatile void *"
10174 as the pointer parameter, so we shouldn't get any complaints from the
10175 call to check_function_arguments what ever type the user used. */
10176 function_args_iter_next (&iter);
10177 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10178 ptype = TYPE_MAIN_VARIANT (ptype);
10180 /* For the rest of the values, we need to cast these to FTYPE, so that we
10181 don't get warnings for passing pointer types, etc. */
10182 parmnum = 0;
10183 while (1)
10185 tree val, arg_type;
10187 arg_type = function_args_iter_cond (&iter);
10188 /* XXX void_type_node belies the abstraction. */
10189 if (arg_type == void_type_node)
10190 break;
10192 ++parmnum;
10193 if (params->length () <= parmnum)
10195 error_at (loc, "too few arguments to function %qE", orig_function);
10196 return false;
10199 /* Only convert parameters if arg_type is unsigned integer type with
10200 new format sync routines, i.e. don't attempt to convert pointer
10201 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10202 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10203 kinds). */
10204 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10206 /* Ideally for the first conversion we'd use convert_for_assignment
10207 so that we get warnings for anything that doesn't match the pointer
10208 type. This isn't portable across the C and C++ front ends atm. */
10209 val = (*params)[parmnum];
10210 val = convert (ptype, val);
10211 val = convert (arg_type, val);
10212 (*params)[parmnum] = val;
10215 function_args_iter_next (&iter);
10218 /* __atomic routines are not variadic. */
10219 if (!orig_format && params->length () != parmnum + 1)
10221 error_at (loc, "too many arguments to function %qE", orig_function);
10222 return false;
10225 /* The definition of these primitives is variadic, with the remaining
10226 being "an optional list of variables protected by the memory barrier".
10227 No clue what that's supposed to mean, precisely, but we consider all
10228 call-clobbered variables to be protected so we're safe. */
10229 params->truncate (parmnum + 1);
10231 return true;
10234 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10235 RESULT to make it match the type of the first pointer argument in
10236 PARAMS. */
10238 static tree
10239 sync_resolve_return (tree first_param, tree result, bool orig_format)
10241 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10242 tree rtype = TREE_TYPE (result);
10243 ptype = TYPE_MAIN_VARIANT (ptype);
10245 /* New format doesn't require casting unless the types are the same size. */
10246 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10247 return convert (ptype, result);
10248 else
10249 return result;
10252 /* This function verifies the PARAMS to generic atomic FUNCTION.
10253 It returns the size if all the parameters are the same size, otherwise
10254 0 is returned if the parameters are invalid. */
10256 static int
10257 get_atomic_generic_size (location_t loc, tree function,
10258 vec<tree, va_gc> *params)
10260 unsigned int n_param;
10261 unsigned int n_model;
10262 unsigned int x;
10263 int size_0;
10264 tree type_0;
10266 /* Determine the parameter makeup. */
10267 switch (DECL_FUNCTION_CODE (function))
10269 case BUILT_IN_ATOMIC_EXCHANGE:
10270 n_param = 4;
10271 n_model = 1;
10272 break;
10273 case BUILT_IN_ATOMIC_LOAD:
10274 case BUILT_IN_ATOMIC_STORE:
10275 n_param = 3;
10276 n_model = 1;
10277 break;
10278 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10279 n_param = 6;
10280 n_model = 2;
10281 break;
10282 default:
10283 gcc_unreachable ();
10286 if (vec_safe_length (params) != n_param)
10288 error_at (loc, "incorrect number of arguments to function %qE", function);
10289 return 0;
10292 /* Get type of first parameter, and determine its size. */
10293 type_0 = TREE_TYPE ((*params)[0]);
10294 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10296 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10297 function);
10298 return 0;
10301 /* Types must be compile time constant sizes. */
10302 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10304 error_at (loc,
10305 "argument 1 of %qE must be a pointer to a constant size type",
10306 function);
10307 return 0;
10310 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10312 /* Zero size objects are not allowed. */
10313 if (size_0 == 0)
10315 error_at (loc,
10316 "argument 1 of %qE must be a pointer to a nonzero size object",
10317 function);
10318 return 0;
10321 /* Check each other parameter is a pointer and the same size. */
10322 for (x = 0; x < n_param - n_model; x++)
10324 int size;
10325 tree type = TREE_TYPE ((*params)[x]);
10326 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10327 if (n_param == 6 && x == 3)
10328 continue;
10329 if (!POINTER_TYPE_P (type))
10331 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10332 function);
10333 return 0;
10335 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10336 if (size != size_0)
10338 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10339 function);
10340 return 0;
10344 /* Check memory model parameters for validity. */
10345 for (x = n_param - n_model ; x < n_param; x++)
10347 tree p = (*params)[x];
10348 if (TREE_CODE (p) == INTEGER_CST)
10350 int i = tree_low_cst (p, 1);
10351 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10353 warning_at (loc, OPT_Winvalid_memory_model,
10354 "invalid memory model argument %d of %qE", x + 1,
10355 function);
10358 else
10359 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10361 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10362 function);
10363 return 0;
10367 return size_0;
10371 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10372 at the beginning of the parameter list PARAMS representing the size of the
10373 objects. This is to match the library ABI requirement. LOC is the location
10374 of the function call.
10375 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10376 returned to allow the external call to be constructed. */
10378 static tree
10379 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10380 vec<tree, va_gc> *params)
10382 tree size_node;
10384 /* Insert a SIZE_T parameter as the first param. If there isn't
10385 enough space, allocate a new vector and recursively re-build with that. */
10386 if (!params->space (1))
10388 unsigned int z, len;
10389 vec<tree, va_gc> *v;
10390 tree f;
10392 len = params->length ();
10393 vec_alloc (v, len + 1);
10394 for (z = 0; z < len; z++)
10395 v->quick_push ((*params)[z]);
10396 f = build_function_call_vec (loc, function, v, NULL);
10397 vec_free (v);
10398 return f;
10401 /* Add the size parameter and leave as a function call for processing. */
10402 size_node = build_int_cst (size_type_node, n);
10403 params->quick_insert (0, size_node);
10404 return NULL_TREE;
10408 /* Return whether atomic operations for naturally aligned N-byte
10409 arguments are supported, whether inline or through libatomic. */
10410 static bool
10411 atomic_size_supported_p (int n)
10413 switch (n)
10415 case 1:
10416 case 2:
10417 case 4:
10418 case 8:
10419 return true;
10421 case 16:
10422 return targetm.scalar_mode_supported_p (TImode);
10424 default:
10425 return false;
10429 /* This will process an __atomic_exchange function call, determine whether it
10430 needs to be mapped to the _N variation, or turned into a library call.
10431 LOC is the location of the builtin call.
10432 FUNCTION is the DECL that has been invoked;
10433 PARAMS is the argument list for the call. The return value is non-null
10434 TRUE is returned if it is translated into the proper format for a call to the
10435 external library, and NEW_RETURN is set the tree for that function.
10436 FALSE is returned if processing for the _N variation is required, and
10437 NEW_RETURN is set to the the return value the result is copied into. */
10438 static bool
10439 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10440 vec<tree, va_gc> *params, tree *new_return)
10442 tree p0, p1, p2, p3;
10443 tree I_type, I_type_ptr;
10444 int n = get_atomic_generic_size (loc, function, params);
10446 /* Size of 0 is an error condition. */
10447 if (n == 0)
10449 *new_return = error_mark_node;
10450 return true;
10453 /* If not a lock-free size, change to the library generic format. */
10454 if (!atomic_size_supported_p (n))
10456 *new_return = add_atomic_size_parameter (n, loc, function, params);
10457 return true;
10460 /* Otherwise there is a lockfree match, transform the call from:
10461 void fn(T* mem, T* desired, T* return, model)
10462 into
10463 *return = (T) (fn (In* mem, (In) *desired, model)) */
10465 p0 = (*params)[0];
10466 p1 = (*params)[1];
10467 p2 = (*params)[2];
10468 p3 = (*params)[3];
10470 /* Create pointer to appropriate size. */
10471 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10472 I_type_ptr = build_pointer_type (I_type);
10474 /* Convert object pointer to required type. */
10475 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10476 (*params)[0] = p0;
10477 /* Convert new value to required type, and dereference it. */
10478 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10479 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10480 (*params)[1] = p1;
10482 /* Move memory model to the 3rd position, and end param list. */
10483 (*params)[2] = p3;
10484 params->truncate (3);
10486 /* Convert return pointer and dereference it for later assignment. */
10487 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10489 return false;
10493 /* This will process an __atomic_compare_exchange function call, determine
10494 whether it needs to be mapped to the _N variation, or turned into a lib call.
10495 LOC is the location of the builtin call.
10496 FUNCTION is the DECL that has been invoked;
10497 PARAMS is the argument list for the call. The return value is non-null
10498 TRUE is returned if it is translated into the proper format for a call to the
10499 external library, and NEW_RETURN is set the tree for that function.
10500 FALSE is returned if processing for the _N variation is required. */
10502 static bool
10503 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10504 vec<tree, va_gc> *params,
10505 tree *new_return)
10507 tree p0, p1, p2;
10508 tree I_type, I_type_ptr;
10509 int n = get_atomic_generic_size (loc, function, params);
10511 /* Size of 0 is an error condition. */
10512 if (n == 0)
10514 *new_return = error_mark_node;
10515 return true;
10518 /* If not a lock-free size, change to the library generic format. */
10519 if (!atomic_size_supported_p (n))
10521 /* The library generic format does not have the weak parameter, so
10522 remove it from the param list. Since a parameter has been removed,
10523 we can be sure that there is room for the SIZE_T parameter, meaning
10524 there will not be a recursive rebuilding of the parameter list, so
10525 there is no danger this will be done twice. */
10526 if (n > 0)
10528 (*params)[3] = (*params)[4];
10529 (*params)[4] = (*params)[5];
10530 params->truncate (5);
10532 *new_return = add_atomic_size_parameter (n, loc, function, params);
10533 return true;
10536 /* Otherwise, there is a match, so the call needs to be transformed from:
10537 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10538 into
10539 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10541 p0 = (*params)[0];
10542 p1 = (*params)[1];
10543 p2 = (*params)[2];
10545 /* Create pointer to appropriate size. */
10546 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10547 I_type_ptr = build_pointer_type (I_type);
10549 /* Convert object pointer to required type. */
10550 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10551 (*params)[0] = p0;
10553 /* Convert expected pointer to required type. */
10554 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10555 (*params)[1] = p1;
10557 /* Convert desired value to required type, and dereference it. */
10558 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10559 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10560 (*params)[2] = p2;
10562 /* The rest of the parameters are fine. NULL means no special return value
10563 processing.*/
10564 *new_return = NULL;
10565 return false;
10569 /* This will process an __atomic_load function call, determine whether it
10570 needs to be mapped to the _N variation, or turned into a library call.
10571 LOC is the location of the builtin call.
10572 FUNCTION is the DECL that has been invoked;
10573 PARAMS is the argument list for the call. The return value is non-null
10574 TRUE is returned if it is translated into the proper format for a call to the
10575 external library, and NEW_RETURN is set the tree for that function.
10576 FALSE is returned if processing for the _N variation is required, and
10577 NEW_RETURN is set to the the return value the result is copied into. */
10579 static bool
10580 resolve_overloaded_atomic_load (location_t loc, tree function,
10581 vec<tree, va_gc> *params, tree *new_return)
10583 tree p0, p1, p2;
10584 tree I_type, I_type_ptr;
10585 int n = get_atomic_generic_size (loc, function, params);
10587 /* Size of 0 is an error condition. */
10588 if (n == 0)
10590 *new_return = error_mark_node;
10591 return true;
10594 /* If not a lock-free size, change to the library generic format. */
10595 if (!atomic_size_supported_p (n))
10597 *new_return = add_atomic_size_parameter (n, loc, function, params);
10598 return true;
10601 /* Otherwise, there is a match, so the call needs to be transformed from:
10602 void fn(T* mem, T* return, model)
10603 into
10604 *return = (T) (fn ((In *) mem, model)) */
10606 p0 = (*params)[0];
10607 p1 = (*params)[1];
10608 p2 = (*params)[2];
10610 /* Create pointer to appropriate size. */
10611 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10612 I_type_ptr = build_pointer_type (I_type);
10614 /* Convert object pointer to required type. */
10615 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10616 (*params)[0] = p0;
10618 /* Move memory model to the 2nd position, and end param list. */
10619 (*params)[1] = p2;
10620 params->truncate (2);
10622 /* Convert return pointer and dereference it for later assignment. */
10623 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10625 return false;
10629 /* This will process an __atomic_store function call, determine whether it
10630 needs to be mapped to the _N variation, or turned into a library call.
10631 LOC is the location of the builtin call.
10632 FUNCTION is the DECL that has been invoked;
10633 PARAMS is the argument list for the call. The return value is non-null
10634 TRUE is returned if it is translated into the proper format for a call to the
10635 external library, and NEW_RETURN is set the tree for that function.
10636 FALSE is returned if processing for the _N variation is required, and
10637 NEW_RETURN is set to the the return value the result is copied into. */
10639 static bool
10640 resolve_overloaded_atomic_store (location_t loc, tree function,
10641 vec<tree, va_gc> *params, tree *new_return)
10643 tree p0, p1;
10644 tree I_type, I_type_ptr;
10645 int n = get_atomic_generic_size (loc, function, params);
10647 /* Size of 0 is an error condition. */
10648 if (n == 0)
10650 *new_return = error_mark_node;
10651 return true;
10654 /* If not a lock-free size, change to the library generic format. */
10655 if (!atomic_size_supported_p (n))
10657 *new_return = add_atomic_size_parameter (n, loc, function, params);
10658 return true;
10661 /* Otherwise, there is a match, so the call needs to be transformed from:
10662 void fn(T* mem, T* value, model)
10663 into
10664 fn ((In *) mem, (In) *value, model) */
10666 p0 = (*params)[0];
10667 p1 = (*params)[1];
10669 /* Create pointer to appropriate size. */
10670 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10671 I_type_ptr = build_pointer_type (I_type);
10673 /* Convert object pointer to required type. */
10674 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10675 (*params)[0] = p0;
10677 /* Convert new value to required type, and dereference it. */
10678 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10679 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10680 (*params)[1] = p1;
10682 /* The memory model is in the right spot already. Return is void. */
10683 *new_return = NULL_TREE;
10685 return false;
10689 /* Some builtin functions are placeholders for other expressions. This
10690 function should be called immediately after parsing the call expression
10691 before surrounding code has committed to the type of the expression.
10693 LOC is the location of the builtin call.
10695 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10696 PARAMS is the argument list for the call. The return value is non-null
10697 when expansion is complete, and null if normal processing should
10698 continue. */
10700 tree
10701 resolve_overloaded_builtin (location_t loc, tree function,
10702 vec<tree, va_gc> *params)
10704 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10705 bool orig_format = true;
10706 tree new_return = NULL_TREE;
10708 switch (DECL_BUILT_IN_CLASS (function))
10710 case BUILT_IN_NORMAL:
10711 break;
10712 case BUILT_IN_MD:
10713 if (targetm.resolve_overloaded_builtin)
10714 return targetm.resolve_overloaded_builtin (loc, function, params);
10715 else
10716 return NULL_TREE;
10717 default:
10718 return NULL_TREE;
10721 /* Handle BUILT_IN_NORMAL here. */
10722 switch (orig_code)
10724 case BUILT_IN_ATOMIC_EXCHANGE:
10725 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10726 case BUILT_IN_ATOMIC_LOAD:
10727 case BUILT_IN_ATOMIC_STORE:
10729 /* Handle these 4 together so that they can fall through to the next
10730 case if the call is transformed to an _N variant. */
10731 switch (orig_code)
10733 case BUILT_IN_ATOMIC_EXCHANGE:
10735 if (resolve_overloaded_atomic_exchange (loc, function, params,
10736 &new_return))
10737 return new_return;
10738 /* Change to the _N variant. */
10739 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10740 break;
10743 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10745 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10746 params,
10747 &new_return))
10748 return new_return;
10749 /* Change to the _N variant. */
10750 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10751 break;
10753 case BUILT_IN_ATOMIC_LOAD:
10755 if (resolve_overloaded_atomic_load (loc, function, params,
10756 &new_return))
10757 return new_return;
10758 /* Change to the _N variant. */
10759 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10760 break;
10762 case BUILT_IN_ATOMIC_STORE:
10764 if (resolve_overloaded_atomic_store (loc, function, params,
10765 &new_return))
10766 return new_return;
10767 /* Change to the _N variant. */
10768 orig_code = BUILT_IN_ATOMIC_STORE_N;
10769 break;
10771 default:
10772 gcc_unreachable ();
10774 /* Fallthrough to the normal processing. */
10776 case BUILT_IN_ATOMIC_EXCHANGE_N:
10777 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10778 case BUILT_IN_ATOMIC_LOAD_N:
10779 case BUILT_IN_ATOMIC_STORE_N:
10780 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10781 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10782 case BUILT_IN_ATOMIC_AND_FETCH_N:
10783 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10784 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10785 case BUILT_IN_ATOMIC_OR_FETCH_N:
10786 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10787 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10788 case BUILT_IN_ATOMIC_FETCH_AND_N:
10789 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10790 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10791 case BUILT_IN_ATOMIC_FETCH_OR_N:
10793 orig_format = false;
10794 /* Fallthru for parameter processing. */
10796 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10797 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10798 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10799 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10800 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10801 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10802 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10803 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10804 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10805 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10806 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10807 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10808 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10809 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10810 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10811 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10813 int n = sync_resolve_size (function, params);
10814 tree new_function, first_param, result;
10815 enum built_in_function fncode;
10817 if (n == 0)
10818 return error_mark_node;
10820 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10821 new_function = builtin_decl_explicit (fncode);
10822 if (!sync_resolve_params (loc, function, new_function, params,
10823 orig_format))
10824 return error_mark_node;
10826 first_param = (*params)[0];
10827 result = build_function_call_vec (loc, new_function, params, NULL);
10828 if (result == error_mark_node)
10829 return result;
10830 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10831 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10832 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10833 result = sync_resolve_return (first_param, result, orig_format);
10835 /* If new_return is set, assign function to that expr and cast the
10836 result to void since the generic interface returned void. */
10837 if (new_return)
10839 /* Cast function result from I{1,2,4,8,16} to the required type. */
10840 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10841 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10842 result);
10843 TREE_SIDE_EFFECTS (result) = 1;
10844 protected_set_expr_location (result, loc);
10845 result = convert (void_type_node, result);
10847 return result;
10850 default:
10851 return NULL_TREE;
10855 /* vector_types_compatible_elements_p is used in type checks of vectors
10856 values used as operands of binary operators. Where it returns true, and
10857 the other checks of the caller succeed (being vector types in he first
10858 place, and matching number of elements), we can just treat the types
10859 as essentially the same.
10860 Contrast with vector_targets_convertible_p, which is used for vector
10861 pointer types, and vector_types_convertible_p, which will allow
10862 language-specific matches under the control of flag_lax_vector_conversions,
10863 and might still require a conversion. */
10864 /* True if vector types T1 and T2 can be inputs to the same binary
10865 operator without conversion.
10866 We don't check the overall vector size here because some of our callers
10867 want to give different error messages when the vectors are compatible
10868 except for the element count. */
10870 bool
10871 vector_types_compatible_elements_p (tree t1, tree t2)
10873 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10874 t1 = TREE_TYPE (t1);
10875 t2 = TREE_TYPE (t2);
10877 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10879 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10880 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10881 || c2 == FIXED_POINT_TYPE));
10883 t1 = c_common_signed_type (t1);
10884 t2 = c_common_signed_type (t2);
10885 /* Equality works here because c_common_signed_type uses
10886 TYPE_MAIN_VARIANT. */
10887 if (t1 == t2)
10888 return true;
10889 if (opaque && c1 == c2
10890 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10891 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10892 return true;
10893 return false;
10896 /* Check for missing format attributes on function pointers. LTYPE is
10897 the new type or left-hand side type. RTYPE is the old type or
10898 right-hand side type. Returns TRUE if LTYPE is missing the desired
10899 attribute. */
10901 bool
10902 check_missing_format_attribute (tree ltype, tree rtype)
10904 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10905 tree ra;
10907 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10908 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10909 break;
10910 if (ra)
10912 tree la;
10913 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10914 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10915 break;
10916 return !la;
10918 else
10919 return false;
10922 /* Subscripting with type char is likely to lose on a machine where
10923 chars are signed. So warn on any machine, but optionally. Don't
10924 warn for unsigned char since that type is safe. Don't warn for
10925 signed char because anyone who uses that must have done so
10926 deliberately. Furthermore, we reduce the false positive load by
10927 warning only for non-constant value of type char. */
10929 void
10930 warn_array_subscript_with_type_char (tree index)
10932 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10933 && TREE_CODE (index) != INTEGER_CST)
10934 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10937 /* Implement -Wparentheses for the unexpected C precedence rules, to
10938 cover cases like x + y << z which readers are likely to
10939 misinterpret. We have seen an expression in which CODE is a binary
10940 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10941 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10942 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10943 expression was not formed using a binary or unary operator, or it
10944 was enclosed in parentheses. */
10946 void
10947 warn_about_parentheses (location_t loc, enum tree_code code,
10948 enum tree_code code_left, tree arg_left,
10949 enum tree_code code_right, tree arg_right)
10951 if (!warn_parentheses)
10952 return;
10954 /* This macro tests that the expression ARG with original tree code
10955 CODE appears to be a boolean expression. or the result of folding a
10956 boolean expression. */
10957 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10958 (truth_value_p (TREE_CODE (ARG)) \
10959 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10960 /* Folding may create 0 or 1 integers from other expressions. */ \
10961 || ((CODE) != INTEGER_CST \
10962 && (integer_onep (ARG) || integer_zerop (ARG))))
10964 switch (code)
10966 case LSHIFT_EXPR:
10967 if (code_left == PLUS_EXPR)
10968 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10969 "suggest parentheses around %<+%> inside %<<<%>");
10970 else if (code_right == PLUS_EXPR)
10971 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10972 "suggest parentheses around %<+%> inside %<<<%>");
10973 else if (code_left == MINUS_EXPR)
10974 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10975 "suggest parentheses around %<-%> inside %<<<%>");
10976 else if (code_right == MINUS_EXPR)
10977 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10978 "suggest parentheses around %<-%> inside %<<<%>");
10979 return;
10981 case RSHIFT_EXPR:
10982 if (code_left == PLUS_EXPR)
10983 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10984 "suggest parentheses around %<+%> inside %<>>%>");
10985 else if (code_right == PLUS_EXPR)
10986 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10987 "suggest parentheses around %<+%> inside %<>>%>");
10988 else if (code_left == MINUS_EXPR)
10989 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10990 "suggest parentheses around %<-%> inside %<>>%>");
10991 else if (code_right == MINUS_EXPR)
10992 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10993 "suggest parentheses around %<-%> inside %<>>%>");
10994 return;
10996 case TRUTH_ORIF_EXPR:
10997 if (code_left == TRUTH_ANDIF_EXPR)
10998 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10999 "suggest parentheses around %<&&%> within %<||%>");
11000 else if (code_right == TRUTH_ANDIF_EXPR)
11001 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11002 "suggest parentheses around %<&&%> within %<||%>");
11003 return;
11005 case BIT_IOR_EXPR:
11006 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11007 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11008 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11009 "suggest parentheses around arithmetic in operand of %<|%>");
11010 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11011 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11012 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11013 "suggest parentheses around arithmetic in operand of %<|%>");
11014 /* Check cases like x|y==z */
11015 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11016 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11017 "suggest parentheses around comparison in operand of %<|%>");
11018 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11019 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11020 "suggest parentheses around comparison in operand of %<|%>");
11021 /* Check cases like !x | y */
11022 else if (code_left == TRUTH_NOT_EXPR
11023 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11024 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11025 "suggest parentheses around operand of "
11026 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11027 return;
11029 case BIT_XOR_EXPR:
11030 if (code_left == BIT_AND_EXPR
11031 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11032 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11033 "suggest parentheses around arithmetic in operand of %<^%>");
11034 else if (code_right == BIT_AND_EXPR
11035 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11036 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11037 "suggest parentheses around arithmetic in operand of %<^%>");
11038 /* Check cases like x^y==z */
11039 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11040 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11041 "suggest parentheses around comparison in operand of %<^%>");
11042 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11043 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11044 "suggest parentheses around comparison in operand of %<^%>");
11045 return;
11047 case BIT_AND_EXPR:
11048 if (code_left == PLUS_EXPR)
11049 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11050 "suggest parentheses around %<+%> in operand of %<&%>");
11051 else if (code_right == PLUS_EXPR)
11052 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11053 "suggest parentheses around %<+%> in operand of %<&%>");
11054 else if (code_left == MINUS_EXPR)
11055 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11056 "suggest parentheses around %<-%> in operand of %<&%>");
11057 else if (code_right == MINUS_EXPR)
11058 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11059 "suggest parentheses around %<-%> in operand of %<&%>");
11060 /* Check cases like x&y==z */
11061 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11062 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11063 "suggest parentheses around comparison in operand of %<&%>");
11064 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11065 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11066 "suggest parentheses around comparison in operand of %<&%>");
11067 /* Check cases like !x & y */
11068 else if (code_left == TRUTH_NOT_EXPR
11069 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11070 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11071 "suggest parentheses around operand of "
11072 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11073 return;
11075 case EQ_EXPR:
11076 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11077 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11078 "suggest parentheses around comparison in operand of %<==%>");
11079 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11080 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11081 "suggest parentheses around comparison in operand of %<==%>");
11082 return;
11083 case NE_EXPR:
11084 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11085 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11086 "suggest parentheses around comparison in operand of %<!=%>");
11087 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11088 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11089 "suggest parentheses around comparison in operand of %<!=%>");
11090 return;
11092 default:
11093 if (TREE_CODE_CLASS (code) == tcc_comparison)
11095 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11096 && code_left != NE_EXPR && code_left != EQ_EXPR
11097 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11098 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11099 "comparisons like %<X<=Y<=Z%> do not "
11100 "have their mathematical meaning");
11101 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11102 && code_right != NE_EXPR && code_right != EQ_EXPR
11103 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11104 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11105 "comparisons like %<X<=Y<=Z%> do not "
11106 "have their mathematical meaning");
11108 return;
11110 #undef NOT_A_BOOLEAN_EXPR_P
11113 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11115 void
11116 warn_for_unused_label (tree label)
11118 if (!TREE_USED (label))
11120 if (DECL_INITIAL (label))
11121 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11122 else
11123 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11127 /* Warn for division by zero according to the value of DIVISOR. LOC
11128 is the location of the division operator. */
11130 void
11131 warn_for_div_by_zero (location_t loc, tree divisor)
11133 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11134 about division by zero. Do not issue a warning if DIVISOR has a
11135 floating-point type, since we consider 0.0/0.0 a valid way of
11136 generating a NaN. */
11137 if (c_inhibit_evaluation_warnings == 0
11138 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11139 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11142 /* Subroutine of build_binary_op. Give warnings for comparisons
11143 between signed and unsigned quantities that may fail. Do the
11144 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11145 so that casts will be considered, but default promotions won't
11148 LOCATION is the location of the comparison operator.
11150 The arguments of this function map directly to local variables
11151 of build_binary_op. */
11153 void
11154 warn_for_sign_compare (location_t location,
11155 tree orig_op0, tree orig_op1,
11156 tree op0, tree op1,
11157 tree result_type, enum tree_code resultcode)
11159 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11160 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11161 int unsignedp0, unsignedp1;
11163 /* In C++, check for comparison of different enum types. */
11164 if (c_dialect_cxx()
11165 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11166 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11167 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11168 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11170 warning_at (location,
11171 OPT_Wsign_compare, "comparison between types %qT and %qT",
11172 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11175 /* Do not warn if the comparison is being done in a signed type,
11176 since the signed type will only be chosen if it can represent
11177 all the values of the unsigned type. */
11178 if (!TYPE_UNSIGNED (result_type))
11179 /* OK */;
11180 /* Do not warn if both operands are unsigned. */
11181 else if (op0_signed == op1_signed)
11182 /* OK */;
11183 else
11185 tree sop, uop, base_type;
11186 bool ovf;
11188 if (op0_signed)
11189 sop = orig_op0, uop = orig_op1;
11190 else
11191 sop = orig_op1, uop = orig_op0;
11193 STRIP_TYPE_NOPS (sop);
11194 STRIP_TYPE_NOPS (uop);
11195 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11196 ? TREE_TYPE (result_type) : result_type);
11198 /* Do not warn if the signed quantity is an unsuffixed integer
11199 literal (or some static constant expression involving such
11200 literals or a conditional expression involving such literals)
11201 and it is non-negative. */
11202 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11203 /* OK */;
11204 /* Do not warn if the comparison is an equality operation, the
11205 unsigned quantity is an integral constant, and it would fit
11206 in the result if the result were signed. */
11207 else if (TREE_CODE (uop) == INTEGER_CST
11208 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11209 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11210 /* OK */;
11211 /* In C, do not warn if the unsigned quantity is an enumeration
11212 constant and its maximum value would fit in the result if the
11213 result were signed. */
11214 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11215 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11216 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11217 c_common_signed_type (base_type)))
11218 /* OK */;
11219 else
11220 warning_at (location,
11221 OPT_Wsign_compare,
11222 "comparison between signed and unsigned integer expressions");
11225 /* Warn if two unsigned values are being compared in a size larger
11226 than their original size, and one (and only one) is the result of
11227 a `~' operator. This comparison will always fail.
11229 Also warn if one operand is a constant, and the constant does not
11230 have all bits set that are set in the ~ operand when it is
11231 extended. */
11233 op0 = c_common_get_narrower (op0, &unsignedp0);
11234 op1 = c_common_get_narrower (op1, &unsignedp1);
11236 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11237 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11239 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11240 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11241 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11242 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11244 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11246 tree primop;
11247 HOST_WIDE_INT constant, mask;
11248 int unsignedp;
11249 unsigned int bits;
11251 if (host_integerp (op0, 0))
11253 primop = op1;
11254 unsignedp = unsignedp1;
11255 constant = tree_low_cst (op0, 0);
11257 else
11259 primop = op0;
11260 unsignedp = unsignedp0;
11261 constant = tree_low_cst (op1, 0);
11264 bits = TYPE_PRECISION (TREE_TYPE (primop));
11265 if (bits < TYPE_PRECISION (result_type)
11266 && bits < HOST_BITS_PER_LONG && unsignedp)
11268 mask = (~ (HOST_WIDE_INT) 0) << bits;
11269 if ((mask & constant) != mask)
11271 if (constant == 0)
11272 warning (OPT_Wsign_compare,
11273 "promoted ~unsigned is always non-zero");
11274 else
11275 warning_at (location, OPT_Wsign_compare,
11276 "comparison of promoted ~unsigned with constant");
11280 else if (unsignedp0 && unsignedp1
11281 && (TYPE_PRECISION (TREE_TYPE (op0))
11282 < TYPE_PRECISION (result_type))
11283 && (TYPE_PRECISION (TREE_TYPE (op1))
11284 < TYPE_PRECISION (result_type)))
11285 warning_at (location, OPT_Wsign_compare,
11286 "comparison of promoted ~unsigned with unsigned");
11290 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11291 type via c_common_type. If -Wdouble-promotion is in use, and the
11292 conditions for warning have been met, issue a warning. GMSGID is
11293 the warning message. It must have two %T specifiers for the type
11294 that was converted (generally "float") and the type to which it was
11295 converted (generally "double), respectively. LOC is the location
11296 to which the awrning should refer. */
11298 void
11299 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11300 const char *gmsgid, location_t loc)
11302 tree source_type;
11304 if (!warn_double_promotion)
11305 return;
11306 /* If the conversion will not occur at run-time, there is no need to
11307 warn about it. */
11308 if (c_inhibit_evaluation_warnings)
11309 return;
11310 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11311 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11312 return;
11313 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11314 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11315 source_type = type1;
11316 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11317 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11318 source_type = type2;
11319 else
11320 return;
11321 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11324 /* Setup a TYPE_DECL node as a typedef representation.
11326 X is a TYPE_DECL for a typedef statement. Create a brand new
11327 ..._TYPE node (which will be just a variant of the existing
11328 ..._TYPE node with identical properties) and then install X
11329 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11331 The whole point here is to end up with a situation where each
11332 and every ..._TYPE node the compiler creates will be uniquely
11333 associated with AT MOST one node representing a typedef name.
11334 This way, even though the compiler substitutes corresponding
11335 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11336 early on, later parts of the compiler can always do the reverse
11337 translation and get back the corresponding typedef name. For
11338 example, given:
11340 typedef struct S MY_TYPE;
11341 MY_TYPE object;
11343 Later parts of the compiler might only know that `object' was of
11344 type `struct S' if it were not for code just below. With this
11345 code however, later parts of the compiler see something like:
11347 struct S' == struct S
11348 typedef struct S' MY_TYPE;
11349 struct S' object;
11351 And they can then deduce (from the node for type struct S') that
11352 the original object declaration was:
11354 MY_TYPE object;
11356 Being able to do this is important for proper support of protoize,
11357 and also for generating precise symbolic debugging information
11358 which takes full account of the programmer's (typedef) vocabulary.
11360 Obviously, we don't want to generate a duplicate ..._TYPE node if
11361 the TYPE_DECL node that we are now processing really represents a
11362 standard built-in type. */
11364 void
11365 set_underlying_type (tree x)
11367 if (x == error_mark_node)
11368 return;
11369 if (DECL_IS_BUILTIN (x))
11371 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11372 TYPE_NAME (TREE_TYPE (x)) = x;
11374 else if (TREE_TYPE (x) != error_mark_node
11375 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11377 tree tt = TREE_TYPE (x);
11378 DECL_ORIGINAL_TYPE (x) = tt;
11379 tt = build_variant_type_copy (tt);
11380 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11381 TYPE_NAME (tt) = x;
11382 TREE_USED (tt) = TREE_USED (x);
11383 TREE_TYPE (x) = tt;
11387 /* Record the types used by the current global variable declaration
11388 being parsed, so that we can decide later to emit their debug info.
11389 Those types are in types_used_by_cur_var_decl, and we are going to
11390 store them in the types_used_by_vars_hash hash table.
11391 DECL is the declaration of the global variable that has been parsed. */
11393 void
11394 record_types_used_by_current_var_decl (tree decl)
11396 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11398 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11400 tree type = types_used_by_cur_var_decl->pop ();
11401 types_used_by_var_decl_insert (type, decl);
11405 /* If DECL is a typedef that is declared in the current function,
11406 record it for the purpose of -Wunused-local-typedefs. */
11408 void
11409 record_locally_defined_typedef (tree decl)
11411 struct c_language_function *l;
11413 if (!warn_unused_local_typedefs
11414 || cfun == NULL
11415 /* if this is not a locally defined typedef then we are not
11416 interested. */
11417 || !is_typedef_decl (decl)
11418 || !decl_function_context (decl))
11419 return;
11421 l = (struct c_language_function *) cfun->language;
11422 vec_safe_push (l->local_typedefs, decl);
11425 /* If T is a TYPE_DECL declared locally, mark it as used. */
11427 void
11428 maybe_record_typedef_use (tree t)
11430 if (!is_typedef_decl (t))
11431 return;
11433 TREE_USED (t) = true;
11436 /* Warn if there are some unused locally defined typedefs in the
11437 current function. */
11439 void
11440 maybe_warn_unused_local_typedefs (void)
11442 int i;
11443 tree decl;
11444 /* The number of times we have emitted -Wunused-local-typedefs
11445 warnings. If this is different from errorcount, that means some
11446 unrelated errors have been issued. In which case, we'll avoid
11447 emitting "unused-local-typedefs" warnings. */
11448 static int unused_local_typedefs_warn_count;
11449 struct c_language_function *l;
11451 if (cfun == NULL)
11452 return;
11454 if ((l = (struct c_language_function *) cfun->language) == NULL)
11455 return;
11457 if (warn_unused_local_typedefs
11458 && errorcount == unused_local_typedefs_warn_count)
11460 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11461 if (!TREE_USED (decl))
11462 warning_at (DECL_SOURCE_LOCATION (decl),
11463 OPT_Wunused_local_typedefs,
11464 "typedef %qD locally defined but not used", decl);
11465 unused_local_typedefs_warn_count = errorcount;
11468 vec_free (l->local_typedefs);
11471 /* The C and C++ parsers both use vectors to hold function arguments.
11472 For efficiency, we keep a cache of unused vectors. This is the
11473 cache. */
11475 typedef vec<tree, va_gc> *tree_gc_vec;
11476 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11478 /* Return a new vector from the cache. If the cache is empty,
11479 allocate a new vector. These vectors are GC'ed, so it is OK if the
11480 pointer is not released.. */
11482 vec<tree, va_gc> *
11483 make_tree_vector (void)
11485 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11486 return tree_vector_cache->pop ();
11487 else
11489 /* Passing 0 to vec::alloc returns NULL, and our callers require
11490 that we always return a non-NULL value. The vector code uses
11491 4 when growing a NULL vector, so we do too. */
11492 vec<tree, va_gc> *v;
11493 vec_alloc (v, 4);
11494 return v;
11498 /* Release a vector of trees back to the cache. */
11500 void
11501 release_tree_vector (vec<tree, va_gc> *vec)
11503 if (vec != NULL)
11505 vec->truncate (0);
11506 vec_safe_push (tree_vector_cache, vec);
11510 /* Get a new tree vector holding a single tree. */
11512 vec<tree, va_gc> *
11513 make_tree_vector_single (tree t)
11515 vec<tree, va_gc> *ret = make_tree_vector ();
11516 ret->quick_push (t);
11517 return ret;
11520 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11522 vec<tree, va_gc> *
11523 make_tree_vector_from_list (tree list)
11525 vec<tree, va_gc> *ret = make_tree_vector ();
11526 for (; list; list = TREE_CHAIN (list))
11527 vec_safe_push (ret, TREE_VALUE (list));
11528 return ret;
11531 /* Get a new tree vector which is a copy of an existing one. */
11533 vec<tree, va_gc> *
11534 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11536 vec<tree, va_gc> *ret;
11537 unsigned int ix;
11538 tree t;
11540 ret = make_tree_vector ();
11541 vec_safe_reserve (ret, vec_safe_length (orig));
11542 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11543 ret->quick_push (t);
11544 return ret;
11547 /* Return true if KEYWORD starts a type specifier. */
11549 bool
11550 keyword_begins_type_specifier (enum rid keyword)
11552 switch (keyword)
11554 case RID_INT:
11555 case RID_CHAR:
11556 case RID_FLOAT:
11557 case RID_DOUBLE:
11558 case RID_VOID:
11559 case RID_INT128:
11560 case RID_UNSIGNED:
11561 case RID_LONG:
11562 case RID_SHORT:
11563 case RID_SIGNED:
11564 case RID_DFLOAT32:
11565 case RID_DFLOAT64:
11566 case RID_DFLOAT128:
11567 case RID_FRACT:
11568 case RID_ACCUM:
11569 case RID_BOOL:
11570 case RID_WCHAR:
11571 case RID_CHAR16:
11572 case RID_CHAR32:
11573 case RID_SAT:
11574 case RID_COMPLEX:
11575 case RID_TYPEOF:
11576 case RID_STRUCT:
11577 case RID_CLASS:
11578 case RID_UNION:
11579 case RID_ENUM:
11580 return true;
11581 default:
11582 return false;
11586 /* Return true if KEYWORD names a type qualifier. */
11588 bool
11589 keyword_is_type_qualifier (enum rid keyword)
11591 switch (keyword)
11593 case RID_CONST:
11594 case RID_VOLATILE:
11595 case RID_RESTRICT:
11596 case RID_ATOMIC:
11597 return true;
11598 default:
11599 return false;
11603 /* Return true if KEYWORD names a storage class specifier.
11605 RID_TYPEDEF is not included in this list despite `typedef' being
11606 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11607 such for syntactic convenience only. */
11609 bool
11610 keyword_is_storage_class_specifier (enum rid keyword)
11612 switch (keyword)
11614 case RID_STATIC:
11615 case RID_EXTERN:
11616 case RID_REGISTER:
11617 case RID_AUTO:
11618 case RID_MUTABLE:
11619 case RID_THREAD:
11620 return true;
11621 default:
11622 return false;
11626 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11628 static bool
11629 keyword_is_function_specifier (enum rid keyword)
11631 switch (keyword)
11633 case RID_INLINE:
11634 case RID_NORETURN:
11635 case RID_VIRTUAL:
11636 case RID_EXPLICIT:
11637 return true;
11638 default:
11639 return false;
11643 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11644 declaration-specifier (C99 6.7). */
11646 bool
11647 keyword_is_decl_specifier (enum rid keyword)
11649 if (keyword_is_storage_class_specifier (keyword)
11650 || keyword_is_type_qualifier (keyword)
11651 || keyword_is_function_specifier (keyword))
11652 return true;
11654 switch (keyword)
11656 case RID_TYPEDEF:
11657 case RID_FRIEND:
11658 case RID_CONSTEXPR:
11659 return true;
11660 default:
11661 return false;
11665 /* Initialize language-specific-bits of tree_contains_struct. */
11667 void
11668 c_common_init_ts (void)
11670 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11671 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11672 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11675 /* Build a user-defined numeric literal out of an integer constant type VALUE
11676 with identifier SUFFIX. */
11678 tree
11679 build_userdef_literal (tree suffix_id, tree value,
11680 enum overflow_type overflow, tree num_string)
11682 tree literal = make_node (USERDEF_LITERAL);
11683 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11684 USERDEF_LITERAL_VALUE (literal) = value;
11685 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11686 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11687 return literal;
11690 /* For vector[index], convert the vector to a
11691 pointer of the underlying type. */
11692 void
11693 convert_vector_to_pointer_for_subscript (location_t loc,
11694 tree* vecp, tree index)
11696 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11698 tree type = TREE_TYPE (*vecp);
11699 tree type1;
11701 if (TREE_CODE (index) == INTEGER_CST)
11702 if (!host_integerp (index, 1)
11703 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11704 >= TYPE_VECTOR_SUBPARTS (type)))
11705 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11707 c_common_mark_addressable_vec (*vecp);
11708 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11709 type = build_pointer_type (type);
11710 type1 = build_pointer_type (TREE_TYPE (*vecp));
11711 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11712 *vecp = convert (type, *vecp);
11716 /* Determine which of the operands, if any, is a scalar that needs to be
11717 converted to a vector, for the range of operations. */
11718 enum stv_conv
11719 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11720 bool complain)
11722 tree type0 = TREE_TYPE (op0);
11723 tree type1 = TREE_TYPE (op1);
11724 bool integer_only_op = false;
11725 enum stv_conv ret = stv_firstarg;
11727 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11728 || TREE_CODE (type1) == VECTOR_TYPE);
11729 switch (code)
11731 /* Most GENERIC binary expressions require homogeneous arguments.
11732 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11733 argument that is a vector and a second one that is a scalar, so
11734 we never return stv_secondarg for them. */
11735 case RSHIFT_EXPR:
11736 case LSHIFT_EXPR:
11737 if (TREE_CODE (type0) == INTEGER_TYPE
11738 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11740 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11742 if (complain)
11743 error_at (loc, "conversion of scalar %qT to vector %qT "
11744 "involves truncation", type0, type1);
11745 return stv_error;
11747 else
11748 return stv_firstarg;
11750 break;
11752 case BIT_IOR_EXPR:
11753 case BIT_XOR_EXPR:
11754 case BIT_AND_EXPR:
11755 integer_only_op = true;
11756 /* ... fall through ... */
11758 case VEC_COND_EXPR:
11760 case PLUS_EXPR:
11761 case MINUS_EXPR:
11762 case MULT_EXPR:
11763 case TRUNC_DIV_EXPR:
11764 case CEIL_DIV_EXPR:
11765 case FLOOR_DIV_EXPR:
11766 case ROUND_DIV_EXPR:
11767 case EXACT_DIV_EXPR:
11768 case TRUNC_MOD_EXPR:
11769 case FLOOR_MOD_EXPR:
11770 case RDIV_EXPR:
11771 case EQ_EXPR:
11772 case NE_EXPR:
11773 case LE_EXPR:
11774 case GE_EXPR:
11775 case LT_EXPR:
11776 case GT_EXPR:
11777 /* What about UNLT_EXPR? */
11778 if (TREE_CODE (type0) == VECTOR_TYPE)
11780 tree tmp;
11781 ret = stv_secondarg;
11782 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11783 tmp = type0; type0 = type1; type1 = tmp;
11784 tmp = op0; op0 = op1; op1 = tmp;
11787 if (TREE_CODE (type0) == INTEGER_TYPE
11788 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11790 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11792 if (complain)
11793 error_at (loc, "conversion of scalar %qT to vector %qT "
11794 "involves truncation", type0, type1);
11795 return stv_error;
11797 return ret;
11799 else if (!integer_only_op
11800 /* Allow integer --> real conversion if safe. */
11801 && (TREE_CODE (type0) == REAL_TYPE
11802 || TREE_CODE (type0) == INTEGER_TYPE)
11803 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11805 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11807 if (complain)
11808 error_at (loc, "conversion of scalar %qT to vector %qT "
11809 "involves truncation", type0, type1);
11810 return stv_error;
11812 return ret;
11814 default:
11815 break;
11818 return stv_nothing;
11821 /* Return true iff ALIGN is an integral constant that is a fundamental
11822 alignment, as defined by [basic.align] in the c++-11
11823 specifications.
11825 That is:
11827 [A fundamental alignment is represented by an alignment less than or
11828 equal to the greatest alignment supported by the implementation
11829 in all contexts, which is equal to
11830 alignof(max_align_t)]. */
11832 bool
11833 cxx_fundamental_alignment_p (unsigned align)
11835 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11836 TYPE_ALIGN (long_double_type_node)));
11839 #include "gt-c-family-c-common.h"