gcc-defs.exp (dg-additional-files-options): Extend regsub for dg-additional-files...
[official-gcc.git] / gcc / c-family / c-common.c
blob030ee5a643d365cb3fb4a9be76f81122e132a0a9
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 { "_Bool", RID_BOOL, D_CONLY },
413 { "_Complex", RID_COMPLEX, 0 },
414 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
415 { "_Cilk_sync", RID_CILK_SYNC, 0 },
416 { "_Imaginary", RID_IMAGINARY, D_CONLY },
417 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
418 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
419 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
420 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
421 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
422 { "_Sat", RID_SAT, D_CONLY | D_EXT },
423 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
424 { "_Noreturn", RID_NORETURN, D_CONLY },
425 { "_Generic", RID_GENERIC, D_CONLY },
426 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
427 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
428 { "__alignof", RID_ALIGNOF, 0 },
429 { "__alignof__", RID_ALIGNOF, 0 },
430 { "__asm", RID_ASM, 0 },
431 { "__asm__", RID_ASM, 0 },
432 { "__attribute", RID_ATTRIBUTE, 0 },
433 { "__attribute__", RID_ATTRIBUTE, 0 },
434 { "__bases", RID_BASES, D_CXXONLY },
435 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
436 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
437 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
438 { "__builtin_offsetof", RID_OFFSETOF, 0 },
439 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
440 { "__builtin_va_arg", RID_VA_ARG, 0 },
441 { "__complex", RID_COMPLEX, 0 },
442 { "__complex__", RID_COMPLEX, 0 },
443 { "__const", RID_CONST, 0 },
444 { "__const__", RID_CONST, 0 },
445 { "__decltype", RID_DECLTYPE, D_CXXONLY },
446 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
447 { "__extension__", RID_EXTENSION, 0 },
448 { "__func__", RID_C99_FUNCTION_NAME, 0 },
449 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
450 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
451 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
452 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
453 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
454 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
455 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
456 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
457 { "__imag", RID_IMAGPART, 0 },
458 { "__imag__", RID_IMAGPART, 0 },
459 { "__inline", RID_INLINE, 0 },
460 { "__inline__", RID_INLINE, 0 },
461 { "__int128", RID_INT128, 0 },
462 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
463 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
464 { "__is_class", RID_IS_CLASS, D_CXXONLY },
465 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
466 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
467 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
468 { "__is_final", RID_IS_FINAL, D_CXXONLY },
469 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
470 { "__is_pod", RID_IS_POD, D_CXXONLY },
471 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
472 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
473 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
474 { "__is_union", RID_IS_UNION, D_CXXONLY },
475 { "__label__", RID_LABEL, 0 },
476 { "__null", RID_NULL, 0 },
477 { "__real", RID_REALPART, 0 },
478 { "__real__", RID_REALPART, 0 },
479 { "__restrict", RID_RESTRICT, 0 },
480 { "__restrict__", RID_RESTRICT, 0 },
481 { "__signed", RID_SIGNED, 0 },
482 { "__signed__", RID_SIGNED, 0 },
483 { "__thread", RID_THREAD, 0 },
484 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
485 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
486 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
487 { "__typeof", RID_TYPEOF, 0 },
488 { "__typeof__", RID_TYPEOF, 0 },
489 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
490 { "__volatile", RID_VOLATILE, 0 },
491 { "__volatile__", RID_VOLATILE, 0 },
492 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
493 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
494 { "asm", RID_ASM, D_ASM },
495 { "auto", RID_AUTO, 0 },
496 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
497 { "break", RID_BREAK, 0 },
498 { "case", RID_CASE, 0 },
499 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
500 { "char", RID_CHAR, 0 },
501 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
502 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
503 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
504 { "const", RID_CONST, 0 },
505 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
506 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
507 { "continue", RID_CONTINUE, 0 },
508 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
509 { "default", RID_DEFAULT, 0 },
510 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
511 { "do", RID_DO, 0 },
512 { "double", RID_DOUBLE, 0 },
513 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
514 { "else", RID_ELSE, 0 },
515 { "enum", RID_ENUM, 0 },
516 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
517 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
518 { "extern", RID_EXTERN, 0 },
519 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
520 { "float", RID_FLOAT, 0 },
521 { "for", RID_FOR, 0 },
522 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
523 { "goto", RID_GOTO, 0 },
524 { "if", RID_IF, 0 },
525 { "inline", RID_INLINE, D_EXT89 },
526 { "int", RID_INT, 0 },
527 { "long", RID_LONG, 0 },
528 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
529 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
530 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
531 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
532 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
533 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
534 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
535 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
536 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
537 { "register", RID_REGISTER, 0 },
538 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
539 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
540 { "return", RID_RETURN, 0 },
541 { "short", RID_SHORT, 0 },
542 { "signed", RID_SIGNED, 0 },
543 { "sizeof", RID_SIZEOF, 0 },
544 { "static", RID_STATIC, 0 },
545 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
546 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
547 { "struct", RID_STRUCT, 0 },
548 { "switch", RID_SWITCH, 0 },
549 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
550 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
551 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
552 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
553 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
554 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
555 { "typedef", RID_TYPEDEF, 0 },
556 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
557 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
558 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
559 { "union", RID_UNION, 0 },
560 { "unsigned", RID_UNSIGNED, 0 },
561 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
562 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
563 { "void", RID_VOID, 0 },
564 { "volatile", RID_VOLATILE, 0 },
565 { "wchar_t", RID_WCHAR, D_CXXONLY },
566 { "while", RID_WHILE, 0 },
567 /* These Objective-C keywords are recognized only immediately after
568 an '@'. */
569 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
570 { "defs", RID_AT_DEFS, D_OBJC },
571 { "encode", RID_AT_ENCODE, D_OBJC },
572 { "end", RID_AT_END, D_OBJC },
573 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
574 { "interface", RID_AT_INTERFACE, D_OBJC },
575 { "protocol", RID_AT_PROTOCOL, D_OBJC },
576 { "selector", RID_AT_SELECTOR, D_OBJC },
577 { "finally", RID_AT_FINALLY, D_OBJC },
578 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
579 { "optional", RID_AT_OPTIONAL, D_OBJC },
580 { "required", RID_AT_REQUIRED, D_OBJC },
581 { "property", RID_AT_PROPERTY, D_OBJC },
582 { "package", RID_AT_PACKAGE, D_OBJC },
583 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
584 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
585 /* These are recognized only in protocol-qualifier context
586 (see above) */
587 { "bycopy", RID_BYCOPY, D_OBJC },
588 { "byref", RID_BYREF, D_OBJC },
589 { "in", RID_IN, D_OBJC },
590 { "inout", RID_INOUT, D_OBJC },
591 { "oneway", RID_ONEWAY, D_OBJC },
592 { "out", RID_OUT, D_OBJC },
593 /* These are recognized inside a property attribute list */
594 { "assign", RID_ASSIGN, D_OBJC },
595 { "copy", RID_COPY, D_OBJC },
596 { "getter", RID_GETTER, D_OBJC },
597 { "nonatomic", RID_NONATOMIC, D_OBJC },
598 { "readonly", RID_READONLY, D_OBJC },
599 { "readwrite", RID_READWRITE, D_OBJC },
600 { "retain", RID_RETAIN, D_OBJC },
601 { "setter", RID_SETTER, D_OBJC },
604 const unsigned int num_c_common_reswords =
605 sizeof c_common_reswords / sizeof (struct c_common_resword);
607 /* Table of machine-independent attributes common to all C-like languages. */
608 const struct attribute_spec c_common_attribute_table[] =
610 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
611 affects_type_identity } */
612 { "packed", 0, 0, false, false, false,
613 handle_packed_attribute , false},
614 { "nocommon", 0, 0, true, false, false,
615 handle_nocommon_attribute, false},
616 { "common", 0, 0, true, false, false,
617 handle_common_attribute, false },
618 /* FIXME: logically, noreturn attributes should be listed as
619 "false, true, true" and apply to function types. But implementing this
620 would require all the places in the compiler that use TREE_THIS_VOLATILE
621 on a decl to identify non-returning functions to be located and fixed
622 to check the function type instead. */
623 { "noreturn", 0, 0, true, false, false,
624 handle_noreturn_attribute, false },
625 { "volatile", 0, 0, true, false, false,
626 handle_noreturn_attribute, false },
627 { "noinline", 0, 0, true, false, false,
628 handle_noinline_attribute, false },
629 { "noclone", 0, 0, true, false, false,
630 handle_noclone_attribute, false },
631 { "leaf", 0, 0, true, false, false,
632 handle_leaf_attribute, false },
633 { "always_inline", 0, 0, true, false, false,
634 handle_always_inline_attribute, false },
635 { "gnu_inline", 0, 0, true, false, false,
636 handle_gnu_inline_attribute, false },
637 { "artificial", 0, 0, true, false, false,
638 handle_artificial_attribute, false },
639 { "flatten", 0, 0, true, false, false,
640 handle_flatten_attribute, false },
641 { "used", 0, 0, true, false, false,
642 handle_used_attribute, false },
643 { "unused", 0, 0, false, false, false,
644 handle_unused_attribute, false },
645 { "externally_visible", 0, 0, true, false, false,
646 handle_externally_visible_attribute, false },
647 /* The same comments as for noreturn attributes apply to const ones. */
648 { "const", 0, 0, true, false, false,
649 handle_const_attribute, false },
650 { "transparent_union", 0, 0, false, false, false,
651 handle_transparent_union_attribute, false },
652 { "constructor", 0, 1, true, false, false,
653 handle_constructor_attribute, false },
654 { "destructor", 0, 1, true, false, false,
655 handle_destructor_attribute, false },
656 { "mode", 1, 1, false, true, false,
657 handle_mode_attribute, false },
658 { "section", 1, 1, true, false, false,
659 handle_section_attribute, false },
660 { "aligned", 0, 1, false, false, false,
661 handle_aligned_attribute, false },
662 { "weak", 0, 0, true, false, false,
663 handle_weak_attribute, false },
664 { "ifunc", 1, 1, true, false, false,
665 handle_ifunc_attribute, false },
666 { "alias", 1, 1, true, false, false,
667 handle_alias_attribute, false },
668 { "weakref", 0, 1, true, false, false,
669 handle_weakref_attribute, false },
670 { "no_instrument_function", 0, 0, true, false, false,
671 handle_no_instrument_function_attribute,
672 false },
673 { "malloc", 0, 0, true, false, false,
674 handle_malloc_attribute, false },
675 { "returns_twice", 0, 0, true, false, false,
676 handle_returns_twice_attribute, false },
677 { "no_stack_limit", 0, 0, true, false, false,
678 handle_no_limit_stack_attribute, false },
679 { "pure", 0, 0, true, false, false,
680 handle_pure_attribute, false },
681 { "transaction_callable", 0, 0, false, true, false,
682 handle_tm_attribute, false },
683 { "transaction_unsafe", 0, 0, false, true, false,
684 handle_tm_attribute, false },
685 { "transaction_safe", 0, 0, false, true, false,
686 handle_tm_attribute, false },
687 { "transaction_may_cancel_outer", 0, 0, false, true, false,
688 handle_tm_attribute, false },
689 /* ??? These two attributes didn't make the transition from the
690 Intel language document to the multi-vendor language document. */
691 { "transaction_pure", 0, 0, false, true, false,
692 handle_tm_attribute, false },
693 { "transaction_wrap", 1, 1, true, false, false,
694 handle_tm_wrap_attribute, false },
695 /* For internal use (marking of builtins) only. The name contains space
696 to prevent its usage in source code. */
697 { "no vops", 0, 0, true, false, false,
698 handle_novops_attribute, false },
699 { "deprecated", 0, 1, false, false, false,
700 handle_deprecated_attribute, false },
701 { "vector_size", 1, 1, false, true, false,
702 handle_vector_size_attribute, false },
703 { "visibility", 1, 1, false, false, false,
704 handle_visibility_attribute, false },
705 { "tls_model", 1, 1, true, false, false,
706 handle_tls_model_attribute, false },
707 { "nonnull", 0, -1, false, true, true,
708 handle_nonnull_attribute, false },
709 { "nothrow", 0, 0, true, false, false,
710 handle_nothrow_attribute, false },
711 { "may_alias", 0, 0, false, true, false, NULL, false },
712 { "cleanup", 1, 1, true, false, false,
713 handle_cleanup_attribute, false },
714 { "warn_unused_result", 0, 0, false, true, true,
715 handle_warn_unused_result_attribute, false },
716 { "sentinel", 0, 1, false, true, true,
717 handle_sentinel_attribute, false },
718 /* For internal use (marking of builtins) only. The name contains space
719 to prevent its usage in source code. */
720 { "type generic", 0, 0, false, true, true,
721 handle_type_generic_attribute, false },
722 { "alloc_size", 1, 2, false, true, true,
723 handle_alloc_size_attribute, false },
724 { "cold", 0, 0, true, false, false,
725 handle_cold_attribute, false },
726 { "hot", 0, 0, true, false, false,
727 handle_hot_attribute, false },
728 { "no_address_safety_analysis",
729 0, 0, true, false, false,
730 handle_no_address_safety_analysis_attribute,
731 false },
732 { "no_sanitize_address", 0, 0, true, false, false,
733 handle_no_sanitize_address_attribute,
734 false },
735 { "no_sanitize_undefined", 0, 0, true, false, false,
736 handle_no_sanitize_undefined_attribute,
737 false },
738 { "warning", 1, 1, true, false, false,
739 handle_error_attribute, false },
740 { "error", 1, 1, true, false, false,
741 handle_error_attribute, false },
742 { "target", 1, -1, true, false, false,
743 handle_target_attribute, false },
744 { "optimize", 1, -1, true, false, false,
745 handle_optimize_attribute, false },
746 /* For internal use only. The leading '*' both prevents its usage in
747 source code and signals that it may be overridden by machine tables. */
748 { "*tm regparm", 0, 0, false, true, true,
749 ignore_attribute, false },
750 { "no_split_stack", 0, 0, true, false, false,
751 handle_no_split_stack_attribute, false },
752 /* For internal use (marking of builtins and runtime functions) only.
753 The name contains space to prevent its usage in source code. */
754 { "fn spec", 1, 1, false, true, true,
755 handle_fnspec_attribute, false },
756 { "warn_unused", 0, 0, false, false, false,
757 handle_warn_unused_attribute, false },
758 { "returns_nonnull", 0, 0, false, true, true,
759 handle_returns_nonnull_attribute, false },
760 { "omp declare simd", 0, -1, true, false, false,
761 handle_omp_declare_simd_attribute, false },
762 { "omp declare target", 0, 0, true, false, false,
763 handle_omp_declare_target_attribute, false },
764 { "bnd_variable_size", 0, 0, true, false, false,
765 handle_bnd_variable_size_attribute, false },
766 { "bnd_legacy", 0, 0, true, false, false,
767 handle_bnd_legacy, false },
768 { NULL, 0, 0, false, false, false, NULL, false }
771 /* Give the specifications for the format attributes, used by C and all
772 descendants. */
774 const struct attribute_spec c_common_format_attribute_table[] =
776 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
777 affects_type_identity } */
778 { "format", 3, 3, false, true, true,
779 handle_format_attribute, false },
780 { "format_arg", 1, 1, false, true, true,
781 handle_format_arg_attribute, false },
782 { NULL, 0, 0, false, false, false, NULL, false }
785 /* Return identifier for address space AS. */
787 const char *
788 c_addr_space_name (addr_space_t as)
790 int rid = RID_FIRST_ADDR_SPACE + as;
791 gcc_assert (ridpointers [rid]);
792 return IDENTIFIER_POINTER (ridpointers [rid]);
795 /* Push current bindings for the function name VAR_DECLS. */
797 void
798 start_fname_decls (void)
800 unsigned ix;
801 tree saved = NULL_TREE;
803 for (ix = 0; fname_vars[ix].decl; ix++)
805 tree decl = *fname_vars[ix].decl;
807 if (decl)
809 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
810 saved);
811 *fname_vars[ix].decl = NULL_TREE;
814 if (saved || saved_function_name_decls)
815 /* Normally they'll have been NULL, so only push if we've got a
816 stack, or they are non-NULL. */
817 saved_function_name_decls = tree_cons (saved, NULL_TREE,
818 saved_function_name_decls);
821 /* Finish up the current bindings, adding them into the current function's
822 statement tree. This must be done _before_ finish_stmt_tree is called.
823 If there is no current function, we must be at file scope and no statements
824 are involved. Pop the previous bindings. */
826 void
827 finish_fname_decls (void)
829 unsigned ix;
830 tree stmts = NULL_TREE;
831 tree stack = saved_function_name_decls;
833 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
834 append_to_statement_list (TREE_VALUE (stack), &stmts);
836 if (stmts)
838 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
840 if (TREE_CODE (*bodyp) == BIND_EXPR)
841 bodyp = &BIND_EXPR_BODY (*bodyp);
843 append_to_statement_list_force (*bodyp, &stmts);
844 *bodyp = stmts;
847 for (ix = 0; fname_vars[ix].decl; ix++)
848 *fname_vars[ix].decl = NULL_TREE;
850 if (stack)
852 /* We had saved values, restore them. */
853 tree saved;
855 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
857 tree decl = TREE_PURPOSE (saved);
858 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
860 *fname_vars[ix].decl = decl;
862 stack = TREE_CHAIN (stack);
864 saved_function_name_decls = stack;
867 /* Return the text name of the current function, suitably prettified
868 by PRETTY_P. Return string must be freed by caller. */
870 const char *
871 fname_as_string (int pretty_p)
873 const char *name = "top level";
874 char *namep;
875 int vrb = 2, len;
876 cpp_string cstr = { 0, 0 }, strname;
878 if (!pretty_p)
880 name = "";
881 vrb = 0;
884 if (current_function_decl)
885 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
887 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
889 namep = XNEWVEC (char, len);
890 snprintf (namep, len, "\"%s\"", name);
891 strname.text = (unsigned char *) namep;
892 strname.len = len - 1;
894 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
896 XDELETEVEC (namep);
897 return (const char *) cstr.text;
900 return namep;
903 /* Return the VAR_DECL for a const char array naming the current
904 function. If the VAR_DECL has not yet been created, create it
905 now. RID indicates how it should be formatted and IDENTIFIER_NODE
906 ID is its name (unfortunately C and C++ hold the RID values of
907 keywords in different places, so we can't derive RID from ID in
908 this language independent code. LOC is the location of the
909 function. */
911 tree
912 fname_decl (location_t loc, unsigned int rid, tree id)
914 unsigned ix;
915 tree decl = NULL_TREE;
917 for (ix = 0; fname_vars[ix].decl; ix++)
918 if (fname_vars[ix].rid == rid)
919 break;
921 decl = *fname_vars[ix].decl;
922 if (!decl)
924 /* If a tree is built here, it would normally have the lineno of
925 the current statement. Later this tree will be moved to the
926 beginning of the function and this line number will be wrong.
927 To avoid this problem set the lineno to 0 here; that prevents
928 it from appearing in the RTL. */
929 tree stmts;
930 location_t saved_location = input_location;
931 input_location = UNKNOWN_LOCATION;
933 stmts = push_stmt_list ();
934 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
935 stmts = pop_stmt_list (stmts);
936 if (!IS_EMPTY_STMT (stmts))
937 saved_function_name_decls
938 = tree_cons (decl, stmts, saved_function_name_decls);
939 *fname_vars[ix].decl = decl;
940 input_location = saved_location;
942 if (!ix && !current_function_decl)
943 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
945 return decl;
948 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
950 tree
951 fix_string_type (tree value)
953 int length = TREE_STRING_LENGTH (value);
954 int nchars;
955 tree e_type, i_type, a_type;
957 /* Compute the number of elements, for the array type. */
958 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
960 nchars = length;
961 e_type = char_type_node;
963 else if (TREE_TYPE (value) == char16_array_type_node)
965 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
966 e_type = char16_type_node;
968 else if (TREE_TYPE (value) == char32_array_type_node)
970 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
971 e_type = char32_type_node;
973 else
975 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
976 e_type = wchar_type_node;
979 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
980 limit in C++98 Annex B is very large (65536) and is not normative,
981 so we do not diagnose it (warn_overlength_strings is forced off
982 in c_common_post_options). */
983 if (warn_overlength_strings)
985 const int nchars_max = flag_isoc99 ? 4095 : 509;
986 const int relevant_std = flag_isoc99 ? 99 : 90;
987 if (nchars - 1 > nchars_max)
988 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
989 separate the %d from the 'C'. 'ISO' should not be
990 translated, but it may be moved after 'C%d' in languages
991 where modifiers follow nouns. */
992 pedwarn (input_location, OPT_Woverlength_strings,
993 "string length %qd is greater than the length %qd "
994 "ISO C%d compilers are required to support",
995 nchars - 1, nchars_max, relevant_std);
998 /* Create the array type for the string constant. The ISO C++
999 standard says that a string literal has type `const char[N]' or
1000 `const wchar_t[N]'. We use the same logic when invoked as a C
1001 front-end with -Wwrite-strings.
1002 ??? We should change the type of an expression depending on the
1003 state of a warning flag. We should just be warning -- see how
1004 this is handled in the C++ front-end for the deprecated implicit
1005 conversion from string literals to `char*' or `wchar_t*'.
1007 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1008 array type being the unqualified version of that type.
1009 Therefore, if we are constructing an array of const char, we must
1010 construct the matching unqualified array type first. The C front
1011 end does not require this, but it does no harm, so we do it
1012 unconditionally. */
1013 i_type = build_index_type (size_int (nchars - 1));
1014 a_type = build_array_type (e_type, i_type);
1015 if (c_dialect_cxx() || warn_write_strings)
1016 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1018 TREE_TYPE (value) = a_type;
1019 TREE_CONSTANT (value) = 1;
1020 TREE_READONLY (value) = 1;
1021 TREE_STATIC (value) = 1;
1022 return value;
1025 /* If DISABLE is true, stop issuing warnings. This is used when
1026 parsing code that we know will not be executed. This function may
1027 be called multiple times, and works as a stack. */
1029 static void
1030 c_disable_warnings (bool disable)
1032 if (disable)
1034 ++c_inhibit_evaluation_warnings;
1035 fold_defer_overflow_warnings ();
1039 /* If ENABLE is true, reenable issuing warnings. */
1041 static void
1042 c_enable_warnings (bool enable)
1044 if (enable)
1046 --c_inhibit_evaluation_warnings;
1047 fold_undefer_and_ignore_overflow_warnings ();
1051 /* Fully fold EXPR, an expression that was not folded (beyond integer
1052 constant expressions and null pointer constants) when being built
1053 up. If IN_INIT, this is in a static initializer and certain
1054 changes are made to the folding done. Clear *MAYBE_CONST if
1055 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1056 expression because it contains an evaluated operator (in C99) or an
1057 operator outside of sizeof returning an integer constant (in C90)
1058 not permitted in constant expressions, or because it contains an
1059 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1060 set to true by callers before calling this function.) Return the
1061 folded expression. Function arguments have already been folded
1062 before calling this function, as have the contents of SAVE_EXPR,
1063 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1064 C_MAYBE_CONST_EXPR. */
1066 tree
1067 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1069 tree ret;
1070 tree eptype = NULL_TREE;
1071 bool dummy = true;
1072 bool maybe_const_itself = true;
1073 location_t loc = EXPR_LOCATION (expr);
1075 /* This function is not relevant to C++ because C++ folds while
1076 parsing, and may need changes to be correct for C++ when C++
1077 stops folding while parsing. */
1078 if (c_dialect_cxx ())
1079 gcc_unreachable ();
1081 if (!maybe_const)
1082 maybe_const = &dummy;
1083 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1085 eptype = TREE_TYPE (expr);
1086 expr = TREE_OPERAND (expr, 0);
1088 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1089 &maybe_const_itself);
1090 if (eptype)
1091 ret = fold_convert_loc (loc, eptype, ret);
1092 *maybe_const &= maybe_const_itself;
1093 return ret;
1096 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1097 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1098 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1099 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1100 both evaluated and unevaluated subexpressions while
1101 *MAYBE_CONST_ITSELF is carried from only evaluated
1102 subexpressions). */
1104 static tree
1105 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1106 bool *maybe_const_itself)
1108 tree ret = expr;
1109 enum tree_code code = TREE_CODE (expr);
1110 enum tree_code_class kind = TREE_CODE_CLASS (code);
1111 location_t loc = EXPR_LOCATION (expr);
1112 tree op0, op1, op2, op3;
1113 tree orig_op0, orig_op1, orig_op2;
1114 bool op0_const = true, op1_const = true, op2_const = true;
1115 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1116 bool nowarning = TREE_NO_WARNING (expr);
1117 bool unused_p;
1119 /* This function is not relevant to C++ because C++ folds while
1120 parsing, and may need changes to be correct for C++ when C++
1121 stops folding while parsing. */
1122 if (c_dialect_cxx ())
1123 gcc_unreachable ();
1125 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1126 anything else not counted as an expression cannot usefully be
1127 folded further at this point. */
1128 if (!IS_EXPR_CODE_CLASS (kind)
1129 || kind == tcc_statement
1130 || code == SAVE_EXPR)
1131 return expr;
1133 /* Operands of variable-length expressions (function calls) have
1134 already been folded, as have __builtin_* function calls, and such
1135 expressions cannot occur in constant expressions. */
1136 if (kind == tcc_vl_exp)
1138 *maybe_const_operands = false;
1139 ret = fold (expr);
1140 goto out;
1143 if (code == C_MAYBE_CONST_EXPR)
1145 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1146 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1147 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1148 *maybe_const_operands = false;
1149 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1150 *maybe_const_itself = false;
1151 if (pre && !in_init)
1152 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1153 else
1154 ret = inner;
1155 goto out;
1158 /* Assignment, increment, decrement, function call and comma
1159 operators, and statement expressions, cannot occur in constant
1160 expressions if evaluated / outside of sizeof. (Function calls
1161 were handled above, though VA_ARG_EXPR is treated like a function
1162 call here, and statement expressions are handled through
1163 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1164 switch (code)
1166 case MODIFY_EXPR:
1167 case PREDECREMENT_EXPR:
1168 case PREINCREMENT_EXPR:
1169 case POSTDECREMENT_EXPR:
1170 case POSTINCREMENT_EXPR:
1171 case COMPOUND_EXPR:
1172 *maybe_const_operands = false;
1173 break;
1175 case VA_ARG_EXPR:
1176 case TARGET_EXPR:
1177 case BIND_EXPR:
1178 case OBJ_TYPE_REF:
1179 *maybe_const_operands = false;
1180 ret = fold (expr);
1181 goto out;
1183 default:
1184 break;
1187 /* Fold individual tree codes as appropriate. */
1188 switch (code)
1190 case COMPOUND_LITERAL_EXPR:
1191 /* Any non-constancy will have been marked in a containing
1192 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1193 goto out;
1195 case COMPONENT_REF:
1196 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1197 op1 = TREE_OPERAND (expr, 1);
1198 op2 = TREE_OPERAND (expr, 2);
1199 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1200 maybe_const_itself);
1201 STRIP_TYPE_NOPS (op0);
1202 if (op0 != orig_op0)
1203 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1204 if (ret != expr)
1206 TREE_READONLY (ret) = TREE_READONLY (expr);
1207 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1209 goto out;
1211 case ARRAY_REF:
1212 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1213 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1214 op2 = TREE_OPERAND (expr, 2);
1215 op3 = TREE_OPERAND (expr, 3);
1216 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1217 maybe_const_itself);
1218 STRIP_TYPE_NOPS (op0);
1219 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1220 maybe_const_itself);
1221 STRIP_TYPE_NOPS (op1);
1222 op1 = decl_constant_value_for_optimization (op1);
1223 if (op0 != orig_op0 || op1 != orig_op1)
1224 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1225 if (ret != expr)
1227 TREE_READONLY (ret) = TREE_READONLY (expr);
1228 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1229 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1231 ret = fold (ret);
1232 goto out;
1234 case COMPOUND_EXPR:
1235 case MODIFY_EXPR:
1236 case PREDECREMENT_EXPR:
1237 case PREINCREMENT_EXPR:
1238 case POSTDECREMENT_EXPR:
1239 case POSTINCREMENT_EXPR:
1240 case PLUS_EXPR:
1241 case MINUS_EXPR:
1242 case MULT_EXPR:
1243 case POINTER_PLUS_EXPR:
1244 case TRUNC_DIV_EXPR:
1245 case CEIL_DIV_EXPR:
1246 case FLOOR_DIV_EXPR:
1247 case TRUNC_MOD_EXPR:
1248 case RDIV_EXPR:
1249 case EXACT_DIV_EXPR:
1250 case LSHIFT_EXPR:
1251 case RSHIFT_EXPR:
1252 case BIT_IOR_EXPR:
1253 case BIT_XOR_EXPR:
1254 case BIT_AND_EXPR:
1255 case LT_EXPR:
1256 case LE_EXPR:
1257 case GT_EXPR:
1258 case GE_EXPR:
1259 case EQ_EXPR:
1260 case NE_EXPR:
1261 case COMPLEX_EXPR:
1262 case TRUTH_AND_EXPR:
1263 case TRUTH_OR_EXPR:
1264 case TRUTH_XOR_EXPR:
1265 case UNORDERED_EXPR:
1266 case ORDERED_EXPR:
1267 case UNLT_EXPR:
1268 case UNLE_EXPR:
1269 case UNGT_EXPR:
1270 case UNGE_EXPR:
1271 case UNEQ_EXPR:
1272 /* Binary operations evaluating both arguments (increment and
1273 decrement are binary internally in GCC). */
1274 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1275 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1276 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1277 maybe_const_itself);
1278 STRIP_TYPE_NOPS (op0);
1279 if (code != MODIFY_EXPR
1280 && code != PREDECREMENT_EXPR
1281 && code != PREINCREMENT_EXPR
1282 && code != POSTDECREMENT_EXPR
1283 && code != POSTINCREMENT_EXPR)
1284 op0 = decl_constant_value_for_optimization (op0);
1285 /* The RHS of a MODIFY_EXPR was fully folded when building that
1286 expression for the sake of conversion warnings. */
1287 if (code != MODIFY_EXPR)
1288 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1289 maybe_const_itself);
1290 STRIP_TYPE_NOPS (op1);
1291 op1 = decl_constant_value_for_optimization (op1);
1292 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1293 ret = in_init
1294 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1295 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1296 else
1297 ret = fold (expr);
1298 if (TREE_OVERFLOW_P (ret)
1299 && !TREE_OVERFLOW_P (op0)
1300 && !TREE_OVERFLOW_P (op1))
1301 overflow_warning (EXPR_LOCATION (expr), ret);
1302 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1303 && TREE_CODE (orig_op1) != INTEGER_CST
1304 && TREE_CODE (op1) == INTEGER_CST
1305 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1306 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1307 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1308 && c_inhibit_evaluation_warnings == 0)
1310 if (tree_int_cst_sgn (op1) < 0)
1311 warning_at (loc, 0, (code == LSHIFT_EXPR
1312 ? G_("left shift count is negative")
1313 : G_("right shift count is negative")));
1314 else if (compare_tree_int (op1,
1315 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1316 >= 0)
1317 warning_at (loc, 0, (code == LSHIFT_EXPR
1318 ? G_("left shift count >= width of type")
1319 : G_("right shift count >= width of type")));
1321 goto out;
1323 case INDIRECT_REF:
1324 case FIX_TRUNC_EXPR:
1325 case FLOAT_EXPR:
1326 CASE_CONVERT:
1327 case VIEW_CONVERT_EXPR:
1328 case NON_LVALUE_EXPR:
1329 case NEGATE_EXPR:
1330 case BIT_NOT_EXPR:
1331 case TRUTH_NOT_EXPR:
1332 case ADDR_EXPR:
1333 case CONJ_EXPR:
1334 case REALPART_EXPR:
1335 case IMAGPART_EXPR:
1336 /* Unary operations. */
1337 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1338 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1339 maybe_const_itself);
1340 STRIP_TYPE_NOPS (op0);
1341 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1342 op0 = decl_constant_value_for_optimization (op0);
1343 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1344 not prepared to deal with them if they occur in initializers. */
1345 if (op0 != orig_op0
1346 && code == ADDR_EXPR
1347 && (op1 = get_base_address (op0)) != NULL_TREE
1348 && TREE_CODE (op1) == INDIRECT_REF
1349 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1350 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1351 else if (op0 != orig_op0 || in_init)
1352 ret = in_init
1353 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1354 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1355 else
1356 ret = fold (expr);
1357 if (code == INDIRECT_REF
1358 && ret != expr
1359 && TREE_CODE (ret) == INDIRECT_REF)
1361 TREE_READONLY (ret) = TREE_READONLY (expr);
1362 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1363 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1365 switch (code)
1367 case FIX_TRUNC_EXPR:
1368 case FLOAT_EXPR:
1369 CASE_CONVERT:
1370 /* Don't warn about explicit conversions. We will already
1371 have warned about suspect implicit conversions. */
1372 break;
1374 default:
1375 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1376 overflow_warning (EXPR_LOCATION (expr), ret);
1377 break;
1379 goto out;
1381 case TRUTH_ANDIF_EXPR:
1382 case TRUTH_ORIF_EXPR:
1383 /* Binary operations not necessarily evaluating both
1384 arguments. */
1385 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1386 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1387 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1388 STRIP_TYPE_NOPS (op0);
1390 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1391 ? truthvalue_false_node
1392 : truthvalue_true_node));
1393 c_disable_warnings (unused_p);
1394 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1395 STRIP_TYPE_NOPS (op1);
1396 c_enable_warnings (unused_p);
1398 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1399 ret = in_init
1400 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1401 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1402 else
1403 ret = fold (expr);
1404 *maybe_const_operands &= op0_const;
1405 *maybe_const_itself &= op0_const_self;
1406 if (!(flag_isoc99
1407 && op0_const
1408 && op0_const_self
1409 && (code == TRUTH_ANDIF_EXPR
1410 ? op0 == truthvalue_false_node
1411 : op0 == truthvalue_true_node)))
1412 *maybe_const_operands &= op1_const;
1413 if (!(op0_const
1414 && op0_const_self
1415 && (code == TRUTH_ANDIF_EXPR
1416 ? op0 == truthvalue_false_node
1417 : op0 == truthvalue_true_node)))
1418 *maybe_const_itself &= op1_const_self;
1419 goto out;
1421 case COND_EXPR:
1422 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1423 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1424 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1425 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1427 STRIP_TYPE_NOPS (op0);
1428 c_disable_warnings (op0 == truthvalue_false_node);
1429 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1430 STRIP_TYPE_NOPS (op1);
1431 c_enable_warnings (op0 == truthvalue_false_node);
1433 c_disable_warnings (op0 == truthvalue_true_node);
1434 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1435 STRIP_TYPE_NOPS (op2);
1436 c_enable_warnings (op0 == truthvalue_true_node);
1438 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1439 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1440 else
1441 ret = fold (expr);
1442 *maybe_const_operands &= op0_const;
1443 *maybe_const_itself &= op0_const_self;
1444 if (!(flag_isoc99
1445 && op0_const
1446 && op0_const_self
1447 && op0 == truthvalue_false_node))
1448 *maybe_const_operands &= op1_const;
1449 if (!(op0_const
1450 && op0_const_self
1451 && op0 == truthvalue_false_node))
1452 *maybe_const_itself &= op1_const_self;
1453 if (!(flag_isoc99
1454 && op0_const
1455 && op0_const_self
1456 && op0 == truthvalue_true_node))
1457 *maybe_const_operands &= op2_const;
1458 if (!(op0_const
1459 && op0_const_self
1460 && op0 == truthvalue_true_node))
1461 *maybe_const_itself &= op2_const_self;
1462 goto out;
1464 case EXCESS_PRECISION_EXPR:
1465 /* Each case where an operand with excess precision may be
1466 encountered must remove the EXCESS_PRECISION_EXPR around
1467 inner operands and possibly put one around the whole
1468 expression or possibly convert to the semantic type (which
1469 c_fully_fold does); we cannot tell at this stage which is
1470 appropriate in any particular case. */
1471 gcc_unreachable ();
1473 default:
1474 /* Various codes may appear through folding built-in functions
1475 and their arguments. */
1476 goto out;
1479 out:
1480 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1481 have been done by this point, so remove them again. */
1482 nowarning |= TREE_NO_WARNING (ret);
1483 STRIP_TYPE_NOPS (ret);
1484 if (nowarning && !TREE_NO_WARNING (ret))
1486 if (!CAN_HAVE_LOCATION_P (ret))
1487 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1488 TREE_NO_WARNING (ret) = 1;
1490 if (ret != expr)
1491 protected_set_expr_location (ret, loc);
1492 return ret;
1495 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1496 return EXP. Otherwise, return either EXP or its known constant
1497 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1498 Is the BLKmode test appropriate? */
1500 tree
1501 decl_constant_value_for_optimization (tree exp)
1503 tree ret;
1505 /* This function is only used by C, for c_fully_fold and other
1506 optimization, and may not be correct for C++. */
1507 if (c_dialect_cxx ())
1508 gcc_unreachable ();
1510 if (!optimize
1511 || TREE_CODE (exp) != VAR_DECL
1512 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1513 || DECL_MODE (exp) == BLKmode)
1514 return exp;
1516 ret = decl_constant_value (exp);
1517 /* Avoid unwanted tree sharing between the initializer and current
1518 function's body where the tree can be modified e.g. by the
1519 gimplifier. */
1520 if (ret != exp && TREE_STATIC (exp))
1521 ret = unshare_expr (ret);
1522 return ret;
1525 /* Print a warning if a constant expression had overflow in folding.
1526 Invoke this function on every expression that the language
1527 requires to be a constant expression.
1528 Note the ANSI C standard says it is erroneous for a
1529 constant expression to overflow. */
1531 void
1532 constant_expression_warning (tree value)
1534 if (warn_overflow && pedantic
1535 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1536 || TREE_CODE (value) == FIXED_CST
1537 || TREE_CODE (value) == VECTOR_CST
1538 || TREE_CODE (value) == COMPLEX_CST)
1539 && TREE_OVERFLOW (value))
1540 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1543 /* The same as above but print an unconditional error. */
1544 void
1545 constant_expression_error (tree value)
1547 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1548 || TREE_CODE (value) == FIXED_CST
1549 || TREE_CODE (value) == VECTOR_CST
1550 || TREE_CODE (value) == COMPLEX_CST)
1551 && TREE_OVERFLOW (value))
1552 error ("overflow in constant expression");
1555 /* Print a warning if an expression had overflow in folding and its
1556 operands hadn't.
1558 Invoke this function on every expression that
1559 (1) appears in the source code, and
1560 (2) is a constant expression that overflowed, and
1561 (3) is not already checked by convert_and_check;
1562 however, do not invoke this function on operands of explicit casts
1563 or when the expression is the result of an operator and any operand
1564 already overflowed. */
1566 void
1567 overflow_warning (location_t loc, tree value)
1569 if (c_inhibit_evaluation_warnings != 0)
1570 return;
1572 switch (TREE_CODE (value))
1574 case INTEGER_CST:
1575 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1576 break;
1578 case REAL_CST:
1579 warning_at (loc, OPT_Woverflow,
1580 "floating point overflow in expression");
1581 break;
1583 case FIXED_CST:
1584 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1585 break;
1587 case VECTOR_CST:
1588 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1589 break;
1591 case COMPLEX_CST:
1592 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1593 warning_at (loc, OPT_Woverflow,
1594 "complex integer overflow in expression");
1595 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1596 warning_at (loc, OPT_Woverflow,
1597 "complex floating point overflow in expression");
1598 break;
1600 default:
1601 break;
1605 /* Warn about uses of logical || / && operator in a context where it
1606 is likely that the bitwise equivalent was intended by the
1607 programmer. We have seen an expression in which CODE is a binary
1608 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1609 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1610 void
1611 warn_logical_operator (location_t location, enum tree_code code, tree type,
1612 enum tree_code code_left, tree op_left,
1613 enum tree_code ARG_UNUSED (code_right), tree op_right)
1615 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1616 int in0_p, in1_p, in_p;
1617 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1618 bool strict_overflow_p = false;
1620 if (code != TRUTH_ANDIF_EXPR
1621 && code != TRUTH_AND_EXPR
1622 && code != TRUTH_ORIF_EXPR
1623 && code != TRUTH_OR_EXPR)
1624 return;
1626 /* Warn if &&/|| are being used in a context where it is
1627 likely that the bitwise equivalent was intended by the
1628 programmer. That is, an expression such as op && MASK
1629 where op should not be any boolean expression, nor a
1630 constant, and mask seems to be a non-boolean integer constant. */
1631 if (!truth_value_p (code_left)
1632 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1633 && !CONSTANT_CLASS_P (op_left)
1634 && !TREE_NO_WARNING (op_left)
1635 && TREE_CODE (op_right) == INTEGER_CST
1636 && !integer_zerop (op_right)
1637 && !integer_onep (op_right))
1639 if (or_op)
1640 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1641 " applied to non-boolean constant");
1642 else
1643 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1644 " applied to non-boolean constant");
1645 TREE_NO_WARNING (op_left) = true;
1646 return;
1649 /* We do not warn for constants because they are typical of macro
1650 expansions that test for features. */
1651 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1652 return;
1654 /* This warning only makes sense with logical operands. */
1655 if (!(truth_value_p (TREE_CODE (op_left))
1656 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1657 || !(truth_value_p (TREE_CODE (op_right))
1658 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1659 return;
1662 /* We first test whether either side separately is trivially true
1663 (with OR) or trivially false (with AND). If so, do not warn.
1664 This is a common idiom for testing ranges of data types in
1665 portable code. */
1666 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1667 if (!lhs)
1668 return;
1669 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1670 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1672 /* If this is an OR operation, invert both sides; now, the result
1673 should be always false to get a warning. */
1674 if (or_op)
1675 in0_p = !in0_p;
1677 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1678 if (tem && integer_zerop (tem))
1679 return;
1681 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1682 if (!rhs)
1683 return;
1684 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1685 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1687 /* If this is an OR operation, invert both sides; now, the result
1688 should be always false to get a warning. */
1689 if (or_op)
1690 in1_p = !in1_p;
1692 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1693 if (tem && integer_zerop (tem))
1694 return;
1696 /* If both expressions have the same operand, if we can merge the
1697 ranges, and if the range test is always false, then warn. */
1698 if (operand_equal_p (lhs, rhs, 0)
1699 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1700 in1_p, low1, high1)
1701 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1702 type, lhs, in_p, low, high))
1703 && integer_zerop (tem))
1705 if (or_op)
1706 warning_at (location, OPT_Wlogical_op,
1707 "logical %<or%> "
1708 "of collectively exhaustive tests is always true");
1709 else
1710 warning_at (location, OPT_Wlogical_op,
1711 "logical %<and%> "
1712 "of mutually exclusive tests is always false");
1717 /* Warn if EXP contains any computations whose results are not used.
1718 Return true if a warning is printed; false otherwise. LOCUS is the
1719 (potential) location of the expression. */
1721 bool
1722 warn_if_unused_value (const_tree exp, location_t locus)
1724 restart:
1725 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1726 return false;
1728 /* Don't warn about void constructs. This includes casting to void,
1729 void function calls, and statement expressions with a final cast
1730 to void. */
1731 if (VOID_TYPE_P (TREE_TYPE (exp)))
1732 return false;
1734 if (EXPR_HAS_LOCATION (exp))
1735 locus = EXPR_LOCATION (exp);
1737 switch (TREE_CODE (exp))
1739 case PREINCREMENT_EXPR:
1740 case POSTINCREMENT_EXPR:
1741 case PREDECREMENT_EXPR:
1742 case POSTDECREMENT_EXPR:
1743 case MODIFY_EXPR:
1744 case INIT_EXPR:
1745 case TARGET_EXPR:
1746 case CALL_EXPR:
1747 case TRY_CATCH_EXPR:
1748 case WITH_CLEANUP_EXPR:
1749 case EXIT_EXPR:
1750 case VA_ARG_EXPR:
1751 return false;
1753 case BIND_EXPR:
1754 /* For a binding, warn if no side effect within it. */
1755 exp = BIND_EXPR_BODY (exp);
1756 goto restart;
1758 case SAVE_EXPR:
1759 case NON_LVALUE_EXPR:
1760 case NOP_EXPR:
1761 exp = TREE_OPERAND (exp, 0);
1762 goto restart;
1764 case TRUTH_ORIF_EXPR:
1765 case TRUTH_ANDIF_EXPR:
1766 /* In && or ||, warn if 2nd operand has no side effect. */
1767 exp = TREE_OPERAND (exp, 1);
1768 goto restart;
1770 case COMPOUND_EXPR:
1771 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1772 return true;
1773 /* Let people do `(foo (), 0)' without a warning. */
1774 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1775 return false;
1776 exp = TREE_OPERAND (exp, 1);
1777 goto restart;
1779 case COND_EXPR:
1780 /* If this is an expression with side effects, don't warn; this
1781 case commonly appears in macro expansions. */
1782 if (TREE_SIDE_EFFECTS (exp))
1783 return false;
1784 goto warn;
1786 case INDIRECT_REF:
1787 /* Don't warn about automatic dereferencing of references, since
1788 the user cannot control it. */
1789 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1791 exp = TREE_OPERAND (exp, 0);
1792 goto restart;
1794 /* Fall through. */
1796 default:
1797 /* Referencing a volatile value is a side effect, so don't warn. */
1798 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1799 && TREE_THIS_VOLATILE (exp))
1800 return false;
1802 /* If this is an expression which has no operands, there is no value
1803 to be unused. There are no such language-independent codes,
1804 but front ends may define such. */
1805 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1806 return false;
1808 warn:
1809 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1814 /* Print a warning about casts that might indicate violation
1815 of strict aliasing rules if -Wstrict-aliasing is used and
1816 strict aliasing mode is in effect. OTYPE is the original
1817 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1819 bool
1820 strict_aliasing_warning (tree otype, tree type, tree expr)
1822 /* Strip pointer conversion chains and get to the correct original type. */
1823 STRIP_NOPS (expr);
1824 otype = TREE_TYPE (expr);
1826 if (!(flag_strict_aliasing
1827 && POINTER_TYPE_P (type)
1828 && POINTER_TYPE_P (otype)
1829 && !VOID_TYPE_P (TREE_TYPE (type)))
1830 /* If the type we are casting to is a ref-all pointer
1831 dereferencing it is always valid. */
1832 || TYPE_REF_CAN_ALIAS_ALL (type))
1833 return false;
1835 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1836 && (DECL_P (TREE_OPERAND (expr, 0))
1837 || handled_component_p (TREE_OPERAND (expr, 0))))
1839 /* Casting the address of an object to non void pointer. Warn
1840 if the cast breaks type based aliasing. */
1841 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1843 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1844 "might break strict-aliasing rules");
1845 return true;
1847 else
1849 /* warn_strict_aliasing >= 3. This includes the default (3).
1850 Only warn if the cast is dereferenced immediately. */
1851 alias_set_type set1 =
1852 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1853 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1855 if (set1 != set2 && set2 != 0
1856 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1858 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1859 "pointer will break strict-aliasing rules");
1860 return true;
1862 else if (warn_strict_aliasing == 2
1863 && !alias_sets_must_conflict_p (set1, set2))
1865 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1866 "pointer might break strict-aliasing rules");
1867 return true;
1871 else
1872 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1874 /* At this level, warn for any conversions, even if an address is
1875 not taken in the same statement. This will likely produce many
1876 false positives, but could be useful to pinpoint problems that
1877 are not revealed at higher levels. */
1878 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1879 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1880 if (!COMPLETE_TYPE_P (type)
1881 || !alias_sets_must_conflict_p (set1, set2))
1883 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1884 "pointer might break strict-aliasing rules");
1885 return true;
1889 return false;
1892 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1893 sizeof as last operand of certain builtins. */
1895 void
1896 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1897 vec<tree, va_gc> *params, tree *sizeof_arg,
1898 bool (*comp_types) (tree, tree))
1900 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1901 bool strop = false, cmp = false;
1902 unsigned int idx = ~0;
1903 location_t loc;
1905 if (TREE_CODE (callee) != FUNCTION_DECL
1906 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1907 || vec_safe_length (params) <= 1)
1908 return;
1910 switch (DECL_FUNCTION_CODE (callee))
1912 case BUILT_IN_STRNCMP:
1913 case BUILT_IN_STRNCASECMP:
1914 cmp = true;
1915 /* FALLTHRU */
1916 case BUILT_IN_STRNCPY:
1917 case BUILT_IN_STRNCPY_CHK:
1918 case BUILT_IN_STRNCAT:
1919 case BUILT_IN_STRNCAT_CHK:
1920 case BUILT_IN_STPNCPY:
1921 case BUILT_IN_STPNCPY_CHK:
1922 strop = true;
1923 /* FALLTHRU */
1924 case BUILT_IN_MEMCPY:
1925 case BUILT_IN_MEMCPY_CHK:
1926 case BUILT_IN_MEMMOVE:
1927 case BUILT_IN_MEMMOVE_CHK:
1928 if (params->length () < 3)
1929 return;
1930 src = (*params)[1];
1931 dest = (*params)[0];
1932 idx = 2;
1933 break;
1934 case BUILT_IN_BCOPY:
1935 if (params->length () < 3)
1936 return;
1937 src = (*params)[0];
1938 dest = (*params)[1];
1939 idx = 2;
1940 break;
1941 case BUILT_IN_MEMCMP:
1942 case BUILT_IN_BCMP:
1943 if (params->length () < 3)
1944 return;
1945 src = (*params)[1];
1946 dest = (*params)[0];
1947 idx = 2;
1948 cmp = true;
1949 break;
1950 case BUILT_IN_MEMSET:
1951 case BUILT_IN_MEMSET_CHK:
1952 if (params->length () < 3)
1953 return;
1954 dest = (*params)[0];
1955 idx = 2;
1956 break;
1957 case BUILT_IN_BZERO:
1958 dest = (*params)[0];
1959 idx = 1;
1960 break;
1961 case BUILT_IN_STRNDUP:
1962 src = (*params)[0];
1963 strop = true;
1964 idx = 1;
1965 break;
1966 case BUILT_IN_MEMCHR:
1967 if (params->length () < 3)
1968 return;
1969 src = (*params)[0];
1970 idx = 2;
1971 break;
1972 case BUILT_IN_SNPRINTF:
1973 case BUILT_IN_SNPRINTF_CHK:
1974 case BUILT_IN_VSNPRINTF:
1975 case BUILT_IN_VSNPRINTF_CHK:
1976 dest = (*params)[0];
1977 idx = 1;
1978 strop = true;
1979 break;
1980 default:
1981 break;
1984 if (idx >= 3)
1985 return;
1987 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1988 return;
1990 type = TYPE_P (sizeof_arg[idx])
1991 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1992 if (!POINTER_TYPE_P (type))
1993 return;
1995 if (dest
1996 && (tem = tree_strip_nop_conversions (dest))
1997 && POINTER_TYPE_P (TREE_TYPE (tem))
1998 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1999 return;
2001 if (src
2002 && (tem = tree_strip_nop_conversions (src))
2003 && POINTER_TYPE_P (TREE_TYPE (tem))
2004 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2005 return;
2007 loc = sizeof_arg_loc[idx];
2009 if (dest && !cmp)
2011 if (!TYPE_P (sizeof_arg[idx])
2012 && operand_equal_p (dest, sizeof_arg[idx], 0)
2013 && comp_types (TREE_TYPE (dest), type))
2015 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2016 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2017 "argument to %<sizeof%> in %qD call is the same "
2018 "expression as the destination; did you mean to "
2019 "remove the addressof?", callee);
2020 else if ((TYPE_PRECISION (TREE_TYPE (type))
2021 == TYPE_PRECISION (char_type_node))
2022 || strop)
2023 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2024 "argument to %<sizeof%> in %qD call is the same "
2025 "expression as the destination; did you mean to "
2026 "provide an explicit length?", callee);
2027 else
2028 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2029 "argument to %<sizeof%> in %qD call is the same "
2030 "expression as the destination; did you mean to "
2031 "dereference it?", callee);
2032 return;
2035 if (POINTER_TYPE_P (TREE_TYPE (dest))
2036 && !strop
2037 && comp_types (TREE_TYPE (dest), type)
2038 && !VOID_TYPE_P (TREE_TYPE (type)))
2040 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2041 "argument to %<sizeof%> in %qD call is the same "
2042 "pointer type %qT as the destination; expected %qT "
2043 "or an explicit length", callee, TREE_TYPE (dest),
2044 TREE_TYPE (TREE_TYPE (dest)));
2045 return;
2049 if (src && !cmp)
2051 if (!TYPE_P (sizeof_arg[idx])
2052 && operand_equal_p (src, sizeof_arg[idx], 0)
2053 && comp_types (TREE_TYPE (src), type))
2055 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2056 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2057 "argument to %<sizeof%> in %qD call is the same "
2058 "expression as the source; did you mean to "
2059 "remove the addressof?", callee);
2060 else if ((TYPE_PRECISION (TREE_TYPE (type))
2061 == TYPE_PRECISION (char_type_node))
2062 || strop)
2063 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2064 "argument to %<sizeof%> in %qD call is the same "
2065 "expression as the source; did you mean to "
2066 "provide an explicit length?", callee);
2067 else
2068 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2069 "argument to %<sizeof%> in %qD call is the same "
2070 "expression as the source; did you mean to "
2071 "dereference it?", callee);
2072 return;
2075 if (POINTER_TYPE_P (TREE_TYPE (src))
2076 && !strop
2077 && comp_types (TREE_TYPE (src), type)
2078 && !VOID_TYPE_P (TREE_TYPE (type)))
2080 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2081 "argument to %<sizeof%> in %qD call is the same "
2082 "pointer type %qT as the source; expected %qT "
2083 "or an explicit length", callee, TREE_TYPE (src),
2084 TREE_TYPE (TREE_TYPE (src)));
2085 return;
2089 if (dest)
2091 if (!TYPE_P (sizeof_arg[idx])
2092 && operand_equal_p (dest, sizeof_arg[idx], 0)
2093 && comp_types (TREE_TYPE (dest), type))
2095 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2096 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2097 "argument to %<sizeof%> in %qD call is the same "
2098 "expression as the first source; did you mean to "
2099 "remove the addressof?", callee);
2100 else if ((TYPE_PRECISION (TREE_TYPE (type))
2101 == TYPE_PRECISION (char_type_node))
2102 || strop)
2103 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2104 "argument to %<sizeof%> in %qD call is the same "
2105 "expression as the first source; did you mean to "
2106 "provide an explicit length?", callee);
2107 else
2108 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2109 "argument to %<sizeof%> in %qD call is the same "
2110 "expression as the first source; did you mean to "
2111 "dereference it?", callee);
2112 return;
2115 if (POINTER_TYPE_P (TREE_TYPE (dest))
2116 && !strop
2117 && comp_types (TREE_TYPE (dest), type)
2118 && !VOID_TYPE_P (TREE_TYPE (type)))
2120 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2121 "argument to %<sizeof%> in %qD call is the same "
2122 "pointer type %qT as the first source; expected %qT "
2123 "or an explicit length", callee, TREE_TYPE (dest),
2124 TREE_TYPE (TREE_TYPE (dest)));
2125 return;
2129 if (src)
2131 if (!TYPE_P (sizeof_arg[idx])
2132 && operand_equal_p (src, sizeof_arg[idx], 0)
2133 && comp_types (TREE_TYPE (src), type))
2135 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2136 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2137 "argument to %<sizeof%> in %qD call is the same "
2138 "expression as the second source; did you mean to "
2139 "remove the addressof?", callee);
2140 else if ((TYPE_PRECISION (TREE_TYPE (type))
2141 == TYPE_PRECISION (char_type_node))
2142 || strop)
2143 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2144 "argument to %<sizeof%> in %qD call is the same "
2145 "expression as the second source; did you mean to "
2146 "provide an explicit length?", callee);
2147 else
2148 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2149 "argument to %<sizeof%> in %qD call is the same "
2150 "expression as the second source; did you mean to "
2151 "dereference it?", callee);
2152 return;
2155 if (POINTER_TYPE_P (TREE_TYPE (src))
2156 && !strop
2157 && comp_types (TREE_TYPE (src), type)
2158 && !VOID_TYPE_P (TREE_TYPE (type)))
2160 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2161 "argument to %<sizeof%> in %qD call is the same "
2162 "pointer type %qT as the second source; expected %qT "
2163 "or an explicit length", callee, TREE_TYPE (src),
2164 TREE_TYPE (TREE_TYPE (src)));
2165 return;
2171 /* Warn for unlikely, improbable, or stupid DECL declarations
2172 of `main'. */
2174 void
2175 check_main_parameter_types (tree decl)
2177 function_args_iterator iter;
2178 tree type;
2179 int argct = 0;
2181 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2183 /* XXX void_type_node belies the abstraction. */
2184 if (type == void_type_node || type == error_mark_node )
2185 break;
2187 ++argct;
2188 switch (argct)
2190 case 1:
2191 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2192 pedwarn (input_location, OPT_Wmain,
2193 "first argument of %q+D should be %<int%>", decl);
2194 break;
2196 case 2:
2197 if (TREE_CODE (type) != POINTER_TYPE
2198 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2199 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2200 != char_type_node))
2201 pedwarn (input_location, OPT_Wmain,
2202 "second argument of %q+D should be %<char **%>", decl);
2203 break;
2205 case 3:
2206 if (TREE_CODE (type) != POINTER_TYPE
2207 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2208 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2209 != char_type_node))
2210 pedwarn (input_location, OPT_Wmain,
2211 "third argument of %q+D should probably be "
2212 "%<char **%>", decl);
2213 break;
2217 /* It is intentional that this message does not mention the third
2218 argument because it's only mentioned in an appendix of the
2219 standard. */
2220 if (argct > 0 && (argct < 2 || argct > 3))
2221 pedwarn (input_location, OPT_Wmain,
2222 "%q+D takes only zero or two arguments", decl);
2225 /* vector_targets_convertible_p is used for vector pointer types. The
2226 callers perform various checks that the qualifiers are satisfactory,
2227 while OTOH vector_targets_convertible_p ignores the number of elements
2228 in the vectors. That's fine with vector pointers as we can consider,
2229 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2230 and that does not require and conversion of the pointer values.
2231 In contrast, vector_types_convertible_p and
2232 vector_types_compatible_elements_p are used for vector value types. */
2233 /* True if pointers to distinct types T1 and T2 can be converted to
2234 each other without an explicit cast. Only returns true for opaque
2235 vector types. */
2236 bool
2237 vector_targets_convertible_p (const_tree t1, const_tree t2)
2239 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2240 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2241 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2242 return true;
2244 return false;
2247 /* vector_types_convertible_p is used for vector value types.
2248 It could in principle call vector_targets_convertible_p as a subroutine,
2249 but then the check for vector type would be duplicated with its callers,
2250 and also the purpose of vector_targets_convertible_p would become
2251 muddled.
2252 Where vector_types_convertible_p returns true, a conversion might still be
2253 needed to make the types match.
2254 In contrast, vector_targets_convertible_p is used for vector pointer
2255 values, and vector_types_compatible_elements_p is used specifically
2256 in the context for binary operators, as a check if use is possible without
2257 conversion. */
2258 /* True if vector types T1 and T2 can be converted to each other
2259 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2260 can only be converted with -flax-vector-conversions yet that is not
2261 in effect, emit a note telling the user about that option if such
2262 a note has not previously been emitted. */
2263 bool
2264 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2266 static bool emitted_lax_note = false;
2267 bool convertible_lax;
2269 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2270 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2271 return true;
2273 convertible_lax =
2274 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2275 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2276 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2277 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2278 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2280 if (!convertible_lax || flag_lax_vector_conversions)
2281 return convertible_lax;
2283 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2284 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2285 return true;
2287 if (emit_lax_note && !emitted_lax_note)
2289 emitted_lax_note = true;
2290 inform (input_location, "use -flax-vector-conversions to permit "
2291 "conversions between vectors with differing "
2292 "element types or numbers of subparts");
2295 return false;
2298 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2299 and have vector types, V0 has the same type as V1, and the number of
2300 elements of V0, V1, MASK is the same.
2302 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2303 called with two arguments. In this case implementation passes the
2304 first argument twice in order to share the same tree code. This fact
2305 could enable the mask-values being twice the vector length. This is
2306 an implementation accident and this semantics is not guaranteed to
2307 the user. */
2308 tree
2309 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2310 bool complain)
2312 tree ret;
2313 bool wrap = true;
2314 bool maybe_const = false;
2315 bool two_arguments = false;
2317 if (v1 == NULL_TREE)
2319 two_arguments = true;
2320 v1 = v0;
2323 if (v0 == error_mark_node || v1 == error_mark_node
2324 || mask == error_mark_node)
2325 return error_mark_node;
2327 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2328 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2330 if (complain)
2331 error_at (loc, "__builtin_shuffle last argument must "
2332 "be an integer vector");
2333 return error_mark_node;
2336 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2337 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2339 if (complain)
2340 error_at (loc, "__builtin_shuffle arguments must be vectors");
2341 return error_mark_node;
2344 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2346 if (complain)
2347 error_at (loc, "__builtin_shuffle argument vectors must be of "
2348 "the same type");
2349 return error_mark_node;
2352 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2353 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2354 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2355 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2357 if (complain)
2358 error_at (loc, "__builtin_shuffle number of elements of the "
2359 "argument vector(s) and the mask vector should "
2360 "be the same");
2361 return error_mark_node;
2364 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2365 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2367 if (complain)
2368 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2369 "must have the same size as inner type of the mask");
2370 return error_mark_node;
2373 if (!c_dialect_cxx ())
2375 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2376 v0 = c_fully_fold (v0, false, &maybe_const);
2377 wrap &= maybe_const;
2379 if (two_arguments)
2380 v1 = v0 = save_expr (v0);
2381 else
2383 v1 = c_fully_fold (v1, false, &maybe_const);
2384 wrap &= maybe_const;
2387 mask = c_fully_fold (mask, false, &maybe_const);
2388 wrap &= maybe_const;
2390 else if (two_arguments)
2391 v1 = v0 = save_expr (v0);
2393 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2395 if (!c_dialect_cxx () && !wrap)
2396 ret = c_wrap_maybe_const (ret, true);
2398 return ret;
2401 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2402 to integral type. */
2404 static tree
2405 c_common_get_narrower (tree op, int *unsignedp_ptr)
2407 op = get_narrower (op, unsignedp_ptr);
2409 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2410 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2412 /* C++0x scoped enumerations don't implicitly convert to integral
2413 type; if we stripped an explicit conversion to a larger type we
2414 need to replace it so common_type will still work. */
2415 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2416 TYPE_UNSIGNED (TREE_TYPE (op)));
2417 op = fold_convert (type, op);
2419 return op;
2422 /* This is a helper function of build_binary_op.
2424 For certain operations if both args were extended from the same
2425 smaller type, do the arithmetic in that type and then extend.
2427 BITWISE indicates a bitwise operation.
2428 For them, this optimization is safe only if
2429 both args are zero-extended or both are sign-extended.
2430 Otherwise, we might change the result.
2431 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2432 but calculated in (unsigned short) it would be (unsigned short)-1.
2434 tree
2435 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2437 int unsigned0, unsigned1;
2438 tree arg0, arg1;
2439 int uns;
2440 tree type;
2442 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2443 excessive narrowing when we call get_narrower below. For
2444 example, suppose that OP0 is of unsigned int extended
2445 from signed char and that RESULT_TYPE is long long int.
2446 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2447 like
2449 (long long int) (unsigned int) signed_char
2451 which get_narrower would narrow down to
2453 (unsigned int) signed char
2455 If we do not cast OP0 first, get_narrower would return
2456 signed_char, which is inconsistent with the case of the
2457 explicit cast. */
2458 op0 = convert (result_type, op0);
2459 op1 = convert (result_type, op1);
2461 arg0 = c_common_get_narrower (op0, &unsigned0);
2462 arg1 = c_common_get_narrower (op1, &unsigned1);
2464 /* UNS is 1 if the operation to be done is an unsigned one. */
2465 uns = TYPE_UNSIGNED (result_type);
2467 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2468 but it *requires* conversion to FINAL_TYPE. */
2470 if ((TYPE_PRECISION (TREE_TYPE (op0))
2471 == TYPE_PRECISION (TREE_TYPE (arg0)))
2472 && TREE_TYPE (op0) != result_type)
2473 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2474 if ((TYPE_PRECISION (TREE_TYPE (op1))
2475 == TYPE_PRECISION (TREE_TYPE (arg1)))
2476 && TREE_TYPE (op1) != result_type)
2477 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2479 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2481 /* For bitwise operations, signedness of nominal type
2482 does not matter. Consider only how operands were extended. */
2483 if (bitwise)
2484 uns = unsigned0;
2486 /* Note that in all three cases below we refrain from optimizing
2487 an unsigned operation on sign-extended args.
2488 That would not be valid. */
2490 /* Both args variable: if both extended in same way
2491 from same width, do it in that width.
2492 Do it unsigned if args were zero-extended. */
2493 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2494 < TYPE_PRECISION (result_type))
2495 && (TYPE_PRECISION (TREE_TYPE (arg1))
2496 == TYPE_PRECISION (TREE_TYPE (arg0)))
2497 && unsigned0 == unsigned1
2498 && (unsigned0 || !uns))
2499 return c_common_signed_or_unsigned_type
2500 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2502 else if (TREE_CODE (arg0) == INTEGER_CST
2503 && (unsigned1 || !uns)
2504 && (TYPE_PRECISION (TREE_TYPE (arg1))
2505 < TYPE_PRECISION (result_type))
2506 && (type
2507 = c_common_signed_or_unsigned_type (unsigned1,
2508 TREE_TYPE (arg1)))
2509 && !POINTER_TYPE_P (type)
2510 && int_fits_type_p (arg0, type))
2511 return type;
2513 else if (TREE_CODE (arg1) == INTEGER_CST
2514 && (unsigned0 || !uns)
2515 && (TYPE_PRECISION (TREE_TYPE (arg0))
2516 < TYPE_PRECISION (result_type))
2517 && (type
2518 = c_common_signed_or_unsigned_type (unsigned0,
2519 TREE_TYPE (arg0)))
2520 && !POINTER_TYPE_P (type)
2521 && int_fits_type_p (arg1, type))
2522 return type;
2524 return result_type;
2527 /* Checks if expression EXPR of real/integer type cannot be converted
2528 to the real/integer type TYPE. Function returns true when:
2529 * EXPR is a constant which cannot be exactly converted to TYPE
2530 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2531 for EXPR type and TYPE being both integers or both real.
2532 * EXPR is not a constant of real type and TYPE is an integer.
2533 * EXPR is not a constant of integer type which cannot be
2534 exactly converted to real type.
2535 Function allows conversions between types of different signedness and
2536 does not return true in that case. Function can produce signedness
2537 warnings if PRODUCE_WARNS is true. */
2538 bool
2539 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2541 bool give_warning = false;
2542 tree expr_type = TREE_TYPE (expr);
2543 location_t loc = EXPR_LOC_OR_HERE (expr);
2545 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2547 /* Warn for real constant that is not an exact integer converted
2548 to integer type. */
2549 if (TREE_CODE (expr_type) == REAL_TYPE
2550 && TREE_CODE (type) == INTEGER_TYPE)
2552 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2553 give_warning = true;
2555 /* Warn for an integer constant that does not fit into integer type. */
2556 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2557 && TREE_CODE (type) == INTEGER_TYPE
2558 && !int_fits_type_p (expr, type))
2560 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2561 && tree_int_cst_sgn (expr) < 0)
2563 if (produce_warns)
2564 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2565 " implicitly converted to unsigned type");
2567 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2569 if (produce_warns)
2570 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2571 " constant value to negative integer");
2573 else
2574 give_warning = true;
2576 else if (TREE_CODE (type) == REAL_TYPE)
2578 /* Warn for an integer constant that does not fit into real type. */
2579 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2581 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2582 if (!exact_real_truncate (TYPE_MODE (type), &a))
2583 give_warning = true;
2585 /* Warn for a real constant that does not fit into a smaller
2586 real type. */
2587 else if (TREE_CODE (expr_type) == REAL_TYPE
2588 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2590 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2591 if (!exact_real_truncate (TYPE_MODE (type), &a))
2592 give_warning = true;
2596 else
2598 /* Warn for real types converted to integer types. */
2599 if (TREE_CODE (expr_type) == REAL_TYPE
2600 && TREE_CODE (type) == INTEGER_TYPE)
2601 give_warning = true;
2603 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2604 && TREE_CODE (type) == INTEGER_TYPE)
2606 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2607 expr = get_unwidened (expr, 0);
2608 expr_type = TREE_TYPE (expr);
2610 /* Don't warn for short y; short x = ((int)y & 0xff); */
2611 if (TREE_CODE (expr) == BIT_AND_EXPR
2612 || TREE_CODE (expr) == BIT_IOR_EXPR
2613 || TREE_CODE (expr) == BIT_XOR_EXPR)
2615 /* If both args were extended from a shortest type,
2616 use that type if that is safe. */
2617 expr_type = shorten_binary_op (expr_type,
2618 TREE_OPERAND (expr, 0),
2619 TREE_OPERAND (expr, 1),
2620 /* bitwise */1);
2622 if (TREE_CODE (expr) == BIT_AND_EXPR)
2624 tree op0 = TREE_OPERAND (expr, 0);
2625 tree op1 = TREE_OPERAND (expr, 1);
2626 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2627 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2629 /* If one of the operands is a non-negative constant
2630 that fits in the target type, then the type of the
2631 other operand does not matter. */
2632 if ((TREE_CODE (op0) == INTEGER_CST
2633 && int_fits_type_p (op0, c_common_signed_type (type))
2634 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2635 || (TREE_CODE (op1) == INTEGER_CST
2636 && int_fits_type_p (op1, c_common_signed_type (type))
2637 && int_fits_type_p (op1,
2638 c_common_unsigned_type (type))))
2639 return false;
2640 /* If constant is unsigned and fits in the target
2641 type, then the result will also fit. */
2642 else if ((TREE_CODE (op0) == INTEGER_CST
2643 && unsigned0
2644 && int_fits_type_p (op0, type))
2645 || (TREE_CODE (op1) == INTEGER_CST
2646 && unsigned1
2647 && int_fits_type_p (op1, type)))
2648 return false;
2651 /* Warn for integer types converted to smaller integer types. */
2652 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2653 give_warning = true;
2655 /* When they are the same width but different signedness,
2656 then the value may change. */
2657 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2658 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2659 /* Even when converted to a bigger type, if the type is
2660 unsigned but expr is signed, then negative values
2661 will be changed. */
2662 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2663 && produce_warns)
2664 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2665 "may change the sign of the result",
2666 type, expr_type);
2669 /* Warn for integer types converted to real types if and only if
2670 all the range of values of the integer type cannot be
2671 represented by the real type. */
2672 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2673 && TREE_CODE (type) == REAL_TYPE)
2675 tree type_low_bound, type_high_bound;
2676 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2678 /* Don't warn about char y = 0xff; float x = (int) y; */
2679 expr = get_unwidened (expr, 0);
2680 expr_type = TREE_TYPE (expr);
2682 type_low_bound = TYPE_MIN_VALUE (expr_type);
2683 type_high_bound = TYPE_MAX_VALUE (expr_type);
2684 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2685 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2687 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2688 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2689 give_warning = true;
2692 /* Warn for real types converted to smaller real types. */
2693 else if (TREE_CODE (expr_type) == REAL_TYPE
2694 && TREE_CODE (type) == REAL_TYPE
2695 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2696 give_warning = true;
2699 return give_warning;
2702 /* Warns if the conversion of EXPR to TYPE may alter a value.
2703 This is a helper function for warnings_for_convert_and_check. */
2705 static void
2706 conversion_warning (tree type, tree expr)
2708 tree expr_type = TREE_TYPE (expr);
2709 location_t loc = EXPR_LOC_OR_HERE (expr);
2711 if (!warn_conversion && !warn_sign_conversion)
2712 return;
2714 switch (TREE_CODE (expr))
2716 case EQ_EXPR:
2717 case NE_EXPR:
2718 case LE_EXPR:
2719 case GE_EXPR:
2720 case LT_EXPR:
2721 case GT_EXPR:
2722 case TRUTH_ANDIF_EXPR:
2723 case TRUTH_ORIF_EXPR:
2724 case TRUTH_AND_EXPR:
2725 case TRUTH_OR_EXPR:
2726 case TRUTH_XOR_EXPR:
2727 case TRUTH_NOT_EXPR:
2728 /* Conversion from boolean to a signed:1 bit-field (which only
2729 can hold the values 0 and -1) doesn't lose information - but
2730 it does change the value. */
2731 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2732 warning_at (loc, OPT_Wconversion,
2733 "conversion to %qT from boolean expression", type);
2734 return;
2736 case REAL_CST:
2737 case INTEGER_CST:
2738 if (unsafe_conversion_p (type, expr, true))
2739 warning_at (loc, OPT_Wconversion,
2740 "conversion to %qT alters %qT constant value",
2741 type, expr_type);
2742 return;
2744 case COND_EXPR:
2746 /* In case of COND_EXPR, we do not care about the type of
2747 COND_EXPR, only about the conversion of each operand. */
2748 tree op1 = TREE_OPERAND (expr, 1);
2749 tree op2 = TREE_OPERAND (expr, 2);
2751 conversion_warning (type, op1);
2752 conversion_warning (type, op2);
2753 return;
2756 default: /* 'expr' is not a constant. */
2757 if (unsafe_conversion_p (type, expr, true))
2758 warning_at (loc, OPT_Wconversion,
2759 "conversion to %qT from %qT may alter its value",
2760 type, expr_type);
2764 /* Produce warnings after a conversion. RESULT is the result of
2765 converting EXPR to TYPE. This is a helper function for
2766 convert_and_check and cp_convert_and_check. */
2768 void
2769 warnings_for_convert_and_check (tree type, tree expr, tree result)
2771 location_t loc = EXPR_LOC_OR_HERE (expr);
2773 if (TREE_CODE (expr) == INTEGER_CST
2774 && (TREE_CODE (type) == INTEGER_TYPE
2775 || TREE_CODE (type) == ENUMERAL_TYPE)
2776 && !int_fits_type_p (expr, type))
2778 /* Do not diagnose overflow in a constant expression merely
2779 because a conversion overflowed. */
2780 if (TREE_OVERFLOW (result))
2781 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2783 if (TYPE_UNSIGNED (type))
2785 /* This detects cases like converting -129 or 256 to
2786 unsigned char. */
2787 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2788 warning_at (loc, OPT_Woverflow,
2789 "large integer implicitly truncated to unsigned type");
2790 else
2791 conversion_warning (type, expr);
2793 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2794 warning (OPT_Woverflow,
2795 "overflow in implicit constant conversion");
2796 /* No warning for converting 0x80000000 to int. */
2797 else if (pedantic
2798 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2799 || TYPE_PRECISION (TREE_TYPE (expr))
2800 != TYPE_PRECISION (type)))
2801 warning_at (loc, OPT_Woverflow,
2802 "overflow in implicit constant conversion");
2804 else
2805 conversion_warning (type, expr);
2807 else if ((TREE_CODE (result) == INTEGER_CST
2808 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2809 warning_at (loc, OPT_Woverflow,
2810 "overflow in implicit constant conversion");
2811 else
2812 conversion_warning (type, expr);
2816 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2817 Invoke this function on every expression that is converted implicitly,
2818 i.e. because of language rules and not because of an explicit cast. */
2820 tree
2821 convert_and_check (tree type, tree expr)
2823 tree result;
2824 tree expr_for_warning;
2826 /* Convert from a value with possible excess precision rather than
2827 via the semantic type, but do not warn about values not fitting
2828 exactly in the semantic type. */
2829 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2831 tree orig_type = TREE_TYPE (expr);
2832 expr = TREE_OPERAND (expr, 0);
2833 expr_for_warning = convert (orig_type, expr);
2834 if (orig_type == type)
2835 return expr_for_warning;
2837 else
2838 expr_for_warning = expr;
2840 if (TREE_TYPE (expr) == type)
2841 return expr;
2843 result = convert (type, expr);
2845 if (c_inhibit_evaluation_warnings == 0
2846 && !TREE_OVERFLOW_P (expr)
2847 && result != error_mark_node)
2848 warnings_for_convert_and_check (type, expr_for_warning, result);
2850 return result;
2853 /* A node in a list that describes references to variables (EXPR), which are
2854 either read accesses if WRITER is zero, or write accesses, in which case
2855 WRITER is the parent of EXPR. */
2856 struct tlist
2858 struct tlist *next;
2859 tree expr, writer;
2862 /* Used to implement a cache the results of a call to verify_tree. We only
2863 use this for SAVE_EXPRs. */
2864 struct tlist_cache
2866 struct tlist_cache *next;
2867 struct tlist *cache_before_sp;
2868 struct tlist *cache_after_sp;
2869 tree expr;
2872 /* Obstack to use when allocating tlist structures, and corresponding
2873 firstobj. */
2874 static struct obstack tlist_obstack;
2875 static char *tlist_firstobj = 0;
2877 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2878 warnings. */
2879 static struct tlist *warned_ids;
2880 /* SAVE_EXPRs need special treatment. We process them only once and then
2881 cache the results. */
2882 static struct tlist_cache *save_expr_cache;
2884 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2885 static void merge_tlist (struct tlist **, struct tlist *, int);
2886 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2887 static int warning_candidate_p (tree);
2888 static bool candidate_equal_p (const_tree, const_tree);
2889 static void warn_for_collisions (struct tlist *);
2890 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2891 static struct tlist *new_tlist (struct tlist *, tree, tree);
2893 /* Create a new struct tlist and fill in its fields. */
2894 static struct tlist *
2895 new_tlist (struct tlist *next, tree t, tree writer)
2897 struct tlist *l;
2898 l = XOBNEW (&tlist_obstack, struct tlist);
2899 l->next = next;
2900 l->expr = t;
2901 l->writer = writer;
2902 return l;
2905 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2906 is nonnull, we ignore any node we find which has a writer equal to it. */
2908 static void
2909 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2911 while (add)
2913 struct tlist *next = add->next;
2914 if (!copy)
2915 add->next = *to;
2916 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2917 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2918 add = next;
2922 /* Merge the nodes of ADD into TO. This merging process is done so that for
2923 each variable that already exists in TO, no new node is added; however if
2924 there is a write access recorded in ADD, and an occurrence on TO is only
2925 a read access, then the occurrence in TO will be modified to record the
2926 write. */
2928 static void
2929 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2931 struct tlist **end = to;
2933 while (*end)
2934 end = &(*end)->next;
2936 while (add)
2938 int found = 0;
2939 struct tlist *tmp2;
2940 struct tlist *next = add->next;
2942 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2943 if (candidate_equal_p (tmp2->expr, add->expr))
2945 found = 1;
2946 if (!tmp2->writer)
2947 tmp2->writer = add->writer;
2949 if (!found)
2951 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2952 end = &(*end)->next;
2953 *end = 0;
2955 add = next;
2959 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2960 references in list LIST conflict with it, excluding reads if ONLY writers
2961 is nonzero. */
2963 static void
2964 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2965 int only_writes)
2967 struct tlist *tmp;
2969 /* Avoid duplicate warnings. */
2970 for (tmp = warned_ids; tmp; tmp = tmp->next)
2971 if (candidate_equal_p (tmp->expr, written))
2972 return;
2974 while (list)
2976 if (candidate_equal_p (list->expr, written)
2977 && !candidate_equal_p (list->writer, writer)
2978 && (!only_writes || list->writer))
2980 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2981 warning_at (EXPR_LOC_OR_HERE (writer),
2982 OPT_Wsequence_point, "operation on %qE may be undefined",
2983 list->expr);
2985 list = list->next;
2989 /* Given a list LIST of references to variables, find whether any of these
2990 can cause conflicts due to missing sequence points. */
2992 static void
2993 warn_for_collisions (struct tlist *list)
2995 struct tlist *tmp;
2997 for (tmp = list; tmp; tmp = tmp->next)
2999 if (tmp->writer)
3000 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3004 /* Return nonzero if X is a tree that can be verified by the sequence point
3005 warnings. */
3006 static int
3007 warning_candidate_p (tree x)
3009 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3010 return 0;
3012 if (TREE_CODE (x) == BLOCK)
3013 return 0;
3015 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3016 (lvalue_p) crash on TRY/CATCH. */
3017 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3018 return 0;
3020 if (!lvalue_p (x))
3021 return 0;
3023 /* No point to track non-const calls, they will never satisfy
3024 operand_equal_p. */
3025 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3026 return 0;
3028 if (TREE_CODE (x) == STRING_CST)
3029 return 0;
3031 return 1;
3034 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3035 static bool
3036 candidate_equal_p (const_tree x, const_tree y)
3038 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3041 /* Walk the tree X, and record accesses to variables. If X is written by the
3042 parent tree, WRITER is the parent.
3043 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3044 expression or its only operand forces a sequence point, then everything up
3045 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3046 in PNO_SP.
3047 Once we return, we will have emitted warnings if any subexpression before
3048 such a sequence point could be undefined. On a higher level, however, the
3049 sequence point may not be relevant, and we'll merge the two lists.
3051 Example: (b++, a) + b;
3052 The call that processes the COMPOUND_EXPR will store the increment of B
3053 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3054 processes the PLUS_EXPR will need to merge the two lists so that
3055 eventually, all accesses end up on the same list (and we'll warn about the
3056 unordered subexpressions b++ and b.
3058 A note on merging. If we modify the former example so that our expression
3059 becomes
3060 (b++, b) + a
3061 care must be taken not simply to add all three expressions into the final
3062 PNO_SP list. The function merge_tlist takes care of that by merging the
3063 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3064 way, so that no more than one access to B is recorded. */
3066 static void
3067 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3068 tree writer)
3070 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3071 enum tree_code code;
3072 enum tree_code_class cl;
3074 /* X may be NULL if it is the operand of an empty statement expression
3075 ({ }). */
3076 if (x == NULL)
3077 return;
3079 restart:
3080 code = TREE_CODE (x);
3081 cl = TREE_CODE_CLASS (code);
3083 if (warning_candidate_p (x))
3084 *pno_sp = new_tlist (*pno_sp, x, writer);
3086 switch (code)
3088 case CONSTRUCTOR:
3089 case SIZEOF_EXPR:
3090 return;
3092 case COMPOUND_EXPR:
3093 case TRUTH_ANDIF_EXPR:
3094 case TRUTH_ORIF_EXPR:
3095 tmp_before = tmp_nosp = tmp_list3 = 0;
3096 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3097 warn_for_collisions (tmp_nosp);
3098 merge_tlist (pbefore_sp, tmp_before, 0);
3099 merge_tlist (pbefore_sp, tmp_nosp, 0);
3100 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3101 merge_tlist (pbefore_sp, tmp_list3, 0);
3102 return;
3104 case COND_EXPR:
3105 tmp_before = tmp_list2 = 0;
3106 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3107 warn_for_collisions (tmp_list2);
3108 merge_tlist (pbefore_sp, tmp_before, 0);
3109 merge_tlist (pbefore_sp, tmp_list2, 1);
3111 tmp_list3 = tmp_nosp = 0;
3112 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3113 warn_for_collisions (tmp_nosp);
3114 merge_tlist (pbefore_sp, tmp_list3, 0);
3116 tmp_list3 = tmp_list2 = 0;
3117 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3118 warn_for_collisions (tmp_list2);
3119 merge_tlist (pbefore_sp, tmp_list3, 0);
3120 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3121 two first, to avoid warning for (a ? b++ : b++). */
3122 merge_tlist (&tmp_nosp, tmp_list2, 0);
3123 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3124 return;
3126 case PREDECREMENT_EXPR:
3127 case PREINCREMENT_EXPR:
3128 case POSTDECREMENT_EXPR:
3129 case POSTINCREMENT_EXPR:
3130 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3131 return;
3133 case MODIFY_EXPR:
3134 tmp_before = tmp_nosp = tmp_list3 = 0;
3135 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3136 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3137 /* Expressions inside the LHS are not ordered wrt. the sequence points
3138 in the RHS. Example:
3139 *a = (a++, 2)
3140 Despite the fact that the modification of "a" is in the before_sp
3141 list (tmp_before), it conflicts with the use of "a" in the LHS.
3142 We can handle this by adding the contents of tmp_list3
3143 to those of tmp_before, and redoing the collision warnings for that
3144 list. */
3145 add_tlist (&tmp_before, tmp_list3, x, 1);
3146 warn_for_collisions (tmp_before);
3147 /* Exclude the LHS itself here; we first have to merge it into the
3148 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3149 didn't exclude the LHS, we'd get it twice, once as a read and once
3150 as a write. */
3151 add_tlist (pno_sp, tmp_list3, x, 0);
3152 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3154 merge_tlist (pbefore_sp, tmp_before, 0);
3155 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3156 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3157 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3158 return;
3160 case CALL_EXPR:
3161 /* We need to warn about conflicts among arguments and conflicts between
3162 args and the function address. Side effects of the function address,
3163 however, are not ordered by the sequence point of the call. */
3165 call_expr_arg_iterator iter;
3166 tree arg;
3167 tmp_before = tmp_nosp = 0;
3168 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3169 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3171 tmp_list2 = tmp_list3 = 0;
3172 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3173 merge_tlist (&tmp_list3, tmp_list2, 0);
3174 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3176 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3177 warn_for_collisions (tmp_before);
3178 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3179 return;
3182 case TREE_LIST:
3183 /* Scan all the list, e.g. indices of multi dimensional array. */
3184 while (x)
3186 tmp_before = tmp_nosp = 0;
3187 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3188 merge_tlist (&tmp_nosp, tmp_before, 0);
3189 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3190 x = TREE_CHAIN (x);
3192 return;
3194 case SAVE_EXPR:
3196 struct tlist_cache *t;
3197 for (t = save_expr_cache; t; t = t->next)
3198 if (candidate_equal_p (t->expr, x))
3199 break;
3201 if (!t)
3203 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3204 t->next = save_expr_cache;
3205 t->expr = x;
3206 save_expr_cache = t;
3208 tmp_before = tmp_nosp = 0;
3209 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3210 warn_for_collisions (tmp_nosp);
3212 tmp_list3 = 0;
3213 while (tmp_nosp)
3215 struct tlist *t = tmp_nosp;
3216 tmp_nosp = t->next;
3217 merge_tlist (&tmp_list3, t, 0);
3219 t->cache_before_sp = tmp_before;
3220 t->cache_after_sp = tmp_list3;
3222 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3223 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3224 return;
3227 case ADDR_EXPR:
3228 x = TREE_OPERAND (x, 0);
3229 if (DECL_P (x))
3230 return;
3231 writer = 0;
3232 goto restart;
3234 default:
3235 /* For other expressions, simply recurse on their operands.
3236 Manual tail recursion for unary expressions.
3237 Other non-expressions need not be processed. */
3238 if (cl == tcc_unary)
3240 x = TREE_OPERAND (x, 0);
3241 writer = 0;
3242 goto restart;
3244 else if (IS_EXPR_CODE_CLASS (cl))
3246 int lp;
3247 int max = TREE_OPERAND_LENGTH (x);
3248 for (lp = 0; lp < max; lp++)
3250 tmp_before = tmp_nosp = 0;
3251 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3252 merge_tlist (&tmp_nosp, tmp_before, 0);
3253 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3256 return;
3260 /* Try to warn for undefined behavior in EXPR due to missing sequence
3261 points. */
3263 DEBUG_FUNCTION void
3264 verify_sequence_points (tree expr)
3266 struct tlist *before_sp = 0, *after_sp = 0;
3268 warned_ids = 0;
3269 save_expr_cache = 0;
3270 if (tlist_firstobj == 0)
3272 gcc_obstack_init (&tlist_obstack);
3273 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3276 verify_tree (expr, &before_sp, &after_sp, 0);
3277 warn_for_collisions (after_sp);
3278 obstack_free (&tlist_obstack, tlist_firstobj);
3281 /* Validate the expression after `case' and apply default promotions. */
3283 static tree
3284 check_case_value (tree value)
3286 if (value == NULL_TREE)
3287 return value;
3289 if (TREE_CODE (value) == INTEGER_CST)
3290 /* Promote char or short to int. */
3291 value = perform_integral_promotions (value);
3292 else if (value != error_mark_node)
3294 error ("case label does not reduce to an integer constant");
3295 value = error_mark_node;
3298 constant_expression_warning (value);
3300 return value;
3303 /* See if the case values LOW and HIGH are in the range of the original
3304 type (i.e. before the default conversion to int) of the switch testing
3305 expression.
3306 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3307 the type before promoting it. CASE_LOW_P is a pointer to the lower
3308 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3309 if the case is not a case range.
3310 The caller has to make sure that we are not called with NULL for
3311 CASE_LOW_P (i.e. the default case).
3312 Returns true if the case label is in range of ORIG_TYPE (saturated or
3313 untouched) or false if the label is out of range. */
3315 static bool
3316 check_case_bounds (tree type, tree orig_type,
3317 tree *case_low_p, tree *case_high_p)
3319 tree min_value, max_value;
3320 tree case_low = *case_low_p;
3321 tree case_high = case_high_p ? *case_high_p : case_low;
3323 /* If there was a problem with the original type, do nothing. */
3324 if (orig_type == error_mark_node)
3325 return true;
3327 min_value = TYPE_MIN_VALUE (orig_type);
3328 max_value = TYPE_MAX_VALUE (orig_type);
3330 /* Case label is less than minimum for type. */
3331 if (tree_int_cst_compare (case_low, min_value) < 0
3332 && tree_int_cst_compare (case_high, min_value) < 0)
3334 warning (0, "case label value is less than minimum value for type");
3335 return false;
3338 /* Case value is greater than maximum for type. */
3339 if (tree_int_cst_compare (case_low, max_value) > 0
3340 && tree_int_cst_compare (case_high, max_value) > 0)
3342 warning (0, "case label value exceeds maximum value for type");
3343 return false;
3346 /* Saturate lower case label value to minimum. */
3347 if (tree_int_cst_compare (case_high, min_value) >= 0
3348 && tree_int_cst_compare (case_low, min_value) < 0)
3350 warning (0, "lower value in case label range"
3351 " less than minimum value for type");
3352 case_low = min_value;
3355 /* Saturate upper case label value to maximum. */
3356 if (tree_int_cst_compare (case_low, max_value) <= 0
3357 && tree_int_cst_compare (case_high, max_value) > 0)
3359 warning (0, "upper value in case label range"
3360 " exceeds maximum value for type");
3361 case_high = max_value;
3364 if (*case_low_p != case_low)
3365 *case_low_p = convert (type, case_low);
3366 if (case_high_p && *case_high_p != case_high)
3367 *case_high_p = convert (type, case_high);
3369 return true;
3372 /* Return an integer type with BITS bits of precision,
3373 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3375 tree
3376 c_common_type_for_size (unsigned int bits, int unsignedp)
3378 if (bits == TYPE_PRECISION (integer_type_node))
3379 return unsignedp ? unsigned_type_node : integer_type_node;
3381 if (bits == TYPE_PRECISION (signed_char_type_node))
3382 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3384 if (bits == TYPE_PRECISION (short_integer_type_node))
3385 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3387 if (bits == TYPE_PRECISION (long_integer_type_node))
3388 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3390 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3391 return (unsignedp ? long_long_unsigned_type_node
3392 : long_long_integer_type_node);
3394 if (int128_integer_type_node
3395 && bits == TYPE_PRECISION (int128_integer_type_node))
3396 return (unsignedp ? int128_unsigned_type_node
3397 : int128_integer_type_node);
3399 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3400 return (unsignedp ? widest_unsigned_literal_type_node
3401 : widest_integer_literal_type_node);
3403 if (bits <= TYPE_PRECISION (intQI_type_node))
3404 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3406 if (bits <= TYPE_PRECISION (intHI_type_node))
3407 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3409 if (bits <= TYPE_PRECISION (intSI_type_node))
3410 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3412 if (bits <= TYPE_PRECISION (intDI_type_node))
3413 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3415 return 0;
3418 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3419 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3420 and saturating if SATP is nonzero, otherwise not saturating. */
3422 tree
3423 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3424 int unsignedp, int satp)
3426 enum machine_mode mode;
3427 if (ibit == 0)
3428 mode = unsignedp ? UQQmode : QQmode;
3429 else
3430 mode = unsignedp ? UHAmode : HAmode;
3432 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3433 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3434 break;
3436 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3438 sorry ("GCC cannot support operators with integer types and "
3439 "fixed-point types that have too many integral and "
3440 "fractional bits together");
3441 return 0;
3444 return c_common_type_for_mode (mode, satp);
3447 /* Used for communication between c_common_type_for_mode and
3448 c_register_builtin_type. */
3449 static GTY(()) tree registered_builtin_types;
3451 /* Return a data type that has machine mode MODE.
3452 If the mode is an integer,
3453 then UNSIGNEDP selects between signed and unsigned types.
3454 If the mode is a fixed-point mode,
3455 then UNSIGNEDP selects between saturating and nonsaturating types. */
3457 tree
3458 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3460 tree t;
3462 if (mode == TYPE_MODE (integer_type_node))
3463 return unsignedp ? unsigned_type_node : integer_type_node;
3465 if (mode == TYPE_MODE (signed_char_type_node))
3466 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3468 if (mode == TYPE_MODE (short_integer_type_node))
3469 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3471 if (mode == TYPE_MODE (long_integer_type_node))
3472 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3474 if (mode == TYPE_MODE (long_long_integer_type_node))
3475 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3477 if (int128_integer_type_node
3478 && mode == TYPE_MODE (int128_integer_type_node))
3479 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3481 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3482 return unsignedp ? widest_unsigned_literal_type_node
3483 : widest_integer_literal_type_node;
3485 if (mode == QImode)
3486 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3488 if (mode == HImode)
3489 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3491 if (mode == SImode)
3492 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3494 if (mode == DImode)
3495 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3497 #if HOST_BITS_PER_WIDE_INT >= 64
3498 if (mode == TYPE_MODE (intTI_type_node))
3499 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3500 #endif
3502 if (mode == TYPE_MODE (float_type_node))
3503 return float_type_node;
3505 if (mode == TYPE_MODE (double_type_node))
3506 return double_type_node;
3508 if (mode == TYPE_MODE (long_double_type_node))
3509 return long_double_type_node;
3511 if (mode == TYPE_MODE (void_type_node))
3512 return void_type_node;
3514 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3515 return (unsignedp
3516 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3517 : make_signed_type (GET_MODE_PRECISION (mode)));
3519 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3520 return (unsignedp
3521 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3522 : make_signed_type (GET_MODE_PRECISION (mode)));
3524 if (COMPLEX_MODE_P (mode))
3526 enum machine_mode inner_mode;
3527 tree inner_type;
3529 if (mode == TYPE_MODE (complex_float_type_node))
3530 return complex_float_type_node;
3531 if (mode == TYPE_MODE (complex_double_type_node))
3532 return complex_double_type_node;
3533 if (mode == TYPE_MODE (complex_long_double_type_node))
3534 return complex_long_double_type_node;
3536 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3537 return complex_integer_type_node;
3539 inner_mode = GET_MODE_INNER (mode);
3540 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3541 if (inner_type != NULL_TREE)
3542 return build_complex_type (inner_type);
3544 else if (VECTOR_MODE_P (mode))
3546 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3547 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3548 if (inner_type != NULL_TREE)
3549 return build_vector_type_for_mode (inner_type, mode);
3552 if (mode == TYPE_MODE (dfloat32_type_node))
3553 return dfloat32_type_node;
3554 if (mode == TYPE_MODE (dfloat64_type_node))
3555 return dfloat64_type_node;
3556 if (mode == TYPE_MODE (dfloat128_type_node))
3557 return dfloat128_type_node;
3559 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3561 if (mode == TYPE_MODE (short_fract_type_node))
3562 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3563 if (mode == TYPE_MODE (fract_type_node))
3564 return unsignedp ? sat_fract_type_node : fract_type_node;
3565 if (mode == TYPE_MODE (long_fract_type_node))
3566 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3567 if (mode == TYPE_MODE (long_long_fract_type_node))
3568 return unsignedp ? sat_long_long_fract_type_node
3569 : long_long_fract_type_node;
3571 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3572 return unsignedp ? sat_unsigned_short_fract_type_node
3573 : unsigned_short_fract_type_node;
3574 if (mode == TYPE_MODE (unsigned_fract_type_node))
3575 return unsignedp ? sat_unsigned_fract_type_node
3576 : unsigned_fract_type_node;
3577 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3578 return unsignedp ? sat_unsigned_long_fract_type_node
3579 : unsigned_long_fract_type_node;
3580 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3581 return unsignedp ? sat_unsigned_long_long_fract_type_node
3582 : unsigned_long_long_fract_type_node;
3584 if (mode == TYPE_MODE (short_accum_type_node))
3585 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3586 if (mode == TYPE_MODE (accum_type_node))
3587 return unsignedp ? sat_accum_type_node : accum_type_node;
3588 if (mode == TYPE_MODE (long_accum_type_node))
3589 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3590 if (mode == TYPE_MODE (long_long_accum_type_node))
3591 return unsignedp ? sat_long_long_accum_type_node
3592 : long_long_accum_type_node;
3594 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3595 return unsignedp ? sat_unsigned_short_accum_type_node
3596 : unsigned_short_accum_type_node;
3597 if (mode == TYPE_MODE (unsigned_accum_type_node))
3598 return unsignedp ? sat_unsigned_accum_type_node
3599 : unsigned_accum_type_node;
3600 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3601 return unsignedp ? sat_unsigned_long_accum_type_node
3602 : unsigned_long_accum_type_node;
3603 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3604 return unsignedp ? sat_unsigned_long_long_accum_type_node
3605 : unsigned_long_long_accum_type_node;
3607 if (mode == QQmode)
3608 return unsignedp ? sat_qq_type_node : qq_type_node;
3609 if (mode == HQmode)
3610 return unsignedp ? sat_hq_type_node : hq_type_node;
3611 if (mode == SQmode)
3612 return unsignedp ? sat_sq_type_node : sq_type_node;
3613 if (mode == DQmode)
3614 return unsignedp ? sat_dq_type_node : dq_type_node;
3615 if (mode == TQmode)
3616 return unsignedp ? sat_tq_type_node : tq_type_node;
3618 if (mode == UQQmode)
3619 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3620 if (mode == UHQmode)
3621 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3622 if (mode == USQmode)
3623 return unsignedp ? sat_usq_type_node : usq_type_node;
3624 if (mode == UDQmode)
3625 return unsignedp ? sat_udq_type_node : udq_type_node;
3626 if (mode == UTQmode)
3627 return unsignedp ? sat_utq_type_node : utq_type_node;
3629 if (mode == HAmode)
3630 return unsignedp ? sat_ha_type_node : ha_type_node;
3631 if (mode == SAmode)
3632 return unsignedp ? sat_sa_type_node : sa_type_node;
3633 if (mode == DAmode)
3634 return unsignedp ? sat_da_type_node : da_type_node;
3635 if (mode == TAmode)
3636 return unsignedp ? sat_ta_type_node : ta_type_node;
3638 if (mode == UHAmode)
3639 return unsignedp ? sat_uha_type_node : uha_type_node;
3640 if (mode == USAmode)
3641 return unsignedp ? sat_usa_type_node : usa_type_node;
3642 if (mode == UDAmode)
3643 return unsignedp ? sat_uda_type_node : uda_type_node;
3644 if (mode == UTAmode)
3645 return unsignedp ? sat_uta_type_node : uta_type_node;
3648 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3649 if (TYPE_MODE (TREE_VALUE (t)) == mode
3650 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3651 return TREE_VALUE (t);
3653 return 0;
3656 tree
3657 c_common_unsigned_type (tree type)
3659 return c_common_signed_or_unsigned_type (1, type);
3662 /* Return a signed type the same as TYPE in other respects. */
3664 tree
3665 c_common_signed_type (tree type)
3667 return c_common_signed_or_unsigned_type (0, type);
3670 /* Return a type the same as TYPE except unsigned or
3671 signed according to UNSIGNEDP. */
3673 tree
3674 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3676 tree type1;
3678 /* This block of code emulates the behavior of the old
3679 c_common_unsigned_type. In particular, it returns
3680 long_unsigned_type_node if passed a long, even when a int would
3681 have the same size. This is necessary for warnings to work
3682 correctly in archs where sizeof(int) == sizeof(long) */
3684 type1 = TYPE_MAIN_VARIANT (type);
3685 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3686 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3687 if (type1 == integer_type_node || type1 == unsigned_type_node)
3688 return unsignedp ? unsigned_type_node : integer_type_node;
3689 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3690 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3691 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3692 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3693 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3694 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3695 if (int128_integer_type_node
3696 && (type1 == int128_integer_type_node
3697 || type1 == int128_unsigned_type_node))
3698 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3699 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3700 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3701 #if HOST_BITS_PER_WIDE_INT >= 64
3702 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3703 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3704 #endif
3705 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3706 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3707 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3708 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3709 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3710 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3711 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3712 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3714 #define C_COMMON_FIXED_TYPES(NAME) \
3715 if (type1 == short_ ## NAME ## _type_node \
3716 || type1 == unsigned_short_ ## NAME ## _type_node) \
3717 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3718 : short_ ## NAME ## _type_node; \
3719 if (type1 == NAME ## _type_node \
3720 || type1 == unsigned_ ## NAME ## _type_node) \
3721 return unsignedp ? unsigned_ ## NAME ## _type_node \
3722 : NAME ## _type_node; \
3723 if (type1 == long_ ## NAME ## _type_node \
3724 || type1 == unsigned_long_ ## NAME ## _type_node) \
3725 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3726 : long_ ## NAME ## _type_node; \
3727 if (type1 == long_long_ ## NAME ## _type_node \
3728 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3729 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3730 : long_long_ ## NAME ## _type_node;
3732 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3733 if (type1 == NAME ## _type_node \
3734 || type1 == u ## NAME ## _type_node) \
3735 return unsignedp ? u ## NAME ## _type_node \
3736 : NAME ## _type_node;
3738 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3739 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3740 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3741 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3742 : sat_ ## short_ ## NAME ## _type_node; \
3743 if (type1 == sat_ ## NAME ## _type_node \
3744 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3745 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3746 : sat_ ## NAME ## _type_node; \
3747 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3748 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3749 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3750 : sat_ ## long_ ## NAME ## _type_node; \
3751 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3752 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3753 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3754 : sat_ ## long_long_ ## NAME ## _type_node;
3756 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3757 if (type1 == sat_ ## NAME ## _type_node \
3758 || type1 == sat_ ## u ## NAME ## _type_node) \
3759 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3760 : sat_ ## NAME ## _type_node;
3762 C_COMMON_FIXED_TYPES (fract);
3763 C_COMMON_FIXED_TYPES_SAT (fract);
3764 C_COMMON_FIXED_TYPES (accum);
3765 C_COMMON_FIXED_TYPES_SAT (accum);
3767 C_COMMON_FIXED_MODE_TYPES (qq);
3768 C_COMMON_FIXED_MODE_TYPES (hq);
3769 C_COMMON_FIXED_MODE_TYPES (sq);
3770 C_COMMON_FIXED_MODE_TYPES (dq);
3771 C_COMMON_FIXED_MODE_TYPES (tq);
3772 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3773 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3774 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3775 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3776 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3777 C_COMMON_FIXED_MODE_TYPES (ha);
3778 C_COMMON_FIXED_MODE_TYPES (sa);
3779 C_COMMON_FIXED_MODE_TYPES (da);
3780 C_COMMON_FIXED_MODE_TYPES (ta);
3781 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3782 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3783 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3784 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3786 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3787 the precision; they have precision set to match their range, but
3788 may use a wider mode to match an ABI. If we change modes, we may
3789 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3790 the precision as well, so as to yield correct results for
3791 bit-field types. C++ does not have these separate bit-field
3792 types, and producing a signed or unsigned variant of an
3793 ENUMERAL_TYPE may cause other problems as well. */
3795 if (!INTEGRAL_TYPE_P (type)
3796 || TYPE_UNSIGNED (type) == unsignedp)
3797 return type;
3799 #define TYPE_OK(node) \
3800 (TYPE_MODE (type) == TYPE_MODE (node) \
3801 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3802 if (TYPE_OK (signed_char_type_node))
3803 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3804 if (TYPE_OK (integer_type_node))
3805 return unsignedp ? unsigned_type_node : integer_type_node;
3806 if (TYPE_OK (short_integer_type_node))
3807 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3808 if (TYPE_OK (long_integer_type_node))
3809 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3810 if (TYPE_OK (long_long_integer_type_node))
3811 return (unsignedp ? long_long_unsigned_type_node
3812 : long_long_integer_type_node);
3813 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3814 return (unsignedp ? int128_unsigned_type_node
3815 : int128_integer_type_node);
3816 if (TYPE_OK (widest_integer_literal_type_node))
3817 return (unsignedp ? widest_unsigned_literal_type_node
3818 : widest_integer_literal_type_node);
3820 #if HOST_BITS_PER_WIDE_INT >= 64
3821 if (TYPE_OK (intTI_type_node))
3822 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3823 #endif
3824 if (TYPE_OK (intDI_type_node))
3825 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3826 if (TYPE_OK (intSI_type_node))
3827 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3828 if (TYPE_OK (intHI_type_node))
3829 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3830 if (TYPE_OK (intQI_type_node))
3831 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3832 #undef TYPE_OK
3834 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3837 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3839 tree
3840 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3842 /* Extended integer types of the same width as a standard type have
3843 lesser rank, so those of the same width as int promote to int or
3844 unsigned int and are valid for printf formats expecting int or
3845 unsigned int. To avoid such special cases, avoid creating
3846 extended integer types for bit-fields if a standard integer type
3847 is available. */
3848 if (width == TYPE_PRECISION (integer_type_node))
3849 return unsignedp ? unsigned_type_node : integer_type_node;
3850 if (width == TYPE_PRECISION (signed_char_type_node))
3851 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3852 if (width == TYPE_PRECISION (short_integer_type_node))
3853 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3854 if (width == TYPE_PRECISION (long_integer_type_node))
3855 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3856 if (width == TYPE_PRECISION (long_long_integer_type_node))
3857 return (unsignedp ? long_long_unsigned_type_node
3858 : long_long_integer_type_node);
3859 if (int128_integer_type_node
3860 && width == TYPE_PRECISION (int128_integer_type_node))
3861 return (unsignedp ? int128_unsigned_type_node
3862 : int128_integer_type_node);
3863 return build_nonstandard_integer_type (width, unsignedp);
3866 /* The C version of the register_builtin_type langhook. */
3868 void
3869 c_register_builtin_type (tree type, const char* name)
3871 tree decl;
3873 decl = build_decl (UNKNOWN_LOCATION,
3874 TYPE_DECL, get_identifier (name), type);
3875 DECL_ARTIFICIAL (decl) = 1;
3876 if (!TYPE_NAME (type))
3877 TYPE_NAME (type) = decl;
3878 pushdecl (decl);
3880 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3883 /* Print an error message for invalid operands to arith operation
3884 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3885 LOCATION is the location of the message. */
3887 void
3888 binary_op_error (location_t location, enum tree_code code,
3889 tree type0, tree type1)
3891 const char *opname;
3893 switch (code)
3895 case PLUS_EXPR:
3896 opname = "+"; break;
3897 case MINUS_EXPR:
3898 opname = "-"; break;
3899 case MULT_EXPR:
3900 opname = "*"; break;
3901 case MAX_EXPR:
3902 opname = "max"; break;
3903 case MIN_EXPR:
3904 opname = "min"; break;
3905 case EQ_EXPR:
3906 opname = "=="; break;
3907 case NE_EXPR:
3908 opname = "!="; break;
3909 case LE_EXPR:
3910 opname = "<="; break;
3911 case GE_EXPR:
3912 opname = ">="; break;
3913 case LT_EXPR:
3914 opname = "<"; break;
3915 case GT_EXPR:
3916 opname = ">"; break;
3917 case LSHIFT_EXPR:
3918 opname = "<<"; break;
3919 case RSHIFT_EXPR:
3920 opname = ">>"; break;
3921 case TRUNC_MOD_EXPR:
3922 case FLOOR_MOD_EXPR:
3923 opname = "%"; break;
3924 case TRUNC_DIV_EXPR:
3925 case FLOOR_DIV_EXPR:
3926 opname = "/"; break;
3927 case BIT_AND_EXPR:
3928 opname = "&"; break;
3929 case BIT_IOR_EXPR:
3930 opname = "|"; break;
3931 case TRUTH_ANDIF_EXPR:
3932 opname = "&&"; break;
3933 case TRUTH_ORIF_EXPR:
3934 opname = "||"; break;
3935 case BIT_XOR_EXPR:
3936 opname = "^"; break;
3937 default:
3938 gcc_unreachable ();
3940 error_at (location,
3941 "invalid operands to binary %s (have %qT and %qT)", opname,
3942 type0, type1);
3945 /* Given an expression as a tree, return its original type. Do this
3946 by stripping any conversion that preserves the sign and precision. */
3947 static tree
3948 expr_original_type (tree expr)
3950 STRIP_SIGN_NOPS (expr);
3951 return TREE_TYPE (expr);
3954 /* Subroutine of build_binary_op, used for comparison operations.
3955 See if the operands have both been converted from subword integer types
3956 and, if so, perhaps change them both back to their original type.
3957 This function is also responsible for converting the two operands
3958 to the proper common type for comparison.
3960 The arguments of this function are all pointers to local variables
3961 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3962 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3964 If this function returns nonzero, it means that the comparison has
3965 a constant value. What this function returns is an expression for
3966 that value. */
3968 tree
3969 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3970 enum tree_code *rescode_ptr)
3972 tree type;
3973 tree op0 = *op0_ptr;
3974 tree op1 = *op1_ptr;
3975 int unsignedp0, unsignedp1;
3976 int real1, real2;
3977 tree primop0, primop1;
3978 enum tree_code code = *rescode_ptr;
3979 location_t loc = EXPR_LOC_OR_HERE (op0);
3981 /* Throw away any conversions to wider types
3982 already present in the operands. */
3984 primop0 = c_common_get_narrower (op0, &unsignedp0);
3985 primop1 = c_common_get_narrower (op1, &unsignedp1);
3987 /* If primopN is first sign-extended from primopN's precision to opN's
3988 precision, then zero-extended from opN's precision to
3989 *restype_ptr precision, shortenings might be invalid. */
3990 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3991 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3992 && !unsignedp0
3993 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3994 primop0 = op0;
3995 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3996 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3997 && !unsignedp1
3998 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3999 primop1 = op1;
4001 /* Handle the case that OP0 does not *contain* a conversion
4002 but it *requires* conversion to FINAL_TYPE. */
4004 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4005 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4006 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4007 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4009 /* If one of the operands must be floated, we cannot optimize. */
4010 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4011 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4013 /* If first arg is constant, swap the args (changing operation
4014 so value is preserved), for canonicalization. Don't do this if
4015 the second arg is 0. */
4017 if (TREE_CONSTANT (primop0)
4018 && !integer_zerop (primop1) && !real_zerop (primop1)
4019 && !fixed_zerop (primop1))
4021 tree tem = primop0;
4022 int temi = unsignedp0;
4023 primop0 = primop1;
4024 primop1 = tem;
4025 tem = op0;
4026 op0 = op1;
4027 op1 = tem;
4028 *op0_ptr = op0;
4029 *op1_ptr = op1;
4030 unsignedp0 = unsignedp1;
4031 unsignedp1 = temi;
4032 temi = real1;
4033 real1 = real2;
4034 real2 = temi;
4036 switch (code)
4038 case LT_EXPR:
4039 code = GT_EXPR;
4040 break;
4041 case GT_EXPR:
4042 code = LT_EXPR;
4043 break;
4044 case LE_EXPR:
4045 code = GE_EXPR;
4046 break;
4047 case GE_EXPR:
4048 code = LE_EXPR;
4049 break;
4050 default:
4051 break;
4053 *rescode_ptr = code;
4056 /* If comparing an integer against a constant more bits wide,
4057 maybe we can deduce a value of 1 or 0 independent of the data.
4058 Or else truncate the constant now
4059 rather than extend the variable at run time.
4061 This is only interesting if the constant is the wider arg.
4062 Also, it is not safe if the constant is unsigned and the
4063 variable arg is signed, since in this case the variable
4064 would be sign-extended and then regarded as unsigned.
4065 Our technique fails in this case because the lowest/highest
4066 possible unsigned results don't follow naturally from the
4067 lowest/highest possible values of the variable operand.
4068 For just EQ_EXPR and NE_EXPR there is another technique that
4069 could be used: see if the constant can be faithfully represented
4070 in the other operand's type, by truncating it and reextending it
4071 and see if that preserves the constant's value. */
4073 if (!real1 && !real2
4074 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4075 && TREE_CODE (primop1) == INTEGER_CST
4076 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4078 int min_gt, max_gt, min_lt, max_lt;
4079 tree maxval, minval;
4080 /* 1 if comparison is nominally unsigned. */
4081 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4082 tree val;
4084 type = c_common_signed_or_unsigned_type (unsignedp0,
4085 TREE_TYPE (primop0));
4087 maxval = TYPE_MAX_VALUE (type);
4088 minval = TYPE_MIN_VALUE (type);
4090 if (unsignedp && !unsignedp0)
4091 *restype_ptr = c_common_signed_type (*restype_ptr);
4093 if (TREE_TYPE (primop1) != *restype_ptr)
4095 /* Convert primop1 to target type, but do not introduce
4096 additional overflow. We know primop1 is an int_cst. */
4097 primop1 = force_fit_type_double (*restype_ptr,
4098 tree_to_double_int (primop1),
4099 0, TREE_OVERFLOW (primop1));
4101 if (type != *restype_ptr)
4103 minval = convert (*restype_ptr, minval);
4104 maxval = convert (*restype_ptr, maxval);
4107 if (unsignedp && unsignedp0)
4109 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4110 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4111 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4112 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4114 else
4116 min_gt = INT_CST_LT (primop1, minval);
4117 max_gt = INT_CST_LT (primop1, maxval);
4118 min_lt = INT_CST_LT (minval, primop1);
4119 max_lt = INT_CST_LT (maxval, primop1);
4122 val = 0;
4123 /* This used to be a switch, but Genix compiler can't handle that. */
4124 if (code == NE_EXPR)
4126 if (max_lt || min_gt)
4127 val = truthvalue_true_node;
4129 else if (code == EQ_EXPR)
4131 if (max_lt || min_gt)
4132 val = truthvalue_false_node;
4134 else if (code == LT_EXPR)
4136 if (max_lt)
4137 val = truthvalue_true_node;
4138 if (!min_lt)
4139 val = truthvalue_false_node;
4141 else if (code == GT_EXPR)
4143 if (min_gt)
4144 val = truthvalue_true_node;
4145 if (!max_gt)
4146 val = truthvalue_false_node;
4148 else if (code == LE_EXPR)
4150 if (!max_gt)
4151 val = truthvalue_true_node;
4152 if (min_gt)
4153 val = truthvalue_false_node;
4155 else if (code == GE_EXPR)
4157 if (!min_lt)
4158 val = truthvalue_true_node;
4159 if (max_lt)
4160 val = truthvalue_false_node;
4163 /* If primop0 was sign-extended and unsigned comparison specd,
4164 we did a signed comparison above using the signed type bounds.
4165 But the comparison we output must be unsigned.
4167 Also, for inequalities, VAL is no good; but if the signed
4168 comparison had *any* fixed result, it follows that the
4169 unsigned comparison just tests the sign in reverse
4170 (positive values are LE, negative ones GE).
4171 So we can generate an unsigned comparison
4172 against an extreme value of the signed type. */
4174 if (unsignedp && !unsignedp0)
4176 if (val != 0)
4177 switch (code)
4179 case LT_EXPR:
4180 case GE_EXPR:
4181 primop1 = TYPE_MIN_VALUE (type);
4182 val = 0;
4183 break;
4185 case LE_EXPR:
4186 case GT_EXPR:
4187 primop1 = TYPE_MAX_VALUE (type);
4188 val = 0;
4189 break;
4191 default:
4192 break;
4194 type = c_common_unsigned_type (type);
4197 if (TREE_CODE (primop0) != INTEGER_CST
4198 && c_inhibit_evaluation_warnings == 0)
4200 if (val == truthvalue_false_node)
4201 warning_at (loc, OPT_Wtype_limits,
4202 "comparison is always false due to limited range of data type");
4203 if (val == truthvalue_true_node)
4204 warning_at (loc, OPT_Wtype_limits,
4205 "comparison is always true due to limited range of data type");
4208 if (val != 0)
4210 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4211 if (TREE_SIDE_EFFECTS (primop0))
4212 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4213 return val;
4216 /* Value is not predetermined, but do the comparison
4217 in the type of the operand that is not constant.
4218 TYPE is already properly set. */
4221 /* If either arg is decimal float and the other is float, find the
4222 proper common type to use for comparison. */
4223 else if (real1 && real2
4224 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4225 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4226 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4228 else if (real1 && real2
4229 && (TYPE_PRECISION (TREE_TYPE (primop0))
4230 == TYPE_PRECISION (TREE_TYPE (primop1))))
4231 type = TREE_TYPE (primop0);
4233 /* If args' natural types are both narrower than nominal type
4234 and both extend in the same manner, compare them
4235 in the type of the wider arg.
4236 Otherwise must actually extend both to the nominal
4237 common type lest different ways of extending
4238 alter the result.
4239 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4241 else if (unsignedp0 == unsignedp1 && real1 == real2
4242 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4243 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4245 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4246 type = c_common_signed_or_unsigned_type (unsignedp0
4247 || TYPE_UNSIGNED (*restype_ptr),
4248 type);
4249 /* Make sure shorter operand is extended the right way
4250 to match the longer operand. */
4251 primop0
4252 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4253 TREE_TYPE (primop0)),
4254 primop0);
4255 primop1
4256 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4257 TREE_TYPE (primop1)),
4258 primop1);
4260 else
4262 /* Here we must do the comparison on the nominal type
4263 using the args exactly as we received them. */
4264 type = *restype_ptr;
4265 primop0 = op0;
4266 primop1 = op1;
4268 if (!real1 && !real2 && integer_zerop (primop1)
4269 && TYPE_UNSIGNED (*restype_ptr))
4271 tree value = 0;
4272 /* All unsigned values are >= 0, so we warn. However,
4273 if OP0 is a constant that is >= 0, the signedness of
4274 the comparison isn't an issue, so suppress the
4275 warning. */
4276 bool warn =
4277 warn_type_limits && !in_system_header
4278 && c_inhibit_evaluation_warnings == 0
4279 && !(TREE_CODE (primop0) == INTEGER_CST
4280 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4281 primop0)))
4282 /* Do not warn for enumeration types. */
4283 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4285 switch (code)
4287 case GE_EXPR:
4288 if (warn)
4289 warning_at (loc, OPT_Wtype_limits,
4290 "comparison of unsigned expression >= 0 is always true");
4291 value = truthvalue_true_node;
4292 break;
4294 case LT_EXPR:
4295 if (warn)
4296 warning_at (loc, OPT_Wtype_limits,
4297 "comparison of unsigned expression < 0 is always false");
4298 value = truthvalue_false_node;
4299 break;
4301 default:
4302 break;
4305 if (value != 0)
4307 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4308 if (TREE_SIDE_EFFECTS (primop0))
4309 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4310 primop0, value);
4311 return value;
4316 *op0_ptr = convert (type, primop0);
4317 *op1_ptr = convert (type, primop1);
4319 *restype_ptr = truthvalue_type_node;
4321 return 0;
4324 /* Return a tree for the sum or difference (RESULTCODE says which)
4325 of pointer PTROP and integer INTOP. */
4327 tree
4328 pointer_int_sum (location_t loc, enum tree_code resultcode,
4329 tree ptrop, tree intop, bool complain)
4331 tree size_exp, ret;
4333 /* The result is a pointer of the same type that is being added. */
4334 tree result_type = TREE_TYPE (ptrop);
4336 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4338 if (complain && warn_pointer_arith)
4339 pedwarn (loc, OPT_Wpointer_arith,
4340 "pointer of type %<void *%> used in arithmetic");
4341 else if (!complain)
4342 return error_mark_node;
4343 size_exp = integer_one_node;
4345 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4347 if (complain && warn_pointer_arith)
4348 pedwarn (loc, OPT_Wpointer_arith,
4349 "pointer to a function used in arithmetic");
4350 else if (!complain)
4351 return error_mark_node;
4352 size_exp = integer_one_node;
4354 else
4355 size_exp = size_in_bytes (TREE_TYPE (result_type));
4357 /* We are manipulating pointer values, so we don't need to warn
4358 about relying on undefined signed overflow. We disable the
4359 warning here because we use integer types so fold won't know that
4360 they are really pointers. */
4361 fold_defer_overflow_warnings ();
4363 /* If what we are about to multiply by the size of the elements
4364 contains a constant term, apply distributive law
4365 and multiply that constant term separately.
4366 This helps produce common subexpressions. */
4367 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4368 && !TREE_CONSTANT (intop)
4369 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4370 && TREE_CONSTANT (size_exp)
4371 /* If the constant comes from pointer subtraction,
4372 skip this optimization--it would cause an error. */
4373 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4374 /* If the constant is unsigned, and smaller than the pointer size,
4375 then we must skip this optimization. This is because it could cause
4376 an overflow error if the constant is negative but INTOP is not. */
4377 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4378 || (TYPE_PRECISION (TREE_TYPE (intop))
4379 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4381 enum tree_code subcode = resultcode;
4382 tree int_type = TREE_TYPE (intop);
4383 if (TREE_CODE (intop) == MINUS_EXPR)
4384 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4385 /* Convert both subexpression types to the type of intop,
4386 because weird cases involving pointer arithmetic
4387 can result in a sum or difference with different type args. */
4388 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4389 subcode, ptrop,
4390 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4391 intop = convert (int_type, TREE_OPERAND (intop, 0));
4394 /* Convert the integer argument to a type the same size as sizetype
4395 so the multiply won't overflow spuriously. */
4396 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4397 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4398 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4399 TYPE_UNSIGNED (sizetype)), intop);
4401 /* Replace the integer argument with a suitable product by the object size.
4402 Do this multiplication as signed, then convert to the appropriate type
4403 for the pointer operation and disregard an overflow that occurred only
4404 because of the sign-extension change in the latter conversion. */
4406 tree t = build_binary_op (loc,
4407 MULT_EXPR, intop,
4408 convert (TREE_TYPE (intop), size_exp), 1);
4409 intop = convert (sizetype, t);
4410 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4411 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4412 TREE_INT_CST_HIGH (intop));
4415 /* Create the sum or difference. */
4416 if (resultcode == MINUS_EXPR)
4417 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4419 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4421 fold_undefer_and_ignore_overflow_warnings ();
4423 return ret;
4426 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4427 and if NON_CONST is known not to be permitted in an evaluated part
4428 of a constant expression. */
4430 tree
4431 c_wrap_maybe_const (tree expr, bool non_const)
4433 bool nowarning = TREE_NO_WARNING (expr);
4434 location_t loc = EXPR_LOCATION (expr);
4436 /* This should never be called for C++. */
4437 if (c_dialect_cxx ())
4438 gcc_unreachable ();
4440 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4441 STRIP_TYPE_NOPS (expr);
4442 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4443 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4444 if (nowarning)
4445 TREE_NO_WARNING (expr) = 1;
4446 protected_set_expr_location (expr, loc);
4448 return expr;
4451 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4452 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4453 around the SAVE_EXPR if needed so that c_fully_fold does not need
4454 to look inside SAVE_EXPRs. */
4456 tree
4457 c_save_expr (tree expr)
4459 bool maybe_const = true;
4460 if (c_dialect_cxx ())
4461 return save_expr (expr);
4462 expr = c_fully_fold (expr, false, &maybe_const);
4463 expr = save_expr (expr);
4464 if (!maybe_const)
4465 expr = c_wrap_maybe_const (expr, true);
4466 return expr;
4469 /* Return whether EXPR is a declaration whose address can never be
4470 NULL. */
4472 bool
4473 decl_with_nonnull_addr_p (const_tree expr)
4475 return (DECL_P (expr)
4476 && (TREE_CODE (expr) == PARM_DECL
4477 || TREE_CODE (expr) == LABEL_DECL
4478 || !DECL_WEAK (expr)));
4481 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4482 or for an `if' or `while' statement or ?..: exp. It should already
4483 have been validated to be of suitable type; otherwise, a bad
4484 diagnostic may result.
4486 The EXPR is located at LOCATION.
4488 This preparation consists of taking the ordinary
4489 representation of an expression expr and producing a valid tree
4490 boolean expression describing whether expr is nonzero. We could
4491 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4492 but we optimize comparisons, &&, ||, and !.
4494 The resulting type should always be `truthvalue_type_node'. */
4496 tree
4497 c_common_truthvalue_conversion (location_t location, tree expr)
4499 switch (TREE_CODE (expr))
4501 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4502 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4503 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4504 case ORDERED_EXPR: case UNORDERED_EXPR:
4505 if (TREE_TYPE (expr) == truthvalue_type_node)
4506 return expr;
4507 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4508 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4509 goto ret;
4511 case TRUTH_ANDIF_EXPR:
4512 case TRUTH_ORIF_EXPR:
4513 case TRUTH_AND_EXPR:
4514 case TRUTH_OR_EXPR:
4515 case TRUTH_XOR_EXPR:
4516 if (TREE_TYPE (expr) == truthvalue_type_node)
4517 return expr;
4518 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4519 c_common_truthvalue_conversion (location,
4520 TREE_OPERAND (expr, 0)),
4521 c_common_truthvalue_conversion (location,
4522 TREE_OPERAND (expr, 1)));
4523 goto ret;
4525 case TRUTH_NOT_EXPR:
4526 if (TREE_TYPE (expr) == truthvalue_type_node)
4527 return expr;
4528 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4529 c_common_truthvalue_conversion (location,
4530 TREE_OPERAND (expr, 0)));
4531 goto ret;
4533 case ERROR_MARK:
4534 return expr;
4536 case INTEGER_CST:
4537 return integer_zerop (expr) ? truthvalue_false_node
4538 : truthvalue_true_node;
4540 case REAL_CST:
4541 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4542 ? truthvalue_true_node
4543 : truthvalue_false_node;
4545 case FIXED_CST:
4546 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4547 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4548 ? truthvalue_true_node
4549 : truthvalue_false_node;
4551 case FUNCTION_DECL:
4552 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4553 /* Fall through. */
4555 case ADDR_EXPR:
4557 tree inner = TREE_OPERAND (expr, 0);
4558 if (decl_with_nonnull_addr_p (inner))
4560 /* Common Ada/Pascal programmer's mistake. */
4561 warning_at (location,
4562 OPT_Waddress,
4563 "the address of %qD will always evaluate as %<true%>",
4564 inner);
4565 return truthvalue_true_node;
4567 break;
4570 case COMPLEX_EXPR:
4571 expr = build_binary_op (EXPR_LOCATION (expr),
4572 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4573 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4574 c_common_truthvalue_conversion (location,
4575 TREE_OPERAND (expr, 0)),
4576 c_common_truthvalue_conversion (location,
4577 TREE_OPERAND (expr, 1)),
4579 goto ret;
4581 case NEGATE_EXPR:
4582 case ABS_EXPR:
4583 case FLOAT_EXPR:
4584 case EXCESS_PRECISION_EXPR:
4585 /* These don't change whether an object is nonzero or zero. */
4586 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4588 case LROTATE_EXPR:
4589 case RROTATE_EXPR:
4590 /* These don't change whether an object is zero or nonzero, but
4591 we can't ignore them if their second arg has side-effects. */
4592 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4594 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4595 TREE_OPERAND (expr, 1),
4596 c_common_truthvalue_conversion
4597 (location, TREE_OPERAND (expr, 0)));
4598 goto ret;
4600 else
4601 return c_common_truthvalue_conversion (location,
4602 TREE_OPERAND (expr, 0));
4604 case COND_EXPR:
4605 /* Distribute the conversion into the arms of a COND_EXPR. */
4606 if (c_dialect_cxx ())
4608 tree op1 = TREE_OPERAND (expr, 1);
4609 tree op2 = TREE_OPERAND (expr, 2);
4610 /* In C++ one of the arms might have void type if it is throw. */
4611 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4612 op1 = c_common_truthvalue_conversion (location, op1);
4613 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4614 op2 = c_common_truthvalue_conversion (location, op2);
4615 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4616 TREE_OPERAND (expr, 0), op1, op2);
4617 goto ret;
4619 else
4621 /* Folding will happen later for C. */
4622 expr = build3 (COND_EXPR, truthvalue_type_node,
4623 TREE_OPERAND (expr, 0),
4624 c_common_truthvalue_conversion (location,
4625 TREE_OPERAND (expr, 1)),
4626 c_common_truthvalue_conversion (location,
4627 TREE_OPERAND (expr, 2)));
4628 goto ret;
4631 CASE_CONVERT:
4633 tree totype = TREE_TYPE (expr);
4634 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4636 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4637 since that affects how `default_conversion' will behave. */
4638 if (TREE_CODE (totype) == REFERENCE_TYPE
4639 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4640 break;
4641 /* Don't strip a conversion from C++0x scoped enum, since they
4642 don't implicitly convert to other types. */
4643 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4644 && ENUM_IS_SCOPED (fromtype))
4645 break;
4646 /* If this isn't narrowing the argument, we can ignore it. */
4647 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4648 return c_common_truthvalue_conversion (location,
4649 TREE_OPERAND (expr, 0));
4651 break;
4653 case MODIFY_EXPR:
4654 if (!TREE_NO_WARNING (expr)
4655 && warn_parentheses)
4657 warning (OPT_Wparentheses,
4658 "suggest parentheses around assignment used as truth value");
4659 TREE_NO_WARNING (expr) = 1;
4661 break;
4663 default:
4664 break;
4667 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4669 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4670 expr = (build_binary_op
4671 (EXPR_LOCATION (expr),
4672 (TREE_SIDE_EFFECTS (expr)
4673 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4674 c_common_truthvalue_conversion
4675 (location,
4676 build_unary_op (location, REALPART_EXPR, t, 0)),
4677 c_common_truthvalue_conversion
4678 (location,
4679 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4680 0));
4681 goto ret;
4684 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4686 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4687 FCONST0 (TYPE_MODE
4688 (TREE_TYPE (expr))));
4689 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4691 else
4692 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4694 ret:
4695 protected_set_expr_location (expr, location);
4696 return expr;
4699 static void def_builtin_1 (enum built_in_function fncode,
4700 const char *name,
4701 enum built_in_class fnclass,
4702 tree fntype, tree libtype,
4703 bool both_p, bool fallback_p, bool nonansi_p,
4704 tree fnattrs, bool implicit_p);
4707 /* Apply the TYPE_QUALS to the new DECL. */
4709 void
4710 c_apply_type_quals_to_decl (int type_quals, tree decl)
4712 tree type = TREE_TYPE (decl);
4714 if (type == error_mark_node)
4715 return;
4717 if ((type_quals & TYPE_QUAL_CONST)
4718 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4719 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4720 constructor can produce constant init, so rely on cp_finish_decl to
4721 clear TREE_READONLY if the variable has non-constant init. */
4722 TREE_READONLY (decl) = 1;
4723 if (type_quals & TYPE_QUAL_VOLATILE)
4725 TREE_SIDE_EFFECTS (decl) = 1;
4726 TREE_THIS_VOLATILE (decl) = 1;
4728 if (type_quals & TYPE_QUAL_RESTRICT)
4730 while (type && TREE_CODE (type) == ARRAY_TYPE)
4731 /* Allow 'restrict' on arrays of pointers.
4732 FIXME currently we just ignore it. */
4733 type = TREE_TYPE (type);
4734 if (!type
4735 || !POINTER_TYPE_P (type)
4736 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4737 error ("invalid use of %<restrict%>");
4741 /* Hash function for the problem of multiple type definitions in
4742 different files. This must hash all types that will compare
4743 equal via comptypes to the same value. In practice it hashes
4744 on some of the simple stuff and leaves the details to comptypes. */
4746 static hashval_t
4747 c_type_hash (const void *p)
4749 int n_elements;
4750 int shift, size;
4751 const_tree const t = (const_tree) p;
4752 tree t2;
4753 switch (TREE_CODE (t))
4755 /* For pointers, hash on pointee type plus some swizzling. */
4756 case POINTER_TYPE:
4757 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4758 /* Hash on number of elements and total size. */
4759 case ENUMERAL_TYPE:
4760 shift = 3;
4761 t2 = TYPE_VALUES (t);
4762 break;
4763 case RECORD_TYPE:
4764 shift = 0;
4765 t2 = TYPE_FIELDS (t);
4766 break;
4767 case QUAL_UNION_TYPE:
4768 shift = 1;
4769 t2 = TYPE_FIELDS (t);
4770 break;
4771 case UNION_TYPE:
4772 shift = 2;
4773 t2 = TYPE_FIELDS (t);
4774 break;
4775 default:
4776 gcc_unreachable ();
4778 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4779 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4780 n_elements = list_length (t2);
4781 /* We might have a VLA here. */
4782 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4783 size = 0;
4784 else
4785 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4786 return ((size << 24) | (n_elements << shift));
4789 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4791 /* Return the typed-based alias set for T, which may be an expression
4792 or a type. Return -1 if we don't do anything special. */
4794 alias_set_type
4795 c_common_get_alias_set (tree t)
4797 tree u;
4798 PTR *slot;
4800 /* For VLAs, use the alias set of the element type rather than the
4801 default of alias set 0 for types compared structurally. */
4802 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4804 if (TREE_CODE (t) == ARRAY_TYPE)
4805 return get_alias_set (TREE_TYPE (t));
4806 return -1;
4809 /* Permit type-punning when accessing a union, provided the access
4810 is directly through the union. For example, this code does not
4811 permit taking the address of a union member and then storing
4812 through it. Even the type-punning allowed here is a GCC
4813 extension, albeit a common and useful one; the C standard says
4814 that such accesses have implementation-defined behavior. */
4815 for (u = t;
4816 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4817 u = TREE_OPERAND (u, 0))
4818 if (TREE_CODE (u) == COMPONENT_REF
4819 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4820 return 0;
4822 /* That's all the expressions we handle specially. */
4823 if (!TYPE_P (t))
4824 return -1;
4826 /* The C standard guarantees that any object may be accessed via an
4827 lvalue that has character type. */
4828 if (t == char_type_node
4829 || t == signed_char_type_node
4830 || t == unsigned_char_type_node)
4831 return 0;
4833 /* The C standard specifically allows aliasing between signed and
4834 unsigned variants of the same type. We treat the signed
4835 variant as canonical. */
4836 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4838 tree t1 = c_common_signed_type (t);
4840 /* t1 == t can happen for boolean nodes which are always unsigned. */
4841 if (t1 != t)
4842 return get_alias_set (t1);
4845 /* Handle the case of multiple type nodes referring to "the same" type,
4846 which occurs with IMA. These share an alias set. FIXME: Currently only
4847 C90 is handled. (In C99 type compatibility is not transitive, which
4848 complicates things mightily. The alias set splay trees can theoretically
4849 represent this, but insertion is tricky when you consider all the
4850 different orders things might arrive in.) */
4852 if (c_language != clk_c || flag_isoc99)
4853 return -1;
4855 /* Save time if there's only one input file. */
4856 if (num_in_fnames == 1)
4857 return -1;
4859 /* Pointers need special handling if they point to any type that
4860 needs special handling (below). */
4861 if (TREE_CODE (t) == POINTER_TYPE)
4863 tree t2;
4864 /* Find bottom type under any nested POINTERs. */
4865 for (t2 = TREE_TYPE (t);
4866 TREE_CODE (t2) == POINTER_TYPE;
4867 t2 = TREE_TYPE (t2))
4869 if (TREE_CODE (t2) != RECORD_TYPE
4870 && TREE_CODE (t2) != ENUMERAL_TYPE
4871 && TREE_CODE (t2) != QUAL_UNION_TYPE
4872 && TREE_CODE (t2) != UNION_TYPE)
4873 return -1;
4874 if (TYPE_SIZE (t2) == 0)
4875 return -1;
4877 /* These are the only cases that need special handling. */
4878 if (TREE_CODE (t) != RECORD_TYPE
4879 && TREE_CODE (t) != ENUMERAL_TYPE
4880 && TREE_CODE (t) != QUAL_UNION_TYPE
4881 && TREE_CODE (t) != UNION_TYPE
4882 && TREE_CODE (t) != POINTER_TYPE)
4883 return -1;
4884 /* Undefined? */
4885 if (TYPE_SIZE (t) == 0)
4886 return -1;
4888 /* Look up t in hash table. Only one of the compatible types within each
4889 alias set is recorded in the table. */
4890 if (!type_hash_table)
4891 type_hash_table = htab_create_ggc (1021, c_type_hash,
4892 (htab_eq) lang_hooks.types_compatible_p,
4893 NULL);
4894 slot = htab_find_slot (type_hash_table, t, INSERT);
4895 if (*slot != NULL)
4897 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4898 return TYPE_ALIAS_SET ((tree)*slot);
4900 else
4901 /* Our caller will assign and record (in t) a new alias set; all we need
4902 to do is remember t in the hash table. */
4903 *slot = t;
4905 return -1;
4908 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4909 the second parameter indicates which OPERATOR is being applied.
4910 The COMPLAIN flag controls whether we should diagnose possibly
4911 ill-formed constructs or not. LOC is the location of the SIZEOF or
4912 TYPEOF operator. */
4914 tree
4915 c_sizeof_or_alignof_type (location_t loc,
4916 tree type, bool is_sizeof, int complain)
4918 const char *op_name;
4919 tree value = NULL;
4920 enum tree_code type_code = TREE_CODE (type);
4922 op_name = is_sizeof ? "sizeof" : "__alignof__";
4924 if (type_code == FUNCTION_TYPE)
4926 if (is_sizeof)
4928 if (complain && warn_pointer_arith)
4929 pedwarn (loc, OPT_Wpointer_arith,
4930 "invalid application of %<sizeof%> to a function type");
4931 else if (!complain)
4932 return error_mark_node;
4933 value = size_one_node;
4935 else
4937 if (complain)
4939 if (c_dialect_cxx ())
4940 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4941 "%<alignof%> applied to a function type");
4942 else
4943 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4944 "%<_Alignof%> applied to a function type");
4946 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4949 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4951 if (type_code == VOID_TYPE
4952 && complain && warn_pointer_arith)
4953 pedwarn (loc, OPT_Wpointer_arith,
4954 "invalid application of %qs to a void type", op_name);
4955 else if (!complain)
4956 return error_mark_node;
4957 value = size_one_node;
4959 else if (!COMPLETE_TYPE_P (type)
4960 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4962 if (complain)
4963 error_at (loc, "invalid application of %qs to incomplete type %qT",
4964 op_name, type);
4965 return error_mark_node;
4967 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4968 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4970 if (complain)
4971 error_at (loc, "invalid application of %qs to array type %qT of "
4972 "incomplete element type", op_name, type);
4973 return error_mark_node;
4975 else
4977 if (is_sizeof)
4978 /* Convert in case a char is more than one unit. */
4979 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4980 size_int (TYPE_PRECISION (char_type_node)
4981 / BITS_PER_UNIT));
4982 else
4983 value = size_int (TYPE_ALIGN_UNIT (type));
4986 /* VALUE will have the middle-end integer type sizetype.
4987 However, we should really return a value of type `size_t',
4988 which is just a typedef for an ordinary integer type. */
4989 value = fold_convert_loc (loc, size_type_node, value);
4991 return value;
4994 /* Implement the __alignof keyword: Return the minimum required
4995 alignment of EXPR, measured in bytes. For VAR_DECLs,
4996 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4997 from an "aligned" __attribute__ specification). LOC is the
4998 location of the ALIGNOF operator. */
5000 tree
5001 c_alignof_expr (location_t loc, tree expr)
5003 tree t;
5005 if (VAR_OR_FUNCTION_DECL_P (expr))
5006 t = size_int (DECL_ALIGN_UNIT (expr));
5008 else if (TREE_CODE (expr) == COMPONENT_REF
5009 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5011 error_at (loc, "%<__alignof%> applied to a bit-field");
5012 t = size_one_node;
5014 else if (TREE_CODE (expr) == COMPONENT_REF
5015 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5016 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5018 else if (TREE_CODE (expr) == INDIRECT_REF)
5020 tree t = TREE_OPERAND (expr, 0);
5021 tree best = t;
5022 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5024 while (CONVERT_EXPR_P (t)
5025 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5027 int thisalign;
5029 t = TREE_OPERAND (t, 0);
5030 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5031 if (thisalign > bestalign)
5032 best = t, bestalign = thisalign;
5034 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5036 else
5037 return c_alignof (loc, TREE_TYPE (expr));
5039 return fold_convert_loc (loc, size_type_node, t);
5042 /* Handle C and C++ default attributes. */
5044 enum built_in_attribute
5046 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5047 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5048 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5049 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5050 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5051 #include "builtin-attrs.def"
5052 #undef DEF_ATTR_NULL_TREE
5053 #undef DEF_ATTR_INT
5054 #undef DEF_ATTR_STRING
5055 #undef DEF_ATTR_IDENT
5056 #undef DEF_ATTR_TREE_LIST
5057 ATTR_LAST
5060 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5062 static void c_init_attributes (void);
5064 enum c_builtin_type
5066 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5067 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5068 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5069 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5070 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5071 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5072 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5073 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5074 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5075 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5076 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5077 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5078 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5079 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5080 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5081 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5082 NAME,
5083 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5084 #include "builtin-types.def"
5085 #undef DEF_PRIMITIVE_TYPE
5086 #undef DEF_FUNCTION_TYPE_0
5087 #undef DEF_FUNCTION_TYPE_1
5088 #undef DEF_FUNCTION_TYPE_2
5089 #undef DEF_FUNCTION_TYPE_3
5090 #undef DEF_FUNCTION_TYPE_4
5091 #undef DEF_FUNCTION_TYPE_5
5092 #undef DEF_FUNCTION_TYPE_6
5093 #undef DEF_FUNCTION_TYPE_7
5094 #undef DEF_FUNCTION_TYPE_8
5095 #undef DEF_FUNCTION_TYPE_VAR_0
5096 #undef DEF_FUNCTION_TYPE_VAR_1
5097 #undef DEF_FUNCTION_TYPE_VAR_2
5098 #undef DEF_FUNCTION_TYPE_VAR_3
5099 #undef DEF_FUNCTION_TYPE_VAR_4
5100 #undef DEF_FUNCTION_TYPE_VAR_5
5101 #undef DEF_POINTER_TYPE
5102 BT_LAST
5105 typedef enum c_builtin_type builtin_type;
5107 /* A temporary array for c_common_nodes_and_builtins. Used in
5108 communication with def_fn_type. */
5109 static tree builtin_types[(int) BT_LAST + 1];
5111 /* A helper function for c_common_nodes_and_builtins. Build function type
5112 for DEF with return type RET and N arguments. If VAR is true, then the
5113 function should be variadic after those N arguments.
5115 Takes special care not to ICE if any of the types involved are
5116 error_mark_node, which indicates that said type is not in fact available
5117 (see builtin_type_for_size). In which case the function type as a whole
5118 should be error_mark_node. */
5120 static void
5121 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5123 tree t;
5124 tree *args = XALLOCAVEC (tree, n);
5125 va_list list;
5126 int i;
5128 va_start (list, n);
5129 for (i = 0; i < n; ++i)
5131 builtin_type a = (builtin_type) va_arg (list, int);
5132 t = builtin_types[a];
5133 if (t == error_mark_node)
5134 goto egress;
5135 args[i] = t;
5138 t = builtin_types[ret];
5139 if (t == error_mark_node)
5140 goto egress;
5141 if (var)
5142 t = build_varargs_function_type_array (t, n, args);
5143 else
5144 t = build_function_type_array (t, n, args);
5146 egress:
5147 builtin_types[def] = t;
5148 va_end (list);
5151 /* Build builtin functions common to both C and C++ language
5152 frontends. */
5154 static void
5155 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5157 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5158 builtin_types[ENUM] = VALUE;
5159 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5160 def_fn_type (ENUM, RETURN, 0, 0);
5161 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5162 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5163 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5164 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5165 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5166 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5167 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5168 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5169 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5170 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5171 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5172 ARG6) \
5173 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5174 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5175 ARG6, ARG7) \
5176 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5177 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5178 ARG6, ARG7, ARG8) \
5179 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5180 ARG7, ARG8);
5181 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5182 def_fn_type (ENUM, RETURN, 1, 0);
5183 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5184 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5185 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5186 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5187 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5188 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5189 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5190 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5191 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5192 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5193 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5194 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5196 #include "builtin-types.def"
5198 #undef DEF_PRIMITIVE_TYPE
5199 #undef DEF_FUNCTION_TYPE_1
5200 #undef DEF_FUNCTION_TYPE_2
5201 #undef DEF_FUNCTION_TYPE_3
5202 #undef DEF_FUNCTION_TYPE_4
5203 #undef DEF_FUNCTION_TYPE_5
5204 #undef DEF_FUNCTION_TYPE_6
5205 #undef DEF_FUNCTION_TYPE_VAR_0
5206 #undef DEF_FUNCTION_TYPE_VAR_1
5207 #undef DEF_FUNCTION_TYPE_VAR_2
5208 #undef DEF_FUNCTION_TYPE_VAR_3
5209 #undef DEF_FUNCTION_TYPE_VAR_4
5210 #undef DEF_FUNCTION_TYPE_VAR_5
5211 #undef DEF_POINTER_TYPE
5212 builtin_types[(int) BT_LAST] = NULL_TREE;
5214 c_init_attributes ();
5216 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5217 NONANSI_P, ATTRS, IMPLICIT, COND) \
5218 if (NAME && COND) \
5219 def_builtin_1 (ENUM, NAME, CLASS, \
5220 builtin_types[(int) TYPE], \
5221 builtin_types[(int) LIBTYPE], \
5222 BOTH_P, FALLBACK_P, NONANSI_P, \
5223 built_in_attributes[(int) ATTRS], IMPLICIT);
5224 #include "builtins.def"
5225 #undef DEF_BUILTIN
5227 targetm.init_builtins ();
5229 build_common_builtin_nodes ();
5231 if (flag_enable_cilkplus)
5232 cilk_init_builtins ();
5235 /* Like get_identifier, but avoid warnings about null arguments when
5236 the argument may be NULL for targets where GCC lacks stdint.h type
5237 information. */
5239 static inline tree
5240 c_get_ident (const char *id)
5242 return get_identifier (id);
5245 /* Build tree nodes and builtin functions common to both C and C++ language
5246 frontends. */
5248 void
5249 c_common_nodes_and_builtins (void)
5251 int char16_type_size;
5252 int char32_type_size;
5253 int wchar_type_size;
5254 tree array_domain_type;
5255 tree va_list_ref_type_node;
5256 tree va_list_arg_type_node;
5258 build_common_tree_nodes (flag_signed_char, flag_short_double);
5260 /* Define `int' and `char' first so that dbx will output them first. */
5261 record_builtin_type (RID_INT, NULL, integer_type_node);
5262 record_builtin_type (RID_CHAR, "char", char_type_node);
5264 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5265 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5266 but not C. Are the conditionals here needed? */
5267 if (c_dialect_cxx ())
5268 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5269 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5270 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5271 record_builtin_type (RID_MAX, "long unsigned int",
5272 long_unsigned_type_node);
5273 if (int128_integer_type_node != NULL_TREE)
5275 record_builtin_type (RID_INT128, "__int128",
5276 int128_integer_type_node);
5277 record_builtin_type (RID_MAX, "__int128 unsigned",
5278 int128_unsigned_type_node);
5280 if (c_dialect_cxx ())
5281 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5282 record_builtin_type (RID_MAX, "long long int",
5283 long_long_integer_type_node);
5284 record_builtin_type (RID_MAX, "long long unsigned int",
5285 long_long_unsigned_type_node);
5286 if (c_dialect_cxx ())
5287 record_builtin_type (RID_MAX, "long long unsigned",
5288 long_long_unsigned_type_node);
5289 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5290 record_builtin_type (RID_MAX, "short unsigned int",
5291 short_unsigned_type_node);
5292 if (c_dialect_cxx ())
5293 record_builtin_type (RID_MAX, "unsigned short",
5294 short_unsigned_type_node);
5296 /* Define both `signed char' and `unsigned char'. */
5297 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5298 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5300 /* These are types that c_common_type_for_size and
5301 c_common_type_for_mode use. */
5302 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5303 TYPE_DECL, NULL_TREE,
5304 intQI_type_node));
5305 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5306 TYPE_DECL, NULL_TREE,
5307 intHI_type_node));
5308 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5309 TYPE_DECL, NULL_TREE,
5310 intSI_type_node));
5311 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5312 TYPE_DECL, NULL_TREE,
5313 intDI_type_node));
5314 #if HOST_BITS_PER_WIDE_INT >= 64
5315 if (targetm.scalar_mode_supported_p (TImode))
5316 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5317 TYPE_DECL,
5318 get_identifier ("__int128_t"),
5319 intTI_type_node));
5320 #endif
5321 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5322 TYPE_DECL, NULL_TREE,
5323 unsigned_intQI_type_node));
5324 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5325 TYPE_DECL, NULL_TREE,
5326 unsigned_intHI_type_node));
5327 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5328 TYPE_DECL, NULL_TREE,
5329 unsigned_intSI_type_node));
5330 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5331 TYPE_DECL, NULL_TREE,
5332 unsigned_intDI_type_node));
5333 #if HOST_BITS_PER_WIDE_INT >= 64
5334 if (targetm.scalar_mode_supported_p (TImode))
5335 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5336 TYPE_DECL,
5337 get_identifier ("__uint128_t"),
5338 unsigned_intTI_type_node));
5339 #endif
5341 /* Create the widest literal types. */
5342 widest_integer_literal_type_node
5343 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5344 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5345 TYPE_DECL, NULL_TREE,
5346 widest_integer_literal_type_node));
5348 widest_unsigned_literal_type_node
5349 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5350 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5351 TYPE_DECL, NULL_TREE,
5352 widest_unsigned_literal_type_node));
5354 signed_size_type_node = c_common_signed_type (size_type_node);
5356 pid_type_node =
5357 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5359 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5360 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5361 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5363 /* Only supported decimal floating point extension if the target
5364 actually supports underlying modes. */
5365 if (targetm.scalar_mode_supported_p (SDmode)
5366 && targetm.scalar_mode_supported_p (DDmode)
5367 && targetm.scalar_mode_supported_p (TDmode))
5369 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5370 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5371 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5374 if (targetm.fixed_point_supported_p ())
5376 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5377 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5378 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5379 record_builtin_type (RID_MAX, "long long _Fract",
5380 long_long_fract_type_node);
5381 record_builtin_type (RID_MAX, "unsigned short _Fract",
5382 unsigned_short_fract_type_node);
5383 record_builtin_type (RID_MAX, "unsigned _Fract",
5384 unsigned_fract_type_node);
5385 record_builtin_type (RID_MAX, "unsigned long _Fract",
5386 unsigned_long_fract_type_node);
5387 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5388 unsigned_long_long_fract_type_node);
5389 record_builtin_type (RID_MAX, "_Sat short _Fract",
5390 sat_short_fract_type_node);
5391 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5392 record_builtin_type (RID_MAX, "_Sat long _Fract",
5393 sat_long_fract_type_node);
5394 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5395 sat_long_long_fract_type_node);
5396 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5397 sat_unsigned_short_fract_type_node);
5398 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5399 sat_unsigned_fract_type_node);
5400 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5401 sat_unsigned_long_fract_type_node);
5402 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5403 sat_unsigned_long_long_fract_type_node);
5404 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5405 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5406 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5407 record_builtin_type (RID_MAX, "long long _Accum",
5408 long_long_accum_type_node);
5409 record_builtin_type (RID_MAX, "unsigned short _Accum",
5410 unsigned_short_accum_type_node);
5411 record_builtin_type (RID_MAX, "unsigned _Accum",
5412 unsigned_accum_type_node);
5413 record_builtin_type (RID_MAX, "unsigned long _Accum",
5414 unsigned_long_accum_type_node);
5415 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5416 unsigned_long_long_accum_type_node);
5417 record_builtin_type (RID_MAX, "_Sat short _Accum",
5418 sat_short_accum_type_node);
5419 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5420 record_builtin_type (RID_MAX, "_Sat long _Accum",
5421 sat_long_accum_type_node);
5422 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5423 sat_long_long_accum_type_node);
5424 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5425 sat_unsigned_short_accum_type_node);
5426 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5427 sat_unsigned_accum_type_node);
5428 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5429 sat_unsigned_long_accum_type_node);
5430 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5431 sat_unsigned_long_long_accum_type_node);
5435 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5436 TYPE_DECL,
5437 get_identifier ("complex int"),
5438 complex_integer_type_node));
5439 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5440 TYPE_DECL,
5441 get_identifier ("complex float"),
5442 complex_float_type_node));
5443 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5444 TYPE_DECL,
5445 get_identifier ("complex double"),
5446 complex_double_type_node));
5447 lang_hooks.decls.pushdecl
5448 (build_decl (UNKNOWN_LOCATION,
5449 TYPE_DECL, get_identifier ("complex long double"),
5450 complex_long_double_type_node));
5452 if (c_dialect_cxx ())
5453 /* For C++, make fileptr_type_node a distinct void * type until
5454 FILE type is defined. */
5455 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5457 record_builtin_type (RID_VOID, NULL, void_type_node);
5459 /* Set the TYPE_NAME for any variants that were built before
5460 record_builtin_type gave names to the built-in types. */
5462 tree void_name = TYPE_NAME (void_type_node);
5463 TYPE_NAME (void_type_node) = NULL_TREE;
5464 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5465 = void_name;
5466 TYPE_NAME (void_type_node) = void_name;
5469 /* This node must not be shared. */
5470 void_zero_node = make_node (INTEGER_CST);
5471 TREE_TYPE (void_zero_node) = void_type_node;
5473 void_list_node = build_void_list_node ();
5475 /* Make a type to be the domain of a few array types
5476 whose domains don't really matter.
5477 200 is small enough that it always fits in size_t
5478 and large enough that it can hold most function names for the
5479 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5480 array_domain_type = build_index_type (size_int (200));
5482 /* Make a type for arrays of characters.
5483 With luck nothing will ever really depend on the length of this
5484 array type. */
5485 char_array_type_node
5486 = build_array_type (char_type_node, array_domain_type);
5488 /* Likewise for arrays of ints. */
5489 int_array_type_node
5490 = build_array_type (integer_type_node, array_domain_type);
5492 string_type_node = build_pointer_type (char_type_node);
5493 const_string_type_node
5494 = build_pointer_type (build_qualified_type
5495 (char_type_node, TYPE_QUAL_CONST));
5497 /* This is special for C++ so functions can be overloaded. */
5498 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5499 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5500 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5501 underlying_wchar_type_node = wchar_type_node;
5502 if (c_dialect_cxx ())
5504 if (TYPE_UNSIGNED (wchar_type_node))
5505 wchar_type_node = make_unsigned_type (wchar_type_size);
5506 else
5507 wchar_type_node = make_signed_type (wchar_type_size);
5508 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5511 /* This is for wide string constants. */
5512 wchar_array_type_node
5513 = build_array_type (wchar_type_node, array_domain_type);
5515 /* Define 'char16_t'. */
5516 char16_type_node = get_identifier (CHAR16_TYPE);
5517 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5518 char16_type_size = TYPE_PRECISION (char16_type_node);
5519 if (c_dialect_cxx ())
5521 char16_type_node = make_unsigned_type (char16_type_size);
5523 if (cxx_dialect >= cxx11)
5524 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5527 /* This is for UTF-16 string constants. */
5528 char16_array_type_node
5529 = build_array_type (char16_type_node, array_domain_type);
5531 /* Define 'char32_t'. */
5532 char32_type_node = get_identifier (CHAR32_TYPE);
5533 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5534 char32_type_size = TYPE_PRECISION (char32_type_node);
5535 if (c_dialect_cxx ())
5537 char32_type_node = make_unsigned_type (char32_type_size);
5539 if (cxx_dialect >= cxx11)
5540 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5543 /* This is for UTF-32 string constants. */
5544 char32_array_type_node
5545 = build_array_type (char32_type_node, array_domain_type);
5547 wint_type_node =
5548 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5550 intmax_type_node =
5551 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5552 uintmax_type_node =
5553 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5555 if (SIG_ATOMIC_TYPE)
5556 sig_atomic_type_node =
5557 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5558 if (INT8_TYPE)
5559 int8_type_node =
5560 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5561 if (INT16_TYPE)
5562 int16_type_node =
5563 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5564 if (INT32_TYPE)
5565 int32_type_node =
5566 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5567 if (INT64_TYPE)
5568 int64_type_node =
5569 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5570 if (UINT8_TYPE)
5571 uint8_type_node =
5572 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5573 if (UINT16_TYPE)
5574 c_uint16_type_node =
5575 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5576 if (UINT32_TYPE)
5577 c_uint32_type_node =
5578 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5579 if (UINT64_TYPE)
5580 c_uint64_type_node =
5581 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5582 if (INT_LEAST8_TYPE)
5583 int_least8_type_node =
5584 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5585 if (INT_LEAST16_TYPE)
5586 int_least16_type_node =
5587 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5588 if (INT_LEAST32_TYPE)
5589 int_least32_type_node =
5590 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5591 if (INT_LEAST64_TYPE)
5592 int_least64_type_node =
5593 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5594 if (UINT_LEAST8_TYPE)
5595 uint_least8_type_node =
5596 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5597 if (UINT_LEAST16_TYPE)
5598 uint_least16_type_node =
5599 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5600 if (UINT_LEAST32_TYPE)
5601 uint_least32_type_node =
5602 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5603 if (UINT_LEAST64_TYPE)
5604 uint_least64_type_node =
5605 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5606 if (INT_FAST8_TYPE)
5607 int_fast8_type_node =
5608 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5609 if (INT_FAST16_TYPE)
5610 int_fast16_type_node =
5611 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5612 if (INT_FAST32_TYPE)
5613 int_fast32_type_node =
5614 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5615 if (INT_FAST64_TYPE)
5616 int_fast64_type_node =
5617 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5618 if (UINT_FAST8_TYPE)
5619 uint_fast8_type_node =
5620 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5621 if (UINT_FAST16_TYPE)
5622 uint_fast16_type_node =
5623 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5624 if (UINT_FAST32_TYPE)
5625 uint_fast32_type_node =
5626 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5627 if (UINT_FAST64_TYPE)
5628 uint_fast64_type_node =
5629 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5630 if (INTPTR_TYPE)
5631 intptr_type_node =
5632 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5633 if (UINTPTR_TYPE)
5634 uintptr_type_node =
5635 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5637 default_function_type
5638 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5639 ptrdiff_type_node
5640 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5641 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5643 lang_hooks.decls.pushdecl
5644 (build_decl (UNKNOWN_LOCATION,
5645 TYPE_DECL, get_identifier ("__builtin_va_list"),
5646 va_list_type_node));
5647 if (targetm.enum_va_list_p)
5649 int l;
5650 const char *pname;
5651 tree ptype;
5653 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5655 lang_hooks.decls.pushdecl
5656 (build_decl (UNKNOWN_LOCATION,
5657 TYPE_DECL, get_identifier (pname),
5658 ptype));
5663 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5665 va_list_arg_type_node = va_list_ref_type_node =
5666 build_pointer_type (TREE_TYPE (va_list_type_node));
5668 else
5670 va_list_arg_type_node = va_list_type_node;
5671 va_list_ref_type_node = build_reference_type (va_list_type_node);
5674 if (!flag_preprocess_only)
5675 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5677 main_identifier_node = get_identifier ("main");
5679 /* Create the built-in __null node. It is important that this is
5680 not shared. */
5681 null_node = make_node (INTEGER_CST);
5682 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5684 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5685 memset (builtin_types, 0, sizeof (builtin_types));
5688 /* The number of named compound-literals generated thus far. */
5689 static GTY(()) int compound_literal_number;
5691 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5693 void
5694 set_compound_literal_name (tree decl)
5696 char *name;
5697 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5698 compound_literal_number);
5699 compound_literal_number++;
5700 DECL_NAME (decl) = get_identifier (name);
5703 tree
5704 build_va_arg (location_t loc, tree expr, tree type)
5706 expr = build1 (VA_ARG_EXPR, type, expr);
5707 SET_EXPR_LOCATION (expr, loc);
5708 return expr;
5712 /* Linked list of disabled built-in functions. */
5714 typedef struct disabled_builtin
5716 const char *name;
5717 struct disabled_builtin *next;
5718 } disabled_builtin;
5719 static disabled_builtin *disabled_builtins = NULL;
5721 static bool builtin_function_disabled_p (const char *);
5723 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5724 begins with "__builtin_", give an error. */
5726 void
5727 disable_builtin_function (const char *name)
5729 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5730 error ("cannot disable built-in function %qs", name);
5731 else
5733 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5734 new_disabled_builtin->name = name;
5735 new_disabled_builtin->next = disabled_builtins;
5736 disabled_builtins = new_disabled_builtin;
5741 /* Return true if the built-in function NAME has been disabled, false
5742 otherwise. */
5744 static bool
5745 builtin_function_disabled_p (const char *name)
5747 disabled_builtin *p;
5748 for (p = disabled_builtins; p != NULL; p = p->next)
5750 if (strcmp (name, p->name) == 0)
5751 return true;
5753 return false;
5757 /* Worker for DEF_BUILTIN.
5758 Possibly define a builtin function with one or two names.
5759 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5760 nonansi_p and flag_no_nonansi_builtin. */
5762 static void
5763 def_builtin_1 (enum built_in_function fncode,
5764 const char *name,
5765 enum built_in_class fnclass,
5766 tree fntype, tree libtype,
5767 bool both_p, bool fallback_p, bool nonansi_p,
5768 tree fnattrs, bool implicit_p)
5770 tree decl;
5771 const char *libname;
5773 if (fntype == error_mark_node)
5774 return;
5776 gcc_assert ((!both_p && !fallback_p)
5777 || !strncmp (name, "__builtin_",
5778 strlen ("__builtin_")));
5780 libname = name + strlen ("__builtin_");
5781 decl = add_builtin_function (name, fntype, fncode, fnclass,
5782 (fallback_p ? libname : NULL),
5783 fnattrs);
5785 set_builtin_decl (fncode, decl, implicit_p);
5787 if (both_p
5788 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5789 && !(nonansi_p && flag_no_nonansi_builtin))
5790 add_builtin_function (libname, libtype, fncode, fnclass,
5791 NULL, fnattrs);
5794 /* Nonzero if the type T promotes to int. This is (nearly) the
5795 integral promotions defined in ISO C99 6.3.1.1/2. */
5797 bool
5798 c_promoting_integer_type_p (const_tree t)
5800 switch (TREE_CODE (t))
5802 case INTEGER_TYPE:
5803 return (TYPE_MAIN_VARIANT (t) == char_type_node
5804 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5805 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5806 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5807 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5808 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5810 case ENUMERAL_TYPE:
5811 /* ??? Technically all enumerations not larger than an int
5812 promote to an int. But this is used along code paths
5813 that only want to notice a size change. */
5814 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5816 case BOOLEAN_TYPE:
5817 return 1;
5819 default:
5820 return 0;
5824 /* Return 1 if PARMS specifies a fixed number of parameters
5825 and none of their types is affected by default promotions. */
5828 self_promoting_args_p (const_tree parms)
5830 const_tree t;
5831 for (t = parms; t; t = TREE_CHAIN (t))
5833 tree type = TREE_VALUE (t);
5835 if (type == error_mark_node)
5836 continue;
5838 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5839 return 0;
5841 if (type == 0)
5842 return 0;
5844 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5845 return 0;
5847 if (c_promoting_integer_type_p (type))
5848 return 0;
5850 return 1;
5853 /* Recursively remove any '*' or '&' operator from TYPE. */
5854 tree
5855 strip_pointer_operator (tree t)
5857 while (POINTER_TYPE_P (t))
5858 t = TREE_TYPE (t);
5859 return t;
5862 /* Recursively remove pointer or array type from TYPE. */
5863 tree
5864 strip_pointer_or_array_types (tree t)
5866 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5867 t = TREE_TYPE (t);
5868 return t;
5871 /* Used to compare case labels. K1 and K2 are actually tree nodes
5872 representing case labels, or NULL_TREE for a `default' label.
5873 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5874 K2, and 0 if K1 and K2 are equal. */
5877 case_compare (splay_tree_key k1, splay_tree_key k2)
5879 /* Consider a NULL key (such as arises with a `default' label) to be
5880 smaller than anything else. */
5881 if (!k1)
5882 return k2 ? -1 : 0;
5883 else if (!k2)
5884 return k1 ? 1 : 0;
5886 return tree_int_cst_compare ((tree) k1, (tree) k2);
5889 /* Process a case label, located at LOC, for the range LOW_VALUE
5890 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5891 then this case label is actually a `default' label. If only
5892 HIGH_VALUE is NULL_TREE, then case label was declared using the
5893 usual C/C++ syntax, rather than the GNU case range extension.
5894 CASES is a tree containing all the case ranges processed so far;
5895 COND is the condition for the switch-statement itself. Returns the
5896 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5897 is created. */
5899 tree
5900 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5901 tree low_value, tree high_value)
5903 tree type;
5904 tree label;
5905 tree case_label;
5906 splay_tree_node node;
5908 /* Create the LABEL_DECL itself. */
5909 label = create_artificial_label (loc);
5911 /* If there was an error processing the switch condition, bail now
5912 before we get more confused. */
5913 if (!cond || cond == error_mark_node)
5914 goto error_out;
5916 if ((low_value && TREE_TYPE (low_value)
5917 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5918 || (high_value && TREE_TYPE (high_value)
5919 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5921 error_at (loc, "pointers are not permitted as case values");
5922 goto error_out;
5925 /* Case ranges are a GNU extension. */
5926 if (high_value)
5927 pedwarn (loc, OPT_Wpedantic,
5928 "range expressions in switch statements are non-standard");
5930 type = TREE_TYPE (cond);
5931 if (low_value)
5933 low_value = check_case_value (low_value);
5934 low_value = convert_and_check (type, low_value);
5935 if (low_value == error_mark_node)
5936 goto error_out;
5938 if (high_value)
5940 high_value = check_case_value (high_value);
5941 high_value = convert_and_check (type, high_value);
5942 if (high_value == error_mark_node)
5943 goto error_out;
5946 if (low_value && high_value)
5948 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5949 really a case range, even though it was written that way.
5950 Remove the HIGH_VALUE to simplify later processing. */
5951 if (tree_int_cst_equal (low_value, high_value))
5952 high_value = NULL_TREE;
5953 else if (!tree_int_cst_lt (low_value, high_value))
5954 warning_at (loc, 0, "empty range specified");
5957 /* See if the case is in range of the type of the original testing
5958 expression. If both low_value and high_value are out of range,
5959 don't insert the case label and return NULL_TREE. */
5960 if (low_value
5961 && !check_case_bounds (type, orig_type,
5962 &low_value, high_value ? &high_value : NULL))
5963 return NULL_TREE;
5965 /* Look up the LOW_VALUE in the table of case labels we already
5966 have. */
5967 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5968 /* If there was not an exact match, check for overlapping ranges.
5969 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5970 that's a `default' label and the only overlap is an exact match. */
5971 if (!node && (low_value || high_value))
5973 splay_tree_node low_bound;
5974 splay_tree_node high_bound;
5976 /* Even though there wasn't an exact match, there might be an
5977 overlap between this case range and another case range.
5978 Since we've (inductively) not allowed any overlapping case
5979 ranges, we simply need to find the greatest low case label
5980 that is smaller that LOW_VALUE, and the smallest low case
5981 label that is greater than LOW_VALUE. If there is an overlap
5982 it will occur in one of these two ranges. */
5983 low_bound = splay_tree_predecessor (cases,
5984 (splay_tree_key) low_value);
5985 high_bound = splay_tree_successor (cases,
5986 (splay_tree_key) low_value);
5988 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5989 the LOW_VALUE, so there is no need to check unless the
5990 LOW_BOUND is in fact itself a case range. */
5991 if (low_bound
5992 && CASE_HIGH ((tree) low_bound->value)
5993 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5994 low_value) >= 0)
5995 node = low_bound;
5996 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5997 range is bigger than the low end of the current range, so we
5998 are only interested if the current range is a real range, and
5999 not an ordinary case label. */
6000 else if (high_bound
6001 && high_value
6002 && (tree_int_cst_compare ((tree) high_bound->key,
6003 high_value)
6004 <= 0))
6005 node = high_bound;
6007 /* If there was an overlap, issue an error. */
6008 if (node)
6010 tree duplicate = CASE_LABEL ((tree) node->value);
6012 if (high_value)
6014 error_at (loc, "duplicate (or overlapping) case value");
6015 error_at (DECL_SOURCE_LOCATION (duplicate),
6016 "this is the first entry overlapping that value");
6018 else if (low_value)
6020 error_at (loc, "duplicate case value") ;
6021 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6023 else
6025 error_at (loc, "multiple default labels in one switch");
6026 error_at (DECL_SOURCE_LOCATION (duplicate),
6027 "this is the first default label");
6029 goto error_out;
6032 /* Add a CASE_LABEL to the statement-tree. */
6033 case_label = add_stmt (build_case_label (low_value, high_value, label));
6034 /* Register this case label in the splay tree. */
6035 splay_tree_insert (cases,
6036 (splay_tree_key) low_value,
6037 (splay_tree_value) case_label);
6039 return case_label;
6041 error_out:
6042 /* Add a label so that the back-end doesn't think that the beginning of
6043 the switch is unreachable. Note that we do not add a case label, as
6044 that just leads to duplicates and thence to failure later on. */
6045 if (!cases->root)
6047 tree t = create_artificial_label (loc);
6048 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6050 return error_mark_node;
6053 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6054 Used to verify that case values match up with enumerator values. */
6056 static void
6057 match_case_to_enum_1 (tree key, tree type, tree label)
6059 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6061 /* ??? Not working too hard to print the double-word value.
6062 Should perhaps be done with %lwd in the diagnostic routines? */
6063 if (TREE_INT_CST_HIGH (key) == 0)
6064 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6065 TREE_INT_CST_LOW (key));
6066 else if (!TYPE_UNSIGNED (type)
6067 && TREE_INT_CST_HIGH (key) == -1
6068 && TREE_INT_CST_LOW (key) != 0)
6069 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6070 -TREE_INT_CST_LOW (key));
6071 else
6072 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6073 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6074 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6076 if (TYPE_NAME (type) == 0)
6077 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6078 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6079 "case value %qs not in enumerated type",
6080 buf);
6081 else
6082 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6083 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6084 "case value %qs not in enumerated type %qT",
6085 buf, type);
6088 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6089 Used to verify that case values match up with enumerator values. */
6091 static int
6092 match_case_to_enum (splay_tree_node node, void *data)
6094 tree label = (tree) node->value;
6095 tree type = (tree) data;
6097 /* Skip default case. */
6098 if (!CASE_LOW (label))
6099 return 0;
6101 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6102 when we did our enum->case scan. Reset our scratch bit after. */
6103 if (!CASE_LOW_SEEN (label))
6104 match_case_to_enum_1 (CASE_LOW (label), type, label);
6105 else
6106 CASE_LOW_SEEN (label) = 0;
6108 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6109 not set, that means that CASE_HIGH did not appear when we did our
6110 enum->case scan. Reset our scratch bit after. */
6111 if (CASE_HIGH (label))
6113 if (!CASE_HIGH_SEEN (label))
6114 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6115 else
6116 CASE_HIGH_SEEN (label) = 0;
6119 return 0;
6122 /* Handle -Wswitch*. Called from the front end after parsing the
6123 switch construct. */
6124 /* ??? Should probably be somewhere generic, since other languages
6125 besides C and C++ would want this. At the moment, however, C/C++
6126 are the only tree-ssa languages that support enumerations at all,
6127 so the point is moot. */
6129 void
6130 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6131 tree type, tree cond)
6133 splay_tree_node default_node;
6134 splay_tree_node node;
6135 tree chain;
6137 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6138 return;
6140 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6141 if (!default_node)
6142 warning_at (switch_location, OPT_Wswitch_default,
6143 "switch missing default case");
6145 /* From here on, we only care about about enumerated types. */
6146 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6147 return;
6149 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6150 if (!warn_switch_enum && !warn_switch)
6151 return;
6153 /* Check the cases. Warn about case values which are not members of
6154 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6155 there is no default case, check that exactly all enumeration
6156 literals are covered by the cases. */
6158 /* Clearing COND if it is not an integer constant simplifies
6159 the tests inside the loop below. */
6160 if (TREE_CODE (cond) != INTEGER_CST)
6161 cond = NULL_TREE;
6163 /* The time complexity here is O(N*lg(N)) worst case, but for the
6164 common case of monotonically increasing enumerators, it is
6165 O(N), since the nature of the splay tree will keep the next
6166 element adjacent to the root at all times. */
6168 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6170 tree value = TREE_VALUE (chain);
6171 if (TREE_CODE (value) == CONST_DECL)
6172 value = DECL_INITIAL (value);
6173 node = splay_tree_lookup (cases, (splay_tree_key) value);
6174 if (node)
6176 /* Mark the CASE_LOW part of the case entry as seen. */
6177 tree label = (tree) node->value;
6178 CASE_LOW_SEEN (label) = 1;
6179 continue;
6182 /* Even though there wasn't an exact match, there might be a
6183 case range which includes the enumerator's value. */
6184 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6185 if (node && CASE_HIGH ((tree) node->value))
6187 tree label = (tree) node->value;
6188 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6189 if (cmp >= 0)
6191 /* If we match the upper bound exactly, mark the CASE_HIGH
6192 part of the case entry as seen. */
6193 if (cmp == 0)
6194 CASE_HIGH_SEEN (label) = 1;
6195 continue;
6199 /* We've now determined that this enumerated literal isn't
6200 handled by the case labels of the switch statement. */
6202 /* If the switch expression is a constant, we only really care
6203 about whether that constant is handled by the switch. */
6204 if (cond && tree_int_cst_compare (cond, value))
6205 continue;
6207 /* If there is a default_node, the only relevant option is
6208 Wswitch-enum. Otherwise, if both are enabled then we prefer
6209 to warn using -Wswitch because -Wswitch is enabled by -Wall
6210 while -Wswitch-enum is explicit. */
6211 warning_at (switch_location,
6212 (default_node || !warn_switch
6213 ? OPT_Wswitch_enum
6214 : OPT_Wswitch),
6215 "enumeration value %qE not handled in switch",
6216 TREE_PURPOSE (chain));
6219 /* Warn if there are case expressions that don't correspond to
6220 enumerators. This can occur since C and C++ don't enforce
6221 type-checking of assignments to enumeration variables.
6223 The time complexity here is now always O(N) worst case, since
6224 we should have marked both the lower bound and upper bound of
6225 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6226 above. This scan also resets those fields. */
6228 splay_tree_foreach (cases, match_case_to_enum, type);
6231 /* Finish an expression taking the address of LABEL (an
6232 IDENTIFIER_NODE). Returns an expression for the address.
6234 LOC is the location for the expression returned. */
6236 tree
6237 finish_label_address_expr (tree label, location_t loc)
6239 tree result;
6241 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6243 if (label == error_mark_node)
6244 return error_mark_node;
6246 label = lookup_label (label);
6247 if (label == NULL_TREE)
6248 result = null_pointer_node;
6249 else
6251 TREE_USED (label) = 1;
6252 result = build1 (ADDR_EXPR, ptr_type_node, label);
6253 /* The current function is not necessarily uninlinable.
6254 Computed gotos are incompatible with inlining, but the value
6255 here could be used only in a diagnostic, for example. */
6256 protected_set_expr_location (result, loc);
6259 return result;
6263 /* Given a boolean expression ARG, return a tree representing an increment
6264 or decrement (as indicated by CODE) of ARG. The front end must check for
6265 invalid cases (e.g., decrement in C++). */
6266 tree
6267 boolean_increment (enum tree_code code, tree arg)
6269 tree val;
6270 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6272 arg = stabilize_reference (arg);
6273 switch (code)
6275 case PREINCREMENT_EXPR:
6276 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6277 break;
6278 case POSTINCREMENT_EXPR:
6279 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6280 arg = save_expr (arg);
6281 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6282 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6283 break;
6284 case PREDECREMENT_EXPR:
6285 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6286 invert_truthvalue_loc (input_location, arg));
6287 break;
6288 case POSTDECREMENT_EXPR:
6289 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6290 invert_truthvalue_loc (input_location, arg));
6291 arg = save_expr (arg);
6292 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6293 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6294 break;
6295 default:
6296 gcc_unreachable ();
6298 TREE_SIDE_EFFECTS (val) = 1;
6299 return val;
6302 /* Built-in macros for stddef.h and stdint.h, that require macros
6303 defined in this file. */
6304 void
6305 c_stddef_cpp_builtins(void)
6307 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6308 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6309 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6310 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6311 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6312 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6313 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6314 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6315 if (SIG_ATOMIC_TYPE)
6316 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6317 if (INT8_TYPE)
6318 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6319 if (INT16_TYPE)
6320 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6321 if (INT32_TYPE)
6322 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6323 if (INT64_TYPE)
6324 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6325 if (UINT8_TYPE)
6326 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6327 if (UINT16_TYPE)
6328 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6329 if (UINT32_TYPE)
6330 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6331 if (UINT64_TYPE)
6332 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6333 if (INT_LEAST8_TYPE)
6334 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6335 if (INT_LEAST16_TYPE)
6336 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6337 if (INT_LEAST32_TYPE)
6338 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6339 if (INT_LEAST64_TYPE)
6340 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6341 if (UINT_LEAST8_TYPE)
6342 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6343 if (UINT_LEAST16_TYPE)
6344 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6345 if (UINT_LEAST32_TYPE)
6346 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6347 if (UINT_LEAST64_TYPE)
6348 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6349 if (INT_FAST8_TYPE)
6350 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6351 if (INT_FAST16_TYPE)
6352 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6353 if (INT_FAST32_TYPE)
6354 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6355 if (INT_FAST64_TYPE)
6356 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6357 if (UINT_FAST8_TYPE)
6358 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6359 if (UINT_FAST16_TYPE)
6360 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6361 if (UINT_FAST32_TYPE)
6362 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6363 if (UINT_FAST64_TYPE)
6364 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6365 if (INTPTR_TYPE)
6366 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6367 if (UINTPTR_TYPE)
6368 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6371 static void
6372 c_init_attributes (void)
6374 /* Fill in the built_in_attributes array. */
6375 #define DEF_ATTR_NULL_TREE(ENUM) \
6376 built_in_attributes[(int) ENUM] = NULL_TREE;
6377 #define DEF_ATTR_INT(ENUM, VALUE) \
6378 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6379 #define DEF_ATTR_STRING(ENUM, VALUE) \
6380 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6381 #define DEF_ATTR_IDENT(ENUM, STRING) \
6382 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6383 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6384 built_in_attributes[(int) ENUM] \
6385 = tree_cons (built_in_attributes[(int) PURPOSE], \
6386 built_in_attributes[(int) VALUE], \
6387 built_in_attributes[(int) CHAIN]);
6388 #include "builtin-attrs.def"
6389 #undef DEF_ATTR_NULL_TREE
6390 #undef DEF_ATTR_INT
6391 #undef DEF_ATTR_IDENT
6392 #undef DEF_ATTR_TREE_LIST
6395 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6396 identifier as an argument, so the front end shouldn't look it up. */
6398 bool
6399 attribute_takes_identifier_p (const_tree attr_id)
6401 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6402 if (spec == NULL)
6403 /* Unknown attribute that we'll end up ignoring, return true so we
6404 don't complain about an identifier argument. */
6405 return true;
6406 else if (!strcmp ("mode", spec->name)
6407 || !strcmp ("format", spec->name)
6408 || !strcmp ("cleanup", spec->name))
6409 return true;
6410 else
6411 return targetm.attribute_takes_identifier_p (attr_id);
6414 /* Attribute handlers common to C front ends. */
6416 /* Handle a "packed" attribute; arguments as in
6417 struct attribute_spec.handler. */
6419 static tree
6420 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6421 int flags, bool *no_add_attrs)
6423 if (TYPE_P (*node))
6425 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6426 *node = build_variant_type_copy (*node);
6427 TYPE_PACKED (*node) = 1;
6429 else if (TREE_CODE (*node) == FIELD_DECL)
6431 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6432 /* Still pack bitfields. */
6433 && ! DECL_INITIAL (*node))
6434 warning (OPT_Wattributes,
6435 "%qE attribute ignored for field of type %qT",
6436 name, TREE_TYPE (*node));
6437 else
6438 DECL_PACKED (*node) = 1;
6440 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6441 used for DECL_REGISTER. It wouldn't mean anything anyway.
6442 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6443 that changes what the typedef is typing. */
6444 else
6446 warning (OPT_Wattributes, "%qE attribute ignored", name);
6447 *no_add_attrs = true;
6450 return NULL_TREE;
6453 /* Handle a "nocommon" attribute; arguments as in
6454 struct attribute_spec.handler. */
6456 static tree
6457 handle_nocommon_attribute (tree *node, tree name,
6458 tree ARG_UNUSED (args),
6459 int ARG_UNUSED (flags), bool *no_add_attrs)
6461 if (TREE_CODE (*node) == VAR_DECL)
6462 DECL_COMMON (*node) = 0;
6463 else
6465 warning (OPT_Wattributes, "%qE attribute ignored", name);
6466 *no_add_attrs = true;
6469 return NULL_TREE;
6472 /* Handle a "common" attribute; arguments as in
6473 struct attribute_spec.handler. */
6475 static tree
6476 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6477 int ARG_UNUSED (flags), bool *no_add_attrs)
6479 if (TREE_CODE (*node) == VAR_DECL)
6480 DECL_COMMON (*node) = 1;
6481 else
6483 warning (OPT_Wattributes, "%qE attribute ignored", name);
6484 *no_add_attrs = true;
6487 return NULL_TREE;
6490 /* Handle a "noreturn" attribute; arguments as in
6491 struct attribute_spec.handler. */
6493 static tree
6494 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6495 int ARG_UNUSED (flags), bool *no_add_attrs)
6497 tree type = TREE_TYPE (*node);
6499 /* See FIXME comment in c_common_attribute_table. */
6500 if (TREE_CODE (*node) == FUNCTION_DECL
6501 || objc_method_decl (TREE_CODE (*node)))
6502 TREE_THIS_VOLATILE (*node) = 1;
6503 else if (TREE_CODE (type) == POINTER_TYPE
6504 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6505 TREE_TYPE (*node)
6506 = build_pointer_type
6507 (build_type_variant (TREE_TYPE (type),
6508 TYPE_READONLY (TREE_TYPE (type)), 1));
6509 else
6511 warning (OPT_Wattributes, "%qE attribute ignored", name);
6512 *no_add_attrs = true;
6515 return NULL_TREE;
6518 /* Handle a "hot" and attribute; arguments as in
6519 struct attribute_spec.handler. */
6521 static tree
6522 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6523 int ARG_UNUSED (flags), bool *no_add_attrs)
6525 if (TREE_CODE (*node) == FUNCTION_DECL
6526 || TREE_CODE (*node) == LABEL_DECL)
6528 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6530 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6531 name, "cold");
6532 *no_add_attrs = true;
6534 /* Most of the rest of the hot processing is done later with
6535 lookup_attribute. */
6537 else
6539 warning (OPT_Wattributes, "%qE attribute ignored", name);
6540 *no_add_attrs = true;
6543 return NULL_TREE;
6546 /* Handle a "cold" and attribute; arguments as in
6547 struct attribute_spec.handler. */
6549 static tree
6550 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6551 int ARG_UNUSED (flags), bool *no_add_attrs)
6553 if (TREE_CODE (*node) == FUNCTION_DECL
6554 || TREE_CODE (*node) == LABEL_DECL)
6556 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6558 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6559 name, "hot");
6560 *no_add_attrs = true;
6562 /* Most of the rest of the cold processing is done later with
6563 lookup_attribute. */
6565 else
6567 warning (OPT_Wattributes, "%qE attribute ignored", name);
6568 *no_add_attrs = true;
6571 return NULL_TREE;
6574 /* Handle a "no_sanitize_address" attribute; arguments as in
6575 struct attribute_spec.handler. */
6577 static tree
6578 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6579 bool *no_add_attrs)
6581 if (TREE_CODE (*node) != FUNCTION_DECL)
6583 warning (OPT_Wattributes, "%qE attribute ignored", name);
6584 *no_add_attrs = true;
6587 return NULL_TREE;
6590 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6591 struct attribute_spec.handler. */
6593 static tree
6594 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6595 bool *no_add_attrs)
6597 if (TREE_CODE (*node) != FUNCTION_DECL)
6598 warning (OPT_Wattributes, "%qE attribute ignored", name);
6599 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6600 DECL_ATTRIBUTES (*node)
6601 = tree_cons (get_identifier ("no_sanitize_address"),
6602 NULL_TREE, DECL_ATTRIBUTES (*node));
6603 *no_add_attrs = true;
6604 return NULL_TREE;
6607 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6608 struct attribute_spec.handler. */
6610 static tree
6611 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6612 bool *no_add_attrs)
6614 if (TREE_CODE (*node) != FUNCTION_DECL)
6616 warning (OPT_Wattributes, "%qE attribute ignored", name);
6617 *no_add_attrs = true;
6620 return NULL_TREE;
6623 /* Handle a "noinline" attribute; arguments as in
6624 struct attribute_spec.handler. */
6626 static tree
6627 handle_noinline_attribute (tree *node, tree name,
6628 tree ARG_UNUSED (args),
6629 int ARG_UNUSED (flags), bool *no_add_attrs)
6631 if (TREE_CODE (*node) == FUNCTION_DECL)
6632 DECL_UNINLINABLE (*node) = 1;
6633 else
6635 warning (OPT_Wattributes, "%qE attribute ignored", name);
6636 *no_add_attrs = true;
6639 return NULL_TREE;
6642 /* Handle a "noclone" attribute; arguments as in
6643 struct attribute_spec.handler. */
6645 static tree
6646 handle_noclone_attribute (tree *node, tree name,
6647 tree ARG_UNUSED (args),
6648 int ARG_UNUSED (flags), bool *no_add_attrs)
6650 if (TREE_CODE (*node) != FUNCTION_DECL)
6652 warning (OPT_Wattributes, "%qE attribute ignored", name);
6653 *no_add_attrs = true;
6656 return NULL_TREE;
6659 /* Handle a "always_inline" attribute; arguments as in
6660 struct attribute_spec.handler. */
6662 static tree
6663 handle_always_inline_attribute (tree *node, tree name,
6664 tree ARG_UNUSED (args),
6665 int ARG_UNUSED (flags),
6666 bool *no_add_attrs)
6668 if (TREE_CODE (*node) == FUNCTION_DECL)
6670 /* Set the attribute and mark it for disregarding inline
6671 limits. */
6672 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6674 else
6676 warning (OPT_Wattributes, "%qE attribute ignored", name);
6677 *no_add_attrs = true;
6680 return NULL_TREE;
6683 /* Handle a "gnu_inline" attribute; arguments as in
6684 struct attribute_spec.handler. */
6686 static tree
6687 handle_gnu_inline_attribute (tree *node, tree name,
6688 tree ARG_UNUSED (args),
6689 int ARG_UNUSED (flags),
6690 bool *no_add_attrs)
6692 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6694 /* Do nothing else, just set the attribute. We'll get at
6695 it later with lookup_attribute. */
6697 else
6699 warning (OPT_Wattributes, "%qE attribute ignored", name);
6700 *no_add_attrs = true;
6703 return NULL_TREE;
6706 /* Handle a "leaf" attribute; arguments as in
6707 struct attribute_spec.handler. */
6709 static tree
6710 handle_leaf_attribute (tree *node, tree name,
6711 tree ARG_UNUSED (args),
6712 int ARG_UNUSED (flags), bool *no_add_attrs)
6714 if (TREE_CODE (*node) != FUNCTION_DECL)
6716 warning (OPT_Wattributes, "%qE attribute ignored", name);
6717 *no_add_attrs = true;
6719 if (!TREE_PUBLIC (*node))
6721 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6722 *no_add_attrs = true;
6725 return NULL_TREE;
6728 /* Handle an "artificial" attribute; arguments as in
6729 struct attribute_spec.handler. */
6731 static tree
6732 handle_artificial_attribute (tree *node, tree name,
6733 tree ARG_UNUSED (args),
6734 int ARG_UNUSED (flags),
6735 bool *no_add_attrs)
6737 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6739 /* Do nothing else, just set the attribute. We'll get at
6740 it later with lookup_attribute. */
6742 else
6744 warning (OPT_Wattributes, "%qE attribute ignored", name);
6745 *no_add_attrs = true;
6748 return NULL_TREE;
6751 /* Handle a "flatten" attribute; arguments as in
6752 struct attribute_spec.handler. */
6754 static tree
6755 handle_flatten_attribute (tree *node, tree name,
6756 tree args ATTRIBUTE_UNUSED,
6757 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6759 if (TREE_CODE (*node) == FUNCTION_DECL)
6760 /* Do nothing else, just set the attribute. We'll get at
6761 it later with lookup_attribute. */
6763 else
6765 warning (OPT_Wattributes, "%qE attribute ignored", name);
6766 *no_add_attrs = true;
6769 return NULL_TREE;
6772 /* Handle a "warning" or "error" attribute; arguments as in
6773 struct attribute_spec.handler. */
6775 static tree
6776 handle_error_attribute (tree *node, tree name, tree args,
6777 int ARG_UNUSED (flags), bool *no_add_attrs)
6779 if (TREE_CODE (*node) == FUNCTION_DECL
6780 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6781 /* Do nothing else, just set the attribute. We'll get at
6782 it later with lookup_attribute. */
6784 else
6786 warning (OPT_Wattributes, "%qE attribute ignored", name);
6787 *no_add_attrs = true;
6790 return NULL_TREE;
6793 /* Handle a "used" attribute; arguments as in
6794 struct attribute_spec.handler. */
6796 static tree
6797 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6798 int ARG_UNUSED (flags), bool *no_add_attrs)
6800 tree node = *pnode;
6802 if (TREE_CODE (node) == FUNCTION_DECL
6803 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6804 || (TREE_CODE (node) == TYPE_DECL))
6806 TREE_USED (node) = 1;
6807 DECL_PRESERVE_P (node) = 1;
6808 if (TREE_CODE (node) == VAR_DECL)
6809 DECL_READ_P (node) = 1;
6811 else
6813 warning (OPT_Wattributes, "%qE attribute ignored", name);
6814 *no_add_attrs = true;
6817 return NULL_TREE;
6820 /* Handle a "unused" attribute; arguments as in
6821 struct attribute_spec.handler. */
6823 static tree
6824 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6825 int flags, bool *no_add_attrs)
6827 if (DECL_P (*node))
6829 tree decl = *node;
6831 if (TREE_CODE (decl) == PARM_DECL
6832 || TREE_CODE (decl) == VAR_DECL
6833 || TREE_CODE (decl) == FUNCTION_DECL
6834 || TREE_CODE (decl) == LABEL_DECL
6835 || TREE_CODE (decl) == TYPE_DECL)
6837 TREE_USED (decl) = 1;
6838 if (TREE_CODE (decl) == VAR_DECL
6839 || TREE_CODE (decl) == PARM_DECL)
6840 DECL_READ_P (decl) = 1;
6842 else
6844 warning (OPT_Wattributes, "%qE attribute ignored", name);
6845 *no_add_attrs = true;
6848 else
6850 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6851 *node = build_variant_type_copy (*node);
6852 TREE_USED (*node) = 1;
6855 return NULL_TREE;
6858 /* Handle a "externally_visible" attribute; arguments as in
6859 struct attribute_spec.handler. */
6861 static tree
6862 handle_externally_visible_attribute (tree *pnode, tree name,
6863 tree ARG_UNUSED (args),
6864 int ARG_UNUSED (flags),
6865 bool *no_add_attrs)
6867 tree node = *pnode;
6869 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6871 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6872 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6874 warning (OPT_Wattributes,
6875 "%qE attribute have effect only on public objects", name);
6876 *no_add_attrs = true;
6879 else
6881 warning (OPT_Wattributes, "%qE attribute ignored", name);
6882 *no_add_attrs = true;
6885 return NULL_TREE;
6888 /* Handle a "const" attribute; arguments as in
6889 struct attribute_spec.handler. */
6891 static tree
6892 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6893 int ARG_UNUSED (flags), bool *no_add_attrs)
6895 tree type = TREE_TYPE (*node);
6897 /* See FIXME comment on noreturn in c_common_attribute_table. */
6898 if (TREE_CODE (*node) == FUNCTION_DECL)
6899 TREE_READONLY (*node) = 1;
6900 else if (TREE_CODE (type) == POINTER_TYPE
6901 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6902 TREE_TYPE (*node)
6903 = build_pointer_type
6904 (build_type_variant (TREE_TYPE (type), 1,
6905 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6906 else
6908 warning (OPT_Wattributes, "%qE attribute ignored", name);
6909 *no_add_attrs = true;
6912 return NULL_TREE;
6915 /* Handle a "transparent_union" attribute; arguments as in
6916 struct attribute_spec.handler. */
6918 static tree
6919 handle_transparent_union_attribute (tree *node, tree name,
6920 tree ARG_UNUSED (args), int flags,
6921 bool *no_add_attrs)
6923 tree type;
6925 *no_add_attrs = true;
6928 if (TREE_CODE (*node) == TYPE_DECL
6929 && ! (flags & ATTR_FLAG_CXX11))
6930 node = &TREE_TYPE (*node);
6931 type = *node;
6933 if (TREE_CODE (type) == UNION_TYPE)
6935 /* Make sure that the first field will work for a transparent union.
6936 If the type isn't complete yet, leave the check to the code in
6937 finish_struct. */
6938 if (TYPE_SIZE (type))
6940 tree first = first_field (type);
6941 if (first == NULL_TREE
6942 || DECL_ARTIFICIAL (first)
6943 || TYPE_MODE (type) != DECL_MODE (first))
6944 goto ignored;
6947 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6949 /* If the type isn't complete yet, setting the flag
6950 on a variant wouldn't ever be checked. */
6951 if (!TYPE_SIZE (type))
6952 goto ignored;
6954 /* build_duplicate_type doesn't work for C++. */
6955 if (c_dialect_cxx ())
6956 goto ignored;
6958 /* A type variant isn't good enough, since we don't a cast
6959 to such a type removed as a no-op. */
6960 *node = type = build_duplicate_type (type);
6963 TYPE_TRANSPARENT_AGGR (type) = 1;
6964 return NULL_TREE;
6967 ignored:
6968 warning (OPT_Wattributes, "%qE attribute ignored", name);
6969 return NULL_TREE;
6972 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6973 get the requested priority for a constructor or destructor,
6974 possibly issuing diagnostics for invalid or reserved
6975 priorities. */
6977 static priority_type
6978 get_priority (tree args, bool is_destructor)
6980 HOST_WIDE_INT pri;
6981 tree arg;
6983 if (!args)
6984 return DEFAULT_INIT_PRIORITY;
6986 if (!SUPPORTS_INIT_PRIORITY)
6988 if (is_destructor)
6989 error ("destructor priorities are not supported");
6990 else
6991 error ("constructor priorities are not supported");
6992 return DEFAULT_INIT_PRIORITY;
6995 arg = TREE_VALUE (args);
6996 arg = default_conversion (arg);
6997 if (!host_integerp (arg, /*pos=*/0)
6998 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6999 goto invalid;
7001 pri = tree_low_cst (arg, /*pos=*/0);
7002 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7003 goto invalid;
7005 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7007 if (is_destructor)
7008 warning (0,
7009 "destructor priorities from 0 to %d are reserved "
7010 "for the implementation",
7011 MAX_RESERVED_INIT_PRIORITY);
7012 else
7013 warning (0,
7014 "constructor priorities from 0 to %d are reserved "
7015 "for the implementation",
7016 MAX_RESERVED_INIT_PRIORITY);
7018 return pri;
7020 invalid:
7021 if (is_destructor)
7022 error ("destructor priorities must be integers from 0 to %d inclusive",
7023 MAX_INIT_PRIORITY);
7024 else
7025 error ("constructor priorities must be integers from 0 to %d inclusive",
7026 MAX_INIT_PRIORITY);
7027 return DEFAULT_INIT_PRIORITY;
7030 /* Handle a "constructor" attribute; arguments as in
7031 struct attribute_spec.handler. */
7033 static tree
7034 handle_constructor_attribute (tree *node, tree name, tree args,
7035 int ARG_UNUSED (flags),
7036 bool *no_add_attrs)
7038 tree decl = *node;
7039 tree type = TREE_TYPE (decl);
7041 if (TREE_CODE (decl) == FUNCTION_DECL
7042 && TREE_CODE (type) == FUNCTION_TYPE
7043 && decl_function_context (decl) == 0)
7045 priority_type priority;
7046 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7047 priority = get_priority (args, /*is_destructor=*/false);
7048 SET_DECL_INIT_PRIORITY (decl, priority);
7049 TREE_USED (decl) = 1;
7051 else
7053 warning (OPT_Wattributes, "%qE attribute ignored", name);
7054 *no_add_attrs = true;
7057 return NULL_TREE;
7060 /* Handle a "destructor" attribute; arguments as in
7061 struct attribute_spec.handler. */
7063 static tree
7064 handle_destructor_attribute (tree *node, tree name, tree args,
7065 int ARG_UNUSED (flags),
7066 bool *no_add_attrs)
7068 tree decl = *node;
7069 tree type = TREE_TYPE (decl);
7071 if (TREE_CODE (decl) == FUNCTION_DECL
7072 && TREE_CODE (type) == FUNCTION_TYPE
7073 && decl_function_context (decl) == 0)
7075 priority_type priority;
7076 DECL_STATIC_DESTRUCTOR (decl) = 1;
7077 priority = get_priority (args, /*is_destructor=*/true);
7078 SET_DECL_FINI_PRIORITY (decl, priority);
7079 TREE_USED (decl) = 1;
7081 else
7083 warning (OPT_Wattributes, "%qE attribute ignored", name);
7084 *no_add_attrs = true;
7087 return NULL_TREE;
7090 /* Nonzero if the mode is a valid vector mode for this architecture.
7091 This returns nonzero even if there is no hardware support for the
7092 vector mode, but we can emulate with narrower modes. */
7094 static int
7095 vector_mode_valid_p (enum machine_mode mode)
7097 enum mode_class mclass = GET_MODE_CLASS (mode);
7098 enum machine_mode innermode;
7100 /* Doh! What's going on? */
7101 if (mclass != MODE_VECTOR_INT
7102 && mclass != MODE_VECTOR_FLOAT
7103 && mclass != MODE_VECTOR_FRACT
7104 && mclass != MODE_VECTOR_UFRACT
7105 && mclass != MODE_VECTOR_ACCUM
7106 && mclass != MODE_VECTOR_UACCUM)
7107 return 0;
7109 /* Hardware support. Woo hoo! */
7110 if (targetm.vector_mode_supported_p (mode))
7111 return 1;
7113 innermode = GET_MODE_INNER (mode);
7115 /* We should probably return 1 if requesting V4DI and we have no DI,
7116 but we have V2DI, but this is probably very unlikely. */
7118 /* If we have support for the inner mode, we can safely emulate it.
7119 We may not have V2DI, but me can emulate with a pair of DIs. */
7120 return targetm.scalar_mode_supported_p (innermode);
7124 /* Handle a "mode" attribute; arguments as in
7125 struct attribute_spec.handler. */
7127 static tree
7128 handle_mode_attribute (tree *node, tree name, tree args,
7129 int ARG_UNUSED (flags), bool *no_add_attrs)
7131 tree type = *node;
7132 tree ident = TREE_VALUE (args);
7134 *no_add_attrs = true;
7136 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7137 warning (OPT_Wattributes, "%qE attribute ignored", name);
7138 else
7140 int j;
7141 const char *p = IDENTIFIER_POINTER (ident);
7142 int len = strlen (p);
7143 enum machine_mode mode = VOIDmode;
7144 tree typefm;
7145 bool valid_mode;
7147 if (len > 4 && p[0] == '_' && p[1] == '_'
7148 && p[len - 1] == '_' && p[len - 2] == '_')
7150 char *newp = (char *) alloca (len - 1);
7152 strcpy (newp, &p[2]);
7153 newp[len - 4] = '\0';
7154 p = newp;
7157 /* Change this type to have a type with the specified mode.
7158 First check for the special modes. */
7159 if (!strcmp (p, "byte"))
7160 mode = byte_mode;
7161 else if (!strcmp (p, "word"))
7162 mode = word_mode;
7163 else if (!strcmp (p, "pointer"))
7164 mode = ptr_mode;
7165 else if (!strcmp (p, "libgcc_cmp_return"))
7166 mode = targetm.libgcc_cmp_return_mode ();
7167 else if (!strcmp (p, "libgcc_shift_count"))
7168 mode = targetm.libgcc_shift_count_mode ();
7169 else if (!strcmp (p, "unwind_word"))
7170 mode = targetm.unwind_word_mode ();
7171 else
7172 for (j = 0; j < NUM_MACHINE_MODES; j++)
7173 if (!strcmp (p, GET_MODE_NAME (j)))
7175 mode = (enum machine_mode) j;
7176 break;
7179 if (mode == VOIDmode)
7181 error ("unknown machine mode %qE", ident);
7182 return NULL_TREE;
7185 valid_mode = false;
7186 switch (GET_MODE_CLASS (mode))
7188 case MODE_INT:
7189 case MODE_PARTIAL_INT:
7190 case MODE_FLOAT:
7191 case MODE_DECIMAL_FLOAT:
7192 case MODE_FRACT:
7193 case MODE_UFRACT:
7194 case MODE_ACCUM:
7195 case MODE_UACCUM:
7196 valid_mode = targetm.scalar_mode_supported_p (mode);
7197 break;
7199 case MODE_COMPLEX_INT:
7200 case MODE_COMPLEX_FLOAT:
7201 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7202 break;
7204 case MODE_VECTOR_INT:
7205 case MODE_VECTOR_FLOAT:
7206 case MODE_VECTOR_FRACT:
7207 case MODE_VECTOR_UFRACT:
7208 case MODE_VECTOR_ACCUM:
7209 case MODE_VECTOR_UACCUM:
7210 warning (OPT_Wattributes, "specifying vector types with "
7211 "__attribute__ ((mode)) is deprecated");
7212 warning (OPT_Wattributes,
7213 "use __attribute__ ((vector_size)) instead");
7214 valid_mode = vector_mode_valid_p (mode);
7215 break;
7217 default:
7218 break;
7220 if (!valid_mode)
7222 error ("unable to emulate %qs", p);
7223 return NULL_TREE;
7226 if (POINTER_TYPE_P (type))
7228 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7229 tree (*fn)(tree, enum machine_mode, bool);
7231 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7233 error ("invalid pointer mode %qs", p);
7234 return NULL_TREE;
7237 if (TREE_CODE (type) == POINTER_TYPE)
7238 fn = build_pointer_type_for_mode;
7239 else
7240 fn = build_reference_type_for_mode;
7241 typefm = fn (TREE_TYPE (type), mode, false);
7243 else
7245 /* For fixed-point modes, we need to test if the signness of type
7246 and the machine mode are consistent. */
7247 if (ALL_FIXED_POINT_MODE_P (mode)
7248 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7250 error ("signedness of type and machine mode %qs don%'t match", p);
7251 return NULL_TREE;
7253 /* For fixed-point modes, we need to pass saturating info. */
7254 typefm = lang_hooks.types.type_for_mode (mode,
7255 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7256 : TYPE_UNSIGNED (type));
7259 if (typefm == NULL_TREE)
7261 error ("no data type for mode %qs", p);
7262 return NULL_TREE;
7264 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7266 /* For enumeral types, copy the precision from the integer
7267 type returned above. If not an INTEGER_TYPE, we can't use
7268 this mode for this type. */
7269 if (TREE_CODE (typefm) != INTEGER_TYPE)
7271 error ("cannot use mode %qs for enumeral types", p);
7272 return NULL_TREE;
7275 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7277 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7278 typefm = type;
7280 else
7282 /* We cannot build a type variant, as there's code that assumes
7283 that TYPE_MAIN_VARIANT has the same mode. This includes the
7284 debug generators. Instead, create a subrange type. This
7285 results in all of the enumeral values being emitted only once
7286 in the original, and the subtype gets them by reference. */
7287 if (TYPE_UNSIGNED (type))
7288 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7289 else
7290 typefm = make_signed_type (TYPE_PRECISION (typefm));
7291 TREE_TYPE (typefm) = type;
7294 else if (VECTOR_MODE_P (mode)
7295 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7296 : TREE_CODE (type) != TREE_CODE (typefm))
7298 error ("mode %qs applied to inappropriate type", p);
7299 return NULL_TREE;
7302 *node = typefm;
7305 return NULL_TREE;
7308 /* Handle a "section" attribute; arguments as in
7309 struct attribute_spec.handler. */
7311 static tree
7312 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7313 int ARG_UNUSED (flags), bool *no_add_attrs)
7315 tree decl = *node;
7317 if (targetm_common.have_named_sections)
7319 user_defined_section_attribute = true;
7321 if ((TREE_CODE (decl) == FUNCTION_DECL
7322 || TREE_CODE (decl) == VAR_DECL)
7323 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7325 if (TREE_CODE (decl) == VAR_DECL
7326 && current_function_decl != NULL_TREE
7327 && !TREE_STATIC (decl))
7329 error_at (DECL_SOURCE_LOCATION (decl),
7330 "section attribute cannot be specified for "
7331 "local variables");
7332 *no_add_attrs = true;
7335 /* The decl may have already been given a section attribute
7336 from a previous declaration. Ensure they match. */
7337 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7338 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7339 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7341 error ("section of %q+D conflicts with previous declaration",
7342 *node);
7343 *no_add_attrs = true;
7345 else if (TREE_CODE (decl) == VAR_DECL
7346 && !targetm.have_tls && targetm.emutls.tmpl_section
7347 && DECL_THREAD_LOCAL_P (decl))
7349 error ("section of %q+D cannot be overridden", *node);
7350 *no_add_attrs = true;
7352 else
7353 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7355 else
7357 error ("section attribute not allowed for %q+D", *node);
7358 *no_add_attrs = true;
7361 else
7363 error_at (DECL_SOURCE_LOCATION (*node),
7364 "section attributes are not supported for this target");
7365 *no_add_attrs = true;
7368 return NULL_TREE;
7371 /* Check whether ALIGN is a valid user-specified alignment. If so,
7372 return its base-2 log; if not, output an error and return -1. If
7373 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7374 no error. */
7376 check_user_alignment (const_tree align, bool allow_zero)
7378 int i;
7380 if (TREE_CODE (align) != INTEGER_CST
7381 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7383 error ("requested alignment is not an integer constant");
7384 return -1;
7386 else if (allow_zero && integer_zerop (align))
7387 return -1;
7388 else if (tree_int_cst_sgn (align) == -1
7389 || (i = tree_log2 (align)) == -1)
7391 error ("requested alignment is not a positive power of 2");
7392 return -1;
7394 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7396 error ("requested alignment is too large");
7397 return -1;
7399 return i;
7403 If in c++-11, check if the c++-11 alignment constraint with respect
7404 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7405 c++-11 mode, does nothing.
7407 [dcl.align]2/ says:
7409 [* if the constant expression evaluates to a fundamental alignment,
7410 the alignment requirement of the declared entity shall be the
7411 specified fundamental alignment.
7413 * if the constant expression evaluates to an extended alignment
7414 and the implementation supports that alignment in the context
7415 of the declaration, the alignment of the declared entity shall
7416 be that alignment
7418 * if the constant expression evaluates to an extended alignment
7419 and the implementation does not support that alignment in the
7420 context of the declaration, the program is ill-formed]. */
7422 static bool
7423 check_cxx_fundamental_alignment_constraints (tree node,
7424 unsigned align_log,
7425 int flags)
7427 bool alignment_too_large_p = false;
7428 unsigned requested_alignment = 1U << align_log;
7429 unsigned max_align = 0;
7431 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7432 || (node == NULL_TREE || node == error_mark_node))
7433 return true;
7435 if (cxx_fundamental_alignment_p (requested_alignment))
7436 return true;
7438 if (DECL_P (node))
7440 if (TREE_STATIC (node))
7442 /* For file scope variables and static members, the target
7443 supports alignments that are at most
7444 MAX_OFILE_ALIGNMENT. */
7445 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7446 alignment_too_large_p = true;
7448 else
7450 #ifdef BIGGEST_FIELD_ALIGNMENT
7451 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7452 #else
7453 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7454 #endif
7455 /* For non-static members, the target supports either
7456 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7457 if it is defined or BIGGEST_ALIGNMENT. */
7458 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7459 if (TREE_CODE (node) == FIELD_DECL
7460 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7461 alignment_too_large_p = true;
7462 #undef MAX_TARGET_FIELD_ALIGNMENT
7463 /* For stack variables, the target supports at most
7464 MAX_STACK_ALIGNMENT. */
7465 else if (decl_function_context (node) != NULL
7466 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7467 alignment_too_large_p = true;
7470 else if (TYPE_P (node))
7472 /* Let's be liberal for types. */
7473 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7474 alignment_too_large_p = true;
7477 if (alignment_too_large_p)
7478 pedwarn (input_location, OPT_Wattributes,
7479 "requested alignment %d is larger than %d",
7480 requested_alignment, max_align);
7482 return !alignment_too_large_p;
7485 /* Handle a "aligned" attribute; arguments as in
7486 struct attribute_spec.handler. */
7488 static tree
7489 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7490 int flags, bool *no_add_attrs)
7492 tree decl = NULL_TREE;
7493 tree *type = NULL;
7494 int is_type = 0;
7495 tree align_expr = (args ? TREE_VALUE (args)
7496 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7497 int i;
7499 if (DECL_P (*node))
7501 decl = *node;
7502 type = &TREE_TYPE (decl);
7503 is_type = TREE_CODE (*node) == TYPE_DECL;
7505 else if (TYPE_P (*node))
7506 type = node, is_type = 1;
7508 if ((i = check_user_alignment (align_expr, false)) == -1
7509 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7510 *no_add_attrs = true;
7511 else if (is_type)
7513 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7514 /* OK, modify the type in place. */;
7515 /* If we have a TYPE_DECL, then copy the type, so that we
7516 don't accidentally modify a builtin type. See pushdecl. */
7517 else if (decl && TREE_TYPE (decl) != error_mark_node
7518 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7520 tree tt = TREE_TYPE (decl);
7521 *type = build_variant_type_copy (*type);
7522 DECL_ORIGINAL_TYPE (decl) = tt;
7523 TYPE_NAME (*type) = decl;
7524 TREE_USED (*type) = TREE_USED (decl);
7525 TREE_TYPE (decl) = *type;
7527 else
7528 *type = build_variant_type_copy (*type);
7530 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7531 TYPE_USER_ALIGN (*type) = 1;
7533 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7534 && TREE_CODE (decl) != FIELD_DECL)
7536 error ("alignment may not be specified for %q+D", decl);
7537 *no_add_attrs = true;
7539 else if (DECL_USER_ALIGN (decl)
7540 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7541 /* C++-11 [dcl.align/4]:
7543 When multiple alignment-specifiers are specified for an
7544 entity, the alignment requirement shall be set to the
7545 strictest specified alignment.
7547 This formally comes from the c++11 specification but we are
7548 doing it for the GNU attribute syntax as well. */
7549 *no_add_attrs = true;
7550 else if (TREE_CODE (decl) == FUNCTION_DECL
7551 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7553 if (DECL_USER_ALIGN (decl))
7554 error ("alignment for %q+D was previously specified as %d "
7555 "and may not be decreased", decl,
7556 DECL_ALIGN (decl) / BITS_PER_UNIT);
7557 else
7558 error ("alignment for %q+D must be at least %d", decl,
7559 DECL_ALIGN (decl) / BITS_PER_UNIT);
7560 *no_add_attrs = true;
7562 else
7564 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7565 DECL_USER_ALIGN (decl) = 1;
7568 return NULL_TREE;
7571 /* Handle a "weak" attribute; arguments as in
7572 struct attribute_spec.handler. */
7574 static tree
7575 handle_weak_attribute (tree *node, tree name,
7576 tree ARG_UNUSED (args),
7577 int ARG_UNUSED (flags),
7578 bool * ARG_UNUSED (no_add_attrs))
7580 if (TREE_CODE (*node) == FUNCTION_DECL
7581 && DECL_DECLARED_INLINE_P (*node))
7583 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7584 *no_add_attrs = true;
7586 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7588 error ("indirect function %q+D cannot be declared weak", *node);
7589 *no_add_attrs = true;
7590 return NULL_TREE;
7592 else if (TREE_CODE (*node) == FUNCTION_DECL
7593 || TREE_CODE (*node) == VAR_DECL)
7594 declare_weak (*node);
7595 else
7596 warning (OPT_Wattributes, "%qE attribute ignored", name);
7598 return NULL_TREE;
7601 /* Handle an "alias" or "ifunc" attribute; arguments as in
7602 struct attribute_spec.handler, except that IS_ALIAS tells us
7603 whether this is an alias as opposed to ifunc attribute. */
7605 static tree
7606 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7607 bool *no_add_attrs)
7609 tree decl = *node;
7611 if (TREE_CODE (decl) != FUNCTION_DECL
7612 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7614 warning (OPT_Wattributes, "%qE attribute ignored", name);
7615 *no_add_attrs = true;
7617 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7618 || (TREE_CODE (decl) != FUNCTION_DECL
7619 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7620 /* A static variable declaration is always a tentative definition,
7621 but the alias is a non-tentative definition which overrides. */
7622 || (TREE_CODE (decl) != FUNCTION_DECL
7623 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7625 error ("%q+D defined both normally and as %qE attribute", decl, name);
7626 *no_add_attrs = true;
7627 return NULL_TREE;
7629 else if (!is_alias
7630 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7631 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7633 error ("weak %q+D cannot be defined %qE", decl, name);
7634 *no_add_attrs = true;
7635 return NULL_TREE;
7638 /* Note that the very first time we process a nested declaration,
7639 decl_function_context will not be set. Indeed, *would* never
7640 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7641 we do below. After such frobbery, pushdecl would set the context.
7642 In any case, this is never what we want. */
7643 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7645 tree id;
7647 id = TREE_VALUE (args);
7648 if (TREE_CODE (id) != STRING_CST)
7650 error ("attribute %qE argument not a string", name);
7651 *no_add_attrs = true;
7652 return NULL_TREE;
7654 id = get_identifier (TREE_STRING_POINTER (id));
7655 /* This counts as a use of the object pointed to. */
7656 TREE_USED (id) = 1;
7658 if (TREE_CODE (decl) == FUNCTION_DECL)
7659 DECL_INITIAL (decl) = error_mark_node;
7660 else
7661 TREE_STATIC (decl) = 1;
7663 if (!is_alias)
7664 /* ifuncs are also aliases, so set that attribute too. */
7665 DECL_ATTRIBUTES (decl)
7666 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7668 else
7670 warning (OPT_Wattributes, "%qE attribute ignored", name);
7671 *no_add_attrs = true;
7674 return NULL_TREE;
7677 /* Handle an "alias" or "ifunc" attribute; arguments as in
7678 struct attribute_spec.handler. */
7680 static tree
7681 handle_ifunc_attribute (tree *node, tree name, tree args,
7682 int ARG_UNUSED (flags), bool *no_add_attrs)
7684 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7687 /* Handle an "alias" or "ifunc" attribute; arguments as in
7688 struct attribute_spec.handler. */
7690 static tree
7691 handle_alias_attribute (tree *node, tree name, tree args,
7692 int ARG_UNUSED (flags), bool *no_add_attrs)
7694 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7697 /* Handle a "weakref" attribute; arguments as in struct
7698 attribute_spec.handler. */
7700 static tree
7701 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7702 int flags, bool *no_add_attrs)
7704 tree attr = NULL_TREE;
7706 /* We must ignore the attribute when it is associated with
7707 local-scoped decls, since attribute alias is ignored and many
7708 such symbols do not even have a DECL_WEAK field. */
7709 if (decl_function_context (*node)
7710 || current_function_decl
7711 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7713 warning (OPT_Wattributes, "%qE attribute ignored", name);
7714 *no_add_attrs = true;
7715 return NULL_TREE;
7718 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7720 error ("indirect function %q+D cannot be declared weakref", *node);
7721 *no_add_attrs = true;
7722 return NULL_TREE;
7725 /* The idea here is that `weakref("name")' mutates into `weakref,
7726 alias("name")', and weakref without arguments, in turn,
7727 implicitly adds weak. */
7729 if (args)
7731 attr = tree_cons (get_identifier ("alias"), args, attr);
7732 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7734 *no_add_attrs = true;
7736 decl_attributes (node, attr, flags);
7738 else
7740 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7741 error_at (DECL_SOURCE_LOCATION (*node),
7742 "weakref attribute must appear before alias attribute");
7744 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7745 and that isn't supported; and because it wants to add it to
7746 the list of weak decls, which isn't helpful. */
7747 DECL_WEAK (*node) = 1;
7750 return NULL_TREE;
7753 /* Handle an "visibility" attribute; arguments as in
7754 struct attribute_spec.handler. */
7756 static tree
7757 handle_visibility_attribute (tree *node, tree name, tree args,
7758 int ARG_UNUSED (flags),
7759 bool *ARG_UNUSED (no_add_attrs))
7761 tree decl = *node;
7762 tree id = TREE_VALUE (args);
7763 enum symbol_visibility vis;
7765 if (TYPE_P (*node))
7767 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7768 /* OK */;
7769 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7771 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7772 name);
7773 return NULL_TREE;
7775 else if (TYPE_FIELDS (*node))
7777 error ("%qE attribute ignored because %qT is already defined",
7778 name, *node);
7779 return NULL_TREE;
7782 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7784 warning (OPT_Wattributes, "%qE attribute ignored", name);
7785 return NULL_TREE;
7788 if (TREE_CODE (id) != STRING_CST)
7790 error ("visibility argument not a string");
7791 return NULL_TREE;
7794 /* If this is a type, set the visibility on the type decl. */
7795 if (TYPE_P (decl))
7797 decl = TYPE_NAME (decl);
7798 if (!decl)
7799 return NULL_TREE;
7800 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7802 warning (OPT_Wattributes, "%qE attribute ignored on types",
7803 name);
7804 return NULL_TREE;
7808 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7809 vis = VISIBILITY_DEFAULT;
7810 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7811 vis = VISIBILITY_INTERNAL;
7812 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7813 vis = VISIBILITY_HIDDEN;
7814 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7815 vis = VISIBILITY_PROTECTED;
7816 else
7818 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7819 vis = VISIBILITY_DEFAULT;
7822 if (DECL_VISIBILITY_SPECIFIED (decl)
7823 && vis != DECL_VISIBILITY (decl))
7825 tree attributes = (TYPE_P (*node)
7826 ? TYPE_ATTRIBUTES (*node)
7827 : DECL_ATTRIBUTES (decl));
7828 if (lookup_attribute ("visibility", attributes))
7829 error ("%qD redeclared with different visibility", decl);
7830 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7831 && lookup_attribute ("dllimport", attributes))
7832 error ("%qD was declared %qs which implies default visibility",
7833 decl, "dllimport");
7834 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7835 && lookup_attribute ("dllexport", attributes))
7836 error ("%qD was declared %qs which implies default visibility",
7837 decl, "dllexport");
7840 DECL_VISIBILITY (decl) = vis;
7841 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7843 /* Go ahead and attach the attribute to the node as well. This is needed
7844 so we can determine whether we have VISIBILITY_DEFAULT because the
7845 visibility was not specified, or because it was explicitly overridden
7846 from the containing scope. */
7848 return NULL_TREE;
7851 /* Determine the ELF symbol visibility for DECL, which is either a
7852 variable or a function. It is an error to use this function if a
7853 definition of DECL is not available in this translation unit.
7854 Returns true if the final visibility has been determined by this
7855 function; false if the caller is free to make additional
7856 modifications. */
7858 bool
7859 c_determine_visibility (tree decl)
7861 gcc_assert (TREE_CODE (decl) == VAR_DECL
7862 || TREE_CODE (decl) == FUNCTION_DECL);
7864 /* If the user explicitly specified the visibility with an
7865 attribute, honor that. DECL_VISIBILITY will have been set during
7866 the processing of the attribute. We check for an explicit
7867 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7868 to distinguish the use of an attribute from the use of a "#pragma
7869 GCC visibility push(...)"; in the latter case we still want other
7870 considerations to be able to overrule the #pragma. */
7871 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7872 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7873 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7874 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7875 return true;
7877 /* Set default visibility to whatever the user supplied with
7878 visibility_specified depending on #pragma GCC visibility. */
7879 if (!DECL_VISIBILITY_SPECIFIED (decl))
7881 if (visibility_options.inpragma
7882 || DECL_VISIBILITY (decl) != default_visibility)
7884 DECL_VISIBILITY (decl) = default_visibility;
7885 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7886 /* If visibility changed and DECL already has DECL_RTL, ensure
7887 symbol flags are updated. */
7888 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7889 || TREE_CODE (decl) == FUNCTION_DECL)
7890 && DECL_RTL_SET_P (decl))
7891 make_decl_rtl (decl);
7894 return false;
7897 /* Handle an "tls_model" attribute; arguments as in
7898 struct attribute_spec.handler. */
7900 static tree
7901 handle_tls_model_attribute (tree *node, tree name, tree args,
7902 int ARG_UNUSED (flags), bool *no_add_attrs)
7904 tree id;
7905 tree decl = *node;
7906 enum tls_model kind;
7908 *no_add_attrs = true;
7910 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7912 warning (OPT_Wattributes, "%qE attribute ignored", name);
7913 return NULL_TREE;
7916 kind = DECL_TLS_MODEL (decl);
7917 id = TREE_VALUE (args);
7918 if (TREE_CODE (id) != STRING_CST)
7920 error ("tls_model argument not a string");
7921 return NULL_TREE;
7924 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7925 kind = TLS_MODEL_LOCAL_EXEC;
7926 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7927 kind = TLS_MODEL_INITIAL_EXEC;
7928 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7929 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7930 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7931 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7932 else
7933 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7935 DECL_TLS_MODEL (decl) = kind;
7936 return NULL_TREE;
7939 /* Handle a "no_instrument_function" attribute; arguments as in
7940 struct attribute_spec.handler. */
7942 static tree
7943 handle_no_instrument_function_attribute (tree *node, tree name,
7944 tree ARG_UNUSED (args),
7945 int ARG_UNUSED (flags),
7946 bool *no_add_attrs)
7948 tree decl = *node;
7950 if (TREE_CODE (decl) != FUNCTION_DECL)
7952 error_at (DECL_SOURCE_LOCATION (decl),
7953 "%qE attribute applies only to functions", name);
7954 *no_add_attrs = true;
7956 else if (DECL_INITIAL (decl))
7958 error_at (DECL_SOURCE_LOCATION (decl),
7959 "can%'t set %qE attribute after definition", name);
7960 *no_add_attrs = true;
7962 else
7963 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7965 return NULL_TREE;
7968 /* Handle a "malloc" attribute; arguments as in
7969 struct attribute_spec.handler. */
7971 static tree
7972 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7973 int ARG_UNUSED (flags), bool *no_add_attrs)
7975 if (TREE_CODE (*node) == FUNCTION_DECL
7976 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7977 DECL_IS_MALLOC (*node) = 1;
7978 else
7980 warning (OPT_Wattributes, "%qE attribute ignored", name);
7981 *no_add_attrs = true;
7984 return NULL_TREE;
7987 /* Handle a "alloc_size" attribute; arguments as in
7988 struct attribute_spec.handler. */
7990 static tree
7991 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7992 int ARG_UNUSED (flags), bool *no_add_attrs)
7994 unsigned arg_count = type_num_arguments (*node);
7995 for (; args; args = TREE_CHAIN (args))
7997 tree position = TREE_VALUE (args);
7999 if (TREE_CODE (position) != INTEGER_CST
8000 || TREE_INT_CST_HIGH (position)
8001 || TREE_INT_CST_LOW (position) < 1
8002 || TREE_INT_CST_LOW (position) > arg_count )
8004 warning (OPT_Wattributes,
8005 "alloc_size parameter outside range");
8006 *no_add_attrs = true;
8007 return NULL_TREE;
8010 return NULL_TREE;
8013 /* Handle a "fn spec" attribute; arguments as in
8014 struct attribute_spec.handler. */
8016 static tree
8017 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8018 tree args, int ARG_UNUSED (flags),
8019 bool *no_add_attrs ATTRIBUTE_UNUSED)
8021 gcc_assert (args
8022 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8023 && !TREE_CHAIN (args));
8024 return NULL_TREE;
8027 /* Handle a "bnd_variable_size" attribute; arguments as in
8028 struct attribute_spec.handler. */
8030 static tree
8031 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8032 int ARG_UNUSED (flags), bool *no_add_attrs)
8034 if (TREE_CODE (*node) != FIELD_DECL)
8036 warning (OPT_Wattributes, "%qE attribute ignored", name);
8037 *no_add_attrs = true;
8040 return NULL_TREE;
8043 /* Handle a "bnd_legacy" attribute; arguments as in
8044 struct attribute_spec.handler. */
8046 static tree
8047 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8048 int ARG_UNUSED (flags), bool *no_add_attrs)
8050 if (TREE_CODE (*node) != FUNCTION_DECL)
8052 warning (OPT_Wattributes, "%qE attribute ignored", name);
8053 *no_add_attrs = true;
8056 return NULL_TREE;
8059 /* Handle a "warn_unused" attribute; arguments as in
8060 struct attribute_spec.handler. */
8062 static tree
8063 handle_warn_unused_attribute (tree *node, tree name,
8064 tree args ATTRIBUTE_UNUSED,
8065 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8067 if (TYPE_P (*node))
8068 /* Do nothing else, just set the attribute. We'll get at
8069 it later with lookup_attribute. */
8071 else
8073 warning (OPT_Wattributes, "%qE attribute ignored", name);
8074 *no_add_attrs = true;
8077 return NULL_TREE;
8080 /* Handle an "omp declare simd" attribute; arguments as in
8081 struct attribute_spec.handler. */
8083 static tree
8084 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8086 return NULL_TREE;
8089 /* Handle an "omp declare target" attribute; arguments as in
8090 struct attribute_spec.handler. */
8092 static tree
8093 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8095 return NULL_TREE;
8098 /* Handle a "returns_twice" attribute; arguments as in
8099 struct attribute_spec.handler. */
8101 static tree
8102 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8103 int ARG_UNUSED (flags), bool *no_add_attrs)
8105 if (TREE_CODE (*node) == FUNCTION_DECL)
8106 DECL_IS_RETURNS_TWICE (*node) = 1;
8107 else
8109 warning (OPT_Wattributes, "%qE attribute ignored", name);
8110 *no_add_attrs = true;
8113 return NULL_TREE;
8116 /* Handle a "no_limit_stack" attribute; arguments as in
8117 struct attribute_spec.handler. */
8119 static tree
8120 handle_no_limit_stack_attribute (tree *node, tree name,
8121 tree ARG_UNUSED (args),
8122 int ARG_UNUSED (flags),
8123 bool *no_add_attrs)
8125 tree decl = *node;
8127 if (TREE_CODE (decl) != FUNCTION_DECL)
8129 error_at (DECL_SOURCE_LOCATION (decl),
8130 "%qE attribute applies only to functions", name);
8131 *no_add_attrs = true;
8133 else if (DECL_INITIAL (decl))
8135 error_at (DECL_SOURCE_LOCATION (decl),
8136 "can%'t set %qE attribute after definition", name);
8137 *no_add_attrs = true;
8139 else
8140 DECL_NO_LIMIT_STACK (decl) = 1;
8142 return NULL_TREE;
8145 /* Handle a "pure" attribute; arguments as in
8146 struct attribute_spec.handler. */
8148 static tree
8149 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8150 int ARG_UNUSED (flags), bool *no_add_attrs)
8152 if (TREE_CODE (*node) == FUNCTION_DECL)
8153 DECL_PURE_P (*node) = 1;
8154 /* ??? TODO: Support types. */
8155 else
8157 warning (OPT_Wattributes, "%qE attribute ignored", name);
8158 *no_add_attrs = true;
8161 return NULL_TREE;
8164 /* Digest an attribute list destined for a transactional memory statement.
8165 ALLOWED is the set of attributes that are allowed for this statement;
8166 return the attribute we parsed. Multiple attributes are never allowed. */
8169 parse_tm_stmt_attr (tree attrs, int allowed)
8171 tree a_seen = NULL;
8172 int m_seen = 0;
8174 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8176 tree a = TREE_PURPOSE (attrs);
8177 int m = 0;
8179 if (is_attribute_p ("outer", a))
8180 m = TM_STMT_ATTR_OUTER;
8182 if ((m & allowed) == 0)
8184 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8185 continue;
8188 if (m_seen == 0)
8190 a_seen = a;
8191 m_seen = m;
8193 else if (m_seen == m)
8194 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8195 else
8196 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8199 return m_seen;
8202 /* Transform a TM attribute name into a maskable integer and back.
8203 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8204 to how the lack of an attribute is treated. */
8207 tm_attr_to_mask (tree attr)
8209 if (attr == NULL)
8210 return 0;
8211 if (is_attribute_p ("transaction_safe", attr))
8212 return TM_ATTR_SAFE;
8213 if (is_attribute_p ("transaction_callable", attr))
8214 return TM_ATTR_CALLABLE;
8215 if (is_attribute_p ("transaction_pure", attr))
8216 return TM_ATTR_PURE;
8217 if (is_attribute_p ("transaction_unsafe", attr))
8218 return TM_ATTR_IRREVOCABLE;
8219 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8220 return TM_ATTR_MAY_CANCEL_OUTER;
8221 return 0;
8224 tree
8225 tm_mask_to_attr (int mask)
8227 const char *str;
8228 switch (mask)
8230 case TM_ATTR_SAFE:
8231 str = "transaction_safe";
8232 break;
8233 case TM_ATTR_CALLABLE:
8234 str = "transaction_callable";
8235 break;
8236 case TM_ATTR_PURE:
8237 str = "transaction_pure";
8238 break;
8239 case TM_ATTR_IRREVOCABLE:
8240 str = "transaction_unsafe";
8241 break;
8242 case TM_ATTR_MAY_CANCEL_OUTER:
8243 str = "transaction_may_cancel_outer";
8244 break;
8245 default:
8246 gcc_unreachable ();
8248 return get_identifier (str);
8251 /* Return the first TM attribute seen in LIST. */
8253 tree
8254 find_tm_attribute (tree list)
8256 for (; list ; list = TREE_CHAIN (list))
8258 tree name = TREE_PURPOSE (list);
8259 if (tm_attr_to_mask (name) != 0)
8260 return name;
8262 return NULL_TREE;
8265 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8266 Here we accept only function types, and verify that none of the other
8267 function TM attributes are also applied. */
8268 /* ??? We need to accept class types for C++, but not C. This greatly
8269 complicates this function, since we can no longer rely on the extra
8270 processing given by function_type_required. */
8272 static tree
8273 handle_tm_attribute (tree *node, tree name, tree args,
8274 int flags, bool *no_add_attrs)
8276 /* Only one path adds the attribute; others don't. */
8277 *no_add_attrs = true;
8279 switch (TREE_CODE (*node))
8281 case RECORD_TYPE:
8282 case UNION_TYPE:
8283 /* Only tm_callable and tm_safe apply to classes. */
8284 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8285 goto ignored;
8286 /* FALLTHRU */
8288 case FUNCTION_TYPE:
8289 case METHOD_TYPE:
8291 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8292 if (old_name == name)
8294 else if (old_name != NULL_TREE)
8295 error ("type was previously declared %qE", old_name);
8296 else
8297 *no_add_attrs = false;
8299 break;
8301 case POINTER_TYPE:
8303 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8304 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8306 tree fn_tmp = TREE_TYPE (*node);
8307 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8308 *node = build_pointer_type (fn_tmp);
8309 break;
8312 /* FALLTHRU */
8314 default:
8315 /* If a function is next, pass it on to be tried next. */
8316 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8317 return tree_cons (name, args, NULL);
8319 ignored:
8320 warning (OPT_Wattributes, "%qE attribute ignored", name);
8321 break;
8324 return NULL_TREE;
8327 /* Handle the TM_WRAP attribute; arguments as in
8328 struct attribute_spec.handler. */
8330 static tree
8331 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8332 int ARG_UNUSED (flags), bool *no_add_attrs)
8334 tree decl = *node;
8336 /* We don't need the attribute even on success, since we
8337 record the entry in an external table. */
8338 *no_add_attrs = true;
8340 if (TREE_CODE (decl) != FUNCTION_DECL)
8341 warning (OPT_Wattributes, "%qE attribute ignored", name);
8342 else
8344 tree wrap_decl = TREE_VALUE (args);
8345 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8346 && TREE_CODE (wrap_decl) != VAR_DECL
8347 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8348 error ("%qE argument not an identifier", name);
8349 else
8351 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8352 wrap_decl = lookup_name (wrap_decl);
8353 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8355 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8356 TREE_TYPE (wrap_decl)))
8357 record_tm_replacement (wrap_decl, decl);
8358 else
8359 error ("%qD is not compatible with %qD", wrap_decl, decl);
8361 else
8362 error ("transaction_wrap argument is not a function");
8366 return NULL_TREE;
8369 /* Ignore the given attribute. Used when this attribute may be usefully
8370 overridden by the target, but is not used generically. */
8372 static tree
8373 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8374 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8375 bool *no_add_attrs)
8377 *no_add_attrs = true;
8378 return NULL_TREE;
8381 /* Handle a "no vops" attribute; arguments as in
8382 struct attribute_spec.handler. */
8384 static tree
8385 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8386 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8387 bool *ARG_UNUSED (no_add_attrs))
8389 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8390 DECL_IS_NOVOPS (*node) = 1;
8391 return NULL_TREE;
8394 /* Handle a "deprecated" attribute; arguments as in
8395 struct attribute_spec.handler. */
8397 static tree
8398 handle_deprecated_attribute (tree *node, tree name,
8399 tree args, int flags,
8400 bool *no_add_attrs)
8402 tree type = NULL_TREE;
8403 int warn = 0;
8404 tree what = NULL_TREE;
8406 if (!args)
8407 *no_add_attrs = true;
8408 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8410 error ("deprecated message is not a string");
8411 *no_add_attrs = true;
8414 if (DECL_P (*node))
8416 tree decl = *node;
8417 type = TREE_TYPE (decl);
8419 if (TREE_CODE (decl) == TYPE_DECL
8420 || TREE_CODE (decl) == PARM_DECL
8421 || TREE_CODE (decl) == VAR_DECL
8422 || TREE_CODE (decl) == FUNCTION_DECL
8423 || TREE_CODE (decl) == FIELD_DECL
8424 || objc_method_decl (TREE_CODE (decl)))
8425 TREE_DEPRECATED (decl) = 1;
8426 else
8427 warn = 1;
8429 else if (TYPE_P (*node))
8431 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8432 *node = build_variant_type_copy (*node);
8433 TREE_DEPRECATED (*node) = 1;
8434 type = *node;
8436 else
8437 warn = 1;
8439 if (warn)
8441 *no_add_attrs = true;
8442 if (type && TYPE_NAME (type))
8444 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8445 what = TYPE_NAME (*node);
8446 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8447 && DECL_NAME (TYPE_NAME (type)))
8448 what = DECL_NAME (TYPE_NAME (type));
8450 if (what)
8451 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8452 else
8453 warning (OPT_Wattributes, "%qE attribute ignored", name);
8456 return NULL_TREE;
8459 /* Handle a "vector_size" attribute; arguments as in
8460 struct attribute_spec.handler. */
8462 static tree
8463 handle_vector_size_attribute (tree *node, tree name, tree args,
8464 int ARG_UNUSED (flags),
8465 bool *no_add_attrs)
8467 unsigned HOST_WIDE_INT vecsize, nunits;
8468 enum machine_mode orig_mode;
8469 tree type = *node, new_type, size;
8471 *no_add_attrs = true;
8473 size = TREE_VALUE (args);
8475 if (!host_integerp (size, 1))
8477 warning (OPT_Wattributes, "%qE attribute ignored", name);
8478 return NULL_TREE;
8481 /* Get the vector size (in bytes). */
8482 vecsize = tree_low_cst (size, 1);
8484 /* We need to provide for vector pointers, vector arrays, and
8485 functions returning vectors. For example:
8487 __attribute__((vector_size(16))) short *foo;
8489 In this case, the mode is SI, but the type being modified is
8490 HI, so we need to look further. */
8492 while (POINTER_TYPE_P (type)
8493 || TREE_CODE (type) == FUNCTION_TYPE
8494 || TREE_CODE (type) == METHOD_TYPE
8495 || TREE_CODE (type) == ARRAY_TYPE
8496 || TREE_CODE (type) == OFFSET_TYPE)
8497 type = TREE_TYPE (type);
8499 /* Get the mode of the type being modified. */
8500 orig_mode = TYPE_MODE (type);
8502 if ((!INTEGRAL_TYPE_P (type)
8503 && !SCALAR_FLOAT_TYPE_P (type)
8504 && !FIXED_POINT_TYPE_P (type))
8505 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8506 && GET_MODE_CLASS (orig_mode) != MODE_INT
8507 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8508 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8509 || TREE_CODE (type) == BOOLEAN_TYPE)
8511 error ("invalid vector type for attribute %qE", name);
8512 return NULL_TREE;
8515 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8517 error ("vector size not an integral multiple of component size");
8518 return NULL;
8521 if (vecsize == 0)
8523 error ("zero vector size");
8524 return NULL;
8527 /* Calculate how many units fit in the vector. */
8528 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8529 if (nunits & (nunits - 1))
8531 error ("number of components of the vector not a power of two");
8532 return NULL_TREE;
8535 new_type = build_vector_type (type, nunits);
8537 /* Build back pointers if needed. */
8538 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8540 return NULL_TREE;
8543 /* Handle the "nonnull" attribute. */
8544 static tree
8545 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8546 tree args, int ARG_UNUSED (flags),
8547 bool *no_add_attrs)
8549 tree type = *node;
8550 unsigned HOST_WIDE_INT attr_arg_num;
8552 /* If no arguments are specified, all pointer arguments should be
8553 non-null. Verify a full prototype is given so that the arguments
8554 will have the correct types when we actually check them later. */
8555 if (!args)
8557 if (!prototype_p (type))
8559 error ("nonnull attribute without arguments on a non-prototype");
8560 *no_add_attrs = true;
8562 return NULL_TREE;
8565 /* Argument list specified. Verify that each argument number references
8566 a pointer argument. */
8567 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8569 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8571 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8573 error ("nonnull argument has invalid operand number (argument %lu)",
8574 (unsigned long) attr_arg_num);
8575 *no_add_attrs = true;
8576 return NULL_TREE;
8579 if (prototype_p (type))
8581 function_args_iterator iter;
8582 tree argument;
8584 function_args_iter_init (&iter, type);
8585 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8587 argument = function_args_iter_cond (&iter);
8588 if (argument == NULL_TREE || ck_num == arg_num)
8589 break;
8592 if (!argument
8593 || TREE_CODE (argument) == VOID_TYPE)
8595 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8596 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8597 *no_add_attrs = true;
8598 return NULL_TREE;
8601 if (TREE_CODE (argument) != POINTER_TYPE)
8603 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8604 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8605 *no_add_attrs = true;
8606 return NULL_TREE;
8611 return NULL_TREE;
8614 /* Check the argument list of a function call for null in argument slots
8615 that are marked as requiring a non-null pointer argument. The NARGS
8616 arguments are passed in the array ARGARRAY.
8619 static void
8620 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8622 tree a;
8623 int i;
8625 attrs = lookup_attribute ("nonnull", attrs);
8626 if (attrs == NULL_TREE)
8627 return;
8629 a = attrs;
8630 /* See if any of the nonnull attributes has no arguments. If so,
8631 then every pointer argument is checked (in which case the check
8632 for pointer type is done in check_nonnull_arg). */
8633 if (TREE_VALUE (a) != NULL_TREE)
8635 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8636 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8638 if (a != NULL_TREE)
8639 for (i = 0; i < nargs; i++)
8640 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8641 i + 1);
8642 else
8644 /* Walk the argument list. If we encounter an argument number we
8645 should check for non-null, do it. */
8646 for (i = 0; i < nargs; i++)
8648 for (a = attrs; ; a = TREE_CHAIN (a))
8650 a = lookup_attribute ("nonnull", a);
8651 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8652 break;
8655 if (a != NULL_TREE)
8656 check_function_arguments_recurse (check_nonnull_arg, NULL,
8657 argarray[i], i + 1);
8662 /* Check that the Nth argument of a function call (counting backwards
8663 from the end) is a (pointer)0. The NARGS arguments are passed in the
8664 array ARGARRAY. */
8666 static void
8667 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8669 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8671 if (attr)
8673 int len = 0;
8674 int pos = 0;
8675 tree sentinel;
8676 function_args_iterator iter;
8677 tree t;
8679 /* Skip over the named arguments. */
8680 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8682 if (len == nargs)
8683 break;
8684 len++;
8687 if (TREE_VALUE (attr))
8689 tree p = TREE_VALUE (TREE_VALUE (attr));
8690 pos = TREE_INT_CST_LOW (p);
8693 /* The sentinel must be one of the varargs, i.e.
8694 in position >= the number of fixed arguments. */
8695 if ((nargs - 1 - pos) < len)
8697 warning (OPT_Wformat_,
8698 "not enough variable arguments to fit a sentinel");
8699 return;
8702 /* Validate the sentinel. */
8703 sentinel = argarray[nargs - 1 - pos];
8704 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8705 || !integer_zerop (sentinel))
8706 /* Although __null (in C++) is only an integer we allow it
8707 nevertheless, as we are guaranteed that it's exactly
8708 as wide as a pointer, and we don't want to force
8709 users to cast the NULL they have written there.
8710 We warn with -Wstrict-null-sentinel, though. */
8711 && (warn_strict_null_sentinel || null_node != sentinel))
8712 warning (OPT_Wformat_, "missing sentinel in function call");
8716 /* Helper for check_function_nonnull; given a list of operands which
8717 must be non-null in ARGS, determine if operand PARAM_NUM should be
8718 checked. */
8720 static bool
8721 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8723 unsigned HOST_WIDE_INT arg_num = 0;
8725 for (; args; args = TREE_CHAIN (args))
8727 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8729 gcc_assert (found);
8731 if (arg_num == param_num)
8732 return true;
8734 return false;
8737 /* Check that the function argument PARAM (which is operand number
8738 PARAM_NUM) is non-null. This is called by check_function_nonnull
8739 via check_function_arguments_recurse. */
8741 static void
8742 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8743 unsigned HOST_WIDE_INT param_num)
8745 /* Just skip checking the argument if it's not a pointer. This can
8746 happen if the "nonnull" attribute was given without an operand
8747 list (which means to check every pointer argument). */
8749 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8750 return;
8752 if (integer_zerop (param))
8753 warning (OPT_Wnonnull, "null argument where non-null required "
8754 "(argument %lu)", (unsigned long) param_num);
8757 /* Helper for nonnull attribute handling; fetch the operand number
8758 from the attribute argument list. */
8760 static bool
8761 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8763 /* Verify the arg number is a constant. */
8764 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8765 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8766 return false;
8768 *valp = TREE_INT_CST_LOW (arg_num_expr);
8769 return true;
8772 /* Handle a "nothrow" attribute; arguments as in
8773 struct attribute_spec.handler. */
8775 static tree
8776 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8777 int ARG_UNUSED (flags), bool *no_add_attrs)
8779 if (TREE_CODE (*node) == FUNCTION_DECL)
8780 TREE_NOTHROW (*node) = 1;
8781 /* ??? TODO: Support types. */
8782 else
8784 warning (OPT_Wattributes, "%qE attribute ignored", name);
8785 *no_add_attrs = true;
8788 return NULL_TREE;
8791 /* Handle a "cleanup" attribute; arguments as in
8792 struct attribute_spec.handler. */
8794 static tree
8795 handle_cleanup_attribute (tree *node, tree name, tree args,
8796 int ARG_UNUSED (flags), bool *no_add_attrs)
8798 tree decl = *node;
8799 tree cleanup_id, cleanup_decl;
8801 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8802 for global destructors in C++. This requires infrastructure that
8803 we don't have generically at the moment. It's also not a feature
8804 we'd be missing too much, since we do have attribute constructor. */
8805 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8807 warning (OPT_Wattributes, "%qE attribute ignored", name);
8808 *no_add_attrs = true;
8809 return NULL_TREE;
8812 /* Verify that the argument is a function in scope. */
8813 /* ??? We could support pointers to functions here as well, if
8814 that was considered desirable. */
8815 cleanup_id = TREE_VALUE (args);
8816 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8818 error ("cleanup argument not an identifier");
8819 *no_add_attrs = true;
8820 return NULL_TREE;
8822 cleanup_decl = lookup_name (cleanup_id);
8823 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8825 error ("cleanup argument not a function");
8826 *no_add_attrs = true;
8827 return NULL_TREE;
8830 /* That the function has proper type is checked with the
8831 eventual call to build_function_call. */
8833 return NULL_TREE;
8836 /* Handle a "warn_unused_result" attribute. No special handling. */
8838 static tree
8839 handle_warn_unused_result_attribute (tree *node, tree name,
8840 tree ARG_UNUSED (args),
8841 int ARG_UNUSED (flags), bool *no_add_attrs)
8843 /* Ignore the attribute for functions not returning any value. */
8844 if (VOID_TYPE_P (TREE_TYPE (*node)))
8846 warning (OPT_Wattributes, "%qE attribute ignored", name);
8847 *no_add_attrs = true;
8850 return NULL_TREE;
8853 /* Handle a "sentinel" attribute. */
8855 static tree
8856 handle_sentinel_attribute (tree *node, tree name, tree args,
8857 int ARG_UNUSED (flags), bool *no_add_attrs)
8859 if (!prototype_p (*node))
8861 warning (OPT_Wattributes,
8862 "%qE attribute requires prototypes with named arguments", name);
8863 *no_add_attrs = true;
8865 else
8867 if (!stdarg_p (*node))
8869 warning (OPT_Wattributes,
8870 "%qE attribute only applies to variadic functions", name);
8871 *no_add_attrs = true;
8875 if (args)
8877 tree position = TREE_VALUE (args);
8879 if (TREE_CODE (position) != INTEGER_CST)
8881 warning (OPT_Wattributes,
8882 "requested position is not an integer constant");
8883 *no_add_attrs = true;
8885 else
8887 if (tree_int_cst_lt (position, integer_zero_node))
8889 warning (OPT_Wattributes,
8890 "requested position is less than zero");
8891 *no_add_attrs = true;
8896 return NULL_TREE;
8899 /* Handle a "type_generic" attribute. */
8901 static tree
8902 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8903 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8904 bool * ARG_UNUSED (no_add_attrs))
8906 /* Ensure we have a function type. */
8907 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8909 /* Ensure we have a variadic function. */
8910 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8912 return NULL_TREE;
8915 /* Handle a "target" attribute. */
8917 static tree
8918 handle_target_attribute (tree *node, tree name, tree args, int flags,
8919 bool *no_add_attrs)
8921 /* Ensure we have a function type. */
8922 if (TREE_CODE (*node) != FUNCTION_DECL)
8924 warning (OPT_Wattributes, "%qE attribute ignored", name);
8925 *no_add_attrs = true;
8927 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8928 flags))
8929 *no_add_attrs = true;
8931 return NULL_TREE;
8934 /* Arguments being collected for optimization. */
8935 typedef const char *const_char_p; /* For DEF_VEC_P. */
8936 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
8939 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8940 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8941 false for #pragma GCC optimize. */
8943 bool
8944 parse_optimize_options (tree args, bool attr_p)
8946 bool ret = true;
8947 unsigned opt_argc;
8948 unsigned i;
8949 int saved_flag_strict_aliasing;
8950 const char **opt_argv;
8951 struct cl_decoded_option *decoded_options;
8952 unsigned int decoded_options_count;
8953 tree ap;
8955 /* Build up argv vector. Just in case the string is stored away, use garbage
8956 collected strings. */
8957 vec_safe_truncate (optimize_args, 0);
8958 vec_safe_push (optimize_args, (const char *) NULL);
8960 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8962 tree value = TREE_VALUE (ap);
8964 if (TREE_CODE (value) == INTEGER_CST)
8966 char buffer[20];
8967 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8968 vec_safe_push (optimize_args, ggc_strdup (buffer));
8971 else if (TREE_CODE (value) == STRING_CST)
8973 /* Split string into multiple substrings. */
8974 size_t len = TREE_STRING_LENGTH (value);
8975 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8976 char *end = p + len;
8977 char *comma;
8978 char *next_p = p;
8980 while (next_p != NULL)
8982 size_t len2;
8983 char *q, *r;
8985 p = next_p;
8986 comma = strchr (p, ',');
8987 if (comma)
8989 len2 = comma - p;
8990 *comma = '\0';
8991 next_p = comma+1;
8993 else
8995 len2 = end - p;
8996 next_p = NULL;
8999 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9001 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9002 options. */
9003 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9005 ret = false;
9006 if (attr_p)
9007 warning (OPT_Wattributes,
9008 "bad option %s to optimize attribute", p);
9009 else
9010 warning (OPT_Wpragmas,
9011 "bad option %s to pragma attribute", p);
9012 continue;
9015 if (*p != '-')
9017 *r++ = '-';
9019 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9020 itself is -Os, and any other switch begins with a -f. */
9021 if ((*p >= '0' && *p <= '9')
9022 || (p[0] == 's' && p[1] == '\0'))
9023 *r++ = 'O';
9024 else if (*p != 'O')
9025 *r++ = 'f';
9028 memcpy (r, p, len2);
9029 r[len2] = '\0';
9030 vec_safe_push (optimize_args, (const char *) q);
9036 opt_argc = optimize_args->length ();
9037 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9039 for (i = 1; i < opt_argc; i++)
9040 opt_argv[i] = (*optimize_args)[i];
9042 saved_flag_strict_aliasing = flag_strict_aliasing;
9044 /* Now parse the options. */
9045 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9046 &decoded_options,
9047 &decoded_options_count);
9048 decode_options (&global_options, &global_options_set,
9049 decoded_options, decoded_options_count,
9050 input_location, global_dc);
9052 targetm.override_options_after_change();
9054 /* Don't allow changing -fstrict-aliasing. */
9055 flag_strict_aliasing = saved_flag_strict_aliasing;
9057 optimize_args->truncate (0);
9058 return ret;
9061 /* For handling "optimize" attribute. arguments as in
9062 struct attribute_spec.handler. */
9064 static tree
9065 handle_optimize_attribute (tree *node, tree name, tree args,
9066 int ARG_UNUSED (flags), bool *no_add_attrs)
9068 /* Ensure we have a function type. */
9069 if (TREE_CODE (*node) != FUNCTION_DECL)
9071 warning (OPT_Wattributes, "%qE attribute ignored", name);
9072 *no_add_attrs = true;
9074 else
9076 struct cl_optimization cur_opts;
9077 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9079 /* Save current options. */
9080 cl_optimization_save (&cur_opts, &global_options);
9082 /* If we previously had some optimization options, use them as the
9083 default. */
9084 if (old_opts)
9085 cl_optimization_restore (&global_options,
9086 TREE_OPTIMIZATION (old_opts));
9088 /* Parse options, and update the vector. */
9089 parse_optimize_options (args, true);
9090 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9091 = build_optimization_node (&global_options);
9093 /* Restore current options. */
9094 cl_optimization_restore (&global_options, &cur_opts);
9097 return NULL_TREE;
9100 /* Handle a "no_split_stack" attribute. */
9102 static tree
9103 handle_no_split_stack_attribute (tree *node, tree name,
9104 tree ARG_UNUSED (args),
9105 int ARG_UNUSED (flags),
9106 bool *no_add_attrs)
9108 tree decl = *node;
9110 if (TREE_CODE (decl) != FUNCTION_DECL)
9112 error_at (DECL_SOURCE_LOCATION (decl),
9113 "%qE attribute applies only to functions", name);
9114 *no_add_attrs = true;
9116 else if (DECL_INITIAL (decl))
9118 error_at (DECL_SOURCE_LOCATION (decl),
9119 "can%'t set %qE attribute after definition", name);
9120 *no_add_attrs = true;
9123 return NULL_TREE;
9126 /* Handle a "returns_nonnull" attribute; arguments as in
9127 struct attribute_spec.handler. */
9129 static tree
9130 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9131 bool *no_add_attrs)
9133 // Even without a prototype we still have a return type we can check.
9134 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9136 error ("returns_nonnull attribute on a function not returning a pointer");
9137 *no_add_attrs = true;
9139 return NULL_TREE;
9143 /* Check for valid arguments being passed to a function with FNTYPE.
9144 There are NARGS arguments in the array ARGARRAY. */
9145 void
9146 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9148 /* Check for null being passed in a pointer argument that must be
9149 non-null. We also need to do this if format checking is enabled. */
9151 if (warn_nonnull)
9152 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9154 /* Check for errors in format strings. */
9156 if (warn_format || warn_suggest_attribute_format)
9157 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9159 if (warn_format)
9160 check_function_sentinel (fntype, nargs, argarray);
9163 /* Generic argument checking recursion routine. PARAM is the argument to
9164 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9165 once the argument is resolved. CTX is context for the callback. */
9166 void
9167 check_function_arguments_recurse (void (*callback)
9168 (void *, tree, unsigned HOST_WIDE_INT),
9169 void *ctx, tree param,
9170 unsigned HOST_WIDE_INT param_num)
9172 if (CONVERT_EXPR_P (param)
9173 && (TYPE_PRECISION (TREE_TYPE (param))
9174 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9176 /* Strip coercion. */
9177 check_function_arguments_recurse (callback, ctx,
9178 TREE_OPERAND (param, 0), param_num);
9179 return;
9182 if (TREE_CODE (param) == CALL_EXPR)
9184 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9185 tree attrs;
9186 bool found_format_arg = false;
9188 /* See if this is a call to a known internationalization function
9189 that modifies a format arg. Such a function may have multiple
9190 format_arg attributes (for example, ngettext). */
9192 for (attrs = TYPE_ATTRIBUTES (type);
9193 attrs;
9194 attrs = TREE_CHAIN (attrs))
9195 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9197 tree inner_arg;
9198 tree format_num_expr;
9199 int format_num;
9200 int i;
9201 call_expr_arg_iterator iter;
9203 /* Extract the argument number, which was previously checked
9204 to be valid. */
9205 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9207 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9208 && !TREE_INT_CST_HIGH (format_num_expr));
9210 format_num = TREE_INT_CST_LOW (format_num_expr);
9212 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9213 inner_arg != 0;
9214 inner_arg = next_call_expr_arg (&iter), i++)
9215 if (i == format_num)
9217 check_function_arguments_recurse (callback, ctx,
9218 inner_arg, param_num);
9219 found_format_arg = true;
9220 break;
9224 /* If we found a format_arg attribute and did a recursive check,
9225 we are done with checking this argument. Otherwise, we continue
9226 and this will be considered a non-literal. */
9227 if (found_format_arg)
9228 return;
9231 if (TREE_CODE (param) == COND_EXPR)
9233 /* Check both halves of the conditional expression. */
9234 check_function_arguments_recurse (callback, ctx,
9235 TREE_OPERAND (param, 1), param_num);
9236 check_function_arguments_recurse (callback, ctx,
9237 TREE_OPERAND (param, 2), param_num);
9238 return;
9241 (*callback) (ctx, param, param_num);
9244 /* Checks for a builtin function FNDECL that the number of arguments
9245 NARGS against the required number REQUIRED and issues an error if
9246 there is a mismatch. Returns true if the number of arguments is
9247 correct, otherwise false. */
9249 static bool
9250 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9252 if (nargs < required)
9254 error_at (input_location,
9255 "not enough arguments to function %qE", fndecl);
9256 return false;
9258 else if (nargs > required)
9260 error_at (input_location,
9261 "too many arguments to function %qE", fndecl);
9262 return false;
9264 return true;
9267 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9268 Returns false if there was an error, otherwise true. */
9270 bool
9271 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9273 if (!DECL_BUILT_IN (fndecl)
9274 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9275 return true;
9277 switch (DECL_FUNCTION_CODE (fndecl))
9279 case BUILT_IN_CONSTANT_P:
9280 return builtin_function_validate_nargs (fndecl, nargs, 1);
9282 case BUILT_IN_ISFINITE:
9283 case BUILT_IN_ISINF:
9284 case BUILT_IN_ISINF_SIGN:
9285 case BUILT_IN_ISNAN:
9286 case BUILT_IN_ISNORMAL:
9287 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9289 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9291 error ("non-floating-point argument in call to "
9292 "function %qE", fndecl);
9293 return false;
9295 return true;
9297 return false;
9299 case BUILT_IN_ISGREATER:
9300 case BUILT_IN_ISGREATEREQUAL:
9301 case BUILT_IN_ISLESS:
9302 case BUILT_IN_ISLESSEQUAL:
9303 case BUILT_IN_ISLESSGREATER:
9304 case BUILT_IN_ISUNORDERED:
9305 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9307 enum tree_code code0, code1;
9308 code0 = TREE_CODE (TREE_TYPE (args[0]));
9309 code1 = TREE_CODE (TREE_TYPE (args[1]));
9310 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9311 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9312 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9314 error ("non-floating-point arguments in call to "
9315 "function %qE", fndecl);
9316 return false;
9318 return true;
9320 return false;
9322 case BUILT_IN_FPCLASSIFY:
9323 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9325 unsigned i;
9327 for (i=0; i<5; i++)
9328 if (TREE_CODE (args[i]) != INTEGER_CST)
9330 error ("non-const integer argument %u in call to function %qE",
9331 i+1, fndecl);
9332 return false;
9335 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9337 error ("non-floating-point argument in call to function %qE",
9338 fndecl);
9339 return false;
9341 return true;
9343 return false;
9345 case BUILT_IN_ASSUME_ALIGNED:
9346 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9348 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9350 error ("non-integer argument 3 in call to function %qE", fndecl);
9351 return false;
9353 return true;
9355 return false;
9357 default:
9358 return true;
9362 /* Function to help qsort sort FIELD_DECLs by name order. */
9365 field_decl_cmp (const void *x_p, const void *y_p)
9367 const tree *const x = (const tree *const) x_p;
9368 const tree *const y = (const tree *const) y_p;
9370 if (DECL_NAME (*x) == DECL_NAME (*y))
9371 /* A nontype is "greater" than a type. */
9372 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9373 if (DECL_NAME (*x) == NULL_TREE)
9374 return -1;
9375 if (DECL_NAME (*y) == NULL_TREE)
9376 return 1;
9377 if (DECL_NAME (*x) < DECL_NAME (*y))
9378 return -1;
9379 return 1;
9382 static struct {
9383 gt_pointer_operator new_value;
9384 void *cookie;
9385 } resort_data;
9387 /* This routine compares two fields like field_decl_cmp but using the
9388 pointer operator in resort_data. */
9390 static int
9391 resort_field_decl_cmp (const void *x_p, const void *y_p)
9393 const tree *const x = (const tree *const) x_p;
9394 const tree *const y = (const tree *const) y_p;
9396 if (DECL_NAME (*x) == DECL_NAME (*y))
9397 /* A nontype is "greater" than a type. */
9398 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9399 if (DECL_NAME (*x) == NULL_TREE)
9400 return -1;
9401 if (DECL_NAME (*y) == NULL_TREE)
9402 return 1;
9404 tree d1 = DECL_NAME (*x);
9405 tree d2 = DECL_NAME (*y);
9406 resort_data.new_value (&d1, resort_data.cookie);
9407 resort_data.new_value (&d2, resort_data.cookie);
9408 if (d1 < d2)
9409 return -1;
9411 return 1;
9414 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9416 void
9417 resort_sorted_fields (void *obj,
9418 void * ARG_UNUSED (orig_obj),
9419 gt_pointer_operator new_value,
9420 void *cookie)
9422 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9423 resort_data.new_value = new_value;
9424 resort_data.cookie = cookie;
9425 qsort (&sf->elts[0], sf->len, sizeof (tree),
9426 resort_field_decl_cmp);
9429 /* Subroutine of c_parse_error.
9430 Return the result of concatenating LHS and RHS. RHS is really
9431 a string literal, its first character is indicated by RHS_START and
9432 RHS_SIZE is its length (including the terminating NUL character).
9434 The caller is responsible for deleting the returned pointer. */
9436 static char *
9437 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9439 const int lhs_size = strlen (lhs);
9440 char *result = XNEWVEC (char, lhs_size + rhs_size);
9441 strncpy (result, lhs, lhs_size);
9442 strncpy (result + lhs_size, rhs_start, rhs_size);
9443 return result;
9446 /* Issue the error given by GMSGID, indicating that it occurred before
9447 TOKEN, which had the associated VALUE. */
9449 void
9450 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9451 tree value, unsigned char token_flags)
9453 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9455 char *message = NULL;
9457 if (token_type == CPP_EOF)
9458 message = catenate_messages (gmsgid, " at end of input");
9459 else if (token_type == CPP_CHAR
9460 || token_type == CPP_WCHAR
9461 || token_type == CPP_CHAR16
9462 || token_type == CPP_CHAR32)
9464 unsigned int val = TREE_INT_CST_LOW (value);
9465 const char *prefix;
9467 switch (token_type)
9469 default:
9470 prefix = "";
9471 break;
9472 case CPP_WCHAR:
9473 prefix = "L";
9474 break;
9475 case CPP_CHAR16:
9476 prefix = "u";
9477 break;
9478 case CPP_CHAR32:
9479 prefix = "U";
9480 break;
9483 if (val <= UCHAR_MAX && ISGRAPH (val))
9484 message = catenate_messages (gmsgid, " before %s'%c'");
9485 else
9486 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9488 error (message, prefix, val);
9489 free (message);
9490 message = NULL;
9492 else if (token_type == CPP_CHAR_USERDEF
9493 || token_type == CPP_WCHAR_USERDEF
9494 || token_type == CPP_CHAR16_USERDEF
9495 || token_type == CPP_CHAR32_USERDEF)
9496 message = catenate_messages (gmsgid,
9497 " before user-defined character literal");
9498 else if (token_type == CPP_STRING_USERDEF
9499 || token_type == CPP_WSTRING_USERDEF
9500 || token_type == CPP_STRING16_USERDEF
9501 || token_type == CPP_STRING32_USERDEF
9502 || token_type == CPP_UTF8STRING_USERDEF)
9503 message = catenate_messages (gmsgid, " before user-defined string literal");
9504 else if (token_type == CPP_STRING
9505 || token_type == CPP_WSTRING
9506 || token_type == CPP_STRING16
9507 || token_type == CPP_STRING32
9508 || token_type == CPP_UTF8STRING)
9509 message = catenate_messages (gmsgid, " before string constant");
9510 else if (token_type == CPP_NUMBER)
9511 message = catenate_messages (gmsgid, " before numeric constant");
9512 else if (token_type == CPP_NAME)
9514 message = catenate_messages (gmsgid, " before %qE");
9515 error (message, value);
9516 free (message);
9517 message = NULL;
9519 else if (token_type == CPP_PRAGMA)
9520 message = catenate_messages (gmsgid, " before %<#pragma%>");
9521 else if (token_type == CPP_PRAGMA_EOL)
9522 message = catenate_messages (gmsgid, " before end of line");
9523 else if (token_type == CPP_DECLTYPE)
9524 message = catenate_messages (gmsgid, " before %<decltype%>");
9525 else if (token_type < N_TTYPES)
9527 message = catenate_messages (gmsgid, " before %qs token");
9528 error (message, cpp_type2name (token_type, token_flags));
9529 free (message);
9530 message = NULL;
9532 else
9533 error (gmsgid);
9535 if (message)
9537 error (message);
9538 free (message);
9540 #undef catenate_messages
9543 /* Mapping for cpp message reasons to the options that enable them. */
9545 struct reason_option_codes_t
9547 const int reason; /* cpplib message reason. */
9548 const int option_code; /* gcc option that controls this message. */
9551 static const struct reason_option_codes_t option_codes[] = {
9552 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9553 {CPP_W_COMMENTS, OPT_Wcomment},
9554 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9555 {CPP_W_MULTICHAR, OPT_Wmultichar},
9556 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9557 {CPP_W_LONG_LONG, OPT_Wlong_long},
9558 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9559 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9560 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9561 {CPP_W_UNDEF, OPT_Wundef},
9562 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9563 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9564 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9565 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9566 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9567 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9568 {CPP_W_NONE, 0}
9571 /* Return the gcc option code associated with the reason for a cpp
9572 message, or 0 if none. */
9574 static int
9575 c_option_controlling_cpp_error (int reason)
9577 const struct reason_option_codes_t *entry;
9579 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9581 if (entry->reason == reason)
9582 return entry->option_code;
9584 return 0;
9587 /* Callback from cpp_error for PFILE to print diagnostics from the
9588 preprocessor. The diagnostic is of type LEVEL, with REASON set
9589 to the reason code if LEVEL is represents a warning, at location
9590 LOCATION unless this is after lexing and the compiler's location
9591 should be used instead, with column number possibly overridden by
9592 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9593 the arguments. Returns true if a diagnostic was emitted, false
9594 otherwise. */
9596 bool
9597 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9598 location_t location, unsigned int column_override,
9599 const char *msg, va_list *ap)
9601 diagnostic_info diagnostic;
9602 diagnostic_t dlevel;
9603 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9604 bool ret;
9606 switch (level)
9608 case CPP_DL_WARNING_SYSHDR:
9609 if (flag_no_output)
9610 return false;
9611 global_dc->dc_warn_system_headers = 1;
9612 /* Fall through. */
9613 case CPP_DL_WARNING:
9614 if (flag_no_output)
9615 return false;
9616 dlevel = DK_WARNING;
9617 break;
9618 case CPP_DL_PEDWARN:
9619 if (flag_no_output && !flag_pedantic_errors)
9620 return false;
9621 dlevel = DK_PEDWARN;
9622 break;
9623 case CPP_DL_ERROR:
9624 dlevel = DK_ERROR;
9625 break;
9626 case CPP_DL_ICE:
9627 dlevel = DK_ICE;
9628 break;
9629 case CPP_DL_NOTE:
9630 dlevel = DK_NOTE;
9631 break;
9632 case CPP_DL_FATAL:
9633 dlevel = DK_FATAL;
9634 break;
9635 default:
9636 gcc_unreachable ();
9638 if (done_lexing)
9639 location = input_location;
9640 diagnostic_set_info_translated (&diagnostic, msg, ap,
9641 location, dlevel);
9642 if (column_override)
9643 diagnostic_override_column (&diagnostic, column_override);
9644 diagnostic_override_option_index (&diagnostic,
9645 c_option_controlling_cpp_error (reason));
9646 ret = report_diagnostic (&diagnostic);
9647 if (level == CPP_DL_WARNING_SYSHDR)
9648 global_dc->dc_warn_system_headers = save_warn_system_headers;
9649 return ret;
9652 /* Convert a character from the host to the target execution character
9653 set. cpplib handles this, mostly. */
9655 HOST_WIDE_INT
9656 c_common_to_target_charset (HOST_WIDE_INT c)
9658 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9659 zero-extended under -fno-signed-char. cpplib insists that characters
9660 and character constants are always unsigned. Hence we must convert
9661 back and forth. */
9662 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9664 uc = cpp_host_to_exec_charset (parse_in, uc);
9666 if (flag_signed_char)
9667 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9668 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9669 else
9670 return uc;
9673 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9674 references with an INDIRECT_REF of a constant at the bottom; much like the
9675 traditional rendering of offsetof as a macro. Return the folded result. */
9677 tree
9678 fold_offsetof_1 (tree expr)
9680 tree base, off, t;
9682 switch (TREE_CODE (expr))
9684 case ERROR_MARK:
9685 return expr;
9687 case VAR_DECL:
9688 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9689 return error_mark_node;
9691 case CALL_EXPR:
9692 case TARGET_EXPR:
9693 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9694 return error_mark_node;
9696 case NOP_EXPR:
9697 case INDIRECT_REF:
9698 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9700 error ("cannot apply %<offsetof%> to a non constant address");
9701 return error_mark_node;
9703 return TREE_OPERAND (expr, 0);
9705 case COMPONENT_REF:
9706 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9707 if (base == error_mark_node)
9708 return base;
9710 t = TREE_OPERAND (expr, 1);
9711 if (DECL_C_BIT_FIELD (t))
9713 error ("attempt to take address of bit-field structure "
9714 "member %qD", t);
9715 return error_mark_node;
9717 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9718 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9720 / BITS_PER_UNIT));
9721 break;
9723 case ARRAY_REF:
9724 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9725 if (base == error_mark_node)
9726 return base;
9728 t = TREE_OPERAND (expr, 1);
9730 /* Check if the offset goes beyond the upper bound of the array. */
9731 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9733 tree upbound = array_ref_up_bound (expr);
9734 if (upbound != NULL_TREE
9735 && TREE_CODE (upbound) == INTEGER_CST
9736 && !tree_int_cst_equal (upbound,
9737 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9739 upbound = size_binop (PLUS_EXPR, upbound,
9740 build_int_cst (TREE_TYPE (upbound), 1));
9741 if (tree_int_cst_lt (upbound, t))
9743 tree v;
9745 for (v = TREE_OPERAND (expr, 0);
9746 TREE_CODE (v) == COMPONENT_REF;
9747 v = TREE_OPERAND (v, 0))
9748 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9749 == RECORD_TYPE)
9751 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9752 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9753 if (TREE_CODE (fld_chain) == FIELD_DECL)
9754 break;
9756 if (fld_chain)
9757 break;
9759 /* Don't warn if the array might be considered a poor
9760 man's flexible array member with a very permissive
9761 definition thereof. */
9762 if (TREE_CODE (v) == ARRAY_REF
9763 || TREE_CODE (v) == COMPONENT_REF)
9764 warning (OPT_Warray_bounds,
9765 "index %E denotes an offset "
9766 "greater than size of %qT",
9767 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9772 t = convert (sizetype, t);
9773 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9774 break;
9776 case COMPOUND_EXPR:
9777 /* Handle static members of volatile structs. */
9778 t = TREE_OPERAND (expr, 1);
9779 gcc_assert (TREE_CODE (t) == VAR_DECL);
9780 return fold_offsetof_1 (t);
9782 default:
9783 gcc_unreachable ();
9786 return fold_build_pointer_plus (base, off);
9789 /* Likewise, but convert it to the return type of offsetof. */
9791 tree
9792 fold_offsetof (tree expr)
9794 return convert (size_type_node, fold_offsetof_1 (expr));
9797 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9798 expression, because B will always be true. */
9800 void
9801 warn_for_omitted_condop (location_t location, tree cond)
9803 if (truth_value_p (TREE_CODE (cond)))
9804 warning_at (location, OPT_Wparentheses,
9805 "the omitted middle operand in ?: will always be %<true%>, "
9806 "suggest explicit middle operand");
9809 /* Give an error for storing into ARG, which is 'const'. USE indicates
9810 how ARG was being used. */
9812 void
9813 readonly_error (tree arg, enum lvalue_use use)
9815 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9816 || use == lv_asm);
9817 /* Using this macro rather than (for example) arrays of messages
9818 ensures that all the format strings are checked at compile
9819 time. */
9820 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9821 : (use == lv_increment ? (I) \
9822 : (use == lv_decrement ? (D) : (AS))))
9823 if (TREE_CODE (arg) == COMPONENT_REF)
9825 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9826 error (READONLY_MSG (G_("assignment of member "
9827 "%qD in read-only object"),
9828 G_("increment of member "
9829 "%qD in read-only object"),
9830 G_("decrement of member "
9831 "%qD in read-only object"),
9832 G_("member %qD in read-only object "
9833 "used as %<asm%> output")),
9834 TREE_OPERAND (arg, 1));
9835 else
9836 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9837 G_("increment of read-only member %qD"),
9838 G_("decrement of read-only member %qD"),
9839 G_("read-only member %qD used as %<asm%> output")),
9840 TREE_OPERAND (arg, 1));
9842 else if (TREE_CODE (arg) == VAR_DECL)
9843 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9844 G_("increment of read-only variable %qD"),
9845 G_("decrement of read-only variable %qD"),
9846 G_("read-only variable %qD used as %<asm%> output")),
9847 arg);
9848 else if (TREE_CODE (arg) == PARM_DECL)
9849 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9850 G_("increment of read-only parameter %qD"),
9851 G_("decrement of read-only parameter %qD"),
9852 G_("read-only parameter %qD use as %<asm%> output")),
9853 arg);
9854 else if (TREE_CODE (arg) == RESULT_DECL)
9856 gcc_assert (c_dialect_cxx ());
9857 error (READONLY_MSG (G_("assignment of "
9858 "read-only named return value %qD"),
9859 G_("increment of "
9860 "read-only named return value %qD"),
9861 G_("decrement of "
9862 "read-only named return value %qD"),
9863 G_("read-only named return value %qD "
9864 "used as %<asm%>output")),
9865 arg);
9867 else if (TREE_CODE (arg) == FUNCTION_DECL)
9868 error (READONLY_MSG (G_("assignment of function %qD"),
9869 G_("increment of function %qD"),
9870 G_("decrement of function %qD"),
9871 G_("function %qD used as %<asm%> output")),
9872 arg);
9873 else
9874 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9875 G_("increment of read-only location %qE"),
9876 G_("decrement of read-only location %qE"),
9877 G_("read-only location %qE used as %<asm%> output")),
9878 arg);
9881 /* Print an error message for an invalid lvalue. USE says
9882 how the lvalue is being used and so selects the error message. LOC
9883 is the location for the error. */
9885 void
9886 lvalue_error (location_t loc, enum lvalue_use use)
9888 switch (use)
9890 case lv_assign:
9891 error_at (loc, "lvalue required as left operand of assignment");
9892 break;
9893 case lv_increment:
9894 error_at (loc, "lvalue required as increment operand");
9895 break;
9896 case lv_decrement:
9897 error_at (loc, "lvalue required as decrement operand");
9898 break;
9899 case lv_addressof:
9900 error_at (loc, "lvalue required as unary %<&%> operand");
9901 break;
9902 case lv_asm:
9903 error_at (loc, "lvalue required in asm statement");
9904 break;
9905 default:
9906 gcc_unreachable ();
9910 /* Print an error message for an invalid indirection of type TYPE.
9911 ERRSTRING is the name of the operator for the indirection. */
9913 void
9914 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9916 switch (errstring)
9918 case RO_NULL:
9919 gcc_assert (c_dialect_cxx ());
9920 error_at (loc, "invalid type argument (have %qT)", type);
9921 break;
9922 case RO_ARRAY_INDEXING:
9923 error_at (loc,
9924 "invalid type argument of array indexing (have %qT)",
9925 type);
9926 break;
9927 case RO_UNARY_STAR:
9928 error_at (loc,
9929 "invalid type argument of unary %<*%> (have %qT)",
9930 type);
9931 break;
9932 case RO_ARROW:
9933 error_at (loc,
9934 "invalid type argument of %<->%> (have %qT)",
9935 type);
9936 break;
9937 case RO_ARROW_STAR:
9938 error_at (loc,
9939 "invalid type argument of %<->*%> (have %qT)",
9940 type);
9941 break;
9942 case RO_IMPLICIT_CONVERSION:
9943 error_at (loc,
9944 "invalid type argument of implicit conversion (have %qT)",
9945 type);
9946 break;
9947 default:
9948 gcc_unreachable ();
9952 /* *PTYPE is an incomplete array. Complete it with a domain based on
9953 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9954 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9955 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9958 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9960 tree maxindex, type, main_type, elt, unqual_elt;
9961 int failure = 0, quals;
9962 hashval_t hashcode = 0;
9963 bool overflow_p = false;
9965 maxindex = size_zero_node;
9966 if (initial_value)
9968 if (TREE_CODE (initial_value) == STRING_CST)
9970 int eltsize
9971 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9972 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9974 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9976 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9978 if (vec_safe_is_empty (v))
9980 if (pedantic)
9981 failure = 3;
9982 maxindex = ssize_int (-1);
9984 else
9986 tree curindex;
9987 unsigned HOST_WIDE_INT cnt;
9988 constructor_elt *ce;
9989 bool fold_p = false;
9991 if ((*v)[0].index)
9992 maxindex = (*v)[0].index, fold_p = true;
9994 curindex = maxindex;
9996 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
9998 bool curfold_p = false;
9999 if (ce->index)
10000 curindex = ce->index, curfold_p = true;
10001 else
10003 if (fold_p)
10005 /* Since we treat size types now as ordinary
10006 unsigned types, we need an explicit overflow
10007 check. */
10008 tree orig = curindex;
10009 curindex = fold_convert (sizetype, curindex);
10010 overflow_p |= tree_int_cst_lt (curindex, orig);
10012 curindex = size_binop (PLUS_EXPR, curindex,
10013 size_one_node);
10015 if (tree_int_cst_lt (maxindex, curindex))
10016 maxindex = curindex, fold_p = curfold_p;
10018 if (fold_p)
10020 tree orig = maxindex;
10021 maxindex = fold_convert (sizetype, maxindex);
10022 overflow_p |= tree_int_cst_lt (maxindex, orig);
10026 else
10028 /* Make an error message unless that happened already. */
10029 if (initial_value != error_mark_node)
10030 failure = 1;
10033 else
10035 failure = 2;
10036 if (!do_default)
10037 return failure;
10040 type = *ptype;
10041 elt = TREE_TYPE (type);
10042 quals = TYPE_QUALS (strip_array_types (elt));
10043 if (quals == 0)
10044 unqual_elt = elt;
10045 else
10046 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10048 /* Using build_distinct_type_copy and modifying things afterward instead
10049 of using build_array_type to create a new type preserves all of the
10050 TYPE_LANG_FLAG_? bits that the front end may have set. */
10051 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10052 TREE_TYPE (main_type) = unqual_elt;
10053 TYPE_DOMAIN (main_type)
10054 = build_range_type (TREE_TYPE (maxindex),
10055 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10056 layout_type (main_type);
10058 /* Make sure we have the canonical MAIN_TYPE. */
10059 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10060 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10061 hashcode);
10062 main_type = type_hash_canon (hashcode, main_type);
10064 /* Fix the canonical type. */
10065 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10066 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10067 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10068 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10069 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10070 != TYPE_DOMAIN (main_type)))
10071 TYPE_CANONICAL (main_type)
10072 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10073 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10074 else
10075 TYPE_CANONICAL (main_type) = main_type;
10077 if (quals == 0)
10078 type = main_type;
10079 else
10080 type = c_build_qualified_type (main_type, quals);
10082 if (COMPLETE_TYPE_P (type)
10083 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10084 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10086 error ("size of array is too large");
10087 /* If we proceed with the array type as it is, we'll eventually
10088 crash in tree_low_cst(). */
10089 type = error_mark_node;
10092 *ptype = type;
10093 return failure;
10096 /* Like c_mark_addressable but don't check register qualifier. */
10097 void
10098 c_common_mark_addressable_vec (tree t)
10100 while (handled_component_p (t))
10101 t = TREE_OPERAND (t, 0);
10102 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10103 return;
10104 TREE_ADDRESSABLE (t) = 1;
10109 /* Used to help initialize the builtin-types.def table. When a type of
10110 the correct size doesn't exist, use error_mark_node instead of NULL.
10111 The later results in segfaults even when a decl using the type doesn't
10112 get invoked. */
10114 tree
10115 builtin_type_for_size (int size, bool unsignedp)
10117 tree type = c_common_type_for_size (size, unsignedp);
10118 return type ? type : error_mark_node;
10121 /* A helper function for resolve_overloaded_builtin in resolving the
10122 overloaded __sync_ builtins. Returns a positive power of 2 if the
10123 first operand of PARAMS is a pointer to a supported data type.
10124 Returns 0 if an error is encountered. */
10126 static int
10127 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10129 tree type;
10130 int size;
10132 if (!params)
10134 error ("too few arguments to function %qE", function);
10135 return 0;
10138 type = TREE_TYPE ((*params)[0]);
10139 if (TREE_CODE (type) != POINTER_TYPE)
10140 goto incompatible;
10142 type = TREE_TYPE (type);
10143 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10144 goto incompatible;
10146 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
10147 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10148 return size;
10150 incompatible:
10151 error ("incompatible type for argument %d of %qE", 1, function);
10152 return 0;
10155 /* A helper function for resolve_overloaded_builtin. Adds casts to
10156 PARAMS to make arguments match up with those of FUNCTION. Drops
10157 the variadic arguments at the end. Returns false if some error
10158 was encountered; true on success. */
10160 static bool
10161 sync_resolve_params (location_t loc, tree orig_function, tree function,
10162 vec<tree, va_gc> *params, bool orig_format)
10164 function_args_iterator iter;
10165 tree ptype;
10166 unsigned int parmnum;
10168 function_args_iter_init (&iter, TREE_TYPE (function));
10169 /* We've declared the implementation functions to use "volatile void *"
10170 as the pointer parameter, so we shouldn't get any complaints from the
10171 call to check_function_arguments what ever type the user used. */
10172 function_args_iter_next (&iter);
10173 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10175 /* For the rest of the values, we need to cast these to FTYPE, so that we
10176 don't get warnings for passing pointer types, etc. */
10177 parmnum = 0;
10178 while (1)
10180 tree val, arg_type;
10182 arg_type = function_args_iter_cond (&iter);
10183 /* XXX void_type_node belies the abstraction. */
10184 if (arg_type == void_type_node)
10185 break;
10187 ++parmnum;
10188 if (params->length () <= parmnum)
10190 error_at (loc, "too few arguments to function %qE", orig_function);
10191 return false;
10194 /* Only convert parameters if arg_type is unsigned integer type with
10195 new format sync routines, i.e. don't attempt to convert pointer
10196 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10197 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10198 kinds). */
10199 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10201 /* Ideally for the first conversion we'd use convert_for_assignment
10202 so that we get warnings for anything that doesn't match the pointer
10203 type. This isn't portable across the C and C++ front ends atm. */
10204 val = (*params)[parmnum];
10205 val = convert (ptype, val);
10206 val = convert (arg_type, val);
10207 (*params)[parmnum] = val;
10210 function_args_iter_next (&iter);
10213 /* __atomic routines are not variadic. */
10214 if (!orig_format && params->length () != parmnum + 1)
10216 error_at (loc, "too many arguments to function %qE", orig_function);
10217 return false;
10220 /* The definition of these primitives is variadic, with the remaining
10221 being "an optional list of variables protected by the memory barrier".
10222 No clue what that's supposed to mean, precisely, but we consider all
10223 call-clobbered variables to be protected so we're safe. */
10224 params->truncate (parmnum + 1);
10226 return true;
10229 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10230 RESULT to make it match the type of the first pointer argument in
10231 PARAMS. */
10233 static tree
10234 sync_resolve_return (tree first_param, tree result, bool orig_format)
10236 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10237 tree rtype = TREE_TYPE (result);
10238 ptype = TYPE_MAIN_VARIANT (ptype);
10240 /* New format doesn't require casting unless the types are the same size. */
10241 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10242 return convert (ptype, result);
10243 else
10244 return result;
10247 /* This function verifies the PARAMS to generic atomic FUNCTION.
10248 It returns the size if all the parameters are the same size, otherwise
10249 0 is returned if the parameters are invalid. */
10251 static int
10252 get_atomic_generic_size (location_t loc, tree function,
10253 vec<tree, va_gc> *params)
10255 unsigned int n_param;
10256 unsigned int n_model;
10257 unsigned int x;
10258 int size_0;
10259 tree type_0;
10261 /* Determine the parameter makeup. */
10262 switch (DECL_FUNCTION_CODE (function))
10264 case BUILT_IN_ATOMIC_EXCHANGE:
10265 n_param = 4;
10266 n_model = 1;
10267 break;
10268 case BUILT_IN_ATOMIC_LOAD:
10269 case BUILT_IN_ATOMIC_STORE:
10270 n_param = 3;
10271 n_model = 1;
10272 break;
10273 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10274 n_param = 6;
10275 n_model = 2;
10276 break;
10277 default:
10278 gcc_unreachable ();
10281 if (vec_safe_length (params) != n_param)
10283 error_at (loc, "incorrect number of arguments to function %qE", function);
10284 return 0;
10287 /* Get type of first parameter, and determine its size. */
10288 type_0 = TREE_TYPE ((*params)[0]);
10289 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10291 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10292 function);
10293 return 0;
10296 /* Types must be compile time constant sizes. */
10297 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10299 error_at (loc,
10300 "argument 1 of %qE must be a pointer to a constant size type",
10301 function);
10302 return 0;
10305 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10307 /* Zero size objects are not allowed. */
10308 if (size_0 == 0)
10310 error_at (loc,
10311 "argument 1 of %qE must be a pointer to a nonzero size object",
10312 function);
10313 return 0;
10316 /* Check each other parameter is a pointer and the same size. */
10317 for (x = 0; x < n_param - n_model; x++)
10319 int size;
10320 tree type = TREE_TYPE ((*params)[x]);
10321 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10322 if (n_param == 6 && x == 3)
10323 continue;
10324 if (!POINTER_TYPE_P (type))
10326 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10327 function);
10328 return 0;
10330 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10331 if (size != size_0)
10333 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10334 function);
10335 return 0;
10339 /* Check memory model parameters for validity. */
10340 for (x = n_param - n_model ; x < n_param; x++)
10342 tree p = (*params)[x];
10343 if (TREE_CODE (p) == INTEGER_CST)
10345 int i = tree_low_cst (p, 1);
10346 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10348 warning_at (loc, OPT_Winvalid_memory_model,
10349 "invalid memory model argument %d of %qE", x + 1,
10350 function);
10353 else
10354 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10356 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10357 function);
10358 return 0;
10362 return size_0;
10366 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10367 at the beginning of the parameter list PARAMS representing the size of the
10368 objects. This is to match the library ABI requirement. LOC is the location
10369 of the function call.
10370 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10371 returned to allow the external call to be constructed. */
10373 static tree
10374 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10375 vec<tree, va_gc> *params)
10377 tree size_node;
10379 /* Insert a SIZE_T parameter as the first param. If there isn't
10380 enough space, allocate a new vector and recursively re-build with that. */
10381 if (!params->space (1))
10383 unsigned int z, len;
10384 vec<tree, va_gc> *v;
10385 tree f;
10387 len = params->length ();
10388 vec_alloc (v, len + 1);
10389 for (z = 0; z < len; z++)
10390 v->quick_push ((*params)[z]);
10391 f = build_function_call_vec (loc, function, v, NULL);
10392 vec_free (v);
10393 return f;
10396 /* Add the size parameter and leave as a function call for processing. */
10397 size_node = build_int_cst (size_type_node, n);
10398 params->quick_insert (0, size_node);
10399 return NULL_TREE;
10403 /* This will process an __atomic_exchange function call, determine whether it
10404 needs to be mapped to the _N variation, or turned into a library call.
10405 LOC is the location of the builtin call.
10406 FUNCTION is the DECL that has been invoked;
10407 PARAMS is the argument list for the call. The return value is non-null
10408 TRUE is returned if it is translated into the proper format for a call to the
10409 external library, and NEW_RETURN is set the tree for that function.
10410 FALSE is returned if processing for the _N variation is required, and
10411 NEW_RETURN is set to the the return value the result is copied into. */
10412 static bool
10413 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10414 vec<tree, va_gc> *params, tree *new_return)
10416 tree p0, p1, p2, p3;
10417 tree I_type, I_type_ptr;
10418 int n = get_atomic_generic_size (loc, function, params);
10420 /* Size of 0 is an error condition. */
10421 if (n == 0)
10423 *new_return = error_mark_node;
10424 return true;
10427 /* If not a lock-free size, change to the library generic format. */
10428 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10430 *new_return = add_atomic_size_parameter (n, loc, function, params);
10431 return true;
10434 /* Otherwise there is a lockfree match, transform the call from:
10435 void fn(T* mem, T* desired, T* return, model)
10436 into
10437 *return = (T) (fn (In* mem, (In) *desired, model)) */
10439 p0 = (*params)[0];
10440 p1 = (*params)[1];
10441 p2 = (*params)[2];
10442 p3 = (*params)[3];
10444 /* Create pointer to appropriate size. */
10445 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10446 I_type_ptr = build_pointer_type (I_type);
10448 /* Convert object pointer to required type. */
10449 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10450 (*params)[0] = p0;
10451 /* Convert new value to required type, and dereference it. */
10452 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10453 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10454 (*params)[1] = p1;
10456 /* Move memory model to the 3rd position, and end param list. */
10457 (*params)[2] = p3;
10458 params->truncate (3);
10460 /* Convert return pointer and dereference it for later assignment. */
10461 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10463 return false;
10467 /* This will process an __atomic_compare_exchange function call, determine
10468 whether it needs to be mapped to the _N variation, or turned into a lib call.
10469 LOC is the location of the builtin call.
10470 FUNCTION is the DECL that has been invoked;
10471 PARAMS is the argument list for the call. The return value is non-null
10472 TRUE is returned if it is translated into the proper format for a call to the
10473 external library, and NEW_RETURN is set the tree for that function.
10474 FALSE is returned if processing for the _N variation is required. */
10476 static bool
10477 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10478 vec<tree, va_gc> *params,
10479 tree *new_return)
10481 tree p0, p1, p2;
10482 tree I_type, I_type_ptr;
10483 int n = get_atomic_generic_size (loc, function, params);
10485 /* Size of 0 is an error condition. */
10486 if (n == 0)
10488 *new_return = error_mark_node;
10489 return true;
10492 /* If not a lock-free size, change to the library generic format. */
10493 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10495 /* The library generic format does not have the weak parameter, so
10496 remove it from the param list. Since a parameter has been removed,
10497 we can be sure that there is room for the SIZE_T parameter, meaning
10498 there will not be a recursive rebuilding of the parameter list, so
10499 there is no danger this will be done twice. */
10500 if (n > 0)
10502 (*params)[3] = (*params)[4];
10503 (*params)[4] = (*params)[5];
10504 params->truncate (5);
10506 *new_return = add_atomic_size_parameter (n, loc, function, params);
10507 return true;
10510 /* Otherwise, there is a match, so the call needs to be transformed from:
10511 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10512 into
10513 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10515 p0 = (*params)[0];
10516 p1 = (*params)[1];
10517 p2 = (*params)[2];
10519 /* Create pointer to appropriate size. */
10520 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10521 I_type_ptr = build_pointer_type (I_type);
10523 /* Convert object pointer to required type. */
10524 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10525 (*params)[0] = p0;
10527 /* Convert expected pointer to required type. */
10528 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10529 (*params)[1] = p1;
10531 /* Convert desired value to required type, and dereference it. */
10532 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10533 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10534 (*params)[2] = p2;
10536 /* The rest of the parameters are fine. NULL means no special return value
10537 processing.*/
10538 *new_return = NULL;
10539 return false;
10543 /* This will process an __atomic_load function call, determine whether it
10544 needs to be mapped to the _N variation, or turned into a library call.
10545 LOC is the location of the builtin call.
10546 FUNCTION is the DECL that has been invoked;
10547 PARAMS is the argument list for the call. The return value is non-null
10548 TRUE is returned if it is translated into the proper format for a call to the
10549 external library, and NEW_RETURN is set the tree for that function.
10550 FALSE is returned if processing for the _N variation is required, and
10551 NEW_RETURN is set to the the return value the result is copied into. */
10553 static bool
10554 resolve_overloaded_atomic_load (location_t loc, tree function,
10555 vec<tree, va_gc> *params, tree *new_return)
10557 tree p0, p1, p2;
10558 tree I_type, I_type_ptr;
10559 int n = get_atomic_generic_size (loc, function, params);
10561 /* Size of 0 is an error condition. */
10562 if (n == 0)
10564 *new_return = error_mark_node;
10565 return true;
10568 /* If not a lock-free size, change to the library generic format. */
10569 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10571 *new_return = add_atomic_size_parameter (n, loc, function, params);
10572 return true;
10575 /* Otherwise, there is a match, so the call needs to be transformed from:
10576 void fn(T* mem, T* return, model)
10577 into
10578 *return = (T) (fn ((In *) mem, model)) */
10580 p0 = (*params)[0];
10581 p1 = (*params)[1];
10582 p2 = (*params)[2];
10584 /* Create pointer to appropriate size. */
10585 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10586 I_type_ptr = build_pointer_type (I_type);
10588 /* Convert object pointer to required type. */
10589 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10590 (*params)[0] = p0;
10592 /* Move memory model to the 2nd position, and end param list. */
10593 (*params)[1] = p2;
10594 params->truncate (2);
10596 /* Convert return pointer and dereference it for later assignment. */
10597 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10599 return false;
10603 /* This will process an __atomic_store function call, determine whether it
10604 needs to be mapped to the _N variation, or turned into a library call.
10605 LOC is the location of the builtin call.
10606 FUNCTION is the DECL that has been invoked;
10607 PARAMS is the argument list for the call. The return value is non-null
10608 TRUE is returned if it is translated into the proper format for a call to the
10609 external library, and NEW_RETURN is set the tree for that function.
10610 FALSE is returned if processing for the _N variation is required, and
10611 NEW_RETURN is set to the the return value the result is copied into. */
10613 static bool
10614 resolve_overloaded_atomic_store (location_t loc, tree function,
10615 vec<tree, va_gc> *params, tree *new_return)
10617 tree p0, p1;
10618 tree I_type, I_type_ptr;
10619 int n = get_atomic_generic_size (loc, function, params);
10621 /* Size of 0 is an error condition. */
10622 if (n == 0)
10624 *new_return = error_mark_node;
10625 return true;
10628 /* If not a lock-free size, change to the library generic format. */
10629 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10631 *new_return = add_atomic_size_parameter (n, loc, function, params);
10632 return true;
10635 /* Otherwise, there is a match, so the call needs to be transformed from:
10636 void fn(T* mem, T* value, model)
10637 into
10638 fn ((In *) mem, (In) *value, model) */
10640 p0 = (*params)[0];
10641 p1 = (*params)[1];
10643 /* Create pointer to appropriate size. */
10644 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10645 I_type_ptr = build_pointer_type (I_type);
10647 /* Convert object pointer to required type. */
10648 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10649 (*params)[0] = p0;
10651 /* Convert new value to required type, and dereference it. */
10652 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10653 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10654 (*params)[1] = p1;
10656 /* The memory model is in the right spot already. Return is void. */
10657 *new_return = NULL_TREE;
10659 return false;
10663 /* Some builtin functions are placeholders for other expressions. This
10664 function should be called immediately after parsing the call expression
10665 before surrounding code has committed to the type of the expression.
10667 LOC is the location of the builtin call.
10669 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10670 PARAMS is the argument list for the call. The return value is non-null
10671 when expansion is complete, and null if normal processing should
10672 continue. */
10674 tree
10675 resolve_overloaded_builtin (location_t loc, tree function,
10676 vec<tree, va_gc> *params)
10678 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10679 bool orig_format = true;
10680 tree new_return = NULL_TREE;
10682 switch (DECL_BUILT_IN_CLASS (function))
10684 case BUILT_IN_NORMAL:
10685 break;
10686 case BUILT_IN_MD:
10687 if (targetm.resolve_overloaded_builtin)
10688 return targetm.resolve_overloaded_builtin (loc, function, params);
10689 else
10690 return NULL_TREE;
10691 default:
10692 return NULL_TREE;
10695 /* Handle BUILT_IN_NORMAL here. */
10696 switch (orig_code)
10698 case BUILT_IN_ATOMIC_EXCHANGE:
10699 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10700 case BUILT_IN_ATOMIC_LOAD:
10701 case BUILT_IN_ATOMIC_STORE:
10703 /* Handle these 4 together so that they can fall through to the next
10704 case if the call is transformed to an _N variant. */
10705 switch (orig_code)
10707 case BUILT_IN_ATOMIC_EXCHANGE:
10709 if (resolve_overloaded_atomic_exchange (loc, function, params,
10710 &new_return))
10711 return new_return;
10712 /* Change to the _N variant. */
10713 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10714 break;
10717 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10719 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10720 params,
10721 &new_return))
10722 return new_return;
10723 /* Change to the _N variant. */
10724 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10725 break;
10727 case BUILT_IN_ATOMIC_LOAD:
10729 if (resolve_overloaded_atomic_load (loc, function, params,
10730 &new_return))
10731 return new_return;
10732 /* Change to the _N variant. */
10733 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10734 break;
10736 case BUILT_IN_ATOMIC_STORE:
10738 if (resolve_overloaded_atomic_store (loc, function, params,
10739 &new_return))
10740 return new_return;
10741 /* Change to the _N variant. */
10742 orig_code = BUILT_IN_ATOMIC_STORE_N;
10743 break;
10745 default:
10746 gcc_unreachable ();
10748 /* Fallthrough to the normal processing. */
10750 case BUILT_IN_ATOMIC_EXCHANGE_N:
10751 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10752 case BUILT_IN_ATOMIC_LOAD_N:
10753 case BUILT_IN_ATOMIC_STORE_N:
10754 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10755 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10756 case BUILT_IN_ATOMIC_AND_FETCH_N:
10757 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10758 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10759 case BUILT_IN_ATOMIC_OR_FETCH_N:
10760 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10761 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10762 case BUILT_IN_ATOMIC_FETCH_AND_N:
10763 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10764 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10765 case BUILT_IN_ATOMIC_FETCH_OR_N:
10767 orig_format = false;
10768 /* Fallthru for parameter processing. */
10770 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10771 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10772 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10773 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10774 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10775 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10776 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10777 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10778 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10779 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10780 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10781 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10782 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10783 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10784 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10785 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10787 int n = sync_resolve_size (function, params);
10788 tree new_function, first_param, result;
10789 enum built_in_function fncode;
10791 if (n == 0)
10792 return error_mark_node;
10794 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10795 new_function = builtin_decl_explicit (fncode);
10796 if (!sync_resolve_params (loc, function, new_function, params,
10797 orig_format))
10798 return error_mark_node;
10800 first_param = (*params)[0];
10801 result = build_function_call_vec (loc, new_function, params, NULL);
10802 if (result == error_mark_node)
10803 return result;
10804 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10805 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10806 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10807 result = sync_resolve_return (first_param, result, orig_format);
10809 /* If new_return is set, assign function to that expr and cast the
10810 result to void since the generic interface returned void. */
10811 if (new_return)
10813 /* Cast function result from I{1,2,4,8,16} to the required type. */
10814 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10815 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10816 result);
10817 TREE_SIDE_EFFECTS (result) = 1;
10818 protected_set_expr_location (result, loc);
10819 result = convert (void_type_node, result);
10821 return result;
10824 default:
10825 return NULL_TREE;
10829 /* vector_types_compatible_elements_p is used in type checks of vectors
10830 values used as operands of binary operators. Where it returns true, and
10831 the other checks of the caller succeed (being vector types in he first
10832 place, and matching number of elements), we can just treat the types
10833 as essentially the same.
10834 Contrast with vector_targets_convertible_p, which is used for vector
10835 pointer types, and vector_types_convertible_p, which will allow
10836 language-specific matches under the control of flag_lax_vector_conversions,
10837 and might still require a conversion. */
10838 /* True if vector types T1 and T2 can be inputs to the same binary
10839 operator without conversion.
10840 We don't check the overall vector size here because some of our callers
10841 want to give different error messages when the vectors are compatible
10842 except for the element count. */
10844 bool
10845 vector_types_compatible_elements_p (tree t1, tree t2)
10847 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10848 t1 = TREE_TYPE (t1);
10849 t2 = TREE_TYPE (t2);
10851 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10853 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10854 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10855 || c2 == FIXED_POINT_TYPE));
10857 t1 = c_common_signed_type (t1);
10858 t2 = c_common_signed_type (t2);
10859 /* Equality works here because c_common_signed_type uses
10860 TYPE_MAIN_VARIANT. */
10861 if (t1 == t2)
10862 return true;
10863 if (opaque && c1 == c2
10864 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10865 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10866 return true;
10867 return false;
10870 /* Check for missing format attributes on function pointers. LTYPE is
10871 the new type or left-hand side type. RTYPE is the old type or
10872 right-hand side type. Returns TRUE if LTYPE is missing the desired
10873 attribute. */
10875 bool
10876 check_missing_format_attribute (tree ltype, tree rtype)
10878 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10879 tree ra;
10881 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10882 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10883 break;
10884 if (ra)
10886 tree la;
10887 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10888 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10889 break;
10890 return !la;
10892 else
10893 return false;
10896 /* Subscripting with type char is likely to lose on a machine where
10897 chars are signed. So warn on any machine, but optionally. Don't
10898 warn for unsigned char since that type is safe. Don't warn for
10899 signed char because anyone who uses that must have done so
10900 deliberately. Furthermore, we reduce the false positive load by
10901 warning only for non-constant value of type char. */
10903 void
10904 warn_array_subscript_with_type_char (tree index)
10906 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10907 && TREE_CODE (index) != INTEGER_CST)
10908 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10911 /* Implement -Wparentheses for the unexpected C precedence rules, to
10912 cover cases like x + y << z which readers are likely to
10913 misinterpret. We have seen an expression in which CODE is a binary
10914 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10915 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10916 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10917 expression was not formed using a binary or unary operator, or it
10918 was enclosed in parentheses. */
10920 void
10921 warn_about_parentheses (location_t loc, enum tree_code code,
10922 enum tree_code code_left, tree arg_left,
10923 enum tree_code code_right, tree arg_right)
10925 if (!warn_parentheses)
10926 return;
10928 /* This macro tests that the expression ARG with original tree code
10929 CODE appears to be a boolean expression. or the result of folding a
10930 boolean expression. */
10931 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10932 (truth_value_p (TREE_CODE (ARG)) \
10933 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10934 /* Folding may create 0 or 1 integers from other expressions. */ \
10935 || ((CODE) != INTEGER_CST \
10936 && (integer_onep (ARG) || integer_zerop (ARG))))
10938 switch (code)
10940 case LSHIFT_EXPR:
10941 if (code_left == PLUS_EXPR)
10942 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10943 "suggest parentheses around %<+%> inside %<<<%>");
10944 else if (code_right == PLUS_EXPR)
10945 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10946 "suggest parentheses around %<+%> inside %<<<%>");
10947 else if (code_left == MINUS_EXPR)
10948 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10949 "suggest parentheses around %<-%> inside %<<<%>");
10950 else if (code_right == MINUS_EXPR)
10951 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10952 "suggest parentheses around %<-%> inside %<<<%>");
10953 return;
10955 case RSHIFT_EXPR:
10956 if (code_left == PLUS_EXPR)
10957 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10958 "suggest parentheses around %<+%> inside %<>>%>");
10959 else if (code_right == PLUS_EXPR)
10960 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10961 "suggest parentheses around %<+%> inside %<>>%>");
10962 else if (code_left == MINUS_EXPR)
10963 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10964 "suggest parentheses around %<-%> inside %<>>%>");
10965 else if (code_right == MINUS_EXPR)
10966 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10967 "suggest parentheses around %<-%> inside %<>>%>");
10968 return;
10970 case TRUTH_ORIF_EXPR:
10971 if (code_left == TRUTH_ANDIF_EXPR)
10972 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10973 "suggest parentheses around %<&&%> within %<||%>");
10974 else if (code_right == TRUTH_ANDIF_EXPR)
10975 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10976 "suggest parentheses around %<&&%> within %<||%>");
10977 return;
10979 case BIT_IOR_EXPR:
10980 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
10981 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
10982 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10983 "suggest parentheses around arithmetic in operand of %<|%>");
10984 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10985 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10986 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10987 "suggest parentheses around arithmetic in operand of %<|%>");
10988 /* Check cases like x|y==z */
10989 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10990 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10991 "suggest parentheses around comparison in operand of %<|%>");
10992 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10993 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10994 "suggest parentheses around comparison in operand of %<|%>");
10995 /* Check cases like !x | y */
10996 else if (code_left == TRUTH_NOT_EXPR
10997 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10998 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10999 "suggest parentheses around operand of "
11000 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11001 return;
11003 case BIT_XOR_EXPR:
11004 if (code_left == BIT_AND_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
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 return;
11021 case BIT_AND_EXPR:
11022 if (code_left == PLUS_EXPR)
11023 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11024 "suggest parentheses around %<+%> in operand of %<&%>");
11025 else if (code_right == PLUS_EXPR)
11026 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11027 "suggest parentheses around %<+%> in operand of %<&%>");
11028 else if (code_left == MINUS_EXPR)
11029 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11030 "suggest parentheses around %<-%> in operand of %<&%>");
11031 else if (code_right == MINUS_EXPR)
11032 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11033 "suggest parentheses around %<-%> in operand of %<&%>");
11034 /* Check cases like x&y==z */
11035 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11036 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11037 "suggest parentheses around comparison in operand of %<&%>");
11038 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11039 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11040 "suggest parentheses around comparison in operand of %<&%>");
11041 /* Check cases like !x & y */
11042 else if (code_left == TRUTH_NOT_EXPR
11043 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11044 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11045 "suggest parentheses around operand of "
11046 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11047 return;
11049 case EQ_EXPR:
11050 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11051 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11052 "suggest parentheses around comparison in operand of %<==%>");
11053 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11054 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11055 "suggest parentheses around comparison in operand of %<==%>");
11056 return;
11057 case NE_EXPR:
11058 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11059 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11060 "suggest parentheses around comparison in operand of %<!=%>");
11061 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11062 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11063 "suggest parentheses around comparison in operand of %<!=%>");
11064 return;
11066 default:
11067 if (TREE_CODE_CLASS (code) == tcc_comparison)
11069 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11070 && code_left != NE_EXPR && code_left != EQ_EXPR
11071 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11072 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11073 "comparisons like %<X<=Y<=Z%> do not "
11074 "have their mathematical meaning");
11075 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11076 && code_right != NE_EXPR && code_right != EQ_EXPR
11077 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11078 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11079 "comparisons like %<X<=Y<=Z%> do not "
11080 "have their mathematical meaning");
11082 return;
11084 #undef NOT_A_BOOLEAN_EXPR_P
11087 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11089 void
11090 warn_for_unused_label (tree label)
11092 if (!TREE_USED (label))
11094 if (DECL_INITIAL (label))
11095 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11096 else
11097 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11101 /* Warn for division by zero according to the value of DIVISOR. LOC
11102 is the location of the division operator. */
11104 void
11105 warn_for_div_by_zero (location_t loc, tree divisor)
11107 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11108 about division by zero. Do not issue a warning if DIVISOR has a
11109 floating-point type, since we consider 0.0/0.0 a valid way of
11110 generating a NaN. */
11111 if (c_inhibit_evaluation_warnings == 0
11112 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11113 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11116 /* Subroutine of build_binary_op. Give warnings for comparisons
11117 between signed and unsigned quantities that may fail. Do the
11118 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11119 so that casts will be considered, but default promotions won't
11122 LOCATION is the location of the comparison operator.
11124 The arguments of this function map directly to local variables
11125 of build_binary_op. */
11127 void
11128 warn_for_sign_compare (location_t location,
11129 tree orig_op0, tree orig_op1,
11130 tree op0, tree op1,
11131 tree result_type, enum tree_code resultcode)
11133 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11134 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11135 int unsignedp0, unsignedp1;
11137 /* In C++, check for comparison of different enum types. */
11138 if (c_dialect_cxx()
11139 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11140 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11141 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11142 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11144 warning_at (location,
11145 OPT_Wsign_compare, "comparison between types %qT and %qT",
11146 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11149 /* Do not warn if the comparison is being done in a signed type,
11150 since the signed type will only be chosen if it can represent
11151 all the values of the unsigned type. */
11152 if (!TYPE_UNSIGNED (result_type))
11153 /* OK */;
11154 /* Do not warn if both operands are unsigned. */
11155 else if (op0_signed == op1_signed)
11156 /* OK */;
11157 else
11159 tree sop, uop, base_type;
11160 bool ovf;
11162 if (op0_signed)
11163 sop = orig_op0, uop = orig_op1;
11164 else
11165 sop = orig_op1, uop = orig_op0;
11167 STRIP_TYPE_NOPS (sop);
11168 STRIP_TYPE_NOPS (uop);
11169 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11170 ? TREE_TYPE (result_type) : result_type);
11172 /* Do not warn if the signed quantity is an unsuffixed integer
11173 literal (or some static constant expression involving such
11174 literals or a conditional expression involving such literals)
11175 and it is non-negative. */
11176 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11177 /* OK */;
11178 /* Do not warn if the comparison is an equality operation, the
11179 unsigned quantity is an integral constant, and it would fit
11180 in the result if the result were signed. */
11181 else if (TREE_CODE (uop) == INTEGER_CST
11182 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11183 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11184 /* OK */;
11185 /* In C, do not warn if the unsigned quantity is an enumeration
11186 constant and its maximum value would fit in the result if the
11187 result were signed. */
11188 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11189 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11190 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11191 c_common_signed_type (base_type)))
11192 /* OK */;
11193 else
11194 warning_at (location,
11195 OPT_Wsign_compare,
11196 "comparison between signed and unsigned integer expressions");
11199 /* Warn if two unsigned values are being compared in a size larger
11200 than their original size, and one (and only one) is the result of
11201 a `~' operator. This comparison will always fail.
11203 Also warn if one operand is a constant, and the constant does not
11204 have all bits set that are set in the ~ operand when it is
11205 extended. */
11207 op0 = c_common_get_narrower (op0, &unsignedp0);
11208 op1 = c_common_get_narrower (op1, &unsignedp1);
11210 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11211 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11213 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11214 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11215 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11216 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11218 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11220 tree primop;
11221 HOST_WIDE_INT constant, mask;
11222 int unsignedp;
11223 unsigned int bits;
11225 if (host_integerp (op0, 0))
11227 primop = op1;
11228 unsignedp = unsignedp1;
11229 constant = tree_low_cst (op0, 0);
11231 else
11233 primop = op0;
11234 unsignedp = unsignedp0;
11235 constant = tree_low_cst (op1, 0);
11238 bits = TYPE_PRECISION (TREE_TYPE (primop));
11239 if (bits < TYPE_PRECISION (result_type)
11240 && bits < HOST_BITS_PER_LONG && unsignedp)
11242 mask = (~ (HOST_WIDE_INT) 0) << bits;
11243 if ((mask & constant) != mask)
11245 if (constant == 0)
11246 warning (OPT_Wsign_compare,
11247 "promoted ~unsigned is always non-zero");
11248 else
11249 warning_at (location, OPT_Wsign_compare,
11250 "comparison of promoted ~unsigned with constant");
11254 else if (unsignedp0 && unsignedp1
11255 && (TYPE_PRECISION (TREE_TYPE (op0))
11256 < TYPE_PRECISION (result_type))
11257 && (TYPE_PRECISION (TREE_TYPE (op1))
11258 < TYPE_PRECISION (result_type)))
11259 warning_at (location, OPT_Wsign_compare,
11260 "comparison of promoted ~unsigned with unsigned");
11264 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11265 type via c_common_type. If -Wdouble-promotion is in use, and the
11266 conditions for warning have been met, issue a warning. GMSGID is
11267 the warning message. It must have two %T specifiers for the type
11268 that was converted (generally "float") and the type to which it was
11269 converted (generally "double), respectively. LOC is the location
11270 to which the awrning should refer. */
11272 void
11273 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11274 const char *gmsgid, location_t loc)
11276 tree source_type;
11278 if (!warn_double_promotion)
11279 return;
11280 /* If the conversion will not occur at run-time, there is no need to
11281 warn about it. */
11282 if (c_inhibit_evaluation_warnings)
11283 return;
11284 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11285 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11286 return;
11287 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11288 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11289 source_type = type1;
11290 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11291 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11292 source_type = type2;
11293 else
11294 return;
11295 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11298 /* Setup a TYPE_DECL node as a typedef representation.
11300 X is a TYPE_DECL for a typedef statement. Create a brand new
11301 ..._TYPE node (which will be just a variant of the existing
11302 ..._TYPE node with identical properties) and then install X
11303 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11305 The whole point here is to end up with a situation where each
11306 and every ..._TYPE node the compiler creates will be uniquely
11307 associated with AT MOST one node representing a typedef name.
11308 This way, even though the compiler substitutes corresponding
11309 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11310 early on, later parts of the compiler can always do the reverse
11311 translation and get back the corresponding typedef name. For
11312 example, given:
11314 typedef struct S MY_TYPE;
11315 MY_TYPE object;
11317 Later parts of the compiler might only know that `object' was of
11318 type `struct S' if it were not for code just below. With this
11319 code however, later parts of the compiler see something like:
11321 struct S' == struct S
11322 typedef struct S' MY_TYPE;
11323 struct S' object;
11325 And they can then deduce (from the node for type struct S') that
11326 the original object declaration was:
11328 MY_TYPE object;
11330 Being able to do this is important for proper support of protoize,
11331 and also for generating precise symbolic debugging information
11332 which takes full account of the programmer's (typedef) vocabulary.
11334 Obviously, we don't want to generate a duplicate ..._TYPE node if
11335 the TYPE_DECL node that we are now processing really represents a
11336 standard built-in type. */
11338 void
11339 set_underlying_type (tree x)
11341 if (x == error_mark_node)
11342 return;
11343 if (DECL_IS_BUILTIN (x))
11345 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11346 TYPE_NAME (TREE_TYPE (x)) = x;
11348 else if (TREE_TYPE (x) != error_mark_node
11349 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11351 tree tt = TREE_TYPE (x);
11352 DECL_ORIGINAL_TYPE (x) = tt;
11353 tt = build_variant_type_copy (tt);
11354 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11355 TYPE_NAME (tt) = x;
11356 TREE_USED (tt) = TREE_USED (x);
11357 TREE_TYPE (x) = tt;
11361 /* Record the types used by the current global variable declaration
11362 being parsed, so that we can decide later to emit their debug info.
11363 Those types are in types_used_by_cur_var_decl, and we are going to
11364 store them in the types_used_by_vars_hash hash table.
11365 DECL is the declaration of the global variable that has been parsed. */
11367 void
11368 record_types_used_by_current_var_decl (tree decl)
11370 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11372 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11374 tree type = types_used_by_cur_var_decl->pop ();
11375 types_used_by_var_decl_insert (type, decl);
11379 /* If DECL is a typedef that is declared in the current function,
11380 record it for the purpose of -Wunused-local-typedefs. */
11382 void
11383 record_locally_defined_typedef (tree decl)
11385 struct c_language_function *l;
11387 if (!warn_unused_local_typedefs
11388 || cfun == NULL
11389 /* if this is not a locally defined typedef then we are not
11390 interested. */
11391 || !is_typedef_decl (decl)
11392 || !decl_function_context (decl))
11393 return;
11395 l = (struct c_language_function *) cfun->language;
11396 vec_safe_push (l->local_typedefs, decl);
11399 /* If T is a TYPE_DECL declared locally, mark it as used. */
11401 void
11402 maybe_record_typedef_use (tree t)
11404 if (!is_typedef_decl (t))
11405 return;
11407 TREE_USED (t) = true;
11410 /* Warn if there are some unused locally defined typedefs in the
11411 current function. */
11413 void
11414 maybe_warn_unused_local_typedefs (void)
11416 int i;
11417 tree decl;
11418 /* The number of times we have emitted -Wunused-local-typedefs
11419 warnings. If this is different from errorcount, that means some
11420 unrelated errors have been issued. In which case, we'll avoid
11421 emitting "unused-local-typedefs" warnings. */
11422 static int unused_local_typedefs_warn_count;
11423 struct c_language_function *l;
11425 if (cfun == NULL)
11426 return;
11428 if ((l = (struct c_language_function *) cfun->language) == NULL)
11429 return;
11431 if (warn_unused_local_typedefs
11432 && errorcount == unused_local_typedefs_warn_count)
11434 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11435 if (!TREE_USED (decl))
11436 warning_at (DECL_SOURCE_LOCATION (decl),
11437 OPT_Wunused_local_typedefs,
11438 "typedef %qD locally defined but not used", decl);
11439 unused_local_typedefs_warn_count = errorcount;
11442 vec_free (l->local_typedefs);
11445 /* The C and C++ parsers both use vectors to hold function arguments.
11446 For efficiency, we keep a cache of unused vectors. This is the
11447 cache. */
11449 typedef vec<tree, va_gc> *tree_gc_vec;
11450 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11452 /* Return a new vector from the cache. If the cache is empty,
11453 allocate a new vector. These vectors are GC'ed, so it is OK if the
11454 pointer is not released.. */
11456 vec<tree, va_gc> *
11457 make_tree_vector (void)
11459 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11460 return tree_vector_cache->pop ();
11461 else
11463 /* Passing 0 to vec::alloc returns NULL, and our callers require
11464 that we always return a non-NULL value. The vector code uses
11465 4 when growing a NULL vector, so we do too. */
11466 vec<tree, va_gc> *v;
11467 vec_alloc (v, 4);
11468 return v;
11472 /* Release a vector of trees back to the cache. */
11474 void
11475 release_tree_vector (vec<tree, va_gc> *vec)
11477 if (vec != NULL)
11479 vec->truncate (0);
11480 vec_safe_push (tree_vector_cache, vec);
11484 /* Get a new tree vector holding a single tree. */
11486 vec<tree, va_gc> *
11487 make_tree_vector_single (tree t)
11489 vec<tree, va_gc> *ret = make_tree_vector ();
11490 ret->quick_push (t);
11491 return ret;
11494 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11496 vec<tree, va_gc> *
11497 make_tree_vector_from_list (tree list)
11499 vec<tree, va_gc> *ret = make_tree_vector ();
11500 for (; list; list = TREE_CHAIN (list))
11501 vec_safe_push (ret, TREE_VALUE (list));
11502 return ret;
11505 /* Get a new tree vector which is a copy of an existing one. */
11507 vec<tree, va_gc> *
11508 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11510 vec<tree, va_gc> *ret;
11511 unsigned int ix;
11512 tree t;
11514 ret = make_tree_vector ();
11515 vec_safe_reserve (ret, vec_safe_length (orig));
11516 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11517 ret->quick_push (t);
11518 return ret;
11521 /* Return true if KEYWORD starts a type specifier. */
11523 bool
11524 keyword_begins_type_specifier (enum rid keyword)
11526 switch (keyword)
11528 case RID_INT:
11529 case RID_CHAR:
11530 case RID_FLOAT:
11531 case RID_DOUBLE:
11532 case RID_VOID:
11533 case RID_INT128:
11534 case RID_UNSIGNED:
11535 case RID_LONG:
11536 case RID_SHORT:
11537 case RID_SIGNED:
11538 case RID_DFLOAT32:
11539 case RID_DFLOAT64:
11540 case RID_DFLOAT128:
11541 case RID_FRACT:
11542 case RID_ACCUM:
11543 case RID_BOOL:
11544 case RID_WCHAR:
11545 case RID_CHAR16:
11546 case RID_CHAR32:
11547 case RID_SAT:
11548 case RID_COMPLEX:
11549 case RID_TYPEOF:
11550 case RID_STRUCT:
11551 case RID_CLASS:
11552 case RID_UNION:
11553 case RID_ENUM:
11554 return true;
11555 default:
11556 return false;
11560 /* Return true if KEYWORD names a type qualifier. */
11562 bool
11563 keyword_is_type_qualifier (enum rid keyword)
11565 switch (keyword)
11567 case RID_CONST:
11568 case RID_VOLATILE:
11569 case RID_RESTRICT:
11570 return true;
11571 default:
11572 return false;
11576 /* Return true if KEYWORD names a storage class specifier.
11578 RID_TYPEDEF is not included in this list despite `typedef' being
11579 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11580 such for syntactic convenience only. */
11582 bool
11583 keyword_is_storage_class_specifier (enum rid keyword)
11585 switch (keyword)
11587 case RID_STATIC:
11588 case RID_EXTERN:
11589 case RID_REGISTER:
11590 case RID_AUTO:
11591 case RID_MUTABLE:
11592 case RID_THREAD:
11593 return true;
11594 default:
11595 return false;
11599 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11601 static bool
11602 keyword_is_function_specifier (enum rid keyword)
11604 switch (keyword)
11606 case RID_INLINE:
11607 case RID_NORETURN:
11608 case RID_VIRTUAL:
11609 case RID_EXPLICIT:
11610 return true;
11611 default:
11612 return false;
11616 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11617 declaration-specifier (C99 6.7). */
11619 bool
11620 keyword_is_decl_specifier (enum rid keyword)
11622 if (keyword_is_storage_class_specifier (keyword)
11623 || keyword_is_type_qualifier (keyword)
11624 || keyword_is_function_specifier (keyword))
11625 return true;
11627 switch (keyword)
11629 case RID_TYPEDEF:
11630 case RID_FRIEND:
11631 case RID_CONSTEXPR:
11632 return true;
11633 default:
11634 return false;
11638 /* Initialize language-specific-bits of tree_contains_struct. */
11640 void
11641 c_common_init_ts (void)
11643 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11644 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11645 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11648 /* Build a user-defined numeric literal out of an integer constant type VALUE
11649 with identifier SUFFIX. */
11651 tree
11652 build_userdef_literal (tree suffix_id, tree value,
11653 enum overflow_type overflow, tree num_string)
11655 tree literal = make_node (USERDEF_LITERAL);
11656 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11657 USERDEF_LITERAL_VALUE (literal) = value;
11658 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11659 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11660 return literal;
11663 /* For vector[index], convert the vector to a
11664 pointer of the underlying type. */
11665 void
11666 convert_vector_to_pointer_for_subscript (location_t loc,
11667 tree* vecp, tree index)
11669 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11671 tree type = TREE_TYPE (*vecp);
11672 tree type1;
11674 if (TREE_CODE (index) == INTEGER_CST)
11675 if (!host_integerp (index, 1)
11676 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11677 >= TYPE_VECTOR_SUBPARTS (type)))
11678 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11680 c_common_mark_addressable_vec (*vecp);
11681 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11682 type = build_pointer_type (type);
11683 type1 = build_pointer_type (TREE_TYPE (*vecp));
11684 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11685 *vecp = convert (type, *vecp);
11689 /* Determine which of the operands, if any, is a scalar that needs to be
11690 converted to a vector, for the range of operations. */
11691 enum stv_conv
11692 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11693 bool complain)
11695 tree type0 = TREE_TYPE (op0);
11696 tree type1 = TREE_TYPE (op1);
11697 bool integer_only_op = false;
11698 enum stv_conv ret = stv_firstarg;
11700 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11701 || TREE_CODE (type1) == VECTOR_TYPE);
11702 switch (code)
11704 /* Most GENERIC binary expressions require homogeneous arguments.
11705 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11706 argument that is a vector and a second one that is a scalar, so
11707 we never return stv_secondarg for them. */
11708 case RSHIFT_EXPR:
11709 case LSHIFT_EXPR:
11710 if (TREE_CODE (type0) == INTEGER_TYPE
11711 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11713 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11715 if (complain)
11716 error_at (loc, "conversion of scalar %qT to vector %qT "
11717 "involves truncation", type0, type1);
11718 return stv_error;
11720 else
11721 return stv_firstarg;
11723 break;
11725 case BIT_IOR_EXPR:
11726 case BIT_XOR_EXPR:
11727 case BIT_AND_EXPR:
11728 integer_only_op = true;
11729 /* ... fall through ... */
11731 case VEC_COND_EXPR:
11733 case PLUS_EXPR:
11734 case MINUS_EXPR:
11735 case MULT_EXPR:
11736 case TRUNC_DIV_EXPR:
11737 case CEIL_DIV_EXPR:
11738 case FLOOR_DIV_EXPR:
11739 case ROUND_DIV_EXPR:
11740 case EXACT_DIV_EXPR:
11741 case TRUNC_MOD_EXPR:
11742 case FLOOR_MOD_EXPR:
11743 case RDIV_EXPR:
11744 case EQ_EXPR:
11745 case NE_EXPR:
11746 case LE_EXPR:
11747 case GE_EXPR:
11748 case LT_EXPR:
11749 case GT_EXPR:
11750 /* What about UNLT_EXPR? */
11751 if (TREE_CODE (type0) == VECTOR_TYPE)
11753 tree tmp;
11754 ret = stv_secondarg;
11755 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11756 tmp = type0; type0 = type1; type1 = tmp;
11757 tmp = op0; op0 = op1; op1 = tmp;
11760 if (TREE_CODE (type0) == INTEGER_TYPE
11761 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11763 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11765 if (complain)
11766 error_at (loc, "conversion of scalar %qT to vector %qT "
11767 "involves truncation", type0, type1);
11768 return stv_error;
11770 return ret;
11772 else if (!integer_only_op
11773 /* Allow integer --> real conversion if safe. */
11774 && (TREE_CODE (type0) == REAL_TYPE
11775 || TREE_CODE (type0) == INTEGER_TYPE)
11776 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11778 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11780 if (complain)
11781 error_at (loc, "conversion of scalar %qT to vector %qT "
11782 "involves truncation", type0, type1);
11783 return stv_error;
11785 return ret;
11787 default:
11788 break;
11791 return stv_nothing;
11794 /* Return true iff ALIGN is an integral constant that is a fundamental
11795 alignment, as defined by [basic.align] in the c++-11
11796 specifications.
11798 That is:
11800 [A fundamental alignment is represented by an alignment less than or
11801 equal to the greatest alignment supported by the implementation
11802 in all contexts, which is equal to
11803 alignof(max_align_t)]. */
11805 bool
11806 cxx_fundamental_alignment_p (unsigned align)
11808 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11809 TYPE_ALIGN (long_double_type_node)));
11812 #include "gt-c-family-c-common.h"