* c-common.c (atomic_size_supported_p): New function.
[official-gcc.git] / gcc / c-family / c-common.c
blob93481b9302713c33e93783d7d3519fedab0dbba2
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"
46 cpp_reader *parse_in; /* Declared in c-pragma.h. */
48 /* The following symbols are subsumed in the c_global_trees array, and
49 listed here individually for documentation purposes.
51 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
53 tree short_integer_type_node;
54 tree long_integer_type_node;
55 tree long_long_integer_type_node;
56 tree int128_integer_type_node;
58 tree short_unsigned_type_node;
59 tree long_unsigned_type_node;
60 tree long_long_unsigned_type_node;
61 tree int128_unsigned_type_node;
63 tree truthvalue_type_node;
64 tree truthvalue_false_node;
65 tree truthvalue_true_node;
67 tree ptrdiff_type_node;
69 tree unsigned_char_type_node;
70 tree signed_char_type_node;
71 tree wchar_type_node;
73 tree char16_type_node;
74 tree char32_type_node;
76 tree float_type_node;
77 tree double_type_node;
78 tree long_double_type_node;
80 tree complex_integer_type_node;
81 tree complex_float_type_node;
82 tree complex_double_type_node;
83 tree complex_long_double_type_node;
85 tree dfloat32_type_node;
86 tree dfloat64_type_node;
87 tree_dfloat128_type_node;
89 tree intQI_type_node;
90 tree intHI_type_node;
91 tree intSI_type_node;
92 tree intDI_type_node;
93 tree intTI_type_node;
95 tree unsigned_intQI_type_node;
96 tree unsigned_intHI_type_node;
97 tree unsigned_intSI_type_node;
98 tree unsigned_intDI_type_node;
99 tree unsigned_intTI_type_node;
101 tree widest_integer_literal_type_node;
102 tree widest_unsigned_literal_type_node;
104 Nodes for types `void *' and `const void *'.
106 tree ptr_type_node, const_ptr_type_node;
108 Nodes for types `char *' and `const char *'.
110 tree string_type_node, const_string_type_node;
112 Type `char[SOMENUMBER]'.
113 Used when an array of char is needed and the size is irrelevant.
115 tree char_array_type_node;
117 Type `int[SOMENUMBER]' or something like it.
118 Used when an array of int needed and the size is irrelevant.
120 tree int_array_type_node;
122 Type `wchar_t[SOMENUMBER]' or something like it.
123 Used when a wide string literal is created.
125 tree wchar_array_type_node;
127 Type `char16_t[SOMENUMBER]' or something like it.
128 Used when a UTF-16 string literal is created.
130 tree char16_array_type_node;
132 Type `char32_t[SOMENUMBER]' or something like it.
133 Used when a UTF-32 string literal is created.
135 tree char32_array_type_node;
137 Type `int ()' -- used for implicit declaration of functions.
139 tree default_function_type;
141 A VOID_TYPE node, packaged in a TREE_LIST.
143 tree void_list_node;
145 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
146 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
147 VAR_DECLS, but C++ does.)
149 tree function_name_decl_node;
150 tree pretty_function_name_decl_node;
151 tree c99_function_name_decl_node;
153 Stack of nested function name VAR_DECLs.
155 tree saved_function_name_decls;
159 tree c_global_trees[CTI_MAX];
161 /* Switches common to the C front ends. */
163 /* Nonzero means don't output line number information. */
165 char flag_no_line_commands;
167 /* Nonzero causes -E output not to be done, but directives such as
168 #define that have side effects are still obeyed. */
170 char flag_no_output;
172 /* Nonzero means dump macros in some fashion. */
174 char flag_dump_macros;
176 /* Nonzero means pass #include lines through to the output. */
178 char flag_dump_includes;
180 /* Nonzero means process PCH files while preprocessing. */
182 bool flag_pch_preprocess;
184 /* The file name to which we should write a precompiled header, or
185 NULL if no header will be written in this compile. */
187 const char *pch_file;
189 /* Nonzero if an ISO standard was selected. It rejects macros in the
190 user's namespace. */
191 int flag_iso;
193 /* C/ObjC language option variables. */
196 /* Nonzero means allow type mismatches in conditional expressions;
197 just make their values `void'. */
199 int flag_cond_mismatch;
201 /* Nonzero means enable C89 Amendment 1 features. */
203 int flag_isoc94;
205 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
207 int flag_isoc99;
209 /* Nonzero means use the ISO C11 dialect of C. */
211 int flag_isoc11;
213 /* Nonzero means that we have builtin functions, and main is an int. */
215 int flag_hosted = 1;
218 /* ObjC language option variables. */
221 /* Tells the compiler that this is a special run. Do not perform any
222 compiling, instead we are to test some platform dependent features
223 and output a C header file with appropriate definitions. */
225 int print_struct_values;
227 /* Tells the compiler what is the constant string class for ObjC. */
229 const char *constant_string_class_name;
232 /* C++ language option variables. */
235 /* Nonzero means generate separate instantiation control files and
236 juggle them at link time. */
238 int flag_use_repository;
240 /* The C++ dialect being used. C++98 is the default. */
242 enum cxx_dialect cxx_dialect = cxx98;
244 /* Maximum template instantiation depth. This limit exists to limit the
245 time it takes to notice excessively recursive template instantiations.
247 The default is lower than the 1024 recommended by the C++0x standard
248 because G++ runs out of stack before 1024 with highly recursive template
249 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
251 int max_tinst_depth = 900;
253 /* The elements of `ridpointers' are identifier nodes for the reserved
254 type names and storage classes. It is indexed by a RID_... value. */
255 tree *ridpointers;
257 tree (*make_fname_decl) (location_t, tree, int);
259 /* Nonzero means don't warn about problems that occur when the code is
260 executed. */
261 int c_inhibit_evaluation_warnings;
263 /* Whether we are building a boolean conversion inside
264 convert_for_assignment, or some other late binary operation. If
265 build_binary_op is called for C (from code shared by C and C++) in
266 this case, then the operands have already been folded and the
267 result will not be folded again, so C_MAYBE_CONST_EXPR should not
268 be generated. */
269 bool in_late_binary_op;
271 /* Whether lexing has been completed, so subsequent preprocessor
272 errors should use the compiler's input_location. */
273 bool done_lexing = false;
275 /* Information about how a function name is generated. */
276 struct fname_var_t
278 tree *const decl; /* pointer to the VAR_DECL. */
279 const unsigned rid; /* RID number for the identifier. */
280 const int pretty; /* How pretty is it? */
283 /* The three ways of getting then name of the current function. */
285 const struct fname_var_t fname_vars[] =
287 /* C99 compliant __func__, must be first. */
288 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
289 /* GCC __FUNCTION__ compliant. */
290 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
291 /* GCC __PRETTY_FUNCTION__ compliant. */
292 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
293 {NULL, 0, 0},
296 /* Global visibility options. */
297 struct visibility_flags visibility_options;
299 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
300 static tree check_case_value (tree);
301 static bool check_case_bounds (tree, tree, tree *, tree *);
303 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
304 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
305 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
306 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
310 int, bool *);
311 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
312 int, bool *);
313 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
314 bool *);
315 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_always_inline_attribute (tree *, tree, tree, int,
319 bool *);
320 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
327 bool *);
328 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_transparent_union_attribute (tree *, tree, tree,
330 int, bool *);
331 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
337 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
338 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
341 static tree handle_visibility_attribute (tree *, tree, tree, int,
342 bool *);
343 static tree handle_tls_model_attribute (tree *, tree, tree, int,
344 bool *);
345 static tree handle_no_instrument_function_attribute (tree *, tree,
346 tree, int, bool *);
347 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
350 bool *);
351 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_deprecated_attribute (tree *, tree, tree, int,
356 bool *);
357 static tree handle_vector_size_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
363 bool *);
364 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
369 static tree ignore_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
375 bool *);
376 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
381 static void check_function_nonnull (tree, int, tree *);
382 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
383 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
384 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
385 static int resort_field_decl_cmp (const void *, const void *);
387 /* Reserved words. The third field is a mask: keywords are disabled
388 if they match the mask.
390 Masks for languages:
391 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
392 C --std=c99: D_CXXONLY | D_OBJC
393 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
394 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
395 C++ --std=c0x: D_CONLY | D_OBJC
396 ObjC++ is like C++ except that D_OBJC is not set
398 If -fno-asm is used, D_ASM is added to the mask. If
399 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
400 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
401 In C with -Wc++-compat, we warn if D_CXXWARN is set.
403 Note the complication of the D_CXX_OBJC keywords. These are
404 reserved words such as 'class'. In C++, 'class' is a reserved
405 word. In Objective-C++ it is too. In Objective-C, it is a
406 reserved word too, but only if it follows an '@' sign.
408 const struct c_common_resword c_common_reswords[] =
410 { "_Alignas", RID_ALIGNAS, D_CONLY },
411 { "_Alignof", RID_ALIGNOF, D_CONLY },
412 { "_Atomic", RID_ATOMIC, D_CONLY },
413 { "_Bool", RID_BOOL, D_CONLY },
414 { "_Complex", RID_COMPLEX, 0 },
415 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
416 { "_Cilk_sync", RID_CILK_SYNC, 0 },
417 { "_Imaginary", RID_IMAGINARY, D_CONLY },
418 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
419 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
420 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
421 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
422 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
423 { "_Sat", RID_SAT, D_CONLY | D_EXT },
424 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
425 { "_Noreturn", RID_NORETURN, D_CONLY },
426 { "_Generic", RID_GENERIC, D_CONLY },
427 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
428 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
429 { "__alignof", RID_ALIGNOF, 0 },
430 { "__alignof__", RID_ALIGNOF, 0 },
431 { "__asm", RID_ASM, 0 },
432 { "__asm__", RID_ASM, 0 },
433 { "__attribute", RID_ATTRIBUTE, 0 },
434 { "__attribute__", RID_ATTRIBUTE, 0 },
435 { "__bases", RID_BASES, D_CXXONLY },
436 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
437 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
438 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
439 { "__builtin_offsetof", RID_OFFSETOF, 0 },
440 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
441 { "__builtin_va_arg", RID_VA_ARG, 0 },
442 { "__complex", RID_COMPLEX, 0 },
443 { "__complex__", RID_COMPLEX, 0 },
444 { "__const", RID_CONST, 0 },
445 { "__const__", RID_CONST, 0 },
446 { "__decltype", RID_DECLTYPE, D_CXXONLY },
447 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
448 { "__extension__", RID_EXTENSION, 0 },
449 { "__func__", RID_C99_FUNCTION_NAME, 0 },
450 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
451 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
452 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
453 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
454 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
455 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
456 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
457 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
458 { "__imag", RID_IMAGPART, 0 },
459 { "__imag__", RID_IMAGPART, 0 },
460 { "__inline", RID_INLINE, 0 },
461 { "__inline__", RID_INLINE, 0 },
462 { "__int128", RID_INT128, 0 },
463 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
464 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
465 { "__is_class", RID_IS_CLASS, D_CXXONLY },
466 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
467 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
468 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
469 { "__is_final", RID_IS_FINAL, D_CXXONLY },
470 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
471 { "__is_pod", RID_IS_POD, D_CXXONLY },
472 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
473 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
474 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
475 { "__is_union", RID_IS_UNION, D_CXXONLY },
476 { "__label__", RID_LABEL, 0 },
477 { "__null", RID_NULL, 0 },
478 { "__real", RID_REALPART, 0 },
479 { "__real__", RID_REALPART, 0 },
480 { "__restrict", RID_RESTRICT, 0 },
481 { "__restrict__", RID_RESTRICT, 0 },
482 { "__signed", RID_SIGNED, 0 },
483 { "__signed__", RID_SIGNED, 0 },
484 { "__thread", RID_THREAD, 0 },
485 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
486 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
487 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
488 { "__typeof", RID_TYPEOF, 0 },
489 { "__typeof__", RID_TYPEOF, 0 },
490 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
491 { "__volatile", RID_VOLATILE, 0 },
492 { "__volatile__", RID_VOLATILE, 0 },
493 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
494 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
495 { "asm", RID_ASM, D_ASM },
496 { "auto", RID_AUTO, 0 },
497 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
498 { "break", RID_BREAK, 0 },
499 { "case", RID_CASE, 0 },
500 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
501 { "char", RID_CHAR, 0 },
502 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
503 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
504 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
505 { "const", RID_CONST, 0 },
506 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
507 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
508 { "continue", RID_CONTINUE, 0 },
509 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
510 { "default", RID_DEFAULT, 0 },
511 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
512 { "do", RID_DO, 0 },
513 { "double", RID_DOUBLE, 0 },
514 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
515 { "else", RID_ELSE, 0 },
516 { "enum", RID_ENUM, 0 },
517 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
518 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
519 { "extern", RID_EXTERN, 0 },
520 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
521 { "float", RID_FLOAT, 0 },
522 { "for", RID_FOR, 0 },
523 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
524 { "goto", RID_GOTO, 0 },
525 { "if", RID_IF, 0 },
526 { "inline", RID_INLINE, D_EXT89 },
527 { "int", RID_INT, 0 },
528 { "long", RID_LONG, 0 },
529 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
530 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
531 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
532 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
533 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
534 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
535 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
536 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
537 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
538 { "register", RID_REGISTER, 0 },
539 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
540 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
541 { "return", RID_RETURN, 0 },
542 { "short", RID_SHORT, 0 },
543 { "signed", RID_SIGNED, 0 },
544 { "sizeof", RID_SIZEOF, 0 },
545 { "static", RID_STATIC, 0 },
546 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
547 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
548 { "struct", RID_STRUCT, 0 },
549 { "switch", RID_SWITCH, 0 },
550 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
551 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
552 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
553 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
554 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
555 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
556 { "typedef", RID_TYPEDEF, 0 },
557 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
558 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
559 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
560 { "union", RID_UNION, 0 },
561 { "unsigned", RID_UNSIGNED, 0 },
562 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
563 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
564 { "void", RID_VOID, 0 },
565 { "volatile", RID_VOLATILE, 0 },
566 { "wchar_t", RID_WCHAR, D_CXXONLY },
567 { "while", RID_WHILE, 0 },
568 /* These Objective-C keywords are recognized only immediately after
569 an '@'. */
570 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
571 { "defs", RID_AT_DEFS, D_OBJC },
572 { "encode", RID_AT_ENCODE, D_OBJC },
573 { "end", RID_AT_END, D_OBJC },
574 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
575 { "interface", RID_AT_INTERFACE, D_OBJC },
576 { "protocol", RID_AT_PROTOCOL, D_OBJC },
577 { "selector", RID_AT_SELECTOR, D_OBJC },
578 { "finally", RID_AT_FINALLY, D_OBJC },
579 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
580 { "optional", RID_AT_OPTIONAL, D_OBJC },
581 { "required", RID_AT_REQUIRED, D_OBJC },
582 { "property", RID_AT_PROPERTY, D_OBJC },
583 { "package", RID_AT_PACKAGE, D_OBJC },
584 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
585 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
586 /* These are recognized only in protocol-qualifier context
587 (see above) */
588 { "bycopy", RID_BYCOPY, D_OBJC },
589 { "byref", RID_BYREF, D_OBJC },
590 { "in", RID_IN, D_OBJC },
591 { "inout", RID_INOUT, D_OBJC },
592 { "oneway", RID_ONEWAY, D_OBJC },
593 { "out", RID_OUT, D_OBJC },
594 /* These are recognized inside a property attribute list */
595 { "assign", RID_ASSIGN, D_OBJC },
596 { "copy", RID_COPY, D_OBJC },
597 { "getter", RID_GETTER, D_OBJC },
598 { "nonatomic", RID_NONATOMIC, D_OBJC },
599 { "readonly", RID_READONLY, D_OBJC },
600 { "readwrite", RID_READWRITE, D_OBJC },
601 { "retain", RID_RETAIN, D_OBJC },
602 { "setter", RID_SETTER, D_OBJC },
605 const unsigned int num_c_common_reswords =
606 sizeof c_common_reswords / sizeof (struct c_common_resword);
608 /* Table of machine-independent attributes common to all C-like languages. */
609 const struct attribute_spec c_common_attribute_table[] =
611 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
612 affects_type_identity } */
613 { "packed", 0, 0, false, false, false,
614 handle_packed_attribute , false},
615 { "nocommon", 0, 0, true, false, false,
616 handle_nocommon_attribute, false},
617 { "common", 0, 0, true, false, false,
618 handle_common_attribute, false },
619 /* FIXME: logically, noreturn attributes should be listed as
620 "false, true, true" and apply to function types. But implementing this
621 would require all the places in the compiler that use TREE_THIS_VOLATILE
622 on a decl to identify non-returning functions to be located and fixed
623 to check the function type instead. */
624 { "noreturn", 0, 0, true, false, false,
625 handle_noreturn_attribute, false },
626 { "volatile", 0, 0, true, false, false,
627 handle_noreturn_attribute, false },
628 { "noinline", 0, 0, true, false, false,
629 handle_noinline_attribute, false },
630 { "noclone", 0, 0, true, false, false,
631 handle_noclone_attribute, false },
632 { "leaf", 0, 0, true, false, false,
633 handle_leaf_attribute, false },
634 { "always_inline", 0, 0, true, false, false,
635 handle_always_inline_attribute, false },
636 { "gnu_inline", 0, 0, true, false, false,
637 handle_gnu_inline_attribute, false },
638 { "artificial", 0, 0, true, false, false,
639 handle_artificial_attribute, false },
640 { "flatten", 0, 0, true, false, false,
641 handle_flatten_attribute, false },
642 { "used", 0, 0, true, false, false,
643 handle_used_attribute, false },
644 { "unused", 0, 0, false, false, false,
645 handle_unused_attribute, false },
646 { "externally_visible", 0, 0, true, false, false,
647 handle_externally_visible_attribute, false },
648 /* The same comments as for noreturn attributes apply to const ones. */
649 { "const", 0, 0, true, false, false,
650 handle_const_attribute, false },
651 { "transparent_union", 0, 0, false, false, false,
652 handle_transparent_union_attribute, false },
653 { "constructor", 0, 1, true, false, false,
654 handle_constructor_attribute, false },
655 { "destructor", 0, 1, true, false, false,
656 handle_destructor_attribute, false },
657 { "mode", 1, 1, false, true, false,
658 handle_mode_attribute, false },
659 { "section", 1, 1, true, false, false,
660 handle_section_attribute, false },
661 { "aligned", 0, 1, false, false, false,
662 handle_aligned_attribute, false },
663 { "weak", 0, 0, true, false, false,
664 handle_weak_attribute, false },
665 { "ifunc", 1, 1, true, false, false,
666 handle_ifunc_attribute, false },
667 { "alias", 1, 1, true, false, false,
668 handle_alias_attribute, false },
669 { "weakref", 0, 1, true, false, false,
670 handle_weakref_attribute, false },
671 { "no_instrument_function", 0, 0, true, false, false,
672 handle_no_instrument_function_attribute,
673 false },
674 { "malloc", 0, 0, true, false, false,
675 handle_malloc_attribute, false },
676 { "returns_twice", 0, 0, true, false, false,
677 handle_returns_twice_attribute, false },
678 { "no_stack_limit", 0, 0, true, false, false,
679 handle_no_limit_stack_attribute, false },
680 { "pure", 0, 0, true, false, false,
681 handle_pure_attribute, false },
682 { "transaction_callable", 0, 0, false, true, false,
683 handle_tm_attribute, false },
684 { "transaction_unsafe", 0, 0, false, true, false,
685 handle_tm_attribute, false },
686 { "transaction_safe", 0, 0, false, true, false,
687 handle_tm_attribute, false },
688 { "transaction_may_cancel_outer", 0, 0, false, true, false,
689 handle_tm_attribute, false },
690 /* ??? These two attributes didn't make the transition from the
691 Intel language document to the multi-vendor language document. */
692 { "transaction_pure", 0, 0, false, true, false,
693 handle_tm_attribute, false },
694 { "transaction_wrap", 1, 1, true, false, false,
695 handle_tm_wrap_attribute, false },
696 /* For internal use (marking of builtins) only. The name contains space
697 to prevent its usage in source code. */
698 { "no vops", 0, 0, true, false, false,
699 handle_novops_attribute, false },
700 { "deprecated", 0, 1, false, false, false,
701 handle_deprecated_attribute, false },
702 { "vector_size", 1, 1, false, true, false,
703 handle_vector_size_attribute, false },
704 { "visibility", 1, 1, false, false, false,
705 handle_visibility_attribute, false },
706 { "tls_model", 1, 1, true, false, false,
707 handle_tls_model_attribute, false },
708 { "nonnull", 0, -1, false, true, true,
709 handle_nonnull_attribute, false },
710 { "nothrow", 0, 0, true, false, false,
711 handle_nothrow_attribute, false },
712 { "may_alias", 0, 0, false, true, false, NULL, false },
713 { "cleanup", 1, 1, true, false, false,
714 handle_cleanup_attribute, false },
715 { "warn_unused_result", 0, 0, false, true, true,
716 handle_warn_unused_result_attribute, false },
717 { "sentinel", 0, 1, false, true, true,
718 handle_sentinel_attribute, false },
719 /* For internal use (marking of builtins) only. The name contains space
720 to prevent its usage in source code. */
721 { "type generic", 0, 0, false, true, true,
722 handle_type_generic_attribute, false },
723 { "alloc_size", 1, 2, false, true, true,
724 handle_alloc_size_attribute, false },
725 { "cold", 0, 0, true, false, false,
726 handle_cold_attribute, false },
727 { "hot", 0, 0, true, false, false,
728 handle_hot_attribute, false },
729 { "no_address_safety_analysis",
730 0, 0, true, false, false,
731 handle_no_address_safety_analysis_attribute,
732 false },
733 { "no_sanitize_address", 0, 0, true, false, false,
734 handle_no_sanitize_address_attribute,
735 false },
736 { "no_sanitize_undefined", 0, 0, true, false, false,
737 handle_no_sanitize_undefined_attribute,
738 false },
739 { "warning", 1, 1, true, false, false,
740 handle_error_attribute, false },
741 { "error", 1, 1, true, false, false,
742 handle_error_attribute, false },
743 { "target", 1, -1, true, false, false,
744 handle_target_attribute, false },
745 { "optimize", 1, -1, true, false, false,
746 handle_optimize_attribute, false },
747 /* For internal use only. The leading '*' both prevents its usage in
748 source code and signals that it may be overridden by machine tables. */
749 { "*tm regparm", 0, 0, false, true, true,
750 ignore_attribute, false },
751 { "no_split_stack", 0, 0, true, false, false,
752 handle_no_split_stack_attribute, false },
753 /* For internal use (marking of builtins and runtime functions) only.
754 The name contains space to prevent its usage in source code. */
755 { "fn spec", 1, 1, false, true, true,
756 handle_fnspec_attribute, false },
757 { "warn_unused", 0, 0, false, false, false,
758 handle_warn_unused_attribute, false },
759 { "returns_nonnull", 0, 0, false, true, true,
760 handle_returns_nonnull_attribute, false },
761 { "omp declare simd", 0, -1, true, false, false,
762 handle_omp_declare_simd_attribute, false },
763 { "omp declare target", 0, 0, true, false, false,
764 handle_omp_declare_target_attribute, false },
765 { "bnd_variable_size", 0, 0, true, false, false,
766 handle_bnd_variable_size_attribute, false },
767 { "bnd_legacy", 0, 0, true, false, false,
768 handle_bnd_legacy, false },
769 { NULL, 0, 0, false, false, false, NULL, false }
772 /* Give the specifications for the format attributes, used by C and all
773 descendants. */
775 const struct attribute_spec c_common_format_attribute_table[] =
777 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
778 affects_type_identity } */
779 { "format", 3, 3, false, true, true,
780 handle_format_attribute, false },
781 { "format_arg", 1, 1, false, true, true,
782 handle_format_arg_attribute, false },
783 { NULL, 0, 0, false, false, false, NULL, false }
786 /* Return identifier for address space AS. */
788 const char *
789 c_addr_space_name (addr_space_t as)
791 int rid = RID_FIRST_ADDR_SPACE + as;
792 gcc_assert (ridpointers [rid]);
793 return IDENTIFIER_POINTER (ridpointers [rid]);
796 /* Push current bindings for the function name VAR_DECLS. */
798 void
799 start_fname_decls (void)
801 unsigned ix;
802 tree saved = NULL_TREE;
804 for (ix = 0; fname_vars[ix].decl; ix++)
806 tree decl = *fname_vars[ix].decl;
808 if (decl)
810 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
811 saved);
812 *fname_vars[ix].decl = NULL_TREE;
815 if (saved || saved_function_name_decls)
816 /* Normally they'll have been NULL, so only push if we've got a
817 stack, or they are non-NULL. */
818 saved_function_name_decls = tree_cons (saved, NULL_TREE,
819 saved_function_name_decls);
822 /* Finish up the current bindings, adding them into the current function's
823 statement tree. This must be done _before_ finish_stmt_tree is called.
824 If there is no current function, we must be at file scope and no statements
825 are involved. Pop the previous bindings. */
827 void
828 finish_fname_decls (void)
830 unsigned ix;
831 tree stmts = NULL_TREE;
832 tree stack = saved_function_name_decls;
834 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
835 append_to_statement_list (TREE_VALUE (stack), &stmts);
837 if (stmts)
839 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
841 if (TREE_CODE (*bodyp) == BIND_EXPR)
842 bodyp = &BIND_EXPR_BODY (*bodyp);
844 append_to_statement_list_force (*bodyp, &stmts);
845 *bodyp = stmts;
848 for (ix = 0; fname_vars[ix].decl; ix++)
849 *fname_vars[ix].decl = NULL_TREE;
851 if (stack)
853 /* We had saved values, restore them. */
854 tree saved;
856 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
858 tree decl = TREE_PURPOSE (saved);
859 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
861 *fname_vars[ix].decl = decl;
863 stack = TREE_CHAIN (stack);
865 saved_function_name_decls = stack;
868 /* Return the text name of the current function, suitably prettified
869 by PRETTY_P. Return string must be freed by caller. */
871 const char *
872 fname_as_string (int pretty_p)
874 const char *name = "top level";
875 char *namep;
876 int vrb = 2, len;
877 cpp_string cstr = { 0, 0 }, strname;
879 if (!pretty_p)
881 name = "";
882 vrb = 0;
885 if (current_function_decl)
886 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
888 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
890 namep = XNEWVEC (char, len);
891 snprintf (namep, len, "\"%s\"", name);
892 strname.text = (unsigned char *) namep;
893 strname.len = len - 1;
895 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
897 XDELETEVEC (namep);
898 return (const char *) cstr.text;
901 return namep;
904 /* Return the VAR_DECL for a const char array naming the current
905 function. If the VAR_DECL has not yet been created, create it
906 now. RID indicates how it should be formatted and IDENTIFIER_NODE
907 ID is its name (unfortunately C and C++ hold the RID values of
908 keywords in different places, so we can't derive RID from ID in
909 this language independent code. LOC is the location of the
910 function. */
912 tree
913 fname_decl (location_t loc, unsigned int rid, tree id)
915 unsigned ix;
916 tree decl = NULL_TREE;
918 for (ix = 0; fname_vars[ix].decl; ix++)
919 if (fname_vars[ix].rid == rid)
920 break;
922 decl = *fname_vars[ix].decl;
923 if (!decl)
925 /* If a tree is built here, it would normally have the lineno of
926 the current statement. Later this tree will be moved to the
927 beginning of the function and this line number will be wrong.
928 To avoid this problem set the lineno to 0 here; that prevents
929 it from appearing in the RTL. */
930 tree stmts;
931 location_t saved_location = input_location;
932 input_location = UNKNOWN_LOCATION;
934 stmts = push_stmt_list ();
935 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
936 stmts = pop_stmt_list (stmts);
937 if (!IS_EMPTY_STMT (stmts))
938 saved_function_name_decls
939 = tree_cons (decl, stmts, saved_function_name_decls);
940 *fname_vars[ix].decl = decl;
941 input_location = saved_location;
943 if (!ix && !current_function_decl)
944 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
946 return decl;
949 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
951 tree
952 fix_string_type (tree value)
954 int length = TREE_STRING_LENGTH (value);
955 int nchars;
956 tree e_type, i_type, a_type;
958 /* Compute the number of elements, for the array type. */
959 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
961 nchars = length;
962 e_type = char_type_node;
964 else if (TREE_TYPE (value) == char16_array_type_node)
966 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
967 e_type = char16_type_node;
969 else if (TREE_TYPE (value) == char32_array_type_node)
971 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
972 e_type = char32_type_node;
974 else
976 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
977 e_type = wchar_type_node;
980 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
981 limit in C++98 Annex B is very large (65536) and is not normative,
982 so we do not diagnose it (warn_overlength_strings is forced off
983 in c_common_post_options). */
984 if (warn_overlength_strings)
986 const int nchars_max = flag_isoc99 ? 4095 : 509;
987 const int relevant_std = flag_isoc99 ? 99 : 90;
988 if (nchars - 1 > nchars_max)
989 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
990 separate the %d from the 'C'. 'ISO' should not be
991 translated, but it may be moved after 'C%d' in languages
992 where modifiers follow nouns. */
993 pedwarn (input_location, OPT_Woverlength_strings,
994 "string length %qd is greater than the length %qd "
995 "ISO C%d compilers are required to support",
996 nchars - 1, nchars_max, relevant_std);
999 /* Create the array type for the string constant. The ISO C++
1000 standard says that a string literal has type `const char[N]' or
1001 `const wchar_t[N]'. We use the same logic when invoked as a C
1002 front-end with -Wwrite-strings.
1003 ??? We should change the type of an expression depending on the
1004 state of a warning flag. We should just be warning -- see how
1005 this is handled in the C++ front-end for the deprecated implicit
1006 conversion from string literals to `char*' or `wchar_t*'.
1008 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1009 array type being the unqualified version of that type.
1010 Therefore, if we are constructing an array of const char, we must
1011 construct the matching unqualified array type first. The C front
1012 end does not require this, but it does no harm, so we do it
1013 unconditionally. */
1014 i_type = build_index_type (size_int (nchars - 1));
1015 a_type = build_array_type (e_type, i_type);
1016 if (c_dialect_cxx() || warn_write_strings)
1017 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1019 TREE_TYPE (value) = a_type;
1020 TREE_CONSTANT (value) = 1;
1021 TREE_READONLY (value) = 1;
1022 TREE_STATIC (value) = 1;
1023 return value;
1026 /* If DISABLE is true, stop issuing warnings. This is used when
1027 parsing code that we know will not be executed. This function may
1028 be called multiple times, and works as a stack. */
1030 static void
1031 c_disable_warnings (bool disable)
1033 if (disable)
1035 ++c_inhibit_evaluation_warnings;
1036 fold_defer_overflow_warnings ();
1040 /* If ENABLE is true, reenable issuing warnings. */
1042 static void
1043 c_enable_warnings (bool enable)
1045 if (enable)
1047 --c_inhibit_evaluation_warnings;
1048 fold_undefer_and_ignore_overflow_warnings ();
1052 /* Fully fold EXPR, an expression that was not folded (beyond integer
1053 constant expressions and null pointer constants) when being built
1054 up. If IN_INIT, this is in a static initializer and certain
1055 changes are made to the folding done. Clear *MAYBE_CONST if
1056 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1057 expression because it contains an evaluated operator (in C99) or an
1058 operator outside of sizeof returning an integer constant (in C90)
1059 not permitted in constant expressions, or because it contains an
1060 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1061 set to true by callers before calling this function.) Return the
1062 folded expression. Function arguments have already been folded
1063 before calling this function, as have the contents of SAVE_EXPR,
1064 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1065 C_MAYBE_CONST_EXPR. */
1067 tree
1068 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1070 tree ret;
1071 tree eptype = NULL_TREE;
1072 bool dummy = true;
1073 bool maybe_const_itself = true;
1074 location_t loc = EXPR_LOCATION (expr);
1076 /* This function is not relevant to C++ because C++ folds while
1077 parsing, and may need changes to be correct for C++ when C++
1078 stops folding while parsing. */
1079 if (c_dialect_cxx ())
1080 gcc_unreachable ();
1082 if (!maybe_const)
1083 maybe_const = &dummy;
1084 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1086 eptype = TREE_TYPE (expr);
1087 expr = TREE_OPERAND (expr, 0);
1089 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1090 &maybe_const_itself);
1091 if (eptype)
1092 ret = fold_convert_loc (loc, eptype, ret);
1093 *maybe_const &= maybe_const_itself;
1094 return ret;
1097 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1098 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1099 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1100 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1101 both evaluated and unevaluated subexpressions while
1102 *MAYBE_CONST_ITSELF is carried from only evaluated
1103 subexpressions). */
1105 static tree
1106 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1107 bool *maybe_const_itself)
1109 tree ret = expr;
1110 enum tree_code code = TREE_CODE (expr);
1111 enum tree_code_class kind = TREE_CODE_CLASS (code);
1112 location_t loc = EXPR_LOCATION (expr);
1113 tree op0, op1, op2, op3;
1114 tree orig_op0, orig_op1, orig_op2;
1115 bool op0_const = true, op1_const = true, op2_const = true;
1116 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1117 bool nowarning = TREE_NO_WARNING (expr);
1118 bool unused_p;
1120 /* This function is not relevant to C++ because C++ folds while
1121 parsing, and may need changes to be correct for C++ when C++
1122 stops folding while parsing. */
1123 if (c_dialect_cxx ())
1124 gcc_unreachable ();
1126 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1127 anything else not counted as an expression cannot usefully be
1128 folded further at this point. */
1129 if (!IS_EXPR_CODE_CLASS (kind)
1130 || kind == tcc_statement
1131 || code == SAVE_EXPR)
1132 return expr;
1134 /* Operands of variable-length expressions (function calls) have
1135 already been folded, as have __builtin_* function calls, and such
1136 expressions cannot occur in constant expressions. */
1137 if (kind == tcc_vl_exp)
1139 *maybe_const_operands = false;
1140 ret = fold (expr);
1141 goto out;
1144 if (code == C_MAYBE_CONST_EXPR)
1146 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1147 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1148 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1149 *maybe_const_operands = false;
1150 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1151 *maybe_const_itself = false;
1152 if (pre && !in_init)
1153 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1154 else
1155 ret = inner;
1156 goto out;
1159 /* Assignment, increment, decrement, function call and comma
1160 operators, and statement expressions, cannot occur in constant
1161 expressions if evaluated / outside of sizeof. (Function calls
1162 were handled above, though VA_ARG_EXPR is treated like a function
1163 call here, and statement expressions are handled through
1164 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1165 switch (code)
1167 case MODIFY_EXPR:
1168 case PREDECREMENT_EXPR:
1169 case PREINCREMENT_EXPR:
1170 case POSTDECREMENT_EXPR:
1171 case POSTINCREMENT_EXPR:
1172 case COMPOUND_EXPR:
1173 *maybe_const_operands = false;
1174 break;
1176 case VA_ARG_EXPR:
1177 case TARGET_EXPR:
1178 case BIND_EXPR:
1179 case OBJ_TYPE_REF:
1180 *maybe_const_operands = false;
1181 ret = fold (expr);
1182 goto out;
1184 default:
1185 break;
1188 /* Fold individual tree codes as appropriate. */
1189 switch (code)
1191 case COMPOUND_LITERAL_EXPR:
1192 /* Any non-constancy will have been marked in a containing
1193 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1194 goto out;
1196 case COMPONENT_REF:
1197 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1198 op1 = TREE_OPERAND (expr, 1);
1199 op2 = TREE_OPERAND (expr, 2);
1200 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1201 maybe_const_itself);
1202 STRIP_TYPE_NOPS (op0);
1203 if (op0 != orig_op0)
1204 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1205 if (ret != expr)
1207 TREE_READONLY (ret) = TREE_READONLY (expr);
1208 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1210 goto out;
1212 case ARRAY_REF:
1213 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1214 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1215 op2 = TREE_OPERAND (expr, 2);
1216 op3 = TREE_OPERAND (expr, 3);
1217 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1218 maybe_const_itself);
1219 STRIP_TYPE_NOPS (op0);
1220 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1221 maybe_const_itself);
1222 STRIP_TYPE_NOPS (op1);
1223 op1 = decl_constant_value_for_optimization (op1);
1224 if (op0 != orig_op0 || op1 != orig_op1)
1225 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1226 if (ret != expr)
1228 TREE_READONLY (ret) = TREE_READONLY (expr);
1229 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1230 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1232 ret = fold (ret);
1233 goto out;
1235 case COMPOUND_EXPR:
1236 case MODIFY_EXPR:
1237 case PREDECREMENT_EXPR:
1238 case PREINCREMENT_EXPR:
1239 case POSTDECREMENT_EXPR:
1240 case POSTINCREMENT_EXPR:
1241 case PLUS_EXPR:
1242 case MINUS_EXPR:
1243 case MULT_EXPR:
1244 case POINTER_PLUS_EXPR:
1245 case TRUNC_DIV_EXPR:
1246 case CEIL_DIV_EXPR:
1247 case FLOOR_DIV_EXPR:
1248 case TRUNC_MOD_EXPR:
1249 case RDIV_EXPR:
1250 case EXACT_DIV_EXPR:
1251 case LSHIFT_EXPR:
1252 case RSHIFT_EXPR:
1253 case BIT_IOR_EXPR:
1254 case BIT_XOR_EXPR:
1255 case BIT_AND_EXPR:
1256 case LT_EXPR:
1257 case LE_EXPR:
1258 case GT_EXPR:
1259 case GE_EXPR:
1260 case EQ_EXPR:
1261 case NE_EXPR:
1262 case COMPLEX_EXPR:
1263 case TRUTH_AND_EXPR:
1264 case TRUTH_OR_EXPR:
1265 case TRUTH_XOR_EXPR:
1266 case UNORDERED_EXPR:
1267 case ORDERED_EXPR:
1268 case UNLT_EXPR:
1269 case UNLE_EXPR:
1270 case UNGT_EXPR:
1271 case UNGE_EXPR:
1272 case UNEQ_EXPR:
1273 /* Binary operations evaluating both arguments (increment and
1274 decrement are binary internally in GCC). */
1275 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1276 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1277 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1278 maybe_const_itself);
1279 STRIP_TYPE_NOPS (op0);
1280 if (code != MODIFY_EXPR
1281 && code != PREDECREMENT_EXPR
1282 && code != PREINCREMENT_EXPR
1283 && code != POSTDECREMENT_EXPR
1284 && code != POSTINCREMENT_EXPR)
1285 op0 = decl_constant_value_for_optimization (op0);
1286 /* The RHS of a MODIFY_EXPR was fully folded when building that
1287 expression for the sake of conversion warnings. */
1288 if (code != MODIFY_EXPR)
1289 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1290 maybe_const_itself);
1291 STRIP_TYPE_NOPS (op1);
1292 op1 = decl_constant_value_for_optimization (op1);
1293 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1294 ret = in_init
1295 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1296 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1297 else
1298 ret = fold (expr);
1299 if (TREE_OVERFLOW_P (ret)
1300 && !TREE_OVERFLOW_P (op0)
1301 && !TREE_OVERFLOW_P (op1))
1302 overflow_warning (EXPR_LOCATION (expr), ret);
1303 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1304 && TREE_CODE (orig_op1) != INTEGER_CST
1305 && TREE_CODE (op1) == INTEGER_CST
1306 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1307 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1308 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1309 && c_inhibit_evaluation_warnings == 0)
1311 if (tree_int_cst_sgn (op1) < 0)
1312 warning_at (loc, 0, (code == LSHIFT_EXPR
1313 ? G_("left shift count is negative")
1314 : G_("right shift count is negative")));
1315 else if (compare_tree_int (op1,
1316 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1317 >= 0)
1318 warning_at (loc, 0, (code == LSHIFT_EXPR
1319 ? G_("left shift count >= width of type")
1320 : G_("right shift count >= width of type")));
1322 goto out;
1324 case INDIRECT_REF:
1325 case FIX_TRUNC_EXPR:
1326 case FLOAT_EXPR:
1327 CASE_CONVERT:
1328 case VIEW_CONVERT_EXPR:
1329 case NON_LVALUE_EXPR:
1330 case NEGATE_EXPR:
1331 case BIT_NOT_EXPR:
1332 case TRUTH_NOT_EXPR:
1333 case ADDR_EXPR:
1334 case CONJ_EXPR:
1335 case REALPART_EXPR:
1336 case IMAGPART_EXPR:
1337 /* Unary operations. */
1338 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1339 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1340 maybe_const_itself);
1341 STRIP_TYPE_NOPS (op0);
1342 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1343 op0 = decl_constant_value_for_optimization (op0);
1344 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1345 not prepared to deal with them if they occur in initializers. */
1346 if (op0 != orig_op0
1347 && code == ADDR_EXPR
1348 && (op1 = get_base_address (op0)) != NULL_TREE
1349 && TREE_CODE (op1) == INDIRECT_REF
1350 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1351 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1352 else if (op0 != orig_op0 || in_init)
1353 ret = in_init
1354 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1355 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1356 else
1357 ret = fold (expr);
1358 if (code == INDIRECT_REF
1359 && ret != expr
1360 && TREE_CODE (ret) == INDIRECT_REF)
1362 TREE_READONLY (ret) = TREE_READONLY (expr);
1363 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1364 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1366 switch (code)
1368 case FIX_TRUNC_EXPR:
1369 case FLOAT_EXPR:
1370 CASE_CONVERT:
1371 /* Don't warn about explicit conversions. We will already
1372 have warned about suspect implicit conversions. */
1373 break;
1375 default:
1376 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1377 overflow_warning (EXPR_LOCATION (expr), ret);
1378 break;
1380 goto out;
1382 case TRUTH_ANDIF_EXPR:
1383 case TRUTH_ORIF_EXPR:
1384 /* Binary operations not necessarily evaluating both
1385 arguments. */
1386 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1387 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1388 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1389 STRIP_TYPE_NOPS (op0);
1391 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1392 ? truthvalue_false_node
1393 : truthvalue_true_node));
1394 c_disable_warnings (unused_p);
1395 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1396 STRIP_TYPE_NOPS (op1);
1397 c_enable_warnings (unused_p);
1399 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1400 ret = in_init
1401 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1402 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1403 else
1404 ret = fold (expr);
1405 *maybe_const_operands &= op0_const;
1406 *maybe_const_itself &= op0_const_self;
1407 if (!(flag_isoc99
1408 && op0_const
1409 && op0_const_self
1410 && (code == TRUTH_ANDIF_EXPR
1411 ? op0 == truthvalue_false_node
1412 : op0 == truthvalue_true_node)))
1413 *maybe_const_operands &= op1_const;
1414 if (!(op0_const
1415 && op0_const_self
1416 && (code == TRUTH_ANDIF_EXPR
1417 ? op0 == truthvalue_false_node
1418 : op0 == truthvalue_true_node)))
1419 *maybe_const_itself &= op1_const_self;
1420 goto out;
1422 case COND_EXPR:
1423 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1424 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1425 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1426 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1428 STRIP_TYPE_NOPS (op0);
1429 c_disable_warnings (op0 == truthvalue_false_node);
1430 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1431 STRIP_TYPE_NOPS (op1);
1432 c_enable_warnings (op0 == truthvalue_false_node);
1434 c_disable_warnings (op0 == truthvalue_true_node);
1435 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1436 STRIP_TYPE_NOPS (op2);
1437 c_enable_warnings (op0 == truthvalue_true_node);
1439 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1440 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1441 else
1442 ret = fold (expr);
1443 *maybe_const_operands &= op0_const;
1444 *maybe_const_itself &= op0_const_self;
1445 if (!(flag_isoc99
1446 && op0_const
1447 && op0_const_self
1448 && op0 == truthvalue_false_node))
1449 *maybe_const_operands &= op1_const;
1450 if (!(op0_const
1451 && op0_const_self
1452 && op0 == truthvalue_false_node))
1453 *maybe_const_itself &= op1_const_self;
1454 if (!(flag_isoc99
1455 && op0_const
1456 && op0_const_self
1457 && op0 == truthvalue_true_node))
1458 *maybe_const_operands &= op2_const;
1459 if (!(op0_const
1460 && op0_const_self
1461 && op0 == truthvalue_true_node))
1462 *maybe_const_itself &= op2_const_self;
1463 goto out;
1465 case EXCESS_PRECISION_EXPR:
1466 /* Each case where an operand with excess precision may be
1467 encountered must remove the EXCESS_PRECISION_EXPR around
1468 inner operands and possibly put one around the whole
1469 expression or possibly convert to the semantic type (which
1470 c_fully_fold does); we cannot tell at this stage which is
1471 appropriate in any particular case. */
1472 gcc_unreachable ();
1474 default:
1475 /* Various codes may appear through folding built-in functions
1476 and their arguments. */
1477 goto out;
1480 out:
1481 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1482 have been done by this point, so remove them again. */
1483 nowarning |= TREE_NO_WARNING (ret);
1484 STRIP_TYPE_NOPS (ret);
1485 if (nowarning && !TREE_NO_WARNING (ret))
1487 if (!CAN_HAVE_LOCATION_P (ret))
1488 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1489 TREE_NO_WARNING (ret) = 1;
1491 if (ret != expr)
1492 protected_set_expr_location (ret, loc);
1493 return ret;
1496 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1497 return EXP. Otherwise, return either EXP or its known constant
1498 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1499 Is the BLKmode test appropriate? */
1501 tree
1502 decl_constant_value_for_optimization (tree exp)
1504 tree ret;
1506 /* This function is only used by C, for c_fully_fold and other
1507 optimization, and may not be correct for C++. */
1508 if (c_dialect_cxx ())
1509 gcc_unreachable ();
1511 if (!optimize
1512 || TREE_CODE (exp) != VAR_DECL
1513 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1514 || DECL_MODE (exp) == BLKmode)
1515 return exp;
1517 ret = decl_constant_value (exp);
1518 /* Avoid unwanted tree sharing between the initializer and current
1519 function's body where the tree can be modified e.g. by the
1520 gimplifier. */
1521 if (ret != exp && TREE_STATIC (exp))
1522 ret = unshare_expr (ret);
1523 return ret;
1526 /* Print a warning if a constant expression had overflow in folding.
1527 Invoke this function on every expression that the language
1528 requires to be a constant expression.
1529 Note the ANSI C standard says it is erroneous for a
1530 constant expression to overflow. */
1532 void
1533 constant_expression_warning (tree value)
1535 if (warn_overflow && pedantic
1536 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1537 || TREE_CODE (value) == FIXED_CST
1538 || TREE_CODE (value) == VECTOR_CST
1539 || TREE_CODE (value) == COMPLEX_CST)
1540 && TREE_OVERFLOW (value))
1541 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1544 /* The same as above but print an unconditional error. */
1545 void
1546 constant_expression_error (tree value)
1548 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1549 || TREE_CODE (value) == FIXED_CST
1550 || TREE_CODE (value) == VECTOR_CST
1551 || TREE_CODE (value) == COMPLEX_CST)
1552 && TREE_OVERFLOW (value))
1553 error ("overflow in constant expression");
1556 /* Print a warning if an expression had overflow in folding and its
1557 operands hadn't.
1559 Invoke this function on every expression that
1560 (1) appears in the source code, and
1561 (2) is a constant expression that overflowed, and
1562 (3) is not already checked by convert_and_check;
1563 however, do not invoke this function on operands of explicit casts
1564 or when the expression is the result of an operator and any operand
1565 already overflowed. */
1567 void
1568 overflow_warning (location_t loc, tree value)
1570 if (c_inhibit_evaluation_warnings != 0)
1571 return;
1573 switch (TREE_CODE (value))
1575 case INTEGER_CST:
1576 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1577 break;
1579 case REAL_CST:
1580 warning_at (loc, OPT_Woverflow,
1581 "floating point overflow in expression");
1582 break;
1584 case FIXED_CST:
1585 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1586 break;
1588 case VECTOR_CST:
1589 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1590 break;
1592 case COMPLEX_CST:
1593 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1594 warning_at (loc, OPT_Woverflow,
1595 "complex integer overflow in expression");
1596 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1597 warning_at (loc, OPT_Woverflow,
1598 "complex floating point overflow in expression");
1599 break;
1601 default:
1602 break;
1606 /* Warn about uses of logical || / && operator in a context where it
1607 is likely that the bitwise equivalent was intended by the
1608 programmer. We have seen an expression in which CODE is a binary
1609 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1610 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1611 void
1612 warn_logical_operator (location_t location, enum tree_code code, tree type,
1613 enum tree_code code_left, tree op_left,
1614 enum tree_code ARG_UNUSED (code_right), tree op_right)
1616 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1617 int in0_p, in1_p, in_p;
1618 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1619 bool strict_overflow_p = false;
1621 if (code != TRUTH_ANDIF_EXPR
1622 && code != TRUTH_AND_EXPR
1623 && code != TRUTH_ORIF_EXPR
1624 && code != TRUTH_OR_EXPR)
1625 return;
1627 /* Warn if &&/|| are being used in a context where it is
1628 likely that the bitwise equivalent was intended by the
1629 programmer. That is, an expression such as op && MASK
1630 where op should not be any boolean expression, nor a
1631 constant, and mask seems to be a non-boolean integer constant. */
1632 if (!truth_value_p (code_left)
1633 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1634 && !CONSTANT_CLASS_P (op_left)
1635 && !TREE_NO_WARNING (op_left)
1636 && TREE_CODE (op_right) == INTEGER_CST
1637 && !integer_zerop (op_right)
1638 && !integer_onep (op_right))
1640 if (or_op)
1641 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1642 " applied to non-boolean constant");
1643 else
1644 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1645 " applied to non-boolean constant");
1646 TREE_NO_WARNING (op_left) = true;
1647 return;
1650 /* We do not warn for constants because they are typical of macro
1651 expansions that test for features. */
1652 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1653 return;
1655 /* This warning only makes sense with logical operands. */
1656 if (!(truth_value_p (TREE_CODE (op_left))
1657 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1658 || !(truth_value_p (TREE_CODE (op_right))
1659 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1660 return;
1663 /* We first test whether either side separately is trivially true
1664 (with OR) or trivially false (with AND). If so, do not warn.
1665 This is a common idiom for testing ranges of data types in
1666 portable code. */
1667 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1668 if (!lhs)
1669 return;
1670 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1671 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1673 /* If this is an OR operation, invert both sides; now, the result
1674 should be always false to get a warning. */
1675 if (or_op)
1676 in0_p = !in0_p;
1678 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1679 if (tem && integer_zerop (tem))
1680 return;
1682 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1683 if (!rhs)
1684 return;
1685 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1686 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1688 /* If this is an OR operation, invert both sides; now, the result
1689 should be always false to get a warning. */
1690 if (or_op)
1691 in1_p = !in1_p;
1693 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1694 if (tem && integer_zerop (tem))
1695 return;
1697 /* If both expressions have the same operand, if we can merge the
1698 ranges, and if the range test is always false, then warn. */
1699 if (operand_equal_p (lhs, rhs, 0)
1700 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1701 in1_p, low1, high1)
1702 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1703 type, lhs, in_p, low, high))
1704 && integer_zerop (tem))
1706 if (or_op)
1707 warning_at (location, OPT_Wlogical_op,
1708 "logical %<or%> "
1709 "of collectively exhaustive tests is always true");
1710 else
1711 warning_at (location, OPT_Wlogical_op,
1712 "logical %<and%> "
1713 "of mutually exclusive tests is always false");
1718 /* Warn if EXP contains any computations whose results are not used.
1719 Return true if a warning is printed; false otherwise. LOCUS is the
1720 (potential) location of the expression. */
1722 bool
1723 warn_if_unused_value (const_tree exp, location_t locus)
1725 restart:
1726 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1727 return false;
1729 /* Don't warn about void constructs. This includes casting to void,
1730 void function calls, and statement expressions with a final cast
1731 to void. */
1732 if (VOID_TYPE_P (TREE_TYPE (exp)))
1733 return false;
1735 if (EXPR_HAS_LOCATION (exp))
1736 locus = EXPR_LOCATION (exp);
1738 switch (TREE_CODE (exp))
1740 case PREINCREMENT_EXPR:
1741 case POSTINCREMENT_EXPR:
1742 case PREDECREMENT_EXPR:
1743 case POSTDECREMENT_EXPR:
1744 case MODIFY_EXPR:
1745 case INIT_EXPR:
1746 case TARGET_EXPR:
1747 case CALL_EXPR:
1748 case TRY_CATCH_EXPR:
1749 case WITH_CLEANUP_EXPR:
1750 case EXIT_EXPR:
1751 case VA_ARG_EXPR:
1752 return false;
1754 case BIND_EXPR:
1755 /* For a binding, warn if no side effect within it. */
1756 exp = BIND_EXPR_BODY (exp);
1757 goto restart;
1759 case SAVE_EXPR:
1760 case NON_LVALUE_EXPR:
1761 case NOP_EXPR:
1762 exp = TREE_OPERAND (exp, 0);
1763 goto restart;
1765 case TRUTH_ORIF_EXPR:
1766 case TRUTH_ANDIF_EXPR:
1767 /* In && or ||, warn if 2nd operand has no side effect. */
1768 exp = TREE_OPERAND (exp, 1);
1769 goto restart;
1771 case COMPOUND_EXPR:
1772 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1773 return true;
1774 /* Let people do `(foo (), 0)' without a warning. */
1775 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1776 return false;
1777 exp = TREE_OPERAND (exp, 1);
1778 goto restart;
1780 case COND_EXPR:
1781 /* If this is an expression with side effects, don't warn; this
1782 case commonly appears in macro expansions. */
1783 if (TREE_SIDE_EFFECTS (exp))
1784 return false;
1785 goto warn;
1787 case INDIRECT_REF:
1788 /* Don't warn about automatic dereferencing of references, since
1789 the user cannot control it. */
1790 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1792 exp = TREE_OPERAND (exp, 0);
1793 goto restart;
1795 /* Fall through. */
1797 default:
1798 /* Referencing a volatile value is a side effect, so don't warn. */
1799 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1800 && TREE_THIS_VOLATILE (exp))
1801 return false;
1803 /* If this is an expression which has no operands, there is no value
1804 to be unused. There are no such language-independent codes,
1805 but front ends may define such. */
1806 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1807 return false;
1809 warn:
1810 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1815 /* Print a warning about casts that might indicate violation
1816 of strict aliasing rules if -Wstrict-aliasing is used and
1817 strict aliasing mode is in effect. OTYPE is the original
1818 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1820 bool
1821 strict_aliasing_warning (tree otype, tree type, tree expr)
1823 /* Strip pointer conversion chains and get to the correct original type. */
1824 STRIP_NOPS (expr);
1825 otype = TREE_TYPE (expr);
1827 if (!(flag_strict_aliasing
1828 && POINTER_TYPE_P (type)
1829 && POINTER_TYPE_P (otype)
1830 && !VOID_TYPE_P (TREE_TYPE (type)))
1831 /* If the type we are casting to is a ref-all pointer
1832 dereferencing it is always valid. */
1833 || TYPE_REF_CAN_ALIAS_ALL (type))
1834 return false;
1836 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1837 && (DECL_P (TREE_OPERAND (expr, 0))
1838 || handled_component_p (TREE_OPERAND (expr, 0))))
1840 /* Casting the address of an object to non void pointer. Warn
1841 if the cast breaks type based aliasing. */
1842 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1844 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1845 "might break strict-aliasing rules");
1846 return true;
1848 else
1850 /* warn_strict_aliasing >= 3. This includes the default (3).
1851 Only warn if the cast is dereferenced immediately. */
1852 alias_set_type set1 =
1853 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1854 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1856 if (set1 != set2 && set2 != 0
1857 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1859 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1860 "pointer will break strict-aliasing rules");
1861 return true;
1863 else if (warn_strict_aliasing == 2
1864 && !alias_sets_must_conflict_p (set1, set2))
1866 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1867 "pointer might break strict-aliasing rules");
1868 return true;
1872 else
1873 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1875 /* At this level, warn for any conversions, even if an address is
1876 not taken in the same statement. This will likely produce many
1877 false positives, but could be useful to pinpoint problems that
1878 are not revealed at higher levels. */
1879 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1880 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1881 if (!COMPLETE_TYPE_P (type)
1882 || !alias_sets_must_conflict_p (set1, set2))
1884 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1885 "pointer might break strict-aliasing rules");
1886 return true;
1890 return false;
1893 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1894 sizeof as last operand of certain builtins. */
1896 void
1897 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1898 vec<tree, va_gc> *params, tree *sizeof_arg,
1899 bool (*comp_types) (tree, tree))
1901 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1902 bool strop = false, cmp = false;
1903 unsigned int idx = ~0;
1904 location_t loc;
1906 if (TREE_CODE (callee) != FUNCTION_DECL
1907 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1908 || vec_safe_length (params) <= 1)
1909 return;
1911 switch (DECL_FUNCTION_CODE (callee))
1913 case BUILT_IN_STRNCMP:
1914 case BUILT_IN_STRNCASECMP:
1915 cmp = true;
1916 /* FALLTHRU */
1917 case BUILT_IN_STRNCPY:
1918 case BUILT_IN_STRNCPY_CHK:
1919 case BUILT_IN_STRNCAT:
1920 case BUILT_IN_STRNCAT_CHK:
1921 case BUILT_IN_STPNCPY:
1922 case BUILT_IN_STPNCPY_CHK:
1923 strop = true;
1924 /* FALLTHRU */
1925 case BUILT_IN_MEMCPY:
1926 case BUILT_IN_MEMCPY_CHK:
1927 case BUILT_IN_MEMMOVE:
1928 case BUILT_IN_MEMMOVE_CHK:
1929 if (params->length () < 3)
1930 return;
1931 src = (*params)[1];
1932 dest = (*params)[0];
1933 idx = 2;
1934 break;
1935 case BUILT_IN_BCOPY:
1936 if (params->length () < 3)
1937 return;
1938 src = (*params)[0];
1939 dest = (*params)[1];
1940 idx = 2;
1941 break;
1942 case BUILT_IN_MEMCMP:
1943 case BUILT_IN_BCMP:
1944 if (params->length () < 3)
1945 return;
1946 src = (*params)[1];
1947 dest = (*params)[0];
1948 idx = 2;
1949 cmp = true;
1950 break;
1951 case BUILT_IN_MEMSET:
1952 case BUILT_IN_MEMSET_CHK:
1953 if (params->length () < 3)
1954 return;
1955 dest = (*params)[0];
1956 idx = 2;
1957 break;
1958 case BUILT_IN_BZERO:
1959 dest = (*params)[0];
1960 idx = 1;
1961 break;
1962 case BUILT_IN_STRNDUP:
1963 src = (*params)[0];
1964 strop = true;
1965 idx = 1;
1966 break;
1967 case BUILT_IN_MEMCHR:
1968 if (params->length () < 3)
1969 return;
1970 src = (*params)[0];
1971 idx = 2;
1972 break;
1973 case BUILT_IN_SNPRINTF:
1974 case BUILT_IN_SNPRINTF_CHK:
1975 case BUILT_IN_VSNPRINTF:
1976 case BUILT_IN_VSNPRINTF_CHK:
1977 dest = (*params)[0];
1978 idx = 1;
1979 strop = true;
1980 break;
1981 default:
1982 break;
1985 if (idx >= 3)
1986 return;
1988 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1989 return;
1991 type = TYPE_P (sizeof_arg[idx])
1992 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1993 if (!POINTER_TYPE_P (type))
1994 return;
1996 if (dest
1997 && (tem = tree_strip_nop_conversions (dest))
1998 && POINTER_TYPE_P (TREE_TYPE (tem))
1999 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2000 return;
2002 if (src
2003 && (tem = tree_strip_nop_conversions (src))
2004 && POINTER_TYPE_P (TREE_TYPE (tem))
2005 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2006 return;
2008 loc = sizeof_arg_loc[idx];
2010 if (dest && !cmp)
2012 if (!TYPE_P (sizeof_arg[idx])
2013 && operand_equal_p (dest, sizeof_arg[idx], 0)
2014 && comp_types (TREE_TYPE (dest), type))
2016 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2017 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2018 "argument to %<sizeof%> in %qD call is the same "
2019 "expression as the destination; did you mean to "
2020 "remove the addressof?", callee);
2021 else if ((TYPE_PRECISION (TREE_TYPE (type))
2022 == TYPE_PRECISION (char_type_node))
2023 || strop)
2024 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2025 "argument to %<sizeof%> in %qD call is the same "
2026 "expression as the destination; did you mean to "
2027 "provide an explicit length?", callee);
2028 else
2029 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2030 "argument to %<sizeof%> in %qD call is the same "
2031 "expression as the destination; did you mean to "
2032 "dereference it?", callee);
2033 return;
2036 if (POINTER_TYPE_P (TREE_TYPE (dest))
2037 && !strop
2038 && comp_types (TREE_TYPE (dest), type)
2039 && !VOID_TYPE_P (TREE_TYPE (type)))
2041 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2042 "argument to %<sizeof%> in %qD call is the same "
2043 "pointer type %qT as the destination; expected %qT "
2044 "or an explicit length", callee, TREE_TYPE (dest),
2045 TREE_TYPE (TREE_TYPE (dest)));
2046 return;
2050 if (src && !cmp)
2052 if (!TYPE_P (sizeof_arg[idx])
2053 && operand_equal_p (src, sizeof_arg[idx], 0)
2054 && comp_types (TREE_TYPE (src), type))
2056 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2057 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2058 "argument to %<sizeof%> in %qD call is the same "
2059 "expression as the source; did you mean to "
2060 "remove the addressof?", callee);
2061 else if ((TYPE_PRECISION (TREE_TYPE (type))
2062 == TYPE_PRECISION (char_type_node))
2063 || strop)
2064 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2065 "argument to %<sizeof%> in %qD call is the same "
2066 "expression as the source; did you mean to "
2067 "provide an explicit length?", callee);
2068 else
2069 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2070 "argument to %<sizeof%> in %qD call is the same "
2071 "expression as the source; did you mean to "
2072 "dereference it?", callee);
2073 return;
2076 if (POINTER_TYPE_P (TREE_TYPE (src))
2077 && !strop
2078 && comp_types (TREE_TYPE (src), type)
2079 && !VOID_TYPE_P (TREE_TYPE (type)))
2081 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2082 "argument to %<sizeof%> in %qD call is the same "
2083 "pointer type %qT as the source; expected %qT "
2084 "or an explicit length", callee, TREE_TYPE (src),
2085 TREE_TYPE (TREE_TYPE (src)));
2086 return;
2090 if (dest)
2092 if (!TYPE_P (sizeof_arg[idx])
2093 && operand_equal_p (dest, sizeof_arg[idx], 0)
2094 && comp_types (TREE_TYPE (dest), type))
2096 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2097 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2098 "argument to %<sizeof%> in %qD call is the same "
2099 "expression as the first source; did you mean to "
2100 "remove the addressof?", callee);
2101 else if ((TYPE_PRECISION (TREE_TYPE (type))
2102 == TYPE_PRECISION (char_type_node))
2103 || strop)
2104 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2105 "argument to %<sizeof%> in %qD call is the same "
2106 "expression as the first source; did you mean to "
2107 "provide an explicit length?", callee);
2108 else
2109 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2110 "argument to %<sizeof%> in %qD call is the same "
2111 "expression as the first source; did you mean to "
2112 "dereference it?", callee);
2113 return;
2116 if (POINTER_TYPE_P (TREE_TYPE (dest))
2117 && !strop
2118 && comp_types (TREE_TYPE (dest), type)
2119 && !VOID_TYPE_P (TREE_TYPE (type)))
2121 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2122 "argument to %<sizeof%> in %qD call is the same "
2123 "pointer type %qT as the first source; expected %qT "
2124 "or an explicit length", callee, TREE_TYPE (dest),
2125 TREE_TYPE (TREE_TYPE (dest)));
2126 return;
2130 if (src)
2132 if (!TYPE_P (sizeof_arg[idx])
2133 && operand_equal_p (src, sizeof_arg[idx], 0)
2134 && comp_types (TREE_TYPE (src), type))
2136 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2137 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2138 "argument to %<sizeof%> in %qD call is the same "
2139 "expression as the second source; did you mean to "
2140 "remove the addressof?", callee);
2141 else if ((TYPE_PRECISION (TREE_TYPE (type))
2142 == TYPE_PRECISION (char_type_node))
2143 || strop)
2144 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2145 "argument to %<sizeof%> in %qD call is the same "
2146 "expression as the second source; did you mean to "
2147 "provide an explicit length?", callee);
2148 else
2149 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2150 "argument to %<sizeof%> in %qD call is the same "
2151 "expression as the second source; did you mean to "
2152 "dereference it?", callee);
2153 return;
2156 if (POINTER_TYPE_P (TREE_TYPE (src))
2157 && !strop
2158 && comp_types (TREE_TYPE (src), type)
2159 && !VOID_TYPE_P (TREE_TYPE (type)))
2161 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2162 "argument to %<sizeof%> in %qD call is the same "
2163 "pointer type %qT as the second source; expected %qT "
2164 "or an explicit length", callee, TREE_TYPE (src),
2165 TREE_TYPE (TREE_TYPE (src)));
2166 return;
2172 /* Warn for unlikely, improbable, or stupid DECL declarations
2173 of `main'. */
2175 void
2176 check_main_parameter_types (tree decl)
2178 function_args_iterator iter;
2179 tree type;
2180 int argct = 0;
2182 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2184 /* XXX void_type_node belies the abstraction. */
2185 if (type == void_type_node || type == error_mark_node )
2186 break;
2188 ++argct;
2189 switch (argct)
2191 case 1:
2192 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2193 pedwarn (input_location, OPT_Wmain,
2194 "first argument of %q+D should be %<int%>", decl);
2195 break;
2197 case 2:
2198 if (TREE_CODE (type) != POINTER_TYPE
2199 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2200 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2201 != char_type_node))
2202 pedwarn (input_location, OPT_Wmain,
2203 "second argument of %q+D should be %<char **%>", decl);
2204 break;
2206 case 3:
2207 if (TREE_CODE (type) != POINTER_TYPE
2208 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2209 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2210 != char_type_node))
2211 pedwarn (input_location, OPT_Wmain,
2212 "third argument of %q+D should probably be "
2213 "%<char **%>", decl);
2214 break;
2218 /* It is intentional that this message does not mention the third
2219 argument because it's only mentioned in an appendix of the
2220 standard. */
2221 if (argct > 0 && (argct < 2 || argct > 3))
2222 pedwarn (input_location, OPT_Wmain,
2223 "%q+D takes only zero or two arguments", decl);
2226 /* vector_targets_convertible_p is used for vector pointer types. The
2227 callers perform various checks that the qualifiers are satisfactory,
2228 while OTOH vector_targets_convertible_p ignores the number of elements
2229 in the vectors. That's fine with vector pointers as we can consider,
2230 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2231 and that does not require and conversion of the pointer values.
2232 In contrast, vector_types_convertible_p and
2233 vector_types_compatible_elements_p are used for vector value types. */
2234 /* True if pointers to distinct types T1 and T2 can be converted to
2235 each other without an explicit cast. Only returns true for opaque
2236 vector types. */
2237 bool
2238 vector_targets_convertible_p (const_tree t1, const_tree t2)
2240 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2241 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2242 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2243 return true;
2245 return false;
2248 /* vector_types_convertible_p is used for vector value types.
2249 It could in principle call vector_targets_convertible_p as a subroutine,
2250 but then the check for vector type would be duplicated with its callers,
2251 and also the purpose of vector_targets_convertible_p would become
2252 muddled.
2253 Where vector_types_convertible_p returns true, a conversion might still be
2254 needed to make the types match.
2255 In contrast, vector_targets_convertible_p is used for vector pointer
2256 values, and vector_types_compatible_elements_p is used specifically
2257 in the context for binary operators, as a check if use is possible without
2258 conversion. */
2259 /* True if vector types T1 and T2 can be converted to each other
2260 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2261 can only be converted with -flax-vector-conversions yet that is not
2262 in effect, emit a note telling the user about that option if such
2263 a note has not previously been emitted. */
2264 bool
2265 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2267 static bool emitted_lax_note = false;
2268 bool convertible_lax;
2270 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2271 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2272 return true;
2274 convertible_lax =
2275 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2276 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2277 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2278 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2279 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2281 if (!convertible_lax || flag_lax_vector_conversions)
2282 return convertible_lax;
2284 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2285 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2286 return true;
2288 if (emit_lax_note && !emitted_lax_note)
2290 emitted_lax_note = true;
2291 inform (input_location, "use -flax-vector-conversions to permit "
2292 "conversions between vectors with differing "
2293 "element types or numbers of subparts");
2296 return false;
2299 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2300 and have vector types, V0 has the same type as V1, and the number of
2301 elements of V0, V1, MASK is the same.
2303 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2304 called with two arguments. In this case implementation passes the
2305 first argument twice in order to share the same tree code. This fact
2306 could enable the mask-values being twice the vector length. This is
2307 an implementation accident and this semantics is not guaranteed to
2308 the user. */
2309 tree
2310 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2311 bool complain)
2313 tree ret;
2314 bool wrap = true;
2315 bool maybe_const = false;
2316 bool two_arguments = false;
2318 if (v1 == NULL_TREE)
2320 two_arguments = true;
2321 v1 = v0;
2324 if (v0 == error_mark_node || v1 == error_mark_node
2325 || mask == error_mark_node)
2326 return error_mark_node;
2328 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2329 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2331 if (complain)
2332 error_at (loc, "__builtin_shuffle last argument must "
2333 "be an integer vector");
2334 return error_mark_node;
2337 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2338 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2340 if (complain)
2341 error_at (loc, "__builtin_shuffle arguments must be vectors");
2342 return error_mark_node;
2345 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2347 if (complain)
2348 error_at (loc, "__builtin_shuffle argument vectors must be of "
2349 "the same type");
2350 return error_mark_node;
2353 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2354 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2355 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2356 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2358 if (complain)
2359 error_at (loc, "__builtin_shuffle number of elements of the "
2360 "argument vector(s) and the mask vector should "
2361 "be the same");
2362 return error_mark_node;
2365 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2366 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2368 if (complain)
2369 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2370 "must have the same size as inner type of the mask");
2371 return error_mark_node;
2374 if (!c_dialect_cxx ())
2376 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2377 v0 = c_fully_fold (v0, false, &maybe_const);
2378 wrap &= maybe_const;
2380 if (two_arguments)
2381 v1 = v0 = save_expr (v0);
2382 else
2384 v1 = c_fully_fold (v1, false, &maybe_const);
2385 wrap &= maybe_const;
2388 mask = c_fully_fold (mask, false, &maybe_const);
2389 wrap &= maybe_const;
2391 else if (two_arguments)
2392 v1 = v0 = save_expr (v0);
2394 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2396 if (!c_dialect_cxx () && !wrap)
2397 ret = c_wrap_maybe_const (ret, true);
2399 return ret;
2402 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2403 to integral type. */
2405 static tree
2406 c_common_get_narrower (tree op, int *unsignedp_ptr)
2408 op = get_narrower (op, unsignedp_ptr);
2410 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2411 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2413 /* C++0x scoped enumerations don't implicitly convert to integral
2414 type; if we stripped an explicit conversion to a larger type we
2415 need to replace it so common_type will still work. */
2416 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2417 TYPE_UNSIGNED (TREE_TYPE (op)));
2418 op = fold_convert (type, op);
2420 return op;
2423 /* This is a helper function of build_binary_op.
2425 For certain operations if both args were extended from the same
2426 smaller type, do the arithmetic in that type and then extend.
2428 BITWISE indicates a bitwise operation.
2429 For them, this optimization is safe only if
2430 both args are zero-extended or both are sign-extended.
2431 Otherwise, we might change the result.
2432 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2433 but calculated in (unsigned short) it would be (unsigned short)-1.
2435 tree
2436 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2438 int unsigned0, unsigned1;
2439 tree arg0, arg1;
2440 int uns;
2441 tree type;
2443 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2444 excessive narrowing when we call get_narrower below. For
2445 example, suppose that OP0 is of unsigned int extended
2446 from signed char and that RESULT_TYPE is long long int.
2447 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2448 like
2450 (long long int) (unsigned int) signed_char
2452 which get_narrower would narrow down to
2454 (unsigned int) signed char
2456 If we do not cast OP0 first, get_narrower would return
2457 signed_char, which is inconsistent with the case of the
2458 explicit cast. */
2459 op0 = convert (result_type, op0);
2460 op1 = convert (result_type, op1);
2462 arg0 = c_common_get_narrower (op0, &unsigned0);
2463 arg1 = c_common_get_narrower (op1, &unsigned1);
2465 /* UNS is 1 if the operation to be done is an unsigned one. */
2466 uns = TYPE_UNSIGNED (result_type);
2468 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2469 but it *requires* conversion to FINAL_TYPE. */
2471 if ((TYPE_PRECISION (TREE_TYPE (op0))
2472 == TYPE_PRECISION (TREE_TYPE (arg0)))
2473 && TREE_TYPE (op0) != result_type)
2474 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2475 if ((TYPE_PRECISION (TREE_TYPE (op1))
2476 == TYPE_PRECISION (TREE_TYPE (arg1)))
2477 && TREE_TYPE (op1) != result_type)
2478 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2480 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2482 /* For bitwise operations, signedness of nominal type
2483 does not matter. Consider only how operands were extended. */
2484 if (bitwise)
2485 uns = unsigned0;
2487 /* Note that in all three cases below we refrain from optimizing
2488 an unsigned operation on sign-extended args.
2489 That would not be valid. */
2491 /* Both args variable: if both extended in same way
2492 from same width, do it in that width.
2493 Do it unsigned if args were zero-extended. */
2494 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2495 < TYPE_PRECISION (result_type))
2496 && (TYPE_PRECISION (TREE_TYPE (arg1))
2497 == TYPE_PRECISION (TREE_TYPE (arg0)))
2498 && unsigned0 == unsigned1
2499 && (unsigned0 || !uns))
2500 return c_common_signed_or_unsigned_type
2501 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2503 else if (TREE_CODE (arg0) == INTEGER_CST
2504 && (unsigned1 || !uns)
2505 && (TYPE_PRECISION (TREE_TYPE (arg1))
2506 < TYPE_PRECISION (result_type))
2507 && (type
2508 = c_common_signed_or_unsigned_type (unsigned1,
2509 TREE_TYPE (arg1)))
2510 && !POINTER_TYPE_P (type)
2511 && int_fits_type_p (arg0, type))
2512 return type;
2514 else if (TREE_CODE (arg1) == INTEGER_CST
2515 && (unsigned0 || !uns)
2516 && (TYPE_PRECISION (TREE_TYPE (arg0))
2517 < TYPE_PRECISION (result_type))
2518 && (type
2519 = c_common_signed_or_unsigned_type (unsigned0,
2520 TREE_TYPE (arg0)))
2521 && !POINTER_TYPE_P (type)
2522 && int_fits_type_p (arg1, type))
2523 return type;
2525 return result_type;
2528 /* Checks if expression EXPR of real/integer type cannot be converted
2529 to the real/integer type TYPE. Function returns true when:
2530 * EXPR is a constant which cannot be exactly converted to TYPE
2531 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2532 for EXPR type and TYPE being both integers or both real.
2533 * EXPR is not a constant of real type and TYPE is an integer.
2534 * EXPR is not a constant of integer type which cannot be
2535 exactly converted to real type.
2536 Function allows conversions between types of different signedness and
2537 does not return true in that case. Function can produce signedness
2538 warnings if PRODUCE_WARNS is true. */
2539 bool
2540 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2542 bool give_warning = false;
2543 tree expr_type = TREE_TYPE (expr);
2544 location_t loc = EXPR_LOC_OR_HERE (expr);
2546 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2548 /* Warn for real constant that is not an exact integer converted
2549 to integer type. */
2550 if (TREE_CODE (expr_type) == REAL_TYPE
2551 && TREE_CODE (type) == INTEGER_TYPE)
2553 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2554 give_warning = true;
2556 /* Warn for an integer constant that does not fit into integer type. */
2557 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2558 && TREE_CODE (type) == INTEGER_TYPE
2559 && !int_fits_type_p (expr, type))
2561 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2562 && tree_int_cst_sgn (expr) < 0)
2564 if (produce_warns)
2565 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2566 " implicitly converted to unsigned type");
2568 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2570 if (produce_warns)
2571 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2572 " constant value to negative integer");
2574 else
2575 give_warning = true;
2577 else if (TREE_CODE (type) == REAL_TYPE)
2579 /* Warn for an integer constant that does not fit into real type. */
2580 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2582 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2583 if (!exact_real_truncate (TYPE_MODE (type), &a))
2584 give_warning = true;
2586 /* Warn for a real constant that does not fit into a smaller
2587 real type. */
2588 else if (TREE_CODE (expr_type) == REAL_TYPE
2589 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2591 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2592 if (!exact_real_truncate (TYPE_MODE (type), &a))
2593 give_warning = true;
2597 else
2599 /* Warn for real types converted to integer types. */
2600 if (TREE_CODE (expr_type) == REAL_TYPE
2601 && TREE_CODE (type) == INTEGER_TYPE)
2602 give_warning = true;
2604 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2605 && TREE_CODE (type) == INTEGER_TYPE)
2607 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2608 expr = get_unwidened (expr, 0);
2609 expr_type = TREE_TYPE (expr);
2611 /* Don't warn for short y; short x = ((int)y & 0xff); */
2612 if (TREE_CODE (expr) == BIT_AND_EXPR
2613 || TREE_CODE (expr) == BIT_IOR_EXPR
2614 || TREE_CODE (expr) == BIT_XOR_EXPR)
2616 /* If both args were extended from a shortest type,
2617 use that type if that is safe. */
2618 expr_type = shorten_binary_op (expr_type,
2619 TREE_OPERAND (expr, 0),
2620 TREE_OPERAND (expr, 1),
2621 /* bitwise */1);
2623 if (TREE_CODE (expr) == BIT_AND_EXPR)
2625 tree op0 = TREE_OPERAND (expr, 0);
2626 tree op1 = TREE_OPERAND (expr, 1);
2627 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2628 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2630 /* If one of the operands is a non-negative constant
2631 that fits in the target type, then the type of the
2632 other operand does not matter. */
2633 if ((TREE_CODE (op0) == INTEGER_CST
2634 && int_fits_type_p (op0, c_common_signed_type (type))
2635 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2636 || (TREE_CODE (op1) == INTEGER_CST
2637 && int_fits_type_p (op1, c_common_signed_type (type))
2638 && int_fits_type_p (op1,
2639 c_common_unsigned_type (type))))
2640 return false;
2641 /* If constant is unsigned and fits in the target
2642 type, then the result will also fit. */
2643 else if ((TREE_CODE (op0) == INTEGER_CST
2644 && unsigned0
2645 && int_fits_type_p (op0, type))
2646 || (TREE_CODE (op1) == INTEGER_CST
2647 && unsigned1
2648 && int_fits_type_p (op1, type)))
2649 return false;
2652 /* Warn for integer types converted to smaller integer types. */
2653 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2654 give_warning = true;
2656 /* When they are the same width but different signedness,
2657 then the value may change. */
2658 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2659 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2660 /* Even when converted to a bigger type, if the type is
2661 unsigned but expr is signed, then negative values
2662 will be changed. */
2663 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2664 && produce_warns)
2665 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2666 "may change the sign of the result",
2667 type, expr_type);
2670 /* Warn for integer types converted to real types if and only if
2671 all the range of values of the integer type cannot be
2672 represented by the real type. */
2673 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2674 && TREE_CODE (type) == REAL_TYPE)
2676 tree type_low_bound, type_high_bound;
2677 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2679 /* Don't warn about char y = 0xff; float x = (int) y; */
2680 expr = get_unwidened (expr, 0);
2681 expr_type = TREE_TYPE (expr);
2683 type_low_bound = TYPE_MIN_VALUE (expr_type);
2684 type_high_bound = TYPE_MAX_VALUE (expr_type);
2685 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2686 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2688 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2689 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2690 give_warning = true;
2693 /* Warn for real types converted to smaller real types. */
2694 else if (TREE_CODE (expr_type) == REAL_TYPE
2695 && TREE_CODE (type) == REAL_TYPE
2696 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2697 give_warning = true;
2700 return give_warning;
2703 /* Warns if the conversion of EXPR to TYPE may alter a value.
2704 This is a helper function for warnings_for_convert_and_check. */
2706 static void
2707 conversion_warning (tree type, tree expr)
2709 tree expr_type = TREE_TYPE (expr);
2710 location_t loc = EXPR_LOC_OR_HERE (expr);
2712 if (!warn_conversion && !warn_sign_conversion)
2713 return;
2715 switch (TREE_CODE (expr))
2717 case EQ_EXPR:
2718 case NE_EXPR:
2719 case LE_EXPR:
2720 case GE_EXPR:
2721 case LT_EXPR:
2722 case GT_EXPR:
2723 case TRUTH_ANDIF_EXPR:
2724 case TRUTH_ORIF_EXPR:
2725 case TRUTH_AND_EXPR:
2726 case TRUTH_OR_EXPR:
2727 case TRUTH_XOR_EXPR:
2728 case TRUTH_NOT_EXPR:
2729 /* Conversion from boolean to a signed:1 bit-field (which only
2730 can hold the values 0 and -1) doesn't lose information - but
2731 it does change the value. */
2732 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2733 warning_at (loc, OPT_Wconversion,
2734 "conversion to %qT from boolean expression", type);
2735 return;
2737 case REAL_CST:
2738 case INTEGER_CST:
2739 if (unsafe_conversion_p (type, expr, true))
2740 warning_at (loc, OPT_Wconversion,
2741 "conversion to %qT alters %qT constant value",
2742 type, expr_type);
2743 return;
2745 case COND_EXPR:
2747 /* In case of COND_EXPR, we do not care about the type of
2748 COND_EXPR, only about the conversion of each operand. */
2749 tree op1 = TREE_OPERAND (expr, 1);
2750 tree op2 = TREE_OPERAND (expr, 2);
2752 conversion_warning (type, op1);
2753 conversion_warning (type, op2);
2754 return;
2757 default: /* 'expr' is not a constant. */
2758 if (unsafe_conversion_p (type, expr, true))
2759 warning_at (loc, OPT_Wconversion,
2760 "conversion to %qT from %qT may alter its value",
2761 type, expr_type);
2765 /* Produce warnings after a conversion. RESULT is the result of
2766 converting EXPR to TYPE. This is a helper function for
2767 convert_and_check and cp_convert_and_check. */
2769 void
2770 warnings_for_convert_and_check (tree type, tree expr, tree result)
2772 location_t loc = EXPR_LOC_OR_HERE (expr);
2774 if (TREE_CODE (expr) == INTEGER_CST
2775 && (TREE_CODE (type) == INTEGER_TYPE
2776 || TREE_CODE (type) == ENUMERAL_TYPE)
2777 && !int_fits_type_p (expr, type))
2779 /* Do not diagnose overflow in a constant expression merely
2780 because a conversion overflowed. */
2781 if (TREE_OVERFLOW (result))
2782 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2784 if (TYPE_UNSIGNED (type))
2786 /* This detects cases like converting -129 or 256 to
2787 unsigned char. */
2788 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2789 warning_at (loc, OPT_Woverflow,
2790 "large integer implicitly truncated to unsigned type");
2791 else
2792 conversion_warning (type, expr);
2794 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2795 warning (OPT_Woverflow,
2796 "overflow in implicit constant conversion");
2797 /* No warning for converting 0x80000000 to int. */
2798 else if (pedantic
2799 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2800 || TYPE_PRECISION (TREE_TYPE (expr))
2801 != TYPE_PRECISION (type)))
2802 warning_at (loc, OPT_Woverflow,
2803 "overflow in implicit constant conversion");
2805 else
2806 conversion_warning (type, expr);
2808 else if ((TREE_CODE (result) == INTEGER_CST
2809 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2810 warning_at (loc, OPT_Woverflow,
2811 "overflow in implicit constant conversion");
2812 else
2813 conversion_warning (type, expr);
2817 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2818 Invoke this function on every expression that is converted implicitly,
2819 i.e. because of language rules and not because of an explicit cast. */
2821 tree
2822 convert_and_check (tree type, tree expr)
2824 tree result;
2825 tree expr_for_warning;
2827 /* Convert from a value with possible excess precision rather than
2828 via the semantic type, but do not warn about values not fitting
2829 exactly in the semantic type. */
2830 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2832 tree orig_type = TREE_TYPE (expr);
2833 expr = TREE_OPERAND (expr, 0);
2834 expr_for_warning = convert (orig_type, expr);
2835 if (orig_type == type)
2836 return expr_for_warning;
2838 else
2839 expr_for_warning = expr;
2841 if (TREE_TYPE (expr) == type)
2842 return expr;
2844 result = convert (type, expr);
2846 if (c_inhibit_evaluation_warnings == 0
2847 && !TREE_OVERFLOW_P (expr)
2848 && result != error_mark_node)
2849 warnings_for_convert_and_check (type, expr_for_warning, result);
2851 return result;
2854 /* A node in a list that describes references to variables (EXPR), which are
2855 either read accesses if WRITER is zero, or write accesses, in which case
2856 WRITER is the parent of EXPR. */
2857 struct tlist
2859 struct tlist *next;
2860 tree expr, writer;
2863 /* Used to implement a cache the results of a call to verify_tree. We only
2864 use this for SAVE_EXPRs. */
2865 struct tlist_cache
2867 struct tlist_cache *next;
2868 struct tlist *cache_before_sp;
2869 struct tlist *cache_after_sp;
2870 tree expr;
2873 /* Obstack to use when allocating tlist structures, and corresponding
2874 firstobj. */
2875 static struct obstack tlist_obstack;
2876 static char *tlist_firstobj = 0;
2878 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2879 warnings. */
2880 static struct tlist *warned_ids;
2881 /* SAVE_EXPRs need special treatment. We process them only once and then
2882 cache the results. */
2883 static struct tlist_cache *save_expr_cache;
2885 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2886 static void merge_tlist (struct tlist **, struct tlist *, int);
2887 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2888 static int warning_candidate_p (tree);
2889 static bool candidate_equal_p (const_tree, const_tree);
2890 static void warn_for_collisions (struct tlist *);
2891 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2892 static struct tlist *new_tlist (struct tlist *, tree, tree);
2894 /* Create a new struct tlist and fill in its fields. */
2895 static struct tlist *
2896 new_tlist (struct tlist *next, tree t, tree writer)
2898 struct tlist *l;
2899 l = XOBNEW (&tlist_obstack, struct tlist);
2900 l->next = next;
2901 l->expr = t;
2902 l->writer = writer;
2903 return l;
2906 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2907 is nonnull, we ignore any node we find which has a writer equal to it. */
2909 static void
2910 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2912 while (add)
2914 struct tlist *next = add->next;
2915 if (!copy)
2916 add->next = *to;
2917 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2918 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2919 add = next;
2923 /* Merge the nodes of ADD into TO. This merging process is done so that for
2924 each variable that already exists in TO, no new node is added; however if
2925 there is a write access recorded in ADD, and an occurrence on TO is only
2926 a read access, then the occurrence in TO will be modified to record the
2927 write. */
2929 static void
2930 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2932 struct tlist **end = to;
2934 while (*end)
2935 end = &(*end)->next;
2937 while (add)
2939 int found = 0;
2940 struct tlist *tmp2;
2941 struct tlist *next = add->next;
2943 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2944 if (candidate_equal_p (tmp2->expr, add->expr))
2946 found = 1;
2947 if (!tmp2->writer)
2948 tmp2->writer = add->writer;
2950 if (!found)
2952 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2953 end = &(*end)->next;
2954 *end = 0;
2956 add = next;
2960 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2961 references in list LIST conflict with it, excluding reads if ONLY writers
2962 is nonzero. */
2964 static void
2965 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2966 int only_writes)
2968 struct tlist *tmp;
2970 /* Avoid duplicate warnings. */
2971 for (tmp = warned_ids; tmp; tmp = tmp->next)
2972 if (candidate_equal_p (tmp->expr, written))
2973 return;
2975 while (list)
2977 if (candidate_equal_p (list->expr, written)
2978 && !candidate_equal_p (list->writer, writer)
2979 && (!only_writes || list->writer))
2981 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2982 warning_at (EXPR_LOC_OR_HERE (writer),
2983 OPT_Wsequence_point, "operation on %qE may be undefined",
2984 list->expr);
2986 list = list->next;
2990 /* Given a list LIST of references to variables, find whether any of these
2991 can cause conflicts due to missing sequence points. */
2993 static void
2994 warn_for_collisions (struct tlist *list)
2996 struct tlist *tmp;
2998 for (tmp = list; tmp; tmp = tmp->next)
3000 if (tmp->writer)
3001 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3005 /* Return nonzero if X is a tree that can be verified by the sequence point
3006 warnings. */
3007 static int
3008 warning_candidate_p (tree x)
3010 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3011 return 0;
3013 if (TREE_CODE (x) == BLOCK)
3014 return 0;
3016 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3017 (lvalue_p) crash on TRY/CATCH. */
3018 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3019 return 0;
3021 if (!lvalue_p (x))
3022 return 0;
3024 /* No point to track non-const calls, they will never satisfy
3025 operand_equal_p. */
3026 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3027 return 0;
3029 if (TREE_CODE (x) == STRING_CST)
3030 return 0;
3032 return 1;
3035 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3036 static bool
3037 candidate_equal_p (const_tree x, const_tree y)
3039 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3042 /* Walk the tree X, and record accesses to variables. If X is written by the
3043 parent tree, WRITER is the parent.
3044 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3045 expression or its only operand forces a sequence point, then everything up
3046 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3047 in PNO_SP.
3048 Once we return, we will have emitted warnings if any subexpression before
3049 such a sequence point could be undefined. On a higher level, however, the
3050 sequence point may not be relevant, and we'll merge the two lists.
3052 Example: (b++, a) + b;
3053 The call that processes the COMPOUND_EXPR will store the increment of B
3054 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3055 processes the PLUS_EXPR will need to merge the two lists so that
3056 eventually, all accesses end up on the same list (and we'll warn about the
3057 unordered subexpressions b++ and b.
3059 A note on merging. If we modify the former example so that our expression
3060 becomes
3061 (b++, b) + a
3062 care must be taken not simply to add all three expressions into the final
3063 PNO_SP list. The function merge_tlist takes care of that by merging the
3064 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3065 way, so that no more than one access to B is recorded. */
3067 static void
3068 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3069 tree writer)
3071 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3072 enum tree_code code;
3073 enum tree_code_class cl;
3075 /* X may be NULL if it is the operand of an empty statement expression
3076 ({ }). */
3077 if (x == NULL)
3078 return;
3080 restart:
3081 code = TREE_CODE (x);
3082 cl = TREE_CODE_CLASS (code);
3084 if (warning_candidate_p (x))
3085 *pno_sp = new_tlist (*pno_sp, x, writer);
3087 switch (code)
3089 case CONSTRUCTOR:
3090 case SIZEOF_EXPR:
3091 return;
3093 case COMPOUND_EXPR:
3094 case TRUTH_ANDIF_EXPR:
3095 case TRUTH_ORIF_EXPR:
3096 tmp_before = tmp_nosp = tmp_list3 = 0;
3097 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3098 warn_for_collisions (tmp_nosp);
3099 merge_tlist (pbefore_sp, tmp_before, 0);
3100 merge_tlist (pbefore_sp, tmp_nosp, 0);
3101 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3102 merge_tlist (pbefore_sp, tmp_list3, 0);
3103 return;
3105 case COND_EXPR:
3106 tmp_before = tmp_list2 = 0;
3107 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3108 warn_for_collisions (tmp_list2);
3109 merge_tlist (pbefore_sp, tmp_before, 0);
3110 merge_tlist (pbefore_sp, tmp_list2, 1);
3112 tmp_list3 = tmp_nosp = 0;
3113 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3114 warn_for_collisions (tmp_nosp);
3115 merge_tlist (pbefore_sp, tmp_list3, 0);
3117 tmp_list3 = tmp_list2 = 0;
3118 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3119 warn_for_collisions (tmp_list2);
3120 merge_tlist (pbefore_sp, tmp_list3, 0);
3121 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3122 two first, to avoid warning for (a ? b++ : b++). */
3123 merge_tlist (&tmp_nosp, tmp_list2, 0);
3124 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3125 return;
3127 case PREDECREMENT_EXPR:
3128 case PREINCREMENT_EXPR:
3129 case POSTDECREMENT_EXPR:
3130 case POSTINCREMENT_EXPR:
3131 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3132 return;
3134 case MODIFY_EXPR:
3135 tmp_before = tmp_nosp = tmp_list3 = 0;
3136 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3137 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3138 /* Expressions inside the LHS are not ordered wrt. the sequence points
3139 in the RHS. Example:
3140 *a = (a++, 2)
3141 Despite the fact that the modification of "a" is in the before_sp
3142 list (tmp_before), it conflicts with the use of "a" in the LHS.
3143 We can handle this by adding the contents of tmp_list3
3144 to those of tmp_before, and redoing the collision warnings for that
3145 list. */
3146 add_tlist (&tmp_before, tmp_list3, x, 1);
3147 warn_for_collisions (tmp_before);
3148 /* Exclude the LHS itself here; we first have to merge it into the
3149 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3150 didn't exclude the LHS, we'd get it twice, once as a read and once
3151 as a write. */
3152 add_tlist (pno_sp, tmp_list3, x, 0);
3153 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3155 merge_tlist (pbefore_sp, tmp_before, 0);
3156 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3157 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3158 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3159 return;
3161 case CALL_EXPR:
3162 /* We need to warn about conflicts among arguments and conflicts between
3163 args and the function address. Side effects of the function address,
3164 however, are not ordered by the sequence point of the call. */
3166 call_expr_arg_iterator iter;
3167 tree arg;
3168 tmp_before = tmp_nosp = 0;
3169 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3170 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3172 tmp_list2 = tmp_list3 = 0;
3173 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3174 merge_tlist (&tmp_list3, tmp_list2, 0);
3175 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3177 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3178 warn_for_collisions (tmp_before);
3179 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3180 return;
3183 case TREE_LIST:
3184 /* Scan all the list, e.g. indices of multi dimensional array. */
3185 while (x)
3187 tmp_before = tmp_nosp = 0;
3188 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3189 merge_tlist (&tmp_nosp, tmp_before, 0);
3190 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3191 x = TREE_CHAIN (x);
3193 return;
3195 case SAVE_EXPR:
3197 struct tlist_cache *t;
3198 for (t = save_expr_cache; t; t = t->next)
3199 if (candidate_equal_p (t->expr, x))
3200 break;
3202 if (!t)
3204 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3205 t->next = save_expr_cache;
3206 t->expr = x;
3207 save_expr_cache = t;
3209 tmp_before = tmp_nosp = 0;
3210 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3211 warn_for_collisions (tmp_nosp);
3213 tmp_list3 = 0;
3214 while (tmp_nosp)
3216 struct tlist *t = tmp_nosp;
3217 tmp_nosp = t->next;
3218 merge_tlist (&tmp_list3, t, 0);
3220 t->cache_before_sp = tmp_before;
3221 t->cache_after_sp = tmp_list3;
3223 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3224 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3225 return;
3228 case ADDR_EXPR:
3229 x = TREE_OPERAND (x, 0);
3230 if (DECL_P (x))
3231 return;
3232 writer = 0;
3233 goto restart;
3235 default:
3236 /* For other expressions, simply recurse on their operands.
3237 Manual tail recursion for unary expressions.
3238 Other non-expressions need not be processed. */
3239 if (cl == tcc_unary)
3241 x = TREE_OPERAND (x, 0);
3242 writer = 0;
3243 goto restart;
3245 else if (IS_EXPR_CODE_CLASS (cl))
3247 int lp;
3248 int max = TREE_OPERAND_LENGTH (x);
3249 for (lp = 0; lp < max; lp++)
3251 tmp_before = tmp_nosp = 0;
3252 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3253 merge_tlist (&tmp_nosp, tmp_before, 0);
3254 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3257 return;
3261 /* Try to warn for undefined behavior in EXPR due to missing sequence
3262 points. */
3264 DEBUG_FUNCTION void
3265 verify_sequence_points (tree expr)
3267 struct tlist *before_sp = 0, *after_sp = 0;
3269 warned_ids = 0;
3270 save_expr_cache = 0;
3271 if (tlist_firstobj == 0)
3273 gcc_obstack_init (&tlist_obstack);
3274 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3277 verify_tree (expr, &before_sp, &after_sp, 0);
3278 warn_for_collisions (after_sp);
3279 obstack_free (&tlist_obstack, tlist_firstobj);
3282 /* Validate the expression after `case' and apply default promotions. */
3284 static tree
3285 check_case_value (tree value)
3287 if (value == NULL_TREE)
3288 return value;
3290 if (TREE_CODE (value) == INTEGER_CST)
3291 /* Promote char or short to int. */
3292 value = perform_integral_promotions (value);
3293 else if (value != error_mark_node)
3295 error ("case label does not reduce to an integer constant");
3296 value = error_mark_node;
3299 constant_expression_warning (value);
3301 return value;
3304 /* See if the case values LOW and HIGH are in the range of the original
3305 type (i.e. before the default conversion to int) of the switch testing
3306 expression.
3307 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3308 the type before promoting it. CASE_LOW_P is a pointer to the lower
3309 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3310 if the case is not a case range.
3311 The caller has to make sure that we are not called with NULL for
3312 CASE_LOW_P (i.e. the default case).
3313 Returns true if the case label is in range of ORIG_TYPE (saturated or
3314 untouched) or false if the label is out of range. */
3316 static bool
3317 check_case_bounds (tree type, tree orig_type,
3318 tree *case_low_p, tree *case_high_p)
3320 tree min_value, max_value;
3321 tree case_low = *case_low_p;
3322 tree case_high = case_high_p ? *case_high_p : case_low;
3324 /* If there was a problem with the original type, do nothing. */
3325 if (orig_type == error_mark_node)
3326 return true;
3328 min_value = TYPE_MIN_VALUE (orig_type);
3329 max_value = TYPE_MAX_VALUE (orig_type);
3331 /* Case label is less than minimum for type. */
3332 if (tree_int_cst_compare (case_low, min_value) < 0
3333 && tree_int_cst_compare (case_high, min_value) < 0)
3335 warning (0, "case label value is less than minimum value for type");
3336 return false;
3339 /* Case value is greater than maximum for type. */
3340 if (tree_int_cst_compare (case_low, max_value) > 0
3341 && tree_int_cst_compare (case_high, max_value) > 0)
3343 warning (0, "case label value exceeds maximum value for type");
3344 return false;
3347 /* Saturate lower case label value to minimum. */
3348 if (tree_int_cst_compare (case_high, min_value) >= 0
3349 && tree_int_cst_compare (case_low, min_value) < 0)
3351 warning (0, "lower value in case label range"
3352 " less than minimum value for type");
3353 case_low = min_value;
3356 /* Saturate upper case label value to maximum. */
3357 if (tree_int_cst_compare (case_low, max_value) <= 0
3358 && tree_int_cst_compare (case_high, max_value) > 0)
3360 warning (0, "upper value in case label range"
3361 " exceeds maximum value for type");
3362 case_high = max_value;
3365 if (*case_low_p != case_low)
3366 *case_low_p = convert (type, case_low);
3367 if (case_high_p && *case_high_p != case_high)
3368 *case_high_p = convert (type, case_high);
3370 return true;
3373 /* Return an integer type with BITS bits of precision,
3374 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3376 tree
3377 c_common_type_for_size (unsigned int bits, int unsignedp)
3379 if (bits == TYPE_PRECISION (integer_type_node))
3380 return unsignedp ? unsigned_type_node : integer_type_node;
3382 if (bits == TYPE_PRECISION (signed_char_type_node))
3383 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3385 if (bits == TYPE_PRECISION (short_integer_type_node))
3386 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3388 if (bits == TYPE_PRECISION (long_integer_type_node))
3389 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3391 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3392 return (unsignedp ? long_long_unsigned_type_node
3393 : long_long_integer_type_node);
3395 if (int128_integer_type_node
3396 && bits == TYPE_PRECISION (int128_integer_type_node))
3397 return (unsignedp ? int128_unsigned_type_node
3398 : int128_integer_type_node);
3400 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3401 return (unsignedp ? widest_unsigned_literal_type_node
3402 : widest_integer_literal_type_node);
3404 if (bits <= TYPE_PRECISION (intQI_type_node))
3405 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3407 if (bits <= TYPE_PRECISION (intHI_type_node))
3408 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3410 if (bits <= TYPE_PRECISION (intSI_type_node))
3411 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3413 if (bits <= TYPE_PRECISION (intDI_type_node))
3414 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3416 return 0;
3419 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3420 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3421 and saturating if SATP is nonzero, otherwise not saturating. */
3423 tree
3424 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3425 int unsignedp, int satp)
3427 enum machine_mode mode;
3428 if (ibit == 0)
3429 mode = unsignedp ? UQQmode : QQmode;
3430 else
3431 mode = unsignedp ? UHAmode : HAmode;
3433 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3434 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3435 break;
3437 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3439 sorry ("GCC cannot support operators with integer types and "
3440 "fixed-point types that have too many integral and "
3441 "fractional bits together");
3442 return 0;
3445 return c_common_type_for_mode (mode, satp);
3448 /* Used for communication between c_common_type_for_mode and
3449 c_register_builtin_type. */
3450 static GTY(()) tree registered_builtin_types;
3452 /* Return a data type that has machine mode MODE.
3453 If the mode is an integer,
3454 then UNSIGNEDP selects between signed and unsigned types.
3455 If the mode is a fixed-point mode,
3456 then UNSIGNEDP selects between saturating and nonsaturating types. */
3458 tree
3459 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3461 tree t;
3463 if (mode == TYPE_MODE (integer_type_node))
3464 return unsignedp ? unsigned_type_node : integer_type_node;
3466 if (mode == TYPE_MODE (signed_char_type_node))
3467 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3469 if (mode == TYPE_MODE (short_integer_type_node))
3470 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3472 if (mode == TYPE_MODE (long_integer_type_node))
3473 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3475 if (mode == TYPE_MODE (long_long_integer_type_node))
3476 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3478 if (int128_integer_type_node
3479 && mode == TYPE_MODE (int128_integer_type_node))
3480 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3482 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3483 return unsignedp ? widest_unsigned_literal_type_node
3484 : widest_integer_literal_type_node;
3486 if (mode == QImode)
3487 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3489 if (mode == HImode)
3490 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3492 if (mode == SImode)
3493 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3495 if (mode == DImode)
3496 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3498 #if HOST_BITS_PER_WIDE_INT >= 64
3499 if (mode == TYPE_MODE (intTI_type_node))
3500 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3501 #endif
3503 if (mode == TYPE_MODE (float_type_node))
3504 return float_type_node;
3506 if (mode == TYPE_MODE (double_type_node))
3507 return double_type_node;
3509 if (mode == TYPE_MODE (long_double_type_node))
3510 return long_double_type_node;
3512 if (mode == TYPE_MODE (void_type_node))
3513 return void_type_node;
3515 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3516 return (unsignedp
3517 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3518 : make_signed_type (GET_MODE_PRECISION (mode)));
3520 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3521 return (unsignedp
3522 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3523 : make_signed_type (GET_MODE_PRECISION (mode)));
3525 if (COMPLEX_MODE_P (mode))
3527 enum machine_mode inner_mode;
3528 tree inner_type;
3530 if (mode == TYPE_MODE (complex_float_type_node))
3531 return complex_float_type_node;
3532 if (mode == TYPE_MODE (complex_double_type_node))
3533 return complex_double_type_node;
3534 if (mode == TYPE_MODE (complex_long_double_type_node))
3535 return complex_long_double_type_node;
3537 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3538 return complex_integer_type_node;
3540 inner_mode = GET_MODE_INNER (mode);
3541 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3542 if (inner_type != NULL_TREE)
3543 return build_complex_type (inner_type);
3545 else if (VECTOR_MODE_P (mode))
3547 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3548 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3549 if (inner_type != NULL_TREE)
3550 return build_vector_type_for_mode (inner_type, mode);
3553 if (mode == TYPE_MODE (dfloat32_type_node))
3554 return dfloat32_type_node;
3555 if (mode == TYPE_MODE (dfloat64_type_node))
3556 return dfloat64_type_node;
3557 if (mode == TYPE_MODE (dfloat128_type_node))
3558 return dfloat128_type_node;
3560 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3562 if (mode == TYPE_MODE (short_fract_type_node))
3563 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3564 if (mode == TYPE_MODE (fract_type_node))
3565 return unsignedp ? sat_fract_type_node : fract_type_node;
3566 if (mode == TYPE_MODE (long_fract_type_node))
3567 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3568 if (mode == TYPE_MODE (long_long_fract_type_node))
3569 return unsignedp ? sat_long_long_fract_type_node
3570 : long_long_fract_type_node;
3572 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3573 return unsignedp ? sat_unsigned_short_fract_type_node
3574 : unsigned_short_fract_type_node;
3575 if (mode == TYPE_MODE (unsigned_fract_type_node))
3576 return unsignedp ? sat_unsigned_fract_type_node
3577 : unsigned_fract_type_node;
3578 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3579 return unsignedp ? sat_unsigned_long_fract_type_node
3580 : unsigned_long_fract_type_node;
3581 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3582 return unsignedp ? sat_unsigned_long_long_fract_type_node
3583 : unsigned_long_long_fract_type_node;
3585 if (mode == TYPE_MODE (short_accum_type_node))
3586 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3587 if (mode == TYPE_MODE (accum_type_node))
3588 return unsignedp ? sat_accum_type_node : accum_type_node;
3589 if (mode == TYPE_MODE (long_accum_type_node))
3590 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3591 if (mode == TYPE_MODE (long_long_accum_type_node))
3592 return unsignedp ? sat_long_long_accum_type_node
3593 : long_long_accum_type_node;
3595 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3596 return unsignedp ? sat_unsigned_short_accum_type_node
3597 : unsigned_short_accum_type_node;
3598 if (mode == TYPE_MODE (unsigned_accum_type_node))
3599 return unsignedp ? sat_unsigned_accum_type_node
3600 : unsigned_accum_type_node;
3601 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3602 return unsignedp ? sat_unsigned_long_accum_type_node
3603 : unsigned_long_accum_type_node;
3604 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3605 return unsignedp ? sat_unsigned_long_long_accum_type_node
3606 : unsigned_long_long_accum_type_node;
3608 if (mode == QQmode)
3609 return unsignedp ? sat_qq_type_node : qq_type_node;
3610 if (mode == HQmode)
3611 return unsignedp ? sat_hq_type_node : hq_type_node;
3612 if (mode == SQmode)
3613 return unsignedp ? sat_sq_type_node : sq_type_node;
3614 if (mode == DQmode)
3615 return unsignedp ? sat_dq_type_node : dq_type_node;
3616 if (mode == TQmode)
3617 return unsignedp ? sat_tq_type_node : tq_type_node;
3619 if (mode == UQQmode)
3620 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3621 if (mode == UHQmode)
3622 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3623 if (mode == USQmode)
3624 return unsignedp ? sat_usq_type_node : usq_type_node;
3625 if (mode == UDQmode)
3626 return unsignedp ? sat_udq_type_node : udq_type_node;
3627 if (mode == UTQmode)
3628 return unsignedp ? sat_utq_type_node : utq_type_node;
3630 if (mode == HAmode)
3631 return unsignedp ? sat_ha_type_node : ha_type_node;
3632 if (mode == SAmode)
3633 return unsignedp ? sat_sa_type_node : sa_type_node;
3634 if (mode == DAmode)
3635 return unsignedp ? sat_da_type_node : da_type_node;
3636 if (mode == TAmode)
3637 return unsignedp ? sat_ta_type_node : ta_type_node;
3639 if (mode == UHAmode)
3640 return unsignedp ? sat_uha_type_node : uha_type_node;
3641 if (mode == USAmode)
3642 return unsignedp ? sat_usa_type_node : usa_type_node;
3643 if (mode == UDAmode)
3644 return unsignedp ? sat_uda_type_node : uda_type_node;
3645 if (mode == UTAmode)
3646 return unsignedp ? sat_uta_type_node : uta_type_node;
3649 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3650 if (TYPE_MODE (TREE_VALUE (t)) == mode
3651 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3652 return TREE_VALUE (t);
3654 return 0;
3657 tree
3658 c_common_unsigned_type (tree type)
3660 return c_common_signed_or_unsigned_type (1, type);
3663 /* Return a signed type the same as TYPE in other respects. */
3665 tree
3666 c_common_signed_type (tree type)
3668 return c_common_signed_or_unsigned_type (0, type);
3671 /* Return a type the same as TYPE except unsigned or
3672 signed according to UNSIGNEDP. */
3674 tree
3675 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3677 tree type1;
3679 /* This block of code emulates the behavior of the old
3680 c_common_unsigned_type. In particular, it returns
3681 long_unsigned_type_node if passed a long, even when a int would
3682 have the same size. This is necessary for warnings to work
3683 correctly in archs where sizeof(int) == sizeof(long) */
3685 type1 = TYPE_MAIN_VARIANT (type);
3686 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3687 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3688 if (type1 == integer_type_node || type1 == unsigned_type_node)
3689 return unsignedp ? unsigned_type_node : integer_type_node;
3690 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3691 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3692 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3693 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3694 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3695 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3696 if (int128_integer_type_node
3697 && (type1 == int128_integer_type_node
3698 || type1 == int128_unsigned_type_node))
3699 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3700 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3701 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3702 #if HOST_BITS_PER_WIDE_INT >= 64
3703 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3704 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3705 #endif
3706 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3707 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3708 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3709 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3710 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3711 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3712 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3713 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3715 #define C_COMMON_FIXED_TYPES(NAME) \
3716 if (type1 == short_ ## NAME ## _type_node \
3717 || type1 == unsigned_short_ ## NAME ## _type_node) \
3718 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3719 : short_ ## NAME ## _type_node; \
3720 if (type1 == NAME ## _type_node \
3721 || type1 == unsigned_ ## NAME ## _type_node) \
3722 return unsignedp ? unsigned_ ## NAME ## _type_node \
3723 : NAME ## _type_node; \
3724 if (type1 == long_ ## NAME ## _type_node \
3725 || type1 == unsigned_long_ ## NAME ## _type_node) \
3726 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3727 : long_ ## NAME ## _type_node; \
3728 if (type1 == long_long_ ## NAME ## _type_node \
3729 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3730 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3731 : long_long_ ## NAME ## _type_node;
3733 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3734 if (type1 == NAME ## _type_node \
3735 || type1 == u ## NAME ## _type_node) \
3736 return unsignedp ? u ## NAME ## _type_node \
3737 : NAME ## _type_node;
3739 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3740 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3741 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3742 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3743 : sat_ ## short_ ## NAME ## _type_node; \
3744 if (type1 == sat_ ## NAME ## _type_node \
3745 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3746 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3747 : sat_ ## NAME ## _type_node; \
3748 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3749 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3750 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3751 : sat_ ## long_ ## NAME ## _type_node; \
3752 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3753 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3754 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3755 : sat_ ## long_long_ ## NAME ## _type_node;
3757 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3758 if (type1 == sat_ ## NAME ## _type_node \
3759 || type1 == sat_ ## u ## NAME ## _type_node) \
3760 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3761 : sat_ ## NAME ## _type_node;
3763 C_COMMON_FIXED_TYPES (fract);
3764 C_COMMON_FIXED_TYPES_SAT (fract);
3765 C_COMMON_FIXED_TYPES (accum);
3766 C_COMMON_FIXED_TYPES_SAT (accum);
3768 C_COMMON_FIXED_MODE_TYPES (qq);
3769 C_COMMON_FIXED_MODE_TYPES (hq);
3770 C_COMMON_FIXED_MODE_TYPES (sq);
3771 C_COMMON_FIXED_MODE_TYPES (dq);
3772 C_COMMON_FIXED_MODE_TYPES (tq);
3773 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3774 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3775 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3776 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3777 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3778 C_COMMON_FIXED_MODE_TYPES (ha);
3779 C_COMMON_FIXED_MODE_TYPES (sa);
3780 C_COMMON_FIXED_MODE_TYPES (da);
3781 C_COMMON_FIXED_MODE_TYPES (ta);
3782 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3783 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3784 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3785 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3787 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3788 the precision; they have precision set to match their range, but
3789 may use a wider mode to match an ABI. If we change modes, we may
3790 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3791 the precision as well, so as to yield correct results for
3792 bit-field types. C++ does not have these separate bit-field
3793 types, and producing a signed or unsigned variant of an
3794 ENUMERAL_TYPE may cause other problems as well. */
3796 if (!INTEGRAL_TYPE_P (type)
3797 || TYPE_UNSIGNED (type) == unsignedp)
3798 return type;
3800 #define TYPE_OK(node) \
3801 (TYPE_MODE (type) == TYPE_MODE (node) \
3802 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3803 if (TYPE_OK (signed_char_type_node))
3804 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3805 if (TYPE_OK (integer_type_node))
3806 return unsignedp ? unsigned_type_node : integer_type_node;
3807 if (TYPE_OK (short_integer_type_node))
3808 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3809 if (TYPE_OK (long_integer_type_node))
3810 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3811 if (TYPE_OK (long_long_integer_type_node))
3812 return (unsignedp ? long_long_unsigned_type_node
3813 : long_long_integer_type_node);
3814 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3815 return (unsignedp ? int128_unsigned_type_node
3816 : int128_integer_type_node);
3817 if (TYPE_OK (widest_integer_literal_type_node))
3818 return (unsignedp ? widest_unsigned_literal_type_node
3819 : widest_integer_literal_type_node);
3821 #if HOST_BITS_PER_WIDE_INT >= 64
3822 if (TYPE_OK (intTI_type_node))
3823 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3824 #endif
3825 if (TYPE_OK (intDI_type_node))
3826 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3827 if (TYPE_OK (intSI_type_node))
3828 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3829 if (TYPE_OK (intHI_type_node))
3830 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3831 if (TYPE_OK (intQI_type_node))
3832 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3833 #undef TYPE_OK
3835 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3838 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3840 tree
3841 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3843 /* Extended integer types of the same width as a standard type have
3844 lesser rank, so those of the same width as int promote to int or
3845 unsigned int and are valid for printf formats expecting int or
3846 unsigned int. To avoid such special cases, avoid creating
3847 extended integer types for bit-fields if a standard integer type
3848 is available. */
3849 if (width == TYPE_PRECISION (integer_type_node))
3850 return unsignedp ? unsigned_type_node : integer_type_node;
3851 if (width == TYPE_PRECISION (signed_char_type_node))
3852 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3853 if (width == TYPE_PRECISION (short_integer_type_node))
3854 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3855 if (width == TYPE_PRECISION (long_integer_type_node))
3856 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3857 if (width == TYPE_PRECISION (long_long_integer_type_node))
3858 return (unsignedp ? long_long_unsigned_type_node
3859 : long_long_integer_type_node);
3860 if (int128_integer_type_node
3861 && width == TYPE_PRECISION (int128_integer_type_node))
3862 return (unsignedp ? int128_unsigned_type_node
3863 : int128_integer_type_node);
3864 return build_nonstandard_integer_type (width, unsignedp);
3867 /* The C version of the register_builtin_type langhook. */
3869 void
3870 c_register_builtin_type (tree type, const char* name)
3872 tree decl;
3874 decl = build_decl (UNKNOWN_LOCATION,
3875 TYPE_DECL, get_identifier (name), type);
3876 DECL_ARTIFICIAL (decl) = 1;
3877 if (!TYPE_NAME (type))
3878 TYPE_NAME (type) = decl;
3879 pushdecl (decl);
3881 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3884 /* Print an error message for invalid operands to arith operation
3885 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3886 LOCATION is the location of the message. */
3888 void
3889 binary_op_error (location_t location, enum tree_code code,
3890 tree type0, tree type1)
3892 const char *opname;
3894 switch (code)
3896 case PLUS_EXPR:
3897 opname = "+"; break;
3898 case MINUS_EXPR:
3899 opname = "-"; break;
3900 case MULT_EXPR:
3901 opname = "*"; break;
3902 case MAX_EXPR:
3903 opname = "max"; break;
3904 case MIN_EXPR:
3905 opname = "min"; break;
3906 case EQ_EXPR:
3907 opname = "=="; break;
3908 case NE_EXPR:
3909 opname = "!="; break;
3910 case LE_EXPR:
3911 opname = "<="; break;
3912 case GE_EXPR:
3913 opname = ">="; break;
3914 case LT_EXPR:
3915 opname = "<"; break;
3916 case GT_EXPR:
3917 opname = ">"; break;
3918 case LSHIFT_EXPR:
3919 opname = "<<"; break;
3920 case RSHIFT_EXPR:
3921 opname = ">>"; break;
3922 case TRUNC_MOD_EXPR:
3923 case FLOOR_MOD_EXPR:
3924 opname = "%"; break;
3925 case TRUNC_DIV_EXPR:
3926 case FLOOR_DIV_EXPR:
3927 opname = "/"; break;
3928 case BIT_AND_EXPR:
3929 opname = "&"; break;
3930 case BIT_IOR_EXPR:
3931 opname = "|"; break;
3932 case TRUTH_ANDIF_EXPR:
3933 opname = "&&"; break;
3934 case TRUTH_ORIF_EXPR:
3935 opname = "||"; break;
3936 case BIT_XOR_EXPR:
3937 opname = "^"; break;
3938 default:
3939 gcc_unreachable ();
3941 error_at (location,
3942 "invalid operands to binary %s (have %qT and %qT)", opname,
3943 type0, type1);
3946 /* Given an expression as a tree, return its original type. Do this
3947 by stripping any conversion that preserves the sign and precision. */
3948 static tree
3949 expr_original_type (tree expr)
3951 STRIP_SIGN_NOPS (expr);
3952 return TREE_TYPE (expr);
3955 /* Subroutine of build_binary_op, used for comparison operations.
3956 See if the operands have both been converted from subword integer types
3957 and, if so, perhaps change them both back to their original type.
3958 This function is also responsible for converting the two operands
3959 to the proper common type for comparison.
3961 The arguments of this function are all pointers to local variables
3962 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3963 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3965 If this function returns nonzero, it means that the comparison has
3966 a constant value. What this function returns is an expression for
3967 that value. */
3969 tree
3970 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3971 enum tree_code *rescode_ptr)
3973 tree type;
3974 tree op0 = *op0_ptr;
3975 tree op1 = *op1_ptr;
3976 int unsignedp0, unsignedp1;
3977 int real1, real2;
3978 tree primop0, primop1;
3979 enum tree_code code = *rescode_ptr;
3980 location_t loc = EXPR_LOC_OR_HERE (op0);
3982 /* Throw away any conversions to wider types
3983 already present in the operands. */
3985 primop0 = c_common_get_narrower (op0, &unsignedp0);
3986 primop1 = c_common_get_narrower (op1, &unsignedp1);
3988 /* If primopN is first sign-extended from primopN's precision to opN's
3989 precision, then zero-extended from opN's precision to
3990 *restype_ptr precision, shortenings might be invalid. */
3991 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3992 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3993 && !unsignedp0
3994 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3995 primop0 = op0;
3996 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3997 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3998 && !unsignedp1
3999 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4000 primop1 = op1;
4002 /* Handle the case that OP0 does not *contain* a conversion
4003 but it *requires* conversion to FINAL_TYPE. */
4005 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4006 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4007 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4008 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4010 /* If one of the operands must be floated, we cannot optimize. */
4011 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4012 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4014 /* If first arg is constant, swap the args (changing operation
4015 so value is preserved), for canonicalization. Don't do this if
4016 the second arg is 0. */
4018 if (TREE_CONSTANT (primop0)
4019 && !integer_zerop (primop1) && !real_zerop (primop1)
4020 && !fixed_zerop (primop1))
4022 tree tem = primop0;
4023 int temi = unsignedp0;
4024 primop0 = primop1;
4025 primop1 = tem;
4026 tem = op0;
4027 op0 = op1;
4028 op1 = tem;
4029 *op0_ptr = op0;
4030 *op1_ptr = op1;
4031 unsignedp0 = unsignedp1;
4032 unsignedp1 = temi;
4033 temi = real1;
4034 real1 = real2;
4035 real2 = temi;
4037 switch (code)
4039 case LT_EXPR:
4040 code = GT_EXPR;
4041 break;
4042 case GT_EXPR:
4043 code = LT_EXPR;
4044 break;
4045 case LE_EXPR:
4046 code = GE_EXPR;
4047 break;
4048 case GE_EXPR:
4049 code = LE_EXPR;
4050 break;
4051 default:
4052 break;
4054 *rescode_ptr = code;
4057 /* If comparing an integer against a constant more bits wide,
4058 maybe we can deduce a value of 1 or 0 independent of the data.
4059 Or else truncate the constant now
4060 rather than extend the variable at run time.
4062 This is only interesting if the constant is the wider arg.
4063 Also, it is not safe if the constant is unsigned and the
4064 variable arg is signed, since in this case the variable
4065 would be sign-extended and then regarded as unsigned.
4066 Our technique fails in this case because the lowest/highest
4067 possible unsigned results don't follow naturally from the
4068 lowest/highest possible values of the variable operand.
4069 For just EQ_EXPR and NE_EXPR there is another technique that
4070 could be used: see if the constant can be faithfully represented
4071 in the other operand's type, by truncating it and reextending it
4072 and see if that preserves the constant's value. */
4074 if (!real1 && !real2
4075 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4076 && TREE_CODE (primop1) == INTEGER_CST
4077 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4079 int min_gt, max_gt, min_lt, max_lt;
4080 tree maxval, minval;
4081 /* 1 if comparison is nominally unsigned. */
4082 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4083 tree val;
4085 type = c_common_signed_or_unsigned_type (unsignedp0,
4086 TREE_TYPE (primop0));
4088 maxval = TYPE_MAX_VALUE (type);
4089 minval = TYPE_MIN_VALUE (type);
4091 if (unsignedp && !unsignedp0)
4092 *restype_ptr = c_common_signed_type (*restype_ptr);
4094 if (TREE_TYPE (primop1) != *restype_ptr)
4096 /* Convert primop1 to target type, but do not introduce
4097 additional overflow. We know primop1 is an int_cst. */
4098 primop1 = force_fit_type_double (*restype_ptr,
4099 tree_to_double_int (primop1),
4100 0, TREE_OVERFLOW (primop1));
4102 if (type != *restype_ptr)
4104 minval = convert (*restype_ptr, minval);
4105 maxval = convert (*restype_ptr, maxval);
4108 if (unsignedp && unsignedp0)
4110 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4111 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4112 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4113 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4115 else
4117 min_gt = INT_CST_LT (primop1, minval);
4118 max_gt = INT_CST_LT (primop1, maxval);
4119 min_lt = INT_CST_LT (minval, primop1);
4120 max_lt = INT_CST_LT (maxval, primop1);
4123 val = 0;
4124 /* This used to be a switch, but Genix compiler can't handle that. */
4125 if (code == NE_EXPR)
4127 if (max_lt || min_gt)
4128 val = truthvalue_true_node;
4130 else if (code == EQ_EXPR)
4132 if (max_lt || min_gt)
4133 val = truthvalue_false_node;
4135 else if (code == LT_EXPR)
4137 if (max_lt)
4138 val = truthvalue_true_node;
4139 if (!min_lt)
4140 val = truthvalue_false_node;
4142 else if (code == GT_EXPR)
4144 if (min_gt)
4145 val = truthvalue_true_node;
4146 if (!max_gt)
4147 val = truthvalue_false_node;
4149 else if (code == LE_EXPR)
4151 if (!max_gt)
4152 val = truthvalue_true_node;
4153 if (min_gt)
4154 val = truthvalue_false_node;
4156 else if (code == GE_EXPR)
4158 if (!min_lt)
4159 val = truthvalue_true_node;
4160 if (max_lt)
4161 val = truthvalue_false_node;
4164 /* If primop0 was sign-extended and unsigned comparison specd,
4165 we did a signed comparison above using the signed type bounds.
4166 But the comparison we output must be unsigned.
4168 Also, for inequalities, VAL is no good; but if the signed
4169 comparison had *any* fixed result, it follows that the
4170 unsigned comparison just tests the sign in reverse
4171 (positive values are LE, negative ones GE).
4172 So we can generate an unsigned comparison
4173 against an extreme value of the signed type. */
4175 if (unsignedp && !unsignedp0)
4177 if (val != 0)
4178 switch (code)
4180 case LT_EXPR:
4181 case GE_EXPR:
4182 primop1 = TYPE_MIN_VALUE (type);
4183 val = 0;
4184 break;
4186 case LE_EXPR:
4187 case GT_EXPR:
4188 primop1 = TYPE_MAX_VALUE (type);
4189 val = 0;
4190 break;
4192 default:
4193 break;
4195 type = c_common_unsigned_type (type);
4198 if (TREE_CODE (primop0) != INTEGER_CST
4199 && c_inhibit_evaluation_warnings == 0)
4201 if (val == truthvalue_false_node)
4202 warning_at (loc, OPT_Wtype_limits,
4203 "comparison is always false due to limited range of data type");
4204 if (val == truthvalue_true_node)
4205 warning_at (loc, OPT_Wtype_limits,
4206 "comparison is always true due to limited range of data type");
4209 if (val != 0)
4211 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4212 if (TREE_SIDE_EFFECTS (primop0))
4213 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4214 return val;
4217 /* Value is not predetermined, but do the comparison
4218 in the type of the operand that is not constant.
4219 TYPE is already properly set. */
4222 /* If either arg is decimal float and the other is float, find the
4223 proper common type to use for comparison. */
4224 else if (real1 && real2
4225 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4226 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4227 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4229 else if (real1 && real2
4230 && (TYPE_PRECISION (TREE_TYPE (primop0))
4231 == TYPE_PRECISION (TREE_TYPE (primop1))))
4232 type = TREE_TYPE (primop0);
4234 /* If args' natural types are both narrower than nominal type
4235 and both extend in the same manner, compare them
4236 in the type of the wider arg.
4237 Otherwise must actually extend both to the nominal
4238 common type lest different ways of extending
4239 alter the result.
4240 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4242 else if (unsignedp0 == unsignedp1 && real1 == real2
4243 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4244 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4246 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4247 type = c_common_signed_or_unsigned_type (unsignedp0
4248 || TYPE_UNSIGNED (*restype_ptr),
4249 type);
4250 /* Make sure shorter operand is extended the right way
4251 to match the longer operand. */
4252 primop0
4253 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4254 TREE_TYPE (primop0)),
4255 primop0);
4256 primop1
4257 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4258 TREE_TYPE (primop1)),
4259 primop1);
4261 else
4263 /* Here we must do the comparison on the nominal type
4264 using the args exactly as we received them. */
4265 type = *restype_ptr;
4266 primop0 = op0;
4267 primop1 = op1;
4269 if (!real1 && !real2 && integer_zerop (primop1)
4270 && TYPE_UNSIGNED (*restype_ptr))
4272 tree value = 0;
4273 /* All unsigned values are >= 0, so we warn. However,
4274 if OP0 is a constant that is >= 0, the signedness of
4275 the comparison isn't an issue, so suppress the
4276 warning. */
4277 bool warn =
4278 warn_type_limits && !in_system_header
4279 && c_inhibit_evaluation_warnings == 0
4280 && !(TREE_CODE (primop0) == INTEGER_CST
4281 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4282 primop0)))
4283 /* Do not warn for enumeration types. */
4284 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4286 switch (code)
4288 case GE_EXPR:
4289 if (warn)
4290 warning_at (loc, OPT_Wtype_limits,
4291 "comparison of unsigned expression >= 0 is always true");
4292 value = truthvalue_true_node;
4293 break;
4295 case LT_EXPR:
4296 if (warn)
4297 warning_at (loc, OPT_Wtype_limits,
4298 "comparison of unsigned expression < 0 is always false");
4299 value = truthvalue_false_node;
4300 break;
4302 default:
4303 break;
4306 if (value != 0)
4308 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4309 if (TREE_SIDE_EFFECTS (primop0))
4310 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4311 primop0, value);
4312 return value;
4317 *op0_ptr = convert (type, primop0);
4318 *op1_ptr = convert (type, primop1);
4320 *restype_ptr = truthvalue_type_node;
4322 return 0;
4325 /* Return a tree for the sum or difference (RESULTCODE says which)
4326 of pointer PTROP and integer INTOP. */
4328 tree
4329 pointer_int_sum (location_t loc, enum tree_code resultcode,
4330 tree ptrop, tree intop, bool complain)
4332 tree size_exp, ret;
4334 /* The result is a pointer of the same type that is being added. */
4335 tree result_type = TREE_TYPE (ptrop);
4337 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4339 if (complain && warn_pointer_arith)
4340 pedwarn (loc, OPT_Wpointer_arith,
4341 "pointer of type %<void *%> used in arithmetic");
4342 else if (!complain)
4343 return error_mark_node;
4344 size_exp = integer_one_node;
4346 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4348 if (complain && warn_pointer_arith)
4349 pedwarn (loc, OPT_Wpointer_arith,
4350 "pointer to a function used in arithmetic");
4351 else if (!complain)
4352 return error_mark_node;
4353 size_exp = integer_one_node;
4355 else
4356 size_exp = size_in_bytes (TREE_TYPE (result_type));
4358 /* We are manipulating pointer values, so we don't need to warn
4359 about relying on undefined signed overflow. We disable the
4360 warning here because we use integer types so fold won't know that
4361 they are really pointers. */
4362 fold_defer_overflow_warnings ();
4364 /* If what we are about to multiply by the size of the elements
4365 contains a constant term, apply distributive law
4366 and multiply that constant term separately.
4367 This helps produce common subexpressions. */
4368 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4369 && !TREE_CONSTANT (intop)
4370 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4371 && TREE_CONSTANT (size_exp)
4372 /* If the constant comes from pointer subtraction,
4373 skip this optimization--it would cause an error. */
4374 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4375 /* If the constant is unsigned, and smaller than the pointer size,
4376 then we must skip this optimization. This is because it could cause
4377 an overflow error if the constant is negative but INTOP is not. */
4378 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4379 || (TYPE_PRECISION (TREE_TYPE (intop))
4380 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4382 enum tree_code subcode = resultcode;
4383 tree int_type = TREE_TYPE (intop);
4384 if (TREE_CODE (intop) == MINUS_EXPR)
4385 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4386 /* Convert both subexpression types to the type of intop,
4387 because weird cases involving pointer arithmetic
4388 can result in a sum or difference with different type args. */
4389 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4390 subcode, ptrop,
4391 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4392 intop = convert (int_type, TREE_OPERAND (intop, 0));
4395 /* Convert the integer argument to a type the same size as sizetype
4396 so the multiply won't overflow spuriously. */
4397 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4398 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4399 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4400 TYPE_UNSIGNED (sizetype)), intop);
4402 /* Replace the integer argument with a suitable product by the object size.
4403 Do this multiplication as signed, then convert to the appropriate type
4404 for the pointer operation and disregard an overflow that occurred only
4405 because of the sign-extension change in the latter conversion. */
4407 tree t = build_binary_op (loc,
4408 MULT_EXPR, intop,
4409 convert (TREE_TYPE (intop), size_exp), 1);
4410 intop = convert (sizetype, t);
4411 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4412 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4413 TREE_INT_CST_HIGH (intop));
4416 /* Create the sum or difference. */
4417 if (resultcode == MINUS_EXPR)
4418 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4420 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4422 fold_undefer_and_ignore_overflow_warnings ();
4424 return ret;
4427 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4428 and if NON_CONST is known not to be permitted in an evaluated part
4429 of a constant expression. */
4431 tree
4432 c_wrap_maybe_const (tree expr, bool non_const)
4434 bool nowarning = TREE_NO_WARNING (expr);
4435 location_t loc = EXPR_LOCATION (expr);
4437 /* This should never be called for C++. */
4438 if (c_dialect_cxx ())
4439 gcc_unreachable ();
4441 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4442 STRIP_TYPE_NOPS (expr);
4443 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4444 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4445 if (nowarning)
4446 TREE_NO_WARNING (expr) = 1;
4447 protected_set_expr_location (expr, loc);
4449 return expr;
4452 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4453 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4454 around the SAVE_EXPR if needed so that c_fully_fold does not need
4455 to look inside SAVE_EXPRs. */
4457 tree
4458 c_save_expr (tree expr)
4460 bool maybe_const = true;
4461 if (c_dialect_cxx ())
4462 return save_expr (expr);
4463 expr = c_fully_fold (expr, false, &maybe_const);
4464 expr = save_expr (expr);
4465 if (!maybe_const)
4466 expr = c_wrap_maybe_const (expr, true);
4467 return expr;
4470 /* Return whether EXPR is a declaration whose address can never be
4471 NULL. */
4473 bool
4474 decl_with_nonnull_addr_p (const_tree expr)
4476 return (DECL_P (expr)
4477 && (TREE_CODE (expr) == PARM_DECL
4478 || TREE_CODE (expr) == LABEL_DECL
4479 || !DECL_WEAK (expr)));
4482 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4483 or for an `if' or `while' statement or ?..: exp. It should already
4484 have been validated to be of suitable type; otherwise, a bad
4485 diagnostic may result.
4487 The EXPR is located at LOCATION.
4489 This preparation consists of taking the ordinary
4490 representation of an expression expr and producing a valid tree
4491 boolean expression describing whether expr is nonzero. We could
4492 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4493 but we optimize comparisons, &&, ||, and !.
4495 The resulting type should always be `truthvalue_type_node'. */
4497 tree
4498 c_common_truthvalue_conversion (location_t location, tree expr)
4500 switch (TREE_CODE (expr))
4502 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4503 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4504 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4505 case ORDERED_EXPR: case UNORDERED_EXPR:
4506 if (TREE_TYPE (expr) == truthvalue_type_node)
4507 return expr;
4508 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4509 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4510 goto ret;
4512 case TRUTH_ANDIF_EXPR:
4513 case TRUTH_ORIF_EXPR:
4514 case TRUTH_AND_EXPR:
4515 case TRUTH_OR_EXPR:
4516 case TRUTH_XOR_EXPR:
4517 if (TREE_TYPE (expr) == truthvalue_type_node)
4518 return expr;
4519 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4520 c_common_truthvalue_conversion (location,
4521 TREE_OPERAND (expr, 0)),
4522 c_common_truthvalue_conversion (location,
4523 TREE_OPERAND (expr, 1)));
4524 goto ret;
4526 case TRUTH_NOT_EXPR:
4527 if (TREE_TYPE (expr) == truthvalue_type_node)
4528 return expr;
4529 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4530 c_common_truthvalue_conversion (location,
4531 TREE_OPERAND (expr, 0)));
4532 goto ret;
4534 case ERROR_MARK:
4535 return expr;
4537 case INTEGER_CST:
4538 return integer_zerop (expr) ? truthvalue_false_node
4539 : truthvalue_true_node;
4541 case REAL_CST:
4542 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4543 ? truthvalue_true_node
4544 : truthvalue_false_node;
4546 case FIXED_CST:
4547 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4548 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4549 ? truthvalue_true_node
4550 : truthvalue_false_node;
4552 case FUNCTION_DECL:
4553 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4554 /* Fall through. */
4556 case ADDR_EXPR:
4558 tree inner = TREE_OPERAND (expr, 0);
4559 if (decl_with_nonnull_addr_p (inner))
4561 /* Common Ada/Pascal programmer's mistake. */
4562 warning_at (location,
4563 OPT_Waddress,
4564 "the address of %qD will always evaluate as %<true%>",
4565 inner);
4566 return truthvalue_true_node;
4568 break;
4571 case COMPLEX_EXPR:
4572 expr = build_binary_op (EXPR_LOCATION (expr),
4573 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4574 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4575 c_common_truthvalue_conversion (location,
4576 TREE_OPERAND (expr, 0)),
4577 c_common_truthvalue_conversion (location,
4578 TREE_OPERAND (expr, 1)),
4580 goto ret;
4582 case NEGATE_EXPR:
4583 case ABS_EXPR:
4584 case FLOAT_EXPR:
4585 case EXCESS_PRECISION_EXPR:
4586 /* These don't change whether an object is nonzero or zero. */
4587 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4589 case LROTATE_EXPR:
4590 case RROTATE_EXPR:
4591 /* These don't change whether an object is zero or nonzero, but
4592 we can't ignore them if their second arg has side-effects. */
4593 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4595 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4596 TREE_OPERAND (expr, 1),
4597 c_common_truthvalue_conversion
4598 (location, TREE_OPERAND (expr, 0)));
4599 goto ret;
4601 else
4602 return c_common_truthvalue_conversion (location,
4603 TREE_OPERAND (expr, 0));
4605 case COND_EXPR:
4606 /* Distribute the conversion into the arms of a COND_EXPR. */
4607 if (c_dialect_cxx ())
4609 tree op1 = TREE_OPERAND (expr, 1);
4610 tree op2 = TREE_OPERAND (expr, 2);
4611 /* In C++ one of the arms might have void type if it is throw. */
4612 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4613 op1 = c_common_truthvalue_conversion (location, op1);
4614 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4615 op2 = c_common_truthvalue_conversion (location, op2);
4616 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4617 TREE_OPERAND (expr, 0), op1, op2);
4618 goto ret;
4620 else
4622 /* Folding will happen later for C. */
4623 expr = build3 (COND_EXPR, truthvalue_type_node,
4624 TREE_OPERAND (expr, 0),
4625 c_common_truthvalue_conversion (location,
4626 TREE_OPERAND (expr, 1)),
4627 c_common_truthvalue_conversion (location,
4628 TREE_OPERAND (expr, 2)));
4629 goto ret;
4632 CASE_CONVERT:
4634 tree totype = TREE_TYPE (expr);
4635 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4637 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4638 since that affects how `default_conversion' will behave. */
4639 if (TREE_CODE (totype) == REFERENCE_TYPE
4640 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4641 break;
4642 /* Don't strip a conversion from C++0x scoped enum, since they
4643 don't implicitly convert to other types. */
4644 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4645 && ENUM_IS_SCOPED (fromtype))
4646 break;
4647 /* If this isn't narrowing the argument, we can ignore it. */
4648 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4649 return c_common_truthvalue_conversion (location,
4650 TREE_OPERAND (expr, 0));
4652 break;
4654 case MODIFY_EXPR:
4655 if (!TREE_NO_WARNING (expr)
4656 && warn_parentheses)
4658 warning (OPT_Wparentheses,
4659 "suggest parentheses around assignment used as truth value");
4660 TREE_NO_WARNING (expr) = 1;
4662 break;
4664 default:
4665 break;
4668 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4670 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4671 expr = (build_binary_op
4672 (EXPR_LOCATION (expr),
4673 (TREE_SIDE_EFFECTS (expr)
4674 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4675 c_common_truthvalue_conversion
4676 (location,
4677 build_unary_op (location, REALPART_EXPR, t, 0)),
4678 c_common_truthvalue_conversion
4679 (location,
4680 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4681 0));
4682 goto ret;
4685 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4687 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4688 FCONST0 (TYPE_MODE
4689 (TREE_TYPE (expr))));
4690 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4692 else
4693 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4695 ret:
4696 protected_set_expr_location (expr, location);
4697 return expr;
4700 static void def_builtin_1 (enum built_in_function fncode,
4701 const char *name,
4702 enum built_in_class fnclass,
4703 tree fntype, tree libtype,
4704 bool both_p, bool fallback_p, bool nonansi_p,
4705 tree fnattrs, bool implicit_p);
4708 /* Apply the TYPE_QUALS to the new DECL. */
4710 void
4711 c_apply_type_quals_to_decl (int type_quals, tree decl)
4713 tree type = TREE_TYPE (decl);
4715 if (type == error_mark_node)
4716 return;
4718 if ((type_quals & TYPE_QUAL_CONST)
4719 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4720 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4721 constructor can produce constant init, so rely on cp_finish_decl to
4722 clear TREE_READONLY if the variable has non-constant init. */
4723 TREE_READONLY (decl) = 1;
4724 if (type_quals & TYPE_QUAL_VOLATILE)
4726 TREE_SIDE_EFFECTS (decl) = 1;
4727 TREE_THIS_VOLATILE (decl) = 1;
4729 if (type_quals & TYPE_QUAL_RESTRICT)
4731 while (type && TREE_CODE (type) == ARRAY_TYPE)
4732 /* Allow 'restrict' on arrays of pointers.
4733 FIXME currently we just ignore it. */
4734 type = TREE_TYPE (type);
4735 if (!type
4736 || !POINTER_TYPE_P (type)
4737 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4738 error ("invalid use of %<restrict%>");
4742 /* Hash function for the problem of multiple type definitions in
4743 different files. This must hash all types that will compare
4744 equal via comptypes to the same value. In practice it hashes
4745 on some of the simple stuff and leaves the details to comptypes. */
4747 static hashval_t
4748 c_type_hash (const void *p)
4750 int n_elements;
4751 int shift, size;
4752 const_tree const t = (const_tree) p;
4753 tree t2;
4754 switch (TREE_CODE (t))
4756 /* For pointers, hash on pointee type plus some swizzling. */
4757 case POINTER_TYPE:
4758 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4759 /* Hash on number of elements and total size. */
4760 case ENUMERAL_TYPE:
4761 shift = 3;
4762 t2 = TYPE_VALUES (t);
4763 break;
4764 case RECORD_TYPE:
4765 shift = 0;
4766 t2 = TYPE_FIELDS (t);
4767 break;
4768 case QUAL_UNION_TYPE:
4769 shift = 1;
4770 t2 = TYPE_FIELDS (t);
4771 break;
4772 case UNION_TYPE:
4773 shift = 2;
4774 t2 = TYPE_FIELDS (t);
4775 break;
4776 default:
4777 gcc_unreachable ();
4779 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4780 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4781 n_elements = list_length (t2);
4782 /* We might have a VLA here. */
4783 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4784 size = 0;
4785 else
4786 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4787 return ((size << 24) | (n_elements << shift));
4790 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4792 /* Return the typed-based alias set for T, which may be an expression
4793 or a type. Return -1 if we don't do anything special. */
4795 alias_set_type
4796 c_common_get_alias_set (tree t)
4798 tree u;
4799 PTR *slot;
4801 /* For VLAs, use the alias set of the element type rather than the
4802 default of alias set 0 for types compared structurally. */
4803 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4805 if (TREE_CODE (t) == ARRAY_TYPE)
4806 return get_alias_set (TREE_TYPE (t));
4807 return -1;
4810 /* Permit type-punning when accessing a union, provided the access
4811 is directly through the union. For example, this code does not
4812 permit taking the address of a union member and then storing
4813 through it. Even the type-punning allowed here is a GCC
4814 extension, albeit a common and useful one; the C standard says
4815 that such accesses have implementation-defined behavior. */
4816 for (u = t;
4817 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4818 u = TREE_OPERAND (u, 0))
4819 if (TREE_CODE (u) == COMPONENT_REF
4820 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4821 return 0;
4823 /* That's all the expressions we handle specially. */
4824 if (!TYPE_P (t))
4825 return -1;
4827 /* The C standard guarantees that any object may be accessed via an
4828 lvalue that has character type. */
4829 if (t == char_type_node
4830 || t == signed_char_type_node
4831 || t == unsigned_char_type_node)
4832 return 0;
4834 /* The C standard specifically allows aliasing between signed and
4835 unsigned variants of the same type. We treat the signed
4836 variant as canonical. */
4837 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4839 tree t1 = c_common_signed_type (t);
4841 /* t1 == t can happen for boolean nodes which are always unsigned. */
4842 if (t1 != t)
4843 return get_alias_set (t1);
4846 /* Handle the case of multiple type nodes referring to "the same" type,
4847 which occurs with IMA. These share an alias set. FIXME: Currently only
4848 C90 is handled. (In C99 type compatibility is not transitive, which
4849 complicates things mightily. The alias set splay trees can theoretically
4850 represent this, but insertion is tricky when you consider all the
4851 different orders things might arrive in.) */
4853 if (c_language != clk_c || flag_isoc99)
4854 return -1;
4856 /* Save time if there's only one input file. */
4857 if (num_in_fnames == 1)
4858 return -1;
4860 /* Pointers need special handling if they point to any type that
4861 needs special handling (below). */
4862 if (TREE_CODE (t) == POINTER_TYPE)
4864 tree t2;
4865 /* Find bottom type under any nested POINTERs. */
4866 for (t2 = TREE_TYPE (t);
4867 TREE_CODE (t2) == POINTER_TYPE;
4868 t2 = TREE_TYPE (t2))
4870 if (TREE_CODE (t2) != RECORD_TYPE
4871 && TREE_CODE (t2) != ENUMERAL_TYPE
4872 && TREE_CODE (t2) != QUAL_UNION_TYPE
4873 && TREE_CODE (t2) != UNION_TYPE)
4874 return -1;
4875 if (TYPE_SIZE (t2) == 0)
4876 return -1;
4878 /* These are the only cases that need special handling. */
4879 if (TREE_CODE (t) != RECORD_TYPE
4880 && TREE_CODE (t) != ENUMERAL_TYPE
4881 && TREE_CODE (t) != QUAL_UNION_TYPE
4882 && TREE_CODE (t) != UNION_TYPE
4883 && TREE_CODE (t) != POINTER_TYPE)
4884 return -1;
4885 /* Undefined? */
4886 if (TYPE_SIZE (t) == 0)
4887 return -1;
4889 /* Look up t in hash table. Only one of the compatible types within each
4890 alias set is recorded in the table. */
4891 if (!type_hash_table)
4892 type_hash_table = htab_create_ggc (1021, c_type_hash,
4893 (htab_eq) lang_hooks.types_compatible_p,
4894 NULL);
4895 slot = htab_find_slot (type_hash_table, t, INSERT);
4896 if (*slot != NULL)
4898 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4899 return TYPE_ALIAS_SET ((tree)*slot);
4901 else
4902 /* Our caller will assign and record (in t) a new alias set; all we need
4903 to do is remember t in the hash table. */
4904 *slot = t;
4906 return -1;
4909 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4910 the second parameter indicates which OPERATOR is being applied.
4911 The COMPLAIN flag controls whether we should diagnose possibly
4912 ill-formed constructs or not. LOC is the location of the SIZEOF or
4913 TYPEOF operator. */
4915 tree
4916 c_sizeof_or_alignof_type (location_t loc,
4917 tree type, bool is_sizeof, int complain)
4919 const char *op_name;
4920 tree value = NULL;
4921 enum tree_code type_code = TREE_CODE (type);
4923 op_name = is_sizeof ? "sizeof" : "__alignof__";
4925 if (type_code == FUNCTION_TYPE)
4927 if (is_sizeof)
4929 if (complain && warn_pointer_arith)
4930 pedwarn (loc, OPT_Wpointer_arith,
4931 "invalid application of %<sizeof%> to a function type");
4932 else if (!complain)
4933 return error_mark_node;
4934 value = size_one_node;
4936 else
4938 if (complain)
4940 if (c_dialect_cxx ())
4941 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4942 "%<alignof%> applied to a function type");
4943 else
4944 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4945 "%<_Alignof%> applied to a function type");
4947 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4950 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4952 if (type_code == VOID_TYPE
4953 && complain && warn_pointer_arith)
4954 pedwarn (loc, OPT_Wpointer_arith,
4955 "invalid application of %qs to a void type", op_name);
4956 else if (!complain)
4957 return error_mark_node;
4958 value = size_one_node;
4960 else if (!COMPLETE_TYPE_P (type)
4961 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4963 if (complain)
4964 error_at (loc, "invalid application of %qs to incomplete type %qT",
4965 op_name, type);
4966 return error_mark_node;
4968 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4969 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4971 if (complain)
4972 error_at (loc, "invalid application of %qs to array type %qT of "
4973 "incomplete element type", op_name, type);
4974 return error_mark_node;
4976 else
4978 if (is_sizeof)
4979 /* Convert in case a char is more than one unit. */
4980 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4981 size_int (TYPE_PRECISION (char_type_node)
4982 / BITS_PER_UNIT));
4983 else
4984 value = size_int (TYPE_ALIGN_UNIT (type));
4987 /* VALUE will have the middle-end integer type sizetype.
4988 However, we should really return a value of type `size_t',
4989 which is just a typedef for an ordinary integer type. */
4990 value = fold_convert_loc (loc, size_type_node, value);
4992 return value;
4995 /* Implement the __alignof keyword: Return the minimum required
4996 alignment of EXPR, measured in bytes. For VAR_DECLs,
4997 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4998 from an "aligned" __attribute__ specification). LOC is the
4999 location of the ALIGNOF operator. */
5001 tree
5002 c_alignof_expr (location_t loc, tree expr)
5004 tree t;
5006 if (VAR_OR_FUNCTION_DECL_P (expr))
5007 t = size_int (DECL_ALIGN_UNIT (expr));
5009 else if (TREE_CODE (expr) == COMPONENT_REF
5010 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5012 error_at (loc, "%<__alignof%> applied to a bit-field");
5013 t = size_one_node;
5015 else if (TREE_CODE (expr) == COMPONENT_REF
5016 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5017 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5019 else if (TREE_CODE (expr) == INDIRECT_REF)
5021 tree t = TREE_OPERAND (expr, 0);
5022 tree best = t;
5023 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5025 while (CONVERT_EXPR_P (t)
5026 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5028 int thisalign;
5030 t = TREE_OPERAND (t, 0);
5031 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5032 if (thisalign > bestalign)
5033 best = t, bestalign = thisalign;
5035 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5037 else
5038 return c_alignof (loc, TREE_TYPE (expr));
5040 return fold_convert_loc (loc, size_type_node, t);
5043 /* Handle C and C++ default attributes. */
5045 enum built_in_attribute
5047 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5048 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5049 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5050 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5051 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5052 #include "builtin-attrs.def"
5053 #undef DEF_ATTR_NULL_TREE
5054 #undef DEF_ATTR_INT
5055 #undef DEF_ATTR_STRING
5056 #undef DEF_ATTR_IDENT
5057 #undef DEF_ATTR_TREE_LIST
5058 ATTR_LAST
5061 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5063 static void c_init_attributes (void);
5065 enum c_builtin_type
5067 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5068 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5069 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5070 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5071 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5072 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5073 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5074 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5075 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5076 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5077 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5078 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5079 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5080 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5081 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5082 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5083 NAME,
5084 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5085 #include "builtin-types.def"
5086 #undef DEF_PRIMITIVE_TYPE
5087 #undef DEF_FUNCTION_TYPE_0
5088 #undef DEF_FUNCTION_TYPE_1
5089 #undef DEF_FUNCTION_TYPE_2
5090 #undef DEF_FUNCTION_TYPE_3
5091 #undef DEF_FUNCTION_TYPE_4
5092 #undef DEF_FUNCTION_TYPE_5
5093 #undef DEF_FUNCTION_TYPE_6
5094 #undef DEF_FUNCTION_TYPE_7
5095 #undef DEF_FUNCTION_TYPE_8
5096 #undef DEF_FUNCTION_TYPE_VAR_0
5097 #undef DEF_FUNCTION_TYPE_VAR_1
5098 #undef DEF_FUNCTION_TYPE_VAR_2
5099 #undef DEF_FUNCTION_TYPE_VAR_3
5100 #undef DEF_FUNCTION_TYPE_VAR_4
5101 #undef DEF_FUNCTION_TYPE_VAR_5
5102 #undef DEF_POINTER_TYPE
5103 BT_LAST
5106 typedef enum c_builtin_type builtin_type;
5108 /* A temporary array for c_common_nodes_and_builtins. Used in
5109 communication with def_fn_type. */
5110 static tree builtin_types[(int) BT_LAST + 1];
5112 /* A helper function for c_common_nodes_and_builtins. Build function type
5113 for DEF with return type RET and N arguments. If VAR is true, then the
5114 function should be variadic after those N arguments.
5116 Takes special care not to ICE if any of the types involved are
5117 error_mark_node, which indicates that said type is not in fact available
5118 (see builtin_type_for_size). In which case the function type as a whole
5119 should be error_mark_node. */
5121 static void
5122 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5124 tree t;
5125 tree *args = XALLOCAVEC (tree, n);
5126 va_list list;
5127 int i;
5129 va_start (list, n);
5130 for (i = 0; i < n; ++i)
5132 builtin_type a = (builtin_type) va_arg (list, int);
5133 t = builtin_types[a];
5134 if (t == error_mark_node)
5135 goto egress;
5136 args[i] = t;
5139 t = builtin_types[ret];
5140 if (t == error_mark_node)
5141 goto egress;
5142 if (var)
5143 t = build_varargs_function_type_array (t, n, args);
5144 else
5145 t = build_function_type_array (t, n, args);
5147 egress:
5148 builtin_types[def] = t;
5149 va_end (list);
5152 /* Build builtin functions common to both C and C++ language
5153 frontends. */
5155 static void
5156 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5158 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5159 builtin_types[ENUM] = VALUE;
5160 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5161 def_fn_type (ENUM, RETURN, 0, 0);
5162 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5163 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5164 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5165 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5166 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5167 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5168 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5169 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5170 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5171 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5172 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5173 ARG6) \
5174 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5175 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5176 ARG6, ARG7) \
5177 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5178 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5179 ARG6, ARG7, ARG8) \
5180 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5181 ARG7, ARG8);
5182 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5183 def_fn_type (ENUM, RETURN, 1, 0);
5184 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5185 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5186 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5187 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5188 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5189 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5190 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5191 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5192 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5193 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5194 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5195 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5197 #include "builtin-types.def"
5199 #undef DEF_PRIMITIVE_TYPE
5200 #undef DEF_FUNCTION_TYPE_1
5201 #undef DEF_FUNCTION_TYPE_2
5202 #undef DEF_FUNCTION_TYPE_3
5203 #undef DEF_FUNCTION_TYPE_4
5204 #undef DEF_FUNCTION_TYPE_5
5205 #undef DEF_FUNCTION_TYPE_6
5206 #undef DEF_FUNCTION_TYPE_VAR_0
5207 #undef DEF_FUNCTION_TYPE_VAR_1
5208 #undef DEF_FUNCTION_TYPE_VAR_2
5209 #undef DEF_FUNCTION_TYPE_VAR_3
5210 #undef DEF_FUNCTION_TYPE_VAR_4
5211 #undef DEF_FUNCTION_TYPE_VAR_5
5212 #undef DEF_POINTER_TYPE
5213 builtin_types[(int) BT_LAST] = NULL_TREE;
5215 c_init_attributes ();
5217 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5218 NONANSI_P, ATTRS, IMPLICIT, COND) \
5219 if (NAME && COND) \
5220 def_builtin_1 (ENUM, NAME, CLASS, \
5221 builtin_types[(int) TYPE], \
5222 builtin_types[(int) LIBTYPE], \
5223 BOTH_P, FALLBACK_P, NONANSI_P, \
5224 built_in_attributes[(int) ATTRS], IMPLICIT);
5225 #include "builtins.def"
5226 #undef DEF_BUILTIN
5228 targetm.init_builtins ();
5230 build_common_builtin_nodes ();
5232 if (flag_enable_cilkplus)
5233 cilk_init_builtins ();
5236 /* Like get_identifier, but avoid warnings about null arguments when
5237 the argument may be NULL for targets where GCC lacks stdint.h type
5238 information. */
5240 static inline tree
5241 c_get_ident (const char *id)
5243 return get_identifier (id);
5246 /* Build tree nodes and builtin functions common to both C and C++ language
5247 frontends. */
5249 void
5250 c_common_nodes_and_builtins (void)
5252 int char16_type_size;
5253 int char32_type_size;
5254 int wchar_type_size;
5255 tree array_domain_type;
5256 tree va_list_ref_type_node;
5257 tree va_list_arg_type_node;
5259 build_common_tree_nodes (flag_signed_char, flag_short_double);
5261 /* Define `int' and `char' first so that dbx will output them first. */
5262 record_builtin_type (RID_INT, NULL, integer_type_node);
5263 record_builtin_type (RID_CHAR, "char", char_type_node);
5265 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5266 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5267 but not C. Are the conditionals here needed? */
5268 if (c_dialect_cxx ())
5269 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5270 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5271 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5272 record_builtin_type (RID_MAX, "long unsigned int",
5273 long_unsigned_type_node);
5274 if (int128_integer_type_node != NULL_TREE)
5276 record_builtin_type (RID_INT128, "__int128",
5277 int128_integer_type_node);
5278 record_builtin_type (RID_MAX, "__int128 unsigned",
5279 int128_unsigned_type_node);
5281 if (c_dialect_cxx ())
5282 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5283 record_builtin_type (RID_MAX, "long long int",
5284 long_long_integer_type_node);
5285 record_builtin_type (RID_MAX, "long long unsigned int",
5286 long_long_unsigned_type_node);
5287 if (c_dialect_cxx ())
5288 record_builtin_type (RID_MAX, "long long unsigned",
5289 long_long_unsigned_type_node);
5290 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5291 record_builtin_type (RID_MAX, "short unsigned int",
5292 short_unsigned_type_node);
5293 if (c_dialect_cxx ())
5294 record_builtin_type (RID_MAX, "unsigned short",
5295 short_unsigned_type_node);
5297 /* Define both `signed char' and `unsigned char'. */
5298 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5299 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5301 /* These are types that c_common_type_for_size and
5302 c_common_type_for_mode use. */
5303 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5304 TYPE_DECL, NULL_TREE,
5305 intQI_type_node));
5306 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5307 TYPE_DECL, NULL_TREE,
5308 intHI_type_node));
5309 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5310 TYPE_DECL, NULL_TREE,
5311 intSI_type_node));
5312 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5313 TYPE_DECL, NULL_TREE,
5314 intDI_type_node));
5315 #if HOST_BITS_PER_WIDE_INT >= 64
5316 if (targetm.scalar_mode_supported_p (TImode))
5317 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5318 TYPE_DECL,
5319 get_identifier ("__int128_t"),
5320 intTI_type_node));
5321 #endif
5322 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5323 TYPE_DECL, NULL_TREE,
5324 unsigned_intQI_type_node));
5325 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5326 TYPE_DECL, NULL_TREE,
5327 unsigned_intHI_type_node));
5328 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5329 TYPE_DECL, NULL_TREE,
5330 unsigned_intSI_type_node));
5331 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5332 TYPE_DECL, NULL_TREE,
5333 unsigned_intDI_type_node));
5334 #if HOST_BITS_PER_WIDE_INT >= 64
5335 if (targetm.scalar_mode_supported_p (TImode))
5336 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5337 TYPE_DECL,
5338 get_identifier ("__uint128_t"),
5339 unsigned_intTI_type_node));
5340 #endif
5342 /* Create the widest literal types. */
5343 widest_integer_literal_type_node
5344 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5345 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5346 TYPE_DECL, NULL_TREE,
5347 widest_integer_literal_type_node));
5349 widest_unsigned_literal_type_node
5350 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5351 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5352 TYPE_DECL, NULL_TREE,
5353 widest_unsigned_literal_type_node));
5355 signed_size_type_node = c_common_signed_type (size_type_node);
5357 pid_type_node =
5358 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5360 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5361 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5362 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5364 /* Only supported decimal floating point extension if the target
5365 actually supports underlying modes. */
5366 if (targetm.scalar_mode_supported_p (SDmode)
5367 && targetm.scalar_mode_supported_p (DDmode)
5368 && targetm.scalar_mode_supported_p (TDmode))
5370 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5371 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5372 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5375 if (targetm.fixed_point_supported_p ())
5377 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5378 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5379 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5380 record_builtin_type (RID_MAX, "long long _Fract",
5381 long_long_fract_type_node);
5382 record_builtin_type (RID_MAX, "unsigned short _Fract",
5383 unsigned_short_fract_type_node);
5384 record_builtin_type (RID_MAX, "unsigned _Fract",
5385 unsigned_fract_type_node);
5386 record_builtin_type (RID_MAX, "unsigned long _Fract",
5387 unsigned_long_fract_type_node);
5388 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5389 unsigned_long_long_fract_type_node);
5390 record_builtin_type (RID_MAX, "_Sat short _Fract",
5391 sat_short_fract_type_node);
5392 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5393 record_builtin_type (RID_MAX, "_Sat long _Fract",
5394 sat_long_fract_type_node);
5395 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5396 sat_long_long_fract_type_node);
5397 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5398 sat_unsigned_short_fract_type_node);
5399 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5400 sat_unsigned_fract_type_node);
5401 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5402 sat_unsigned_long_fract_type_node);
5403 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5404 sat_unsigned_long_long_fract_type_node);
5405 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5406 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5407 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5408 record_builtin_type (RID_MAX, "long long _Accum",
5409 long_long_accum_type_node);
5410 record_builtin_type (RID_MAX, "unsigned short _Accum",
5411 unsigned_short_accum_type_node);
5412 record_builtin_type (RID_MAX, "unsigned _Accum",
5413 unsigned_accum_type_node);
5414 record_builtin_type (RID_MAX, "unsigned long _Accum",
5415 unsigned_long_accum_type_node);
5416 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5417 unsigned_long_long_accum_type_node);
5418 record_builtin_type (RID_MAX, "_Sat short _Accum",
5419 sat_short_accum_type_node);
5420 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5421 record_builtin_type (RID_MAX, "_Sat long _Accum",
5422 sat_long_accum_type_node);
5423 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5424 sat_long_long_accum_type_node);
5425 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5426 sat_unsigned_short_accum_type_node);
5427 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5428 sat_unsigned_accum_type_node);
5429 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5430 sat_unsigned_long_accum_type_node);
5431 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5432 sat_unsigned_long_long_accum_type_node);
5436 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5437 TYPE_DECL,
5438 get_identifier ("complex int"),
5439 complex_integer_type_node));
5440 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5441 TYPE_DECL,
5442 get_identifier ("complex float"),
5443 complex_float_type_node));
5444 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5445 TYPE_DECL,
5446 get_identifier ("complex double"),
5447 complex_double_type_node));
5448 lang_hooks.decls.pushdecl
5449 (build_decl (UNKNOWN_LOCATION,
5450 TYPE_DECL, get_identifier ("complex long double"),
5451 complex_long_double_type_node));
5453 if (c_dialect_cxx ())
5454 /* For C++, make fileptr_type_node a distinct void * type until
5455 FILE type is defined. */
5456 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5458 record_builtin_type (RID_VOID, NULL, void_type_node);
5460 /* Set the TYPE_NAME for any variants that were built before
5461 record_builtin_type gave names to the built-in types. */
5463 tree void_name = TYPE_NAME (void_type_node);
5464 TYPE_NAME (void_type_node) = NULL_TREE;
5465 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5466 = void_name;
5467 TYPE_NAME (void_type_node) = void_name;
5470 /* This node must not be shared. */
5471 void_zero_node = make_node (INTEGER_CST);
5472 TREE_TYPE (void_zero_node) = void_type_node;
5474 void_list_node = build_void_list_node ();
5476 /* Make a type to be the domain of a few array types
5477 whose domains don't really matter.
5478 200 is small enough that it always fits in size_t
5479 and large enough that it can hold most function names for the
5480 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5481 array_domain_type = build_index_type (size_int (200));
5483 /* Make a type for arrays of characters.
5484 With luck nothing will ever really depend on the length of this
5485 array type. */
5486 char_array_type_node
5487 = build_array_type (char_type_node, array_domain_type);
5489 /* Likewise for arrays of ints. */
5490 int_array_type_node
5491 = build_array_type (integer_type_node, array_domain_type);
5493 string_type_node = build_pointer_type (char_type_node);
5494 const_string_type_node
5495 = build_pointer_type (build_qualified_type
5496 (char_type_node, TYPE_QUAL_CONST));
5498 /* This is special for C++ so functions can be overloaded. */
5499 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5500 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5501 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5502 underlying_wchar_type_node = wchar_type_node;
5503 if (c_dialect_cxx ())
5505 if (TYPE_UNSIGNED (wchar_type_node))
5506 wchar_type_node = make_unsigned_type (wchar_type_size);
5507 else
5508 wchar_type_node = make_signed_type (wchar_type_size);
5509 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5512 /* This is for wide string constants. */
5513 wchar_array_type_node
5514 = build_array_type (wchar_type_node, array_domain_type);
5516 /* Define 'char16_t'. */
5517 char16_type_node = get_identifier (CHAR16_TYPE);
5518 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5519 char16_type_size = TYPE_PRECISION (char16_type_node);
5520 if (c_dialect_cxx ())
5522 char16_type_node = make_unsigned_type (char16_type_size);
5524 if (cxx_dialect >= cxx11)
5525 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5528 /* This is for UTF-16 string constants. */
5529 char16_array_type_node
5530 = build_array_type (char16_type_node, array_domain_type);
5532 /* Define 'char32_t'. */
5533 char32_type_node = get_identifier (CHAR32_TYPE);
5534 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5535 char32_type_size = TYPE_PRECISION (char32_type_node);
5536 if (c_dialect_cxx ())
5538 char32_type_node = make_unsigned_type (char32_type_size);
5540 if (cxx_dialect >= cxx11)
5541 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5544 /* This is for UTF-32 string constants. */
5545 char32_array_type_node
5546 = build_array_type (char32_type_node, array_domain_type);
5548 wint_type_node =
5549 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5551 intmax_type_node =
5552 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5553 uintmax_type_node =
5554 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5556 if (SIG_ATOMIC_TYPE)
5557 sig_atomic_type_node =
5558 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5559 if (INT8_TYPE)
5560 int8_type_node =
5561 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5562 if (INT16_TYPE)
5563 int16_type_node =
5564 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5565 if (INT32_TYPE)
5566 int32_type_node =
5567 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5568 if (INT64_TYPE)
5569 int64_type_node =
5570 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5571 if (UINT8_TYPE)
5572 uint8_type_node =
5573 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5574 if (UINT16_TYPE)
5575 c_uint16_type_node =
5576 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5577 if (UINT32_TYPE)
5578 c_uint32_type_node =
5579 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5580 if (UINT64_TYPE)
5581 c_uint64_type_node =
5582 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5583 if (INT_LEAST8_TYPE)
5584 int_least8_type_node =
5585 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5586 if (INT_LEAST16_TYPE)
5587 int_least16_type_node =
5588 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5589 if (INT_LEAST32_TYPE)
5590 int_least32_type_node =
5591 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5592 if (INT_LEAST64_TYPE)
5593 int_least64_type_node =
5594 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5595 if (UINT_LEAST8_TYPE)
5596 uint_least8_type_node =
5597 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5598 if (UINT_LEAST16_TYPE)
5599 uint_least16_type_node =
5600 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5601 if (UINT_LEAST32_TYPE)
5602 uint_least32_type_node =
5603 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5604 if (UINT_LEAST64_TYPE)
5605 uint_least64_type_node =
5606 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5607 if (INT_FAST8_TYPE)
5608 int_fast8_type_node =
5609 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5610 if (INT_FAST16_TYPE)
5611 int_fast16_type_node =
5612 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5613 if (INT_FAST32_TYPE)
5614 int_fast32_type_node =
5615 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5616 if (INT_FAST64_TYPE)
5617 int_fast64_type_node =
5618 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5619 if (UINT_FAST8_TYPE)
5620 uint_fast8_type_node =
5621 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5622 if (UINT_FAST16_TYPE)
5623 uint_fast16_type_node =
5624 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5625 if (UINT_FAST32_TYPE)
5626 uint_fast32_type_node =
5627 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5628 if (UINT_FAST64_TYPE)
5629 uint_fast64_type_node =
5630 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5631 if (INTPTR_TYPE)
5632 intptr_type_node =
5633 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5634 if (UINTPTR_TYPE)
5635 uintptr_type_node =
5636 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5638 default_function_type
5639 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5640 ptrdiff_type_node
5641 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5642 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5644 lang_hooks.decls.pushdecl
5645 (build_decl (UNKNOWN_LOCATION,
5646 TYPE_DECL, get_identifier ("__builtin_va_list"),
5647 va_list_type_node));
5648 if (targetm.enum_va_list_p)
5650 int l;
5651 const char *pname;
5652 tree ptype;
5654 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5656 lang_hooks.decls.pushdecl
5657 (build_decl (UNKNOWN_LOCATION,
5658 TYPE_DECL, get_identifier (pname),
5659 ptype));
5664 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5666 va_list_arg_type_node = va_list_ref_type_node =
5667 build_pointer_type (TREE_TYPE (va_list_type_node));
5669 else
5671 va_list_arg_type_node = va_list_type_node;
5672 va_list_ref_type_node = build_reference_type (va_list_type_node);
5675 if (!flag_preprocess_only)
5676 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5678 main_identifier_node = get_identifier ("main");
5680 /* Create the built-in __null node. It is important that this is
5681 not shared. */
5682 null_node = make_node (INTEGER_CST);
5683 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5685 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5686 memset (builtin_types, 0, sizeof (builtin_types));
5689 /* The number of named compound-literals generated thus far. */
5690 static GTY(()) int compound_literal_number;
5692 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5694 void
5695 set_compound_literal_name (tree decl)
5697 char *name;
5698 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5699 compound_literal_number);
5700 compound_literal_number++;
5701 DECL_NAME (decl) = get_identifier (name);
5704 tree
5705 build_va_arg (location_t loc, tree expr, tree type)
5707 expr = build1 (VA_ARG_EXPR, type, expr);
5708 SET_EXPR_LOCATION (expr, loc);
5709 return expr;
5713 /* Linked list of disabled built-in functions. */
5715 typedef struct disabled_builtin
5717 const char *name;
5718 struct disabled_builtin *next;
5719 } disabled_builtin;
5720 static disabled_builtin *disabled_builtins = NULL;
5722 static bool builtin_function_disabled_p (const char *);
5724 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5725 begins with "__builtin_", give an error. */
5727 void
5728 disable_builtin_function (const char *name)
5730 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5731 error ("cannot disable built-in function %qs", name);
5732 else
5734 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5735 new_disabled_builtin->name = name;
5736 new_disabled_builtin->next = disabled_builtins;
5737 disabled_builtins = new_disabled_builtin;
5742 /* Return true if the built-in function NAME has been disabled, false
5743 otherwise. */
5745 static bool
5746 builtin_function_disabled_p (const char *name)
5748 disabled_builtin *p;
5749 for (p = disabled_builtins; p != NULL; p = p->next)
5751 if (strcmp (name, p->name) == 0)
5752 return true;
5754 return false;
5758 /* Worker for DEF_BUILTIN.
5759 Possibly define a builtin function with one or two names.
5760 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5761 nonansi_p and flag_no_nonansi_builtin. */
5763 static void
5764 def_builtin_1 (enum built_in_function fncode,
5765 const char *name,
5766 enum built_in_class fnclass,
5767 tree fntype, tree libtype,
5768 bool both_p, bool fallback_p, bool nonansi_p,
5769 tree fnattrs, bool implicit_p)
5771 tree decl;
5772 const char *libname;
5774 if (fntype == error_mark_node)
5775 return;
5777 gcc_assert ((!both_p && !fallback_p)
5778 || !strncmp (name, "__builtin_",
5779 strlen ("__builtin_")));
5781 libname = name + strlen ("__builtin_");
5782 decl = add_builtin_function (name, fntype, fncode, fnclass,
5783 (fallback_p ? libname : NULL),
5784 fnattrs);
5786 set_builtin_decl (fncode, decl, implicit_p);
5788 if (both_p
5789 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5790 && !(nonansi_p && flag_no_nonansi_builtin))
5791 add_builtin_function (libname, libtype, fncode, fnclass,
5792 NULL, fnattrs);
5795 /* Nonzero if the type T promotes to int. This is (nearly) the
5796 integral promotions defined in ISO C99 6.3.1.1/2. */
5798 bool
5799 c_promoting_integer_type_p (const_tree t)
5801 switch (TREE_CODE (t))
5803 case INTEGER_TYPE:
5804 return (TYPE_MAIN_VARIANT (t) == char_type_node
5805 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5806 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5807 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5808 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5809 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5811 case ENUMERAL_TYPE:
5812 /* ??? Technically all enumerations not larger than an int
5813 promote to an int. But this is used along code paths
5814 that only want to notice a size change. */
5815 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5817 case BOOLEAN_TYPE:
5818 return 1;
5820 default:
5821 return 0;
5825 /* Return 1 if PARMS specifies a fixed number of parameters
5826 and none of their types is affected by default promotions. */
5829 self_promoting_args_p (const_tree parms)
5831 const_tree t;
5832 for (t = parms; t; t = TREE_CHAIN (t))
5834 tree type = TREE_VALUE (t);
5836 if (type == error_mark_node)
5837 continue;
5839 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5840 return 0;
5842 if (type == 0)
5843 return 0;
5845 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5846 return 0;
5848 if (c_promoting_integer_type_p (type))
5849 return 0;
5851 return 1;
5854 /* Recursively remove any '*' or '&' operator from TYPE. */
5855 tree
5856 strip_pointer_operator (tree t)
5858 while (POINTER_TYPE_P (t))
5859 t = TREE_TYPE (t);
5860 return t;
5863 /* Recursively remove pointer or array type from TYPE. */
5864 tree
5865 strip_pointer_or_array_types (tree t)
5867 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5868 t = TREE_TYPE (t);
5869 return t;
5872 /* Used to compare case labels. K1 and K2 are actually tree nodes
5873 representing case labels, or NULL_TREE for a `default' label.
5874 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5875 K2, and 0 if K1 and K2 are equal. */
5878 case_compare (splay_tree_key k1, splay_tree_key k2)
5880 /* Consider a NULL key (such as arises with a `default' label) to be
5881 smaller than anything else. */
5882 if (!k1)
5883 return k2 ? -1 : 0;
5884 else if (!k2)
5885 return k1 ? 1 : 0;
5887 return tree_int_cst_compare ((tree) k1, (tree) k2);
5890 /* Process a case label, located at LOC, for the range LOW_VALUE
5891 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5892 then this case label is actually a `default' label. If only
5893 HIGH_VALUE is NULL_TREE, then case label was declared using the
5894 usual C/C++ syntax, rather than the GNU case range extension.
5895 CASES is a tree containing all the case ranges processed so far;
5896 COND is the condition for the switch-statement itself. Returns the
5897 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5898 is created. */
5900 tree
5901 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5902 tree low_value, tree high_value)
5904 tree type;
5905 tree label;
5906 tree case_label;
5907 splay_tree_node node;
5909 /* Create the LABEL_DECL itself. */
5910 label = create_artificial_label (loc);
5912 /* If there was an error processing the switch condition, bail now
5913 before we get more confused. */
5914 if (!cond || cond == error_mark_node)
5915 goto error_out;
5917 if ((low_value && TREE_TYPE (low_value)
5918 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5919 || (high_value && TREE_TYPE (high_value)
5920 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5922 error_at (loc, "pointers are not permitted as case values");
5923 goto error_out;
5926 /* Case ranges are a GNU extension. */
5927 if (high_value)
5928 pedwarn (loc, OPT_Wpedantic,
5929 "range expressions in switch statements are non-standard");
5931 type = TREE_TYPE (cond);
5932 if (low_value)
5934 low_value = check_case_value (low_value);
5935 low_value = convert_and_check (type, low_value);
5936 if (low_value == error_mark_node)
5937 goto error_out;
5939 if (high_value)
5941 high_value = check_case_value (high_value);
5942 high_value = convert_and_check (type, high_value);
5943 if (high_value == error_mark_node)
5944 goto error_out;
5947 if (low_value && high_value)
5949 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5950 really a case range, even though it was written that way.
5951 Remove the HIGH_VALUE to simplify later processing. */
5952 if (tree_int_cst_equal (low_value, high_value))
5953 high_value = NULL_TREE;
5954 else if (!tree_int_cst_lt (low_value, high_value))
5955 warning_at (loc, 0, "empty range specified");
5958 /* See if the case is in range of the type of the original testing
5959 expression. If both low_value and high_value are out of range,
5960 don't insert the case label and return NULL_TREE. */
5961 if (low_value
5962 && !check_case_bounds (type, orig_type,
5963 &low_value, high_value ? &high_value : NULL))
5964 return NULL_TREE;
5966 /* Look up the LOW_VALUE in the table of case labels we already
5967 have. */
5968 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5969 /* If there was not an exact match, check for overlapping ranges.
5970 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5971 that's a `default' label and the only overlap is an exact match. */
5972 if (!node && (low_value || high_value))
5974 splay_tree_node low_bound;
5975 splay_tree_node high_bound;
5977 /* Even though there wasn't an exact match, there might be an
5978 overlap between this case range and another case range.
5979 Since we've (inductively) not allowed any overlapping case
5980 ranges, we simply need to find the greatest low case label
5981 that is smaller that LOW_VALUE, and the smallest low case
5982 label that is greater than LOW_VALUE. If there is an overlap
5983 it will occur in one of these two ranges. */
5984 low_bound = splay_tree_predecessor (cases,
5985 (splay_tree_key) low_value);
5986 high_bound = splay_tree_successor (cases,
5987 (splay_tree_key) low_value);
5989 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5990 the LOW_VALUE, so there is no need to check unless the
5991 LOW_BOUND is in fact itself a case range. */
5992 if (low_bound
5993 && CASE_HIGH ((tree) low_bound->value)
5994 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5995 low_value) >= 0)
5996 node = low_bound;
5997 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5998 range is bigger than the low end of the current range, so we
5999 are only interested if the current range is a real range, and
6000 not an ordinary case label. */
6001 else if (high_bound
6002 && high_value
6003 && (tree_int_cst_compare ((tree) high_bound->key,
6004 high_value)
6005 <= 0))
6006 node = high_bound;
6008 /* If there was an overlap, issue an error. */
6009 if (node)
6011 tree duplicate = CASE_LABEL ((tree) node->value);
6013 if (high_value)
6015 error_at (loc, "duplicate (or overlapping) case value");
6016 error_at (DECL_SOURCE_LOCATION (duplicate),
6017 "this is the first entry overlapping that value");
6019 else if (low_value)
6021 error_at (loc, "duplicate case value") ;
6022 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6024 else
6026 error_at (loc, "multiple default labels in one switch");
6027 error_at (DECL_SOURCE_LOCATION (duplicate),
6028 "this is the first default label");
6030 goto error_out;
6033 /* Add a CASE_LABEL to the statement-tree. */
6034 case_label = add_stmt (build_case_label (low_value, high_value, label));
6035 /* Register this case label in the splay tree. */
6036 splay_tree_insert (cases,
6037 (splay_tree_key) low_value,
6038 (splay_tree_value) case_label);
6040 return case_label;
6042 error_out:
6043 /* Add a label so that the back-end doesn't think that the beginning of
6044 the switch is unreachable. Note that we do not add a case label, as
6045 that just leads to duplicates and thence to failure later on. */
6046 if (!cases->root)
6048 tree t = create_artificial_label (loc);
6049 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6051 return error_mark_node;
6054 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6055 Used to verify that case values match up with enumerator values. */
6057 static void
6058 match_case_to_enum_1 (tree key, tree type, tree label)
6060 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6062 /* ??? Not working too hard to print the double-word value.
6063 Should perhaps be done with %lwd in the diagnostic routines? */
6064 if (TREE_INT_CST_HIGH (key) == 0)
6065 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6066 TREE_INT_CST_LOW (key));
6067 else if (!TYPE_UNSIGNED (type)
6068 && TREE_INT_CST_HIGH (key) == -1
6069 && TREE_INT_CST_LOW (key) != 0)
6070 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6071 -TREE_INT_CST_LOW (key));
6072 else
6073 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6074 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6075 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6077 if (TYPE_NAME (type) == 0)
6078 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6079 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6080 "case value %qs not in enumerated type",
6081 buf);
6082 else
6083 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6084 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6085 "case value %qs not in enumerated type %qT",
6086 buf, type);
6089 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6090 Used to verify that case values match up with enumerator values. */
6092 static int
6093 match_case_to_enum (splay_tree_node node, void *data)
6095 tree label = (tree) node->value;
6096 tree type = (tree) data;
6098 /* Skip default case. */
6099 if (!CASE_LOW (label))
6100 return 0;
6102 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6103 when we did our enum->case scan. Reset our scratch bit after. */
6104 if (!CASE_LOW_SEEN (label))
6105 match_case_to_enum_1 (CASE_LOW (label), type, label);
6106 else
6107 CASE_LOW_SEEN (label) = 0;
6109 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6110 not set, that means that CASE_HIGH did not appear when we did our
6111 enum->case scan. Reset our scratch bit after. */
6112 if (CASE_HIGH (label))
6114 if (!CASE_HIGH_SEEN (label))
6115 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6116 else
6117 CASE_HIGH_SEEN (label) = 0;
6120 return 0;
6123 /* Handle -Wswitch*. Called from the front end after parsing the
6124 switch construct. */
6125 /* ??? Should probably be somewhere generic, since other languages
6126 besides C and C++ would want this. At the moment, however, C/C++
6127 are the only tree-ssa languages that support enumerations at all,
6128 so the point is moot. */
6130 void
6131 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6132 tree type, tree cond)
6134 splay_tree_node default_node;
6135 splay_tree_node node;
6136 tree chain;
6138 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6139 return;
6141 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6142 if (!default_node)
6143 warning_at (switch_location, OPT_Wswitch_default,
6144 "switch missing default case");
6146 /* From here on, we only care about about enumerated types. */
6147 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6148 return;
6150 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6151 if (!warn_switch_enum && !warn_switch)
6152 return;
6154 /* Check the cases. Warn about case values which are not members of
6155 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6156 there is no default case, check that exactly all enumeration
6157 literals are covered by the cases. */
6159 /* Clearing COND if it is not an integer constant simplifies
6160 the tests inside the loop below. */
6161 if (TREE_CODE (cond) != INTEGER_CST)
6162 cond = NULL_TREE;
6164 /* The time complexity here is O(N*lg(N)) worst case, but for the
6165 common case of monotonically increasing enumerators, it is
6166 O(N), since the nature of the splay tree will keep the next
6167 element adjacent to the root at all times. */
6169 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6171 tree value = TREE_VALUE (chain);
6172 if (TREE_CODE (value) == CONST_DECL)
6173 value = DECL_INITIAL (value);
6174 node = splay_tree_lookup (cases, (splay_tree_key) value);
6175 if (node)
6177 /* Mark the CASE_LOW part of the case entry as seen. */
6178 tree label = (tree) node->value;
6179 CASE_LOW_SEEN (label) = 1;
6180 continue;
6183 /* Even though there wasn't an exact match, there might be a
6184 case range which includes the enumerator's value. */
6185 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6186 if (node && CASE_HIGH ((tree) node->value))
6188 tree label = (tree) node->value;
6189 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6190 if (cmp >= 0)
6192 /* If we match the upper bound exactly, mark the CASE_HIGH
6193 part of the case entry as seen. */
6194 if (cmp == 0)
6195 CASE_HIGH_SEEN (label) = 1;
6196 continue;
6200 /* We've now determined that this enumerated literal isn't
6201 handled by the case labels of the switch statement. */
6203 /* If the switch expression is a constant, we only really care
6204 about whether that constant is handled by the switch. */
6205 if (cond && tree_int_cst_compare (cond, value))
6206 continue;
6208 /* If there is a default_node, the only relevant option is
6209 Wswitch-enum. Otherwise, if both are enabled then we prefer
6210 to warn using -Wswitch because -Wswitch is enabled by -Wall
6211 while -Wswitch-enum is explicit. */
6212 warning_at (switch_location,
6213 (default_node || !warn_switch
6214 ? OPT_Wswitch_enum
6215 : OPT_Wswitch),
6216 "enumeration value %qE not handled in switch",
6217 TREE_PURPOSE (chain));
6220 /* Warn if there are case expressions that don't correspond to
6221 enumerators. This can occur since C and C++ don't enforce
6222 type-checking of assignments to enumeration variables.
6224 The time complexity here is now always O(N) worst case, since
6225 we should have marked both the lower bound and upper bound of
6226 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6227 above. This scan also resets those fields. */
6229 splay_tree_foreach (cases, match_case_to_enum, type);
6232 /* Finish an expression taking the address of LABEL (an
6233 IDENTIFIER_NODE). Returns an expression for the address.
6235 LOC is the location for the expression returned. */
6237 tree
6238 finish_label_address_expr (tree label, location_t loc)
6240 tree result;
6242 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6244 if (label == error_mark_node)
6245 return error_mark_node;
6247 label = lookup_label (label);
6248 if (label == NULL_TREE)
6249 result = null_pointer_node;
6250 else
6252 TREE_USED (label) = 1;
6253 result = build1 (ADDR_EXPR, ptr_type_node, label);
6254 /* The current function is not necessarily uninlinable.
6255 Computed gotos are incompatible with inlining, but the value
6256 here could be used only in a diagnostic, for example. */
6257 protected_set_expr_location (result, loc);
6260 return result;
6264 /* Given a boolean expression ARG, return a tree representing an increment
6265 or decrement (as indicated by CODE) of ARG. The front end must check for
6266 invalid cases (e.g., decrement in C++). */
6267 tree
6268 boolean_increment (enum tree_code code, tree arg)
6270 tree val;
6271 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6273 arg = stabilize_reference (arg);
6274 switch (code)
6276 case PREINCREMENT_EXPR:
6277 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6278 break;
6279 case POSTINCREMENT_EXPR:
6280 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6281 arg = save_expr (arg);
6282 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6283 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6284 break;
6285 case PREDECREMENT_EXPR:
6286 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6287 invert_truthvalue_loc (input_location, arg));
6288 break;
6289 case POSTDECREMENT_EXPR:
6290 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6291 invert_truthvalue_loc (input_location, arg));
6292 arg = save_expr (arg);
6293 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6294 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6295 break;
6296 default:
6297 gcc_unreachable ();
6299 TREE_SIDE_EFFECTS (val) = 1;
6300 return val;
6303 /* Built-in macros for stddef.h and stdint.h, that require macros
6304 defined in this file. */
6305 void
6306 c_stddef_cpp_builtins(void)
6308 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6309 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6310 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6311 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6312 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6313 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6314 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6315 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6316 if (SIG_ATOMIC_TYPE)
6317 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6318 if (INT8_TYPE)
6319 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6320 if (INT16_TYPE)
6321 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6322 if (INT32_TYPE)
6323 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6324 if (INT64_TYPE)
6325 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6326 if (UINT8_TYPE)
6327 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6328 if (UINT16_TYPE)
6329 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6330 if (UINT32_TYPE)
6331 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6332 if (UINT64_TYPE)
6333 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6334 if (INT_LEAST8_TYPE)
6335 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6336 if (INT_LEAST16_TYPE)
6337 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6338 if (INT_LEAST32_TYPE)
6339 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6340 if (INT_LEAST64_TYPE)
6341 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6342 if (UINT_LEAST8_TYPE)
6343 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6344 if (UINT_LEAST16_TYPE)
6345 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6346 if (UINT_LEAST32_TYPE)
6347 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6348 if (UINT_LEAST64_TYPE)
6349 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6350 if (INT_FAST8_TYPE)
6351 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6352 if (INT_FAST16_TYPE)
6353 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6354 if (INT_FAST32_TYPE)
6355 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6356 if (INT_FAST64_TYPE)
6357 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6358 if (UINT_FAST8_TYPE)
6359 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6360 if (UINT_FAST16_TYPE)
6361 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6362 if (UINT_FAST32_TYPE)
6363 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6364 if (UINT_FAST64_TYPE)
6365 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6366 if (INTPTR_TYPE)
6367 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6368 if (UINTPTR_TYPE)
6369 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6372 static void
6373 c_init_attributes (void)
6375 /* Fill in the built_in_attributes array. */
6376 #define DEF_ATTR_NULL_TREE(ENUM) \
6377 built_in_attributes[(int) ENUM] = NULL_TREE;
6378 #define DEF_ATTR_INT(ENUM, VALUE) \
6379 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6380 #define DEF_ATTR_STRING(ENUM, VALUE) \
6381 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6382 #define DEF_ATTR_IDENT(ENUM, STRING) \
6383 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6384 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6385 built_in_attributes[(int) ENUM] \
6386 = tree_cons (built_in_attributes[(int) PURPOSE], \
6387 built_in_attributes[(int) VALUE], \
6388 built_in_attributes[(int) CHAIN]);
6389 #include "builtin-attrs.def"
6390 #undef DEF_ATTR_NULL_TREE
6391 #undef DEF_ATTR_INT
6392 #undef DEF_ATTR_IDENT
6393 #undef DEF_ATTR_TREE_LIST
6396 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6397 identifier as an argument, so the front end shouldn't look it up. */
6399 bool
6400 attribute_takes_identifier_p (const_tree attr_id)
6402 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6403 if (spec == NULL)
6404 /* Unknown attribute that we'll end up ignoring, return true so we
6405 don't complain about an identifier argument. */
6406 return true;
6407 else if (!strcmp ("mode", spec->name)
6408 || !strcmp ("format", spec->name)
6409 || !strcmp ("cleanup", spec->name))
6410 return true;
6411 else
6412 return targetm.attribute_takes_identifier_p (attr_id);
6415 /* Attribute handlers common to C front ends. */
6417 /* Handle a "packed" attribute; arguments as in
6418 struct attribute_spec.handler. */
6420 static tree
6421 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6422 int flags, bool *no_add_attrs)
6424 if (TYPE_P (*node))
6426 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6427 *node = build_variant_type_copy (*node);
6428 TYPE_PACKED (*node) = 1;
6430 else if (TREE_CODE (*node) == FIELD_DECL)
6432 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6433 /* Still pack bitfields. */
6434 && ! DECL_INITIAL (*node))
6435 warning (OPT_Wattributes,
6436 "%qE attribute ignored for field of type %qT",
6437 name, TREE_TYPE (*node));
6438 else
6439 DECL_PACKED (*node) = 1;
6441 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6442 used for DECL_REGISTER. It wouldn't mean anything anyway.
6443 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6444 that changes what the typedef is typing. */
6445 else
6447 warning (OPT_Wattributes, "%qE attribute ignored", name);
6448 *no_add_attrs = true;
6451 return NULL_TREE;
6454 /* Handle a "nocommon" attribute; arguments as in
6455 struct attribute_spec.handler. */
6457 static tree
6458 handle_nocommon_attribute (tree *node, tree name,
6459 tree ARG_UNUSED (args),
6460 int ARG_UNUSED (flags), bool *no_add_attrs)
6462 if (TREE_CODE (*node) == VAR_DECL)
6463 DECL_COMMON (*node) = 0;
6464 else
6466 warning (OPT_Wattributes, "%qE attribute ignored", name);
6467 *no_add_attrs = true;
6470 return NULL_TREE;
6473 /* Handle a "common" attribute; arguments as in
6474 struct attribute_spec.handler. */
6476 static tree
6477 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6478 int ARG_UNUSED (flags), bool *no_add_attrs)
6480 if (TREE_CODE (*node) == VAR_DECL)
6481 DECL_COMMON (*node) = 1;
6482 else
6484 warning (OPT_Wattributes, "%qE attribute ignored", name);
6485 *no_add_attrs = true;
6488 return NULL_TREE;
6491 /* Handle a "noreturn" attribute; arguments as in
6492 struct attribute_spec.handler. */
6494 static tree
6495 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6496 int ARG_UNUSED (flags), bool *no_add_attrs)
6498 tree type = TREE_TYPE (*node);
6500 /* See FIXME comment in c_common_attribute_table. */
6501 if (TREE_CODE (*node) == FUNCTION_DECL
6502 || objc_method_decl (TREE_CODE (*node)))
6503 TREE_THIS_VOLATILE (*node) = 1;
6504 else if (TREE_CODE (type) == POINTER_TYPE
6505 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6506 TREE_TYPE (*node)
6507 = build_pointer_type
6508 (build_type_variant (TREE_TYPE (type),
6509 TYPE_READONLY (TREE_TYPE (type)), 1));
6510 else
6512 warning (OPT_Wattributes, "%qE attribute ignored", name);
6513 *no_add_attrs = true;
6516 return NULL_TREE;
6519 /* Handle a "hot" and attribute; arguments as in
6520 struct attribute_spec.handler. */
6522 static tree
6523 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6524 int ARG_UNUSED (flags), bool *no_add_attrs)
6526 if (TREE_CODE (*node) == FUNCTION_DECL
6527 || TREE_CODE (*node) == LABEL_DECL)
6529 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6531 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6532 name, "cold");
6533 *no_add_attrs = true;
6535 /* Most of the rest of the hot processing is done later with
6536 lookup_attribute. */
6538 else
6540 warning (OPT_Wattributes, "%qE attribute ignored", name);
6541 *no_add_attrs = true;
6544 return NULL_TREE;
6547 /* Handle a "cold" and attribute; arguments as in
6548 struct attribute_spec.handler. */
6550 static tree
6551 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6552 int ARG_UNUSED (flags), bool *no_add_attrs)
6554 if (TREE_CODE (*node) == FUNCTION_DECL
6555 || TREE_CODE (*node) == LABEL_DECL)
6557 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6559 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6560 name, "hot");
6561 *no_add_attrs = true;
6563 /* Most of the rest of the cold processing is done later with
6564 lookup_attribute. */
6566 else
6568 warning (OPT_Wattributes, "%qE attribute ignored", name);
6569 *no_add_attrs = true;
6572 return NULL_TREE;
6575 /* Handle a "no_sanitize_address" attribute; arguments as in
6576 struct attribute_spec.handler. */
6578 static tree
6579 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6580 bool *no_add_attrs)
6582 if (TREE_CODE (*node) != FUNCTION_DECL)
6584 warning (OPT_Wattributes, "%qE attribute ignored", name);
6585 *no_add_attrs = true;
6588 return NULL_TREE;
6591 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6592 struct attribute_spec.handler. */
6594 static tree
6595 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6596 bool *no_add_attrs)
6598 if (TREE_CODE (*node) != FUNCTION_DECL)
6599 warning (OPT_Wattributes, "%qE attribute ignored", name);
6600 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6601 DECL_ATTRIBUTES (*node)
6602 = tree_cons (get_identifier ("no_sanitize_address"),
6603 NULL_TREE, DECL_ATTRIBUTES (*node));
6604 *no_add_attrs = true;
6605 return NULL_TREE;
6608 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6609 struct attribute_spec.handler. */
6611 static tree
6612 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6613 bool *no_add_attrs)
6615 if (TREE_CODE (*node) != FUNCTION_DECL)
6617 warning (OPT_Wattributes, "%qE attribute ignored", name);
6618 *no_add_attrs = true;
6621 return NULL_TREE;
6624 /* Handle a "noinline" attribute; arguments as in
6625 struct attribute_spec.handler. */
6627 static tree
6628 handle_noinline_attribute (tree *node, tree name,
6629 tree ARG_UNUSED (args),
6630 int ARG_UNUSED (flags), bool *no_add_attrs)
6632 if (TREE_CODE (*node) == FUNCTION_DECL)
6633 DECL_UNINLINABLE (*node) = 1;
6634 else
6636 warning (OPT_Wattributes, "%qE attribute ignored", name);
6637 *no_add_attrs = true;
6640 return NULL_TREE;
6643 /* Handle a "noclone" attribute; arguments as in
6644 struct attribute_spec.handler. */
6646 static tree
6647 handle_noclone_attribute (tree *node, tree name,
6648 tree ARG_UNUSED (args),
6649 int ARG_UNUSED (flags), bool *no_add_attrs)
6651 if (TREE_CODE (*node) != FUNCTION_DECL)
6653 warning (OPT_Wattributes, "%qE attribute ignored", name);
6654 *no_add_attrs = true;
6657 return NULL_TREE;
6660 /* Handle a "always_inline" attribute; arguments as in
6661 struct attribute_spec.handler. */
6663 static tree
6664 handle_always_inline_attribute (tree *node, tree name,
6665 tree ARG_UNUSED (args),
6666 int ARG_UNUSED (flags),
6667 bool *no_add_attrs)
6669 if (TREE_CODE (*node) == FUNCTION_DECL)
6671 /* Set the attribute and mark it for disregarding inline
6672 limits. */
6673 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6675 else
6677 warning (OPT_Wattributes, "%qE attribute ignored", name);
6678 *no_add_attrs = true;
6681 return NULL_TREE;
6684 /* Handle a "gnu_inline" attribute; arguments as in
6685 struct attribute_spec.handler. */
6687 static tree
6688 handle_gnu_inline_attribute (tree *node, tree name,
6689 tree ARG_UNUSED (args),
6690 int ARG_UNUSED (flags),
6691 bool *no_add_attrs)
6693 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6695 /* Do nothing else, just set the attribute. We'll get at
6696 it later with lookup_attribute. */
6698 else
6700 warning (OPT_Wattributes, "%qE attribute ignored", name);
6701 *no_add_attrs = true;
6704 return NULL_TREE;
6707 /* Handle a "leaf" attribute; arguments as in
6708 struct attribute_spec.handler. */
6710 static tree
6711 handle_leaf_attribute (tree *node, tree name,
6712 tree ARG_UNUSED (args),
6713 int ARG_UNUSED (flags), bool *no_add_attrs)
6715 if (TREE_CODE (*node) != FUNCTION_DECL)
6717 warning (OPT_Wattributes, "%qE attribute ignored", name);
6718 *no_add_attrs = true;
6720 if (!TREE_PUBLIC (*node))
6722 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6723 *no_add_attrs = true;
6726 return NULL_TREE;
6729 /* Handle an "artificial" attribute; arguments as in
6730 struct attribute_spec.handler. */
6732 static tree
6733 handle_artificial_attribute (tree *node, tree name,
6734 tree ARG_UNUSED (args),
6735 int ARG_UNUSED (flags),
6736 bool *no_add_attrs)
6738 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6740 /* Do nothing else, just set the attribute. We'll get at
6741 it later with lookup_attribute. */
6743 else
6745 warning (OPT_Wattributes, "%qE attribute ignored", name);
6746 *no_add_attrs = true;
6749 return NULL_TREE;
6752 /* Handle a "flatten" attribute; arguments as in
6753 struct attribute_spec.handler. */
6755 static tree
6756 handle_flatten_attribute (tree *node, tree name,
6757 tree args ATTRIBUTE_UNUSED,
6758 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6760 if (TREE_CODE (*node) == FUNCTION_DECL)
6761 /* Do nothing else, just set the attribute. We'll get at
6762 it later with lookup_attribute. */
6764 else
6766 warning (OPT_Wattributes, "%qE attribute ignored", name);
6767 *no_add_attrs = true;
6770 return NULL_TREE;
6773 /* Handle a "warning" or "error" attribute; arguments as in
6774 struct attribute_spec.handler. */
6776 static tree
6777 handle_error_attribute (tree *node, tree name, tree args,
6778 int ARG_UNUSED (flags), bool *no_add_attrs)
6780 if (TREE_CODE (*node) == FUNCTION_DECL
6781 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6782 /* Do nothing else, just set the attribute. We'll get at
6783 it later with lookup_attribute. */
6785 else
6787 warning (OPT_Wattributes, "%qE attribute ignored", name);
6788 *no_add_attrs = true;
6791 return NULL_TREE;
6794 /* Handle a "used" attribute; arguments as in
6795 struct attribute_spec.handler. */
6797 static tree
6798 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6799 int ARG_UNUSED (flags), bool *no_add_attrs)
6801 tree node = *pnode;
6803 if (TREE_CODE (node) == FUNCTION_DECL
6804 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6805 || (TREE_CODE (node) == TYPE_DECL))
6807 TREE_USED (node) = 1;
6808 DECL_PRESERVE_P (node) = 1;
6809 if (TREE_CODE (node) == VAR_DECL)
6810 DECL_READ_P (node) = 1;
6812 else
6814 warning (OPT_Wattributes, "%qE attribute ignored", name);
6815 *no_add_attrs = true;
6818 return NULL_TREE;
6821 /* Handle a "unused" attribute; arguments as in
6822 struct attribute_spec.handler. */
6824 static tree
6825 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6826 int flags, bool *no_add_attrs)
6828 if (DECL_P (*node))
6830 tree decl = *node;
6832 if (TREE_CODE (decl) == PARM_DECL
6833 || TREE_CODE (decl) == VAR_DECL
6834 || TREE_CODE (decl) == FUNCTION_DECL
6835 || TREE_CODE (decl) == LABEL_DECL
6836 || TREE_CODE (decl) == TYPE_DECL)
6838 TREE_USED (decl) = 1;
6839 if (TREE_CODE (decl) == VAR_DECL
6840 || TREE_CODE (decl) == PARM_DECL)
6841 DECL_READ_P (decl) = 1;
6843 else
6845 warning (OPT_Wattributes, "%qE attribute ignored", name);
6846 *no_add_attrs = true;
6849 else
6851 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6852 *node = build_variant_type_copy (*node);
6853 TREE_USED (*node) = 1;
6856 return NULL_TREE;
6859 /* Handle a "externally_visible" attribute; arguments as in
6860 struct attribute_spec.handler. */
6862 static tree
6863 handle_externally_visible_attribute (tree *pnode, tree name,
6864 tree ARG_UNUSED (args),
6865 int ARG_UNUSED (flags),
6866 bool *no_add_attrs)
6868 tree node = *pnode;
6870 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6872 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6873 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6875 warning (OPT_Wattributes,
6876 "%qE attribute have effect only on public objects", name);
6877 *no_add_attrs = true;
6880 else
6882 warning (OPT_Wattributes, "%qE attribute ignored", name);
6883 *no_add_attrs = true;
6886 return NULL_TREE;
6889 /* Handle a "const" attribute; arguments as in
6890 struct attribute_spec.handler. */
6892 static tree
6893 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6894 int ARG_UNUSED (flags), bool *no_add_attrs)
6896 tree type = TREE_TYPE (*node);
6898 /* See FIXME comment on noreturn in c_common_attribute_table. */
6899 if (TREE_CODE (*node) == FUNCTION_DECL)
6900 TREE_READONLY (*node) = 1;
6901 else if (TREE_CODE (type) == POINTER_TYPE
6902 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6903 TREE_TYPE (*node)
6904 = build_pointer_type
6905 (build_type_variant (TREE_TYPE (type), 1,
6906 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6907 else
6909 warning (OPT_Wattributes, "%qE attribute ignored", name);
6910 *no_add_attrs = true;
6913 return NULL_TREE;
6916 /* Handle a "transparent_union" attribute; arguments as in
6917 struct attribute_spec.handler. */
6919 static tree
6920 handle_transparent_union_attribute (tree *node, tree name,
6921 tree ARG_UNUSED (args), int flags,
6922 bool *no_add_attrs)
6924 tree type;
6926 *no_add_attrs = true;
6929 if (TREE_CODE (*node) == TYPE_DECL
6930 && ! (flags & ATTR_FLAG_CXX11))
6931 node = &TREE_TYPE (*node);
6932 type = *node;
6934 if (TREE_CODE (type) == UNION_TYPE)
6936 /* Make sure that the first field will work for a transparent union.
6937 If the type isn't complete yet, leave the check to the code in
6938 finish_struct. */
6939 if (TYPE_SIZE (type))
6941 tree first = first_field (type);
6942 if (first == NULL_TREE
6943 || DECL_ARTIFICIAL (first)
6944 || TYPE_MODE (type) != DECL_MODE (first))
6945 goto ignored;
6948 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6950 /* If the type isn't complete yet, setting the flag
6951 on a variant wouldn't ever be checked. */
6952 if (!TYPE_SIZE (type))
6953 goto ignored;
6955 /* build_duplicate_type doesn't work for C++. */
6956 if (c_dialect_cxx ())
6957 goto ignored;
6959 /* A type variant isn't good enough, since we don't a cast
6960 to such a type removed as a no-op. */
6961 *node = type = build_duplicate_type (type);
6964 TYPE_TRANSPARENT_AGGR (type) = 1;
6965 return NULL_TREE;
6968 ignored:
6969 warning (OPT_Wattributes, "%qE attribute ignored", name);
6970 return NULL_TREE;
6973 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6974 get the requested priority for a constructor or destructor,
6975 possibly issuing diagnostics for invalid or reserved
6976 priorities. */
6978 static priority_type
6979 get_priority (tree args, bool is_destructor)
6981 HOST_WIDE_INT pri;
6982 tree arg;
6984 if (!args)
6985 return DEFAULT_INIT_PRIORITY;
6987 if (!SUPPORTS_INIT_PRIORITY)
6989 if (is_destructor)
6990 error ("destructor priorities are not supported");
6991 else
6992 error ("constructor priorities are not supported");
6993 return DEFAULT_INIT_PRIORITY;
6996 arg = TREE_VALUE (args);
6997 arg = default_conversion (arg);
6998 if (!host_integerp (arg, /*pos=*/0)
6999 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7000 goto invalid;
7002 pri = tree_low_cst (arg, /*pos=*/0);
7003 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7004 goto invalid;
7006 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7008 if (is_destructor)
7009 warning (0,
7010 "destructor priorities from 0 to %d are reserved "
7011 "for the implementation",
7012 MAX_RESERVED_INIT_PRIORITY);
7013 else
7014 warning (0,
7015 "constructor priorities from 0 to %d are reserved "
7016 "for the implementation",
7017 MAX_RESERVED_INIT_PRIORITY);
7019 return pri;
7021 invalid:
7022 if (is_destructor)
7023 error ("destructor priorities must be integers from 0 to %d inclusive",
7024 MAX_INIT_PRIORITY);
7025 else
7026 error ("constructor priorities must be integers from 0 to %d inclusive",
7027 MAX_INIT_PRIORITY);
7028 return DEFAULT_INIT_PRIORITY;
7031 /* Handle a "constructor" attribute; arguments as in
7032 struct attribute_spec.handler. */
7034 static tree
7035 handle_constructor_attribute (tree *node, tree name, tree args,
7036 int ARG_UNUSED (flags),
7037 bool *no_add_attrs)
7039 tree decl = *node;
7040 tree type = TREE_TYPE (decl);
7042 if (TREE_CODE (decl) == FUNCTION_DECL
7043 && TREE_CODE (type) == FUNCTION_TYPE
7044 && decl_function_context (decl) == 0)
7046 priority_type priority;
7047 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7048 priority = get_priority (args, /*is_destructor=*/false);
7049 SET_DECL_INIT_PRIORITY (decl, priority);
7050 TREE_USED (decl) = 1;
7052 else
7054 warning (OPT_Wattributes, "%qE attribute ignored", name);
7055 *no_add_attrs = true;
7058 return NULL_TREE;
7061 /* Handle a "destructor" attribute; arguments as in
7062 struct attribute_spec.handler. */
7064 static tree
7065 handle_destructor_attribute (tree *node, tree name, tree args,
7066 int ARG_UNUSED (flags),
7067 bool *no_add_attrs)
7069 tree decl = *node;
7070 tree type = TREE_TYPE (decl);
7072 if (TREE_CODE (decl) == FUNCTION_DECL
7073 && TREE_CODE (type) == FUNCTION_TYPE
7074 && decl_function_context (decl) == 0)
7076 priority_type priority;
7077 DECL_STATIC_DESTRUCTOR (decl) = 1;
7078 priority = get_priority (args, /*is_destructor=*/true);
7079 SET_DECL_FINI_PRIORITY (decl, priority);
7080 TREE_USED (decl) = 1;
7082 else
7084 warning (OPT_Wattributes, "%qE attribute ignored", name);
7085 *no_add_attrs = true;
7088 return NULL_TREE;
7091 /* Nonzero if the mode is a valid vector mode for this architecture.
7092 This returns nonzero even if there is no hardware support for the
7093 vector mode, but we can emulate with narrower modes. */
7095 static int
7096 vector_mode_valid_p (enum machine_mode mode)
7098 enum mode_class mclass = GET_MODE_CLASS (mode);
7099 enum machine_mode innermode;
7101 /* Doh! What's going on? */
7102 if (mclass != MODE_VECTOR_INT
7103 && mclass != MODE_VECTOR_FLOAT
7104 && mclass != MODE_VECTOR_FRACT
7105 && mclass != MODE_VECTOR_UFRACT
7106 && mclass != MODE_VECTOR_ACCUM
7107 && mclass != MODE_VECTOR_UACCUM)
7108 return 0;
7110 /* Hardware support. Woo hoo! */
7111 if (targetm.vector_mode_supported_p (mode))
7112 return 1;
7114 innermode = GET_MODE_INNER (mode);
7116 /* We should probably return 1 if requesting V4DI and we have no DI,
7117 but we have V2DI, but this is probably very unlikely. */
7119 /* If we have support for the inner mode, we can safely emulate it.
7120 We may not have V2DI, but me can emulate with a pair of DIs. */
7121 return targetm.scalar_mode_supported_p (innermode);
7125 /* Handle a "mode" attribute; arguments as in
7126 struct attribute_spec.handler. */
7128 static tree
7129 handle_mode_attribute (tree *node, tree name, tree args,
7130 int ARG_UNUSED (flags), bool *no_add_attrs)
7132 tree type = *node;
7133 tree ident = TREE_VALUE (args);
7135 *no_add_attrs = true;
7137 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7138 warning (OPT_Wattributes, "%qE attribute ignored", name);
7139 else
7141 int j;
7142 const char *p = IDENTIFIER_POINTER (ident);
7143 int len = strlen (p);
7144 enum machine_mode mode = VOIDmode;
7145 tree typefm;
7146 bool valid_mode;
7148 if (len > 4 && p[0] == '_' && p[1] == '_'
7149 && p[len - 1] == '_' && p[len - 2] == '_')
7151 char *newp = (char *) alloca (len - 1);
7153 strcpy (newp, &p[2]);
7154 newp[len - 4] = '\0';
7155 p = newp;
7158 /* Change this type to have a type with the specified mode.
7159 First check for the special modes. */
7160 if (!strcmp (p, "byte"))
7161 mode = byte_mode;
7162 else if (!strcmp (p, "word"))
7163 mode = word_mode;
7164 else if (!strcmp (p, "pointer"))
7165 mode = ptr_mode;
7166 else if (!strcmp (p, "libgcc_cmp_return"))
7167 mode = targetm.libgcc_cmp_return_mode ();
7168 else if (!strcmp (p, "libgcc_shift_count"))
7169 mode = targetm.libgcc_shift_count_mode ();
7170 else if (!strcmp (p, "unwind_word"))
7171 mode = targetm.unwind_word_mode ();
7172 else
7173 for (j = 0; j < NUM_MACHINE_MODES; j++)
7174 if (!strcmp (p, GET_MODE_NAME (j)))
7176 mode = (enum machine_mode) j;
7177 break;
7180 if (mode == VOIDmode)
7182 error ("unknown machine mode %qE", ident);
7183 return NULL_TREE;
7186 valid_mode = false;
7187 switch (GET_MODE_CLASS (mode))
7189 case MODE_INT:
7190 case MODE_PARTIAL_INT:
7191 case MODE_FLOAT:
7192 case MODE_DECIMAL_FLOAT:
7193 case MODE_FRACT:
7194 case MODE_UFRACT:
7195 case MODE_ACCUM:
7196 case MODE_UACCUM:
7197 valid_mode = targetm.scalar_mode_supported_p (mode);
7198 break;
7200 case MODE_COMPLEX_INT:
7201 case MODE_COMPLEX_FLOAT:
7202 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7203 break;
7205 case MODE_VECTOR_INT:
7206 case MODE_VECTOR_FLOAT:
7207 case MODE_VECTOR_FRACT:
7208 case MODE_VECTOR_UFRACT:
7209 case MODE_VECTOR_ACCUM:
7210 case MODE_VECTOR_UACCUM:
7211 warning (OPT_Wattributes, "specifying vector types with "
7212 "__attribute__ ((mode)) is deprecated");
7213 warning (OPT_Wattributes,
7214 "use __attribute__ ((vector_size)) instead");
7215 valid_mode = vector_mode_valid_p (mode);
7216 break;
7218 default:
7219 break;
7221 if (!valid_mode)
7223 error ("unable to emulate %qs", p);
7224 return NULL_TREE;
7227 if (POINTER_TYPE_P (type))
7229 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7230 tree (*fn)(tree, enum machine_mode, bool);
7232 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7234 error ("invalid pointer mode %qs", p);
7235 return NULL_TREE;
7238 if (TREE_CODE (type) == POINTER_TYPE)
7239 fn = build_pointer_type_for_mode;
7240 else
7241 fn = build_reference_type_for_mode;
7242 typefm = fn (TREE_TYPE (type), mode, false);
7244 else
7246 /* For fixed-point modes, we need to test if the signness of type
7247 and the machine mode are consistent. */
7248 if (ALL_FIXED_POINT_MODE_P (mode)
7249 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7251 error ("signedness of type and machine mode %qs don%'t match", p);
7252 return NULL_TREE;
7254 /* For fixed-point modes, we need to pass saturating info. */
7255 typefm = lang_hooks.types.type_for_mode (mode,
7256 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7257 : TYPE_UNSIGNED (type));
7260 if (typefm == NULL_TREE)
7262 error ("no data type for mode %qs", p);
7263 return NULL_TREE;
7265 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7267 /* For enumeral types, copy the precision from the integer
7268 type returned above. If not an INTEGER_TYPE, we can't use
7269 this mode for this type. */
7270 if (TREE_CODE (typefm) != INTEGER_TYPE)
7272 error ("cannot use mode %qs for enumeral types", p);
7273 return NULL_TREE;
7276 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7278 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7279 typefm = type;
7281 else
7283 /* We cannot build a type variant, as there's code that assumes
7284 that TYPE_MAIN_VARIANT has the same mode. This includes the
7285 debug generators. Instead, create a subrange type. This
7286 results in all of the enumeral values being emitted only once
7287 in the original, and the subtype gets them by reference. */
7288 if (TYPE_UNSIGNED (type))
7289 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7290 else
7291 typefm = make_signed_type (TYPE_PRECISION (typefm));
7292 TREE_TYPE (typefm) = type;
7295 else if (VECTOR_MODE_P (mode)
7296 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7297 : TREE_CODE (type) != TREE_CODE (typefm))
7299 error ("mode %qs applied to inappropriate type", p);
7300 return NULL_TREE;
7303 *node = typefm;
7306 return NULL_TREE;
7309 /* Handle a "section" attribute; arguments as in
7310 struct attribute_spec.handler. */
7312 static tree
7313 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7314 int ARG_UNUSED (flags), bool *no_add_attrs)
7316 tree decl = *node;
7318 if (targetm_common.have_named_sections)
7320 user_defined_section_attribute = true;
7322 if ((TREE_CODE (decl) == FUNCTION_DECL
7323 || TREE_CODE (decl) == VAR_DECL)
7324 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7326 if (TREE_CODE (decl) == VAR_DECL
7327 && current_function_decl != NULL_TREE
7328 && !TREE_STATIC (decl))
7330 error_at (DECL_SOURCE_LOCATION (decl),
7331 "section attribute cannot be specified for "
7332 "local variables");
7333 *no_add_attrs = true;
7336 /* The decl may have already been given a section attribute
7337 from a previous declaration. Ensure they match. */
7338 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7339 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7340 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7342 error ("section of %q+D conflicts with previous declaration",
7343 *node);
7344 *no_add_attrs = true;
7346 else if (TREE_CODE (decl) == VAR_DECL
7347 && !targetm.have_tls && targetm.emutls.tmpl_section
7348 && DECL_THREAD_LOCAL_P (decl))
7350 error ("section of %q+D cannot be overridden", *node);
7351 *no_add_attrs = true;
7353 else
7354 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7356 else
7358 error ("section attribute not allowed for %q+D", *node);
7359 *no_add_attrs = true;
7362 else
7364 error_at (DECL_SOURCE_LOCATION (*node),
7365 "section attributes are not supported for this target");
7366 *no_add_attrs = true;
7369 return NULL_TREE;
7372 /* Check whether ALIGN is a valid user-specified alignment. If so,
7373 return its base-2 log; if not, output an error and return -1. If
7374 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7375 no error. */
7377 check_user_alignment (const_tree align, bool allow_zero)
7379 int i;
7381 if (TREE_CODE (align) != INTEGER_CST
7382 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7384 error ("requested alignment is not an integer constant");
7385 return -1;
7387 else if (allow_zero && integer_zerop (align))
7388 return -1;
7389 else if (tree_int_cst_sgn (align) == -1
7390 || (i = tree_log2 (align)) == -1)
7392 error ("requested alignment is not a positive power of 2");
7393 return -1;
7395 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7397 error ("requested alignment is too large");
7398 return -1;
7400 return i;
7404 If in c++-11, check if the c++-11 alignment constraint with respect
7405 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7406 c++-11 mode, does nothing.
7408 [dcl.align]2/ says:
7410 [* if the constant expression evaluates to a fundamental alignment,
7411 the alignment requirement of the declared entity shall be the
7412 specified fundamental alignment.
7414 * if the constant expression evaluates to an extended alignment
7415 and the implementation supports that alignment in the context
7416 of the declaration, the alignment of the declared entity shall
7417 be that alignment
7419 * if the constant expression evaluates to an extended alignment
7420 and the implementation does not support that alignment in the
7421 context of the declaration, the program is ill-formed]. */
7423 static bool
7424 check_cxx_fundamental_alignment_constraints (tree node,
7425 unsigned align_log,
7426 int flags)
7428 bool alignment_too_large_p = false;
7429 unsigned requested_alignment = 1U << align_log;
7430 unsigned max_align = 0;
7432 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7433 || (node == NULL_TREE || node == error_mark_node))
7434 return true;
7436 if (cxx_fundamental_alignment_p (requested_alignment))
7437 return true;
7439 if (DECL_P (node))
7441 if (TREE_STATIC (node))
7443 /* For file scope variables and static members, the target
7444 supports alignments that are at most
7445 MAX_OFILE_ALIGNMENT. */
7446 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7447 alignment_too_large_p = true;
7449 else
7451 #ifdef BIGGEST_FIELD_ALIGNMENT
7452 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7453 #else
7454 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7455 #endif
7456 /* For non-static members, the target supports either
7457 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7458 if it is defined or BIGGEST_ALIGNMENT. */
7459 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7460 if (TREE_CODE (node) == FIELD_DECL
7461 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7462 alignment_too_large_p = true;
7463 #undef MAX_TARGET_FIELD_ALIGNMENT
7464 /* For stack variables, the target supports at most
7465 MAX_STACK_ALIGNMENT. */
7466 else if (decl_function_context (node) != NULL
7467 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7468 alignment_too_large_p = true;
7471 else if (TYPE_P (node))
7473 /* Let's be liberal for types. */
7474 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7475 alignment_too_large_p = true;
7478 if (alignment_too_large_p)
7479 pedwarn (input_location, OPT_Wattributes,
7480 "requested alignment %d is larger than %d",
7481 requested_alignment, max_align);
7483 return !alignment_too_large_p;
7486 /* Handle a "aligned" attribute; arguments as in
7487 struct attribute_spec.handler. */
7489 static tree
7490 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7491 int flags, bool *no_add_attrs)
7493 tree decl = NULL_TREE;
7494 tree *type = NULL;
7495 int is_type = 0;
7496 tree align_expr = (args ? TREE_VALUE (args)
7497 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7498 int i;
7500 if (DECL_P (*node))
7502 decl = *node;
7503 type = &TREE_TYPE (decl);
7504 is_type = TREE_CODE (*node) == TYPE_DECL;
7506 else if (TYPE_P (*node))
7507 type = node, is_type = 1;
7509 if ((i = check_user_alignment (align_expr, false)) == -1
7510 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7511 *no_add_attrs = true;
7512 else if (is_type)
7514 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7515 /* OK, modify the type in place. */;
7516 /* If we have a TYPE_DECL, then copy the type, so that we
7517 don't accidentally modify a builtin type. See pushdecl. */
7518 else if (decl && TREE_TYPE (decl) != error_mark_node
7519 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7521 tree tt = TREE_TYPE (decl);
7522 *type = build_variant_type_copy (*type);
7523 DECL_ORIGINAL_TYPE (decl) = tt;
7524 TYPE_NAME (*type) = decl;
7525 TREE_USED (*type) = TREE_USED (decl);
7526 TREE_TYPE (decl) = *type;
7528 else
7529 *type = build_variant_type_copy (*type);
7531 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7532 TYPE_USER_ALIGN (*type) = 1;
7534 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7535 && TREE_CODE (decl) != FIELD_DECL)
7537 error ("alignment may not be specified for %q+D", decl);
7538 *no_add_attrs = true;
7540 else if (DECL_USER_ALIGN (decl)
7541 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7542 /* C++-11 [dcl.align/4]:
7544 When multiple alignment-specifiers are specified for an
7545 entity, the alignment requirement shall be set to the
7546 strictest specified alignment.
7548 This formally comes from the c++11 specification but we are
7549 doing it for the GNU attribute syntax as well. */
7550 *no_add_attrs = true;
7551 else if (TREE_CODE (decl) == FUNCTION_DECL
7552 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7554 if (DECL_USER_ALIGN (decl))
7555 error ("alignment for %q+D was previously specified as %d "
7556 "and may not be decreased", decl,
7557 DECL_ALIGN (decl) / BITS_PER_UNIT);
7558 else
7559 error ("alignment for %q+D must be at least %d", decl,
7560 DECL_ALIGN (decl) / BITS_PER_UNIT);
7561 *no_add_attrs = true;
7563 else
7565 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7566 DECL_USER_ALIGN (decl) = 1;
7569 return NULL_TREE;
7572 /* Handle a "weak" attribute; arguments as in
7573 struct attribute_spec.handler. */
7575 static tree
7576 handle_weak_attribute (tree *node, tree name,
7577 tree ARG_UNUSED (args),
7578 int ARG_UNUSED (flags),
7579 bool * ARG_UNUSED (no_add_attrs))
7581 if (TREE_CODE (*node) == FUNCTION_DECL
7582 && DECL_DECLARED_INLINE_P (*node))
7584 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7585 *no_add_attrs = true;
7587 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7589 error ("indirect function %q+D cannot be declared weak", *node);
7590 *no_add_attrs = true;
7591 return NULL_TREE;
7593 else if (TREE_CODE (*node) == FUNCTION_DECL
7594 || TREE_CODE (*node) == VAR_DECL)
7595 declare_weak (*node);
7596 else
7597 warning (OPT_Wattributes, "%qE attribute ignored", name);
7599 return NULL_TREE;
7602 /* Handle an "alias" or "ifunc" attribute; arguments as in
7603 struct attribute_spec.handler, except that IS_ALIAS tells us
7604 whether this is an alias as opposed to ifunc attribute. */
7606 static tree
7607 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7608 bool *no_add_attrs)
7610 tree decl = *node;
7612 if (TREE_CODE (decl) != FUNCTION_DECL
7613 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7615 warning (OPT_Wattributes, "%qE attribute ignored", name);
7616 *no_add_attrs = true;
7618 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7619 || (TREE_CODE (decl) != FUNCTION_DECL
7620 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7621 /* A static variable declaration is always a tentative definition,
7622 but the alias is a non-tentative definition which overrides. */
7623 || (TREE_CODE (decl) != FUNCTION_DECL
7624 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7626 error ("%q+D defined both normally and as %qE attribute", decl, name);
7627 *no_add_attrs = true;
7628 return NULL_TREE;
7630 else if (!is_alias
7631 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7632 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7634 error ("weak %q+D cannot be defined %qE", decl, name);
7635 *no_add_attrs = true;
7636 return NULL_TREE;
7639 /* Note that the very first time we process a nested declaration,
7640 decl_function_context will not be set. Indeed, *would* never
7641 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7642 we do below. After such frobbery, pushdecl would set the context.
7643 In any case, this is never what we want. */
7644 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7646 tree id;
7648 id = TREE_VALUE (args);
7649 if (TREE_CODE (id) != STRING_CST)
7651 error ("attribute %qE argument not a string", name);
7652 *no_add_attrs = true;
7653 return NULL_TREE;
7655 id = get_identifier (TREE_STRING_POINTER (id));
7656 /* This counts as a use of the object pointed to. */
7657 TREE_USED (id) = 1;
7659 if (TREE_CODE (decl) == FUNCTION_DECL)
7660 DECL_INITIAL (decl) = error_mark_node;
7661 else
7662 TREE_STATIC (decl) = 1;
7664 if (!is_alias)
7665 /* ifuncs are also aliases, so set that attribute too. */
7666 DECL_ATTRIBUTES (decl)
7667 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7669 else
7671 warning (OPT_Wattributes, "%qE attribute ignored", name);
7672 *no_add_attrs = true;
7675 return NULL_TREE;
7678 /* Handle an "alias" or "ifunc" attribute; arguments as in
7679 struct attribute_spec.handler. */
7681 static tree
7682 handle_ifunc_attribute (tree *node, tree name, tree args,
7683 int ARG_UNUSED (flags), bool *no_add_attrs)
7685 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7688 /* Handle an "alias" or "ifunc" attribute; arguments as in
7689 struct attribute_spec.handler. */
7691 static tree
7692 handle_alias_attribute (tree *node, tree name, tree args,
7693 int ARG_UNUSED (flags), bool *no_add_attrs)
7695 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7698 /* Handle a "weakref" attribute; arguments as in struct
7699 attribute_spec.handler. */
7701 static tree
7702 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7703 int flags, bool *no_add_attrs)
7705 tree attr = NULL_TREE;
7707 /* We must ignore the attribute when it is associated with
7708 local-scoped decls, since attribute alias is ignored and many
7709 such symbols do not even have a DECL_WEAK field. */
7710 if (decl_function_context (*node)
7711 || current_function_decl
7712 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7714 warning (OPT_Wattributes, "%qE attribute ignored", name);
7715 *no_add_attrs = true;
7716 return NULL_TREE;
7719 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7721 error ("indirect function %q+D cannot be declared weakref", *node);
7722 *no_add_attrs = true;
7723 return NULL_TREE;
7726 /* The idea here is that `weakref("name")' mutates into `weakref,
7727 alias("name")', and weakref without arguments, in turn,
7728 implicitly adds weak. */
7730 if (args)
7732 attr = tree_cons (get_identifier ("alias"), args, attr);
7733 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7735 *no_add_attrs = true;
7737 decl_attributes (node, attr, flags);
7739 else
7741 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7742 error_at (DECL_SOURCE_LOCATION (*node),
7743 "weakref attribute must appear before alias attribute");
7745 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7746 and that isn't supported; and because it wants to add it to
7747 the list of weak decls, which isn't helpful. */
7748 DECL_WEAK (*node) = 1;
7751 return NULL_TREE;
7754 /* Handle an "visibility" attribute; arguments as in
7755 struct attribute_spec.handler. */
7757 static tree
7758 handle_visibility_attribute (tree *node, tree name, tree args,
7759 int ARG_UNUSED (flags),
7760 bool *ARG_UNUSED (no_add_attrs))
7762 tree decl = *node;
7763 tree id = TREE_VALUE (args);
7764 enum symbol_visibility vis;
7766 if (TYPE_P (*node))
7768 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7769 /* OK */;
7770 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7772 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7773 name);
7774 return NULL_TREE;
7776 else if (TYPE_FIELDS (*node))
7778 error ("%qE attribute ignored because %qT is already defined",
7779 name, *node);
7780 return NULL_TREE;
7783 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7785 warning (OPT_Wattributes, "%qE attribute ignored", name);
7786 return NULL_TREE;
7789 if (TREE_CODE (id) != STRING_CST)
7791 error ("visibility argument not a string");
7792 return NULL_TREE;
7795 /* If this is a type, set the visibility on the type decl. */
7796 if (TYPE_P (decl))
7798 decl = TYPE_NAME (decl);
7799 if (!decl)
7800 return NULL_TREE;
7801 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7803 warning (OPT_Wattributes, "%qE attribute ignored on types",
7804 name);
7805 return NULL_TREE;
7809 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7810 vis = VISIBILITY_DEFAULT;
7811 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7812 vis = VISIBILITY_INTERNAL;
7813 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7814 vis = VISIBILITY_HIDDEN;
7815 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7816 vis = VISIBILITY_PROTECTED;
7817 else
7819 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7820 vis = VISIBILITY_DEFAULT;
7823 if (DECL_VISIBILITY_SPECIFIED (decl)
7824 && vis != DECL_VISIBILITY (decl))
7826 tree attributes = (TYPE_P (*node)
7827 ? TYPE_ATTRIBUTES (*node)
7828 : DECL_ATTRIBUTES (decl));
7829 if (lookup_attribute ("visibility", attributes))
7830 error ("%qD redeclared with different visibility", decl);
7831 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7832 && lookup_attribute ("dllimport", attributes))
7833 error ("%qD was declared %qs which implies default visibility",
7834 decl, "dllimport");
7835 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7836 && lookup_attribute ("dllexport", attributes))
7837 error ("%qD was declared %qs which implies default visibility",
7838 decl, "dllexport");
7841 DECL_VISIBILITY (decl) = vis;
7842 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7844 /* Go ahead and attach the attribute to the node as well. This is needed
7845 so we can determine whether we have VISIBILITY_DEFAULT because the
7846 visibility was not specified, or because it was explicitly overridden
7847 from the containing scope. */
7849 return NULL_TREE;
7852 /* Determine the ELF symbol visibility for DECL, which is either a
7853 variable or a function. It is an error to use this function if a
7854 definition of DECL is not available in this translation unit.
7855 Returns true if the final visibility has been determined by this
7856 function; false if the caller is free to make additional
7857 modifications. */
7859 bool
7860 c_determine_visibility (tree decl)
7862 gcc_assert (TREE_CODE (decl) == VAR_DECL
7863 || TREE_CODE (decl) == FUNCTION_DECL);
7865 /* If the user explicitly specified the visibility with an
7866 attribute, honor that. DECL_VISIBILITY will have been set during
7867 the processing of the attribute. We check for an explicit
7868 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7869 to distinguish the use of an attribute from the use of a "#pragma
7870 GCC visibility push(...)"; in the latter case we still want other
7871 considerations to be able to overrule the #pragma. */
7872 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7873 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7874 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7875 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7876 return true;
7878 /* Set default visibility to whatever the user supplied with
7879 visibility_specified depending on #pragma GCC visibility. */
7880 if (!DECL_VISIBILITY_SPECIFIED (decl))
7882 if (visibility_options.inpragma
7883 || DECL_VISIBILITY (decl) != default_visibility)
7885 DECL_VISIBILITY (decl) = default_visibility;
7886 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7887 /* If visibility changed and DECL already has DECL_RTL, ensure
7888 symbol flags are updated. */
7889 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7890 || TREE_CODE (decl) == FUNCTION_DECL)
7891 && DECL_RTL_SET_P (decl))
7892 make_decl_rtl (decl);
7895 return false;
7898 /* Handle an "tls_model" attribute; arguments as in
7899 struct attribute_spec.handler. */
7901 static tree
7902 handle_tls_model_attribute (tree *node, tree name, tree args,
7903 int ARG_UNUSED (flags), bool *no_add_attrs)
7905 tree id;
7906 tree decl = *node;
7907 enum tls_model kind;
7909 *no_add_attrs = true;
7911 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7913 warning (OPT_Wattributes, "%qE attribute ignored", name);
7914 return NULL_TREE;
7917 kind = DECL_TLS_MODEL (decl);
7918 id = TREE_VALUE (args);
7919 if (TREE_CODE (id) != STRING_CST)
7921 error ("tls_model argument not a string");
7922 return NULL_TREE;
7925 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7926 kind = TLS_MODEL_LOCAL_EXEC;
7927 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7928 kind = TLS_MODEL_INITIAL_EXEC;
7929 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7930 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7931 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7932 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7933 else
7934 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7936 DECL_TLS_MODEL (decl) = kind;
7937 return NULL_TREE;
7940 /* Handle a "no_instrument_function" attribute; arguments as in
7941 struct attribute_spec.handler. */
7943 static tree
7944 handle_no_instrument_function_attribute (tree *node, tree name,
7945 tree ARG_UNUSED (args),
7946 int ARG_UNUSED (flags),
7947 bool *no_add_attrs)
7949 tree decl = *node;
7951 if (TREE_CODE (decl) != FUNCTION_DECL)
7953 error_at (DECL_SOURCE_LOCATION (decl),
7954 "%qE attribute applies only to functions", name);
7955 *no_add_attrs = true;
7957 else if (DECL_INITIAL (decl))
7959 error_at (DECL_SOURCE_LOCATION (decl),
7960 "can%'t set %qE attribute after definition", name);
7961 *no_add_attrs = true;
7963 else
7964 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7966 return NULL_TREE;
7969 /* Handle a "malloc" attribute; arguments as in
7970 struct attribute_spec.handler. */
7972 static tree
7973 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7974 int ARG_UNUSED (flags), bool *no_add_attrs)
7976 if (TREE_CODE (*node) == FUNCTION_DECL
7977 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7978 DECL_IS_MALLOC (*node) = 1;
7979 else
7981 warning (OPT_Wattributes, "%qE attribute ignored", name);
7982 *no_add_attrs = true;
7985 return NULL_TREE;
7988 /* Handle a "alloc_size" attribute; arguments as in
7989 struct attribute_spec.handler. */
7991 static tree
7992 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7993 int ARG_UNUSED (flags), bool *no_add_attrs)
7995 unsigned arg_count = type_num_arguments (*node);
7996 for (; args; args = TREE_CHAIN (args))
7998 tree position = TREE_VALUE (args);
8000 if (TREE_CODE (position) != INTEGER_CST
8001 || TREE_INT_CST_HIGH (position)
8002 || TREE_INT_CST_LOW (position) < 1
8003 || TREE_INT_CST_LOW (position) > arg_count )
8005 warning (OPT_Wattributes,
8006 "alloc_size parameter outside range");
8007 *no_add_attrs = true;
8008 return NULL_TREE;
8011 return NULL_TREE;
8014 /* Handle a "fn spec" attribute; arguments as in
8015 struct attribute_spec.handler. */
8017 static tree
8018 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8019 tree args, int ARG_UNUSED (flags),
8020 bool *no_add_attrs ATTRIBUTE_UNUSED)
8022 gcc_assert (args
8023 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8024 && !TREE_CHAIN (args));
8025 return NULL_TREE;
8028 /* Handle a "bnd_variable_size" attribute; arguments as in
8029 struct attribute_spec.handler. */
8031 static tree
8032 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8033 int ARG_UNUSED (flags), bool *no_add_attrs)
8035 if (TREE_CODE (*node) != FIELD_DECL)
8037 warning (OPT_Wattributes, "%qE attribute ignored", name);
8038 *no_add_attrs = true;
8041 return NULL_TREE;
8044 /* Handle a "bnd_legacy" attribute; arguments as in
8045 struct attribute_spec.handler. */
8047 static tree
8048 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8049 int ARG_UNUSED (flags), bool *no_add_attrs)
8051 if (TREE_CODE (*node) != FUNCTION_DECL)
8053 warning (OPT_Wattributes, "%qE attribute ignored", name);
8054 *no_add_attrs = true;
8057 return NULL_TREE;
8060 /* Handle a "warn_unused" attribute; arguments as in
8061 struct attribute_spec.handler. */
8063 static tree
8064 handle_warn_unused_attribute (tree *node, tree name,
8065 tree args ATTRIBUTE_UNUSED,
8066 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8068 if (TYPE_P (*node))
8069 /* Do nothing else, just set the attribute. We'll get at
8070 it later with lookup_attribute. */
8072 else
8074 warning (OPT_Wattributes, "%qE attribute ignored", name);
8075 *no_add_attrs = true;
8078 return NULL_TREE;
8081 /* Handle an "omp declare simd" attribute; arguments as in
8082 struct attribute_spec.handler. */
8084 static tree
8085 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8087 return NULL_TREE;
8090 /* Handle an "omp declare target" attribute; arguments as in
8091 struct attribute_spec.handler. */
8093 static tree
8094 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8096 return NULL_TREE;
8099 /* Handle a "returns_twice" attribute; arguments as in
8100 struct attribute_spec.handler. */
8102 static tree
8103 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8104 int ARG_UNUSED (flags), bool *no_add_attrs)
8106 if (TREE_CODE (*node) == FUNCTION_DECL)
8107 DECL_IS_RETURNS_TWICE (*node) = 1;
8108 else
8110 warning (OPT_Wattributes, "%qE attribute ignored", name);
8111 *no_add_attrs = true;
8114 return NULL_TREE;
8117 /* Handle a "no_limit_stack" attribute; arguments as in
8118 struct attribute_spec.handler. */
8120 static tree
8121 handle_no_limit_stack_attribute (tree *node, tree name,
8122 tree ARG_UNUSED (args),
8123 int ARG_UNUSED (flags),
8124 bool *no_add_attrs)
8126 tree decl = *node;
8128 if (TREE_CODE (decl) != FUNCTION_DECL)
8130 error_at (DECL_SOURCE_LOCATION (decl),
8131 "%qE attribute applies only to functions", name);
8132 *no_add_attrs = true;
8134 else if (DECL_INITIAL (decl))
8136 error_at (DECL_SOURCE_LOCATION (decl),
8137 "can%'t set %qE attribute after definition", name);
8138 *no_add_attrs = true;
8140 else
8141 DECL_NO_LIMIT_STACK (decl) = 1;
8143 return NULL_TREE;
8146 /* Handle a "pure" attribute; arguments as in
8147 struct attribute_spec.handler. */
8149 static tree
8150 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8151 int ARG_UNUSED (flags), bool *no_add_attrs)
8153 if (TREE_CODE (*node) == FUNCTION_DECL)
8154 DECL_PURE_P (*node) = 1;
8155 /* ??? TODO: Support types. */
8156 else
8158 warning (OPT_Wattributes, "%qE attribute ignored", name);
8159 *no_add_attrs = true;
8162 return NULL_TREE;
8165 /* Digest an attribute list destined for a transactional memory statement.
8166 ALLOWED is the set of attributes that are allowed for this statement;
8167 return the attribute we parsed. Multiple attributes are never allowed. */
8170 parse_tm_stmt_attr (tree attrs, int allowed)
8172 tree a_seen = NULL;
8173 int m_seen = 0;
8175 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8177 tree a = TREE_PURPOSE (attrs);
8178 int m = 0;
8180 if (is_attribute_p ("outer", a))
8181 m = TM_STMT_ATTR_OUTER;
8183 if ((m & allowed) == 0)
8185 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8186 continue;
8189 if (m_seen == 0)
8191 a_seen = a;
8192 m_seen = m;
8194 else if (m_seen == m)
8195 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8196 else
8197 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8200 return m_seen;
8203 /* Transform a TM attribute name into a maskable integer and back.
8204 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8205 to how the lack of an attribute is treated. */
8208 tm_attr_to_mask (tree attr)
8210 if (attr == NULL)
8211 return 0;
8212 if (is_attribute_p ("transaction_safe", attr))
8213 return TM_ATTR_SAFE;
8214 if (is_attribute_p ("transaction_callable", attr))
8215 return TM_ATTR_CALLABLE;
8216 if (is_attribute_p ("transaction_pure", attr))
8217 return TM_ATTR_PURE;
8218 if (is_attribute_p ("transaction_unsafe", attr))
8219 return TM_ATTR_IRREVOCABLE;
8220 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8221 return TM_ATTR_MAY_CANCEL_OUTER;
8222 return 0;
8225 tree
8226 tm_mask_to_attr (int mask)
8228 const char *str;
8229 switch (mask)
8231 case TM_ATTR_SAFE:
8232 str = "transaction_safe";
8233 break;
8234 case TM_ATTR_CALLABLE:
8235 str = "transaction_callable";
8236 break;
8237 case TM_ATTR_PURE:
8238 str = "transaction_pure";
8239 break;
8240 case TM_ATTR_IRREVOCABLE:
8241 str = "transaction_unsafe";
8242 break;
8243 case TM_ATTR_MAY_CANCEL_OUTER:
8244 str = "transaction_may_cancel_outer";
8245 break;
8246 default:
8247 gcc_unreachable ();
8249 return get_identifier (str);
8252 /* Return the first TM attribute seen in LIST. */
8254 tree
8255 find_tm_attribute (tree list)
8257 for (; list ; list = TREE_CHAIN (list))
8259 tree name = TREE_PURPOSE (list);
8260 if (tm_attr_to_mask (name) != 0)
8261 return name;
8263 return NULL_TREE;
8266 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8267 Here we accept only function types, and verify that none of the other
8268 function TM attributes are also applied. */
8269 /* ??? We need to accept class types for C++, but not C. This greatly
8270 complicates this function, since we can no longer rely on the extra
8271 processing given by function_type_required. */
8273 static tree
8274 handle_tm_attribute (tree *node, tree name, tree args,
8275 int flags, bool *no_add_attrs)
8277 /* Only one path adds the attribute; others don't. */
8278 *no_add_attrs = true;
8280 switch (TREE_CODE (*node))
8282 case RECORD_TYPE:
8283 case UNION_TYPE:
8284 /* Only tm_callable and tm_safe apply to classes. */
8285 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8286 goto ignored;
8287 /* FALLTHRU */
8289 case FUNCTION_TYPE:
8290 case METHOD_TYPE:
8292 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8293 if (old_name == name)
8295 else if (old_name != NULL_TREE)
8296 error ("type was previously declared %qE", old_name);
8297 else
8298 *no_add_attrs = false;
8300 break;
8302 case POINTER_TYPE:
8304 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8305 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8307 tree fn_tmp = TREE_TYPE (*node);
8308 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8309 *node = build_pointer_type (fn_tmp);
8310 break;
8313 /* FALLTHRU */
8315 default:
8316 /* If a function is next, pass it on to be tried next. */
8317 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8318 return tree_cons (name, args, NULL);
8320 ignored:
8321 warning (OPT_Wattributes, "%qE attribute ignored", name);
8322 break;
8325 return NULL_TREE;
8328 /* Handle the TM_WRAP attribute; arguments as in
8329 struct attribute_spec.handler. */
8331 static tree
8332 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8333 int ARG_UNUSED (flags), bool *no_add_attrs)
8335 tree decl = *node;
8337 /* We don't need the attribute even on success, since we
8338 record the entry in an external table. */
8339 *no_add_attrs = true;
8341 if (TREE_CODE (decl) != FUNCTION_DECL)
8342 warning (OPT_Wattributes, "%qE attribute ignored", name);
8343 else
8345 tree wrap_decl = TREE_VALUE (args);
8346 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8347 && TREE_CODE (wrap_decl) != VAR_DECL
8348 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8349 error ("%qE argument not an identifier", name);
8350 else
8352 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8353 wrap_decl = lookup_name (wrap_decl);
8354 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8356 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8357 TREE_TYPE (wrap_decl)))
8358 record_tm_replacement (wrap_decl, decl);
8359 else
8360 error ("%qD is not compatible with %qD", wrap_decl, decl);
8362 else
8363 error ("transaction_wrap argument is not a function");
8367 return NULL_TREE;
8370 /* Ignore the given attribute. Used when this attribute may be usefully
8371 overridden by the target, but is not used generically. */
8373 static tree
8374 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8375 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8376 bool *no_add_attrs)
8378 *no_add_attrs = true;
8379 return NULL_TREE;
8382 /* Handle a "no vops" attribute; arguments as in
8383 struct attribute_spec.handler. */
8385 static tree
8386 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8387 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8388 bool *ARG_UNUSED (no_add_attrs))
8390 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8391 DECL_IS_NOVOPS (*node) = 1;
8392 return NULL_TREE;
8395 /* Handle a "deprecated" attribute; arguments as in
8396 struct attribute_spec.handler. */
8398 static tree
8399 handle_deprecated_attribute (tree *node, tree name,
8400 tree args, int flags,
8401 bool *no_add_attrs)
8403 tree type = NULL_TREE;
8404 int warn = 0;
8405 tree what = NULL_TREE;
8407 if (!args)
8408 *no_add_attrs = true;
8409 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8411 error ("deprecated message is not a string");
8412 *no_add_attrs = true;
8415 if (DECL_P (*node))
8417 tree decl = *node;
8418 type = TREE_TYPE (decl);
8420 if (TREE_CODE (decl) == TYPE_DECL
8421 || TREE_CODE (decl) == PARM_DECL
8422 || TREE_CODE (decl) == VAR_DECL
8423 || TREE_CODE (decl) == FUNCTION_DECL
8424 || TREE_CODE (decl) == FIELD_DECL
8425 || objc_method_decl (TREE_CODE (decl)))
8426 TREE_DEPRECATED (decl) = 1;
8427 else
8428 warn = 1;
8430 else if (TYPE_P (*node))
8432 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8433 *node = build_variant_type_copy (*node);
8434 TREE_DEPRECATED (*node) = 1;
8435 type = *node;
8437 else
8438 warn = 1;
8440 if (warn)
8442 *no_add_attrs = true;
8443 if (type && TYPE_NAME (type))
8445 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8446 what = TYPE_NAME (*node);
8447 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8448 && DECL_NAME (TYPE_NAME (type)))
8449 what = DECL_NAME (TYPE_NAME (type));
8451 if (what)
8452 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8453 else
8454 warning (OPT_Wattributes, "%qE attribute ignored", name);
8457 return NULL_TREE;
8460 /* Handle a "vector_size" attribute; arguments as in
8461 struct attribute_spec.handler. */
8463 static tree
8464 handle_vector_size_attribute (tree *node, tree name, tree args,
8465 int ARG_UNUSED (flags),
8466 bool *no_add_attrs)
8468 unsigned HOST_WIDE_INT vecsize, nunits;
8469 enum machine_mode orig_mode;
8470 tree type = *node, new_type, size;
8472 *no_add_attrs = true;
8474 size = TREE_VALUE (args);
8476 if (!host_integerp (size, 1))
8478 warning (OPT_Wattributes, "%qE attribute ignored", name);
8479 return NULL_TREE;
8482 /* Get the vector size (in bytes). */
8483 vecsize = tree_low_cst (size, 1);
8485 /* We need to provide for vector pointers, vector arrays, and
8486 functions returning vectors. For example:
8488 __attribute__((vector_size(16))) short *foo;
8490 In this case, the mode is SI, but the type being modified is
8491 HI, so we need to look further. */
8493 while (POINTER_TYPE_P (type)
8494 || TREE_CODE (type) == FUNCTION_TYPE
8495 || TREE_CODE (type) == METHOD_TYPE
8496 || TREE_CODE (type) == ARRAY_TYPE
8497 || TREE_CODE (type) == OFFSET_TYPE)
8498 type = TREE_TYPE (type);
8500 /* Get the mode of the type being modified. */
8501 orig_mode = TYPE_MODE (type);
8503 if ((!INTEGRAL_TYPE_P (type)
8504 && !SCALAR_FLOAT_TYPE_P (type)
8505 && !FIXED_POINT_TYPE_P (type))
8506 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8507 && GET_MODE_CLASS (orig_mode) != MODE_INT
8508 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8509 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8510 || TREE_CODE (type) == BOOLEAN_TYPE)
8512 error ("invalid vector type for attribute %qE", name);
8513 return NULL_TREE;
8516 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8518 error ("vector size not an integral multiple of component size");
8519 return NULL;
8522 if (vecsize == 0)
8524 error ("zero vector size");
8525 return NULL;
8528 /* Calculate how many units fit in the vector. */
8529 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8530 if (nunits & (nunits - 1))
8532 error ("number of components of the vector not a power of two");
8533 return NULL_TREE;
8536 new_type = build_vector_type (type, nunits);
8538 /* Build back pointers if needed. */
8539 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8541 return NULL_TREE;
8544 /* Handle the "nonnull" attribute. */
8545 static tree
8546 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8547 tree args, int ARG_UNUSED (flags),
8548 bool *no_add_attrs)
8550 tree type = *node;
8551 unsigned HOST_WIDE_INT attr_arg_num;
8553 /* If no arguments are specified, all pointer arguments should be
8554 non-null. Verify a full prototype is given so that the arguments
8555 will have the correct types when we actually check them later. */
8556 if (!args)
8558 if (!prototype_p (type))
8560 error ("nonnull attribute without arguments on a non-prototype");
8561 *no_add_attrs = true;
8563 return NULL_TREE;
8566 /* Argument list specified. Verify that each argument number references
8567 a pointer argument. */
8568 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8570 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8572 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8574 error ("nonnull argument has invalid operand number (argument %lu)",
8575 (unsigned long) attr_arg_num);
8576 *no_add_attrs = true;
8577 return NULL_TREE;
8580 if (prototype_p (type))
8582 function_args_iterator iter;
8583 tree argument;
8585 function_args_iter_init (&iter, type);
8586 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8588 argument = function_args_iter_cond (&iter);
8589 if (argument == NULL_TREE || ck_num == arg_num)
8590 break;
8593 if (!argument
8594 || TREE_CODE (argument) == VOID_TYPE)
8596 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8597 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8598 *no_add_attrs = true;
8599 return NULL_TREE;
8602 if (TREE_CODE (argument) != POINTER_TYPE)
8604 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8605 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8606 *no_add_attrs = true;
8607 return NULL_TREE;
8612 return NULL_TREE;
8615 /* Check the argument list of a function call for null in argument slots
8616 that are marked as requiring a non-null pointer argument. The NARGS
8617 arguments are passed in the array ARGARRAY.
8620 static void
8621 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8623 tree a;
8624 int i;
8626 attrs = lookup_attribute ("nonnull", attrs);
8627 if (attrs == NULL_TREE)
8628 return;
8630 a = attrs;
8631 /* See if any of the nonnull attributes has no arguments. If so,
8632 then every pointer argument is checked (in which case the check
8633 for pointer type is done in check_nonnull_arg). */
8634 if (TREE_VALUE (a) != NULL_TREE)
8636 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8637 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8639 if (a != NULL_TREE)
8640 for (i = 0; i < nargs; i++)
8641 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8642 i + 1);
8643 else
8645 /* Walk the argument list. If we encounter an argument number we
8646 should check for non-null, do it. */
8647 for (i = 0; i < nargs; i++)
8649 for (a = attrs; ; a = TREE_CHAIN (a))
8651 a = lookup_attribute ("nonnull", a);
8652 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8653 break;
8656 if (a != NULL_TREE)
8657 check_function_arguments_recurse (check_nonnull_arg, NULL,
8658 argarray[i], i + 1);
8663 /* Check that the Nth argument of a function call (counting backwards
8664 from the end) is a (pointer)0. The NARGS arguments are passed in the
8665 array ARGARRAY. */
8667 static void
8668 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8670 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8672 if (attr)
8674 int len = 0;
8675 int pos = 0;
8676 tree sentinel;
8677 function_args_iterator iter;
8678 tree t;
8680 /* Skip over the named arguments. */
8681 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8683 if (len == nargs)
8684 break;
8685 len++;
8688 if (TREE_VALUE (attr))
8690 tree p = TREE_VALUE (TREE_VALUE (attr));
8691 pos = TREE_INT_CST_LOW (p);
8694 /* The sentinel must be one of the varargs, i.e.
8695 in position >= the number of fixed arguments. */
8696 if ((nargs - 1 - pos) < len)
8698 warning (OPT_Wformat_,
8699 "not enough variable arguments to fit a sentinel");
8700 return;
8703 /* Validate the sentinel. */
8704 sentinel = argarray[nargs - 1 - pos];
8705 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8706 || !integer_zerop (sentinel))
8707 /* Although __null (in C++) is only an integer we allow it
8708 nevertheless, as we are guaranteed that it's exactly
8709 as wide as a pointer, and we don't want to force
8710 users to cast the NULL they have written there.
8711 We warn with -Wstrict-null-sentinel, though. */
8712 && (warn_strict_null_sentinel || null_node != sentinel))
8713 warning (OPT_Wformat_, "missing sentinel in function call");
8717 /* Helper for check_function_nonnull; given a list of operands which
8718 must be non-null in ARGS, determine if operand PARAM_NUM should be
8719 checked. */
8721 static bool
8722 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8724 unsigned HOST_WIDE_INT arg_num = 0;
8726 for (; args; args = TREE_CHAIN (args))
8728 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8730 gcc_assert (found);
8732 if (arg_num == param_num)
8733 return true;
8735 return false;
8738 /* Check that the function argument PARAM (which is operand number
8739 PARAM_NUM) is non-null. This is called by check_function_nonnull
8740 via check_function_arguments_recurse. */
8742 static void
8743 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8744 unsigned HOST_WIDE_INT param_num)
8746 /* Just skip checking the argument if it's not a pointer. This can
8747 happen if the "nonnull" attribute was given without an operand
8748 list (which means to check every pointer argument). */
8750 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8751 return;
8753 if (integer_zerop (param))
8754 warning (OPT_Wnonnull, "null argument where non-null required "
8755 "(argument %lu)", (unsigned long) param_num);
8758 /* Helper for nonnull attribute handling; fetch the operand number
8759 from the attribute argument list. */
8761 static bool
8762 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8764 /* Verify the arg number is a constant. */
8765 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8766 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8767 return false;
8769 *valp = TREE_INT_CST_LOW (arg_num_expr);
8770 return true;
8773 /* Handle a "nothrow" attribute; arguments as in
8774 struct attribute_spec.handler. */
8776 static tree
8777 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8778 int ARG_UNUSED (flags), bool *no_add_attrs)
8780 if (TREE_CODE (*node) == FUNCTION_DECL)
8781 TREE_NOTHROW (*node) = 1;
8782 /* ??? TODO: Support types. */
8783 else
8785 warning (OPT_Wattributes, "%qE attribute ignored", name);
8786 *no_add_attrs = true;
8789 return NULL_TREE;
8792 /* Handle a "cleanup" attribute; arguments as in
8793 struct attribute_spec.handler. */
8795 static tree
8796 handle_cleanup_attribute (tree *node, tree name, tree args,
8797 int ARG_UNUSED (flags), bool *no_add_attrs)
8799 tree decl = *node;
8800 tree cleanup_id, cleanup_decl;
8802 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8803 for global destructors in C++. This requires infrastructure that
8804 we don't have generically at the moment. It's also not a feature
8805 we'd be missing too much, since we do have attribute constructor. */
8806 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8808 warning (OPT_Wattributes, "%qE attribute ignored", name);
8809 *no_add_attrs = true;
8810 return NULL_TREE;
8813 /* Verify that the argument is a function in scope. */
8814 /* ??? We could support pointers to functions here as well, if
8815 that was considered desirable. */
8816 cleanup_id = TREE_VALUE (args);
8817 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8819 error ("cleanup argument not an identifier");
8820 *no_add_attrs = true;
8821 return NULL_TREE;
8823 cleanup_decl = lookup_name (cleanup_id);
8824 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8826 error ("cleanup argument not a function");
8827 *no_add_attrs = true;
8828 return NULL_TREE;
8831 /* That the function has proper type is checked with the
8832 eventual call to build_function_call. */
8834 return NULL_TREE;
8837 /* Handle a "warn_unused_result" attribute. No special handling. */
8839 static tree
8840 handle_warn_unused_result_attribute (tree *node, tree name,
8841 tree ARG_UNUSED (args),
8842 int ARG_UNUSED (flags), bool *no_add_attrs)
8844 /* Ignore the attribute for functions not returning any value. */
8845 if (VOID_TYPE_P (TREE_TYPE (*node)))
8847 warning (OPT_Wattributes, "%qE attribute ignored", name);
8848 *no_add_attrs = true;
8851 return NULL_TREE;
8854 /* Handle a "sentinel" attribute. */
8856 static tree
8857 handle_sentinel_attribute (tree *node, tree name, tree args,
8858 int ARG_UNUSED (flags), bool *no_add_attrs)
8860 if (!prototype_p (*node))
8862 warning (OPT_Wattributes,
8863 "%qE attribute requires prototypes with named arguments", name);
8864 *no_add_attrs = true;
8866 else
8868 if (!stdarg_p (*node))
8870 warning (OPT_Wattributes,
8871 "%qE attribute only applies to variadic functions", name);
8872 *no_add_attrs = true;
8876 if (args)
8878 tree position = TREE_VALUE (args);
8880 if (TREE_CODE (position) != INTEGER_CST)
8882 warning (OPT_Wattributes,
8883 "requested position is not an integer constant");
8884 *no_add_attrs = true;
8886 else
8888 if (tree_int_cst_lt (position, integer_zero_node))
8890 warning (OPT_Wattributes,
8891 "requested position is less than zero");
8892 *no_add_attrs = true;
8897 return NULL_TREE;
8900 /* Handle a "type_generic" attribute. */
8902 static tree
8903 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8904 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8905 bool * ARG_UNUSED (no_add_attrs))
8907 /* Ensure we have a function type. */
8908 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8910 /* Ensure we have a variadic function. */
8911 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8913 return NULL_TREE;
8916 /* Handle a "target" attribute. */
8918 static tree
8919 handle_target_attribute (tree *node, tree name, tree args, int flags,
8920 bool *no_add_attrs)
8922 /* Ensure we have a function type. */
8923 if (TREE_CODE (*node) != FUNCTION_DECL)
8925 warning (OPT_Wattributes, "%qE attribute ignored", name);
8926 *no_add_attrs = true;
8928 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8929 flags))
8930 *no_add_attrs = true;
8932 return NULL_TREE;
8935 /* Arguments being collected for optimization. */
8936 typedef const char *const_char_p; /* For DEF_VEC_P. */
8937 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
8940 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8941 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8942 false for #pragma GCC optimize. */
8944 bool
8945 parse_optimize_options (tree args, bool attr_p)
8947 bool ret = true;
8948 unsigned opt_argc;
8949 unsigned i;
8950 int saved_flag_strict_aliasing;
8951 const char **opt_argv;
8952 struct cl_decoded_option *decoded_options;
8953 unsigned int decoded_options_count;
8954 tree ap;
8956 /* Build up argv vector. Just in case the string is stored away, use garbage
8957 collected strings. */
8958 vec_safe_truncate (optimize_args, 0);
8959 vec_safe_push (optimize_args, (const char *) NULL);
8961 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8963 tree value = TREE_VALUE (ap);
8965 if (TREE_CODE (value) == INTEGER_CST)
8967 char buffer[20];
8968 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8969 vec_safe_push (optimize_args, ggc_strdup (buffer));
8972 else if (TREE_CODE (value) == STRING_CST)
8974 /* Split string into multiple substrings. */
8975 size_t len = TREE_STRING_LENGTH (value);
8976 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8977 char *end = p + len;
8978 char *comma;
8979 char *next_p = p;
8981 while (next_p != NULL)
8983 size_t len2;
8984 char *q, *r;
8986 p = next_p;
8987 comma = strchr (p, ',');
8988 if (comma)
8990 len2 = comma - p;
8991 *comma = '\0';
8992 next_p = comma+1;
8994 else
8996 len2 = end - p;
8997 next_p = NULL;
9000 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9002 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9003 options. */
9004 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9006 ret = false;
9007 if (attr_p)
9008 warning (OPT_Wattributes,
9009 "bad option %s to optimize attribute", p);
9010 else
9011 warning (OPT_Wpragmas,
9012 "bad option %s to pragma attribute", p);
9013 continue;
9016 if (*p != '-')
9018 *r++ = '-';
9020 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9021 itself is -Os, and any other switch begins with a -f. */
9022 if ((*p >= '0' && *p <= '9')
9023 || (p[0] == 's' && p[1] == '\0'))
9024 *r++ = 'O';
9025 else if (*p != 'O')
9026 *r++ = 'f';
9029 memcpy (r, p, len2);
9030 r[len2] = '\0';
9031 vec_safe_push (optimize_args, (const char *) q);
9037 opt_argc = optimize_args->length ();
9038 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9040 for (i = 1; i < opt_argc; i++)
9041 opt_argv[i] = (*optimize_args)[i];
9043 saved_flag_strict_aliasing = flag_strict_aliasing;
9045 /* Now parse the options. */
9046 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9047 &decoded_options,
9048 &decoded_options_count);
9049 decode_options (&global_options, &global_options_set,
9050 decoded_options, decoded_options_count,
9051 input_location, global_dc);
9053 targetm.override_options_after_change();
9055 /* Don't allow changing -fstrict-aliasing. */
9056 flag_strict_aliasing = saved_flag_strict_aliasing;
9058 optimize_args->truncate (0);
9059 return ret;
9062 /* For handling "optimize" attribute. arguments as in
9063 struct attribute_spec.handler. */
9065 static tree
9066 handle_optimize_attribute (tree *node, tree name, tree args,
9067 int ARG_UNUSED (flags), bool *no_add_attrs)
9069 /* Ensure we have a function type. */
9070 if (TREE_CODE (*node) != FUNCTION_DECL)
9072 warning (OPT_Wattributes, "%qE attribute ignored", name);
9073 *no_add_attrs = true;
9075 else
9077 struct cl_optimization cur_opts;
9078 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9080 /* Save current options. */
9081 cl_optimization_save (&cur_opts, &global_options);
9083 /* If we previously had some optimization options, use them as the
9084 default. */
9085 if (old_opts)
9086 cl_optimization_restore (&global_options,
9087 TREE_OPTIMIZATION (old_opts));
9089 /* Parse options, and update the vector. */
9090 parse_optimize_options (args, true);
9091 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9092 = build_optimization_node (&global_options);
9094 /* Restore current options. */
9095 cl_optimization_restore (&global_options, &cur_opts);
9098 return NULL_TREE;
9101 /* Handle a "no_split_stack" attribute. */
9103 static tree
9104 handle_no_split_stack_attribute (tree *node, tree name,
9105 tree ARG_UNUSED (args),
9106 int ARG_UNUSED (flags),
9107 bool *no_add_attrs)
9109 tree decl = *node;
9111 if (TREE_CODE (decl) != FUNCTION_DECL)
9113 error_at (DECL_SOURCE_LOCATION (decl),
9114 "%qE attribute applies only to functions", name);
9115 *no_add_attrs = true;
9117 else if (DECL_INITIAL (decl))
9119 error_at (DECL_SOURCE_LOCATION (decl),
9120 "can%'t set %qE attribute after definition", name);
9121 *no_add_attrs = true;
9124 return NULL_TREE;
9127 /* Handle a "returns_nonnull" attribute; arguments as in
9128 struct attribute_spec.handler. */
9130 static tree
9131 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9132 bool *no_add_attrs)
9134 // Even without a prototype we still have a return type we can check.
9135 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9137 error ("returns_nonnull attribute on a function not returning a pointer");
9138 *no_add_attrs = true;
9140 return NULL_TREE;
9144 /* Check for valid arguments being passed to a function with FNTYPE.
9145 There are NARGS arguments in the array ARGARRAY. */
9146 void
9147 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9149 /* Check for null being passed in a pointer argument that must be
9150 non-null. We also need to do this if format checking is enabled. */
9152 if (warn_nonnull)
9153 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9155 /* Check for errors in format strings. */
9157 if (warn_format || warn_suggest_attribute_format)
9158 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9160 if (warn_format)
9161 check_function_sentinel (fntype, nargs, argarray);
9164 /* Generic argument checking recursion routine. PARAM is the argument to
9165 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9166 once the argument is resolved. CTX is context for the callback. */
9167 void
9168 check_function_arguments_recurse (void (*callback)
9169 (void *, tree, unsigned HOST_WIDE_INT),
9170 void *ctx, tree param,
9171 unsigned HOST_WIDE_INT param_num)
9173 if (CONVERT_EXPR_P (param)
9174 && (TYPE_PRECISION (TREE_TYPE (param))
9175 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9177 /* Strip coercion. */
9178 check_function_arguments_recurse (callback, ctx,
9179 TREE_OPERAND (param, 0), param_num);
9180 return;
9183 if (TREE_CODE (param) == CALL_EXPR)
9185 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9186 tree attrs;
9187 bool found_format_arg = false;
9189 /* See if this is a call to a known internationalization function
9190 that modifies a format arg. Such a function may have multiple
9191 format_arg attributes (for example, ngettext). */
9193 for (attrs = TYPE_ATTRIBUTES (type);
9194 attrs;
9195 attrs = TREE_CHAIN (attrs))
9196 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9198 tree inner_arg;
9199 tree format_num_expr;
9200 int format_num;
9201 int i;
9202 call_expr_arg_iterator iter;
9204 /* Extract the argument number, which was previously checked
9205 to be valid. */
9206 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9208 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9209 && !TREE_INT_CST_HIGH (format_num_expr));
9211 format_num = TREE_INT_CST_LOW (format_num_expr);
9213 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9214 inner_arg != 0;
9215 inner_arg = next_call_expr_arg (&iter), i++)
9216 if (i == format_num)
9218 check_function_arguments_recurse (callback, ctx,
9219 inner_arg, param_num);
9220 found_format_arg = true;
9221 break;
9225 /* If we found a format_arg attribute and did a recursive check,
9226 we are done with checking this argument. Otherwise, we continue
9227 and this will be considered a non-literal. */
9228 if (found_format_arg)
9229 return;
9232 if (TREE_CODE (param) == COND_EXPR)
9234 /* Check both halves of the conditional expression. */
9235 check_function_arguments_recurse (callback, ctx,
9236 TREE_OPERAND (param, 1), param_num);
9237 check_function_arguments_recurse (callback, ctx,
9238 TREE_OPERAND (param, 2), param_num);
9239 return;
9242 (*callback) (ctx, param, param_num);
9245 /* Checks for a builtin function FNDECL that the number of arguments
9246 NARGS against the required number REQUIRED and issues an error if
9247 there is a mismatch. Returns true if the number of arguments is
9248 correct, otherwise false. */
9250 static bool
9251 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9253 if (nargs < required)
9255 error_at (input_location,
9256 "not enough arguments to function %qE", fndecl);
9257 return false;
9259 else if (nargs > required)
9261 error_at (input_location,
9262 "too many arguments to function %qE", fndecl);
9263 return false;
9265 return true;
9268 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9269 Returns false if there was an error, otherwise true. */
9271 bool
9272 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9274 if (!DECL_BUILT_IN (fndecl)
9275 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9276 return true;
9278 switch (DECL_FUNCTION_CODE (fndecl))
9280 case BUILT_IN_CONSTANT_P:
9281 return builtin_function_validate_nargs (fndecl, nargs, 1);
9283 case BUILT_IN_ISFINITE:
9284 case BUILT_IN_ISINF:
9285 case BUILT_IN_ISINF_SIGN:
9286 case BUILT_IN_ISNAN:
9287 case BUILT_IN_ISNORMAL:
9288 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9290 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9292 error ("non-floating-point argument in call to "
9293 "function %qE", fndecl);
9294 return false;
9296 return true;
9298 return false;
9300 case BUILT_IN_ISGREATER:
9301 case BUILT_IN_ISGREATEREQUAL:
9302 case BUILT_IN_ISLESS:
9303 case BUILT_IN_ISLESSEQUAL:
9304 case BUILT_IN_ISLESSGREATER:
9305 case BUILT_IN_ISUNORDERED:
9306 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9308 enum tree_code code0, code1;
9309 code0 = TREE_CODE (TREE_TYPE (args[0]));
9310 code1 = TREE_CODE (TREE_TYPE (args[1]));
9311 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9312 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9313 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9315 error ("non-floating-point arguments in call to "
9316 "function %qE", fndecl);
9317 return false;
9319 return true;
9321 return false;
9323 case BUILT_IN_FPCLASSIFY:
9324 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9326 unsigned i;
9328 for (i=0; i<5; i++)
9329 if (TREE_CODE (args[i]) != INTEGER_CST)
9331 error ("non-const integer argument %u in call to function %qE",
9332 i+1, fndecl);
9333 return false;
9336 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9338 error ("non-floating-point argument in call to function %qE",
9339 fndecl);
9340 return false;
9342 return true;
9344 return false;
9346 case BUILT_IN_ASSUME_ALIGNED:
9347 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9349 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9351 error ("non-integer argument 3 in call to function %qE", fndecl);
9352 return false;
9354 return true;
9356 return false;
9358 default:
9359 return true;
9363 /* Function to help qsort sort FIELD_DECLs by name order. */
9366 field_decl_cmp (const void *x_p, const void *y_p)
9368 const tree *const x = (const tree *const) x_p;
9369 const tree *const y = (const tree *const) y_p;
9371 if (DECL_NAME (*x) == DECL_NAME (*y))
9372 /* A nontype is "greater" than a type. */
9373 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9374 if (DECL_NAME (*x) == NULL_TREE)
9375 return -1;
9376 if (DECL_NAME (*y) == NULL_TREE)
9377 return 1;
9378 if (DECL_NAME (*x) < DECL_NAME (*y))
9379 return -1;
9380 return 1;
9383 static struct {
9384 gt_pointer_operator new_value;
9385 void *cookie;
9386 } resort_data;
9388 /* This routine compares two fields like field_decl_cmp but using the
9389 pointer operator in resort_data. */
9391 static int
9392 resort_field_decl_cmp (const void *x_p, const void *y_p)
9394 const tree *const x = (const tree *const) x_p;
9395 const tree *const y = (const tree *const) y_p;
9397 if (DECL_NAME (*x) == DECL_NAME (*y))
9398 /* A nontype is "greater" than a type. */
9399 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9400 if (DECL_NAME (*x) == NULL_TREE)
9401 return -1;
9402 if (DECL_NAME (*y) == NULL_TREE)
9403 return 1;
9405 tree d1 = DECL_NAME (*x);
9406 tree d2 = DECL_NAME (*y);
9407 resort_data.new_value (&d1, resort_data.cookie);
9408 resort_data.new_value (&d2, resort_data.cookie);
9409 if (d1 < d2)
9410 return -1;
9412 return 1;
9415 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9417 void
9418 resort_sorted_fields (void *obj,
9419 void * ARG_UNUSED (orig_obj),
9420 gt_pointer_operator new_value,
9421 void *cookie)
9423 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9424 resort_data.new_value = new_value;
9425 resort_data.cookie = cookie;
9426 qsort (&sf->elts[0], sf->len, sizeof (tree),
9427 resort_field_decl_cmp);
9430 /* Subroutine of c_parse_error.
9431 Return the result of concatenating LHS and RHS. RHS is really
9432 a string literal, its first character is indicated by RHS_START and
9433 RHS_SIZE is its length (including the terminating NUL character).
9435 The caller is responsible for deleting the returned pointer. */
9437 static char *
9438 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9440 const int lhs_size = strlen (lhs);
9441 char *result = XNEWVEC (char, lhs_size + rhs_size);
9442 strncpy (result, lhs, lhs_size);
9443 strncpy (result + lhs_size, rhs_start, rhs_size);
9444 return result;
9447 /* Issue the error given by GMSGID, indicating that it occurred before
9448 TOKEN, which had the associated VALUE. */
9450 void
9451 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9452 tree value, unsigned char token_flags)
9454 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9456 char *message = NULL;
9458 if (token_type == CPP_EOF)
9459 message = catenate_messages (gmsgid, " at end of input");
9460 else if (token_type == CPP_CHAR
9461 || token_type == CPP_WCHAR
9462 || token_type == CPP_CHAR16
9463 || token_type == CPP_CHAR32)
9465 unsigned int val = TREE_INT_CST_LOW (value);
9466 const char *prefix;
9468 switch (token_type)
9470 default:
9471 prefix = "";
9472 break;
9473 case CPP_WCHAR:
9474 prefix = "L";
9475 break;
9476 case CPP_CHAR16:
9477 prefix = "u";
9478 break;
9479 case CPP_CHAR32:
9480 prefix = "U";
9481 break;
9484 if (val <= UCHAR_MAX && ISGRAPH (val))
9485 message = catenate_messages (gmsgid, " before %s'%c'");
9486 else
9487 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9489 error (message, prefix, val);
9490 free (message);
9491 message = NULL;
9493 else if (token_type == CPP_CHAR_USERDEF
9494 || token_type == CPP_WCHAR_USERDEF
9495 || token_type == CPP_CHAR16_USERDEF
9496 || token_type == CPP_CHAR32_USERDEF)
9497 message = catenate_messages (gmsgid,
9498 " before user-defined character literal");
9499 else if (token_type == CPP_STRING_USERDEF
9500 || token_type == CPP_WSTRING_USERDEF
9501 || token_type == CPP_STRING16_USERDEF
9502 || token_type == CPP_STRING32_USERDEF
9503 || token_type == CPP_UTF8STRING_USERDEF)
9504 message = catenate_messages (gmsgid, " before user-defined string literal");
9505 else if (token_type == CPP_STRING
9506 || token_type == CPP_WSTRING
9507 || token_type == CPP_STRING16
9508 || token_type == CPP_STRING32
9509 || token_type == CPP_UTF8STRING)
9510 message = catenate_messages (gmsgid, " before string constant");
9511 else if (token_type == CPP_NUMBER)
9512 message = catenate_messages (gmsgid, " before numeric constant");
9513 else if (token_type == CPP_NAME)
9515 message = catenate_messages (gmsgid, " before %qE");
9516 error (message, value);
9517 free (message);
9518 message = NULL;
9520 else if (token_type == CPP_PRAGMA)
9521 message = catenate_messages (gmsgid, " before %<#pragma%>");
9522 else if (token_type == CPP_PRAGMA_EOL)
9523 message = catenate_messages (gmsgid, " before end of line");
9524 else if (token_type == CPP_DECLTYPE)
9525 message = catenate_messages (gmsgid, " before %<decltype%>");
9526 else if (token_type < N_TTYPES)
9528 message = catenate_messages (gmsgid, " before %qs token");
9529 error (message, cpp_type2name (token_type, token_flags));
9530 free (message);
9531 message = NULL;
9533 else
9534 error (gmsgid);
9536 if (message)
9538 error (message);
9539 free (message);
9541 #undef catenate_messages
9544 /* Mapping for cpp message reasons to the options that enable them. */
9546 struct reason_option_codes_t
9548 const int reason; /* cpplib message reason. */
9549 const int option_code; /* gcc option that controls this message. */
9552 static const struct reason_option_codes_t option_codes[] = {
9553 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9554 {CPP_W_COMMENTS, OPT_Wcomment},
9555 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9556 {CPP_W_MULTICHAR, OPT_Wmultichar},
9557 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9558 {CPP_W_LONG_LONG, OPT_Wlong_long},
9559 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9560 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9561 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9562 {CPP_W_UNDEF, OPT_Wundef},
9563 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9564 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9565 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9566 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9567 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9568 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9569 {CPP_W_DATE_TIME, OPT_Wdate_time},
9570 {CPP_W_NONE, 0}
9573 /* Return the gcc option code associated with the reason for a cpp
9574 message, or 0 if none. */
9576 static int
9577 c_option_controlling_cpp_error (int reason)
9579 const struct reason_option_codes_t *entry;
9581 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9583 if (entry->reason == reason)
9584 return entry->option_code;
9586 return 0;
9589 /* Callback from cpp_error for PFILE to print diagnostics from the
9590 preprocessor. The diagnostic is of type LEVEL, with REASON set
9591 to the reason code if LEVEL is represents a warning, at location
9592 LOCATION unless this is after lexing and the compiler's location
9593 should be used instead, with column number possibly overridden by
9594 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9595 the arguments. Returns true if a diagnostic was emitted, false
9596 otherwise. */
9598 bool
9599 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9600 location_t location, unsigned int column_override,
9601 const char *msg, va_list *ap)
9603 diagnostic_info diagnostic;
9604 diagnostic_t dlevel;
9605 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9606 bool ret;
9608 switch (level)
9610 case CPP_DL_WARNING_SYSHDR:
9611 if (flag_no_output)
9612 return false;
9613 global_dc->dc_warn_system_headers = 1;
9614 /* Fall through. */
9615 case CPP_DL_WARNING:
9616 if (flag_no_output)
9617 return false;
9618 dlevel = DK_WARNING;
9619 break;
9620 case CPP_DL_PEDWARN:
9621 if (flag_no_output && !flag_pedantic_errors)
9622 return false;
9623 dlevel = DK_PEDWARN;
9624 break;
9625 case CPP_DL_ERROR:
9626 dlevel = DK_ERROR;
9627 break;
9628 case CPP_DL_ICE:
9629 dlevel = DK_ICE;
9630 break;
9631 case CPP_DL_NOTE:
9632 dlevel = DK_NOTE;
9633 break;
9634 case CPP_DL_FATAL:
9635 dlevel = DK_FATAL;
9636 break;
9637 default:
9638 gcc_unreachable ();
9640 if (done_lexing)
9641 location = input_location;
9642 diagnostic_set_info_translated (&diagnostic, msg, ap,
9643 location, dlevel);
9644 if (column_override)
9645 diagnostic_override_column (&diagnostic, column_override);
9646 diagnostic_override_option_index (&diagnostic,
9647 c_option_controlling_cpp_error (reason));
9648 ret = report_diagnostic (&diagnostic);
9649 if (level == CPP_DL_WARNING_SYSHDR)
9650 global_dc->dc_warn_system_headers = save_warn_system_headers;
9651 return ret;
9654 /* Convert a character from the host to the target execution character
9655 set. cpplib handles this, mostly. */
9657 HOST_WIDE_INT
9658 c_common_to_target_charset (HOST_WIDE_INT c)
9660 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9661 zero-extended under -fno-signed-char. cpplib insists that characters
9662 and character constants are always unsigned. Hence we must convert
9663 back and forth. */
9664 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9666 uc = cpp_host_to_exec_charset (parse_in, uc);
9668 if (flag_signed_char)
9669 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9670 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9671 else
9672 return uc;
9675 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9676 references with an INDIRECT_REF of a constant at the bottom; much like the
9677 traditional rendering of offsetof as a macro. Return the folded result. */
9679 tree
9680 fold_offsetof_1 (tree expr)
9682 tree base, off, t;
9684 switch (TREE_CODE (expr))
9686 case ERROR_MARK:
9687 return expr;
9689 case VAR_DECL:
9690 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9691 return error_mark_node;
9693 case CALL_EXPR:
9694 case TARGET_EXPR:
9695 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9696 return error_mark_node;
9698 case NOP_EXPR:
9699 case INDIRECT_REF:
9700 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9702 error ("cannot apply %<offsetof%> to a non constant address");
9703 return error_mark_node;
9705 return TREE_OPERAND (expr, 0);
9707 case COMPONENT_REF:
9708 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9709 if (base == error_mark_node)
9710 return base;
9712 t = TREE_OPERAND (expr, 1);
9713 if (DECL_C_BIT_FIELD (t))
9715 error ("attempt to take address of bit-field structure "
9716 "member %qD", t);
9717 return error_mark_node;
9719 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9720 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9722 / BITS_PER_UNIT));
9723 break;
9725 case ARRAY_REF:
9726 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9727 if (base == error_mark_node)
9728 return base;
9730 t = TREE_OPERAND (expr, 1);
9732 /* Check if the offset goes beyond the upper bound of the array. */
9733 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9735 tree upbound = array_ref_up_bound (expr);
9736 if (upbound != NULL_TREE
9737 && TREE_CODE (upbound) == INTEGER_CST
9738 && !tree_int_cst_equal (upbound,
9739 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9741 upbound = size_binop (PLUS_EXPR, upbound,
9742 build_int_cst (TREE_TYPE (upbound), 1));
9743 if (tree_int_cst_lt (upbound, t))
9745 tree v;
9747 for (v = TREE_OPERAND (expr, 0);
9748 TREE_CODE (v) == COMPONENT_REF;
9749 v = TREE_OPERAND (v, 0))
9750 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9751 == RECORD_TYPE)
9753 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9754 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9755 if (TREE_CODE (fld_chain) == FIELD_DECL)
9756 break;
9758 if (fld_chain)
9759 break;
9761 /* Don't warn if the array might be considered a poor
9762 man's flexible array member with a very permissive
9763 definition thereof. */
9764 if (TREE_CODE (v) == ARRAY_REF
9765 || TREE_CODE (v) == COMPONENT_REF)
9766 warning (OPT_Warray_bounds,
9767 "index %E denotes an offset "
9768 "greater than size of %qT",
9769 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9774 t = convert (sizetype, t);
9775 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9776 break;
9778 case COMPOUND_EXPR:
9779 /* Handle static members of volatile structs. */
9780 t = TREE_OPERAND (expr, 1);
9781 gcc_assert (TREE_CODE (t) == VAR_DECL);
9782 return fold_offsetof_1 (t);
9784 default:
9785 gcc_unreachable ();
9788 return fold_build_pointer_plus (base, off);
9791 /* Likewise, but convert it to the return type of offsetof. */
9793 tree
9794 fold_offsetof (tree expr)
9796 return convert (size_type_node, fold_offsetof_1 (expr));
9799 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9800 expression, because B will always be true. */
9802 void
9803 warn_for_omitted_condop (location_t location, tree cond)
9805 if (truth_value_p (TREE_CODE (cond)))
9806 warning_at (location, OPT_Wparentheses,
9807 "the omitted middle operand in ?: will always be %<true%>, "
9808 "suggest explicit middle operand");
9811 /* Give an error for storing into ARG, which is 'const'. USE indicates
9812 how ARG was being used. */
9814 void
9815 readonly_error (tree arg, enum lvalue_use use)
9817 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9818 || use == lv_asm);
9819 /* Using this macro rather than (for example) arrays of messages
9820 ensures that all the format strings are checked at compile
9821 time. */
9822 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9823 : (use == lv_increment ? (I) \
9824 : (use == lv_decrement ? (D) : (AS))))
9825 if (TREE_CODE (arg) == COMPONENT_REF)
9827 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9828 error (READONLY_MSG (G_("assignment of member "
9829 "%qD in read-only object"),
9830 G_("increment of member "
9831 "%qD in read-only object"),
9832 G_("decrement of member "
9833 "%qD in read-only object"),
9834 G_("member %qD in read-only object "
9835 "used as %<asm%> output")),
9836 TREE_OPERAND (arg, 1));
9837 else
9838 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9839 G_("increment of read-only member %qD"),
9840 G_("decrement of read-only member %qD"),
9841 G_("read-only member %qD used as %<asm%> output")),
9842 TREE_OPERAND (arg, 1));
9844 else if (TREE_CODE (arg) == VAR_DECL)
9845 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9846 G_("increment of read-only variable %qD"),
9847 G_("decrement of read-only variable %qD"),
9848 G_("read-only variable %qD used as %<asm%> output")),
9849 arg);
9850 else if (TREE_CODE (arg) == PARM_DECL)
9851 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9852 G_("increment of read-only parameter %qD"),
9853 G_("decrement of read-only parameter %qD"),
9854 G_("read-only parameter %qD use as %<asm%> output")),
9855 arg);
9856 else if (TREE_CODE (arg) == RESULT_DECL)
9858 gcc_assert (c_dialect_cxx ());
9859 error (READONLY_MSG (G_("assignment of "
9860 "read-only named return value %qD"),
9861 G_("increment of "
9862 "read-only named return value %qD"),
9863 G_("decrement of "
9864 "read-only named return value %qD"),
9865 G_("read-only named return value %qD "
9866 "used as %<asm%>output")),
9867 arg);
9869 else if (TREE_CODE (arg) == FUNCTION_DECL)
9870 error (READONLY_MSG (G_("assignment of function %qD"),
9871 G_("increment of function %qD"),
9872 G_("decrement of function %qD"),
9873 G_("function %qD used as %<asm%> output")),
9874 arg);
9875 else
9876 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9877 G_("increment of read-only location %qE"),
9878 G_("decrement of read-only location %qE"),
9879 G_("read-only location %qE used as %<asm%> output")),
9880 arg);
9883 /* Print an error message for an invalid lvalue. USE says
9884 how the lvalue is being used and so selects the error message. LOC
9885 is the location for the error. */
9887 void
9888 lvalue_error (location_t loc, enum lvalue_use use)
9890 switch (use)
9892 case lv_assign:
9893 error_at (loc, "lvalue required as left operand of assignment");
9894 break;
9895 case lv_increment:
9896 error_at (loc, "lvalue required as increment operand");
9897 break;
9898 case lv_decrement:
9899 error_at (loc, "lvalue required as decrement operand");
9900 break;
9901 case lv_addressof:
9902 error_at (loc, "lvalue required as unary %<&%> operand");
9903 break;
9904 case lv_asm:
9905 error_at (loc, "lvalue required in asm statement");
9906 break;
9907 default:
9908 gcc_unreachable ();
9912 /* Print an error message for an invalid indirection of type TYPE.
9913 ERRSTRING is the name of the operator for the indirection. */
9915 void
9916 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9918 switch (errstring)
9920 case RO_NULL:
9921 gcc_assert (c_dialect_cxx ());
9922 error_at (loc, "invalid type argument (have %qT)", type);
9923 break;
9924 case RO_ARRAY_INDEXING:
9925 error_at (loc,
9926 "invalid type argument of array indexing (have %qT)",
9927 type);
9928 break;
9929 case RO_UNARY_STAR:
9930 error_at (loc,
9931 "invalid type argument of unary %<*%> (have %qT)",
9932 type);
9933 break;
9934 case RO_ARROW:
9935 error_at (loc,
9936 "invalid type argument of %<->%> (have %qT)",
9937 type);
9938 break;
9939 case RO_ARROW_STAR:
9940 error_at (loc,
9941 "invalid type argument of %<->*%> (have %qT)",
9942 type);
9943 break;
9944 case RO_IMPLICIT_CONVERSION:
9945 error_at (loc,
9946 "invalid type argument of implicit conversion (have %qT)",
9947 type);
9948 break;
9949 default:
9950 gcc_unreachable ();
9954 /* *PTYPE is an incomplete array. Complete it with a domain based on
9955 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9956 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9957 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9960 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9962 tree maxindex, type, main_type, elt, unqual_elt;
9963 int failure = 0, quals;
9964 hashval_t hashcode = 0;
9965 bool overflow_p = false;
9967 maxindex = size_zero_node;
9968 if (initial_value)
9970 if (TREE_CODE (initial_value) == STRING_CST)
9972 int eltsize
9973 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9974 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9976 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9978 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9980 if (vec_safe_is_empty (v))
9982 if (pedantic)
9983 failure = 3;
9984 maxindex = ssize_int (-1);
9986 else
9988 tree curindex;
9989 unsigned HOST_WIDE_INT cnt;
9990 constructor_elt *ce;
9991 bool fold_p = false;
9993 if ((*v)[0].index)
9994 maxindex = (*v)[0].index, fold_p = true;
9996 curindex = maxindex;
9998 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10000 bool curfold_p = false;
10001 if (ce->index)
10002 curindex = ce->index, curfold_p = true;
10003 else
10005 if (fold_p)
10007 /* Since we treat size types now as ordinary
10008 unsigned types, we need an explicit overflow
10009 check. */
10010 tree orig = curindex;
10011 curindex = fold_convert (sizetype, curindex);
10012 overflow_p |= tree_int_cst_lt (curindex, orig);
10014 curindex = size_binop (PLUS_EXPR, curindex,
10015 size_one_node);
10017 if (tree_int_cst_lt (maxindex, curindex))
10018 maxindex = curindex, fold_p = curfold_p;
10020 if (fold_p)
10022 tree orig = maxindex;
10023 maxindex = fold_convert (sizetype, maxindex);
10024 overflow_p |= tree_int_cst_lt (maxindex, orig);
10028 else
10030 /* Make an error message unless that happened already. */
10031 if (initial_value != error_mark_node)
10032 failure = 1;
10035 else
10037 failure = 2;
10038 if (!do_default)
10039 return failure;
10042 type = *ptype;
10043 elt = TREE_TYPE (type);
10044 quals = TYPE_QUALS (strip_array_types (elt));
10045 if (quals == 0)
10046 unqual_elt = elt;
10047 else
10048 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10050 /* Using build_distinct_type_copy and modifying things afterward instead
10051 of using build_array_type to create a new type preserves all of the
10052 TYPE_LANG_FLAG_? bits that the front end may have set. */
10053 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10054 TREE_TYPE (main_type) = unqual_elt;
10055 TYPE_DOMAIN (main_type)
10056 = build_range_type (TREE_TYPE (maxindex),
10057 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10058 layout_type (main_type);
10060 /* Make sure we have the canonical MAIN_TYPE. */
10061 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10062 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10063 hashcode);
10064 main_type = type_hash_canon (hashcode, main_type);
10066 /* Fix the canonical type. */
10067 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10068 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10069 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10070 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10071 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10072 != TYPE_DOMAIN (main_type)))
10073 TYPE_CANONICAL (main_type)
10074 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10075 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10076 else
10077 TYPE_CANONICAL (main_type) = main_type;
10079 if (quals == 0)
10080 type = main_type;
10081 else
10082 type = c_build_qualified_type (main_type, quals);
10084 if (COMPLETE_TYPE_P (type)
10085 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10086 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10088 error ("size of array is too large");
10089 /* If we proceed with the array type as it is, we'll eventually
10090 crash in tree_low_cst(). */
10091 type = error_mark_node;
10094 *ptype = type;
10095 return failure;
10098 /* Like c_mark_addressable but don't check register qualifier. */
10099 void
10100 c_common_mark_addressable_vec (tree t)
10102 while (handled_component_p (t))
10103 t = TREE_OPERAND (t, 0);
10104 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10105 return;
10106 TREE_ADDRESSABLE (t) = 1;
10111 /* Used to help initialize the builtin-types.def table. When a type of
10112 the correct size doesn't exist, use error_mark_node instead of NULL.
10113 The later results in segfaults even when a decl using the type doesn't
10114 get invoked. */
10116 tree
10117 builtin_type_for_size (int size, bool unsignedp)
10119 tree type = c_common_type_for_size (size, unsignedp);
10120 return type ? type : error_mark_node;
10123 /* A helper function for resolve_overloaded_builtin in resolving the
10124 overloaded __sync_ builtins. Returns a positive power of 2 if the
10125 first operand of PARAMS is a pointer to a supported data type.
10126 Returns 0 if an error is encountered. */
10128 static int
10129 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10131 tree type;
10132 int size;
10134 if (!params)
10136 error ("too few arguments to function %qE", function);
10137 return 0;
10140 type = TREE_TYPE ((*params)[0]);
10141 if (TREE_CODE (type) != POINTER_TYPE)
10142 goto incompatible;
10144 type = TREE_TYPE (type);
10145 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10146 goto incompatible;
10148 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
10149 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10150 return size;
10152 incompatible:
10153 error ("incompatible type for argument %d of %qE", 1, function);
10154 return 0;
10157 /* A helper function for resolve_overloaded_builtin. Adds casts to
10158 PARAMS to make arguments match up with those of FUNCTION. Drops
10159 the variadic arguments at the end. Returns false if some error
10160 was encountered; true on success. */
10162 static bool
10163 sync_resolve_params (location_t loc, tree orig_function, tree function,
10164 vec<tree, va_gc> *params, bool orig_format)
10166 function_args_iterator iter;
10167 tree ptype;
10168 unsigned int parmnum;
10170 function_args_iter_init (&iter, TREE_TYPE (function));
10171 /* We've declared the implementation functions to use "volatile void *"
10172 as the pointer parameter, so we shouldn't get any complaints from the
10173 call to check_function_arguments what ever type the user used. */
10174 function_args_iter_next (&iter);
10175 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10176 ptype = TYPE_MAIN_VARIANT (ptype);
10178 /* For the rest of the values, we need to cast these to FTYPE, so that we
10179 don't get warnings for passing pointer types, etc. */
10180 parmnum = 0;
10181 while (1)
10183 tree val, arg_type;
10185 arg_type = function_args_iter_cond (&iter);
10186 /* XXX void_type_node belies the abstraction. */
10187 if (arg_type == void_type_node)
10188 break;
10190 ++parmnum;
10191 if (params->length () <= parmnum)
10193 error_at (loc, "too few arguments to function %qE", orig_function);
10194 return false;
10197 /* Only convert parameters if arg_type is unsigned integer type with
10198 new format sync routines, i.e. don't attempt to convert pointer
10199 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10200 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10201 kinds). */
10202 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10204 /* Ideally for the first conversion we'd use convert_for_assignment
10205 so that we get warnings for anything that doesn't match the pointer
10206 type. This isn't portable across the C and C++ front ends atm. */
10207 val = (*params)[parmnum];
10208 val = convert (ptype, val);
10209 val = convert (arg_type, val);
10210 (*params)[parmnum] = val;
10213 function_args_iter_next (&iter);
10216 /* __atomic routines are not variadic. */
10217 if (!orig_format && params->length () != parmnum + 1)
10219 error_at (loc, "too many arguments to function %qE", orig_function);
10220 return false;
10223 /* The definition of these primitives is variadic, with the remaining
10224 being "an optional list of variables protected by the memory barrier".
10225 No clue what that's supposed to mean, precisely, but we consider all
10226 call-clobbered variables to be protected so we're safe. */
10227 params->truncate (parmnum + 1);
10229 return true;
10232 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10233 RESULT to make it match the type of the first pointer argument in
10234 PARAMS. */
10236 static tree
10237 sync_resolve_return (tree first_param, tree result, bool orig_format)
10239 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10240 tree rtype = TREE_TYPE (result);
10241 ptype = TYPE_MAIN_VARIANT (ptype);
10243 /* New format doesn't require casting unless the types are the same size. */
10244 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10245 return convert (ptype, result);
10246 else
10247 return result;
10250 /* This function verifies the PARAMS to generic atomic FUNCTION.
10251 It returns the size if all the parameters are the same size, otherwise
10252 0 is returned if the parameters are invalid. */
10254 static int
10255 get_atomic_generic_size (location_t loc, tree function,
10256 vec<tree, va_gc> *params)
10258 unsigned int n_param;
10259 unsigned int n_model;
10260 unsigned int x;
10261 int size_0;
10262 tree type_0;
10264 /* Determine the parameter makeup. */
10265 switch (DECL_FUNCTION_CODE (function))
10267 case BUILT_IN_ATOMIC_EXCHANGE:
10268 n_param = 4;
10269 n_model = 1;
10270 break;
10271 case BUILT_IN_ATOMIC_LOAD:
10272 case BUILT_IN_ATOMIC_STORE:
10273 n_param = 3;
10274 n_model = 1;
10275 break;
10276 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10277 n_param = 6;
10278 n_model = 2;
10279 break;
10280 default:
10281 gcc_unreachable ();
10284 if (vec_safe_length (params) != n_param)
10286 error_at (loc, "incorrect number of arguments to function %qE", function);
10287 return 0;
10290 /* Get type of first parameter, and determine its size. */
10291 type_0 = TREE_TYPE ((*params)[0]);
10292 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10294 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10295 function);
10296 return 0;
10299 /* Types must be compile time constant sizes. */
10300 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10302 error_at (loc,
10303 "argument 1 of %qE must be a pointer to a constant size type",
10304 function);
10305 return 0;
10308 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10310 /* Zero size objects are not allowed. */
10311 if (size_0 == 0)
10313 error_at (loc,
10314 "argument 1 of %qE must be a pointer to a nonzero size object",
10315 function);
10316 return 0;
10319 /* Check each other parameter is a pointer and the same size. */
10320 for (x = 0; x < n_param - n_model; x++)
10322 int size;
10323 tree type = TREE_TYPE ((*params)[x]);
10324 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10325 if (n_param == 6 && x == 3)
10326 continue;
10327 if (!POINTER_TYPE_P (type))
10329 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10330 function);
10331 return 0;
10333 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10334 if (size != size_0)
10336 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10337 function);
10338 return 0;
10342 /* Check memory model parameters for validity. */
10343 for (x = n_param - n_model ; x < n_param; x++)
10345 tree p = (*params)[x];
10346 if (TREE_CODE (p) == INTEGER_CST)
10348 int i = tree_low_cst (p, 1);
10349 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10351 warning_at (loc, OPT_Winvalid_memory_model,
10352 "invalid memory model argument %d of %qE", x + 1,
10353 function);
10356 else
10357 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10359 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10360 function);
10361 return 0;
10365 return size_0;
10369 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10370 at the beginning of the parameter list PARAMS representing the size of the
10371 objects. This is to match the library ABI requirement. LOC is the location
10372 of the function call.
10373 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10374 returned to allow the external call to be constructed. */
10376 static tree
10377 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10378 vec<tree, va_gc> *params)
10380 tree size_node;
10382 /* Insert a SIZE_T parameter as the first param. If there isn't
10383 enough space, allocate a new vector and recursively re-build with that. */
10384 if (!params->space (1))
10386 unsigned int z, len;
10387 vec<tree, va_gc> *v;
10388 tree f;
10390 len = params->length ();
10391 vec_alloc (v, len + 1);
10392 for (z = 0; z < len; z++)
10393 v->quick_push ((*params)[z]);
10394 f = build_function_call_vec (loc, function, v, NULL);
10395 vec_free (v);
10396 return f;
10399 /* Add the size parameter and leave as a function call for processing. */
10400 size_node = build_int_cst (size_type_node, n);
10401 params->quick_insert (0, size_node);
10402 return NULL_TREE;
10406 /* Return whether atomic operations for naturally aligned N-byte
10407 arguments are supported, whether inline or through libatomic. */
10408 static bool
10409 atomic_size_supported_p (int n)
10411 switch (n)
10413 case 1:
10414 case 2:
10415 case 4:
10416 case 8:
10417 return true;
10419 case 16:
10420 return targetm.scalar_mode_supported_p (TImode);
10422 default:
10423 return false;
10427 /* This will process an __atomic_exchange function call, determine whether it
10428 needs to be mapped to the _N variation, or turned into a library call.
10429 LOC is the location of the builtin call.
10430 FUNCTION is the DECL that has been invoked;
10431 PARAMS is the argument list for the call. The return value is non-null
10432 TRUE is returned if it is translated into the proper format for a call to the
10433 external library, and NEW_RETURN is set the tree for that function.
10434 FALSE is returned if processing for the _N variation is required, and
10435 NEW_RETURN is set to the the return value the result is copied into. */
10436 static bool
10437 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10438 vec<tree, va_gc> *params, tree *new_return)
10440 tree p0, p1, p2, p3;
10441 tree I_type, I_type_ptr;
10442 int n = get_atomic_generic_size (loc, function, params);
10444 /* Size of 0 is an error condition. */
10445 if (n == 0)
10447 *new_return = error_mark_node;
10448 return true;
10451 /* If not a lock-free size, change to the library generic format. */
10452 if (!atomic_size_supported_p (n))
10454 *new_return = add_atomic_size_parameter (n, loc, function, params);
10455 return true;
10458 /* Otherwise there is a lockfree match, transform the call from:
10459 void fn(T* mem, T* desired, T* return, model)
10460 into
10461 *return = (T) (fn (In* mem, (In) *desired, model)) */
10463 p0 = (*params)[0];
10464 p1 = (*params)[1];
10465 p2 = (*params)[2];
10466 p3 = (*params)[3];
10468 /* Create pointer to appropriate size. */
10469 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10470 I_type_ptr = build_pointer_type (I_type);
10472 /* Convert object pointer to required type. */
10473 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10474 (*params)[0] = p0;
10475 /* Convert new value to required type, and dereference it. */
10476 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10477 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10478 (*params)[1] = p1;
10480 /* Move memory model to the 3rd position, and end param list. */
10481 (*params)[2] = p3;
10482 params->truncate (3);
10484 /* Convert return pointer and dereference it for later assignment. */
10485 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10487 return false;
10491 /* This will process an __atomic_compare_exchange function call, determine
10492 whether it needs to be mapped to the _N variation, or turned into a lib call.
10493 LOC is the location of the builtin call.
10494 FUNCTION is the DECL that has been invoked;
10495 PARAMS is the argument list for the call. The return value is non-null
10496 TRUE is returned if it is translated into the proper format for a call to the
10497 external library, and NEW_RETURN is set the tree for that function.
10498 FALSE is returned if processing for the _N variation is required. */
10500 static bool
10501 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10502 vec<tree, va_gc> *params,
10503 tree *new_return)
10505 tree p0, p1, p2;
10506 tree I_type, I_type_ptr;
10507 int n = get_atomic_generic_size (loc, function, params);
10509 /* Size of 0 is an error condition. */
10510 if (n == 0)
10512 *new_return = error_mark_node;
10513 return true;
10516 /* If not a lock-free size, change to the library generic format. */
10517 if (!atomic_size_supported_p (n))
10519 /* The library generic format does not have the weak parameter, so
10520 remove it from the param list. Since a parameter has been removed,
10521 we can be sure that there is room for the SIZE_T parameter, meaning
10522 there will not be a recursive rebuilding of the parameter list, so
10523 there is no danger this will be done twice. */
10524 if (n > 0)
10526 (*params)[3] = (*params)[4];
10527 (*params)[4] = (*params)[5];
10528 params->truncate (5);
10530 *new_return = add_atomic_size_parameter (n, loc, function, params);
10531 return true;
10534 /* Otherwise, there is a match, so the call needs to be transformed from:
10535 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10536 into
10537 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10539 p0 = (*params)[0];
10540 p1 = (*params)[1];
10541 p2 = (*params)[2];
10543 /* Create pointer to appropriate size. */
10544 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10545 I_type_ptr = build_pointer_type (I_type);
10547 /* Convert object pointer to required type. */
10548 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10549 (*params)[0] = p0;
10551 /* Convert expected pointer to required type. */
10552 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10553 (*params)[1] = p1;
10555 /* Convert desired value to required type, and dereference it. */
10556 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10557 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10558 (*params)[2] = p2;
10560 /* The rest of the parameters are fine. NULL means no special return value
10561 processing.*/
10562 *new_return = NULL;
10563 return false;
10567 /* This will process an __atomic_load function call, determine whether it
10568 needs to be mapped to the _N variation, or turned into a library call.
10569 LOC is the location of the builtin call.
10570 FUNCTION is the DECL that has been invoked;
10571 PARAMS is the argument list for the call. The return value is non-null
10572 TRUE is returned if it is translated into the proper format for a call to the
10573 external library, and NEW_RETURN is set the tree for that function.
10574 FALSE is returned if processing for the _N variation is required, and
10575 NEW_RETURN is set to the the return value the result is copied into. */
10577 static bool
10578 resolve_overloaded_atomic_load (location_t loc, tree function,
10579 vec<tree, va_gc> *params, tree *new_return)
10581 tree p0, p1, p2;
10582 tree I_type, I_type_ptr;
10583 int n = get_atomic_generic_size (loc, function, params);
10585 /* Size of 0 is an error condition. */
10586 if (n == 0)
10588 *new_return = error_mark_node;
10589 return true;
10592 /* If not a lock-free size, change to the library generic format. */
10593 if (!atomic_size_supported_p (n))
10595 *new_return = add_atomic_size_parameter (n, loc, function, params);
10596 return true;
10599 /* Otherwise, there is a match, so the call needs to be transformed from:
10600 void fn(T* mem, T* return, model)
10601 into
10602 *return = (T) (fn ((In *) mem, model)) */
10604 p0 = (*params)[0];
10605 p1 = (*params)[1];
10606 p2 = (*params)[2];
10608 /* Create pointer to appropriate size. */
10609 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10610 I_type_ptr = build_pointer_type (I_type);
10612 /* Convert object pointer to required type. */
10613 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10614 (*params)[0] = p0;
10616 /* Move memory model to the 2nd position, and end param list. */
10617 (*params)[1] = p2;
10618 params->truncate (2);
10620 /* Convert return pointer and dereference it for later assignment. */
10621 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10623 return false;
10627 /* This will process an __atomic_store function call, determine whether it
10628 needs to be mapped to the _N variation, or turned into a library call.
10629 LOC is the location of the builtin call.
10630 FUNCTION is the DECL that has been invoked;
10631 PARAMS is the argument list for the call. The return value is non-null
10632 TRUE is returned if it is translated into the proper format for a call to the
10633 external library, and NEW_RETURN is set the tree for that function.
10634 FALSE is returned if processing for the _N variation is required, and
10635 NEW_RETURN is set to the the return value the result is copied into. */
10637 static bool
10638 resolve_overloaded_atomic_store (location_t loc, tree function,
10639 vec<tree, va_gc> *params, tree *new_return)
10641 tree p0, p1;
10642 tree I_type, I_type_ptr;
10643 int n = get_atomic_generic_size (loc, function, params);
10645 /* Size of 0 is an error condition. */
10646 if (n == 0)
10648 *new_return = error_mark_node;
10649 return true;
10652 /* If not a lock-free size, change to the library generic format. */
10653 if (!atomic_size_supported_p (n))
10655 *new_return = add_atomic_size_parameter (n, loc, function, params);
10656 return true;
10659 /* Otherwise, there is a match, so the call needs to be transformed from:
10660 void fn(T* mem, T* value, model)
10661 into
10662 fn ((In *) mem, (In) *value, model) */
10664 p0 = (*params)[0];
10665 p1 = (*params)[1];
10667 /* Create pointer to appropriate size. */
10668 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10669 I_type_ptr = build_pointer_type (I_type);
10671 /* Convert object pointer to required type. */
10672 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10673 (*params)[0] = p0;
10675 /* Convert new value to required type, and dereference it. */
10676 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10677 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10678 (*params)[1] = p1;
10680 /* The memory model is in the right spot already. Return is void. */
10681 *new_return = NULL_TREE;
10683 return false;
10687 /* Some builtin functions are placeholders for other expressions. This
10688 function should be called immediately after parsing the call expression
10689 before surrounding code has committed to the type of the expression.
10691 LOC is the location of the builtin call.
10693 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10694 PARAMS is the argument list for the call. The return value is non-null
10695 when expansion is complete, and null if normal processing should
10696 continue. */
10698 tree
10699 resolve_overloaded_builtin (location_t loc, tree function,
10700 vec<tree, va_gc> *params)
10702 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10703 bool orig_format = true;
10704 tree new_return = NULL_TREE;
10706 switch (DECL_BUILT_IN_CLASS (function))
10708 case BUILT_IN_NORMAL:
10709 break;
10710 case BUILT_IN_MD:
10711 if (targetm.resolve_overloaded_builtin)
10712 return targetm.resolve_overloaded_builtin (loc, function, params);
10713 else
10714 return NULL_TREE;
10715 default:
10716 return NULL_TREE;
10719 /* Handle BUILT_IN_NORMAL here. */
10720 switch (orig_code)
10722 case BUILT_IN_ATOMIC_EXCHANGE:
10723 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10724 case BUILT_IN_ATOMIC_LOAD:
10725 case BUILT_IN_ATOMIC_STORE:
10727 /* Handle these 4 together so that they can fall through to the next
10728 case if the call is transformed to an _N variant. */
10729 switch (orig_code)
10731 case BUILT_IN_ATOMIC_EXCHANGE:
10733 if (resolve_overloaded_atomic_exchange (loc, function, params,
10734 &new_return))
10735 return new_return;
10736 /* Change to the _N variant. */
10737 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10738 break;
10741 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10743 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10744 params,
10745 &new_return))
10746 return new_return;
10747 /* Change to the _N variant. */
10748 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10749 break;
10751 case BUILT_IN_ATOMIC_LOAD:
10753 if (resolve_overloaded_atomic_load (loc, function, params,
10754 &new_return))
10755 return new_return;
10756 /* Change to the _N variant. */
10757 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10758 break;
10760 case BUILT_IN_ATOMIC_STORE:
10762 if (resolve_overloaded_atomic_store (loc, function, params,
10763 &new_return))
10764 return new_return;
10765 /* Change to the _N variant. */
10766 orig_code = BUILT_IN_ATOMIC_STORE_N;
10767 break;
10769 default:
10770 gcc_unreachable ();
10772 /* Fallthrough to the normal processing. */
10774 case BUILT_IN_ATOMIC_EXCHANGE_N:
10775 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10776 case BUILT_IN_ATOMIC_LOAD_N:
10777 case BUILT_IN_ATOMIC_STORE_N:
10778 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10779 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10780 case BUILT_IN_ATOMIC_AND_FETCH_N:
10781 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10782 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10783 case BUILT_IN_ATOMIC_OR_FETCH_N:
10784 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10785 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10786 case BUILT_IN_ATOMIC_FETCH_AND_N:
10787 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10788 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10789 case BUILT_IN_ATOMIC_FETCH_OR_N:
10791 orig_format = false;
10792 /* Fallthru for parameter processing. */
10794 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10795 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10796 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10797 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10798 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10799 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10800 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10801 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10802 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10803 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10804 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10805 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10806 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10807 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10808 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10809 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10811 int n = sync_resolve_size (function, params);
10812 tree new_function, first_param, result;
10813 enum built_in_function fncode;
10815 if (n == 0)
10816 return error_mark_node;
10818 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10819 new_function = builtin_decl_explicit (fncode);
10820 if (!sync_resolve_params (loc, function, new_function, params,
10821 orig_format))
10822 return error_mark_node;
10824 first_param = (*params)[0];
10825 result = build_function_call_vec (loc, new_function, params, NULL);
10826 if (result == error_mark_node)
10827 return result;
10828 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10829 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10830 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10831 result = sync_resolve_return (first_param, result, orig_format);
10833 /* If new_return is set, assign function to that expr and cast the
10834 result to void since the generic interface returned void. */
10835 if (new_return)
10837 /* Cast function result from I{1,2,4,8,16} to the required type. */
10838 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10839 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10840 result);
10841 TREE_SIDE_EFFECTS (result) = 1;
10842 protected_set_expr_location (result, loc);
10843 result = convert (void_type_node, result);
10845 return result;
10848 default:
10849 return NULL_TREE;
10853 /* vector_types_compatible_elements_p is used in type checks of vectors
10854 values used as operands of binary operators. Where it returns true, and
10855 the other checks of the caller succeed (being vector types in he first
10856 place, and matching number of elements), we can just treat the types
10857 as essentially the same.
10858 Contrast with vector_targets_convertible_p, which is used for vector
10859 pointer types, and vector_types_convertible_p, which will allow
10860 language-specific matches under the control of flag_lax_vector_conversions,
10861 and might still require a conversion. */
10862 /* True if vector types T1 and T2 can be inputs to the same binary
10863 operator without conversion.
10864 We don't check the overall vector size here because some of our callers
10865 want to give different error messages when the vectors are compatible
10866 except for the element count. */
10868 bool
10869 vector_types_compatible_elements_p (tree t1, tree t2)
10871 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10872 t1 = TREE_TYPE (t1);
10873 t2 = TREE_TYPE (t2);
10875 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10877 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10878 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10879 || c2 == FIXED_POINT_TYPE));
10881 t1 = c_common_signed_type (t1);
10882 t2 = c_common_signed_type (t2);
10883 /* Equality works here because c_common_signed_type uses
10884 TYPE_MAIN_VARIANT. */
10885 if (t1 == t2)
10886 return true;
10887 if (opaque && c1 == c2
10888 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10889 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10890 return true;
10891 return false;
10894 /* Check for missing format attributes on function pointers. LTYPE is
10895 the new type or left-hand side type. RTYPE is the old type or
10896 right-hand side type. Returns TRUE if LTYPE is missing the desired
10897 attribute. */
10899 bool
10900 check_missing_format_attribute (tree ltype, tree rtype)
10902 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10903 tree ra;
10905 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10906 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10907 break;
10908 if (ra)
10910 tree la;
10911 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10912 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10913 break;
10914 return !la;
10916 else
10917 return false;
10920 /* Subscripting with type char is likely to lose on a machine where
10921 chars are signed. So warn on any machine, but optionally. Don't
10922 warn for unsigned char since that type is safe. Don't warn for
10923 signed char because anyone who uses that must have done so
10924 deliberately. Furthermore, we reduce the false positive load by
10925 warning only for non-constant value of type char. */
10927 void
10928 warn_array_subscript_with_type_char (tree index)
10930 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10931 && TREE_CODE (index) != INTEGER_CST)
10932 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10935 /* Implement -Wparentheses for the unexpected C precedence rules, to
10936 cover cases like x + y << z which readers are likely to
10937 misinterpret. We have seen an expression in which CODE is a binary
10938 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10939 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10940 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10941 expression was not formed using a binary or unary operator, or it
10942 was enclosed in parentheses. */
10944 void
10945 warn_about_parentheses (location_t loc, enum tree_code code,
10946 enum tree_code code_left, tree arg_left,
10947 enum tree_code code_right, tree arg_right)
10949 if (!warn_parentheses)
10950 return;
10952 /* This macro tests that the expression ARG with original tree code
10953 CODE appears to be a boolean expression. or the result of folding a
10954 boolean expression. */
10955 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10956 (truth_value_p (TREE_CODE (ARG)) \
10957 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10958 /* Folding may create 0 or 1 integers from other expressions. */ \
10959 || ((CODE) != INTEGER_CST \
10960 && (integer_onep (ARG) || integer_zerop (ARG))))
10962 switch (code)
10964 case LSHIFT_EXPR:
10965 if (code_left == PLUS_EXPR)
10966 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10967 "suggest parentheses around %<+%> inside %<<<%>");
10968 else if (code_right == PLUS_EXPR)
10969 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10970 "suggest parentheses around %<+%> inside %<<<%>");
10971 else if (code_left == MINUS_EXPR)
10972 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10973 "suggest parentheses around %<-%> inside %<<<%>");
10974 else if (code_right == MINUS_EXPR)
10975 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10976 "suggest parentheses around %<-%> inside %<<<%>");
10977 return;
10979 case RSHIFT_EXPR:
10980 if (code_left == PLUS_EXPR)
10981 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10982 "suggest parentheses around %<+%> inside %<>>%>");
10983 else if (code_right == PLUS_EXPR)
10984 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10985 "suggest parentheses around %<+%> inside %<>>%>");
10986 else if (code_left == MINUS_EXPR)
10987 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10988 "suggest parentheses around %<-%> inside %<>>%>");
10989 else if (code_right == MINUS_EXPR)
10990 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10991 "suggest parentheses around %<-%> inside %<>>%>");
10992 return;
10994 case TRUTH_ORIF_EXPR:
10995 if (code_left == TRUTH_ANDIF_EXPR)
10996 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10997 "suggest parentheses around %<&&%> within %<||%>");
10998 else if (code_right == TRUTH_ANDIF_EXPR)
10999 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11000 "suggest parentheses around %<&&%> within %<||%>");
11001 return;
11003 case BIT_IOR_EXPR:
11004 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11005 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11006 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11007 "suggest parentheses around arithmetic in operand of %<|%>");
11008 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11009 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11010 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11011 "suggest parentheses around arithmetic in operand of %<|%>");
11012 /* Check cases like x|y==z */
11013 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11014 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11015 "suggest parentheses around comparison in operand of %<|%>");
11016 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11017 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11018 "suggest parentheses around comparison in operand of %<|%>");
11019 /* Check cases like !x | y */
11020 else if (code_left == TRUTH_NOT_EXPR
11021 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11022 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11023 "suggest parentheses around operand of "
11024 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11025 return;
11027 case BIT_XOR_EXPR:
11028 if (code_left == BIT_AND_EXPR
11029 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11030 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11031 "suggest parentheses around arithmetic in operand of %<^%>");
11032 else if (code_right == BIT_AND_EXPR
11033 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11034 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11035 "suggest parentheses around arithmetic in operand of %<^%>");
11036 /* Check cases like x^y==z */
11037 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11038 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11039 "suggest parentheses around comparison in operand of %<^%>");
11040 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11041 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11042 "suggest parentheses around comparison in operand of %<^%>");
11043 return;
11045 case BIT_AND_EXPR:
11046 if (code_left == PLUS_EXPR)
11047 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11048 "suggest parentheses around %<+%> in operand of %<&%>");
11049 else if (code_right == PLUS_EXPR)
11050 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11051 "suggest parentheses around %<+%> in operand of %<&%>");
11052 else if (code_left == MINUS_EXPR)
11053 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11054 "suggest parentheses around %<-%> in operand of %<&%>");
11055 else if (code_right == MINUS_EXPR)
11056 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11057 "suggest parentheses around %<-%> in operand of %<&%>");
11058 /* Check cases like x&y==z */
11059 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11060 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11061 "suggest parentheses around comparison in operand of %<&%>");
11062 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11063 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11064 "suggest parentheses around comparison in operand of %<&%>");
11065 /* Check cases like !x & y */
11066 else if (code_left == TRUTH_NOT_EXPR
11067 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11068 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11069 "suggest parentheses around operand of "
11070 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11071 return;
11073 case EQ_EXPR:
11074 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11075 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11076 "suggest parentheses around comparison in operand of %<==%>");
11077 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11078 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11079 "suggest parentheses around comparison in operand of %<==%>");
11080 return;
11081 case NE_EXPR:
11082 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11083 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11084 "suggest parentheses around comparison in operand of %<!=%>");
11085 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11086 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11087 "suggest parentheses around comparison in operand of %<!=%>");
11088 return;
11090 default:
11091 if (TREE_CODE_CLASS (code) == tcc_comparison)
11093 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11094 && code_left != NE_EXPR && code_left != EQ_EXPR
11095 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11096 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11097 "comparisons like %<X<=Y<=Z%> do not "
11098 "have their mathematical meaning");
11099 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11100 && code_right != NE_EXPR && code_right != EQ_EXPR
11101 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11102 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11103 "comparisons like %<X<=Y<=Z%> do not "
11104 "have their mathematical meaning");
11106 return;
11108 #undef NOT_A_BOOLEAN_EXPR_P
11111 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11113 void
11114 warn_for_unused_label (tree label)
11116 if (!TREE_USED (label))
11118 if (DECL_INITIAL (label))
11119 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11120 else
11121 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11125 /* Warn for division by zero according to the value of DIVISOR. LOC
11126 is the location of the division operator. */
11128 void
11129 warn_for_div_by_zero (location_t loc, tree divisor)
11131 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11132 about division by zero. Do not issue a warning if DIVISOR has a
11133 floating-point type, since we consider 0.0/0.0 a valid way of
11134 generating a NaN. */
11135 if (c_inhibit_evaluation_warnings == 0
11136 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11137 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11140 /* Subroutine of build_binary_op. Give warnings for comparisons
11141 between signed and unsigned quantities that may fail. Do the
11142 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11143 so that casts will be considered, but default promotions won't
11146 LOCATION is the location of the comparison operator.
11148 The arguments of this function map directly to local variables
11149 of build_binary_op. */
11151 void
11152 warn_for_sign_compare (location_t location,
11153 tree orig_op0, tree orig_op1,
11154 tree op0, tree op1,
11155 tree result_type, enum tree_code resultcode)
11157 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11158 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11159 int unsignedp0, unsignedp1;
11161 /* In C++, check for comparison of different enum types. */
11162 if (c_dialect_cxx()
11163 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11164 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11165 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11166 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11168 warning_at (location,
11169 OPT_Wsign_compare, "comparison between types %qT and %qT",
11170 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11173 /* Do not warn if the comparison is being done in a signed type,
11174 since the signed type will only be chosen if it can represent
11175 all the values of the unsigned type. */
11176 if (!TYPE_UNSIGNED (result_type))
11177 /* OK */;
11178 /* Do not warn if both operands are unsigned. */
11179 else if (op0_signed == op1_signed)
11180 /* OK */;
11181 else
11183 tree sop, uop, base_type;
11184 bool ovf;
11186 if (op0_signed)
11187 sop = orig_op0, uop = orig_op1;
11188 else
11189 sop = orig_op1, uop = orig_op0;
11191 STRIP_TYPE_NOPS (sop);
11192 STRIP_TYPE_NOPS (uop);
11193 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11194 ? TREE_TYPE (result_type) : result_type);
11196 /* Do not warn if the signed quantity is an unsuffixed integer
11197 literal (or some static constant expression involving such
11198 literals or a conditional expression involving such literals)
11199 and it is non-negative. */
11200 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11201 /* OK */;
11202 /* Do not warn if the comparison is an equality operation, the
11203 unsigned quantity is an integral constant, and it would fit
11204 in the result if the result were signed. */
11205 else if (TREE_CODE (uop) == INTEGER_CST
11206 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11207 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11208 /* OK */;
11209 /* In C, do not warn if the unsigned quantity is an enumeration
11210 constant and its maximum value would fit in the result if the
11211 result were signed. */
11212 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11213 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11214 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11215 c_common_signed_type (base_type)))
11216 /* OK */;
11217 else
11218 warning_at (location,
11219 OPT_Wsign_compare,
11220 "comparison between signed and unsigned integer expressions");
11223 /* Warn if two unsigned values are being compared in a size larger
11224 than their original size, and one (and only one) is the result of
11225 a `~' operator. This comparison will always fail.
11227 Also warn if one operand is a constant, and the constant does not
11228 have all bits set that are set in the ~ operand when it is
11229 extended. */
11231 op0 = c_common_get_narrower (op0, &unsignedp0);
11232 op1 = c_common_get_narrower (op1, &unsignedp1);
11234 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11235 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11237 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11238 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11239 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11240 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11242 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11244 tree primop;
11245 HOST_WIDE_INT constant, mask;
11246 int unsignedp;
11247 unsigned int bits;
11249 if (host_integerp (op0, 0))
11251 primop = op1;
11252 unsignedp = unsignedp1;
11253 constant = tree_low_cst (op0, 0);
11255 else
11257 primop = op0;
11258 unsignedp = unsignedp0;
11259 constant = tree_low_cst (op1, 0);
11262 bits = TYPE_PRECISION (TREE_TYPE (primop));
11263 if (bits < TYPE_PRECISION (result_type)
11264 && bits < HOST_BITS_PER_LONG && unsignedp)
11266 mask = (~ (HOST_WIDE_INT) 0) << bits;
11267 if ((mask & constant) != mask)
11269 if (constant == 0)
11270 warning (OPT_Wsign_compare,
11271 "promoted ~unsigned is always non-zero");
11272 else
11273 warning_at (location, OPT_Wsign_compare,
11274 "comparison of promoted ~unsigned with constant");
11278 else if (unsignedp0 && unsignedp1
11279 && (TYPE_PRECISION (TREE_TYPE (op0))
11280 < TYPE_PRECISION (result_type))
11281 && (TYPE_PRECISION (TREE_TYPE (op1))
11282 < TYPE_PRECISION (result_type)))
11283 warning_at (location, OPT_Wsign_compare,
11284 "comparison of promoted ~unsigned with unsigned");
11288 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11289 type via c_common_type. If -Wdouble-promotion is in use, and the
11290 conditions for warning have been met, issue a warning. GMSGID is
11291 the warning message. It must have two %T specifiers for the type
11292 that was converted (generally "float") and the type to which it was
11293 converted (generally "double), respectively. LOC is the location
11294 to which the awrning should refer. */
11296 void
11297 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11298 const char *gmsgid, location_t loc)
11300 tree source_type;
11302 if (!warn_double_promotion)
11303 return;
11304 /* If the conversion will not occur at run-time, there is no need to
11305 warn about it. */
11306 if (c_inhibit_evaluation_warnings)
11307 return;
11308 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11309 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11310 return;
11311 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11312 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11313 source_type = type1;
11314 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11315 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11316 source_type = type2;
11317 else
11318 return;
11319 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11322 /* Setup a TYPE_DECL node as a typedef representation.
11324 X is a TYPE_DECL for a typedef statement. Create a brand new
11325 ..._TYPE node (which will be just a variant of the existing
11326 ..._TYPE node with identical properties) and then install X
11327 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11329 The whole point here is to end up with a situation where each
11330 and every ..._TYPE node the compiler creates will be uniquely
11331 associated with AT MOST one node representing a typedef name.
11332 This way, even though the compiler substitutes corresponding
11333 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11334 early on, later parts of the compiler can always do the reverse
11335 translation and get back the corresponding typedef name. For
11336 example, given:
11338 typedef struct S MY_TYPE;
11339 MY_TYPE object;
11341 Later parts of the compiler might only know that `object' was of
11342 type `struct S' if it were not for code just below. With this
11343 code however, later parts of the compiler see something like:
11345 struct S' == struct S
11346 typedef struct S' MY_TYPE;
11347 struct S' object;
11349 And they can then deduce (from the node for type struct S') that
11350 the original object declaration was:
11352 MY_TYPE object;
11354 Being able to do this is important for proper support of protoize,
11355 and also for generating precise symbolic debugging information
11356 which takes full account of the programmer's (typedef) vocabulary.
11358 Obviously, we don't want to generate a duplicate ..._TYPE node if
11359 the TYPE_DECL node that we are now processing really represents a
11360 standard built-in type. */
11362 void
11363 set_underlying_type (tree x)
11365 if (x == error_mark_node)
11366 return;
11367 if (DECL_IS_BUILTIN (x))
11369 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11370 TYPE_NAME (TREE_TYPE (x)) = x;
11372 else if (TREE_TYPE (x) != error_mark_node
11373 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11375 tree tt = TREE_TYPE (x);
11376 DECL_ORIGINAL_TYPE (x) = tt;
11377 tt = build_variant_type_copy (tt);
11378 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11379 TYPE_NAME (tt) = x;
11380 TREE_USED (tt) = TREE_USED (x);
11381 TREE_TYPE (x) = tt;
11385 /* Record the types used by the current global variable declaration
11386 being parsed, so that we can decide later to emit their debug info.
11387 Those types are in types_used_by_cur_var_decl, and we are going to
11388 store them in the types_used_by_vars_hash hash table.
11389 DECL is the declaration of the global variable that has been parsed. */
11391 void
11392 record_types_used_by_current_var_decl (tree decl)
11394 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11396 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11398 tree type = types_used_by_cur_var_decl->pop ();
11399 types_used_by_var_decl_insert (type, decl);
11403 /* If DECL is a typedef that is declared in the current function,
11404 record it for the purpose of -Wunused-local-typedefs. */
11406 void
11407 record_locally_defined_typedef (tree decl)
11409 struct c_language_function *l;
11411 if (!warn_unused_local_typedefs
11412 || cfun == NULL
11413 /* if this is not a locally defined typedef then we are not
11414 interested. */
11415 || !is_typedef_decl (decl)
11416 || !decl_function_context (decl))
11417 return;
11419 l = (struct c_language_function *) cfun->language;
11420 vec_safe_push (l->local_typedefs, decl);
11423 /* If T is a TYPE_DECL declared locally, mark it as used. */
11425 void
11426 maybe_record_typedef_use (tree t)
11428 if (!is_typedef_decl (t))
11429 return;
11431 TREE_USED (t) = true;
11434 /* Warn if there are some unused locally defined typedefs in the
11435 current function. */
11437 void
11438 maybe_warn_unused_local_typedefs (void)
11440 int i;
11441 tree decl;
11442 /* The number of times we have emitted -Wunused-local-typedefs
11443 warnings. If this is different from errorcount, that means some
11444 unrelated errors have been issued. In which case, we'll avoid
11445 emitting "unused-local-typedefs" warnings. */
11446 static int unused_local_typedefs_warn_count;
11447 struct c_language_function *l;
11449 if (cfun == NULL)
11450 return;
11452 if ((l = (struct c_language_function *) cfun->language) == NULL)
11453 return;
11455 if (warn_unused_local_typedefs
11456 && errorcount == unused_local_typedefs_warn_count)
11458 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11459 if (!TREE_USED (decl))
11460 warning_at (DECL_SOURCE_LOCATION (decl),
11461 OPT_Wunused_local_typedefs,
11462 "typedef %qD locally defined but not used", decl);
11463 unused_local_typedefs_warn_count = errorcount;
11466 vec_free (l->local_typedefs);
11469 /* The C and C++ parsers both use vectors to hold function arguments.
11470 For efficiency, we keep a cache of unused vectors. This is the
11471 cache. */
11473 typedef vec<tree, va_gc> *tree_gc_vec;
11474 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11476 /* Return a new vector from the cache. If the cache is empty,
11477 allocate a new vector. These vectors are GC'ed, so it is OK if the
11478 pointer is not released.. */
11480 vec<tree, va_gc> *
11481 make_tree_vector (void)
11483 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11484 return tree_vector_cache->pop ();
11485 else
11487 /* Passing 0 to vec::alloc returns NULL, and our callers require
11488 that we always return a non-NULL value. The vector code uses
11489 4 when growing a NULL vector, so we do too. */
11490 vec<tree, va_gc> *v;
11491 vec_alloc (v, 4);
11492 return v;
11496 /* Release a vector of trees back to the cache. */
11498 void
11499 release_tree_vector (vec<tree, va_gc> *vec)
11501 if (vec != NULL)
11503 vec->truncate (0);
11504 vec_safe_push (tree_vector_cache, vec);
11508 /* Get a new tree vector holding a single tree. */
11510 vec<tree, va_gc> *
11511 make_tree_vector_single (tree t)
11513 vec<tree, va_gc> *ret = make_tree_vector ();
11514 ret->quick_push (t);
11515 return ret;
11518 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11520 vec<tree, va_gc> *
11521 make_tree_vector_from_list (tree list)
11523 vec<tree, va_gc> *ret = make_tree_vector ();
11524 for (; list; list = TREE_CHAIN (list))
11525 vec_safe_push (ret, TREE_VALUE (list));
11526 return ret;
11529 /* Get a new tree vector which is a copy of an existing one. */
11531 vec<tree, va_gc> *
11532 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11534 vec<tree, va_gc> *ret;
11535 unsigned int ix;
11536 tree t;
11538 ret = make_tree_vector ();
11539 vec_safe_reserve (ret, vec_safe_length (orig));
11540 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11541 ret->quick_push (t);
11542 return ret;
11545 /* Return true if KEYWORD starts a type specifier. */
11547 bool
11548 keyword_begins_type_specifier (enum rid keyword)
11550 switch (keyword)
11552 case RID_INT:
11553 case RID_CHAR:
11554 case RID_FLOAT:
11555 case RID_DOUBLE:
11556 case RID_VOID:
11557 case RID_INT128:
11558 case RID_UNSIGNED:
11559 case RID_LONG:
11560 case RID_SHORT:
11561 case RID_SIGNED:
11562 case RID_DFLOAT32:
11563 case RID_DFLOAT64:
11564 case RID_DFLOAT128:
11565 case RID_FRACT:
11566 case RID_ACCUM:
11567 case RID_BOOL:
11568 case RID_WCHAR:
11569 case RID_CHAR16:
11570 case RID_CHAR32:
11571 case RID_SAT:
11572 case RID_COMPLEX:
11573 case RID_TYPEOF:
11574 case RID_STRUCT:
11575 case RID_CLASS:
11576 case RID_UNION:
11577 case RID_ENUM:
11578 return true;
11579 default:
11580 return false;
11584 /* Return true if KEYWORD names a type qualifier. */
11586 bool
11587 keyword_is_type_qualifier (enum rid keyword)
11589 switch (keyword)
11591 case RID_CONST:
11592 case RID_VOLATILE:
11593 case RID_RESTRICT:
11594 case RID_ATOMIC:
11595 return true;
11596 default:
11597 return false;
11601 /* Return true if KEYWORD names a storage class specifier.
11603 RID_TYPEDEF is not included in this list despite `typedef' being
11604 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11605 such for syntactic convenience only. */
11607 bool
11608 keyword_is_storage_class_specifier (enum rid keyword)
11610 switch (keyword)
11612 case RID_STATIC:
11613 case RID_EXTERN:
11614 case RID_REGISTER:
11615 case RID_AUTO:
11616 case RID_MUTABLE:
11617 case RID_THREAD:
11618 return true;
11619 default:
11620 return false;
11624 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11626 static bool
11627 keyword_is_function_specifier (enum rid keyword)
11629 switch (keyword)
11631 case RID_INLINE:
11632 case RID_NORETURN:
11633 case RID_VIRTUAL:
11634 case RID_EXPLICIT:
11635 return true;
11636 default:
11637 return false;
11641 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11642 declaration-specifier (C99 6.7). */
11644 bool
11645 keyword_is_decl_specifier (enum rid keyword)
11647 if (keyword_is_storage_class_specifier (keyword)
11648 || keyword_is_type_qualifier (keyword)
11649 || keyword_is_function_specifier (keyword))
11650 return true;
11652 switch (keyword)
11654 case RID_TYPEDEF:
11655 case RID_FRIEND:
11656 case RID_CONSTEXPR:
11657 return true;
11658 default:
11659 return false;
11663 /* Initialize language-specific-bits of tree_contains_struct. */
11665 void
11666 c_common_init_ts (void)
11668 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11669 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11670 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11673 /* Build a user-defined numeric literal out of an integer constant type VALUE
11674 with identifier SUFFIX. */
11676 tree
11677 build_userdef_literal (tree suffix_id, tree value,
11678 enum overflow_type overflow, tree num_string)
11680 tree literal = make_node (USERDEF_LITERAL);
11681 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11682 USERDEF_LITERAL_VALUE (literal) = value;
11683 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11684 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11685 return literal;
11688 /* For vector[index], convert the vector to a
11689 pointer of the underlying type. */
11690 void
11691 convert_vector_to_pointer_for_subscript (location_t loc,
11692 tree* vecp, tree index)
11694 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11696 tree type = TREE_TYPE (*vecp);
11697 tree type1;
11699 if (TREE_CODE (index) == INTEGER_CST)
11700 if (!host_integerp (index, 1)
11701 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11702 >= TYPE_VECTOR_SUBPARTS (type)))
11703 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11705 c_common_mark_addressable_vec (*vecp);
11706 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11707 type = build_pointer_type (type);
11708 type1 = build_pointer_type (TREE_TYPE (*vecp));
11709 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11710 *vecp = convert (type, *vecp);
11714 /* Determine which of the operands, if any, is a scalar that needs to be
11715 converted to a vector, for the range of operations. */
11716 enum stv_conv
11717 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11718 bool complain)
11720 tree type0 = TREE_TYPE (op0);
11721 tree type1 = TREE_TYPE (op1);
11722 bool integer_only_op = false;
11723 enum stv_conv ret = stv_firstarg;
11725 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11726 || TREE_CODE (type1) == VECTOR_TYPE);
11727 switch (code)
11729 /* Most GENERIC binary expressions require homogeneous arguments.
11730 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11731 argument that is a vector and a second one that is a scalar, so
11732 we never return stv_secondarg for them. */
11733 case RSHIFT_EXPR:
11734 case LSHIFT_EXPR:
11735 if (TREE_CODE (type0) == INTEGER_TYPE
11736 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11738 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11740 if (complain)
11741 error_at (loc, "conversion of scalar %qT to vector %qT "
11742 "involves truncation", type0, type1);
11743 return stv_error;
11745 else
11746 return stv_firstarg;
11748 break;
11750 case BIT_IOR_EXPR:
11751 case BIT_XOR_EXPR:
11752 case BIT_AND_EXPR:
11753 integer_only_op = true;
11754 /* ... fall through ... */
11756 case VEC_COND_EXPR:
11758 case PLUS_EXPR:
11759 case MINUS_EXPR:
11760 case MULT_EXPR:
11761 case TRUNC_DIV_EXPR:
11762 case CEIL_DIV_EXPR:
11763 case FLOOR_DIV_EXPR:
11764 case ROUND_DIV_EXPR:
11765 case EXACT_DIV_EXPR:
11766 case TRUNC_MOD_EXPR:
11767 case FLOOR_MOD_EXPR:
11768 case RDIV_EXPR:
11769 case EQ_EXPR:
11770 case NE_EXPR:
11771 case LE_EXPR:
11772 case GE_EXPR:
11773 case LT_EXPR:
11774 case GT_EXPR:
11775 /* What about UNLT_EXPR? */
11776 if (TREE_CODE (type0) == VECTOR_TYPE)
11778 tree tmp;
11779 ret = stv_secondarg;
11780 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11781 tmp = type0; type0 = type1; type1 = tmp;
11782 tmp = op0; op0 = op1; op1 = tmp;
11785 if (TREE_CODE (type0) == INTEGER_TYPE
11786 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11788 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11790 if (complain)
11791 error_at (loc, "conversion of scalar %qT to vector %qT "
11792 "involves truncation", type0, type1);
11793 return stv_error;
11795 return ret;
11797 else if (!integer_only_op
11798 /* Allow integer --> real conversion if safe. */
11799 && (TREE_CODE (type0) == REAL_TYPE
11800 || TREE_CODE (type0) == INTEGER_TYPE)
11801 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11803 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11805 if (complain)
11806 error_at (loc, "conversion of scalar %qT to vector %qT "
11807 "involves truncation", type0, type1);
11808 return stv_error;
11810 return ret;
11812 default:
11813 break;
11816 return stv_nothing;
11819 /* Return true iff ALIGN is an integral constant that is a fundamental
11820 alignment, as defined by [basic.align] in the c++-11
11821 specifications.
11823 That is:
11825 [A fundamental alignment is represented by an alignment less than or
11826 equal to the greatest alignment supported by the implementation
11827 in all contexts, which is equal to
11828 alignof(max_align_t)]. */
11830 bool
11831 cxx_fundamental_alignment_p (unsigned align)
11833 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11834 TYPE_ALIGN (long_double_type_node)));
11837 #include "gt-c-family-c-common.h"