Merge from trunk
[official-gcc.git] / gcc / c-family / c-common.c
blobd617f3eb2449d37eae2dab7a92b32f2f004b35ab
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2014 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 "fold-const.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "varasm.h"
32 #include "trans-mem.h"
33 #include "flags.h"
34 #include "c-pragma.h"
35 #include "c-common.h"
36 #include "c-objc.h"
37 #include "tm_p.h"
38 #include "obstack.h"
39 #include "cpplib.h"
40 #include "target.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "diagnostic.h"
46 #include "tree-iterator.h"
47 #include "hashtab.h"
48 #include "opts.h"
49 #include "cgraph.h"
50 #include "target-def.h"
51 #include "gimplify.h"
53 cpp_reader *parse_in; /* Declared in c-pragma.h. */
55 /* The following symbols are subsumed in the c_global_trees array, and
56 listed here individually for documentation purposes.
58 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
60 tree short_integer_type_node;
61 tree long_integer_type_node;
62 tree long_long_integer_type_node;
63 tree int128_integer_type_node;
65 tree short_unsigned_type_node;
66 tree long_unsigned_type_node;
67 tree long_long_unsigned_type_node;
68 tree int128_unsigned_type_node;
70 tree truthvalue_type_node;
71 tree truthvalue_false_node;
72 tree truthvalue_true_node;
74 tree ptrdiff_type_node;
76 tree unsigned_char_type_node;
77 tree signed_char_type_node;
78 tree wchar_type_node;
80 tree char16_type_node;
81 tree char32_type_node;
83 tree float_type_node;
84 tree double_type_node;
85 tree long_double_type_node;
87 tree complex_integer_type_node;
88 tree complex_float_type_node;
89 tree complex_double_type_node;
90 tree complex_long_double_type_node;
92 tree dfloat32_type_node;
93 tree dfloat64_type_node;
94 tree_dfloat128_type_node;
96 tree intQI_type_node;
97 tree intHI_type_node;
98 tree intSI_type_node;
99 tree intDI_type_node;
100 tree intTI_type_node;
102 tree unsigned_intQI_type_node;
103 tree unsigned_intHI_type_node;
104 tree unsigned_intSI_type_node;
105 tree unsigned_intDI_type_node;
106 tree unsigned_intTI_type_node;
108 tree widest_integer_literal_type_node;
109 tree widest_unsigned_literal_type_node;
111 Nodes for types `void *' and `const void *'.
113 tree ptr_type_node, const_ptr_type_node;
115 Nodes for types `char *' and `const char *'.
117 tree string_type_node, const_string_type_node;
119 Type `char[SOMENUMBER]'.
120 Used when an array of char is needed and the size is irrelevant.
122 tree char_array_type_node;
124 Type `wchar_t[SOMENUMBER]' or something like it.
125 Used when a wide string literal is created.
127 tree wchar_array_type_node;
129 Type `char16_t[SOMENUMBER]' or something like it.
130 Used when a UTF-16 string literal is created.
132 tree char16_array_type_node;
134 Type `char32_t[SOMENUMBER]' or something like it.
135 Used when a UTF-32 string literal is created.
137 tree char32_array_type_node;
139 Type `int ()' -- used for implicit declaration of functions.
141 tree default_function_type;
143 A VOID_TYPE node, packaged in a TREE_LIST.
145 tree void_list_node;
147 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
148 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
149 VAR_DECLS, but C++ does.)
151 tree function_name_decl_node;
152 tree pretty_function_name_decl_node;
153 tree c99_function_name_decl_node;
155 Stack of nested function name VAR_DECLs.
157 tree saved_function_name_decls;
161 tree c_global_trees[CTI_MAX];
163 /* Switches common to the C front ends. */
165 /* Nonzero means don't output line number information. */
167 char flag_no_line_commands;
169 /* Nonzero causes -E output not to be done, but directives such as
170 #define that have side effects are still obeyed. */
172 char flag_no_output;
174 /* Nonzero means dump macros in some fashion. */
176 char flag_dump_macros;
178 /* Nonzero means pass #include lines through to the output. */
180 char flag_dump_includes;
182 /* Nonzero means process PCH files while preprocessing. */
184 bool flag_pch_preprocess;
186 /* The file name to which we should write a precompiled header, or
187 NULL if no header will be written in this compile. */
189 const char *pch_file;
191 /* Nonzero if an ISO standard was selected. It rejects macros in the
192 user's namespace. */
193 int flag_iso;
195 /* C/ObjC language option variables. */
198 /* Nonzero means allow type mismatches in conditional expressions;
199 just make their values `void'. */
201 int flag_cond_mismatch;
203 /* Nonzero means enable C89 Amendment 1 features. */
205 int flag_isoc94;
207 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
209 int flag_isoc99;
211 /* Nonzero means use the ISO C11 dialect of C. */
213 int flag_isoc11;
215 /* Nonzero means that we have builtin functions, and main is an int. */
217 int flag_hosted = 1;
220 /* ObjC language option variables. */
223 /* Tells the compiler that this is a special run. Do not perform any
224 compiling, instead we are to test some platform dependent features
225 and output a C header file with appropriate definitions. */
227 int print_struct_values;
229 /* Tells the compiler what is the constant string class for ObjC. */
231 const char *constant_string_class_name;
234 /* C++ language option variables. */
237 /* Nonzero means generate separate instantiation control files and
238 juggle them at link time. */
240 int flag_use_repository;
242 /* The C++ dialect being used. C++11 is the default. */
244 enum cxx_dialect cxx_dialect = cxx11;
246 /* Maximum template instantiation depth. This limit exists to limit the
247 time it takes to notice excessively recursive template instantiations.
249 The default is lower than the 1024 recommended by the C++0x standard
250 because G++ runs out of stack before 1024 with highly recursive template
251 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
253 int max_tinst_depth = 900;
255 /* The elements of `ridpointers' are identifier nodes for the reserved
256 type names and storage classes. It is indexed by a RID_... value. */
257 tree *ridpointers;
259 tree (*make_fname_decl) (location_t, tree, int);
261 /* Nonzero means don't warn about problems that occur when the code is
262 executed. */
263 int c_inhibit_evaluation_warnings;
265 /* Whether we are building a boolean conversion inside
266 convert_for_assignment, or some other late binary operation. If
267 build_binary_op is called for C (from code shared by C and C++) in
268 this case, then the operands have already been folded and the
269 result will not be folded again, so C_MAYBE_CONST_EXPR should not
270 be generated. */
271 bool in_late_binary_op;
273 /* Whether lexing has been completed, so subsequent preprocessor
274 errors should use the compiler's input_location. */
275 bool done_lexing = false;
277 /* Information about how a function name is generated. */
278 struct fname_var_t
280 tree *const decl; /* pointer to the VAR_DECL. */
281 const unsigned rid; /* RID number for the identifier. */
282 const int pretty; /* How pretty is it? */
285 /* The three ways of getting then name of the current function. */
287 const struct fname_var_t fname_vars[] =
289 /* C99 compliant __func__, must be first. */
290 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
291 /* GCC __FUNCTION__ compliant. */
292 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
293 /* GCC __PRETTY_FUNCTION__ compliant. */
294 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
295 {NULL, 0, 0},
298 /* Global visibility options. */
299 struct visibility_flags visibility_options;
301 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
302 static tree check_case_value (tree);
303 static bool check_case_bounds (tree, tree, tree *, tree *);
305 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
306 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
312 int, bool *);
313 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
314 int, bool *);
315 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
316 bool *);
317 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_always_inline_attribute (tree *, tree, tree, int,
321 bool *);
322 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
329 bool *);
330 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_transparent_union_attribute (tree *, tree, tree,
332 int, bool *);
333 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
339 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
340 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
343 static tree handle_visibility_attribute (tree *, tree, tree, int,
344 bool *);
345 static tree handle_tls_model_attribute (tree *, tree, tree, int,
346 bool *);
347 static tree handle_no_instrument_function_attribute (tree *, tree,
348 tree, int, bool *);
349 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_deprecated_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_vector_size_attribute (tree *, tree, tree, int,
360 bool *);
361 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
365 bool *);
366 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
373 static tree ignore_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
379 bool *);
380 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
381 bool *);
383 static void check_function_nonnull (tree, int, tree *);
384 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
385 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
386 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
387 static int resort_field_decl_cmp (const void *, const void *);
389 /* Reserved words. The third field is a mask: keywords are disabled
390 if they match the mask.
392 Masks for languages:
393 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
394 C --std=c99: D_CXXONLY | D_OBJC
395 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
396 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
397 C++ --std=c0x: D_CONLY | D_OBJC
398 ObjC++ is like C++ except that D_OBJC is not set
400 If -fno-asm is used, D_ASM is added to the mask. If
401 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
402 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
403 In C with -Wc++-compat, we warn if D_CXXWARN is set.
405 Note the complication of the D_CXX_OBJC keywords. These are
406 reserved words such as 'class'. In C++, 'class' is a reserved
407 word. In Objective-C++ it is too. In Objective-C, it is a
408 reserved word too, but only if it follows an '@' sign.
410 const struct c_common_resword c_common_reswords[] =
412 { "_Alignas", RID_ALIGNAS, D_CONLY },
413 { "_Alignof", RID_ALIGNOF, D_CONLY },
414 { "_Atomic", RID_ATOMIC, D_CONLY },
415 { "_Bool", RID_BOOL, D_CONLY },
416 { "_Complex", RID_COMPLEX, 0 },
417 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
418 { "_Cilk_sync", RID_CILK_SYNC, 0 },
419 { "_Imaginary", RID_IMAGINARY, D_CONLY },
420 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
421 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
422 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
423 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
424 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
425 { "_Sat", RID_SAT, D_CONLY | D_EXT },
426 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
427 { "_Noreturn", RID_NORETURN, D_CONLY },
428 { "_Generic", RID_GENERIC, D_CONLY },
429 { "_Thread_local", RID_THREAD, D_CONLY },
430 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
431 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
432 { "__alignof", RID_ALIGNOF, 0 },
433 { "__alignof__", RID_ALIGNOF, 0 },
434 { "__asm", RID_ASM, 0 },
435 { "__asm__", RID_ASM, 0 },
436 { "__attribute", RID_ATTRIBUTE, 0 },
437 { "__attribute__", RID_ATTRIBUTE, 0 },
438 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
439 { "__bases", RID_BASES, D_CXXONLY },
440 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
441 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
442 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
443 { "__builtin_offsetof", RID_OFFSETOF, 0 },
444 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
445 { "__builtin_va_arg", RID_VA_ARG, 0 },
446 { "__complex", RID_COMPLEX, 0 },
447 { "__complex__", RID_COMPLEX, 0 },
448 { "__const", RID_CONST, 0 },
449 { "__const__", RID_CONST, 0 },
450 { "__decltype", RID_DECLTYPE, D_CXXONLY },
451 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
452 { "__extension__", RID_EXTENSION, 0 },
453 { "__func__", RID_C99_FUNCTION_NAME, 0 },
454 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
455 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
456 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
457 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
458 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
459 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
460 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
461 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
462 { "__imag", RID_IMAGPART, 0 },
463 { "__imag__", RID_IMAGPART, 0 },
464 { "__inline", RID_INLINE, 0 },
465 { "__inline__", RID_INLINE, 0 },
466 { "__int128", RID_INT128, 0 },
467 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
468 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
469 { "__is_class", RID_IS_CLASS, D_CXXONLY },
470 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
471 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
472 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
473 { "__is_final", RID_IS_FINAL, D_CXXONLY },
474 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
475 { "__is_pod", RID_IS_POD, D_CXXONLY },
476 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
477 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
478 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
479 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
480 { "__is_union", RID_IS_UNION, D_CXXONLY },
481 { "__label__", RID_LABEL, 0 },
482 { "__null", RID_NULL, 0 },
483 { "__real", RID_REALPART, 0 },
484 { "__real__", RID_REALPART, 0 },
485 { "__restrict", RID_RESTRICT, 0 },
486 { "__restrict__", RID_RESTRICT, 0 },
487 { "__signed", RID_SIGNED, 0 },
488 { "__signed__", RID_SIGNED, 0 },
489 { "__thread", RID_THREAD, 0 },
490 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
491 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
492 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
493 { "__typeof", RID_TYPEOF, 0 },
494 { "__typeof__", RID_TYPEOF, 0 },
495 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
496 { "__volatile", RID_VOLATILE, 0 },
497 { "__volatile__", RID_VOLATILE, 0 },
498 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
499 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
500 { "asm", RID_ASM, D_ASM },
501 { "auto", RID_AUTO, 0 },
502 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
503 { "break", RID_BREAK, 0 },
504 { "case", RID_CASE, 0 },
505 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
506 { "char", RID_CHAR, 0 },
507 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
508 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
509 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
510 { "const", RID_CONST, 0 },
511 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
512 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
513 { "continue", RID_CONTINUE, 0 },
514 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
515 { "default", RID_DEFAULT, 0 },
516 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
517 { "do", RID_DO, 0 },
518 { "double", RID_DOUBLE, 0 },
519 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
520 { "else", RID_ELSE, 0 },
521 { "enum", RID_ENUM, 0 },
522 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
523 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
524 { "extern", RID_EXTERN, 0 },
525 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
526 { "float", RID_FLOAT, 0 },
527 { "for", RID_FOR, 0 },
528 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
529 { "goto", RID_GOTO, 0 },
530 { "if", RID_IF, 0 },
531 { "inline", RID_INLINE, D_EXT89 },
532 { "int", RID_INT, 0 },
533 { "long", RID_LONG, 0 },
534 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
535 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
536 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
537 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
538 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
539 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
540 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
541 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
542 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
543 { "register", RID_REGISTER, 0 },
544 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
545 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
546 { "return", RID_RETURN, 0 },
547 { "short", RID_SHORT, 0 },
548 { "signed", RID_SIGNED, 0 },
549 { "sizeof", RID_SIZEOF, 0 },
550 { "static", RID_STATIC, 0 },
551 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
552 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
553 { "struct", RID_STRUCT, 0 },
554 { "switch", RID_SWITCH, 0 },
555 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
556 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
557 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
558 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
559 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
560 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
561 { "typedef", RID_TYPEDEF, 0 },
562 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
563 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
564 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
565 { "union", RID_UNION, 0 },
566 { "unsigned", RID_UNSIGNED, 0 },
567 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
568 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
569 { "void", RID_VOID, 0 },
570 { "volatile", RID_VOLATILE, 0 },
571 { "wchar_t", RID_WCHAR, D_CXXONLY },
572 { "while", RID_WHILE, 0 },
574 /* Concepts-related keywords */
575 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
576 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
578 /* These Objective-C keywords are recognized only immediately after
579 an '@'. */
580 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
581 { "defs", RID_AT_DEFS, D_OBJC },
582 { "encode", RID_AT_ENCODE, D_OBJC },
583 { "end", RID_AT_END, D_OBJC },
584 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
585 { "interface", RID_AT_INTERFACE, D_OBJC },
586 { "protocol", RID_AT_PROTOCOL, D_OBJC },
587 { "selector", RID_AT_SELECTOR, D_OBJC },
588 { "finally", RID_AT_FINALLY, D_OBJC },
589 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
590 { "optional", RID_AT_OPTIONAL, D_OBJC },
591 { "required", RID_AT_REQUIRED, D_OBJC },
592 { "property", RID_AT_PROPERTY, D_OBJC },
593 { "package", RID_AT_PACKAGE, D_OBJC },
594 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
595 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
596 /* These are recognized only in protocol-qualifier context
597 (see above) */
598 { "bycopy", RID_BYCOPY, D_OBJC },
599 { "byref", RID_BYREF, D_OBJC },
600 { "in", RID_IN, D_OBJC },
601 { "inout", RID_INOUT, D_OBJC },
602 { "oneway", RID_ONEWAY, D_OBJC },
603 { "out", RID_OUT, D_OBJC },
604 /* These are recognized inside a property attribute list */
605 { "assign", RID_ASSIGN, D_OBJC },
606 { "copy", RID_COPY, D_OBJC },
607 { "getter", RID_GETTER, D_OBJC },
608 { "nonatomic", RID_NONATOMIC, D_OBJC },
609 { "readonly", RID_READONLY, D_OBJC },
610 { "readwrite", RID_READWRITE, D_OBJC },
611 { "retain", RID_RETAIN, D_OBJC },
612 { "setter", RID_SETTER, D_OBJC },
615 const unsigned int num_c_common_reswords =
616 sizeof c_common_reswords / sizeof (struct c_common_resword);
618 /* Table of machine-independent attributes common to all C-like languages. */
619 const struct attribute_spec c_common_attribute_table[] =
621 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
622 affects_type_identity } */
623 { "packed", 0, 0, false, false, false,
624 handle_packed_attribute , false},
625 { "nocommon", 0, 0, true, false, false,
626 handle_nocommon_attribute, false},
627 { "common", 0, 0, true, false, false,
628 handle_common_attribute, false },
629 /* FIXME: logically, noreturn attributes should be listed as
630 "false, true, true" and apply to function types. But implementing this
631 would require all the places in the compiler that use TREE_THIS_VOLATILE
632 on a decl to identify non-returning functions to be located and fixed
633 to check the function type instead. */
634 { "noreturn", 0, 0, true, false, false,
635 handle_noreturn_attribute, false },
636 { "volatile", 0, 0, true, false, false,
637 handle_noreturn_attribute, false },
638 { "noinline", 0, 0, true, false, false,
639 handle_noinline_attribute, false },
640 { "noclone", 0, 0, true, false, false,
641 handle_noclone_attribute, false },
642 { "leaf", 0, 0, true, false, false,
643 handle_leaf_attribute, false },
644 { "always_inline", 0, 0, true, false, false,
645 handle_always_inline_attribute, false },
646 { "gnu_inline", 0, 0, true, false, false,
647 handle_gnu_inline_attribute, false },
648 { "artificial", 0, 0, true, false, false,
649 handle_artificial_attribute, false },
650 { "flatten", 0, 0, true, false, false,
651 handle_flatten_attribute, false },
652 { "used", 0, 0, true, false, false,
653 handle_used_attribute, false },
654 { "unused", 0, 0, false, false, false,
655 handle_unused_attribute, false },
656 { "externally_visible", 0, 0, true, false, false,
657 handle_externally_visible_attribute, false },
658 /* The same comments as for noreturn attributes apply to const ones. */
659 { "const", 0, 0, true, false, false,
660 handle_const_attribute, false },
661 { "transparent_union", 0, 0, false, false, false,
662 handle_transparent_union_attribute, false },
663 { "constructor", 0, 1, true, false, false,
664 handle_constructor_attribute, false },
665 { "destructor", 0, 1, true, false, false,
666 handle_destructor_attribute, false },
667 { "mode", 1, 1, false, true, false,
668 handle_mode_attribute, false },
669 { "section", 1, 1, true, false, false,
670 handle_section_attribute, false },
671 { "aligned", 0, 1, false, false, false,
672 handle_aligned_attribute, false },
673 { "weak", 0, 0, true, false, false,
674 handle_weak_attribute, false },
675 { "ifunc", 1, 1, true, false, false,
676 handle_ifunc_attribute, false },
677 { "alias", 1, 1, true, false, false,
678 handle_alias_attribute, false },
679 { "weakref", 0, 1, true, false, false,
680 handle_weakref_attribute, false },
681 { "no_instrument_function", 0, 0, true, false, false,
682 handle_no_instrument_function_attribute,
683 false },
684 { "malloc", 0, 0, true, false, false,
685 handle_malloc_attribute, false },
686 { "returns_twice", 0, 0, true, false, false,
687 handle_returns_twice_attribute, false },
688 { "no_stack_limit", 0, 0, true, false, false,
689 handle_no_limit_stack_attribute, false },
690 { "pure", 0, 0, true, false, false,
691 handle_pure_attribute, false },
692 { "transaction_callable", 0, 0, false, true, false,
693 handle_tm_attribute, false },
694 { "transaction_unsafe", 0, 0, false, true, false,
695 handle_tm_attribute, false },
696 { "transaction_safe", 0, 0, false, true, false,
697 handle_tm_attribute, false },
698 { "transaction_may_cancel_outer", 0, 0, false, true, false,
699 handle_tm_attribute, false },
700 /* ??? These two attributes didn't make the transition from the
701 Intel language document to the multi-vendor language document. */
702 { "transaction_pure", 0, 0, false, true, false,
703 handle_tm_attribute, false },
704 { "transaction_wrap", 1, 1, true, false, false,
705 handle_tm_wrap_attribute, false },
706 /* For internal use (marking of builtins) only. The name contains space
707 to prevent its usage in source code. */
708 { "no vops", 0, 0, true, false, false,
709 handle_novops_attribute, false },
710 { "deprecated", 0, 1, false, false, false,
711 handle_deprecated_attribute, false },
712 { "vector_size", 1, 1, false, true, false,
713 handle_vector_size_attribute, false },
714 { "visibility", 1, 1, false, false, false,
715 handle_visibility_attribute, false },
716 { "tls_model", 1, 1, true, false, false,
717 handle_tls_model_attribute, false },
718 { "nonnull", 0, -1, false, true, true,
719 handle_nonnull_attribute, false },
720 { "nothrow", 0, 0, true, false, false,
721 handle_nothrow_attribute, false },
722 { "may_alias", 0, 0, false, true, false, NULL, false },
723 { "cleanup", 1, 1, true, false, false,
724 handle_cleanup_attribute, false },
725 { "warn_unused_result", 0, 0, false, true, true,
726 handle_warn_unused_result_attribute, false },
727 { "sentinel", 0, 1, false, true, true,
728 handle_sentinel_attribute, false },
729 /* For internal use (marking of builtins) only. The name contains space
730 to prevent its usage in source code. */
731 { "type generic", 0, 0, false, true, true,
732 handle_type_generic_attribute, false },
733 { "alloc_size", 1, 2, false, true, true,
734 handle_alloc_size_attribute, false },
735 { "cold", 0, 0, true, false, false,
736 handle_cold_attribute, false },
737 { "hot", 0, 0, true, false, false,
738 handle_hot_attribute, false },
739 { "no_address_safety_analysis",
740 0, 0, true, false, false,
741 handle_no_address_safety_analysis_attribute,
742 false },
743 { "no_sanitize_address", 0, 0, true, false, false,
744 handle_no_sanitize_address_attribute,
745 false },
746 { "no_sanitize_undefined", 0, 0, true, false, false,
747 handle_no_sanitize_undefined_attribute,
748 false },
749 { "warning", 1, 1, true, false, false,
750 handle_error_attribute, false },
751 { "error", 1, 1, true, false, false,
752 handle_error_attribute, false },
753 { "target", 1, -1, true, false, false,
754 handle_target_attribute, false },
755 { "optimize", 1, -1, true, false, false,
756 handle_optimize_attribute, false },
757 /* For internal use only. The leading '*' both prevents its usage in
758 source code and signals that it may be overridden by machine tables. */
759 { "*tm regparm", 0, 0, false, true, true,
760 ignore_attribute, false },
761 { "no_split_stack", 0, 0, true, false, false,
762 handle_no_split_stack_attribute, false },
763 /* For internal use (marking of builtins and runtime functions) only.
764 The name contains space to prevent its usage in source code. */
765 { "fn spec", 1, 1, false, true, true,
766 handle_fnspec_attribute, false },
767 { "warn_unused", 0, 0, false, false, false,
768 handle_warn_unused_attribute, false },
769 { "returns_nonnull", 0, 0, false, true, true,
770 handle_returns_nonnull_attribute, false },
771 { "omp declare simd", 0, -1, true, false, false,
772 handle_omp_declare_simd_attribute, false },
773 { "cilk simd function", 0, -1, true, false, false,
774 handle_omp_declare_simd_attribute, false },
775 { "omp declare target", 0, 0, true, false, false,
776 handle_omp_declare_target_attribute, false },
777 { "alloc_align", 1, 1, false, true, true,
778 handle_alloc_align_attribute, false },
779 { "assume_aligned", 1, 2, false, true, true,
780 handle_assume_aligned_attribute, false },
781 { NULL, 0, 0, false, false, false, NULL, false }
784 /* Give the specifications for the format attributes, used by C and all
785 descendants. */
787 const struct attribute_spec c_common_format_attribute_table[] =
789 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
790 affects_type_identity } */
791 { "format", 3, 3, false, true, true,
792 handle_format_attribute, false },
793 { "format_arg", 1, 1, false, true, true,
794 handle_format_arg_attribute, false },
795 { NULL, 0, 0, false, false, false, NULL, false }
798 /* Return identifier for address space AS. */
800 const char *
801 c_addr_space_name (addr_space_t as)
803 int rid = RID_FIRST_ADDR_SPACE + as;
804 gcc_assert (ridpointers [rid]);
805 return IDENTIFIER_POINTER (ridpointers [rid]);
808 /* Push current bindings for the function name VAR_DECLS. */
810 void
811 start_fname_decls (void)
813 unsigned ix;
814 tree saved = NULL_TREE;
816 for (ix = 0; fname_vars[ix].decl; ix++)
818 tree decl = *fname_vars[ix].decl;
820 if (decl)
822 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
823 saved);
824 *fname_vars[ix].decl = NULL_TREE;
827 if (saved || saved_function_name_decls)
828 /* Normally they'll have been NULL, so only push if we've got a
829 stack, or they are non-NULL. */
830 saved_function_name_decls = tree_cons (saved, NULL_TREE,
831 saved_function_name_decls);
834 /* Finish up the current bindings, adding them into the current function's
835 statement tree. This must be done _before_ finish_stmt_tree is called.
836 If there is no current function, we must be at file scope and no statements
837 are involved. Pop the previous bindings. */
839 void
840 finish_fname_decls (void)
842 unsigned ix;
843 tree stmts = NULL_TREE;
844 tree stack = saved_function_name_decls;
846 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
847 append_to_statement_list (TREE_VALUE (stack), &stmts);
849 if (stmts)
851 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
853 if (TREE_CODE (*bodyp) == BIND_EXPR)
854 bodyp = &BIND_EXPR_BODY (*bodyp);
856 append_to_statement_list_force (*bodyp, &stmts);
857 *bodyp = stmts;
860 for (ix = 0; fname_vars[ix].decl; ix++)
861 *fname_vars[ix].decl = NULL_TREE;
863 if (stack)
865 /* We had saved values, restore them. */
866 tree saved;
868 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
870 tree decl = TREE_PURPOSE (saved);
871 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
873 *fname_vars[ix].decl = decl;
875 stack = TREE_CHAIN (stack);
877 saved_function_name_decls = stack;
880 /* Return the text name of the current function, suitably prettified
881 by PRETTY_P. Return string must be freed by caller. */
883 const char *
884 fname_as_string (int pretty_p)
886 const char *name = "top level";
887 char *namep;
888 int vrb = 2, len;
889 cpp_string cstr = { 0, 0 }, strname;
891 if (!pretty_p)
893 name = "";
894 vrb = 0;
897 if (current_function_decl)
898 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
900 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
902 namep = XNEWVEC (char, len);
903 snprintf (namep, len, "\"%s\"", name);
904 strname.text = (unsigned char *) namep;
905 strname.len = len - 1;
907 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
909 XDELETEVEC (namep);
910 return (const char *) cstr.text;
913 return namep;
916 /* Return the VAR_DECL for a const char array naming the current
917 function. If the VAR_DECL has not yet been created, create it
918 now. RID indicates how it should be formatted and IDENTIFIER_NODE
919 ID is its name (unfortunately C and C++ hold the RID values of
920 keywords in different places, so we can't derive RID from ID in
921 this language independent code. LOC is the location of the
922 function. */
924 tree
925 fname_decl (location_t loc, unsigned int rid, tree id)
927 unsigned ix;
928 tree decl = NULL_TREE;
930 for (ix = 0; fname_vars[ix].decl; ix++)
931 if (fname_vars[ix].rid == rid)
932 break;
934 decl = *fname_vars[ix].decl;
935 if (!decl)
937 /* If a tree is built here, it would normally have the lineno of
938 the current statement. Later this tree will be moved to the
939 beginning of the function and this line number will be wrong.
940 To avoid this problem set the lineno to 0 here; that prevents
941 it from appearing in the RTL. */
942 tree stmts;
943 location_t saved_location = input_location;
944 input_location = UNKNOWN_LOCATION;
946 stmts = push_stmt_list ();
947 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
948 stmts = pop_stmt_list (stmts);
949 if (!IS_EMPTY_STMT (stmts))
950 saved_function_name_decls
951 = tree_cons (decl, stmts, saved_function_name_decls);
952 *fname_vars[ix].decl = decl;
953 input_location = saved_location;
955 if (!ix && !current_function_decl)
956 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
958 return decl;
961 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
963 tree
964 fix_string_type (tree value)
966 int length = TREE_STRING_LENGTH (value);
967 int nchars;
968 tree e_type, i_type, a_type;
970 /* Compute the number of elements, for the array type. */
971 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
973 nchars = length;
974 e_type = char_type_node;
976 else if (TREE_TYPE (value) == char16_array_type_node)
978 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
979 e_type = char16_type_node;
981 else if (TREE_TYPE (value) == char32_array_type_node)
983 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
984 e_type = char32_type_node;
986 else
988 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
989 e_type = wchar_type_node;
992 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
993 limit in C++98 Annex B is very large (65536) and is not normative,
994 so we do not diagnose it (warn_overlength_strings is forced off
995 in c_common_post_options). */
996 if (warn_overlength_strings)
998 const int nchars_max = flag_isoc99 ? 4095 : 509;
999 const int relevant_std = flag_isoc99 ? 99 : 90;
1000 if (nchars - 1 > nchars_max)
1001 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1002 separate the %d from the 'C'. 'ISO' should not be
1003 translated, but it may be moved after 'C%d' in languages
1004 where modifiers follow nouns. */
1005 pedwarn (input_location, OPT_Woverlength_strings,
1006 "string length %qd is greater than the length %qd "
1007 "ISO C%d compilers are required to support",
1008 nchars - 1, nchars_max, relevant_std);
1011 /* Create the array type for the string constant. The ISO C++
1012 standard says that a string literal has type `const char[N]' or
1013 `const wchar_t[N]'. We use the same logic when invoked as a C
1014 front-end with -Wwrite-strings.
1015 ??? We should change the type of an expression depending on the
1016 state of a warning flag. We should just be warning -- see how
1017 this is handled in the C++ front-end for the deprecated implicit
1018 conversion from string literals to `char*' or `wchar_t*'.
1020 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1021 array type being the unqualified version of that type.
1022 Therefore, if we are constructing an array of const char, we must
1023 construct the matching unqualified array type first. The C front
1024 end does not require this, but it does no harm, so we do it
1025 unconditionally. */
1026 i_type = build_index_type (size_int (nchars - 1));
1027 a_type = build_array_type (e_type, i_type);
1028 if (c_dialect_cxx() || warn_write_strings)
1029 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1031 TREE_TYPE (value) = a_type;
1032 TREE_CONSTANT (value) = 1;
1033 TREE_READONLY (value) = 1;
1034 TREE_STATIC (value) = 1;
1035 return value;
1038 /* If DISABLE is true, stop issuing warnings. This is used when
1039 parsing code that we know will not be executed. This function may
1040 be called multiple times, and works as a stack. */
1042 static void
1043 c_disable_warnings (bool disable)
1045 if (disable)
1047 ++c_inhibit_evaluation_warnings;
1048 fold_defer_overflow_warnings ();
1052 /* If ENABLE is true, reenable issuing warnings. */
1054 static void
1055 c_enable_warnings (bool enable)
1057 if (enable)
1059 --c_inhibit_evaluation_warnings;
1060 fold_undefer_and_ignore_overflow_warnings ();
1064 /* Fully fold EXPR, an expression that was not folded (beyond integer
1065 constant expressions and null pointer constants) when being built
1066 up. If IN_INIT, this is in a static initializer and certain
1067 changes are made to the folding done. Clear *MAYBE_CONST if
1068 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1069 expression because it contains an evaluated operator (in C99) or an
1070 operator outside of sizeof returning an integer constant (in C90)
1071 not permitted in constant expressions, or because it contains an
1072 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1073 set to true by callers before calling this function.) Return the
1074 folded expression. Function arguments have already been folded
1075 before calling this function, as have the contents of SAVE_EXPR,
1076 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1077 C_MAYBE_CONST_EXPR. */
1079 tree
1080 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1082 tree ret;
1083 tree eptype = NULL_TREE;
1084 bool dummy = true;
1085 bool maybe_const_itself = true;
1086 location_t loc = EXPR_LOCATION (expr);
1088 /* This function is not relevant to C++ because C++ folds while
1089 parsing, and may need changes to be correct for C++ when C++
1090 stops folding while parsing. */
1091 if (c_dialect_cxx ())
1092 gcc_unreachable ();
1094 if (!maybe_const)
1095 maybe_const = &dummy;
1096 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1098 eptype = TREE_TYPE (expr);
1099 expr = TREE_OPERAND (expr, 0);
1101 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1102 &maybe_const_itself);
1103 if (eptype)
1104 ret = fold_convert_loc (loc, eptype, ret);
1105 *maybe_const &= maybe_const_itself;
1106 return ret;
1109 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1110 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1111 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1112 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1113 both evaluated and unevaluated subexpressions while
1114 *MAYBE_CONST_ITSELF is carried from only evaluated
1115 subexpressions). */
1117 static tree
1118 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1119 bool *maybe_const_itself)
1121 tree ret = expr;
1122 enum tree_code code = TREE_CODE (expr);
1123 enum tree_code_class kind = TREE_CODE_CLASS (code);
1124 location_t loc = EXPR_LOCATION (expr);
1125 tree op0, op1, op2, op3;
1126 tree orig_op0, orig_op1, orig_op2;
1127 bool op0_const = true, op1_const = true, op2_const = true;
1128 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1129 bool nowarning = TREE_NO_WARNING (expr);
1130 bool unused_p;
1132 /* This function is not relevant to C++ because C++ folds while
1133 parsing, and may need changes to be correct for C++ when C++
1134 stops folding while parsing. */
1135 if (c_dialect_cxx ())
1136 gcc_unreachable ();
1138 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1139 anything else not counted as an expression cannot usefully be
1140 folded further at this point. */
1141 if (!IS_EXPR_CODE_CLASS (kind)
1142 || kind == tcc_statement
1143 || code == SAVE_EXPR)
1144 return expr;
1146 /* Operands of variable-length expressions (function calls) have
1147 already been folded, as have __builtin_* function calls, and such
1148 expressions cannot occur in constant expressions. */
1149 if (kind == tcc_vl_exp)
1151 *maybe_const_operands = false;
1152 ret = fold (expr);
1153 goto out;
1156 if (code == C_MAYBE_CONST_EXPR)
1158 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1159 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1160 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1161 *maybe_const_operands = false;
1162 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1163 *maybe_const_itself = false;
1164 if (pre && !in_init)
1165 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1166 else
1167 ret = inner;
1168 goto out;
1171 /* Assignment, increment, decrement, function call and comma
1172 operators, and statement expressions, cannot occur in constant
1173 expressions if evaluated / outside of sizeof. (Function calls
1174 were handled above, though VA_ARG_EXPR is treated like a function
1175 call here, and statement expressions are handled through
1176 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1177 switch (code)
1179 case MODIFY_EXPR:
1180 case PREDECREMENT_EXPR:
1181 case PREINCREMENT_EXPR:
1182 case POSTDECREMENT_EXPR:
1183 case POSTINCREMENT_EXPR:
1184 case COMPOUND_EXPR:
1185 *maybe_const_operands = false;
1186 break;
1188 case VA_ARG_EXPR:
1189 case TARGET_EXPR:
1190 case BIND_EXPR:
1191 case OBJ_TYPE_REF:
1192 *maybe_const_operands = false;
1193 ret = fold (expr);
1194 goto out;
1196 default:
1197 break;
1200 /* Fold individual tree codes as appropriate. */
1201 switch (code)
1203 case COMPOUND_LITERAL_EXPR:
1204 /* Any non-constancy will have been marked in a containing
1205 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1206 goto out;
1208 case COMPONENT_REF:
1209 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1210 op1 = TREE_OPERAND (expr, 1);
1211 op2 = TREE_OPERAND (expr, 2);
1212 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1213 maybe_const_itself);
1214 STRIP_TYPE_NOPS (op0);
1215 if (op0 != orig_op0)
1216 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1217 if (ret != expr)
1219 TREE_READONLY (ret) = TREE_READONLY (expr);
1220 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1222 goto out;
1224 case ARRAY_REF:
1225 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1226 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1227 op2 = TREE_OPERAND (expr, 2);
1228 op3 = TREE_OPERAND (expr, 3);
1229 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1230 maybe_const_itself);
1231 STRIP_TYPE_NOPS (op0);
1232 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1233 maybe_const_itself);
1234 STRIP_TYPE_NOPS (op1);
1235 op1 = decl_constant_value_for_optimization (op1);
1236 if (op0 != orig_op0 || op1 != orig_op1)
1237 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1238 if (ret != expr)
1240 TREE_READONLY (ret) = TREE_READONLY (expr);
1241 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1242 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1244 ret = fold (ret);
1245 goto out;
1247 case COMPOUND_EXPR:
1248 case MODIFY_EXPR:
1249 case PREDECREMENT_EXPR:
1250 case PREINCREMENT_EXPR:
1251 case POSTDECREMENT_EXPR:
1252 case POSTINCREMENT_EXPR:
1253 case PLUS_EXPR:
1254 case MINUS_EXPR:
1255 case MULT_EXPR:
1256 case POINTER_PLUS_EXPR:
1257 case TRUNC_DIV_EXPR:
1258 case CEIL_DIV_EXPR:
1259 case FLOOR_DIV_EXPR:
1260 case TRUNC_MOD_EXPR:
1261 case RDIV_EXPR:
1262 case EXACT_DIV_EXPR:
1263 case LSHIFT_EXPR:
1264 case RSHIFT_EXPR:
1265 case BIT_IOR_EXPR:
1266 case BIT_XOR_EXPR:
1267 case BIT_AND_EXPR:
1268 case LT_EXPR:
1269 case LE_EXPR:
1270 case GT_EXPR:
1271 case GE_EXPR:
1272 case EQ_EXPR:
1273 case NE_EXPR:
1274 case COMPLEX_EXPR:
1275 case TRUTH_AND_EXPR:
1276 case TRUTH_OR_EXPR:
1277 case TRUTH_XOR_EXPR:
1278 case UNORDERED_EXPR:
1279 case ORDERED_EXPR:
1280 case UNLT_EXPR:
1281 case UNLE_EXPR:
1282 case UNGT_EXPR:
1283 case UNGE_EXPR:
1284 case UNEQ_EXPR:
1285 /* Binary operations evaluating both arguments (increment and
1286 decrement are binary internally in GCC). */
1287 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1288 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1289 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1290 maybe_const_itself);
1291 STRIP_TYPE_NOPS (op0);
1292 if (code != MODIFY_EXPR
1293 && code != PREDECREMENT_EXPR
1294 && code != PREINCREMENT_EXPR
1295 && code != POSTDECREMENT_EXPR
1296 && code != POSTINCREMENT_EXPR)
1297 op0 = decl_constant_value_for_optimization (op0);
1298 /* The RHS of a MODIFY_EXPR was fully folded when building that
1299 expression for the sake of conversion warnings. */
1300 if (code != MODIFY_EXPR)
1301 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1302 maybe_const_itself);
1303 STRIP_TYPE_NOPS (op1);
1304 op1 = decl_constant_value_for_optimization (op1);
1305 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1306 ret = in_init
1307 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1308 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1309 else
1310 ret = fold (expr);
1311 if (TREE_OVERFLOW_P (ret)
1312 && !TREE_OVERFLOW_P (op0)
1313 && !TREE_OVERFLOW_P (op1))
1314 overflow_warning (EXPR_LOCATION (expr), ret);
1315 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1316 && TREE_CODE (orig_op1) != INTEGER_CST
1317 && TREE_CODE (op1) == INTEGER_CST
1318 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1319 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1320 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1321 && c_inhibit_evaluation_warnings == 0)
1323 if (tree_int_cst_sgn (op1) < 0)
1324 warning_at (loc, 0, (code == LSHIFT_EXPR
1325 ? G_("left shift count is negative")
1326 : G_("right shift count is negative")));
1327 else if (compare_tree_int (op1,
1328 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1329 >= 0)
1330 warning_at (loc, 0, (code == LSHIFT_EXPR
1331 ? G_("left shift count >= width of type")
1332 : G_("right shift count >= width of type")));
1334 goto out;
1336 case INDIRECT_REF:
1337 case FIX_TRUNC_EXPR:
1338 case FLOAT_EXPR:
1339 CASE_CONVERT:
1340 case ADDR_SPACE_CONVERT_EXPR:
1341 case VIEW_CONVERT_EXPR:
1342 case NON_LVALUE_EXPR:
1343 case NEGATE_EXPR:
1344 case BIT_NOT_EXPR:
1345 case TRUTH_NOT_EXPR:
1346 case ADDR_EXPR:
1347 case CONJ_EXPR:
1348 case REALPART_EXPR:
1349 case IMAGPART_EXPR:
1350 /* Unary operations. */
1351 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1352 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1353 maybe_const_itself);
1354 STRIP_TYPE_NOPS (op0);
1355 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1356 op0 = decl_constant_value_for_optimization (op0);
1357 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1358 not prepared to deal with them if they occur in initializers. */
1359 if (op0 != orig_op0
1360 && code == ADDR_EXPR
1361 && (op1 = get_base_address (op0)) != NULL_TREE
1362 && TREE_CODE (op1) == INDIRECT_REF
1363 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1364 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1365 else if (op0 != orig_op0 || in_init)
1366 ret = in_init
1367 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1368 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1369 else
1370 ret = fold (expr);
1371 if (code == INDIRECT_REF
1372 && ret != expr
1373 && TREE_CODE (ret) == INDIRECT_REF)
1375 TREE_READONLY (ret) = TREE_READONLY (expr);
1376 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1377 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1379 switch (code)
1381 case FIX_TRUNC_EXPR:
1382 case FLOAT_EXPR:
1383 CASE_CONVERT:
1384 /* Don't warn about explicit conversions. We will already
1385 have warned about suspect implicit conversions. */
1386 break;
1388 default:
1389 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1390 overflow_warning (EXPR_LOCATION (expr), ret);
1391 break;
1393 goto out;
1395 case TRUTH_ANDIF_EXPR:
1396 case TRUTH_ORIF_EXPR:
1397 /* Binary operations not necessarily evaluating both
1398 arguments. */
1399 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1400 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1401 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1402 STRIP_TYPE_NOPS (op0);
1404 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1405 ? truthvalue_false_node
1406 : truthvalue_true_node));
1407 c_disable_warnings (unused_p);
1408 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1409 STRIP_TYPE_NOPS (op1);
1410 c_enable_warnings (unused_p);
1412 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1413 ret = in_init
1414 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1415 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1416 else
1417 ret = fold (expr);
1418 *maybe_const_operands &= op0_const;
1419 *maybe_const_itself &= op0_const_self;
1420 if (!(flag_isoc99
1421 && op0_const
1422 && op0_const_self
1423 && (code == TRUTH_ANDIF_EXPR
1424 ? op0 == truthvalue_false_node
1425 : op0 == truthvalue_true_node)))
1426 *maybe_const_operands &= op1_const;
1427 if (!(op0_const
1428 && op0_const_self
1429 && (code == TRUTH_ANDIF_EXPR
1430 ? op0 == truthvalue_false_node
1431 : op0 == truthvalue_true_node)))
1432 *maybe_const_itself &= op1_const_self;
1433 goto out;
1435 case COND_EXPR:
1436 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1437 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1438 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1439 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1441 STRIP_TYPE_NOPS (op0);
1442 c_disable_warnings (op0 == truthvalue_false_node);
1443 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1444 STRIP_TYPE_NOPS (op1);
1445 c_enable_warnings (op0 == truthvalue_false_node);
1447 c_disable_warnings (op0 == truthvalue_true_node);
1448 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1449 STRIP_TYPE_NOPS (op2);
1450 c_enable_warnings (op0 == truthvalue_true_node);
1452 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1453 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1454 else
1455 ret = fold (expr);
1456 *maybe_const_operands &= op0_const;
1457 *maybe_const_itself &= op0_const_self;
1458 if (!(flag_isoc99
1459 && op0_const
1460 && op0_const_self
1461 && op0 == truthvalue_false_node))
1462 *maybe_const_operands &= op1_const;
1463 if (!(op0_const
1464 && op0_const_self
1465 && op0 == truthvalue_false_node))
1466 *maybe_const_itself &= op1_const_self;
1467 if (!(flag_isoc99
1468 && op0_const
1469 && op0_const_self
1470 && op0 == truthvalue_true_node))
1471 *maybe_const_operands &= op2_const;
1472 if (!(op0_const
1473 && op0_const_self
1474 && op0 == truthvalue_true_node))
1475 *maybe_const_itself &= op2_const_self;
1476 goto out;
1478 case EXCESS_PRECISION_EXPR:
1479 /* Each case where an operand with excess precision may be
1480 encountered must remove the EXCESS_PRECISION_EXPR around
1481 inner operands and possibly put one around the whole
1482 expression or possibly convert to the semantic type (which
1483 c_fully_fold does); we cannot tell at this stage which is
1484 appropriate in any particular case. */
1485 gcc_unreachable ();
1487 default:
1488 /* Various codes may appear through folding built-in functions
1489 and their arguments. */
1490 goto out;
1493 out:
1494 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1495 have been done by this point, so remove them again. */
1496 nowarning |= TREE_NO_WARNING (ret);
1497 STRIP_TYPE_NOPS (ret);
1498 if (nowarning && !TREE_NO_WARNING (ret))
1500 if (!CAN_HAVE_LOCATION_P (ret))
1501 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1502 TREE_NO_WARNING (ret) = 1;
1504 if (ret != expr)
1505 protected_set_expr_location (ret, loc);
1506 return ret;
1509 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1510 return EXP. Otherwise, return either EXP or its known constant
1511 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1512 Is the BLKmode test appropriate? */
1514 tree
1515 decl_constant_value_for_optimization (tree exp)
1517 tree ret;
1519 /* This function is only used by C, for c_fully_fold and other
1520 optimization, and may not be correct for C++. */
1521 if (c_dialect_cxx ())
1522 gcc_unreachable ();
1524 if (!optimize
1525 || TREE_CODE (exp) != VAR_DECL
1526 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1527 || DECL_MODE (exp) == BLKmode)
1528 return exp;
1530 ret = decl_constant_value (exp);
1531 /* Avoid unwanted tree sharing between the initializer and current
1532 function's body where the tree can be modified e.g. by the
1533 gimplifier. */
1534 if (ret != exp && TREE_STATIC (exp))
1535 ret = unshare_expr (ret);
1536 return ret;
1539 /* Print a warning if a constant expression had overflow in folding.
1540 Invoke this function on every expression that the language
1541 requires to be a constant expression.
1542 Note the ANSI C standard says it is erroneous for a
1543 constant expression to overflow. */
1545 void
1546 constant_expression_warning (tree value)
1548 if (warn_overflow && pedantic
1549 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1550 || TREE_CODE (value) == FIXED_CST
1551 || TREE_CODE (value) == VECTOR_CST
1552 || TREE_CODE (value) == COMPLEX_CST)
1553 && TREE_OVERFLOW (value))
1554 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1557 /* The same as above but print an unconditional error. */
1558 void
1559 constant_expression_error (tree value)
1561 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1562 || TREE_CODE (value) == FIXED_CST
1563 || TREE_CODE (value) == VECTOR_CST
1564 || TREE_CODE (value) == COMPLEX_CST)
1565 && TREE_OVERFLOW (value))
1566 error ("overflow in constant expression");
1569 /* Print a warning if an expression had overflow in folding and its
1570 operands hadn't.
1572 Invoke this function on every expression that
1573 (1) appears in the source code, and
1574 (2) is a constant expression that overflowed, and
1575 (3) is not already checked by convert_and_check;
1576 however, do not invoke this function on operands of explicit casts
1577 or when the expression is the result of an operator and any operand
1578 already overflowed. */
1580 void
1581 overflow_warning (location_t loc, tree value)
1583 if (c_inhibit_evaluation_warnings != 0)
1584 return;
1586 switch (TREE_CODE (value))
1588 case INTEGER_CST:
1589 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1590 break;
1592 case REAL_CST:
1593 warning_at (loc, OPT_Woverflow,
1594 "floating point overflow in expression");
1595 break;
1597 case FIXED_CST:
1598 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1599 break;
1601 case VECTOR_CST:
1602 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1603 break;
1605 case COMPLEX_CST:
1606 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1607 warning_at (loc, OPT_Woverflow,
1608 "complex integer overflow in expression");
1609 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1610 warning_at (loc, OPT_Woverflow,
1611 "complex floating point overflow in expression");
1612 break;
1614 default:
1615 break;
1619 /* Warn about uses of logical || / && operator in a context where it
1620 is likely that the bitwise equivalent was intended by the
1621 programmer. We have seen an expression in which CODE is a binary
1622 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1623 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1624 void
1625 warn_logical_operator (location_t location, enum tree_code code, tree type,
1626 enum tree_code code_left, tree op_left,
1627 enum tree_code ARG_UNUSED (code_right), tree op_right)
1629 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1630 int in0_p, in1_p, in_p;
1631 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1632 bool strict_overflow_p = false;
1634 if (code != TRUTH_ANDIF_EXPR
1635 && code != TRUTH_AND_EXPR
1636 && code != TRUTH_ORIF_EXPR
1637 && code != TRUTH_OR_EXPR)
1638 return;
1640 /* Warn if &&/|| are being used in a context where it is
1641 likely that the bitwise equivalent was intended by the
1642 programmer. That is, an expression such as op && MASK
1643 where op should not be any boolean expression, nor a
1644 constant, and mask seems to be a non-boolean integer constant. */
1645 if (!truth_value_p (code_left)
1646 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1647 && !CONSTANT_CLASS_P (op_left)
1648 && !TREE_NO_WARNING (op_left)
1649 && TREE_CODE (op_right) == INTEGER_CST
1650 && !integer_zerop (op_right)
1651 && !integer_onep (op_right))
1653 if (or_op)
1654 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1655 " applied to non-boolean constant");
1656 else
1657 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1658 " applied to non-boolean constant");
1659 TREE_NO_WARNING (op_left) = true;
1660 return;
1663 /* We do not warn for constants because they are typical of macro
1664 expansions that test for features. */
1665 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1666 return;
1668 /* This warning only makes sense with logical operands. */
1669 if (!(truth_value_p (TREE_CODE (op_left))
1670 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1671 || !(truth_value_p (TREE_CODE (op_right))
1672 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1673 return;
1676 /* We first test whether either side separately is trivially true
1677 (with OR) or trivially false (with AND). If so, do not warn.
1678 This is a common idiom for testing ranges of data types in
1679 portable code. */
1680 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1681 if (!lhs)
1682 return;
1683 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1684 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1686 /* If this is an OR operation, invert both sides; now, the result
1687 should be always false to get a warning. */
1688 if (or_op)
1689 in0_p = !in0_p;
1691 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1692 if (tem && integer_zerop (tem))
1693 return;
1695 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1696 if (!rhs)
1697 return;
1698 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1699 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1701 /* If this is an OR operation, invert both sides; now, the result
1702 should be always false to get a warning. */
1703 if (or_op)
1704 in1_p = !in1_p;
1706 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1707 if (tem && integer_zerop (tem))
1708 return;
1710 /* If both expressions have the same operand, if we can merge the
1711 ranges, and if the range test is always false, then warn. */
1712 if (operand_equal_p (lhs, rhs, 0)
1713 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1714 in1_p, low1, high1)
1715 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1716 type, lhs, in_p, low, high))
1717 && integer_zerop (tem))
1719 if (or_op)
1720 warning_at (location, OPT_Wlogical_op,
1721 "logical %<or%> "
1722 "of collectively exhaustive tests is always true");
1723 else
1724 warning_at (location, OPT_Wlogical_op,
1725 "logical %<and%> "
1726 "of mutually exclusive tests is always false");
1731 /* Warn if EXP contains any computations whose results are not used.
1732 Return true if a warning is printed; false otherwise. LOCUS is the
1733 (potential) location of the expression. */
1735 bool
1736 warn_if_unused_value (const_tree exp, location_t locus)
1738 restart:
1739 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1740 return false;
1742 /* Don't warn about void constructs. This includes casting to void,
1743 void function calls, and statement expressions with a final cast
1744 to void. */
1745 if (VOID_TYPE_P (TREE_TYPE (exp)))
1746 return false;
1748 if (EXPR_HAS_LOCATION (exp))
1749 locus = EXPR_LOCATION (exp);
1751 switch (TREE_CODE (exp))
1753 case PREINCREMENT_EXPR:
1754 case POSTINCREMENT_EXPR:
1755 case PREDECREMENT_EXPR:
1756 case POSTDECREMENT_EXPR:
1757 case MODIFY_EXPR:
1758 case INIT_EXPR:
1759 case TARGET_EXPR:
1760 case CALL_EXPR:
1761 case TRY_CATCH_EXPR:
1762 case WITH_CLEANUP_EXPR:
1763 case EXIT_EXPR:
1764 case VA_ARG_EXPR:
1765 return false;
1767 case BIND_EXPR:
1768 /* For a binding, warn if no side effect within it. */
1769 exp = BIND_EXPR_BODY (exp);
1770 goto restart;
1772 case SAVE_EXPR:
1773 case NON_LVALUE_EXPR:
1774 case NOP_EXPR:
1775 exp = TREE_OPERAND (exp, 0);
1776 goto restart;
1778 case TRUTH_ORIF_EXPR:
1779 case TRUTH_ANDIF_EXPR:
1780 /* In && or ||, warn if 2nd operand has no side effect. */
1781 exp = TREE_OPERAND (exp, 1);
1782 goto restart;
1784 case COMPOUND_EXPR:
1785 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1786 return true;
1787 /* Let people do `(foo (), 0)' without a warning. */
1788 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1789 return false;
1790 exp = TREE_OPERAND (exp, 1);
1791 goto restart;
1793 case COND_EXPR:
1794 /* If this is an expression with side effects, don't warn; this
1795 case commonly appears in macro expansions. */
1796 if (TREE_SIDE_EFFECTS (exp))
1797 return false;
1798 goto warn;
1800 case INDIRECT_REF:
1801 /* Don't warn about automatic dereferencing of references, since
1802 the user cannot control it. */
1803 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1805 exp = TREE_OPERAND (exp, 0);
1806 goto restart;
1808 /* Fall through. */
1810 default:
1811 /* Referencing a volatile value is a side effect, so don't warn. */
1812 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1813 && TREE_THIS_VOLATILE (exp))
1814 return false;
1816 /* If this is an expression which has no operands, there is no value
1817 to be unused. There are no such language-independent codes,
1818 but front ends may define such. */
1819 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1820 return false;
1822 warn:
1823 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1828 /* Print a warning about casts that might indicate violation
1829 of strict aliasing rules if -Wstrict-aliasing is used and
1830 strict aliasing mode is in effect. OTYPE is the original
1831 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1833 bool
1834 strict_aliasing_warning (tree otype, tree type, tree expr)
1836 /* Strip pointer conversion chains and get to the correct original type. */
1837 STRIP_NOPS (expr);
1838 otype = TREE_TYPE (expr);
1840 if (!(flag_strict_aliasing
1841 && POINTER_TYPE_P (type)
1842 && POINTER_TYPE_P (otype)
1843 && !VOID_TYPE_P (TREE_TYPE (type)))
1844 /* If the type we are casting to is a ref-all pointer
1845 dereferencing it is always valid. */
1846 || TYPE_REF_CAN_ALIAS_ALL (type))
1847 return false;
1849 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1850 && (DECL_P (TREE_OPERAND (expr, 0))
1851 || handled_component_p (TREE_OPERAND (expr, 0))))
1853 /* Casting the address of an object to non void pointer. Warn
1854 if the cast breaks type based aliasing. */
1855 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1857 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1858 "might break strict-aliasing rules");
1859 return true;
1861 else
1863 /* warn_strict_aliasing >= 3. This includes the default (3).
1864 Only warn if the cast is dereferenced immediately. */
1865 alias_set_type set1 =
1866 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1867 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1869 if (set1 != set2 && set2 != 0
1870 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1872 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1873 "pointer will break strict-aliasing rules");
1874 return true;
1876 else if (warn_strict_aliasing == 2
1877 && !alias_sets_must_conflict_p (set1, set2))
1879 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1880 "pointer might break strict-aliasing rules");
1881 return true;
1885 else
1886 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1888 /* At this level, warn for any conversions, even if an address is
1889 not taken in the same statement. This will likely produce many
1890 false positives, but could be useful to pinpoint problems that
1891 are not revealed at higher levels. */
1892 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1893 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1894 if (!COMPLETE_TYPE_P (type)
1895 || !alias_sets_must_conflict_p (set1, set2))
1897 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1898 "pointer might break strict-aliasing rules");
1899 return true;
1903 return false;
1906 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1907 sizeof as last operand of certain builtins. */
1909 void
1910 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1911 vec<tree, va_gc> *params, tree *sizeof_arg,
1912 bool (*comp_types) (tree, tree))
1914 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1915 bool strop = false, cmp = false;
1916 unsigned int idx = ~0;
1917 location_t loc;
1919 if (TREE_CODE (callee) != FUNCTION_DECL
1920 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1921 || vec_safe_length (params) <= 1)
1922 return;
1924 switch (DECL_FUNCTION_CODE (callee))
1926 case BUILT_IN_STRNCMP:
1927 case BUILT_IN_STRNCASECMP:
1928 cmp = true;
1929 /* FALLTHRU */
1930 case BUILT_IN_STRNCPY:
1931 case BUILT_IN_STRNCPY_CHK:
1932 case BUILT_IN_STRNCAT:
1933 case BUILT_IN_STRNCAT_CHK:
1934 case BUILT_IN_STPNCPY:
1935 case BUILT_IN_STPNCPY_CHK:
1936 strop = true;
1937 /* FALLTHRU */
1938 case BUILT_IN_MEMCPY:
1939 case BUILT_IN_MEMCPY_CHK:
1940 case BUILT_IN_MEMMOVE:
1941 case BUILT_IN_MEMMOVE_CHK:
1942 if (params->length () < 3)
1943 return;
1944 src = (*params)[1];
1945 dest = (*params)[0];
1946 idx = 2;
1947 break;
1948 case BUILT_IN_BCOPY:
1949 if (params->length () < 3)
1950 return;
1951 src = (*params)[0];
1952 dest = (*params)[1];
1953 idx = 2;
1954 break;
1955 case BUILT_IN_MEMCMP:
1956 case BUILT_IN_BCMP:
1957 if (params->length () < 3)
1958 return;
1959 src = (*params)[1];
1960 dest = (*params)[0];
1961 idx = 2;
1962 cmp = true;
1963 break;
1964 case BUILT_IN_MEMSET:
1965 case BUILT_IN_MEMSET_CHK:
1966 if (params->length () < 3)
1967 return;
1968 dest = (*params)[0];
1969 idx = 2;
1970 break;
1971 case BUILT_IN_BZERO:
1972 dest = (*params)[0];
1973 idx = 1;
1974 break;
1975 case BUILT_IN_STRNDUP:
1976 src = (*params)[0];
1977 strop = true;
1978 idx = 1;
1979 break;
1980 case BUILT_IN_MEMCHR:
1981 if (params->length () < 3)
1982 return;
1983 src = (*params)[0];
1984 idx = 2;
1985 break;
1986 case BUILT_IN_SNPRINTF:
1987 case BUILT_IN_SNPRINTF_CHK:
1988 case BUILT_IN_VSNPRINTF:
1989 case BUILT_IN_VSNPRINTF_CHK:
1990 dest = (*params)[0];
1991 idx = 1;
1992 strop = true;
1993 break;
1994 default:
1995 break;
1998 if (idx >= 3)
1999 return;
2001 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2002 return;
2004 type = TYPE_P (sizeof_arg[idx])
2005 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2006 if (!POINTER_TYPE_P (type))
2007 return;
2009 if (dest
2010 && (tem = tree_strip_nop_conversions (dest))
2011 && POINTER_TYPE_P (TREE_TYPE (tem))
2012 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2013 return;
2015 if (src
2016 && (tem = tree_strip_nop_conversions (src))
2017 && POINTER_TYPE_P (TREE_TYPE (tem))
2018 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2019 return;
2021 loc = sizeof_arg_loc[idx];
2023 if (dest && !cmp)
2025 if (!TYPE_P (sizeof_arg[idx])
2026 && operand_equal_p (dest, sizeof_arg[idx], 0)
2027 && comp_types (TREE_TYPE (dest), type))
2029 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2030 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2031 "argument to %<sizeof%> in %qD call is the same "
2032 "expression as the destination; did you mean to "
2033 "remove the addressof?", callee);
2034 else if ((TYPE_PRECISION (TREE_TYPE (type))
2035 == TYPE_PRECISION (char_type_node))
2036 || strop)
2037 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2038 "argument to %<sizeof%> in %qD call is the same "
2039 "expression as the destination; did you mean to "
2040 "provide an explicit length?", callee);
2041 else
2042 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2043 "argument to %<sizeof%> in %qD call is the same "
2044 "expression as the destination; did you mean to "
2045 "dereference it?", callee);
2046 return;
2049 if (POINTER_TYPE_P (TREE_TYPE (dest))
2050 && !strop
2051 && comp_types (TREE_TYPE (dest), type)
2052 && !VOID_TYPE_P (TREE_TYPE (type)))
2054 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2055 "argument to %<sizeof%> in %qD call is the same "
2056 "pointer type %qT as the destination; expected %qT "
2057 "or an explicit length", callee, TREE_TYPE (dest),
2058 TREE_TYPE (TREE_TYPE (dest)));
2059 return;
2063 if (src && !cmp)
2065 if (!TYPE_P (sizeof_arg[idx])
2066 && operand_equal_p (src, sizeof_arg[idx], 0)
2067 && comp_types (TREE_TYPE (src), type))
2069 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2070 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2071 "argument to %<sizeof%> in %qD call is the same "
2072 "expression as the source; did you mean to "
2073 "remove the addressof?", callee);
2074 else if ((TYPE_PRECISION (TREE_TYPE (type))
2075 == TYPE_PRECISION (char_type_node))
2076 || strop)
2077 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2078 "argument to %<sizeof%> in %qD call is the same "
2079 "expression as the source; did you mean to "
2080 "provide an explicit length?", callee);
2081 else
2082 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2083 "argument to %<sizeof%> in %qD call is the same "
2084 "expression as the source; did you mean to "
2085 "dereference it?", callee);
2086 return;
2089 if (POINTER_TYPE_P (TREE_TYPE (src))
2090 && !strop
2091 && comp_types (TREE_TYPE (src), type)
2092 && !VOID_TYPE_P (TREE_TYPE (type)))
2094 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2095 "argument to %<sizeof%> in %qD call is the same "
2096 "pointer type %qT as the source; expected %qT "
2097 "or an explicit length", callee, TREE_TYPE (src),
2098 TREE_TYPE (TREE_TYPE (src)));
2099 return;
2103 if (dest)
2105 if (!TYPE_P (sizeof_arg[idx])
2106 && operand_equal_p (dest, sizeof_arg[idx], 0)
2107 && comp_types (TREE_TYPE (dest), type))
2109 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2110 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2111 "argument to %<sizeof%> in %qD call is the same "
2112 "expression as the first source; did you mean to "
2113 "remove the addressof?", callee);
2114 else if ((TYPE_PRECISION (TREE_TYPE (type))
2115 == TYPE_PRECISION (char_type_node))
2116 || strop)
2117 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2118 "argument to %<sizeof%> in %qD call is the same "
2119 "expression as the first source; did you mean to "
2120 "provide an explicit length?", callee);
2121 else
2122 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2123 "argument to %<sizeof%> in %qD call is the same "
2124 "expression as the first source; did you mean to "
2125 "dereference it?", callee);
2126 return;
2129 if (POINTER_TYPE_P (TREE_TYPE (dest))
2130 && !strop
2131 && comp_types (TREE_TYPE (dest), type)
2132 && !VOID_TYPE_P (TREE_TYPE (type)))
2134 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2135 "argument to %<sizeof%> in %qD call is the same "
2136 "pointer type %qT as the first source; expected %qT "
2137 "or an explicit length", callee, TREE_TYPE (dest),
2138 TREE_TYPE (TREE_TYPE (dest)));
2139 return;
2143 if (src)
2145 if (!TYPE_P (sizeof_arg[idx])
2146 && operand_equal_p (src, sizeof_arg[idx], 0)
2147 && comp_types (TREE_TYPE (src), type))
2149 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2150 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2151 "argument to %<sizeof%> in %qD call is the same "
2152 "expression as the second source; did you mean to "
2153 "remove the addressof?", callee);
2154 else if ((TYPE_PRECISION (TREE_TYPE (type))
2155 == TYPE_PRECISION (char_type_node))
2156 || strop)
2157 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2158 "argument to %<sizeof%> in %qD call is the same "
2159 "expression as the second source; did you mean to "
2160 "provide an explicit length?", callee);
2161 else
2162 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2163 "argument to %<sizeof%> in %qD call is the same "
2164 "expression as the second source; did you mean to "
2165 "dereference it?", callee);
2166 return;
2169 if (POINTER_TYPE_P (TREE_TYPE (src))
2170 && !strop
2171 && comp_types (TREE_TYPE (src), type)
2172 && !VOID_TYPE_P (TREE_TYPE (type)))
2174 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2175 "argument to %<sizeof%> in %qD call is the same "
2176 "pointer type %qT as the second source; expected %qT "
2177 "or an explicit length", callee, TREE_TYPE (src),
2178 TREE_TYPE (TREE_TYPE (src)));
2179 return;
2185 /* Warn for unlikely, improbable, or stupid DECL declarations
2186 of `main'. */
2188 void
2189 check_main_parameter_types (tree decl)
2191 function_args_iterator iter;
2192 tree type;
2193 int argct = 0;
2195 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2197 /* XXX void_type_node belies the abstraction. */
2198 if (type == void_type_node || type == error_mark_node )
2199 break;
2201 ++argct;
2202 switch (argct)
2204 case 1:
2205 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2206 pedwarn (input_location, OPT_Wmain,
2207 "first argument of %q+D should be %<int%>", decl);
2208 break;
2210 case 2:
2211 if (TREE_CODE (type) != POINTER_TYPE
2212 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2213 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2214 != char_type_node))
2215 pedwarn (input_location, OPT_Wmain,
2216 "second argument of %q+D should be %<char **%>", decl);
2217 break;
2219 case 3:
2220 if (TREE_CODE (type) != POINTER_TYPE
2221 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2222 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2223 != char_type_node))
2224 pedwarn (input_location, OPT_Wmain,
2225 "third argument of %q+D should probably be "
2226 "%<char **%>", decl);
2227 break;
2231 /* It is intentional that this message does not mention the third
2232 argument because it's only mentioned in an appendix of the
2233 standard. */
2234 if (argct > 0 && (argct < 2 || argct > 3))
2235 pedwarn (input_location, OPT_Wmain,
2236 "%q+D takes only zero or two arguments", decl);
2239 /* vector_targets_convertible_p is used for vector pointer types. The
2240 callers perform various checks that the qualifiers are satisfactory,
2241 while OTOH vector_targets_convertible_p ignores the number of elements
2242 in the vectors. That's fine with vector pointers as we can consider,
2243 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2244 and that does not require and conversion of the pointer values.
2245 In contrast, vector_types_convertible_p and
2246 vector_types_compatible_elements_p are used for vector value types. */
2247 /* True if pointers to distinct types T1 and T2 can be converted to
2248 each other without an explicit cast. Only returns true for opaque
2249 vector types. */
2250 bool
2251 vector_targets_convertible_p (const_tree t1, const_tree t2)
2253 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2254 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2255 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2256 return true;
2258 return false;
2261 /* vector_types_convertible_p is used for vector value types.
2262 It could in principle call vector_targets_convertible_p as a subroutine,
2263 but then the check for vector type would be duplicated with its callers,
2264 and also the purpose of vector_targets_convertible_p would become
2265 muddled.
2266 Where vector_types_convertible_p returns true, a conversion might still be
2267 needed to make the types match.
2268 In contrast, vector_targets_convertible_p is used for vector pointer
2269 values, and vector_types_compatible_elements_p is used specifically
2270 in the context for binary operators, as a check if use is possible without
2271 conversion. */
2272 /* True if vector types T1 and T2 can be converted to each other
2273 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2274 can only be converted with -flax-vector-conversions yet that is not
2275 in effect, emit a note telling the user about that option if such
2276 a note has not previously been emitted. */
2277 bool
2278 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2280 static bool emitted_lax_note = false;
2281 bool convertible_lax;
2283 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2284 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2285 return true;
2287 convertible_lax =
2288 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2289 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2290 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2291 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2292 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2294 if (!convertible_lax || flag_lax_vector_conversions)
2295 return convertible_lax;
2297 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2298 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2299 return true;
2301 if (emit_lax_note && !emitted_lax_note)
2303 emitted_lax_note = true;
2304 inform (input_location, "use -flax-vector-conversions to permit "
2305 "conversions between vectors with differing "
2306 "element types or numbers of subparts");
2309 return false;
2312 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2313 and have vector types, V0 has the same type as V1, and the number of
2314 elements of V0, V1, MASK is the same.
2316 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2317 called with two arguments. In this case implementation passes the
2318 first argument twice in order to share the same tree code. This fact
2319 could enable the mask-values being twice the vector length. This is
2320 an implementation accident and this semantics is not guaranteed to
2321 the user. */
2322 tree
2323 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2324 bool complain)
2326 tree ret;
2327 bool wrap = true;
2328 bool maybe_const = false;
2329 bool two_arguments = false;
2331 if (v1 == NULL_TREE)
2333 two_arguments = true;
2334 v1 = v0;
2337 if (v0 == error_mark_node || v1 == error_mark_node
2338 || mask == error_mark_node)
2339 return error_mark_node;
2341 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2342 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2344 if (complain)
2345 error_at (loc, "__builtin_shuffle last argument must "
2346 "be an integer vector");
2347 return error_mark_node;
2350 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2351 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2353 if (complain)
2354 error_at (loc, "__builtin_shuffle arguments must be vectors");
2355 return error_mark_node;
2358 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2360 if (complain)
2361 error_at (loc, "__builtin_shuffle argument vectors must be of "
2362 "the same type");
2363 return error_mark_node;
2366 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2367 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2368 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2369 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2371 if (complain)
2372 error_at (loc, "__builtin_shuffle number of elements of the "
2373 "argument vector(s) and the mask vector should "
2374 "be the same");
2375 return error_mark_node;
2378 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2379 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2381 if (complain)
2382 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2383 "must have the same size as inner type of the mask");
2384 return error_mark_node;
2387 if (!c_dialect_cxx ())
2389 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2390 v0 = c_fully_fold (v0, false, &maybe_const);
2391 wrap &= maybe_const;
2393 if (two_arguments)
2394 v1 = v0 = save_expr (v0);
2395 else
2397 v1 = c_fully_fold (v1, false, &maybe_const);
2398 wrap &= maybe_const;
2401 mask = c_fully_fold (mask, false, &maybe_const);
2402 wrap &= maybe_const;
2404 else if (two_arguments)
2405 v1 = v0 = save_expr (v0);
2407 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2409 if (!c_dialect_cxx () && !wrap)
2410 ret = c_wrap_maybe_const (ret, true);
2412 return ret;
2415 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2416 to integral type. */
2418 static tree
2419 c_common_get_narrower (tree op, int *unsignedp_ptr)
2421 op = get_narrower (op, unsignedp_ptr);
2423 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2424 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2426 /* C++0x scoped enumerations don't implicitly convert to integral
2427 type; if we stripped an explicit conversion to a larger type we
2428 need to replace it so common_type will still work. */
2429 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2430 TYPE_UNSIGNED (TREE_TYPE (op)));
2431 op = fold_convert (type, op);
2433 return op;
2436 /* This is a helper function of build_binary_op.
2438 For certain operations if both args were extended from the same
2439 smaller type, do the arithmetic in that type and then extend.
2441 BITWISE indicates a bitwise operation.
2442 For them, this optimization is safe only if
2443 both args are zero-extended or both are sign-extended.
2444 Otherwise, we might change the result.
2445 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2446 but calculated in (unsigned short) it would be (unsigned short)-1.
2448 tree
2449 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2451 int unsigned0, unsigned1;
2452 tree arg0, arg1;
2453 int uns;
2454 tree type;
2456 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2457 excessive narrowing when we call get_narrower below. For
2458 example, suppose that OP0 is of unsigned int extended
2459 from signed char and that RESULT_TYPE is long long int.
2460 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2461 like
2463 (long long int) (unsigned int) signed_char
2465 which get_narrower would narrow down to
2467 (unsigned int) signed char
2469 If we do not cast OP0 first, get_narrower would return
2470 signed_char, which is inconsistent with the case of the
2471 explicit cast. */
2472 op0 = convert (result_type, op0);
2473 op1 = convert (result_type, op1);
2475 arg0 = c_common_get_narrower (op0, &unsigned0);
2476 arg1 = c_common_get_narrower (op1, &unsigned1);
2478 /* UNS is 1 if the operation to be done is an unsigned one. */
2479 uns = TYPE_UNSIGNED (result_type);
2481 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2482 but it *requires* conversion to FINAL_TYPE. */
2484 if ((TYPE_PRECISION (TREE_TYPE (op0))
2485 == TYPE_PRECISION (TREE_TYPE (arg0)))
2486 && TREE_TYPE (op0) != result_type)
2487 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2488 if ((TYPE_PRECISION (TREE_TYPE (op1))
2489 == TYPE_PRECISION (TREE_TYPE (arg1)))
2490 && TREE_TYPE (op1) != result_type)
2491 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2493 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2495 /* For bitwise operations, signedness of nominal type
2496 does not matter. Consider only how operands were extended. */
2497 if (bitwise)
2498 uns = unsigned0;
2500 /* Note that in all three cases below we refrain from optimizing
2501 an unsigned operation on sign-extended args.
2502 That would not be valid. */
2504 /* Both args variable: if both extended in same way
2505 from same width, do it in that width.
2506 Do it unsigned if args were zero-extended. */
2507 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2508 < TYPE_PRECISION (result_type))
2509 && (TYPE_PRECISION (TREE_TYPE (arg1))
2510 == TYPE_PRECISION (TREE_TYPE (arg0)))
2511 && unsigned0 == unsigned1
2512 && (unsigned0 || !uns))
2513 return c_common_signed_or_unsigned_type
2514 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2516 else if (TREE_CODE (arg0) == INTEGER_CST
2517 && (unsigned1 || !uns)
2518 && (TYPE_PRECISION (TREE_TYPE (arg1))
2519 < TYPE_PRECISION (result_type))
2520 && (type
2521 = c_common_signed_or_unsigned_type (unsigned1,
2522 TREE_TYPE (arg1)))
2523 && !POINTER_TYPE_P (type)
2524 && int_fits_type_p (arg0, type))
2525 return type;
2527 else if (TREE_CODE (arg1) == INTEGER_CST
2528 && (unsigned0 || !uns)
2529 && (TYPE_PRECISION (TREE_TYPE (arg0))
2530 < TYPE_PRECISION (result_type))
2531 && (type
2532 = c_common_signed_or_unsigned_type (unsigned0,
2533 TREE_TYPE (arg0)))
2534 && !POINTER_TYPE_P (type)
2535 && int_fits_type_p (arg1, type))
2536 return type;
2538 return result_type;
2541 /* Checks if expression EXPR of real/integer type cannot be converted
2542 to the real/integer type TYPE. Function returns non-zero when:
2543 * EXPR is a constant which cannot be exactly converted to TYPE.
2544 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2545 for EXPR type and TYPE being both integers or both real.
2546 * EXPR is not a constant of real type and TYPE is an integer.
2547 * EXPR is not a constant of integer type which cannot be
2548 exactly converted to real type.
2549 Function allows conversions between types of different signedness and
2550 can return SAFE_CONVERSION (zero) in that case. Function can produce
2551 signedness warnings if PRODUCE_WARNS is true. */
2553 enum conversion_safety
2554 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2556 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2557 tree expr_type = TREE_TYPE (expr);
2558 loc = expansion_point_location_if_in_system_header (loc);
2560 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2562 /* Warn for real constant that is not an exact integer converted
2563 to integer type. */
2564 if (TREE_CODE (expr_type) == REAL_TYPE
2565 && TREE_CODE (type) == INTEGER_TYPE)
2567 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2568 give_warning = UNSAFE_REAL;
2570 /* Warn for an integer constant that does not fit into integer type. */
2571 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2572 && TREE_CODE (type) == INTEGER_TYPE
2573 && !int_fits_type_p (expr, type))
2575 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2576 && tree_int_cst_sgn (expr) < 0)
2578 if (produce_warns)
2579 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2580 " implicitly converted to unsigned type");
2582 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2584 if (produce_warns)
2585 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2586 " constant value to negative integer");
2588 else
2589 give_warning = UNSAFE_OTHER;
2591 else if (TREE_CODE (type) == REAL_TYPE)
2593 /* Warn for an integer constant that does not fit into real type. */
2594 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2596 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2597 if (!exact_real_truncate (TYPE_MODE (type), &a))
2598 give_warning = UNSAFE_REAL;
2600 /* Warn for a real constant that does not fit into a smaller
2601 real type. */
2602 else if (TREE_CODE (expr_type) == REAL_TYPE
2603 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2605 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2606 if (!exact_real_truncate (TYPE_MODE (type), &a))
2607 give_warning = UNSAFE_REAL;
2611 else
2613 /* Warn for real types converted to integer types. */
2614 if (TREE_CODE (expr_type) == REAL_TYPE
2615 && TREE_CODE (type) == INTEGER_TYPE)
2616 give_warning = UNSAFE_REAL;
2618 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2619 && TREE_CODE (type) == INTEGER_TYPE)
2621 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2622 expr = get_unwidened (expr, 0);
2623 expr_type = TREE_TYPE (expr);
2625 /* Don't warn for short y; short x = ((int)y & 0xff); */
2626 if (TREE_CODE (expr) == BIT_AND_EXPR
2627 || TREE_CODE (expr) == BIT_IOR_EXPR
2628 || TREE_CODE (expr) == BIT_XOR_EXPR)
2630 /* If both args were extended from a shortest type,
2631 use that type if that is safe. */
2632 expr_type = shorten_binary_op (expr_type,
2633 TREE_OPERAND (expr, 0),
2634 TREE_OPERAND (expr, 1),
2635 /* bitwise */1);
2637 if (TREE_CODE (expr) == BIT_AND_EXPR)
2639 tree op0 = TREE_OPERAND (expr, 0);
2640 tree op1 = TREE_OPERAND (expr, 1);
2641 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2642 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2644 /* If one of the operands is a non-negative constant
2645 that fits in the target type, then the type of the
2646 other operand does not matter. */
2647 if ((TREE_CODE (op0) == INTEGER_CST
2648 && int_fits_type_p (op0, c_common_signed_type (type))
2649 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2650 || (TREE_CODE (op1) == INTEGER_CST
2651 && int_fits_type_p (op1, c_common_signed_type (type))
2652 && int_fits_type_p (op1,
2653 c_common_unsigned_type (type))))
2654 return SAFE_CONVERSION;
2655 /* If constant is unsigned and fits in the target
2656 type, then the result will also fit. */
2657 else if ((TREE_CODE (op0) == INTEGER_CST
2658 && unsigned0
2659 && int_fits_type_p (op0, type))
2660 || (TREE_CODE (op1) == INTEGER_CST
2661 && unsigned1
2662 && int_fits_type_p (op1, type)))
2663 return SAFE_CONVERSION;
2666 /* Warn for integer types converted to smaller integer types. */
2667 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2668 give_warning = UNSAFE_OTHER;
2670 /* When they are the same width but different signedness,
2671 then the value may change. */
2672 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2673 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2674 /* Even when converted to a bigger type, if the type is
2675 unsigned but expr is signed, then negative values
2676 will be changed. */
2677 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2678 && produce_warns)
2679 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2680 "may change the sign of the result",
2681 type, expr_type);
2684 /* Warn for integer types converted to real types if and only if
2685 all the range of values of the integer type cannot be
2686 represented by the real type. */
2687 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2688 && TREE_CODE (type) == REAL_TYPE)
2690 tree type_low_bound, type_high_bound;
2691 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2693 /* Don't warn about char y = 0xff; float x = (int) y; */
2694 expr = get_unwidened (expr, 0);
2695 expr_type = TREE_TYPE (expr);
2697 type_low_bound = TYPE_MIN_VALUE (expr_type);
2698 type_high_bound = TYPE_MAX_VALUE (expr_type);
2699 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2700 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2702 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2703 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2704 give_warning = UNSAFE_OTHER;
2707 /* Warn for real types converted to smaller real types. */
2708 else if (TREE_CODE (expr_type) == REAL_TYPE
2709 && TREE_CODE (type) == REAL_TYPE
2710 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2711 give_warning = UNSAFE_REAL;
2714 return give_warning;
2717 /* Warns if the conversion of EXPR to TYPE may alter a value.
2718 This is a helper function for warnings_for_convert_and_check. */
2720 static void
2721 conversion_warning (location_t loc, tree type, tree expr)
2723 tree expr_type = TREE_TYPE (expr);
2724 enum conversion_safety conversion_kind;
2726 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2727 return;
2729 /* This may happen, because for LHS op= RHS we preevaluate
2730 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2731 means we could no longer see the code of the EXPR. */
2732 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2733 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2734 if (TREE_CODE (expr) == SAVE_EXPR)
2735 expr = TREE_OPERAND (expr, 0);
2737 switch (TREE_CODE (expr))
2739 case EQ_EXPR:
2740 case NE_EXPR:
2741 case LE_EXPR:
2742 case GE_EXPR:
2743 case LT_EXPR:
2744 case GT_EXPR:
2745 case TRUTH_ANDIF_EXPR:
2746 case TRUTH_ORIF_EXPR:
2747 case TRUTH_AND_EXPR:
2748 case TRUTH_OR_EXPR:
2749 case TRUTH_XOR_EXPR:
2750 case TRUTH_NOT_EXPR:
2751 /* Conversion from boolean to a signed:1 bit-field (which only
2752 can hold the values 0 and -1) doesn't lose information - but
2753 it does change the value. */
2754 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2755 warning_at (loc, OPT_Wconversion,
2756 "conversion to %qT from boolean expression", type);
2757 return;
2759 case REAL_CST:
2760 case INTEGER_CST:
2761 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2762 if (conversion_kind == UNSAFE_REAL)
2763 warning_at (loc, OPT_Wfloat_conversion,
2764 "conversion to %qT alters %qT constant value",
2765 type, expr_type);
2766 else if (conversion_kind)
2767 warning_at (loc, OPT_Wconversion,
2768 "conversion to %qT alters %qT constant value",
2769 type, expr_type);
2770 return;
2772 case COND_EXPR:
2774 /* In case of COND_EXPR, we do not care about the type of
2775 COND_EXPR, only about the conversion of each operand. */
2776 tree op1 = TREE_OPERAND (expr, 1);
2777 tree op2 = TREE_OPERAND (expr, 2);
2779 conversion_warning (loc, type, op1);
2780 conversion_warning (loc, type, op2);
2781 return;
2784 default: /* 'expr' is not a constant. */
2785 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2786 if (conversion_kind == UNSAFE_REAL)
2787 warning_at (loc, OPT_Wfloat_conversion,
2788 "conversion to %qT from %qT may alter its value",
2789 type, expr_type);
2790 else if (conversion_kind)
2791 warning_at (loc, OPT_Wconversion,
2792 "conversion to %qT from %qT may alter its value",
2793 type, expr_type);
2797 /* Produce warnings after a conversion. RESULT is the result of
2798 converting EXPR to TYPE. This is a helper function for
2799 convert_and_check and cp_convert_and_check. */
2801 void
2802 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2803 tree result)
2805 loc = expansion_point_location_if_in_system_header (loc);
2807 if (TREE_CODE (expr) == INTEGER_CST
2808 && (TREE_CODE (type) == INTEGER_TYPE
2809 || TREE_CODE (type) == ENUMERAL_TYPE)
2810 && !int_fits_type_p (expr, type))
2812 /* Do not diagnose overflow in a constant expression merely
2813 because a conversion overflowed. */
2814 if (TREE_OVERFLOW (result))
2815 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2817 if (TYPE_UNSIGNED (type))
2819 /* This detects cases like converting -129 or 256 to
2820 unsigned char. */
2821 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2822 warning_at (loc, OPT_Woverflow,
2823 "large integer implicitly truncated to unsigned type");
2824 else
2825 conversion_warning (loc, type, expr);
2827 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2828 warning_at (loc, OPT_Woverflow,
2829 "overflow in implicit constant conversion");
2830 /* No warning for converting 0x80000000 to int. */
2831 else if (pedantic
2832 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2833 || TYPE_PRECISION (TREE_TYPE (expr))
2834 != TYPE_PRECISION (type)))
2835 warning_at (loc, OPT_Woverflow,
2836 "overflow in implicit constant conversion");
2838 else
2839 conversion_warning (loc, type, expr);
2841 else if ((TREE_CODE (result) == INTEGER_CST
2842 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2843 warning_at (loc, OPT_Woverflow,
2844 "overflow in implicit constant conversion");
2845 else
2846 conversion_warning (loc, type, expr);
2850 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2851 Invoke this function on every expression that is converted implicitly,
2852 i.e. because of language rules and not because of an explicit cast. */
2854 tree
2855 convert_and_check (location_t loc, tree type, tree expr)
2857 tree result;
2858 tree expr_for_warning;
2860 /* Convert from a value with possible excess precision rather than
2861 via the semantic type, but do not warn about values not fitting
2862 exactly in the semantic type. */
2863 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2865 tree orig_type = TREE_TYPE (expr);
2866 expr = TREE_OPERAND (expr, 0);
2867 expr_for_warning = convert (orig_type, expr);
2868 if (orig_type == type)
2869 return expr_for_warning;
2871 else
2872 expr_for_warning = expr;
2874 if (TREE_TYPE (expr) == type)
2875 return expr;
2877 result = convert (type, expr);
2879 if (c_inhibit_evaluation_warnings == 0
2880 && !TREE_OVERFLOW_P (expr)
2881 && result != error_mark_node)
2882 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2884 return result;
2887 /* A node in a list that describes references to variables (EXPR), which are
2888 either read accesses if WRITER is zero, or write accesses, in which case
2889 WRITER is the parent of EXPR. */
2890 struct tlist
2892 struct tlist *next;
2893 tree expr, writer;
2896 /* Used to implement a cache the results of a call to verify_tree. We only
2897 use this for SAVE_EXPRs. */
2898 struct tlist_cache
2900 struct tlist_cache *next;
2901 struct tlist *cache_before_sp;
2902 struct tlist *cache_after_sp;
2903 tree expr;
2906 /* Obstack to use when allocating tlist structures, and corresponding
2907 firstobj. */
2908 static struct obstack tlist_obstack;
2909 static char *tlist_firstobj = 0;
2911 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2912 warnings. */
2913 static struct tlist *warned_ids;
2914 /* SAVE_EXPRs need special treatment. We process them only once and then
2915 cache the results. */
2916 static struct tlist_cache *save_expr_cache;
2918 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2919 static void merge_tlist (struct tlist **, struct tlist *, int);
2920 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2921 static int warning_candidate_p (tree);
2922 static bool candidate_equal_p (const_tree, const_tree);
2923 static void warn_for_collisions (struct tlist *);
2924 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2925 static struct tlist *new_tlist (struct tlist *, tree, tree);
2927 /* Create a new struct tlist and fill in its fields. */
2928 static struct tlist *
2929 new_tlist (struct tlist *next, tree t, tree writer)
2931 struct tlist *l;
2932 l = XOBNEW (&tlist_obstack, struct tlist);
2933 l->next = next;
2934 l->expr = t;
2935 l->writer = writer;
2936 return l;
2939 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2940 is nonnull, we ignore any node we find which has a writer equal to it. */
2942 static void
2943 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2945 while (add)
2947 struct tlist *next = add->next;
2948 if (!copy)
2949 add->next = *to;
2950 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2951 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2952 add = next;
2956 /* Merge the nodes of ADD into TO. This merging process is done so that for
2957 each variable that already exists in TO, no new node is added; however if
2958 there is a write access recorded in ADD, and an occurrence on TO is only
2959 a read access, then the occurrence in TO will be modified to record the
2960 write. */
2962 static void
2963 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2965 struct tlist **end = to;
2967 while (*end)
2968 end = &(*end)->next;
2970 while (add)
2972 int found = 0;
2973 struct tlist *tmp2;
2974 struct tlist *next = add->next;
2976 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2977 if (candidate_equal_p (tmp2->expr, add->expr))
2979 found = 1;
2980 if (!tmp2->writer)
2981 tmp2->writer = add->writer;
2983 if (!found)
2985 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
2986 end = &(*end)->next;
2987 *end = 0;
2989 add = next;
2993 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2994 references in list LIST conflict with it, excluding reads if ONLY writers
2995 is nonzero. */
2997 static void
2998 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2999 int only_writes)
3001 struct tlist *tmp;
3003 /* Avoid duplicate warnings. */
3004 for (tmp = warned_ids; tmp; tmp = tmp->next)
3005 if (candidate_equal_p (tmp->expr, written))
3006 return;
3008 while (list)
3010 if (candidate_equal_p (list->expr, written)
3011 && !candidate_equal_p (list->writer, writer)
3012 && (!only_writes || list->writer))
3014 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3015 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3016 OPT_Wsequence_point, "operation on %qE may be undefined",
3017 list->expr);
3019 list = list->next;
3023 /* Given a list LIST of references to variables, find whether any of these
3024 can cause conflicts due to missing sequence points. */
3026 static void
3027 warn_for_collisions (struct tlist *list)
3029 struct tlist *tmp;
3031 for (tmp = list; tmp; tmp = tmp->next)
3033 if (tmp->writer)
3034 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3038 /* Return nonzero if X is a tree that can be verified by the sequence point
3039 warnings. */
3040 static int
3041 warning_candidate_p (tree x)
3043 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3044 return 0;
3046 if (TREE_CODE (x) == BLOCK)
3047 return 0;
3049 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3050 (lvalue_p) crash on TRY/CATCH. */
3051 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3052 return 0;
3054 if (!lvalue_p (x))
3055 return 0;
3057 /* No point to track non-const calls, they will never satisfy
3058 operand_equal_p. */
3059 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3060 return 0;
3062 if (TREE_CODE (x) == STRING_CST)
3063 return 0;
3065 return 1;
3068 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3069 static bool
3070 candidate_equal_p (const_tree x, const_tree y)
3072 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3075 /* Walk the tree X, and record accesses to variables. If X is written by the
3076 parent tree, WRITER is the parent.
3077 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3078 expression or its only operand forces a sequence point, then everything up
3079 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3080 in PNO_SP.
3081 Once we return, we will have emitted warnings if any subexpression before
3082 such a sequence point could be undefined. On a higher level, however, the
3083 sequence point may not be relevant, and we'll merge the two lists.
3085 Example: (b++, a) + b;
3086 The call that processes the COMPOUND_EXPR will store the increment of B
3087 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3088 processes the PLUS_EXPR will need to merge the two lists so that
3089 eventually, all accesses end up on the same list (and we'll warn about the
3090 unordered subexpressions b++ and b.
3092 A note on merging. If we modify the former example so that our expression
3093 becomes
3094 (b++, b) + a
3095 care must be taken not simply to add all three expressions into the final
3096 PNO_SP list. The function merge_tlist takes care of that by merging the
3097 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3098 way, so that no more than one access to B is recorded. */
3100 static void
3101 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3102 tree writer)
3104 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3105 enum tree_code code;
3106 enum tree_code_class cl;
3108 /* X may be NULL if it is the operand of an empty statement expression
3109 ({ }). */
3110 if (x == NULL)
3111 return;
3113 restart:
3114 code = TREE_CODE (x);
3115 cl = TREE_CODE_CLASS (code);
3117 if (warning_candidate_p (x))
3118 *pno_sp = new_tlist (*pno_sp, x, writer);
3120 switch (code)
3122 case CONSTRUCTOR:
3123 case SIZEOF_EXPR:
3124 return;
3126 case COMPOUND_EXPR:
3127 case TRUTH_ANDIF_EXPR:
3128 case TRUTH_ORIF_EXPR:
3129 tmp_before = tmp_nosp = tmp_list3 = 0;
3130 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3131 warn_for_collisions (tmp_nosp);
3132 merge_tlist (pbefore_sp, tmp_before, 0);
3133 merge_tlist (pbefore_sp, tmp_nosp, 0);
3134 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3135 merge_tlist (pbefore_sp, tmp_list3, 0);
3136 return;
3138 case COND_EXPR:
3139 tmp_before = tmp_list2 = 0;
3140 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3141 warn_for_collisions (tmp_list2);
3142 merge_tlist (pbefore_sp, tmp_before, 0);
3143 merge_tlist (pbefore_sp, tmp_list2, 0);
3145 tmp_list3 = tmp_nosp = 0;
3146 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3147 warn_for_collisions (tmp_nosp);
3148 merge_tlist (pbefore_sp, tmp_list3, 0);
3150 tmp_list3 = tmp_list2 = 0;
3151 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3152 warn_for_collisions (tmp_list2);
3153 merge_tlist (pbefore_sp, tmp_list3, 0);
3154 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3155 two first, to avoid warning for (a ? b++ : b++). */
3156 merge_tlist (&tmp_nosp, tmp_list2, 0);
3157 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3158 return;
3160 case PREDECREMENT_EXPR:
3161 case PREINCREMENT_EXPR:
3162 case POSTDECREMENT_EXPR:
3163 case POSTINCREMENT_EXPR:
3164 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3165 return;
3167 case MODIFY_EXPR:
3168 tmp_before = tmp_nosp = tmp_list3 = 0;
3169 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3170 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3171 /* Expressions inside the LHS are not ordered wrt. the sequence points
3172 in the RHS. Example:
3173 *a = (a++, 2)
3174 Despite the fact that the modification of "a" is in the before_sp
3175 list (tmp_before), it conflicts with the use of "a" in the LHS.
3176 We can handle this by adding the contents of tmp_list3
3177 to those of tmp_before, and redoing the collision warnings for that
3178 list. */
3179 add_tlist (&tmp_before, tmp_list3, x, 1);
3180 warn_for_collisions (tmp_before);
3181 /* Exclude the LHS itself here; we first have to merge it into the
3182 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3183 didn't exclude the LHS, we'd get it twice, once as a read and once
3184 as a write. */
3185 add_tlist (pno_sp, tmp_list3, x, 0);
3186 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3188 merge_tlist (pbefore_sp, tmp_before, 0);
3189 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3190 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3191 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3192 return;
3194 case CALL_EXPR:
3195 /* We need to warn about conflicts among arguments and conflicts between
3196 args and the function address. Side effects of the function address,
3197 however, are not ordered by the sequence point of the call. */
3199 call_expr_arg_iterator iter;
3200 tree arg;
3201 tmp_before = tmp_nosp = 0;
3202 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3203 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3205 tmp_list2 = tmp_list3 = 0;
3206 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3207 merge_tlist (&tmp_list3, tmp_list2, 0);
3208 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3210 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3211 warn_for_collisions (tmp_before);
3212 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3213 return;
3216 case TREE_LIST:
3217 /* Scan all the list, e.g. indices of multi dimensional array. */
3218 while (x)
3220 tmp_before = tmp_nosp = 0;
3221 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3222 merge_tlist (&tmp_nosp, tmp_before, 0);
3223 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3224 x = TREE_CHAIN (x);
3226 return;
3228 case SAVE_EXPR:
3230 struct tlist_cache *t;
3231 for (t = save_expr_cache; t; t = t->next)
3232 if (candidate_equal_p (t->expr, x))
3233 break;
3235 if (!t)
3237 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3238 t->next = save_expr_cache;
3239 t->expr = x;
3240 save_expr_cache = t;
3242 tmp_before = tmp_nosp = 0;
3243 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3244 warn_for_collisions (tmp_nosp);
3246 tmp_list3 = 0;
3247 merge_tlist (&tmp_list3, tmp_nosp, 0);
3248 t->cache_before_sp = tmp_before;
3249 t->cache_after_sp = tmp_list3;
3251 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3252 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3253 return;
3256 case ADDR_EXPR:
3257 x = TREE_OPERAND (x, 0);
3258 if (DECL_P (x))
3259 return;
3260 writer = 0;
3261 goto restart;
3263 default:
3264 /* For other expressions, simply recurse on their operands.
3265 Manual tail recursion for unary expressions.
3266 Other non-expressions need not be processed. */
3267 if (cl == tcc_unary)
3269 x = TREE_OPERAND (x, 0);
3270 writer = 0;
3271 goto restart;
3273 else if (IS_EXPR_CODE_CLASS (cl))
3275 int lp;
3276 int max = TREE_OPERAND_LENGTH (x);
3277 for (lp = 0; lp < max; lp++)
3279 tmp_before = tmp_nosp = 0;
3280 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3281 merge_tlist (&tmp_nosp, tmp_before, 0);
3282 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3285 return;
3289 /* Try to warn for undefined behavior in EXPR due to missing sequence
3290 points. */
3292 DEBUG_FUNCTION void
3293 verify_sequence_points (tree expr)
3295 struct tlist *before_sp = 0, *after_sp = 0;
3297 warned_ids = 0;
3298 save_expr_cache = 0;
3299 if (tlist_firstobj == 0)
3301 gcc_obstack_init (&tlist_obstack);
3302 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3305 verify_tree (expr, &before_sp, &after_sp, 0);
3306 warn_for_collisions (after_sp);
3307 obstack_free (&tlist_obstack, tlist_firstobj);
3310 /* Validate the expression after `case' and apply default promotions. */
3312 static tree
3313 check_case_value (tree value)
3315 if (value == NULL_TREE)
3316 return value;
3318 if (TREE_CODE (value) == INTEGER_CST)
3319 /* Promote char or short to int. */
3320 value = perform_integral_promotions (value);
3321 else if (value != error_mark_node)
3323 error ("case label does not reduce to an integer constant");
3324 value = error_mark_node;
3327 constant_expression_warning (value);
3329 return value;
3332 /* See if the case values LOW and HIGH are in the range of the original
3333 type (i.e. before the default conversion to int) of the switch testing
3334 expression.
3335 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3336 the type before promoting it. CASE_LOW_P is a pointer to the lower
3337 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3338 if the case is not a case range.
3339 The caller has to make sure that we are not called with NULL for
3340 CASE_LOW_P (i.e. the default case).
3341 Returns true if the case label is in range of ORIG_TYPE (saturated or
3342 untouched) or false if the label is out of range. */
3344 static bool
3345 check_case_bounds (tree type, tree orig_type,
3346 tree *case_low_p, tree *case_high_p)
3348 tree min_value, max_value;
3349 tree case_low = *case_low_p;
3350 tree case_high = case_high_p ? *case_high_p : case_low;
3352 /* If there was a problem with the original type, do nothing. */
3353 if (orig_type == error_mark_node)
3354 return true;
3356 min_value = TYPE_MIN_VALUE (orig_type);
3357 max_value = TYPE_MAX_VALUE (orig_type);
3359 /* Case label is less than minimum for type. */
3360 if (tree_int_cst_compare (case_low, min_value) < 0
3361 && tree_int_cst_compare (case_high, min_value) < 0)
3363 warning (0, "case label value is less than minimum value for type");
3364 return false;
3367 /* Case value is greater than maximum for type. */
3368 if (tree_int_cst_compare (case_low, max_value) > 0
3369 && tree_int_cst_compare (case_high, max_value) > 0)
3371 warning (0, "case label value exceeds maximum value for type");
3372 return false;
3375 /* Saturate lower case label value to minimum. */
3376 if (tree_int_cst_compare (case_high, min_value) >= 0
3377 && tree_int_cst_compare (case_low, min_value) < 0)
3379 warning (0, "lower value in case label range"
3380 " less than minimum value for type");
3381 case_low = min_value;
3384 /* Saturate upper case label value to maximum. */
3385 if (tree_int_cst_compare (case_low, max_value) <= 0
3386 && tree_int_cst_compare (case_high, max_value) > 0)
3388 warning (0, "upper value in case label range"
3389 " exceeds maximum value for type");
3390 case_high = max_value;
3393 if (*case_low_p != case_low)
3394 *case_low_p = convert (type, case_low);
3395 if (case_high_p && *case_high_p != case_high)
3396 *case_high_p = convert (type, case_high);
3398 return true;
3401 /* Return an integer type with BITS bits of precision,
3402 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3404 tree
3405 c_common_type_for_size (unsigned int bits, int unsignedp)
3407 if (bits == TYPE_PRECISION (integer_type_node))
3408 return unsignedp ? unsigned_type_node : integer_type_node;
3410 if (bits == TYPE_PRECISION (signed_char_type_node))
3411 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3413 if (bits == TYPE_PRECISION (short_integer_type_node))
3414 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3416 if (bits == TYPE_PRECISION (long_integer_type_node))
3417 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3419 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3420 return (unsignedp ? long_long_unsigned_type_node
3421 : long_long_integer_type_node);
3423 if (int128_integer_type_node
3424 && bits == TYPE_PRECISION (int128_integer_type_node))
3425 return (unsignedp ? int128_unsigned_type_node
3426 : int128_integer_type_node);
3428 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3429 return (unsignedp ? widest_unsigned_literal_type_node
3430 : widest_integer_literal_type_node);
3432 if (bits <= TYPE_PRECISION (intQI_type_node))
3433 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3435 if (bits <= TYPE_PRECISION (intHI_type_node))
3436 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3438 if (bits <= TYPE_PRECISION (intSI_type_node))
3439 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3441 if (bits <= TYPE_PRECISION (intDI_type_node))
3442 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3444 return 0;
3447 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3448 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3449 and saturating if SATP is nonzero, otherwise not saturating. */
3451 tree
3452 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3453 int unsignedp, int satp)
3455 enum machine_mode mode;
3456 if (ibit == 0)
3457 mode = unsignedp ? UQQmode : QQmode;
3458 else
3459 mode = unsignedp ? UHAmode : HAmode;
3461 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3462 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3463 break;
3465 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3467 sorry ("GCC cannot support operators with integer types and "
3468 "fixed-point types that have too many integral and "
3469 "fractional bits together");
3470 return 0;
3473 return c_common_type_for_mode (mode, satp);
3476 /* Used for communication between c_common_type_for_mode and
3477 c_register_builtin_type. */
3478 static GTY(()) tree registered_builtin_types;
3480 /* Return a data type that has machine mode MODE.
3481 If the mode is an integer,
3482 then UNSIGNEDP selects between signed and unsigned types.
3483 If the mode is a fixed-point mode,
3484 then UNSIGNEDP selects between saturating and nonsaturating types. */
3486 tree
3487 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3489 tree t;
3491 if (mode == TYPE_MODE (integer_type_node))
3492 return unsignedp ? unsigned_type_node : integer_type_node;
3494 if (mode == TYPE_MODE (signed_char_type_node))
3495 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3497 if (mode == TYPE_MODE (short_integer_type_node))
3498 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3500 if (mode == TYPE_MODE (long_integer_type_node))
3501 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3503 if (mode == TYPE_MODE (long_long_integer_type_node))
3504 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3506 if (int128_integer_type_node
3507 && mode == TYPE_MODE (int128_integer_type_node))
3508 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3510 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3511 return unsignedp ? widest_unsigned_literal_type_node
3512 : widest_integer_literal_type_node;
3514 if (mode == QImode)
3515 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3517 if (mode == HImode)
3518 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3520 if (mode == SImode)
3521 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3523 if (mode == DImode)
3524 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3526 #if HOST_BITS_PER_WIDE_INT >= 64
3527 if (mode == TYPE_MODE (intTI_type_node))
3528 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3529 #endif
3531 if (mode == TYPE_MODE (float_type_node))
3532 return float_type_node;
3534 if (mode == TYPE_MODE (double_type_node))
3535 return double_type_node;
3537 if (mode == TYPE_MODE (long_double_type_node))
3538 return long_double_type_node;
3540 if (mode == TYPE_MODE (void_type_node))
3541 return void_type_node;
3543 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3544 return (unsignedp
3545 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3546 : make_signed_type (GET_MODE_PRECISION (mode)));
3548 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3549 return (unsignedp
3550 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3551 : make_signed_type (GET_MODE_PRECISION (mode)));
3553 if (COMPLEX_MODE_P (mode))
3555 enum machine_mode inner_mode;
3556 tree inner_type;
3558 if (mode == TYPE_MODE (complex_float_type_node))
3559 return complex_float_type_node;
3560 if (mode == TYPE_MODE (complex_double_type_node))
3561 return complex_double_type_node;
3562 if (mode == TYPE_MODE (complex_long_double_type_node))
3563 return complex_long_double_type_node;
3565 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3566 return complex_integer_type_node;
3568 inner_mode = GET_MODE_INNER (mode);
3569 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3570 if (inner_type != NULL_TREE)
3571 return build_complex_type (inner_type);
3573 else if (VECTOR_MODE_P (mode))
3575 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3576 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3577 if (inner_type != NULL_TREE)
3578 return build_vector_type_for_mode (inner_type, mode);
3581 if (mode == TYPE_MODE (dfloat32_type_node))
3582 return dfloat32_type_node;
3583 if (mode == TYPE_MODE (dfloat64_type_node))
3584 return dfloat64_type_node;
3585 if (mode == TYPE_MODE (dfloat128_type_node))
3586 return dfloat128_type_node;
3588 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3590 if (mode == TYPE_MODE (short_fract_type_node))
3591 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3592 if (mode == TYPE_MODE (fract_type_node))
3593 return unsignedp ? sat_fract_type_node : fract_type_node;
3594 if (mode == TYPE_MODE (long_fract_type_node))
3595 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3596 if (mode == TYPE_MODE (long_long_fract_type_node))
3597 return unsignedp ? sat_long_long_fract_type_node
3598 : long_long_fract_type_node;
3600 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3601 return unsignedp ? sat_unsigned_short_fract_type_node
3602 : unsigned_short_fract_type_node;
3603 if (mode == TYPE_MODE (unsigned_fract_type_node))
3604 return unsignedp ? sat_unsigned_fract_type_node
3605 : unsigned_fract_type_node;
3606 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3607 return unsignedp ? sat_unsigned_long_fract_type_node
3608 : unsigned_long_fract_type_node;
3609 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3610 return unsignedp ? sat_unsigned_long_long_fract_type_node
3611 : unsigned_long_long_fract_type_node;
3613 if (mode == TYPE_MODE (short_accum_type_node))
3614 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3615 if (mode == TYPE_MODE (accum_type_node))
3616 return unsignedp ? sat_accum_type_node : accum_type_node;
3617 if (mode == TYPE_MODE (long_accum_type_node))
3618 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3619 if (mode == TYPE_MODE (long_long_accum_type_node))
3620 return unsignedp ? sat_long_long_accum_type_node
3621 : long_long_accum_type_node;
3623 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3624 return unsignedp ? sat_unsigned_short_accum_type_node
3625 : unsigned_short_accum_type_node;
3626 if (mode == TYPE_MODE (unsigned_accum_type_node))
3627 return unsignedp ? sat_unsigned_accum_type_node
3628 : unsigned_accum_type_node;
3629 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3630 return unsignedp ? sat_unsigned_long_accum_type_node
3631 : unsigned_long_accum_type_node;
3632 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3633 return unsignedp ? sat_unsigned_long_long_accum_type_node
3634 : unsigned_long_long_accum_type_node;
3636 if (mode == QQmode)
3637 return unsignedp ? sat_qq_type_node : qq_type_node;
3638 if (mode == HQmode)
3639 return unsignedp ? sat_hq_type_node : hq_type_node;
3640 if (mode == SQmode)
3641 return unsignedp ? sat_sq_type_node : sq_type_node;
3642 if (mode == DQmode)
3643 return unsignedp ? sat_dq_type_node : dq_type_node;
3644 if (mode == TQmode)
3645 return unsignedp ? sat_tq_type_node : tq_type_node;
3647 if (mode == UQQmode)
3648 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3649 if (mode == UHQmode)
3650 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3651 if (mode == USQmode)
3652 return unsignedp ? sat_usq_type_node : usq_type_node;
3653 if (mode == UDQmode)
3654 return unsignedp ? sat_udq_type_node : udq_type_node;
3655 if (mode == UTQmode)
3656 return unsignedp ? sat_utq_type_node : utq_type_node;
3658 if (mode == HAmode)
3659 return unsignedp ? sat_ha_type_node : ha_type_node;
3660 if (mode == SAmode)
3661 return unsignedp ? sat_sa_type_node : sa_type_node;
3662 if (mode == DAmode)
3663 return unsignedp ? sat_da_type_node : da_type_node;
3664 if (mode == TAmode)
3665 return unsignedp ? sat_ta_type_node : ta_type_node;
3667 if (mode == UHAmode)
3668 return unsignedp ? sat_uha_type_node : uha_type_node;
3669 if (mode == USAmode)
3670 return unsignedp ? sat_usa_type_node : usa_type_node;
3671 if (mode == UDAmode)
3672 return unsignedp ? sat_uda_type_node : uda_type_node;
3673 if (mode == UTAmode)
3674 return unsignedp ? sat_uta_type_node : uta_type_node;
3677 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3678 if (TYPE_MODE (TREE_VALUE (t)) == mode
3679 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3680 return TREE_VALUE (t);
3682 return 0;
3685 tree
3686 c_common_unsigned_type (tree type)
3688 return c_common_signed_or_unsigned_type (1, type);
3691 /* Return a signed type the same as TYPE in other respects. */
3693 tree
3694 c_common_signed_type (tree type)
3696 return c_common_signed_or_unsigned_type (0, type);
3699 /* Return a type the same as TYPE except unsigned or
3700 signed according to UNSIGNEDP. */
3702 tree
3703 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3705 tree type1;
3707 /* This block of code emulates the behavior of the old
3708 c_common_unsigned_type. In particular, it returns
3709 long_unsigned_type_node if passed a long, even when a int would
3710 have the same size. This is necessary for warnings to work
3711 correctly in archs where sizeof(int) == sizeof(long) */
3713 type1 = TYPE_MAIN_VARIANT (type);
3714 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3715 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3716 if (type1 == integer_type_node || type1 == unsigned_type_node)
3717 return unsignedp ? unsigned_type_node : integer_type_node;
3718 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3719 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3720 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3721 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3722 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3723 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3724 if (int128_integer_type_node
3725 && (type1 == int128_integer_type_node
3726 || type1 == int128_unsigned_type_node))
3727 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3728 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3729 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3730 #if HOST_BITS_PER_WIDE_INT >= 64
3731 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3732 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3733 #endif
3734 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3735 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3736 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3737 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3738 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3739 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3740 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3741 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3743 #define C_COMMON_FIXED_TYPES(NAME) \
3744 if (type1 == short_ ## NAME ## _type_node \
3745 || type1 == unsigned_short_ ## NAME ## _type_node) \
3746 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3747 : short_ ## NAME ## _type_node; \
3748 if (type1 == NAME ## _type_node \
3749 || type1 == unsigned_ ## NAME ## _type_node) \
3750 return unsignedp ? unsigned_ ## NAME ## _type_node \
3751 : NAME ## _type_node; \
3752 if (type1 == long_ ## NAME ## _type_node \
3753 || type1 == unsigned_long_ ## NAME ## _type_node) \
3754 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3755 : long_ ## NAME ## _type_node; \
3756 if (type1 == long_long_ ## NAME ## _type_node \
3757 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3758 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3759 : long_long_ ## NAME ## _type_node;
3761 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3762 if (type1 == NAME ## _type_node \
3763 || type1 == u ## NAME ## _type_node) \
3764 return unsignedp ? u ## NAME ## _type_node \
3765 : NAME ## _type_node;
3767 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3768 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3769 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3770 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3771 : sat_ ## short_ ## NAME ## _type_node; \
3772 if (type1 == sat_ ## NAME ## _type_node \
3773 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3774 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3775 : sat_ ## NAME ## _type_node; \
3776 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3777 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3778 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3779 : sat_ ## long_ ## NAME ## _type_node; \
3780 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3781 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3782 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3783 : sat_ ## long_long_ ## NAME ## _type_node;
3785 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3786 if (type1 == sat_ ## NAME ## _type_node \
3787 || type1 == sat_ ## u ## NAME ## _type_node) \
3788 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3789 : sat_ ## NAME ## _type_node;
3791 C_COMMON_FIXED_TYPES (fract);
3792 C_COMMON_FIXED_TYPES_SAT (fract);
3793 C_COMMON_FIXED_TYPES (accum);
3794 C_COMMON_FIXED_TYPES_SAT (accum);
3796 C_COMMON_FIXED_MODE_TYPES (qq);
3797 C_COMMON_FIXED_MODE_TYPES (hq);
3798 C_COMMON_FIXED_MODE_TYPES (sq);
3799 C_COMMON_FIXED_MODE_TYPES (dq);
3800 C_COMMON_FIXED_MODE_TYPES (tq);
3801 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3802 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3803 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3804 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3805 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3806 C_COMMON_FIXED_MODE_TYPES (ha);
3807 C_COMMON_FIXED_MODE_TYPES (sa);
3808 C_COMMON_FIXED_MODE_TYPES (da);
3809 C_COMMON_FIXED_MODE_TYPES (ta);
3810 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3811 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3812 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3813 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3815 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3816 the precision; they have precision set to match their range, but
3817 may use a wider mode to match an ABI. If we change modes, we may
3818 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3819 the precision as well, so as to yield correct results for
3820 bit-field types. C++ does not have these separate bit-field
3821 types, and producing a signed or unsigned variant of an
3822 ENUMERAL_TYPE may cause other problems as well. */
3824 if (!INTEGRAL_TYPE_P (type)
3825 || TYPE_UNSIGNED (type) == unsignedp)
3826 return type;
3828 #define TYPE_OK(node) \
3829 (TYPE_MODE (type) == TYPE_MODE (node) \
3830 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3831 if (TYPE_OK (signed_char_type_node))
3832 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3833 if (TYPE_OK (integer_type_node))
3834 return unsignedp ? unsigned_type_node : integer_type_node;
3835 if (TYPE_OK (short_integer_type_node))
3836 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3837 if (TYPE_OK (long_integer_type_node))
3838 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3839 if (TYPE_OK (long_long_integer_type_node))
3840 return (unsignedp ? long_long_unsigned_type_node
3841 : long_long_integer_type_node);
3842 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3843 return (unsignedp ? int128_unsigned_type_node
3844 : int128_integer_type_node);
3845 if (TYPE_OK (widest_integer_literal_type_node))
3846 return (unsignedp ? widest_unsigned_literal_type_node
3847 : widest_integer_literal_type_node);
3849 #if HOST_BITS_PER_WIDE_INT >= 64
3850 if (TYPE_OK (intTI_type_node))
3851 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3852 #endif
3853 if (TYPE_OK (intDI_type_node))
3854 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3855 if (TYPE_OK (intSI_type_node))
3856 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3857 if (TYPE_OK (intHI_type_node))
3858 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3859 if (TYPE_OK (intQI_type_node))
3860 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3861 #undef TYPE_OK
3863 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3866 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3868 tree
3869 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3871 /* Extended integer types of the same width as a standard type have
3872 lesser rank, so those of the same width as int promote to int or
3873 unsigned int and are valid for printf formats expecting int or
3874 unsigned int. To avoid such special cases, avoid creating
3875 extended integer types for bit-fields if a standard integer type
3876 is available. */
3877 if (width == TYPE_PRECISION (integer_type_node))
3878 return unsignedp ? unsigned_type_node : integer_type_node;
3879 if (width == TYPE_PRECISION (signed_char_type_node))
3880 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3881 if (width == TYPE_PRECISION (short_integer_type_node))
3882 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3883 if (width == TYPE_PRECISION (long_integer_type_node))
3884 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3885 if (width == TYPE_PRECISION (long_long_integer_type_node))
3886 return (unsignedp ? long_long_unsigned_type_node
3887 : long_long_integer_type_node);
3888 if (int128_integer_type_node
3889 && width == TYPE_PRECISION (int128_integer_type_node))
3890 return (unsignedp ? int128_unsigned_type_node
3891 : int128_integer_type_node);
3892 return build_nonstandard_integer_type (width, unsignedp);
3895 /* The C version of the register_builtin_type langhook. */
3897 void
3898 c_register_builtin_type (tree type, const char* name)
3900 tree decl;
3902 decl = build_decl (UNKNOWN_LOCATION,
3903 TYPE_DECL, get_identifier (name), type);
3904 DECL_ARTIFICIAL (decl) = 1;
3905 if (!TYPE_NAME (type))
3906 TYPE_NAME (type) = decl;
3907 pushdecl (decl);
3909 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3912 /* Print an error message for invalid operands to arith operation
3913 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3914 LOCATION is the location of the message. */
3916 void
3917 binary_op_error (location_t location, enum tree_code code,
3918 tree type0, tree type1)
3920 const char *opname;
3922 switch (code)
3924 case PLUS_EXPR:
3925 opname = "+"; break;
3926 case MINUS_EXPR:
3927 opname = "-"; break;
3928 case MULT_EXPR:
3929 opname = "*"; break;
3930 case MAX_EXPR:
3931 opname = "max"; break;
3932 case MIN_EXPR:
3933 opname = "min"; break;
3934 case EQ_EXPR:
3935 opname = "=="; break;
3936 case NE_EXPR:
3937 opname = "!="; break;
3938 case LE_EXPR:
3939 opname = "<="; break;
3940 case GE_EXPR:
3941 opname = ">="; break;
3942 case LT_EXPR:
3943 opname = "<"; break;
3944 case GT_EXPR:
3945 opname = ">"; break;
3946 case LSHIFT_EXPR:
3947 opname = "<<"; break;
3948 case RSHIFT_EXPR:
3949 opname = ">>"; break;
3950 case TRUNC_MOD_EXPR:
3951 case FLOOR_MOD_EXPR:
3952 opname = "%"; break;
3953 case TRUNC_DIV_EXPR:
3954 case FLOOR_DIV_EXPR:
3955 opname = "/"; break;
3956 case BIT_AND_EXPR:
3957 opname = "&"; break;
3958 case BIT_IOR_EXPR:
3959 opname = "|"; break;
3960 case TRUTH_ANDIF_EXPR:
3961 opname = "&&"; break;
3962 case TRUTH_ORIF_EXPR:
3963 opname = "||"; break;
3964 case BIT_XOR_EXPR:
3965 opname = "^"; break;
3966 default:
3967 gcc_unreachable ();
3969 error_at (location,
3970 "invalid operands to binary %s (have %qT and %qT)", opname,
3971 type0, type1);
3974 /* Given an expression as a tree, return its original type. Do this
3975 by stripping any conversion that preserves the sign and precision. */
3976 static tree
3977 expr_original_type (tree expr)
3979 STRIP_SIGN_NOPS (expr);
3980 return TREE_TYPE (expr);
3983 /* Subroutine of build_binary_op, used for comparison operations.
3984 See if the operands have both been converted from subword integer types
3985 and, if so, perhaps change them both back to their original type.
3986 This function is also responsible for converting the two operands
3987 to the proper common type for comparison.
3989 The arguments of this function are all pointers to local variables
3990 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3991 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3993 LOC is the location of the comparison.
3995 If this function returns nonzero, it means that the comparison has
3996 a constant value. What this function returns is an expression for
3997 that value. */
3999 tree
4000 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4001 tree *restype_ptr, enum tree_code *rescode_ptr)
4003 tree type;
4004 tree op0 = *op0_ptr;
4005 tree op1 = *op1_ptr;
4006 int unsignedp0, unsignedp1;
4007 int real1, real2;
4008 tree primop0, primop1;
4009 enum tree_code code = *rescode_ptr;
4011 /* Throw away any conversions to wider types
4012 already present in the operands. */
4014 primop0 = c_common_get_narrower (op0, &unsignedp0);
4015 primop1 = c_common_get_narrower (op1, &unsignedp1);
4017 /* If primopN is first sign-extended from primopN's precision to opN's
4018 precision, then zero-extended from opN's precision to
4019 *restype_ptr precision, shortenings might be invalid. */
4020 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4021 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4022 && !unsignedp0
4023 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4024 primop0 = op0;
4025 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4026 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4027 && !unsignedp1
4028 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4029 primop1 = op1;
4031 /* Handle the case that OP0 does not *contain* a conversion
4032 but it *requires* conversion to FINAL_TYPE. */
4034 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4035 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4036 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4037 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4039 /* If one of the operands must be floated, we cannot optimize. */
4040 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4041 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4043 /* If first arg is constant, swap the args (changing operation
4044 so value is preserved), for canonicalization. Don't do this if
4045 the second arg is 0. */
4047 if (TREE_CONSTANT (primop0)
4048 && !integer_zerop (primop1) && !real_zerop (primop1)
4049 && !fixed_zerop (primop1))
4051 tree tem = primop0;
4052 int temi = unsignedp0;
4053 primop0 = primop1;
4054 primop1 = tem;
4055 tem = op0;
4056 op0 = op1;
4057 op1 = tem;
4058 *op0_ptr = op0;
4059 *op1_ptr = op1;
4060 unsignedp0 = unsignedp1;
4061 unsignedp1 = temi;
4062 temi = real1;
4063 real1 = real2;
4064 real2 = temi;
4066 switch (code)
4068 case LT_EXPR:
4069 code = GT_EXPR;
4070 break;
4071 case GT_EXPR:
4072 code = LT_EXPR;
4073 break;
4074 case LE_EXPR:
4075 code = GE_EXPR;
4076 break;
4077 case GE_EXPR:
4078 code = LE_EXPR;
4079 break;
4080 default:
4081 break;
4083 *rescode_ptr = code;
4086 /* If comparing an integer against a constant more bits wide,
4087 maybe we can deduce a value of 1 or 0 independent of the data.
4088 Or else truncate the constant now
4089 rather than extend the variable at run time.
4091 This is only interesting if the constant is the wider arg.
4092 Also, it is not safe if the constant is unsigned and the
4093 variable arg is signed, since in this case the variable
4094 would be sign-extended and then regarded as unsigned.
4095 Our technique fails in this case because the lowest/highest
4096 possible unsigned results don't follow naturally from the
4097 lowest/highest possible values of the variable operand.
4098 For just EQ_EXPR and NE_EXPR there is another technique that
4099 could be used: see if the constant can be faithfully represented
4100 in the other operand's type, by truncating it and reextending it
4101 and see if that preserves the constant's value. */
4103 if (!real1 && !real2
4104 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4105 && TREE_CODE (primop1) == INTEGER_CST
4106 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4108 int min_gt, max_gt, min_lt, max_lt;
4109 tree maxval, minval;
4110 /* 1 if comparison is nominally unsigned. */
4111 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4112 tree val;
4114 type = c_common_signed_or_unsigned_type (unsignedp0,
4115 TREE_TYPE (primop0));
4117 maxval = TYPE_MAX_VALUE (type);
4118 minval = TYPE_MIN_VALUE (type);
4120 if (unsignedp && !unsignedp0)
4121 *restype_ptr = c_common_signed_type (*restype_ptr);
4123 if (TREE_TYPE (primop1) != *restype_ptr)
4125 /* Convert primop1 to target type, but do not introduce
4126 additional overflow. We know primop1 is an int_cst. */
4127 primop1 = force_fit_type_double (*restype_ptr,
4128 tree_to_double_int (primop1),
4129 0, TREE_OVERFLOW (primop1));
4131 if (type != *restype_ptr)
4133 minval = convert (*restype_ptr, minval);
4134 maxval = convert (*restype_ptr, maxval);
4137 if (unsignedp && unsignedp0)
4139 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4140 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4141 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4142 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4144 else
4146 min_gt = INT_CST_LT (primop1, minval);
4147 max_gt = INT_CST_LT (primop1, maxval);
4148 min_lt = INT_CST_LT (minval, primop1);
4149 max_lt = INT_CST_LT (maxval, primop1);
4152 val = 0;
4153 /* This used to be a switch, but Genix compiler can't handle that. */
4154 if (code == NE_EXPR)
4156 if (max_lt || min_gt)
4157 val = truthvalue_true_node;
4159 else if (code == EQ_EXPR)
4161 if (max_lt || min_gt)
4162 val = truthvalue_false_node;
4164 else if (code == LT_EXPR)
4166 if (max_lt)
4167 val = truthvalue_true_node;
4168 if (!min_lt)
4169 val = truthvalue_false_node;
4171 else if (code == GT_EXPR)
4173 if (min_gt)
4174 val = truthvalue_true_node;
4175 if (!max_gt)
4176 val = truthvalue_false_node;
4178 else if (code == LE_EXPR)
4180 if (!max_gt)
4181 val = truthvalue_true_node;
4182 if (min_gt)
4183 val = truthvalue_false_node;
4185 else if (code == GE_EXPR)
4187 if (!min_lt)
4188 val = truthvalue_true_node;
4189 if (max_lt)
4190 val = truthvalue_false_node;
4193 /* If primop0 was sign-extended and unsigned comparison specd,
4194 we did a signed comparison above using the signed type bounds.
4195 But the comparison we output must be unsigned.
4197 Also, for inequalities, VAL is no good; but if the signed
4198 comparison had *any* fixed result, it follows that the
4199 unsigned comparison just tests the sign in reverse
4200 (positive values are LE, negative ones GE).
4201 So we can generate an unsigned comparison
4202 against an extreme value of the signed type. */
4204 if (unsignedp && !unsignedp0)
4206 if (val != 0)
4207 switch (code)
4209 case LT_EXPR:
4210 case GE_EXPR:
4211 primop1 = TYPE_MIN_VALUE (type);
4212 val = 0;
4213 break;
4215 case LE_EXPR:
4216 case GT_EXPR:
4217 primop1 = TYPE_MAX_VALUE (type);
4218 val = 0;
4219 break;
4221 default:
4222 break;
4224 type = c_common_unsigned_type (type);
4227 if (TREE_CODE (primop0) != INTEGER_CST)
4229 if (val == truthvalue_false_node)
4230 warning_at (loc, OPT_Wtype_limits,
4231 "comparison is always false due to limited range of data type");
4232 if (val == truthvalue_true_node)
4233 warning_at (loc, OPT_Wtype_limits,
4234 "comparison is always true due to limited range of data type");
4237 if (val != 0)
4239 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4240 if (TREE_SIDE_EFFECTS (primop0))
4241 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4242 return val;
4245 /* Value is not predetermined, but do the comparison
4246 in the type of the operand that is not constant.
4247 TYPE is already properly set. */
4250 /* If either arg is decimal float and the other is float, find the
4251 proper common type to use for comparison. */
4252 else if (real1 && real2
4253 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4254 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4255 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4257 else if (real1 && real2
4258 && (TYPE_PRECISION (TREE_TYPE (primop0))
4259 == TYPE_PRECISION (TREE_TYPE (primop1))))
4260 type = TREE_TYPE (primop0);
4262 /* If args' natural types are both narrower than nominal type
4263 and both extend in the same manner, compare them
4264 in the type of the wider arg.
4265 Otherwise must actually extend both to the nominal
4266 common type lest different ways of extending
4267 alter the result.
4268 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4270 else if (unsignedp0 == unsignedp1 && real1 == real2
4271 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4272 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4274 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4275 type = c_common_signed_or_unsigned_type (unsignedp0
4276 || TYPE_UNSIGNED (*restype_ptr),
4277 type);
4278 /* Make sure shorter operand is extended the right way
4279 to match the longer operand. */
4280 primop0
4281 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4282 TREE_TYPE (primop0)),
4283 primop0);
4284 primop1
4285 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4286 TREE_TYPE (primop1)),
4287 primop1);
4289 else
4291 /* Here we must do the comparison on the nominal type
4292 using the args exactly as we received them. */
4293 type = *restype_ptr;
4294 primop0 = op0;
4295 primop1 = op1;
4297 if (!real1 && !real2 && integer_zerop (primop1)
4298 && TYPE_UNSIGNED (*restype_ptr))
4300 tree value = 0;
4301 /* All unsigned values are >= 0, so we warn. However,
4302 if OP0 is a constant that is >= 0, the signedness of
4303 the comparison isn't an issue, so suppress the
4304 warning. */
4305 bool warn =
4306 warn_type_limits && !in_system_header_at (loc)
4307 && !(TREE_CODE (primop0) == INTEGER_CST
4308 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4309 primop0)))
4310 /* Do not warn for enumeration types. */
4311 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4313 switch (code)
4315 case GE_EXPR:
4316 if (warn)
4317 warning_at (loc, OPT_Wtype_limits,
4318 "comparison of unsigned expression >= 0 is always true");
4319 value = truthvalue_true_node;
4320 break;
4322 case LT_EXPR:
4323 if (warn)
4324 warning_at (loc, OPT_Wtype_limits,
4325 "comparison of unsigned expression < 0 is always false");
4326 value = truthvalue_false_node;
4327 break;
4329 default:
4330 break;
4333 if (value != 0)
4335 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4336 if (TREE_SIDE_EFFECTS (primop0))
4337 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4338 primop0, value);
4339 return value;
4344 *op0_ptr = convert (type, primop0);
4345 *op1_ptr = convert (type, primop1);
4347 *restype_ptr = truthvalue_type_node;
4349 return 0;
4352 /* Return a tree for the sum or difference (RESULTCODE says which)
4353 of pointer PTROP and integer INTOP. */
4355 tree
4356 pointer_int_sum (location_t loc, enum tree_code resultcode,
4357 tree ptrop, tree intop, bool complain)
4359 tree size_exp, ret;
4361 /* The result is a pointer of the same type that is being added. */
4362 tree result_type = TREE_TYPE (ptrop);
4364 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4366 if (complain && warn_pointer_arith)
4367 pedwarn (loc, OPT_Wpointer_arith,
4368 "pointer of type %<void *%> used in arithmetic");
4369 else if (!complain)
4370 return error_mark_node;
4371 size_exp = integer_one_node;
4373 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4375 if (complain && warn_pointer_arith)
4376 pedwarn (loc, OPT_Wpointer_arith,
4377 "pointer to a function used in arithmetic");
4378 else if (!complain)
4379 return error_mark_node;
4380 size_exp = integer_one_node;
4382 else
4383 size_exp = size_in_bytes (TREE_TYPE (result_type));
4385 /* We are manipulating pointer values, so we don't need to warn
4386 about relying on undefined signed overflow. We disable the
4387 warning here because we use integer types so fold won't know that
4388 they are really pointers. */
4389 fold_defer_overflow_warnings ();
4391 /* If what we are about to multiply by the size of the elements
4392 contains a constant term, apply distributive law
4393 and multiply that constant term separately.
4394 This helps produce common subexpressions. */
4395 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4396 && !TREE_CONSTANT (intop)
4397 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4398 && TREE_CONSTANT (size_exp)
4399 /* If the constant comes from pointer subtraction,
4400 skip this optimization--it would cause an error. */
4401 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4402 /* If the constant is unsigned, and smaller than the pointer size,
4403 then we must skip this optimization. This is because it could cause
4404 an overflow error if the constant is negative but INTOP is not. */
4405 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4406 || (TYPE_PRECISION (TREE_TYPE (intop))
4407 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4409 enum tree_code subcode = resultcode;
4410 tree int_type = TREE_TYPE (intop);
4411 if (TREE_CODE (intop) == MINUS_EXPR)
4412 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4413 /* Convert both subexpression types to the type of intop,
4414 because weird cases involving pointer arithmetic
4415 can result in a sum or difference with different type args. */
4416 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4417 subcode, ptrop,
4418 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4419 intop = convert (int_type, TREE_OPERAND (intop, 0));
4422 /* Convert the integer argument to a type the same size as sizetype
4423 so the multiply won't overflow spuriously. */
4424 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4425 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4426 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4427 TYPE_UNSIGNED (sizetype)), intop);
4429 /* Replace the integer argument with a suitable product by the object size.
4430 Do this multiplication as signed, then convert to the appropriate type
4431 for the pointer operation and disregard an overflow that occurred only
4432 because of the sign-extension change in the latter conversion. */
4434 tree t = build_binary_op (loc,
4435 MULT_EXPR, intop,
4436 convert (TREE_TYPE (intop), size_exp), 1);
4437 intop = convert (sizetype, t);
4438 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4439 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4440 TREE_INT_CST_HIGH (intop));
4443 /* Create the sum or difference. */
4444 if (resultcode == MINUS_EXPR)
4445 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4447 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4449 fold_undefer_and_ignore_overflow_warnings ();
4451 return ret;
4454 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4455 and if NON_CONST is known not to be permitted in an evaluated part
4456 of a constant expression. */
4458 tree
4459 c_wrap_maybe_const (tree expr, bool non_const)
4461 bool nowarning = TREE_NO_WARNING (expr);
4462 location_t loc = EXPR_LOCATION (expr);
4464 /* This should never be called for C++. */
4465 if (c_dialect_cxx ())
4466 gcc_unreachable ();
4468 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4469 STRIP_TYPE_NOPS (expr);
4470 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4471 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4472 if (nowarning)
4473 TREE_NO_WARNING (expr) = 1;
4474 protected_set_expr_location (expr, loc);
4476 return expr;
4479 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4480 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4481 around the SAVE_EXPR if needed so that c_fully_fold does not need
4482 to look inside SAVE_EXPRs. */
4484 tree
4485 c_save_expr (tree expr)
4487 bool maybe_const = true;
4488 if (c_dialect_cxx ())
4489 return save_expr (expr);
4490 expr = c_fully_fold (expr, false, &maybe_const);
4491 expr = save_expr (expr);
4492 if (!maybe_const)
4493 expr = c_wrap_maybe_const (expr, true);
4494 return expr;
4497 /* Return whether EXPR is a declaration whose address can never be
4498 NULL. */
4500 bool
4501 decl_with_nonnull_addr_p (const_tree expr)
4503 return (DECL_P (expr)
4504 && (TREE_CODE (expr) == PARM_DECL
4505 || TREE_CODE (expr) == LABEL_DECL
4506 || !DECL_WEAK (expr)));
4509 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4510 or for an `if' or `while' statement or ?..: exp. It should already
4511 have been validated to be of suitable type; otherwise, a bad
4512 diagnostic may result.
4514 The EXPR is located at LOCATION.
4516 This preparation consists of taking the ordinary
4517 representation of an expression expr and producing a valid tree
4518 boolean expression describing whether expr is nonzero. We could
4519 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4520 but we optimize comparisons, &&, ||, and !.
4522 The resulting type should always be `truthvalue_type_node'. */
4524 tree
4525 c_common_truthvalue_conversion (location_t location, tree expr)
4527 switch (TREE_CODE (expr))
4529 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4530 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4531 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4532 case ORDERED_EXPR: case UNORDERED_EXPR:
4533 if (TREE_TYPE (expr) == truthvalue_type_node)
4534 return expr;
4535 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4536 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4537 goto ret;
4539 case TRUTH_ANDIF_EXPR:
4540 case TRUTH_ORIF_EXPR:
4541 case TRUTH_AND_EXPR:
4542 case TRUTH_OR_EXPR:
4543 case TRUTH_XOR_EXPR:
4544 if (TREE_TYPE (expr) == truthvalue_type_node)
4545 return expr;
4546 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4547 c_common_truthvalue_conversion (location,
4548 TREE_OPERAND (expr, 0)),
4549 c_common_truthvalue_conversion (location,
4550 TREE_OPERAND (expr, 1)));
4551 goto ret;
4553 case TRUTH_NOT_EXPR:
4554 if (TREE_TYPE (expr) == truthvalue_type_node)
4555 return expr;
4556 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4557 c_common_truthvalue_conversion (location,
4558 TREE_OPERAND (expr, 0)));
4559 goto ret;
4561 case ERROR_MARK:
4562 return expr;
4564 case INTEGER_CST:
4565 return integer_zerop (expr) ? truthvalue_false_node
4566 : truthvalue_true_node;
4568 case REAL_CST:
4569 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4570 ? truthvalue_true_node
4571 : truthvalue_false_node;
4573 case FIXED_CST:
4574 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4575 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4576 ? truthvalue_true_node
4577 : truthvalue_false_node;
4579 case FUNCTION_DECL:
4580 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4581 /* Fall through. */
4583 case ADDR_EXPR:
4585 tree inner = TREE_OPERAND (expr, 0);
4586 if (decl_with_nonnull_addr_p (inner))
4588 /* Common Ada/Pascal programmer's mistake. */
4589 warning_at (location,
4590 OPT_Waddress,
4591 "the address of %qD will always evaluate as %<true%>",
4592 inner);
4593 return truthvalue_true_node;
4595 break;
4598 case COMPLEX_EXPR:
4599 expr = build_binary_op (EXPR_LOCATION (expr),
4600 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4601 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4602 c_common_truthvalue_conversion (location,
4603 TREE_OPERAND (expr, 0)),
4604 c_common_truthvalue_conversion (location,
4605 TREE_OPERAND (expr, 1)),
4607 goto ret;
4609 case NEGATE_EXPR:
4610 case ABS_EXPR:
4611 case FLOAT_EXPR:
4612 case EXCESS_PRECISION_EXPR:
4613 /* These don't change whether an object is nonzero or zero. */
4614 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4616 case LROTATE_EXPR:
4617 case RROTATE_EXPR:
4618 /* These don't change whether an object is zero or nonzero, but
4619 we can't ignore them if their second arg has side-effects. */
4620 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4622 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4623 TREE_OPERAND (expr, 1),
4624 c_common_truthvalue_conversion
4625 (location, TREE_OPERAND (expr, 0)));
4626 goto ret;
4628 else
4629 return c_common_truthvalue_conversion (location,
4630 TREE_OPERAND (expr, 0));
4632 case COND_EXPR:
4633 /* Distribute the conversion into the arms of a COND_EXPR. */
4634 if (c_dialect_cxx ())
4636 tree op1 = TREE_OPERAND (expr, 1);
4637 tree op2 = TREE_OPERAND (expr, 2);
4638 /* In C++ one of the arms might have void type if it is throw. */
4639 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4640 op1 = c_common_truthvalue_conversion (location, op1);
4641 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4642 op2 = c_common_truthvalue_conversion (location, op2);
4643 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4644 TREE_OPERAND (expr, 0), op1, op2);
4645 goto ret;
4647 else
4649 /* Folding will happen later for C. */
4650 expr = build3 (COND_EXPR, truthvalue_type_node,
4651 TREE_OPERAND (expr, 0),
4652 c_common_truthvalue_conversion (location,
4653 TREE_OPERAND (expr, 1)),
4654 c_common_truthvalue_conversion (location,
4655 TREE_OPERAND (expr, 2)));
4656 goto ret;
4659 CASE_CONVERT:
4661 tree totype = TREE_TYPE (expr);
4662 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4664 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4665 since that affects how `default_conversion' will behave. */
4666 if (TREE_CODE (totype) == REFERENCE_TYPE
4667 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4668 break;
4669 /* Don't strip a conversion from C++0x scoped enum, since they
4670 don't implicitly convert to other types. */
4671 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4672 && ENUM_IS_SCOPED (fromtype))
4673 break;
4674 /* If this isn't narrowing the argument, we can ignore it. */
4675 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4676 return c_common_truthvalue_conversion (location,
4677 TREE_OPERAND (expr, 0));
4679 break;
4681 case MODIFY_EXPR:
4682 if (!TREE_NO_WARNING (expr)
4683 && warn_parentheses)
4685 warning (OPT_Wparentheses,
4686 "suggest parentheses around assignment used as truth value");
4687 TREE_NO_WARNING (expr) = 1;
4689 break;
4691 default:
4692 break;
4695 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4697 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4698 expr = (build_binary_op
4699 (EXPR_LOCATION (expr),
4700 (TREE_SIDE_EFFECTS (expr)
4701 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4702 c_common_truthvalue_conversion
4703 (location,
4704 build_unary_op (location, REALPART_EXPR, t, 0)),
4705 c_common_truthvalue_conversion
4706 (location,
4707 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4708 0));
4709 goto ret;
4712 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4714 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4715 FCONST0 (TYPE_MODE
4716 (TREE_TYPE (expr))));
4717 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4719 else
4720 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4722 ret:
4723 protected_set_expr_location (expr, location);
4724 return expr;
4727 static void def_builtin_1 (enum built_in_function fncode,
4728 const char *name,
4729 enum built_in_class fnclass,
4730 tree fntype, tree libtype,
4731 bool both_p, bool fallback_p, bool nonansi_p,
4732 tree fnattrs, bool implicit_p);
4735 /* Apply the TYPE_QUALS to the new DECL. */
4737 void
4738 c_apply_type_quals_to_decl (int type_quals, tree decl)
4740 tree type = TREE_TYPE (decl);
4742 if (type == error_mark_node)
4743 return;
4745 if ((type_quals & TYPE_QUAL_CONST)
4746 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4747 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4748 constructor can produce constant init, so rely on cp_finish_decl to
4749 clear TREE_READONLY if the variable has non-constant init. */
4750 TREE_READONLY (decl) = 1;
4751 if (type_quals & TYPE_QUAL_VOLATILE)
4753 TREE_SIDE_EFFECTS (decl) = 1;
4754 TREE_THIS_VOLATILE (decl) = 1;
4756 if (type_quals & TYPE_QUAL_RESTRICT)
4758 while (type && TREE_CODE (type) == ARRAY_TYPE)
4759 /* Allow 'restrict' on arrays of pointers.
4760 FIXME currently we just ignore it. */
4761 type = TREE_TYPE (type);
4762 if (!type
4763 || !POINTER_TYPE_P (type)
4764 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4765 error ("invalid use of %<restrict%>");
4769 /* Hash function for the problem of multiple type definitions in
4770 different files. This must hash all types that will compare
4771 equal via comptypes to the same value. In practice it hashes
4772 on some of the simple stuff and leaves the details to comptypes. */
4774 static hashval_t
4775 c_type_hash (const void *p)
4777 int n_elements;
4778 int shift, size;
4779 const_tree const t = (const_tree) p;
4780 tree t2;
4781 switch (TREE_CODE (t))
4783 /* For pointers, hash on pointee type plus some swizzling. */
4784 case POINTER_TYPE:
4785 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4786 /* Hash on number of elements and total size. */
4787 case ENUMERAL_TYPE:
4788 shift = 3;
4789 t2 = TYPE_VALUES (t);
4790 break;
4791 case RECORD_TYPE:
4792 shift = 0;
4793 t2 = TYPE_FIELDS (t);
4794 break;
4795 case QUAL_UNION_TYPE:
4796 shift = 1;
4797 t2 = TYPE_FIELDS (t);
4798 break;
4799 case UNION_TYPE:
4800 shift = 2;
4801 t2 = TYPE_FIELDS (t);
4802 break;
4803 default:
4804 gcc_unreachable ();
4806 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4807 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4808 n_elements = list_length (t2);
4809 /* We might have a VLA here. */
4810 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4811 size = 0;
4812 else
4813 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4814 return ((size << 24) | (n_elements << shift));
4817 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4819 /* Return the typed-based alias set for T, which may be an expression
4820 or a type. Return -1 if we don't do anything special. */
4822 alias_set_type
4823 c_common_get_alias_set (tree t)
4825 tree u;
4826 PTR *slot;
4828 /* For VLAs, use the alias set of the element type rather than the
4829 default of alias set 0 for types compared structurally. */
4830 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4832 if (TREE_CODE (t) == ARRAY_TYPE)
4833 return get_alias_set (TREE_TYPE (t));
4834 return -1;
4837 /* Permit type-punning when accessing a union, provided the access
4838 is directly through the union. For example, this code does not
4839 permit taking the address of a union member and then storing
4840 through it. Even the type-punning allowed here is a GCC
4841 extension, albeit a common and useful one; the C standard says
4842 that such accesses have implementation-defined behavior. */
4843 for (u = t;
4844 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4845 u = TREE_OPERAND (u, 0))
4846 if (TREE_CODE (u) == COMPONENT_REF
4847 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4848 return 0;
4850 /* That's all the expressions we handle specially. */
4851 if (!TYPE_P (t))
4852 return -1;
4854 /* The C standard guarantees that any object may be accessed via an
4855 lvalue that has character type. */
4856 if (t == char_type_node
4857 || t == signed_char_type_node
4858 || t == unsigned_char_type_node)
4859 return 0;
4861 /* The C standard specifically allows aliasing between signed and
4862 unsigned variants of the same type. We treat the signed
4863 variant as canonical. */
4864 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4866 tree t1 = c_common_signed_type (t);
4868 /* t1 == t can happen for boolean nodes which are always unsigned. */
4869 if (t1 != t)
4870 return get_alias_set (t1);
4873 /* Handle the case of multiple type nodes referring to "the same" type,
4874 which occurs with IMA. These share an alias set. FIXME: Currently only
4875 C90 is handled. (In C99 type compatibility is not transitive, which
4876 complicates things mightily. The alias set splay trees can theoretically
4877 represent this, but insertion is tricky when you consider all the
4878 different orders things might arrive in.) */
4880 if (c_language != clk_c || flag_isoc99)
4881 return -1;
4883 /* Save time if there's only one input file. */
4884 if (num_in_fnames == 1)
4885 return -1;
4887 /* Pointers need special handling if they point to any type that
4888 needs special handling (below). */
4889 if (TREE_CODE (t) == POINTER_TYPE)
4891 tree t2;
4892 /* Find bottom type under any nested POINTERs. */
4893 for (t2 = TREE_TYPE (t);
4894 TREE_CODE (t2) == POINTER_TYPE;
4895 t2 = TREE_TYPE (t2))
4897 if (TREE_CODE (t2) != RECORD_TYPE
4898 && TREE_CODE (t2) != ENUMERAL_TYPE
4899 && TREE_CODE (t2) != QUAL_UNION_TYPE
4900 && TREE_CODE (t2) != UNION_TYPE)
4901 return -1;
4902 if (TYPE_SIZE (t2) == 0)
4903 return -1;
4905 /* These are the only cases that need special handling. */
4906 if (TREE_CODE (t) != RECORD_TYPE
4907 && TREE_CODE (t) != ENUMERAL_TYPE
4908 && TREE_CODE (t) != QUAL_UNION_TYPE
4909 && TREE_CODE (t) != UNION_TYPE
4910 && TREE_CODE (t) != POINTER_TYPE)
4911 return -1;
4912 /* Undefined? */
4913 if (TYPE_SIZE (t) == 0)
4914 return -1;
4916 /* Look up t in hash table. Only one of the compatible types within each
4917 alias set is recorded in the table. */
4918 if (!type_hash_table)
4919 type_hash_table = htab_create_ggc (1021, c_type_hash,
4920 (htab_eq) lang_hooks.types_compatible_p,
4921 NULL);
4922 slot = htab_find_slot (type_hash_table, t, INSERT);
4923 if (*slot != NULL)
4925 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4926 return TYPE_ALIAS_SET ((tree)*slot);
4928 else
4929 /* Our caller will assign and record (in t) a new alias set; all we need
4930 to do is remember t in the hash table. */
4931 *slot = t;
4933 return -1;
4936 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4937 the IS_SIZEOF parameter indicates which operator is being applied.
4938 The COMPLAIN flag controls whether we should diagnose possibly
4939 ill-formed constructs or not. LOC is the location of the SIZEOF or
4940 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4941 a type in any context should be returned, rather than the normal
4942 alignment for that type. */
4944 tree
4945 c_sizeof_or_alignof_type (location_t loc,
4946 tree type, bool is_sizeof, bool min_alignof,
4947 int complain)
4949 const char *op_name;
4950 tree value = NULL;
4951 enum tree_code type_code = TREE_CODE (type);
4953 op_name = is_sizeof ? "sizeof" : "__alignof__";
4955 if (type_code == FUNCTION_TYPE)
4957 if (is_sizeof)
4959 if (complain && warn_pointer_arith)
4960 pedwarn (loc, OPT_Wpointer_arith,
4961 "invalid application of %<sizeof%> to a function type");
4962 else if (!complain)
4963 return error_mark_node;
4964 value = size_one_node;
4966 else
4968 if (complain)
4970 if (c_dialect_cxx ())
4971 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4972 "%<alignof%> applied to a function type");
4973 else
4974 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4975 "%<_Alignof%> applied to a function type");
4977 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4980 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4982 if (type_code == VOID_TYPE
4983 && complain && warn_pointer_arith)
4984 pedwarn (loc, OPT_Wpointer_arith,
4985 "invalid application of %qs to a void type", op_name);
4986 else if (!complain)
4987 return error_mark_node;
4988 value = size_one_node;
4990 else if (!COMPLETE_TYPE_P (type)
4991 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4993 if (complain)
4994 error_at (loc, "invalid application of %qs to incomplete type %qT",
4995 op_name, type);
4996 return error_mark_node;
4998 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4999 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5001 if (complain)
5002 error_at (loc, "invalid application of %qs to array type %qT of "
5003 "incomplete element type", op_name, type);
5004 return error_mark_node;
5006 else
5008 if (is_sizeof)
5009 /* Convert in case a char is more than one unit. */
5010 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5011 size_int (TYPE_PRECISION (char_type_node)
5012 / BITS_PER_UNIT));
5013 else if (min_alignof)
5015 unsigned int align = TYPE_ALIGN (type);
5016 align = MIN (align, BIGGEST_ALIGNMENT);
5017 #ifdef BIGGEST_FIELD_ALIGNMENT
5018 align = MIN (align, BIGGEST_FIELD_ALIGNMENT);
5019 #endif
5020 unsigned int field_align = align;
5021 #ifdef ADJUST_FIELD_ALIGN
5022 tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
5023 type);
5024 field_align = ADJUST_FIELD_ALIGN (field, field_align);
5025 #endif
5026 align = MIN (align, field_align);
5027 value = size_int (align / BITS_PER_UNIT);
5029 else
5030 value = size_int (TYPE_ALIGN_UNIT (type));
5033 /* VALUE will have the middle-end integer type sizetype.
5034 However, we should really return a value of type `size_t',
5035 which is just a typedef for an ordinary integer type. */
5036 value = fold_convert_loc (loc, size_type_node, value);
5038 return value;
5041 /* Implement the __alignof keyword: Return the minimum required
5042 alignment of EXPR, measured in bytes. For VAR_DECLs,
5043 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5044 from an "aligned" __attribute__ specification). LOC is the
5045 location of the ALIGNOF operator. */
5047 tree
5048 c_alignof_expr (location_t loc, tree expr)
5050 tree t;
5052 if (VAR_OR_FUNCTION_DECL_P (expr))
5053 t = size_int (DECL_ALIGN_UNIT (expr));
5055 else if (TREE_CODE (expr) == COMPONENT_REF
5056 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5058 error_at (loc, "%<__alignof%> applied to a bit-field");
5059 t = size_one_node;
5061 else if (TREE_CODE (expr) == COMPONENT_REF
5062 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5063 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5065 else if (TREE_CODE (expr) == INDIRECT_REF)
5067 tree t = TREE_OPERAND (expr, 0);
5068 tree best = t;
5069 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5071 while (CONVERT_EXPR_P (t)
5072 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5074 int thisalign;
5076 t = TREE_OPERAND (t, 0);
5077 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5078 if (thisalign > bestalign)
5079 best = t, bestalign = thisalign;
5081 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5083 else
5084 return c_alignof (loc, TREE_TYPE (expr));
5086 return fold_convert_loc (loc, size_type_node, t);
5089 /* Handle C and C++ default attributes. */
5091 enum built_in_attribute
5093 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5094 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5095 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5096 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5097 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5098 #include "builtin-attrs.def"
5099 #undef DEF_ATTR_NULL_TREE
5100 #undef DEF_ATTR_INT
5101 #undef DEF_ATTR_STRING
5102 #undef DEF_ATTR_IDENT
5103 #undef DEF_ATTR_TREE_LIST
5104 ATTR_LAST
5107 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5109 static void c_init_attributes (void);
5111 enum c_builtin_type
5113 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5114 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5115 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5116 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5117 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5118 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5119 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5120 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5121 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5122 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5123 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5124 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5125 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5126 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5127 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5128 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5129 NAME,
5130 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5131 #include "builtin-types.def"
5132 #undef DEF_PRIMITIVE_TYPE
5133 #undef DEF_FUNCTION_TYPE_0
5134 #undef DEF_FUNCTION_TYPE_1
5135 #undef DEF_FUNCTION_TYPE_2
5136 #undef DEF_FUNCTION_TYPE_3
5137 #undef DEF_FUNCTION_TYPE_4
5138 #undef DEF_FUNCTION_TYPE_5
5139 #undef DEF_FUNCTION_TYPE_6
5140 #undef DEF_FUNCTION_TYPE_7
5141 #undef DEF_FUNCTION_TYPE_8
5142 #undef DEF_FUNCTION_TYPE_VAR_0
5143 #undef DEF_FUNCTION_TYPE_VAR_1
5144 #undef DEF_FUNCTION_TYPE_VAR_2
5145 #undef DEF_FUNCTION_TYPE_VAR_3
5146 #undef DEF_FUNCTION_TYPE_VAR_4
5147 #undef DEF_FUNCTION_TYPE_VAR_5
5148 #undef DEF_POINTER_TYPE
5149 BT_LAST
5152 typedef enum c_builtin_type builtin_type;
5154 /* A temporary array for c_common_nodes_and_builtins. Used in
5155 communication with def_fn_type. */
5156 static tree builtin_types[(int) BT_LAST + 1];
5158 /* A helper function for c_common_nodes_and_builtins. Build function type
5159 for DEF with return type RET and N arguments. If VAR is true, then the
5160 function should be variadic after those N arguments.
5162 Takes special care not to ICE if any of the types involved are
5163 error_mark_node, which indicates that said type is not in fact available
5164 (see builtin_type_for_size). In which case the function type as a whole
5165 should be error_mark_node. */
5167 static void
5168 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5170 tree t;
5171 tree *args = XALLOCAVEC (tree, n);
5172 va_list list;
5173 int i;
5175 va_start (list, n);
5176 for (i = 0; i < n; ++i)
5178 builtin_type a = (builtin_type) va_arg (list, int);
5179 t = builtin_types[a];
5180 if (t == error_mark_node)
5181 goto egress;
5182 args[i] = t;
5185 t = builtin_types[ret];
5186 if (t == error_mark_node)
5187 goto egress;
5188 if (var)
5189 t = build_varargs_function_type_array (t, n, args);
5190 else
5191 t = build_function_type_array (t, n, args);
5193 egress:
5194 builtin_types[def] = t;
5195 va_end (list);
5198 /* Build builtin functions common to both C and C++ language
5199 frontends. */
5201 static void
5202 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5204 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5205 builtin_types[ENUM] = VALUE;
5206 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5207 def_fn_type (ENUM, RETURN, 0, 0);
5208 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5209 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5210 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5211 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5212 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5213 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5214 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5215 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5216 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5217 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5218 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5219 ARG6) \
5220 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5221 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5222 ARG6, ARG7) \
5223 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5224 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5225 ARG6, ARG7, ARG8) \
5226 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5227 ARG7, ARG8);
5228 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5229 def_fn_type (ENUM, RETURN, 1, 0);
5230 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5231 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5232 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5233 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5234 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5235 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5236 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5237 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5238 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5239 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5240 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5241 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5243 #include "builtin-types.def"
5245 #undef DEF_PRIMITIVE_TYPE
5246 #undef DEF_FUNCTION_TYPE_1
5247 #undef DEF_FUNCTION_TYPE_2
5248 #undef DEF_FUNCTION_TYPE_3
5249 #undef DEF_FUNCTION_TYPE_4
5250 #undef DEF_FUNCTION_TYPE_5
5251 #undef DEF_FUNCTION_TYPE_6
5252 #undef DEF_FUNCTION_TYPE_VAR_0
5253 #undef DEF_FUNCTION_TYPE_VAR_1
5254 #undef DEF_FUNCTION_TYPE_VAR_2
5255 #undef DEF_FUNCTION_TYPE_VAR_3
5256 #undef DEF_FUNCTION_TYPE_VAR_4
5257 #undef DEF_FUNCTION_TYPE_VAR_5
5258 #undef DEF_POINTER_TYPE
5259 builtin_types[(int) BT_LAST] = NULL_TREE;
5261 c_init_attributes ();
5263 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5264 NONANSI_P, ATTRS, IMPLICIT, COND) \
5265 if (NAME && COND) \
5266 def_builtin_1 (ENUM, NAME, CLASS, \
5267 builtin_types[(int) TYPE], \
5268 builtin_types[(int) LIBTYPE], \
5269 BOTH_P, FALLBACK_P, NONANSI_P, \
5270 built_in_attributes[(int) ATTRS], IMPLICIT);
5271 #include "builtins.def"
5272 #undef DEF_BUILTIN
5274 targetm.init_builtins ();
5276 build_common_builtin_nodes ();
5278 if (flag_cilkplus)
5279 cilk_init_builtins ();
5282 /* Like get_identifier, but avoid warnings about null arguments when
5283 the argument may be NULL for targets where GCC lacks stdint.h type
5284 information. */
5286 static inline tree
5287 c_get_ident (const char *id)
5289 return get_identifier (id);
5292 /* Build tree nodes and builtin functions common to both C and C++ language
5293 frontends. */
5295 void
5296 c_common_nodes_and_builtins (void)
5298 int char16_type_size;
5299 int char32_type_size;
5300 int wchar_type_size;
5301 tree array_domain_type;
5302 tree va_list_ref_type_node;
5303 tree va_list_arg_type_node;
5305 build_common_tree_nodes (flag_signed_char, flag_short_double);
5307 /* Define `int' and `char' first so that dbx will output them first. */
5308 record_builtin_type (RID_INT, NULL, integer_type_node);
5309 record_builtin_type (RID_CHAR, "char", char_type_node);
5311 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5312 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5313 but not C. Are the conditionals here needed? */
5314 if (c_dialect_cxx ())
5315 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5316 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5317 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5318 record_builtin_type (RID_MAX, "long unsigned int",
5319 long_unsigned_type_node);
5320 if (int128_integer_type_node != NULL_TREE)
5322 record_builtin_type (RID_INT128, "__int128",
5323 int128_integer_type_node);
5324 record_builtin_type (RID_MAX, "__int128 unsigned",
5325 int128_unsigned_type_node);
5327 if (c_dialect_cxx ())
5328 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5329 record_builtin_type (RID_MAX, "long long int",
5330 long_long_integer_type_node);
5331 record_builtin_type (RID_MAX, "long long unsigned int",
5332 long_long_unsigned_type_node);
5333 if (c_dialect_cxx ())
5334 record_builtin_type (RID_MAX, "long long unsigned",
5335 long_long_unsigned_type_node);
5336 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5337 record_builtin_type (RID_MAX, "short unsigned int",
5338 short_unsigned_type_node);
5339 if (c_dialect_cxx ())
5340 record_builtin_type (RID_MAX, "unsigned short",
5341 short_unsigned_type_node);
5343 /* Define both `signed char' and `unsigned char'. */
5344 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5345 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5347 /* These are types that c_common_type_for_size and
5348 c_common_type_for_mode use. */
5349 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5350 TYPE_DECL, NULL_TREE,
5351 intQI_type_node));
5352 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5353 TYPE_DECL, NULL_TREE,
5354 intHI_type_node));
5355 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5356 TYPE_DECL, NULL_TREE,
5357 intSI_type_node));
5358 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5359 TYPE_DECL, NULL_TREE,
5360 intDI_type_node));
5361 #if HOST_BITS_PER_WIDE_INT >= 64
5362 if (targetm.scalar_mode_supported_p (TImode))
5363 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5364 TYPE_DECL,
5365 get_identifier ("__int128_t"),
5366 intTI_type_node));
5367 #endif
5368 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5369 TYPE_DECL, NULL_TREE,
5370 unsigned_intQI_type_node));
5371 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5372 TYPE_DECL, NULL_TREE,
5373 unsigned_intHI_type_node));
5374 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5375 TYPE_DECL, NULL_TREE,
5376 unsigned_intSI_type_node));
5377 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5378 TYPE_DECL, NULL_TREE,
5379 unsigned_intDI_type_node));
5380 #if HOST_BITS_PER_WIDE_INT >= 64
5381 if (targetm.scalar_mode_supported_p (TImode))
5382 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5383 TYPE_DECL,
5384 get_identifier ("__uint128_t"),
5385 unsigned_intTI_type_node));
5386 #endif
5388 /* Create the widest literal types. */
5389 widest_integer_literal_type_node
5390 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5391 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5392 TYPE_DECL, NULL_TREE,
5393 widest_integer_literal_type_node));
5395 widest_unsigned_literal_type_node
5396 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5397 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5398 TYPE_DECL, NULL_TREE,
5399 widest_unsigned_literal_type_node));
5401 signed_size_type_node = c_common_signed_type (size_type_node);
5403 pid_type_node =
5404 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5406 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5407 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5408 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5410 /* Only supported decimal floating point extension if the target
5411 actually supports underlying modes. */
5412 if (targetm.scalar_mode_supported_p (SDmode)
5413 && targetm.scalar_mode_supported_p (DDmode)
5414 && targetm.scalar_mode_supported_p (TDmode))
5416 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5417 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5418 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5421 if (targetm.fixed_point_supported_p ())
5423 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5424 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5425 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5426 record_builtin_type (RID_MAX, "long long _Fract",
5427 long_long_fract_type_node);
5428 record_builtin_type (RID_MAX, "unsigned short _Fract",
5429 unsigned_short_fract_type_node);
5430 record_builtin_type (RID_MAX, "unsigned _Fract",
5431 unsigned_fract_type_node);
5432 record_builtin_type (RID_MAX, "unsigned long _Fract",
5433 unsigned_long_fract_type_node);
5434 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5435 unsigned_long_long_fract_type_node);
5436 record_builtin_type (RID_MAX, "_Sat short _Fract",
5437 sat_short_fract_type_node);
5438 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5439 record_builtin_type (RID_MAX, "_Sat long _Fract",
5440 sat_long_fract_type_node);
5441 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5442 sat_long_long_fract_type_node);
5443 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5444 sat_unsigned_short_fract_type_node);
5445 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5446 sat_unsigned_fract_type_node);
5447 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5448 sat_unsigned_long_fract_type_node);
5449 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5450 sat_unsigned_long_long_fract_type_node);
5451 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5452 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5453 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5454 record_builtin_type (RID_MAX, "long long _Accum",
5455 long_long_accum_type_node);
5456 record_builtin_type (RID_MAX, "unsigned short _Accum",
5457 unsigned_short_accum_type_node);
5458 record_builtin_type (RID_MAX, "unsigned _Accum",
5459 unsigned_accum_type_node);
5460 record_builtin_type (RID_MAX, "unsigned long _Accum",
5461 unsigned_long_accum_type_node);
5462 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5463 unsigned_long_long_accum_type_node);
5464 record_builtin_type (RID_MAX, "_Sat short _Accum",
5465 sat_short_accum_type_node);
5466 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5467 record_builtin_type (RID_MAX, "_Sat long _Accum",
5468 sat_long_accum_type_node);
5469 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5470 sat_long_long_accum_type_node);
5471 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5472 sat_unsigned_short_accum_type_node);
5473 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5474 sat_unsigned_accum_type_node);
5475 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5476 sat_unsigned_long_accum_type_node);
5477 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5478 sat_unsigned_long_long_accum_type_node);
5482 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5483 TYPE_DECL,
5484 get_identifier ("complex int"),
5485 complex_integer_type_node));
5486 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5487 TYPE_DECL,
5488 get_identifier ("complex float"),
5489 complex_float_type_node));
5490 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5491 TYPE_DECL,
5492 get_identifier ("complex double"),
5493 complex_double_type_node));
5494 lang_hooks.decls.pushdecl
5495 (build_decl (UNKNOWN_LOCATION,
5496 TYPE_DECL, get_identifier ("complex long double"),
5497 complex_long_double_type_node));
5499 if (c_dialect_cxx ())
5500 /* For C++, make fileptr_type_node a distinct void * type until
5501 FILE type is defined. */
5502 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5504 record_builtin_type (RID_VOID, NULL, void_type_node);
5506 /* Set the TYPE_NAME for any variants that were built before
5507 record_builtin_type gave names to the built-in types. */
5509 tree void_name = TYPE_NAME (void_type_node);
5510 TYPE_NAME (void_type_node) = NULL_TREE;
5511 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5512 = void_name;
5513 TYPE_NAME (void_type_node) = void_name;
5516 /* This node must not be shared. */
5517 void_zero_node = make_node (INTEGER_CST);
5518 TREE_TYPE (void_zero_node) = void_type_node;
5520 void_list_node = build_void_list_node ();
5522 /* Make a type to be the domain of a few array types
5523 whose domains don't really matter.
5524 200 is small enough that it always fits in size_t
5525 and large enough that it can hold most function names for the
5526 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5527 array_domain_type = build_index_type (size_int (200));
5529 /* Make a type for arrays of characters.
5530 With luck nothing will ever really depend on the length of this
5531 array type. */
5532 char_array_type_node
5533 = build_array_type (char_type_node, array_domain_type);
5535 string_type_node = build_pointer_type (char_type_node);
5536 const_string_type_node
5537 = build_pointer_type (build_qualified_type
5538 (char_type_node, TYPE_QUAL_CONST));
5540 /* This is special for C++ so functions can be overloaded. */
5541 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5542 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5543 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5544 underlying_wchar_type_node = wchar_type_node;
5545 if (c_dialect_cxx ())
5547 if (TYPE_UNSIGNED (wchar_type_node))
5548 wchar_type_node = make_unsigned_type (wchar_type_size);
5549 else
5550 wchar_type_node = make_signed_type (wchar_type_size);
5551 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5554 /* This is for wide string constants. */
5555 wchar_array_type_node
5556 = build_array_type (wchar_type_node, array_domain_type);
5558 /* Define 'char16_t'. */
5559 char16_type_node = get_identifier (CHAR16_TYPE);
5560 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5561 char16_type_size = TYPE_PRECISION (char16_type_node);
5562 if (c_dialect_cxx ())
5564 char16_type_node = make_unsigned_type (char16_type_size);
5566 if (cxx_dialect >= cxx11)
5567 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5570 /* This is for UTF-16 string constants. */
5571 char16_array_type_node
5572 = build_array_type (char16_type_node, array_domain_type);
5574 /* Define 'char32_t'. */
5575 char32_type_node = get_identifier (CHAR32_TYPE);
5576 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5577 char32_type_size = TYPE_PRECISION (char32_type_node);
5578 if (c_dialect_cxx ())
5580 char32_type_node = make_unsigned_type (char32_type_size);
5582 if (cxx_dialect >= cxx11)
5583 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5586 /* This is for UTF-32 string constants. */
5587 char32_array_type_node
5588 = build_array_type (char32_type_node, array_domain_type);
5590 wint_type_node =
5591 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5593 intmax_type_node =
5594 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5595 uintmax_type_node =
5596 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5598 if (SIG_ATOMIC_TYPE)
5599 sig_atomic_type_node =
5600 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5601 if (INT8_TYPE)
5602 int8_type_node =
5603 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5604 if (INT16_TYPE)
5605 int16_type_node =
5606 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5607 if (INT32_TYPE)
5608 int32_type_node =
5609 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5610 if (INT64_TYPE)
5611 int64_type_node =
5612 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5613 if (UINT8_TYPE)
5614 uint8_type_node =
5615 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5616 if (UINT16_TYPE)
5617 c_uint16_type_node = uint16_type_node =
5618 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5619 if (UINT32_TYPE)
5620 c_uint32_type_node = uint32_type_node =
5621 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5622 if (UINT64_TYPE)
5623 c_uint64_type_node = uint64_type_node =
5624 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5625 if (INT_LEAST8_TYPE)
5626 int_least8_type_node =
5627 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5628 if (INT_LEAST16_TYPE)
5629 int_least16_type_node =
5630 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5631 if (INT_LEAST32_TYPE)
5632 int_least32_type_node =
5633 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5634 if (INT_LEAST64_TYPE)
5635 int_least64_type_node =
5636 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5637 if (UINT_LEAST8_TYPE)
5638 uint_least8_type_node =
5639 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5640 if (UINT_LEAST16_TYPE)
5641 uint_least16_type_node =
5642 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5643 if (UINT_LEAST32_TYPE)
5644 uint_least32_type_node =
5645 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5646 if (UINT_LEAST64_TYPE)
5647 uint_least64_type_node =
5648 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5649 if (INT_FAST8_TYPE)
5650 int_fast8_type_node =
5651 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5652 if (INT_FAST16_TYPE)
5653 int_fast16_type_node =
5654 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5655 if (INT_FAST32_TYPE)
5656 int_fast32_type_node =
5657 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5658 if (INT_FAST64_TYPE)
5659 int_fast64_type_node =
5660 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5661 if (UINT_FAST8_TYPE)
5662 uint_fast8_type_node =
5663 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5664 if (UINT_FAST16_TYPE)
5665 uint_fast16_type_node =
5666 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5667 if (UINT_FAST32_TYPE)
5668 uint_fast32_type_node =
5669 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5670 if (UINT_FAST64_TYPE)
5671 uint_fast64_type_node =
5672 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5673 if (INTPTR_TYPE)
5674 intptr_type_node =
5675 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5676 if (UINTPTR_TYPE)
5677 uintptr_type_node =
5678 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5680 default_function_type
5681 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5682 ptrdiff_type_node
5683 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5684 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5686 lang_hooks.decls.pushdecl
5687 (build_decl (UNKNOWN_LOCATION,
5688 TYPE_DECL, get_identifier ("__builtin_va_list"),
5689 va_list_type_node));
5690 if (targetm.enum_va_list_p)
5692 int l;
5693 const char *pname;
5694 tree ptype;
5696 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5698 lang_hooks.decls.pushdecl
5699 (build_decl (UNKNOWN_LOCATION,
5700 TYPE_DECL, get_identifier (pname),
5701 ptype));
5706 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5708 va_list_arg_type_node = va_list_ref_type_node =
5709 build_pointer_type (TREE_TYPE (va_list_type_node));
5711 else
5713 va_list_arg_type_node = va_list_type_node;
5714 va_list_ref_type_node = build_reference_type (va_list_type_node);
5717 if (!flag_preprocess_only)
5718 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5720 main_identifier_node = get_identifier ("main");
5722 /* Create the built-in __null node. It is important that this is
5723 not shared. */
5724 null_node = make_node (INTEGER_CST);
5725 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5727 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5728 memset (builtin_types, 0, sizeof (builtin_types));
5731 /* The number of named compound-literals generated thus far. */
5732 static GTY(()) int compound_literal_number;
5734 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5736 void
5737 set_compound_literal_name (tree decl)
5739 char *name;
5740 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5741 compound_literal_number);
5742 compound_literal_number++;
5743 DECL_NAME (decl) = get_identifier (name);
5746 tree
5747 build_va_arg (location_t loc, tree expr, tree type)
5749 expr = build1 (VA_ARG_EXPR, type, expr);
5750 SET_EXPR_LOCATION (expr, loc);
5751 return expr;
5755 /* Linked list of disabled built-in functions. */
5757 typedef struct disabled_builtin
5759 const char *name;
5760 struct disabled_builtin *next;
5761 } disabled_builtin;
5762 static disabled_builtin *disabled_builtins = NULL;
5764 static bool builtin_function_disabled_p (const char *);
5766 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5767 begins with "__builtin_", give an error. */
5769 void
5770 disable_builtin_function (const char *name)
5772 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5773 error ("cannot disable built-in function %qs", name);
5774 else
5776 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5777 new_disabled_builtin->name = name;
5778 new_disabled_builtin->next = disabled_builtins;
5779 disabled_builtins = new_disabled_builtin;
5784 /* Return true if the built-in function NAME has been disabled, false
5785 otherwise. */
5787 static bool
5788 builtin_function_disabled_p (const char *name)
5790 disabled_builtin *p;
5791 for (p = disabled_builtins; p != NULL; p = p->next)
5793 if (strcmp (name, p->name) == 0)
5794 return true;
5796 return false;
5800 /* Worker for DEF_BUILTIN.
5801 Possibly define a builtin function with one or two names.
5802 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5803 nonansi_p and flag_no_nonansi_builtin. */
5805 static void
5806 def_builtin_1 (enum built_in_function fncode,
5807 const char *name,
5808 enum built_in_class fnclass,
5809 tree fntype, tree libtype,
5810 bool both_p, bool fallback_p, bool nonansi_p,
5811 tree fnattrs, bool implicit_p)
5813 tree decl;
5814 const char *libname;
5816 if (fntype == error_mark_node)
5817 return;
5819 gcc_assert ((!both_p && !fallback_p)
5820 || !strncmp (name, "__builtin_",
5821 strlen ("__builtin_")));
5823 libname = name + strlen ("__builtin_");
5824 decl = add_builtin_function (name, fntype, fncode, fnclass,
5825 (fallback_p ? libname : NULL),
5826 fnattrs);
5828 set_builtin_decl (fncode, decl, implicit_p);
5830 if (both_p
5831 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5832 && !(nonansi_p && flag_no_nonansi_builtin))
5833 add_builtin_function (libname, libtype, fncode, fnclass,
5834 NULL, fnattrs);
5837 /* Nonzero if the type T promotes to int. This is (nearly) the
5838 integral promotions defined in ISO C99 6.3.1.1/2. */
5840 bool
5841 c_promoting_integer_type_p (const_tree t)
5843 switch (TREE_CODE (t))
5845 case INTEGER_TYPE:
5846 return (TYPE_MAIN_VARIANT (t) == char_type_node
5847 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5848 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5849 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5850 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5851 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5853 case ENUMERAL_TYPE:
5854 /* ??? Technically all enumerations not larger than an int
5855 promote to an int. But this is used along code paths
5856 that only want to notice a size change. */
5857 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5859 case BOOLEAN_TYPE:
5860 return 1;
5862 default:
5863 return 0;
5867 /* Return 1 if PARMS specifies a fixed number of parameters
5868 and none of their types is affected by default promotions. */
5871 self_promoting_args_p (const_tree parms)
5873 const_tree t;
5874 for (t = parms; t; t = TREE_CHAIN (t))
5876 tree type = TREE_VALUE (t);
5878 if (type == error_mark_node)
5879 continue;
5881 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5882 return 0;
5884 if (type == 0)
5885 return 0;
5887 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5888 return 0;
5890 if (c_promoting_integer_type_p (type))
5891 return 0;
5893 return 1;
5896 /* Recursively remove any '*' or '&' operator from TYPE. */
5897 tree
5898 strip_pointer_operator (tree t)
5900 while (POINTER_TYPE_P (t))
5901 t = TREE_TYPE (t);
5902 return t;
5905 /* Recursively remove pointer or array type from TYPE. */
5906 tree
5907 strip_pointer_or_array_types (tree t)
5909 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5910 t = TREE_TYPE (t);
5911 return t;
5914 /* Used to compare case labels. K1 and K2 are actually tree nodes
5915 representing case labels, or NULL_TREE for a `default' label.
5916 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5917 K2, and 0 if K1 and K2 are equal. */
5920 case_compare (splay_tree_key k1, splay_tree_key k2)
5922 /* Consider a NULL key (such as arises with a `default' label) to be
5923 smaller than anything else. */
5924 if (!k1)
5925 return k2 ? -1 : 0;
5926 else if (!k2)
5927 return k1 ? 1 : 0;
5929 return tree_int_cst_compare ((tree) k1, (tree) k2);
5932 /* Process a case label, located at LOC, for the range LOW_VALUE
5933 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5934 then this case label is actually a `default' label. If only
5935 HIGH_VALUE is NULL_TREE, then case label was declared using the
5936 usual C/C++ syntax, rather than the GNU case range extension.
5937 CASES is a tree containing all the case ranges processed so far;
5938 COND is the condition for the switch-statement itself. Returns the
5939 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5940 is created. */
5942 tree
5943 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5944 tree low_value, tree high_value)
5946 tree type;
5947 tree label;
5948 tree case_label;
5949 splay_tree_node node;
5951 /* Create the LABEL_DECL itself. */
5952 label = create_artificial_label (loc);
5954 /* If there was an error processing the switch condition, bail now
5955 before we get more confused. */
5956 if (!cond || cond == error_mark_node)
5957 goto error_out;
5959 if ((low_value && TREE_TYPE (low_value)
5960 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5961 || (high_value && TREE_TYPE (high_value)
5962 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5964 error_at (loc, "pointers are not permitted as case values");
5965 goto error_out;
5968 /* Case ranges are a GNU extension. */
5969 if (high_value)
5970 pedwarn (loc, OPT_Wpedantic,
5971 "range expressions in switch statements are non-standard");
5973 type = TREE_TYPE (cond);
5974 if (low_value)
5976 low_value = check_case_value (low_value);
5977 low_value = convert_and_check (loc, type, low_value);
5978 if (low_value == error_mark_node)
5979 goto error_out;
5981 if (high_value)
5983 high_value = check_case_value (high_value);
5984 high_value = convert_and_check (loc, type, high_value);
5985 if (high_value == error_mark_node)
5986 goto error_out;
5989 if (low_value && high_value)
5991 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5992 really a case range, even though it was written that way.
5993 Remove the HIGH_VALUE to simplify later processing. */
5994 if (tree_int_cst_equal (low_value, high_value))
5995 high_value = NULL_TREE;
5996 else if (!tree_int_cst_lt (low_value, high_value))
5997 warning_at (loc, 0, "empty range specified");
6000 /* See if the case is in range of the type of the original testing
6001 expression. If both low_value and high_value are out of range,
6002 don't insert the case label and return NULL_TREE. */
6003 if (low_value
6004 && !check_case_bounds (type, orig_type,
6005 &low_value, high_value ? &high_value : NULL))
6006 return NULL_TREE;
6008 /* Look up the LOW_VALUE in the table of case labels we already
6009 have. */
6010 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6011 /* If there was not an exact match, check for overlapping ranges.
6012 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6013 that's a `default' label and the only overlap is an exact match. */
6014 if (!node && (low_value || high_value))
6016 splay_tree_node low_bound;
6017 splay_tree_node high_bound;
6019 /* Even though there wasn't an exact match, there might be an
6020 overlap between this case range and another case range.
6021 Since we've (inductively) not allowed any overlapping case
6022 ranges, we simply need to find the greatest low case label
6023 that is smaller that LOW_VALUE, and the smallest low case
6024 label that is greater than LOW_VALUE. If there is an overlap
6025 it will occur in one of these two ranges. */
6026 low_bound = splay_tree_predecessor (cases,
6027 (splay_tree_key) low_value);
6028 high_bound = splay_tree_successor (cases,
6029 (splay_tree_key) low_value);
6031 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6032 the LOW_VALUE, so there is no need to check unless the
6033 LOW_BOUND is in fact itself a case range. */
6034 if (low_bound
6035 && CASE_HIGH ((tree) low_bound->value)
6036 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6037 low_value) >= 0)
6038 node = low_bound;
6039 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6040 range is bigger than the low end of the current range, so we
6041 are only interested if the current range is a real range, and
6042 not an ordinary case label. */
6043 else if (high_bound
6044 && high_value
6045 && (tree_int_cst_compare ((tree) high_bound->key,
6046 high_value)
6047 <= 0))
6048 node = high_bound;
6050 /* If there was an overlap, issue an error. */
6051 if (node)
6053 tree duplicate = CASE_LABEL ((tree) node->value);
6055 if (high_value)
6057 error_at (loc, "duplicate (or overlapping) case value");
6058 error_at (DECL_SOURCE_LOCATION (duplicate),
6059 "this is the first entry overlapping that value");
6061 else if (low_value)
6063 error_at (loc, "duplicate case value") ;
6064 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6066 else
6068 error_at (loc, "multiple default labels in one switch");
6069 error_at (DECL_SOURCE_LOCATION (duplicate),
6070 "this is the first default label");
6072 goto error_out;
6075 /* Add a CASE_LABEL to the statement-tree. */
6076 case_label = add_stmt (build_case_label (low_value, high_value, label));
6077 /* Register this case label in the splay tree. */
6078 splay_tree_insert (cases,
6079 (splay_tree_key) low_value,
6080 (splay_tree_value) case_label);
6082 return case_label;
6084 error_out:
6085 /* Add a label so that the back-end doesn't think that the beginning of
6086 the switch is unreachable. Note that we do not add a case label, as
6087 that just leads to duplicates and thence to failure later on. */
6088 if (!cases->root)
6090 tree t = create_artificial_label (loc);
6091 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6093 return error_mark_node;
6096 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6097 Used to verify that case values match up with enumerator values. */
6099 static void
6100 match_case_to_enum_1 (tree key, tree type, tree label)
6102 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6104 /* ??? Not working too hard to print the double-word value.
6105 Should perhaps be done with %lwd in the diagnostic routines? */
6106 if (TREE_INT_CST_HIGH (key) == 0)
6107 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6108 TREE_INT_CST_LOW (key));
6109 else if (!TYPE_UNSIGNED (type)
6110 && TREE_INT_CST_HIGH (key) == -1
6111 && TREE_INT_CST_LOW (key) != 0)
6112 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6113 -TREE_INT_CST_LOW (key));
6114 else
6115 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6116 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6117 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6119 if (TYPE_NAME (type) == 0)
6120 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6121 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6122 "case value %qs not in enumerated type",
6123 buf);
6124 else
6125 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6126 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6127 "case value %qs not in enumerated type %qT",
6128 buf, type);
6131 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6132 Used to verify that case values match up with enumerator values. */
6134 static int
6135 match_case_to_enum (splay_tree_node node, void *data)
6137 tree label = (tree) node->value;
6138 tree type = (tree) data;
6140 /* Skip default case. */
6141 if (!CASE_LOW (label))
6142 return 0;
6144 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6145 when we did our enum->case scan. Reset our scratch bit after. */
6146 if (!CASE_LOW_SEEN (label))
6147 match_case_to_enum_1 (CASE_LOW (label), type, label);
6148 else
6149 CASE_LOW_SEEN (label) = 0;
6151 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6152 not set, that means that CASE_HIGH did not appear when we did our
6153 enum->case scan. Reset our scratch bit after. */
6154 if (CASE_HIGH (label))
6156 if (!CASE_HIGH_SEEN (label))
6157 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6158 else
6159 CASE_HIGH_SEEN (label) = 0;
6162 return 0;
6165 /* Handle -Wswitch*. Called from the front end after parsing the
6166 switch construct. */
6167 /* ??? Should probably be somewhere generic, since other languages
6168 besides C and C++ would want this. At the moment, however, C/C++
6169 are the only tree-ssa languages that support enumerations at all,
6170 so the point is moot. */
6172 void
6173 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6174 tree type, tree cond)
6176 splay_tree_node default_node;
6177 splay_tree_node node;
6178 tree chain;
6180 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6181 return;
6183 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6184 if (!default_node)
6185 warning_at (switch_location, OPT_Wswitch_default,
6186 "switch missing default case");
6188 /* From here on, we only care about about enumerated types. */
6189 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6190 return;
6192 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6193 if (!warn_switch_enum && !warn_switch)
6194 return;
6196 /* Check the cases. Warn about case values which are not members of
6197 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6198 there is no default case, check that exactly all enumeration
6199 literals are covered by the cases. */
6201 /* Clearing COND if it is not an integer constant simplifies
6202 the tests inside the loop below. */
6203 if (TREE_CODE (cond) != INTEGER_CST)
6204 cond = NULL_TREE;
6206 /* The time complexity here is O(N*lg(N)) worst case, but for the
6207 common case of monotonically increasing enumerators, it is
6208 O(N), since the nature of the splay tree will keep the next
6209 element adjacent to the root at all times. */
6211 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6213 tree value = TREE_VALUE (chain);
6214 if (TREE_CODE (value) == CONST_DECL)
6215 value = DECL_INITIAL (value);
6216 node = splay_tree_lookup (cases, (splay_tree_key) value);
6217 if (node)
6219 /* Mark the CASE_LOW part of the case entry as seen. */
6220 tree label = (tree) node->value;
6221 CASE_LOW_SEEN (label) = 1;
6222 continue;
6225 /* Even though there wasn't an exact match, there might be a
6226 case range which includes the enumerator's value. */
6227 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6228 if (node && CASE_HIGH ((tree) node->value))
6230 tree label = (tree) node->value;
6231 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6232 if (cmp >= 0)
6234 /* If we match the upper bound exactly, mark the CASE_HIGH
6235 part of the case entry as seen. */
6236 if (cmp == 0)
6237 CASE_HIGH_SEEN (label) = 1;
6238 continue;
6242 /* We've now determined that this enumerated literal isn't
6243 handled by the case labels of the switch statement. */
6245 /* If the switch expression is a constant, we only really care
6246 about whether that constant is handled by the switch. */
6247 if (cond && tree_int_cst_compare (cond, value))
6248 continue;
6250 /* If there is a default_node, the only relevant option is
6251 Wswitch-enum. Otherwise, if both are enabled then we prefer
6252 to warn using -Wswitch because -Wswitch is enabled by -Wall
6253 while -Wswitch-enum is explicit. */
6254 warning_at (switch_location,
6255 (default_node || !warn_switch
6256 ? OPT_Wswitch_enum
6257 : OPT_Wswitch),
6258 "enumeration value %qE not handled in switch",
6259 TREE_PURPOSE (chain));
6262 /* Warn if there are case expressions that don't correspond to
6263 enumerators. This can occur since C and C++ don't enforce
6264 type-checking of assignments to enumeration variables.
6266 The time complexity here is now always O(N) worst case, since
6267 we should have marked both the lower bound and upper bound of
6268 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6269 above. This scan also resets those fields. */
6271 splay_tree_foreach (cases, match_case_to_enum, type);
6274 /* Finish an expression taking the address of LABEL (an
6275 IDENTIFIER_NODE). Returns an expression for the address.
6277 LOC is the location for the expression returned. */
6279 tree
6280 finish_label_address_expr (tree label, location_t loc)
6282 tree result;
6284 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6286 if (label == error_mark_node)
6287 return error_mark_node;
6289 label = lookup_label (label);
6290 if (label == NULL_TREE)
6291 result = null_pointer_node;
6292 else
6294 TREE_USED (label) = 1;
6295 result = build1 (ADDR_EXPR, ptr_type_node, label);
6296 /* The current function is not necessarily uninlinable.
6297 Computed gotos are incompatible with inlining, but the value
6298 here could be used only in a diagnostic, for example. */
6299 protected_set_expr_location (result, loc);
6302 return result;
6306 /* Given a boolean expression ARG, return a tree representing an increment
6307 or decrement (as indicated by CODE) of ARG. The front end must check for
6308 invalid cases (e.g., decrement in C++). */
6309 tree
6310 boolean_increment (enum tree_code code, tree arg)
6312 tree val;
6313 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6315 arg = stabilize_reference (arg);
6316 switch (code)
6318 case PREINCREMENT_EXPR:
6319 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6320 break;
6321 case POSTINCREMENT_EXPR:
6322 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6323 arg = save_expr (arg);
6324 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6325 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6326 break;
6327 case PREDECREMENT_EXPR:
6328 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6329 invert_truthvalue_loc (input_location, arg));
6330 break;
6331 case POSTDECREMENT_EXPR:
6332 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6333 invert_truthvalue_loc (input_location, arg));
6334 arg = save_expr (arg);
6335 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6336 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6337 break;
6338 default:
6339 gcc_unreachable ();
6341 TREE_SIDE_EFFECTS (val) = 1;
6342 return val;
6345 /* Built-in macros for stddef.h and stdint.h, that require macros
6346 defined in this file. */
6347 void
6348 c_stddef_cpp_builtins(void)
6350 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6351 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6352 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6353 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6354 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6355 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6356 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6357 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6358 if (SIG_ATOMIC_TYPE)
6359 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6360 if (INT8_TYPE)
6361 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6362 if (INT16_TYPE)
6363 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6364 if (INT32_TYPE)
6365 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6366 if (INT64_TYPE)
6367 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6368 if (UINT8_TYPE)
6369 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6370 if (UINT16_TYPE)
6371 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6372 if (UINT32_TYPE)
6373 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6374 if (UINT64_TYPE)
6375 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6376 if (INT_LEAST8_TYPE)
6377 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6378 if (INT_LEAST16_TYPE)
6379 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6380 if (INT_LEAST32_TYPE)
6381 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6382 if (INT_LEAST64_TYPE)
6383 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6384 if (UINT_LEAST8_TYPE)
6385 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6386 if (UINT_LEAST16_TYPE)
6387 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6388 if (UINT_LEAST32_TYPE)
6389 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6390 if (UINT_LEAST64_TYPE)
6391 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6392 if (INT_FAST8_TYPE)
6393 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6394 if (INT_FAST16_TYPE)
6395 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6396 if (INT_FAST32_TYPE)
6397 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6398 if (INT_FAST64_TYPE)
6399 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6400 if (UINT_FAST8_TYPE)
6401 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6402 if (UINT_FAST16_TYPE)
6403 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6404 if (UINT_FAST32_TYPE)
6405 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6406 if (UINT_FAST64_TYPE)
6407 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6408 if (INTPTR_TYPE)
6409 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6410 if (UINTPTR_TYPE)
6411 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6414 static void
6415 c_init_attributes (void)
6417 /* Fill in the built_in_attributes array. */
6418 #define DEF_ATTR_NULL_TREE(ENUM) \
6419 built_in_attributes[(int) ENUM] = NULL_TREE;
6420 #define DEF_ATTR_INT(ENUM, VALUE) \
6421 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6422 #define DEF_ATTR_STRING(ENUM, VALUE) \
6423 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6424 #define DEF_ATTR_IDENT(ENUM, STRING) \
6425 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6426 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6427 built_in_attributes[(int) ENUM] \
6428 = tree_cons (built_in_attributes[(int) PURPOSE], \
6429 built_in_attributes[(int) VALUE], \
6430 built_in_attributes[(int) CHAIN]);
6431 #include "builtin-attrs.def"
6432 #undef DEF_ATTR_NULL_TREE
6433 #undef DEF_ATTR_INT
6434 #undef DEF_ATTR_IDENT
6435 #undef DEF_ATTR_TREE_LIST
6438 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6439 identifier as an argument, so the front end shouldn't look it up. */
6441 bool
6442 attribute_takes_identifier_p (const_tree attr_id)
6444 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6445 if (spec == NULL)
6446 /* Unknown attribute that we'll end up ignoring, return true so we
6447 don't complain about an identifier argument. */
6448 return true;
6449 else if (!strcmp ("mode", spec->name)
6450 || !strcmp ("format", spec->name)
6451 || !strcmp ("cleanup", spec->name))
6452 return true;
6453 else
6454 return targetm.attribute_takes_identifier_p (attr_id);
6457 /* Attribute handlers common to C front ends. */
6459 /* Handle a "packed" attribute; arguments as in
6460 struct attribute_spec.handler. */
6462 static tree
6463 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6464 int flags, bool *no_add_attrs)
6466 if (TYPE_P (*node))
6468 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6469 *node = build_variant_type_copy (*node);
6470 TYPE_PACKED (*node) = 1;
6472 else if (TREE_CODE (*node) == FIELD_DECL)
6474 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6475 /* Still pack bitfields. */
6476 && ! DECL_INITIAL (*node))
6477 warning (OPT_Wattributes,
6478 "%qE attribute ignored for field of type %qT",
6479 name, TREE_TYPE (*node));
6480 else
6481 DECL_PACKED (*node) = 1;
6483 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6484 used for DECL_REGISTER. It wouldn't mean anything anyway.
6485 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6486 that changes what the typedef is typing. */
6487 else
6489 warning (OPT_Wattributes, "%qE attribute ignored", name);
6490 *no_add_attrs = true;
6493 return NULL_TREE;
6496 /* Handle a "nocommon" attribute; arguments as in
6497 struct attribute_spec.handler. */
6499 static tree
6500 handle_nocommon_attribute (tree *node, tree name,
6501 tree ARG_UNUSED (args),
6502 int ARG_UNUSED (flags), bool *no_add_attrs)
6504 if (TREE_CODE (*node) == VAR_DECL)
6505 DECL_COMMON (*node) = 0;
6506 else
6508 warning (OPT_Wattributes, "%qE attribute ignored", name);
6509 *no_add_attrs = true;
6512 return NULL_TREE;
6515 /* Handle a "common" attribute; arguments as in
6516 struct attribute_spec.handler. */
6518 static tree
6519 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6520 int ARG_UNUSED (flags), bool *no_add_attrs)
6522 if (TREE_CODE (*node) == VAR_DECL)
6523 DECL_COMMON (*node) = 1;
6524 else
6526 warning (OPT_Wattributes, "%qE attribute ignored", name);
6527 *no_add_attrs = true;
6530 return NULL_TREE;
6533 /* Handle a "noreturn" attribute; arguments as in
6534 struct attribute_spec.handler. */
6536 static tree
6537 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6538 int ARG_UNUSED (flags), bool *no_add_attrs)
6540 tree type = TREE_TYPE (*node);
6542 /* See FIXME comment in c_common_attribute_table. */
6543 if (TREE_CODE (*node) == FUNCTION_DECL
6544 || objc_method_decl (TREE_CODE (*node)))
6545 TREE_THIS_VOLATILE (*node) = 1;
6546 else if (TREE_CODE (type) == POINTER_TYPE
6547 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6548 TREE_TYPE (*node)
6549 = build_pointer_type
6550 (build_type_variant (TREE_TYPE (type),
6551 TYPE_READONLY (TREE_TYPE (type)), 1));
6552 else
6554 warning (OPT_Wattributes, "%qE attribute ignored", name);
6555 *no_add_attrs = true;
6558 return NULL_TREE;
6561 /* Handle a "hot" and attribute; arguments as in
6562 struct attribute_spec.handler. */
6564 static tree
6565 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6566 int ARG_UNUSED (flags), bool *no_add_attrs)
6568 if (TREE_CODE (*node) == FUNCTION_DECL
6569 || TREE_CODE (*node) == LABEL_DECL)
6571 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6573 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6574 name, "cold");
6575 *no_add_attrs = true;
6577 /* Most of the rest of the hot processing is done later with
6578 lookup_attribute. */
6580 else
6582 warning (OPT_Wattributes, "%qE attribute ignored", name);
6583 *no_add_attrs = true;
6586 return NULL_TREE;
6589 /* Handle a "cold" and attribute; arguments as in
6590 struct attribute_spec.handler. */
6592 static tree
6593 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6594 int ARG_UNUSED (flags), bool *no_add_attrs)
6596 if (TREE_CODE (*node) == FUNCTION_DECL
6597 || TREE_CODE (*node) == LABEL_DECL)
6599 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6601 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6602 name, "hot");
6603 *no_add_attrs = true;
6605 /* Most of the rest of the cold processing is done later with
6606 lookup_attribute. */
6608 else
6610 warning (OPT_Wattributes, "%qE attribute ignored", name);
6611 *no_add_attrs = true;
6614 return NULL_TREE;
6617 /* Handle a "no_sanitize_address" attribute; arguments as in
6618 struct attribute_spec.handler. */
6620 static tree
6621 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6622 bool *no_add_attrs)
6624 if (TREE_CODE (*node) != FUNCTION_DECL)
6626 warning (OPT_Wattributes, "%qE attribute ignored", name);
6627 *no_add_attrs = true;
6630 return NULL_TREE;
6633 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6634 struct attribute_spec.handler. */
6636 static tree
6637 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6638 bool *no_add_attrs)
6640 if (TREE_CODE (*node) != FUNCTION_DECL)
6641 warning (OPT_Wattributes, "%qE attribute ignored", name);
6642 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6643 DECL_ATTRIBUTES (*node)
6644 = tree_cons (get_identifier ("no_sanitize_address"),
6645 NULL_TREE, DECL_ATTRIBUTES (*node));
6646 *no_add_attrs = true;
6647 return NULL_TREE;
6650 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6651 struct attribute_spec.handler. */
6653 static tree
6654 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6655 bool *no_add_attrs)
6657 if (TREE_CODE (*node) != FUNCTION_DECL)
6659 warning (OPT_Wattributes, "%qE attribute ignored", name);
6660 *no_add_attrs = true;
6663 return NULL_TREE;
6666 /* Handle a "noinline" attribute; arguments as in
6667 struct attribute_spec.handler. */
6669 static tree
6670 handle_noinline_attribute (tree *node, tree name,
6671 tree ARG_UNUSED (args),
6672 int ARG_UNUSED (flags), bool *no_add_attrs)
6674 if (TREE_CODE (*node) == FUNCTION_DECL)
6675 DECL_UNINLINABLE (*node) = 1;
6676 else
6678 warning (OPT_Wattributes, "%qE attribute ignored", name);
6679 *no_add_attrs = true;
6682 return NULL_TREE;
6685 /* Handle a "noclone" attribute; arguments as in
6686 struct attribute_spec.handler. */
6688 static tree
6689 handle_noclone_attribute (tree *node, tree name,
6690 tree ARG_UNUSED (args),
6691 int ARG_UNUSED (flags), bool *no_add_attrs)
6693 if (TREE_CODE (*node) != FUNCTION_DECL)
6695 warning (OPT_Wattributes, "%qE attribute ignored", name);
6696 *no_add_attrs = true;
6699 return NULL_TREE;
6702 /* Handle a "always_inline" attribute; arguments as in
6703 struct attribute_spec.handler. */
6705 static tree
6706 handle_always_inline_attribute (tree *node, tree name,
6707 tree ARG_UNUSED (args),
6708 int ARG_UNUSED (flags),
6709 bool *no_add_attrs)
6711 if (TREE_CODE (*node) == FUNCTION_DECL)
6713 /* Set the attribute and mark it for disregarding inline
6714 limits. */
6715 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6717 else
6719 warning (OPT_Wattributes, "%qE attribute ignored", name);
6720 *no_add_attrs = true;
6723 return NULL_TREE;
6726 /* Handle a "gnu_inline" attribute; arguments as in
6727 struct attribute_spec.handler. */
6729 static tree
6730 handle_gnu_inline_attribute (tree *node, tree name,
6731 tree ARG_UNUSED (args),
6732 int ARG_UNUSED (flags),
6733 bool *no_add_attrs)
6735 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6737 /* Do nothing else, just set the attribute. We'll get at
6738 it later with lookup_attribute. */
6740 else
6742 warning (OPT_Wattributes, "%qE attribute ignored", name);
6743 *no_add_attrs = true;
6746 return NULL_TREE;
6749 /* Handle a "leaf" attribute; arguments as in
6750 struct attribute_spec.handler. */
6752 static tree
6753 handle_leaf_attribute (tree *node, tree name,
6754 tree ARG_UNUSED (args),
6755 int ARG_UNUSED (flags), bool *no_add_attrs)
6757 if (TREE_CODE (*node) != FUNCTION_DECL)
6759 warning (OPT_Wattributes, "%qE attribute ignored", name);
6760 *no_add_attrs = true;
6762 if (!TREE_PUBLIC (*node))
6764 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6765 *no_add_attrs = true;
6768 return NULL_TREE;
6771 /* Handle an "artificial" attribute; arguments as in
6772 struct attribute_spec.handler. */
6774 static tree
6775 handle_artificial_attribute (tree *node, tree name,
6776 tree ARG_UNUSED (args),
6777 int ARG_UNUSED (flags),
6778 bool *no_add_attrs)
6780 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6782 /* Do nothing else, just set the attribute. We'll get at
6783 it later with lookup_attribute. */
6785 else
6787 warning (OPT_Wattributes, "%qE attribute ignored", name);
6788 *no_add_attrs = true;
6791 return NULL_TREE;
6794 /* Handle a "flatten" attribute; arguments as in
6795 struct attribute_spec.handler. */
6797 static tree
6798 handle_flatten_attribute (tree *node, tree name,
6799 tree args ATTRIBUTE_UNUSED,
6800 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6802 if (TREE_CODE (*node) == FUNCTION_DECL)
6803 /* Do nothing else, just set the attribute. We'll get at
6804 it later with lookup_attribute. */
6806 else
6808 warning (OPT_Wattributes, "%qE attribute ignored", name);
6809 *no_add_attrs = true;
6812 return NULL_TREE;
6815 /* Handle a "warning" or "error" attribute; arguments as in
6816 struct attribute_spec.handler. */
6818 static tree
6819 handle_error_attribute (tree *node, tree name, tree args,
6820 int ARG_UNUSED (flags), bool *no_add_attrs)
6822 if (TREE_CODE (*node) == FUNCTION_DECL
6823 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6824 /* Do nothing else, just set the attribute. We'll get at
6825 it later with lookup_attribute. */
6827 else
6829 warning (OPT_Wattributes, "%qE attribute ignored", name);
6830 *no_add_attrs = true;
6833 return NULL_TREE;
6836 /* Handle a "used" attribute; arguments as in
6837 struct attribute_spec.handler. */
6839 static tree
6840 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6841 int ARG_UNUSED (flags), bool *no_add_attrs)
6843 tree node = *pnode;
6845 if (TREE_CODE (node) == FUNCTION_DECL
6846 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6847 || (TREE_CODE (node) == TYPE_DECL))
6849 TREE_USED (node) = 1;
6850 DECL_PRESERVE_P (node) = 1;
6851 if (TREE_CODE (node) == VAR_DECL)
6852 DECL_READ_P (node) = 1;
6854 else
6856 warning (OPT_Wattributes, "%qE attribute ignored", name);
6857 *no_add_attrs = true;
6860 return NULL_TREE;
6863 /* Handle a "unused" attribute; arguments as in
6864 struct attribute_spec.handler. */
6866 static tree
6867 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6868 int flags, bool *no_add_attrs)
6870 if (DECL_P (*node))
6872 tree decl = *node;
6874 if (TREE_CODE (decl) == PARM_DECL
6875 || TREE_CODE (decl) == VAR_DECL
6876 || TREE_CODE (decl) == FUNCTION_DECL
6877 || TREE_CODE (decl) == LABEL_DECL
6878 || TREE_CODE (decl) == TYPE_DECL)
6880 TREE_USED (decl) = 1;
6881 if (TREE_CODE (decl) == VAR_DECL
6882 || TREE_CODE (decl) == PARM_DECL)
6883 DECL_READ_P (decl) = 1;
6885 else
6887 warning (OPT_Wattributes, "%qE attribute ignored", name);
6888 *no_add_attrs = true;
6891 else
6893 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6894 *node = build_variant_type_copy (*node);
6895 TREE_USED (*node) = 1;
6898 return NULL_TREE;
6901 /* Handle a "externally_visible" attribute; arguments as in
6902 struct attribute_spec.handler. */
6904 static tree
6905 handle_externally_visible_attribute (tree *pnode, tree name,
6906 tree ARG_UNUSED (args),
6907 int ARG_UNUSED (flags),
6908 bool *no_add_attrs)
6910 tree node = *pnode;
6912 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6914 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6915 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6917 warning (OPT_Wattributes,
6918 "%qE attribute have effect only on public objects", name);
6919 *no_add_attrs = true;
6922 else
6924 warning (OPT_Wattributes, "%qE attribute ignored", name);
6925 *no_add_attrs = true;
6928 return NULL_TREE;
6931 /* Handle a "const" attribute; arguments as in
6932 struct attribute_spec.handler. */
6934 static tree
6935 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6936 int ARG_UNUSED (flags), bool *no_add_attrs)
6938 tree type = TREE_TYPE (*node);
6940 /* See FIXME comment on noreturn in c_common_attribute_table. */
6941 if (TREE_CODE (*node) == FUNCTION_DECL)
6942 TREE_READONLY (*node) = 1;
6943 else if (TREE_CODE (type) == POINTER_TYPE
6944 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6945 TREE_TYPE (*node)
6946 = build_pointer_type
6947 (build_type_variant (TREE_TYPE (type), 1,
6948 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6949 else
6951 warning (OPT_Wattributes, "%qE attribute ignored", name);
6952 *no_add_attrs = true;
6955 return NULL_TREE;
6958 /* Handle a "transparent_union" attribute; arguments as in
6959 struct attribute_spec.handler. */
6961 static tree
6962 handle_transparent_union_attribute (tree *node, tree name,
6963 tree ARG_UNUSED (args), int flags,
6964 bool *no_add_attrs)
6966 tree type;
6968 *no_add_attrs = true;
6971 if (TREE_CODE (*node) == TYPE_DECL
6972 && ! (flags & ATTR_FLAG_CXX11))
6973 node = &TREE_TYPE (*node);
6974 type = *node;
6976 if (TREE_CODE (type) == UNION_TYPE)
6978 /* Make sure that the first field will work for a transparent union.
6979 If the type isn't complete yet, leave the check to the code in
6980 finish_struct. */
6981 if (TYPE_SIZE (type))
6983 tree first = first_field (type);
6984 if (first == NULL_TREE
6985 || DECL_ARTIFICIAL (first)
6986 || TYPE_MODE (type) != DECL_MODE (first))
6987 goto ignored;
6990 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6992 /* If the type isn't complete yet, setting the flag
6993 on a variant wouldn't ever be checked. */
6994 if (!TYPE_SIZE (type))
6995 goto ignored;
6997 /* build_duplicate_type doesn't work for C++. */
6998 if (c_dialect_cxx ())
6999 goto ignored;
7001 /* A type variant isn't good enough, since we don't a cast
7002 to such a type removed as a no-op. */
7003 *node = type = build_duplicate_type (type);
7006 TYPE_TRANSPARENT_AGGR (type) = 1;
7007 return NULL_TREE;
7010 ignored:
7011 warning (OPT_Wattributes, "%qE attribute ignored", name);
7012 return NULL_TREE;
7015 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7016 get the requested priority for a constructor or destructor,
7017 possibly issuing diagnostics for invalid or reserved
7018 priorities. */
7020 static priority_type
7021 get_priority (tree args, bool is_destructor)
7023 HOST_WIDE_INT pri;
7024 tree arg;
7026 if (!args)
7027 return DEFAULT_INIT_PRIORITY;
7029 if (!SUPPORTS_INIT_PRIORITY)
7031 if (is_destructor)
7032 error ("destructor priorities are not supported");
7033 else
7034 error ("constructor priorities are not supported");
7035 return DEFAULT_INIT_PRIORITY;
7038 arg = TREE_VALUE (args);
7039 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7040 goto invalid;
7041 if (arg == error_mark_node)
7042 return DEFAULT_INIT_PRIORITY;
7043 arg = default_conversion (arg);
7044 if (!tree_fits_shwi_p (arg)
7045 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7046 goto invalid;
7048 pri = tree_to_shwi (arg);
7049 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7050 goto invalid;
7052 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7054 if (is_destructor)
7055 warning (0,
7056 "destructor priorities from 0 to %d are reserved "
7057 "for the implementation",
7058 MAX_RESERVED_INIT_PRIORITY);
7059 else
7060 warning (0,
7061 "constructor priorities from 0 to %d are reserved "
7062 "for the implementation",
7063 MAX_RESERVED_INIT_PRIORITY);
7065 return pri;
7067 invalid:
7068 if (is_destructor)
7069 error ("destructor priorities must be integers from 0 to %d inclusive",
7070 MAX_INIT_PRIORITY);
7071 else
7072 error ("constructor priorities must be integers from 0 to %d inclusive",
7073 MAX_INIT_PRIORITY);
7074 return DEFAULT_INIT_PRIORITY;
7077 /* Handle a "constructor" attribute; arguments as in
7078 struct attribute_spec.handler. */
7080 static tree
7081 handle_constructor_attribute (tree *node, tree name, tree args,
7082 int ARG_UNUSED (flags),
7083 bool *no_add_attrs)
7085 tree decl = *node;
7086 tree type = TREE_TYPE (decl);
7088 if (TREE_CODE (decl) == FUNCTION_DECL
7089 && TREE_CODE (type) == FUNCTION_TYPE
7090 && decl_function_context (decl) == 0)
7092 priority_type priority;
7093 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7094 priority = get_priority (args, /*is_destructor=*/false);
7095 SET_DECL_INIT_PRIORITY (decl, priority);
7096 TREE_USED (decl) = 1;
7098 else
7100 warning (OPT_Wattributes, "%qE attribute ignored", name);
7101 *no_add_attrs = true;
7104 return NULL_TREE;
7107 /* Handle a "destructor" attribute; arguments as in
7108 struct attribute_spec.handler. */
7110 static tree
7111 handle_destructor_attribute (tree *node, tree name, tree args,
7112 int ARG_UNUSED (flags),
7113 bool *no_add_attrs)
7115 tree decl = *node;
7116 tree type = TREE_TYPE (decl);
7118 if (TREE_CODE (decl) == FUNCTION_DECL
7119 && TREE_CODE (type) == FUNCTION_TYPE
7120 && decl_function_context (decl) == 0)
7122 priority_type priority;
7123 DECL_STATIC_DESTRUCTOR (decl) = 1;
7124 priority = get_priority (args, /*is_destructor=*/true);
7125 SET_DECL_FINI_PRIORITY (decl, priority);
7126 TREE_USED (decl) = 1;
7128 else
7130 warning (OPT_Wattributes, "%qE attribute ignored", name);
7131 *no_add_attrs = true;
7134 return NULL_TREE;
7137 /* Nonzero if the mode is a valid vector mode for this architecture.
7138 This returns nonzero even if there is no hardware support for the
7139 vector mode, but we can emulate with narrower modes. */
7141 static int
7142 vector_mode_valid_p (enum machine_mode mode)
7144 enum mode_class mclass = GET_MODE_CLASS (mode);
7145 enum machine_mode innermode;
7147 /* Doh! What's going on? */
7148 if (mclass != MODE_VECTOR_INT
7149 && mclass != MODE_VECTOR_FLOAT
7150 && mclass != MODE_VECTOR_FRACT
7151 && mclass != MODE_VECTOR_UFRACT
7152 && mclass != MODE_VECTOR_ACCUM
7153 && mclass != MODE_VECTOR_UACCUM)
7154 return 0;
7156 /* Hardware support. Woo hoo! */
7157 if (targetm.vector_mode_supported_p (mode))
7158 return 1;
7160 innermode = GET_MODE_INNER (mode);
7162 /* We should probably return 1 if requesting V4DI and we have no DI,
7163 but we have V2DI, but this is probably very unlikely. */
7165 /* If we have support for the inner mode, we can safely emulate it.
7166 We may not have V2DI, but me can emulate with a pair of DIs. */
7167 return targetm.scalar_mode_supported_p (innermode);
7171 /* Handle a "mode" attribute; arguments as in
7172 struct attribute_spec.handler. */
7174 static tree
7175 handle_mode_attribute (tree *node, tree name, tree args,
7176 int ARG_UNUSED (flags), bool *no_add_attrs)
7178 tree type = *node;
7179 tree ident = TREE_VALUE (args);
7181 *no_add_attrs = true;
7183 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7184 warning (OPT_Wattributes, "%qE attribute ignored", name);
7185 else
7187 int j;
7188 const char *p = IDENTIFIER_POINTER (ident);
7189 int len = strlen (p);
7190 enum machine_mode mode = VOIDmode;
7191 tree typefm;
7192 bool valid_mode;
7194 if (len > 4 && p[0] == '_' && p[1] == '_'
7195 && p[len - 1] == '_' && p[len - 2] == '_')
7197 char *newp = (char *) alloca (len - 1);
7199 strcpy (newp, &p[2]);
7200 newp[len - 4] = '\0';
7201 p = newp;
7204 /* Change this type to have a type with the specified mode.
7205 First check for the special modes. */
7206 if (!strcmp (p, "byte"))
7207 mode = byte_mode;
7208 else if (!strcmp (p, "word"))
7209 mode = word_mode;
7210 else if (!strcmp (p, "pointer"))
7211 mode = ptr_mode;
7212 else if (!strcmp (p, "libgcc_cmp_return"))
7213 mode = targetm.libgcc_cmp_return_mode ();
7214 else if (!strcmp (p, "libgcc_shift_count"))
7215 mode = targetm.libgcc_shift_count_mode ();
7216 else if (!strcmp (p, "unwind_word"))
7217 mode = targetm.unwind_word_mode ();
7218 else
7219 for (j = 0; j < NUM_MACHINE_MODES; j++)
7220 if (!strcmp (p, GET_MODE_NAME (j)))
7222 mode = (enum machine_mode) j;
7223 break;
7226 if (mode == VOIDmode)
7228 error ("unknown machine mode %qE", ident);
7229 return NULL_TREE;
7232 valid_mode = false;
7233 switch (GET_MODE_CLASS (mode))
7235 case MODE_INT:
7236 case MODE_PARTIAL_INT:
7237 case MODE_FLOAT:
7238 case MODE_DECIMAL_FLOAT:
7239 case MODE_FRACT:
7240 case MODE_UFRACT:
7241 case MODE_ACCUM:
7242 case MODE_UACCUM:
7243 valid_mode = targetm.scalar_mode_supported_p (mode);
7244 break;
7246 case MODE_COMPLEX_INT:
7247 case MODE_COMPLEX_FLOAT:
7248 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7249 break;
7251 case MODE_VECTOR_INT:
7252 case MODE_VECTOR_FLOAT:
7253 case MODE_VECTOR_FRACT:
7254 case MODE_VECTOR_UFRACT:
7255 case MODE_VECTOR_ACCUM:
7256 case MODE_VECTOR_UACCUM:
7257 warning (OPT_Wattributes, "specifying vector types with "
7258 "__attribute__ ((mode)) is deprecated");
7259 warning (OPT_Wattributes,
7260 "use __attribute__ ((vector_size)) instead");
7261 valid_mode = vector_mode_valid_p (mode);
7262 break;
7264 default:
7265 break;
7267 if (!valid_mode)
7269 error ("unable to emulate %qs", p);
7270 return NULL_TREE;
7273 if (POINTER_TYPE_P (type))
7275 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7276 tree (*fn)(tree, enum machine_mode, bool);
7278 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7280 error ("invalid pointer mode %qs", p);
7281 return NULL_TREE;
7284 if (TREE_CODE (type) == POINTER_TYPE)
7285 fn = build_pointer_type_for_mode;
7286 else
7287 fn = build_reference_type_for_mode;
7288 typefm = fn (TREE_TYPE (type), mode, false);
7290 else
7292 /* For fixed-point modes, we need to test if the signness of type
7293 and the machine mode are consistent. */
7294 if (ALL_FIXED_POINT_MODE_P (mode)
7295 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7297 error ("signedness of type and machine mode %qs don%'t match", p);
7298 return NULL_TREE;
7300 /* For fixed-point modes, we need to pass saturating info. */
7301 typefm = lang_hooks.types.type_for_mode (mode,
7302 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7303 : TYPE_UNSIGNED (type));
7306 if (typefm == NULL_TREE)
7308 error ("no data type for mode %qs", p);
7309 return NULL_TREE;
7311 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7313 /* For enumeral types, copy the precision from the integer
7314 type returned above. If not an INTEGER_TYPE, we can't use
7315 this mode for this type. */
7316 if (TREE_CODE (typefm) != INTEGER_TYPE)
7318 error ("cannot use mode %qs for enumeral types", p);
7319 return NULL_TREE;
7322 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7324 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7325 typefm = type;
7327 else
7329 /* We cannot build a type variant, as there's code that assumes
7330 that TYPE_MAIN_VARIANT has the same mode. This includes the
7331 debug generators. Instead, create a subrange type. This
7332 results in all of the enumeral values being emitted only once
7333 in the original, and the subtype gets them by reference. */
7334 if (TYPE_UNSIGNED (type))
7335 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7336 else
7337 typefm = make_signed_type (TYPE_PRECISION (typefm));
7338 TREE_TYPE (typefm) = type;
7341 else if (VECTOR_MODE_P (mode)
7342 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7343 : TREE_CODE (type) != TREE_CODE (typefm))
7345 error ("mode %qs applied to inappropriate type", p);
7346 return NULL_TREE;
7349 *node = typefm;
7352 return NULL_TREE;
7355 /* Handle a "section" attribute; arguments as in
7356 struct attribute_spec.handler. */
7358 static tree
7359 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7360 int ARG_UNUSED (flags), bool *no_add_attrs)
7362 tree decl = *node;
7364 if (targetm_common.have_named_sections)
7366 user_defined_section_attribute = true;
7368 if ((TREE_CODE (decl) == FUNCTION_DECL
7369 || TREE_CODE (decl) == VAR_DECL)
7370 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7372 if (TREE_CODE (decl) == VAR_DECL
7373 && current_function_decl != NULL_TREE
7374 && !TREE_STATIC (decl))
7376 error_at (DECL_SOURCE_LOCATION (decl),
7377 "section attribute cannot be specified for "
7378 "local variables");
7379 *no_add_attrs = true;
7382 /* The decl may have already been given a section attribute
7383 from a previous declaration. Ensure they match. */
7384 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7385 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7386 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7388 error ("section of %q+D conflicts with previous declaration",
7389 *node);
7390 *no_add_attrs = true;
7392 else if (TREE_CODE (decl) == VAR_DECL
7393 && !targetm.have_tls && targetm.emutls.tmpl_section
7394 && DECL_THREAD_LOCAL_P (decl))
7396 error ("section of %q+D cannot be overridden", *node);
7397 *no_add_attrs = true;
7399 else
7400 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7402 else
7404 error ("section attribute not allowed for %q+D", *node);
7405 *no_add_attrs = true;
7408 else
7410 error_at (DECL_SOURCE_LOCATION (*node),
7411 "section attributes are not supported for this target");
7412 *no_add_attrs = true;
7415 return NULL_TREE;
7418 /* Check whether ALIGN is a valid user-specified alignment. If so,
7419 return its base-2 log; if not, output an error and return -1. If
7420 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7421 no error. */
7423 check_user_alignment (const_tree align, bool allow_zero)
7425 int i;
7427 if (TREE_CODE (align) != INTEGER_CST
7428 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7430 error ("requested alignment is not an integer constant");
7431 return -1;
7433 else if (allow_zero && integer_zerop (align))
7434 return -1;
7435 else if (tree_int_cst_sgn (align) == -1
7436 || (i = tree_log2 (align)) == -1)
7438 error ("requested alignment is not a positive power of 2");
7439 return -1;
7441 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7443 error ("requested alignment is too large");
7444 return -1;
7446 return i;
7450 If in c++-11, check if the c++-11 alignment constraint with respect
7451 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7452 c++-11 mode, does nothing.
7454 [dcl.align]2/ says:
7456 [* if the constant expression evaluates to a fundamental alignment,
7457 the alignment requirement of the declared entity shall be the
7458 specified fundamental alignment.
7460 * if the constant expression evaluates to an extended alignment
7461 and the implementation supports that alignment in the context
7462 of the declaration, the alignment of the declared entity shall
7463 be that alignment
7465 * if the constant expression evaluates to an extended alignment
7466 and the implementation does not support that alignment in the
7467 context of the declaration, the program is ill-formed]. */
7469 static bool
7470 check_cxx_fundamental_alignment_constraints (tree node,
7471 unsigned align_log,
7472 int flags)
7474 bool alignment_too_large_p = false;
7475 unsigned requested_alignment = 1U << align_log;
7476 unsigned max_align = 0;
7478 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7479 || (node == NULL_TREE || node == error_mark_node))
7480 return true;
7482 if (cxx_fundamental_alignment_p (requested_alignment))
7483 return true;
7485 if (DECL_P (node))
7487 if (TREE_STATIC (node))
7489 /* For file scope variables and static members, the target
7490 supports alignments that are at most
7491 MAX_OFILE_ALIGNMENT. */
7492 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7493 alignment_too_large_p = true;
7495 else
7497 #ifdef BIGGEST_FIELD_ALIGNMENT
7498 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7499 #else
7500 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7501 #endif
7502 /* For non-static members, the target supports either
7503 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7504 if it is defined or BIGGEST_ALIGNMENT. */
7505 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7506 if (TREE_CODE (node) == FIELD_DECL
7507 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7508 alignment_too_large_p = true;
7509 #undef MAX_TARGET_FIELD_ALIGNMENT
7510 /* For stack variables, the target supports at most
7511 MAX_STACK_ALIGNMENT. */
7512 else if (decl_function_context (node) != NULL
7513 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7514 alignment_too_large_p = true;
7517 else if (TYPE_P (node))
7519 /* Let's be liberal for types. */
7520 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7521 alignment_too_large_p = true;
7524 if (alignment_too_large_p)
7525 pedwarn (input_location, OPT_Wattributes,
7526 "requested alignment %d is larger than %d",
7527 requested_alignment, max_align);
7529 return !alignment_too_large_p;
7532 /* Handle a "aligned" attribute; arguments as in
7533 struct attribute_spec.handler. */
7535 static tree
7536 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7537 int flags, bool *no_add_attrs)
7539 tree decl = NULL_TREE;
7540 tree *type = NULL;
7541 int is_type = 0;
7542 tree align_expr;
7543 int i;
7545 if (args)
7547 align_expr = TREE_VALUE (args);
7548 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE)
7549 align_expr = default_conversion (align_expr);
7551 else
7552 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7554 if (DECL_P (*node))
7556 decl = *node;
7557 type = &TREE_TYPE (decl);
7558 is_type = TREE_CODE (*node) == TYPE_DECL;
7560 else if (TYPE_P (*node))
7561 type = node, is_type = 1;
7563 if ((i = check_user_alignment (align_expr, false)) == -1
7564 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7565 *no_add_attrs = true;
7566 else if (is_type)
7568 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7569 /* OK, modify the type in place. */;
7570 /* If we have a TYPE_DECL, then copy the type, so that we
7571 don't accidentally modify a builtin type. See pushdecl. */
7572 else if (decl && TREE_TYPE (decl) != error_mark_node
7573 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7575 tree tt = TREE_TYPE (decl);
7576 *type = build_variant_type_copy (*type);
7577 DECL_ORIGINAL_TYPE (decl) = tt;
7578 TYPE_NAME (*type) = decl;
7579 TREE_USED (*type) = TREE_USED (decl);
7580 TREE_TYPE (decl) = *type;
7582 else
7583 *type = build_variant_type_copy (*type);
7585 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7586 TYPE_USER_ALIGN (*type) = 1;
7588 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7589 && TREE_CODE (decl) != FIELD_DECL)
7591 error ("alignment may not be specified for %q+D", decl);
7592 *no_add_attrs = true;
7594 else if (DECL_USER_ALIGN (decl)
7595 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7596 /* C++-11 [dcl.align/4]:
7598 When multiple alignment-specifiers are specified for an
7599 entity, the alignment requirement shall be set to the
7600 strictest specified alignment.
7602 This formally comes from the c++11 specification but we are
7603 doing it for the GNU attribute syntax as well. */
7604 *no_add_attrs = true;
7605 else if (TREE_CODE (decl) == FUNCTION_DECL
7606 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7608 if (DECL_USER_ALIGN (decl))
7609 error ("alignment for %q+D was previously specified as %d "
7610 "and may not be decreased", decl,
7611 DECL_ALIGN (decl) / BITS_PER_UNIT);
7612 else
7613 error ("alignment for %q+D must be at least %d", decl,
7614 DECL_ALIGN (decl) / BITS_PER_UNIT);
7615 *no_add_attrs = true;
7617 else
7619 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7620 DECL_USER_ALIGN (decl) = 1;
7623 return NULL_TREE;
7626 /* Handle a "weak" attribute; arguments as in
7627 struct attribute_spec.handler. */
7629 static tree
7630 handle_weak_attribute (tree *node, tree name,
7631 tree ARG_UNUSED (args),
7632 int ARG_UNUSED (flags),
7633 bool * ARG_UNUSED (no_add_attrs))
7635 if (TREE_CODE (*node) == FUNCTION_DECL
7636 && DECL_DECLARED_INLINE_P (*node))
7638 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7639 *no_add_attrs = true;
7641 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7643 error ("indirect function %q+D cannot be declared weak", *node);
7644 *no_add_attrs = true;
7645 return NULL_TREE;
7647 else if (TREE_CODE (*node) == FUNCTION_DECL
7648 || TREE_CODE (*node) == VAR_DECL)
7649 declare_weak (*node);
7650 else
7651 warning (OPT_Wattributes, "%qE attribute ignored", name);
7653 return NULL_TREE;
7656 /* Handle an "alias" or "ifunc" attribute; arguments as in
7657 struct attribute_spec.handler, except that IS_ALIAS tells us
7658 whether this is an alias as opposed to ifunc attribute. */
7660 static tree
7661 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7662 bool *no_add_attrs)
7664 tree decl = *node;
7666 if (TREE_CODE (decl) != FUNCTION_DECL
7667 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7669 warning (OPT_Wattributes, "%qE attribute ignored", name);
7670 *no_add_attrs = true;
7672 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7673 || (TREE_CODE (decl) != FUNCTION_DECL
7674 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7675 /* A static variable declaration is always a tentative definition,
7676 but the alias is a non-tentative definition which overrides. */
7677 || (TREE_CODE (decl) != FUNCTION_DECL
7678 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7680 error ("%q+D defined both normally and as %qE attribute", decl, name);
7681 *no_add_attrs = true;
7682 return NULL_TREE;
7684 else if (!is_alias
7685 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7686 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7688 error ("weak %q+D cannot be defined %qE", decl, name);
7689 *no_add_attrs = true;
7690 return NULL_TREE;
7693 /* Note that the very first time we process a nested declaration,
7694 decl_function_context will not be set. Indeed, *would* never
7695 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7696 we do below. After such frobbery, pushdecl would set the context.
7697 In any case, this is never what we want. */
7698 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7700 tree id;
7702 id = TREE_VALUE (args);
7703 if (TREE_CODE (id) != STRING_CST)
7705 error ("attribute %qE argument not a string", name);
7706 *no_add_attrs = true;
7707 return NULL_TREE;
7709 id = get_identifier (TREE_STRING_POINTER (id));
7710 /* This counts as a use of the object pointed to. */
7711 TREE_USED (id) = 1;
7713 if (TREE_CODE (decl) == FUNCTION_DECL)
7714 DECL_INITIAL (decl) = error_mark_node;
7715 else
7716 TREE_STATIC (decl) = 1;
7718 if (!is_alias)
7719 /* ifuncs are also aliases, so set that attribute too. */
7720 DECL_ATTRIBUTES (decl)
7721 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7723 else
7725 warning (OPT_Wattributes, "%qE attribute ignored", name);
7726 *no_add_attrs = true;
7729 return NULL_TREE;
7732 /* Handle an "alias" or "ifunc" attribute; arguments as in
7733 struct attribute_spec.handler. */
7735 static tree
7736 handle_ifunc_attribute (tree *node, tree name, tree args,
7737 int ARG_UNUSED (flags), bool *no_add_attrs)
7739 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7742 /* Handle an "alias" or "ifunc" attribute; arguments as in
7743 struct attribute_spec.handler. */
7745 static tree
7746 handle_alias_attribute (tree *node, tree name, tree args,
7747 int ARG_UNUSED (flags), bool *no_add_attrs)
7749 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7752 /* Handle a "weakref" attribute; arguments as in struct
7753 attribute_spec.handler. */
7755 static tree
7756 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7757 int flags, bool *no_add_attrs)
7759 tree attr = NULL_TREE;
7761 /* We must ignore the attribute when it is associated with
7762 local-scoped decls, since attribute alias is ignored and many
7763 such symbols do not even have a DECL_WEAK field. */
7764 if (decl_function_context (*node)
7765 || current_function_decl
7766 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7768 warning (OPT_Wattributes, "%qE attribute ignored", name);
7769 *no_add_attrs = true;
7770 return NULL_TREE;
7773 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7775 error ("indirect function %q+D cannot be declared weakref", *node);
7776 *no_add_attrs = true;
7777 return NULL_TREE;
7780 /* The idea here is that `weakref("name")' mutates into `weakref,
7781 alias("name")', and weakref without arguments, in turn,
7782 implicitly adds weak. */
7784 if (args)
7786 attr = tree_cons (get_identifier ("alias"), args, attr);
7787 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7789 *no_add_attrs = true;
7791 decl_attributes (node, attr, flags);
7793 else
7795 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7796 error_at (DECL_SOURCE_LOCATION (*node),
7797 "weakref attribute must appear before alias attribute");
7799 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7800 and that isn't supported; and because it wants to add it to
7801 the list of weak decls, which isn't helpful. */
7802 DECL_WEAK (*node) = 1;
7805 return NULL_TREE;
7808 /* Handle an "visibility" attribute; arguments as in
7809 struct attribute_spec.handler. */
7811 static tree
7812 handle_visibility_attribute (tree *node, tree name, tree args,
7813 int ARG_UNUSED (flags),
7814 bool *ARG_UNUSED (no_add_attrs))
7816 tree decl = *node;
7817 tree id = TREE_VALUE (args);
7818 enum symbol_visibility vis;
7820 if (TYPE_P (*node))
7822 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7823 /* OK */;
7824 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7826 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7827 name);
7828 return NULL_TREE;
7830 else if (TYPE_FIELDS (*node))
7832 error ("%qE attribute ignored because %qT is already defined",
7833 name, *node);
7834 return NULL_TREE;
7837 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7839 warning (OPT_Wattributes, "%qE attribute ignored", name);
7840 return NULL_TREE;
7843 if (TREE_CODE (id) != STRING_CST)
7845 error ("visibility argument not a string");
7846 return NULL_TREE;
7849 /* If this is a type, set the visibility on the type decl. */
7850 if (TYPE_P (decl))
7852 decl = TYPE_NAME (decl);
7853 if (!decl)
7854 return NULL_TREE;
7855 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7857 warning (OPT_Wattributes, "%qE attribute ignored on types",
7858 name);
7859 return NULL_TREE;
7863 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7864 vis = VISIBILITY_DEFAULT;
7865 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7866 vis = VISIBILITY_INTERNAL;
7867 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7868 vis = VISIBILITY_HIDDEN;
7869 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7870 vis = VISIBILITY_PROTECTED;
7871 else
7873 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7874 vis = VISIBILITY_DEFAULT;
7877 if (DECL_VISIBILITY_SPECIFIED (decl)
7878 && vis != DECL_VISIBILITY (decl))
7880 tree attributes = (TYPE_P (*node)
7881 ? TYPE_ATTRIBUTES (*node)
7882 : DECL_ATTRIBUTES (decl));
7883 if (lookup_attribute ("visibility", attributes))
7884 error ("%qD redeclared with different visibility", decl);
7885 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7886 && lookup_attribute ("dllimport", attributes))
7887 error ("%qD was declared %qs which implies default visibility",
7888 decl, "dllimport");
7889 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7890 && lookup_attribute ("dllexport", attributes))
7891 error ("%qD was declared %qs which implies default visibility",
7892 decl, "dllexport");
7895 DECL_VISIBILITY (decl) = vis;
7896 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7898 /* Go ahead and attach the attribute to the node as well. This is needed
7899 so we can determine whether we have VISIBILITY_DEFAULT because the
7900 visibility was not specified, or because it was explicitly overridden
7901 from the containing scope. */
7903 return NULL_TREE;
7906 /* Determine the ELF symbol visibility for DECL, which is either a
7907 variable or a function. It is an error to use this function if a
7908 definition of DECL is not available in this translation unit.
7909 Returns true if the final visibility has been determined by this
7910 function; false if the caller is free to make additional
7911 modifications. */
7913 bool
7914 c_determine_visibility (tree decl)
7916 gcc_assert (TREE_CODE (decl) == VAR_DECL
7917 || TREE_CODE (decl) == FUNCTION_DECL);
7919 /* If the user explicitly specified the visibility with an
7920 attribute, honor that. DECL_VISIBILITY will have been set during
7921 the processing of the attribute. We check for an explicit
7922 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7923 to distinguish the use of an attribute from the use of a "#pragma
7924 GCC visibility push(...)"; in the latter case we still want other
7925 considerations to be able to overrule the #pragma. */
7926 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7927 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7928 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7929 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7930 return true;
7932 /* Set default visibility to whatever the user supplied with
7933 visibility_specified depending on #pragma GCC visibility. */
7934 if (!DECL_VISIBILITY_SPECIFIED (decl))
7936 if (visibility_options.inpragma
7937 || DECL_VISIBILITY (decl) != default_visibility)
7939 DECL_VISIBILITY (decl) = default_visibility;
7940 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7941 /* If visibility changed and DECL already has DECL_RTL, ensure
7942 symbol flags are updated. */
7943 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7944 || TREE_CODE (decl) == FUNCTION_DECL)
7945 && DECL_RTL_SET_P (decl))
7946 make_decl_rtl (decl);
7949 return false;
7952 /* Handle an "tls_model" attribute; arguments as in
7953 struct attribute_spec.handler. */
7955 static tree
7956 handle_tls_model_attribute (tree *node, tree name, tree args,
7957 int ARG_UNUSED (flags), bool *no_add_attrs)
7959 tree id;
7960 tree decl = *node;
7961 enum tls_model kind;
7963 *no_add_attrs = true;
7965 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7967 warning (OPT_Wattributes, "%qE attribute ignored", name);
7968 return NULL_TREE;
7971 kind = DECL_TLS_MODEL (decl);
7972 id = TREE_VALUE (args);
7973 if (TREE_CODE (id) != STRING_CST)
7975 error ("tls_model argument not a string");
7976 return NULL_TREE;
7979 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7980 kind = TLS_MODEL_LOCAL_EXEC;
7981 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7982 kind = TLS_MODEL_INITIAL_EXEC;
7983 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7984 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7985 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7986 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7987 else
7988 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7990 DECL_TLS_MODEL (decl) = kind;
7991 return NULL_TREE;
7994 /* Handle a "no_instrument_function" attribute; arguments as in
7995 struct attribute_spec.handler. */
7997 static tree
7998 handle_no_instrument_function_attribute (tree *node, tree name,
7999 tree ARG_UNUSED (args),
8000 int ARG_UNUSED (flags),
8001 bool *no_add_attrs)
8003 tree decl = *node;
8005 if (TREE_CODE (decl) != FUNCTION_DECL)
8007 error_at (DECL_SOURCE_LOCATION (decl),
8008 "%qE attribute applies only to functions", name);
8009 *no_add_attrs = true;
8011 else
8012 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8014 return NULL_TREE;
8017 /* Handle a "malloc" attribute; arguments as in
8018 struct attribute_spec.handler. */
8020 static tree
8021 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8022 int ARG_UNUSED (flags), bool *no_add_attrs)
8024 if (TREE_CODE (*node) == FUNCTION_DECL
8025 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8026 DECL_IS_MALLOC (*node) = 1;
8027 else
8029 warning (OPT_Wattributes, "%qE attribute ignored", name);
8030 *no_add_attrs = true;
8033 return NULL_TREE;
8036 /* Handle a "alloc_size" attribute; arguments as in
8037 struct attribute_spec.handler. */
8039 static tree
8040 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8041 int ARG_UNUSED (flags), bool *no_add_attrs)
8043 unsigned arg_count = type_num_arguments (*node);
8044 for (; args; args = TREE_CHAIN (args))
8046 tree position = TREE_VALUE (args);
8047 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8048 && TREE_CODE (position) != FUNCTION_DECL)
8049 position = default_conversion (position);
8051 if (!tree_fits_uhwi_p (position)
8052 || !arg_count
8053 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8055 warning (OPT_Wattributes,
8056 "alloc_size parameter outside range");
8057 *no_add_attrs = true;
8058 return NULL_TREE;
8061 return NULL_TREE;
8064 /* Handle a "alloc_align" attribute; arguments as in
8065 struct attribute_spec.handler. */
8067 static tree
8068 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8069 bool *no_add_attrs)
8071 unsigned arg_count = type_num_arguments (*node);
8072 tree position = TREE_VALUE (args);
8073 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8074 position = default_conversion (position);
8076 if (!tree_fits_uhwi_p (position)
8077 || !arg_count
8078 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8080 warning (OPT_Wattributes,
8081 "alloc_align parameter outside range");
8082 *no_add_attrs = true;
8083 return NULL_TREE;
8085 return NULL_TREE;
8088 /* Handle a "assume_aligned" attribute; arguments as in
8089 struct attribute_spec.handler. */
8091 static tree
8092 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8093 bool *no_add_attrs)
8095 for (; args; args = TREE_CHAIN (args))
8097 tree position = TREE_VALUE (args);
8098 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8099 && TREE_CODE (position) != FUNCTION_DECL)
8100 position = default_conversion (position);
8102 if (TREE_CODE (position) != INTEGER_CST)
8104 warning (OPT_Wattributes,
8105 "assume_aligned parameter not integer constant");
8106 *no_add_attrs = true;
8107 return NULL_TREE;
8110 return NULL_TREE;
8113 /* Handle a "fn spec" attribute; arguments as in
8114 struct attribute_spec.handler. */
8116 static tree
8117 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8118 tree args, int ARG_UNUSED (flags),
8119 bool *no_add_attrs ATTRIBUTE_UNUSED)
8121 gcc_assert (args
8122 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8123 && !TREE_CHAIN (args));
8124 return NULL_TREE;
8127 /* Handle a "warn_unused" attribute; arguments as in
8128 struct attribute_spec.handler. */
8130 static tree
8131 handle_warn_unused_attribute (tree *node, tree name,
8132 tree args ATTRIBUTE_UNUSED,
8133 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8135 if (TYPE_P (*node))
8136 /* Do nothing else, just set the attribute. We'll get at
8137 it later with lookup_attribute. */
8139 else
8141 warning (OPT_Wattributes, "%qE attribute ignored", name);
8142 *no_add_attrs = true;
8145 return NULL_TREE;
8148 /* Handle an "omp declare simd" attribute; arguments as in
8149 struct attribute_spec.handler. */
8151 static tree
8152 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8154 return NULL_TREE;
8157 /* Handle an "omp declare target" attribute; arguments as in
8158 struct attribute_spec.handler. */
8160 static tree
8161 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8163 return NULL_TREE;
8166 /* Handle a "returns_twice" attribute; arguments as in
8167 struct attribute_spec.handler. */
8169 static tree
8170 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8171 int ARG_UNUSED (flags), bool *no_add_attrs)
8173 if (TREE_CODE (*node) == FUNCTION_DECL)
8174 DECL_IS_RETURNS_TWICE (*node) = 1;
8175 else
8177 warning (OPT_Wattributes, "%qE attribute ignored", name);
8178 *no_add_attrs = true;
8181 return NULL_TREE;
8184 /* Handle a "no_limit_stack" attribute; arguments as in
8185 struct attribute_spec.handler. */
8187 static tree
8188 handle_no_limit_stack_attribute (tree *node, tree name,
8189 tree ARG_UNUSED (args),
8190 int ARG_UNUSED (flags),
8191 bool *no_add_attrs)
8193 tree decl = *node;
8195 if (TREE_CODE (decl) != FUNCTION_DECL)
8197 error_at (DECL_SOURCE_LOCATION (decl),
8198 "%qE attribute applies only to functions", name);
8199 *no_add_attrs = true;
8201 else if (DECL_INITIAL (decl))
8203 error_at (DECL_SOURCE_LOCATION (decl),
8204 "can%'t set %qE attribute after definition", name);
8205 *no_add_attrs = true;
8207 else
8208 DECL_NO_LIMIT_STACK (decl) = 1;
8210 return NULL_TREE;
8213 /* Handle a "pure" attribute; arguments as in
8214 struct attribute_spec.handler. */
8216 static tree
8217 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8218 int ARG_UNUSED (flags), bool *no_add_attrs)
8220 if (TREE_CODE (*node) == FUNCTION_DECL)
8221 DECL_PURE_P (*node) = 1;
8222 /* ??? TODO: Support types. */
8223 else
8225 warning (OPT_Wattributes, "%qE attribute ignored", name);
8226 *no_add_attrs = true;
8229 return NULL_TREE;
8232 /* Digest an attribute list destined for a transactional memory statement.
8233 ALLOWED is the set of attributes that are allowed for this statement;
8234 return the attribute we parsed. Multiple attributes are never allowed. */
8237 parse_tm_stmt_attr (tree attrs, int allowed)
8239 tree a_seen = NULL;
8240 int m_seen = 0;
8242 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8244 tree a = TREE_PURPOSE (attrs);
8245 int m = 0;
8247 if (is_attribute_p ("outer", a))
8248 m = TM_STMT_ATTR_OUTER;
8250 if ((m & allowed) == 0)
8252 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8253 continue;
8256 if (m_seen == 0)
8258 a_seen = a;
8259 m_seen = m;
8261 else if (m_seen == m)
8262 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8263 else
8264 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8267 return m_seen;
8270 /* Transform a TM attribute name into a maskable integer and back.
8271 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8272 to how the lack of an attribute is treated. */
8275 tm_attr_to_mask (tree attr)
8277 if (attr == NULL)
8278 return 0;
8279 if (is_attribute_p ("transaction_safe", attr))
8280 return TM_ATTR_SAFE;
8281 if (is_attribute_p ("transaction_callable", attr))
8282 return TM_ATTR_CALLABLE;
8283 if (is_attribute_p ("transaction_pure", attr))
8284 return TM_ATTR_PURE;
8285 if (is_attribute_p ("transaction_unsafe", attr))
8286 return TM_ATTR_IRREVOCABLE;
8287 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8288 return TM_ATTR_MAY_CANCEL_OUTER;
8289 return 0;
8292 tree
8293 tm_mask_to_attr (int mask)
8295 const char *str;
8296 switch (mask)
8298 case TM_ATTR_SAFE:
8299 str = "transaction_safe";
8300 break;
8301 case TM_ATTR_CALLABLE:
8302 str = "transaction_callable";
8303 break;
8304 case TM_ATTR_PURE:
8305 str = "transaction_pure";
8306 break;
8307 case TM_ATTR_IRREVOCABLE:
8308 str = "transaction_unsafe";
8309 break;
8310 case TM_ATTR_MAY_CANCEL_OUTER:
8311 str = "transaction_may_cancel_outer";
8312 break;
8313 default:
8314 gcc_unreachable ();
8316 return get_identifier (str);
8319 /* Return the first TM attribute seen in LIST. */
8321 tree
8322 find_tm_attribute (tree list)
8324 for (; list ; list = TREE_CHAIN (list))
8326 tree name = TREE_PURPOSE (list);
8327 if (tm_attr_to_mask (name) != 0)
8328 return name;
8330 return NULL_TREE;
8333 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8334 Here we accept only function types, and verify that none of the other
8335 function TM attributes are also applied. */
8336 /* ??? We need to accept class types for C++, but not C. This greatly
8337 complicates this function, since we can no longer rely on the extra
8338 processing given by function_type_required. */
8340 static tree
8341 handle_tm_attribute (tree *node, tree name, tree args,
8342 int flags, bool *no_add_attrs)
8344 /* Only one path adds the attribute; others don't. */
8345 *no_add_attrs = true;
8347 switch (TREE_CODE (*node))
8349 case RECORD_TYPE:
8350 case UNION_TYPE:
8351 /* Only tm_callable and tm_safe apply to classes. */
8352 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8353 goto ignored;
8354 /* FALLTHRU */
8356 case FUNCTION_TYPE:
8357 case METHOD_TYPE:
8359 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8360 if (old_name == name)
8362 else if (old_name != NULL_TREE)
8363 error ("type was previously declared %qE", old_name);
8364 else
8365 *no_add_attrs = false;
8367 break;
8369 case POINTER_TYPE:
8371 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8372 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8374 tree fn_tmp = TREE_TYPE (*node);
8375 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8376 *node = build_pointer_type (fn_tmp);
8377 break;
8380 /* FALLTHRU */
8382 default:
8383 /* If a function is next, pass it on to be tried next. */
8384 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8385 return tree_cons (name, args, NULL);
8387 ignored:
8388 warning (OPT_Wattributes, "%qE attribute ignored", name);
8389 break;
8392 return NULL_TREE;
8395 /* Handle the TM_WRAP attribute; arguments as in
8396 struct attribute_spec.handler. */
8398 static tree
8399 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8400 int ARG_UNUSED (flags), bool *no_add_attrs)
8402 tree decl = *node;
8404 /* We don't need the attribute even on success, since we
8405 record the entry in an external table. */
8406 *no_add_attrs = true;
8408 if (TREE_CODE (decl) != FUNCTION_DECL)
8409 warning (OPT_Wattributes, "%qE attribute ignored", name);
8410 else
8412 tree wrap_decl = TREE_VALUE (args);
8413 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8414 && TREE_CODE (wrap_decl) != VAR_DECL
8415 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8416 error ("%qE argument not an identifier", name);
8417 else
8419 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8420 wrap_decl = lookup_name (wrap_decl);
8421 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8423 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8424 TREE_TYPE (wrap_decl)))
8425 record_tm_replacement (wrap_decl, decl);
8426 else
8427 error ("%qD is not compatible with %qD", wrap_decl, decl);
8429 else
8430 error ("transaction_wrap argument is not a function");
8434 return NULL_TREE;
8437 /* Ignore the given attribute. Used when this attribute may be usefully
8438 overridden by the target, but is not used generically. */
8440 static tree
8441 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8442 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8443 bool *no_add_attrs)
8445 *no_add_attrs = true;
8446 return NULL_TREE;
8449 /* Handle a "no vops" attribute; arguments as in
8450 struct attribute_spec.handler. */
8452 static tree
8453 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8454 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8455 bool *ARG_UNUSED (no_add_attrs))
8457 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8458 DECL_IS_NOVOPS (*node) = 1;
8459 return NULL_TREE;
8462 /* Handle a "deprecated" attribute; arguments as in
8463 struct attribute_spec.handler. */
8465 static tree
8466 handle_deprecated_attribute (tree *node, tree name,
8467 tree args, int flags,
8468 bool *no_add_attrs)
8470 tree type = NULL_TREE;
8471 int warn = 0;
8472 tree what = NULL_TREE;
8474 if (!args)
8475 *no_add_attrs = true;
8476 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8478 error ("deprecated message is not a string");
8479 *no_add_attrs = true;
8482 if (DECL_P (*node))
8484 tree decl = *node;
8485 type = TREE_TYPE (decl);
8487 if (TREE_CODE (decl) == TYPE_DECL
8488 || TREE_CODE (decl) == PARM_DECL
8489 || TREE_CODE (decl) == VAR_DECL
8490 || TREE_CODE (decl) == FUNCTION_DECL
8491 || TREE_CODE (decl) == FIELD_DECL
8492 || objc_method_decl (TREE_CODE (decl)))
8493 TREE_DEPRECATED (decl) = 1;
8494 else
8495 warn = 1;
8497 else if (TYPE_P (*node))
8499 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8500 *node = build_variant_type_copy (*node);
8501 TREE_DEPRECATED (*node) = 1;
8502 type = *node;
8504 else
8505 warn = 1;
8507 if (warn)
8509 *no_add_attrs = true;
8510 if (type && TYPE_NAME (type))
8512 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8513 what = TYPE_NAME (*node);
8514 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8515 && DECL_NAME (TYPE_NAME (type)))
8516 what = DECL_NAME (TYPE_NAME (type));
8518 if (what)
8519 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8520 else
8521 warning (OPT_Wattributes, "%qE attribute ignored", name);
8524 return NULL_TREE;
8527 /* Handle a "vector_size" attribute; arguments as in
8528 struct attribute_spec.handler. */
8530 static tree
8531 handle_vector_size_attribute (tree *node, tree name, tree args,
8532 int ARG_UNUSED (flags),
8533 bool *no_add_attrs)
8535 unsigned HOST_WIDE_INT vecsize, nunits;
8536 enum machine_mode orig_mode;
8537 tree type = *node, new_type, size;
8539 *no_add_attrs = true;
8541 size = TREE_VALUE (args);
8542 if (size && TREE_CODE (size) != IDENTIFIER_NODE)
8543 size = default_conversion (size);
8545 if (!tree_fits_uhwi_p (size))
8547 warning (OPT_Wattributes, "%qE attribute ignored", name);
8548 return NULL_TREE;
8551 /* Get the vector size (in bytes). */
8552 vecsize = tree_to_uhwi (size);
8554 /* We need to provide for vector pointers, vector arrays, and
8555 functions returning vectors. For example:
8557 __attribute__((vector_size(16))) short *foo;
8559 In this case, the mode is SI, but the type being modified is
8560 HI, so we need to look further. */
8562 while (POINTER_TYPE_P (type)
8563 || TREE_CODE (type) == FUNCTION_TYPE
8564 || TREE_CODE (type) == METHOD_TYPE
8565 || TREE_CODE (type) == ARRAY_TYPE
8566 || TREE_CODE (type) == OFFSET_TYPE)
8567 type = TREE_TYPE (type);
8569 /* Get the mode of the type being modified. */
8570 orig_mode = TYPE_MODE (type);
8572 if ((!INTEGRAL_TYPE_P (type)
8573 && !SCALAR_FLOAT_TYPE_P (type)
8574 && !FIXED_POINT_TYPE_P (type))
8575 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8576 && GET_MODE_CLASS (orig_mode) != MODE_INT
8577 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8578 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8579 || TREE_CODE (type) == BOOLEAN_TYPE)
8581 error ("invalid vector type for attribute %qE", name);
8582 return NULL_TREE;
8585 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8587 error ("vector size not an integral multiple of component size");
8588 return NULL;
8591 if (vecsize == 0)
8593 error ("zero vector size");
8594 return NULL;
8597 /* Calculate how many units fit in the vector. */
8598 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8599 if (nunits & (nunits - 1))
8601 error ("number of components of the vector not a power of two");
8602 return NULL_TREE;
8605 new_type = build_vector_type (type, nunits);
8607 /* Build back pointers if needed. */
8608 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8610 return NULL_TREE;
8613 /* Handle the "nonnull" attribute. */
8614 static tree
8615 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8616 tree args, int ARG_UNUSED (flags),
8617 bool *no_add_attrs)
8619 tree type = *node;
8620 unsigned HOST_WIDE_INT attr_arg_num;
8622 /* If no arguments are specified, all pointer arguments should be
8623 non-null. Verify a full prototype is given so that the arguments
8624 will have the correct types when we actually check them later. */
8625 if (!args)
8627 if (!prototype_p (type))
8629 error ("nonnull attribute without arguments on a non-prototype");
8630 *no_add_attrs = true;
8632 return NULL_TREE;
8635 /* Argument list specified. Verify that each argument number references
8636 a pointer argument. */
8637 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8639 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8641 tree arg = TREE_VALUE (args);
8642 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8643 && TREE_CODE (arg) != FUNCTION_DECL)
8644 arg = default_conversion (arg);
8646 if (!get_nonnull_operand (arg, &arg_num))
8648 error ("nonnull argument has invalid operand number (argument %lu)",
8649 (unsigned long) attr_arg_num);
8650 *no_add_attrs = true;
8651 return NULL_TREE;
8654 if (prototype_p (type))
8656 function_args_iterator iter;
8657 tree argument;
8659 function_args_iter_init (&iter, type);
8660 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8662 argument = function_args_iter_cond (&iter);
8663 if (argument == NULL_TREE || ck_num == arg_num)
8664 break;
8667 if (!argument
8668 || TREE_CODE (argument) == VOID_TYPE)
8670 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8671 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8672 *no_add_attrs = true;
8673 return NULL_TREE;
8676 if (TREE_CODE (argument) != POINTER_TYPE)
8678 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8679 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8680 *no_add_attrs = true;
8681 return NULL_TREE;
8686 return NULL_TREE;
8689 /* Check the argument list of a function call for null in argument slots
8690 that are marked as requiring a non-null pointer argument. The NARGS
8691 arguments are passed in the array ARGARRAY.
8694 static void
8695 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8697 tree a;
8698 int i;
8700 attrs = lookup_attribute ("nonnull", attrs);
8701 if (attrs == NULL_TREE)
8702 return;
8704 a = attrs;
8705 /* See if any of the nonnull attributes has no arguments. If so,
8706 then every pointer argument is checked (in which case the check
8707 for pointer type is done in check_nonnull_arg). */
8708 if (TREE_VALUE (a) != NULL_TREE)
8710 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8711 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8713 if (a != NULL_TREE)
8714 for (i = 0; i < nargs; i++)
8715 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8716 i + 1);
8717 else
8719 /* Walk the argument list. If we encounter an argument number we
8720 should check for non-null, do it. */
8721 for (i = 0; i < nargs; i++)
8723 for (a = attrs; ; a = TREE_CHAIN (a))
8725 a = lookup_attribute ("nonnull", a);
8726 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8727 break;
8730 if (a != NULL_TREE)
8731 check_function_arguments_recurse (check_nonnull_arg, NULL,
8732 argarray[i], i + 1);
8737 /* Check that the Nth argument of a function call (counting backwards
8738 from the end) is a (pointer)0. The NARGS arguments are passed in the
8739 array ARGARRAY. */
8741 static void
8742 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8744 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8746 if (attr)
8748 int len = 0;
8749 int pos = 0;
8750 tree sentinel;
8751 function_args_iterator iter;
8752 tree t;
8754 /* Skip over the named arguments. */
8755 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8757 if (len == nargs)
8758 break;
8759 len++;
8762 if (TREE_VALUE (attr))
8764 tree p = TREE_VALUE (TREE_VALUE (attr));
8765 pos = TREE_INT_CST_LOW (p);
8768 /* The sentinel must be one of the varargs, i.e.
8769 in position >= the number of fixed arguments. */
8770 if ((nargs - 1 - pos) < len)
8772 warning (OPT_Wformat_,
8773 "not enough variable arguments to fit a sentinel");
8774 return;
8777 /* Validate the sentinel. */
8778 sentinel = argarray[nargs - 1 - pos];
8779 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8780 || !integer_zerop (sentinel))
8781 /* Although __null (in C++) is only an integer we allow it
8782 nevertheless, as we are guaranteed that it's exactly
8783 as wide as a pointer, and we don't want to force
8784 users to cast the NULL they have written there.
8785 We warn with -Wstrict-null-sentinel, though. */
8786 && (warn_strict_null_sentinel || null_node != sentinel))
8787 warning (OPT_Wformat_, "missing sentinel in function call");
8791 /* Helper for check_function_nonnull; given a list of operands which
8792 must be non-null in ARGS, determine if operand PARAM_NUM should be
8793 checked. */
8795 static bool
8796 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8798 unsigned HOST_WIDE_INT arg_num = 0;
8800 for (; args; args = TREE_CHAIN (args))
8802 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8804 gcc_assert (found);
8806 if (arg_num == param_num)
8807 return true;
8809 return false;
8812 /* Check that the function argument PARAM (which is operand number
8813 PARAM_NUM) is non-null. This is called by check_function_nonnull
8814 via check_function_arguments_recurse. */
8816 static void
8817 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8818 unsigned HOST_WIDE_INT param_num)
8820 /* Just skip checking the argument if it's not a pointer. This can
8821 happen if the "nonnull" attribute was given without an operand
8822 list (which means to check every pointer argument). */
8824 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8825 return;
8827 if (integer_zerop (param))
8828 warning (OPT_Wnonnull, "null argument where non-null required "
8829 "(argument %lu)", (unsigned long) param_num);
8832 /* Helper for nonnull attribute handling; fetch the operand number
8833 from the attribute argument list. */
8835 static bool
8836 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8838 /* Verify the arg number is a constant. */
8839 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8840 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8841 return false;
8843 *valp = TREE_INT_CST_LOW (arg_num_expr);
8844 return true;
8847 /* Handle a "nothrow" attribute; arguments as in
8848 struct attribute_spec.handler. */
8850 static tree
8851 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8852 int ARG_UNUSED (flags), bool *no_add_attrs)
8854 if (TREE_CODE (*node) == FUNCTION_DECL)
8855 TREE_NOTHROW (*node) = 1;
8856 /* ??? TODO: Support types. */
8857 else
8859 warning (OPT_Wattributes, "%qE attribute ignored", name);
8860 *no_add_attrs = true;
8863 return NULL_TREE;
8866 /* Handle a "cleanup" attribute; arguments as in
8867 struct attribute_spec.handler. */
8869 static tree
8870 handle_cleanup_attribute (tree *node, tree name, tree args,
8871 int ARG_UNUSED (flags), bool *no_add_attrs)
8873 tree decl = *node;
8874 tree cleanup_id, cleanup_decl;
8876 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8877 for global destructors in C++. This requires infrastructure that
8878 we don't have generically at the moment. It's also not a feature
8879 we'd be missing too much, since we do have attribute constructor. */
8880 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8882 warning (OPT_Wattributes, "%qE attribute ignored", name);
8883 *no_add_attrs = true;
8884 return NULL_TREE;
8887 /* Verify that the argument is a function in scope. */
8888 /* ??? We could support pointers to functions here as well, if
8889 that was considered desirable. */
8890 cleanup_id = TREE_VALUE (args);
8891 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8893 error ("cleanup argument not an identifier");
8894 *no_add_attrs = true;
8895 return NULL_TREE;
8897 cleanup_decl = lookup_name (cleanup_id);
8898 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8900 error ("cleanup argument not a function");
8901 *no_add_attrs = true;
8902 return NULL_TREE;
8905 /* That the function has proper type is checked with the
8906 eventual call to build_function_call. */
8908 return NULL_TREE;
8911 /* Handle a "warn_unused_result" attribute. No special handling. */
8913 static tree
8914 handle_warn_unused_result_attribute (tree *node, tree name,
8915 tree ARG_UNUSED (args),
8916 int ARG_UNUSED (flags), bool *no_add_attrs)
8918 /* Ignore the attribute for functions not returning any value. */
8919 if (VOID_TYPE_P (TREE_TYPE (*node)))
8921 warning (OPT_Wattributes, "%qE attribute ignored", name);
8922 *no_add_attrs = true;
8925 return NULL_TREE;
8928 /* Handle a "sentinel" attribute. */
8930 static tree
8931 handle_sentinel_attribute (tree *node, tree name, tree args,
8932 int ARG_UNUSED (flags), bool *no_add_attrs)
8934 if (!prototype_p (*node))
8936 warning (OPT_Wattributes,
8937 "%qE attribute requires prototypes with named arguments", name);
8938 *no_add_attrs = true;
8940 else
8942 if (!stdarg_p (*node))
8944 warning (OPT_Wattributes,
8945 "%qE attribute only applies to variadic functions", name);
8946 *no_add_attrs = true;
8950 if (args)
8952 tree position = TREE_VALUE (args);
8954 if (TREE_CODE (position) != INTEGER_CST)
8956 warning (OPT_Wattributes,
8957 "requested position is not an integer constant");
8958 *no_add_attrs = true;
8960 else
8962 if (tree_int_cst_lt (position, integer_zero_node))
8964 warning (OPT_Wattributes,
8965 "requested position is less than zero");
8966 *no_add_attrs = true;
8971 return NULL_TREE;
8974 /* Handle a "type_generic" attribute. */
8976 static tree
8977 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8978 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8979 bool * ARG_UNUSED (no_add_attrs))
8981 /* Ensure we have a function type. */
8982 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8984 /* Ensure we have a variadic function. */
8985 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8987 return NULL_TREE;
8990 /* Handle a "target" attribute. */
8992 static tree
8993 handle_target_attribute (tree *node, tree name, tree args, int flags,
8994 bool *no_add_attrs)
8996 /* Ensure we have a function type. */
8997 if (TREE_CODE (*node) != FUNCTION_DECL)
8999 warning (OPT_Wattributes, "%qE attribute ignored", name);
9000 *no_add_attrs = true;
9002 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9003 flags))
9004 *no_add_attrs = true;
9006 return NULL_TREE;
9009 /* Arguments being collected for optimization. */
9010 typedef const char *const_char_p; /* For DEF_VEC_P. */
9011 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9014 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9015 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9016 false for #pragma GCC optimize. */
9018 bool
9019 parse_optimize_options (tree args, bool attr_p)
9021 bool ret = true;
9022 unsigned opt_argc;
9023 unsigned i;
9024 int saved_flag_strict_aliasing;
9025 const char **opt_argv;
9026 struct cl_decoded_option *decoded_options;
9027 unsigned int decoded_options_count;
9028 tree ap;
9030 /* Build up argv vector. Just in case the string is stored away, use garbage
9031 collected strings. */
9032 vec_safe_truncate (optimize_args, 0);
9033 vec_safe_push (optimize_args, (const char *) NULL);
9035 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9037 tree value = TREE_VALUE (ap);
9039 if (TREE_CODE (value) == INTEGER_CST)
9041 char buffer[20];
9042 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9043 vec_safe_push (optimize_args, ggc_strdup (buffer));
9046 else if (TREE_CODE (value) == STRING_CST)
9048 /* Split string into multiple substrings. */
9049 size_t len = TREE_STRING_LENGTH (value);
9050 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9051 char *end = p + len;
9052 char *comma;
9053 char *next_p = p;
9055 while (next_p != NULL)
9057 size_t len2;
9058 char *q, *r;
9060 p = next_p;
9061 comma = strchr (p, ',');
9062 if (comma)
9064 len2 = comma - p;
9065 *comma = '\0';
9066 next_p = comma+1;
9068 else
9070 len2 = end - p;
9071 next_p = NULL;
9074 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9076 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9077 options. */
9078 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9080 ret = false;
9081 if (attr_p)
9082 warning (OPT_Wattributes,
9083 "bad option %s to optimize attribute", p);
9084 else
9085 warning (OPT_Wpragmas,
9086 "bad option %s to pragma attribute", p);
9087 continue;
9090 if (*p != '-')
9092 *r++ = '-';
9094 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9095 itself is -Os, and any other switch begins with a -f. */
9096 if ((*p >= '0' && *p <= '9')
9097 || (p[0] == 's' && p[1] == '\0'))
9098 *r++ = 'O';
9099 else if (*p != 'O')
9100 *r++ = 'f';
9103 memcpy (r, p, len2);
9104 r[len2] = '\0';
9105 vec_safe_push (optimize_args, (const char *) q);
9111 opt_argc = optimize_args->length ();
9112 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9114 for (i = 1; i < opt_argc; i++)
9115 opt_argv[i] = (*optimize_args)[i];
9117 saved_flag_strict_aliasing = flag_strict_aliasing;
9119 /* Now parse the options. */
9120 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9121 &decoded_options,
9122 &decoded_options_count);
9123 decode_options (&global_options, &global_options_set,
9124 decoded_options, decoded_options_count,
9125 input_location, global_dc);
9127 targetm.override_options_after_change();
9129 /* Don't allow changing -fstrict-aliasing. */
9130 flag_strict_aliasing = saved_flag_strict_aliasing;
9132 optimize_args->truncate (0);
9133 return ret;
9136 /* For handling "optimize" attribute. arguments as in
9137 struct attribute_spec.handler. */
9139 static tree
9140 handle_optimize_attribute (tree *node, tree name, tree args,
9141 int ARG_UNUSED (flags), bool *no_add_attrs)
9143 /* Ensure we have a function type. */
9144 if (TREE_CODE (*node) != FUNCTION_DECL)
9146 warning (OPT_Wattributes, "%qE attribute ignored", name);
9147 *no_add_attrs = true;
9149 else
9151 struct cl_optimization cur_opts;
9152 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9154 /* Save current options. */
9155 cl_optimization_save (&cur_opts, &global_options);
9157 /* If we previously had some optimization options, use them as the
9158 default. */
9159 if (old_opts)
9160 cl_optimization_restore (&global_options,
9161 TREE_OPTIMIZATION (old_opts));
9163 /* Parse options, and update the vector. */
9164 parse_optimize_options (args, true);
9165 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9166 = build_optimization_node (&global_options);
9168 /* Restore current options. */
9169 cl_optimization_restore (&global_options, &cur_opts);
9172 return NULL_TREE;
9175 /* Handle a "no_split_stack" attribute. */
9177 static tree
9178 handle_no_split_stack_attribute (tree *node, tree name,
9179 tree ARG_UNUSED (args),
9180 int ARG_UNUSED (flags),
9181 bool *no_add_attrs)
9183 tree decl = *node;
9185 if (TREE_CODE (decl) != FUNCTION_DECL)
9187 error_at (DECL_SOURCE_LOCATION (decl),
9188 "%qE attribute applies only to functions", name);
9189 *no_add_attrs = true;
9191 else if (DECL_INITIAL (decl))
9193 error_at (DECL_SOURCE_LOCATION (decl),
9194 "can%'t set %qE attribute after definition", name);
9195 *no_add_attrs = true;
9198 return NULL_TREE;
9201 /* Handle a "returns_nonnull" attribute; arguments as in
9202 struct attribute_spec.handler. */
9204 static tree
9205 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9206 bool *no_add_attrs)
9208 // Even without a prototype we still have a return type we can check.
9209 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9211 error ("returns_nonnull attribute on a function not returning a pointer");
9212 *no_add_attrs = true;
9214 return NULL_TREE;
9218 /* Check for valid arguments being passed to a function with FNTYPE.
9219 There are NARGS arguments in the array ARGARRAY. */
9220 void
9221 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9223 /* Check for null being passed in a pointer argument that must be
9224 non-null. We also need to do this if format checking is enabled. */
9226 if (warn_nonnull)
9227 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9229 /* Check for errors in format strings. */
9231 if (warn_format || warn_suggest_attribute_format)
9232 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9234 if (warn_format)
9235 check_function_sentinel (fntype, nargs, argarray);
9238 /* Generic argument checking recursion routine. PARAM is the argument to
9239 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9240 once the argument is resolved. CTX is context for the callback. */
9241 void
9242 check_function_arguments_recurse (void (*callback)
9243 (void *, tree, unsigned HOST_WIDE_INT),
9244 void *ctx, tree param,
9245 unsigned HOST_WIDE_INT param_num)
9247 if (CONVERT_EXPR_P (param)
9248 && (TYPE_PRECISION (TREE_TYPE (param))
9249 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9251 /* Strip coercion. */
9252 check_function_arguments_recurse (callback, ctx,
9253 TREE_OPERAND (param, 0), param_num);
9254 return;
9257 if (TREE_CODE (param) == CALL_EXPR)
9259 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9260 tree attrs;
9261 bool found_format_arg = false;
9263 /* See if this is a call to a known internationalization function
9264 that modifies a format arg. Such a function may have multiple
9265 format_arg attributes (for example, ngettext). */
9267 for (attrs = TYPE_ATTRIBUTES (type);
9268 attrs;
9269 attrs = TREE_CHAIN (attrs))
9270 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9272 tree inner_arg;
9273 tree format_num_expr;
9274 int format_num;
9275 int i;
9276 call_expr_arg_iterator iter;
9278 /* Extract the argument number, which was previously checked
9279 to be valid. */
9280 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9282 format_num = tree_to_uhwi (format_num_expr);
9284 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9285 inner_arg != 0;
9286 inner_arg = next_call_expr_arg (&iter), i++)
9287 if (i == format_num)
9289 check_function_arguments_recurse (callback, ctx,
9290 inner_arg, param_num);
9291 found_format_arg = true;
9292 break;
9296 /* If we found a format_arg attribute and did a recursive check,
9297 we are done with checking this argument. Otherwise, we continue
9298 and this will be considered a non-literal. */
9299 if (found_format_arg)
9300 return;
9303 if (TREE_CODE (param) == COND_EXPR)
9305 /* Check both halves of the conditional expression. */
9306 check_function_arguments_recurse (callback, ctx,
9307 TREE_OPERAND (param, 1), param_num);
9308 check_function_arguments_recurse (callback, ctx,
9309 TREE_OPERAND (param, 2), param_num);
9310 return;
9313 (*callback) (ctx, param, param_num);
9316 /* Checks for a builtin function FNDECL that the number of arguments
9317 NARGS against the required number REQUIRED and issues an error if
9318 there is a mismatch. Returns true if the number of arguments is
9319 correct, otherwise false. */
9321 static bool
9322 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9324 if (nargs < required)
9326 error_at (input_location,
9327 "not enough arguments to function %qE", fndecl);
9328 return false;
9330 else if (nargs > required)
9332 error_at (input_location,
9333 "too many arguments to function %qE", fndecl);
9334 return false;
9336 return true;
9339 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9340 Returns false if there was an error, otherwise true. */
9342 bool
9343 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9345 if (!DECL_BUILT_IN (fndecl)
9346 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9347 return true;
9349 switch (DECL_FUNCTION_CODE (fndecl))
9351 case BUILT_IN_CONSTANT_P:
9352 return builtin_function_validate_nargs (fndecl, nargs, 1);
9354 case BUILT_IN_ISFINITE:
9355 case BUILT_IN_ISINF:
9356 case BUILT_IN_ISINF_SIGN:
9357 case BUILT_IN_ISNAN:
9358 case BUILT_IN_ISNORMAL:
9359 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9361 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9363 error ("non-floating-point argument in call to "
9364 "function %qE", fndecl);
9365 return false;
9367 return true;
9369 return false;
9371 case BUILT_IN_ISGREATER:
9372 case BUILT_IN_ISGREATEREQUAL:
9373 case BUILT_IN_ISLESS:
9374 case BUILT_IN_ISLESSEQUAL:
9375 case BUILT_IN_ISLESSGREATER:
9376 case BUILT_IN_ISUNORDERED:
9377 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9379 enum tree_code code0, code1;
9380 code0 = TREE_CODE (TREE_TYPE (args[0]));
9381 code1 = TREE_CODE (TREE_TYPE (args[1]));
9382 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9383 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9384 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9386 error ("non-floating-point arguments in call to "
9387 "function %qE", fndecl);
9388 return false;
9390 return true;
9392 return false;
9394 case BUILT_IN_FPCLASSIFY:
9395 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9397 unsigned i;
9399 for (i=0; i<5; i++)
9400 if (TREE_CODE (args[i]) != INTEGER_CST)
9402 error ("non-const integer argument %u in call to function %qE",
9403 i+1, fndecl);
9404 return false;
9407 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9409 error ("non-floating-point argument in call to function %qE",
9410 fndecl);
9411 return false;
9413 return true;
9415 return false;
9417 case BUILT_IN_ASSUME_ALIGNED:
9418 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9420 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9422 error ("non-integer argument 3 in call to function %qE", fndecl);
9423 return false;
9425 return true;
9427 return false;
9429 default:
9430 return true;
9434 /* Function to help qsort sort FIELD_DECLs by name order. */
9437 field_decl_cmp (const void *x_p, const void *y_p)
9439 const tree *const x = (const tree *const) x_p;
9440 const tree *const y = (const tree *const) y_p;
9442 if (DECL_NAME (*x) == DECL_NAME (*y))
9443 /* A nontype is "greater" than a type. */
9444 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9445 if (DECL_NAME (*x) == NULL_TREE)
9446 return -1;
9447 if (DECL_NAME (*y) == NULL_TREE)
9448 return 1;
9449 if (DECL_NAME (*x) < DECL_NAME (*y))
9450 return -1;
9451 return 1;
9454 static struct {
9455 gt_pointer_operator new_value;
9456 void *cookie;
9457 } resort_data;
9459 /* This routine compares two fields like field_decl_cmp but using the
9460 pointer operator in resort_data. */
9462 static int
9463 resort_field_decl_cmp (const void *x_p, const void *y_p)
9465 const tree *const x = (const tree *const) x_p;
9466 const tree *const y = (const tree *const) y_p;
9468 if (DECL_NAME (*x) == DECL_NAME (*y))
9469 /* A nontype is "greater" than a type. */
9470 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9471 if (DECL_NAME (*x) == NULL_TREE)
9472 return -1;
9473 if (DECL_NAME (*y) == NULL_TREE)
9474 return 1;
9476 tree d1 = DECL_NAME (*x);
9477 tree d2 = DECL_NAME (*y);
9478 resort_data.new_value (&d1, resort_data.cookie);
9479 resort_data.new_value (&d2, resort_data.cookie);
9480 if (d1 < d2)
9481 return -1;
9483 return 1;
9486 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9488 void
9489 resort_sorted_fields (void *obj,
9490 void * ARG_UNUSED (orig_obj),
9491 gt_pointer_operator new_value,
9492 void *cookie)
9494 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9495 resort_data.new_value = new_value;
9496 resort_data.cookie = cookie;
9497 qsort (&sf->elts[0], sf->len, sizeof (tree),
9498 resort_field_decl_cmp);
9501 /* Subroutine of c_parse_error.
9502 Return the result of concatenating LHS and RHS. RHS is really
9503 a string literal, its first character is indicated by RHS_START and
9504 RHS_SIZE is its length (including the terminating NUL character).
9506 The caller is responsible for deleting the returned pointer. */
9508 static char *
9509 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9511 const int lhs_size = strlen (lhs);
9512 char *result = XNEWVEC (char, lhs_size + rhs_size);
9513 strncpy (result, lhs, lhs_size);
9514 strncpy (result + lhs_size, rhs_start, rhs_size);
9515 return result;
9518 /* Issue the error given by GMSGID, indicating that it occurred before
9519 TOKEN, which had the associated VALUE. */
9521 void
9522 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9523 tree value, unsigned char token_flags)
9525 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9527 char *message = NULL;
9529 if (token_type == CPP_EOF)
9530 message = catenate_messages (gmsgid, " at end of input");
9531 else if (token_type == CPP_CHAR
9532 || token_type == CPP_WCHAR
9533 || token_type == CPP_CHAR16
9534 || token_type == CPP_CHAR32)
9536 unsigned int val = TREE_INT_CST_LOW (value);
9537 const char *prefix;
9539 switch (token_type)
9541 default:
9542 prefix = "";
9543 break;
9544 case CPP_WCHAR:
9545 prefix = "L";
9546 break;
9547 case CPP_CHAR16:
9548 prefix = "u";
9549 break;
9550 case CPP_CHAR32:
9551 prefix = "U";
9552 break;
9555 if (val <= UCHAR_MAX && ISGRAPH (val))
9556 message = catenate_messages (gmsgid, " before %s'%c'");
9557 else
9558 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9560 error (message, prefix, val);
9561 free (message);
9562 message = NULL;
9564 else if (token_type == CPP_CHAR_USERDEF
9565 || token_type == CPP_WCHAR_USERDEF
9566 || token_type == CPP_CHAR16_USERDEF
9567 || token_type == CPP_CHAR32_USERDEF)
9568 message = catenate_messages (gmsgid,
9569 " before user-defined character literal");
9570 else if (token_type == CPP_STRING_USERDEF
9571 || token_type == CPP_WSTRING_USERDEF
9572 || token_type == CPP_STRING16_USERDEF
9573 || token_type == CPP_STRING32_USERDEF
9574 || token_type == CPP_UTF8STRING_USERDEF)
9575 message = catenate_messages (gmsgid, " before user-defined string literal");
9576 else if (token_type == CPP_STRING
9577 || token_type == CPP_WSTRING
9578 || token_type == CPP_STRING16
9579 || token_type == CPP_STRING32
9580 || token_type == CPP_UTF8STRING)
9581 message = catenate_messages (gmsgid, " before string constant");
9582 else if (token_type == CPP_NUMBER)
9583 message = catenate_messages (gmsgid, " before numeric constant");
9584 else if (token_type == CPP_NAME)
9586 message = catenate_messages (gmsgid, " before %qE");
9587 error (message, value);
9588 free (message);
9589 message = NULL;
9591 else if (token_type == CPP_PRAGMA)
9592 message = catenate_messages (gmsgid, " before %<#pragma%>");
9593 else if (token_type == CPP_PRAGMA_EOL)
9594 message = catenate_messages (gmsgid, " before end of line");
9595 else if (token_type == CPP_DECLTYPE)
9596 message = catenate_messages (gmsgid, " before %<decltype%>");
9597 else if (token_type < N_TTYPES)
9599 message = catenate_messages (gmsgid, " before %qs token");
9600 error (message, cpp_type2name (token_type, token_flags));
9601 free (message);
9602 message = NULL;
9604 else
9605 error (gmsgid);
9607 if (message)
9609 error (message);
9610 free (message);
9612 #undef catenate_messages
9615 /* Mapping for cpp message reasons to the options that enable them. */
9617 struct reason_option_codes_t
9619 const int reason; /* cpplib message reason. */
9620 const int option_code; /* gcc option that controls this message. */
9623 static const struct reason_option_codes_t option_codes[] = {
9624 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9625 {CPP_W_COMMENTS, OPT_Wcomment},
9626 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9627 {CPP_W_MULTICHAR, OPT_Wmultichar},
9628 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9629 {CPP_W_LONG_LONG, OPT_Wlong_long},
9630 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9631 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9632 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9633 {CPP_W_UNDEF, OPT_Wundef},
9634 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9635 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9636 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9637 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9638 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9639 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9640 {CPP_W_DATE_TIME, OPT_Wdate_time},
9641 {CPP_W_NONE, 0}
9644 /* Return the gcc option code associated with the reason for a cpp
9645 message, or 0 if none. */
9647 static int
9648 c_option_controlling_cpp_error (int reason)
9650 const struct reason_option_codes_t *entry;
9652 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9654 if (entry->reason == reason)
9655 return entry->option_code;
9657 return 0;
9660 /* Callback from cpp_error for PFILE to print diagnostics from the
9661 preprocessor. The diagnostic is of type LEVEL, with REASON set
9662 to the reason code if LEVEL is represents a warning, at location
9663 LOCATION unless this is after lexing and the compiler's location
9664 should be used instead, with column number possibly overridden by
9665 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9666 the arguments. Returns true if a diagnostic was emitted, false
9667 otherwise. */
9669 bool
9670 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9671 location_t location, unsigned int column_override,
9672 const char *msg, va_list *ap)
9674 diagnostic_info diagnostic;
9675 diagnostic_t dlevel;
9676 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9677 bool ret;
9679 switch (level)
9681 case CPP_DL_WARNING_SYSHDR:
9682 if (flag_no_output)
9683 return false;
9684 global_dc->dc_warn_system_headers = 1;
9685 /* Fall through. */
9686 case CPP_DL_WARNING:
9687 if (flag_no_output)
9688 return false;
9689 dlevel = DK_WARNING;
9690 break;
9691 case CPP_DL_PEDWARN:
9692 if (flag_no_output && !flag_pedantic_errors)
9693 return false;
9694 dlevel = DK_PEDWARN;
9695 break;
9696 case CPP_DL_ERROR:
9697 dlevel = DK_ERROR;
9698 break;
9699 case CPP_DL_ICE:
9700 dlevel = DK_ICE;
9701 break;
9702 case CPP_DL_NOTE:
9703 dlevel = DK_NOTE;
9704 break;
9705 case CPP_DL_FATAL:
9706 dlevel = DK_FATAL;
9707 break;
9708 default:
9709 gcc_unreachable ();
9711 if (done_lexing)
9712 location = input_location;
9713 diagnostic_set_info_translated (&diagnostic, msg, ap,
9714 location, dlevel);
9715 if (column_override)
9716 diagnostic_override_column (&diagnostic, column_override);
9717 diagnostic_override_option_index (&diagnostic,
9718 c_option_controlling_cpp_error (reason));
9719 ret = report_diagnostic (&diagnostic);
9720 if (level == CPP_DL_WARNING_SYSHDR)
9721 global_dc->dc_warn_system_headers = save_warn_system_headers;
9722 return ret;
9725 /* Convert a character from the host to the target execution character
9726 set. cpplib handles this, mostly. */
9728 HOST_WIDE_INT
9729 c_common_to_target_charset (HOST_WIDE_INT c)
9731 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9732 zero-extended under -fno-signed-char. cpplib insists that characters
9733 and character constants are always unsigned. Hence we must convert
9734 back and forth. */
9735 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9737 uc = cpp_host_to_exec_charset (parse_in, uc);
9739 if (flag_signed_char)
9740 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9741 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9742 else
9743 return uc;
9746 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9747 references with an INDIRECT_REF of a constant at the bottom; much like the
9748 traditional rendering of offsetof as a macro. Return the folded result. */
9750 tree
9751 fold_offsetof_1 (tree expr)
9753 tree base, off, t;
9755 switch (TREE_CODE (expr))
9757 case ERROR_MARK:
9758 return expr;
9760 case VAR_DECL:
9761 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9762 return error_mark_node;
9764 case CALL_EXPR:
9765 case TARGET_EXPR:
9766 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9767 return error_mark_node;
9769 case NOP_EXPR:
9770 case INDIRECT_REF:
9771 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9773 error ("cannot apply %<offsetof%> to a non constant address");
9774 return error_mark_node;
9776 return TREE_OPERAND (expr, 0);
9778 case COMPONENT_REF:
9779 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9780 if (base == error_mark_node)
9781 return base;
9783 t = TREE_OPERAND (expr, 1);
9784 if (DECL_C_BIT_FIELD (t))
9786 error ("attempt to take address of bit-field structure "
9787 "member %qD", t);
9788 return error_mark_node;
9790 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9791 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
9792 / BITS_PER_UNIT));
9793 break;
9795 case ARRAY_REF:
9796 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9797 if (base == error_mark_node)
9798 return base;
9800 t = TREE_OPERAND (expr, 1);
9802 /* Check if the offset goes beyond the upper bound of the array. */
9803 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9805 tree upbound = array_ref_up_bound (expr);
9806 if (upbound != NULL_TREE
9807 && TREE_CODE (upbound) == INTEGER_CST
9808 && !tree_int_cst_equal (upbound,
9809 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9811 upbound = size_binop (PLUS_EXPR, upbound,
9812 build_int_cst (TREE_TYPE (upbound), 1));
9813 if (tree_int_cst_lt (upbound, t))
9815 tree v;
9817 for (v = TREE_OPERAND (expr, 0);
9818 TREE_CODE (v) == COMPONENT_REF;
9819 v = TREE_OPERAND (v, 0))
9820 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9821 == RECORD_TYPE)
9823 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9824 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9825 if (TREE_CODE (fld_chain) == FIELD_DECL)
9826 break;
9828 if (fld_chain)
9829 break;
9831 /* Don't warn if the array might be considered a poor
9832 man's flexible array member with a very permissive
9833 definition thereof. */
9834 if (TREE_CODE (v) == ARRAY_REF
9835 || TREE_CODE (v) == COMPONENT_REF)
9836 warning (OPT_Warray_bounds,
9837 "index %E denotes an offset "
9838 "greater than size of %qT",
9839 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9844 t = convert (sizetype, t);
9845 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9846 break;
9848 case COMPOUND_EXPR:
9849 /* Handle static members of volatile structs. */
9850 t = TREE_OPERAND (expr, 1);
9851 gcc_assert (TREE_CODE (t) == VAR_DECL);
9852 return fold_offsetof_1 (t);
9854 default:
9855 gcc_unreachable ();
9858 return fold_build_pointer_plus (base, off);
9861 /* Likewise, but convert it to the return type of offsetof. */
9863 tree
9864 fold_offsetof (tree expr)
9866 return convert (size_type_node, fold_offsetof_1 (expr));
9869 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9870 expression, because B will always be true. */
9872 void
9873 warn_for_omitted_condop (location_t location, tree cond)
9875 if (truth_value_p (TREE_CODE (cond)))
9876 warning_at (location, OPT_Wparentheses,
9877 "the omitted middle operand in ?: will always be %<true%>, "
9878 "suggest explicit middle operand");
9881 /* Give an error for storing into ARG, which is 'const'. USE indicates
9882 how ARG was being used. */
9884 void
9885 readonly_error (location_t loc, tree arg, enum lvalue_use use)
9887 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9888 || use == lv_asm);
9889 /* Using this macro rather than (for example) arrays of messages
9890 ensures that all the format strings are checked at compile
9891 time. */
9892 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9893 : (use == lv_increment ? (I) \
9894 : (use == lv_decrement ? (D) : (AS))))
9895 if (TREE_CODE (arg) == COMPONENT_REF)
9897 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9898 error_at (loc, READONLY_MSG (G_("assignment of member "
9899 "%qD in read-only object"),
9900 G_("increment of member "
9901 "%qD in read-only object"),
9902 G_("decrement of member "
9903 "%qD in read-only object"),
9904 G_("member %qD in read-only object "
9905 "used as %<asm%> output")),
9906 TREE_OPERAND (arg, 1));
9907 else
9908 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
9909 G_("increment of read-only member %qD"),
9910 G_("decrement of read-only member %qD"),
9911 G_("read-only member %qD used as %<asm%> output")),
9912 TREE_OPERAND (arg, 1));
9914 else if (TREE_CODE (arg) == VAR_DECL)
9915 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
9916 G_("increment of read-only variable %qD"),
9917 G_("decrement of read-only variable %qD"),
9918 G_("read-only variable %qD used as %<asm%> output")),
9919 arg);
9920 else if (TREE_CODE (arg) == PARM_DECL)
9921 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
9922 G_("increment of read-only parameter %qD"),
9923 G_("decrement of read-only parameter %qD"),
9924 G_("read-only parameter %qD use as %<asm%> output")),
9925 arg);
9926 else if (TREE_CODE (arg) == RESULT_DECL)
9928 gcc_assert (c_dialect_cxx ());
9929 error_at (loc, READONLY_MSG (G_("assignment of "
9930 "read-only named return value %qD"),
9931 G_("increment of "
9932 "read-only named return value %qD"),
9933 G_("decrement of "
9934 "read-only named return value %qD"),
9935 G_("read-only named return value %qD "
9936 "used as %<asm%>output")),
9937 arg);
9939 else if (TREE_CODE (arg) == FUNCTION_DECL)
9940 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
9941 G_("increment of function %qD"),
9942 G_("decrement of function %qD"),
9943 G_("function %qD used as %<asm%> output")),
9944 arg);
9945 else
9946 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
9947 G_("increment of read-only location %qE"),
9948 G_("decrement of read-only location %qE"),
9949 G_("read-only location %qE used as %<asm%> output")),
9950 arg);
9953 /* Print an error message for an invalid lvalue. USE says
9954 how the lvalue is being used and so selects the error message. LOC
9955 is the location for the error. */
9957 void
9958 lvalue_error (location_t loc, enum lvalue_use use)
9960 switch (use)
9962 case lv_assign:
9963 error_at (loc, "lvalue required as left operand of assignment");
9964 break;
9965 case lv_increment:
9966 error_at (loc, "lvalue required as increment operand");
9967 break;
9968 case lv_decrement:
9969 error_at (loc, "lvalue required as decrement operand");
9970 break;
9971 case lv_addressof:
9972 error_at (loc, "lvalue required as unary %<&%> operand");
9973 break;
9974 case lv_asm:
9975 error_at (loc, "lvalue required in asm statement");
9976 break;
9977 default:
9978 gcc_unreachable ();
9982 /* Print an error message for an invalid indirection of type TYPE.
9983 ERRSTRING is the name of the operator for the indirection. */
9985 void
9986 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9988 switch (errstring)
9990 case RO_NULL:
9991 gcc_assert (c_dialect_cxx ());
9992 error_at (loc, "invalid type argument (have %qT)", type);
9993 break;
9994 case RO_ARRAY_INDEXING:
9995 error_at (loc,
9996 "invalid type argument of array indexing (have %qT)",
9997 type);
9998 break;
9999 case RO_UNARY_STAR:
10000 error_at (loc,
10001 "invalid type argument of unary %<*%> (have %qT)",
10002 type);
10003 break;
10004 case RO_ARROW:
10005 error_at (loc,
10006 "invalid type argument of %<->%> (have %qT)",
10007 type);
10008 break;
10009 case RO_ARROW_STAR:
10010 error_at (loc,
10011 "invalid type argument of %<->*%> (have %qT)",
10012 type);
10013 break;
10014 case RO_IMPLICIT_CONVERSION:
10015 error_at (loc,
10016 "invalid type argument of implicit conversion (have %qT)",
10017 type);
10018 break;
10019 default:
10020 gcc_unreachable ();
10024 /* *PTYPE is an incomplete array. Complete it with a domain based on
10025 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10026 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10027 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10030 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10032 tree maxindex, type, main_type, elt, unqual_elt;
10033 int failure = 0, quals;
10034 hashval_t hashcode = 0;
10035 bool overflow_p = false;
10037 maxindex = size_zero_node;
10038 if (initial_value)
10040 if (TREE_CODE (initial_value) == STRING_CST)
10042 int eltsize
10043 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10044 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10046 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10048 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10050 if (vec_safe_is_empty (v))
10052 if (pedantic)
10053 failure = 3;
10054 maxindex = ssize_int (-1);
10056 else
10058 tree curindex;
10059 unsigned HOST_WIDE_INT cnt;
10060 constructor_elt *ce;
10061 bool fold_p = false;
10063 if ((*v)[0].index)
10064 maxindex = (*v)[0].index, fold_p = true;
10066 curindex = maxindex;
10068 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10070 bool curfold_p = false;
10071 if (ce->index)
10072 curindex = ce->index, curfold_p = true;
10073 else
10075 if (fold_p)
10077 /* Since we treat size types now as ordinary
10078 unsigned types, we need an explicit overflow
10079 check. */
10080 tree orig = curindex;
10081 curindex = fold_convert (sizetype, curindex);
10082 overflow_p |= tree_int_cst_lt (curindex, orig);
10084 curindex = size_binop (PLUS_EXPR, curindex,
10085 size_one_node);
10087 if (tree_int_cst_lt (maxindex, curindex))
10088 maxindex = curindex, fold_p = curfold_p;
10090 if (fold_p)
10092 tree orig = maxindex;
10093 maxindex = fold_convert (sizetype, maxindex);
10094 overflow_p |= tree_int_cst_lt (maxindex, orig);
10098 else
10100 /* Make an error message unless that happened already. */
10101 if (initial_value != error_mark_node)
10102 failure = 1;
10105 else
10107 failure = 2;
10108 if (!do_default)
10109 return failure;
10112 type = *ptype;
10113 elt = TREE_TYPE (type);
10114 quals = TYPE_QUALS (strip_array_types (elt));
10115 if (quals == 0)
10116 unqual_elt = elt;
10117 else
10118 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10120 /* Using build_distinct_type_copy and modifying things afterward instead
10121 of using build_array_type to create a new type preserves all of the
10122 TYPE_LANG_FLAG_? bits that the front end may have set. */
10123 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10124 TREE_TYPE (main_type) = unqual_elt;
10125 TYPE_DOMAIN (main_type)
10126 = build_range_type (TREE_TYPE (maxindex),
10127 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10128 layout_type (main_type);
10130 /* Make sure we have the canonical MAIN_TYPE. */
10131 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10132 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10133 hashcode);
10134 main_type = type_hash_canon (hashcode, main_type);
10136 /* Fix the canonical type. */
10137 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10138 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10139 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10140 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10141 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10142 != TYPE_DOMAIN (main_type)))
10143 TYPE_CANONICAL (main_type)
10144 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10145 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10146 else
10147 TYPE_CANONICAL (main_type) = main_type;
10149 if (quals == 0)
10150 type = main_type;
10151 else
10152 type = c_build_qualified_type (main_type, quals);
10154 if (COMPLETE_TYPE_P (type)
10155 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10156 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10158 error ("size of array is too large");
10159 /* If we proceed with the array type as it is, we'll eventually
10160 crash in tree_to_[su]hwi(). */
10161 type = error_mark_node;
10164 *ptype = type;
10165 return failure;
10168 /* Like c_mark_addressable but don't check register qualifier. */
10169 void
10170 c_common_mark_addressable_vec (tree t)
10172 while (handled_component_p (t))
10173 t = TREE_OPERAND (t, 0);
10174 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10175 return;
10176 TREE_ADDRESSABLE (t) = 1;
10181 /* Used to help initialize the builtin-types.def table. When a type of
10182 the correct size doesn't exist, use error_mark_node instead of NULL.
10183 The later results in segfaults even when a decl using the type doesn't
10184 get invoked. */
10186 tree
10187 builtin_type_for_size (int size, bool unsignedp)
10189 tree type = c_common_type_for_size (size, unsignedp);
10190 return type ? type : error_mark_node;
10193 /* A helper function for resolve_overloaded_builtin in resolving the
10194 overloaded __sync_ builtins. Returns a positive power of 2 if the
10195 first operand of PARAMS is a pointer to a supported data type.
10196 Returns 0 if an error is encountered. */
10198 static int
10199 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10201 tree type;
10202 int size;
10204 if (!params)
10206 error ("too few arguments to function %qE", function);
10207 return 0;
10210 type = TREE_TYPE ((*params)[0]);
10211 if (TREE_CODE (type) != POINTER_TYPE)
10212 goto incompatible;
10214 type = TREE_TYPE (type);
10215 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10216 goto incompatible;
10218 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10219 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10220 return size;
10222 incompatible:
10223 error ("incompatible type for argument %d of %qE", 1, function);
10224 return 0;
10227 /* A helper function for resolve_overloaded_builtin. Adds casts to
10228 PARAMS to make arguments match up with those of FUNCTION. Drops
10229 the variadic arguments at the end. Returns false if some error
10230 was encountered; true on success. */
10232 static bool
10233 sync_resolve_params (location_t loc, tree orig_function, tree function,
10234 vec<tree, va_gc> *params, bool orig_format)
10236 function_args_iterator iter;
10237 tree ptype;
10238 unsigned int parmnum;
10240 function_args_iter_init (&iter, TREE_TYPE (function));
10241 /* We've declared the implementation functions to use "volatile void *"
10242 as the pointer parameter, so we shouldn't get any complaints from the
10243 call to check_function_arguments what ever type the user used. */
10244 function_args_iter_next (&iter);
10245 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10246 ptype = TYPE_MAIN_VARIANT (ptype);
10248 /* For the rest of the values, we need to cast these to FTYPE, so that we
10249 don't get warnings for passing pointer types, etc. */
10250 parmnum = 0;
10251 while (1)
10253 tree val, arg_type;
10255 arg_type = function_args_iter_cond (&iter);
10256 /* XXX void_type_node belies the abstraction. */
10257 if (arg_type == void_type_node)
10258 break;
10260 ++parmnum;
10261 if (params->length () <= parmnum)
10263 error_at (loc, "too few arguments to function %qE", orig_function);
10264 return false;
10267 /* Only convert parameters if arg_type is unsigned integer type with
10268 new format sync routines, i.e. don't attempt to convert pointer
10269 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10270 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10271 kinds). */
10272 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10274 /* Ideally for the first conversion we'd use convert_for_assignment
10275 so that we get warnings for anything that doesn't match the pointer
10276 type. This isn't portable across the C and C++ front ends atm. */
10277 val = (*params)[parmnum];
10278 val = convert (ptype, val);
10279 val = convert (arg_type, val);
10280 (*params)[parmnum] = val;
10283 function_args_iter_next (&iter);
10286 /* __atomic routines are not variadic. */
10287 if (!orig_format && params->length () != parmnum + 1)
10289 error_at (loc, "too many arguments to function %qE", orig_function);
10290 return false;
10293 /* The definition of these primitives is variadic, with the remaining
10294 being "an optional list of variables protected by the memory barrier".
10295 No clue what that's supposed to mean, precisely, but we consider all
10296 call-clobbered variables to be protected so we're safe. */
10297 params->truncate (parmnum + 1);
10299 return true;
10302 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10303 RESULT to make it match the type of the first pointer argument in
10304 PARAMS. */
10306 static tree
10307 sync_resolve_return (tree first_param, tree result, bool orig_format)
10309 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10310 tree rtype = TREE_TYPE (result);
10311 ptype = TYPE_MAIN_VARIANT (ptype);
10313 /* New format doesn't require casting unless the types are the same size. */
10314 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10315 return convert (ptype, result);
10316 else
10317 return result;
10320 /* This function verifies the PARAMS to generic atomic FUNCTION.
10321 It returns the size if all the parameters are the same size, otherwise
10322 0 is returned if the parameters are invalid. */
10324 static int
10325 get_atomic_generic_size (location_t loc, tree function,
10326 vec<tree, va_gc> *params)
10328 unsigned int n_param;
10329 unsigned int n_model;
10330 unsigned int x;
10331 int size_0;
10332 tree type_0;
10334 /* Determine the parameter makeup. */
10335 switch (DECL_FUNCTION_CODE (function))
10337 case BUILT_IN_ATOMIC_EXCHANGE:
10338 n_param = 4;
10339 n_model = 1;
10340 break;
10341 case BUILT_IN_ATOMIC_LOAD:
10342 case BUILT_IN_ATOMIC_STORE:
10343 n_param = 3;
10344 n_model = 1;
10345 break;
10346 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10347 n_param = 6;
10348 n_model = 2;
10349 break;
10350 default:
10351 gcc_unreachable ();
10354 if (vec_safe_length (params) != n_param)
10356 error_at (loc, "incorrect number of arguments to function %qE", function);
10357 return 0;
10360 /* Get type of first parameter, and determine its size. */
10361 type_0 = TREE_TYPE ((*params)[0]);
10362 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10364 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10365 function);
10366 return 0;
10369 /* Types must be compile time constant sizes. */
10370 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10372 error_at (loc,
10373 "argument 1 of %qE must be a pointer to a constant size type",
10374 function);
10375 return 0;
10378 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10380 /* Zero size objects are not allowed. */
10381 if (size_0 == 0)
10383 error_at (loc,
10384 "argument 1 of %qE must be a pointer to a nonzero size object",
10385 function);
10386 return 0;
10389 /* Check each other parameter is a pointer and the same size. */
10390 for (x = 0; x < n_param - n_model; x++)
10392 int size;
10393 tree type = TREE_TYPE ((*params)[x]);
10394 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10395 if (n_param == 6 && x == 3)
10396 continue;
10397 if (!POINTER_TYPE_P (type))
10399 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10400 function);
10401 return 0;
10403 size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
10404 if (size != size_0)
10406 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10407 function);
10408 return 0;
10412 /* Check memory model parameters for validity. */
10413 for (x = n_param - n_model ; x < n_param; x++)
10415 tree p = (*params)[x];
10416 if (TREE_CODE (p) == INTEGER_CST)
10418 int i = tree_to_uhwi (p);
10419 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10421 warning_at (loc, OPT_Winvalid_memory_model,
10422 "invalid memory model argument %d of %qE", x + 1,
10423 function);
10426 else
10427 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10429 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10430 function);
10431 return 0;
10435 return size_0;
10439 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10440 at the beginning of the parameter list PARAMS representing the size of the
10441 objects. This is to match the library ABI requirement. LOC is the location
10442 of the function call.
10443 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10444 returned to allow the external call to be constructed. */
10446 static tree
10447 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10448 vec<tree, va_gc> *params)
10450 tree size_node;
10452 /* Insert a SIZE_T parameter as the first param. If there isn't
10453 enough space, allocate a new vector and recursively re-build with that. */
10454 if (!params->space (1))
10456 unsigned int z, len;
10457 vec<tree, va_gc> *v;
10458 tree f;
10460 len = params->length ();
10461 vec_alloc (v, len + 1);
10462 for (z = 0; z < len; z++)
10463 v->quick_push ((*params)[z]);
10464 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10465 vec_free (v);
10466 return f;
10469 /* Add the size parameter and leave as a function call for processing. */
10470 size_node = build_int_cst (size_type_node, n);
10471 params->quick_insert (0, size_node);
10472 return NULL_TREE;
10476 /* Return whether atomic operations for naturally aligned N-byte
10477 arguments are supported, whether inline or through libatomic. */
10478 static bool
10479 atomic_size_supported_p (int n)
10481 switch (n)
10483 case 1:
10484 case 2:
10485 case 4:
10486 case 8:
10487 return true;
10489 case 16:
10490 return targetm.scalar_mode_supported_p (TImode);
10492 default:
10493 return false;
10497 /* This will process an __atomic_exchange function call, determine whether it
10498 needs to be mapped to the _N variation, or turned into a library call.
10499 LOC is the location of the builtin call.
10500 FUNCTION is the DECL that has been invoked;
10501 PARAMS is the argument list for the call. The return value is non-null
10502 TRUE is returned if it is translated into the proper format for a call to the
10503 external library, and NEW_RETURN is set the tree for that function.
10504 FALSE is returned if processing for the _N variation is required, and
10505 NEW_RETURN is set to the the return value the result is copied into. */
10506 static bool
10507 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10508 vec<tree, va_gc> *params, tree *new_return)
10510 tree p0, p1, p2, p3;
10511 tree I_type, I_type_ptr;
10512 int n = get_atomic_generic_size (loc, function, params);
10514 /* Size of 0 is an error condition. */
10515 if (n == 0)
10517 *new_return = error_mark_node;
10518 return true;
10521 /* If not a lock-free size, change to the library generic format. */
10522 if (!atomic_size_supported_p (n))
10524 *new_return = add_atomic_size_parameter (n, loc, function, params);
10525 return true;
10528 /* Otherwise there is a lockfree match, transform the call from:
10529 void fn(T* mem, T* desired, T* return, model)
10530 into
10531 *return = (T) (fn (In* mem, (In) *desired, model)) */
10533 p0 = (*params)[0];
10534 p1 = (*params)[1];
10535 p2 = (*params)[2];
10536 p3 = (*params)[3];
10538 /* Create pointer to appropriate size. */
10539 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10540 I_type_ptr = build_pointer_type (I_type);
10542 /* Convert object pointer to required type. */
10543 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10544 (*params)[0] = p0;
10545 /* Convert new value to required type, and dereference it. */
10546 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10547 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10548 (*params)[1] = p1;
10550 /* Move memory model to the 3rd position, and end param list. */
10551 (*params)[2] = p3;
10552 params->truncate (3);
10554 /* Convert return pointer and dereference it for later assignment. */
10555 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10557 return false;
10561 /* This will process an __atomic_compare_exchange function call, determine
10562 whether it needs to be mapped to the _N variation, or turned into a lib call.
10563 LOC is the location of the builtin call.
10564 FUNCTION is the DECL that has been invoked;
10565 PARAMS is the argument list for the call. The return value is non-null
10566 TRUE is returned if it is translated into the proper format for a call to the
10567 external library, and NEW_RETURN is set the tree for that function.
10568 FALSE is returned if processing for the _N variation is required. */
10570 static bool
10571 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10572 vec<tree, va_gc> *params,
10573 tree *new_return)
10575 tree p0, p1, p2;
10576 tree I_type, I_type_ptr;
10577 int n = get_atomic_generic_size (loc, function, params);
10579 /* Size of 0 is an error condition. */
10580 if (n == 0)
10582 *new_return = error_mark_node;
10583 return true;
10586 /* If not a lock-free size, change to the library generic format. */
10587 if (!atomic_size_supported_p (n))
10589 /* The library generic format does not have the weak parameter, so
10590 remove it from the param list. Since a parameter has been removed,
10591 we can be sure that there is room for the SIZE_T parameter, meaning
10592 there will not be a recursive rebuilding of the parameter list, so
10593 there is no danger this will be done twice. */
10594 if (n > 0)
10596 (*params)[3] = (*params)[4];
10597 (*params)[4] = (*params)[5];
10598 params->truncate (5);
10600 *new_return = add_atomic_size_parameter (n, loc, function, params);
10601 return true;
10604 /* Otherwise, there is a match, so the call needs to be transformed from:
10605 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10606 into
10607 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10609 p0 = (*params)[0];
10610 p1 = (*params)[1];
10611 p2 = (*params)[2];
10613 /* Create pointer to appropriate size. */
10614 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10615 I_type_ptr = build_pointer_type (I_type);
10617 /* Convert object pointer to required type. */
10618 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10619 (*params)[0] = p0;
10621 /* Convert expected pointer to required type. */
10622 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10623 (*params)[1] = p1;
10625 /* Convert desired value to required type, and dereference it. */
10626 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10627 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10628 (*params)[2] = p2;
10630 /* The rest of the parameters are fine. NULL means no special return value
10631 processing.*/
10632 *new_return = NULL;
10633 return false;
10637 /* This will process an __atomic_load function call, determine whether it
10638 needs to be mapped to the _N variation, or turned into a library call.
10639 LOC is the location of the builtin call.
10640 FUNCTION is the DECL that has been invoked;
10641 PARAMS is the argument list for the call. The return value is non-null
10642 TRUE is returned if it is translated into the proper format for a call to the
10643 external library, and NEW_RETURN is set the tree for that function.
10644 FALSE is returned if processing for the _N variation is required, and
10645 NEW_RETURN is set to the the return value the result is copied into. */
10647 static bool
10648 resolve_overloaded_atomic_load (location_t loc, tree function,
10649 vec<tree, va_gc> *params, tree *new_return)
10651 tree p0, p1, p2;
10652 tree I_type, I_type_ptr;
10653 int n = get_atomic_generic_size (loc, function, params);
10655 /* Size of 0 is an error condition. */
10656 if (n == 0)
10658 *new_return = error_mark_node;
10659 return true;
10662 /* If not a lock-free size, change to the library generic format. */
10663 if (!atomic_size_supported_p (n))
10665 *new_return = add_atomic_size_parameter (n, loc, function, params);
10666 return true;
10669 /* Otherwise, there is a match, so the call needs to be transformed from:
10670 void fn(T* mem, T* return, model)
10671 into
10672 *return = (T) (fn ((In *) mem, model)) */
10674 p0 = (*params)[0];
10675 p1 = (*params)[1];
10676 p2 = (*params)[2];
10678 /* Create pointer to appropriate size. */
10679 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10680 I_type_ptr = build_pointer_type (I_type);
10682 /* Convert object pointer to required type. */
10683 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10684 (*params)[0] = p0;
10686 /* Move memory model to the 2nd position, and end param list. */
10687 (*params)[1] = p2;
10688 params->truncate (2);
10690 /* Convert return pointer and dereference it for later assignment. */
10691 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10693 return false;
10697 /* This will process an __atomic_store function call, determine whether it
10698 needs to be mapped to the _N variation, or turned into a library call.
10699 LOC is the location of the builtin call.
10700 FUNCTION is the DECL that has been invoked;
10701 PARAMS is the argument list for the call. The return value is non-null
10702 TRUE is returned if it is translated into the proper format for a call to the
10703 external library, and NEW_RETURN is set the tree for that function.
10704 FALSE is returned if processing for the _N variation is required, and
10705 NEW_RETURN is set to the the return value the result is copied into. */
10707 static bool
10708 resolve_overloaded_atomic_store (location_t loc, tree function,
10709 vec<tree, va_gc> *params, tree *new_return)
10711 tree p0, p1;
10712 tree I_type, I_type_ptr;
10713 int n = get_atomic_generic_size (loc, function, params);
10715 /* Size of 0 is an error condition. */
10716 if (n == 0)
10718 *new_return = error_mark_node;
10719 return true;
10722 /* If not a lock-free size, change to the library generic format. */
10723 if (!atomic_size_supported_p (n))
10725 *new_return = add_atomic_size_parameter (n, loc, function, params);
10726 return true;
10729 /* Otherwise, there is a match, so the call needs to be transformed from:
10730 void fn(T* mem, T* value, model)
10731 into
10732 fn ((In *) mem, (In) *value, model) */
10734 p0 = (*params)[0];
10735 p1 = (*params)[1];
10737 /* Create pointer to appropriate size. */
10738 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10739 I_type_ptr = build_pointer_type (I_type);
10741 /* Convert object pointer to required type. */
10742 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10743 (*params)[0] = p0;
10745 /* Convert new value to required type, and dereference it. */
10746 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10747 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10748 (*params)[1] = p1;
10750 /* The memory model is in the right spot already. Return is void. */
10751 *new_return = NULL_TREE;
10753 return false;
10757 /* Some builtin functions are placeholders for other expressions. This
10758 function should be called immediately after parsing the call expression
10759 before surrounding code has committed to the type of the expression.
10761 LOC is the location of the builtin call.
10763 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10764 PARAMS is the argument list for the call. The return value is non-null
10765 when expansion is complete, and null if normal processing should
10766 continue. */
10768 tree
10769 resolve_overloaded_builtin (location_t loc, tree function,
10770 vec<tree, va_gc> *params)
10772 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10773 bool orig_format = true;
10774 tree new_return = NULL_TREE;
10776 switch (DECL_BUILT_IN_CLASS (function))
10778 case BUILT_IN_NORMAL:
10779 break;
10780 case BUILT_IN_MD:
10781 if (targetm.resolve_overloaded_builtin)
10782 return targetm.resolve_overloaded_builtin (loc, function, params);
10783 else
10784 return NULL_TREE;
10785 default:
10786 return NULL_TREE;
10789 /* Handle BUILT_IN_NORMAL here. */
10790 switch (orig_code)
10792 case BUILT_IN_ATOMIC_EXCHANGE:
10793 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10794 case BUILT_IN_ATOMIC_LOAD:
10795 case BUILT_IN_ATOMIC_STORE:
10797 /* Handle these 4 together so that they can fall through to the next
10798 case if the call is transformed to an _N variant. */
10799 switch (orig_code)
10801 case BUILT_IN_ATOMIC_EXCHANGE:
10803 if (resolve_overloaded_atomic_exchange (loc, function, params,
10804 &new_return))
10805 return new_return;
10806 /* Change to the _N variant. */
10807 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10808 break;
10811 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10813 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10814 params,
10815 &new_return))
10816 return new_return;
10817 /* Change to the _N variant. */
10818 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10819 break;
10821 case BUILT_IN_ATOMIC_LOAD:
10823 if (resolve_overloaded_atomic_load (loc, function, params,
10824 &new_return))
10825 return new_return;
10826 /* Change to the _N variant. */
10827 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10828 break;
10830 case BUILT_IN_ATOMIC_STORE:
10832 if (resolve_overloaded_atomic_store (loc, function, params,
10833 &new_return))
10834 return new_return;
10835 /* Change to the _N variant. */
10836 orig_code = BUILT_IN_ATOMIC_STORE_N;
10837 break;
10839 default:
10840 gcc_unreachable ();
10842 /* Fallthrough to the normal processing. */
10844 case BUILT_IN_ATOMIC_EXCHANGE_N:
10845 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10846 case BUILT_IN_ATOMIC_LOAD_N:
10847 case BUILT_IN_ATOMIC_STORE_N:
10848 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10849 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10850 case BUILT_IN_ATOMIC_AND_FETCH_N:
10851 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10852 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10853 case BUILT_IN_ATOMIC_OR_FETCH_N:
10854 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10855 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10856 case BUILT_IN_ATOMIC_FETCH_AND_N:
10857 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10858 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10859 case BUILT_IN_ATOMIC_FETCH_OR_N:
10861 orig_format = false;
10862 /* Fallthru for parameter processing. */
10864 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10865 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10866 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10867 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10868 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10869 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10870 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10871 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10872 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10873 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10874 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10875 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10876 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10877 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10878 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10879 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10881 int n = sync_resolve_size (function, params);
10882 tree new_function, first_param, result;
10883 enum built_in_function fncode;
10885 if (n == 0)
10886 return error_mark_node;
10888 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10889 new_function = builtin_decl_explicit (fncode);
10890 if (!sync_resolve_params (loc, function, new_function, params,
10891 orig_format))
10892 return error_mark_node;
10894 first_param = (*params)[0];
10895 result = build_function_call_vec (loc, vNULL, new_function, params,
10896 NULL);
10897 if (result == error_mark_node)
10898 return result;
10899 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10900 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10901 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10902 result = sync_resolve_return (first_param, result, orig_format);
10904 /* If new_return is set, assign function to that expr and cast the
10905 result to void since the generic interface returned void. */
10906 if (new_return)
10908 /* Cast function result from I{1,2,4,8,16} to the required type. */
10909 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10910 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10911 result);
10912 TREE_SIDE_EFFECTS (result) = 1;
10913 protected_set_expr_location (result, loc);
10914 result = convert (void_type_node, result);
10916 return result;
10919 default:
10920 return NULL_TREE;
10924 /* vector_types_compatible_elements_p is used in type checks of vectors
10925 values used as operands of binary operators. Where it returns true, and
10926 the other checks of the caller succeed (being vector types in he first
10927 place, and matching number of elements), we can just treat the types
10928 as essentially the same.
10929 Contrast with vector_targets_convertible_p, which is used for vector
10930 pointer types, and vector_types_convertible_p, which will allow
10931 language-specific matches under the control of flag_lax_vector_conversions,
10932 and might still require a conversion. */
10933 /* True if vector types T1 and T2 can be inputs to the same binary
10934 operator without conversion.
10935 We don't check the overall vector size here because some of our callers
10936 want to give different error messages when the vectors are compatible
10937 except for the element count. */
10939 bool
10940 vector_types_compatible_elements_p (tree t1, tree t2)
10942 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10943 t1 = TREE_TYPE (t1);
10944 t2 = TREE_TYPE (t2);
10946 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10948 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10949 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10950 || c2 == FIXED_POINT_TYPE));
10952 t1 = c_common_signed_type (t1);
10953 t2 = c_common_signed_type (t2);
10954 /* Equality works here because c_common_signed_type uses
10955 TYPE_MAIN_VARIANT. */
10956 if (t1 == t2)
10957 return true;
10958 if (opaque && c1 == c2
10959 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10960 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10961 return true;
10962 return false;
10965 /* Check for missing format attributes on function pointers. LTYPE is
10966 the new type or left-hand side type. RTYPE is the old type or
10967 right-hand side type. Returns TRUE if LTYPE is missing the desired
10968 attribute. */
10970 bool
10971 check_missing_format_attribute (tree ltype, tree rtype)
10973 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10974 tree ra;
10976 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10977 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10978 break;
10979 if (ra)
10981 tree la;
10982 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10983 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10984 break;
10985 return !la;
10987 else
10988 return false;
10991 /* Subscripting with type char is likely to lose on a machine where
10992 chars are signed. So warn on any machine, but optionally. Don't
10993 warn for unsigned char since that type is safe. Don't warn for
10994 signed char because anyone who uses that must have done so
10995 deliberately. Furthermore, we reduce the false positive load by
10996 warning only for non-constant value of type char. */
10998 void
10999 warn_array_subscript_with_type_char (tree index)
11001 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11002 && TREE_CODE (index) != INTEGER_CST)
11003 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11006 /* Implement -Wparentheses for the unexpected C precedence rules, to
11007 cover cases like x + y << z which readers are likely to
11008 misinterpret. We have seen an expression in which CODE is a binary
11009 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11010 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11011 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11012 expression was not formed using a binary or unary operator, or it
11013 was enclosed in parentheses. */
11015 void
11016 warn_about_parentheses (location_t loc, enum tree_code code,
11017 enum tree_code code_left, tree arg_left,
11018 enum tree_code code_right, tree arg_right)
11020 if (!warn_parentheses)
11021 return;
11023 /* This macro tests that the expression ARG with original tree code
11024 CODE appears to be a boolean expression. or the result of folding a
11025 boolean expression. */
11026 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11027 (truth_value_p (TREE_CODE (ARG)) \
11028 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11029 /* Folding may create 0 or 1 integers from other expressions. */ \
11030 || ((CODE) != INTEGER_CST \
11031 && (integer_onep (ARG) || integer_zerop (ARG))))
11033 switch (code)
11035 case LSHIFT_EXPR:
11036 if (code_left == PLUS_EXPR)
11037 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11038 "suggest parentheses around %<+%> inside %<<<%>");
11039 else if (code_right == PLUS_EXPR)
11040 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11041 "suggest parentheses around %<+%> inside %<<<%>");
11042 else if (code_left == MINUS_EXPR)
11043 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11044 "suggest parentheses around %<-%> inside %<<<%>");
11045 else if (code_right == MINUS_EXPR)
11046 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11047 "suggest parentheses around %<-%> inside %<<<%>");
11048 return;
11050 case RSHIFT_EXPR:
11051 if (code_left == PLUS_EXPR)
11052 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11053 "suggest parentheses around %<+%> inside %<>>%>");
11054 else if (code_right == PLUS_EXPR)
11055 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11056 "suggest parentheses around %<+%> inside %<>>%>");
11057 else if (code_left == MINUS_EXPR)
11058 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11059 "suggest parentheses around %<-%> inside %<>>%>");
11060 else if (code_right == MINUS_EXPR)
11061 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11062 "suggest parentheses around %<-%> inside %<>>%>");
11063 return;
11065 case TRUTH_ORIF_EXPR:
11066 if (code_left == TRUTH_ANDIF_EXPR)
11067 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11068 "suggest parentheses around %<&&%> within %<||%>");
11069 else if (code_right == TRUTH_ANDIF_EXPR)
11070 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11071 "suggest parentheses around %<&&%> within %<||%>");
11072 return;
11074 case BIT_IOR_EXPR:
11075 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11076 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11077 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11078 "suggest parentheses around arithmetic in operand of %<|%>");
11079 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11080 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11081 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11082 "suggest parentheses around arithmetic in operand of %<|%>");
11083 /* Check cases like x|y==z */
11084 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11085 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11086 "suggest parentheses around comparison in operand of %<|%>");
11087 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11088 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11089 "suggest parentheses around comparison in operand of %<|%>");
11090 /* Check cases like !x | y */
11091 else if (code_left == TRUTH_NOT_EXPR
11092 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11093 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11094 "suggest parentheses around operand of "
11095 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11096 return;
11098 case BIT_XOR_EXPR:
11099 if (code_left == BIT_AND_EXPR
11100 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11101 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11102 "suggest parentheses around arithmetic in operand of %<^%>");
11103 else if (code_right == BIT_AND_EXPR
11104 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11105 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11106 "suggest parentheses around arithmetic in operand of %<^%>");
11107 /* Check cases like x^y==z */
11108 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11109 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11110 "suggest parentheses around comparison in operand of %<^%>");
11111 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11112 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11113 "suggest parentheses around comparison in operand of %<^%>");
11114 return;
11116 case BIT_AND_EXPR:
11117 if (code_left == PLUS_EXPR)
11118 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11119 "suggest parentheses around %<+%> in operand of %<&%>");
11120 else if (code_right == PLUS_EXPR)
11121 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11122 "suggest parentheses around %<+%> in operand of %<&%>");
11123 else if (code_left == MINUS_EXPR)
11124 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11125 "suggest parentheses around %<-%> in operand of %<&%>");
11126 else if (code_right == MINUS_EXPR)
11127 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11128 "suggest parentheses around %<-%> in operand of %<&%>");
11129 /* Check cases like x&y==z */
11130 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11131 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11132 "suggest parentheses around comparison in operand of %<&%>");
11133 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11134 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11135 "suggest parentheses around comparison in operand of %<&%>");
11136 /* Check cases like !x & y */
11137 else if (code_left == TRUTH_NOT_EXPR
11138 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11139 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11140 "suggest parentheses around operand of "
11141 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11142 return;
11144 case EQ_EXPR:
11145 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11146 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11147 "suggest parentheses around comparison in operand of %<==%>");
11148 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11149 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11150 "suggest parentheses around comparison in operand of %<==%>");
11151 return;
11152 case NE_EXPR:
11153 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11154 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11155 "suggest parentheses around comparison in operand of %<!=%>");
11156 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11157 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11158 "suggest parentheses around comparison in operand of %<!=%>");
11159 return;
11161 default:
11162 if (TREE_CODE_CLASS (code) == tcc_comparison)
11164 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11165 && code_left != NE_EXPR && code_left != EQ_EXPR
11166 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11167 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11168 "comparisons like %<X<=Y<=Z%> do not "
11169 "have their mathematical meaning");
11170 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11171 && code_right != NE_EXPR && code_right != EQ_EXPR
11172 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11173 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11174 "comparisons like %<X<=Y<=Z%> do not "
11175 "have their mathematical meaning");
11177 return;
11179 #undef NOT_A_BOOLEAN_EXPR_P
11182 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11184 void
11185 warn_for_unused_label (tree label)
11187 if (!TREE_USED (label))
11189 if (DECL_INITIAL (label))
11190 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11191 else
11192 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11196 /* Warn for division by zero according to the value of DIVISOR. LOC
11197 is the location of the division operator. */
11199 void
11200 warn_for_div_by_zero (location_t loc, tree divisor)
11202 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11203 about division by zero. Do not issue a warning if DIVISOR has a
11204 floating-point type, since we consider 0.0/0.0 a valid way of
11205 generating a NaN. */
11206 if (c_inhibit_evaluation_warnings == 0
11207 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11208 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11211 /* Subroutine of build_binary_op. Give warnings for comparisons
11212 between signed and unsigned quantities that may fail. Do the
11213 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11214 so that casts will be considered, but default promotions won't
11217 LOCATION is the location of the comparison operator.
11219 The arguments of this function map directly to local variables
11220 of build_binary_op. */
11222 void
11223 warn_for_sign_compare (location_t location,
11224 tree orig_op0, tree orig_op1,
11225 tree op0, tree op1,
11226 tree result_type, enum tree_code resultcode)
11228 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11229 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11230 int unsignedp0, unsignedp1;
11232 /* In C++, check for comparison of different enum types. */
11233 if (c_dialect_cxx()
11234 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11235 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11236 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11237 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11239 warning_at (location,
11240 OPT_Wsign_compare, "comparison between types %qT and %qT",
11241 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11244 /* Do not warn if the comparison is being done in a signed type,
11245 since the signed type will only be chosen if it can represent
11246 all the values of the unsigned type. */
11247 if (!TYPE_UNSIGNED (result_type))
11248 /* OK */;
11249 /* Do not warn if both operands are unsigned. */
11250 else if (op0_signed == op1_signed)
11251 /* OK */;
11252 else
11254 tree sop, uop, base_type;
11255 bool ovf;
11257 if (op0_signed)
11258 sop = orig_op0, uop = orig_op1;
11259 else
11260 sop = orig_op1, uop = orig_op0;
11262 STRIP_TYPE_NOPS (sop);
11263 STRIP_TYPE_NOPS (uop);
11264 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11265 ? TREE_TYPE (result_type) : result_type);
11267 /* Do not warn if the signed quantity is an unsuffixed integer
11268 literal (or some static constant expression involving such
11269 literals or a conditional expression involving such literals)
11270 and it is non-negative. */
11271 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11272 /* OK */;
11273 /* Do not warn if the comparison is an equality operation, the
11274 unsigned quantity is an integral constant, and it would fit
11275 in the result if the result were signed. */
11276 else if (TREE_CODE (uop) == INTEGER_CST
11277 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11278 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11279 /* OK */;
11280 /* In C, do not warn if the unsigned quantity is an enumeration
11281 constant and its maximum value would fit in the result if the
11282 result were signed. */
11283 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11284 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11285 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11286 c_common_signed_type (base_type)))
11287 /* OK */;
11288 else
11289 warning_at (location,
11290 OPT_Wsign_compare,
11291 "comparison between signed and unsigned integer expressions");
11294 /* Warn if two unsigned values are being compared in a size larger
11295 than their original size, and one (and only one) is the result of
11296 a `~' operator. This comparison will always fail.
11298 Also warn if one operand is a constant, and the constant does not
11299 have all bits set that are set in the ~ operand when it is
11300 extended. */
11302 op0 = c_common_get_narrower (op0, &unsignedp0);
11303 op1 = c_common_get_narrower (op1, &unsignedp1);
11305 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11306 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11308 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11309 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11310 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11311 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11313 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11315 tree primop;
11316 HOST_WIDE_INT constant, mask;
11317 int unsignedp;
11318 unsigned int bits;
11320 if (tree_fits_shwi_p (op0))
11322 primop = op1;
11323 unsignedp = unsignedp1;
11324 constant = tree_to_shwi (op0);
11326 else
11328 primop = op0;
11329 unsignedp = unsignedp0;
11330 constant = tree_to_shwi (op1);
11333 bits = TYPE_PRECISION (TREE_TYPE (primop));
11334 if (bits < TYPE_PRECISION (result_type)
11335 && bits < HOST_BITS_PER_LONG && unsignedp)
11337 mask = (~ (HOST_WIDE_INT) 0) << bits;
11338 if ((mask & constant) != mask)
11340 if (constant == 0)
11341 warning_at (location, OPT_Wsign_compare,
11342 "promoted ~unsigned is always non-zero");
11343 else
11344 warning_at (location, OPT_Wsign_compare,
11345 "comparison of promoted ~unsigned with constant");
11349 else if (unsignedp0 && unsignedp1
11350 && (TYPE_PRECISION (TREE_TYPE (op0))
11351 < TYPE_PRECISION (result_type))
11352 && (TYPE_PRECISION (TREE_TYPE (op1))
11353 < TYPE_PRECISION (result_type)))
11354 warning_at (location, OPT_Wsign_compare,
11355 "comparison of promoted ~unsigned with unsigned");
11359 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11360 type via c_common_type. If -Wdouble-promotion is in use, and the
11361 conditions for warning have been met, issue a warning. GMSGID is
11362 the warning message. It must have two %T specifiers for the type
11363 that was converted (generally "float") and the type to which it was
11364 converted (generally "double), respectively. LOC is the location
11365 to which the awrning should refer. */
11367 void
11368 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11369 const char *gmsgid, location_t loc)
11371 tree source_type;
11373 if (!warn_double_promotion)
11374 return;
11375 /* If the conversion will not occur at run-time, there is no need to
11376 warn about it. */
11377 if (c_inhibit_evaluation_warnings)
11378 return;
11379 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11380 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11381 return;
11382 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11383 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11384 source_type = type1;
11385 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11386 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11387 source_type = type2;
11388 else
11389 return;
11390 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11393 /* Setup a TYPE_DECL node as a typedef representation.
11395 X is a TYPE_DECL for a typedef statement. Create a brand new
11396 ..._TYPE node (which will be just a variant of the existing
11397 ..._TYPE node with identical properties) and then install X
11398 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11400 The whole point here is to end up with a situation where each
11401 and every ..._TYPE node the compiler creates will be uniquely
11402 associated with AT MOST one node representing a typedef name.
11403 This way, even though the compiler substitutes corresponding
11404 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11405 early on, later parts of the compiler can always do the reverse
11406 translation and get back the corresponding typedef name. For
11407 example, given:
11409 typedef struct S MY_TYPE;
11410 MY_TYPE object;
11412 Later parts of the compiler might only know that `object' was of
11413 type `struct S' if it were not for code just below. With this
11414 code however, later parts of the compiler see something like:
11416 struct S' == struct S
11417 typedef struct S' MY_TYPE;
11418 struct S' object;
11420 And they can then deduce (from the node for type struct S') that
11421 the original object declaration was:
11423 MY_TYPE object;
11425 Being able to do this is important for proper support of protoize,
11426 and also for generating precise symbolic debugging information
11427 which takes full account of the programmer's (typedef) vocabulary.
11429 Obviously, we don't want to generate a duplicate ..._TYPE node if
11430 the TYPE_DECL node that we are now processing really represents a
11431 standard built-in type. */
11433 void
11434 set_underlying_type (tree x)
11436 if (x == error_mark_node)
11437 return;
11438 if (DECL_IS_BUILTIN (x))
11440 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11441 TYPE_NAME (TREE_TYPE (x)) = x;
11443 else if (TREE_TYPE (x) != error_mark_node
11444 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11446 tree tt = TREE_TYPE (x);
11447 DECL_ORIGINAL_TYPE (x) = tt;
11448 tt = build_variant_type_copy (tt);
11449 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11450 TYPE_NAME (tt) = x;
11451 TREE_USED (tt) = TREE_USED (x);
11452 TREE_TYPE (x) = tt;
11456 /* Record the types used by the current global variable declaration
11457 being parsed, so that we can decide later to emit their debug info.
11458 Those types are in types_used_by_cur_var_decl, and we are going to
11459 store them in the types_used_by_vars_hash hash table.
11460 DECL is the declaration of the global variable that has been parsed. */
11462 void
11463 record_types_used_by_current_var_decl (tree decl)
11465 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11467 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11469 tree type = types_used_by_cur_var_decl->pop ();
11470 types_used_by_var_decl_insert (type, decl);
11474 /* If DECL is a typedef that is declared in the current function,
11475 record it for the purpose of -Wunused-local-typedefs. */
11477 void
11478 record_locally_defined_typedef (tree decl)
11480 struct c_language_function *l;
11482 if (!warn_unused_local_typedefs
11483 || cfun == NULL
11484 /* if this is not a locally defined typedef then we are not
11485 interested. */
11486 || !is_typedef_decl (decl)
11487 || !decl_function_context (decl))
11488 return;
11490 l = (struct c_language_function *) cfun->language;
11491 vec_safe_push (l->local_typedefs, decl);
11494 /* If T is a TYPE_DECL declared locally, mark it as used. */
11496 void
11497 maybe_record_typedef_use (tree t)
11499 if (!is_typedef_decl (t))
11500 return;
11502 TREE_USED (t) = true;
11505 /* Warn if there are some unused locally defined typedefs in the
11506 current function. */
11508 void
11509 maybe_warn_unused_local_typedefs (void)
11511 int i;
11512 tree decl;
11513 /* The number of times we have emitted -Wunused-local-typedefs
11514 warnings. If this is different from errorcount, that means some
11515 unrelated errors have been issued. In which case, we'll avoid
11516 emitting "unused-local-typedefs" warnings. */
11517 static int unused_local_typedefs_warn_count;
11518 struct c_language_function *l;
11520 if (cfun == NULL)
11521 return;
11523 if ((l = (struct c_language_function *) cfun->language) == NULL)
11524 return;
11526 if (warn_unused_local_typedefs
11527 && errorcount == unused_local_typedefs_warn_count)
11529 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11530 if (!TREE_USED (decl))
11531 warning_at (DECL_SOURCE_LOCATION (decl),
11532 OPT_Wunused_local_typedefs,
11533 "typedef %qD locally defined but not used", decl);
11534 unused_local_typedefs_warn_count = errorcount;
11537 vec_free (l->local_typedefs);
11540 /* The C and C++ parsers both use vectors to hold function arguments.
11541 For efficiency, we keep a cache of unused vectors. This is the
11542 cache. */
11544 typedef vec<tree, va_gc> *tree_gc_vec;
11545 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11547 /* Return a new vector from the cache. If the cache is empty,
11548 allocate a new vector. These vectors are GC'ed, so it is OK if the
11549 pointer is not released.. */
11551 vec<tree, va_gc> *
11552 make_tree_vector (void)
11554 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11555 return tree_vector_cache->pop ();
11556 else
11558 /* Passing 0 to vec::alloc returns NULL, and our callers require
11559 that we always return a non-NULL value. The vector code uses
11560 4 when growing a NULL vector, so we do too. */
11561 vec<tree, va_gc> *v;
11562 vec_alloc (v, 4);
11563 return v;
11567 /* Release a vector of trees back to the cache. */
11569 void
11570 release_tree_vector (vec<tree, va_gc> *vec)
11572 if (vec != NULL)
11574 vec->truncate (0);
11575 vec_safe_push (tree_vector_cache, vec);
11579 /* Get a new tree vector holding a single tree. */
11581 vec<tree, va_gc> *
11582 make_tree_vector_single (tree t)
11584 vec<tree, va_gc> *ret = make_tree_vector ();
11585 ret->quick_push (t);
11586 return ret;
11589 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11591 vec<tree, va_gc> *
11592 make_tree_vector_from_list (tree list)
11594 vec<tree, va_gc> *ret = make_tree_vector ();
11595 for (; list; list = TREE_CHAIN (list))
11596 vec_safe_push (ret, TREE_VALUE (list));
11597 return ret;
11600 /* Get a new tree vector which is a copy of an existing one. */
11602 vec<tree, va_gc> *
11603 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11605 vec<tree, va_gc> *ret;
11606 unsigned int ix;
11607 tree t;
11609 ret = make_tree_vector ();
11610 vec_safe_reserve (ret, vec_safe_length (orig));
11611 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11612 ret->quick_push (t);
11613 return ret;
11616 /* Return true if KEYWORD starts a type specifier. */
11618 bool
11619 keyword_begins_type_specifier (enum rid keyword)
11621 switch (keyword)
11623 case RID_AUTO_TYPE:
11624 case RID_INT:
11625 case RID_CHAR:
11626 case RID_FLOAT:
11627 case RID_DOUBLE:
11628 case RID_VOID:
11629 case RID_INT128:
11630 case RID_UNSIGNED:
11631 case RID_LONG:
11632 case RID_SHORT:
11633 case RID_SIGNED:
11634 case RID_DFLOAT32:
11635 case RID_DFLOAT64:
11636 case RID_DFLOAT128:
11637 case RID_FRACT:
11638 case RID_ACCUM:
11639 case RID_BOOL:
11640 case RID_WCHAR:
11641 case RID_CHAR16:
11642 case RID_CHAR32:
11643 case RID_SAT:
11644 case RID_COMPLEX:
11645 case RID_TYPEOF:
11646 case RID_STRUCT:
11647 case RID_CLASS:
11648 case RID_UNION:
11649 case RID_ENUM:
11650 return true;
11651 default:
11652 return false;
11656 /* Return true if KEYWORD names a type qualifier. */
11658 bool
11659 keyword_is_type_qualifier (enum rid keyword)
11661 switch (keyword)
11663 case RID_CONST:
11664 case RID_VOLATILE:
11665 case RID_RESTRICT:
11666 case RID_ATOMIC:
11667 return true;
11668 default:
11669 return false;
11673 /* Return true if KEYWORD names a storage class specifier.
11675 RID_TYPEDEF is not included in this list despite `typedef' being
11676 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11677 such for syntactic convenience only. */
11679 bool
11680 keyword_is_storage_class_specifier (enum rid keyword)
11682 switch (keyword)
11684 case RID_STATIC:
11685 case RID_EXTERN:
11686 case RID_REGISTER:
11687 case RID_AUTO:
11688 case RID_MUTABLE:
11689 case RID_THREAD:
11690 return true;
11691 default:
11692 return false;
11696 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11698 static bool
11699 keyword_is_function_specifier (enum rid keyword)
11701 switch (keyword)
11703 case RID_INLINE:
11704 case RID_NORETURN:
11705 case RID_VIRTUAL:
11706 case RID_EXPLICIT:
11707 return true;
11708 default:
11709 return false;
11713 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11714 declaration-specifier (C99 6.7). */
11716 bool
11717 keyword_is_decl_specifier (enum rid keyword)
11719 if (keyword_is_storage_class_specifier (keyword)
11720 || keyword_is_type_qualifier (keyword)
11721 || keyword_is_function_specifier (keyword))
11722 return true;
11724 switch (keyword)
11726 case RID_TYPEDEF:
11727 case RID_FRIEND:
11728 case RID_CONSTEXPR:
11729 return true;
11730 default:
11731 return false;
11735 /* Initialize language-specific-bits of tree_contains_struct. */
11737 void
11738 c_common_init_ts (void)
11740 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11741 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11742 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11745 /* Build a user-defined numeric literal out of an integer constant type VALUE
11746 with identifier SUFFIX. */
11748 tree
11749 build_userdef_literal (tree suffix_id, tree value,
11750 enum overflow_type overflow, tree num_string)
11752 tree literal = make_node (USERDEF_LITERAL);
11753 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11754 USERDEF_LITERAL_VALUE (literal) = value;
11755 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11756 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11757 return literal;
11760 /* For vector[index], convert the vector to a
11761 pointer of the underlying type. */
11762 void
11763 convert_vector_to_pointer_for_subscript (location_t loc,
11764 tree* vecp, tree index)
11766 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11768 tree type = TREE_TYPE (*vecp);
11769 tree type1;
11771 if (TREE_CODE (index) == INTEGER_CST)
11772 if (!tree_fits_uhwi_p (index)
11773 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
11774 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11776 c_common_mark_addressable_vec (*vecp);
11777 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11778 type = build_pointer_type (type);
11779 type1 = build_pointer_type (TREE_TYPE (*vecp));
11780 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11781 *vecp = convert (type, *vecp);
11785 /* Determine which of the operands, if any, is a scalar that needs to be
11786 converted to a vector, for the range of operations. */
11787 enum stv_conv
11788 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11789 bool complain)
11791 tree type0 = TREE_TYPE (op0);
11792 tree type1 = TREE_TYPE (op1);
11793 bool integer_only_op = false;
11794 enum stv_conv ret = stv_firstarg;
11796 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11797 || TREE_CODE (type1) == VECTOR_TYPE);
11798 switch (code)
11800 /* Most GENERIC binary expressions require homogeneous arguments.
11801 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11802 argument that is a vector and a second one that is a scalar, so
11803 we never return stv_secondarg for them. */
11804 case RSHIFT_EXPR:
11805 case LSHIFT_EXPR:
11806 if (TREE_CODE (type0) == INTEGER_TYPE
11807 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11809 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11811 if (complain)
11812 error_at (loc, "conversion of scalar %qT to vector %qT "
11813 "involves truncation", type0, type1);
11814 return stv_error;
11816 else
11817 return stv_firstarg;
11819 break;
11821 case BIT_IOR_EXPR:
11822 case BIT_XOR_EXPR:
11823 case BIT_AND_EXPR:
11824 integer_only_op = true;
11825 /* ... fall through ... */
11827 case VEC_COND_EXPR:
11829 case PLUS_EXPR:
11830 case MINUS_EXPR:
11831 case MULT_EXPR:
11832 case TRUNC_DIV_EXPR:
11833 case CEIL_DIV_EXPR:
11834 case FLOOR_DIV_EXPR:
11835 case ROUND_DIV_EXPR:
11836 case EXACT_DIV_EXPR:
11837 case TRUNC_MOD_EXPR:
11838 case FLOOR_MOD_EXPR:
11839 case RDIV_EXPR:
11840 case EQ_EXPR:
11841 case NE_EXPR:
11842 case LE_EXPR:
11843 case GE_EXPR:
11844 case LT_EXPR:
11845 case GT_EXPR:
11846 /* What about UNLT_EXPR? */
11847 if (TREE_CODE (type0) == VECTOR_TYPE)
11849 tree tmp;
11850 ret = stv_secondarg;
11851 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11852 tmp = type0; type0 = type1; type1 = tmp;
11853 tmp = op0; op0 = op1; op1 = tmp;
11856 if (TREE_CODE (type0) == INTEGER_TYPE
11857 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11859 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11861 if (complain)
11862 error_at (loc, "conversion of scalar %qT to vector %qT "
11863 "involves truncation", type0, type1);
11864 return stv_error;
11866 return ret;
11868 else if (!integer_only_op
11869 /* Allow integer --> real conversion if safe. */
11870 && (TREE_CODE (type0) == REAL_TYPE
11871 || TREE_CODE (type0) == INTEGER_TYPE)
11872 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11874 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11876 if (complain)
11877 error_at (loc, "conversion of scalar %qT to vector %qT "
11878 "involves truncation", type0, type1);
11879 return stv_error;
11881 return ret;
11883 default:
11884 break;
11887 return stv_nothing;
11890 /* Return true iff ALIGN is an integral constant that is a fundamental
11891 alignment, as defined by [basic.align] in the c++-11
11892 specifications.
11894 That is:
11896 [A fundamental alignment is represented by an alignment less than or
11897 equal to the greatest alignment supported by the implementation
11898 in all contexts, which is equal to
11899 alignof(max_align_t)]. */
11901 bool
11902 cxx_fundamental_alignment_p (unsigned align)
11904 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11905 TYPE_ALIGN (long_double_type_node)));
11908 /* Return true if T is a pointer to a zero-sized aggregate. */
11910 bool
11911 pointer_to_zero_sized_aggr_p (tree t)
11913 if (!POINTER_TYPE_P (t))
11914 return false;
11915 t = TREE_TYPE (t);
11916 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
11919 #include "gt-c-family-c-common.h"