PR c/61053
[official-gcc.git] / gcc / c-family / c-common.c
blobd7c85fcaf7f33742e83715171e3e0ae573fe8ea1
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"
52 #include "wide-int-print.h"
54 cpp_reader *parse_in; /* Declared in c-pragma.h. */
56 /* The following symbols are subsumed in the c_global_trees array, and
57 listed here individually for documentation purposes.
59 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
61 tree short_integer_type_node;
62 tree long_integer_type_node;
63 tree long_long_integer_type_node;
64 tree int128_integer_type_node;
66 tree short_unsigned_type_node;
67 tree long_unsigned_type_node;
68 tree long_long_unsigned_type_node;
69 tree int128_unsigned_type_node;
71 tree truthvalue_type_node;
72 tree truthvalue_false_node;
73 tree truthvalue_true_node;
75 tree ptrdiff_type_node;
77 tree unsigned_char_type_node;
78 tree signed_char_type_node;
79 tree wchar_type_node;
81 tree char16_type_node;
82 tree char32_type_node;
84 tree float_type_node;
85 tree double_type_node;
86 tree long_double_type_node;
88 tree complex_integer_type_node;
89 tree complex_float_type_node;
90 tree complex_double_type_node;
91 tree complex_long_double_type_node;
93 tree dfloat32_type_node;
94 tree dfloat64_type_node;
95 tree_dfloat128_type_node;
97 tree intQI_type_node;
98 tree intHI_type_node;
99 tree intSI_type_node;
100 tree intDI_type_node;
101 tree intTI_type_node;
103 tree unsigned_intQI_type_node;
104 tree unsigned_intHI_type_node;
105 tree unsigned_intSI_type_node;
106 tree unsigned_intDI_type_node;
107 tree unsigned_intTI_type_node;
109 tree widest_integer_literal_type_node;
110 tree widest_unsigned_literal_type_node;
112 Nodes for types `void *' and `const void *'.
114 tree ptr_type_node, const_ptr_type_node;
116 Nodes for types `char *' and `const char *'.
118 tree string_type_node, const_string_type_node;
120 Type `char[SOMENUMBER]'.
121 Used when an array of char is needed and the size is irrelevant.
123 tree char_array_type_node;
125 Type `wchar_t[SOMENUMBER]' or something like it.
126 Used when a wide string literal is created.
128 tree wchar_array_type_node;
130 Type `char16_t[SOMENUMBER]' or something like it.
131 Used when a UTF-16 string literal is created.
133 tree char16_array_type_node;
135 Type `char32_t[SOMENUMBER]' or something like it.
136 Used when a UTF-32 string literal is created.
138 tree char32_array_type_node;
140 Type `int ()' -- used for implicit declaration of functions.
142 tree default_function_type;
144 A VOID_TYPE node, packaged in a TREE_LIST.
146 tree void_list_node;
148 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
149 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
150 VAR_DECLS, but C++ does.)
152 tree function_name_decl_node;
153 tree pretty_function_name_decl_node;
154 tree c99_function_name_decl_node;
156 Stack of nested function name VAR_DECLs.
158 tree saved_function_name_decls;
162 tree c_global_trees[CTI_MAX];
164 /* Switches common to the C front ends. */
166 /* Nonzero means don't output line number information. */
168 char flag_no_line_commands;
170 /* Nonzero causes -E output not to be done, but directives such as
171 #define that have side effects are still obeyed. */
173 char flag_no_output;
175 /* Nonzero means dump macros in some fashion. */
177 char flag_dump_macros;
179 /* Nonzero means pass #include lines through to the output. */
181 char flag_dump_includes;
183 /* Nonzero means process PCH files while preprocessing. */
185 bool flag_pch_preprocess;
187 /* The file name to which we should write a precompiled header, or
188 NULL if no header will be written in this compile. */
190 const char *pch_file;
192 /* Nonzero if an ISO standard was selected. It rejects macros in the
193 user's namespace. */
194 int flag_iso;
196 /* C/ObjC language option variables. */
199 /* Nonzero means allow type mismatches in conditional expressions;
200 just make their values `void'. */
202 int flag_cond_mismatch;
204 /* Nonzero means enable C89 Amendment 1 features. */
206 int flag_isoc94;
208 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
210 int flag_isoc99;
212 /* Nonzero means use the ISO C11 dialect of C. */
214 int flag_isoc11;
216 /* Nonzero means that we have builtin functions, and main is an int. */
218 int flag_hosted = 1;
221 /* ObjC language option variables. */
224 /* Tells the compiler that this is a special run. Do not perform any
225 compiling, instead we are to test some platform dependent features
226 and output a C header file with appropriate definitions. */
228 int print_struct_values;
230 /* Tells the compiler what is the constant string class for ObjC. */
232 const char *constant_string_class_name;
235 /* C++ language option variables. */
238 /* Nonzero means generate separate instantiation control files and
239 juggle them at link time. */
241 int flag_use_repository;
243 /* The C++ dialect being used. C++98 is the default. */
245 enum cxx_dialect cxx_dialect = cxx98;
247 /* Maximum template instantiation depth. This limit exists to limit the
248 time it takes to notice excessively recursive template instantiations.
250 The default is lower than the 1024 recommended by the C++0x standard
251 because G++ runs out of stack before 1024 with highly recursive template
252 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
254 int max_tinst_depth = 900;
256 /* The elements of `ridpointers' are identifier nodes for the reserved
257 type names and storage classes. It is indexed by a RID_... value. */
258 tree *ridpointers;
260 tree (*make_fname_decl) (location_t, tree, int);
262 /* Nonzero means don't warn about problems that occur when the code is
263 executed. */
264 int c_inhibit_evaluation_warnings;
266 /* Whether we are building a boolean conversion inside
267 convert_for_assignment, or some other late binary operation. If
268 build_binary_op is called for C (from code shared by C and C++) in
269 this case, then the operands have already been folded and the
270 result will not be folded again, so C_MAYBE_CONST_EXPR should not
271 be generated. */
272 bool in_late_binary_op;
274 /* Whether lexing has been completed, so subsequent preprocessor
275 errors should use the compiler's input_location. */
276 bool done_lexing = false;
278 /* Information about how a function name is generated. */
279 struct fname_var_t
281 tree *const decl; /* pointer to the VAR_DECL. */
282 const unsigned rid; /* RID number for the identifier. */
283 const int pretty; /* How pretty is it? */
286 /* The three ways of getting then name of the current function. */
288 const struct fname_var_t fname_vars[] =
290 /* C99 compliant __func__, must be first. */
291 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
292 /* GCC __FUNCTION__ compliant. */
293 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
294 /* GCC __PRETTY_FUNCTION__ compliant. */
295 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
296 {NULL, 0, 0},
299 /* Global visibility options. */
300 struct visibility_flags visibility_options;
302 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
303 static tree check_case_value (tree);
304 static bool check_case_bounds (tree, tree, tree *, tree *);
306 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
312 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
313 int, bool *);
314 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
315 int, bool *);
316 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
317 bool *);
318 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_always_inline_attribute (tree *, tree, tree, int,
322 bool *);
323 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
330 bool *);
331 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_transparent_union_attribute (tree *, tree, tree,
333 int, bool *);
334 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
340 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
341 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
344 static tree handle_visibility_attribute (tree *, tree, tree, int,
345 bool *);
346 static tree handle_tls_model_attribute (tree *, tree, tree, int,
347 bool *);
348 static tree handle_no_instrument_function_attribute (tree *, tree,
349 tree, int, bool *);
350 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
353 bool *);
354 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_deprecated_attribute (tree *, tree, tree, int,
359 bool *);
360 static tree handle_vector_size_attribute (tree *, tree, tree, int,
361 bool *);
362 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
366 bool *);
367 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
374 static tree ignore_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
380 bool *);
381 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
382 bool *);
384 static void check_function_nonnull (tree, int, tree *);
385 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
386 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
387 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
388 static int resort_field_decl_cmp (const void *, const void *);
390 /* Reserved words. The third field is a mask: keywords are disabled
391 if they match the mask.
393 Masks for languages:
394 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
395 C --std=c99: D_CXXONLY | D_OBJC
396 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
397 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
398 C++ --std=c0x: D_CONLY | D_OBJC
399 ObjC++ is like C++ except that D_OBJC is not set
401 If -fno-asm is used, D_ASM is added to the mask. If
402 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
403 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
404 In C with -Wc++-compat, we warn if D_CXXWARN is set.
406 Note the complication of the D_CXX_OBJC keywords. These are
407 reserved words such as 'class'. In C++, 'class' is a reserved
408 word. In Objective-C++ it is too. In Objective-C, it is a
409 reserved word too, but only if it follows an '@' sign.
411 const struct c_common_resword c_common_reswords[] =
413 { "_Alignas", RID_ALIGNAS, D_CONLY },
414 { "_Alignof", RID_ALIGNOF, D_CONLY },
415 { "_Atomic", RID_ATOMIC, D_CONLY },
416 { "_Bool", RID_BOOL, D_CONLY },
417 { "_Complex", RID_COMPLEX, 0 },
418 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
419 { "_Cilk_sync", RID_CILK_SYNC, 0 },
420 { "_Imaginary", RID_IMAGINARY, D_CONLY },
421 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
422 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
423 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
424 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
425 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
426 { "_Sat", RID_SAT, D_CONLY | D_EXT },
427 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
428 { "_Noreturn", RID_NORETURN, D_CONLY },
429 { "_Generic", RID_GENERIC, D_CONLY },
430 { "_Thread_local", RID_THREAD, D_CONLY },
431 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
432 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
433 { "__alignof", RID_ALIGNOF, 0 },
434 { "__alignof__", RID_ALIGNOF, 0 },
435 { "__asm", RID_ASM, 0 },
436 { "__asm__", RID_ASM, 0 },
437 { "__attribute", RID_ATTRIBUTE, 0 },
438 { "__attribute__", RID_ATTRIBUTE, 0 },
439 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
440 { "__bases", RID_BASES, D_CXXONLY },
441 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
442 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
443 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
444 { "__builtin_offsetof", RID_OFFSETOF, 0 },
445 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
446 { "__builtin_va_arg", RID_VA_ARG, 0 },
447 { "__complex", RID_COMPLEX, 0 },
448 { "__complex__", RID_COMPLEX, 0 },
449 { "__const", RID_CONST, 0 },
450 { "__const__", RID_CONST, 0 },
451 { "__decltype", RID_DECLTYPE, D_CXXONLY },
452 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
453 { "__extension__", RID_EXTENSION, 0 },
454 { "__func__", RID_C99_FUNCTION_NAME, 0 },
455 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
456 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
457 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
458 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
459 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
460 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
461 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
462 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
463 { "__imag", RID_IMAGPART, 0 },
464 { "__imag__", RID_IMAGPART, 0 },
465 { "__inline", RID_INLINE, 0 },
466 { "__inline__", RID_INLINE, 0 },
467 { "__int128", RID_INT128, 0 },
468 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
469 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
470 { "__is_class", RID_IS_CLASS, D_CXXONLY },
471 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
472 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
473 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
474 { "__is_final", RID_IS_FINAL, D_CXXONLY },
475 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
476 { "__is_pod", RID_IS_POD, D_CXXONLY },
477 { "__is_polymorphic", RID_IS_POLYMORPHIC, 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 },
573 /* These Objective-C keywords are recognized only immediately after
574 an '@'. */
575 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
576 { "defs", RID_AT_DEFS, D_OBJC },
577 { "encode", RID_AT_ENCODE, D_OBJC },
578 { "end", RID_AT_END, D_OBJC },
579 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
580 { "interface", RID_AT_INTERFACE, D_OBJC },
581 { "protocol", RID_AT_PROTOCOL, D_OBJC },
582 { "selector", RID_AT_SELECTOR, D_OBJC },
583 { "finally", RID_AT_FINALLY, D_OBJC },
584 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
585 { "optional", RID_AT_OPTIONAL, D_OBJC },
586 { "required", RID_AT_REQUIRED, D_OBJC },
587 { "property", RID_AT_PROPERTY, D_OBJC },
588 { "package", RID_AT_PACKAGE, D_OBJC },
589 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
590 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
591 /* These are recognized only in protocol-qualifier context
592 (see above) */
593 { "bycopy", RID_BYCOPY, D_OBJC },
594 { "byref", RID_BYREF, D_OBJC },
595 { "in", RID_IN, D_OBJC },
596 { "inout", RID_INOUT, D_OBJC },
597 { "oneway", RID_ONEWAY, D_OBJC },
598 { "out", RID_OUT, D_OBJC },
599 /* These are recognized inside a property attribute list */
600 { "assign", RID_ASSIGN, D_OBJC },
601 { "copy", RID_COPY, D_OBJC },
602 { "getter", RID_GETTER, D_OBJC },
603 { "nonatomic", RID_NONATOMIC, D_OBJC },
604 { "readonly", RID_READONLY, D_OBJC },
605 { "readwrite", RID_READWRITE, D_OBJC },
606 { "retain", RID_RETAIN, D_OBJC },
607 { "setter", RID_SETTER, D_OBJC },
610 const unsigned int num_c_common_reswords =
611 sizeof c_common_reswords / sizeof (struct c_common_resword);
613 /* Table of machine-independent attributes common to all C-like languages. */
614 const struct attribute_spec c_common_attribute_table[] =
616 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
617 affects_type_identity } */
618 { "packed", 0, 0, false, false, false,
619 handle_packed_attribute , false},
620 { "nocommon", 0, 0, true, false, false,
621 handle_nocommon_attribute, false},
622 { "common", 0, 0, true, false, false,
623 handle_common_attribute, false },
624 /* FIXME: logically, noreturn attributes should be listed as
625 "false, true, true" and apply to function types. But implementing this
626 would require all the places in the compiler that use TREE_THIS_VOLATILE
627 on a decl to identify non-returning functions to be located and fixed
628 to check the function type instead. */
629 { "noreturn", 0, 0, true, false, false,
630 handle_noreturn_attribute, false },
631 { "volatile", 0, 0, true, false, false,
632 handle_noreturn_attribute, false },
633 { "noinline", 0, 0, true, false, false,
634 handle_noinline_attribute, false },
635 { "noclone", 0, 0, true, false, false,
636 handle_noclone_attribute, false },
637 { "leaf", 0, 0, true, false, false,
638 handle_leaf_attribute, false },
639 { "always_inline", 0, 0, true, false, false,
640 handle_always_inline_attribute, false },
641 { "gnu_inline", 0, 0, true, false, false,
642 handle_gnu_inline_attribute, false },
643 { "artificial", 0, 0, true, false, false,
644 handle_artificial_attribute, false },
645 { "flatten", 0, 0, true, false, false,
646 handle_flatten_attribute, false },
647 { "used", 0, 0, true, false, false,
648 handle_used_attribute, false },
649 { "unused", 0, 0, false, false, false,
650 handle_unused_attribute, false },
651 { "externally_visible", 0, 0, true, false, false,
652 handle_externally_visible_attribute, false },
653 /* The same comments as for noreturn attributes apply to const ones. */
654 { "const", 0, 0, true, false, false,
655 handle_const_attribute, false },
656 { "transparent_union", 0, 0, false, false, false,
657 handle_transparent_union_attribute, false },
658 { "constructor", 0, 1, true, false, false,
659 handle_constructor_attribute, false },
660 { "destructor", 0, 1, true, false, false,
661 handle_destructor_attribute, false },
662 { "mode", 1, 1, false, true, false,
663 handle_mode_attribute, false },
664 { "section", 1, 1, true, false, false,
665 handle_section_attribute, false },
666 { "aligned", 0, 1, false, false, false,
667 handle_aligned_attribute, false },
668 { "weak", 0, 0, true, false, false,
669 handle_weak_attribute, false },
670 { "ifunc", 1, 1, true, false, false,
671 handle_ifunc_attribute, false },
672 { "alias", 1, 1, true, false, false,
673 handle_alias_attribute, false },
674 { "weakref", 0, 1, true, false, false,
675 handle_weakref_attribute, false },
676 { "no_instrument_function", 0, 0, true, false, false,
677 handle_no_instrument_function_attribute,
678 false },
679 { "malloc", 0, 0, true, false, false,
680 handle_malloc_attribute, false },
681 { "returns_twice", 0, 0, true, false, false,
682 handle_returns_twice_attribute, false },
683 { "no_stack_limit", 0, 0, true, false, false,
684 handle_no_limit_stack_attribute, false },
685 { "pure", 0, 0, true, false, false,
686 handle_pure_attribute, false },
687 { "transaction_callable", 0, 0, false, true, false,
688 handle_tm_attribute, false },
689 { "transaction_unsafe", 0, 0, false, true, false,
690 handle_tm_attribute, false },
691 { "transaction_safe", 0, 0, false, true, false,
692 handle_tm_attribute, false },
693 { "transaction_may_cancel_outer", 0, 0, false, true, false,
694 handle_tm_attribute, false },
695 /* ??? These two attributes didn't make the transition from the
696 Intel language document to the multi-vendor language document. */
697 { "transaction_pure", 0, 0, false, true, false,
698 handle_tm_attribute, false },
699 { "transaction_wrap", 1, 1, true, false, false,
700 handle_tm_wrap_attribute, false },
701 /* For internal use (marking of builtins) only. The name contains space
702 to prevent its usage in source code. */
703 { "no vops", 0, 0, true, false, false,
704 handle_novops_attribute, false },
705 { "deprecated", 0, 1, false, false, false,
706 handle_deprecated_attribute, false },
707 { "vector_size", 1, 1, false, true, false,
708 handle_vector_size_attribute, false },
709 { "visibility", 1, 1, false, false, false,
710 handle_visibility_attribute, false },
711 { "tls_model", 1, 1, true, false, false,
712 handle_tls_model_attribute, false },
713 { "nonnull", 0, -1, false, true, true,
714 handle_nonnull_attribute, false },
715 { "nothrow", 0, 0, true, false, false,
716 handle_nothrow_attribute, false },
717 { "may_alias", 0, 0, false, true, false, NULL, false },
718 { "cleanup", 1, 1, true, false, false,
719 handle_cleanup_attribute, false },
720 { "warn_unused_result", 0, 0, false, true, true,
721 handle_warn_unused_result_attribute, false },
722 { "sentinel", 0, 1, false, true, true,
723 handle_sentinel_attribute, false },
724 /* For internal use (marking of builtins) only. The name contains space
725 to prevent its usage in source code. */
726 { "type generic", 0, 0, false, true, true,
727 handle_type_generic_attribute, false },
728 { "alloc_size", 1, 2, false, true, true,
729 handle_alloc_size_attribute, false },
730 { "cold", 0, 0, true, false, false,
731 handle_cold_attribute, false },
732 { "hot", 0, 0, true, false, false,
733 handle_hot_attribute, false },
734 { "no_address_safety_analysis",
735 0, 0, true, false, false,
736 handle_no_address_safety_analysis_attribute,
737 false },
738 { "no_sanitize_address", 0, 0, true, false, false,
739 handle_no_sanitize_address_attribute,
740 false },
741 { "no_sanitize_undefined", 0, 0, true, false, false,
742 handle_no_sanitize_undefined_attribute,
743 false },
744 { "warning", 1, 1, true, false, false,
745 handle_error_attribute, false },
746 { "error", 1, 1, true, false, false,
747 handle_error_attribute, false },
748 { "target", 1, -1, true, false, false,
749 handle_target_attribute, false },
750 { "optimize", 1, -1, true, false, false,
751 handle_optimize_attribute, false },
752 /* For internal use only. The leading '*' both prevents its usage in
753 source code and signals that it may be overridden by machine tables. */
754 { "*tm regparm", 0, 0, false, true, true,
755 ignore_attribute, false },
756 { "no_split_stack", 0, 0, true, false, false,
757 handle_no_split_stack_attribute, false },
758 /* For internal use (marking of builtins and runtime functions) only.
759 The name contains space to prevent its usage in source code. */
760 { "fn spec", 1, 1, false, true, true,
761 handle_fnspec_attribute, false },
762 { "warn_unused", 0, 0, false, false, false,
763 handle_warn_unused_attribute, false },
764 { "returns_nonnull", 0, 0, false, true, true,
765 handle_returns_nonnull_attribute, false },
766 { "omp declare simd", 0, -1, true, false, false,
767 handle_omp_declare_simd_attribute, false },
768 { "cilk simd function", 0, -1, true, false, false,
769 handle_omp_declare_simd_attribute, false },
770 { "omp declare target", 0, 0, true, false, false,
771 handle_omp_declare_target_attribute, false },
772 { "alloc_align", 1, 1, false, true, true,
773 handle_alloc_align_attribute, false },
774 { "assume_aligned", 1, 2, false, true, true,
775 handle_assume_aligned_attribute, false },
776 { NULL, 0, 0, false, false, false, NULL, false }
779 /* Give the specifications for the format attributes, used by C and all
780 descendants. */
782 const struct attribute_spec c_common_format_attribute_table[] =
784 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
785 affects_type_identity } */
786 { "format", 3, 3, false, true, true,
787 handle_format_attribute, false },
788 { "format_arg", 1, 1, false, true, true,
789 handle_format_arg_attribute, false },
790 { NULL, 0, 0, false, false, false, NULL, false }
793 /* Return identifier for address space AS. */
795 const char *
796 c_addr_space_name (addr_space_t as)
798 int rid = RID_FIRST_ADDR_SPACE + as;
799 gcc_assert (ridpointers [rid]);
800 return IDENTIFIER_POINTER (ridpointers [rid]);
803 /* Push current bindings for the function name VAR_DECLS. */
805 void
806 start_fname_decls (void)
808 unsigned ix;
809 tree saved = NULL_TREE;
811 for (ix = 0; fname_vars[ix].decl; ix++)
813 tree decl = *fname_vars[ix].decl;
815 if (decl)
817 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
818 saved);
819 *fname_vars[ix].decl = NULL_TREE;
822 if (saved || saved_function_name_decls)
823 /* Normally they'll have been NULL, so only push if we've got a
824 stack, or they are non-NULL. */
825 saved_function_name_decls = tree_cons (saved, NULL_TREE,
826 saved_function_name_decls);
829 /* Finish up the current bindings, adding them into the current function's
830 statement tree. This must be done _before_ finish_stmt_tree is called.
831 If there is no current function, we must be at file scope and no statements
832 are involved. Pop the previous bindings. */
834 void
835 finish_fname_decls (void)
837 unsigned ix;
838 tree stmts = NULL_TREE;
839 tree stack = saved_function_name_decls;
841 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
842 append_to_statement_list (TREE_VALUE (stack), &stmts);
844 if (stmts)
846 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
848 if (TREE_CODE (*bodyp) == BIND_EXPR)
849 bodyp = &BIND_EXPR_BODY (*bodyp);
851 append_to_statement_list_force (*bodyp, &stmts);
852 *bodyp = stmts;
855 for (ix = 0; fname_vars[ix].decl; ix++)
856 *fname_vars[ix].decl = NULL_TREE;
858 if (stack)
860 /* We had saved values, restore them. */
861 tree saved;
863 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
865 tree decl = TREE_PURPOSE (saved);
866 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
868 *fname_vars[ix].decl = decl;
870 stack = TREE_CHAIN (stack);
872 saved_function_name_decls = stack;
875 /* Return the text name of the current function, suitably prettified
876 by PRETTY_P. Return string must be freed by caller. */
878 const char *
879 fname_as_string (int pretty_p)
881 const char *name = "top level";
882 char *namep;
883 int vrb = 2, len;
884 cpp_string cstr = { 0, 0 }, strname;
886 if (!pretty_p)
888 name = "";
889 vrb = 0;
892 if (current_function_decl)
893 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
895 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
897 namep = XNEWVEC (char, len);
898 snprintf (namep, len, "\"%s\"", name);
899 strname.text = (unsigned char *) namep;
900 strname.len = len - 1;
902 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
904 XDELETEVEC (namep);
905 return (const char *) cstr.text;
908 return namep;
911 /* Return the VAR_DECL for a const char array naming the current
912 function. If the VAR_DECL has not yet been created, create it
913 now. RID indicates how it should be formatted and IDENTIFIER_NODE
914 ID is its name (unfortunately C and C++ hold the RID values of
915 keywords in different places, so we can't derive RID from ID in
916 this language independent code. LOC is the location of the
917 function. */
919 tree
920 fname_decl (location_t loc, unsigned int rid, tree id)
922 unsigned ix;
923 tree decl = NULL_TREE;
925 for (ix = 0; fname_vars[ix].decl; ix++)
926 if (fname_vars[ix].rid == rid)
927 break;
929 decl = *fname_vars[ix].decl;
930 if (!decl)
932 /* If a tree is built here, it would normally have the lineno of
933 the current statement. Later this tree will be moved to the
934 beginning of the function and this line number will be wrong.
935 To avoid this problem set the lineno to 0 here; that prevents
936 it from appearing in the RTL. */
937 tree stmts;
938 location_t saved_location = input_location;
939 input_location = UNKNOWN_LOCATION;
941 stmts = push_stmt_list ();
942 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
943 stmts = pop_stmt_list (stmts);
944 if (!IS_EMPTY_STMT (stmts))
945 saved_function_name_decls
946 = tree_cons (decl, stmts, saved_function_name_decls);
947 *fname_vars[ix].decl = decl;
948 input_location = saved_location;
950 if (!ix && !current_function_decl)
951 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
953 return decl;
956 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
958 tree
959 fix_string_type (tree value)
961 int length = TREE_STRING_LENGTH (value);
962 int nchars;
963 tree e_type, i_type, a_type;
965 /* Compute the number of elements, for the array type. */
966 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
968 nchars = length;
969 e_type = char_type_node;
971 else if (TREE_TYPE (value) == char16_array_type_node)
973 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
974 e_type = char16_type_node;
976 else if (TREE_TYPE (value) == char32_array_type_node)
978 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
979 e_type = char32_type_node;
981 else
983 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
984 e_type = wchar_type_node;
987 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
988 limit in C++98 Annex B is very large (65536) and is not normative,
989 so we do not diagnose it (warn_overlength_strings is forced off
990 in c_common_post_options). */
991 if (warn_overlength_strings)
993 const int nchars_max = flag_isoc99 ? 4095 : 509;
994 const int relevant_std = flag_isoc99 ? 99 : 90;
995 if (nchars - 1 > nchars_max)
996 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
997 separate the %d from the 'C'. 'ISO' should not be
998 translated, but it may be moved after 'C%d' in languages
999 where modifiers follow nouns. */
1000 pedwarn (input_location, OPT_Woverlength_strings,
1001 "string length %qd is greater than the length %qd "
1002 "ISO C%d compilers are required to support",
1003 nchars - 1, nchars_max, relevant_std);
1006 /* Create the array type for the string constant. The ISO C++
1007 standard says that a string literal has type `const char[N]' or
1008 `const wchar_t[N]'. We use the same logic when invoked as a C
1009 front-end with -Wwrite-strings.
1010 ??? We should change the type of an expression depending on the
1011 state of a warning flag. We should just be warning -- see how
1012 this is handled in the C++ front-end for the deprecated implicit
1013 conversion from string literals to `char*' or `wchar_t*'.
1015 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1016 array type being the unqualified version of that type.
1017 Therefore, if we are constructing an array of const char, we must
1018 construct the matching unqualified array type first. The C front
1019 end does not require this, but it does no harm, so we do it
1020 unconditionally. */
1021 i_type = build_index_type (size_int (nchars - 1));
1022 a_type = build_array_type (e_type, i_type);
1023 if (c_dialect_cxx() || warn_write_strings)
1024 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1026 TREE_TYPE (value) = a_type;
1027 TREE_CONSTANT (value) = 1;
1028 TREE_READONLY (value) = 1;
1029 TREE_STATIC (value) = 1;
1030 return value;
1033 /* If DISABLE is true, stop issuing warnings. This is used when
1034 parsing code that we know will not be executed. This function may
1035 be called multiple times, and works as a stack. */
1037 static void
1038 c_disable_warnings (bool disable)
1040 if (disable)
1042 ++c_inhibit_evaluation_warnings;
1043 fold_defer_overflow_warnings ();
1047 /* If ENABLE is true, reenable issuing warnings. */
1049 static void
1050 c_enable_warnings (bool enable)
1052 if (enable)
1054 --c_inhibit_evaluation_warnings;
1055 fold_undefer_and_ignore_overflow_warnings ();
1059 /* Fully fold EXPR, an expression that was not folded (beyond integer
1060 constant expressions and null pointer constants) when being built
1061 up. If IN_INIT, this is in a static initializer and certain
1062 changes are made to the folding done. Clear *MAYBE_CONST if
1063 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1064 expression because it contains an evaluated operator (in C99) or an
1065 operator outside of sizeof returning an integer constant (in C90)
1066 not permitted in constant expressions, or because it contains an
1067 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1068 set to true by callers before calling this function.) Return the
1069 folded expression. Function arguments have already been folded
1070 before calling this function, as have the contents of SAVE_EXPR,
1071 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1072 C_MAYBE_CONST_EXPR. */
1074 tree
1075 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1077 tree ret;
1078 tree eptype = NULL_TREE;
1079 bool dummy = true;
1080 bool maybe_const_itself = true;
1081 location_t loc = EXPR_LOCATION (expr);
1083 /* This function is not relevant to C++ because C++ folds while
1084 parsing, and may need changes to be correct for C++ when C++
1085 stops folding while parsing. */
1086 if (c_dialect_cxx ())
1087 gcc_unreachable ();
1089 if (!maybe_const)
1090 maybe_const = &dummy;
1091 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1093 eptype = TREE_TYPE (expr);
1094 expr = TREE_OPERAND (expr, 0);
1096 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1097 &maybe_const_itself);
1098 if (eptype)
1099 ret = fold_convert_loc (loc, eptype, ret);
1100 *maybe_const &= maybe_const_itself;
1101 return ret;
1104 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1105 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1106 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1107 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1108 both evaluated and unevaluated subexpressions while
1109 *MAYBE_CONST_ITSELF is carried from only evaluated
1110 subexpressions). */
1112 static tree
1113 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1114 bool *maybe_const_itself)
1116 tree ret = expr;
1117 enum tree_code code = TREE_CODE (expr);
1118 enum tree_code_class kind = TREE_CODE_CLASS (code);
1119 location_t loc = EXPR_LOCATION (expr);
1120 tree op0, op1, op2, op3;
1121 tree orig_op0, orig_op1, orig_op2;
1122 bool op0_const = true, op1_const = true, op2_const = true;
1123 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1124 bool nowarning = TREE_NO_WARNING (expr);
1125 bool unused_p;
1127 /* This function is not relevant to C++ because C++ folds while
1128 parsing, and may need changes to be correct for C++ when C++
1129 stops folding while parsing. */
1130 if (c_dialect_cxx ())
1131 gcc_unreachable ();
1133 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1134 anything else not counted as an expression cannot usefully be
1135 folded further at this point. */
1136 if (!IS_EXPR_CODE_CLASS (kind)
1137 || kind == tcc_statement
1138 || code == SAVE_EXPR)
1139 return expr;
1141 /* Operands of variable-length expressions (function calls) have
1142 already been folded, as have __builtin_* function calls, and such
1143 expressions cannot occur in constant expressions. */
1144 if (kind == tcc_vl_exp)
1146 *maybe_const_operands = false;
1147 ret = fold (expr);
1148 goto out;
1151 if (code == C_MAYBE_CONST_EXPR)
1153 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1154 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1155 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1156 *maybe_const_operands = false;
1157 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1158 *maybe_const_itself = false;
1159 if (pre && !in_init)
1160 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1161 else
1162 ret = inner;
1163 goto out;
1166 /* Assignment, increment, decrement, function call and comma
1167 operators, and statement expressions, cannot occur in constant
1168 expressions if evaluated / outside of sizeof. (Function calls
1169 were handled above, though VA_ARG_EXPR is treated like a function
1170 call here, and statement expressions are handled through
1171 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1172 switch (code)
1174 case MODIFY_EXPR:
1175 case PREDECREMENT_EXPR:
1176 case PREINCREMENT_EXPR:
1177 case POSTDECREMENT_EXPR:
1178 case POSTINCREMENT_EXPR:
1179 case COMPOUND_EXPR:
1180 *maybe_const_operands = false;
1181 break;
1183 case VA_ARG_EXPR:
1184 case TARGET_EXPR:
1185 case BIND_EXPR:
1186 case OBJ_TYPE_REF:
1187 *maybe_const_operands = false;
1188 ret = fold (expr);
1189 goto out;
1191 default:
1192 break;
1195 /* Fold individual tree codes as appropriate. */
1196 switch (code)
1198 case COMPOUND_LITERAL_EXPR:
1199 /* Any non-constancy will have been marked in a containing
1200 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1201 goto out;
1203 case COMPONENT_REF:
1204 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1205 op1 = TREE_OPERAND (expr, 1);
1206 op2 = TREE_OPERAND (expr, 2);
1207 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1208 maybe_const_itself);
1209 STRIP_TYPE_NOPS (op0);
1210 if (op0 != orig_op0)
1211 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1212 if (ret != expr)
1214 TREE_READONLY (ret) = TREE_READONLY (expr);
1215 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1217 goto out;
1219 case ARRAY_REF:
1220 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1221 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1222 op2 = TREE_OPERAND (expr, 2);
1223 op3 = TREE_OPERAND (expr, 3);
1224 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1225 maybe_const_itself);
1226 STRIP_TYPE_NOPS (op0);
1227 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1228 maybe_const_itself);
1229 STRIP_TYPE_NOPS (op1);
1230 op1 = decl_constant_value_for_optimization (op1);
1231 if (op0 != orig_op0 || op1 != orig_op1)
1232 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1233 if (ret != expr)
1235 TREE_READONLY (ret) = TREE_READONLY (expr);
1236 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1237 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1239 ret = fold (ret);
1240 goto out;
1242 case COMPOUND_EXPR:
1243 case MODIFY_EXPR:
1244 case PREDECREMENT_EXPR:
1245 case PREINCREMENT_EXPR:
1246 case POSTDECREMENT_EXPR:
1247 case POSTINCREMENT_EXPR:
1248 case PLUS_EXPR:
1249 case MINUS_EXPR:
1250 case MULT_EXPR:
1251 case POINTER_PLUS_EXPR:
1252 case TRUNC_DIV_EXPR:
1253 case CEIL_DIV_EXPR:
1254 case FLOOR_DIV_EXPR:
1255 case TRUNC_MOD_EXPR:
1256 case RDIV_EXPR:
1257 case EXACT_DIV_EXPR:
1258 case LSHIFT_EXPR:
1259 case RSHIFT_EXPR:
1260 case BIT_IOR_EXPR:
1261 case BIT_XOR_EXPR:
1262 case BIT_AND_EXPR:
1263 case LT_EXPR:
1264 case LE_EXPR:
1265 case GT_EXPR:
1266 case GE_EXPR:
1267 case EQ_EXPR:
1268 case NE_EXPR:
1269 case COMPLEX_EXPR:
1270 case TRUTH_AND_EXPR:
1271 case TRUTH_OR_EXPR:
1272 case TRUTH_XOR_EXPR:
1273 case UNORDERED_EXPR:
1274 case ORDERED_EXPR:
1275 case UNLT_EXPR:
1276 case UNLE_EXPR:
1277 case UNGT_EXPR:
1278 case UNGE_EXPR:
1279 case UNEQ_EXPR:
1280 /* Binary operations evaluating both arguments (increment and
1281 decrement are binary internally in GCC). */
1282 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1283 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1284 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1285 maybe_const_itself);
1286 STRIP_TYPE_NOPS (op0);
1287 if (code != MODIFY_EXPR
1288 && code != PREDECREMENT_EXPR
1289 && code != PREINCREMENT_EXPR
1290 && code != POSTDECREMENT_EXPR
1291 && code != POSTINCREMENT_EXPR)
1292 op0 = decl_constant_value_for_optimization (op0);
1293 /* The RHS of a MODIFY_EXPR was fully folded when building that
1294 expression for the sake of conversion warnings. */
1295 if (code != MODIFY_EXPR)
1296 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1297 maybe_const_itself);
1298 STRIP_TYPE_NOPS (op1);
1299 op1 = decl_constant_value_for_optimization (op1);
1300 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1301 ret = in_init
1302 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1303 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1304 else
1305 ret = fold (expr);
1306 if (TREE_OVERFLOW_P (ret)
1307 && !TREE_OVERFLOW_P (op0)
1308 && !TREE_OVERFLOW_P (op1))
1309 overflow_warning (EXPR_LOCATION (expr), ret);
1310 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1311 && TREE_CODE (orig_op1) != INTEGER_CST
1312 && TREE_CODE (op1) == INTEGER_CST
1313 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1314 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1315 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1316 && c_inhibit_evaluation_warnings == 0)
1318 if (tree_int_cst_sgn (op1) < 0)
1319 warning_at (loc, 0, (code == LSHIFT_EXPR
1320 ? G_("left shift count is negative")
1321 : G_("right shift count is negative")));
1322 else if (compare_tree_int (op1,
1323 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1324 >= 0)
1325 warning_at (loc, 0, (code == LSHIFT_EXPR
1326 ? G_("left shift count >= width of type")
1327 : G_("right shift count >= width of type")));
1329 goto out;
1331 case INDIRECT_REF:
1332 case FIX_TRUNC_EXPR:
1333 case FLOAT_EXPR:
1334 CASE_CONVERT:
1335 case ADDR_SPACE_CONVERT_EXPR:
1336 case VIEW_CONVERT_EXPR:
1337 case NON_LVALUE_EXPR:
1338 case NEGATE_EXPR:
1339 case BIT_NOT_EXPR:
1340 case TRUTH_NOT_EXPR:
1341 case ADDR_EXPR:
1342 case CONJ_EXPR:
1343 case REALPART_EXPR:
1344 case IMAGPART_EXPR:
1345 /* Unary operations. */
1346 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1347 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1348 maybe_const_itself);
1349 STRIP_TYPE_NOPS (op0);
1350 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1351 op0 = decl_constant_value_for_optimization (op0);
1352 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1353 not prepared to deal with them if they occur in initializers. */
1354 if (op0 != orig_op0
1355 && code == ADDR_EXPR
1356 && (op1 = get_base_address (op0)) != NULL_TREE
1357 && TREE_CODE (op1) == INDIRECT_REF
1358 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1359 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1360 else if (op0 != orig_op0 || in_init)
1361 ret = in_init
1362 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1363 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1364 else
1365 ret = fold (expr);
1366 if (code == INDIRECT_REF
1367 && ret != expr
1368 && TREE_CODE (ret) == INDIRECT_REF)
1370 TREE_READONLY (ret) = TREE_READONLY (expr);
1371 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1372 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1374 switch (code)
1376 case FIX_TRUNC_EXPR:
1377 case FLOAT_EXPR:
1378 CASE_CONVERT:
1379 /* Don't warn about explicit conversions. We will already
1380 have warned about suspect implicit conversions. */
1381 break;
1383 default:
1384 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1385 overflow_warning (EXPR_LOCATION (expr), ret);
1386 break;
1388 goto out;
1390 case TRUTH_ANDIF_EXPR:
1391 case TRUTH_ORIF_EXPR:
1392 /* Binary operations not necessarily evaluating both
1393 arguments. */
1394 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1395 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1396 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1397 STRIP_TYPE_NOPS (op0);
1399 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1400 ? truthvalue_false_node
1401 : truthvalue_true_node));
1402 c_disable_warnings (unused_p);
1403 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1404 STRIP_TYPE_NOPS (op1);
1405 c_enable_warnings (unused_p);
1407 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1408 ret = in_init
1409 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1410 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1411 else
1412 ret = fold (expr);
1413 *maybe_const_operands &= op0_const;
1414 *maybe_const_itself &= op0_const_self;
1415 if (!(flag_isoc99
1416 && op0_const
1417 && op0_const_self
1418 && (code == TRUTH_ANDIF_EXPR
1419 ? op0 == truthvalue_false_node
1420 : op0 == truthvalue_true_node)))
1421 *maybe_const_operands &= op1_const;
1422 if (!(op0_const
1423 && op0_const_self
1424 && (code == TRUTH_ANDIF_EXPR
1425 ? op0 == truthvalue_false_node
1426 : op0 == truthvalue_true_node)))
1427 *maybe_const_itself &= op1_const_self;
1428 goto out;
1430 case COND_EXPR:
1431 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1432 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1433 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1434 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1436 STRIP_TYPE_NOPS (op0);
1437 c_disable_warnings (op0 == truthvalue_false_node);
1438 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1439 STRIP_TYPE_NOPS (op1);
1440 c_enable_warnings (op0 == truthvalue_false_node);
1442 c_disable_warnings (op0 == truthvalue_true_node);
1443 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1444 STRIP_TYPE_NOPS (op2);
1445 c_enable_warnings (op0 == truthvalue_true_node);
1447 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1448 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1449 else
1450 ret = fold (expr);
1451 *maybe_const_operands &= op0_const;
1452 *maybe_const_itself &= op0_const_self;
1453 if (!(flag_isoc99
1454 && op0_const
1455 && op0_const_self
1456 && op0 == truthvalue_false_node))
1457 *maybe_const_operands &= op1_const;
1458 if (!(op0_const
1459 && op0_const_self
1460 && op0 == truthvalue_false_node))
1461 *maybe_const_itself &= op1_const_self;
1462 if (!(flag_isoc99
1463 && op0_const
1464 && op0_const_self
1465 && op0 == truthvalue_true_node))
1466 *maybe_const_operands &= op2_const;
1467 if (!(op0_const
1468 && op0_const_self
1469 && op0 == truthvalue_true_node))
1470 *maybe_const_itself &= op2_const_self;
1471 goto out;
1473 case EXCESS_PRECISION_EXPR:
1474 /* Each case where an operand with excess precision may be
1475 encountered must remove the EXCESS_PRECISION_EXPR around
1476 inner operands and possibly put one around the whole
1477 expression or possibly convert to the semantic type (which
1478 c_fully_fold does); we cannot tell at this stage which is
1479 appropriate in any particular case. */
1480 gcc_unreachable ();
1482 default:
1483 /* Various codes may appear through folding built-in functions
1484 and their arguments. */
1485 goto out;
1488 out:
1489 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1490 have been done by this point, so remove them again. */
1491 nowarning |= TREE_NO_WARNING (ret);
1492 STRIP_TYPE_NOPS (ret);
1493 if (nowarning && !TREE_NO_WARNING (ret))
1495 if (!CAN_HAVE_LOCATION_P (ret))
1496 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1497 TREE_NO_WARNING (ret) = 1;
1499 if (ret != expr)
1500 protected_set_expr_location (ret, loc);
1501 return ret;
1504 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1505 return EXP. Otherwise, return either EXP or its known constant
1506 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1507 Is the BLKmode test appropriate? */
1509 tree
1510 decl_constant_value_for_optimization (tree exp)
1512 tree ret;
1514 /* This function is only used by C, for c_fully_fold and other
1515 optimization, and may not be correct for C++. */
1516 if (c_dialect_cxx ())
1517 gcc_unreachable ();
1519 if (!optimize
1520 || TREE_CODE (exp) != VAR_DECL
1521 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1522 || DECL_MODE (exp) == BLKmode)
1523 return exp;
1525 ret = decl_constant_value (exp);
1526 /* Avoid unwanted tree sharing between the initializer and current
1527 function's body where the tree can be modified e.g. by the
1528 gimplifier. */
1529 if (ret != exp && TREE_STATIC (exp))
1530 ret = unshare_expr (ret);
1531 return ret;
1534 /* Print a warning if a constant expression had overflow in folding.
1535 Invoke this function on every expression that the language
1536 requires to be a constant expression.
1537 Note the ANSI C standard says it is erroneous for a
1538 constant expression to overflow. */
1540 void
1541 constant_expression_warning (tree value)
1543 if (warn_overflow && pedantic
1544 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1545 || TREE_CODE (value) == FIXED_CST
1546 || TREE_CODE (value) == VECTOR_CST
1547 || TREE_CODE (value) == COMPLEX_CST)
1548 && TREE_OVERFLOW (value))
1549 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1552 /* The same as above but print an unconditional error. */
1553 void
1554 constant_expression_error (tree value)
1556 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1557 || TREE_CODE (value) == FIXED_CST
1558 || TREE_CODE (value) == VECTOR_CST
1559 || TREE_CODE (value) == COMPLEX_CST)
1560 && TREE_OVERFLOW (value))
1561 error ("overflow in constant expression");
1564 /* Print a warning if an expression had overflow in folding and its
1565 operands hadn't.
1567 Invoke this function on every expression that
1568 (1) appears in the source code, and
1569 (2) is a constant expression that overflowed, and
1570 (3) is not already checked by convert_and_check;
1571 however, do not invoke this function on operands of explicit casts
1572 or when the expression is the result of an operator and any operand
1573 already overflowed. */
1575 void
1576 overflow_warning (location_t loc, tree value)
1578 if (c_inhibit_evaluation_warnings != 0)
1579 return;
1581 switch (TREE_CODE (value))
1583 case INTEGER_CST:
1584 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1585 break;
1587 case REAL_CST:
1588 warning_at (loc, OPT_Woverflow,
1589 "floating point overflow in expression");
1590 break;
1592 case FIXED_CST:
1593 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1594 break;
1596 case VECTOR_CST:
1597 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1598 break;
1600 case COMPLEX_CST:
1601 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1602 warning_at (loc, OPT_Woverflow,
1603 "complex integer overflow in expression");
1604 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1605 warning_at (loc, OPT_Woverflow,
1606 "complex floating point overflow in expression");
1607 break;
1609 default:
1610 break;
1614 /* Warn about uses of logical || / && operator in a context where it
1615 is likely that the bitwise equivalent was intended by the
1616 programmer. We have seen an expression in which CODE is a binary
1617 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1618 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1619 void
1620 warn_logical_operator (location_t location, enum tree_code code, tree type,
1621 enum tree_code code_left, tree op_left,
1622 enum tree_code ARG_UNUSED (code_right), tree op_right)
1624 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1625 int in0_p, in1_p, in_p;
1626 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1627 bool strict_overflow_p = false;
1629 if (code != TRUTH_ANDIF_EXPR
1630 && code != TRUTH_AND_EXPR
1631 && code != TRUTH_ORIF_EXPR
1632 && code != TRUTH_OR_EXPR)
1633 return;
1635 /* Warn if &&/|| are being used in a context where it is
1636 likely that the bitwise equivalent was intended by the
1637 programmer. That is, an expression such as op && MASK
1638 where op should not be any boolean expression, nor a
1639 constant, and mask seems to be a non-boolean integer constant. */
1640 if (!truth_value_p (code_left)
1641 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1642 && !CONSTANT_CLASS_P (op_left)
1643 && !TREE_NO_WARNING (op_left)
1644 && TREE_CODE (op_right) == INTEGER_CST
1645 && !integer_zerop (op_right)
1646 && !integer_onep (op_right))
1648 if (or_op)
1649 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1650 " applied to non-boolean constant");
1651 else
1652 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1653 " applied to non-boolean constant");
1654 TREE_NO_WARNING (op_left) = true;
1655 return;
1658 /* We do not warn for constants because they are typical of macro
1659 expansions that test for features. */
1660 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1661 return;
1663 /* This warning only makes sense with logical operands. */
1664 if (!(truth_value_p (TREE_CODE (op_left))
1665 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1666 || !(truth_value_p (TREE_CODE (op_right))
1667 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1668 return;
1671 /* We first test whether either side separately is trivially true
1672 (with OR) or trivially false (with AND). If so, do not warn.
1673 This is a common idiom for testing ranges of data types in
1674 portable code. */
1675 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1676 if (!lhs)
1677 return;
1678 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1679 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1681 /* If this is an OR operation, invert both sides; now, the result
1682 should be always false to get a warning. */
1683 if (or_op)
1684 in0_p = !in0_p;
1686 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1687 if (tem && integer_zerop (tem))
1688 return;
1690 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1691 if (!rhs)
1692 return;
1693 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1694 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1696 /* If this is an OR operation, invert both sides; now, the result
1697 should be always false to get a warning. */
1698 if (or_op)
1699 in1_p = !in1_p;
1701 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1702 if (tem && integer_zerop (tem))
1703 return;
1705 /* If both expressions have the same operand, if we can merge the
1706 ranges, and if the range test is always false, then warn. */
1707 if (operand_equal_p (lhs, rhs, 0)
1708 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1709 in1_p, low1, high1)
1710 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1711 type, lhs, in_p, low, high))
1712 && integer_zerop (tem))
1714 if (or_op)
1715 warning_at (location, OPT_Wlogical_op,
1716 "logical %<or%> "
1717 "of collectively exhaustive tests is always true");
1718 else
1719 warning_at (location, OPT_Wlogical_op,
1720 "logical %<and%> "
1721 "of mutually exclusive tests is always false");
1726 /* Warn if EXP contains any computations whose results are not used.
1727 Return true if a warning is printed; false otherwise. LOCUS is the
1728 (potential) location of the expression. */
1730 bool
1731 warn_if_unused_value (const_tree exp, location_t locus)
1733 restart:
1734 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1735 return false;
1737 /* Don't warn about void constructs. This includes casting to void,
1738 void function calls, and statement expressions with a final cast
1739 to void. */
1740 if (VOID_TYPE_P (TREE_TYPE (exp)))
1741 return false;
1743 if (EXPR_HAS_LOCATION (exp))
1744 locus = EXPR_LOCATION (exp);
1746 switch (TREE_CODE (exp))
1748 case PREINCREMENT_EXPR:
1749 case POSTINCREMENT_EXPR:
1750 case PREDECREMENT_EXPR:
1751 case POSTDECREMENT_EXPR:
1752 case MODIFY_EXPR:
1753 case INIT_EXPR:
1754 case TARGET_EXPR:
1755 case CALL_EXPR:
1756 case TRY_CATCH_EXPR:
1757 case WITH_CLEANUP_EXPR:
1758 case EXIT_EXPR:
1759 case VA_ARG_EXPR:
1760 return false;
1762 case BIND_EXPR:
1763 /* For a binding, warn if no side effect within it. */
1764 exp = BIND_EXPR_BODY (exp);
1765 goto restart;
1767 case SAVE_EXPR:
1768 case NON_LVALUE_EXPR:
1769 case NOP_EXPR:
1770 exp = TREE_OPERAND (exp, 0);
1771 goto restart;
1773 case TRUTH_ORIF_EXPR:
1774 case TRUTH_ANDIF_EXPR:
1775 /* In && or ||, warn if 2nd operand has no side effect. */
1776 exp = TREE_OPERAND (exp, 1);
1777 goto restart;
1779 case COMPOUND_EXPR:
1780 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1781 return true;
1782 /* Let people do `(foo (), 0)' without a warning. */
1783 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1784 return false;
1785 exp = TREE_OPERAND (exp, 1);
1786 goto restart;
1788 case COND_EXPR:
1789 /* If this is an expression with side effects, don't warn; this
1790 case commonly appears in macro expansions. */
1791 if (TREE_SIDE_EFFECTS (exp))
1792 return false;
1793 goto warn;
1795 case INDIRECT_REF:
1796 /* Don't warn about automatic dereferencing of references, since
1797 the user cannot control it. */
1798 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1800 exp = TREE_OPERAND (exp, 0);
1801 goto restart;
1803 /* Fall through. */
1805 default:
1806 /* Referencing a volatile value is a side effect, so don't warn. */
1807 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1808 && TREE_THIS_VOLATILE (exp))
1809 return false;
1811 /* If this is an expression which has no operands, there is no value
1812 to be unused. There are no such language-independent codes,
1813 but front ends may define such. */
1814 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1815 return false;
1817 warn:
1818 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1823 /* Print a warning about casts that might indicate violation
1824 of strict aliasing rules if -Wstrict-aliasing is used and
1825 strict aliasing mode is in effect. OTYPE is the original
1826 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1828 bool
1829 strict_aliasing_warning (tree otype, tree type, tree expr)
1831 /* Strip pointer conversion chains and get to the correct original type. */
1832 STRIP_NOPS (expr);
1833 otype = TREE_TYPE (expr);
1835 if (!(flag_strict_aliasing
1836 && POINTER_TYPE_P (type)
1837 && POINTER_TYPE_P (otype)
1838 && !VOID_TYPE_P (TREE_TYPE (type)))
1839 /* If the type we are casting to is a ref-all pointer
1840 dereferencing it is always valid. */
1841 || TYPE_REF_CAN_ALIAS_ALL (type))
1842 return false;
1844 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1845 && (DECL_P (TREE_OPERAND (expr, 0))
1846 || handled_component_p (TREE_OPERAND (expr, 0))))
1848 /* Casting the address of an object to non void pointer. Warn
1849 if the cast breaks type based aliasing. */
1850 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1852 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1853 "might break strict-aliasing rules");
1854 return true;
1856 else
1858 /* warn_strict_aliasing >= 3. This includes the default (3).
1859 Only warn if the cast is dereferenced immediately. */
1860 alias_set_type set1 =
1861 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1862 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1864 if (set1 != set2 && set2 != 0
1865 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1867 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1868 "pointer will break strict-aliasing rules");
1869 return true;
1871 else if (warn_strict_aliasing == 2
1872 && !alias_sets_must_conflict_p (set1, set2))
1874 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1875 "pointer might break strict-aliasing rules");
1876 return true;
1880 else
1881 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1883 /* At this level, warn for any conversions, even if an address is
1884 not taken in the same statement. This will likely produce many
1885 false positives, but could be useful to pinpoint problems that
1886 are not revealed at higher levels. */
1887 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1888 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1889 if (!COMPLETE_TYPE_P (type)
1890 || !alias_sets_must_conflict_p (set1, set2))
1892 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1893 "pointer might break strict-aliasing rules");
1894 return true;
1898 return false;
1901 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1902 sizeof as last operand of certain builtins. */
1904 void
1905 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1906 vec<tree, va_gc> *params, tree *sizeof_arg,
1907 bool (*comp_types) (tree, tree))
1909 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1910 bool strop = false, cmp = false;
1911 unsigned int idx = ~0;
1912 location_t loc;
1914 if (TREE_CODE (callee) != FUNCTION_DECL
1915 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1916 || vec_safe_length (params) <= 1)
1917 return;
1919 switch (DECL_FUNCTION_CODE (callee))
1921 case BUILT_IN_STRNCMP:
1922 case BUILT_IN_STRNCASECMP:
1923 cmp = true;
1924 /* FALLTHRU */
1925 case BUILT_IN_STRNCPY:
1926 case BUILT_IN_STRNCPY_CHK:
1927 case BUILT_IN_STRNCAT:
1928 case BUILT_IN_STRNCAT_CHK:
1929 case BUILT_IN_STPNCPY:
1930 case BUILT_IN_STPNCPY_CHK:
1931 strop = true;
1932 /* FALLTHRU */
1933 case BUILT_IN_MEMCPY:
1934 case BUILT_IN_MEMCPY_CHK:
1935 case BUILT_IN_MEMMOVE:
1936 case BUILT_IN_MEMMOVE_CHK:
1937 if (params->length () < 3)
1938 return;
1939 src = (*params)[1];
1940 dest = (*params)[0];
1941 idx = 2;
1942 break;
1943 case BUILT_IN_BCOPY:
1944 if (params->length () < 3)
1945 return;
1946 src = (*params)[0];
1947 dest = (*params)[1];
1948 idx = 2;
1949 break;
1950 case BUILT_IN_MEMCMP:
1951 case BUILT_IN_BCMP:
1952 if (params->length () < 3)
1953 return;
1954 src = (*params)[1];
1955 dest = (*params)[0];
1956 idx = 2;
1957 cmp = true;
1958 break;
1959 case BUILT_IN_MEMSET:
1960 case BUILT_IN_MEMSET_CHK:
1961 if (params->length () < 3)
1962 return;
1963 dest = (*params)[0];
1964 idx = 2;
1965 break;
1966 case BUILT_IN_BZERO:
1967 dest = (*params)[0];
1968 idx = 1;
1969 break;
1970 case BUILT_IN_STRNDUP:
1971 src = (*params)[0];
1972 strop = true;
1973 idx = 1;
1974 break;
1975 case BUILT_IN_MEMCHR:
1976 if (params->length () < 3)
1977 return;
1978 src = (*params)[0];
1979 idx = 2;
1980 break;
1981 case BUILT_IN_SNPRINTF:
1982 case BUILT_IN_SNPRINTF_CHK:
1983 case BUILT_IN_VSNPRINTF:
1984 case BUILT_IN_VSNPRINTF_CHK:
1985 dest = (*params)[0];
1986 idx = 1;
1987 strop = true;
1988 break;
1989 default:
1990 break;
1993 if (idx >= 3)
1994 return;
1996 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1997 return;
1999 type = TYPE_P (sizeof_arg[idx])
2000 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2001 if (!POINTER_TYPE_P (type))
2002 return;
2004 if (dest
2005 && (tem = tree_strip_nop_conversions (dest))
2006 && POINTER_TYPE_P (TREE_TYPE (tem))
2007 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2008 return;
2010 if (src
2011 && (tem = tree_strip_nop_conversions (src))
2012 && POINTER_TYPE_P (TREE_TYPE (tem))
2013 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2014 return;
2016 loc = sizeof_arg_loc[idx];
2018 if (dest && !cmp)
2020 if (!TYPE_P (sizeof_arg[idx])
2021 && operand_equal_p (dest, sizeof_arg[idx], 0)
2022 && comp_types (TREE_TYPE (dest), type))
2024 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2025 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2026 "argument to %<sizeof%> in %qD call is the same "
2027 "expression as the destination; did you mean to "
2028 "remove the addressof?", callee);
2029 else if ((TYPE_PRECISION (TREE_TYPE (type))
2030 == TYPE_PRECISION (char_type_node))
2031 || strop)
2032 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2033 "argument to %<sizeof%> in %qD call is the same "
2034 "expression as the destination; did you mean to "
2035 "provide an explicit length?", callee);
2036 else
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 "dereference it?", callee);
2041 return;
2044 if (POINTER_TYPE_P (TREE_TYPE (dest))
2045 && !strop
2046 && comp_types (TREE_TYPE (dest), type)
2047 && !VOID_TYPE_P (TREE_TYPE (type)))
2049 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2050 "argument to %<sizeof%> in %qD call is the same "
2051 "pointer type %qT as the destination; expected %qT "
2052 "or an explicit length", callee, TREE_TYPE (dest),
2053 TREE_TYPE (TREE_TYPE (dest)));
2054 return;
2058 if (src && !cmp)
2060 if (!TYPE_P (sizeof_arg[idx])
2061 && operand_equal_p (src, sizeof_arg[idx], 0)
2062 && comp_types (TREE_TYPE (src), type))
2064 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2065 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2066 "argument to %<sizeof%> in %qD call is the same "
2067 "expression as the source; did you mean to "
2068 "remove the addressof?", callee);
2069 else if ((TYPE_PRECISION (TREE_TYPE (type))
2070 == TYPE_PRECISION (char_type_node))
2071 || strop)
2072 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2073 "argument to %<sizeof%> in %qD call is the same "
2074 "expression as the source; did you mean to "
2075 "provide an explicit length?", callee);
2076 else
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 "dereference it?", callee);
2081 return;
2084 if (POINTER_TYPE_P (TREE_TYPE (src))
2085 && !strop
2086 && comp_types (TREE_TYPE (src), type)
2087 && !VOID_TYPE_P (TREE_TYPE (type)))
2089 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2090 "argument to %<sizeof%> in %qD call is the same "
2091 "pointer type %qT as the source; expected %qT "
2092 "or an explicit length", callee, TREE_TYPE (src),
2093 TREE_TYPE (TREE_TYPE (src)));
2094 return;
2098 if (dest)
2100 if (!TYPE_P (sizeof_arg[idx])
2101 && operand_equal_p (dest, sizeof_arg[idx], 0)
2102 && comp_types (TREE_TYPE (dest), type))
2104 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2105 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2106 "argument to %<sizeof%> in %qD call is the same "
2107 "expression as the first source; did you mean to "
2108 "remove the addressof?", callee);
2109 else if ((TYPE_PRECISION (TREE_TYPE (type))
2110 == TYPE_PRECISION (char_type_node))
2111 || strop)
2112 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2113 "argument to %<sizeof%> in %qD call is the same "
2114 "expression as the first source; did you mean to "
2115 "provide an explicit length?", callee);
2116 else
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 "dereference it?", callee);
2121 return;
2124 if (POINTER_TYPE_P (TREE_TYPE (dest))
2125 && !strop
2126 && comp_types (TREE_TYPE (dest), type)
2127 && !VOID_TYPE_P (TREE_TYPE (type)))
2129 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2130 "argument to %<sizeof%> in %qD call is the same "
2131 "pointer type %qT as the first source; expected %qT "
2132 "or an explicit length", callee, TREE_TYPE (dest),
2133 TREE_TYPE (TREE_TYPE (dest)));
2134 return;
2138 if (src)
2140 if (!TYPE_P (sizeof_arg[idx])
2141 && operand_equal_p (src, sizeof_arg[idx], 0)
2142 && comp_types (TREE_TYPE (src), type))
2144 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2145 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2146 "argument to %<sizeof%> in %qD call is the same "
2147 "expression as the second source; did you mean to "
2148 "remove the addressof?", callee);
2149 else if ((TYPE_PRECISION (TREE_TYPE (type))
2150 == TYPE_PRECISION (char_type_node))
2151 || strop)
2152 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2153 "argument to %<sizeof%> in %qD call is the same "
2154 "expression as the second source; did you mean to "
2155 "provide an explicit length?", callee);
2156 else
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 "dereference it?", callee);
2161 return;
2164 if (POINTER_TYPE_P (TREE_TYPE (src))
2165 && !strop
2166 && comp_types (TREE_TYPE (src), type)
2167 && !VOID_TYPE_P (TREE_TYPE (type)))
2169 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2170 "argument to %<sizeof%> in %qD call is the same "
2171 "pointer type %qT as the second source; expected %qT "
2172 "or an explicit length", callee, TREE_TYPE (src),
2173 TREE_TYPE (TREE_TYPE (src)));
2174 return;
2180 /* Warn for unlikely, improbable, or stupid DECL declarations
2181 of `main'. */
2183 void
2184 check_main_parameter_types (tree decl)
2186 function_args_iterator iter;
2187 tree type;
2188 int argct = 0;
2190 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2192 /* XXX void_type_node belies the abstraction. */
2193 if (type == void_type_node || type == error_mark_node )
2194 break;
2196 tree t = type;
2197 if (TYPE_ATOMIC (t))
2198 pedwarn (input_location, OPT_Wmain,
2199 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2200 type, decl);
2201 while (POINTER_TYPE_P (t))
2203 t = TREE_TYPE (t);
2204 if (TYPE_ATOMIC (t))
2205 pedwarn (input_location, OPT_Wmain,
2206 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2207 type, decl);
2210 ++argct;
2211 switch (argct)
2213 case 1:
2214 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2215 pedwarn (input_location, OPT_Wmain,
2216 "first argument of %q+D should be %<int%>", decl);
2217 break;
2219 case 2:
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 "second argument of %q+D should be %<char **%>", decl);
2226 break;
2228 case 3:
2229 if (TREE_CODE (type) != POINTER_TYPE
2230 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2231 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2232 != char_type_node))
2233 pedwarn (input_location, OPT_Wmain,
2234 "third argument of %q+D should probably be "
2235 "%<char **%>", decl);
2236 break;
2240 /* It is intentional that this message does not mention the third
2241 argument because it's only mentioned in an appendix of the
2242 standard. */
2243 if (argct > 0 && (argct < 2 || argct > 3))
2244 pedwarn (input_location, OPT_Wmain,
2245 "%q+D takes only zero or two arguments", decl);
2247 if (stdarg_p (TREE_TYPE (decl)))
2248 pedwarn (input_location, OPT_Wmain,
2249 "%q+D declared as variadic function", decl);
2252 /* vector_targets_convertible_p is used for vector pointer types. The
2253 callers perform various checks that the qualifiers are satisfactory,
2254 while OTOH vector_targets_convertible_p ignores the number of elements
2255 in the vectors. That's fine with vector pointers as we can consider,
2256 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2257 and that does not require and conversion of the pointer values.
2258 In contrast, vector_types_convertible_p and
2259 vector_types_compatible_elements_p are used for vector value types. */
2260 /* True if pointers to distinct types T1 and T2 can be converted to
2261 each other without an explicit cast. Only returns true for opaque
2262 vector types. */
2263 bool
2264 vector_targets_convertible_p (const_tree t1, const_tree t2)
2266 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2267 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2268 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2269 return true;
2271 return false;
2274 /* vector_types_convertible_p is used for vector value types.
2275 It could in principle call vector_targets_convertible_p as a subroutine,
2276 but then the check for vector type would be duplicated with its callers,
2277 and also the purpose of vector_targets_convertible_p would become
2278 muddled.
2279 Where vector_types_convertible_p returns true, a conversion might still be
2280 needed to make the types match.
2281 In contrast, vector_targets_convertible_p is used for vector pointer
2282 values, and vector_types_compatible_elements_p is used specifically
2283 in the context for binary operators, as a check if use is possible without
2284 conversion. */
2285 /* True if vector types T1 and T2 can be converted to each other
2286 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2287 can only be converted with -flax-vector-conversions yet that is not
2288 in effect, emit a note telling the user about that option if such
2289 a note has not previously been emitted. */
2290 bool
2291 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2293 static bool emitted_lax_note = false;
2294 bool convertible_lax;
2296 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2297 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2298 return true;
2300 convertible_lax =
2301 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2302 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2303 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2304 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2305 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2307 if (!convertible_lax || flag_lax_vector_conversions)
2308 return convertible_lax;
2310 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2311 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2312 return true;
2314 if (emit_lax_note && !emitted_lax_note)
2316 emitted_lax_note = true;
2317 inform (input_location, "use -flax-vector-conversions to permit "
2318 "conversions between vectors with differing "
2319 "element types or numbers of subparts");
2322 return false;
2325 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2326 and have vector types, V0 has the same type as V1, and the number of
2327 elements of V0, V1, MASK is the same.
2329 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2330 called with two arguments. In this case implementation passes the
2331 first argument twice in order to share the same tree code. This fact
2332 could enable the mask-values being twice the vector length. This is
2333 an implementation accident and this semantics is not guaranteed to
2334 the user. */
2335 tree
2336 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2337 bool complain)
2339 tree ret;
2340 bool wrap = true;
2341 bool maybe_const = false;
2342 bool two_arguments = false;
2344 if (v1 == NULL_TREE)
2346 two_arguments = true;
2347 v1 = v0;
2350 if (v0 == error_mark_node || v1 == error_mark_node
2351 || mask == error_mark_node)
2352 return error_mark_node;
2354 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2355 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2357 if (complain)
2358 error_at (loc, "__builtin_shuffle last argument must "
2359 "be an integer vector");
2360 return error_mark_node;
2363 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2364 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2366 if (complain)
2367 error_at (loc, "__builtin_shuffle arguments must be vectors");
2368 return error_mark_node;
2371 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2373 if (complain)
2374 error_at (loc, "__builtin_shuffle argument vectors must be of "
2375 "the same type");
2376 return error_mark_node;
2379 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2380 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2381 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2382 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2384 if (complain)
2385 error_at (loc, "__builtin_shuffle number of elements of the "
2386 "argument vector(s) and the mask vector should "
2387 "be the same");
2388 return error_mark_node;
2391 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2392 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2394 if (complain)
2395 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2396 "must have the same size as inner type of the mask");
2397 return error_mark_node;
2400 if (!c_dialect_cxx ())
2402 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2403 v0 = c_fully_fold (v0, false, &maybe_const);
2404 wrap &= maybe_const;
2406 if (two_arguments)
2407 v1 = v0 = save_expr (v0);
2408 else
2410 v1 = c_fully_fold (v1, false, &maybe_const);
2411 wrap &= maybe_const;
2414 mask = c_fully_fold (mask, false, &maybe_const);
2415 wrap &= maybe_const;
2417 else if (two_arguments)
2418 v1 = v0 = save_expr (v0);
2420 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2422 if (!c_dialect_cxx () && !wrap)
2423 ret = c_wrap_maybe_const (ret, true);
2425 return ret;
2428 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2429 to integral type. */
2431 static tree
2432 c_common_get_narrower (tree op, int *unsignedp_ptr)
2434 op = get_narrower (op, unsignedp_ptr);
2436 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2437 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2439 /* C++0x scoped enumerations don't implicitly convert to integral
2440 type; if we stripped an explicit conversion to a larger type we
2441 need to replace it so common_type will still work. */
2442 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2443 TYPE_UNSIGNED (TREE_TYPE (op)));
2444 op = fold_convert (type, op);
2446 return op;
2449 /* This is a helper function of build_binary_op.
2451 For certain operations if both args were extended from the same
2452 smaller type, do the arithmetic in that type and then extend.
2454 BITWISE indicates a bitwise operation.
2455 For them, this optimization is safe only if
2456 both args are zero-extended or both are sign-extended.
2457 Otherwise, we might change the result.
2458 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2459 but calculated in (unsigned short) it would be (unsigned short)-1.
2461 tree
2462 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2464 int unsigned0, unsigned1;
2465 tree arg0, arg1;
2466 int uns;
2467 tree type;
2469 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2470 excessive narrowing when we call get_narrower below. For
2471 example, suppose that OP0 is of unsigned int extended
2472 from signed char and that RESULT_TYPE is long long int.
2473 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2474 like
2476 (long long int) (unsigned int) signed_char
2478 which get_narrower would narrow down to
2480 (unsigned int) signed char
2482 If we do not cast OP0 first, get_narrower would return
2483 signed_char, which is inconsistent with the case of the
2484 explicit cast. */
2485 op0 = convert (result_type, op0);
2486 op1 = convert (result_type, op1);
2488 arg0 = c_common_get_narrower (op0, &unsigned0);
2489 arg1 = c_common_get_narrower (op1, &unsigned1);
2491 /* UNS is 1 if the operation to be done is an unsigned one. */
2492 uns = TYPE_UNSIGNED (result_type);
2494 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2495 but it *requires* conversion to FINAL_TYPE. */
2497 if ((TYPE_PRECISION (TREE_TYPE (op0))
2498 == TYPE_PRECISION (TREE_TYPE (arg0)))
2499 && TREE_TYPE (op0) != result_type)
2500 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2501 if ((TYPE_PRECISION (TREE_TYPE (op1))
2502 == TYPE_PRECISION (TREE_TYPE (arg1)))
2503 && TREE_TYPE (op1) != result_type)
2504 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2506 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2508 /* For bitwise operations, signedness of nominal type
2509 does not matter. Consider only how operands were extended. */
2510 if (bitwise)
2511 uns = unsigned0;
2513 /* Note that in all three cases below we refrain from optimizing
2514 an unsigned operation on sign-extended args.
2515 That would not be valid. */
2517 /* Both args variable: if both extended in same way
2518 from same width, do it in that width.
2519 Do it unsigned if args were zero-extended. */
2520 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2521 < TYPE_PRECISION (result_type))
2522 && (TYPE_PRECISION (TREE_TYPE (arg1))
2523 == TYPE_PRECISION (TREE_TYPE (arg0)))
2524 && unsigned0 == unsigned1
2525 && (unsigned0 || !uns))
2526 return c_common_signed_or_unsigned_type
2527 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2529 else if (TREE_CODE (arg0) == INTEGER_CST
2530 && (unsigned1 || !uns)
2531 && (TYPE_PRECISION (TREE_TYPE (arg1))
2532 < TYPE_PRECISION (result_type))
2533 && (type
2534 = c_common_signed_or_unsigned_type (unsigned1,
2535 TREE_TYPE (arg1)))
2536 && !POINTER_TYPE_P (type)
2537 && int_fits_type_p (arg0, type))
2538 return type;
2540 else if (TREE_CODE (arg1) == INTEGER_CST
2541 && (unsigned0 || !uns)
2542 && (TYPE_PRECISION (TREE_TYPE (arg0))
2543 < TYPE_PRECISION (result_type))
2544 && (type
2545 = c_common_signed_or_unsigned_type (unsigned0,
2546 TREE_TYPE (arg0)))
2547 && !POINTER_TYPE_P (type)
2548 && int_fits_type_p (arg1, type))
2549 return type;
2551 return result_type;
2554 /* Checks if expression EXPR of real/integer type cannot be converted
2555 to the real/integer type TYPE. Function returns non-zero when:
2556 * EXPR is a constant which cannot be exactly converted to TYPE.
2557 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2558 for EXPR type and TYPE being both integers or both real.
2559 * EXPR is not a constant of real type and TYPE is an integer.
2560 * EXPR is not a constant of integer type which cannot be
2561 exactly converted to real type.
2562 Function allows conversions between types of different signedness and
2563 can return SAFE_CONVERSION (zero) in that case. Function can produce
2564 signedness warnings if PRODUCE_WARNS is true. */
2566 enum conversion_safety
2567 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2569 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2570 tree expr_type = TREE_TYPE (expr);
2571 loc = expansion_point_location_if_in_system_header (loc);
2573 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2575 /* Warn for real constant that is not an exact integer converted
2576 to integer type. */
2577 if (TREE_CODE (expr_type) == REAL_TYPE
2578 && TREE_CODE (type) == INTEGER_TYPE)
2580 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2581 give_warning = UNSAFE_REAL;
2583 /* Warn for an integer constant that does not fit into integer type. */
2584 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2585 && TREE_CODE (type) == INTEGER_TYPE
2586 && !int_fits_type_p (expr, type))
2588 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2589 && tree_int_cst_sgn (expr) < 0)
2591 if (produce_warns)
2592 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2593 " implicitly converted to unsigned type");
2595 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2597 if (produce_warns)
2598 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2599 " constant value to negative integer");
2601 else
2602 give_warning = UNSAFE_OTHER;
2604 else if (TREE_CODE (type) == REAL_TYPE)
2606 /* Warn for an integer constant that does not fit into real type. */
2607 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2609 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2610 if (!exact_real_truncate (TYPE_MODE (type), &a))
2611 give_warning = UNSAFE_REAL;
2613 /* Warn for a real constant that does not fit into a smaller
2614 real type. */
2615 else if (TREE_CODE (expr_type) == REAL_TYPE
2616 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2618 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2619 if (!exact_real_truncate (TYPE_MODE (type), &a))
2620 give_warning = UNSAFE_REAL;
2624 else
2626 /* Warn for real types converted to integer types. */
2627 if (TREE_CODE (expr_type) == REAL_TYPE
2628 && TREE_CODE (type) == INTEGER_TYPE)
2629 give_warning = UNSAFE_REAL;
2631 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2632 && TREE_CODE (type) == INTEGER_TYPE)
2634 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2635 expr = get_unwidened (expr, 0);
2636 expr_type = TREE_TYPE (expr);
2638 /* Don't warn for short y; short x = ((int)y & 0xff); */
2639 if (TREE_CODE (expr) == BIT_AND_EXPR
2640 || TREE_CODE (expr) == BIT_IOR_EXPR
2641 || TREE_CODE (expr) == BIT_XOR_EXPR)
2643 /* If both args were extended from a shortest type,
2644 use that type if that is safe. */
2645 expr_type = shorten_binary_op (expr_type,
2646 TREE_OPERAND (expr, 0),
2647 TREE_OPERAND (expr, 1),
2648 /* bitwise */1);
2650 if (TREE_CODE (expr) == BIT_AND_EXPR)
2652 tree op0 = TREE_OPERAND (expr, 0);
2653 tree op1 = TREE_OPERAND (expr, 1);
2654 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2655 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2657 /* If one of the operands is a non-negative constant
2658 that fits in the target type, then the type of the
2659 other operand does not matter. */
2660 if ((TREE_CODE (op0) == INTEGER_CST
2661 && int_fits_type_p (op0, c_common_signed_type (type))
2662 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2663 || (TREE_CODE (op1) == INTEGER_CST
2664 && int_fits_type_p (op1, c_common_signed_type (type))
2665 && int_fits_type_p (op1,
2666 c_common_unsigned_type (type))))
2667 return SAFE_CONVERSION;
2668 /* If constant is unsigned and fits in the target
2669 type, then the result will also fit. */
2670 else if ((TREE_CODE (op0) == INTEGER_CST
2671 && unsigned0
2672 && int_fits_type_p (op0, type))
2673 || (TREE_CODE (op1) == INTEGER_CST
2674 && unsigned1
2675 && int_fits_type_p (op1, type)))
2676 return SAFE_CONVERSION;
2679 /* Warn for integer types converted to smaller integer types. */
2680 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2681 give_warning = UNSAFE_OTHER;
2683 /* When they are the same width but different signedness,
2684 then the value may change. */
2685 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2686 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2687 /* Even when converted to a bigger type, if the type is
2688 unsigned but expr is signed, then negative values
2689 will be changed. */
2690 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2691 && produce_warns)
2692 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2693 "may change the sign of the result",
2694 type, expr_type);
2697 /* Warn for integer types converted to real types if and only if
2698 all the range of values of the integer type cannot be
2699 represented by the real type. */
2700 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2701 && TREE_CODE (type) == REAL_TYPE)
2703 tree type_low_bound, type_high_bound;
2704 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2706 /* Don't warn about char y = 0xff; float x = (int) y; */
2707 expr = get_unwidened (expr, 0);
2708 expr_type = TREE_TYPE (expr);
2710 type_low_bound = TYPE_MIN_VALUE (expr_type);
2711 type_high_bound = TYPE_MAX_VALUE (expr_type);
2712 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2713 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2715 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2716 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2717 give_warning = UNSAFE_OTHER;
2720 /* Warn for real types converted to smaller real types. */
2721 else if (TREE_CODE (expr_type) == REAL_TYPE
2722 && TREE_CODE (type) == REAL_TYPE
2723 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2724 give_warning = UNSAFE_REAL;
2727 return give_warning;
2730 /* Warns if the conversion of EXPR to TYPE may alter a value.
2731 This is a helper function for warnings_for_convert_and_check. */
2733 static void
2734 conversion_warning (location_t loc, tree type, tree expr)
2736 tree expr_type = TREE_TYPE (expr);
2737 enum conversion_safety conversion_kind;
2739 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2740 return;
2742 /* This may happen, because for LHS op= RHS we preevaluate
2743 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2744 means we could no longer see the code of the EXPR. */
2745 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2746 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2747 if (TREE_CODE (expr) == SAVE_EXPR)
2748 expr = TREE_OPERAND (expr, 0);
2750 switch (TREE_CODE (expr))
2752 case EQ_EXPR:
2753 case NE_EXPR:
2754 case LE_EXPR:
2755 case GE_EXPR:
2756 case LT_EXPR:
2757 case GT_EXPR:
2758 case TRUTH_ANDIF_EXPR:
2759 case TRUTH_ORIF_EXPR:
2760 case TRUTH_AND_EXPR:
2761 case TRUTH_OR_EXPR:
2762 case TRUTH_XOR_EXPR:
2763 case TRUTH_NOT_EXPR:
2764 /* Conversion from boolean to a signed:1 bit-field (which only
2765 can hold the values 0 and -1) doesn't lose information - but
2766 it does change the value. */
2767 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2768 warning_at (loc, OPT_Wconversion,
2769 "conversion to %qT from boolean expression", type);
2770 return;
2772 case REAL_CST:
2773 case INTEGER_CST:
2774 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2775 if (conversion_kind == UNSAFE_REAL)
2776 warning_at (loc, OPT_Wfloat_conversion,
2777 "conversion to %qT alters %qT constant value",
2778 type, expr_type);
2779 else if (conversion_kind)
2780 warning_at (loc, OPT_Wconversion,
2781 "conversion to %qT alters %qT constant value",
2782 type, expr_type);
2783 return;
2785 case COND_EXPR:
2787 /* In case of COND_EXPR, we do not care about the type of
2788 COND_EXPR, only about the conversion of each operand. */
2789 tree op1 = TREE_OPERAND (expr, 1);
2790 tree op2 = TREE_OPERAND (expr, 2);
2792 conversion_warning (loc, type, op1);
2793 conversion_warning (loc, type, op2);
2794 return;
2797 default: /* 'expr' is not a constant. */
2798 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2799 if (conversion_kind == UNSAFE_REAL)
2800 warning_at (loc, OPT_Wfloat_conversion,
2801 "conversion to %qT from %qT may alter its value",
2802 type, expr_type);
2803 else if (conversion_kind)
2804 warning_at (loc, OPT_Wconversion,
2805 "conversion to %qT from %qT may alter its value",
2806 type, expr_type);
2810 /* Produce warnings after a conversion. RESULT is the result of
2811 converting EXPR to TYPE. This is a helper function for
2812 convert_and_check and cp_convert_and_check. */
2814 void
2815 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2816 tree result)
2818 loc = expansion_point_location_if_in_system_header (loc);
2820 if (TREE_CODE (expr) == INTEGER_CST
2821 && (TREE_CODE (type) == INTEGER_TYPE
2822 || TREE_CODE (type) == ENUMERAL_TYPE)
2823 && !int_fits_type_p (expr, type))
2825 /* Do not diagnose overflow in a constant expression merely
2826 because a conversion overflowed. */
2827 if (TREE_OVERFLOW (result))
2828 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2830 if (TYPE_UNSIGNED (type))
2832 /* This detects cases like converting -129 or 256 to
2833 unsigned char. */
2834 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2835 warning_at (loc, OPT_Woverflow,
2836 "large integer implicitly truncated to unsigned type");
2837 else
2838 conversion_warning (loc, type, expr);
2840 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2841 warning_at (loc, OPT_Woverflow,
2842 "overflow in implicit constant conversion");
2843 /* No warning for converting 0x80000000 to int. */
2844 else if (pedantic
2845 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2846 || TYPE_PRECISION (TREE_TYPE (expr))
2847 != TYPE_PRECISION (type)))
2848 warning_at (loc, OPT_Woverflow,
2849 "overflow in implicit constant conversion");
2851 else
2852 conversion_warning (loc, type, expr);
2854 else if ((TREE_CODE (result) == INTEGER_CST
2855 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2856 warning_at (loc, OPT_Woverflow,
2857 "overflow in implicit constant conversion");
2858 else
2859 conversion_warning (loc, type, expr);
2863 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2864 Invoke this function on every expression that is converted implicitly,
2865 i.e. because of language rules and not because of an explicit cast. */
2867 tree
2868 convert_and_check (location_t loc, tree type, tree expr)
2870 tree result;
2871 tree expr_for_warning;
2873 /* Convert from a value with possible excess precision rather than
2874 via the semantic type, but do not warn about values not fitting
2875 exactly in the semantic type. */
2876 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2878 tree orig_type = TREE_TYPE (expr);
2879 expr = TREE_OPERAND (expr, 0);
2880 expr_for_warning = convert (orig_type, expr);
2881 if (orig_type == type)
2882 return expr_for_warning;
2884 else
2885 expr_for_warning = expr;
2887 if (TREE_TYPE (expr) == type)
2888 return expr;
2890 result = convert (type, expr);
2892 if (c_inhibit_evaluation_warnings == 0
2893 && !TREE_OVERFLOW_P (expr)
2894 && result != error_mark_node)
2895 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2897 return result;
2900 /* A node in a list that describes references to variables (EXPR), which are
2901 either read accesses if WRITER is zero, or write accesses, in which case
2902 WRITER is the parent of EXPR. */
2903 struct tlist
2905 struct tlist *next;
2906 tree expr, writer;
2909 /* Used to implement a cache the results of a call to verify_tree. We only
2910 use this for SAVE_EXPRs. */
2911 struct tlist_cache
2913 struct tlist_cache *next;
2914 struct tlist *cache_before_sp;
2915 struct tlist *cache_after_sp;
2916 tree expr;
2919 /* Obstack to use when allocating tlist structures, and corresponding
2920 firstobj. */
2921 static struct obstack tlist_obstack;
2922 static char *tlist_firstobj = 0;
2924 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2925 warnings. */
2926 static struct tlist *warned_ids;
2927 /* SAVE_EXPRs need special treatment. We process them only once and then
2928 cache the results. */
2929 static struct tlist_cache *save_expr_cache;
2931 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2932 static void merge_tlist (struct tlist **, struct tlist *, int);
2933 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2934 static int warning_candidate_p (tree);
2935 static bool candidate_equal_p (const_tree, const_tree);
2936 static void warn_for_collisions (struct tlist *);
2937 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2938 static struct tlist *new_tlist (struct tlist *, tree, tree);
2940 /* Create a new struct tlist and fill in its fields. */
2941 static struct tlist *
2942 new_tlist (struct tlist *next, tree t, tree writer)
2944 struct tlist *l;
2945 l = XOBNEW (&tlist_obstack, struct tlist);
2946 l->next = next;
2947 l->expr = t;
2948 l->writer = writer;
2949 return l;
2952 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2953 is nonnull, we ignore any node we find which has a writer equal to it. */
2955 static void
2956 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2958 while (add)
2960 struct tlist *next = add->next;
2961 if (!copy)
2962 add->next = *to;
2963 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2964 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2965 add = next;
2969 /* Merge the nodes of ADD into TO. This merging process is done so that for
2970 each variable that already exists in TO, no new node is added; however if
2971 there is a write access recorded in ADD, and an occurrence on TO is only
2972 a read access, then the occurrence in TO will be modified to record the
2973 write. */
2975 static void
2976 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2978 struct tlist **end = to;
2980 while (*end)
2981 end = &(*end)->next;
2983 while (add)
2985 int found = 0;
2986 struct tlist *tmp2;
2987 struct tlist *next = add->next;
2989 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2990 if (candidate_equal_p (tmp2->expr, add->expr))
2992 found = 1;
2993 if (!tmp2->writer)
2994 tmp2->writer = add->writer;
2996 if (!found)
2998 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
2999 end = &(*end)->next;
3000 *end = 0;
3002 add = next;
3006 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3007 references in list LIST conflict with it, excluding reads if ONLY writers
3008 is nonzero. */
3010 static void
3011 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3012 int only_writes)
3014 struct tlist *tmp;
3016 /* Avoid duplicate warnings. */
3017 for (tmp = warned_ids; tmp; tmp = tmp->next)
3018 if (candidate_equal_p (tmp->expr, written))
3019 return;
3021 while (list)
3023 if (candidate_equal_p (list->expr, written)
3024 && !candidate_equal_p (list->writer, writer)
3025 && (!only_writes || list->writer))
3027 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3028 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3029 OPT_Wsequence_point, "operation on %qE may be undefined",
3030 list->expr);
3032 list = list->next;
3036 /* Given a list LIST of references to variables, find whether any of these
3037 can cause conflicts due to missing sequence points. */
3039 static void
3040 warn_for_collisions (struct tlist *list)
3042 struct tlist *tmp;
3044 for (tmp = list; tmp; tmp = tmp->next)
3046 if (tmp->writer)
3047 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3051 /* Return nonzero if X is a tree that can be verified by the sequence point
3052 warnings. */
3053 static int
3054 warning_candidate_p (tree x)
3056 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3057 return 0;
3059 if (TREE_CODE (x) == BLOCK)
3060 return 0;
3062 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3063 (lvalue_p) crash on TRY/CATCH. */
3064 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3065 return 0;
3067 if (!lvalue_p (x))
3068 return 0;
3070 /* No point to track non-const calls, they will never satisfy
3071 operand_equal_p. */
3072 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3073 return 0;
3075 if (TREE_CODE (x) == STRING_CST)
3076 return 0;
3078 return 1;
3081 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3082 static bool
3083 candidate_equal_p (const_tree x, const_tree y)
3085 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3088 /* Walk the tree X, and record accesses to variables. If X is written by the
3089 parent tree, WRITER is the parent.
3090 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3091 expression or its only operand forces a sequence point, then everything up
3092 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3093 in PNO_SP.
3094 Once we return, we will have emitted warnings if any subexpression before
3095 such a sequence point could be undefined. On a higher level, however, the
3096 sequence point may not be relevant, and we'll merge the two lists.
3098 Example: (b++, a) + b;
3099 The call that processes the COMPOUND_EXPR will store the increment of B
3100 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3101 processes the PLUS_EXPR will need to merge the two lists so that
3102 eventually, all accesses end up on the same list (and we'll warn about the
3103 unordered subexpressions b++ and b.
3105 A note on merging. If we modify the former example so that our expression
3106 becomes
3107 (b++, b) + a
3108 care must be taken not simply to add all three expressions into the final
3109 PNO_SP list. The function merge_tlist takes care of that by merging the
3110 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3111 way, so that no more than one access to B is recorded. */
3113 static void
3114 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3115 tree writer)
3117 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3118 enum tree_code code;
3119 enum tree_code_class cl;
3121 /* X may be NULL if it is the operand of an empty statement expression
3122 ({ }). */
3123 if (x == NULL)
3124 return;
3126 restart:
3127 code = TREE_CODE (x);
3128 cl = TREE_CODE_CLASS (code);
3130 if (warning_candidate_p (x))
3131 *pno_sp = new_tlist (*pno_sp, x, writer);
3133 switch (code)
3135 case CONSTRUCTOR:
3136 case SIZEOF_EXPR:
3137 return;
3139 case COMPOUND_EXPR:
3140 case TRUTH_ANDIF_EXPR:
3141 case TRUTH_ORIF_EXPR:
3142 tmp_before = tmp_nosp = tmp_list3 = 0;
3143 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3144 warn_for_collisions (tmp_nosp);
3145 merge_tlist (pbefore_sp, tmp_before, 0);
3146 merge_tlist (pbefore_sp, tmp_nosp, 0);
3147 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3148 merge_tlist (pbefore_sp, tmp_list3, 0);
3149 return;
3151 case COND_EXPR:
3152 tmp_before = tmp_list2 = 0;
3153 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3154 warn_for_collisions (tmp_list2);
3155 merge_tlist (pbefore_sp, tmp_before, 0);
3156 merge_tlist (pbefore_sp, tmp_list2, 0);
3158 tmp_list3 = tmp_nosp = 0;
3159 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3160 warn_for_collisions (tmp_nosp);
3161 merge_tlist (pbefore_sp, tmp_list3, 0);
3163 tmp_list3 = tmp_list2 = 0;
3164 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3165 warn_for_collisions (tmp_list2);
3166 merge_tlist (pbefore_sp, tmp_list3, 0);
3167 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3168 two first, to avoid warning for (a ? b++ : b++). */
3169 merge_tlist (&tmp_nosp, tmp_list2, 0);
3170 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3171 return;
3173 case PREDECREMENT_EXPR:
3174 case PREINCREMENT_EXPR:
3175 case POSTDECREMENT_EXPR:
3176 case POSTINCREMENT_EXPR:
3177 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3178 return;
3180 case MODIFY_EXPR:
3181 tmp_before = tmp_nosp = tmp_list3 = 0;
3182 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3183 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3184 /* Expressions inside the LHS are not ordered wrt. the sequence points
3185 in the RHS. Example:
3186 *a = (a++, 2)
3187 Despite the fact that the modification of "a" is in the before_sp
3188 list (tmp_before), it conflicts with the use of "a" in the LHS.
3189 We can handle this by adding the contents of tmp_list3
3190 to those of tmp_before, and redoing the collision warnings for that
3191 list. */
3192 add_tlist (&tmp_before, tmp_list3, x, 1);
3193 warn_for_collisions (tmp_before);
3194 /* Exclude the LHS itself here; we first have to merge it into the
3195 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3196 didn't exclude the LHS, we'd get it twice, once as a read and once
3197 as a write. */
3198 add_tlist (pno_sp, tmp_list3, x, 0);
3199 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3201 merge_tlist (pbefore_sp, tmp_before, 0);
3202 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3203 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3204 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3205 return;
3207 case CALL_EXPR:
3208 /* We need to warn about conflicts among arguments and conflicts between
3209 args and the function address. Side effects of the function address,
3210 however, are not ordered by the sequence point of the call. */
3212 call_expr_arg_iterator iter;
3213 tree arg;
3214 tmp_before = tmp_nosp = 0;
3215 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3216 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3218 tmp_list2 = tmp_list3 = 0;
3219 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3220 merge_tlist (&tmp_list3, tmp_list2, 0);
3221 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3223 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3224 warn_for_collisions (tmp_before);
3225 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3226 return;
3229 case TREE_LIST:
3230 /* Scan all the list, e.g. indices of multi dimensional array. */
3231 while (x)
3233 tmp_before = tmp_nosp = 0;
3234 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3235 merge_tlist (&tmp_nosp, tmp_before, 0);
3236 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3237 x = TREE_CHAIN (x);
3239 return;
3241 case SAVE_EXPR:
3243 struct tlist_cache *t;
3244 for (t = save_expr_cache; t; t = t->next)
3245 if (candidate_equal_p (t->expr, x))
3246 break;
3248 if (!t)
3250 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3251 t->next = save_expr_cache;
3252 t->expr = x;
3253 save_expr_cache = t;
3255 tmp_before = tmp_nosp = 0;
3256 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3257 warn_for_collisions (tmp_nosp);
3259 tmp_list3 = 0;
3260 merge_tlist (&tmp_list3, tmp_nosp, 0);
3261 t->cache_before_sp = tmp_before;
3262 t->cache_after_sp = tmp_list3;
3264 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3265 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3266 return;
3269 case ADDR_EXPR:
3270 x = TREE_OPERAND (x, 0);
3271 if (DECL_P (x))
3272 return;
3273 writer = 0;
3274 goto restart;
3276 default:
3277 /* For other expressions, simply recurse on their operands.
3278 Manual tail recursion for unary expressions.
3279 Other non-expressions need not be processed. */
3280 if (cl == tcc_unary)
3282 x = TREE_OPERAND (x, 0);
3283 writer = 0;
3284 goto restart;
3286 else if (IS_EXPR_CODE_CLASS (cl))
3288 int lp;
3289 int max = TREE_OPERAND_LENGTH (x);
3290 for (lp = 0; lp < max; lp++)
3292 tmp_before = tmp_nosp = 0;
3293 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3294 merge_tlist (&tmp_nosp, tmp_before, 0);
3295 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3298 return;
3302 /* Try to warn for undefined behavior in EXPR due to missing sequence
3303 points. */
3305 DEBUG_FUNCTION void
3306 verify_sequence_points (tree expr)
3308 struct tlist *before_sp = 0, *after_sp = 0;
3310 warned_ids = 0;
3311 save_expr_cache = 0;
3312 if (tlist_firstobj == 0)
3314 gcc_obstack_init (&tlist_obstack);
3315 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3318 verify_tree (expr, &before_sp, &after_sp, 0);
3319 warn_for_collisions (after_sp);
3320 obstack_free (&tlist_obstack, tlist_firstobj);
3323 /* Validate the expression after `case' and apply default promotions. */
3325 static tree
3326 check_case_value (tree value)
3328 if (value == NULL_TREE)
3329 return value;
3331 if (TREE_CODE (value) == INTEGER_CST)
3332 /* Promote char or short to int. */
3333 value = perform_integral_promotions (value);
3334 else if (value != error_mark_node)
3336 error ("case label does not reduce to an integer constant");
3337 value = error_mark_node;
3340 constant_expression_warning (value);
3342 return value;
3345 /* See if the case values LOW and HIGH are in the range of the original
3346 type (i.e. before the default conversion to int) of the switch testing
3347 expression.
3348 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3349 the type before promoting it. CASE_LOW_P is a pointer to the lower
3350 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3351 if the case is not a case range.
3352 The caller has to make sure that we are not called with NULL for
3353 CASE_LOW_P (i.e. the default case).
3354 Returns true if the case label is in range of ORIG_TYPE (saturated or
3355 untouched) or false if the label is out of range. */
3357 static bool
3358 check_case_bounds (tree type, tree orig_type,
3359 tree *case_low_p, tree *case_high_p)
3361 tree min_value, max_value;
3362 tree case_low = *case_low_p;
3363 tree case_high = case_high_p ? *case_high_p : case_low;
3365 /* If there was a problem with the original type, do nothing. */
3366 if (orig_type == error_mark_node)
3367 return true;
3369 min_value = TYPE_MIN_VALUE (orig_type);
3370 max_value = TYPE_MAX_VALUE (orig_type);
3372 /* Case label is less than minimum for type. */
3373 if (tree_int_cst_compare (case_low, min_value) < 0
3374 && tree_int_cst_compare (case_high, min_value) < 0)
3376 warning (0, "case label value is less than minimum value for type");
3377 return false;
3380 /* Case value is greater than maximum for type. */
3381 if (tree_int_cst_compare (case_low, max_value) > 0
3382 && tree_int_cst_compare (case_high, max_value) > 0)
3384 warning (0, "case label value exceeds maximum value for type");
3385 return false;
3388 /* Saturate lower case label value to minimum. */
3389 if (tree_int_cst_compare (case_high, min_value) >= 0
3390 && tree_int_cst_compare (case_low, min_value) < 0)
3392 warning (0, "lower value in case label range"
3393 " less than minimum value for type");
3394 case_low = min_value;
3397 /* Saturate upper case label value to maximum. */
3398 if (tree_int_cst_compare (case_low, max_value) <= 0
3399 && tree_int_cst_compare (case_high, max_value) > 0)
3401 warning (0, "upper value in case label range"
3402 " exceeds maximum value for type");
3403 case_high = max_value;
3406 if (*case_low_p != case_low)
3407 *case_low_p = convert (type, case_low);
3408 if (case_high_p && *case_high_p != case_high)
3409 *case_high_p = convert (type, case_high);
3411 return true;
3414 /* Return an integer type with BITS bits of precision,
3415 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3417 tree
3418 c_common_type_for_size (unsigned int bits, int unsignedp)
3420 if (bits == TYPE_PRECISION (integer_type_node))
3421 return unsignedp ? unsigned_type_node : integer_type_node;
3423 if (bits == TYPE_PRECISION (signed_char_type_node))
3424 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3426 if (bits == TYPE_PRECISION (short_integer_type_node))
3427 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3429 if (bits == TYPE_PRECISION (long_integer_type_node))
3430 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3432 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3433 return (unsignedp ? long_long_unsigned_type_node
3434 : long_long_integer_type_node);
3436 if (int128_integer_type_node
3437 && bits == TYPE_PRECISION (int128_integer_type_node))
3438 return (unsignedp ? int128_unsigned_type_node
3439 : int128_integer_type_node);
3441 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3442 return (unsignedp ? widest_unsigned_literal_type_node
3443 : widest_integer_literal_type_node);
3445 if (bits <= TYPE_PRECISION (intQI_type_node))
3446 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3448 if (bits <= TYPE_PRECISION (intHI_type_node))
3449 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3451 if (bits <= TYPE_PRECISION (intSI_type_node))
3452 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3454 if (bits <= TYPE_PRECISION (intDI_type_node))
3455 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3457 return 0;
3460 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3461 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3462 and saturating if SATP is nonzero, otherwise not saturating. */
3464 tree
3465 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3466 int unsignedp, int satp)
3468 enum machine_mode mode;
3469 if (ibit == 0)
3470 mode = unsignedp ? UQQmode : QQmode;
3471 else
3472 mode = unsignedp ? UHAmode : HAmode;
3474 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3475 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3476 break;
3478 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3480 sorry ("GCC cannot support operators with integer types and "
3481 "fixed-point types that have too many integral and "
3482 "fractional bits together");
3483 return 0;
3486 return c_common_type_for_mode (mode, satp);
3489 /* Used for communication between c_common_type_for_mode and
3490 c_register_builtin_type. */
3491 tree registered_builtin_types;
3493 /* Return a data type that has machine mode MODE.
3494 If the mode is an integer,
3495 then UNSIGNEDP selects between signed and unsigned types.
3496 If the mode is a fixed-point mode,
3497 then UNSIGNEDP selects between saturating and nonsaturating types. */
3499 tree
3500 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3502 tree t;
3504 if (mode == TYPE_MODE (integer_type_node))
3505 return unsignedp ? unsigned_type_node : integer_type_node;
3507 if (mode == TYPE_MODE (signed_char_type_node))
3508 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3510 if (mode == TYPE_MODE (short_integer_type_node))
3511 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3513 if (mode == TYPE_MODE (long_integer_type_node))
3514 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3516 if (mode == TYPE_MODE (long_long_integer_type_node))
3517 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3519 if (int128_integer_type_node
3520 && mode == TYPE_MODE (int128_integer_type_node))
3521 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3523 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3524 return unsignedp ? widest_unsigned_literal_type_node
3525 : widest_integer_literal_type_node;
3527 if (mode == QImode)
3528 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3530 if (mode == HImode)
3531 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3533 if (mode == SImode)
3534 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3536 if (mode == DImode)
3537 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3539 #if HOST_BITS_PER_WIDE_INT >= 64
3540 if (mode == TYPE_MODE (intTI_type_node))
3541 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3542 #endif
3544 if (mode == TYPE_MODE (float_type_node))
3545 return float_type_node;
3547 if (mode == TYPE_MODE (double_type_node))
3548 return double_type_node;
3550 if (mode == TYPE_MODE (long_double_type_node))
3551 return long_double_type_node;
3553 if (mode == TYPE_MODE (void_type_node))
3554 return void_type_node;
3556 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3557 return (unsignedp
3558 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3559 : make_signed_type (GET_MODE_PRECISION (mode)));
3561 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3562 return (unsignedp
3563 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3564 : make_signed_type (GET_MODE_PRECISION (mode)));
3566 if (COMPLEX_MODE_P (mode))
3568 enum machine_mode inner_mode;
3569 tree inner_type;
3571 if (mode == TYPE_MODE (complex_float_type_node))
3572 return complex_float_type_node;
3573 if (mode == TYPE_MODE (complex_double_type_node))
3574 return complex_double_type_node;
3575 if (mode == TYPE_MODE (complex_long_double_type_node))
3576 return complex_long_double_type_node;
3578 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3579 return complex_integer_type_node;
3581 inner_mode = GET_MODE_INNER (mode);
3582 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3583 if (inner_type != NULL_TREE)
3584 return build_complex_type (inner_type);
3586 else if (VECTOR_MODE_P (mode))
3588 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3589 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3590 if (inner_type != NULL_TREE)
3591 return build_vector_type_for_mode (inner_type, mode);
3594 if (mode == TYPE_MODE (dfloat32_type_node))
3595 return dfloat32_type_node;
3596 if (mode == TYPE_MODE (dfloat64_type_node))
3597 return dfloat64_type_node;
3598 if (mode == TYPE_MODE (dfloat128_type_node))
3599 return dfloat128_type_node;
3601 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3603 if (mode == TYPE_MODE (short_fract_type_node))
3604 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3605 if (mode == TYPE_MODE (fract_type_node))
3606 return unsignedp ? sat_fract_type_node : fract_type_node;
3607 if (mode == TYPE_MODE (long_fract_type_node))
3608 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3609 if (mode == TYPE_MODE (long_long_fract_type_node))
3610 return unsignedp ? sat_long_long_fract_type_node
3611 : long_long_fract_type_node;
3613 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3614 return unsignedp ? sat_unsigned_short_fract_type_node
3615 : unsigned_short_fract_type_node;
3616 if (mode == TYPE_MODE (unsigned_fract_type_node))
3617 return unsignedp ? sat_unsigned_fract_type_node
3618 : unsigned_fract_type_node;
3619 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3620 return unsignedp ? sat_unsigned_long_fract_type_node
3621 : unsigned_long_fract_type_node;
3622 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3623 return unsignedp ? sat_unsigned_long_long_fract_type_node
3624 : unsigned_long_long_fract_type_node;
3626 if (mode == TYPE_MODE (short_accum_type_node))
3627 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3628 if (mode == TYPE_MODE (accum_type_node))
3629 return unsignedp ? sat_accum_type_node : accum_type_node;
3630 if (mode == TYPE_MODE (long_accum_type_node))
3631 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3632 if (mode == TYPE_MODE (long_long_accum_type_node))
3633 return unsignedp ? sat_long_long_accum_type_node
3634 : long_long_accum_type_node;
3636 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3637 return unsignedp ? sat_unsigned_short_accum_type_node
3638 : unsigned_short_accum_type_node;
3639 if (mode == TYPE_MODE (unsigned_accum_type_node))
3640 return unsignedp ? sat_unsigned_accum_type_node
3641 : unsigned_accum_type_node;
3642 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3643 return unsignedp ? sat_unsigned_long_accum_type_node
3644 : unsigned_long_accum_type_node;
3645 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3646 return unsignedp ? sat_unsigned_long_long_accum_type_node
3647 : unsigned_long_long_accum_type_node;
3649 if (mode == QQmode)
3650 return unsignedp ? sat_qq_type_node : qq_type_node;
3651 if (mode == HQmode)
3652 return unsignedp ? sat_hq_type_node : hq_type_node;
3653 if (mode == SQmode)
3654 return unsignedp ? sat_sq_type_node : sq_type_node;
3655 if (mode == DQmode)
3656 return unsignedp ? sat_dq_type_node : dq_type_node;
3657 if (mode == TQmode)
3658 return unsignedp ? sat_tq_type_node : tq_type_node;
3660 if (mode == UQQmode)
3661 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3662 if (mode == UHQmode)
3663 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3664 if (mode == USQmode)
3665 return unsignedp ? sat_usq_type_node : usq_type_node;
3666 if (mode == UDQmode)
3667 return unsignedp ? sat_udq_type_node : udq_type_node;
3668 if (mode == UTQmode)
3669 return unsignedp ? sat_utq_type_node : utq_type_node;
3671 if (mode == HAmode)
3672 return unsignedp ? sat_ha_type_node : ha_type_node;
3673 if (mode == SAmode)
3674 return unsignedp ? sat_sa_type_node : sa_type_node;
3675 if (mode == DAmode)
3676 return unsignedp ? sat_da_type_node : da_type_node;
3677 if (mode == TAmode)
3678 return unsignedp ? sat_ta_type_node : ta_type_node;
3680 if (mode == UHAmode)
3681 return unsignedp ? sat_uha_type_node : uha_type_node;
3682 if (mode == USAmode)
3683 return unsignedp ? sat_usa_type_node : usa_type_node;
3684 if (mode == UDAmode)
3685 return unsignedp ? sat_uda_type_node : uda_type_node;
3686 if (mode == UTAmode)
3687 return unsignedp ? sat_uta_type_node : uta_type_node;
3690 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3691 if (TYPE_MODE (TREE_VALUE (t)) == mode
3692 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3693 return TREE_VALUE (t);
3695 return 0;
3698 tree
3699 c_common_unsigned_type (tree type)
3701 return c_common_signed_or_unsigned_type (1, type);
3704 /* Return a signed type the same as TYPE in other respects. */
3706 tree
3707 c_common_signed_type (tree type)
3709 return c_common_signed_or_unsigned_type (0, type);
3712 /* Return a type the same as TYPE except unsigned or
3713 signed according to UNSIGNEDP. */
3715 tree
3716 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3718 tree type1;
3720 /* This block of code emulates the behavior of the old
3721 c_common_unsigned_type. In particular, it returns
3722 long_unsigned_type_node if passed a long, even when a int would
3723 have the same size. This is necessary for warnings to work
3724 correctly in archs where sizeof(int) == sizeof(long) */
3726 type1 = TYPE_MAIN_VARIANT (type);
3727 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3728 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3729 if (type1 == integer_type_node || type1 == unsigned_type_node)
3730 return unsignedp ? unsigned_type_node : integer_type_node;
3731 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3732 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3733 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3734 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3735 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3736 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3737 if (int128_integer_type_node
3738 && (type1 == int128_integer_type_node
3739 || type1 == int128_unsigned_type_node))
3740 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3741 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3742 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3743 #if HOST_BITS_PER_WIDE_INT >= 64
3744 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3745 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3746 #endif
3747 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3748 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3749 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3750 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3751 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3752 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3753 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3754 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3756 #define C_COMMON_FIXED_TYPES(NAME) \
3757 if (type1 == short_ ## NAME ## _type_node \
3758 || type1 == unsigned_short_ ## NAME ## _type_node) \
3759 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3760 : short_ ## NAME ## _type_node; \
3761 if (type1 == NAME ## _type_node \
3762 || type1 == unsigned_ ## NAME ## _type_node) \
3763 return unsignedp ? unsigned_ ## NAME ## _type_node \
3764 : NAME ## _type_node; \
3765 if (type1 == long_ ## NAME ## _type_node \
3766 || type1 == unsigned_long_ ## NAME ## _type_node) \
3767 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3768 : long_ ## NAME ## _type_node; \
3769 if (type1 == long_long_ ## NAME ## _type_node \
3770 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3771 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3772 : long_long_ ## NAME ## _type_node;
3774 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3775 if (type1 == NAME ## _type_node \
3776 || type1 == u ## NAME ## _type_node) \
3777 return unsignedp ? u ## NAME ## _type_node \
3778 : NAME ## _type_node;
3780 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3781 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3782 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3783 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3784 : sat_ ## short_ ## NAME ## _type_node; \
3785 if (type1 == sat_ ## NAME ## _type_node \
3786 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3787 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3788 : sat_ ## NAME ## _type_node; \
3789 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3790 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3791 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3792 : sat_ ## long_ ## NAME ## _type_node; \
3793 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3794 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3795 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3796 : sat_ ## long_long_ ## NAME ## _type_node;
3798 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3799 if (type1 == sat_ ## NAME ## _type_node \
3800 || type1 == sat_ ## u ## NAME ## _type_node) \
3801 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3802 : sat_ ## NAME ## _type_node;
3804 C_COMMON_FIXED_TYPES (fract);
3805 C_COMMON_FIXED_TYPES_SAT (fract);
3806 C_COMMON_FIXED_TYPES (accum);
3807 C_COMMON_FIXED_TYPES_SAT (accum);
3809 C_COMMON_FIXED_MODE_TYPES (qq);
3810 C_COMMON_FIXED_MODE_TYPES (hq);
3811 C_COMMON_FIXED_MODE_TYPES (sq);
3812 C_COMMON_FIXED_MODE_TYPES (dq);
3813 C_COMMON_FIXED_MODE_TYPES (tq);
3814 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3815 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3816 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3817 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3818 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3819 C_COMMON_FIXED_MODE_TYPES (ha);
3820 C_COMMON_FIXED_MODE_TYPES (sa);
3821 C_COMMON_FIXED_MODE_TYPES (da);
3822 C_COMMON_FIXED_MODE_TYPES (ta);
3823 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3824 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3825 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3826 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3828 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3829 the precision; they have precision set to match their range, but
3830 may use a wider mode to match an ABI. If we change modes, we may
3831 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3832 the precision as well, so as to yield correct results for
3833 bit-field types. C++ does not have these separate bit-field
3834 types, and producing a signed or unsigned variant of an
3835 ENUMERAL_TYPE may cause other problems as well. */
3837 if (!INTEGRAL_TYPE_P (type)
3838 || TYPE_UNSIGNED (type) == unsignedp)
3839 return type;
3841 #define TYPE_OK(node) \
3842 (TYPE_MODE (type) == TYPE_MODE (node) \
3843 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3844 if (TYPE_OK (signed_char_type_node))
3845 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3846 if (TYPE_OK (integer_type_node))
3847 return unsignedp ? unsigned_type_node : integer_type_node;
3848 if (TYPE_OK (short_integer_type_node))
3849 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3850 if (TYPE_OK (long_integer_type_node))
3851 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3852 if (TYPE_OK (long_long_integer_type_node))
3853 return (unsignedp ? long_long_unsigned_type_node
3854 : long_long_integer_type_node);
3855 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3856 return (unsignedp ? int128_unsigned_type_node
3857 : int128_integer_type_node);
3858 if (TYPE_OK (widest_integer_literal_type_node))
3859 return (unsignedp ? widest_unsigned_literal_type_node
3860 : widest_integer_literal_type_node);
3862 #if HOST_BITS_PER_WIDE_INT >= 64
3863 if (TYPE_OK (intTI_type_node))
3864 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3865 #endif
3866 if (TYPE_OK (intDI_type_node))
3867 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3868 if (TYPE_OK (intSI_type_node))
3869 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3870 if (TYPE_OK (intHI_type_node))
3871 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3872 if (TYPE_OK (intQI_type_node))
3873 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3874 #undef TYPE_OK
3876 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3879 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3881 tree
3882 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3884 /* Extended integer types of the same width as a standard type have
3885 lesser rank, so those of the same width as int promote to int or
3886 unsigned int and are valid for printf formats expecting int or
3887 unsigned int. To avoid such special cases, avoid creating
3888 extended integer types for bit-fields if a standard integer type
3889 is available. */
3890 if (width == TYPE_PRECISION (integer_type_node))
3891 return unsignedp ? unsigned_type_node : integer_type_node;
3892 if (width == TYPE_PRECISION (signed_char_type_node))
3893 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3894 if (width == TYPE_PRECISION (short_integer_type_node))
3895 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3896 if (width == TYPE_PRECISION (long_integer_type_node))
3897 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3898 if (width == TYPE_PRECISION (long_long_integer_type_node))
3899 return (unsignedp ? long_long_unsigned_type_node
3900 : long_long_integer_type_node);
3901 if (int128_integer_type_node
3902 && width == TYPE_PRECISION (int128_integer_type_node))
3903 return (unsignedp ? int128_unsigned_type_node
3904 : int128_integer_type_node);
3905 return build_nonstandard_integer_type (width, unsignedp);
3908 /* The C version of the register_builtin_type langhook. */
3910 void
3911 c_register_builtin_type (tree type, const char* name)
3913 tree decl;
3915 decl = build_decl (UNKNOWN_LOCATION,
3916 TYPE_DECL, get_identifier (name), type);
3917 DECL_ARTIFICIAL (decl) = 1;
3918 if (!TYPE_NAME (type))
3919 TYPE_NAME (type) = decl;
3920 pushdecl (decl);
3922 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3925 /* Print an error message for invalid operands to arith operation
3926 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3927 LOCATION is the location of the message. */
3929 void
3930 binary_op_error (location_t location, enum tree_code code,
3931 tree type0, tree type1)
3933 const char *opname;
3935 switch (code)
3937 case PLUS_EXPR:
3938 opname = "+"; break;
3939 case MINUS_EXPR:
3940 opname = "-"; break;
3941 case MULT_EXPR:
3942 opname = "*"; break;
3943 case MAX_EXPR:
3944 opname = "max"; break;
3945 case MIN_EXPR:
3946 opname = "min"; break;
3947 case EQ_EXPR:
3948 opname = "=="; break;
3949 case NE_EXPR:
3950 opname = "!="; break;
3951 case LE_EXPR:
3952 opname = "<="; break;
3953 case GE_EXPR:
3954 opname = ">="; break;
3955 case LT_EXPR:
3956 opname = "<"; break;
3957 case GT_EXPR:
3958 opname = ">"; break;
3959 case LSHIFT_EXPR:
3960 opname = "<<"; break;
3961 case RSHIFT_EXPR:
3962 opname = ">>"; break;
3963 case TRUNC_MOD_EXPR:
3964 case FLOOR_MOD_EXPR:
3965 opname = "%"; break;
3966 case TRUNC_DIV_EXPR:
3967 case FLOOR_DIV_EXPR:
3968 opname = "/"; break;
3969 case BIT_AND_EXPR:
3970 opname = "&"; break;
3971 case BIT_IOR_EXPR:
3972 opname = "|"; break;
3973 case TRUTH_ANDIF_EXPR:
3974 opname = "&&"; break;
3975 case TRUTH_ORIF_EXPR:
3976 opname = "||"; break;
3977 case BIT_XOR_EXPR:
3978 opname = "^"; break;
3979 default:
3980 gcc_unreachable ();
3982 error_at (location,
3983 "invalid operands to binary %s (have %qT and %qT)", opname,
3984 type0, type1);
3987 /* Given an expression as a tree, return its original type. Do this
3988 by stripping any conversion that preserves the sign and precision. */
3989 static tree
3990 expr_original_type (tree expr)
3992 STRIP_SIGN_NOPS (expr);
3993 return TREE_TYPE (expr);
3996 /* Subroutine of build_binary_op, used for comparison operations.
3997 See if the operands have both been converted from subword integer types
3998 and, if so, perhaps change them both back to their original type.
3999 This function is also responsible for converting the two operands
4000 to the proper common type for comparison.
4002 The arguments of this function are all pointers to local variables
4003 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4004 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4006 LOC is the location of the comparison.
4008 If this function returns nonzero, it means that the comparison has
4009 a constant value. What this function returns is an expression for
4010 that value. */
4012 tree
4013 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4014 tree *restype_ptr, enum tree_code *rescode_ptr)
4016 tree type;
4017 tree op0 = *op0_ptr;
4018 tree op1 = *op1_ptr;
4019 int unsignedp0, unsignedp1;
4020 int real1, real2;
4021 tree primop0, primop1;
4022 enum tree_code code = *rescode_ptr;
4024 /* Throw away any conversions to wider types
4025 already present in the operands. */
4027 primop0 = c_common_get_narrower (op0, &unsignedp0);
4028 primop1 = c_common_get_narrower (op1, &unsignedp1);
4030 /* If primopN is first sign-extended from primopN's precision to opN's
4031 precision, then zero-extended from opN's precision to
4032 *restype_ptr precision, shortenings might be invalid. */
4033 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4034 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4035 && !unsignedp0
4036 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4037 primop0 = op0;
4038 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4039 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4040 && !unsignedp1
4041 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4042 primop1 = op1;
4044 /* Handle the case that OP0 does not *contain* a conversion
4045 but it *requires* conversion to FINAL_TYPE. */
4047 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4048 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4049 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4050 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4052 /* If one of the operands must be floated, we cannot optimize. */
4053 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4054 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4056 /* If first arg is constant, swap the args (changing operation
4057 so value is preserved), for canonicalization. Don't do this if
4058 the second arg is 0. */
4060 if (TREE_CONSTANT (primop0)
4061 && !integer_zerop (primop1) && !real_zerop (primop1)
4062 && !fixed_zerop (primop1))
4064 tree tem = primop0;
4065 int temi = unsignedp0;
4066 primop0 = primop1;
4067 primop1 = tem;
4068 tem = op0;
4069 op0 = op1;
4070 op1 = tem;
4071 *op0_ptr = op0;
4072 *op1_ptr = op1;
4073 unsignedp0 = unsignedp1;
4074 unsignedp1 = temi;
4075 temi = real1;
4076 real1 = real2;
4077 real2 = temi;
4079 switch (code)
4081 case LT_EXPR:
4082 code = GT_EXPR;
4083 break;
4084 case GT_EXPR:
4085 code = LT_EXPR;
4086 break;
4087 case LE_EXPR:
4088 code = GE_EXPR;
4089 break;
4090 case GE_EXPR:
4091 code = LE_EXPR;
4092 break;
4093 default:
4094 break;
4096 *rescode_ptr = code;
4099 /* If comparing an integer against a constant more bits wide,
4100 maybe we can deduce a value of 1 or 0 independent of the data.
4101 Or else truncate the constant now
4102 rather than extend the variable at run time.
4104 This is only interesting if the constant is the wider arg.
4105 Also, it is not safe if the constant is unsigned and the
4106 variable arg is signed, since in this case the variable
4107 would be sign-extended and then regarded as unsigned.
4108 Our technique fails in this case because the lowest/highest
4109 possible unsigned results don't follow naturally from the
4110 lowest/highest possible values of the variable operand.
4111 For just EQ_EXPR and NE_EXPR there is another technique that
4112 could be used: see if the constant can be faithfully represented
4113 in the other operand's type, by truncating it and reextending it
4114 and see if that preserves the constant's value. */
4116 if (!real1 && !real2
4117 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4118 && TREE_CODE (primop1) == INTEGER_CST
4119 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4121 int min_gt, max_gt, min_lt, max_lt;
4122 tree maxval, minval;
4123 /* 1 if comparison is nominally unsigned. */
4124 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4125 tree val;
4127 type = c_common_signed_or_unsigned_type (unsignedp0,
4128 TREE_TYPE (primop0));
4130 maxval = TYPE_MAX_VALUE (type);
4131 minval = TYPE_MIN_VALUE (type);
4133 if (unsignedp && !unsignedp0)
4134 *restype_ptr = c_common_signed_type (*restype_ptr);
4136 if (TREE_TYPE (primop1) != *restype_ptr)
4138 /* Convert primop1 to target type, but do not introduce
4139 additional overflow. We know primop1 is an int_cst. */
4140 primop1 = force_fit_type (*restype_ptr,
4141 wide_int::from
4142 (primop1,
4143 TYPE_PRECISION (*restype_ptr),
4144 TYPE_SIGN (TREE_TYPE (primop1))),
4145 0, TREE_OVERFLOW (primop1));
4147 if (type != *restype_ptr)
4149 minval = convert (*restype_ptr, minval);
4150 maxval = convert (*restype_ptr, maxval);
4153 min_gt = tree_int_cst_lt (primop1, minval);
4154 max_gt = tree_int_cst_lt (primop1, maxval);
4155 min_lt = tree_int_cst_lt (minval, primop1);
4156 max_lt = tree_int_cst_lt (maxval, primop1);
4158 val = 0;
4159 /* This used to be a switch, but Genix compiler can't handle that. */
4160 if (code == NE_EXPR)
4162 if (max_lt || min_gt)
4163 val = truthvalue_true_node;
4165 else if (code == EQ_EXPR)
4167 if (max_lt || min_gt)
4168 val = truthvalue_false_node;
4170 else if (code == LT_EXPR)
4172 if (max_lt)
4173 val = truthvalue_true_node;
4174 if (!min_lt)
4175 val = truthvalue_false_node;
4177 else if (code == GT_EXPR)
4179 if (min_gt)
4180 val = truthvalue_true_node;
4181 if (!max_gt)
4182 val = truthvalue_false_node;
4184 else if (code == LE_EXPR)
4186 if (!max_gt)
4187 val = truthvalue_true_node;
4188 if (min_gt)
4189 val = truthvalue_false_node;
4191 else if (code == GE_EXPR)
4193 if (!min_lt)
4194 val = truthvalue_true_node;
4195 if (max_lt)
4196 val = truthvalue_false_node;
4199 /* If primop0 was sign-extended and unsigned comparison specd,
4200 we did a signed comparison above using the signed type bounds.
4201 But the comparison we output must be unsigned.
4203 Also, for inequalities, VAL is no good; but if the signed
4204 comparison had *any* fixed result, it follows that the
4205 unsigned comparison just tests the sign in reverse
4206 (positive values are LE, negative ones GE).
4207 So we can generate an unsigned comparison
4208 against an extreme value of the signed type. */
4210 if (unsignedp && !unsignedp0)
4212 if (val != 0)
4213 switch (code)
4215 case LT_EXPR:
4216 case GE_EXPR:
4217 primop1 = TYPE_MIN_VALUE (type);
4218 val = 0;
4219 break;
4221 case LE_EXPR:
4222 case GT_EXPR:
4223 primop1 = TYPE_MAX_VALUE (type);
4224 val = 0;
4225 break;
4227 default:
4228 break;
4230 type = c_common_unsigned_type (type);
4233 if (TREE_CODE (primop0) != INTEGER_CST)
4235 if (val == truthvalue_false_node)
4236 warning_at (loc, OPT_Wtype_limits,
4237 "comparison is always false due to limited range of data type");
4238 if (val == truthvalue_true_node)
4239 warning_at (loc, OPT_Wtype_limits,
4240 "comparison is always true due to limited range of data type");
4243 if (val != 0)
4245 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4246 if (TREE_SIDE_EFFECTS (primop0))
4247 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4248 return val;
4251 /* Value is not predetermined, but do the comparison
4252 in the type of the operand that is not constant.
4253 TYPE is already properly set. */
4256 /* If either arg is decimal float and the other is float, find the
4257 proper common type to use for comparison. */
4258 else if (real1 && real2
4259 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4260 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4261 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4263 else if (real1 && real2
4264 && (TYPE_PRECISION (TREE_TYPE (primop0))
4265 == TYPE_PRECISION (TREE_TYPE (primop1))))
4266 type = TREE_TYPE (primop0);
4268 /* If args' natural types are both narrower than nominal type
4269 and both extend in the same manner, compare them
4270 in the type of the wider arg.
4271 Otherwise must actually extend both to the nominal
4272 common type lest different ways of extending
4273 alter the result.
4274 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4276 else if (unsignedp0 == unsignedp1 && real1 == real2
4277 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4278 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4280 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4281 type = c_common_signed_or_unsigned_type (unsignedp0
4282 || TYPE_UNSIGNED (*restype_ptr),
4283 type);
4284 /* Make sure shorter operand is extended the right way
4285 to match the longer operand. */
4286 primop0
4287 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4288 TREE_TYPE (primop0)),
4289 primop0);
4290 primop1
4291 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4292 TREE_TYPE (primop1)),
4293 primop1);
4295 else
4297 /* Here we must do the comparison on the nominal type
4298 using the args exactly as we received them. */
4299 type = *restype_ptr;
4300 primop0 = op0;
4301 primop1 = op1;
4303 if (!real1 && !real2 && integer_zerop (primop1)
4304 && TYPE_UNSIGNED (*restype_ptr))
4306 tree value = 0;
4307 /* All unsigned values are >= 0, so we warn. However,
4308 if OP0 is a constant that is >= 0, the signedness of
4309 the comparison isn't an issue, so suppress the
4310 warning. */
4311 bool warn =
4312 warn_type_limits && !in_system_header_at (loc)
4313 && !(TREE_CODE (primop0) == INTEGER_CST
4314 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4315 primop0)))
4316 /* Do not warn for enumeration types. */
4317 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4319 switch (code)
4321 case GE_EXPR:
4322 if (warn)
4323 warning_at (loc, OPT_Wtype_limits,
4324 "comparison of unsigned expression >= 0 is always true");
4325 value = truthvalue_true_node;
4326 break;
4328 case LT_EXPR:
4329 if (warn)
4330 warning_at (loc, OPT_Wtype_limits,
4331 "comparison of unsigned expression < 0 is always false");
4332 value = truthvalue_false_node;
4333 break;
4335 default:
4336 break;
4339 if (value != 0)
4341 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4342 if (TREE_SIDE_EFFECTS (primop0))
4343 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4344 primop0, value);
4345 return value;
4350 *op0_ptr = convert (type, primop0);
4351 *op1_ptr = convert (type, primop1);
4353 *restype_ptr = truthvalue_type_node;
4355 return 0;
4358 /* Return a tree for the sum or difference (RESULTCODE says which)
4359 of pointer PTROP and integer INTOP. */
4361 tree
4362 pointer_int_sum (location_t loc, enum tree_code resultcode,
4363 tree ptrop, tree intop, bool complain)
4365 tree size_exp, ret;
4367 /* The result is a pointer of the same type that is being added. */
4368 tree result_type = TREE_TYPE (ptrop);
4370 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4372 if (complain && warn_pointer_arith)
4373 pedwarn (loc, OPT_Wpointer_arith,
4374 "pointer of type %<void *%> used in arithmetic");
4375 else if (!complain)
4376 return error_mark_node;
4377 size_exp = integer_one_node;
4379 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4381 if (complain && warn_pointer_arith)
4382 pedwarn (loc, OPT_Wpointer_arith,
4383 "pointer to a function used in arithmetic");
4384 else if (!complain)
4385 return error_mark_node;
4386 size_exp = integer_one_node;
4388 else
4389 size_exp = size_in_bytes (TREE_TYPE (result_type));
4391 /* We are manipulating pointer values, so we don't need to warn
4392 about relying on undefined signed overflow. We disable the
4393 warning here because we use integer types so fold won't know that
4394 they are really pointers. */
4395 fold_defer_overflow_warnings ();
4397 /* If what we are about to multiply by the size of the elements
4398 contains a constant term, apply distributive law
4399 and multiply that constant term separately.
4400 This helps produce common subexpressions. */
4401 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4402 && !TREE_CONSTANT (intop)
4403 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4404 && TREE_CONSTANT (size_exp)
4405 /* If the constant comes from pointer subtraction,
4406 skip this optimization--it would cause an error. */
4407 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4408 /* If the constant is unsigned, and smaller than the pointer size,
4409 then we must skip this optimization. This is because it could cause
4410 an overflow error if the constant is negative but INTOP is not. */
4411 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4412 || (TYPE_PRECISION (TREE_TYPE (intop))
4413 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4415 enum tree_code subcode = resultcode;
4416 tree int_type = TREE_TYPE (intop);
4417 if (TREE_CODE (intop) == MINUS_EXPR)
4418 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4419 /* Convert both subexpression types to the type of intop,
4420 because weird cases involving pointer arithmetic
4421 can result in a sum or difference with different type args. */
4422 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4423 subcode, ptrop,
4424 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4425 intop = convert (int_type, TREE_OPERAND (intop, 0));
4428 /* Convert the integer argument to a type the same size as sizetype
4429 so the multiply won't overflow spuriously. */
4430 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4431 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4432 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4433 TYPE_UNSIGNED (sizetype)), intop);
4435 /* Replace the integer argument with a suitable product by the object size.
4436 Do this multiplication as signed, then convert to the appropriate type
4437 for the pointer operation and disregard an overflow that occurred only
4438 because of the sign-extension change in the latter conversion. */
4440 tree t = build_binary_op (loc,
4441 MULT_EXPR, intop,
4442 convert (TREE_TYPE (intop), size_exp), 1);
4443 intop = convert (sizetype, t);
4444 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4445 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4448 /* Create the sum or difference. */
4449 if (resultcode == MINUS_EXPR)
4450 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4452 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4454 fold_undefer_and_ignore_overflow_warnings ();
4456 return ret;
4459 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4460 and if NON_CONST is known not to be permitted in an evaluated part
4461 of a constant expression. */
4463 tree
4464 c_wrap_maybe_const (tree expr, bool non_const)
4466 bool nowarning = TREE_NO_WARNING (expr);
4467 location_t loc = EXPR_LOCATION (expr);
4469 /* This should never be called for C++. */
4470 if (c_dialect_cxx ())
4471 gcc_unreachable ();
4473 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4474 STRIP_TYPE_NOPS (expr);
4475 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4476 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4477 if (nowarning)
4478 TREE_NO_WARNING (expr) = 1;
4479 protected_set_expr_location (expr, loc);
4481 return expr;
4484 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4485 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4486 around the SAVE_EXPR if needed so that c_fully_fold does not need
4487 to look inside SAVE_EXPRs. */
4489 tree
4490 c_save_expr (tree expr)
4492 bool maybe_const = true;
4493 if (c_dialect_cxx ())
4494 return save_expr (expr);
4495 expr = c_fully_fold (expr, false, &maybe_const);
4496 expr = save_expr (expr);
4497 if (!maybe_const)
4498 expr = c_wrap_maybe_const (expr, true);
4499 return expr;
4502 /* Return whether EXPR is a declaration whose address can never be
4503 NULL. */
4505 bool
4506 decl_with_nonnull_addr_p (const_tree expr)
4508 return (DECL_P (expr)
4509 && (TREE_CODE (expr) == PARM_DECL
4510 || TREE_CODE (expr) == LABEL_DECL
4511 || !DECL_WEAK (expr)));
4514 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4515 or for an `if' or `while' statement or ?..: exp. It should already
4516 have been validated to be of suitable type; otherwise, a bad
4517 diagnostic may result.
4519 The EXPR is located at LOCATION.
4521 This preparation consists of taking the ordinary
4522 representation of an expression expr and producing a valid tree
4523 boolean expression describing whether expr is nonzero. We could
4524 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4525 but we optimize comparisons, &&, ||, and !.
4527 The resulting type should always be `truthvalue_type_node'. */
4529 tree
4530 c_common_truthvalue_conversion (location_t location, tree expr)
4532 switch (TREE_CODE (expr))
4534 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4535 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4536 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4537 case ORDERED_EXPR: case UNORDERED_EXPR:
4538 if (TREE_TYPE (expr) == truthvalue_type_node)
4539 return expr;
4540 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4541 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4542 goto ret;
4544 case TRUTH_ANDIF_EXPR:
4545 case TRUTH_ORIF_EXPR:
4546 case TRUTH_AND_EXPR:
4547 case TRUTH_OR_EXPR:
4548 case TRUTH_XOR_EXPR:
4549 if (TREE_TYPE (expr) == truthvalue_type_node)
4550 return expr;
4551 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4552 c_common_truthvalue_conversion (location,
4553 TREE_OPERAND (expr, 0)),
4554 c_common_truthvalue_conversion (location,
4555 TREE_OPERAND (expr, 1)));
4556 goto ret;
4558 case TRUTH_NOT_EXPR:
4559 if (TREE_TYPE (expr) == truthvalue_type_node)
4560 return expr;
4561 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4562 c_common_truthvalue_conversion (location,
4563 TREE_OPERAND (expr, 0)));
4564 goto ret;
4566 case ERROR_MARK:
4567 return expr;
4569 case INTEGER_CST:
4570 return integer_zerop (expr) ? truthvalue_false_node
4571 : truthvalue_true_node;
4573 case REAL_CST:
4574 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4575 ? truthvalue_true_node
4576 : truthvalue_false_node;
4578 case FIXED_CST:
4579 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4580 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4581 ? truthvalue_true_node
4582 : truthvalue_false_node;
4584 case FUNCTION_DECL:
4585 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4586 /* Fall through. */
4588 case ADDR_EXPR:
4590 tree inner = TREE_OPERAND (expr, 0);
4591 if (decl_with_nonnull_addr_p (inner))
4593 /* Common Ada/Pascal programmer's mistake. */
4594 warning_at (location,
4595 OPT_Waddress,
4596 "the address of %qD will always evaluate as %<true%>",
4597 inner);
4598 return truthvalue_true_node;
4600 break;
4603 case COMPLEX_EXPR:
4604 expr = build_binary_op (EXPR_LOCATION (expr),
4605 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4606 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4607 c_common_truthvalue_conversion (location,
4608 TREE_OPERAND (expr, 0)),
4609 c_common_truthvalue_conversion (location,
4610 TREE_OPERAND (expr, 1)),
4612 goto ret;
4614 case NEGATE_EXPR:
4615 case ABS_EXPR:
4616 case FLOAT_EXPR:
4617 case EXCESS_PRECISION_EXPR:
4618 /* These don't change whether an object is nonzero or zero. */
4619 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4621 case LROTATE_EXPR:
4622 case RROTATE_EXPR:
4623 /* These don't change whether an object is zero or nonzero, but
4624 we can't ignore them if their second arg has side-effects. */
4625 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4627 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4628 TREE_OPERAND (expr, 1),
4629 c_common_truthvalue_conversion
4630 (location, TREE_OPERAND (expr, 0)));
4631 goto ret;
4633 else
4634 return c_common_truthvalue_conversion (location,
4635 TREE_OPERAND (expr, 0));
4637 case COND_EXPR:
4638 /* Distribute the conversion into the arms of a COND_EXPR. */
4639 if (c_dialect_cxx ())
4641 tree op1 = TREE_OPERAND (expr, 1);
4642 tree op2 = TREE_OPERAND (expr, 2);
4643 /* In C++ one of the arms might have void type if it is throw. */
4644 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4645 op1 = c_common_truthvalue_conversion (location, op1);
4646 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4647 op2 = c_common_truthvalue_conversion (location, op2);
4648 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4649 TREE_OPERAND (expr, 0), op1, op2);
4650 goto ret;
4652 else
4654 /* Folding will happen later for C. */
4655 expr = build3 (COND_EXPR, truthvalue_type_node,
4656 TREE_OPERAND (expr, 0),
4657 c_common_truthvalue_conversion (location,
4658 TREE_OPERAND (expr, 1)),
4659 c_common_truthvalue_conversion (location,
4660 TREE_OPERAND (expr, 2)));
4661 goto ret;
4664 CASE_CONVERT:
4666 tree totype = TREE_TYPE (expr);
4667 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4669 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4670 since that affects how `default_conversion' will behave. */
4671 if (TREE_CODE (totype) == REFERENCE_TYPE
4672 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4673 break;
4674 /* Don't strip a conversion from C++0x scoped enum, since they
4675 don't implicitly convert to other types. */
4676 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4677 && ENUM_IS_SCOPED (fromtype))
4678 break;
4679 /* If this isn't narrowing the argument, we can ignore it. */
4680 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4681 return c_common_truthvalue_conversion (location,
4682 TREE_OPERAND (expr, 0));
4684 break;
4686 case MODIFY_EXPR:
4687 if (!TREE_NO_WARNING (expr)
4688 && warn_parentheses)
4690 warning (OPT_Wparentheses,
4691 "suggest parentheses around assignment used as truth value");
4692 TREE_NO_WARNING (expr) = 1;
4694 break;
4696 default:
4697 break;
4700 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4702 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4703 expr = (build_binary_op
4704 (EXPR_LOCATION (expr),
4705 (TREE_SIDE_EFFECTS (expr)
4706 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4707 c_common_truthvalue_conversion
4708 (location,
4709 build_unary_op (location, REALPART_EXPR, t, 0)),
4710 c_common_truthvalue_conversion
4711 (location,
4712 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4713 0));
4714 goto ret;
4717 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4719 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4720 FCONST0 (TYPE_MODE
4721 (TREE_TYPE (expr))));
4722 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4724 else
4725 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4727 ret:
4728 protected_set_expr_location (expr, location);
4729 return expr;
4732 static void def_builtin_1 (enum built_in_function fncode,
4733 const char *name,
4734 enum built_in_class fnclass,
4735 tree fntype, tree libtype,
4736 bool both_p, bool fallback_p, bool nonansi_p,
4737 tree fnattrs, bool implicit_p);
4740 /* Apply the TYPE_QUALS to the new DECL. */
4742 void
4743 c_apply_type_quals_to_decl (int type_quals, tree decl)
4745 tree type = TREE_TYPE (decl);
4747 if (type == error_mark_node)
4748 return;
4750 if ((type_quals & TYPE_QUAL_CONST)
4751 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4752 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4753 constructor can produce constant init, so rely on cp_finish_decl to
4754 clear TREE_READONLY if the variable has non-constant init. */
4755 TREE_READONLY (decl) = 1;
4756 if (type_quals & TYPE_QUAL_VOLATILE)
4758 TREE_SIDE_EFFECTS (decl) = 1;
4759 TREE_THIS_VOLATILE (decl) = 1;
4761 if (type_quals & TYPE_QUAL_RESTRICT)
4763 while (type && TREE_CODE (type) == ARRAY_TYPE)
4764 /* Allow 'restrict' on arrays of pointers.
4765 FIXME currently we just ignore it. */
4766 type = TREE_TYPE (type);
4767 if (!type
4768 || !POINTER_TYPE_P (type)
4769 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4770 error ("invalid use of %<restrict%>");
4774 /* Hash function for the problem of multiple type definitions in
4775 different files. This must hash all types that will compare
4776 equal via comptypes to the same value. In practice it hashes
4777 on some of the simple stuff and leaves the details to comptypes. */
4779 static hashval_t
4780 c_type_hash (const void *p)
4782 int n_elements;
4783 int shift, size;
4784 const_tree const t = (const_tree) p;
4785 tree t2;
4786 switch (TREE_CODE (t))
4788 /* For pointers, hash on pointee type plus some swizzling. */
4789 case POINTER_TYPE:
4790 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4791 /* Hash on number of elements and total size. */
4792 case ENUMERAL_TYPE:
4793 shift = 3;
4794 t2 = TYPE_VALUES (t);
4795 break;
4796 case RECORD_TYPE:
4797 shift = 0;
4798 t2 = TYPE_FIELDS (t);
4799 break;
4800 case QUAL_UNION_TYPE:
4801 shift = 1;
4802 t2 = TYPE_FIELDS (t);
4803 break;
4804 case UNION_TYPE:
4805 shift = 2;
4806 t2 = TYPE_FIELDS (t);
4807 break;
4808 default:
4809 gcc_unreachable ();
4811 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4812 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4813 n_elements = list_length (t2);
4814 /* We might have a VLA here. */
4815 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4816 size = 0;
4817 else
4818 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4819 return ((size << 24) | (n_elements << shift));
4822 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4824 /* Return the typed-based alias set for T, which may be an expression
4825 or a type. Return -1 if we don't do anything special. */
4827 alias_set_type
4828 c_common_get_alias_set (tree t)
4830 tree u;
4831 PTR *slot;
4833 /* For VLAs, use the alias set of the element type rather than the
4834 default of alias set 0 for types compared structurally. */
4835 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4837 if (TREE_CODE (t) == ARRAY_TYPE)
4838 return get_alias_set (TREE_TYPE (t));
4839 return -1;
4842 /* Permit type-punning when accessing a union, provided the access
4843 is directly through the union. For example, this code does not
4844 permit taking the address of a union member and then storing
4845 through it. Even the type-punning allowed here is a GCC
4846 extension, albeit a common and useful one; the C standard says
4847 that such accesses have implementation-defined behavior. */
4848 for (u = t;
4849 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4850 u = TREE_OPERAND (u, 0))
4851 if (TREE_CODE (u) == COMPONENT_REF
4852 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4853 return 0;
4855 /* That's all the expressions we handle specially. */
4856 if (!TYPE_P (t))
4857 return -1;
4859 /* The C standard guarantees that any object may be accessed via an
4860 lvalue that has character type. */
4861 if (t == char_type_node
4862 || t == signed_char_type_node
4863 || t == unsigned_char_type_node)
4864 return 0;
4866 /* The C standard specifically allows aliasing between signed and
4867 unsigned variants of the same type. We treat the signed
4868 variant as canonical. */
4869 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4871 tree t1 = c_common_signed_type (t);
4873 /* t1 == t can happen for boolean nodes which are always unsigned. */
4874 if (t1 != t)
4875 return get_alias_set (t1);
4878 /* Handle the case of multiple type nodes referring to "the same" type,
4879 which occurs with IMA. These share an alias set. FIXME: Currently only
4880 C90 is handled. (In C99 type compatibility is not transitive, which
4881 complicates things mightily. The alias set splay trees can theoretically
4882 represent this, but insertion is tricky when you consider all the
4883 different orders things might arrive in.) */
4885 if (c_language != clk_c || flag_isoc99)
4886 return -1;
4888 /* Save time if there's only one input file. */
4889 if (num_in_fnames == 1)
4890 return -1;
4892 /* Pointers need special handling if they point to any type that
4893 needs special handling (below). */
4894 if (TREE_CODE (t) == POINTER_TYPE)
4896 tree t2;
4897 /* Find bottom type under any nested POINTERs. */
4898 for (t2 = TREE_TYPE (t);
4899 TREE_CODE (t2) == POINTER_TYPE;
4900 t2 = TREE_TYPE (t2))
4902 if (TREE_CODE (t2) != RECORD_TYPE
4903 && TREE_CODE (t2) != ENUMERAL_TYPE
4904 && TREE_CODE (t2) != QUAL_UNION_TYPE
4905 && TREE_CODE (t2) != UNION_TYPE)
4906 return -1;
4907 if (TYPE_SIZE (t2) == 0)
4908 return -1;
4910 /* These are the only cases that need special handling. */
4911 if (TREE_CODE (t) != RECORD_TYPE
4912 && TREE_CODE (t) != ENUMERAL_TYPE
4913 && TREE_CODE (t) != QUAL_UNION_TYPE
4914 && TREE_CODE (t) != UNION_TYPE
4915 && TREE_CODE (t) != POINTER_TYPE)
4916 return -1;
4917 /* Undefined? */
4918 if (TYPE_SIZE (t) == 0)
4919 return -1;
4921 /* Look up t in hash table. Only one of the compatible types within each
4922 alias set is recorded in the table. */
4923 if (!type_hash_table)
4924 type_hash_table = htab_create_ggc (1021, c_type_hash,
4925 (htab_eq) lang_hooks.types_compatible_p,
4926 NULL);
4927 slot = htab_find_slot (type_hash_table, t, INSERT);
4928 if (*slot != NULL)
4930 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4931 return TYPE_ALIAS_SET ((tree)*slot);
4933 else
4934 /* Our caller will assign and record (in t) a new alias set; all we need
4935 to do is remember t in the hash table. */
4936 *slot = t;
4938 return -1;
4941 /* Return the least alignment required for type TYPE. */
4943 unsigned int
4944 min_align_of_type (tree type)
4946 unsigned int align = TYPE_ALIGN (type);
4947 align = MIN (align, BIGGEST_ALIGNMENT);
4948 #ifdef BIGGEST_FIELD_ALIGNMENT
4949 align = MIN (align, BIGGEST_FIELD_ALIGNMENT);
4950 #endif
4951 unsigned int field_align = align;
4952 #ifdef ADJUST_FIELD_ALIGN
4953 tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
4954 type);
4955 field_align = ADJUST_FIELD_ALIGN (field, field_align);
4956 #endif
4957 align = MIN (align, field_align);
4958 return align / BITS_PER_UNIT;
4961 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4962 the IS_SIZEOF parameter indicates which operator is being applied.
4963 The COMPLAIN flag controls whether we should diagnose possibly
4964 ill-formed constructs or not. LOC is the location of the SIZEOF or
4965 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4966 a type in any context should be returned, rather than the normal
4967 alignment for that type. */
4969 tree
4970 c_sizeof_or_alignof_type (location_t loc,
4971 tree type, bool is_sizeof, bool min_alignof,
4972 int complain)
4974 const char *op_name;
4975 tree value = NULL;
4976 enum tree_code type_code = TREE_CODE (type);
4978 op_name = is_sizeof ? "sizeof" : "__alignof__";
4980 if (type_code == FUNCTION_TYPE)
4982 if (is_sizeof)
4984 if (complain && warn_pointer_arith)
4985 pedwarn (loc, OPT_Wpointer_arith,
4986 "invalid application of %<sizeof%> to a function type");
4987 else if (!complain)
4988 return error_mark_node;
4989 value = size_one_node;
4991 else
4993 if (complain)
4995 if (c_dialect_cxx ())
4996 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4997 "%<alignof%> applied to a function type");
4998 else
4999 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5000 "%<_Alignof%> applied to a function type");
5002 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5005 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5007 if (type_code == VOID_TYPE
5008 && complain && warn_pointer_arith)
5009 pedwarn (loc, OPT_Wpointer_arith,
5010 "invalid application of %qs to a void type", op_name);
5011 else if (!complain)
5012 return error_mark_node;
5013 value = size_one_node;
5015 else if (!COMPLETE_TYPE_P (type)
5016 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5018 if (complain)
5019 error_at (loc, "invalid application of %qs to incomplete type %qT",
5020 op_name, type);
5021 return error_mark_node;
5023 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5024 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5026 if (complain)
5027 error_at (loc, "invalid application of %qs to array type %qT of "
5028 "incomplete element type", op_name, type);
5029 return error_mark_node;
5031 else
5033 if (is_sizeof)
5034 /* Convert in case a char is more than one unit. */
5035 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5036 size_int (TYPE_PRECISION (char_type_node)
5037 / BITS_PER_UNIT));
5038 else if (min_alignof)
5039 value = size_int (min_align_of_type (type));
5040 else
5041 value = size_int (TYPE_ALIGN_UNIT (type));
5044 /* VALUE will have the middle-end integer type sizetype.
5045 However, we should really return a value of type `size_t',
5046 which is just a typedef for an ordinary integer type. */
5047 value = fold_convert_loc (loc, size_type_node, value);
5049 return value;
5052 /* Implement the __alignof keyword: Return the minimum required
5053 alignment of EXPR, measured in bytes. For VAR_DECLs,
5054 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5055 from an "aligned" __attribute__ specification). LOC is the
5056 location of the ALIGNOF operator. */
5058 tree
5059 c_alignof_expr (location_t loc, tree expr)
5061 tree t;
5063 if (VAR_OR_FUNCTION_DECL_P (expr))
5064 t = size_int (DECL_ALIGN_UNIT (expr));
5066 else if (TREE_CODE (expr) == COMPONENT_REF
5067 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5069 error_at (loc, "%<__alignof%> applied to a bit-field");
5070 t = size_one_node;
5072 else if (TREE_CODE (expr) == COMPONENT_REF
5073 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5074 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5076 else if (TREE_CODE (expr) == INDIRECT_REF)
5078 tree t = TREE_OPERAND (expr, 0);
5079 tree best = t;
5080 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5082 while (CONVERT_EXPR_P (t)
5083 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5085 int thisalign;
5087 t = TREE_OPERAND (t, 0);
5088 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5089 if (thisalign > bestalign)
5090 best = t, bestalign = thisalign;
5092 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5094 else
5095 return c_alignof (loc, TREE_TYPE (expr));
5097 return fold_convert_loc (loc, size_type_node, t);
5100 /* Handle C and C++ default attributes. */
5102 enum built_in_attribute
5104 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5105 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5106 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5107 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5108 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5109 #include "builtin-attrs.def"
5110 #undef DEF_ATTR_NULL_TREE
5111 #undef DEF_ATTR_INT
5112 #undef DEF_ATTR_STRING
5113 #undef DEF_ATTR_IDENT
5114 #undef DEF_ATTR_TREE_LIST
5115 ATTR_LAST
5118 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5120 static void c_init_attributes (void);
5122 enum c_builtin_type
5124 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5125 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5126 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5127 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5128 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5129 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5130 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5131 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5132 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5133 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5134 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5135 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5136 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5137 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5138 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5139 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5140 NAME,
5141 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5142 #include "builtin-types.def"
5143 #undef DEF_PRIMITIVE_TYPE
5144 #undef DEF_FUNCTION_TYPE_0
5145 #undef DEF_FUNCTION_TYPE_1
5146 #undef DEF_FUNCTION_TYPE_2
5147 #undef DEF_FUNCTION_TYPE_3
5148 #undef DEF_FUNCTION_TYPE_4
5149 #undef DEF_FUNCTION_TYPE_5
5150 #undef DEF_FUNCTION_TYPE_6
5151 #undef DEF_FUNCTION_TYPE_7
5152 #undef DEF_FUNCTION_TYPE_8
5153 #undef DEF_FUNCTION_TYPE_VAR_0
5154 #undef DEF_FUNCTION_TYPE_VAR_1
5155 #undef DEF_FUNCTION_TYPE_VAR_2
5156 #undef DEF_FUNCTION_TYPE_VAR_3
5157 #undef DEF_FUNCTION_TYPE_VAR_4
5158 #undef DEF_FUNCTION_TYPE_VAR_5
5159 #undef DEF_POINTER_TYPE
5160 BT_LAST
5163 typedef enum c_builtin_type builtin_type;
5165 /* A temporary array for c_common_nodes_and_builtins. Used in
5166 communication with def_fn_type. */
5167 static tree builtin_types[(int) BT_LAST + 1];
5169 /* A helper function for c_common_nodes_and_builtins. Build function type
5170 for DEF with return type RET and N arguments. If VAR is true, then the
5171 function should be variadic after those N arguments.
5173 Takes special care not to ICE if any of the types involved are
5174 error_mark_node, which indicates that said type is not in fact available
5175 (see builtin_type_for_size). In which case the function type as a whole
5176 should be error_mark_node. */
5178 static void
5179 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5181 tree t;
5182 tree *args = XALLOCAVEC (tree, n);
5183 va_list list;
5184 int i;
5186 va_start (list, n);
5187 for (i = 0; i < n; ++i)
5189 builtin_type a = (builtin_type) va_arg (list, int);
5190 t = builtin_types[a];
5191 if (t == error_mark_node)
5192 goto egress;
5193 args[i] = t;
5196 t = builtin_types[ret];
5197 if (t == error_mark_node)
5198 goto egress;
5199 if (var)
5200 t = build_varargs_function_type_array (t, n, args);
5201 else
5202 t = build_function_type_array (t, n, args);
5204 egress:
5205 builtin_types[def] = t;
5206 va_end (list);
5209 /* Build builtin functions common to both C and C++ language
5210 frontends. */
5212 static void
5213 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5215 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5216 builtin_types[ENUM] = VALUE;
5217 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5218 def_fn_type (ENUM, RETURN, 0, 0);
5219 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5220 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5221 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5222 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5223 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5224 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5225 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5226 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5227 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5228 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5229 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5230 ARG6) \
5231 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5232 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5233 ARG6, ARG7) \
5234 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5235 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5236 ARG6, ARG7, ARG8) \
5237 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5238 ARG7, ARG8);
5239 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5240 def_fn_type (ENUM, RETURN, 1, 0);
5241 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5242 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5243 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5244 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5245 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5246 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5247 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5248 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5249 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5250 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5251 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5252 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5254 #include "builtin-types.def"
5256 #undef DEF_PRIMITIVE_TYPE
5257 #undef DEF_FUNCTION_TYPE_1
5258 #undef DEF_FUNCTION_TYPE_2
5259 #undef DEF_FUNCTION_TYPE_3
5260 #undef DEF_FUNCTION_TYPE_4
5261 #undef DEF_FUNCTION_TYPE_5
5262 #undef DEF_FUNCTION_TYPE_6
5263 #undef DEF_FUNCTION_TYPE_VAR_0
5264 #undef DEF_FUNCTION_TYPE_VAR_1
5265 #undef DEF_FUNCTION_TYPE_VAR_2
5266 #undef DEF_FUNCTION_TYPE_VAR_3
5267 #undef DEF_FUNCTION_TYPE_VAR_4
5268 #undef DEF_FUNCTION_TYPE_VAR_5
5269 #undef DEF_POINTER_TYPE
5270 builtin_types[(int) BT_LAST] = NULL_TREE;
5272 c_init_attributes ();
5274 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5275 NONANSI_P, ATTRS, IMPLICIT, COND) \
5276 if (NAME && COND) \
5277 def_builtin_1 (ENUM, NAME, CLASS, \
5278 builtin_types[(int) TYPE], \
5279 builtin_types[(int) LIBTYPE], \
5280 BOTH_P, FALLBACK_P, NONANSI_P, \
5281 built_in_attributes[(int) ATTRS], IMPLICIT);
5282 #include "builtins.def"
5283 #undef DEF_BUILTIN
5285 targetm.init_builtins ();
5287 build_common_builtin_nodes ();
5289 if (flag_cilkplus)
5290 cilk_init_builtins ();
5293 /* Like get_identifier, but avoid warnings about null arguments when
5294 the argument may be NULL for targets where GCC lacks stdint.h type
5295 information. */
5297 static inline tree
5298 c_get_ident (const char *id)
5300 return get_identifier (id);
5303 /* Build tree nodes and builtin functions common to both C and C++ language
5304 frontends. */
5306 void
5307 c_common_nodes_and_builtins (void)
5309 int char16_type_size;
5310 int char32_type_size;
5311 int wchar_type_size;
5312 tree array_domain_type;
5313 tree va_list_ref_type_node;
5314 tree va_list_arg_type_node;
5316 build_common_tree_nodes (flag_signed_char, flag_short_double);
5318 /* Define `int' and `char' first so that dbx will output them first. */
5319 record_builtin_type (RID_INT, NULL, integer_type_node);
5320 record_builtin_type (RID_CHAR, "char", char_type_node);
5322 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5323 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5324 but not C. Are the conditionals here needed? */
5325 if (c_dialect_cxx ())
5326 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5327 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5328 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5329 record_builtin_type (RID_MAX, "long unsigned int",
5330 long_unsigned_type_node);
5331 if (int128_integer_type_node != NULL_TREE)
5333 record_builtin_type (RID_INT128, "__int128",
5334 int128_integer_type_node);
5335 record_builtin_type (RID_MAX, "__int128 unsigned",
5336 int128_unsigned_type_node);
5338 if (c_dialect_cxx ())
5339 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5340 record_builtin_type (RID_MAX, "long long int",
5341 long_long_integer_type_node);
5342 record_builtin_type (RID_MAX, "long long unsigned int",
5343 long_long_unsigned_type_node);
5344 if (c_dialect_cxx ())
5345 record_builtin_type (RID_MAX, "long long unsigned",
5346 long_long_unsigned_type_node);
5347 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5348 record_builtin_type (RID_MAX, "short unsigned int",
5349 short_unsigned_type_node);
5350 if (c_dialect_cxx ())
5351 record_builtin_type (RID_MAX, "unsigned short",
5352 short_unsigned_type_node);
5354 /* Define both `signed char' and `unsigned char'. */
5355 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5356 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5358 /* These are types that c_common_type_for_size and
5359 c_common_type_for_mode use. */
5360 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5361 TYPE_DECL, NULL_TREE,
5362 intQI_type_node));
5363 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5364 TYPE_DECL, NULL_TREE,
5365 intHI_type_node));
5366 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5367 TYPE_DECL, NULL_TREE,
5368 intSI_type_node));
5369 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5370 TYPE_DECL, NULL_TREE,
5371 intDI_type_node));
5372 #if HOST_BITS_PER_WIDE_INT >= 64
5373 if (targetm.scalar_mode_supported_p (TImode))
5374 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5375 TYPE_DECL,
5376 get_identifier ("__int128_t"),
5377 intTI_type_node));
5378 #endif
5379 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5380 TYPE_DECL, NULL_TREE,
5381 unsigned_intQI_type_node));
5382 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5383 TYPE_DECL, NULL_TREE,
5384 unsigned_intHI_type_node));
5385 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5386 TYPE_DECL, NULL_TREE,
5387 unsigned_intSI_type_node));
5388 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5389 TYPE_DECL, NULL_TREE,
5390 unsigned_intDI_type_node));
5391 #if HOST_BITS_PER_WIDE_INT >= 64
5392 if (targetm.scalar_mode_supported_p (TImode))
5393 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5394 TYPE_DECL,
5395 get_identifier ("__uint128_t"),
5396 unsigned_intTI_type_node));
5397 #endif
5399 /* Create the widest literal types. */
5400 widest_integer_literal_type_node
5401 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5402 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5403 TYPE_DECL, NULL_TREE,
5404 widest_integer_literal_type_node));
5406 widest_unsigned_literal_type_node
5407 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5408 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5409 TYPE_DECL, NULL_TREE,
5410 widest_unsigned_literal_type_node));
5412 signed_size_type_node = c_common_signed_type (size_type_node);
5414 pid_type_node =
5415 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5417 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5418 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5419 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5421 /* Only supported decimal floating point extension if the target
5422 actually supports underlying modes. */
5423 if (targetm.scalar_mode_supported_p (SDmode)
5424 && targetm.scalar_mode_supported_p (DDmode)
5425 && targetm.scalar_mode_supported_p (TDmode))
5427 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5428 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5429 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5432 if (targetm.fixed_point_supported_p ())
5434 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5435 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5436 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5437 record_builtin_type (RID_MAX, "long long _Fract",
5438 long_long_fract_type_node);
5439 record_builtin_type (RID_MAX, "unsigned short _Fract",
5440 unsigned_short_fract_type_node);
5441 record_builtin_type (RID_MAX, "unsigned _Fract",
5442 unsigned_fract_type_node);
5443 record_builtin_type (RID_MAX, "unsigned long _Fract",
5444 unsigned_long_fract_type_node);
5445 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5446 unsigned_long_long_fract_type_node);
5447 record_builtin_type (RID_MAX, "_Sat short _Fract",
5448 sat_short_fract_type_node);
5449 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5450 record_builtin_type (RID_MAX, "_Sat long _Fract",
5451 sat_long_fract_type_node);
5452 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5453 sat_long_long_fract_type_node);
5454 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5455 sat_unsigned_short_fract_type_node);
5456 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5457 sat_unsigned_fract_type_node);
5458 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5459 sat_unsigned_long_fract_type_node);
5460 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5461 sat_unsigned_long_long_fract_type_node);
5462 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5463 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5464 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5465 record_builtin_type (RID_MAX, "long long _Accum",
5466 long_long_accum_type_node);
5467 record_builtin_type (RID_MAX, "unsigned short _Accum",
5468 unsigned_short_accum_type_node);
5469 record_builtin_type (RID_MAX, "unsigned _Accum",
5470 unsigned_accum_type_node);
5471 record_builtin_type (RID_MAX, "unsigned long _Accum",
5472 unsigned_long_accum_type_node);
5473 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5474 unsigned_long_long_accum_type_node);
5475 record_builtin_type (RID_MAX, "_Sat short _Accum",
5476 sat_short_accum_type_node);
5477 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5478 record_builtin_type (RID_MAX, "_Sat long _Accum",
5479 sat_long_accum_type_node);
5480 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5481 sat_long_long_accum_type_node);
5482 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5483 sat_unsigned_short_accum_type_node);
5484 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5485 sat_unsigned_accum_type_node);
5486 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5487 sat_unsigned_long_accum_type_node);
5488 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5489 sat_unsigned_long_long_accum_type_node);
5493 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5494 TYPE_DECL,
5495 get_identifier ("complex int"),
5496 complex_integer_type_node));
5497 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5498 TYPE_DECL,
5499 get_identifier ("complex float"),
5500 complex_float_type_node));
5501 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5502 TYPE_DECL,
5503 get_identifier ("complex double"),
5504 complex_double_type_node));
5505 lang_hooks.decls.pushdecl
5506 (build_decl (UNKNOWN_LOCATION,
5507 TYPE_DECL, get_identifier ("complex long double"),
5508 complex_long_double_type_node));
5510 if (c_dialect_cxx ())
5511 /* For C++, make fileptr_type_node a distinct void * type until
5512 FILE type is defined. */
5513 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5515 record_builtin_type (RID_VOID, NULL, void_type_node);
5517 /* Set the TYPE_NAME for any variants that were built before
5518 record_builtin_type gave names to the built-in types. */
5520 tree void_name = TYPE_NAME (void_type_node);
5521 TYPE_NAME (void_type_node) = NULL_TREE;
5522 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5523 = void_name;
5524 TYPE_NAME (void_type_node) = void_name;
5527 /* This node must not be shared. */
5528 void_zero_node = make_int_cst (1, 1);
5529 TREE_TYPE (void_zero_node) = void_type_node;
5531 void_list_node = build_void_list_node ();
5533 /* Make a type to be the domain of a few array types
5534 whose domains don't really matter.
5535 200 is small enough that it always fits in size_t
5536 and large enough that it can hold most function names for the
5537 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5538 array_domain_type = build_index_type (size_int (200));
5540 /* Make a type for arrays of characters.
5541 With luck nothing will ever really depend on the length of this
5542 array type. */
5543 char_array_type_node
5544 = build_array_type (char_type_node, array_domain_type);
5546 string_type_node = build_pointer_type (char_type_node);
5547 const_string_type_node
5548 = build_pointer_type (build_qualified_type
5549 (char_type_node, TYPE_QUAL_CONST));
5551 /* This is special for C++ so functions can be overloaded. */
5552 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5553 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5554 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5555 underlying_wchar_type_node = wchar_type_node;
5556 if (c_dialect_cxx ())
5558 if (TYPE_UNSIGNED (wchar_type_node))
5559 wchar_type_node = make_unsigned_type (wchar_type_size);
5560 else
5561 wchar_type_node = make_signed_type (wchar_type_size);
5562 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5565 /* This is for wide string constants. */
5566 wchar_array_type_node
5567 = build_array_type (wchar_type_node, array_domain_type);
5569 /* Define 'char16_t'. */
5570 char16_type_node = get_identifier (CHAR16_TYPE);
5571 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5572 char16_type_size = TYPE_PRECISION (char16_type_node);
5573 if (c_dialect_cxx ())
5575 char16_type_node = make_unsigned_type (char16_type_size);
5577 if (cxx_dialect >= cxx11)
5578 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5581 /* This is for UTF-16 string constants. */
5582 char16_array_type_node
5583 = build_array_type (char16_type_node, array_domain_type);
5585 /* Define 'char32_t'. */
5586 char32_type_node = get_identifier (CHAR32_TYPE);
5587 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5588 char32_type_size = TYPE_PRECISION (char32_type_node);
5589 if (c_dialect_cxx ())
5591 char32_type_node = make_unsigned_type (char32_type_size);
5593 if (cxx_dialect >= cxx11)
5594 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5597 /* This is for UTF-32 string constants. */
5598 char32_array_type_node
5599 = build_array_type (char32_type_node, array_domain_type);
5601 wint_type_node =
5602 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5604 intmax_type_node =
5605 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5606 uintmax_type_node =
5607 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5609 if (SIG_ATOMIC_TYPE)
5610 sig_atomic_type_node =
5611 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5612 if (INT8_TYPE)
5613 int8_type_node =
5614 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5615 if (INT16_TYPE)
5616 int16_type_node =
5617 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5618 if (INT32_TYPE)
5619 int32_type_node =
5620 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5621 if (INT64_TYPE)
5622 int64_type_node =
5623 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5624 if (UINT8_TYPE)
5625 uint8_type_node =
5626 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5627 if (UINT16_TYPE)
5628 c_uint16_type_node = uint16_type_node =
5629 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5630 if (UINT32_TYPE)
5631 c_uint32_type_node = uint32_type_node =
5632 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5633 if (UINT64_TYPE)
5634 c_uint64_type_node = uint64_type_node =
5635 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5636 if (INT_LEAST8_TYPE)
5637 int_least8_type_node =
5638 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5639 if (INT_LEAST16_TYPE)
5640 int_least16_type_node =
5641 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5642 if (INT_LEAST32_TYPE)
5643 int_least32_type_node =
5644 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5645 if (INT_LEAST64_TYPE)
5646 int_least64_type_node =
5647 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5648 if (UINT_LEAST8_TYPE)
5649 uint_least8_type_node =
5650 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5651 if (UINT_LEAST16_TYPE)
5652 uint_least16_type_node =
5653 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5654 if (UINT_LEAST32_TYPE)
5655 uint_least32_type_node =
5656 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5657 if (UINT_LEAST64_TYPE)
5658 uint_least64_type_node =
5659 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5660 if (INT_FAST8_TYPE)
5661 int_fast8_type_node =
5662 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5663 if (INT_FAST16_TYPE)
5664 int_fast16_type_node =
5665 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5666 if (INT_FAST32_TYPE)
5667 int_fast32_type_node =
5668 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5669 if (INT_FAST64_TYPE)
5670 int_fast64_type_node =
5671 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5672 if (UINT_FAST8_TYPE)
5673 uint_fast8_type_node =
5674 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5675 if (UINT_FAST16_TYPE)
5676 uint_fast16_type_node =
5677 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5678 if (UINT_FAST32_TYPE)
5679 uint_fast32_type_node =
5680 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5681 if (UINT_FAST64_TYPE)
5682 uint_fast64_type_node =
5683 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5684 if (INTPTR_TYPE)
5685 intptr_type_node =
5686 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5687 if (UINTPTR_TYPE)
5688 uintptr_type_node =
5689 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5691 default_function_type
5692 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5693 ptrdiff_type_node
5694 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5695 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5697 lang_hooks.decls.pushdecl
5698 (build_decl (UNKNOWN_LOCATION,
5699 TYPE_DECL, get_identifier ("__builtin_va_list"),
5700 va_list_type_node));
5701 if (targetm.enum_va_list_p)
5703 int l;
5704 const char *pname;
5705 tree ptype;
5707 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5709 lang_hooks.decls.pushdecl
5710 (build_decl (UNKNOWN_LOCATION,
5711 TYPE_DECL, get_identifier (pname),
5712 ptype));
5717 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5719 va_list_arg_type_node = va_list_ref_type_node =
5720 build_pointer_type (TREE_TYPE (va_list_type_node));
5722 else
5724 va_list_arg_type_node = va_list_type_node;
5725 va_list_ref_type_node = build_reference_type (va_list_type_node);
5728 if (!flag_preprocess_only)
5729 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5731 main_identifier_node = get_identifier ("main");
5733 /* Create the built-in __null node. It is important that this is
5734 not shared. */
5735 null_node = make_int_cst (1, 1);
5736 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5738 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5739 memset (builtin_types, 0, sizeof (builtin_types));
5742 /* The number of named compound-literals generated thus far. */
5743 static GTY(()) int compound_literal_number;
5745 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5747 void
5748 set_compound_literal_name (tree decl)
5750 char *name;
5751 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5752 compound_literal_number);
5753 compound_literal_number++;
5754 DECL_NAME (decl) = get_identifier (name);
5757 tree
5758 build_va_arg (location_t loc, tree expr, tree type)
5760 expr = build1 (VA_ARG_EXPR, type, expr);
5761 SET_EXPR_LOCATION (expr, loc);
5762 return expr;
5766 /* Linked list of disabled built-in functions. */
5768 typedef struct disabled_builtin
5770 const char *name;
5771 struct disabled_builtin *next;
5772 } disabled_builtin;
5773 static disabled_builtin *disabled_builtins = NULL;
5775 static bool builtin_function_disabled_p (const char *);
5777 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5778 begins with "__builtin_", give an error. */
5780 void
5781 disable_builtin_function (const char *name)
5783 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5784 error ("cannot disable built-in function %qs", name);
5785 else
5787 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5788 new_disabled_builtin->name = name;
5789 new_disabled_builtin->next = disabled_builtins;
5790 disabled_builtins = new_disabled_builtin;
5795 /* Return true if the built-in function NAME has been disabled, false
5796 otherwise. */
5798 static bool
5799 builtin_function_disabled_p (const char *name)
5801 disabled_builtin *p;
5802 for (p = disabled_builtins; p != NULL; p = p->next)
5804 if (strcmp (name, p->name) == 0)
5805 return true;
5807 return false;
5811 /* Worker for DEF_BUILTIN.
5812 Possibly define a builtin function with one or two names.
5813 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5814 nonansi_p and flag_no_nonansi_builtin. */
5816 static void
5817 def_builtin_1 (enum built_in_function fncode,
5818 const char *name,
5819 enum built_in_class fnclass,
5820 tree fntype, tree libtype,
5821 bool both_p, bool fallback_p, bool nonansi_p,
5822 tree fnattrs, bool implicit_p)
5824 tree decl;
5825 const char *libname;
5827 if (fntype == error_mark_node)
5828 return;
5830 gcc_assert ((!both_p && !fallback_p)
5831 || !strncmp (name, "__builtin_",
5832 strlen ("__builtin_")));
5834 libname = name + strlen ("__builtin_");
5835 decl = add_builtin_function (name, fntype, fncode, fnclass,
5836 (fallback_p ? libname : NULL),
5837 fnattrs);
5839 set_builtin_decl (fncode, decl, implicit_p);
5841 if (both_p
5842 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5843 && !(nonansi_p && flag_no_nonansi_builtin))
5844 add_builtin_function (libname, libtype, fncode, fnclass,
5845 NULL, fnattrs);
5848 /* Nonzero if the type T promotes to int. This is (nearly) the
5849 integral promotions defined in ISO C99 6.3.1.1/2. */
5851 bool
5852 c_promoting_integer_type_p (const_tree t)
5854 switch (TREE_CODE (t))
5856 case INTEGER_TYPE:
5857 return (TYPE_MAIN_VARIANT (t) == char_type_node
5858 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5859 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5860 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5861 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5862 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5864 case ENUMERAL_TYPE:
5865 /* ??? Technically all enumerations not larger than an int
5866 promote to an int. But this is used along code paths
5867 that only want to notice a size change. */
5868 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5870 case BOOLEAN_TYPE:
5871 return 1;
5873 default:
5874 return 0;
5878 /* Return 1 if PARMS specifies a fixed number of parameters
5879 and none of their types is affected by default promotions. */
5882 self_promoting_args_p (const_tree parms)
5884 const_tree t;
5885 for (t = parms; t; t = TREE_CHAIN (t))
5887 tree type = TREE_VALUE (t);
5889 if (type == error_mark_node)
5890 continue;
5892 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5893 return 0;
5895 if (type == 0)
5896 return 0;
5898 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5899 return 0;
5901 if (c_promoting_integer_type_p (type))
5902 return 0;
5904 return 1;
5907 /* Recursively remove any '*' or '&' operator from TYPE. */
5908 tree
5909 strip_pointer_operator (tree t)
5911 while (POINTER_TYPE_P (t))
5912 t = TREE_TYPE (t);
5913 return t;
5916 /* Recursively remove pointer or array type from TYPE. */
5917 tree
5918 strip_pointer_or_array_types (tree t)
5920 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5921 t = TREE_TYPE (t);
5922 return t;
5925 /* Used to compare case labels. K1 and K2 are actually tree nodes
5926 representing case labels, or NULL_TREE for a `default' label.
5927 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5928 K2, and 0 if K1 and K2 are equal. */
5931 case_compare (splay_tree_key k1, splay_tree_key k2)
5933 /* Consider a NULL key (such as arises with a `default' label) to be
5934 smaller than anything else. */
5935 if (!k1)
5936 return k2 ? -1 : 0;
5937 else if (!k2)
5938 return k1 ? 1 : 0;
5940 return tree_int_cst_compare ((tree) k1, (tree) k2);
5943 /* Process a case label, located at LOC, for the range LOW_VALUE
5944 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5945 then this case label is actually a `default' label. If only
5946 HIGH_VALUE is NULL_TREE, then case label was declared using the
5947 usual C/C++ syntax, rather than the GNU case range extension.
5948 CASES is a tree containing all the case ranges processed so far;
5949 COND is the condition for the switch-statement itself. Returns the
5950 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5951 is created. */
5953 tree
5954 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5955 tree low_value, tree high_value)
5957 tree type;
5958 tree label;
5959 tree case_label;
5960 splay_tree_node node;
5962 /* Create the LABEL_DECL itself. */
5963 label = create_artificial_label (loc);
5965 /* If there was an error processing the switch condition, bail now
5966 before we get more confused. */
5967 if (!cond || cond == error_mark_node)
5968 goto error_out;
5970 if ((low_value && TREE_TYPE (low_value)
5971 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5972 || (high_value && TREE_TYPE (high_value)
5973 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5975 error_at (loc, "pointers are not permitted as case values");
5976 goto error_out;
5979 /* Case ranges are a GNU extension. */
5980 if (high_value)
5981 pedwarn (loc, OPT_Wpedantic,
5982 "range expressions in switch statements are non-standard");
5984 type = TREE_TYPE (cond);
5985 if (low_value)
5987 low_value = check_case_value (low_value);
5988 low_value = convert_and_check (loc, type, low_value);
5989 if (low_value == error_mark_node)
5990 goto error_out;
5992 if (high_value)
5994 high_value = check_case_value (high_value);
5995 high_value = convert_and_check (loc, type, high_value);
5996 if (high_value == error_mark_node)
5997 goto error_out;
6000 if (low_value && high_value)
6002 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6003 really a case range, even though it was written that way.
6004 Remove the HIGH_VALUE to simplify later processing. */
6005 if (tree_int_cst_equal (low_value, high_value))
6006 high_value = NULL_TREE;
6007 else if (!tree_int_cst_lt (low_value, high_value))
6008 warning_at (loc, 0, "empty range specified");
6011 /* See if the case is in range of the type of the original testing
6012 expression. If both low_value and high_value are out of range,
6013 don't insert the case label and return NULL_TREE. */
6014 if (low_value
6015 && !check_case_bounds (type, orig_type,
6016 &low_value, high_value ? &high_value : NULL))
6017 return NULL_TREE;
6019 /* Look up the LOW_VALUE in the table of case labels we already
6020 have. */
6021 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6022 /* If there was not an exact match, check for overlapping ranges.
6023 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6024 that's a `default' label and the only overlap is an exact match. */
6025 if (!node && (low_value || high_value))
6027 splay_tree_node low_bound;
6028 splay_tree_node high_bound;
6030 /* Even though there wasn't an exact match, there might be an
6031 overlap between this case range and another case range.
6032 Since we've (inductively) not allowed any overlapping case
6033 ranges, we simply need to find the greatest low case label
6034 that is smaller that LOW_VALUE, and the smallest low case
6035 label that is greater than LOW_VALUE. If there is an overlap
6036 it will occur in one of these two ranges. */
6037 low_bound = splay_tree_predecessor (cases,
6038 (splay_tree_key) low_value);
6039 high_bound = splay_tree_successor (cases,
6040 (splay_tree_key) low_value);
6042 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6043 the LOW_VALUE, so there is no need to check unless the
6044 LOW_BOUND is in fact itself a case range. */
6045 if (low_bound
6046 && CASE_HIGH ((tree) low_bound->value)
6047 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6048 low_value) >= 0)
6049 node = low_bound;
6050 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6051 range is bigger than the low end of the current range, so we
6052 are only interested if the current range is a real range, and
6053 not an ordinary case label. */
6054 else if (high_bound
6055 && high_value
6056 && (tree_int_cst_compare ((tree) high_bound->key,
6057 high_value)
6058 <= 0))
6059 node = high_bound;
6061 /* If there was an overlap, issue an error. */
6062 if (node)
6064 tree duplicate = CASE_LABEL ((tree) node->value);
6066 if (high_value)
6068 error_at (loc, "duplicate (or overlapping) case value");
6069 error_at (DECL_SOURCE_LOCATION (duplicate),
6070 "this is the first entry overlapping that value");
6072 else if (low_value)
6074 error_at (loc, "duplicate case value") ;
6075 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6077 else
6079 error_at (loc, "multiple default labels in one switch");
6080 error_at (DECL_SOURCE_LOCATION (duplicate),
6081 "this is the first default label");
6083 goto error_out;
6086 /* Add a CASE_LABEL to the statement-tree. */
6087 case_label = add_stmt (build_case_label (low_value, high_value, label));
6088 /* Register this case label in the splay tree. */
6089 splay_tree_insert (cases,
6090 (splay_tree_key) low_value,
6091 (splay_tree_value) case_label);
6093 return case_label;
6095 error_out:
6096 /* Add a label so that the back-end doesn't think that the beginning of
6097 the switch is unreachable. Note that we do not add a case label, as
6098 that just leads to duplicates and thence to failure later on. */
6099 if (!cases->root)
6101 tree t = create_artificial_label (loc);
6102 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6104 return error_mark_node;
6107 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6108 Used to verify that case values match up with enumerator values. */
6110 static void
6111 match_case_to_enum_1 (tree key, tree type, tree label)
6113 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6115 if (tree_fits_uhwi_p (key))
6116 print_dec (key, buf, UNSIGNED);
6117 else if (tree_fits_shwi_p (key))
6118 print_dec (key, buf, SIGNED);
6119 else
6120 print_hex (key, buf);
6122 if (TYPE_NAME (type) == 0)
6123 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6124 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6125 "case value %qs not in enumerated type",
6126 buf);
6127 else
6128 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6129 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6130 "case value %qs not in enumerated type %qT",
6131 buf, type);
6134 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6135 Used to verify that case values match up with enumerator values. */
6137 static int
6138 match_case_to_enum (splay_tree_node node, void *data)
6140 tree label = (tree) node->value;
6141 tree type = (tree) data;
6143 /* Skip default case. */
6144 if (!CASE_LOW (label))
6145 return 0;
6147 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6148 when we did our enum->case scan. Reset our scratch bit after. */
6149 if (!CASE_LOW_SEEN (label))
6150 match_case_to_enum_1 (CASE_LOW (label), type, label);
6151 else
6152 CASE_LOW_SEEN (label) = 0;
6154 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6155 not set, that means that CASE_HIGH did not appear when we did our
6156 enum->case scan. Reset our scratch bit after. */
6157 if (CASE_HIGH (label))
6159 if (!CASE_HIGH_SEEN (label))
6160 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6161 else
6162 CASE_HIGH_SEEN (label) = 0;
6165 return 0;
6168 /* Handle -Wswitch*. Called from the front end after parsing the
6169 switch construct. */
6170 /* ??? Should probably be somewhere generic, since other languages
6171 besides C and C++ would want this. At the moment, however, C/C++
6172 are the only tree-ssa languages that support enumerations at all,
6173 so the point is moot. */
6175 void
6176 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6177 tree type, tree cond)
6179 splay_tree_node default_node;
6180 splay_tree_node node;
6181 tree chain;
6183 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6184 return;
6186 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6187 if (!default_node)
6188 warning_at (switch_location, OPT_Wswitch_default,
6189 "switch missing default case");
6191 /* From here on, we only care about about enumerated types. */
6192 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6193 return;
6195 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6196 if (!warn_switch_enum && !warn_switch)
6197 return;
6199 /* Check the cases. Warn about case values which are not members of
6200 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6201 there is no default case, check that exactly all enumeration
6202 literals are covered by the cases. */
6204 /* Clearing COND if it is not an integer constant simplifies
6205 the tests inside the loop below. */
6206 if (TREE_CODE (cond) != INTEGER_CST)
6207 cond = NULL_TREE;
6209 /* The time complexity here is O(N*lg(N)) worst case, but for the
6210 common case of monotonically increasing enumerators, it is
6211 O(N), since the nature of the splay tree will keep the next
6212 element adjacent to the root at all times. */
6214 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6216 tree value = TREE_VALUE (chain);
6217 if (TREE_CODE (value) == CONST_DECL)
6218 value = DECL_INITIAL (value);
6219 node = splay_tree_lookup (cases, (splay_tree_key) value);
6220 if (node)
6222 /* Mark the CASE_LOW part of the case entry as seen. */
6223 tree label = (tree) node->value;
6224 CASE_LOW_SEEN (label) = 1;
6225 continue;
6228 /* Even though there wasn't an exact match, there might be a
6229 case range which includes the enumerator's value. */
6230 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6231 if (node && CASE_HIGH ((tree) node->value))
6233 tree label = (tree) node->value;
6234 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6235 if (cmp >= 0)
6237 /* If we match the upper bound exactly, mark the CASE_HIGH
6238 part of the case entry as seen. */
6239 if (cmp == 0)
6240 CASE_HIGH_SEEN (label) = 1;
6241 continue;
6245 /* We've now determined that this enumerated literal isn't
6246 handled by the case labels of the switch statement. */
6248 /* If the switch expression is a constant, we only really care
6249 about whether that constant is handled by the switch. */
6250 if (cond && tree_int_cst_compare (cond, value))
6251 continue;
6253 /* If there is a default_node, the only relevant option is
6254 Wswitch-enum. Otherwise, if both are enabled then we prefer
6255 to warn using -Wswitch because -Wswitch is enabled by -Wall
6256 while -Wswitch-enum is explicit. */
6257 warning_at (switch_location,
6258 (default_node || !warn_switch
6259 ? OPT_Wswitch_enum
6260 : OPT_Wswitch),
6261 "enumeration value %qE not handled in switch",
6262 TREE_PURPOSE (chain));
6265 /* Warn if there are case expressions that don't correspond to
6266 enumerators. This can occur since C and C++ don't enforce
6267 type-checking of assignments to enumeration variables.
6269 The time complexity here is now always O(N) worst case, since
6270 we should have marked both the lower bound and upper bound of
6271 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6272 above. This scan also resets those fields. */
6274 splay_tree_foreach (cases, match_case_to_enum, type);
6277 /* Finish an expression taking the address of LABEL (an
6278 IDENTIFIER_NODE). Returns an expression for the address.
6280 LOC is the location for the expression returned. */
6282 tree
6283 finish_label_address_expr (tree label, location_t loc)
6285 tree result;
6287 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6289 if (label == error_mark_node)
6290 return error_mark_node;
6292 label = lookup_label (label);
6293 if (label == NULL_TREE)
6294 result = null_pointer_node;
6295 else
6297 TREE_USED (label) = 1;
6298 result = build1 (ADDR_EXPR, ptr_type_node, label);
6299 /* The current function is not necessarily uninlinable.
6300 Computed gotos are incompatible with inlining, but the value
6301 here could be used only in a diagnostic, for example. */
6302 protected_set_expr_location (result, loc);
6305 return result;
6309 /* Given a boolean expression ARG, return a tree representing an increment
6310 or decrement (as indicated by CODE) of ARG. The front end must check for
6311 invalid cases (e.g., decrement in C++). */
6312 tree
6313 boolean_increment (enum tree_code code, tree arg)
6315 tree val;
6316 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6318 arg = stabilize_reference (arg);
6319 switch (code)
6321 case PREINCREMENT_EXPR:
6322 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6323 break;
6324 case POSTINCREMENT_EXPR:
6325 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6326 arg = save_expr (arg);
6327 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6328 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6329 break;
6330 case PREDECREMENT_EXPR:
6331 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6332 invert_truthvalue_loc (input_location, arg));
6333 break;
6334 case POSTDECREMENT_EXPR:
6335 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6336 invert_truthvalue_loc (input_location, arg));
6337 arg = save_expr (arg);
6338 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6339 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6340 break;
6341 default:
6342 gcc_unreachable ();
6344 TREE_SIDE_EFFECTS (val) = 1;
6345 return val;
6348 /* Built-in macros for stddef.h and stdint.h, that require macros
6349 defined in this file. */
6350 void
6351 c_stddef_cpp_builtins(void)
6353 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6354 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6355 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6356 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6357 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6358 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6359 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6360 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6361 if (SIG_ATOMIC_TYPE)
6362 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6363 if (INT8_TYPE)
6364 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6365 if (INT16_TYPE)
6366 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6367 if (INT32_TYPE)
6368 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6369 if (INT64_TYPE)
6370 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6371 if (UINT8_TYPE)
6372 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6373 if (UINT16_TYPE)
6374 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6375 if (UINT32_TYPE)
6376 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6377 if (UINT64_TYPE)
6378 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6379 if (INT_LEAST8_TYPE)
6380 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6381 if (INT_LEAST16_TYPE)
6382 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6383 if (INT_LEAST32_TYPE)
6384 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6385 if (INT_LEAST64_TYPE)
6386 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6387 if (UINT_LEAST8_TYPE)
6388 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6389 if (UINT_LEAST16_TYPE)
6390 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6391 if (UINT_LEAST32_TYPE)
6392 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6393 if (UINT_LEAST64_TYPE)
6394 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6395 if (INT_FAST8_TYPE)
6396 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6397 if (INT_FAST16_TYPE)
6398 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6399 if (INT_FAST32_TYPE)
6400 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6401 if (INT_FAST64_TYPE)
6402 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6403 if (UINT_FAST8_TYPE)
6404 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6405 if (UINT_FAST16_TYPE)
6406 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6407 if (UINT_FAST32_TYPE)
6408 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6409 if (UINT_FAST64_TYPE)
6410 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6411 if (INTPTR_TYPE)
6412 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6413 if (UINTPTR_TYPE)
6414 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6417 static void
6418 c_init_attributes (void)
6420 /* Fill in the built_in_attributes array. */
6421 #define DEF_ATTR_NULL_TREE(ENUM) \
6422 built_in_attributes[(int) ENUM] = NULL_TREE;
6423 #define DEF_ATTR_INT(ENUM, VALUE) \
6424 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6425 #define DEF_ATTR_STRING(ENUM, VALUE) \
6426 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6427 #define DEF_ATTR_IDENT(ENUM, STRING) \
6428 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6429 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6430 built_in_attributes[(int) ENUM] \
6431 = tree_cons (built_in_attributes[(int) PURPOSE], \
6432 built_in_attributes[(int) VALUE], \
6433 built_in_attributes[(int) CHAIN]);
6434 #include "builtin-attrs.def"
6435 #undef DEF_ATTR_NULL_TREE
6436 #undef DEF_ATTR_INT
6437 #undef DEF_ATTR_IDENT
6438 #undef DEF_ATTR_TREE_LIST
6441 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6442 identifier as an argument, so the front end shouldn't look it up. */
6444 bool
6445 attribute_takes_identifier_p (const_tree attr_id)
6447 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6448 if (spec == NULL)
6449 /* Unknown attribute that we'll end up ignoring, return true so we
6450 don't complain about an identifier argument. */
6451 return true;
6452 else if (!strcmp ("mode", spec->name)
6453 || !strcmp ("format", spec->name)
6454 || !strcmp ("cleanup", spec->name))
6455 return true;
6456 else
6457 return targetm.attribute_takes_identifier_p (attr_id);
6460 /* Attribute handlers common to C front ends. */
6462 /* Handle a "packed" attribute; arguments as in
6463 struct attribute_spec.handler. */
6465 static tree
6466 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6467 int flags, bool *no_add_attrs)
6469 if (TYPE_P (*node))
6471 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6472 *node = build_variant_type_copy (*node);
6473 TYPE_PACKED (*node) = 1;
6475 else if (TREE_CODE (*node) == FIELD_DECL)
6477 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6478 /* Still pack bitfields. */
6479 && ! DECL_INITIAL (*node))
6480 warning (OPT_Wattributes,
6481 "%qE attribute ignored for field of type %qT",
6482 name, TREE_TYPE (*node));
6483 else
6484 DECL_PACKED (*node) = 1;
6486 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6487 used for DECL_REGISTER. It wouldn't mean anything anyway.
6488 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6489 that changes what the typedef is typing. */
6490 else
6492 warning (OPT_Wattributes, "%qE attribute ignored", name);
6493 *no_add_attrs = true;
6496 return NULL_TREE;
6499 /* Handle a "nocommon" attribute; arguments as in
6500 struct attribute_spec.handler. */
6502 static tree
6503 handle_nocommon_attribute (tree *node, tree name,
6504 tree ARG_UNUSED (args),
6505 int ARG_UNUSED (flags), bool *no_add_attrs)
6507 if (TREE_CODE (*node) == VAR_DECL)
6508 DECL_COMMON (*node) = 0;
6509 else
6511 warning (OPT_Wattributes, "%qE attribute ignored", name);
6512 *no_add_attrs = true;
6515 return NULL_TREE;
6518 /* Handle a "common" attribute; arguments as in
6519 struct attribute_spec.handler. */
6521 static tree
6522 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6523 int ARG_UNUSED (flags), bool *no_add_attrs)
6525 if (TREE_CODE (*node) == VAR_DECL)
6526 DECL_COMMON (*node) = 1;
6527 else
6529 warning (OPT_Wattributes, "%qE attribute ignored", name);
6530 *no_add_attrs = true;
6533 return NULL_TREE;
6536 /* Handle a "noreturn" attribute; arguments as in
6537 struct attribute_spec.handler. */
6539 static tree
6540 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6541 int ARG_UNUSED (flags), bool *no_add_attrs)
6543 tree type = TREE_TYPE (*node);
6545 /* See FIXME comment in c_common_attribute_table. */
6546 if (TREE_CODE (*node) == FUNCTION_DECL
6547 || objc_method_decl (TREE_CODE (*node)))
6548 TREE_THIS_VOLATILE (*node) = 1;
6549 else if (TREE_CODE (type) == POINTER_TYPE
6550 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6551 TREE_TYPE (*node)
6552 = build_pointer_type
6553 (build_type_variant (TREE_TYPE (type),
6554 TYPE_READONLY (TREE_TYPE (type)), 1));
6555 else
6557 warning (OPT_Wattributes, "%qE attribute ignored", name);
6558 *no_add_attrs = true;
6561 return NULL_TREE;
6564 /* Handle a "hot" and attribute; arguments as in
6565 struct attribute_spec.handler. */
6567 static tree
6568 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6569 int ARG_UNUSED (flags), bool *no_add_attrs)
6571 if (TREE_CODE (*node) == FUNCTION_DECL
6572 || TREE_CODE (*node) == LABEL_DECL)
6574 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6576 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6577 "with attribute %qs", name, "cold");
6578 *no_add_attrs = true;
6580 /* Most of the rest of the hot processing is done later with
6581 lookup_attribute. */
6583 else
6585 warning (OPT_Wattributes, "%qE attribute ignored", name);
6586 *no_add_attrs = true;
6589 return NULL_TREE;
6592 /* Handle a "cold" and attribute; arguments as in
6593 struct attribute_spec.handler. */
6595 static tree
6596 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6597 int ARG_UNUSED (flags), bool *no_add_attrs)
6599 if (TREE_CODE (*node) == FUNCTION_DECL
6600 || TREE_CODE (*node) == LABEL_DECL)
6602 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6604 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6605 "with attribute %qs", name, "hot");
6606 *no_add_attrs = true;
6608 /* Most of the rest of the cold processing is done later with
6609 lookup_attribute. */
6611 else
6613 warning (OPT_Wattributes, "%qE attribute ignored", name);
6614 *no_add_attrs = true;
6617 return NULL_TREE;
6620 /* Handle a "no_sanitize_address" attribute; arguments as in
6621 struct attribute_spec.handler. */
6623 static tree
6624 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6625 bool *no_add_attrs)
6627 if (TREE_CODE (*node) != FUNCTION_DECL)
6629 warning (OPT_Wattributes, "%qE attribute ignored", name);
6630 *no_add_attrs = true;
6633 return NULL_TREE;
6636 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6637 struct attribute_spec.handler. */
6639 static tree
6640 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6641 bool *no_add_attrs)
6643 if (TREE_CODE (*node) != FUNCTION_DECL)
6644 warning (OPT_Wattributes, "%qE attribute ignored", name);
6645 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6646 DECL_ATTRIBUTES (*node)
6647 = tree_cons (get_identifier ("no_sanitize_address"),
6648 NULL_TREE, DECL_ATTRIBUTES (*node));
6649 *no_add_attrs = true;
6650 return NULL_TREE;
6653 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6654 struct attribute_spec.handler. */
6656 static tree
6657 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6658 bool *no_add_attrs)
6660 if (TREE_CODE (*node) != FUNCTION_DECL)
6662 warning (OPT_Wattributes, "%qE attribute ignored", name);
6663 *no_add_attrs = true;
6666 return NULL_TREE;
6669 /* Handle a "noinline" attribute; arguments as in
6670 struct attribute_spec.handler. */
6672 static tree
6673 handle_noinline_attribute (tree *node, tree name,
6674 tree ARG_UNUSED (args),
6675 int ARG_UNUSED (flags), bool *no_add_attrs)
6677 if (TREE_CODE (*node) == FUNCTION_DECL)
6679 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6681 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6682 "with attribute %qs", name, "always_inline");
6683 *no_add_attrs = true;
6685 else
6686 DECL_UNINLINABLE (*node) = 1;
6688 else
6690 warning (OPT_Wattributes, "%qE attribute ignored", name);
6691 *no_add_attrs = true;
6694 return NULL_TREE;
6697 /* Handle a "noclone" attribute; arguments as in
6698 struct attribute_spec.handler. */
6700 static tree
6701 handle_noclone_attribute (tree *node, tree name,
6702 tree ARG_UNUSED (args),
6703 int ARG_UNUSED (flags), bool *no_add_attrs)
6705 if (TREE_CODE (*node) != FUNCTION_DECL)
6707 warning (OPT_Wattributes, "%qE attribute ignored", name);
6708 *no_add_attrs = true;
6711 return NULL_TREE;
6714 /* Handle a "always_inline" attribute; arguments as in
6715 struct attribute_spec.handler. */
6717 static tree
6718 handle_always_inline_attribute (tree *node, tree name,
6719 tree ARG_UNUSED (args),
6720 int ARG_UNUSED (flags),
6721 bool *no_add_attrs)
6723 if (TREE_CODE (*node) == FUNCTION_DECL)
6725 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6727 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6728 "with %qs attribute", name, "noinline");
6729 *no_add_attrs = true;
6731 else
6732 /* Set the attribute and mark it for disregarding inline
6733 limits. */
6734 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6736 else
6738 warning (OPT_Wattributes, "%qE attribute ignored", name);
6739 *no_add_attrs = true;
6742 return NULL_TREE;
6745 /* Handle a "gnu_inline" attribute; arguments as in
6746 struct attribute_spec.handler. */
6748 static tree
6749 handle_gnu_inline_attribute (tree *node, tree name,
6750 tree ARG_UNUSED (args),
6751 int ARG_UNUSED (flags),
6752 bool *no_add_attrs)
6754 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6756 /* Do nothing else, just set the attribute. We'll get at
6757 it later with lookup_attribute. */
6759 else
6761 warning (OPT_Wattributes, "%qE attribute ignored", name);
6762 *no_add_attrs = true;
6765 return NULL_TREE;
6768 /* Handle a "leaf" attribute; arguments as in
6769 struct attribute_spec.handler. */
6771 static tree
6772 handle_leaf_attribute (tree *node, tree name,
6773 tree ARG_UNUSED (args),
6774 int ARG_UNUSED (flags), bool *no_add_attrs)
6776 if (TREE_CODE (*node) != FUNCTION_DECL)
6778 warning (OPT_Wattributes, "%qE attribute ignored", name);
6779 *no_add_attrs = true;
6781 if (!TREE_PUBLIC (*node))
6783 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6784 *no_add_attrs = true;
6787 return NULL_TREE;
6790 /* Handle an "artificial" attribute; arguments as in
6791 struct attribute_spec.handler. */
6793 static tree
6794 handle_artificial_attribute (tree *node, tree name,
6795 tree ARG_UNUSED (args),
6796 int ARG_UNUSED (flags),
6797 bool *no_add_attrs)
6799 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6801 /* Do nothing else, just set the attribute. We'll get at
6802 it later with lookup_attribute. */
6804 else
6806 warning (OPT_Wattributes, "%qE attribute ignored", name);
6807 *no_add_attrs = true;
6810 return NULL_TREE;
6813 /* Handle a "flatten" attribute; arguments as in
6814 struct attribute_spec.handler. */
6816 static tree
6817 handle_flatten_attribute (tree *node, tree name,
6818 tree args ATTRIBUTE_UNUSED,
6819 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6821 if (TREE_CODE (*node) == FUNCTION_DECL)
6822 /* Do nothing else, just set the attribute. We'll get at
6823 it later with lookup_attribute. */
6825 else
6827 warning (OPT_Wattributes, "%qE attribute ignored", name);
6828 *no_add_attrs = true;
6831 return NULL_TREE;
6834 /* Handle a "warning" or "error" attribute; arguments as in
6835 struct attribute_spec.handler. */
6837 static tree
6838 handle_error_attribute (tree *node, tree name, tree args,
6839 int ARG_UNUSED (flags), bool *no_add_attrs)
6841 if (TREE_CODE (*node) == FUNCTION_DECL
6842 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6843 /* Do nothing else, just set the attribute. We'll get at
6844 it later with lookup_attribute. */
6846 else
6848 warning (OPT_Wattributes, "%qE attribute ignored", name);
6849 *no_add_attrs = true;
6852 return NULL_TREE;
6855 /* Handle a "used" attribute; arguments as in
6856 struct attribute_spec.handler. */
6858 static tree
6859 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6860 int ARG_UNUSED (flags), bool *no_add_attrs)
6862 tree node = *pnode;
6864 if (TREE_CODE (node) == FUNCTION_DECL
6865 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6866 || (TREE_CODE (node) == TYPE_DECL))
6868 TREE_USED (node) = 1;
6869 DECL_PRESERVE_P (node) = 1;
6870 if (TREE_CODE (node) == VAR_DECL)
6871 DECL_READ_P (node) = 1;
6873 else
6875 warning (OPT_Wattributes, "%qE attribute ignored", name);
6876 *no_add_attrs = true;
6879 return NULL_TREE;
6882 /* Handle a "unused" attribute; arguments as in
6883 struct attribute_spec.handler. */
6885 static tree
6886 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6887 int flags, bool *no_add_attrs)
6889 if (DECL_P (*node))
6891 tree decl = *node;
6893 if (TREE_CODE (decl) == PARM_DECL
6894 || TREE_CODE (decl) == VAR_DECL
6895 || TREE_CODE (decl) == FUNCTION_DECL
6896 || TREE_CODE (decl) == LABEL_DECL
6897 || TREE_CODE (decl) == TYPE_DECL)
6899 TREE_USED (decl) = 1;
6900 if (TREE_CODE (decl) == VAR_DECL
6901 || TREE_CODE (decl) == PARM_DECL)
6902 DECL_READ_P (decl) = 1;
6904 else
6906 warning (OPT_Wattributes, "%qE attribute ignored", name);
6907 *no_add_attrs = true;
6910 else
6912 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6913 *node = build_variant_type_copy (*node);
6914 TREE_USED (*node) = 1;
6917 return NULL_TREE;
6920 /* Handle a "externally_visible" attribute; arguments as in
6921 struct attribute_spec.handler. */
6923 static tree
6924 handle_externally_visible_attribute (tree *pnode, tree name,
6925 tree ARG_UNUSED (args),
6926 int ARG_UNUSED (flags),
6927 bool *no_add_attrs)
6929 tree node = *pnode;
6931 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6933 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6934 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6936 warning (OPT_Wattributes,
6937 "%qE attribute have effect only on public objects", name);
6938 *no_add_attrs = true;
6941 else
6943 warning (OPT_Wattributes, "%qE attribute ignored", name);
6944 *no_add_attrs = true;
6947 return NULL_TREE;
6950 /* Handle a "const" attribute; arguments as in
6951 struct attribute_spec.handler. */
6953 static tree
6954 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6955 int ARG_UNUSED (flags), bool *no_add_attrs)
6957 tree type = TREE_TYPE (*node);
6959 /* See FIXME comment on noreturn in c_common_attribute_table. */
6960 if (TREE_CODE (*node) == FUNCTION_DECL)
6961 TREE_READONLY (*node) = 1;
6962 else if (TREE_CODE (type) == POINTER_TYPE
6963 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6964 TREE_TYPE (*node)
6965 = build_pointer_type
6966 (build_type_variant (TREE_TYPE (type), 1,
6967 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6968 else
6970 warning (OPT_Wattributes, "%qE attribute ignored", name);
6971 *no_add_attrs = true;
6974 return NULL_TREE;
6977 /* Handle a "transparent_union" attribute; arguments as in
6978 struct attribute_spec.handler. */
6980 static tree
6981 handle_transparent_union_attribute (tree *node, tree name,
6982 tree ARG_UNUSED (args), int flags,
6983 bool *no_add_attrs)
6985 tree type;
6987 *no_add_attrs = true;
6990 if (TREE_CODE (*node) == TYPE_DECL
6991 && ! (flags & ATTR_FLAG_CXX11))
6992 node = &TREE_TYPE (*node);
6993 type = *node;
6995 if (TREE_CODE (type) == UNION_TYPE)
6997 /* Make sure that the first field will work for a transparent union.
6998 If the type isn't complete yet, leave the check to the code in
6999 finish_struct. */
7000 if (TYPE_SIZE (type))
7002 tree first = first_field (type);
7003 if (first == NULL_TREE
7004 || DECL_ARTIFICIAL (first)
7005 || TYPE_MODE (type) != DECL_MODE (first))
7006 goto ignored;
7009 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7011 /* If the type isn't complete yet, setting the flag
7012 on a variant wouldn't ever be checked. */
7013 if (!TYPE_SIZE (type))
7014 goto ignored;
7016 /* build_duplicate_type doesn't work for C++. */
7017 if (c_dialect_cxx ())
7018 goto ignored;
7020 /* A type variant isn't good enough, since we don't a cast
7021 to such a type removed as a no-op. */
7022 *node = type = build_duplicate_type (type);
7025 TYPE_TRANSPARENT_AGGR (type) = 1;
7026 return NULL_TREE;
7029 ignored:
7030 warning (OPT_Wattributes, "%qE attribute ignored", name);
7031 return NULL_TREE;
7034 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7035 get the requested priority for a constructor or destructor,
7036 possibly issuing diagnostics for invalid or reserved
7037 priorities. */
7039 static priority_type
7040 get_priority (tree args, bool is_destructor)
7042 HOST_WIDE_INT pri;
7043 tree arg;
7045 if (!args)
7046 return DEFAULT_INIT_PRIORITY;
7048 if (!SUPPORTS_INIT_PRIORITY)
7050 if (is_destructor)
7051 error ("destructor priorities are not supported");
7052 else
7053 error ("constructor priorities are not supported");
7054 return DEFAULT_INIT_PRIORITY;
7057 arg = TREE_VALUE (args);
7058 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7059 goto invalid;
7060 if (arg == error_mark_node)
7061 return DEFAULT_INIT_PRIORITY;
7062 arg = default_conversion (arg);
7063 if (!tree_fits_shwi_p (arg)
7064 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7065 goto invalid;
7067 pri = tree_to_shwi (arg);
7068 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7069 goto invalid;
7071 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7073 if (is_destructor)
7074 warning (0,
7075 "destructor priorities from 0 to %d are reserved "
7076 "for the implementation",
7077 MAX_RESERVED_INIT_PRIORITY);
7078 else
7079 warning (0,
7080 "constructor priorities from 0 to %d are reserved "
7081 "for the implementation",
7082 MAX_RESERVED_INIT_PRIORITY);
7084 return pri;
7086 invalid:
7087 if (is_destructor)
7088 error ("destructor priorities must be integers from 0 to %d inclusive",
7089 MAX_INIT_PRIORITY);
7090 else
7091 error ("constructor priorities must be integers from 0 to %d inclusive",
7092 MAX_INIT_PRIORITY);
7093 return DEFAULT_INIT_PRIORITY;
7096 /* Handle a "constructor" attribute; arguments as in
7097 struct attribute_spec.handler. */
7099 static tree
7100 handle_constructor_attribute (tree *node, tree name, tree args,
7101 int ARG_UNUSED (flags),
7102 bool *no_add_attrs)
7104 tree decl = *node;
7105 tree type = TREE_TYPE (decl);
7107 if (TREE_CODE (decl) == FUNCTION_DECL
7108 && TREE_CODE (type) == FUNCTION_TYPE
7109 && decl_function_context (decl) == 0)
7111 priority_type priority;
7112 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7113 priority = get_priority (args, /*is_destructor=*/false);
7114 SET_DECL_INIT_PRIORITY (decl, priority);
7115 TREE_USED (decl) = 1;
7117 else
7119 warning (OPT_Wattributes, "%qE attribute ignored", name);
7120 *no_add_attrs = true;
7123 return NULL_TREE;
7126 /* Handle a "destructor" attribute; arguments as in
7127 struct attribute_spec.handler. */
7129 static tree
7130 handle_destructor_attribute (tree *node, tree name, tree args,
7131 int ARG_UNUSED (flags),
7132 bool *no_add_attrs)
7134 tree decl = *node;
7135 tree type = TREE_TYPE (decl);
7137 if (TREE_CODE (decl) == FUNCTION_DECL
7138 && TREE_CODE (type) == FUNCTION_TYPE
7139 && decl_function_context (decl) == 0)
7141 priority_type priority;
7142 DECL_STATIC_DESTRUCTOR (decl) = 1;
7143 priority = get_priority (args, /*is_destructor=*/true);
7144 SET_DECL_FINI_PRIORITY (decl, priority);
7145 TREE_USED (decl) = 1;
7147 else
7149 warning (OPT_Wattributes, "%qE attribute ignored", name);
7150 *no_add_attrs = true;
7153 return NULL_TREE;
7156 /* Nonzero if the mode is a valid vector mode for this architecture.
7157 This returns nonzero even if there is no hardware support for the
7158 vector mode, but we can emulate with narrower modes. */
7160 static int
7161 vector_mode_valid_p (enum machine_mode mode)
7163 enum mode_class mclass = GET_MODE_CLASS (mode);
7164 enum machine_mode innermode;
7166 /* Doh! What's going on? */
7167 if (mclass != MODE_VECTOR_INT
7168 && mclass != MODE_VECTOR_FLOAT
7169 && mclass != MODE_VECTOR_FRACT
7170 && mclass != MODE_VECTOR_UFRACT
7171 && mclass != MODE_VECTOR_ACCUM
7172 && mclass != MODE_VECTOR_UACCUM)
7173 return 0;
7175 /* Hardware support. Woo hoo! */
7176 if (targetm.vector_mode_supported_p (mode))
7177 return 1;
7179 innermode = GET_MODE_INNER (mode);
7181 /* We should probably return 1 if requesting V4DI and we have no DI,
7182 but we have V2DI, but this is probably very unlikely. */
7184 /* If we have support for the inner mode, we can safely emulate it.
7185 We may not have V2DI, but me can emulate with a pair of DIs. */
7186 return targetm.scalar_mode_supported_p (innermode);
7190 /* Handle a "mode" attribute; arguments as in
7191 struct attribute_spec.handler. */
7193 static tree
7194 handle_mode_attribute (tree *node, tree name, tree args,
7195 int ARG_UNUSED (flags), bool *no_add_attrs)
7197 tree type = *node;
7198 tree ident = TREE_VALUE (args);
7200 *no_add_attrs = true;
7202 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7203 warning (OPT_Wattributes, "%qE attribute ignored", name);
7204 else
7206 int j;
7207 const char *p = IDENTIFIER_POINTER (ident);
7208 int len = strlen (p);
7209 enum machine_mode mode = VOIDmode;
7210 tree typefm;
7211 bool valid_mode;
7213 if (len > 4 && p[0] == '_' && p[1] == '_'
7214 && p[len - 1] == '_' && p[len - 2] == '_')
7216 char *newp = (char *) alloca (len - 1);
7218 strcpy (newp, &p[2]);
7219 newp[len - 4] = '\0';
7220 p = newp;
7223 /* Change this type to have a type with the specified mode.
7224 First check for the special modes. */
7225 if (!strcmp (p, "byte"))
7226 mode = byte_mode;
7227 else if (!strcmp (p, "word"))
7228 mode = word_mode;
7229 else if (!strcmp (p, "pointer"))
7230 mode = ptr_mode;
7231 else if (!strcmp (p, "libgcc_cmp_return"))
7232 mode = targetm.libgcc_cmp_return_mode ();
7233 else if (!strcmp (p, "libgcc_shift_count"))
7234 mode = targetm.libgcc_shift_count_mode ();
7235 else if (!strcmp (p, "unwind_word"))
7236 mode = targetm.unwind_word_mode ();
7237 else
7238 for (j = 0; j < NUM_MACHINE_MODES; j++)
7239 if (!strcmp (p, GET_MODE_NAME (j)))
7241 mode = (enum machine_mode) j;
7242 break;
7245 if (mode == VOIDmode)
7247 error ("unknown machine mode %qE", ident);
7248 return NULL_TREE;
7251 valid_mode = false;
7252 switch (GET_MODE_CLASS (mode))
7254 case MODE_INT:
7255 case MODE_PARTIAL_INT:
7256 case MODE_FLOAT:
7257 case MODE_DECIMAL_FLOAT:
7258 case MODE_FRACT:
7259 case MODE_UFRACT:
7260 case MODE_ACCUM:
7261 case MODE_UACCUM:
7262 valid_mode = targetm.scalar_mode_supported_p (mode);
7263 break;
7265 case MODE_COMPLEX_INT:
7266 case MODE_COMPLEX_FLOAT:
7267 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7268 break;
7270 case MODE_VECTOR_INT:
7271 case MODE_VECTOR_FLOAT:
7272 case MODE_VECTOR_FRACT:
7273 case MODE_VECTOR_UFRACT:
7274 case MODE_VECTOR_ACCUM:
7275 case MODE_VECTOR_UACCUM:
7276 warning (OPT_Wattributes, "specifying vector types with "
7277 "__attribute__ ((mode)) is deprecated");
7278 warning (OPT_Wattributes,
7279 "use __attribute__ ((vector_size)) instead");
7280 valid_mode = vector_mode_valid_p (mode);
7281 break;
7283 default:
7284 break;
7286 if (!valid_mode)
7288 error ("unable to emulate %qs", p);
7289 return NULL_TREE;
7292 if (POINTER_TYPE_P (type))
7294 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7295 tree (*fn)(tree, enum machine_mode, bool);
7297 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7299 error ("invalid pointer mode %qs", p);
7300 return NULL_TREE;
7303 if (TREE_CODE (type) == POINTER_TYPE)
7304 fn = build_pointer_type_for_mode;
7305 else
7306 fn = build_reference_type_for_mode;
7307 typefm = fn (TREE_TYPE (type), mode, false);
7309 else
7311 /* For fixed-point modes, we need to test if the signness of type
7312 and the machine mode are consistent. */
7313 if (ALL_FIXED_POINT_MODE_P (mode)
7314 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7316 error ("signedness of type and machine mode %qs don%'t match", p);
7317 return NULL_TREE;
7319 /* For fixed-point modes, we need to pass saturating info. */
7320 typefm = lang_hooks.types.type_for_mode (mode,
7321 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7322 : TYPE_UNSIGNED (type));
7325 if (typefm == NULL_TREE)
7327 error ("no data type for mode %qs", p);
7328 return NULL_TREE;
7330 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7332 /* For enumeral types, copy the precision from the integer
7333 type returned above. If not an INTEGER_TYPE, we can't use
7334 this mode for this type. */
7335 if (TREE_CODE (typefm) != INTEGER_TYPE)
7337 error ("cannot use mode %qs for enumeral types", p);
7338 return NULL_TREE;
7341 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7343 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7344 typefm = type;
7346 else
7348 /* We cannot build a type variant, as there's code that assumes
7349 that TYPE_MAIN_VARIANT has the same mode. This includes the
7350 debug generators. Instead, create a subrange type. This
7351 results in all of the enumeral values being emitted only once
7352 in the original, and the subtype gets them by reference. */
7353 if (TYPE_UNSIGNED (type))
7354 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7355 else
7356 typefm = make_signed_type (TYPE_PRECISION (typefm));
7357 TREE_TYPE (typefm) = type;
7360 else if (VECTOR_MODE_P (mode)
7361 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7362 : TREE_CODE (type) != TREE_CODE (typefm))
7364 error ("mode %qs applied to inappropriate type", p);
7365 return NULL_TREE;
7368 *node = typefm;
7371 return NULL_TREE;
7374 /* Handle a "section" attribute; arguments as in
7375 struct attribute_spec.handler. */
7377 static tree
7378 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7379 int ARG_UNUSED (flags), bool *no_add_attrs)
7381 tree decl = *node;
7383 if (targetm_common.have_named_sections)
7385 user_defined_section_attribute = true;
7387 if ((TREE_CODE (decl) == FUNCTION_DECL
7388 || TREE_CODE (decl) == VAR_DECL)
7389 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7391 if (TREE_CODE (decl) == VAR_DECL
7392 && current_function_decl != NULL_TREE
7393 && !TREE_STATIC (decl))
7395 error_at (DECL_SOURCE_LOCATION (decl),
7396 "section attribute cannot be specified for "
7397 "local variables");
7398 *no_add_attrs = true;
7401 /* The decl may have already been given a section attribute
7402 from a previous declaration. Ensure they match. */
7403 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7404 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7405 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7407 error ("section of %q+D conflicts with previous declaration",
7408 *node);
7409 *no_add_attrs = true;
7411 else if (TREE_CODE (decl) == VAR_DECL
7412 && !targetm.have_tls && targetm.emutls.tmpl_section
7413 && DECL_THREAD_LOCAL_P (decl))
7415 error ("section of %q+D cannot be overridden", *node);
7416 *no_add_attrs = true;
7418 else
7419 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7421 else
7423 error ("section attribute not allowed for %q+D", *node);
7424 *no_add_attrs = true;
7427 else
7429 error_at (DECL_SOURCE_LOCATION (*node),
7430 "section attributes are not supported for this target");
7431 *no_add_attrs = true;
7434 return NULL_TREE;
7437 /* Check whether ALIGN is a valid user-specified alignment. If so,
7438 return its base-2 log; if not, output an error and return -1. If
7439 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7440 no error. */
7442 check_user_alignment (const_tree align, bool allow_zero)
7444 int i;
7446 if (TREE_CODE (align) != INTEGER_CST
7447 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7449 error ("requested alignment is not an integer constant");
7450 return -1;
7452 else if (allow_zero && integer_zerop (align))
7453 return -1;
7454 else if (tree_int_cst_sgn (align) == -1
7455 || (i = tree_log2 (align)) == -1)
7457 error ("requested alignment is not a positive power of 2");
7458 return -1;
7460 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7462 error ("requested alignment is too large");
7463 return -1;
7465 return i;
7469 If in c++-11, check if the c++-11 alignment constraint with respect
7470 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7471 c++-11 mode, does nothing.
7473 [dcl.align]2/ says:
7475 [* if the constant expression evaluates to a fundamental alignment,
7476 the alignment requirement of the declared entity shall be the
7477 specified fundamental alignment.
7479 * if the constant expression evaluates to an extended alignment
7480 and the implementation supports that alignment in the context
7481 of the declaration, the alignment of the declared entity shall
7482 be that alignment
7484 * if the constant expression evaluates to an extended alignment
7485 and the implementation does not support that alignment in the
7486 context of the declaration, the program is ill-formed]. */
7488 static bool
7489 check_cxx_fundamental_alignment_constraints (tree node,
7490 unsigned align_log,
7491 int flags)
7493 bool alignment_too_large_p = false;
7494 unsigned requested_alignment = 1U << align_log;
7495 unsigned max_align = 0;
7497 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7498 || (node == NULL_TREE || node == error_mark_node))
7499 return true;
7501 if (cxx_fundamental_alignment_p (requested_alignment))
7502 return true;
7504 if (DECL_P (node))
7506 if (TREE_STATIC (node))
7508 /* For file scope variables and static members, the target
7509 supports alignments that are at most
7510 MAX_OFILE_ALIGNMENT. */
7511 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7512 alignment_too_large_p = true;
7514 else
7516 #ifdef BIGGEST_FIELD_ALIGNMENT
7517 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7518 #else
7519 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7520 #endif
7521 /* For non-static members, the target supports either
7522 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7523 if it is defined or BIGGEST_ALIGNMENT. */
7524 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7525 if (TREE_CODE (node) == FIELD_DECL
7526 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7527 alignment_too_large_p = true;
7528 #undef MAX_TARGET_FIELD_ALIGNMENT
7529 /* For stack variables, the target supports at most
7530 MAX_STACK_ALIGNMENT. */
7531 else if (decl_function_context (node) != NULL
7532 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7533 alignment_too_large_p = true;
7536 else if (TYPE_P (node))
7538 /* Let's be liberal for types. */
7539 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7540 alignment_too_large_p = true;
7543 if (alignment_too_large_p)
7544 pedwarn (input_location, OPT_Wattributes,
7545 "requested alignment %d is larger than %d",
7546 requested_alignment, max_align);
7548 return !alignment_too_large_p;
7551 /* Handle a "aligned" attribute; arguments as in
7552 struct attribute_spec.handler. */
7554 static tree
7555 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7556 int flags, bool *no_add_attrs)
7558 tree decl = NULL_TREE;
7559 tree *type = NULL;
7560 int is_type = 0;
7561 tree align_expr;
7562 int i;
7564 if (args)
7566 align_expr = TREE_VALUE (args);
7567 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE)
7568 align_expr = default_conversion (align_expr);
7570 else
7571 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7573 if (DECL_P (*node))
7575 decl = *node;
7576 type = &TREE_TYPE (decl);
7577 is_type = TREE_CODE (*node) == TYPE_DECL;
7579 else if (TYPE_P (*node))
7580 type = node, is_type = 1;
7582 if ((i = check_user_alignment (align_expr, false)) == -1
7583 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7584 *no_add_attrs = true;
7585 else if (is_type)
7587 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7588 /* OK, modify the type in place. */;
7589 /* If we have a TYPE_DECL, then copy the type, so that we
7590 don't accidentally modify a builtin type. See pushdecl. */
7591 else if (decl && TREE_TYPE (decl) != error_mark_node
7592 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7594 tree tt = TREE_TYPE (decl);
7595 *type = build_variant_type_copy (*type);
7596 DECL_ORIGINAL_TYPE (decl) = tt;
7597 TYPE_NAME (*type) = decl;
7598 TREE_USED (*type) = TREE_USED (decl);
7599 TREE_TYPE (decl) = *type;
7601 else
7602 *type = build_variant_type_copy (*type);
7604 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7605 TYPE_USER_ALIGN (*type) = 1;
7607 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7608 && TREE_CODE (decl) != FIELD_DECL)
7610 error ("alignment may not be specified for %q+D", decl);
7611 *no_add_attrs = true;
7613 else if (DECL_USER_ALIGN (decl)
7614 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7615 /* C++-11 [dcl.align/4]:
7617 When multiple alignment-specifiers are specified for an
7618 entity, the alignment requirement shall be set to the
7619 strictest specified alignment.
7621 This formally comes from the c++11 specification but we are
7622 doing it for the GNU attribute syntax as well. */
7623 *no_add_attrs = true;
7624 else if (TREE_CODE (decl) == FUNCTION_DECL
7625 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7627 if (DECL_USER_ALIGN (decl))
7628 error ("alignment for %q+D was previously specified as %d "
7629 "and may not be decreased", decl,
7630 DECL_ALIGN (decl) / BITS_PER_UNIT);
7631 else
7632 error ("alignment for %q+D must be at least %d", decl,
7633 DECL_ALIGN (decl) / BITS_PER_UNIT);
7634 *no_add_attrs = true;
7636 else
7638 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7639 DECL_USER_ALIGN (decl) = 1;
7642 return NULL_TREE;
7645 /* Handle a "weak" attribute; arguments as in
7646 struct attribute_spec.handler. */
7648 static tree
7649 handle_weak_attribute (tree *node, tree name,
7650 tree ARG_UNUSED (args),
7651 int ARG_UNUSED (flags),
7652 bool * ARG_UNUSED (no_add_attrs))
7654 if (TREE_CODE (*node) == FUNCTION_DECL
7655 && DECL_DECLARED_INLINE_P (*node))
7657 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7658 *no_add_attrs = true;
7660 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7662 error ("indirect function %q+D cannot be declared weak", *node);
7663 *no_add_attrs = true;
7664 return NULL_TREE;
7666 else if (TREE_CODE (*node) == FUNCTION_DECL
7667 || TREE_CODE (*node) == VAR_DECL)
7668 declare_weak (*node);
7669 else
7670 warning (OPT_Wattributes, "%qE attribute ignored", name);
7672 return NULL_TREE;
7675 /* Handle an "alias" or "ifunc" attribute; arguments as in
7676 struct attribute_spec.handler, except that IS_ALIAS tells us
7677 whether this is an alias as opposed to ifunc attribute. */
7679 static tree
7680 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7681 bool *no_add_attrs)
7683 tree decl = *node;
7685 if (TREE_CODE (decl) != FUNCTION_DECL
7686 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7688 warning (OPT_Wattributes, "%qE attribute ignored", name);
7689 *no_add_attrs = true;
7691 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7692 || (TREE_CODE (decl) != FUNCTION_DECL
7693 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7694 /* A static variable declaration is always a tentative definition,
7695 but the alias is a non-tentative definition which overrides. */
7696 || (TREE_CODE (decl) != FUNCTION_DECL
7697 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7699 error ("%q+D defined both normally and as %qE attribute", decl, name);
7700 *no_add_attrs = true;
7701 return NULL_TREE;
7703 else if (!is_alias
7704 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7705 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7707 error ("weak %q+D cannot be defined %qE", decl, name);
7708 *no_add_attrs = true;
7709 return NULL_TREE;
7712 /* Note that the very first time we process a nested declaration,
7713 decl_function_context will not be set. Indeed, *would* never
7714 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7715 we do below. After such frobbery, pushdecl would set the context.
7716 In any case, this is never what we want. */
7717 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7719 tree id;
7721 id = TREE_VALUE (args);
7722 if (TREE_CODE (id) != STRING_CST)
7724 error ("attribute %qE argument not a string", name);
7725 *no_add_attrs = true;
7726 return NULL_TREE;
7728 id = get_identifier (TREE_STRING_POINTER (id));
7729 /* This counts as a use of the object pointed to. */
7730 TREE_USED (id) = 1;
7732 if (TREE_CODE (decl) == FUNCTION_DECL)
7733 DECL_INITIAL (decl) = error_mark_node;
7734 else
7735 TREE_STATIC (decl) = 1;
7737 if (!is_alias)
7738 /* ifuncs are also aliases, so set that attribute too. */
7739 DECL_ATTRIBUTES (decl)
7740 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7742 else
7744 warning (OPT_Wattributes, "%qE attribute ignored", name);
7745 *no_add_attrs = true;
7748 return NULL_TREE;
7751 /* Handle an "alias" or "ifunc" attribute; arguments as in
7752 struct attribute_spec.handler. */
7754 static tree
7755 handle_ifunc_attribute (tree *node, tree name, tree args,
7756 int ARG_UNUSED (flags), bool *no_add_attrs)
7758 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7761 /* Handle an "alias" or "ifunc" attribute; arguments as in
7762 struct attribute_spec.handler. */
7764 static tree
7765 handle_alias_attribute (tree *node, tree name, tree args,
7766 int ARG_UNUSED (flags), bool *no_add_attrs)
7768 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7771 /* Handle a "weakref" attribute; arguments as in struct
7772 attribute_spec.handler. */
7774 static tree
7775 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7776 int flags, bool *no_add_attrs)
7778 tree attr = NULL_TREE;
7780 /* We must ignore the attribute when it is associated with
7781 local-scoped decls, since attribute alias is ignored and many
7782 such symbols do not even have a DECL_WEAK field. */
7783 if (decl_function_context (*node)
7784 || current_function_decl
7785 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7787 warning (OPT_Wattributes, "%qE attribute ignored", name);
7788 *no_add_attrs = true;
7789 return NULL_TREE;
7792 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7794 error ("indirect function %q+D cannot be declared weakref", *node);
7795 *no_add_attrs = true;
7796 return NULL_TREE;
7799 /* The idea here is that `weakref("name")' mutates into `weakref,
7800 alias("name")', and weakref without arguments, in turn,
7801 implicitly adds weak. */
7803 if (args)
7805 attr = tree_cons (get_identifier ("alias"), args, attr);
7806 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7808 *no_add_attrs = true;
7810 decl_attributes (node, attr, flags);
7812 else
7814 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7815 error_at (DECL_SOURCE_LOCATION (*node),
7816 "weakref attribute must appear before alias attribute");
7818 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7819 and that isn't supported; and because it wants to add it to
7820 the list of weak decls, which isn't helpful. */
7821 DECL_WEAK (*node) = 1;
7824 return NULL_TREE;
7827 /* Handle an "visibility" attribute; arguments as in
7828 struct attribute_spec.handler. */
7830 static tree
7831 handle_visibility_attribute (tree *node, tree name, tree args,
7832 int ARG_UNUSED (flags),
7833 bool *ARG_UNUSED (no_add_attrs))
7835 tree decl = *node;
7836 tree id = TREE_VALUE (args);
7837 enum symbol_visibility vis;
7839 if (TYPE_P (*node))
7841 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7842 /* OK */;
7843 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7845 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7846 name);
7847 return NULL_TREE;
7849 else if (TYPE_FIELDS (*node))
7851 error ("%qE attribute ignored because %qT is already defined",
7852 name, *node);
7853 return NULL_TREE;
7856 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7858 warning (OPT_Wattributes, "%qE attribute ignored", name);
7859 return NULL_TREE;
7862 if (TREE_CODE (id) != STRING_CST)
7864 error ("visibility argument not a string");
7865 return NULL_TREE;
7868 /* If this is a type, set the visibility on the type decl. */
7869 if (TYPE_P (decl))
7871 decl = TYPE_NAME (decl);
7872 if (!decl)
7873 return NULL_TREE;
7874 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7876 warning (OPT_Wattributes, "%qE attribute ignored on types",
7877 name);
7878 return NULL_TREE;
7882 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7883 vis = VISIBILITY_DEFAULT;
7884 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7885 vis = VISIBILITY_INTERNAL;
7886 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7887 vis = VISIBILITY_HIDDEN;
7888 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7889 vis = VISIBILITY_PROTECTED;
7890 else
7892 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7893 vis = VISIBILITY_DEFAULT;
7896 if (DECL_VISIBILITY_SPECIFIED (decl)
7897 && vis != DECL_VISIBILITY (decl))
7899 tree attributes = (TYPE_P (*node)
7900 ? TYPE_ATTRIBUTES (*node)
7901 : DECL_ATTRIBUTES (decl));
7902 if (lookup_attribute ("visibility", attributes))
7903 error ("%qD redeclared with different visibility", decl);
7904 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7905 && lookup_attribute ("dllimport", attributes))
7906 error ("%qD was declared %qs which implies default visibility",
7907 decl, "dllimport");
7908 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7909 && lookup_attribute ("dllexport", attributes))
7910 error ("%qD was declared %qs which implies default visibility",
7911 decl, "dllexport");
7914 DECL_VISIBILITY (decl) = vis;
7915 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7917 /* Go ahead and attach the attribute to the node as well. This is needed
7918 so we can determine whether we have VISIBILITY_DEFAULT because the
7919 visibility was not specified, or because it was explicitly overridden
7920 from the containing scope. */
7922 return NULL_TREE;
7925 /* Determine the ELF symbol visibility for DECL, which is either a
7926 variable or a function. It is an error to use this function if a
7927 definition of DECL is not available in this translation unit.
7928 Returns true if the final visibility has been determined by this
7929 function; false if the caller is free to make additional
7930 modifications. */
7932 bool
7933 c_determine_visibility (tree decl)
7935 gcc_assert (TREE_CODE (decl) == VAR_DECL
7936 || TREE_CODE (decl) == FUNCTION_DECL);
7938 /* If the user explicitly specified the visibility with an
7939 attribute, honor that. DECL_VISIBILITY will have been set during
7940 the processing of the attribute. We check for an explicit
7941 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7942 to distinguish the use of an attribute from the use of a "#pragma
7943 GCC visibility push(...)"; in the latter case we still want other
7944 considerations to be able to overrule the #pragma. */
7945 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7946 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7947 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7948 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7949 return true;
7951 /* Set default visibility to whatever the user supplied with
7952 visibility_specified depending on #pragma GCC visibility. */
7953 if (!DECL_VISIBILITY_SPECIFIED (decl))
7955 if (visibility_options.inpragma
7956 || DECL_VISIBILITY (decl) != default_visibility)
7958 DECL_VISIBILITY (decl) = default_visibility;
7959 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7960 /* If visibility changed and DECL already has DECL_RTL, ensure
7961 symbol flags are updated. */
7962 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7963 || TREE_CODE (decl) == FUNCTION_DECL)
7964 && DECL_RTL_SET_P (decl))
7965 make_decl_rtl (decl);
7968 return false;
7971 /* Handle an "tls_model" attribute; arguments as in
7972 struct attribute_spec.handler. */
7974 static tree
7975 handle_tls_model_attribute (tree *node, tree name, tree args,
7976 int ARG_UNUSED (flags), bool *no_add_attrs)
7978 tree id;
7979 tree decl = *node;
7980 enum tls_model kind;
7982 *no_add_attrs = true;
7984 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7986 warning (OPT_Wattributes, "%qE attribute ignored", name);
7987 return NULL_TREE;
7990 kind = DECL_TLS_MODEL (decl);
7991 id = TREE_VALUE (args);
7992 if (TREE_CODE (id) != STRING_CST)
7994 error ("tls_model argument not a string");
7995 return NULL_TREE;
7998 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7999 kind = TLS_MODEL_LOCAL_EXEC;
8000 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8001 kind = TLS_MODEL_INITIAL_EXEC;
8002 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8003 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8004 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8005 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8006 else
8007 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8009 DECL_TLS_MODEL (decl) = kind;
8010 return NULL_TREE;
8013 /* Handle a "no_instrument_function" attribute; arguments as in
8014 struct attribute_spec.handler. */
8016 static tree
8017 handle_no_instrument_function_attribute (tree *node, tree name,
8018 tree ARG_UNUSED (args),
8019 int ARG_UNUSED (flags),
8020 bool *no_add_attrs)
8022 tree decl = *node;
8024 if (TREE_CODE (decl) != FUNCTION_DECL)
8026 error_at (DECL_SOURCE_LOCATION (decl),
8027 "%qE attribute applies only to functions", name);
8028 *no_add_attrs = true;
8030 else
8031 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8033 return NULL_TREE;
8036 /* Handle a "malloc" attribute; arguments as in
8037 struct attribute_spec.handler. */
8039 static tree
8040 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8041 int ARG_UNUSED (flags), bool *no_add_attrs)
8043 if (TREE_CODE (*node) == FUNCTION_DECL
8044 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8045 DECL_IS_MALLOC (*node) = 1;
8046 else
8048 warning (OPT_Wattributes, "%qE attribute ignored", name);
8049 *no_add_attrs = true;
8052 return NULL_TREE;
8055 /* Handle a "alloc_size" attribute; arguments as in
8056 struct attribute_spec.handler. */
8058 static tree
8059 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8060 int ARG_UNUSED (flags), bool *no_add_attrs)
8062 unsigned arg_count = type_num_arguments (*node);
8063 for (; args; args = TREE_CHAIN (args))
8065 tree position = TREE_VALUE (args);
8066 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8067 && TREE_CODE (position) != FUNCTION_DECL)
8068 position = default_conversion (position);
8070 if (!tree_fits_uhwi_p (position)
8071 || !arg_count
8072 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8074 warning (OPT_Wattributes,
8075 "alloc_size parameter outside range");
8076 *no_add_attrs = true;
8077 return NULL_TREE;
8080 return NULL_TREE;
8083 /* Handle a "alloc_align" attribute; arguments as in
8084 struct attribute_spec.handler. */
8086 static tree
8087 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8088 bool *no_add_attrs)
8090 unsigned arg_count = type_num_arguments (*node);
8091 tree position = TREE_VALUE (args);
8092 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8093 position = default_conversion (position);
8095 if (!tree_fits_uhwi_p (position)
8096 || !arg_count
8097 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8099 warning (OPT_Wattributes,
8100 "alloc_align parameter outside range");
8101 *no_add_attrs = true;
8102 return NULL_TREE;
8104 return NULL_TREE;
8107 /* Handle a "assume_aligned" attribute; arguments as in
8108 struct attribute_spec.handler. */
8110 static tree
8111 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8112 bool *no_add_attrs)
8114 for (; args; args = TREE_CHAIN (args))
8116 tree position = TREE_VALUE (args);
8117 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8118 && TREE_CODE (position) != FUNCTION_DECL)
8119 position = default_conversion (position);
8121 if (TREE_CODE (position) != INTEGER_CST)
8123 warning (OPT_Wattributes,
8124 "assume_aligned parameter not integer constant");
8125 *no_add_attrs = true;
8126 return NULL_TREE;
8129 return NULL_TREE;
8132 /* Handle a "fn spec" attribute; arguments as in
8133 struct attribute_spec.handler. */
8135 static tree
8136 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8137 tree args, int ARG_UNUSED (flags),
8138 bool *no_add_attrs ATTRIBUTE_UNUSED)
8140 gcc_assert (args
8141 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8142 && !TREE_CHAIN (args));
8143 return NULL_TREE;
8146 /* Handle a "warn_unused" attribute; arguments as in
8147 struct attribute_spec.handler. */
8149 static tree
8150 handle_warn_unused_attribute (tree *node, tree name,
8151 tree args ATTRIBUTE_UNUSED,
8152 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8154 if (TYPE_P (*node))
8155 /* Do nothing else, just set the attribute. We'll get at
8156 it later with lookup_attribute. */
8158 else
8160 warning (OPT_Wattributes, "%qE attribute ignored", name);
8161 *no_add_attrs = true;
8164 return NULL_TREE;
8167 /* Handle an "omp declare simd" attribute; arguments as in
8168 struct attribute_spec.handler. */
8170 static tree
8171 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8173 return NULL_TREE;
8176 /* Handle an "omp declare target" attribute; arguments as in
8177 struct attribute_spec.handler. */
8179 static tree
8180 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8182 return NULL_TREE;
8185 /* Handle a "returns_twice" attribute; arguments as in
8186 struct attribute_spec.handler. */
8188 static tree
8189 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8190 int ARG_UNUSED (flags), bool *no_add_attrs)
8192 if (TREE_CODE (*node) == FUNCTION_DECL)
8193 DECL_IS_RETURNS_TWICE (*node) = 1;
8194 else
8196 warning (OPT_Wattributes, "%qE attribute ignored", name);
8197 *no_add_attrs = true;
8200 return NULL_TREE;
8203 /* Handle a "no_limit_stack" attribute; arguments as in
8204 struct attribute_spec.handler. */
8206 static tree
8207 handle_no_limit_stack_attribute (tree *node, tree name,
8208 tree ARG_UNUSED (args),
8209 int ARG_UNUSED (flags),
8210 bool *no_add_attrs)
8212 tree decl = *node;
8214 if (TREE_CODE (decl) != FUNCTION_DECL)
8216 error_at (DECL_SOURCE_LOCATION (decl),
8217 "%qE attribute applies only to functions", name);
8218 *no_add_attrs = true;
8220 else if (DECL_INITIAL (decl))
8222 error_at (DECL_SOURCE_LOCATION (decl),
8223 "can%'t set %qE attribute after definition", name);
8224 *no_add_attrs = true;
8226 else
8227 DECL_NO_LIMIT_STACK (decl) = 1;
8229 return NULL_TREE;
8232 /* Handle a "pure" attribute; arguments as in
8233 struct attribute_spec.handler. */
8235 static tree
8236 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8237 int ARG_UNUSED (flags), bool *no_add_attrs)
8239 if (TREE_CODE (*node) == FUNCTION_DECL)
8240 DECL_PURE_P (*node) = 1;
8241 /* ??? TODO: Support types. */
8242 else
8244 warning (OPT_Wattributes, "%qE attribute ignored", name);
8245 *no_add_attrs = true;
8248 return NULL_TREE;
8251 /* Digest an attribute list destined for a transactional memory statement.
8252 ALLOWED is the set of attributes that are allowed for this statement;
8253 return the attribute we parsed. Multiple attributes are never allowed. */
8256 parse_tm_stmt_attr (tree attrs, int allowed)
8258 tree a_seen = NULL;
8259 int m_seen = 0;
8261 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8263 tree a = TREE_PURPOSE (attrs);
8264 int m = 0;
8266 if (is_attribute_p ("outer", a))
8267 m = TM_STMT_ATTR_OUTER;
8269 if ((m & allowed) == 0)
8271 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8272 continue;
8275 if (m_seen == 0)
8277 a_seen = a;
8278 m_seen = m;
8280 else if (m_seen == m)
8281 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8282 else
8283 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8286 return m_seen;
8289 /* Transform a TM attribute name into a maskable integer and back.
8290 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8291 to how the lack of an attribute is treated. */
8294 tm_attr_to_mask (tree attr)
8296 if (attr == NULL)
8297 return 0;
8298 if (is_attribute_p ("transaction_safe", attr))
8299 return TM_ATTR_SAFE;
8300 if (is_attribute_p ("transaction_callable", attr))
8301 return TM_ATTR_CALLABLE;
8302 if (is_attribute_p ("transaction_pure", attr))
8303 return TM_ATTR_PURE;
8304 if (is_attribute_p ("transaction_unsafe", attr))
8305 return TM_ATTR_IRREVOCABLE;
8306 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8307 return TM_ATTR_MAY_CANCEL_OUTER;
8308 return 0;
8311 tree
8312 tm_mask_to_attr (int mask)
8314 const char *str;
8315 switch (mask)
8317 case TM_ATTR_SAFE:
8318 str = "transaction_safe";
8319 break;
8320 case TM_ATTR_CALLABLE:
8321 str = "transaction_callable";
8322 break;
8323 case TM_ATTR_PURE:
8324 str = "transaction_pure";
8325 break;
8326 case TM_ATTR_IRREVOCABLE:
8327 str = "transaction_unsafe";
8328 break;
8329 case TM_ATTR_MAY_CANCEL_OUTER:
8330 str = "transaction_may_cancel_outer";
8331 break;
8332 default:
8333 gcc_unreachable ();
8335 return get_identifier (str);
8338 /* Return the first TM attribute seen in LIST. */
8340 tree
8341 find_tm_attribute (tree list)
8343 for (; list ; list = TREE_CHAIN (list))
8345 tree name = TREE_PURPOSE (list);
8346 if (tm_attr_to_mask (name) != 0)
8347 return name;
8349 return NULL_TREE;
8352 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8353 Here we accept only function types, and verify that none of the other
8354 function TM attributes are also applied. */
8355 /* ??? We need to accept class types for C++, but not C. This greatly
8356 complicates this function, since we can no longer rely on the extra
8357 processing given by function_type_required. */
8359 static tree
8360 handle_tm_attribute (tree *node, tree name, tree args,
8361 int flags, bool *no_add_attrs)
8363 /* Only one path adds the attribute; others don't. */
8364 *no_add_attrs = true;
8366 switch (TREE_CODE (*node))
8368 case RECORD_TYPE:
8369 case UNION_TYPE:
8370 /* Only tm_callable and tm_safe apply to classes. */
8371 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8372 goto ignored;
8373 /* FALLTHRU */
8375 case FUNCTION_TYPE:
8376 case METHOD_TYPE:
8378 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8379 if (old_name == name)
8381 else if (old_name != NULL_TREE)
8382 error ("type was previously declared %qE", old_name);
8383 else
8384 *no_add_attrs = false;
8386 break;
8388 case POINTER_TYPE:
8390 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8391 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8393 tree fn_tmp = TREE_TYPE (*node);
8394 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8395 *node = build_pointer_type (fn_tmp);
8396 break;
8399 /* FALLTHRU */
8401 default:
8402 /* If a function is next, pass it on to be tried next. */
8403 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8404 return tree_cons (name, args, NULL);
8406 ignored:
8407 warning (OPT_Wattributes, "%qE attribute ignored", name);
8408 break;
8411 return NULL_TREE;
8414 /* Handle the TM_WRAP attribute; arguments as in
8415 struct attribute_spec.handler. */
8417 static tree
8418 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8419 int ARG_UNUSED (flags), bool *no_add_attrs)
8421 tree decl = *node;
8423 /* We don't need the attribute even on success, since we
8424 record the entry in an external table. */
8425 *no_add_attrs = true;
8427 if (TREE_CODE (decl) != FUNCTION_DECL)
8428 warning (OPT_Wattributes, "%qE attribute ignored", name);
8429 else
8431 tree wrap_decl = TREE_VALUE (args);
8432 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8433 && TREE_CODE (wrap_decl) != VAR_DECL
8434 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8435 error ("%qE argument not an identifier", name);
8436 else
8438 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8439 wrap_decl = lookup_name (wrap_decl);
8440 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8442 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8443 TREE_TYPE (wrap_decl)))
8444 record_tm_replacement (wrap_decl, decl);
8445 else
8446 error ("%qD is not compatible with %qD", wrap_decl, decl);
8448 else
8449 error ("%qE argument is not a function", name);
8453 return NULL_TREE;
8456 /* Ignore the given attribute. Used when this attribute may be usefully
8457 overridden by the target, but is not used generically. */
8459 static tree
8460 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8461 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8462 bool *no_add_attrs)
8464 *no_add_attrs = true;
8465 return NULL_TREE;
8468 /* Handle a "no vops" attribute; arguments as in
8469 struct attribute_spec.handler. */
8471 static tree
8472 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8473 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8474 bool *ARG_UNUSED (no_add_attrs))
8476 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8477 DECL_IS_NOVOPS (*node) = 1;
8478 return NULL_TREE;
8481 /* Handle a "deprecated" attribute; arguments as in
8482 struct attribute_spec.handler. */
8484 static tree
8485 handle_deprecated_attribute (tree *node, tree name,
8486 tree args, int flags,
8487 bool *no_add_attrs)
8489 tree type = NULL_TREE;
8490 int warn = 0;
8491 tree what = NULL_TREE;
8493 if (!args)
8494 *no_add_attrs = true;
8495 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8497 error ("deprecated message is not a string");
8498 *no_add_attrs = true;
8501 if (DECL_P (*node))
8503 tree decl = *node;
8504 type = TREE_TYPE (decl);
8506 if (TREE_CODE (decl) == TYPE_DECL
8507 || TREE_CODE (decl) == PARM_DECL
8508 || TREE_CODE (decl) == VAR_DECL
8509 || TREE_CODE (decl) == FUNCTION_DECL
8510 || TREE_CODE (decl) == FIELD_DECL
8511 || objc_method_decl (TREE_CODE (decl)))
8512 TREE_DEPRECATED (decl) = 1;
8513 else
8514 warn = 1;
8516 else if (TYPE_P (*node))
8518 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8519 *node = build_variant_type_copy (*node);
8520 TREE_DEPRECATED (*node) = 1;
8521 type = *node;
8523 else
8524 warn = 1;
8526 if (warn)
8528 *no_add_attrs = true;
8529 if (type && TYPE_NAME (type))
8531 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8532 what = TYPE_NAME (*node);
8533 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8534 && DECL_NAME (TYPE_NAME (type)))
8535 what = DECL_NAME (TYPE_NAME (type));
8537 if (what)
8538 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8539 else
8540 warning (OPT_Wattributes, "%qE attribute ignored", name);
8543 return NULL_TREE;
8546 /* Handle a "vector_size" attribute; arguments as in
8547 struct attribute_spec.handler. */
8549 static tree
8550 handle_vector_size_attribute (tree *node, tree name, tree args,
8551 int ARG_UNUSED (flags),
8552 bool *no_add_attrs)
8554 unsigned HOST_WIDE_INT vecsize, nunits;
8555 enum machine_mode orig_mode;
8556 tree type = *node, new_type, size;
8558 *no_add_attrs = true;
8560 size = TREE_VALUE (args);
8561 if (size && TREE_CODE (size) != IDENTIFIER_NODE)
8562 size = default_conversion (size);
8564 if (!tree_fits_uhwi_p (size))
8566 warning (OPT_Wattributes, "%qE attribute ignored", name);
8567 return NULL_TREE;
8570 /* Get the vector size (in bytes). */
8571 vecsize = tree_to_uhwi (size);
8573 /* We need to provide for vector pointers, vector arrays, and
8574 functions returning vectors. For example:
8576 __attribute__((vector_size(16))) short *foo;
8578 In this case, the mode is SI, but the type being modified is
8579 HI, so we need to look further. */
8581 while (POINTER_TYPE_P (type)
8582 || TREE_CODE (type) == FUNCTION_TYPE
8583 || TREE_CODE (type) == METHOD_TYPE
8584 || TREE_CODE (type) == ARRAY_TYPE
8585 || TREE_CODE (type) == OFFSET_TYPE)
8586 type = TREE_TYPE (type);
8588 /* Get the mode of the type being modified. */
8589 orig_mode = TYPE_MODE (type);
8591 if ((!INTEGRAL_TYPE_P (type)
8592 && !SCALAR_FLOAT_TYPE_P (type)
8593 && !FIXED_POINT_TYPE_P (type))
8594 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8595 && GET_MODE_CLASS (orig_mode) != MODE_INT
8596 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8597 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8598 || TREE_CODE (type) == BOOLEAN_TYPE)
8600 error ("invalid vector type for attribute %qE", name);
8601 return NULL_TREE;
8604 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8606 error ("vector size not an integral multiple of component size");
8607 return NULL;
8610 if (vecsize == 0)
8612 error ("zero vector size");
8613 return NULL;
8616 /* Calculate how many units fit in the vector. */
8617 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8618 if (nunits & (nunits - 1))
8620 error ("number of components of the vector not a power of two");
8621 return NULL_TREE;
8624 new_type = build_vector_type (type, nunits);
8626 /* Build back pointers if needed. */
8627 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8629 return NULL_TREE;
8632 /* Handle the "nonnull" attribute. */
8633 static tree
8634 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8635 tree args, int ARG_UNUSED (flags),
8636 bool *no_add_attrs)
8638 tree type = *node;
8639 unsigned HOST_WIDE_INT attr_arg_num;
8641 /* If no arguments are specified, all pointer arguments should be
8642 non-null. Verify a full prototype is given so that the arguments
8643 will have the correct types when we actually check them later. */
8644 if (!args)
8646 if (!prototype_p (type))
8648 error ("nonnull attribute without arguments on a non-prototype");
8649 *no_add_attrs = true;
8651 return NULL_TREE;
8654 /* Argument list specified. Verify that each argument number references
8655 a pointer argument. */
8656 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8658 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8660 tree arg = TREE_VALUE (args);
8661 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8662 && TREE_CODE (arg) != FUNCTION_DECL)
8663 arg = default_conversion (arg);
8665 if (!get_nonnull_operand (arg, &arg_num))
8667 error ("nonnull argument has invalid operand number (argument %lu)",
8668 (unsigned long) attr_arg_num);
8669 *no_add_attrs = true;
8670 return NULL_TREE;
8673 if (prototype_p (type))
8675 function_args_iterator iter;
8676 tree argument;
8678 function_args_iter_init (&iter, type);
8679 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8681 argument = function_args_iter_cond (&iter);
8682 if (argument == NULL_TREE || ck_num == arg_num)
8683 break;
8686 if (!argument
8687 || TREE_CODE (argument) == VOID_TYPE)
8689 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8690 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8691 *no_add_attrs = true;
8692 return NULL_TREE;
8695 if (TREE_CODE (argument) != POINTER_TYPE)
8697 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8698 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8699 *no_add_attrs = true;
8700 return NULL_TREE;
8705 return NULL_TREE;
8708 /* Check the argument list of a function call for null in argument slots
8709 that are marked as requiring a non-null pointer argument. The NARGS
8710 arguments are passed in the array ARGARRAY.
8713 static void
8714 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8716 tree a;
8717 int i;
8719 attrs = lookup_attribute ("nonnull", attrs);
8720 if (attrs == NULL_TREE)
8721 return;
8723 a = attrs;
8724 /* See if any of the nonnull attributes has no arguments. If so,
8725 then every pointer argument is checked (in which case the check
8726 for pointer type is done in check_nonnull_arg). */
8727 if (TREE_VALUE (a) != NULL_TREE)
8729 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8730 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8732 if (a != NULL_TREE)
8733 for (i = 0; i < nargs; i++)
8734 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8735 i + 1);
8736 else
8738 /* Walk the argument list. If we encounter an argument number we
8739 should check for non-null, do it. */
8740 for (i = 0; i < nargs; i++)
8742 for (a = attrs; ; a = TREE_CHAIN (a))
8744 a = lookup_attribute ("nonnull", a);
8745 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8746 break;
8749 if (a != NULL_TREE)
8750 check_function_arguments_recurse (check_nonnull_arg, NULL,
8751 argarray[i], i + 1);
8756 /* Check that the Nth argument of a function call (counting backwards
8757 from the end) is a (pointer)0. The NARGS arguments are passed in the
8758 array ARGARRAY. */
8760 static void
8761 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8763 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8765 if (attr)
8767 int len = 0;
8768 int pos = 0;
8769 tree sentinel;
8770 function_args_iterator iter;
8771 tree t;
8773 /* Skip over the named arguments. */
8774 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8776 if (len == nargs)
8777 break;
8778 len++;
8781 if (TREE_VALUE (attr))
8783 tree p = TREE_VALUE (TREE_VALUE (attr));
8784 pos = TREE_INT_CST_LOW (p);
8787 /* The sentinel must be one of the varargs, i.e.
8788 in position >= the number of fixed arguments. */
8789 if ((nargs - 1 - pos) < len)
8791 warning (OPT_Wformat_,
8792 "not enough variable arguments to fit a sentinel");
8793 return;
8796 /* Validate the sentinel. */
8797 sentinel = argarray[nargs - 1 - pos];
8798 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8799 || !integer_zerop (sentinel))
8800 /* Although __null (in C++) is only an integer we allow it
8801 nevertheless, as we are guaranteed that it's exactly
8802 as wide as a pointer, and we don't want to force
8803 users to cast the NULL they have written there.
8804 We warn with -Wstrict-null-sentinel, though. */
8805 && (warn_strict_null_sentinel || null_node != sentinel))
8806 warning (OPT_Wformat_, "missing sentinel in function call");
8810 /* Helper for check_function_nonnull; given a list of operands which
8811 must be non-null in ARGS, determine if operand PARAM_NUM should be
8812 checked. */
8814 static bool
8815 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8817 unsigned HOST_WIDE_INT arg_num = 0;
8819 for (; args; args = TREE_CHAIN (args))
8821 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8823 gcc_assert (found);
8825 if (arg_num == param_num)
8826 return true;
8828 return false;
8831 /* Check that the function argument PARAM (which is operand number
8832 PARAM_NUM) is non-null. This is called by check_function_nonnull
8833 via check_function_arguments_recurse. */
8835 static void
8836 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8837 unsigned HOST_WIDE_INT param_num)
8839 /* Just skip checking the argument if it's not a pointer. This can
8840 happen if the "nonnull" attribute was given without an operand
8841 list (which means to check every pointer argument). */
8843 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8844 return;
8846 if (integer_zerop (param))
8847 warning (OPT_Wnonnull, "null argument where non-null required "
8848 "(argument %lu)", (unsigned long) param_num);
8851 /* Helper for nonnull attribute handling; fetch the operand number
8852 from the attribute argument list. */
8854 static bool
8855 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8857 /* Verify the arg number is a small constant. */
8858 if (tree_fits_uhwi_p (arg_num_expr))
8860 *valp = TREE_INT_CST_LOW (arg_num_expr);
8861 return true;
8863 else
8864 return false;
8867 /* Handle a "nothrow" attribute; arguments as in
8868 struct attribute_spec.handler. */
8870 static tree
8871 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8872 int ARG_UNUSED (flags), bool *no_add_attrs)
8874 if (TREE_CODE (*node) == FUNCTION_DECL)
8875 TREE_NOTHROW (*node) = 1;
8876 /* ??? TODO: Support types. */
8877 else
8879 warning (OPT_Wattributes, "%qE attribute ignored", name);
8880 *no_add_attrs = true;
8883 return NULL_TREE;
8886 /* Handle a "cleanup" attribute; arguments as in
8887 struct attribute_spec.handler. */
8889 static tree
8890 handle_cleanup_attribute (tree *node, tree name, tree args,
8891 int ARG_UNUSED (flags), bool *no_add_attrs)
8893 tree decl = *node;
8894 tree cleanup_id, cleanup_decl;
8896 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8897 for global destructors in C++. This requires infrastructure that
8898 we don't have generically at the moment. It's also not a feature
8899 we'd be missing too much, since we do have attribute constructor. */
8900 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8902 warning (OPT_Wattributes, "%qE attribute ignored", name);
8903 *no_add_attrs = true;
8904 return NULL_TREE;
8907 /* Verify that the argument is a function in scope. */
8908 /* ??? We could support pointers to functions here as well, if
8909 that was considered desirable. */
8910 cleanup_id = TREE_VALUE (args);
8911 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8913 error ("cleanup argument not an identifier");
8914 *no_add_attrs = true;
8915 return NULL_TREE;
8917 cleanup_decl = lookup_name (cleanup_id);
8918 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8920 error ("cleanup argument not a function");
8921 *no_add_attrs = true;
8922 return NULL_TREE;
8925 /* That the function has proper type is checked with the
8926 eventual call to build_function_call. */
8928 return NULL_TREE;
8931 /* Handle a "warn_unused_result" attribute. No special handling. */
8933 static tree
8934 handle_warn_unused_result_attribute (tree *node, tree name,
8935 tree ARG_UNUSED (args),
8936 int ARG_UNUSED (flags), bool *no_add_attrs)
8938 /* Ignore the attribute for functions not returning any value. */
8939 if (VOID_TYPE_P (TREE_TYPE (*node)))
8941 warning (OPT_Wattributes, "%qE attribute ignored", name);
8942 *no_add_attrs = true;
8945 return NULL_TREE;
8948 /* Handle a "sentinel" attribute. */
8950 static tree
8951 handle_sentinel_attribute (tree *node, tree name, tree args,
8952 int ARG_UNUSED (flags), bool *no_add_attrs)
8954 if (!prototype_p (*node))
8956 warning (OPT_Wattributes,
8957 "%qE attribute requires prototypes with named arguments", name);
8958 *no_add_attrs = true;
8960 else
8962 if (!stdarg_p (*node))
8964 warning (OPT_Wattributes,
8965 "%qE attribute only applies to variadic functions", name);
8966 *no_add_attrs = true;
8970 if (args)
8972 tree position = TREE_VALUE (args);
8974 if (TREE_CODE (position) != INTEGER_CST)
8976 warning (OPT_Wattributes,
8977 "requested position is not an integer constant");
8978 *no_add_attrs = true;
8980 else
8982 if (tree_int_cst_lt (position, integer_zero_node))
8984 warning (OPT_Wattributes,
8985 "requested position is less than zero");
8986 *no_add_attrs = true;
8991 return NULL_TREE;
8994 /* Handle a "type_generic" attribute. */
8996 static tree
8997 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8998 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8999 bool * ARG_UNUSED (no_add_attrs))
9001 /* Ensure we have a function type. */
9002 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9004 /* Ensure we have a variadic function. */
9005 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9007 return NULL_TREE;
9010 /* Handle a "target" attribute. */
9012 static tree
9013 handle_target_attribute (tree *node, tree name, tree args, int flags,
9014 bool *no_add_attrs)
9016 /* Ensure we have a function type. */
9017 if (TREE_CODE (*node) != FUNCTION_DECL)
9019 warning (OPT_Wattributes, "%qE attribute ignored", name);
9020 *no_add_attrs = true;
9022 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9023 flags))
9024 *no_add_attrs = true;
9026 return NULL_TREE;
9029 /* Arguments being collected for optimization. */
9030 typedef const char *const_char_p; /* For DEF_VEC_P. */
9031 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9034 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9035 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9036 false for #pragma GCC optimize. */
9038 bool
9039 parse_optimize_options (tree args, bool attr_p)
9041 bool ret = true;
9042 unsigned opt_argc;
9043 unsigned i;
9044 int saved_flag_strict_aliasing;
9045 const char **opt_argv;
9046 struct cl_decoded_option *decoded_options;
9047 unsigned int decoded_options_count;
9048 tree ap;
9050 /* Build up argv vector. Just in case the string is stored away, use garbage
9051 collected strings. */
9052 vec_safe_truncate (optimize_args, 0);
9053 vec_safe_push (optimize_args, (const char *) NULL);
9055 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9057 tree value = TREE_VALUE (ap);
9059 if (TREE_CODE (value) == INTEGER_CST)
9061 char buffer[20];
9062 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9063 vec_safe_push (optimize_args, ggc_strdup (buffer));
9066 else if (TREE_CODE (value) == STRING_CST)
9068 /* Split string into multiple substrings. */
9069 size_t len = TREE_STRING_LENGTH (value);
9070 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9071 char *end = p + len;
9072 char *comma;
9073 char *next_p = p;
9075 while (next_p != NULL)
9077 size_t len2;
9078 char *q, *r;
9080 p = next_p;
9081 comma = strchr (p, ',');
9082 if (comma)
9084 len2 = comma - p;
9085 *comma = '\0';
9086 next_p = comma+1;
9088 else
9090 len2 = end - p;
9091 next_p = NULL;
9094 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9096 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9097 options. */
9098 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9100 ret = false;
9101 if (attr_p)
9102 warning (OPT_Wattributes,
9103 "bad option %s to optimize attribute", p);
9104 else
9105 warning (OPT_Wpragmas,
9106 "bad option %s to pragma attribute", p);
9107 continue;
9110 if (*p != '-')
9112 *r++ = '-';
9114 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9115 itself is -Os, and any other switch begins with a -f. */
9116 if ((*p >= '0' && *p <= '9')
9117 || (p[0] == 's' && p[1] == '\0'))
9118 *r++ = 'O';
9119 else if (*p != 'O')
9120 *r++ = 'f';
9123 memcpy (r, p, len2);
9124 r[len2] = '\0';
9125 vec_safe_push (optimize_args, (const char *) q);
9131 opt_argc = optimize_args->length ();
9132 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9134 for (i = 1; i < opt_argc; i++)
9135 opt_argv[i] = (*optimize_args)[i];
9137 saved_flag_strict_aliasing = flag_strict_aliasing;
9139 /* Now parse the options. */
9140 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9141 &decoded_options,
9142 &decoded_options_count);
9143 decode_options (&global_options, &global_options_set,
9144 decoded_options, decoded_options_count,
9145 input_location, global_dc);
9147 targetm.override_options_after_change();
9149 /* Don't allow changing -fstrict-aliasing. */
9150 flag_strict_aliasing = saved_flag_strict_aliasing;
9152 optimize_args->truncate (0);
9153 return ret;
9156 /* For handling "optimize" attribute. arguments as in
9157 struct attribute_spec.handler. */
9159 static tree
9160 handle_optimize_attribute (tree *node, tree name, tree args,
9161 int ARG_UNUSED (flags), bool *no_add_attrs)
9163 /* Ensure we have a function type. */
9164 if (TREE_CODE (*node) != FUNCTION_DECL)
9166 warning (OPT_Wattributes, "%qE attribute ignored", name);
9167 *no_add_attrs = true;
9169 else
9171 struct cl_optimization cur_opts;
9172 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9174 /* Save current options. */
9175 cl_optimization_save (&cur_opts, &global_options);
9177 /* If we previously had some optimization options, use them as the
9178 default. */
9179 if (old_opts)
9180 cl_optimization_restore (&global_options,
9181 TREE_OPTIMIZATION (old_opts));
9183 /* Parse options, and update the vector. */
9184 parse_optimize_options (args, true);
9185 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9186 = build_optimization_node (&global_options);
9188 /* Restore current options. */
9189 cl_optimization_restore (&global_options, &cur_opts);
9192 return NULL_TREE;
9195 /* Handle a "no_split_stack" attribute. */
9197 static tree
9198 handle_no_split_stack_attribute (tree *node, tree name,
9199 tree ARG_UNUSED (args),
9200 int ARG_UNUSED (flags),
9201 bool *no_add_attrs)
9203 tree decl = *node;
9205 if (TREE_CODE (decl) != FUNCTION_DECL)
9207 error_at (DECL_SOURCE_LOCATION (decl),
9208 "%qE attribute applies only to functions", name);
9209 *no_add_attrs = true;
9211 else if (DECL_INITIAL (decl))
9213 error_at (DECL_SOURCE_LOCATION (decl),
9214 "can%'t set %qE attribute after definition", name);
9215 *no_add_attrs = true;
9218 return NULL_TREE;
9221 /* Handle a "returns_nonnull" attribute; arguments as in
9222 struct attribute_spec.handler. */
9224 static tree
9225 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9226 bool *no_add_attrs)
9228 // Even without a prototype we still have a return type we can check.
9229 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9231 error ("returns_nonnull attribute on a function not returning a pointer");
9232 *no_add_attrs = true;
9234 return NULL_TREE;
9238 /* Check for valid arguments being passed to a function with FNTYPE.
9239 There are NARGS arguments in the array ARGARRAY. */
9240 void
9241 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9243 /* Check for null being passed in a pointer argument that must be
9244 non-null. We also need to do this if format checking is enabled. */
9246 if (warn_nonnull)
9247 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9249 /* Check for errors in format strings. */
9251 if (warn_format || warn_suggest_attribute_format)
9252 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9254 if (warn_format)
9255 check_function_sentinel (fntype, nargs, argarray);
9258 /* Generic argument checking recursion routine. PARAM is the argument to
9259 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9260 once the argument is resolved. CTX is context for the callback. */
9261 void
9262 check_function_arguments_recurse (void (*callback)
9263 (void *, tree, unsigned HOST_WIDE_INT),
9264 void *ctx, tree param,
9265 unsigned HOST_WIDE_INT param_num)
9267 if (CONVERT_EXPR_P (param)
9268 && (TYPE_PRECISION (TREE_TYPE (param))
9269 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9271 /* Strip coercion. */
9272 check_function_arguments_recurse (callback, ctx,
9273 TREE_OPERAND (param, 0), param_num);
9274 return;
9277 if (TREE_CODE (param) == CALL_EXPR)
9279 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9280 tree attrs;
9281 bool found_format_arg = false;
9283 /* See if this is a call to a known internationalization function
9284 that modifies a format arg. Such a function may have multiple
9285 format_arg attributes (for example, ngettext). */
9287 for (attrs = TYPE_ATTRIBUTES (type);
9288 attrs;
9289 attrs = TREE_CHAIN (attrs))
9290 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9292 tree inner_arg;
9293 tree format_num_expr;
9294 int format_num;
9295 int i;
9296 call_expr_arg_iterator iter;
9298 /* Extract the argument number, which was previously checked
9299 to be valid. */
9300 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9302 format_num = tree_to_uhwi (format_num_expr);
9304 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9305 inner_arg != 0;
9306 inner_arg = next_call_expr_arg (&iter), i++)
9307 if (i == format_num)
9309 check_function_arguments_recurse (callback, ctx,
9310 inner_arg, param_num);
9311 found_format_arg = true;
9312 break;
9316 /* If we found a format_arg attribute and did a recursive check,
9317 we are done with checking this argument. Otherwise, we continue
9318 and this will be considered a non-literal. */
9319 if (found_format_arg)
9320 return;
9323 if (TREE_CODE (param) == COND_EXPR)
9325 /* Check both halves of the conditional expression. */
9326 check_function_arguments_recurse (callback, ctx,
9327 TREE_OPERAND (param, 1), param_num);
9328 check_function_arguments_recurse (callback, ctx,
9329 TREE_OPERAND (param, 2), param_num);
9330 return;
9333 (*callback) (ctx, param, param_num);
9336 /* Checks for a builtin function FNDECL that the number of arguments
9337 NARGS against the required number REQUIRED and issues an error if
9338 there is a mismatch. Returns true if the number of arguments is
9339 correct, otherwise false. */
9341 static bool
9342 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9344 if (nargs < required)
9346 error_at (input_location,
9347 "not enough arguments to function %qE", fndecl);
9348 return false;
9350 else if (nargs > required)
9352 error_at (input_location,
9353 "too many arguments to function %qE", fndecl);
9354 return false;
9356 return true;
9359 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9360 Returns false if there was an error, otherwise true. */
9362 bool
9363 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9365 if (!DECL_BUILT_IN (fndecl)
9366 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9367 return true;
9369 switch (DECL_FUNCTION_CODE (fndecl))
9371 case BUILT_IN_CONSTANT_P:
9372 return builtin_function_validate_nargs (fndecl, nargs, 1);
9374 case BUILT_IN_ISFINITE:
9375 case BUILT_IN_ISINF:
9376 case BUILT_IN_ISINF_SIGN:
9377 case BUILT_IN_ISNAN:
9378 case BUILT_IN_ISNORMAL:
9379 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9381 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9383 error ("non-floating-point argument in call to "
9384 "function %qE", fndecl);
9385 return false;
9387 return true;
9389 return false;
9391 case BUILT_IN_ISGREATER:
9392 case BUILT_IN_ISGREATEREQUAL:
9393 case BUILT_IN_ISLESS:
9394 case BUILT_IN_ISLESSEQUAL:
9395 case BUILT_IN_ISLESSGREATER:
9396 case BUILT_IN_ISUNORDERED:
9397 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9399 enum tree_code code0, code1;
9400 code0 = TREE_CODE (TREE_TYPE (args[0]));
9401 code1 = TREE_CODE (TREE_TYPE (args[1]));
9402 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9403 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9404 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9406 error ("non-floating-point arguments in call to "
9407 "function %qE", fndecl);
9408 return false;
9410 return true;
9412 return false;
9414 case BUILT_IN_FPCLASSIFY:
9415 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9417 unsigned i;
9419 for (i=0; i<5; i++)
9420 if (TREE_CODE (args[i]) != INTEGER_CST)
9422 error ("non-const integer argument %u in call to function %qE",
9423 i+1, fndecl);
9424 return false;
9427 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9429 error ("non-floating-point argument in call to function %qE",
9430 fndecl);
9431 return false;
9433 return true;
9435 return false;
9437 case BUILT_IN_ASSUME_ALIGNED:
9438 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9440 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9442 error ("non-integer argument 3 in call to function %qE", fndecl);
9443 return false;
9445 return true;
9447 return false;
9449 default:
9450 return true;
9454 /* Function to help qsort sort FIELD_DECLs by name order. */
9457 field_decl_cmp (const void *x_p, const void *y_p)
9459 const tree *const x = (const tree *const) x_p;
9460 const tree *const y = (const tree *const) y_p;
9462 if (DECL_NAME (*x) == DECL_NAME (*y))
9463 /* A nontype is "greater" than a type. */
9464 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9465 if (DECL_NAME (*x) == NULL_TREE)
9466 return -1;
9467 if (DECL_NAME (*y) == NULL_TREE)
9468 return 1;
9469 if (DECL_NAME (*x) < DECL_NAME (*y))
9470 return -1;
9471 return 1;
9474 static struct {
9475 gt_pointer_operator new_value;
9476 void *cookie;
9477 } resort_data;
9479 /* This routine compares two fields like field_decl_cmp but using the
9480 pointer operator in resort_data. */
9482 static int
9483 resort_field_decl_cmp (const void *x_p, const void *y_p)
9485 const tree *const x = (const tree *const) x_p;
9486 const tree *const y = (const tree *const) y_p;
9488 if (DECL_NAME (*x) == DECL_NAME (*y))
9489 /* A nontype is "greater" than a type. */
9490 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9491 if (DECL_NAME (*x) == NULL_TREE)
9492 return -1;
9493 if (DECL_NAME (*y) == NULL_TREE)
9494 return 1;
9496 tree d1 = DECL_NAME (*x);
9497 tree d2 = DECL_NAME (*y);
9498 resort_data.new_value (&d1, resort_data.cookie);
9499 resort_data.new_value (&d2, resort_data.cookie);
9500 if (d1 < d2)
9501 return -1;
9503 return 1;
9506 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9508 void
9509 resort_sorted_fields (void *obj,
9510 void * ARG_UNUSED (orig_obj),
9511 gt_pointer_operator new_value,
9512 void *cookie)
9514 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9515 resort_data.new_value = new_value;
9516 resort_data.cookie = cookie;
9517 qsort (&sf->elts[0], sf->len, sizeof (tree),
9518 resort_field_decl_cmp);
9521 /* Subroutine of c_parse_error.
9522 Return the result of concatenating LHS and RHS. RHS is really
9523 a string literal, its first character is indicated by RHS_START and
9524 RHS_SIZE is its length (including the terminating NUL character).
9526 The caller is responsible for deleting the returned pointer. */
9528 static char *
9529 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9531 const int lhs_size = strlen (lhs);
9532 char *result = XNEWVEC (char, lhs_size + rhs_size);
9533 strncpy (result, lhs, lhs_size);
9534 strncpy (result + lhs_size, rhs_start, rhs_size);
9535 return result;
9538 /* Issue the error given by GMSGID, indicating that it occurred before
9539 TOKEN, which had the associated VALUE. */
9541 void
9542 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9543 tree value, unsigned char token_flags)
9545 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9547 char *message = NULL;
9549 if (token_type == CPP_EOF)
9550 message = catenate_messages (gmsgid, " at end of input");
9551 else if (token_type == CPP_CHAR
9552 || token_type == CPP_WCHAR
9553 || token_type == CPP_CHAR16
9554 || token_type == CPP_CHAR32)
9556 unsigned int val = TREE_INT_CST_LOW (value);
9557 const char *prefix;
9559 switch (token_type)
9561 default:
9562 prefix = "";
9563 break;
9564 case CPP_WCHAR:
9565 prefix = "L";
9566 break;
9567 case CPP_CHAR16:
9568 prefix = "u";
9569 break;
9570 case CPP_CHAR32:
9571 prefix = "U";
9572 break;
9575 if (val <= UCHAR_MAX && ISGRAPH (val))
9576 message = catenate_messages (gmsgid, " before %s'%c'");
9577 else
9578 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9580 error (message, prefix, val);
9581 free (message);
9582 message = NULL;
9584 else if (token_type == CPP_CHAR_USERDEF
9585 || token_type == CPP_WCHAR_USERDEF
9586 || token_type == CPP_CHAR16_USERDEF
9587 || token_type == CPP_CHAR32_USERDEF)
9588 message = catenate_messages (gmsgid,
9589 " before user-defined character literal");
9590 else if (token_type == CPP_STRING_USERDEF
9591 || token_type == CPP_WSTRING_USERDEF
9592 || token_type == CPP_STRING16_USERDEF
9593 || token_type == CPP_STRING32_USERDEF
9594 || token_type == CPP_UTF8STRING_USERDEF)
9595 message = catenate_messages (gmsgid, " before user-defined string literal");
9596 else if (token_type == CPP_STRING
9597 || token_type == CPP_WSTRING
9598 || token_type == CPP_STRING16
9599 || token_type == CPP_STRING32
9600 || token_type == CPP_UTF8STRING)
9601 message = catenate_messages (gmsgid, " before string constant");
9602 else if (token_type == CPP_NUMBER)
9603 message = catenate_messages (gmsgid, " before numeric constant");
9604 else if (token_type == CPP_NAME)
9606 message = catenate_messages (gmsgid, " before %qE");
9607 error (message, value);
9608 free (message);
9609 message = NULL;
9611 else if (token_type == CPP_PRAGMA)
9612 message = catenate_messages (gmsgid, " before %<#pragma%>");
9613 else if (token_type == CPP_PRAGMA_EOL)
9614 message = catenate_messages (gmsgid, " before end of line");
9615 else if (token_type == CPP_DECLTYPE)
9616 message = catenate_messages (gmsgid, " before %<decltype%>");
9617 else if (token_type < N_TTYPES)
9619 message = catenate_messages (gmsgid, " before %qs token");
9620 error (message, cpp_type2name (token_type, token_flags));
9621 free (message);
9622 message = NULL;
9624 else
9625 error (gmsgid);
9627 if (message)
9629 error (message);
9630 free (message);
9632 #undef catenate_messages
9635 /* Mapping for cpp message reasons to the options that enable them. */
9637 struct reason_option_codes_t
9639 const int reason; /* cpplib message reason. */
9640 const int option_code; /* gcc option that controls this message. */
9643 static const struct reason_option_codes_t option_codes[] = {
9644 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9645 {CPP_W_COMMENTS, OPT_Wcomment},
9646 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9647 {CPP_W_MULTICHAR, OPT_Wmultichar},
9648 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9649 {CPP_W_LONG_LONG, OPT_Wlong_long},
9650 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9651 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9652 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9653 {CPP_W_UNDEF, OPT_Wundef},
9654 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9655 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9656 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9657 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9658 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9659 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9660 {CPP_W_DATE_TIME, OPT_Wdate_time},
9661 {CPP_W_NONE, 0}
9664 /* Return the gcc option code associated with the reason for a cpp
9665 message, or 0 if none. */
9667 static int
9668 c_option_controlling_cpp_error (int reason)
9670 const struct reason_option_codes_t *entry;
9672 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9674 if (entry->reason == reason)
9675 return entry->option_code;
9677 return 0;
9680 /* Callback from cpp_error for PFILE to print diagnostics from the
9681 preprocessor. The diagnostic is of type LEVEL, with REASON set
9682 to the reason code if LEVEL is represents a warning, at location
9683 LOCATION unless this is after lexing and the compiler's location
9684 should be used instead, with column number possibly overridden by
9685 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9686 the arguments. Returns true if a diagnostic was emitted, false
9687 otherwise. */
9689 bool
9690 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9691 location_t location, unsigned int column_override,
9692 const char *msg, va_list *ap)
9694 diagnostic_info diagnostic;
9695 diagnostic_t dlevel;
9696 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9697 bool ret;
9699 switch (level)
9701 case CPP_DL_WARNING_SYSHDR:
9702 if (flag_no_output)
9703 return false;
9704 global_dc->dc_warn_system_headers = 1;
9705 /* Fall through. */
9706 case CPP_DL_WARNING:
9707 if (flag_no_output)
9708 return false;
9709 dlevel = DK_WARNING;
9710 break;
9711 case CPP_DL_PEDWARN:
9712 if (flag_no_output && !flag_pedantic_errors)
9713 return false;
9714 dlevel = DK_PEDWARN;
9715 break;
9716 case CPP_DL_ERROR:
9717 dlevel = DK_ERROR;
9718 break;
9719 case CPP_DL_ICE:
9720 dlevel = DK_ICE;
9721 break;
9722 case CPP_DL_NOTE:
9723 dlevel = DK_NOTE;
9724 break;
9725 case CPP_DL_FATAL:
9726 dlevel = DK_FATAL;
9727 break;
9728 default:
9729 gcc_unreachable ();
9731 if (done_lexing)
9732 location = input_location;
9733 diagnostic_set_info_translated (&diagnostic, msg, ap,
9734 location, dlevel);
9735 if (column_override)
9736 diagnostic_override_column (&diagnostic, column_override);
9737 diagnostic_override_option_index (&diagnostic,
9738 c_option_controlling_cpp_error (reason));
9739 ret = report_diagnostic (&diagnostic);
9740 if (level == CPP_DL_WARNING_SYSHDR)
9741 global_dc->dc_warn_system_headers = save_warn_system_headers;
9742 return ret;
9745 /* Convert a character from the host to the target execution character
9746 set. cpplib handles this, mostly. */
9748 HOST_WIDE_INT
9749 c_common_to_target_charset (HOST_WIDE_INT c)
9751 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9752 zero-extended under -fno-signed-char. cpplib insists that characters
9753 and character constants are always unsigned. Hence we must convert
9754 back and forth. */
9755 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9757 uc = cpp_host_to_exec_charset (parse_in, uc);
9759 if (flag_signed_char)
9760 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9761 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9762 else
9763 return uc;
9766 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9767 references with an INDIRECT_REF of a constant at the bottom; much like the
9768 traditional rendering of offsetof as a macro. Return the folded result. */
9770 tree
9771 fold_offsetof_1 (tree expr)
9773 tree base, off, t;
9775 switch (TREE_CODE (expr))
9777 case ERROR_MARK:
9778 return expr;
9780 case VAR_DECL:
9781 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9782 return error_mark_node;
9784 case CALL_EXPR:
9785 case TARGET_EXPR:
9786 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9787 return error_mark_node;
9789 case NOP_EXPR:
9790 case INDIRECT_REF:
9791 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9793 error ("cannot apply %<offsetof%> to a non constant address");
9794 return error_mark_node;
9796 return TREE_OPERAND (expr, 0);
9798 case COMPONENT_REF:
9799 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9800 if (base == error_mark_node)
9801 return base;
9803 t = TREE_OPERAND (expr, 1);
9804 if (DECL_C_BIT_FIELD (t))
9806 error ("attempt to take address of bit-field structure "
9807 "member %qD", t);
9808 return error_mark_node;
9810 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9811 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
9812 / BITS_PER_UNIT));
9813 break;
9815 case ARRAY_REF:
9816 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9817 if (base == error_mark_node)
9818 return base;
9820 t = TREE_OPERAND (expr, 1);
9822 /* Check if the offset goes beyond the upper bound of the array. */
9823 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9825 tree upbound = array_ref_up_bound (expr);
9826 if (upbound != NULL_TREE
9827 && TREE_CODE (upbound) == INTEGER_CST
9828 && !tree_int_cst_equal (upbound,
9829 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9831 upbound = size_binop (PLUS_EXPR, upbound,
9832 build_int_cst (TREE_TYPE (upbound), 1));
9833 if (tree_int_cst_lt (upbound, t))
9835 tree v;
9837 for (v = TREE_OPERAND (expr, 0);
9838 TREE_CODE (v) == COMPONENT_REF;
9839 v = TREE_OPERAND (v, 0))
9840 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9841 == RECORD_TYPE)
9843 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9844 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9845 if (TREE_CODE (fld_chain) == FIELD_DECL)
9846 break;
9848 if (fld_chain)
9849 break;
9851 /* Don't warn if the array might be considered a poor
9852 man's flexible array member with a very permissive
9853 definition thereof. */
9854 if (TREE_CODE (v) == ARRAY_REF
9855 || TREE_CODE (v) == COMPONENT_REF)
9856 warning (OPT_Warray_bounds,
9857 "index %E denotes an offset "
9858 "greater than size of %qT",
9859 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9864 t = convert (sizetype, t);
9865 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9866 break;
9868 case COMPOUND_EXPR:
9869 /* Handle static members of volatile structs. */
9870 t = TREE_OPERAND (expr, 1);
9871 gcc_assert (TREE_CODE (t) == VAR_DECL);
9872 return fold_offsetof_1 (t);
9874 default:
9875 gcc_unreachable ();
9878 return fold_build_pointer_plus (base, off);
9881 /* Likewise, but convert it to the return type of offsetof. */
9883 tree
9884 fold_offsetof (tree expr)
9886 return convert (size_type_node, fold_offsetof_1 (expr));
9889 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9890 expression, because B will always be true. */
9892 void
9893 warn_for_omitted_condop (location_t location, tree cond)
9895 if (truth_value_p (TREE_CODE (cond)))
9896 warning_at (location, OPT_Wparentheses,
9897 "the omitted middle operand in ?: will always be %<true%>, "
9898 "suggest explicit middle operand");
9901 /* Give an error for storing into ARG, which is 'const'. USE indicates
9902 how ARG was being used. */
9904 void
9905 readonly_error (location_t loc, tree arg, enum lvalue_use use)
9907 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9908 || use == lv_asm);
9909 /* Using this macro rather than (for example) arrays of messages
9910 ensures that all the format strings are checked at compile
9911 time. */
9912 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9913 : (use == lv_increment ? (I) \
9914 : (use == lv_decrement ? (D) : (AS))))
9915 if (TREE_CODE (arg) == COMPONENT_REF)
9917 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9918 error_at (loc, READONLY_MSG (G_("assignment of member "
9919 "%qD in read-only object"),
9920 G_("increment of member "
9921 "%qD in read-only object"),
9922 G_("decrement of member "
9923 "%qD in read-only object"),
9924 G_("member %qD in read-only object "
9925 "used as %<asm%> output")),
9926 TREE_OPERAND (arg, 1));
9927 else
9928 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
9929 G_("increment of read-only member %qD"),
9930 G_("decrement of read-only member %qD"),
9931 G_("read-only member %qD used as %<asm%> output")),
9932 TREE_OPERAND (arg, 1));
9934 else if (TREE_CODE (arg) == VAR_DECL)
9935 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
9936 G_("increment of read-only variable %qD"),
9937 G_("decrement of read-only variable %qD"),
9938 G_("read-only variable %qD used as %<asm%> output")),
9939 arg);
9940 else if (TREE_CODE (arg) == PARM_DECL)
9941 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
9942 G_("increment of read-only parameter %qD"),
9943 G_("decrement of read-only parameter %qD"),
9944 G_("read-only parameter %qD use as %<asm%> output")),
9945 arg);
9946 else if (TREE_CODE (arg) == RESULT_DECL)
9948 gcc_assert (c_dialect_cxx ());
9949 error_at (loc, READONLY_MSG (G_("assignment of "
9950 "read-only named return value %qD"),
9951 G_("increment of "
9952 "read-only named return value %qD"),
9953 G_("decrement of "
9954 "read-only named return value %qD"),
9955 G_("read-only named return value %qD "
9956 "used as %<asm%>output")),
9957 arg);
9959 else if (TREE_CODE (arg) == FUNCTION_DECL)
9960 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
9961 G_("increment of function %qD"),
9962 G_("decrement of function %qD"),
9963 G_("function %qD used as %<asm%> output")),
9964 arg);
9965 else
9966 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
9967 G_("increment of read-only location %qE"),
9968 G_("decrement of read-only location %qE"),
9969 G_("read-only location %qE used as %<asm%> output")),
9970 arg);
9973 /* Print an error message for an invalid lvalue. USE says
9974 how the lvalue is being used and so selects the error message. LOC
9975 is the location for the error. */
9977 void
9978 lvalue_error (location_t loc, enum lvalue_use use)
9980 switch (use)
9982 case lv_assign:
9983 error_at (loc, "lvalue required as left operand of assignment");
9984 break;
9985 case lv_increment:
9986 error_at (loc, "lvalue required as increment operand");
9987 break;
9988 case lv_decrement:
9989 error_at (loc, "lvalue required as decrement operand");
9990 break;
9991 case lv_addressof:
9992 error_at (loc, "lvalue required as unary %<&%> operand");
9993 break;
9994 case lv_asm:
9995 error_at (loc, "lvalue required in asm statement");
9996 break;
9997 default:
9998 gcc_unreachable ();
10002 /* Print an error message for an invalid indirection of type TYPE.
10003 ERRSTRING is the name of the operator for the indirection. */
10005 void
10006 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10008 switch (errstring)
10010 case RO_NULL:
10011 gcc_assert (c_dialect_cxx ());
10012 error_at (loc, "invalid type argument (have %qT)", type);
10013 break;
10014 case RO_ARRAY_INDEXING:
10015 error_at (loc,
10016 "invalid type argument of array indexing (have %qT)",
10017 type);
10018 break;
10019 case RO_UNARY_STAR:
10020 error_at (loc,
10021 "invalid type argument of unary %<*%> (have %qT)",
10022 type);
10023 break;
10024 case RO_ARROW:
10025 error_at (loc,
10026 "invalid type argument of %<->%> (have %qT)",
10027 type);
10028 break;
10029 case RO_ARROW_STAR:
10030 error_at (loc,
10031 "invalid type argument of %<->*%> (have %qT)",
10032 type);
10033 break;
10034 case RO_IMPLICIT_CONVERSION:
10035 error_at (loc,
10036 "invalid type argument of implicit conversion (have %qT)",
10037 type);
10038 break;
10039 default:
10040 gcc_unreachable ();
10044 /* *PTYPE is an incomplete array. Complete it with a domain based on
10045 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10046 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10047 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10050 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10052 tree maxindex, type, main_type, elt, unqual_elt;
10053 int failure = 0, quals;
10054 hashval_t hashcode = 0;
10055 bool overflow_p = false;
10057 maxindex = size_zero_node;
10058 if (initial_value)
10060 if (TREE_CODE (initial_value) == STRING_CST)
10062 int eltsize
10063 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10064 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10066 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10068 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10070 if (vec_safe_is_empty (v))
10072 if (pedantic)
10073 failure = 3;
10074 maxindex = ssize_int (-1);
10076 else
10078 tree curindex;
10079 unsigned HOST_WIDE_INT cnt;
10080 constructor_elt *ce;
10081 bool fold_p = false;
10083 if ((*v)[0].index)
10084 maxindex = (*v)[0].index, fold_p = true;
10086 curindex = maxindex;
10088 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10090 bool curfold_p = false;
10091 if (ce->index)
10092 curindex = ce->index, curfold_p = true;
10093 else
10095 if (fold_p)
10097 /* Since we treat size types now as ordinary
10098 unsigned types, we need an explicit overflow
10099 check. */
10100 tree orig = curindex;
10101 curindex = fold_convert (sizetype, curindex);
10102 overflow_p |= tree_int_cst_lt (curindex, orig);
10104 curindex = size_binop (PLUS_EXPR, curindex,
10105 size_one_node);
10107 if (tree_int_cst_lt (maxindex, curindex))
10108 maxindex = curindex, fold_p = curfold_p;
10110 if (fold_p)
10112 tree orig = maxindex;
10113 maxindex = fold_convert (sizetype, maxindex);
10114 overflow_p |= tree_int_cst_lt (maxindex, orig);
10118 else
10120 /* Make an error message unless that happened already. */
10121 if (initial_value != error_mark_node)
10122 failure = 1;
10125 else
10127 failure = 2;
10128 if (!do_default)
10129 return failure;
10132 type = *ptype;
10133 elt = TREE_TYPE (type);
10134 quals = TYPE_QUALS (strip_array_types (elt));
10135 if (quals == 0)
10136 unqual_elt = elt;
10137 else
10138 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10140 /* Using build_distinct_type_copy and modifying things afterward instead
10141 of using build_array_type to create a new type preserves all of the
10142 TYPE_LANG_FLAG_? bits that the front end may have set. */
10143 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10144 TREE_TYPE (main_type) = unqual_elt;
10145 TYPE_DOMAIN (main_type)
10146 = build_range_type (TREE_TYPE (maxindex),
10147 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10148 layout_type (main_type);
10150 /* Make sure we have the canonical MAIN_TYPE. */
10151 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10152 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10153 hashcode);
10154 main_type = type_hash_canon (hashcode, main_type);
10156 /* Fix the canonical type. */
10157 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10158 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10159 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10160 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10161 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10162 != TYPE_DOMAIN (main_type)))
10163 TYPE_CANONICAL (main_type)
10164 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10165 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10166 else
10167 TYPE_CANONICAL (main_type) = main_type;
10169 if (quals == 0)
10170 type = main_type;
10171 else
10172 type = c_build_qualified_type (main_type, quals);
10174 if (COMPLETE_TYPE_P (type)
10175 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10176 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10178 error ("size of array is too large");
10179 /* If we proceed with the array type as it is, we'll eventually
10180 crash in tree_to_[su]hwi(). */
10181 type = error_mark_node;
10184 *ptype = type;
10185 return failure;
10188 /* Like c_mark_addressable but don't check register qualifier. */
10189 void
10190 c_common_mark_addressable_vec (tree t)
10192 while (handled_component_p (t))
10193 t = TREE_OPERAND (t, 0);
10194 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10195 return;
10196 TREE_ADDRESSABLE (t) = 1;
10201 /* Used to help initialize the builtin-types.def table. When a type of
10202 the correct size doesn't exist, use error_mark_node instead of NULL.
10203 The later results in segfaults even when a decl using the type doesn't
10204 get invoked. */
10206 tree
10207 builtin_type_for_size (int size, bool unsignedp)
10209 tree type = c_common_type_for_size (size, unsignedp);
10210 return type ? type : error_mark_node;
10213 /* A helper function for resolve_overloaded_builtin in resolving the
10214 overloaded __sync_ builtins. Returns a positive power of 2 if the
10215 first operand of PARAMS is a pointer to a supported data type.
10216 Returns 0 if an error is encountered. */
10218 static int
10219 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10221 tree type;
10222 int size;
10224 if (!params)
10226 error ("too few arguments to function %qE", function);
10227 return 0;
10230 type = TREE_TYPE ((*params)[0]);
10231 if (TREE_CODE (type) == ARRAY_TYPE)
10233 /* Force array-to-pointer decay for C++. */
10234 gcc_assert (c_dialect_cxx());
10235 (*params)[0] = default_conversion ((*params)[0]);
10236 type = TREE_TYPE ((*params)[0]);
10238 if (TREE_CODE (type) != POINTER_TYPE)
10239 goto incompatible;
10241 type = TREE_TYPE (type);
10242 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10243 goto incompatible;
10245 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10246 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10247 return size;
10249 incompatible:
10250 error ("incompatible type for argument %d of %qE", 1, function);
10251 return 0;
10254 /* A helper function for resolve_overloaded_builtin. Adds casts to
10255 PARAMS to make arguments match up with those of FUNCTION. Drops
10256 the variadic arguments at the end. Returns false if some error
10257 was encountered; true on success. */
10259 static bool
10260 sync_resolve_params (location_t loc, tree orig_function, tree function,
10261 vec<tree, va_gc> *params, bool orig_format)
10263 function_args_iterator iter;
10264 tree ptype;
10265 unsigned int parmnum;
10267 function_args_iter_init (&iter, TREE_TYPE (function));
10268 /* We've declared the implementation functions to use "volatile void *"
10269 as the pointer parameter, so we shouldn't get any complaints from the
10270 call to check_function_arguments what ever type the user used. */
10271 function_args_iter_next (&iter);
10272 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10273 ptype = TYPE_MAIN_VARIANT (ptype);
10275 /* For the rest of the values, we need to cast these to FTYPE, so that we
10276 don't get warnings for passing pointer types, etc. */
10277 parmnum = 0;
10278 while (1)
10280 tree val, arg_type;
10282 arg_type = function_args_iter_cond (&iter);
10283 /* XXX void_type_node belies the abstraction. */
10284 if (arg_type == void_type_node)
10285 break;
10287 ++parmnum;
10288 if (params->length () <= parmnum)
10290 error_at (loc, "too few arguments to function %qE", orig_function);
10291 return false;
10294 /* Only convert parameters if arg_type is unsigned integer type with
10295 new format sync routines, i.e. don't attempt to convert pointer
10296 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10297 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10298 kinds). */
10299 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10301 /* Ideally for the first conversion we'd use convert_for_assignment
10302 so that we get warnings for anything that doesn't match the pointer
10303 type. This isn't portable across the C and C++ front ends atm. */
10304 val = (*params)[parmnum];
10305 val = convert (ptype, val);
10306 val = convert (arg_type, val);
10307 (*params)[parmnum] = val;
10310 function_args_iter_next (&iter);
10313 /* __atomic routines are not variadic. */
10314 if (!orig_format && params->length () != parmnum + 1)
10316 error_at (loc, "too many arguments to function %qE", orig_function);
10317 return false;
10320 /* The definition of these primitives is variadic, with the remaining
10321 being "an optional list of variables protected by the memory barrier".
10322 No clue what that's supposed to mean, precisely, but we consider all
10323 call-clobbered variables to be protected so we're safe. */
10324 params->truncate (parmnum + 1);
10326 return true;
10329 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10330 RESULT to make it match the type of the first pointer argument in
10331 PARAMS. */
10333 static tree
10334 sync_resolve_return (tree first_param, tree result, bool orig_format)
10336 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10337 tree rtype = TREE_TYPE (result);
10338 ptype = TYPE_MAIN_VARIANT (ptype);
10340 /* New format doesn't require casting unless the types are the same size. */
10341 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10342 return convert (ptype, result);
10343 else
10344 return result;
10347 /* This function verifies the PARAMS to generic atomic FUNCTION.
10348 It returns the size if all the parameters are the same size, otherwise
10349 0 is returned if the parameters are invalid. */
10351 static int
10352 get_atomic_generic_size (location_t loc, tree function,
10353 vec<tree, va_gc> *params)
10355 unsigned int n_param;
10356 unsigned int n_model;
10357 unsigned int x;
10358 int size_0;
10359 tree type_0;
10361 /* Determine the parameter makeup. */
10362 switch (DECL_FUNCTION_CODE (function))
10364 case BUILT_IN_ATOMIC_EXCHANGE:
10365 n_param = 4;
10366 n_model = 1;
10367 break;
10368 case BUILT_IN_ATOMIC_LOAD:
10369 case BUILT_IN_ATOMIC_STORE:
10370 n_param = 3;
10371 n_model = 1;
10372 break;
10373 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10374 n_param = 6;
10375 n_model = 2;
10376 break;
10377 default:
10378 gcc_unreachable ();
10381 if (vec_safe_length (params) != n_param)
10383 error_at (loc, "incorrect number of arguments to function %qE", function);
10384 return 0;
10387 /* Get type of first parameter, and determine its size. */
10388 type_0 = TREE_TYPE ((*params)[0]);
10389 if (TREE_CODE (type_0) == ARRAY_TYPE)
10391 /* Force array-to-pointer decay for C++. */
10392 gcc_assert (c_dialect_cxx());
10393 (*params)[0] = default_conversion ((*params)[0]);
10394 type_0 = TREE_TYPE ((*params)[0]);
10396 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10398 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10399 function);
10400 return 0;
10403 /* Types must be compile time constant sizes. */
10404 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10406 error_at (loc,
10407 "argument 1 of %qE must be a pointer to a constant size type",
10408 function);
10409 return 0;
10412 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10414 /* Zero size objects are not allowed. */
10415 if (size_0 == 0)
10417 error_at (loc,
10418 "argument 1 of %qE must be a pointer to a nonzero size object",
10419 function);
10420 return 0;
10423 /* Check each other parameter is a pointer and the same size. */
10424 for (x = 0; x < n_param - n_model; x++)
10426 int size;
10427 tree type = TREE_TYPE ((*params)[x]);
10428 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10429 if (n_param == 6 && x == 3)
10430 continue;
10431 if (!POINTER_TYPE_P (type))
10433 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10434 function);
10435 return 0;
10437 size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
10438 if (size != size_0)
10440 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10441 function);
10442 return 0;
10446 /* Check memory model parameters for validity. */
10447 for (x = n_param - n_model ; x < n_param; x++)
10449 tree p = (*params)[x];
10450 if (TREE_CODE (p) == INTEGER_CST)
10452 int i = tree_to_uhwi (p);
10453 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10455 warning_at (loc, OPT_Winvalid_memory_model,
10456 "invalid memory model argument %d of %qE", x + 1,
10457 function);
10460 else
10461 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10463 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10464 function);
10465 return 0;
10469 return size_0;
10473 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10474 at the beginning of the parameter list PARAMS representing the size of the
10475 objects. This is to match the library ABI requirement. LOC is the location
10476 of the function call.
10477 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10478 returned to allow the external call to be constructed. */
10480 static tree
10481 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10482 vec<tree, va_gc> *params)
10484 tree size_node;
10486 /* Insert a SIZE_T parameter as the first param. If there isn't
10487 enough space, allocate a new vector and recursively re-build with that. */
10488 if (!params->space (1))
10490 unsigned int z, len;
10491 vec<tree, va_gc> *v;
10492 tree f;
10494 len = params->length ();
10495 vec_alloc (v, len + 1);
10496 v->quick_push (build_int_cst (size_type_node, n));
10497 for (z = 0; z < len; z++)
10498 v->quick_push ((*params)[z]);
10499 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10500 vec_free (v);
10501 return f;
10504 /* Add the size parameter and leave as a function call for processing. */
10505 size_node = build_int_cst (size_type_node, n);
10506 params->quick_insert (0, size_node);
10507 return NULL_TREE;
10511 /* Return whether atomic operations for naturally aligned N-byte
10512 arguments are supported, whether inline or through libatomic. */
10513 static bool
10514 atomic_size_supported_p (int n)
10516 switch (n)
10518 case 1:
10519 case 2:
10520 case 4:
10521 case 8:
10522 return true;
10524 case 16:
10525 return targetm.scalar_mode_supported_p (TImode);
10527 default:
10528 return false;
10532 /* This will process an __atomic_exchange function call, determine whether it
10533 needs to be mapped to the _N variation, or turned into a library call.
10534 LOC is the location of the builtin call.
10535 FUNCTION is the DECL that has been invoked;
10536 PARAMS is the argument list for the call. The return value is non-null
10537 TRUE is returned if it is translated into the proper format for a call to the
10538 external library, and NEW_RETURN is set the tree for that function.
10539 FALSE is returned if processing for the _N variation is required, and
10540 NEW_RETURN is set to the the return value the result is copied into. */
10541 static bool
10542 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10543 vec<tree, va_gc> *params, tree *new_return)
10545 tree p0, p1, p2, p3;
10546 tree I_type, I_type_ptr;
10547 int n = get_atomic_generic_size (loc, function, params);
10549 /* Size of 0 is an error condition. */
10550 if (n == 0)
10552 *new_return = error_mark_node;
10553 return true;
10556 /* If not a lock-free size, change to the library generic format. */
10557 if (!atomic_size_supported_p (n))
10559 *new_return = add_atomic_size_parameter (n, loc, function, params);
10560 return true;
10563 /* Otherwise there is a lockfree match, transform the call from:
10564 void fn(T* mem, T* desired, T* return, model)
10565 into
10566 *return = (T) (fn (In* mem, (In) *desired, model)) */
10568 p0 = (*params)[0];
10569 p1 = (*params)[1];
10570 p2 = (*params)[2];
10571 p3 = (*params)[3];
10573 /* Create pointer to appropriate size. */
10574 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10575 I_type_ptr = build_pointer_type (I_type);
10577 /* Convert object pointer to required type. */
10578 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10579 (*params)[0] = p0;
10580 /* Convert new value to required type, and dereference it. */
10581 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10582 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10583 (*params)[1] = p1;
10585 /* Move memory model to the 3rd position, and end param list. */
10586 (*params)[2] = p3;
10587 params->truncate (3);
10589 /* Convert return pointer and dereference it for later assignment. */
10590 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10592 return false;
10596 /* This will process an __atomic_compare_exchange function call, determine
10597 whether it needs to be mapped to the _N variation, or turned into a lib call.
10598 LOC is the location of the builtin call.
10599 FUNCTION is the DECL that has been invoked;
10600 PARAMS is the argument list for the call. The return value is non-null
10601 TRUE is returned if it is translated into the proper format for a call to the
10602 external library, and NEW_RETURN is set the tree for that function.
10603 FALSE is returned if processing for the _N variation is required. */
10605 static bool
10606 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10607 vec<tree, va_gc> *params,
10608 tree *new_return)
10610 tree p0, p1, p2;
10611 tree I_type, I_type_ptr;
10612 int n = get_atomic_generic_size (loc, function, params);
10614 /* Size of 0 is an error condition. */
10615 if (n == 0)
10617 *new_return = error_mark_node;
10618 return true;
10621 /* If not a lock-free size, change to the library generic format. */
10622 if (!atomic_size_supported_p (n))
10624 /* The library generic format does not have the weak parameter, so
10625 remove it from the param list. Since a parameter has been removed,
10626 we can be sure that there is room for the SIZE_T parameter, meaning
10627 there will not be a recursive rebuilding of the parameter list, so
10628 there is no danger this will be done twice. */
10629 if (n > 0)
10631 (*params)[3] = (*params)[4];
10632 (*params)[4] = (*params)[5];
10633 params->truncate (5);
10635 *new_return = add_atomic_size_parameter (n, loc, function, params);
10636 return true;
10639 /* Otherwise, there is a match, so the call needs to be transformed from:
10640 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10641 into
10642 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10644 p0 = (*params)[0];
10645 p1 = (*params)[1];
10646 p2 = (*params)[2];
10648 /* Create pointer to appropriate size. */
10649 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10650 I_type_ptr = build_pointer_type (I_type);
10652 /* Convert object pointer to required type. */
10653 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10654 (*params)[0] = p0;
10656 /* Convert expected pointer to required type. */
10657 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10658 (*params)[1] = p1;
10660 /* Convert desired value to required type, and dereference it. */
10661 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10662 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10663 (*params)[2] = p2;
10665 /* The rest of the parameters are fine. NULL means no special return value
10666 processing.*/
10667 *new_return = NULL;
10668 return false;
10672 /* This will process an __atomic_load function call, determine whether it
10673 needs to be mapped to the _N variation, or turned into a library call.
10674 LOC is the location of the builtin call.
10675 FUNCTION is the DECL that has been invoked;
10676 PARAMS is the argument list for the call. The return value is non-null
10677 TRUE is returned if it is translated into the proper format for a call to the
10678 external library, and NEW_RETURN is set the tree for that function.
10679 FALSE is returned if processing for the _N variation is required, and
10680 NEW_RETURN is set to the the return value the result is copied into. */
10682 static bool
10683 resolve_overloaded_atomic_load (location_t loc, tree function,
10684 vec<tree, va_gc> *params, tree *new_return)
10686 tree p0, p1, p2;
10687 tree I_type, I_type_ptr;
10688 int n = get_atomic_generic_size (loc, function, params);
10690 /* Size of 0 is an error condition. */
10691 if (n == 0)
10693 *new_return = error_mark_node;
10694 return true;
10697 /* If not a lock-free size, change to the library generic format. */
10698 if (!atomic_size_supported_p (n))
10700 *new_return = add_atomic_size_parameter (n, loc, function, params);
10701 return true;
10704 /* Otherwise, there is a match, so the call needs to be transformed from:
10705 void fn(T* mem, T* return, model)
10706 into
10707 *return = (T) (fn ((In *) mem, model)) */
10709 p0 = (*params)[0];
10710 p1 = (*params)[1];
10711 p2 = (*params)[2];
10713 /* Create pointer to appropriate size. */
10714 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10715 I_type_ptr = build_pointer_type (I_type);
10717 /* Convert object pointer to required type. */
10718 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10719 (*params)[0] = p0;
10721 /* Move memory model to the 2nd position, and end param list. */
10722 (*params)[1] = p2;
10723 params->truncate (2);
10725 /* Convert return pointer and dereference it for later assignment. */
10726 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10728 return false;
10732 /* This will process an __atomic_store function call, determine whether it
10733 needs to be mapped to the _N variation, or turned into a library call.
10734 LOC is the location of the builtin call.
10735 FUNCTION is the DECL that has been invoked;
10736 PARAMS is the argument list for the call. The return value is non-null
10737 TRUE is returned if it is translated into the proper format for a call to the
10738 external library, and NEW_RETURN is set the tree for that function.
10739 FALSE is returned if processing for the _N variation is required, and
10740 NEW_RETURN is set to the the return value the result is copied into. */
10742 static bool
10743 resolve_overloaded_atomic_store (location_t loc, tree function,
10744 vec<tree, va_gc> *params, tree *new_return)
10746 tree p0, p1;
10747 tree I_type, I_type_ptr;
10748 int n = get_atomic_generic_size (loc, function, params);
10750 /* Size of 0 is an error condition. */
10751 if (n == 0)
10753 *new_return = error_mark_node;
10754 return true;
10757 /* If not a lock-free size, change to the library generic format. */
10758 if (!atomic_size_supported_p (n))
10760 *new_return = add_atomic_size_parameter (n, loc, function, params);
10761 return true;
10764 /* Otherwise, there is a match, so the call needs to be transformed from:
10765 void fn(T* mem, T* value, model)
10766 into
10767 fn ((In *) mem, (In) *value, model) */
10769 p0 = (*params)[0];
10770 p1 = (*params)[1];
10772 /* Create pointer to appropriate size. */
10773 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10774 I_type_ptr = build_pointer_type (I_type);
10776 /* Convert object pointer to required type. */
10777 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10778 (*params)[0] = p0;
10780 /* Convert new value to required type, and dereference it. */
10781 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10782 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10783 (*params)[1] = p1;
10785 /* The memory model is in the right spot already. Return is void. */
10786 *new_return = NULL_TREE;
10788 return false;
10792 /* Some builtin functions are placeholders for other expressions. This
10793 function should be called immediately after parsing the call expression
10794 before surrounding code has committed to the type of the expression.
10796 LOC is the location of the builtin call.
10798 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10799 PARAMS is the argument list for the call. The return value is non-null
10800 when expansion is complete, and null if normal processing should
10801 continue. */
10803 tree
10804 resolve_overloaded_builtin (location_t loc, tree function,
10805 vec<tree, va_gc> *params)
10807 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10808 bool orig_format = true;
10809 tree new_return = NULL_TREE;
10811 switch (DECL_BUILT_IN_CLASS (function))
10813 case BUILT_IN_NORMAL:
10814 break;
10815 case BUILT_IN_MD:
10816 if (targetm.resolve_overloaded_builtin)
10817 return targetm.resolve_overloaded_builtin (loc, function, params);
10818 else
10819 return NULL_TREE;
10820 default:
10821 return NULL_TREE;
10824 /* Handle BUILT_IN_NORMAL here. */
10825 switch (orig_code)
10827 case BUILT_IN_ATOMIC_EXCHANGE:
10828 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10829 case BUILT_IN_ATOMIC_LOAD:
10830 case BUILT_IN_ATOMIC_STORE:
10832 /* Handle these 4 together so that they can fall through to the next
10833 case if the call is transformed to an _N variant. */
10834 switch (orig_code)
10836 case BUILT_IN_ATOMIC_EXCHANGE:
10838 if (resolve_overloaded_atomic_exchange (loc, function, params,
10839 &new_return))
10840 return new_return;
10841 /* Change to the _N variant. */
10842 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10843 break;
10846 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10848 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10849 params,
10850 &new_return))
10851 return new_return;
10852 /* Change to the _N variant. */
10853 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10854 break;
10856 case BUILT_IN_ATOMIC_LOAD:
10858 if (resolve_overloaded_atomic_load (loc, function, params,
10859 &new_return))
10860 return new_return;
10861 /* Change to the _N variant. */
10862 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10863 break;
10865 case BUILT_IN_ATOMIC_STORE:
10867 if (resolve_overloaded_atomic_store (loc, function, params,
10868 &new_return))
10869 return new_return;
10870 /* Change to the _N variant. */
10871 orig_code = BUILT_IN_ATOMIC_STORE_N;
10872 break;
10874 default:
10875 gcc_unreachable ();
10877 /* Fallthrough to the normal processing. */
10879 case BUILT_IN_ATOMIC_EXCHANGE_N:
10880 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10881 case BUILT_IN_ATOMIC_LOAD_N:
10882 case BUILT_IN_ATOMIC_STORE_N:
10883 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10884 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10885 case BUILT_IN_ATOMIC_AND_FETCH_N:
10886 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10887 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10888 case BUILT_IN_ATOMIC_OR_FETCH_N:
10889 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10890 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10891 case BUILT_IN_ATOMIC_FETCH_AND_N:
10892 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10893 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10894 case BUILT_IN_ATOMIC_FETCH_OR_N:
10896 orig_format = false;
10897 /* Fallthru for parameter processing. */
10899 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10900 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10901 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10902 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10903 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10904 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10905 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10906 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10907 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10908 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10909 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10910 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10911 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10912 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10913 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10914 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10916 int n = sync_resolve_size (function, params);
10917 tree new_function, first_param, result;
10918 enum built_in_function fncode;
10920 if (n == 0)
10921 return error_mark_node;
10923 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10924 new_function = builtin_decl_explicit (fncode);
10925 if (!sync_resolve_params (loc, function, new_function, params,
10926 orig_format))
10927 return error_mark_node;
10929 first_param = (*params)[0];
10930 result = build_function_call_vec (loc, vNULL, new_function, params,
10931 NULL);
10932 if (result == error_mark_node)
10933 return result;
10934 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10935 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10936 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10937 result = sync_resolve_return (first_param, result, orig_format);
10939 /* If new_return is set, assign function to that expr and cast the
10940 result to void since the generic interface returned void. */
10941 if (new_return)
10943 /* Cast function result from I{1,2,4,8,16} to the required type. */
10944 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10945 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10946 result);
10947 TREE_SIDE_EFFECTS (result) = 1;
10948 protected_set_expr_location (result, loc);
10949 result = convert (void_type_node, result);
10951 return result;
10954 default:
10955 return NULL_TREE;
10959 /* vector_types_compatible_elements_p is used in type checks of vectors
10960 values used as operands of binary operators. Where it returns true, and
10961 the other checks of the caller succeed (being vector types in he first
10962 place, and matching number of elements), we can just treat the types
10963 as essentially the same.
10964 Contrast with vector_targets_convertible_p, which is used for vector
10965 pointer types, and vector_types_convertible_p, which will allow
10966 language-specific matches under the control of flag_lax_vector_conversions,
10967 and might still require a conversion. */
10968 /* True if vector types T1 and T2 can be inputs to the same binary
10969 operator without conversion.
10970 We don't check the overall vector size here because some of our callers
10971 want to give different error messages when the vectors are compatible
10972 except for the element count. */
10974 bool
10975 vector_types_compatible_elements_p (tree t1, tree t2)
10977 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10978 t1 = TREE_TYPE (t1);
10979 t2 = TREE_TYPE (t2);
10981 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10983 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10984 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10985 || c2 == FIXED_POINT_TYPE));
10987 t1 = c_common_signed_type (t1);
10988 t2 = c_common_signed_type (t2);
10989 /* Equality works here because c_common_signed_type uses
10990 TYPE_MAIN_VARIANT. */
10991 if (t1 == t2)
10992 return true;
10993 if (opaque && c1 == c2
10994 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10995 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10996 return true;
10997 return false;
11000 /* Check for missing format attributes on function pointers. LTYPE is
11001 the new type or left-hand side type. RTYPE is the old type or
11002 right-hand side type. Returns TRUE if LTYPE is missing the desired
11003 attribute. */
11005 bool
11006 check_missing_format_attribute (tree ltype, tree rtype)
11008 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11009 tree ra;
11011 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11012 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11013 break;
11014 if (ra)
11016 tree la;
11017 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11018 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11019 break;
11020 return !la;
11022 else
11023 return false;
11026 /* Subscripting with type char is likely to lose on a machine where
11027 chars are signed. So warn on any machine, but optionally. Don't
11028 warn for unsigned char since that type is safe. Don't warn for
11029 signed char because anyone who uses that must have done so
11030 deliberately. Furthermore, we reduce the false positive load by
11031 warning only for non-constant value of type char. */
11033 void
11034 warn_array_subscript_with_type_char (tree index)
11036 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11037 && TREE_CODE (index) != INTEGER_CST)
11038 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11041 /* Implement -Wparentheses for the unexpected C precedence rules, to
11042 cover cases like x + y << z which readers are likely to
11043 misinterpret. We have seen an expression in which CODE is a binary
11044 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11045 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11046 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11047 expression was not formed using a binary or unary operator, or it
11048 was enclosed in parentheses. */
11050 void
11051 warn_about_parentheses (location_t loc, enum tree_code code,
11052 enum tree_code code_left, tree arg_left,
11053 enum tree_code code_right, tree arg_right)
11055 if (!warn_parentheses)
11056 return;
11058 /* This macro tests that the expression ARG with original tree code
11059 CODE appears to be a boolean expression. or the result of folding a
11060 boolean expression. */
11061 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11062 (truth_value_p (TREE_CODE (ARG)) \
11063 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11064 /* Folding may create 0 or 1 integers from other expressions. */ \
11065 || ((CODE) != INTEGER_CST \
11066 && (integer_onep (ARG) || integer_zerop (ARG))))
11068 switch (code)
11070 case LSHIFT_EXPR:
11071 if (code_left == PLUS_EXPR)
11072 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11073 "suggest parentheses around %<+%> inside %<<<%>");
11074 else if (code_right == PLUS_EXPR)
11075 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11076 "suggest parentheses around %<+%> inside %<<<%>");
11077 else if (code_left == MINUS_EXPR)
11078 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11079 "suggest parentheses around %<-%> inside %<<<%>");
11080 else if (code_right == MINUS_EXPR)
11081 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11082 "suggest parentheses around %<-%> inside %<<<%>");
11083 return;
11085 case RSHIFT_EXPR:
11086 if (code_left == PLUS_EXPR)
11087 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11088 "suggest parentheses around %<+%> inside %<>>%>");
11089 else if (code_right == PLUS_EXPR)
11090 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11091 "suggest parentheses around %<+%> inside %<>>%>");
11092 else if (code_left == MINUS_EXPR)
11093 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11094 "suggest parentheses around %<-%> inside %<>>%>");
11095 else if (code_right == MINUS_EXPR)
11096 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11097 "suggest parentheses around %<-%> inside %<>>%>");
11098 return;
11100 case TRUTH_ORIF_EXPR:
11101 if (code_left == TRUTH_ANDIF_EXPR)
11102 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11103 "suggest parentheses around %<&&%> within %<||%>");
11104 else if (code_right == TRUTH_ANDIF_EXPR)
11105 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11106 "suggest parentheses around %<&&%> within %<||%>");
11107 return;
11109 case BIT_IOR_EXPR:
11110 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11111 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11112 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11113 "suggest parentheses around arithmetic in operand of %<|%>");
11114 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11115 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11116 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11117 "suggest parentheses around arithmetic in operand of %<|%>");
11118 /* Check cases like x|y==z */
11119 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11120 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11121 "suggest parentheses around comparison in operand of %<|%>");
11122 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11123 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11124 "suggest parentheses around comparison in operand of %<|%>");
11125 /* Check cases like !x | y */
11126 else if (code_left == TRUTH_NOT_EXPR
11127 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11128 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11129 "suggest parentheses around operand of "
11130 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11131 return;
11133 case BIT_XOR_EXPR:
11134 if (code_left == BIT_AND_EXPR
11135 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11136 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11137 "suggest parentheses around arithmetic in operand of %<^%>");
11138 else if (code_right == BIT_AND_EXPR
11139 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11140 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11141 "suggest parentheses around arithmetic in operand of %<^%>");
11142 /* Check cases like x^y==z */
11143 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11144 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11145 "suggest parentheses around comparison in operand of %<^%>");
11146 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11147 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11148 "suggest parentheses around comparison in operand of %<^%>");
11149 return;
11151 case BIT_AND_EXPR:
11152 if (code_left == PLUS_EXPR)
11153 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11154 "suggest parentheses around %<+%> in operand of %<&%>");
11155 else if (code_right == PLUS_EXPR)
11156 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11157 "suggest parentheses around %<+%> in operand of %<&%>");
11158 else if (code_left == MINUS_EXPR)
11159 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11160 "suggest parentheses around %<-%> in operand of %<&%>");
11161 else if (code_right == MINUS_EXPR)
11162 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11163 "suggest parentheses around %<-%> in operand of %<&%>");
11164 /* Check cases like x&y==z */
11165 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11166 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11167 "suggest parentheses around comparison in operand of %<&%>");
11168 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11169 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11170 "suggest parentheses around comparison in operand of %<&%>");
11171 /* Check cases like !x & y */
11172 else if (code_left == TRUTH_NOT_EXPR
11173 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11174 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11175 "suggest parentheses around operand of "
11176 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11177 return;
11179 case EQ_EXPR:
11180 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11181 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11182 "suggest parentheses around comparison in operand of %<==%>");
11183 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11184 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11185 "suggest parentheses around comparison in operand of %<==%>");
11186 return;
11187 case NE_EXPR:
11188 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11189 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11190 "suggest parentheses around comparison in operand of %<!=%>");
11191 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11192 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11193 "suggest parentheses around comparison in operand of %<!=%>");
11194 return;
11196 default:
11197 if (TREE_CODE_CLASS (code) == tcc_comparison)
11199 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11200 && code_left != NE_EXPR && code_left != EQ_EXPR
11201 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11202 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11203 "comparisons like %<X<=Y<=Z%> do not "
11204 "have their mathematical meaning");
11205 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11206 && code_right != NE_EXPR && code_right != EQ_EXPR
11207 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11208 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11209 "comparisons like %<X<=Y<=Z%> do not "
11210 "have their mathematical meaning");
11212 return;
11214 #undef NOT_A_BOOLEAN_EXPR_P
11217 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11219 void
11220 warn_for_unused_label (tree label)
11222 if (!TREE_USED (label))
11224 if (DECL_INITIAL (label))
11225 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11226 else
11227 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11231 /* Warn for division by zero according to the value of DIVISOR. LOC
11232 is the location of the division operator. */
11234 void
11235 warn_for_div_by_zero (location_t loc, tree divisor)
11237 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11238 about division by zero. Do not issue a warning if DIVISOR has a
11239 floating-point type, since we consider 0.0/0.0 a valid way of
11240 generating a NaN. */
11241 if (c_inhibit_evaluation_warnings == 0
11242 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11243 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11246 /* Subroutine of build_binary_op. Give warnings for comparisons
11247 between signed and unsigned quantities that may fail. Do the
11248 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11249 so that casts will be considered, but default promotions won't
11252 LOCATION is the location of the comparison operator.
11254 The arguments of this function map directly to local variables
11255 of build_binary_op. */
11257 void
11258 warn_for_sign_compare (location_t location,
11259 tree orig_op0, tree orig_op1,
11260 tree op0, tree op1,
11261 tree result_type, enum tree_code resultcode)
11263 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11264 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11265 int unsignedp0, unsignedp1;
11267 /* In C++, check for comparison of different enum types. */
11268 if (c_dialect_cxx()
11269 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11270 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11271 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11272 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11274 warning_at (location,
11275 OPT_Wsign_compare, "comparison between types %qT and %qT",
11276 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11279 /* Do not warn if the comparison is being done in a signed type,
11280 since the signed type will only be chosen if it can represent
11281 all the values of the unsigned type. */
11282 if (!TYPE_UNSIGNED (result_type))
11283 /* OK */;
11284 /* Do not warn if both operands are unsigned. */
11285 else if (op0_signed == op1_signed)
11286 /* OK */;
11287 else
11289 tree sop, uop, base_type;
11290 bool ovf;
11292 if (op0_signed)
11293 sop = orig_op0, uop = orig_op1;
11294 else
11295 sop = orig_op1, uop = orig_op0;
11297 STRIP_TYPE_NOPS (sop);
11298 STRIP_TYPE_NOPS (uop);
11299 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11300 ? TREE_TYPE (result_type) : result_type);
11302 /* Do not warn if the signed quantity is an unsuffixed integer
11303 literal (or some static constant expression involving such
11304 literals or a conditional expression involving such literals)
11305 and it is non-negative. */
11306 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11307 /* OK */;
11308 /* Do not warn if the comparison is an equality operation, the
11309 unsigned quantity is an integral constant, and it would fit
11310 in the result if the result were signed. */
11311 else if (TREE_CODE (uop) == INTEGER_CST
11312 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11313 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11314 /* OK */;
11315 /* In C, do not warn if the unsigned quantity is an enumeration
11316 constant and its maximum value would fit in the result if the
11317 result were signed. */
11318 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11319 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11320 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11321 c_common_signed_type (base_type)))
11322 /* OK */;
11323 else
11324 warning_at (location,
11325 OPT_Wsign_compare,
11326 "comparison between signed and unsigned integer expressions");
11329 /* Warn if two unsigned values are being compared in a size larger
11330 than their original size, and one (and only one) is the result of
11331 a `~' operator. This comparison will always fail.
11333 Also warn if one operand is a constant, and the constant does not
11334 have all bits set that are set in the ~ operand when it is
11335 extended. */
11337 op0 = c_common_get_narrower (op0, &unsignedp0);
11338 op1 = c_common_get_narrower (op1, &unsignedp1);
11340 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11341 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11343 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11344 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11345 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11346 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11348 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11350 tree primop;
11351 HOST_WIDE_INT constant, mask;
11352 int unsignedp;
11353 unsigned int bits;
11355 if (tree_fits_shwi_p (op0))
11357 primop = op1;
11358 unsignedp = unsignedp1;
11359 constant = tree_to_shwi (op0);
11361 else
11363 primop = op0;
11364 unsignedp = unsignedp0;
11365 constant = tree_to_shwi (op1);
11368 bits = TYPE_PRECISION (TREE_TYPE (primop));
11369 if (bits < TYPE_PRECISION (result_type)
11370 && bits < HOST_BITS_PER_LONG && unsignedp)
11372 mask = (~ (HOST_WIDE_INT) 0) << bits;
11373 if ((mask & constant) != mask)
11375 if (constant == 0)
11376 warning_at (location, OPT_Wsign_compare,
11377 "promoted ~unsigned is always non-zero");
11378 else
11379 warning_at (location, OPT_Wsign_compare,
11380 "comparison of promoted ~unsigned with constant");
11384 else if (unsignedp0 && unsignedp1
11385 && (TYPE_PRECISION (TREE_TYPE (op0))
11386 < TYPE_PRECISION (result_type))
11387 && (TYPE_PRECISION (TREE_TYPE (op1))
11388 < TYPE_PRECISION (result_type)))
11389 warning_at (location, OPT_Wsign_compare,
11390 "comparison of promoted ~unsigned with unsigned");
11394 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11395 type via c_common_type. If -Wdouble-promotion is in use, and the
11396 conditions for warning have been met, issue a warning. GMSGID is
11397 the warning message. It must have two %T specifiers for the type
11398 that was converted (generally "float") and the type to which it was
11399 converted (generally "double), respectively. LOC is the location
11400 to which the awrning should refer. */
11402 void
11403 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11404 const char *gmsgid, location_t loc)
11406 tree source_type;
11408 if (!warn_double_promotion)
11409 return;
11410 /* If the conversion will not occur at run-time, there is no need to
11411 warn about it. */
11412 if (c_inhibit_evaluation_warnings)
11413 return;
11414 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11415 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11416 return;
11417 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11418 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11419 source_type = type1;
11420 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11421 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11422 source_type = type2;
11423 else
11424 return;
11425 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11428 /* Setup a TYPE_DECL node as a typedef representation.
11430 X is a TYPE_DECL for a typedef statement. Create a brand new
11431 ..._TYPE node (which will be just a variant of the existing
11432 ..._TYPE node with identical properties) and then install X
11433 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11435 The whole point here is to end up with a situation where each
11436 and every ..._TYPE node the compiler creates will be uniquely
11437 associated with AT MOST one node representing a typedef name.
11438 This way, even though the compiler substitutes corresponding
11439 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11440 early on, later parts of the compiler can always do the reverse
11441 translation and get back the corresponding typedef name. For
11442 example, given:
11444 typedef struct S MY_TYPE;
11445 MY_TYPE object;
11447 Later parts of the compiler might only know that `object' was of
11448 type `struct S' if it were not for code just below. With this
11449 code however, later parts of the compiler see something like:
11451 struct S' == struct S
11452 typedef struct S' MY_TYPE;
11453 struct S' object;
11455 And they can then deduce (from the node for type struct S') that
11456 the original object declaration was:
11458 MY_TYPE object;
11460 Being able to do this is important for proper support of protoize,
11461 and also for generating precise symbolic debugging information
11462 which takes full account of the programmer's (typedef) vocabulary.
11464 Obviously, we don't want to generate a duplicate ..._TYPE node if
11465 the TYPE_DECL node that we are now processing really represents a
11466 standard built-in type. */
11468 void
11469 set_underlying_type (tree x)
11471 if (x == error_mark_node)
11472 return;
11473 if (DECL_IS_BUILTIN (x))
11475 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11476 TYPE_NAME (TREE_TYPE (x)) = x;
11478 else if (TREE_TYPE (x) != error_mark_node
11479 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11481 tree tt = TREE_TYPE (x);
11482 DECL_ORIGINAL_TYPE (x) = tt;
11483 tt = build_variant_type_copy (tt);
11484 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11485 TYPE_NAME (tt) = x;
11486 TREE_USED (tt) = TREE_USED (x);
11487 TREE_TYPE (x) = tt;
11491 /* Record the types used by the current global variable declaration
11492 being parsed, so that we can decide later to emit their debug info.
11493 Those types are in types_used_by_cur_var_decl, and we are going to
11494 store them in the types_used_by_vars_hash hash table.
11495 DECL is the declaration of the global variable that has been parsed. */
11497 void
11498 record_types_used_by_current_var_decl (tree decl)
11500 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11502 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11504 tree type = types_used_by_cur_var_decl->pop ();
11505 types_used_by_var_decl_insert (type, decl);
11509 /* If DECL is a typedef that is declared in the current function,
11510 record it for the purpose of -Wunused-local-typedefs. */
11512 void
11513 record_locally_defined_typedef (tree decl)
11515 struct c_language_function *l;
11517 if (!warn_unused_local_typedefs
11518 || cfun == NULL
11519 /* if this is not a locally defined typedef then we are not
11520 interested. */
11521 || !is_typedef_decl (decl)
11522 || !decl_function_context (decl))
11523 return;
11525 l = (struct c_language_function *) cfun->language;
11526 vec_safe_push (l->local_typedefs, decl);
11529 /* If T is a TYPE_DECL declared locally, mark it as used. */
11531 void
11532 maybe_record_typedef_use (tree t)
11534 if (!is_typedef_decl (t))
11535 return;
11537 TREE_USED (t) = true;
11540 /* Warn if there are some unused locally defined typedefs in the
11541 current function. */
11543 void
11544 maybe_warn_unused_local_typedefs (void)
11546 int i;
11547 tree decl;
11548 /* The number of times we have emitted -Wunused-local-typedefs
11549 warnings. If this is different from errorcount, that means some
11550 unrelated errors have been issued. In which case, we'll avoid
11551 emitting "unused-local-typedefs" warnings. */
11552 static int unused_local_typedefs_warn_count;
11553 struct c_language_function *l;
11555 if (cfun == NULL)
11556 return;
11558 if ((l = (struct c_language_function *) cfun->language) == NULL)
11559 return;
11561 if (warn_unused_local_typedefs
11562 && errorcount == unused_local_typedefs_warn_count)
11564 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11565 if (!TREE_USED (decl))
11566 warning_at (DECL_SOURCE_LOCATION (decl),
11567 OPT_Wunused_local_typedefs,
11568 "typedef %qD locally defined but not used", decl);
11569 unused_local_typedefs_warn_count = errorcount;
11572 vec_free (l->local_typedefs);
11575 /* The C and C++ parsers both use vectors to hold function arguments.
11576 For efficiency, we keep a cache of unused vectors. This is the
11577 cache. */
11579 typedef vec<tree, va_gc> *tree_gc_vec;
11580 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11582 /* Return a new vector from the cache. If the cache is empty,
11583 allocate a new vector. These vectors are GC'ed, so it is OK if the
11584 pointer is not released.. */
11586 vec<tree, va_gc> *
11587 make_tree_vector (void)
11589 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11590 return tree_vector_cache->pop ();
11591 else
11593 /* Passing 0 to vec::alloc returns NULL, and our callers require
11594 that we always return a non-NULL value. The vector code uses
11595 4 when growing a NULL vector, so we do too. */
11596 vec<tree, va_gc> *v;
11597 vec_alloc (v, 4);
11598 return v;
11602 /* Release a vector of trees back to the cache. */
11604 void
11605 release_tree_vector (vec<tree, va_gc> *vec)
11607 if (vec != NULL)
11609 vec->truncate (0);
11610 vec_safe_push (tree_vector_cache, vec);
11614 /* Get a new tree vector holding a single tree. */
11616 vec<tree, va_gc> *
11617 make_tree_vector_single (tree t)
11619 vec<tree, va_gc> *ret = make_tree_vector ();
11620 ret->quick_push (t);
11621 return ret;
11624 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11626 vec<tree, va_gc> *
11627 make_tree_vector_from_list (tree list)
11629 vec<tree, va_gc> *ret = make_tree_vector ();
11630 for (; list; list = TREE_CHAIN (list))
11631 vec_safe_push (ret, TREE_VALUE (list));
11632 return ret;
11635 /* Get a new tree vector which is a copy of an existing one. */
11637 vec<tree, va_gc> *
11638 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11640 vec<tree, va_gc> *ret;
11641 unsigned int ix;
11642 tree t;
11644 ret = make_tree_vector ();
11645 vec_safe_reserve (ret, vec_safe_length (orig));
11646 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11647 ret->quick_push (t);
11648 return ret;
11651 /* Return true if KEYWORD starts a type specifier. */
11653 bool
11654 keyword_begins_type_specifier (enum rid keyword)
11656 switch (keyword)
11658 case RID_AUTO_TYPE:
11659 case RID_INT:
11660 case RID_CHAR:
11661 case RID_FLOAT:
11662 case RID_DOUBLE:
11663 case RID_VOID:
11664 case RID_INT128:
11665 case RID_UNSIGNED:
11666 case RID_LONG:
11667 case RID_SHORT:
11668 case RID_SIGNED:
11669 case RID_DFLOAT32:
11670 case RID_DFLOAT64:
11671 case RID_DFLOAT128:
11672 case RID_FRACT:
11673 case RID_ACCUM:
11674 case RID_BOOL:
11675 case RID_WCHAR:
11676 case RID_CHAR16:
11677 case RID_CHAR32:
11678 case RID_SAT:
11679 case RID_COMPLEX:
11680 case RID_TYPEOF:
11681 case RID_STRUCT:
11682 case RID_CLASS:
11683 case RID_UNION:
11684 case RID_ENUM:
11685 return true;
11686 default:
11687 return false;
11691 /* Return true if KEYWORD names a type qualifier. */
11693 bool
11694 keyword_is_type_qualifier (enum rid keyword)
11696 switch (keyword)
11698 case RID_CONST:
11699 case RID_VOLATILE:
11700 case RID_RESTRICT:
11701 case RID_ATOMIC:
11702 return true;
11703 default:
11704 return false;
11708 /* Return true if KEYWORD names a storage class specifier.
11710 RID_TYPEDEF is not included in this list despite `typedef' being
11711 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11712 such for syntactic convenience only. */
11714 bool
11715 keyword_is_storage_class_specifier (enum rid keyword)
11717 switch (keyword)
11719 case RID_STATIC:
11720 case RID_EXTERN:
11721 case RID_REGISTER:
11722 case RID_AUTO:
11723 case RID_MUTABLE:
11724 case RID_THREAD:
11725 return true;
11726 default:
11727 return false;
11731 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11733 static bool
11734 keyword_is_function_specifier (enum rid keyword)
11736 switch (keyword)
11738 case RID_INLINE:
11739 case RID_NORETURN:
11740 case RID_VIRTUAL:
11741 case RID_EXPLICIT:
11742 return true;
11743 default:
11744 return false;
11748 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11749 declaration-specifier (C99 6.7). */
11751 bool
11752 keyword_is_decl_specifier (enum rid keyword)
11754 if (keyword_is_storage_class_specifier (keyword)
11755 || keyword_is_type_qualifier (keyword)
11756 || keyword_is_function_specifier (keyword))
11757 return true;
11759 switch (keyword)
11761 case RID_TYPEDEF:
11762 case RID_FRIEND:
11763 case RID_CONSTEXPR:
11764 return true;
11765 default:
11766 return false;
11770 /* Initialize language-specific-bits of tree_contains_struct. */
11772 void
11773 c_common_init_ts (void)
11775 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11776 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11777 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11780 /* Build a user-defined numeric literal out of an integer constant type VALUE
11781 with identifier SUFFIX. */
11783 tree
11784 build_userdef_literal (tree suffix_id, tree value,
11785 enum overflow_type overflow, tree num_string)
11787 tree literal = make_node (USERDEF_LITERAL);
11788 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11789 USERDEF_LITERAL_VALUE (literal) = value;
11790 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11791 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11792 return literal;
11795 /* For vector[index], convert the vector to a
11796 pointer of the underlying type. */
11797 void
11798 convert_vector_to_pointer_for_subscript (location_t loc,
11799 tree* vecp, tree index)
11801 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11803 tree type = TREE_TYPE (*vecp);
11804 tree type1;
11806 if (TREE_CODE (index) == INTEGER_CST)
11807 if (!tree_fits_uhwi_p (index)
11808 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
11809 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11811 c_common_mark_addressable_vec (*vecp);
11812 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11813 type1 = build_pointer_type (TREE_TYPE (*vecp));
11814 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
11815 if (!ref_all
11816 && !DECL_P (*vecp))
11818 /* If the original vector isn't declared may_alias and it
11819 isn't a bare vector look if the subscripting would
11820 alias the vector we subscript, and if not, force ref-all. */
11821 alias_set_type vecset = get_alias_set (*vecp);
11822 alias_set_type sset = get_alias_set (type);
11823 if (!alias_sets_must_conflict_p (sset, vecset)
11824 && !alias_set_subset_of (sset, vecset))
11825 ref_all = true;
11827 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
11828 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11829 *vecp = convert (type, *vecp);
11833 /* Determine which of the operands, if any, is a scalar that needs to be
11834 converted to a vector, for the range of operations. */
11835 enum stv_conv
11836 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11837 bool complain)
11839 tree type0 = TREE_TYPE (op0);
11840 tree type1 = TREE_TYPE (op1);
11841 bool integer_only_op = false;
11842 enum stv_conv ret = stv_firstarg;
11844 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11845 || TREE_CODE (type1) == VECTOR_TYPE);
11846 switch (code)
11848 /* Most GENERIC binary expressions require homogeneous arguments.
11849 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11850 argument that is a vector and a second one that is a scalar, so
11851 we never return stv_secondarg for them. */
11852 case RSHIFT_EXPR:
11853 case LSHIFT_EXPR:
11854 if (TREE_CODE (type0) == INTEGER_TYPE
11855 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11857 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11859 if (complain)
11860 error_at (loc, "conversion of scalar %qT to vector %qT "
11861 "involves truncation", type0, type1);
11862 return stv_error;
11864 else
11865 return stv_firstarg;
11867 break;
11869 case BIT_IOR_EXPR:
11870 case BIT_XOR_EXPR:
11871 case BIT_AND_EXPR:
11872 integer_only_op = true;
11873 /* ... fall through ... */
11875 case VEC_COND_EXPR:
11877 case PLUS_EXPR:
11878 case MINUS_EXPR:
11879 case MULT_EXPR:
11880 case TRUNC_DIV_EXPR:
11881 case CEIL_DIV_EXPR:
11882 case FLOOR_DIV_EXPR:
11883 case ROUND_DIV_EXPR:
11884 case EXACT_DIV_EXPR:
11885 case TRUNC_MOD_EXPR:
11886 case FLOOR_MOD_EXPR:
11887 case RDIV_EXPR:
11888 case EQ_EXPR:
11889 case NE_EXPR:
11890 case LE_EXPR:
11891 case GE_EXPR:
11892 case LT_EXPR:
11893 case GT_EXPR:
11894 /* What about UNLT_EXPR? */
11895 if (TREE_CODE (type0) == VECTOR_TYPE)
11897 tree tmp;
11898 ret = stv_secondarg;
11899 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11900 tmp = type0; type0 = type1; type1 = tmp;
11901 tmp = op0; op0 = op1; op1 = tmp;
11904 if (TREE_CODE (type0) == INTEGER_TYPE
11905 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11907 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11909 if (complain)
11910 error_at (loc, "conversion of scalar %qT to vector %qT "
11911 "involves truncation", type0, type1);
11912 return stv_error;
11914 return ret;
11916 else if (!integer_only_op
11917 /* Allow integer --> real conversion if safe. */
11918 && (TREE_CODE (type0) == REAL_TYPE
11919 || TREE_CODE (type0) == INTEGER_TYPE)
11920 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11922 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11924 if (complain)
11925 error_at (loc, "conversion of scalar %qT to vector %qT "
11926 "involves truncation", type0, type1);
11927 return stv_error;
11929 return ret;
11931 default:
11932 break;
11935 return stv_nothing;
11938 /* Return true iff ALIGN is an integral constant that is a fundamental
11939 alignment, as defined by [basic.align] in the c++-11
11940 specifications.
11942 That is:
11944 [A fundamental alignment is represented by an alignment less than or
11945 equal to the greatest alignment supported by the implementation
11946 in all contexts, which is equal to
11947 alignof(max_align_t)]. */
11949 bool
11950 cxx_fundamental_alignment_p (unsigned align)
11952 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11953 TYPE_ALIGN (long_double_type_node)));
11956 /* Return true if T is a pointer to a zero-sized aggregate. */
11958 bool
11959 pointer_to_zero_sized_aggr_p (tree t)
11961 if (!POINTER_TYPE_P (t))
11962 return false;
11963 t = TREE_TYPE (t);
11964 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
11967 #include "gt-c-family-c-common.h"