Merge from trunk.
[official-gcc.git] / gcc / c-family / c-common.c
blob069a90396bcae46766f1db15a0d9fb4947c75980
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "intl.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "c-pragma.h"
28 #include "ggc.h"
29 #include "c-common.h"
30 #include "c-objc.h"
31 #include "tm_p.h"
32 #include "obstack.h"
33 #include "cpplib.h"
34 #include "target.h"
35 #include "common/common-target.h"
36 #include "langhooks.h"
37 #include "tree-inline.h"
38 #include "toplev.h"
39 #include "diagnostic.h"
40 #include "tree-iterator.h"
41 #include "hashtab.h"
42 #include "opts.h"
43 #include "cgraph.h"
44 #include "target-def.h"
45 #include "gimple.h"
46 #include "gimplify.h"
48 cpp_reader *parse_in; /* Declared in c-pragma.h. */
50 /* The following symbols are subsumed in the c_global_trees array, and
51 listed here individually for documentation purposes.
53 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
55 tree short_integer_type_node;
56 tree long_integer_type_node;
57 tree long_long_integer_type_node;
58 tree int128_integer_type_node;
60 tree short_unsigned_type_node;
61 tree long_unsigned_type_node;
62 tree long_long_unsigned_type_node;
63 tree int128_unsigned_type_node;
65 tree truthvalue_type_node;
66 tree truthvalue_false_node;
67 tree truthvalue_true_node;
69 tree ptrdiff_type_node;
71 tree unsigned_char_type_node;
72 tree signed_char_type_node;
73 tree wchar_type_node;
75 tree char16_type_node;
76 tree char32_type_node;
78 tree float_type_node;
79 tree double_type_node;
80 tree long_double_type_node;
82 tree complex_integer_type_node;
83 tree complex_float_type_node;
84 tree complex_double_type_node;
85 tree complex_long_double_type_node;
87 tree dfloat32_type_node;
88 tree dfloat64_type_node;
89 tree_dfloat128_type_node;
91 tree intQI_type_node;
92 tree intHI_type_node;
93 tree intSI_type_node;
94 tree intDI_type_node;
95 tree intTI_type_node;
97 tree unsigned_intQI_type_node;
98 tree unsigned_intHI_type_node;
99 tree unsigned_intSI_type_node;
100 tree unsigned_intDI_type_node;
101 tree unsigned_intTI_type_node;
103 tree widest_integer_literal_type_node;
104 tree widest_unsigned_literal_type_node;
106 Nodes for types `void *' and `const void *'.
108 tree ptr_type_node, const_ptr_type_node;
110 Nodes for types `char *' and `const char *'.
112 tree string_type_node, const_string_type_node;
114 Type `char[SOMENUMBER]'.
115 Used when an array of char is needed and the size is irrelevant.
117 tree char_array_type_node;
119 Type `int[SOMENUMBER]' or something like it.
120 Used when an array of int needed and the size is irrelevant.
122 tree int_array_type_node;
124 Type `wchar_t[SOMENUMBER]' or something like it.
125 Used when a wide string literal is created.
127 tree wchar_array_type_node;
129 Type `char16_t[SOMENUMBER]' or something like it.
130 Used when a UTF-16 string literal is created.
132 tree char16_array_type_node;
134 Type `char32_t[SOMENUMBER]' or something like it.
135 Used when a UTF-32 string literal is created.
137 tree char32_array_type_node;
139 Type `int ()' -- used for implicit declaration of functions.
141 tree default_function_type;
143 A VOID_TYPE node, packaged in a TREE_LIST.
145 tree void_list_node;
147 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
148 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
149 VAR_DECLS, but C++ does.)
151 tree function_name_decl_node;
152 tree pretty_function_name_decl_node;
153 tree c99_function_name_decl_node;
155 Stack of nested function name VAR_DECLs.
157 tree saved_function_name_decls;
161 tree c_global_trees[CTI_MAX];
163 /* Switches common to the C front ends. */
165 /* Nonzero means don't output line number information. */
167 char flag_no_line_commands;
169 /* Nonzero causes -E output not to be done, but directives such as
170 #define that have side effects are still obeyed. */
172 char flag_no_output;
174 /* Nonzero means dump macros in some fashion. */
176 char flag_dump_macros;
178 /* Nonzero means pass #include lines through to the output. */
180 char flag_dump_includes;
182 /* Nonzero means process PCH files while preprocessing. */
184 bool flag_pch_preprocess;
186 /* The file name to which we should write a precompiled header, or
187 NULL if no header will be written in this compile. */
189 const char *pch_file;
191 /* Nonzero if an ISO standard was selected. It rejects macros in the
192 user's namespace. */
193 int flag_iso;
195 /* C/ObjC language option variables. */
198 /* Nonzero means allow type mismatches in conditional expressions;
199 just make their values `void'. */
201 int flag_cond_mismatch;
203 /* Nonzero means enable C89 Amendment 1 features. */
205 int flag_isoc94;
207 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
209 int flag_isoc99;
211 /* Nonzero means use the ISO C11 dialect of C. */
213 int flag_isoc11;
215 /* Nonzero means that we have builtin functions, and main is an int. */
217 int flag_hosted = 1;
220 /* ObjC language option variables. */
223 /* Tells the compiler that this is a special run. Do not perform any
224 compiling, instead we are to test some platform dependent features
225 and output a C header file with appropriate definitions. */
227 int print_struct_values;
229 /* Tells the compiler what is the constant string class for ObjC. */
231 const char *constant_string_class_name;
234 /* C++ language option variables. */
237 /* Nonzero means generate separate instantiation control files and
238 juggle them at link time. */
240 int flag_use_repository;
242 /* The C++ dialect being used. C++11 is the default. */
244 enum cxx_dialect cxx_dialect = cxx11;
246 /* Maximum template instantiation depth. This limit exists to limit the
247 time it takes to notice excessively recursive template instantiations.
249 The default is lower than the 1024 recommended by the C++0x standard
250 because G++ runs out of stack before 1024 with highly recursive template
251 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
253 int max_tinst_depth = 900;
255 /* The elements of `ridpointers' are identifier nodes for the reserved
256 type names and storage classes. It is indexed by a RID_... value. */
257 tree *ridpointers;
259 tree (*make_fname_decl) (location_t, tree, int);
261 /* Nonzero means don't warn about problems that occur when the code is
262 executed. */
263 int c_inhibit_evaluation_warnings;
265 /* Whether we are building a boolean conversion inside
266 convert_for_assignment, or some other late binary operation. If
267 build_binary_op is called for C (from code shared by C and C++) in
268 this case, then the operands have already been folded and the
269 result will not be folded again, so C_MAYBE_CONST_EXPR should not
270 be generated. */
271 bool in_late_binary_op;
273 /* Whether lexing has been completed, so subsequent preprocessor
274 errors should use the compiler's input_location. */
275 bool done_lexing = false;
277 /* Information about how a function name is generated. */
278 struct fname_var_t
280 tree *const decl; /* pointer to the VAR_DECL. */
281 const unsigned rid; /* RID number for the identifier. */
282 const int pretty; /* How pretty is it? */
285 /* The three ways of getting then name of the current function. */
287 const struct fname_var_t fname_vars[] =
289 /* C99 compliant __func__, must be first. */
290 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
291 /* GCC __FUNCTION__ compliant. */
292 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
293 /* GCC __PRETTY_FUNCTION__ compliant. */
294 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
295 {NULL, 0, 0},
298 /* Global visibility options. */
299 struct visibility_flags visibility_options;
301 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
302 static tree check_case_value (tree);
303 static bool check_case_bounds (tree, tree, tree *, tree *);
305 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
306 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
312 int, bool *);
313 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
314 int, bool *);
315 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
316 bool *);
317 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_always_inline_attribute (tree *, tree, tree, int,
321 bool *);
322 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
329 bool *);
330 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_transparent_union_attribute (tree *, tree, tree,
332 int, bool *);
333 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
339 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
340 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
343 static tree handle_visibility_attribute (tree *, tree, tree, int,
344 bool *);
345 static tree handle_tls_model_attribute (tree *, tree, tree, int,
346 bool *);
347 static tree handle_no_instrument_function_attribute (tree *, tree,
348 tree, int, bool *);
349 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_deprecated_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_vector_size_attribute (tree *, tree, tree, int,
360 bool *);
361 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
365 bool *);
366 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
371 static tree ignore_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
379 bool *);
380 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
383 static void check_function_nonnull (tree, int, tree *);
384 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
385 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
386 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
387 static int resort_field_decl_cmp (const void *, const void *);
389 /* Reserved words. The third field is a mask: keywords are disabled
390 if they match the mask.
392 Masks for languages:
393 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
394 C --std=c99: D_CXXONLY | D_OBJC
395 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
396 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
397 C++ --std=c0x: D_CONLY | D_OBJC
398 ObjC++ is like C++ except that D_OBJC is not set
400 If -fno-asm is used, D_ASM is added to the mask. If
401 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
402 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
403 In C with -Wc++-compat, we warn if D_CXXWARN is set.
405 Note the complication of the D_CXX_OBJC keywords. These are
406 reserved words such as 'class'. In C++, 'class' is a reserved
407 word. In Objective-C++ it is too. In Objective-C, it is a
408 reserved word too, but only if it follows an '@' sign.
410 const struct c_common_resword c_common_reswords[] =
412 { "_Alignas", RID_ALIGNAS, D_CONLY },
413 { "_Alignof", RID_ALIGNOF, D_CONLY },
414 { "_Atomic", RID_ATOMIC, D_CONLY },
415 { "_Bool", RID_BOOL, D_CONLY },
416 { "_Complex", RID_COMPLEX, 0 },
417 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
418 { "_Cilk_sync", RID_CILK_SYNC, 0 },
419 { "_Imaginary", RID_IMAGINARY, D_CONLY },
420 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
421 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
422 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
423 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
424 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
425 { "_Sat", RID_SAT, D_CONLY | D_EXT },
426 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
427 { "_Noreturn", RID_NORETURN, D_CONLY },
428 { "_Generic", RID_GENERIC, D_CONLY },
429 { "_Thread_local", RID_THREAD, D_CONLY },
430 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
431 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
432 { "__alignof", RID_ALIGNOF, 0 },
433 { "__alignof__", RID_ALIGNOF, 0 },
434 { "__asm", RID_ASM, 0 },
435 { "__asm__", RID_ASM, 0 },
436 { "__attribute", RID_ATTRIBUTE, 0 },
437 { "__attribute__", RID_ATTRIBUTE, 0 },
438 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
439 { "__bases", RID_BASES, D_CXXONLY },
440 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
441 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
442 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
443 { "__builtin_offsetof", RID_OFFSETOF, 0 },
444 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
445 { "__builtin_va_arg", RID_VA_ARG, 0 },
446 { "__complex", RID_COMPLEX, 0 },
447 { "__complex__", RID_COMPLEX, 0 },
448 { "__const", RID_CONST, 0 },
449 { "__const__", RID_CONST, 0 },
450 { "__decltype", RID_DECLTYPE, D_CXXONLY },
451 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
452 { "__extension__", RID_EXTENSION, 0 },
453 { "__func__", RID_C99_FUNCTION_NAME, 0 },
454 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
455 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
456 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
457 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
458 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
459 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
460 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
461 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
462 { "__imag", RID_IMAGPART, 0 },
463 { "__imag__", RID_IMAGPART, 0 },
464 { "__inline", RID_INLINE, 0 },
465 { "__inline__", RID_INLINE, 0 },
466 { "__int128", RID_INT128, 0 },
467 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
468 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
469 { "__is_class", RID_IS_CLASS, D_CXXONLY },
470 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
471 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
472 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
473 { "__is_final", RID_IS_FINAL, D_CXXONLY },
474 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
475 { "__is_pod", RID_IS_POD, D_CXXONLY },
476 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
477 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
478 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
479 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
480 { "__is_union", RID_IS_UNION, D_CXXONLY },
481 { "__label__", RID_LABEL, 0 },
482 { "__null", RID_NULL, 0 },
483 { "__real", RID_REALPART, 0 },
484 { "__real__", RID_REALPART, 0 },
485 { "__restrict", RID_RESTRICT, 0 },
486 { "__restrict__", RID_RESTRICT, 0 },
487 { "__signed", RID_SIGNED, 0 },
488 { "__signed__", RID_SIGNED, 0 },
489 { "__thread", RID_THREAD, 0 },
490 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
491 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
492 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
493 { "__typeof", RID_TYPEOF, 0 },
494 { "__typeof__", RID_TYPEOF, 0 },
495 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
496 { "__volatile", RID_VOLATILE, 0 },
497 { "__volatile__", RID_VOLATILE, 0 },
498 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
499 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
500 { "asm", RID_ASM, D_ASM },
501 { "auto", RID_AUTO, 0 },
502 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
503 { "break", RID_BREAK, 0 },
504 { "case", RID_CASE, 0 },
505 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
506 { "char", RID_CHAR, 0 },
507 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
508 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
509 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
510 { "const", RID_CONST, 0 },
511 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
512 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
513 { "continue", RID_CONTINUE, 0 },
514 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
515 { "default", RID_DEFAULT, 0 },
516 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
517 { "do", RID_DO, 0 },
518 { "double", RID_DOUBLE, 0 },
519 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
520 { "else", RID_ELSE, 0 },
521 { "enum", RID_ENUM, 0 },
522 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
523 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
524 { "extern", RID_EXTERN, 0 },
525 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
526 { "float", RID_FLOAT, 0 },
527 { "for", RID_FOR, 0 },
528 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
529 { "goto", RID_GOTO, 0 },
530 { "if", RID_IF, 0 },
531 { "inline", RID_INLINE, D_EXT89 },
532 { "int", RID_INT, 0 },
533 { "long", RID_LONG, 0 },
534 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
535 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
536 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
537 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
538 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
539 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
540 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
541 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
542 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
543 { "register", RID_REGISTER, 0 },
544 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
545 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
546 { "return", RID_RETURN, 0 },
547 { "short", RID_SHORT, 0 },
548 { "signed", RID_SIGNED, 0 },
549 { "sizeof", RID_SIZEOF, 0 },
550 { "static", RID_STATIC, 0 },
551 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
552 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
553 { "struct", RID_STRUCT, 0 },
554 { "switch", RID_SWITCH, 0 },
555 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
556 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
557 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
558 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
559 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
560 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
561 { "typedef", RID_TYPEDEF, 0 },
562 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
563 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
564 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
565 { "union", RID_UNION, 0 },
566 { "unsigned", RID_UNSIGNED, 0 },
567 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
568 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
569 { "void", RID_VOID, 0 },
570 { "volatile", RID_VOLATILE, 0 },
571 { "wchar_t", RID_WCHAR, D_CXXONLY },
572 { "while", RID_WHILE, 0 },
574 /* Concepts-related keywords */
575 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
576 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
578 /* These Objective-C keywords are recognized only immediately after
579 an '@'. */
580 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
581 { "defs", RID_AT_DEFS, D_OBJC },
582 { "encode", RID_AT_ENCODE, D_OBJC },
583 { "end", RID_AT_END, D_OBJC },
584 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
585 { "interface", RID_AT_INTERFACE, D_OBJC },
586 { "protocol", RID_AT_PROTOCOL, D_OBJC },
587 { "selector", RID_AT_SELECTOR, D_OBJC },
588 { "finally", RID_AT_FINALLY, D_OBJC },
589 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
590 { "optional", RID_AT_OPTIONAL, D_OBJC },
591 { "required", RID_AT_REQUIRED, D_OBJC },
592 { "property", RID_AT_PROPERTY, D_OBJC },
593 { "package", RID_AT_PACKAGE, D_OBJC },
594 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
595 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
596 /* These are recognized only in protocol-qualifier context
597 (see above) */
598 { "bycopy", RID_BYCOPY, D_OBJC },
599 { "byref", RID_BYREF, D_OBJC },
600 { "in", RID_IN, D_OBJC },
601 { "inout", RID_INOUT, D_OBJC },
602 { "oneway", RID_ONEWAY, D_OBJC },
603 { "out", RID_OUT, D_OBJC },
604 /* These are recognized inside a property attribute list */
605 { "assign", RID_ASSIGN, D_OBJC },
606 { "copy", RID_COPY, D_OBJC },
607 { "getter", RID_GETTER, D_OBJC },
608 { "nonatomic", RID_NONATOMIC, D_OBJC },
609 { "readonly", RID_READONLY, D_OBJC },
610 { "readwrite", RID_READWRITE, D_OBJC },
611 { "retain", RID_RETAIN, D_OBJC },
612 { "setter", RID_SETTER, D_OBJC },
615 const unsigned int num_c_common_reswords =
616 sizeof c_common_reswords / sizeof (struct c_common_resword);
618 /* Table of machine-independent attributes common to all C-like languages. */
619 const struct attribute_spec c_common_attribute_table[] =
621 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
622 affects_type_identity } */
623 { "packed", 0, 0, false, false, false,
624 handle_packed_attribute , false},
625 { "nocommon", 0, 0, true, false, false,
626 handle_nocommon_attribute, false},
627 { "common", 0, 0, true, false, false,
628 handle_common_attribute, false },
629 /* FIXME: logically, noreturn attributes should be listed as
630 "false, true, true" and apply to function types. But implementing this
631 would require all the places in the compiler that use TREE_THIS_VOLATILE
632 on a decl to identify non-returning functions to be located and fixed
633 to check the function type instead. */
634 { "noreturn", 0, 0, true, false, false,
635 handle_noreturn_attribute, false },
636 { "volatile", 0, 0, true, false, false,
637 handle_noreturn_attribute, false },
638 { "noinline", 0, 0, true, false, false,
639 handle_noinline_attribute, false },
640 { "noclone", 0, 0, true, false, false,
641 handle_noclone_attribute, false },
642 { "leaf", 0, 0, true, false, false,
643 handle_leaf_attribute, false },
644 { "always_inline", 0, 0, true, false, false,
645 handle_always_inline_attribute, false },
646 { "gnu_inline", 0, 0, true, false, false,
647 handle_gnu_inline_attribute, false },
648 { "artificial", 0, 0, true, false, false,
649 handle_artificial_attribute, false },
650 { "flatten", 0, 0, true, false, false,
651 handle_flatten_attribute, false },
652 { "used", 0, 0, true, false, false,
653 handle_used_attribute, false },
654 { "unused", 0, 0, false, false, false,
655 handle_unused_attribute, false },
656 { "externally_visible", 0, 0, true, false, false,
657 handle_externally_visible_attribute, false },
658 /* The same comments as for noreturn attributes apply to const ones. */
659 { "const", 0, 0, true, false, false,
660 handle_const_attribute, false },
661 { "transparent_union", 0, 0, false, false, false,
662 handle_transparent_union_attribute, false },
663 { "constructor", 0, 1, true, false, false,
664 handle_constructor_attribute, false },
665 { "destructor", 0, 1, true, false, false,
666 handle_destructor_attribute, false },
667 { "mode", 1, 1, false, true, false,
668 handle_mode_attribute, false },
669 { "section", 1, 1, true, false, false,
670 handle_section_attribute, false },
671 { "aligned", 0, 1, false, false, false,
672 handle_aligned_attribute, false },
673 { "weak", 0, 0, true, false, false,
674 handle_weak_attribute, false },
675 { "ifunc", 1, 1, true, false, false,
676 handle_ifunc_attribute, false },
677 { "alias", 1, 1, true, false, false,
678 handle_alias_attribute, false },
679 { "weakref", 0, 1, true, false, false,
680 handle_weakref_attribute, false },
681 { "no_instrument_function", 0, 0, true, false, false,
682 handle_no_instrument_function_attribute,
683 false },
684 { "malloc", 0, 0, true, false, false,
685 handle_malloc_attribute, false },
686 { "returns_twice", 0, 0, true, false, false,
687 handle_returns_twice_attribute, false },
688 { "no_stack_limit", 0, 0, true, false, false,
689 handle_no_limit_stack_attribute, false },
690 { "pure", 0, 0, true, false, false,
691 handle_pure_attribute, false },
692 { "transaction_callable", 0, 0, false, true, false,
693 handle_tm_attribute, false },
694 { "transaction_unsafe", 0, 0, false, true, false,
695 handle_tm_attribute, false },
696 { "transaction_safe", 0, 0, false, true, false,
697 handle_tm_attribute, false },
698 { "transaction_may_cancel_outer", 0, 0, false, true, false,
699 handle_tm_attribute, false },
700 /* ??? These two attributes didn't make the transition from the
701 Intel language document to the multi-vendor language document. */
702 { "transaction_pure", 0, 0, false, true, false,
703 handle_tm_attribute, false },
704 { "transaction_wrap", 1, 1, true, false, false,
705 handle_tm_wrap_attribute, false },
706 /* For internal use (marking of builtins) only. The name contains space
707 to prevent its usage in source code. */
708 { "no vops", 0, 0, true, false, false,
709 handle_novops_attribute, false },
710 { "deprecated", 0, 1, false, false, false,
711 handle_deprecated_attribute, false },
712 { "vector_size", 1, 1, false, true, false,
713 handle_vector_size_attribute, false },
714 { "visibility", 1, 1, false, false, false,
715 handle_visibility_attribute, false },
716 { "tls_model", 1, 1, true, false, false,
717 handle_tls_model_attribute, false },
718 { "nonnull", 0, -1, false, true, true,
719 handle_nonnull_attribute, false },
720 { "nothrow", 0, 0, true, false, false,
721 handle_nothrow_attribute, false },
722 { "may_alias", 0, 0, false, true, false, NULL, false },
723 { "cleanup", 1, 1, true, false, false,
724 handle_cleanup_attribute, false },
725 { "warn_unused_result", 0, 0, false, true, true,
726 handle_warn_unused_result_attribute, false },
727 { "sentinel", 0, 1, false, true, true,
728 handle_sentinel_attribute, false },
729 /* For internal use (marking of builtins) only. The name contains space
730 to prevent its usage in source code. */
731 { "type generic", 0, 0, false, true, true,
732 handle_type_generic_attribute, false },
733 { "alloc_size", 1, 2, false, true, true,
734 handle_alloc_size_attribute, false },
735 { "cold", 0, 0, true, false, false,
736 handle_cold_attribute, false },
737 { "hot", 0, 0, true, false, false,
738 handle_hot_attribute, false },
739 { "no_address_safety_analysis",
740 0, 0, true, false, false,
741 handle_no_address_safety_analysis_attribute,
742 false },
743 { "no_sanitize_address", 0, 0, true, false, false,
744 handle_no_sanitize_address_attribute,
745 false },
746 { "no_sanitize_undefined", 0, 0, true, false, false,
747 handle_no_sanitize_undefined_attribute,
748 false },
749 { "warning", 1, 1, true, false, false,
750 handle_error_attribute, false },
751 { "error", 1, 1, true, false, false,
752 handle_error_attribute, false },
753 { "target", 1, -1, true, false, false,
754 handle_target_attribute, false },
755 { "optimize", 1, -1, true, false, false,
756 handle_optimize_attribute, false },
757 /* For internal use only. The leading '*' both prevents its usage in
758 source code and signals that it may be overridden by machine tables. */
759 { "*tm regparm", 0, 0, false, true, true,
760 ignore_attribute, false },
761 { "no_split_stack", 0, 0, true, false, false,
762 handle_no_split_stack_attribute, false },
763 /* For internal use (marking of builtins and runtime functions) only.
764 The name contains space to prevent its usage in source code. */
765 { "fn spec", 1, 1, false, true, true,
766 handle_fnspec_attribute, false },
767 { "warn_unused", 0, 0, false, false, false,
768 handle_warn_unused_attribute, false },
769 { "returns_nonnull", 0, 0, false, true, true,
770 handle_returns_nonnull_attribute, false },
771 { "omp declare simd", 0, -1, true, false, false,
772 handle_omp_declare_simd_attribute, false },
773 { "omp declare target", 0, 0, true, false, false,
774 handle_omp_declare_target_attribute, false },
775 { "bnd_variable_size", 0, 0, true, false, false,
776 handle_bnd_variable_size_attribute, false },
777 { "bnd_legacy", 0, 0, true, false, false,
778 handle_bnd_legacy, false },
779 { NULL, 0, 0, false, false, false, NULL, false }
782 /* Give the specifications for the format attributes, used by C and all
783 descendants. */
785 const struct attribute_spec c_common_format_attribute_table[] =
787 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
788 affects_type_identity } */
789 { "format", 3, 3, false, true, true,
790 handle_format_attribute, false },
791 { "format_arg", 1, 1, false, true, true,
792 handle_format_arg_attribute, false },
793 { NULL, 0, 0, false, false, false, NULL, false }
796 /* Return identifier for address space AS. */
798 const char *
799 c_addr_space_name (addr_space_t as)
801 int rid = RID_FIRST_ADDR_SPACE + as;
802 gcc_assert (ridpointers [rid]);
803 return IDENTIFIER_POINTER (ridpointers [rid]);
806 /* Push current bindings for the function name VAR_DECLS. */
808 void
809 start_fname_decls (void)
811 unsigned ix;
812 tree saved = NULL_TREE;
814 for (ix = 0; fname_vars[ix].decl; ix++)
816 tree decl = *fname_vars[ix].decl;
818 if (decl)
820 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
821 saved);
822 *fname_vars[ix].decl = NULL_TREE;
825 if (saved || saved_function_name_decls)
826 /* Normally they'll have been NULL, so only push if we've got a
827 stack, or they are non-NULL. */
828 saved_function_name_decls = tree_cons (saved, NULL_TREE,
829 saved_function_name_decls);
832 /* Finish up the current bindings, adding them into the current function's
833 statement tree. This must be done _before_ finish_stmt_tree is called.
834 If there is no current function, we must be at file scope and no statements
835 are involved. Pop the previous bindings. */
837 void
838 finish_fname_decls (void)
840 unsigned ix;
841 tree stmts = NULL_TREE;
842 tree stack = saved_function_name_decls;
844 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
845 append_to_statement_list (TREE_VALUE (stack), &stmts);
847 if (stmts)
849 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
851 if (TREE_CODE (*bodyp) == BIND_EXPR)
852 bodyp = &BIND_EXPR_BODY (*bodyp);
854 append_to_statement_list_force (*bodyp, &stmts);
855 *bodyp = stmts;
858 for (ix = 0; fname_vars[ix].decl; ix++)
859 *fname_vars[ix].decl = NULL_TREE;
861 if (stack)
863 /* We had saved values, restore them. */
864 tree saved;
866 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
868 tree decl = TREE_PURPOSE (saved);
869 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
871 *fname_vars[ix].decl = decl;
873 stack = TREE_CHAIN (stack);
875 saved_function_name_decls = stack;
878 /* Return the text name of the current function, suitably prettified
879 by PRETTY_P. Return string must be freed by caller. */
881 const char *
882 fname_as_string (int pretty_p)
884 const char *name = "top level";
885 char *namep;
886 int vrb = 2, len;
887 cpp_string cstr = { 0, 0 }, strname;
889 if (!pretty_p)
891 name = "";
892 vrb = 0;
895 if (current_function_decl)
896 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
898 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
900 namep = XNEWVEC (char, len);
901 snprintf (namep, len, "\"%s\"", name);
902 strname.text = (unsigned char *) namep;
903 strname.len = len - 1;
905 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
907 XDELETEVEC (namep);
908 return (const char *) cstr.text;
911 return namep;
914 /* Return the VAR_DECL for a const char array naming the current
915 function. If the VAR_DECL has not yet been created, create it
916 now. RID indicates how it should be formatted and IDENTIFIER_NODE
917 ID is its name (unfortunately C and C++ hold the RID values of
918 keywords in different places, so we can't derive RID from ID in
919 this language independent code. LOC is the location of the
920 function. */
922 tree
923 fname_decl (location_t loc, unsigned int rid, tree id)
925 unsigned ix;
926 tree decl = NULL_TREE;
928 for (ix = 0; fname_vars[ix].decl; ix++)
929 if (fname_vars[ix].rid == rid)
930 break;
932 decl = *fname_vars[ix].decl;
933 if (!decl)
935 /* If a tree is built here, it would normally have the lineno of
936 the current statement. Later this tree will be moved to the
937 beginning of the function and this line number will be wrong.
938 To avoid this problem set the lineno to 0 here; that prevents
939 it from appearing in the RTL. */
940 tree stmts;
941 location_t saved_location = input_location;
942 input_location = UNKNOWN_LOCATION;
944 stmts = push_stmt_list ();
945 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
946 stmts = pop_stmt_list (stmts);
947 if (!IS_EMPTY_STMT (stmts))
948 saved_function_name_decls
949 = tree_cons (decl, stmts, saved_function_name_decls);
950 *fname_vars[ix].decl = decl;
951 input_location = saved_location;
953 if (!ix && !current_function_decl)
954 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
956 return decl;
959 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
961 tree
962 fix_string_type (tree value)
964 int length = TREE_STRING_LENGTH (value);
965 int nchars;
966 tree e_type, i_type, a_type;
968 /* Compute the number of elements, for the array type. */
969 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
971 nchars = length;
972 e_type = char_type_node;
974 else if (TREE_TYPE (value) == char16_array_type_node)
976 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
977 e_type = char16_type_node;
979 else if (TREE_TYPE (value) == char32_array_type_node)
981 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
982 e_type = char32_type_node;
984 else
986 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
987 e_type = wchar_type_node;
990 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
991 limit in C++98 Annex B is very large (65536) and is not normative,
992 so we do not diagnose it (warn_overlength_strings is forced off
993 in c_common_post_options). */
994 if (warn_overlength_strings)
996 const int nchars_max = flag_isoc99 ? 4095 : 509;
997 const int relevant_std = flag_isoc99 ? 99 : 90;
998 if (nchars - 1 > nchars_max)
999 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1000 separate the %d from the 'C'. 'ISO' should not be
1001 translated, but it may be moved after 'C%d' in languages
1002 where modifiers follow nouns. */
1003 pedwarn (input_location, OPT_Woverlength_strings,
1004 "string length %qd is greater than the length %qd "
1005 "ISO C%d compilers are required to support",
1006 nchars - 1, nchars_max, relevant_std);
1009 /* Create the array type for the string constant. The ISO C++
1010 standard says that a string literal has type `const char[N]' or
1011 `const wchar_t[N]'. We use the same logic when invoked as a C
1012 front-end with -Wwrite-strings.
1013 ??? We should change the type of an expression depending on the
1014 state of a warning flag. We should just be warning -- see how
1015 this is handled in the C++ front-end for the deprecated implicit
1016 conversion from string literals to `char*' or `wchar_t*'.
1018 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1019 array type being the unqualified version of that type.
1020 Therefore, if we are constructing an array of const char, we must
1021 construct the matching unqualified array type first. The C front
1022 end does not require this, but it does no harm, so we do it
1023 unconditionally. */
1024 i_type = build_index_type (size_int (nchars - 1));
1025 a_type = build_array_type (e_type, i_type);
1026 if (c_dialect_cxx() || warn_write_strings)
1027 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1029 TREE_TYPE (value) = a_type;
1030 TREE_CONSTANT (value) = 1;
1031 TREE_READONLY (value) = 1;
1032 TREE_STATIC (value) = 1;
1033 return value;
1036 /* If DISABLE is true, stop issuing warnings. This is used when
1037 parsing code that we know will not be executed. This function may
1038 be called multiple times, and works as a stack. */
1040 static void
1041 c_disable_warnings (bool disable)
1043 if (disable)
1045 ++c_inhibit_evaluation_warnings;
1046 fold_defer_overflow_warnings ();
1050 /* If ENABLE is true, reenable issuing warnings. */
1052 static void
1053 c_enable_warnings (bool enable)
1055 if (enable)
1057 --c_inhibit_evaluation_warnings;
1058 fold_undefer_and_ignore_overflow_warnings ();
1062 /* Fully fold EXPR, an expression that was not folded (beyond integer
1063 constant expressions and null pointer constants) when being built
1064 up. If IN_INIT, this is in a static initializer and certain
1065 changes are made to the folding done. Clear *MAYBE_CONST if
1066 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1067 expression because it contains an evaluated operator (in C99) or an
1068 operator outside of sizeof returning an integer constant (in C90)
1069 not permitted in constant expressions, or because it contains an
1070 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1071 set to true by callers before calling this function.) Return the
1072 folded expression. Function arguments have already been folded
1073 before calling this function, as have the contents of SAVE_EXPR,
1074 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1075 C_MAYBE_CONST_EXPR. */
1077 tree
1078 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1080 tree ret;
1081 tree eptype = NULL_TREE;
1082 bool dummy = true;
1083 bool maybe_const_itself = true;
1084 location_t loc = EXPR_LOCATION (expr);
1086 /* This function is not relevant to C++ because C++ folds while
1087 parsing, and may need changes to be correct for C++ when C++
1088 stops folding while parsing. */
1089 if (c_dialect_cxx ())
1090 gcc_unreachable ();
1092 if (!maybe_const)
1093 maybe_const = &dummy;
1094 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1096 eptype = TREE_TYPE (expr);
1097 expr = TREE_OPERAND (expr, 0);
1099 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1100 &maybe_const_itself);
1101 if (eptype)
1102 ret = fold_convert_loc (loc, eptype, ret);
1103 *maybe_const &= maybe_const_itself;
1104 return ret;
1107 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1108 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1109 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1110 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1111 both evaluated and unevaluated subexpressions while
1112 *MAYBE_CONST_ITSELF is carried from only evaluated
1113 subexpressions). */
1115 static tree
1116 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1117 bool *maybe_const_itself)
1119 tree ret = expr;
1120 enum tree_code code = TREE_CODE (expr);
1121 enum tree_code_class kind = TREE_CODE_CLASS (code);
1122 location_t loc = EXPR_LOCATION (expr);
1123 tree op0, op1, op2, op3;
1124 tree orig_op0, orig_op1, orig_op2;
1125 bool op0_const = true, op1_const = true, op2_const = true;
1126 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1127 bool nowarning = TREE_NO_WARNING (expr);
1128 bool unused_p;
1130 /* This function is not relevant to C++ because C++ folds while
1131 parsing, and may need changes to be correct for C++ when C++
1132 stops folding while parsing. */
1133 if (c_dialect_cxx ())
1134 gcc_unreachable ();
1136 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1137 anything else not counted as an expression cannot usefully be
1138 folded further at this point. */
1139 if (!IS_EXPR_CODE_CLASS (kind)
1140 || kind == tcc_statement
1141 || code == SAVE_EXPR)
1142 return expr;
1144 /* Operands of variable-length expressions (function calls) have
1145 already been folded, as have __builtin_* function calls, and such
1146 expressions cannot occur in constant expressions. */
1147 if (kind == tcc_vl_exp)
1149 *maybe_const_operands = false;
1150 ret = fold (expr);
1151 goto out;
1154 if (code == C_MAYBE_CONST_EXPR)
1156 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1157 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1158 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1159 *maybe_const_operands = false;
1160 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1161 *maybe_const_itself = false;
1162 if (pre && !in_init)
1163 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1164 else
1165 ret = inner;
1166 goto out;
1169 /* Assignment, increment, decrement, function call and comma
1170 operators, and statement expressions, cannot occur in constant
1171 expressions if evaluated / outside of sizeof. (Function calls
1172 were handled above, though VA_ARG_EXPR is treated like a function
1173 call here, and statement expressions are handled through
1174 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1175 switch (code)
1177 case MODIFY_EXPR:
1178 case PREDECREMENT_EXPR:
1179 case PREINCREMENT_EXPR:
1180 case POSTDECREMENT_EXPR:
1181 case POSTINCREMENT_EXPR:
1182 case COMPOUND_EXPR:
1183 *maybe_const_operands = false;
1184 break;
1186 case VA_ARG_EXPR:
1187 case TARGET_EXPR:
1188 case BIND_EXPR:
1189 case OBJ_TYPE_REF:
1190 *maybe_const_operands = false;
1191 ret = fold (expr);
1192 goto out;
1194 default:
1195 break;
1198 /* Fold individual tree codes as appropriate. */
1199 switch (code)
1201 case COMPOUND_LITERAL_EXPR:
1202 /* Any non-constancy will have been marked in a containing
1203 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1204 goto out;
1206 case COMPONENT_REF:
1207 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1208 op1 = TREE_OPERAND (expr, 1);
1209 op2 = TREE_OPERAND (expr, 2);
1210 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1211 maybe_const_itself);
1212 STRIP_TYPE_NOPS (op0);
1213 if (op0 != orig_op0)
1214 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1215 if (ret != expr)
1217 TREE_READONLY (ret) = TREE_READONLY (expr);
1218 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1220 goto out;
1222 case ARRAY_REF:
1223 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1224 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1225 op2 = TREE_OPERAND (expr, 2);
1226 op3 = TREE_OPERAND (expr, 3);
1227 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1228 maybe_const_itself);
1229 STRIP_TYPE_NOPS (op0);
1230 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1231 maybe_const_itself);
1232 STRIP_TYPE_NOPS (op1);
1233 op1 = decl_constant_value_for_optimization (op1);
1234 if (op0 != orig_op0 || op1 != orig_op1)
1235 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1236 if (ret != expr)
1238 TREE_READONLY (ret) = TREE_READONLY (expr);
1239 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1240 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1242 ret = fold (ret);
1243 goto out;
1245 case COMPOUND_EXPR:
1246 case MODIFY_EXPR:
1247 case PREDECREMENT_EXPR:
1248 case PREINCREMENT_EXPR:
1249 case POSTDECREMENT_EXPR:
1250 case POSTINCREMENT_EXPR:
1251 case PLUS_EXPR:
1252 case MINUS_EXPR:
1253 case MULT_EXPR:
1254 case POINTER_PLUS_EXPR:
1255 case TRUNC_DIV_EXPR:
1256 case CEIL_DIV_EXPR:
1257 case FLOOR_DIV_EXPR:
1258 case TRUNC_MOD_EXPR:
1259 case RDIV_EXPR:
1260 case EXACT_DIV_EXPR:
1261 case LSHIFT_EXPR:
1262 case RSHIFT_EXPR:
1263 case BIT_IOR_EXPR:
1264 case BIT_XOR_EXPR:
1265 case BIT_AND_EXPR:
1266 case LT_EXPR:
1267 case LE_EXPR:
1268 case GT_EXPR:
1269 case GE_EXPR:
1270 case EQ_EXPR:
1271 case NE_EXPR:
1272 case COMPLEX_EXPR:
1273 case TRUTH_AND_EXPR:
1274 case TRUTH_OR_EXPR:
1275 case TRUTH_XOR_EXPR:
1276 case UNORDERED_EXPR:
1277 case ORDERED_EXPR:
1278 case UNLT_EXPR:
1279 case UNLE_EXPR:
1280 case UNGT_EXPR:
1281 case UNGE_EXPR:
1282 case UNEQ_EXPR:
1283 /* Binary operations evaluating both arguments (increment and
1284 decrement are binary internally in GCC). */
1285 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1286 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1287 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1288 maybe_const_itself);
1289 STRIP_TYPE_NOPS (op0);
1290 if (code != MODIFY_EXPR
1291 && code != PREDECREMENT_EXPR
1292 && code != PREINCREMENT_EXPR
1293 && code != POSTDECREMENT_EXPR
1294 && code != POSTINCREMENT_EXPR)
1295 op0 = decl_constant_value_for_optimization (op0);
1296 /* The RHS of a MODIFY_EXPR was fully folded when building that
1297 expression for the sake of conversion warnings. */
1298 if (code != MODIFY_EXPR)
1299 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1300 maybe_const_itself);
1301 STRIP_TYPE_NOPS (op1);
1302 op1 = decl_constant_value_for_optimization (op1);
1303 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1304 ret = in_init
1305 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1306 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1307 else
1308 ret = fold (expr);
1309 if (TREE_OVERFLOW_P (ret)
1310 && !TREE_OVERFLOW_P (op0)
1311 && !TREE_OVERFLOW_P (op1))
1312 overflow_warning (EXPR_LOCATION (expr), ret);
1313 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1314 && TREE_CODE (orig_op1) != INTEGER_CST
1315 && TREE_CODE (op1) == INTEGER_CST
1316 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1317 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1318 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1319 && c_inhibit_evaluation_warnings == 0)
1321 if (tree_int_cst_sgn (op1) < 0)
1322 warning_at (loc, 0, (code == LSHIFT_EXPR
1323 ? G_("left shift count is negative")
1324 : G_("right shift count is negative")));
1325 else if (compare_tree_int (op1,
1326 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1327 >= 0)
1328 warning_at (loc, 0, (code == LSHIFT_EXPR
1329 ? G_("left shift count >= width of type")
1330 : G_("right shift count >= width of type")));
1332 goto out;
1334 case INDIRECT_REF:
1335 case FIX_TRUNC_EXPR:
1336 case FLOAT_EXPR:
1337 CASE_CONVERT:
1338 case VIEW_CONVERT_EXPR:
1339 case NON_LVALUE_EXPR:
1340 case NEGATE_EXPR:
1341 case BIT_NOT_EXPR:
1342 case TRUTH_NOT_EXPR:
1343 case ADDR_EXPR:
1344 case CONJ_EXPR:
1345 case REALPART_EXPR:
1346 case IMAGPART_EXPR:
1347 /* Unary operations. */
1348 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1349 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1350 maybe_const_itself);
1351 STRIP_TYPE_NOPS (op0);
1352 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1353 op0 = decl_constant_value_for_optimization (op0);
1354 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1355 not prepared to deal with them if they occur in initializers. */
1356 if (op0 != orig_op0
1357 && code == ADDR_EXPR
1358 && (op1 = get_base_address (op0)) != NULL_TREE
1359 && TREE_CODE (op1) == INDIRECT_REF
1360 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1361 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1362 else if (op0 != orig_op0 || in_init)
1363 ret = in_init
1364 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1365 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1366 else
1367 ret = fold (expr);
1368 if (code == INDIRECT_REF
1369 && ret != expr
1370 && TREE_CODE (ret) == INDIRECT_REF)
1372 TREE_READONLY (ret) = TREE_READONLY (expr);
1373 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1374 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1376 switch (code)
1378 case FIX_TRUNC_EXPR:
1379 case FLOAT_EXPR:
1380 CASE_CONVERT:
1381 /* Don't warn about explicit conversions. We will already
1382 have warned about suspect implicit conversions. */
1383 break;
1385 default:
1386 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1387 overflow_warning (EXPR_LOCATION (expr), ret);
1388 break;
1390 goto out;
1392 case TRUTH_ANDIF_EXPR:
1393 case TRUTH_ORIF_EXPR:
1394 /* Binary operations not necessarily evaluating both
1395 arguments. */
1396 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1397 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1398 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1399 STRIP_TYPE_NOPS (op0);
1401 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1402 ? truthvalue_false_node
1403 : truthvalue_true_node));
1404 c_disable_warnings (unused_p);
1405 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1406 STRIP_TYPE_NOPS (op1);
1407 c_enable_warnings (unused_p);
1409 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1410 ret = in_init
1411 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1412 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1413 else
1414 ret = fold (expr);
1415 *maybe_const_operands &= op0_const;
1416 *maybe_const_itself &= op0_const_self;
1417 if (!(flag_isoc99
1418 && op0_const
1419 && op0_const_self
1420 && (code == TRUTH_ANDIF_EXPR
1421 ? op0 == truthvalue_false_node
1422 : op0 == truthvalue_true_node)))
1423 *maybe_const_operands &= op1_const;
1424 if (!(op0_const
1425 && op0_const_self
1426 && (code == TRUTH_ANDIF_EXPR
1427 ? op0 == truthvalue_false_node
1428 : op0 == truthvalue_true_node)))
1429 *maybe_const_itself &= op1_const_self;
1430 goto out;
1432 case COND_EXPR:
1433 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1434 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1435 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1436 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1438 STRIP_TYPE_NOPS (op0);
1439 c_disable_warnings (op0 == truthvalue_false_node);
1440 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1441 STRIP_TYPE_NOPS (op1);
1442 c_enable_warnings (op0 == truthvalue_false_node);
1444 c_disable_warnings (op0 == truthvalue_true_node);
1445 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1446 STRIP_TYPE_NOPS (op2);
1447 c_enable_warnings (op0 == truthvalue_true_node);
1449 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1450 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1451 else
1452 ret = fold (expr);
1453 *maybe_const_operands &= op0_const;
1454 *maybe_const_itself &= op0_const_self;
1455 if (!(flag_isoc99
1456 && op0_const
1457 && op0_const_self
1458 && op0 == truthvalue_false_node))
1459 *maybe_const_operands &= op1_const;
1460 if (!(op0_const
1461 && op0_const_self
1462 && op0 == truthvalue_false_node))
1463 *maybe_const_itself &= op1_const_self;
1464 if (!(flag_isoc99
1465 && op0_const
1466 && op0_const_self
1467 && op0 == truthvalue_true_node))
1468 *maybe_const_operands &= op2_const;
1469 if (!(op0_const
1470 && op0_const_self
1471 && op0 == truthvalue_true_node))
1472 *maybe_const_itself &= op2_const_self;
1473 goto out;
1475 case EXCESS_PRECISION_EXPR:
1476 /* Each case where an operand with excess precision may be
1477 encountered must remove the EXCESS_PRECISION_EXPR around
1478 inner operands and possibly put one around the whole
1479 expression or possibly convert to the semantic type (which
1480 c_fully_fold does); we cannot tell at this stage which is
1481 appropriate in any particular case. */
1482 gcc_unreachable ();
1484 default:
1485 /* Various codes may appear through folding built-in functions
1486 and their arguments. */
1487 goto out;
1490 out:
1491 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1492 have been done by this point, so remove them again. */
1493 nowarning |= TREE_NO_WARNING (ret);
1494 STRIP_TYPE_NOPS (ret);
1495 if (nowarning && !TREE_NO_WARNING (ret))
1497 if (!CAN_HAVE_LOCATION_P (ret))
1498 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1499 TREE_NO_WARNING (ret) = 1;
1501 if (ret != expr)
1502 protected_set_expr_location (ret, loc);
1503 return ret;
1506 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1507 return EXP. Otherwise, return either EXP or its known constant
1508 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1509 Is the BLKmode test appropriate? */
1511 tree
1512 decl_constant_value_for_optimization (tree exp)
1514 tree ret;
1516 /* This function is only used by C, for c_fully_fold and other
1517 optimization, and may not be correct for C++. */
1518 if (c_dialect_cxx ())
1519 gcc_unreachable ();
1521 if (!optimize
1522 || TREE_CODE (exp) != VAR_DECL
1523 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1524 || DECL_MODE (exp) == BLKmode)
1525 return exp;
1527 ret = decl_constant_value (exp);
1528 /* Avoid unwanted tree sharing between the initializer and current
1529 function's body where the tree can be modified e.g. by the
1530 gimplifier. */
1531 if (ret != exp && TREE_STATIC (exp))
1532 ret = unshare_expr (ret);
1533 return ret;
1536 /* Print a warning if a constant expression had overflow in folding.
1537 Invoke this function on every expression that the language
1538 requires to be a constant expression.
1539 Note the ANSI C standard says it is erroneous for a
1540 constant expression to overflow. */
1542 void
1543 constant_expression_warning (tree value)
1545 if (warn_overflow && pedantic
1546 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1547 || TREE_CODE (value) == FIXED_CST
1548 || TREE_CODE (value) == VECTOR_CST
1549 || TREE_CODE (value) == COMPLEX_CST)
1550 && TREE_OVERFLOW (value))
1551 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1554 /* The same as above but print an unconditional error. */
1555 void
1556 constant_expression_error (tree value)
1558 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1559 || TREE_CODE (value) == FIXED_CST
1560 || TREE_CODE (value) == VECTOR_CST
1561 || TREE_CODE (value) == COMPLEX_CST)
1562 && TREE_OVERFLOW (value))
1563 error ("overflow in constant expression");
1566 /* Print a warning if an expression had overflow in folding and its
1567 operands hadn't.
1569 Invoke this function on every expression that
1570 (1) appears in the source code, and
1571 (2) is a constant expression that overflowed, and
1572 (3) is not already checked by convert_and_check;
1573 however, do not invoke this function on operands of explicit casts
1574 or when the expression is the result of an operator and any operand
1575 already overflowed. */
1577 void
1578 overflow_warning (location_t loc, tree value)
1580 if (c_inhibit_evaluation_warnings != 0)
1581 return;
1583 switch (TREE_CODE (value))
1585 case INTEGER_CST:
1586 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1587 break;
1589 case REAL_CST:
1590 warning_at (loc, OPT_Woverflow,
1591 "floating point overflow in expression");
1592 break;
1594 case FIXED_CST:
1595 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1596 break;
1598 case VECTOR_CST:
1599 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1600 break;
1602 case COMPLEX_CST:
1603 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1604 warning_at (loc, OPT_Woverflow,
1605 "complex integer overflow in expression");
1606 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1607 warning_at (loc, OPT_Woverflow,
1608 "complex floating point overflow in expression");
1609 break;
1611 default:
1612 break;
1616 /* Warn about uses of logical || / && operator in a context where it
1617 is likely that the bitwise equivalent was intended by the
1618 programmer. We have seen an expression in which CODE is a binary
1619 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1620 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1621 void
1622 warn_logical_operator (location_t location, enum tree_code code, tree type,
1623 enum tree_code code_left, tree op_left,
1624 enum tree_code ARG_UNUSED (code_right), tree op_right)
1626 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1627 int in0_p, in1_p, in_p;
1628 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1629 bool strict_overflow_p = false;
1631 if (code != TRUTH_ANDIF_EXPR
1632 && code != TRUTH_AND_EXPR
1633 && code != TRUTH_ORIF_EXPR
1634 && code != TRUTH_OR_EXPR)
1635 return;
1637 /* Warn if &&/|| are being used in a context where it is
1638 likely that the bitwise equivalent was intended by the
1639 programmer. That is, an expression such as op && MASK
1640 where op should not be any boolean expression, nor a
1641 constant, and mask seems to be a non-boolean integer constant. */
1642 if (!truth_value_p (code_left)
1643 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1644 && !CONSTANT_CLASS_P (op_left)
1645 && !TREE_NO_WARNING (op_left)
1646 && TREE_CODE (op_right) == INTEGER_CST
1647 && !integer_zerop (op_right)
1648 && !integer_onep (op_right))
1650 if (or_op)
1651 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1652 " applied to non-boolean constant");
1653 else
1654 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1655 " applied to non-boolean constant");
1656 TREE_NO_WARNING (op_left) = true;
1657 return;
1660 /* We do not warn for constants because they are typical of macro
1661 expansions that test for features. */
1662 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1663 return;
1665 /* This warning only makes sense with logical operands. */
1666 if (!(truth_value_p (TREE_CODE (op_left))
1667 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1668 || !(truth_value_p (TREE_CODE (op_right))
1669 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1670 return;
1673 /* We first test whether either side separately is trivially true
1674 (with OR) or trivially false (with AND). If so, do not warn.
1675 This is a common idiom for testing ranges of data types in
1676 portable code. */
1677 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1678 if (!lhs)
1679 return;
1680 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1681 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1683 /* If this is an OR operation, invert both sides; now, the result
1684 should be always false to get a warning. */
1685 if (or_op)
1686 in0_p = !in0_p;
1688 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1689 if (tem && integer_zerop (tem))
1690 return;
1692 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1693 if (!rhs)
1694 return;
1695 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1696 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1698 /* If this is an OR operation, invert both sides; now, the result
1699 should be always false to get a warning. */
1700 if (or_op)
1701 in1_p = !in1_p;
1703 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1704 if (tem && integer_zerop (tem))
1705 return;
1707 /* If both expressions have the same operand, if we can merge the
1708 ranges, and if the range test is always false, then warn. */
1709 if (operand_equal_p (lhs, rhs, 0)
1710 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1711 in1_p, low1, high1)
1712 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1713 type, lhs, in_p, low, high))
1714 && integer_zerop (tem))
1716 if (or_op)
1717 warning_at (location, OPT_Wlogical_op,
1718 "logical %<or%> "
1719 "of collectively exhaustive tests is always true");
1720 else
1721 warning_at (location, OPT_Wlogical_op,
1722 "logical %<and%> "
1723 "of mutually exclusive tests is always false");
1728 /* Warn if EXP contains any computations whose results are not used.
1729 Return true if a warning is printed; false otherwise. LOCUS is the
1730 (potential) location of the expression. */
1732 bool
1733 warn_if_unused_value (const_tree exp, location_t locus)
1735 restart:
1736 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1737 return false;
1739 /* Don't warn about void constructs. This includes casting to void,
1740 void function calls, and statement expressions with a final cast
1741 to void. */
1742 if (VOID_TYPE_P (TREE_TYPE (exp)))
1743 return false;
1745 if (EXPR_HAS_LOCATION (exp))
1746 locus = EXPR_LOCATION (exp);
1748 switch (TREE_CODE (exp))
1750 case PREINCREMENT_EXPR:
1751 case POSTINCREMENT_EXPR:
1752 case PREDECREMENT_EXPR:
1753 case POSTDECREMENT_EXPR:
1754 case MODIFY_EXPR:
1755 case INIT_EXPR:
1756 case TARGET_EXPR:
1757 case CALL_EXPR:
1758 case TRY_CATCH_EXPR:
1759 case WITH_CLEANUP_EXPR:
1760 case EXIT_EXPR:
1761 case VA_ARG_EXPR:
1762 return false;
1764 case BIND_EXPR:
1765 /* For a binding, warn if no side effect within it. */
1766 exp = BIND_EXPR_BODY (exp);
1767 goto restart;
1769 case SAVE_EXPR:
1770 case NON_LVALUE_EXPR:
1771 case NOP_EXPR:
1772 exp = TREE_OPERAND (exp, 0);
1773 goto restart;
1775 case TRUTH_ORIF_EXPR:
1776 case TRUTH_ANDIF_EXPR:
1777 /* In && or ||, warn if 2nd operand has no side effect. */
1778 exp = TREE_OPERAND (exp, 1);
1779 goto restart;
1781 case COMPOUND_EXPR:
1782 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1783 return true;
1784 /* Let people do `(foo (), 0)' without a warning. */
1785 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1786 return false;
1787 exp = TREE_OPERAND (exp, 1);
1788 goto restart;
1790 case COND_EXPR:
1791 /* If this is an expression with side effects, don't warn; this
1792 case commonly appears in macro expansions. */
1793 if (TREE_SIDE_EFFECTS (exp))
1794 return false;
1795 goto warn;
1797 case INDIRECT_REF:
1798 /* Don't warn about automatic dereferencing of references, since
1799 the user cannot control it. */
1800 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1802 exp = TREE_OPERAND (exp, 0);
1803 goto restart;
1805 /* Fall through. */
1807 default:
1808 /* Referencing a volatile value is a side effect, so don't warn. */
1809 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1810 && TREE_THIS_VOLATILE (exp))
1811 return false;
1813 /* If this is an expression which has no operands, there is no value
1814 to be unused. There are no such language-independent codes,
1815 but front ends may define such. */
1816 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1817 return false;
1819 warn:
1820 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1825 /* Print a warning about casts that might indicate violation
1826 of strict aliasing rules if -Wstrict-aliasing is used and
1827 strict aliasing mode is in effect. OTYPE is the original
1828 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1830 bool
1831 strict_aliasing_warning (tree otype, tree type, tree expr)
1833 /* Strip pointer conversion chains and get to the correct original type. */
1834 STRIP_NOPS (expr);
1835 otype = TREE_TYPE (expr);
1837 if (!(flag_strict_aliasing
1838 && POINTER_TYPE_P (type)
1839 && POINTER_TYPE_P (otype)
1840 && !VOID_TYPE_P (TREE_TYPE (type)))
1841 /* If the type we are casting to is a ref-all pointer
1842 dereferencing it is always valid. */
1843 || TYPE_REF_CAN_ALIAS_ALL (type))
1844 return false;
1846 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1847 && (DECL_P (TREE_OPERAND (expr, 0))
1848 || handled_component_p (TREE_OPERAND (expr, 0))))
1850 /* Casting the address of an object to non void pointer. Warn
1851 if the cast breaks type based aliasing. */
1852 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1854 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1855 "might break strict-aliasing rules");
1856 return true;
1858 else
1860 /* warn_strict_aliasing >= 3. This includes the default (3).
1861 Only warn if the cast is dereferenced immediately. */
1862 alias_set_type set1 =
1863 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1864 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1866 if (set1 != set2 && set2 != 0
1867 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1869 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1870 "pointer will break strict-aliasing rules");
1871 return true;
1873 else if (warn_strict_aliasing == 2
1874 && !alias_sets_must_conflict_p (set1, set2))
1876 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1877 "pointer might break strict-aliasing rules");
1878 return true;
1882 else
1883 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1885 /* At this level, warn for any conversions, even if an address is
1886 not taken in the same statement. This will likely produce many
1887 false positives, but could be useful to pinpoint problems that
1888 are not revealed at higher levels. */
1889 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1890 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1891 if (!COMPLETE_TYPE_P (type)
1892 || !alias_sets_must_conflict_p (set1, set2))
1894 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1895 "pointer might break strict-aliasing rules");
1896 return true;
1900 return false;
1903 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1904 sizeof as last operand of certain builtins. */
1906 void
1907 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1908 vec<tree, va_gc> *params, tree *sizeof_arg,
1909 bool (*comp_types) (tree, tree))
1911 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1912 bool strop = false, cmp = false;
1913 unsigned int idx = ~0;
1914 location_t loc;
1916 if (TREE_CODE (callee) != FUNCTION_DECL
1917 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1918 || vec_safe_length (params) <= 1)
1919 return;
1921 switch (DECL_FUNCTION_CODE (callee))
1923 case BUILT_IN_STRNCMP:
1924 case BUILT_IN_STRNCASECMP:
1925 cmp = true;
1926 /* FALLTHRU */
1927 case BUILT_IN_STRNCPY:
1928 case BUILT_IN_STRNCPY_CHK:
1929 case BUILT_IN_STRNCAT:
1930 case BUILT_IN_STRNCAT_CHK:
1931 case BUILT_IN_STPNCPY:
1932 case BUILT_IN_STPNCPY_CHK:
1933 strop = true;
1934 /* FALLTHRU */
1935 case BUILT_IN_MEMCPY:
1936 case BUILT_IN_MEMCPY_CHK:
1937 case BUILT_IN_MEMMOVE:
1938 case BUILT_IN_MEMMOVE_CHK:
1939 if (params->length () < 3)
1940 return;
1941 src = (*params)[1];
1942 dest = (*params)[0];
1943 idx = 2;
1944 break;
1945 case BUILT_IN_BCOPY:
1946 if (params->length () < 3)
1947 return;
1948 src = (*params)[0];
1949 dest = (*params)[1];
1950 idx = 2;
1951 break;
1952 case BUILT_IN_MEMCMP:
1953 case BUILT_IN_BCMP:
1954 if (params->length () < 3)
1955 return;
1956 src = (*params)[1];
1957 dest = (*params)[0];
1958 idx = 2;
1959 cmp = true;
1960 break;
1961 case BUILT_IN_MEMSET:
1962 case BUILT_IN_MEMSET_CHK:
1963 if (params->length () < 3)
1964 return;
1965 dest = (*params)[0];
1966 idx = 2;
1967 break;
1968 case BUILT_IN_BZERO:
1969 dest = (*params)[0];
1970 idx = 1;
1971 break;
1972 case BUILT_IN_STRNDUP:
1973 src = (*params)[0];
1974 strop = true;
1975 idx = 1;
1976 break;
1977 case BUILT_IN_MEMCHR:
1978 if (params->length () < 3)
1979 return;
1980 src = (*params)[0];
1981 idx = 2;
1982 break;
1983 case BUILT_IN_SNPRINTF:
1984 case BUILT_IN_SNPRINTF_CHK:
1985 case BUILT_IN_VSNPRINTF:
1986 case BUILT_IN_VSNPRINTF_CHK:
1987 dest = (*params)[0];
1988 idx = 1;
1989 strop = true;
1990 break;
1991 default:
1992 break;
1995 if (idx >= 3)
1996 return;
1998 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1999 return;
2001 type = TYPE_P (sizeof_arg[idx])
2002 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2003 if (!POINTER_TYPE_P (type))
2004 return;
2006 if (dest
2007 && (tem = tree_strip_nop_conversions (dest))
2008 && POINTER_TYPE_P (TREE_TYPE (tem))
2009 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2010 return;
2012 if (src
2013 && (tem = tree_strip_nop_conversions (src))
2014 && POINTER_TYPE_P (TREE_TYPE (tem))
2015 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2016 return;
2018 loc = sizeof_arg_loc[idx];
2020 if (dest && !cmp)
2022 if (!TYPE_P (sizeof_arg[idx])
2023 && operand_equal_p (dest, sizeof_arg[idx], 0)
2024 && comp_types (TREE_TYPE (dest), type))
2026 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2027 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2028 "argument to %<sizeof%> in %qD call is the same "
2029 "expression as the destination; did you mean to "
2030 "remove the addressof?", callee);
2031 else if ((TYPE_PRECISION (TREE_TYPE (type))
2032 == TYPE_PRECISION (char_type_node))
2033 || strop)
2034 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2035 "argument to %<sizeof%> in %qD call is the same "
2036 "expression as the destination; did you mean to "
2037 "provide an explicit length?", callee);
2038 else
2039 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2040 "argument to %<sizeof%> in %qD call is the same "
2041 "expression as the destination; did you mean to "
2042 "dereference it?", callee);
2043 return;
2046 if (POINTER_TYPE_P (TREE_TYPE (dest))
2047 && !strop
2048 && comp_types (TREE_TYPE (dest), type)
2049 && !VOID_TYPE_P (TREE_TYPE (type)))
2051 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2052 "argument to %<sizeof%> in %qD call is the same "
2053 "pointer type %qT as the destination; expected %qT "
2054 "or an explicit length", callee, TREE_TYPE (dest),
2055 TREE_TYPE (TREE_TYPE (dest)));
2056 return;
2060 if (src && !cmp)
2062 if (!TYPE_P (sizeof_arg[idx])
2063 && operand_equal_p (src, sizeof_arg[idx], 0)
2064 && comp_types (TREE_TYPE (src), type))
2066 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2067 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2068 "argument to %<sizeof%> in %qD call is the same "
2069 "expression as the source; did you mean to "
2070 "remove the addressof?", callee);
2071 else if ((TYPE_PRECISION (TREE_TYPE (type))
2072 == TYPE_PRECISION (char_type_node))
2073 || strop)
2074 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2075 "argument to %<sizeof%> in %qD call is the same "
2076 "expression as the source; did you mean to "
2077 "provide an explicit length?", callee);
2078 else
2079 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2080 "argument to %<sizeof%> in %qD call is the same "
2081 "expression as the source; did you mean to "
2082 "dereference it?", callee);
2083 return;
2086 if (POINTER_TYPE_P (TREE_TYPE (src))
2087 && !strop
2088 && comp_types (TREE_TYPE (src), type)
2089 && !VOID_TYPE_P (TREE_TYPE (type)))
2091 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2092 "argument to %<sizeof%> in %qD call is the same "
2093 "pointer type %qT as the source; expected %qT "
2094 "or an explicit length", callee, TREE_TYPE (src),
2095 TREE_TYPE (TREE_TYPE (src)));
2096 return;
2100 if (dest)
2102 if (!TYPE_P (sizeof_arg[idx])
2103 && operand_equal_p (dest, sizeof_arg[idx], 0)
2104 && comp_types (TREE_TYPE (dest), type))
2106 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2107 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2108 "argument to %<sizeof%> in %qD call is the same "
2109 "expression as the first source; did you mean to "
2110 "remove the addressof?", callee);
2111 else if ((TYPE_PRECISION (TREE_TYPE (type))
2112 == TYPE_PRECISION (char_type_node))
2113 || strop)
2114 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2115 "argument to %<sizeof%> in %qD call is the same "
2116 "expression as the first source; did you mean to "
2117 "provide an explicit length?", callee);
2118 else
2119 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2120 "argument to %<sizeof%> in %qD call is the same "
2121 "expression as the first source; did you mean to "
2122 "dereference it?", callee);
2123 return;
2126 if (POINTER_TYPE_P (TREE_TYPE (dest))
2127 && !strop
2128 && comp_types (TREE_TYPE (dest), type)
2129 && !VOID_TYPE_P (TREE_TYPE (type)))
2131 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2132 "argument to %<sizeof%> in %qD call is the same "
2133 "pointer type %qT as the first source; expected %qT "
2134 "or an explicit length", callee, TREE_TYPE (dest),
2135 TREE_TYPE (TREE_TYPE (dest)));
2136 return;
2140 if (src)
2142 if (!TYPE_P (sizeof_arg[idx])
2143 && operand_equal_p (src, sizeof_arg[idx], 0)
2144 && comp_types (TREE_TYPE (src), type))
2146 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2147 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2148 "argument to %<sizeof%> in %qD call is the same "
2149 "expression as the second source; did you mean to "
2150 "remove the addressof?", callee);
2151 else if ((TYPE_PRECISION (TREE_TYPE (type))
2152 == TYPE_PRECISION (char_type_node))
2153 || strop)
2154 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2155 "argument to %<sizeof%> in %qD call is the same "
2156 "expression as the second source; did you mean to "
2157 "provide an explicit length?", callee);
2158 else
2159 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2160 "argument to %<sizeof%> in %qD call is the same "
2161 "expression as the second source; did you mean to "
2162 "dereference it?", callee);
2163 return;
2166 if (POINTER_TYPE_P (TREE_TYPE (src))
2167 && !strop
2168 && comp_types (TREE_TYPE (src), type)
2169 && !VOID_TYPE_P (TREE_TYPE (type)))
2171 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2172 "argument to %<sizeof%> in %qD call is the same "
2173 "pointer type %qT as the second source; expected %qT "
2174 "or an explicit length", callee, TREE_TYPE (src),
2175 TREE_TYPE (TREE_TYPE (src)));
2176 return;
2182 /* Warn for unlikely, improbable, or stupid DECL declarations
2183 of `main'. */
2185 void
2186 check_main_parameter_types (tree decl)
2188 function_args_iterator iter;
2189 tree type;
2190 int argct = 0;
2192 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2194 /* XXX void_type_node belies the abstraction. */
2195 if (type == void_type_node || type == error_mark_node )
2196 break;
2198 ++argct;
2199 switch (argct)
2201 case 1:
2202 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2203 pedwarn (input_location, OPT_Wmain,
2204 "first argument of %q+D should be %<int%>", decl);
2205 break;
2207 case 2:
2208 if (TREE_CODE (type) != POINTER_TYPE
2209 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2210 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2211 != char_type_node))
2212 pedwarn (input_location, OPT_Wmain,
2213 "second argument of %q+D should be %<char **%>", decl);
2214 break;
2216 case 3:
2217 if (TREE_CODE (type) != POINTER_TYPE
2218 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2219 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2220 != char_type_node))
2221 pedwarn (input_location, OPT_Wmain,
2222 "third argument of %q+D should probably be "
2223 "%<char **%>", decl);
2224 break;
2228 /* It is intentional that this message does not mention the third
2229 argument because it's only mentioned in an appendix of the
2230 standard. */
2231 if (argct > 0 && (argct < 2 || argct > 3))
2232 pedwarn (input_location, OPT_Wmain,
2233 "%q+D takes only zero or two arguments", decl);
2236 /* vector_targets_convertible_p is used for vector pointer types. The
2237 callers perform various checks that the qualifiers are satisfactory,
2238 while OTOH vector_targets_convertible_p ignores the number of elements
2239 in the vectors. That's fine with vector pointers as we can consider,
2240 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2241 and that does not require and conversion of the pointer values.
2242 In contrast, vector_types_convertible_p and
2243 vector_types_compatible_elements_p are used for vector value types. */
2244 /* True if pointers to distinct types T1 and T2 can be converted to
2245 each other without an explicit cast. Only returns true for opaque
2246 vector types. */
2247 bool
2248 vector_targets_convertible_p (const_tree t1, const_tree t2)
2250 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2251 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2252 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2253 return true;
2255 return false;
2258 /* vector_types_convertible_p is used for vector value types.
2259 It could in principle call vector_targets_convertible_p as a subroutine,
2260 but then the check for vector type would be duplicated with its callers,
2261 and also the purpose of vector_targets_convertible_p would become
2262 muddled.
2263 Where vector_types_convertible_p returns true, a conversion might still be
2264 needed to make the types match.
2265 In contrast, vector_targets_convertible_p is used for vector pointer
2266 values, and vector_types_compatible_elements_p is used specifically
2267 in the context for binary operators, as a check if use is possible without
2268 conversion. */
2269 /* True if vector types T1 and T2 can be converted to each other
2270 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2271 can only be converted with -flax-vector-conversions yet that is not
2272 in effect, emit a note telling the user about that option if such
2273 a note has not previously been emitted. */
2274 bool
2275 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2277 static bool emitted_lax_note = false;
2278 bool convertible_lax;
2280 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2281 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2282 return true;
2284 convertible_lax =
2285 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2286 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2287 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2288 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2289 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2291 if (!convertible_lax || flag_lax_vector_conversions)
2292 return convertible_lax;
2294 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2295 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2296 return true;
2298 if (emit_lax_note && !emitted_lax_note)
2300 emitted_lax_note = true;
2301 inform (input_location, "use -flax-vector-conversions to permit "
2302 "conversions between vectors with differing "
2303 "element types or numbers of subparts");
2306 return false;
2309 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2310 and have vector types, V0 has the same type as V1, and the number of
2311 elements of V0, V1, MASK is the same.
2313 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2314 called with two arguments. In this case implementation passes the
2315 first argument twice in order to share the same tree code. This fact
2316 could enable the mask-values being twice the vector length. This is
2317 an implementation accident and this semantics is not guaranteed to
2318 the user. */
2319 tree
2320 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2321 bool complain)
2323 tree ret;
2324 bool wrap = true;
2325 bool maybe_const = false;
2326 bool two_arguments = false;
2328 if (v1 == NULL_TREE)
2330 two_arguments = true;
2331 v1 = v0;
2334 if (v0 == error_mark_node || v1 == error_mark_node
2335 || mask == error_mark_node)
2336 return error_mark_node;
2338 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2339 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2341 if (complain)
2342 error_at (loc, "__builtin_shuffle last argument must "
2343 "be an integer vector");
2344 return error_mark_node;
2347 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2348 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2350 if (complain)
2351 error_at (loc, "__builtin_shuffle arguments must be vectors");
2352 return error_mark_node;
2355 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2357 if (complain)
2358 error_at (loc, "__builtin_shuffle argument vectors must be of "
2359 "the same type");
2360 return error_mark_node;
2363 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2364 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2365 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2366 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2368 if (complain)
2369 error_at (loc, "__builtin_shuffle number of elements of the "
2370 "argument vector(s) and the mask vector should "
2371 "be the same");
2372 return error_mark_node;
2375 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2376 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2378 if (complain)
2379 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2380 "must have the same size as inner type of the mask");
2381 return error_mark_node;
2384 if (!c_dialect_cxx ())
2386 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2387 v0 = c_fully_fold (v0, false, &maybe_const);
2388 wrap &= maybe_const;
2390 if (two_arguments)
2391 v1 = v0 = save_expr (v0);
2392 else
2394 v1 = c_fully_fold (v1, false, &maybe_const);
2395 wrap &= maybe_const;
2398 mask = c_fully_fold (mask, false, &maybe_const);
2399 wrap &= maybe_const;
2401 else if (two_arguments)
2402 v1 = v0 = save_expr (v0);
2404 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2406 if (!c_dialect_cxx () && !wrap)
2407 ret = c_wrap_maybe_const (ret, true);
2409 return ret;
2412 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2413 to integral type. */
2415 static tree
2416 c_common_get_narrower (tree op, int *unsignedp_ptr)
2418 op = get_narrower (op, unsignedp_ptr);
2420 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2421 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2423 /* C++0x scoped enumerations don't implicitly convert to integral
2424 type; if we stripped an explicit conversion to a larger type we
2425 need to replace it so common_type will still work. */
2426 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2427 TYPE_UNSIGNED (TREE_TYPE (op)));
2428 op = fold_convert (type, op);
2430 return op;
2433 /* This is a helper function of build_binary_op.
2435 For certain operations if both args were extended from the same
2436 smaller type, do the arithmetic in that type and then extend.
2438 BITWISE indicates a bitwise operation.
2439 For them, this optimization is safe only if
2440 both args are zero-extended or both are sign-extended.
2441 Otherwise, we might change the result.
2442 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2443 but calculated in (unsigned short) it would be (unsigned short)-1.
2445 tree
2446 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2448 int unsigned0, unsigned1;
2449 tree arg0, arg1;
2450 int uns;
2451 tree type;
2453 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2454 excessive narrowing when we call get_narrower below. For
2455 example, suppose that OP0 is of unsigned int extended
2456 from signed char and that RESULT_TYPE is long long int.
2457 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2458 like
2460 (long long int) (unsigned int) signed_char
2462 which get_narrower would narrow down to
2464 (unsigned int) signed char
2466 If we do not cast OP0 first, get_narrower would return
2467 signed_char, which is inconsistent with the case of the
2468 explicit cast. */
2469 op0 = convert (result_type, op0);
2470 op1 = convert (result_type, op1);
2472 arg0 = c_common_get_narrower (op0, &unsigned0);
2473 arg1 = c_common_get_narrower (op1, &unsigned1);
2475 /* UNS is 1 if the operation to be done is an unsigned one. */
2476 uns = TYPE_UNSIGNED (result_type);
2478 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2479 but it *requires* conversion to FINAL_TYPE. */
2481 if ((TYPE_PRECISION (TREE_TYPE (op0))
2482 == TYPE_PRECISION (TREE_TYPE (arg0)))
2483 && TREE_TYPE (op0) != result_type)
2484 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2485 if ((TYPE_PRECISION (TREE_TYPE (op1))
2486 == TYPE_PRECISION (TREE_TYPE (arg1)))
2487 && TREE_TYPE (op1) != result_type)
2488 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2490 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2492 /* For bitwise operations, signedness of nominal type
2493 does not matter. Consider only how operands were extended. */
2494 if (bitwise)
2495 uns = unsigned0;
2497 /* Note that in all three cases below we refrain from optimizing
2498 an unsigned operation on sign-extended args.
2499 That would not be valid. */
2501 /* Both args variable: if both extended in same way
2502 from same width, do it in that width.
2503 Do it unsigned if args were zero-extended. */
2504 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2505 < TYPE_PRECISION (result_type))
2506 && (TYPE_PRECISION (TREE_TYPE (arg1))
2507 == TYPE_PRECISION (TREE_TYPE (arg0)))
2508 && unsigned0 == unsigned1
2509 && (unsigned0 || !uns))
2510 return c_common_signed_or_unsigned_type
2511 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2513 else if (TREE_CODE (arg0) == INTEGER_CST
2514 && (unsigned1 || !uns)
2515 && (TYPE_PRECISION (TREE_TYPE (arg1))
2516 < TYPE_PRECISION (result_type))
2517 && (type
2518 = c_common_signed_or_unsigned_type (unsigned1,
2519 TREE_TYPE (arg1)))
2520 && !POINTER_TYPE_P (type)
2521 && int_fits_type_p (arg0, type))
2522 return type;
2524 else if (TREE_CODE (arg1) == INTEGER_CST
2525 && (unsigned0 || !uns)
2526 && (TYPE_PRECISION (TREE_TYPE (arg0))
2527 < TYPE_PRECISION (result_type))
2528 && (type
2529 = c_common_signed_or_unsigned_type (unsigned0,
2530 TREE_TYPE (arg0)))
2531 && !POINTER_TYPE_P (type)
2532 && int_fits_type_p (arg1, type))
2533 return type;
2535 return result_type;
2538 /* Checks if expression EXPR of real/integer type cannot be converted
2539 to the real/integer type TYPE. Function returns true when:
2540 * EXPR is a constant which cannot be exactly converted to TYPE
2541 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2542 for EXPR type and TYPE being both integers or both real.
2543 * EXPR is not a constant of real type and TYPE is an integer.
2544 * EXPR is not a constant of integer type which cannot be
2545 exactly converted to real type.
2546 Function allows conversions between types of different signedness and
2547 does not return true in that case. Function can produce signedness
2548 warnings if PRODUCE_WARNS is true. */
2549 bool
2550 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2552 bool give_warning = false;
2553 tree expr_type = TREE_TYPE (expr);
2554 location_t loc = EXPR_LOC_OR_HERE (expr);
2556 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2558 /* Warn for real constant that is not an exact integer converted
2559 to integer type. */
2560 if (TREE_CODE (expr_type) == REAL_TYPE
2561 && TREE_CODE (type) == INTEGER_TYPE)
2563 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2564 give_warning = true;
2566 /* Warn for an integer constant that does not fit into integer type. */
2567 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2568 && TREE_CODE (type) == INTEGER_TYPE
2569 && !int_fits_type_p (expr, type))
2571 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2572 && tree_int_cst_sgn (expr) < 0)
2574 if (produce_warns)
2575 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2576 " implicitly converted to unsigned type");
2578 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2580 if (produce_warns)
2581 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2582 " constant value to negative integer");
2584 else
2585 give_warning = true;
2587 else if (TREE_CODE (type) == REAL_TYPE)
2589 /* Warn for an integer constant that does not fit into real type. */
2590 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2592 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2593 if (!exact_real_truncate (TYPE_MODE (type), &a))
2594 give_warning = true;
2596 /* Warn for a real constant that does not fit into a smaller
2597 real type. */
2598 else if (TREE_CODE (expr_type) == REAL_TYPE
2599 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2601 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2602 if (!exact_real_truncate (TYPE_MODE (type), &a))
2603 give_warning = true;
2607 else
2609 /* Warn for real types converted to integer types. */
2610 if (TREE_CODE (expr_type) == REAL_TYPE
2611 && TREE_CODE (type) == INTEGER_TYPE)
2612 give_warning = true;
2614 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2615 && TREE_CODE (type) == INTEGER_TYPE)
2617 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2618 expr = get_unwidened (expr, 0);
2619 expr_type = TREE_TYPE (expr);
2621 /* Don't warn for short y; short x = ((int)y & 0xff); */
2622 if (TREE_CODE (expr) == BIT_AND_EXPR
2623 || TREE_CODE (expr) == BIT_IOR_EXPR
2624 || TREE_CODE (expr) == BIT_XOR_EXPR)
2626 /* If both args were extended from a shortest type,
2627 use that type if that is safe. */
2628 expr_type = shorten_binary_op (expr_type,
2629 TREE_OPERAND (expr, 0),
2630 TREE_OPERAND (expr, 1),
2631 /* bitwise */1);
2633 if (TREE_CODE (expr) == BIT_AND_EXPR)
2635 tree op0 = TREE_OPERAND (expr, 0);
2636 tree op1 = TREE_OPERAND (expr, 1);
2637 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2638 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2640 /* If one of the operands is a non-negative constant
2641 that fits in the target type, then the type of the
2642 other operand does not matter. */
2643 if ((TREE_CODE (op0) == INTEGER_CST
2644 && int_fits_type_p (op0, c_common_signed_type (type))
2645 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2646 || (TREE_CODE (op1) == INTEGER_CST
2647 && int_fits_type_p (op1, c_common_signed_type (type))
2648 && int_fits_type_p (op1,
2649 c_common_unsigned_type (type))))
2650 return false;
2651 /* If constant is unsigned and fits in the target
2652 type, then the result will also fit. */
2653 else if ((TREE_CODE (op0) == INTEGER_CST
2654 && unsigned0
2655 && int_fits_type_p (op0, type))
2656 || (TREE_CODE (op1) == INTEGER_CST
2657 && unsigned1
2658 && int_fits_type_p (op1, type)))
2659 return false;
2662 /* Warn for integer types converted to smaller integer types. */
2663 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2664 give_warning = true;
2666 /* When they are the same width but different signedness,
2667 then the value may change. */
2668 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2669 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2670 /* Even when converted to a bigger type, if the type is
2671 unsigned but expr is signed, then negative values
2672 will be changed. */
2673 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2674 && produce_warns)
2675 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2676 "may change the sign of the result",
2677 type, expr_type);
2680 /* Warn for integer types converted to real types if and only if
2681 all the range of values of the integer type cannot be
2682 represented by the real type. */
2683 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2684 && TREE_CODE (type) == REAL_TYPE)
2686 tree type_low_bound, type_high_bound;
2687 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2689 /* Don't warn about char y = 0xff; float x = (int) y; */
2690 expr = get_unwidened (expr, 0);
2691 expr_type = TREE_TYPE (expr);
2693 type_low_bound = TYPE_MIN_VALUE (expr_type);
2694 type_high_bound = TYPE_MAX_VALUE (expr_type);
2695 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2696 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2698 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2699 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2700 give_warning = true;
2703 /* Warn for real types converted to smaller real types. */
2704 else if (TREE_CODE (expr_type) == REAL_TYPE
2705 && TREE_CODE (type) == REAL_TYPE
2706 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2707 give_warning = true;
2710 return give_warning;
2713 /* Warns if the conversion of EXPR to TYPE may alter a value.
2714 This is a helper function for warnings_for_convert_and_check. */
2716 static void
2717 conversion_warning (tree type, tree expr)
2719 tree expr_type = TREE_TYPE (expr);
2720 location_t loc = EXPR_LOC_OR_HERE (expr);
2722 if (!warn_conversion && !warn_sign_conversion)
2723 return;
2725 switch (TREE_CODE (expr))
2727 case EQ_EXPR:
2728 case NE_EXPR:
2729 case LE_EXPR:
2730 case GE_EXPR:
2731 case LT_EXPR:
2732 case GT_EXPR:
2733 case TRUTH_ANDIF_EXPR:
2734 case TRUTH_ORIF_EXPR:
2735 case TRUTH_AND_EXPR:
2736 case TRUTH_OR_EXPR:
2737 case TRUTH_XOR_EXPR:
2738 case TRUTH_NOT_EXPR:
2739 /* Conversion from boolean to a signed:1 bit-field (which only
2740 can hold the values 0 and -1) doesn't lose information - but
2741 it does change the value. */
2742 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2743 warning_at (loc, OPT_Wconversion,
2744 "conversion to %qT from boolean expression", type);
2745 return;
2747 case REAL_CST:
2748 case INTEGER_CST:
2749 if (unsafe_conversion_p (type, expr, true))
2750 warning_at (loc, OPT_Wconversion,
2751 "conversion to %qT alters %qT constant value",
2752 type, expr_type);
2753 return;
2755 case COND_EXPR:
2757 /* In case of COND_EXPR, we do not care about the type of
2758 COND_EXPR, only about the conversion of each operand. */
2759 tree op1 = TREE_OPERAND (expr, 1);
2760 tree op2 = TREE_OPERAND (expr, 2);
2762 conversion_warning (type, op1);
2763 conversion_warning (type, op2);
2764 return;
2767 default: /* 'expr' is not a constant. */
2768 if (unsafe_conversion_p (type, expr, true))
2769 warning_at (loc, OPT_Wconversion,
2770 "conversion to %qT from %qT may alter its value",
2771 type, expr_type);
2775 /* Produce warnings after a conversion. RESULT is the result of
2776 converting EXPR to TYPE. This is a helper function for
2777 convert_and_check and cp_convert_and_check. */
2779 void
2780 warnings_for_convert_and_check (tree type, tree expr, tree result)
2782 location_t loc = EXPR_LOC_OR_HERE (expr);
2784 if (TREE_CODE (expr) == INTEGER_CST
2785 && (TREE_CODE (type) == INTEGER_TYPE
2786 || TREE_CODE (type) == ENUMERAL_TYPE)
2787 && !int_fits_type_p (expr, type))
2789 /* Do not diagnose overflow in a constant expression merely
2790 because a conversion overflowed. */
2791 if (TREE_OVERFLOW (result))
2792 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2794 if (TYPE_UNSIGNED (type))
2796 /* This detects cases like converting -129 or 256 to
2797 unsigned char. */
2798 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2799 warning_at (loc, OPT_Woverflow,
2800 "large integer implicitly truncated to unsigned type");
2801 else
2802 conversion_warning (type, expr);
2804 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2805 warning (OPT_Woverflow,
2806 "overflow in implicit constant conversion");
2807 /* No warning for converting 0x80000000 to int. */
2808 else if (pedantic
2809 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2810 || TYPE_PRECISION (TREE_TYPE (expr))
2811 != TYPE_PRECISION (type)))
2812 warning_at (loc, OPT_Woverflow,
2813 "overflow in implicit constant conversion");
2815 else
2816 conversion_warning (type, expr);
2818 else if ((TREE_CODE (result) == INTEGER_CST
2819 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2820 warning_at (loc, OPT_Woverflow,
2821 "overflow in implicit constant conversion");
2822 else
2823 conversion_warning (type, expr);
2827 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2828 Invoke this function on every expression that is converted implicitly,
2829 i.e. because of language rules and not because of an explicit cast. */
2831 tree
2832 convert_and_check (tree type, tree expr)
2834 tree result;
2835 tree expr_for_warning;
2837 /* Convert from a value with possible excess precision rather than
2838 via the semantic type, but do not warn about values not fitting
2839 exactly in the semantic type. */
2840 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2842 tree orig_type = TREE_TYPE (expr);
2843 expr = TREE_OPERAND (expr, 0);
2844 expr_for_warning = convert (orig_type, expr);
2845 if (orig_type == type)
2846 return expr_for_warning;
2848 else
2849 expr_for_warning = expr;
2851 if (TREE_TYPE (expr) == type)
2852 return expr;
2854 result = convert (type, expr);
2856 if (c_inhibit_evaluation_warnings == 0
2857 && !TREE_OVERFLOW_P (expr)
2858 && result != error_mark_node)
2859 warnings_for_convert_and_check (type, expr_for_warning, result);
2861 return result;
2864 /* A node in a list that describes references to variables (EXPR), which are
2865 either read accesses if WRITER is zero, or write accesses, in which case
2866 WRITER is the parent of EXPR. */
2867 struct tlist
2869 struct tlist *next;
2870 tree expr, writer;
2873 /* Used to implement a cache the results of a call to verify_tree. We only
2874 use this for SAVE_EXPRs. */
2875 struct tlist_cache
2877 struct tlist_cache *next;
2878 struct tlist *cache_before_sp;
2879 struct tlist *cache_after_sp;
2880 tree expr;
2883 /* Obstack to use when allocating tlist structures, and corresponding
2884 firstobj. */
2885 static struct obstack tlist_obstack;
2886 static char *tlist_firstobj = 0;
2888 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2889 warnings. */
2890 static struct tlist *warned_ids;
2891 /* SAVE_EXPRs need special treatment. We process them only once and then
2892 cache the results. */
2893 static struct tlist_cache *save_expr_cache;
2895 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2896 static void merge_tlist (struct tlist **, struct tlist *, int);
2897 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2898 static int warning_candidate_p (tree);
2899 static bool candidate_equal_p (const_tree, const_tree);
2900 static void warn_for_collisions (struct tlist *);
2901 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2902 static struct tlist *new_tlist (struct tlist *, tree, tree);
2904 /* Create a new struct tlist and fill in its fields. */
2905 static struct tlist *
2906 new_tlist (struct tlist *next, tree t, tree writer)
2908 struct tlist *l;
2909 l = XOBNEW (&tlist_obstack, struct tlist);
2910 l->next = next;
2911 l->expr = t;
2912 l->writer = writer;
2913 return l;
2916 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2917 is nonnull, we ignore any node we find which has a writer equal to it. */
2919 static void
2920 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2922 while (add)
2924 struct tlist *next = add->next;
2925 if (!copy)
2926 add->next = *to;
2927 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2928 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2929 add = next;
2933 /* Merge the nodes of ADD into TO. This merging process is done so that for
2934 each variable that already exists in TO, no new node is added; however if
2935 there is a write access recorded in ADD, and an occurrence on TO is only
2936 a read access, then the occurrence in TO will be modified to record the
2937 write. */
2939 static void
2940 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2942 struct tlist **end = to;
2944 while (*end)
2945 end = &(*end)->next;
2947 while (add)
2949 int found = 0;
2950 struct tlist *tmp2;
2951 struct tlist *next = add->next;
2953 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2954 if (candidate_equal_p (tmp2->expr, add->expr))
2956 found = 1;
2957 if (!tmp2->writer)
2958 tmp2->writer = add->writer;
2960 if (!found)
2962 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2963 end = &(*end)->next;
2964 *end = 0;
2966 add = next;
2970 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2971 references in list LIST conflict with it, excluding reads if ONLY writers
2972 is nonzero. */
2974 static void
2975 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2976 int only_writes)
2978 struct tlist *tmp;
2980 /* Avoid duplicate warnings. */
2981 for (tmp = warned_ids; tmp; tmp = tmp->next)
2982 if (candidate_equal_p (tmp->expr, written))
2983 return;
2985 while (list)
2987 if (candidate_equal_p (list->expr, written)
2988 && !candidate_equal_p (list->writer, writer)
2989 && (!only_writes || list->writer))
2991 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2992 warning_at (EXPR_LOC_OR_HERE (writer),
2993 OPT_Wsequence_point, "operation on %qE may be undefined",
2994 list->expr);
2996 list = list->next;
3000 /* Given a list LIST of references to variables, find whether any of these
3001 can cause conflicts due to missing sequence points. */
3003 static void
3004 warn_for_collisions (struct tlist *list)
3006 struct tlist *tmp;
3008 for (tmp = list; tmp; tmp = tmp->next)
3010 if (tmp->writer)
3011 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3015 /* Return nonzero if X is a tree that can be verified by the sequence point
3016 warnings. */
3017 static int
3018 warning_candidate_p (tree x)
3020 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3021 return 0;
3023 if (TREE_CODE (x) == BLOCK)
3024 return 0;
3026 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3027 (lvalue_p) crash on TRY/CATCH. */
3028 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3029 return 0;
3031 if (!lvalue_p (x))
3032 return 0;
3034 /* No point to track non-const calls, they will never satisfy
3035 operand_equal_p. */
3036 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3037 return 0;
3039 if (TREE_CODE (x) == STRING_CST)
3040 return 0;
3042 return 1;
3045 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3046 static bool
3047 candidate_equal_p (const_tree x, const_tree y)
3049 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3052 /* Walk the tree X, and record accesses to variables. If X is written by the
3053 parent tree, WRITER is the parent.
3054 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3055 expression or its only operand forces a sequence point, then everything up
3056 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3057 in PNO_SP.
3058 Once we return, we will have emitted warnings if any subexpression before
3059 such a sequence point could be undefined. On a higher level, however, the
3060 sequence point may not be relevant, and we'll merge the two lists.
3062 Example: (b++, a) + b;
3063 The call that processes the COMPOUND_EXPR will store the increment of B
3064 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3065 processes the PLUS_EXPR will need to merge the two lists so that
3066 eventually, all accesses end up on the same list (and we'll warn about the
3067 unordered subexpressions b++ and b.
3069 A note on merging. If we modify the former example so that our expression
3070 becomes
3071 (b++, b) + a
3072 care must be taken not simply to add all three expressions into the final
3073 PNO_SP list. The function merge_tlist takes care of that by merging the
3074 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3075 way, so that no more than one access to B is recorded. */
3077 static void
3078 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3079 tree writer)
3081 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3082 enum tree_code code;
3083 enum tree_code_class cl;
3085 /* X may be NULL if it is the operand of an empty statement expression
3086 ({ }). */
3087 if (x == NULL)
3088 return;
3090 restart:
3091 code = TREE_CODE (x);
3092 cl = TREE_CODE_CLASS (code);
3094 if (warning_candidate_p (x))
3095 *pno_sp = new_tlist (*pno_sp, x, writer);
3097 switch (code)
3099 case CONSTRUCTOR:
3100 case SIZEOF_EXPR:
3101 return;
3103 case COMPOUND_EXPR:
3104 case TRUTH_ANDIF_EXPR:
3105 case TRUTH_ORIF_EXPR:
3106 tmp_before = tmp_nosp = tmp_list3 = 0;
3107 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3108 warn_for_collisions (tmp_nosp);
3109 merge_tlist (pbefore_sp, tmp_before, 0);
3110 merge_tlist (pbefore_sp, tmp_nosp, 0);
3111 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3112 merge_tlist (pbefore_sp, tmp_list3, 0);
3113 return;
3115 case COND_EXPR:
3116 tmp_before = tmp_list2 = 0;
3117 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3118 warn_for_collisions (tmp_list2);
3119 merge_tlist (pbefore_sp, tmp_before, 0);
3120 merge_tlist (pbefore_sp, tmp_list2, 1);
3122 tmp_list3 = tmp_nosp = 0;
3123 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3124 warn_for_collisions (tmp_nosp);
3125 merge_tlist (pbefore_sp, tmp_list3, 0);
3127 tmp_list3 = tmp_list2 = 0;
3128 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3129 warn_for_collisions (tmp_list2);
3130 merge_tlist (pbefore_sp, tmp_list3, 0);
3131 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3132 two first, to avoid warning for (a ? b++ : b++). */
3133 merge_tlist (&tmp_nosp, tmp_list2, 0);
3134 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3135 return;
3137 case PREDECREMENT_EXPR:
3138 case PREINCREMENT_EXPR:
3139 case POSTDECREMENT_EXPR:
3140 case POSTINCREMENT_EXPR:
3141 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3142 return;
3144 case MODIFY_EXPR:
3145 tmp_before = tmp_nosp = tmp_list3 = 0;
3146 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3147 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3148 /* Expressions inside the LHS are not ordered wrt. the sequence points
3149 in the RHS. Example:
3150 *a = (a++, 2)
3151 Despite the fact that the modification of "a" is in the before_sp
3152 list (tmp_before), it conflicts with the use of "a" in the LHS.
3153 We can handle this by adding the contents of tmp_list3
3154 to those of tmp_before, and redoing the collision warnings for that
3155 list. */
3156 add_tlist (&tmp_before, tmp_list3, x, 1);
3157 warn_for_collisions (tmp_before);
3158 /* Exclude the LHS itself here; we first have to merge it into the
3159 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3160 didn't exclude the LHS, we'd get it twice, once as a read and once
3161 as a write. */
3162 add_tlist (pno_sp, tmp_list3, x, 0);
3163 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3165 merge_tlist (pbefore_sp, tmp_before, 0);
3166 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3167 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3168 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3169 return;
3171 case CALL_EXPR:
3172 /* We need to warn about conflicts among arguments and conflicts between
3173 args and the function address. Side effects of the function address,
3174 however, are not ordered by the sequence point of the call. */
3176 call_expr_arg_iterator iter;
3177 tree arg;
3178 tmp_before = tmp_nosp = 0;
3179 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3180 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3182 tmp_list2 = tmp_list3 = 0;
3183 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3184 merge_tlist (&tmp_list3, tmp_list2, 0);
3185 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3187 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3188 warn_for_collisions (tmp_before);
3189 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3190 return;
3193 case TREE_LIST:
3194 /* Scan all the list, e.g. indices of multi dimensional array. */
3195 while (x)
3197 tmp_before = tmp_nosp = 0;
3198 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3199 merge_tlist (&tmp_nosp, tmp_before, 0);
3200 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3201 x = TREE_CHAIN (x);
3203 return;
3205 case SAVE_EXPR:
3207 struct tlist_cache *t;
3208 for (t = save_expr_cache; t; t = t->next)
3209 if (candidate_equal_p (t->expr, x))
3210 break;
3212 if (!t)
3214 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3215 t->next = save_expr_cache;
3216 t->expr = x;
3217 save_expr_cache = t;
3219 tmp_before = tmp_nosp = 0;
3220 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3221 warn_for_collisions (tmp_nosp);
3223 tmp_list3 = 0;
3224 while (tmp_nosp)
3226 struct tlist *t = tmp_nosp;
3227 tmp_nosp = t->next;
3228 merge_tlist (&tmp_list3, t, 0);
3230 t->cache_before_sp = tmp_before;
3231 t->cache_after_sp = tmp_list3;
3233 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3234 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3235 return;
3238 case ADDR_EXPR:
3239 x = TREE_OPERAND (x, 0);
3240 if (DECL_P (x))
3241 return;
3242 writer = 0;
3243 goto restart;
3245 default:
3246 /* For other expressions, simply recurse on their operands.
3247 Manual tail recursion for unary expressions.
3248 Other non-expressions need not be processed. */
3249 if (cl == tcc_unary)
3251 x = TREE_OPERAND (x, 0);
3252 writer = 0;
3253 goto restart;
3255 else if (IS_EXPR_CODE_CLASS (cl))
3257 int lp;
3258 int max = TREE_OPERAND_LENGTH (x);
3259 for (lp = 0; lp < max; lp++)
3261 tmp_before = tmp_nosp = 0;
3262 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3263 merge_tlist (&tmp_nosp, tmp_before, 0);
3264 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3267 return;
3271 /* Try to warn for undefined behavior in EXPR due to missing sequence
3272 points. */
3274 DEBUG_FUNCTION void
3275 verify_sequence_points (tree expr)
3277 struct tlist *before_sp = 0, *after_sp = 0;
3279 warned_ids = 0;
3280 save_expr_cache = 0;
3281 if (tlist_firstobj == 0)
3283 gcc_obstack_init (&tlist_obstack);
3284 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3287 verify_tree (expr, &before_sp, &after_sp, 0);
3288 warn_for_collisions (after_sp);
3289 obstack_free (&tlist_obstack, tlist_firstobj);
3292 /* Validate the expression after `case' and apply default promotions. */
3294 static tree
3295 check_case_value (tree value)
3297 if (value == NULL_TREE)
3298 return value;
3300 if (TREE_CODE (value) == INTEGER_CST)
3301 /* Promote char or short to int. */
3302 value = perform_integral_promotions (value);
3303 else if (value != error_mark_node)
3305 error ("case label does not reduce to an integer constant");
3306 value = error_mark_node;
3309 constant_expression_warning (value);
3311 return value;
3314 /* See if the case values LOW and HIGH are in the range of the original
3315 type (i.e. before the default conversion to int) of the switch testing
3316 expression.
3317 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3318 the type before promoting it. CASE_LOW_P is a pointer to the lower
3319 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3320 if the case is not a case range.
3321 The caller has to make sure that we are not called with NULL for
3322 CASE_LOW_P (i.e. the default case).
3323 Returns true if the case label is in range of ORIG_TYPE (saturated or
3324 untouched) or false if the label is out of range. */
3326 static bool
3327 check_case_bounds (tree type, tree orig_type,
3328 tree *case_low_p, tree *case_high_p)
3330 tree min_value, max_value;
3331 tree case_low = *case_low_p;
3332 tree case_high = case_high_p ? *case_high_p : case_low;
3334 /* If there was a problem with the original type, do nothing. */
3335 if (orig_type == error_mark_node)
3336 return true;
3338 min_value = TYPE_MIN_VALUE (orig_type);
3339 max_value = TYPE_MAX_VALUE (orig_type);
3341 /* Case label is less than minimum for type. */
3342 if (tree_int_cst_compare (case_low, min_value) < 0
3343 && tree_int_cst_compare (case_high, min_value) < 0)
3345 warning (0, "case label value is less than minimum value for type");
3346 return false;
3349 /* Case value is greater than maximum for type. */
3350 if (tree_int_cst_compare (case_low, max_value) > 0
3351 && tree_int_cst_compare (case_high, max_value) > 0)
3353 warning (0, "case label value exceeds maximum value for type");
3354 return false;
3357 /* Saturate lower case label value to minimum. */
3358 if (tree_int_cst_compare (case_high, min_value) >= 0
3359 && tree_int_cst_compare (case_low, min_value) < 0)
3361 warning (0, "lower value in case label range"
3362 " less than minimum value for type");
3363 case_low = min_value;
3366 /* Saturate upper case label value to maximum. */
3367 if (tree_int_cst_compare (case_low, max_value) <= 0
3368 && tree_int_cst_compare (case_high, max_value) > 0)
3370 warning (0, "upper value in case label range"
3371 " exceeds maximum value for type");
3372 case_high = max_value;
3375 if (*case_low_p != case_low)
3376 *case_low_p = convert (type, case_low);
3377 if (case_high_p && *case_high_p != case_high)
3378 *case_high_p = convert (type, case_high);
3380 return true;
3383 /* Return an integer type with BITS bits of precision,
3384 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3386 tree
3387 c_common_type_for_size (unsigned int bits, int unsignedp)
3389 if (bits == TYPE_PRECISION (integer_type_node))
3390 return unsignedp ? unsigned_type_node : integer_type_node;
3392 if (bits == TYPE_PRECISION (signed_char_type_node))
3393 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3395 if (bits == TYPE_PRECISION (short_integer_type_node))
3396 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3398 if (bits == TYPE_PRECISION (long_integer_type_node))
3399 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3401 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3402 return (unsignedp ? long_long_unsigned_type_node
3403 : long_long_integer_type_node);
3405 if (int128_integer_type_node
3406 && bits == TYPE_PRECISION (int128_integer_type_node))
3407 return (unsignedp ? int128_unsigned_type_node
3408 : int128_integer_type_node);
3410 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3411 return (unsignedp ? widest_unsigned_literal_type_node
3412 : widest_integer_literal_type_node);
3414 if (bits <= TYPE_PRECISION (intQI_type_node))
3415 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3417 if (bits <= TYPE_PRECISION (intHI_type_node))
3418 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3420 if (bits <= TYPE_PRECISION (intSI_type_node))
3421 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3423 if (bits <= TYPE_PRECISION (intDI_type_node))
3424 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3426 return 0;
3429 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3430 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3431 and saturating if SATP is nonzero, otherwise not saturating. */
3433 tree
3434 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3435 int unsignedp, int satp)
3437 enum machine_mode mode;
3438 if (ibit == 0)
3439 mode = unsignedp ? UQQmode : QQmode;
3440 else
3441 mode = unsignedp ? UHAmode : HAmode;
3443 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3444 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3445 break;
3447 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3449 sorry ("GCC cannot support operators with integer types and "
3450 "fixed-point types that have too many integral and "
3451 "fractional bits together");
3452 return 0;
3455 return c_common_type_for_mode (mode, satp);
3458 /* Used for communication between c_common_type_for_mode and
3459 c_register_builtin_type. */
3460 static GTY(()) tree registered_builtin_types;
3462 /* Return a data type that has machine mode MODE.
3463 If the mode is an integer,
3464 then UNSIGNEDP selects between signed and unsigned types.
3465 If the mode is a fixed-point mode,
3466 then UNSIGNEDP selects between saturating and nonsaturating types. */
3468 tree
3469 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3471 tree t;
3473 if (mode == TYPE_MODE (integer_type_node))
3474 return unsignedp ? unsigned_type_node : integer_type_node;
3476 if (mode == TYPE_MODE (signed_char_type_node))
3477 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3479 if (mode == TYPE_MODE (short_integer_type_node))
3480 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3482 if (mode == TYPE_MODE (long_integer_type_node))
3483 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3485 if (mode == TYPE_MODE (long_long_integer_type_node))
3486 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3488 if (int128_integer_type_node
3489 && mode == TYPE_MODE (int128_integer_type_node))
3490 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3492 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3493 return unsignedp ? widest_unsigned_literal_type_node
3494 : widest_integer_literal_type_node;
3496 if (mode == QImode)
3497 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3499 if (mode == HImode)
3500 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3502 if (mode == SImode)
3503 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3505 if (mode == DImode)
3506 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3508 #if HOST_BITS_PER_WIDE_INT >= 64
3509 if (mode == TYPE_MODE (intTI_type_node))
3510 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3511 #endif
3513 if (mode == TYPE_MODE (float_type_node))
3514 return float_type_node;
3516 if (mode == TYPE_MODE (double_type_node))
3517 return double_type_node;
3519 if (mode == TYPE_MODE (long_double_type_node))
3520 return long_double_type_node;
3522 if (mode == TYPE_MODE (void_type_node))
3523 return void_type_node;
3525 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3526 return (unsignedp
3527 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3528 : make_signed_type (GET_MODE_PRECISION (mode)));
3530 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3531 return (unsignedp
3532 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3533 : make_signed_type (GET_MODE_PRECISION (mode)));
3535 if (COMPLEX_MODE_P (mode))
3537 enum machine_mode inner_mode;
3538 tree inner_type;
3540 if (mode == TYPE_MODE (complex_float_type_node))
3541 return complex_float_type_node;
3542 if (mode == TYPE_MODE (complex_double_type_node))
3543 return complex_double_type_node;
3544 if (mode == TYPE_MODE (complex_long_double_type_node))
3545 return complex_long_double_type_node;
3547 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3548 return complex_integer_type_node;
3550 inner_mode = GET_MODE_INNER (mode);
3551 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3552 if (inner_type != NULL_TREE)
3553 return build_complex_type (inner_type);
3555 else if (VECTOR_MODE_P (mode))
3557 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3558 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3559 if (inner_type != NULL_TREE)
3560 return build_vector_type_for_mode (inner_type, mode);
3563 if (mode == TYPE_MODE (dfloat32_type_node))
3564 return dfloat32_type_node;
3565 if (mode == TYPE_MODE (dfloat64_type_node))
3566 return dfloat64_type_node;
3567 if (mode == TYPE_MODE (dfloat128_type_node))
3568 return dfloat128_type_node;
3570 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3572 if (mode == TYPE_MODE (short_fract_type_node))
3573 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3574 if (mode == TYPE_MODE (fract_type_node))
3575 return unsignedp ? sat_fract_type_node : fract_type_node;
3576 if (mode == TYPE_MODE (long_fract_type_node))
3577 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3578 if (mode == TYPE_MODE (long_long_fract_type_node))
3579 return unsignedp ? sat_long_long_fract_type_node
3580 : long_long_fract_type_node;
3582 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3583 return unsignedp ? sat_unsigned_short_fract_type_node
3584 : unsigned_short_fract_type_node;
3585 if (mode == TYPE_MODE (unsigned_fract_type_node))
3586 return unsignedp ? sat_unsigned_fract_type_node
3587 : unsigned_fract_type_node;
3588 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3589 return unsignedp ? sat_unsigned_long_fract_type_node
3590 : unsigned_long_fract_type_node;
3591 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3592 return unsignedp ? sat_unsigned_long_long_fract_type_node
3593 : unsigned_long_long_fract_type_node;
3595 if (mode == TYPE_MODE (short_accum_type_node))
3596 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3597 if (mode == TYPE_MODE (accum_type_node))
3598 return unsignedp ? sat_accum_type_node : accum_type_node;
3599 if (mode == TYPE_MODE (long_accum_type_node))
3600 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3601 if (mode == TYPE_MODE (long_long_accum_type_node))
3602 return unsignedp ? sat_long_long_accum_type_node
3603 : long_long_accum_type_node;
3605 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3606 return unsignedp ? sat_unsigned_short_accum_type_node
3607 : unsigned_short_accum_type_node;
3608 if (mode == TYPE_MODE (unsigned_accum_type_node))
3609 return unsignedp ? sat_unsigned_accum_type_node
3610 : unsigned_accum_type_node;
3611 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3612 return unsignedp ? sat_unsigned_long_accum_type_node
3613 : unsigned_long_accum_type_node;
3614 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3615 return unsignedp ? sat_unsigned_long_long_accum_type_node
3616 : unsigned_long_long_accum_type_node;
3618 if (mode == QQmode)
3619 return unsignedp ? sat_qq_type_node : qq_type_node;
3620 if (mode == HQmode)
3621 return unsignedp ? sat_hq_type_node : hq_type_node;
3622 if (mode == SQmode)
3623 return unsignedp ? sat_sq_type_node : sq_type_node;
3624 if (mode == DQmode)
3625 return unsignedp ? sat_dq_type_node : dq_type_node;
3626 if (mode == TQmode)
3627 return unsignedp ? sat_tq_type_node : tq_type_node;
3629 if (mode == UQQmode)
3630 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3631 if (mode == UHQmode)
3632 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3633 if (mode == USQmode)
3634 return unsignedp ? sat_usq_type_node : usq_type_node;
3635 if (mode == UDQmode)
3636 return unsignedp ? sat_udq_type_node : udq_type_node;
3637 if (mode == UTQmode)
3638 return unsignedp ? sat_utq_type_node : utq_type_node;
3640 if (mode == HAmode)
3641 return unsignedp ? sat_ha_type_node : ha_type_node;
3642 if (mode == SAmode)
3643 return unsignedp ? sat_sa_type_node : sa_type_node;
3644 if (mode == DAmode)
3645 return unsignedp ? sat_da_type_node : da_type_node;
3646 if (mode == TAmode)
3647 return unsignedp ? sat_ta_type_node : ta_type_node;
3649 if (mode == UHAmode)
3650 return unsignedp ? sat_uha_type_node : uha_type_node;
3651 if (mode == USAmode)
3652 return unsignedp ? sat_usa_type_node : usa_type_node;
3653 if (mode == UDAmode)
3654 return unsignedp ? sat_uda_type_node : uda_type_node;
3655 if (mode == UTAmode)
3656 return unsignedp ? sat_uta_type_node : uta_type_node;
3659 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3660 if (TYPE_MODE (TREE_VALUE (t)) == mode
3661 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3662 return TREE_VALUE (t);
3664 return 0;
3667 tree
3668 c_common_unsigned_type (tree type)
3670 return c_common_signed_or_unsigned_type (1, type);
3673 /* Return a signed type the same as TYPE in other respects. */
3675 tree
3676 c_common_signed_type (tree type)
3678 return c_common_signed_or_unsigned_type (0, type);
3681 /* Return a type the same as TYPE except unsigned or
3682 signed according to UNSIGNEDP. */
3684 tree
3685 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3687 tree type1;
3689 /* This block of code emulates the behavior of the old
3690 c_common_unsigned_type. In particular, it returns
3691 long_unsigned_type_node if passed a long, even when a int would
3692 have the same size. This is necessary for warnings to work
3693 correctly in archs where sizeof(int) == sizeof(long) */
3695 type1 = TYPE_MAIN_VARIANT (type);
3696 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3697 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3698 if (type1 == integer_type_node || type1 == unsigned_type_node)
3699 return unsignedp ? unsigned_type_node : integer_type_node;
3700 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3701 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3702 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3703 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3704 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3705 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3706 if (int128_integer_type_node
3707 && (type1 == int128_integer_type_node
3708 || type1 == int128_unsigned_type_node))
3709 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3710 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3711 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3712 #if HOST_BITS_PER_WIDE_INT >= 64
3713 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3714 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3715 #endif
3716 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3717 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3718 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3719 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3720 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3721 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3722 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3723 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3725 #define C_COMMON_FIXED_TYPES(NAME) \
3726 if (type1 == short_ ## NAME ## _type_node \
3727 || type1 == unsigned_short_ ## NAME ## _type_node) \
3728 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3729 : short_ ## NAME ## _type_node; \
3730 if (type1 == NAME ## _type_node \
3731 || type1 == unsigned_ ## NAME ## _type_node) \
3732 return unsignedp ? unsigned_ ## NAME ## _type_node \
3733 : NAME ## _type_node; \
3734 if (type1 == long_ ## NAME ## _type_node \
3735 || type1 == unsigned_long_ ## NAME ## _type_node) \
3736 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3737 : long_ ## NAME ## _type_node; \
3738 if (type1 == long_long_ ## NAME ## _type_node \
3739 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3740 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3741 : long_long_ ## NAME ## _type_node;
3743 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3744 if (type1 == NAME ## _type_node \
3745 || type1 == u ## NAME ## _type_node) \
3746 return unsignedp ? u ## NAME ## _type_node \
3747 : NAME ## _type_node;
3749 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3750 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3751 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3752 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3753 : sat_ ## short_ ## NAME ## _type_node; \
3754 if (type1 == sat_ ## NAME ## _type_node \
3755 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3756 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3757 : sat_ ## NAME ## _type_node; \
3758 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3759 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3760 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3761 : sat_ ## long_ ## NAME ## _type_node; \
3762 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3763 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3764 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3765 : sat_ ## long_long_ ## NAME ## _type_node;
3767 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3768 if (type1 == sat_ ## NAME ## _type_node \
3769 || type1 == sat_ ## u ## NAME ## _type_node) \
3770 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3771 : sat_ ## NAME ## _type_node;
3773 C_COMMON_FIXED_TYPES (fract);
3774 C_COMMON_FIXED_TYPES_SAT (fract);
3775 C_COMMON_FIXED_TYPES (accum);
3776 C_COMMON_FIXED_TYPES_SAT (accum);
3778 C_COMMON_FIXED_MODE_TYPES (qq);
3779 C_COMMON_FIXED_MODE_TYPES (hq);
3780 C_COMMON_FIXED_MODE_TYPES (sq);
3781 C_COMMON_FIXED_MODE_TYPES (dq);
3782 C_COMMON_FIXED_MODE_TYPES (tq);
3783 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3784 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3785 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3786 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3787 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3788 C_COMMON_FIXED_MODE_TYPES (ha);
3789 C_COMMON_FIXED_MODE_TYPES (sa);
3790 C_COMMON_FIXED_MODE_TYPES (da);
3791 C_COMMON_FIXED_MODE_TYPES (ta);
3792 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3793 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3794 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3795 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3797 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3798 the precision; they have precision set to match their range, but
3799 may use a wider mode to match an ABI. If we change modes, we may
3800 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3801 the precision as well, so as to yield correct results for
3802 bit-field types. C++ does not have these separate bit-field
3803 types, and producing a signed or unsigned variant of an
3804 ENUMERAL_TYPE may cause other problems as well. */
3806 if (!INTEGRAL_TYPE_P (type)
3807 || TYPE_UNSIGNED (type) == unsignedp)
3808 return type;
3810 #define TYPE_OK(node) \
3811 (TYPE_MODE (type) == TYPE_MODE (node) \
3812 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3813 if (TYPE_OK (signed_char_type_node))
3814 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3815 if (TYPE_OK (integer_type_node))
3816 return unsignedp ? unsigned_type_node : integer_type_node;
3817 if (TYPE_OK (short_integer_type_node))
3818 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3819 if (TYPE_OK (long_integer_type_node))
3820 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3821 if (TYPE_OK (long_long_integer_type_node))
3822 return (unsignedp ? long_long_unsigned_type_node
3823 : long_long_integer_type_node);
3824 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3825 return (unsignedp ? int128_unsigned_type_node
3826 : int128_integer_type_node);
3827 if (TYPE_OK (widest_integer_literal_type_node))
3828 return (unsignedp ? widest_unsigned_literal_type_node
3829 : widest_integer_literal_type_node);
3831 #if HOST_BITS_PER_WIDE_INT >= 64
3832 if (TYPE_OK (intTI_type_node))
3833 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3834 #endif
3835 if (TYPE_OK (intDI_type_node))
3836 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3837 if (TYPE_OK (intSI_type_node))
3838 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3839 if (TYPE_OK (intHI_type_node))
3840 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3841 if (TYPE_OK (intQI_type_node))
3842 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3843 #undef TYPE_OK
3845 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3848 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3850 tree
3851 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3853 /* Extended integer types of the same width as a standard type have
3854 lesser rank, so those of the same width as int promote to int or
3855 unsigned int and are valid for printf formats expecting int or
3856 unsigned int. To avoid such special cases, avoid creating
3857 extended integer types for bit-fields if a standard integer type
3858 is available. */
3859 if (width == TYPE_PRECISION (integer_type_node))
3860 return unsignedp ? unsigned_type_node : integer_type_node;
3861 if (width == TYPE_PRECISION (signed_char_type_node))
3862 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3863 if (width == TYPE_PRECISION (short_integer_type_node))
3864 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3865 if (width == TYPE_PRECISION (long_integer_type_node))
3866 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3867 if (width == TYPE_PRECISION (long_long_integer_type_node))
3868 return (unsignedp ? long_long_unsigned_type_node
3869 : long_long_integer_type_node);
3870 if (int128_integer_type_node
3871 && width == TYPE_PRECISION (int128_integer_type_node))
3872 return (unsignedp ? int128_unsigned_type_node
3873 : int128_integer_type_node);
3874 return build_nonstandard_integer_type (width, unsignedp);
3877 /* The C version of the register_builtin_type langhook. */
3879 void
3880 c_register_builtin_type (tree type, const char* name)
3882 tree decl;
3884 decl = build_decl (UNKNOWN_LOCATION,
3885 TYPE_DECL, get_identifier (name), type);
3886 DECL_ARTIFICIAL (decl) = 1;
3887 if (!TYPE_NAME (type))
3888 TYPE_NAME (type) = decl;
3889 pushdecl (decl);
3891 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3894 /* Print an error message for invalid operands to arith operation
3895 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3896 LOCATION is the location of the message. */
3898 void
3899 binary_op_error (location_t location, enum tree_code code,
3900 tree type0, tree type1)
3902 const char *opname;
3904 switch (code)
3906 case PLUS_EXPR:
3907 opname = "+"; break;
3908 case MINUS_EXPR:
3909 opname = "-"; break;
3910 case MULT_EXPR:
3911 opname = "*"; break;
3912 case MAX_EXPR:
3913 opname = "max"; break;
3914 case MIN_EXPR:
3915 opname = "min"; break;
3916 case EQ_EXPR:
3917 opname = "=="; break;
3918 case NE_EXPR:
3919 opname = "!="; break;
3920 case LE_EXPR:
3921 opname = "<="; break;
3922 case GE_EXPR:
3923 opname = ">="; break;
3924 case LT_EXPR:
3925 opname = "<"; break;
3926 case GT_EXPR:
3927 opname = ">"; break;
3928 case LSHIFT_EXPR:
3929 opname = "<<"; break;
3930 case RSHIFT_EXPR:
3931 opname = ">>"; break;
3932 case TRUNC_MOD_EXPR:
3933 case FLOOR_MOD_EXPR:
3934 opname = "%"; break;
3935 case TRUNC_DIV_EXPR:
3936 case FLOOR_DIV_EXPR:
3937 opname = "/"; break;
3938 case BIT_AND_EXPR:
3939 opname = "&"; break;
3940 case BIT_IOR_EXPR:
3941 opname = "|"; break;
3942 case TRUTH_ANDIF_EXPR:
3943 opname = "&&"; break;
3944 case TRUTH_ORIF_EXPR:
3945 opname = "||"; break;
3946 case BIT_XOR_EXPR:
3947 opname = "^"; break;
3948 default:
3949 gcc_unreachable ();
3951 error_at (location,
3952 "invalid operands to binary %s (have %qT and %qT)", opname,
3953 type0, type1);
3956 /* Given an expression as a tree, return its original type. Do this
3957 by stripping any conversion that preserves the sign and precision. */
3958 static tree
3959 expr_original_type (tree expr)
3961 STRIP_SIGN_NOPS (expr);
3962 return TREE_TYPE (expr);
3965 /* Subroutine of build_binary_op, used for comparison operations.
3966 See if the operands have both been converted from subword integer types
3967 and, if so, perhaps change them both back to their original type.
3968 This function is also responsible for converting the two operands
3969 to the proper common type for comparison.
3971 The arguments of this function are all pointers to local variables
3972 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3973 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3975 If this function returns nonzero, it means that the comparison has
3976 a constant value. What this function returns is an expression for
3977 that value. */
3979 tree
3980 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3981 enum tree_code *rescode_ptr)
3983 tree type;
3984 tree op0 = *op0_ptr;
3985 tree op1 = *op1_ptr;
3986 int unsignedp0, unsignedp1;
3987 int real1, real2;
3988 tree primop0, primop1;
3989 enum tree_code code = *rescode_ptr;
3990 location_t loc = EXPR_LOC_OR_HERE (op0);
3992 /* Throw away any conversions to wider types
3993 already present in the operands. */
3995 primop0 = c_common_get_narrower (op0, &unsignedp0);
3996 primop1 = c_common_get_narrower (op1, &unsignedp1);
3998 /* If primopN is first sign-extended from primopN's precision to opN's
3999 precision, then zero-extended from opN's precision to
4000 *restype_ptr precision, shortenings might be invalid. */
4001 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4002 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4003 && !unsignedp0
4004 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4005 primop0 = op0;
4006 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4007 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4008 && !unsignedp1
4009 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4010 primop1 = op1;
4012 /* Handle the case that OP0 does not *contain* a conversion
4013 but it *requires* conversion to FINAL_TYPE. */
4015 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4016 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4017 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4018 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4020 /* If one of the operands must be floated, we cannot optimize. */
4021 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4022 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4024 /* If first arg is constant, swap the args (changing operation
4025 so value is preserved), for canonicalization. Don't do this if
4026 the second arg is 0. */
4028 if (TREE_CONSTANT (primop0)
4029 && !integer_zerop (primop1) && !real_zerop (primop1)
4030 && !fixed_zerop (primop1))
4032 tree tem = primop0;
4033 int temi = unsignedp0;
4034 primop0 = primop1;
4035 primop1 = tem;
4036 tem = op0;
4037 op0 = op1;
4038 op1 = tem;
4039 *op0_ptr = op0;
4040 *op1_ptr = op1;
4041 unsignedp0 = unsignedp1;
4042 unsignedp1 = temi;
4043 temi = real1;
4044 real1 = real2;
4045 real2 = temi;
4047 switch (code)
4049 case LT_EXPR:
4050 code = GT_EXPR;
4051 break;
4052 case GT_EXPR:
4053 code = LT_EXPR;
4054 break;
4055 case LE_EXPR:
4056 code = GE_EXPR;
4057 break;
4058 case GE_EXPR:
4059 code = LE_EXPR;
4060 break;
4061 default:
4062 break;
4064 *rescode_ptr = code;
4067 /* If comparing an integer against a constant more bits wide,
4068 maybe we can deduce a value of 1 or 0 independent of the data.
4069 Or else truncate the constant now
4070 rather than extend the variable at run time.
4072 This is only interesting if the constant is the wider arg.
4073 Also, it is not safe if the constant is unsigned and the
4074 variable arg is signed, since in this case the variable
4075 would be sign-extended and then regarded as unsigned.
4076 Our technique fails in this case because the lowest/highest
4077 possible unsigned results don't follow naturally from the
4078 lowest/highest possible values of the variable operand.
4079 For just EQ_EXPR and NE_EXPR there is another technique that
4080 could be used: see if the constant can be faithfully represented
4081 in the other operand's type, by truncating it and reextending it
4082 and see if that preserves the constant's value. */
4084 if (!real1 && !real2
4085 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4086 && TREE_CODE (primop1) == INTEGER_CST
4087 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4089 int min_gt, max_gt, min_lt, max_lt;
4090 tree maxval, minval;
4091 /* 1 if comparison is nominally unsigned. */
4092 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4093 tree val;
4095 type = c_common_signed_or_unsigned_type (unsignedp0,
4096 TREE_TYPE (primop0));
4098 maxval = TYPE_MAX_VALUE (type);
4099 minval = TYPE_MIN_VALUE (type);
4101 if (unsignedp && !unsignedp0)
4102 *restype_ptr = c_common_signed_type (*restype_ptr);
4104 if (TREE_TYPE (primop1) != *restype_ptr)
4106 /* Convert primop1 to target type, but do not introduce
4107 additional overflow. We know primop1 is an int_cst. */
4108 primop1 = force_fit_type_double (*restype_ptr,
4109 tree_to_double_int (primop1),
4110 0, TREE_OVERFLOW (primop1));
4112 if (type != *restype_ptr)
4114 minval = convert (*restype_ptr, minval);
4115 maxval = convert (*restype_ptr, maxval);
4118 if (unsignedp && unsignedp0)
4120 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4121 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4122 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4123 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4125 else
4127 min_gt = INT_CST_LT (primop1, minval);
4128 max_gt = INT_CST_LT (primop1, maxval);
4129 min_lt = INT_CST_LT (minval, primop1);
4130 max_lt = INT_CST_LT (maxval, primop1);
4133 val = 0;
4134 /* This used to be a switch, but Genix compiler can't handle that. */
4135 if (code == NE_EXPR)
4137 if (max_lt || min_gt)
4138 val = truthvalue_true_node;
4140 else if (code == EQ_EXPR)
4142 if (max_lt || min_gt)
4143 val = truthvalue_false_node;
4145 else if (code == LT_EXPR)
4147 if (max_lt)
4148 val = truthvalue_true_node;
4149 if (!min_lt)
4150 val = truthvalue_false_node;
4152 else if (code == GT_EXPR)
4154 if (min_gt)
4155 val = truthvalue_true_node;
4156 if (!max_gt)
4157 val = truthvalue_false_node;
4159 else if (code == LE_EXPR)
4161 if (!max_gt)
4162 val = truthvalue_true_node;
4163 if (min_gt)
4164 val = truthvalue_false_node;
4166 else if (code == GE_EXPR)
4168 if (!min_lt)
4169 val = truthvalue_true_node;
4170 if (max_lt)
4171 val = truthvalue_false_node;
4174 /* If primop0 was sign-extended and unsigned comparison specd,
4175 we did a signed comparison above using the signed type bounds.
4176 But the comparison we output must be unsigned.
4178 Also, for inequalities, VAL is no good; but if the signed
4179 comparison had *any* fixed result, it follows that the
4180 unsigned comparison just tests the sign in reverse
4181 (positive values are LE, negative ones GE).
4182 So we can generate an unsigned comparison
4183 against an extreme value of the signed type. */
4185 if (unsignedp && !unsignedp0)
4187 if (val != 0)
4188 switch (code)
4190 case LT_EXPR:
4191 case GE_EXPR:
4192 primop1 = TYPE_MIN_VALUE (type);
4193 val = 0;
4194 break;
4196 case LE_EXPR:
4197 case GT_EXPR:
4198 primop1 = TYPE_MAX_VALUE (type);
4199 val = 0;
4200 break;
4202 default:
4203 break;
4205 type = c_common_unsigned_type (type);
4208 if (TREE_CODE (primop0) != INTEGER_CST
4209 && c_inhibit_evaluation_warnings == 0)
4211 if (val == truthvalue_false_node)
4212 warning_at (loc, OPT_Wtype_limits,
4213 "comparison is always false due to limited range of data type");
4214 if (val == truthvalue_true_node)
4215 warning_at (loc, OPT_Wtype_limits,
4216 "comparison is always true due to limited range of data type");
4219 if (val != 0)
4221 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4222 if (TREE_SIDE_EFFECTS (primop0))
4223 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4224 return val;
4227 /* Value is not predetermined, but do the comparison
4228 in the type of the operand that is not constant.
4229 TYPE is already properly set. */
4232 /* If either arg is decimal float and the other is float, find the
4233 proper common type to use for comparison. */
4234 else if (real1 && real2
4235 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4236 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4237 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4239 else if (real1 && real2
4240 && (TYPE_PRECISION (TREE_TYPE (primop0))
4241 == TYPE_PRECISION (TREE_TYPE (primop1))))
4242 type = TREE_TYPE (primop0);
4244 /* If args' natural types are both narrower than nominal type
4245 and both extend in the same manner, compare them
4246 in the type of the wider arg.
4247 Otherwise must actually extend both to the nominal
4248 common type lest different ways of extending
4249 alter the result.
4250 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4252 else if (unsignedp0 == unsignedp1 && real1 == real2
4253 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4254 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4256 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4257 type = c_common_signed_or_unsigned_type (unsignedp0
4258 || TYPE_UNSIGNED (*restype_ptr),
4259 type);
4260 /* Make sure shorter operand is extended the right way
4261 to match the longer operand. */
4262 primop0
4263 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4264 TREE_TYPE (primop0)),
4265 primop0);
4266 primop1
4267 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4268 TREE_TYPE (primop1)),
4269 primop1);
4271 else
4273 /* Here we must do the comparison on the nominal type
4274 using the args exactly as we received them. */
4275 type = *restype_ptr;
4276 primop0 = op0;
4277 primop1 = op1;
4279 if (!real1 && !real2 && integer_zerop (primop1)
4280 && TYPE_UNSIGNED (*restype_ptr))
4282 tree value = 0;
4283 /* All unsigned values are >= 0, so we warn. However,
4284 if OP0 is a constant that is >= 0, the signedness of
4285 the comparison isn't an issue, so suppress the
4286 warning. */
4287 bool warn =
4288 warn_type_limits && !in_system_header
4289 && c_inhibit_evaluation_warnings == 0
4290 && !(TREE_CODE (primop0) == INTEGER_CST
4291 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4292 primop0)))
4293 /* Do not warn for enumeration types. */
4294 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4296 switch (code)
4298 case GE_EXPR:
4299 if (warn)
4300 warning_at (loc, OPT_Wtype_limits,
4301 "comparison of unsigned expression >= 0 is always true");
4302 value = truthvalue_true_node;
4303 break;
4305 case LT_EXPR:
4306 if (warn)
4307 warning_at (loc, OPT_Wtype_limits,
4308 "comparison of unsigned expression < 0 is always false");
4309 value = truthvalue_false_node;
4310 break;
4312 default:
4313 break;
4316 if (value != 0)
4318 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4319 if (TREE_SIDE_EFFECTS (primop0))
4320 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4321 primop0, value);
4322 return value;
4327 *op0_ptr = convert (type, primop0);
4328 *op1_ptr = convert (type, primop1);
4330 *restype_ptr = truthvalue_type_node;
4332 return 0;
4335 /* Return a tree for the sum or difference (RESULTCODE says which)
4336 of pointer PTROP and integer INTOP. */
4338 tree
4339 pointer_int_sum (location_t loc, enum tree_code resultcode,
4340 tree ptrop, tree intop, bool complain)
4342 tree size_exp, ret;
4344 /* The result is a pointer of the same type that is being added. */
4345 tree result_type = TREE_TYPE (ptrop);
4347 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4349 if (complain && warn_pointer_arith)
4350 pedwarn (loc, OPT_Wpointer_arith,
4351 "pointer of type %<void *%> used in arithmetic");
4352 else if (!complain)
4353 return error_mark_node;
4354 size_exp = integer_one_node;
4356 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4358 if (complain && warn_pointer_arith)
4359 pedwarn (loc, OPT_Wpointer_arith,
4360 "pointer to a function used in arithmetic");
4361 else if (!complain)
4362 return error_mark_node;
4363 size_exp = integer_one_node;
4365 else
4366 size_exp = size_in_bytes (TREE_TYPE (result_type));
4368 /* We are manipulating pointer values, so we don't need to warn
4369 about relying on undefined signed overflow. We disable the
4370 warning here because we use integer types so fold won't know that
4371 they are really pointers. */
4372 fold_defer_overflow_warnings ();
4374 /* If what we are about to multiply by the size of the elements
4375 contains a constant term, apply distributive law
4376 and multiply that constant term separately.
4377 This helps produce common subexpressions. */
4378 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4379 && !TREE_CONSTANT (intop)
4380 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4381 && TREE_CONSTANT (size_exp)
4382 /* If the constant comes from pointer subtraction,
4383 skip this optimization--it would cause an error. */
4384 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4385 /* If the constant is unsigned, and smaller than the pointer size,
4386 then we must skip this optimization. This is because it could cause
4387 an overflow error if the constant is negative but INTOP is not. */
4388 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4389 || (TYPE_PRECISION (TREE_TYPE (intop))
4390 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4392 enum tree_code subcode = resultcode;
4393 tree int_type = TREE_TYPE (intop);
4394 if (TREE_CODE (intop) == MINUS_EXPR)
4395 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4396 /* Convert both subexpression types to the type of intop,
4397 because weird cases involving pointer arithmetic
4398 can result in a sum or difference with different type args. */
4399 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4400 subcode, ptrop,
4401 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4402 intop = convert (int_type, TREE_OPERAND (intop, 0));
4405 /* Convert the integer argument to a type the same size as sizetype
4406 so the multiply won't overflow spuriously. */
4407 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4408 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4409 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4410 TYPE_UNSIGNED (sizetype)), intop);
4412 /* Replace the integer argument with a suitable product by the object size.
4413 Do this multiplication as signed, then convert to the appropriate type
4414 for the pointer operation and disregard an overflow that occurred only
4415 because of the sign-extension change in the latter conversion. */
4417 tree t = build_binary_op (loc,
4418 MULT_EXPR, intop,
4419 convert (TREE_TYPE (intop), size_exp), 1);
4420 intop = convert (sizetype, t);
4421 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4422 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4423 TREE_INT_CST_HIGH (intop));
4426 /* Create the sum or difference. */
4427 if (resultcode == MINUS_EXPR)
4428 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4430 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4432 fold_undefer_and_ignore_overflow_warnings ();
4434 return ret;
4437 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4438 and if NON_CONST is known not to be permitted in an evaluated part
4439 of a constant expression. */
4441 tree
4442 c_wrap_maybe_const (tree expr, bool non_const)
4444 bool nowarning = TREE_NO_WARNING (expr);
4445 location_t loc = EXPR_LOCATION (expr);
4447 /* This should never be called for C++. */
4448 if (c_dialect_cxx ())
4449 gcc_unreachable ();
4451 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4452 STRIP_TYPE_NOPS (expr);
4453 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4454 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4455 if (nowarning)
4456 TREE_NO_WARNING (expr) = 1;
4457 protected_set_expr_location (expr, loc);
4459 return expr;
4462 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4463 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4464 around the SAVE_EXPR if needed so that c_fully_fold does not need
4465 to look inside SAVE_EXPRs. */
4467 tree
4468 c_save_expr (tree expr)
4470 bool maybe_const = true;
4471 if (c_dialect_cxx ())
4472 return save_expr (expr);
4473 expr = c_fully_fold (expr, false, &maybe_const);
4474 expr = save_expr (expr);
4475 if (!maybe_const)
4476 expr = c_wrap_maybe_const (expr, true);
4477 return expr;
4480 /* Return whether EXPR is a declaration whose address can never be
4481 NULL. */
4483 bool
4484 decl_with_nonnull_addr_p (const_tree expr)
4486 return (DECL_P (expr)
4487 && (TREE_CODE (expr) == PARM_DECL
4488 || TREE_CODE (expr) == LABEL_DECL
4489 || !DECL_WEAK (expr)));
4492 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4493 or for an `if' or `while' statement or ?..: exp. It should already
4494 have been validated to be of suitable type; otherwise, a bad
4495 diagnostic may result.
4497 The EXPR is located at LOCATION.
4499 This preparation consists of taking the ordinary
4500 representation of an expression expr and producing a valid tree
4501 boolean expression describing whether expr is nonzero. We could
4502 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4503 but we optimize comparisons, &&, ||, and !.
4505 The resulting type should always be `truthvalue_type_node'. */
4507 tree
4508 c_common_truthvalue_conversion (location_t location, tree expr)
4510 switch (TREE_CODE (expr))
4512 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4513 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4514 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4515 case ORDERED_EXPR: case UNORDERED_EXPR:
4516 if (TREE_TYPE (expr) == truthvalue_type_node)
4517 return expr;
4518 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4519 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4520 goto ret;
4522 case TRUTH_ANDIF_EXPR:
4523 case TRUTH_ORIF_EXPR:
4524 case TRUTH_AND_EXPR:
4525 case TRUTH_OR_EXPR:
4526 case TRUTH_XOR_EXPR:
4527 if (TREE_TYPE (expr) == truthvalue_type_node)
4528 return expr;
4529 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4530 c_common_truthvalue_conversion (location,
4531 TREE_OPERAND (expr, 0)),
4532 c_common_truthvalue_conversion (location,
4533 TREE_OPERAND (expr, 1)));
4534 goto ret;
4536 case TRUTH_NOT_EXPR:
4537 if (TREE_TYPE (expr) == truthvalue_type_node)
4538 return expr;
4539 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4540 c_common_truthvalue_conversion (location,
4541 TREE_OPERAND (expr, 0)));
4542 goto ret;
4544 case ERROR_MARK:
4545 return expr;
4547 case INTEGER_CST:
4548 return integer_zerop (expr) ? truthvalue_false_node
4549 : truthvalue_true_node;
4551 case REAL_CST:
4552 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4553 ? truthvalue_true_node
4554 : truthvalue_false_node;
4556 case FIXED_CST:
4557 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4558 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4559 ? truthvalue_true_node
4560 : truthvalue_false_node;
4562 case FUNCTION_DECL:
4563 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4564 /* Fall through. */
4566 case ADDR_EXPR:
4568 tree inner = TREE_OPERAND (expr, 0);
4569 if (decl_with_nonnull_addr_p (inner))
4571 /* Common Ada/Pascal programmer's mistake. */
4572 warning_at (location,
4573 OPT_Waddress,
4574 "the address of %qD will always evaluate as %<true%>",
4575 inner);
4576 return truthvalue_true_node;
4578 break;
4581 case COMPLEX_EXPR:
4582 expr = build_binary_op (EXPR_LOCATION (expr),
4583 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4584 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4585 c_common_truthvalue_conversion (location,
4586 TREE_OPERAND (expr, 0)),
4587 c_common_truthvalue_conversion (location,
4588 TREE_OPERAND (expr, 1)),
4590 goto ret;
4592 case NEGATE_EXPR:
4593 case ABS_EXPR:
4594 case FLOAT_EXPR:
4595 case EXCESS_PRECISION_EXPR:
4596 /* These don't change whether an object is nonzero or zero. */
4597 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4599 case LROTATE_EXPR:
4600 case RROTATE_EXPR:
4601 /* These don't change whether an object is zero or nonzero, but
4602 we can't ignore them if their second arg has side-effects. */
4603 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4605 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4606 TREE_OPERAND (expr, 1),
4607 c_common_truthvalue_conversion
4608 (location, TREE_OPERAND (expr, 0)));
4609 goto ret;
4611 else
4612 return c_common_truthvalue_conversion (location,
4613 TREE_OPERAND (expr, 0));
4615 case COND_EXPR:
4616 /* Distribute the conversion into the arms of a COND_EXPR. */
4617 if (c_dialect_cxx ())
4619 tree op1 = TREE_OPERAND (expr, 1);
4620 tree op2 = TREE_OPERAND (expr, 2);
4621 /* In C++ one of the arms might have void type if it is throw. */
4622 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4623 op1 = c_common_truthvalue_conversion (location, op1);
4624 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4625 op2 = c_common_truthvalue_conversion (location, op2);
4626 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4627 TREE_OPERAND (expr, 0), op1, op2);
4628 goto ret;
4630 else
4632 /* Folding will happen later for C. */
4633 expr = build3 (COND_EXPR, truthvalue_type_node,
4634 TREE_OPERAND (expr, 0),
4635 c_common_truthvalue_conversion (location,
4636 TREE_OPERAND (expr, 1)),
4637 c_common_truthvalue_conversion (location,
4638 TREE_OPERAND (expr, 2)));
4639 goto ret;
4642 CASE_CONVERT:
4644 tree totype = TREE_TYPE (expr);
4645 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4647 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4648 since that affects how `default_conversion' will behave. */
4649 if (TREE_CODE (totype) == REFERENCE_TYPE
4650 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4651 break;
4652 /* Don't strip a conversion from C++0x scoped enum, since they
4653 don't implicitly convert to other types. */
4654 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4655 && ENUM_IS_SCOPED (fromtype))
4656 break;
4657 /* If this isn't narrowing the argument, we can ignore it. */
4658 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4659 return c_common_truthvalue_conversion (location,
4660 TREE_OPERAND (expr, 0));
4662 break;
4664 case MODIFY_EXPR:
4665 if (!TREE_NO_WARNING (expr)
4666 && warn_parentheses)
4668 warning (OPT_Wparentheses,
4669 "suggest parentheses around assignment used as truth value");
4670 TREE_NO_WARNING (expr) = 1;
4672 break;
4674 default:
4675 break;
4678 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4680 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4681 expr = (build_binary_op
4682 (EXPR_LOCATION (expr),
4683 (TREE_SIDE_EFFECTS (expr)
4684 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4685 c_common_truthvalue_conversion
4686 (location,
4687 build_unary_op (location, REALPART_EXPR, t, 0)),
4688 c_common_truthvalue_conversion
4689 (location,
4690 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4691 0));
4692 goto ret;
4695 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4697 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4698 FCONST0 (TYPE_MODE
4699 (TREE_TYPE (expr))));
4700 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4702 else
4703 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4705 ret:
4706 protected_set_expr_location (expr, location);
4707 return expr;
4710 static void def_builtin_1 (enum built_in_function fncode,
4711 const char *name,
4712 enum built_in_class fnclass,
4713 tree fntype, tree libtype,
4714 bool both_p, bool fallback_p, bool nonansi_p,
4715 tree fnattrs, bool implicit_p);
4718 /* Apply the TYPE_QUALS to the new DECL. */
4720 void
4721 c_apply_type_quals_to_decl (int type_quals, tree decl)
4723 tree type = TREE_TYPE (decl);
4725 if (type == error_mark_node)
4726 return;
4728 if ((type_quals & TYPE_QUAL_CONST)
4729 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4730 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4731 constructor can produce constant init, so rely on cp_finish_decl to
4732 clear TREE_READONLY if the variable has non-constant init. */
4733 TREE_READONLY (decl) = 1;
4734 if (type_quals & TYPE_QUAL_VOLATILE)
4736 TREE_SIDE_EFFECTS (decl) = 1;
4737 TREE_THIS_VOLATILE (decl) = 1;
4739 if (type_quals & TYPE_QUAL_RESTRICT)
4741 while (type && TREE_CODE (type) == ARRAY_TYPE)
4742 /* Allow 'restrict' on arrays of pointers.
4743 FIXME currently we just ignore it. */
4744 type = TREE_TYPE (type);
4745 if (!type
4746 || !POINTER_TYPE_P (type)
4747 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4748 error ("invalid use of %<restrict%>");
4752 /* Hash function for the problem of multiple type definitions in
4753 different files. This must hash all types that will compare
4754 equal via comptypes to the same value. In practice it hashes
4755 on some of the simple stuff and leaves the details to comptypes. */
4757 static hashval_t
4758 c_type_hash (const void *p)
4760 int n_elements;
4761 int shift, size;
4762 const_tree const t = (const_tree) p;
4763 tree t2;
4764 switch (TREE_CODE (t))
4766 /* For pointers, hash on pointee type plus some swizzling. */
4767 case POINTER_TYPE:
4768 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4769 /* Hash on number of elements and total size. */
4770 case ENUMERAL_TYPE:
4771 shift = 3;
4772 t2 = TYPE_VALUES (t);
4773 break;
4774 case RECORD_TYPE:
4775 shift = 0;
4776 t2 = TYPE_FIELDS (t);
4777 break;
4778 case QUAL_UNION_TYPE:
4779 shift = 1;
4780 t2 = TYPE_FIELDS (t);
4781 break;
4782 case UNION_TYPE:
4783 shift = 2;
4784 t2 = TYPE_FIELDS (t);
4785 break;
4786 default:
4787 gcc_unreachable ();
4789 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4790 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4791 n_elements = list_length (t2);
4792 /* We might have a VLA here. */
4793 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4794 size = 0;
4795 else
4796 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4797 return ((size << 24) | (n_elements << shift));
4800 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4802 /* Return the typed-based alias set for T, which may be an expression
4803 or a type. Return -1 if we don't do anything special. */
4805 alias_set_type
4806 c_common_get_alias_set (tree t)
4808 tree u;
4809 PTR *slot;
4811 /* For VLAs, use the alias set of the element type rather than the
4812 default of alias set 0 for types compared structurally. */
4813 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4815 if (TREE_CODE (t) == ARRAY_TYPE)
4816 return get_alias_set (TREE_TYPE (t));
4817 return -1;
4820 /* Permit type-punning when accessing a union, provided the access
4821 is directly through the union. For example, this code does not
4822 permit taking the address of a union member and then storing
4823 through it. Even the type-punning allowed here is a GCC
4824 extension, albeit a common and useful one; the C standard says
4825 that such accesses have implementation-defined behavior. */
4826 for (u = t;
4827 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4828 u = TREE_OPERAND (u, 0))
4829 if (TREE_CODE (u) == COMPONENT_REF
4830 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4831 return 0;
4833 /* That's all the expressions we handle specially. */
4834 if (!TYPE_P (t))
4835 return -1;
4837 /* The C standard guarantees that any object may be accessed via an
4838 lvalue that has character type. */
4839 if (t == char_type_node
4840 || t == signed_char_type_node
4841 || t == unsigned_char_type_node)
4842 return 0;
4844 /* The C standard specifically allows aliasing between signed and
4845 unsigned variants of the same type. We treat the signed
4846 variant as canonical. */
4847 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4849 tree t1 = c_common_signed_type (t);
4851 /* t1 == t can happen for boolean nodes which are always unsigned. */
4852 if (t1 != t)
4853 return get_alias_set (t1);
4856 /* Handle the case of multiple type nodes referring to "the same" type,
4857 which occurs with IMA. These share an alias set. FIXME: Currently only
4858 C90 is handled. (In C99 type compatibility is not transitive, which
4859 complicates things mightily. The alias set splay trees can theoretically
4860 represent this, but insertion is tricky when you consider all the
4861 different orders things might arrive in.) */
4863 if (c_language != clk_c || flag_isoc99)
4864 return -1;
4866 /* Save time if there's only one input file. */
4867 if (num_in_fnames == 1)
4868 return -1;
4870 /* Pointers need special handling if they point to any type that
4871 needs special handling (below). */
4872 if (TREE_CODE (t) == POINTER_TYPE)
4874 tree t2;
4875 /* Find bottom type under any nested POINTERs. */
4876 for (t2 = TREE_TYPE (t);
4877 TREE_CODE (t2) == POINTER_TYPE;
4878 t2 = TREE_TYPE (t2))
4880 if (TREE_CODE (t2) != RECORD_TYPE
4881 && TREE_CODE (t2) != ENUMERAL_TYPE
4882 && TREE_CODE (t2) != QUAL_UNION_TYPE
4883 && TREE_CODE (t2) != UNION_TYPE)
4884 return -1;
4885 if (TYPE_SIZE (t2) == 0)
4886 return -1;
4888 /* These are the only cases that need special handling. */
4889 if (TREE_CODE (t) != RECORD_TYPE
4890 && TREE_CODE (t) != ENUMERAL_TYPE
4891 && TREE_CODE (t) != QUAL_UNION_TYPE
4892 && TREE_CODE (t) != UNION_TYPE
4893 && TREE_CODE (t) != POINTER_TYPE)
4894 return -1;
4895 /* Undefined? */
4896 if (TYPE_SIZE (t) == 0)
4897 return -1;
4899 /* Look up t in hash table. Only one of the compatible types within each
4900 alias set is recorded in the table. */
4901 if (!type_hash_table)
4902 type_hash_table = htab_create_ggc (1021, c_type_hash,
4903 (htab_eq) lang_hooks.types_compatible_p,
4904 NULL);
4905 slot = htab_find_slot (type_hash_table, t, INSERT);
4906 if (*slot != NULL)
4908 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4909 return TYPE_ALIAS_SET ((tree)*slot);
4911 else
4912 /* Our caller will assign and record (in t) a new alias set; all we need
4913 to do is remember t in the hash table. */
4914 *slot = t;
4916 return -1;
4919 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4920 the second parameter indicates which OPERATOR is being applied.
4921 The COMPLAIN flag controls whether we should diagnose possibly
4922 ill-formed constructs or not. LOC is the location of the SIZEOF or
4923 TYPEOF operator. */
4925 tree
4926 c_sizeof_or_alignof_type (location_t loc,
4927 tree type, bool is_sizeof, int complain)
4929 const char *op_name;
4930 tree value = NULL;
4931 enum tree_code type_code = TREE_CODE (type);
4933 op_name = is_sizeof ? "sizeof" : "__alignof__";
4935 if (type_code == FUNCTION_TYPE)
4937 if (is_sizeof)
4939 if (complain && warn_pointer_arith)
4940 pedwarn (loc, OPT_Wpointer_arith,
4941 "invalid application of %<sizeof%> to a function type");
4942 else if (!complain)
4943 return error_mark_node;
4944 value = size_one_node;
4946 else
4948 if (complain)
4950 if (c_dialect_cxx ())
4951 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4952 "%<alignof%> applied to a function type");
4953 else
4954 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4955 "%<_Alignof%> applied to a function type");
4957 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4960 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4962 if (type_code == VOID_TYPE
4963 && complain && warn_pointer_arith)
4964 pedwarn (loc, OPT_Wpointer_arith,
4965 "invalid application of %qs to a void type", op_name);
4966 else if (!complain)
4967 return error_mark_node;
4968 value = size_one_node;
4970 else if (!COMPLETE_TYPE_P (type)
4971 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4973 if (complain)
4974 error_at (loc, "invalid application of %qs to incomplete type %qT",
4975 op_name, type);
4976 return error_mark_node;
4978 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4979 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4981 if (complain)
4982 error_at (loc, "invalid application of %qs to array type %qT of "
4983 "incomplete element type", op_name, type);
4984 return error_mark_node;
4986 else
4988 if (is_sizeof)
4989 /* Convert in case a char is more than one unit. */
4990 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4991 size_int (TYPE_PRECISION (char_type_node)
4992 / BITS_PER_UNIT));
4993 else
4994 value = size_int (TYPE_ALIGN_UNIT (type));
4997 /* VALUE will have the middle-end integer type sizetype.
4998 However, we should really return a value of type `size_t',
4999 which is just a typedef for an ordinary integer type. */
5000 value = fold_convert_loc (loc, size_type_node, value);
5002 return value;
5005 /* Implement the __alignof keyword: Return the minimum required
5006 alignment of EXPR, measured in bytes. For VAR_DECLs,
5007 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5008 from an "aligned" __attribute__ specification). LOC is the
5009 location of the ALIGNOF operator. */
5011 tree
5012 c_alignof_expr (location_t loc, tree expr)
5014 tree t;
5016 if (VAR_OR_FUNCTION_DECL_P (expr))
5017 t = size_int (DECL_ALIGN_UNIT (expr));
5019 else if (TREE_CODE (expr) == COMPONENT_REF
5020 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5022 error_at (loc, "%<__alignof%> applied to a bit-field");
5023 t = size_one_node;
5025 else if (TREE_CODE (expr) == COMPONENT_REF
5026 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5027 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5029 else if (TREE_CODE (expr) == INDIRECT_REF)
5031 tree t = TREE_OPERAND (expr, 0);
5032 tree best = t;
5033 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5035 while (CONVERT_EXPR_P (t)
5036 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5038 int thisalign;
5040 t = TREE_OPERAND (t, 0);
5041 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5042 if (thisalign > bestalign)
5043 best = t, bestalign = thisalign;
5045 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5047 else
5048 return c_alignof (loc, TREE_TYPE (expr));
5050 return fold_convert_loc (loc, size_type_node, t);
5053 /* Handle C and C++ default attributes. */
5055 enum built_in_attribute
5057 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5058 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5059 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5060 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5061 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5062 #include "builtin-attrs.def"
5063 #undef DEF_ATTR_NULL_TREE
5064 #undef DEF_ATTR_INT
5065 #undef DEF_ATTR_STRING
5066 #undef DEF_ATTR_IDENT
5067 #undef DEF_ATTR_TREE_LIST
5068 ATTR_LAST
5071 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5073 static void c_init_attributes (void);
5075 enum c_builtin_type
5077 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5078 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5079 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5080 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5081 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5082 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5083 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5084 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5085 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5086 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5087 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5088 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5089 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5090 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5091 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5092 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5093 NAME,
5094 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5095 #include "builtin-types.def"
5096 #undef DEF_PRIMITIVE_TYPE
5097 #undef DEF_FUNCTION_TYPE_0
5098 #undef DEF_FUNCTION_TYPE_1
5099 #undef DEF_FUNCTION_TYPE_2
5100 #undef DEF_FUNCTION_TYPE_3
5101 #undef DEF_FUNCTION_TYPE_4
5102 #undef DEF_FUNCTION_TYPE_5
5103 #undef DEF_FUNCTION_TYPE_6
5104 #undef DEF_FUNCTION_TYPE_7
5105 #undef DEF_FUNCTION_TYPE_8
5106 #undef DEF_FUNCTION_TYPE_VAR_0
5107 #undef DEF_FUNCTION_TYPE_VAR_1
5108 #undef DEF_FUNCTION_TYPE_VAR_2
5109 #undef DEF_FUNCTION_TYPE_VAR_3
5110 #undef DEF_FUNCTION_TYPE_VAR_4
5111 #undef DEF_FUNCTION_TYPE_VAR_5
5112 #undef DEF_POINTER_TYPE
5113 BT_LAST
5116 typedef enum c_builtin_type builtin_type;
5118 /* A temporary array for c_common_nodes_and_builtins. Used in
5119 communication with def_fn_type. */
5120 static tree builtin_types[(int) BT_LAST + 1];
5122 /* A helper function for c_common_nodes_and_builtins. Build function type
5123 for DEF with return type RET and N arguments. If VAR is true, then the
5124 function should be variadic after those N arguments.
5126 Takes special care not to ICE if any of the types involved are
5127 error_mark_node, which indicates that said type is not in fact available
5128 (see builtin_type_for_size). In which case the function type as a whole
5129 should be error_mark_node. */
5131 static void
5132 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5134 tree t;
5135 tree *args = XALLOCAVEC (tree, n);
5136 va_list list;
5137 int i;
5139 va_start (list, n);
5140 for (i = 0; i < n; ++i)
5142 builtin_type a = (builtin_type) va_arg (list, int);
5143 t = builtin_types[a];
5144 if (t == error_mark_node)
5145 goto egress;
5146 args[i] = t;
5149 t = builtin_types[ret];
5150 if (t == error_mark_node)
5151 goto egress;
5152 if (var)
5153 t = build_varargs_function_type_array (t, n, args);
5154 else
5155 t = build_function_type_array (t, n, args);
5157 egress:
5158 builtin_types[def] = t;
5159 va_end (list);
5162 /* Build builtin functions common to both C and C++ language
5163 frontends. */
5165 static void
5166 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5168 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5169 builtin_types[ENUM] = VALUE;
5170 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5171 def_fn_type (ENUM, RETURN, 0, 0);
5172 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5173 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5174 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5175 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5176 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5177 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5178 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5179 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5180 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5181 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5182 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5183 ARG6) \
5184 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5185 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5186 ARG6, ARG7) \
5187 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5188 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5189 ARG6, ARG7, ARG8) \
5190 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5191 ARG7, ARG8);
5192 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5193 def_fn_type (ENUM, RETURN, 1, 0);
5194 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5195 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5196 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5197 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5198 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5199 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5200 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5201 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5202 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5203 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5204 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5205 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5207 #include "builtin-types.def"
5209 #undef DEF_PRIMITIVE_TYPE
5210 #undef DEF_FUNCTION_TYPE_1
5211 #undef DEF_FUNCTION_TYPE_2
5212 #undef DEF_FUNCTION_TYPE_3
5213 #undef DEF_FUNCTION_TYPE_4
5214 #undef DEF_FUNCTION_TYPE_5
5215 #undef DEF_FUNCTION_TYPE_6
5216 #undef DEF_FUNCTION_TYPE_VAR_0
5217 #undef DEF_FUNCTION_TYPE_VAR_1
5218 #undef DEF_FUNCTION_TYPE_VAR_2
5219 #undef DEF_FUNCTION_TYPE_VAR_3
5220 #undef DEF_FUNCTION_TYPE_VAR_4
5221 #undef DEF_FUNCTION_TYPE_VAR_5
5222 #undef DEF_POINTER_TYPE
5223 builtin_types[(int) BT_LAST] = NULL_TREE;
5225 c_init_attributes ();
5227 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5228 NONANSI_P, ATTRS, IMPLICIT, COND) \
5229 if (NAME && COND) \
5230 def_builtin_1 (ENUM, NAME, CLASS, \
5231 builtin_types[(int) TYPE], \
5232 builtin_types[(int) LIBTYPE], \
5233 BOTH_P, FALLBACK_P, NONANSI_P, \
5234 built_in_attributes[(int) ATTRS], IMPLICIT);
5235 #include "builtins.def"
5236 #undef DEF_BUILTIN
5238 targetm.init_builtins ();
5240 build_common_builtin_nodes ();
5242 if (flag_enable_cilkplus)
5243 cilk_init_builtins ();
5246 /* Like get_identifier, but avoid warnings about null arguments when
5247 the argument may be NULL for targets where GCC lacks stdint.h type
5248 information. */
5250 static inline tree
5251 c_get_ident (const char *id)
5253 return get_identifier (id);
5256 /* Build tree nodes and builtin functions common to both C and C++ language
5257 frontends. */
5259 void
5260 c_common_nodes_and_builtins (void)
5262 int char16_type_size;
5263 int char32_type_size;
5264 int wchar_type_size;
5265 tree array_domain_type;
5266 tree va_list_ref_type_node;
5267 tree va_list_arg_type_node;
5269 build_common_tree_nodes (flag_signed_char, flag_short_double);
5271 /* Define `int' and `char' first so that dbx will output them first. */
5272 record_builtin_type (RID_INT, NULL, integer_type_node);
5273 record_builtin_type (RID_CHAR, "char", char_type_node);
5275 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5276 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5277 but not C. Are the conditionals here needed? */
5278 if (c_dialect_cxx ())
5279 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5280 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5281 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5282 record_builtin_type (RID_MAX, "long unsigned int",
5283 long_unsigned_type_node);
5284 if (int128_integer_type_node != NULL_TREE)
5286 record_builtin_type (RID_INT128, "__int128",
5287 int128_integer_type_node);
5288 record_builtin_type (RID_MAX, "__int128 unsigned",
5289 int128_unsigned_type_node);
5291 if (c_dialect_cxx ())
5292 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5293 record_builtin_type (RID_MAX, "long long int",
5294 long_long_integer_type_node);
5295 record_builtin_type (RID_MAX, "long long unsigned int",
5296 long_long_unsigned_type_node);
5297 if (c_dialect_cxx ())
5298 record_builtin_type (RID_MAX, "long long unsigned",
5299 long_long_unsigned_type_node);
5300 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5301 record_builtin_type (RID_MAX, "short unsigned int",
5302 short_unsigned_type_node);
5303 if (c_dialect_cxx ())
5304 record_builtin_type (RID_MAX, "unsigned short",
5305 short_unsigned_type_node);
5307 /* Define both `signed char' and `unsigned char'. */
5308 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5309 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5311 /* These are types that c_common_type_for_size and
5312 c_common_type_for_mode use. */
5313 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5314 TYPE_DECL, NULL_TREE,
5315 intQI_type_node));
5316 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5317 TYPE_DECL, NULL_TREE,
5318 intHI_type_node));
5319 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5320 TYPE_DECL, NULL_TREE,
5321 intSI_type_node));
5322 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5323 TYPE_DECL, NULL_TREE,
5324 intDI_type_node));
5325 #if HOST_BITS_PER_WIDE_INT >= 64
5326 if (targetm.scalar_mode_supported_p (TImode))
5327 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5328 TYPE_DECL,
5329 get_identifier ("__int128_t"),
5330 intTI_type_node));
5331 #endif
5332 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5333 TYPE_DECL, NULL_TREE,
5334 unsigned_intQI_type_node));
5335 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5336 TYPE_DECL, NULL_TREE,
5337 unsigned_intHI_type_node));
5338 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5339 TYPE_DECL, NULL_TREE,
5340 unsigned_intSI_type_node));
5341 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5342 TYPE_DECL, NULL_TREE,
5343 unsigned_intDI_type_node));
5344 #if HOST_BITS_PER_WIDE_INT >= 64
5345 if (targetm.scalar_mode_supported_p (TImode))
5346 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5347 TYPE_DECL,
5348 get_identifier ("__uint128_t"),
5349 unsigned_intTI_type_node));
5350 #endif
5352 /* Create the widest literal types. */
5353 widest_integer_literal_type_node
5354 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5355 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5356 TYPE_DECL, NULL_TREE,
5357 widest_integer_literal_type_node));
5359 widest_unsigned_literal_type_node
5360 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5361 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5362 TYPE_DECL, NULL_TREE,
5363 widest_unsigned_literal_type_node));
5365 signed_size_type_node = c_common_signed_type (size_type_node);
5367 pid_type_node =
5368 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5370 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5371 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5372 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5374 /* Only supported decimal floating point extension if the target
5375 actually supports underlying modes. */
5376 if (targetm.scalar_mode_supported_p (SDmode)
5377 && targetm.scalar_mode_supported_p (DDmode)
5378 && targetm.scalar_mode_supported_p (TDmode))
5380 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5381 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5382 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5385 if (targetm.fixed_point_supported_p ())
5387 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5388 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5389 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5390 record_builtin_type (RID_MAX, "long long _Fract",
5391 long_long_fract_type_node);
5392 record_builtin_type (RID_MAX, "unsigned short _Fract",
5393 unsigned_short_fract_type_node);
5394 record_builtin_type (RID_MAX, "unsigned _Fract",
5395 unsigned_fract_type_node);
5396 record_builtin_type (RID_MAX, "unsigned long _Fract",
5397 unsigned_long_fract_type_node);
5398 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5399 unsigned_long_long_fract_type_node);
5400 record_builtin_type (RID_MAX, "_Sat short _Fract",
5401 sat_short_fract_type_node);
5402 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5403 record_builtin_type (RID_MAX, "_Sat long _Fract",
5404 sat_long_fract_type_node);
5405 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5406 sat_long_long_fract_type_node);
5407 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5408 sat_unsigned_short_fract_type_node);
5409 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5410 sat_unsigned_fract_type_node);
5411 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5412 sat_unsigned_long_fract_type_node);
5413 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5414 sat_unsigned_long_long_fract_type_node);
5415 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5416 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5417 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5418 record_builtin_type (RID_MAX, "long long _Accum",
5419 long_long_accum_type_node);
5420 record_builtin_type (RID_MAX, "unsigned short _Accum",
5421 unsigned_short_accum_type_node);
5422 record_builtin_type (RID_MAX, "unsigned _Accum",
5423 unsigned_accum_type_node);
5424 record_builtin_type (RID_MAX, "unsigned long _Accum",
5425 unsigned_long_accum_type_node);
5426 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5427 unsigned_long_long_accum_type_node);
5428 record_builtin_type (RID_MAX, "_Sat short _Accum",
5429 sat_short_accum_type_node);
5430 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5431 record_builtin_type (RID_MAX, "_Sat long _Accum",
5432 sat_long_accum_type_node);
5433 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5434 sat_long_long_accum_type_node);
5435 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5436 sat_unsigned_short_accum_type_node);
5437 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5438 sat_unsigned_accum_type_node);
5439 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5440 sat_unsigned_long_accum_type_node);
5441 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5442 sat_unsigned_long_long_accum_type_node);
5446 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5447 TYPE_DECL,
5448 get_identifier ("complex int"),
5449 complex_integer_type_node));
5450 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5451 TYPE_DECL,
5452 get_identifier ("complex float"),
5453 complex_float_type_node));
5454 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5455 TYPE_DECL,
5456 get_identifier ("complex double"),
5457 complex_double_type_node));
5458 lang_hooks.decls.pushdecl
5459 (build_decl (UNKNOWN_LOCATION,
5460 TYPE_DECL, get_identifier ("complex long double"),
5461 complex_long_double_type_node));
5463 if (c_dialect_cxx ())
5464 /* For C++, make fileptr_type_node a distinct void * type until
5465 FILE type is defined. */
5466 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5468 record_builtin_type (RID_VOID, NULL, void_type_node);
5470 /* Set the TYPE_NAME for any variants that were built before
5471 record_builtin_type gave names to the built-in types. */
5473 tree void_name = TYPE_NAME (void_type_node);
5474 TYPE_NAME (void_type_node) = NULL_TREE;
5475 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5476 = void_name;
5477 TYPE_NAME (void_type_node) = void_name;
5480 /* This node must not be shared. */
5481 void_zero_node = make_node (INTEGER_CST);
5482 TREE_TYPE (void_zero_node) = void_type_node;
5484 void_list_node = build_void_list_node ();
5486 /* Make a type to be the domain of a few array types
5487 whose domains don't really matter.
5488 200 is small enough that it always fits in size_t
5489 and large enough that it can hold most function names for the
5490 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5491 array_domain_type = build_index_type (size_int (200));
5493 /* Make a type for arrays of characters.
5494 With luck nothing will ever really depend on the length of this
5495 array type. */
5496 char_array_type_node
5497 = build_array_type (char_type_node, array_domain_type);
5499 /* Likewise for arrays of ints. */
5500 int_array_type_node
5501 = build_array_type (integer_type_node, array_domain_type);
5503 string_type_node = build_pointer_type (char_type_node);
5504 const_string_type_node
5505 = build_pointer_type (build_qualified_type
5506 (char_type_node, TYPE_QUAL_CONST));
5508 /* This is special for C++ so functions can be overloaded. */
5509 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5510 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5511 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5512 underlying_wchar_type_node = wchar_type_node;
5513 if (c_dialect_cxx ())
5515 if (TYPE_UNSIGNED (wchar_type_node))
5516 wchar_type_node = make_unsigned_type (wchar_type_size);
5517 else
5518 wchar_type_node = make_signed_type (wchar_type_size);
5519 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5522 /* This is for wide string constants. */
5523 wchar_array_type_node
5524 = build_array_type (wchar_type_node, array_domain_type);
5526 /* Define 'char16_t'. */
5527 char16_type_node = get_identifier (CHAR16_TYPE);
5528 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5529 char16_type_size = TYPE_PRECISION (char16_type_node);
5530 if (c_dialect_cxx ())
5532 char16_type_node = make_unsigned_type (char16_type_size);
5534 if (cxx_dialect >= cxx11)
5535 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5538 /* This is for UTF-16 string constants. */
5539 char16_array_type_node
5540 = build_array_type (char16_type_node, array_domain_type);
5542 /* Define 'char32_t'. */
5543 char32_type_node = get_identifier (CHAR32_TYPE);
5544 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5545 char32_type_size = TYPE_PRECISION (char32_type_node);
5546 if (c_dialect_cxx ())
5548 char32_type_node = make_unsigned_type (char32_type_size);
5550 if (cxx_dialect >= cxx11)
5551 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5554 /* This is for UTF-32 string constants. */
5555 char32_array_type_node
5556 = build_array_type (char32_type_node, array_domain_type);
5558 wint_type_node =
5559 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5561 intmax_type_node =
5562 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5563 uintmax_type_node =
5564 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5566 if (SIG_ATOMIC_TYPE)
5567 sig_atomic_type_node =
5568 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5569 if (INT8_TYPE)
5570 int8_type_node =
5571 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5572 if (INT16_TYPE)
5573 int16_type_node =
5574 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5575 if (INT32_TYPE)
5576 int32_type_node =
5577 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5578 if (INT64_TYPE)
5579 int64_type_node =
5580 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5581 if (UINT8_TYPE)
5582 uint8_type_node =
5583 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5584 if (UINT16_TYPE)
5585 c_uint16_type_node =
5586 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5587 if (UINT32_TYPE)
5588 c_uint32_type_node =
5589 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5590 if (UINT64_TYPE)
5591 c_uint64_type_node =
5592 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5593 if (INT_LEAST8_TYPE)
5594 int_least8_type_node =
5595 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5596 if (INT_LEAST16_TYPE)
5597 int_least16_type_node =
5598 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5599 if (INT_LEAST32_TYPE)
5600 int_least32_type_node =
5601 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5602 if (INT_LEAST64_TYPE)
5603 int_least64_type_node =
5604 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5605 if (UINT_LEAST8_TYPE)
5606 uint_least8_type_node =
5607 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5608 if (UINT_LEAST16_TYPE)
5609 uint_least16_type_node =
5610 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5611 if (UINT_LEAST32_TYPE)
5612 uint_least32_type_node =
5613 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5614 if (UINT_LEAST64_TYPE)
5615 uint_least64_type_node =
5616 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5617 if (INT_FAST8_TYPE)
5618 int_fast8_type_node =
5619 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5620 if (INT_FAST16_TYPE)
5621 int_fast16_type_node =
5622 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5623 if (INT_FAST32_TYPE)
5624 int_fast32_type_node =
5625 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5626 if (INT_FAST64_TYPE)
5627 int_fast64_type_node =
5628 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5629 if (UINT_FAST8_TYPE)
5630 uint_fast8_type_node =
5631 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5632 if (UINT_FAST16_TYPE)
5633 uint_fast16_type_node =
5634 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5635 if (UINT_FAST32_TYPE)
5636 uint_fast32_type_node =
5637 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5638 if (UINT_FAST64_TYPE)
5639 uint_fast64_type_node =
5640 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5641 if (INTPTR_TYPE)
5642 intptr_type_node =
5643 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5644 if (UINTPTR_TYPE)
5645 uintptr_type_node =
5646 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5648 default_function_type
5649 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5650 ptrdiff_type_node
5651 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5652 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5654 lang_hooks.decls.pushdecl
5655 (build_decl (UNKNOWN_LOCATION,
5656 TYPE_DECL, get_identifier ("__builtin_va_list"),
5657 va_list_type_node));
5658 if (targetm.enum_va_list_p)
5660 int l;
5661 const char *pname;
5662 tree ptype;
5664 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5666 lang_hooks.decls.pushdecl
5667 (build_decl (UNKNOWN_LOCATION,
5668 TYPE_DECL, get_identifier (pname),
5669 ptype));
5674 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5676 va_list_arg_type_node = va_list_ref_type_node =
5677 build_pointer_type (TREE_TYPE (va_list_type_node));
5679 else
5681 va_list_arg_type_node = va_list_type_node;
5682 va_list_ref_type_node = build_reference_type (va_list_type_node);
5685 if (!flag_preprocess_only)
5686 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5688 main_identifier_node = get_identifier ("main");
5690 /* Create the built-in __null node. It is important that this is
5691 not shared. */
5692 null_node = make_node (INTEGER_CST);
5693 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5695 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5696 memset (builtin_types, 0, sizeof (builtin_types));
5699 /* The number of named compound-literals generated thus far. */
5700 static GTY(()) int compound_literal_number;
5702 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5704 void
5705 set_compound_literal_name (tree decl)
5707 char *name;
5708 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5709 compound_literal_number);
5710 compound_literal_number++;
5711 DECL_NAME (decl) = get_identifier (name);
5714 tree
5715 build_va_arg (location_t loc, tree expr, tree type)
5717 expr = build1 (VA_ARG_EXPR, type, expr);
5718 SET_EXPR_LOCATION (expr, loc);
5719 return expr;
5723 /* Linked list of disabled built-in functions. */
5725 typedef struct disabled_builtin
5727 const char *name;
5728 struct disabled_builtin *next;
5729 } disabled_builtin;
5730 static disabled_builtin *disabled_builtins = NULL;
5732 static bool builtin_function_disabled_p (const char *);
5734 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5735 begins with "__builtin_", give an error. */
5737 void
5738 disable_builtin_function (const char *name)
5740 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5741 error ("cannot disable built-in function %qs", name);
5742 else
5744 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5745 new_disabled_builtin->name = name;
5746 new_disabled_builtin->next = disabled_builtins;
5747 disabled_builtins = new_disabled_builtin;
5752 /* Return true if the built-in function NAME has been disabled, false
5753 otherwise. */
5755 static bool
5756 builtin_function_disabled_p (const char *name)
5758 disabled_builtin *p;
5759 for (p = disabled_builtins; p != NULL; p = p->next)
5761 if (strcmp (name, p->name) == 0)
5762 return true;
5764 return false;
5768 /* Worker for DEF_BUILTIN.
5769 Possibly define a builtin function with one or two names.
5770 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5771 nonansi_p and flag_no_nonansi_builtin. */
5773 static void
5774 def_builtin_1 (enum built_in_function fncode,
5775 const char *name,
5776 enum built_in_class fnclass,
5777 tree fntype, tree libtype,
5778 bool both_p, bool fallback_p, bool nonansi_p,
5779 tree fnattrs, bool implicit_p)
5781 tree decl;
5782 const char *libname;
5784 if (fntype == error_mark_node)
5785 return;
5787 gcc_assert ((!both_p && !fallback_p)
5788 || !strncmp (name, "__builtin_",
5789 strlen ("__builtin_")));
5791 libname = name + strlen ("__builtin_");
5792 decl = add_builtin_function (name, fntype, fncode, fnclass,
5793 (fallback_p ? libname : NULL),
5794 fnattrs);
5796 set_builtin_decl (fncode, decl, implicit_p);
5798 if (both_p
5799 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5800 && !(nonansi_p && flag_no_nonansi_builtin))
5801 add_builtin_function (libname, libtype, fncode, fnclass,
5802 NULL, fnattrs);
5805 /* Nonzero if the type T promotes to int. This is (nearly) the
5806 integral promotions defined in ISO C99 6.3.1.1/2. */
5808 bool
5809 c_promoting_integer_type_p (const_tree t)
5811 switch (TREE_CODE (t))
5813 case INTEGER_TYPE:
5814 return (TYPE_MAIN_VARIANT (t) == char_type_node
5815 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5816 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5817 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5818 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5819 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5821 case ENUMERAL_TYPE:
5822 /* ??? Technically all enumerations not larger than an int
5823 promote to an int. But this is used along code paths
5824 that only want to notice a size change. */
5825 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5827 case BOOLEAN_TYPE:
5828 return 1;
5830 default:
5831 return 0;
5835 /* Return 1 if PARMS specifies a fixed number of parameters
5836 and none of their types is affected by default promotions. */
5839 self_promoting_args_p (const_tree parms)
5841 const_tree t;
5842 for (t = parms; t; t = TREE_CHAIN (t))
5844 tree type = TREE_VALUE (t);
5846 if (type == error_mark_node)
5847 continue;
5849 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5850 return 0;
5852 if (type == 0)
5853 return 0;
5855 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5856 return 0;
5858 if (c_promoting_integer_type_p (type))
5859 return 0;
5861 return 1;
5864 /* Recursively remove any '*' or '&' operator from TYPE. */
5865 tree
5866 strip_pointer_operator (tree t)
5868 while (POINTER_TYPE_P (t))
5869 t = TREE_TYPE (t);
5870 return t;
5873 /* Recursively remove pointer or array type from TYPE. */
5874 tree
5875 strip_pointer_or_array_types (tree t)
5877 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5878 t = TREE_TYPE (t);
5879 return t;
5882 /* Used to compare case labels. K1 and K2 are actually tree nodes
5883 representing case labels, or NULL_TREE for a `default' label.
5884 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5885 K2, and 0 if K1 and K2 are equal. */
5888 case_compare (splay_tree_key k1, splay_tree_key k2)
5890 /* Consider a NULL key (such as arises with a `default' label) to be
5891 smaller than anything else. */
5892 if (!k1)
5893 return k2 ? -1 : 0;
5894 else if (!k2)
5895 return k1 ? 1 : 0;
5897 return tree_int_cst_compare ((tree) k1, (tree) k2);
5900 /* Process a case label, located at LOC, for the range LOW_VALUE
5901 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5902 then this case label is actually a `default' label. If only
5903 HIGH_VALUE is NULL_TREE, then case label was declared using the
5904 usual C/C++ syntax, rather than the GNU case range extension.
5905 CASES is a tree containing all the case ranges processed so far;
5906 COND is the condition for the switch-statement itself. Returns the
5907 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5908 is created. */
5910 tree
5911 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5912 tree low_value, tree high_value)
5914 tree type;
5915 tree label;
5916 tree case_label;
5917 splay_tree_node node;
5919 /* Create the LABEL_DECL itself. */
5920 label = create_artificial_label (loc);
5922 /* If there was an error processing the switch condition, bail now
5923 before we get more confused. */
5924 if (!cond || cond == error_mark_node)
5925 goto error_out;
5927 if ((low_value && TREE_TYPE (low_value)
5928 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5929 || (high_value && TREE_TYPE (high_value)
5930 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5932 error_at (loc, "pointers are not permitted as case values");
5933 goto error_out;
5936 /* Case ranges are a GNU extension. */
5937 if (high_value)
5938 pedwarn (loc, OPT_Wpedantic,
5939 "range expressions in switch statements are non-standard");
5941 type = TREE_TYPE (cond);
5942 if (low_value)
5944 low_value = check_case_value (low_value);
5945 low_value = convert_and_check (type, low_value);
5946 if (low_value == error_mark_node)
5947 goto error_out;
5949 if (high_value)
5951 high_value = check_case_value (high_value);
5952 high_value = convert_and_check (type, high_value);
5953 if (high_value == error_mark_node)
5954 goto error_out;
5957 if (low_value && high_value)
5959 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5960 really a case range, even though it was written that way.
5961 Remove the HIGH_VALUE to simplify later processing. */
5962 if (tree_int_cst_equal (low_value, high_value))
5963 high_value = NULL_TREE;
5964 else if (!tree_int_cst_lt (low_value, high_value))
5965 warning_at (loc, 0, "empty range specified");
5968 /* See if the case is in range of the type of the original testing
5969 expression. If both low_value and high_value are out of range,
5970 don't insert the case label and return NULL_TREE. */
5971 if (low_value
5972 && !check_case_bounds (type, orig_type,
5973 &low_value, high_value ? &high_value : NULL))
5974 return NULL_TREE;
5976 /* Look up the LOW_VALUE in the table of case labels we already
5977 have. */
5978 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5979 /* If there was not an exact match, check for overlapping ranges.
5980 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5981 that's a `default' label and the only overlap is an exact match. */
5982 if (!node && (low_value || high_value))
5984 splay_tree_node low_bound;
5985 splay_tree_node high_bound;
5987 /* Even though there wasn't an exact match, there might be an
5988 overlap between this case range and another case range.
5989 Since we've (inductively) not allowed any overlapping case
5990 ranges, we simply need to find the greatest low case label
5991 that is smaller that LOW_VALUE, and the smallest low case
5992 label that is greater than LOW_VALUE. If there is an overlap
5993 it will occur in one of these two ranges. */
5994 low_bound = splay_tree_predecessor (cases,
5995 (splay_tree_key) low_value);
5996 high_bound = splay_tree_successor (cases,
5997 (splay_tree_key) low_value);
5999 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6000 the LOW_VALUE, so there is no need to check unless the
6001 LOW_BOUND is in fact itself a case range. */
6002 if (low_bound
6003 && CASE_HIGH ((tree) low_bound->value)
6004 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6005 low_value) >= 0)
6006 node = low_bound;
6007 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6008 range is bigger than the low end of the current range, so we
6009 are only interested if the current range is a real range, and
6010 not an ordinary case label. */
6011 else if (high_bound
6012 && high_value
6013 && (tree_int_cst_compare ((tree) high_bound->key,
6014 high_value)
6015 <= 0))
6016 node = high_bound;
6018 /* If there was an overlap, issue an error. */
6019 if (node)
6021 tree duplicate = CASE_LABEL ((tree) node->value);
6023 if (high_value)
6025 error_at (loc, "duplicate (or overlapping) case value");
6026 error_at (DECL_SOURCE_LOCATION (duplicate),
6027 "this is the first entry overlapping that value");
6029 else if (low_value)
6031 error_at (loc, "duplicate case value") ;
6032 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6034 else
6036 error_at (loc, "multiple default labels in one switch");
6037 error_at (DECL_SOURCE_LOCATION (duplicate),
6038 "this is the first default label");
6040 goto error_out;
6043 /* Add a CASE_LABEL to the statement-tree. */
6044 case_label = add_stmt (build_case_label (low_value, high_value, label));
6045 /* Register this case label in the splay tree. */
6046 splay_tree_insert (cases,
6047 (splay_tree_key) low_value,
6048 (splay_tree_value) case_label);
6050 return case_label;
6052 error_out:
6053 /* Add a label so that the back-end doesn't think that the beginning of
6054 the switch is unreachable. Note that we do not add a case label, as
6055 that just leads to duplicates and thence to failure later on. */
6056 if (!cases->root)
6058 tree t = create_artificial_label (loc);
6059 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6061 return error_mark_node;
6064 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6065 Used to verify that case values match up with enumerator values. */
6067 static void
6068 match_case_to_enum_1 (tree key, tree type, tree label)
6070 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6072 /* ??? Not working too hard to print the double-word value.
6073 Should perhaps be done with %lwd in the diagnostic routines? */
6074 if (TREE_INT_CST_HIGH (key) == 0)
6075 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6076 TREE_INT_CST_LOW (key));
6077 else if (!TYPE_UNSIGNED (type)
6078 && TREE_INT_CST_HIGH (key) == -1
6079 && TREE_INT_CST_LOW (key) != 0)
6080 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6081 -TREE_INT_CST_LOW (key));
6082 else
6083 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6084 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6085 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6087 if (TYPE_NAME (type) == 0)
6088 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6089 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6090 "case value %qs not in enumerated type",
6091 buf);
6092 else
6093 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6094 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6095 "case value %qs not in enumerated type %qT",
6096 buf, type);
6099 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6100 Used to verify that case values match up with enumerator values. */
6102 static int
6103 match_case_to_enum (splay_tree_node node, void *data)
6105 tree label = (tree) node->value;
6106 tree type = (tree) data;
6108 /* Skip default case. */
6109 if (!CASE_LOW (label))
6110 return 0;
6112 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6113 when we did our enum->case scan. Reset our scratch bit after. */
6114 if (!CASE_LOW_SEEN (label))
6115 match_case_to_enum_1 (CASE_LOW (label), type, label);
6116 else
6117 CASE_LOW_SEEN (label) = 0;
6119 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6120 not set, that means that CASE_HIGH did not appear when we did our
6121 enum->case scan. Reset our scratch bit after. */
6122 if (CASE_HIGH (label))
6124 if (!CASE_HIGH_SEEN (label))
6125 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6126 else
6127 CASE_HIGH_SEEN (label) = 0;
6130 return 0;
6133 /* Handle -Wswitch*. Called from the front end after parsing the
6134 switch construct. */
6135 /* ??? Should probably be somewhere generic, since other languages
6136 besides C and C++ would want this. At the moment, however, C/C++
6137 are the only tree-ssa languages that support enumerations at all,
6138 so the point is moot. */
6140 void
6141 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6142 tree type, tree cond)
6144 splay_tree_node default_node;
6145 splay_tree_node node;
6146 tree chain;
6148 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6149 return;
6151 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6152 if (!default_node)
6153 warning_at (switch_location, OPT_Wswitch_default,
6154 "switch missing default case");
6156 /* From here on, we only care about about enumerated types. */
6157 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6158 return;
6160 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6161 if (!warn_switch_enum && !warn_switch)
6162 return;
6164 /* Check the cases. Warn about case values which are not members of
6165 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6166 there is no default case, check that exactly all enumeration
6167 literals are covered by the cases. */
6169 /* Clearing COND if it is not an integer constant simplifies
6170 the tests inside the loop below. */
6171 if (TREE_CODE (cond) != INTEGER_CST)
6172 cond = NULL_TREE;
6174 /* The time complexity here is O(N*lg(N)) worst case, but for the
6175 common case of monotonically increasing enumerators, it is
6176 O(N), since the nature of the splay tree will keep the next
6177 element adjacent to the root at all times. */
6179 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6181 tree value = TREE_VALUE (chain);
6182 if (TREE_CODE (value) == CONST_DECL)
6183 value = DECL_INITIAL (value);
6184 node = splay_tree_lookup (cases, (splay_tree_key) value);
6185 if (node)
6187 /* Mark the CASE_LOW part of the case entry as seen. */
6188 tree label = (tree) node->value;
6189 CASE_LOW_SEEN (label) = 1;
6190 continue;
6193 /* Even though there wasn't an exact match, there might be a
6194 case range which includes the enumerator's value. */
6195 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6196 if (node && CASE_HIGH ((tree) node->value))
6198 tree label = (tree) node->value;
6199 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6200 if (cmp >= 0)
6202 /* If we match the upper bound exactly, mark the CASE_HIGH
6203 part of the case entry as seen. */
6204 if (cmp == 0)
6205 CASE_HIGH_SEEN (label) = 1;
6206 continue;
6210 /* We've now determined that this enumerated literal isn't
6211 handled by the case labels of the switch statement. */
6213 /* If the switch expression is a constant, we only really care
6214 about whether that constant is handled by the switch. */
6215 if (cond && tree_int_cst_compare (cond, value))
6216 continue;
6218 /* If there is a default_node, the only relevant option is
6219 Wswitch-enum. Otherwise, if both are enabled then we prefer
6220 to warn using -Wswitch because -Wswitch is enabled by -Wall
6221 while -Wswitch-enum is explicit. */
6222 warning_at (switch_location,
6223 (default_node || !warn_switch
6224 ? OPT_Wswitch_enum
6225 : OPT_Wswitch),
6226 "enumeration value %qE not handled in switch",
6227 TREE_PURPOSE (chain));
6230 /* Warn if there are case expressions that don't correspond to
6231 enumerators. This can occur since C and C++ don't enforce
6232 type-checking of assignments to enumeration variables.
6234 The time complexity here is now always O(N) worst case, since
6235 we should have marked both the lower bound and upper bound of
6236 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6237 above. This scan also resets those fields. */
6239 splay_tree_foreach (cases, match_case_to_enum, type);
6242 /* Finish an expression taking the address of LABEL (an
6243 IDENTIFIER_NODE). Returns an expression for the address.
6245 LOC is the location for the expression returned. */
6247 tree
6248 finish_label_address_expr (tree label, location_t loc)
6250 tree result;
6252 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6254 if (label == error_mark_node)
6255 return error_mark_node;
6257 label = lookup_label (label);
6258 if (label == NULL_TREE)
6259 result = null_pointer_node;
6260 else
6262 TREE_USED (label) = 1;
6263 result = build1 (ADDR_EXPR, ptr_type_node, label);
6264 /* The current function is not necessarily uninlinable.
6265 Computed gotos are incompatible with inlining, but the value
6266 here could be used only in a diagnostic, for example. */
6267 protected_set_expr_location (result, loc);
6270 return result;
6274 /* Given a boolean expression ARG, return a tree representing an increment
6275 or decrement (as indicated by CODE) of ARG. The front end must check for
6276 invalid cases (e.g., decrement in C++). */
6277 tree
6278 boolean_increment (enum tree_code code, tree arg)
6280 tree val;
6281 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6283 arg = stabilize_reference (arg);
6284 switch (code)
6286 case PREINCREMENT_EXPR:
6287 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6288 break;
6289 case POSTINCREMENT_EXPR:
6290 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6291 arg = save_expr (arg);
6292 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6293 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6294 break;
6295 case PREDECREMENT_EXPR:
6296 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6297 invert_truthvalue_loc (input_location, arg));
6298 break;
6299 case POSTDECREMENT_EXPR:
6300 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6301 invert_truthvalue_loc (input_location, arg));
6302 arg = save_expr (arg);
6303 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6304 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6305 break;
6306 default:
6307 gcc_unreachable ();
6309 TREE_SIDE_EFFECTS (val) = 1;
6310 return val;
6313 /* Built-in macros for stddef.h and stdint.h, that require macros
6314 defined in this file. */
6315 void
6316 c_stddef_cpp_builtins(void)
6318 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6319 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6320 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6321 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6322 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6323 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6324 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6325 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6326 if (SIG_ATOMIC_TYPE)
6327 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6328 if (INT8_TYPE)
6329 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6330 if (INT16_TYPE)
6331 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6332 if (INT32_TYPE)
6333 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6334 if (INT64_TYPE)
6335 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6336 if (UINT8_TYPE)
6337 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6338 if (UINT16_TYPE)
6339 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6340 if (UINT32_TYPE)
6341 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6342 if (UINT64_TYPE)
6343 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6344 if (INT_LEAST8_TYPE)
6345 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6346 if (INT_LEAST16_TYPE)
6347 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6348 if (INT_LEAST32_TYPE)
6349 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6350 if (INT_LEAST64_TYPE)
6351 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6352 if (UINT_LEAST8_TYPE)
6353 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6354 if (UINT_LEAST16_TYPE)
6355 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6356 if (UINT_LEAST32_TYPE)
6357 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6358 if (UINT_LEAST64_TYPE)
6359 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6360 if (INT_FAST8_TYPE)
6361 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6362 if (INT_FAST16_TYPE)
6363 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6364 if (INT_FAST32_TYPE)
6365 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6366 if (INT_FAST64_TYPE)
6367 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6368 if (UINT_FAST8_TYPE)
6369 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6370 if (UINT_FAST16_TYPE)
6371 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6372 if (UINT_FAST32_TYPE)
6373 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6374 if (UINT_FAST64_TYPE)
6375 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6376 if (INTPTR_TYPE)
6377 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6378 if (UINTPTR_TYPE)
6379 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6382 static void
6383 c_init_attributes (void)
6385 /* Fill in the built_in_attributes array. */
6386 #define DEF_ATTR_NULL_TREE(ENUM) \
6387 built_in_attributes[(int) ENUM] = NULL_TREE;
6388 #define DEF_ATTR_INT(ENUM, VALUE) \
6389 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6390 #define DEF_ATTR_STRING(ENUM, VALUE) \
6391 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6392 #define DEF_ATTR_IDENT(ENUM, STRING) \
6393 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6394 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6395 built_in_attributes[(int) ENUM] \
6396 = tree_cons (built_in_attributes[(int) PURPOSE], \
6397 built_in_attributes[(int) VALUE], \
6398 built_in_attributes[(int) CHAIN]);
6399 #include "builtin-attrs.def"
6400 #undef DEF_ATTR_NULL_TREE
6401 #undef DEF_ATTR_INT
6402 #undef DEF_ATTR_IDENT
6403 #undef DEF_ATTR_TREE_LIST
6406 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6407 identifier as an argument, so the front end shouldn't look it up. */
6409 bool
6410 attribute_takes_identifier_p (const_tree attr_id)
6412 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6413 if (spec == NULL)
6414 /* Unknown attribute that we'll end up ignoring, return true so we
6415 don't complain about an identifier argument. */
6416 return true;
6417 else if (!strcmp ("mode", spec->name)
6418 || !strcmp ("format", spec->name)
6419 || !strcmp ("cleanup", spec->name))
6420 return true;
6421 else
6422 return targetm.attribute_takes_identifier_p (attr_id);
6425 /* Attribute handlers common to C front ends. */
6427 /* Handle a "packed" attribute; arguments as in
6428 struct attribute_spec.handler. */
6430 static tree
6431 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6432 int flags, bool *no_add_attrs)
6434 if (TYPE_P (*node))
6436 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6437 *node = build_variant_type_copy (*node);
6438 TYPE_PACKED (*node) = 1;
6440 else if (TREE_CODE (*node) == FIELD_DECL)
6442 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6443 /* Still pack bitfields. */
6444 && ! DECL_INITIAL (*node))
6445 warning (OPT_Wattributes,
6446 "%qE attribute ignored for field of type %qT",
6447 name, TREE_TYPE (*node));
6448 else
6449 DECL_PACKED (*node) = 1;
6451 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6452 used for DECL_REGISTER. It wouldn't mean anything anyway.
6453 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6454 that changes what the typedef is typing. */
6455 else
6457 warning (OPT_Wattributes, "%qE attribute ignored", name);
6458 *no_add_attrs = true;
6461 return NULL_TREE;
6464 /* Handle a "nocommon" attribute; arguments as in
6465 struct attribute_spec.handler. */
6467 static tree
6468 handle_nocommon_attribute (tree *node, tree name,
6469 tree ARG_UNUSED (args),
6470 int ARG_UNUSED (flags), bool *no_add_attrs)
6472 if (TREE_CODE (*node) == VAR_DECL)
6473 DECL_COMMON (*node) = 0;
6474 else
6476 warning (OPT_Wattributes, "%qE attribute ignored", name);
6477 *no_add_attrs = true;
6480 return NULL_TREE;
6483 /* Handle a "common" attribute; arguments as in
6484 struct attribute_spec.handler. */
6486 static tree
6487 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6488 int ARG_UNUSED (flags), bool *no_add_attrs)
6490 if (TREE_CODE (*node) == VAR_DECL)
6491 DECL_COMMON (*node) = 1;
6492 else
6494 warning (OPT_Wattributes, "%qE attribute ignored", name);
6495 *no_add_attrs = true;
6498 return NULL_TREE;
6501 /* Handle a "noreturn" attribute; arguments as in
6502 struct attribute_spec.handler. */
6504 static tree
6505 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6506 int ARG_UNUSED (flags), bool *no_add_attrs)
6508 tree type = TREE_TYPE (*node);
6510 /* See FIXME comment in c_common_attribute_table. */
6511 if (TREE_CODE (*node) == FUNCTION_DECL
6512 || objc_method_decl (TREE_CODE (*node)))
6513 TREE_THIS_VOLATILE (*node) = 1;
6514 else if (TREE_CODE (type) == POINTER_TYPE
6515 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6516 TREE_TYPE (*node)
6517 = build_pointer_type
6518 (build_type_variant (TREE_TYPE (type),
6519 TYPE_READONLY (TREE_TYPE (type)), 1));
6520 else
6522 warning (OPT_Wattributes, "%qE attribute ignored", name);
6523 *no_add_attrs = true;
6526 return NULL_TREE;
6529 /* Handle a "hot" and attribute; arguments as in
6530 struct attribute_spec.handler. */
6532 static tree
6533 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6534 int ARG_UNUSED (flags), bool *no_add_attrs)
6536 if (TREE_CODE (*node) == FUNCTION_DECL
6537 || TREE_CODE (*node) == LABEL_DECL)
6539 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6541 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6542 name, "cold");
6543 *no_add_attrs = true;
6545 /* Most of the rest of the hot processing is done later with
6546 lookup_attribute. */
6548 else
6550 warning (OPT_Wattributes, "%qE attribute ignored", name);
6551 *no_add_attrs = true;
6554 return NULL_TREE;
6557 /* Handle a "cold" and attribute; arguments as in
6558 struct attribute_spec.handler. */
6560 static tree
6561 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6562 int ARG_UNUSED (flags), bool *no_add_attrs)
6564 if (TREE_CODE (*node) == FUNCTION_DECL
6565 || TREE_CODE (*node) == LABEL_DECL)
6567 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6569 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6570 name, "hot");
6571 *no_add_attrs = true;
6573 /* Most of the rest of the cold processing is done later with
6574 lookup_attribute. */
6576 else
6578 warning (OPT_Wattributes, "%qE attribute ignored", name);
6579 *no_add_attrs = true;
6582 return NULL_TREE;
6585 /* Handle a "no_sanitize_address" attribute; arguments as in
6586 struct attribute_spec.handler. */
6588 static tree
6589 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6590 bool *no_add_attrs)
6592 if (TREE_CODE (*node) != FUNCTION_DECL)
6594 warning (OPT_Wattributes, "%qE attribute ignored", name);
6595 *no_add_attrs = true;
6598 return NULL_TREE;
6601 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6602 struct attribute_spec.handler. */
6604 static tree
6605 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6606 bool *no_add_attrs)
6608 if (TREE_CODE (*node) != FUNCTION_DECL)
6609 warning (OPT_Wattributes, "%qE attribute ignored", name);
6610 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6611 DECL_ATTRIBUTES (*node)
6612 = tree_cons (get_identifier ("no_sanitize_address"),
6613 NULL_TREE, DECL_ATTRIBUTES (*node));
6614 *no_add_attrs = true;
6615 return NULL_TREE;
6618 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6619 struct attribute_spec.handler. */
6621 static tree
6622 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6623 bool *no_add_attrs)
6625 if (TREE_CODE (*node) != FUNCTION_DECL)
6627 warning (OPT_Wattributes, "%qE attribute ignored", name);
6628 *no_add_attrs = true;
6631 return NULL_TREE;
6634 /* Handle a "noinline" attribute; arguments as in
6635 struct attribute_spec.handler. */
6637 static tree
6638 handle_noinline_attribute (tree *node, tree name,
6639 tree ARG_UNUSED (args),
6640 int ARG_UNUSED (flags), bool *no_add_attrs)
6642 if (TREE_CODE (*node) == FUNCTION_DECL)
6643 DECL_UNINLINABLE (*node) = 1;
6644 else
6646 warning (OPT_Wattributes, "%qE attribute ignored", name);
6647 *no_add_attrs = true;
6650 return NULL_TREE;
6653 /* Handle a "noclone" attribute; arguments as in
6654 struct attribute_spec.handler. */
6656 static tree
6657 handle_noclone_attribute (tree *node, tree name,
6658 tree ARG_UNUSED (args),
6659 int ARG_UNUSED (flags), bool *no_add_attrs)
6661 if (TREE_CODE (*node) != FUNCTION_DECL)
6663 warning (OPT_Wattributes, "%qE attribute ignored", name);
6664 *no_add_attrs = true;
6667 return NULL_TREE;
6670 /* Handle a "always_inline" attribute; arguments as in
6671 struct attribute_spec.handler. */
6673 static tree
6674 handle_always_inline_attribute (tree *node, tree name,
6675 tree ARG_UNUSED (args),
6676 int ARG_UNUSED (flags),
6677 bool *no_add_attrs)
6679 if (TREE_CODE (*node) == FUNCTION_DECL)
6681 /* Set the attribute and mark it for disregarding inline
6682 limits. */
6683 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6685 else
6687 warning (OPT_Wattributes, "%qE attribute ignored", name);
6688 *no_add_attrs = true;
6691 return NULL_TREE;
6694 /* Handle a "gnu_inline" attribute; arguments as in
6695 struct attribute_spec.handler. */
6697 static tree
6698 handle_gnu_inline_attribute (tree *node, tree name,
6699 tree ARG_UNUSED (args),
6700 int ARG_UNUSED (flags),
6701 bool *no_add_attrs)
6703 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6705 /* Do nothing else, just set the attribute. We'll get at
6706 it later with lookup_attribute. */
6708 else
6710 warning (OPT_Wattributes, "%qE attribute ignored", name);
6711 *no_add_attrs = true;
6714 return NULL_TREE;
6717 /* Handle a "leaf" attribute; arguments as in
6718 struct attribute_spec.handler. */
6720 static tree
6721 handle_leaf_attribute (tree *node, tree name,
6722 tree ARG_UNUSED (args),
6723 int ARG_UNUSED (flags), bool *no_add_attrs)
6725 if (TREE_CODE (*node) != FUNCTION_DECL)
6727 warning (OPT_Wattributes, "%qE attribute ignored", name);
6728 *no_add_attrs = true;
6730 if (!TREE_PUBLIC (*node))
6732 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6733 *no_add_attrs = true;
6736 return NULL_TREE;
6739 /* Handle an "artificial" attribute; arguments as in
6740 struct attribute_spec.handler. */
6742 static tree
6743 handle_artificial_attribute (tree *node, tree name,
6744 tree ARG_UNUSED (args),
6745 int ARG_UNUSED (flags),
6746 bool *no_add_attrs)
6748 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6750 /* Do nothing else, just set the attribute. We'll get at
6751 it later with lookup_attribute. */
6753 else
6755 warning (OPT_Wattributes, "%qE attribute ignored", name);
6756 *no_add_attrs = true;
6759 return NULL_TREE;
6762 /* Handle a "flatten" attribute; arguments as in
6763 struct attribute_spec.handler. */
6765 static tree
6766 handle_flatten_attribute (tree *node, tree name,
6767 tree args ATTRIBUTE_UNUSED,
6768 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6770 if (TREE_CODE (*node) == FUNCTION_DECL)
6771 /* Do nothing else, just set the attribute. We'll get at
6772 it later with lookup_attribute. */
6774 else
6776 warning (OPT_Wattributes, "%qE attribute ignored", name);
6777 *no_add_attrs = true;
6780 return NULL_TREE;
6783 /* Handle a "warning" or "error" attribute; arguments as in
6784 struct attribute_spec.handler. */
6786 static tree
6787 handle_error_attribute (tree *node, tree name, tree args,
6788 int ARG_UNUSED (flags), bool *no_add_attrs)
6790 if (TREE_CODE (*node) == FUNCTION_DECL
6791 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6792 /* Do nothing else, just set the attribute. We'll get at
6793 it later with lookup_attribute. */
6795 else
6797 warning (OPT_Wattributes, "%qE attribute ignored", name);
6798 *no_add_attrs = true;
6801 return NULL_TREE;
6804 /* Handle a "used" attribute; arguments as in
6805 struct attribute_spec.handler. */
6807 static tree
6808 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6809 int ARG_UNUSED (flags), bool *no_add_attrs)
6811 tree node = *pnode;
6813 if (TREE_CODE (node) == FUNCTION_DECL
6814 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6815 || (TREE_CODE (node) == TYPE_DECL))
6817 TREE_USED (node) = 1;
6818 DECL_PRESERVE_P (node) = 1;
6819 if (TREE_CODE (node) == VAR_DECL)
6820 DECL_READ_P (node) = 1;
6822 else
6824 warning (OPT_Wattributes, "%qE attribute ignored", name);
6825 *no_add_attrs = true;
6828 return NULL_TREE;
6831 /* Handle a "unused" attribute; arguments as in
6832 struct attribute_spec.handler. */
6834 static tree
6835 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6836 int flags, bool *no_add_attrs)
6838 if (DECL_P (*node))
6840 tree decl = *node;
6842 if (TREE_CODE (decl) == PARM_DECL
6843 || TREE_CODE (decl) == VAR_DECL
6844 || TREE_CODE (decl) == FUNCTION_DECL
6845 || TREE_CODE (decl) == LABEL_DECL
6846 || TREE_CODE (decl) == TYPE_DECL)
6848 TREE_USED (decl) = 1;
6849 if (TREE_CODE (decl) == VAR_DECL
6850 || TREE_CODE (decl) == PARM_DECL)
6851 DECL_READ_P (decl) = 1;
6853 else
6855 warning (OPT_Wattributes, "%qE attribute ignored", name);
6856 *no_add_attrs = true;
6859 else
6861 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6862 *node = build_variant_type_copy (*node);
6863 TREE_USED (*node) = 1;
6866 return NULL_TREE;
6869 /* Handle a "externally_visible" attribute; arguments as in
6870 struct attribute_spec.handler. */
6872 static tree
6873 handle_externally_visible_attribute (tree *pnode, tree name,
6874 tree ARG_UNUSED (args),
6875 int ARG_UNUSED (flags),
6876 bool *no_add_attrs)
6878 tree node = *pnode;
6880 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6882 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6883 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6885 warning (OPT_Wattributes,
6886 "%qE attribute have effect only on public objects", name);
6887 *no_add_attrs = true;
6890 else
6892 warning (OPT_Wattributes, "%qE attribute ignored", name);
6893 *no_add_attrs = true;
6896 return NULL_TREE;
6899 /* Handle a "const" attribute; arguments as in
6900 struct attribute_spec.handler. */
6902 static tree
6903 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6904 int ARG_UNUSED (flags), bool *no_add_attrs)
6906 tree type = TREE_TYPE (*node);
6908 /* See FIXME comment on noreturn in c_common_attribute_table. */
6909 if (TREE_CODE (*node) == FUNCTION_DECL)
6910 TREE_READONLY (*node) = 1;
6911 else if (TREE_CODE (type) == POINTER_TYPE
6912 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6913 TREE_TYPE (*node)
6914 = build_pointer_type
6915 (build_type_variant (TREE_TYPE (type), 1,
6916 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6917 else
6919 warning (OPT_Wattributes, "%qE attribute ignored", name);
6920 *no_add_attrs = true;
6923 return NULL_TREE;
6926 /* Handle a "transparent_union" attribute; arguments as in
6927 struct attribute_spec.handler. */
6929 static tree
6930 handle_transparent_union_attribute (tree *node, tree name,
6931 tree ARG_UNUSED (args), int flags,
6932 bool *no_add_attrs)
6934 tree type;
6936 *no_add_attrs = true;
6939 if (TREE_CODE (*node) == TYPE_DECL
6940 && ! (flags & ATTR_FLAG_CXX11))
6941 node = &TREE_TYPE (*node);
6942 type = *node;
6944 if (TREE_CODE (type) == UNION_TYPE)
6946 /* Make sure that the first field will work for a transparent union.
6947 If the type isn't complete yet, leave the check to the code in
6948 finish_struct. */
6949 if (TYPE_SIZE (type))
6951 tree first = first_field (type);
6952 if (first == NULL_TREE
6953 || DECL_ARTIFICIAL (first)
6954 || TYPE_MODE (type) != DECL_MODE (first))
6955 goto ignored;
6958 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6960 /* If the type isn't complete yet, setting the flag
6961 on a variant wouldn't ever be checked. */
6962 if (!TYPE_SIZE (type))
6963 goto ignored;
6965 /* build_duplicate_type doesn't work for C++. */
6966 if (c_dialect_cxx ())
6967 goto ignored;
6969 /* A type variant isn't good enough, since we don't a cast
6970 to such a type removed as a no-op. */
6971 *node = type = build_duplicate_type (type);
6974 TYPE_TRANSPARENT_AGGR (type) = 1;
6975 return NULL_TREE;
6978 ignored:
6979 warning (OPT_Wattributes, "%qE attribute ignored", name);
6980 return NULL_TREE;
6983 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6984 get the requested priority for a constructor or destructor,
6985 possibly issuing diagnostics for invalid or reserved
6986 priorities. */
6988 static priority_type
6989 get_priority (tree args, bool is_destructor)
6991 HOST_WIDE_INT pri;
6992 tree arg;
6994 if (!args)
6995 return DEFAULT_INIT_PRIORITY;
6997 if (!SUPPORTS_INIT_PRIORITY)
6999 if (is_destructor)
7000 error ("destructor priorities are not supported");
7001 else
7002 error ("constructor priorities are not supported");
7003 return DEFAULT_INIT_PRIORITY;
7006 arg = TREE_VALUE (args);
7007 arg = default_conversion (arg);
7008 if (!host_integerp (arg, /*pos=*/0)
7009 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7010 goto invalid;
7012 pri = tree_low_cst (arg, /*pos=*/0);
7013 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7014 goto invalid;
7016 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7018 if (is_destructor)
7019 warning (0,
7020 "destructor priorities from 0 to %d are reserved "
7021 "for the implementation",
7022 MAX_RESERVED_INIT_PRIORITY);
7023 else
7024 warning (0,
7025 "constructor priorities from 0 to %d are reserved "
7026 "for the implementation",
7027 MAX_RESERVED_INIT_PRIORITY);
7029 return pri;
7031 invalid:
7032 if (is_destructor)
7033 error ("destructor priorities must be integers from 0 to %d inclusive",
7034 MAX_INIT_PRIORITY);
7035 else
7036 error ("constructor priorities must be integers from 0 to %d inclusive",
7037 MAX_INIT_PRIORITY);
7038 return DEFAULT_INIT_PRIORITY;
7041 /* Handle a "constructor" attribute; arguments as in
7042 struct attribute_spec.handler. */
7044 static tree
7045 handle_constructor_attribute (tree *node, tree name, tree args,
7046 int ARG_UNUSED (flags),
7047 bool *no_add_attrs)
7049 tree decl = *node;
7050 tree type = TREE_TYPE (decl);
7052 if (TREE_CODE (decl) == FUNCTION_DECL
7053 && TREE_CODE (type) == FUNCTION_TYPE
7054 && decl_function_context (decl) == 0)
7056 priority_type priority;
7057 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7058 priority = get_priority (args, /*is_destructor=*/false);
7059 SET_DECL_INIT_PRIORITY (decl, priority);
7060 TREE_USED (decl) = 1;
7062 else
7064 warning (OPT_Wattributes, "%qE attribute ignored", name);
7065 *no_add_attrs = true;
7068 return NULL_TREE;
7071 /* Handle a "destructor" attribute; arguments as in
7072 struct attribute_spec.handler. */
7074 static tree
7075 handle_destructor_attribute (tree *node, tree name, tree args,
7076 int ARG_UNUSED (flags),
7077 bool *no_add_attrs)
7079 tree decl = *node;
7080 tree type = TREE_TYPE (decl);
7082 if (TREE_CODE (decl) == FUNCTION_DECL
7083 && TREE_CODE (type) == FUNCTION_TYPE
7084 && decl_function_context (decl) == 0)
7086 priority_type priority;
7087 DECL_STATIC_DESTRUCTOR (decl) = 1;
7088 priority = get_priority (args, /*is_destructor=*/true);
7089 SET_DECL_FINI_PRIORITY (decl, priority);
7090 TREE_USED (decl) = 1;
7092 else
7094 warning (OPT_Wattributes, "%qE attribute ignored", name);
7095 *no_add_attrs = true;
7098 return NULL_TREE;
7101 /* Nonzero if the mode is a valid vector mode for this architecture.
7102 This returns nonzero even if there is no hardware support for the
7103 vector mode, but we can emulate with narrower modes. */
7105 static int
7106 vector_mode_valid_p (enum machine_mode mode)
7108 enum mode_class mclass = GET_MODE_CLASS (mode);
7109 enum machine_mode innermode;
7111 /* Doh! What's going on? */
7112 if (mclass != MODE_VECTOR_INT
7113 && mclass != MODE_VECTOR_FLOAT
7114 && mclass != MODE_VECTOR_FRACT
7115 && mclass != MODE_VECTOR_UFRACT
7116 && mclass != MODE_VECTOR_ACCUM
7117 && mclass != MODE_VECTOR_UACCUM)
7118 return 0;
7120 /* Hardware support. Woo hoo! */
7121 if (targetm.vector_mode_supported_p (mode))
7122 return 1;
7124 innermode = GET_MODE_INNER (mode);
7126 /* We should probably return 1 if requesting V4DI and we have no DI,
7127 but we have V2DI, but this is probably very unlikely. */
7129 /* If we have support for the inner mode, we can safely emulate it.
7130 We may not have V2DI, but me can emulate with a pair of DIs. */
7131 return targetm.scalar_mode_supported_p (innermode);
7135 /* Handle a "mode" attribute; arguments as in
7136 struct attribute_spec.handler. */
7138 static tree
7139 handle_mode_attribute (tree *node, tree name, tree args,
7140 int ARG_UNUSED (flags), bool *no_add_attrs)
7142 tree type = *node;
7143 tree ident = TREE_VALUE (args);
7145 *no_add_attrs = true;
7147 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7148 warning (OPT_Wattributes, "%qE attribute ignored", name);
7149 else
7151 int j;
7152 const char *p = IDENTIFIER_POINTER (ident);
7153 int len = strlen (p);
7154 enum machine_mode mode = VOIDmode;
7155 tree typefm;
7156 bool valid_mode;
7158 if (len > 4 && p[0] == '_' && p[1] == '_'
7159 && p[len - 1] == '_' && p[len - 2] == '_')
7161 char *newp = (char *) alloca (len - 1);
7163 strcpy (newp, &p[2]);
7164 newp[len - 4] = '\0';
7165 p = newp;
7168 /* Change this type to have a type with the specified mode.
7169 First check for the special modes. */
7170 if (!strcmp (p, "byte"))
7171 mode = byte_mode;
7172 else if (!strcmp (p, "word"))
7173 mode = word_mode;
7174 else if (!strcmp (p, "pointer"))
7175 mode = ptr_mode;
7176 else if (!strcmp (p, "libgcc_cmp_return"))
7177 mode = targetm.libgcc_cmp_return_mode ();
7178 else if (!strcmp (p, "libgcc_shift_count"))
7179 mode = targetm.libgcc_shift_count_mode ();
7180 else if (!strcmp (p, "unwind_word"))
7181 mode = targetm.unwind_word_mode ();
7182 else
7183 for (j = 0; j < NUM_MACHINE_MODES; j++)
7184 if (!strcmp (p, GET_MODE_NAME (j)))
7186 mode = (enum machine_mode) j;
7187 break;
7190 if (mode == VOIDmode)
7192 error ("unknown machine mode %qE", ident);
7193 return NULL_TREE;
7196 valid_mode = false;
7197 switch (GET_MODE_CLASS (mode))
7199 case MODE_INT:
7200 case MODE_PARTIAL_INT:
7201 case MODE_FLOAT:
7202 case MODE_DECIMAL_FLOAT:
7203 case MODE_FRACT:
7204 case MODE_UFRACT:
7205 case MODE_ACCUM:
7206 case MODE_UACCUM:
7207 valid_mode = targetm.scalar_mode_supported_p (mode);
7208 break;
7210 case MODE_COMPLEX_INT:
7211 case MODE_COMPLEX_FLOAT:
7212 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7213 break;
7215 case MODE_VECTOR_INT:
7216 case MODE_VECTOR_FLOAT:
7217 case MODE_VECTOR_FRACT:
7218 case MODE_VECTOR_UFRACT:
7219 case MODE_VECTOR_ACCUM:
7220 case MODE_VECTOR_UACCUM:
7221 warning (OPT_Wattributes, "specifying vector types with "
7222 "__attribute__ ((mode)) is deprecated");
7223 warning (OPT_Wattributes,
7224 "use __attribute__ ((vector_size)) instead");
7225 valid_mode = vector_mode_valid_p (mode);
7226 break;
7228 default:
7229 break;
7231 if (!valid_mode)
7233 error ("unable to emulate %qs", p);
7234 return NULL_TREE;
7237 if (POINTER_TYPE_P (type))
7239 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7240 tree (*fn)(tree, enum machine_mode, bool);
7242 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7244 error ("invalid pointer mode %qs", p);
7245 return NULL_TREE;
7248 if (TREE_CODE (type) == POINTER_TYPE)
7249 fn = build_pointer_type_for_mode;
7250 else
7251 fn = build_reference_type_for_mode;
7252 typefm = fn (TREE_TYPE (type), mode, false);
7254 else
7256 /* For fixed-point modes, we need to test if the signness of type
7257 and the machine mode are consistent. */
7258 if (ALL_FIXED_POINT_MODE_P (mode)
7259 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7261 error ("signedness of type and machine mode %qs don%'t match", p);
7262 return NULL_TREE;
7264 /* For fixed-point modes, we need to pass saturating info. */
7265 typefm = lang_hooks.types.type_for_mode (mode,
7266 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7267 : TYPE_UNSIGNED (type));
7270 if (typefm == NULL_TREE)
7272 error ("no data type for mode %qs", p);
7273 return NULL_TREE;
7275 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7277 /* For enumeral types, copy the precision from the integer
7278 type returned above. If not an INTEGER_TYPE, we can't use
7279 this mode for this type. */
7280 if (TREE_CODE (typefm) != INTEGER_TYPE)
7282 error ("cannot use mode %qs for enumeral types", p);
7283 return NULL_TREE;
7286 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7288 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7289 typefm = type;
7291 else
7293 /* We cannot build a type variant, as there's code that assumes
7294 that TYPE_MAIN_VARIANT has the same mode. This includes the
7295 debug generators. Instead, create a subrange type. This
7296 results in all of the enumeral values being emitted only once
7297 in the original, and the subtype gets them by reference. */
7298 if (TYPE_UNSIGNED (type))
7299 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7300 else
7301 typefm = make_signed_type (TYPE_PRECISION (typefm));
7302 TREE_TYPE (typefm) = type;
7305 else if (VECTOR_MODE_P (mode)
7306 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7307 : TREE_CODE (type) != TREE_CODE (typefm))
7309 error ("mode %qs applied to inappropriate type", p);
7310 return NULL_TREE;
7313 *node = typefm;
7316 return NULL_TREE;
7319 /* Handle a "section" attribute; arguments as in
7320 struct attribute_spec.handler. */
7322 static tree
7323 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7324 int ARG_UNUSED (flags), bool *no_add_attrs)
7326 tree decl = *node;
7328 if (targetm_common.have_named_sections)
7330 user_defined_section_attribute = true;
7332 if ((TREE_CODE (decl) == FUNCTION_DECL
7333 || TREE_CODE (decl) == VAR_DECL)
7334 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7336 if (TREE_CODE (decl) == VAR_DECL
7337 && current_function_decl != NULL_TREE
7338 && !TREE_STATIC (decl))
7340 error_at (DECL_SOURCE_LOCATION (decl),
7341 "section attribute cannot be specified for "
7342 "local variables");
7343 *no_add_attrs = true;
7346 /* The decl may have already been given a section attribute
7347 from a previous declaration. Ensure they match. */
7348 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7349 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7350 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7352 error ("section of %q+D conflicts with previous declaration",
7353 *node);
7354 *no_add_attrs = true;
7356 else if (TREE_CODE (decl) == VAR_DECL
7357 && !targetm.have_tls && targetm.emutls.tmpl_section
7358 && DECL_THREAD_LOCAL_P (decl))
7360 error ("section of %q+D cannot be overridden", *node);
7361 *no_add_attrs = true;
7363 else
7364 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7366 else
7368 error ("section attribute not allowed for %q+D", *node);
7369 *no_add_attrs = true;
7372 else
7374 error_at (DECL_SOURCE_LOCATION (*node),
7375 "section attributes are not supported for this target");
7376 *no_add_attrs = true;
7379 return NULL_TREE;
7382 /* Check whether ALIGN is a valid user-specified alignment. If so,
7383 return its base-2 log; if not, output an error and return -1. If
7384 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7385 no error. */
7387 check_user_alignment (const_tree align, bool allow_zero)
7389 int i;
7391 if (TREE_CODE (align) != INTEGER_CST
7392 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7394 error ("requested alignment is not an integer constant");
7395 return -1;
7397 else if (allow_zero && integer_zerop (align))
7398 return -1;
7399 else if (tree_int_cst_sgn (align) == -1
7400 || (i = tree_log2 (align)) == -1)
7402 error ("requested alignment is not a positive power of 2");
7403 return -1;
7405 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7407 error ("requested alignment is too large");
7408 return -1;
7410 return i;
7414 If in c++-11, check if the c++-11 alignment constraint with respect
7415 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7416 c++-11 mode, does nothing.
7418 [dcl.align]2/ says:
7420 [* if the constant expression evaluates to a fundamental alignment,
7421 the alignment requirement of the declared entity shall be the
7422 specified fundamental alignment.
7424 * if the constant expression evaluates to an extended alignment
7425 and the implementation supports that alignment in the context
7426 of the declaration, the alignment of the declared entity shall
7427 be that alignment
7429 * if the constant expression evaluates to an extended alignment
7430 and the implementation does not support that alignment in the
7431 context of the declaration, the program is ill-formed]. */
7433 static bool
7434 check_cxx_fundamental_alignment_constraints (tree node,
7435 unsigned align_log,
7436 int flags)
7438 bool alignment_too_large_p = false;
7439 unsigned requested_alignment = 1U << align_log;
7440 unsigned max_align = 0;
7442 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7443 || (node == NULL_TREE || node == error_mark_node))
7444 return true;
7446 if (cxx_fundamental_alignment_p (requested_alignment))
7447 return true;
7449 if (DECL_P (node))
7451 if (TREE_STATIC (node))
7453 /* For file scope variables and static members, the target
7454 supports alignments that are at most
7455 MAX_OFILE_ALIGNMENT. */
7456 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7457 alignment_too_large_p = true;
7459 else
7461 #ifdef BIGGEST_FIELD_ALIGNMENT
7462 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7463 #else
7464 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7465 #endif
7466 /* For non-static members, the target supports either
7467 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7468 if it is defined or BIGGEST_ALIGNMENT. */
7469 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7470 if (TREE_CODE (node) == FIELD_DECL
7471 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7472 alignment_too_large_p = true;
7473 #undef MAX_TARGET_FIELD_ALIGNMENT
7474 /* For stack variables, the target supports at most
7475 MAX_STACK_ALIGNMENT. */
7476 else if (decl_function_context (node) != NULL
7477 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7478 alignment_too_large_p = true;
7481 else if (TYPE_P (node))
7483 /* Let's be liberal for types. */
7484 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7485 alignment_too_large_p = true;
7488 if (alignment_too_large_p)
7489 pedwarn (input_location, OPT_Wattributes,
7490 "requested alignment %d is larger than %d",
7491 requested_alignment, max_align);
7493 return !alignment_too_large_p;
7496 /* Handle a "aligned" attribute; arguments as in
7497 struct attribute_spec.handler. */
7499 static tree
7500 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7501 int flags, bool *no_add_attrs)
7503 tree decl = NULL_TREE;
7504 tree *type = NULL;
7505 int is_type = 0;
7506 tree align_expr = (args ? TREE_VALUE (args)
7507 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7508 int i;
7510 if (DECL_P (*node))
7512 decl = *node;
7513 type = &TREE_TYPE (decl);
7514 is_type = TREE_CODE (*node) == TYPE_DECL;
7516 else if (TYPE_P (*node))
7517 type = node, is_type = 1;
7519 if ((i = check_user_alignment (align_expr, false)) == -1
7520 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7521 *no_add_attrs = true;
7522 else if (is_type)
7524 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7525 /* OK, modify the type in place. */;
7526 /* If we have a TYPE_DECL, then copy the type, so that we
7527 don't accidentally modify a builtin type. See pushdecl. */
7528 else if (decl && TREE_TYPE (decl) != error_mark_node
7529 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7531 tree tt = TREE_TYPE (decl);
7532 *type = build_variant_type_copy (*type);
7533 DECL_ORIGINAL_TYPE (decl) = tt;
7534 TYPE_NAME (*type) = decl;
7535 TREE_USED (*type) = TREE_USED (decl);
7536 TREE_TYPE (decl) = *type;
7538 else
7539 *type = build_variant_type_copy (*type);
7541 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7542 TYPE_USER_ALIGN (*type) = 1;
7544 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7545 && TREE_CODE (decl) != FIELD_DECL)
7547 error ("alignment may not be specified for %q+D", decl);
7548 *no_add_attrs = true;
7550 else if (DECL_USER_ALIGN (decl)
7551 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7552 /* C++-11 [dcl.align/4]:
7554 When multiple alignment-specifiers are specified for an
7555 entity, the alignment requirement shall be set to the
7556 strictest specified alignment.
7558 This formally comes from the c++11 specification but we are
7559 doing it for the GNU attribute syntax as well. */
7560 *no_add_attrs = true;
7561 else if (TREE_CODE (decl) == FUNCTION_DECL
7562 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7564 if (DECL_USER_ALIGN (decl))
7565 error ("alignment for %q+D was previously specified as %d "
7566 "and may not be decreased", decl,
7567 DECL_ALIGN (decl) / BITS_PER_UNIT);
7568 else
7569 error ("alignment for %q+D must be at least %d", decl,
7570 DECL_ALIGN (decl) / BITS_PER_UNIT);
7571 *no_add_attrs = true;
7573 else
7575 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7576 DECL_USER_ALIGN (decl) = 1;
7579 return NULL_TREE;
7582 /* Handle a "weak" attribute; arguments as in
7583 struct attribute_spec.handler. */
7585 static tree
7586 handle_weak_attribute (tree *node, tree name,
7587 tree ARG_UNUSED (args),
7588 int ARG_UNUSED (flags),
7589 bool * ARG_UNUSED (no_add_attrs))
7591 if (TREE_CODE (*node) == FUNCTION_DECL
7592 && DECL_DECLARED_INLINE_P (*node))
7594 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7595 *no_add_attrs = true;
7597 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7599 error ("indirect function %q+D cannot be declared weak", *node);
7600 *no_add_attrs = true;
7601 return NULL_TREE;
7603 else if (TREE_CODE (*node) == FUNCTION_DECL
7604 || TREE_CODE (*node) == VAR_DECL)
7605 declare_weak (*node);
7606 else
7607 warning (OPT_Wattributes, "%qE attribute ignored", name);
7609 return NULL_TREE;
7612 /* Handle an "alias" or "ifunc" attribute; arguments as in
7613 struct attribute_spec.handler, except that IS_ALIAS tells us
7614 whether this is an alias as opposed to ifunc attribute. */
7616 static tree
7617 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7618 bool *no_add_attrs)
7620 tree decl = *node;
7622 if (TREE_CODE (decl) != FUNCTION_DECL
7623 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7625 warning (OPT_Wattributes, "%qE attribute ignored", name);
7626 *no_add_attrs = true;
7628 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7629 || (TREE_CODE (decl) != FUNCTION_DECL
7630 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7631 /* A static variable declaration is always a tentative definition,
7632 but the alias is a non-tentative definition which overrides. */
7633 || (TREE_CODE (decl) != FUNCTION_DECL
7634 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7636 error ("%q+D defined both normally and as %qE attribute", decl, name);
7637 *no_add_attrs = true;
7638 return NULL_TREE;
7640 else if (!is_alias
7641 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7642 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7644 error ("weak %q+D cannot be defined %qE", decl, name);
7645 *no_add_attrs = true;
7646 return NULL_TREE;
7649 /* Note that the very first time we process a nested declaration,
7650 decl_function_context will not be set. Indeed, *would* never
7651 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7652 we do below. After such frobbery, pushdecl would set the context.
7653 In any case, this is never what we want. */
7654 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7656 tree id;
7658 id = TREE_VALUE (args);
7659 if (TREE_CODE (id) != STRING_CST)
7661 error ("attribute %qE argument not a string", name);
7662 *no_add_attrs = true;
7663 return NULL_TREE;
7665 id = get_identifier (TREE_STRING_POINTER (id));
7666 /* This counts as a use of the object pointed to. */
7667 TREE_USED (id) = 1;
7669 if (TREE_CODE (decl) == FUNCTION_DECL)
7670 DECL_INITIAL (decl) = error_mark_node;
7671 else
7672 TREE_STATIC (decl) = 1;
7674 if (!is_alias)
7675 /* ifuncs are also aliases, so set that attribute too. */
7676 DECL_ATTRIBUTES (decl)
7677 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7679 else
7681 warning (OPT_Wattributes, "%qE attribute ignored", name);
7682 *no_add_attrs = true;
7685 return NULL_TREE;
7688 /* Handle an "alias" or "ifunc" attribute; arguments as in
7689 struct attribute_spec.handler. */
7691 static tree
7692 handle_ifunc_attribute (tree *node, tree name, tree args,
7693 int ARG_UNUSED (flags), bool *no_add_attrs)
7695 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7698 /* Handle an "alias" or "ifunc" attribute; arguments as in
7699 struct attribute_spec.handler. */
7701 static tree
7702 handle_alias_attribute (tree *node, tree name, tree args,
7703 int ARG_UNUSED (flags), bool *no_add_attrs)
7705 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7708 /* Handle a "weakref" attribute; arguments as in struct
7709 attribute_spec.handler. */
7711 static tree
7712 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7713 int flags, bool *no_add_attrs)
7715 tree attr = NULL_TREE;
7717 /* We must ignore the attribute when it is associated with
7718 local-scoped decls, since attribute alias is ignored and many
7719 such symbols do not even have a DECL_WEAK field. */
7720 if (decl_function_context (*node)
7721 || current_function_decl
7722 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7724 warning (OPT_Wattributes, "%qE attribute ignored", name);
7725 *no_add_attrs = true;
7726 return NULL_TREE;
7729 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7731 error ("indirect function %q+D cannot be declared weakref", *node);
7732 *no_add_attrs = true;
7733 return NULL_TREE;
7736 /* The idea here is that `weakref("name")' mutates into `weakref,
7737 alias("name")', and weakref without arguments, in turn,
7738 implicitly adds weak. */
7740 if (args)
7742 attr = tree_cons (get_identifier ("alias"), args, attr);
7743 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7745 *no_add_attrs = true;
7747 decl_attributes (node, attr, flags);
7749 else
7751 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7752 error_at (DECL_SOURCE_LOCATION (*node),
7753 "weakref attribute must appear before alias attribute");
7755 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7756 and that isn't supported; and because it wants to add it to
7757 the list of weak decls, which isn't helpful. */
7758 DECL_WEAK (*node) = 1;
7761 return NULL_TREE;
7764 /* Handle an "visibility" attribute; arguments as in
7765 struct attribute_spec.handler. */
7767 static tree
7768 handle_visibility_attribute (tree *node, tree name, tree args,
7769 int ARG_UNUSED (flags),
7770 bool *ARG_UNUSED (no_add_attrs))
7772 tree decl = *node;
7773 tree id = TREE_VALUE (args);
7774 enum symbol_visibility vis;
7776 if (TYPE_P (*node))
7778 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7779 /* OK */;
7780 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7782 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7783 name);
7784 return NULL_TREE;
7786 else if (TYPE_FIELDS (*node))
7788 error ("%qE attribute ignored because %qT is already defined",
7789 name, *node);
7790 return NULL_TREE;
7793 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7795 warning (OPT_Wattributes, "%qE attribute ignored", name);
7796 return NULL_TREE;
7799 if (TREE_CODE (id) != STRING_CST)
7801 error ("visibility argument not a string");
7802 return NULL_TREE;
7805 /* If this is a type, set the visibility on the type decl. */
7806 if (TYPE_P (decl))
7808 decl = TYPE_NAME (decl);
7809 if (!decl)
7810 return NULL_TREE;
7811 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7813 warning (OPT_Wattributes, "%qE attribute ignored on types",
7814 name);
7815 return NULL_TREE;
7819 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7820 vis = VISIBILITY_DEFAULT;
7821 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7822 vis = VISIBILITY_INTERNAL;
7823 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7824 vis = VISIBILITY_HIDDEN;
7825 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7826 vis = VISIBILITY_PROTECTED;
7827 else
7829 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7830 vis = VISIBILITY_DEFAULT;
7833 if (DECL_VISIBILITY_SPECIFIED (decl)
7834 && vis != DECL_VISIBILITY (decl))
7836 tree attributes = (TYPE_P (*node)
7837 ? TYPE_ATTRIBUTES (*node)
7838 : DECL_ATTRIBUTES (decl));
7839 if (lookup_attribute ("visibility", attributes))
7840 error ("%qD redeclared with different visibility", decl);
7841 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7842 && lookup_attribute ("dllimport", attributes))
7843 error ("%qD was declared %qs which implies default visibility",
7844 decl, "dllimport");
7845 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7846 && lookup_attribute ("dllexport", attributes))
7847 error ("%qD was declared %qs which implies default visibility",
7848 decl, "dllexport");
7851 DECL_VISIBILITY (decl) = vis;
7852 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7854 /* Go ahead and attach the attribute to the node as well. This is needed
7855 so we can determine whether we have VISIBILITY_DEFAULT because the
7856 visibility was not specified, or because it was explicitly overridden
7857 from the containing scope. */
7859 return NULL_TREE;
7862 /* Determine the ELF symbol visibility for DECL, which is either a
7863 variable or a function. It is an error to use this function if a
7864 definition of DECL is not available in this translation unit.
7865 Returns true if the final visibility has been determined by this
7866 function; false if the caller is free to make additional
7867 modifications. */
7869 bool
7870 c_determine_visibility (tree decl)
7872 gcc_assert (TREE_CODE (decl) == VAR_DECL
7873 || TREE_CODE (decl) == FUNCTION_DECL);
7875 /* If the user explicitly specified the visibility with an
7876 attribute, honor that. DECL_VISIBILITY will have been set during
7877 the processing of the attribute. We check for an explicit
7878 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7879 to distinguish the use of an attribute from the use of a "#pragma
7880 GCC visibility push(...)"; in the latter case we still want other
7881 considerations to be able to overrule the #pragma. */
7882 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7883 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7884 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7885 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7886 return true;
7888 /* Set default visibility to whatever the user supplied with
7889 visibility_specified depending on #pragma GCC visibility. */
7890 if (!DECL_VISIBILITY_SPECIFIED (decl))
7892 if (visibility_options.inpragma
7893 || DECL_VISIBILITY (decl) != default_visibility)
7895 DECL_VISIBILITY (decl) = default_visibility;
7896 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7897 /* If visibility changed and DECL already has DECL_RTL, ensure
7898 symbol flags are updated. */
7899 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7900 || TREE_CODE (decl) == FUNCTION_DECL)
7901 && DECL_RTL_SET_P (decl))
7902 make_decl_rtl (decl);
7905 return false;
7908 /* Handle an "tls_model" attribute; arguments as in
7909 struct attribute_spec.handler. */
7911 static tree
7912 handle_tls_model_attribute (tree *node, tree name, tree args,
7913 int ARG_UNUSED (flags), bool *no_add_attrs)
7915 tree id;
7916 tree decl = *node;
7917 enum tls_model kind;
7919 *no_add_attrs = true;
7921 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7923 warning (OPT_Wattributes, "%qE attribute ignored", name);
7924 return NULL_TREE;
7927 kind = DECL_TLS_MODEL (decl);
7928 id = TREE_VALUE (args);
7929 if (TREE_CODE (id) != STRING_CST)
7931 error ("tls_model argument not a string");
7932 return NULL_TREE;
7935 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7936 kind = TLS_MODEL_LOCAL_EXEC;
7937 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7938 kind = TLS_MODEL_INITIAL_EXEC;
7939 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7940 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7941 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7942 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7943 else
7944 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7946 DECL_TLS_MODEL (decl) = kind;
7947 return NULL_TREE;
7950 /* Handle a "no_instrument_function" attribute; arguments as in
7951 struct attribute_spec.handler. */
7953 static tree
7954 handle_no_instrument_function_attribute (tree *node, tree name,
7955 tree ARG_UNUSED (args),
7956 int ARG_UNUSED (flags),
7957 bool *no_add_attrs)
7959 tree decl = *node;
7961 if (TREE_CODE (decl) != FUNCTION_DECL)
7963 error_at (DECL_SOURCE_LOCATION (decl),
7964 "%qE attribute applies only to functions", name);
7965 *no_add_attrs = true;
7967 else if (DECL_INITIAL (decl))
7969 error_at (DECL_SOURCE_LOCATION (decl),
7970 "can%'t set %qE attribute after definition", name);
7971 *no_add_attrs = true;
7973 else
7974 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7976 return NULL_TREE;
7979 /* Handle a "malloc" attribute; arguments as in
7980 struct attribute_spec.handler. */
7982 static tree
7983 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7984 int ARG_UNUSED (flags), bool *no_add_attrs)
7986 if (TREE_CODE (*node) == FUNCTION_DECL
7987 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7988 DECL_IS_MALLOC (*node) = 1;
7989 else
7991 warning (OPT_Wattributes, "%qE attribute ignored", name);
7992 *no_add_attrs = true;
7995 return NULL_TREE;
7998 /* Handle a "alloc_size" attribute; arguments as in
7999 struct attribute_spec.handler. */
8001 static tree
8002 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8003 int ARG_UNUSED (flags), bool *no_add_attrs)
8005 unsigned arg_count = type_num_arguments (*node);
8006 for (; args; args = TREE_CHAIN (args))
8008 tree position = TREE_VALUE (args);
8010 if (TREE_CODE (position) != INTEGER_CST
8011 || TREE_INT_CST_HIGH (position)
8012 || TREE_INT_CST_LOW (position) < 1
8013 || TREE_INT_CST_LOW (position) > arg_count )
8015 warning (OPT_Wattributes,
8016 "alloc_size parameter outside range");
8017 *no_add_attrs = true;
8018 return NULL_TREE;
8021 return NULL_TREE;
8024 /* Handle a "fn spec" attribute; arguments as in
8025 struct attribute_spec.handler. */
8027 static tree
8028 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8029 tree args, int ARG_UNUSED (flags),
8030 bool *no_add_attrs ATTRIBUTE_UNUSED)
8032 gcc_assert (args
8033 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8034 && !TREE_CHAIN (args));
8035 return NULL_TREE;
8038 /* Handle a "bnd_variable_size" attribute; arguments as in
8039 struct attribute_spec.handler. */
8041 static tree
8042 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8043 int ARG_UNUSED (flags), bool *no_add_attrs)
8045 if (TREE_CODE (*node) != FIELD_DECL)
8047 warning (OPT_Wattributes, "%qE attribute ignored", name);
8048 *no_add_attrs = true;
8051 return NULL_TREE;
8054 /* Handle a "bnd_legacy" attribute; arguments as in
8055 struct attribute_spec.handler. */
8057 static tree
8058 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8059 int ARG_UNUSED (flags), bool *no_add_attrs)
8061 if (TREE_CODE (*node) != FUNCTION_DECL)
8063 warning (OPT_Wattributes, "%qE attribute ignored", name);
8064 *no_add_attrs = true;
8067 return NULL_TREE;
8070 /* Handle a "warn_unused" attribute; arguments as in
8071 struct attribute_spec.handler. */
8073 static tree
8074 handle_warn_unused_attribute (tree *node, tree name,
8075 tree args ATTRIBUTE_UNUSED,
8076 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8078 if (TYPE_P (*node))
8079 /* Do nothing else, just set the attribute. We'll get at
8080 it later with lookup_attribute. */
8082 else
8084 warning (OPT_Wattributes, "%qE attribute ignored", name);
8085 *no_add_attrs = true;
8088 return NULL_TREE;
8091 /* Handle an "omp declare simd" attribute; arguments as in
8092 struct attribute_spec.handler. */
8094 static tree
8095 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8097 return NULL_TREE;
8100 /* Handle an "omp declare target" attribute; arguments as in
8101 struct attribute_spec.handler. */
8103 static tree
8104 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8106 return NULL_TREE;
8109 /* Handle a "returns_twice" attribute; arguments as in
8110 struct attribute_spec.handler. */
8112 static tree
8113 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8114 int ARG_UNUSED (flags), bool *no_add_attrs)
8116 if (TREE_CODE (*node) == FUNCTION_DECL)
8117 DECL_IS_RETURNS_TWICE (*node) = 1;
8118 else
8120 warning (OPT_Wattributes, "%qE attribute ignored", name);
8121 *no_add_attrs = true;
8124 return NULL_TREE;
8127 /* Handle a "no_limit_stack" attribute; arguments as in
8128 struct attribute_spec.handler. */
8130 static tree
8131 handle_no_limit_stack_attribute (tree *node, tree name,
8132 tree ARG_UNUSED (args),
8133 int ARG_UNUSED (flags),
8134 bool *no_add_attrs)
8136 tree decl = *node;
8138 if (TREE_CODE (decl) != FUNCTION_DECL)
8140 error_at (DECL_SOURCE_LOCATION (decl),
8141 "%qE attribute applies only to functions", name);
8142 *no_add_attrs = true;
8144 else if (DECL_INITIAL (decl))
8146 error_at (DECL_SOURCE_LOCATION (decl),
8147 "can%'t set %qE attribute after definition", name);
8148 *no_add_attrs = true;
8150 else
8151 DECL_NO_LIMIT_STACK (decl) = 1;
8153 return NULL_TREE;
8156 /* Handle a "pure" attribute; arguments as in
8157 struct attribute_spec.handler. */
8159 static tree
8160 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8161 int ARG_UNUSED (flags), bool *no_add_attrs)
8163 if (TREE_CODE (*node) == FUNCTION_DECL)
8164 DECL_PURE_P (*node) = 1;
8165 /* ??? TODO: Support types. */
8166 else
8168 warning (OPT_Wattributes, "%qE attribute ignored", name);
8169 *no_add_attrs = true;
8172 return NULL_TREE;
8175 /* Digest an attribute list destined for a transactional memory statement.
8176 ALLOWED is the set of attributes that are allowed for this statement;
8177 return the attribute we parsed. Multiple attributes are never allowed. */
8180 parse_tm_stmt_attr (tree attrs, int allowed)
8182 tree a_seen = NULL;
8183 int m_seen = 0;
8185 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8187 tree a = TREE_PURPOSE (attrs);
8188 int m = 0;
8190 if (is_attribute_p ("outer", a))
8191 m = TM_STMT_ATTR_OUTER;
8193 if ((m & allowed) == 0)
8195 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8196 continue;
8199 if (m_seen == 0)
8201 a_seen = a;
8202 m_seen = m;
8204 else if (m_seen == m)
8205 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8206 else
8207 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8210 return m_seen;
8213 /* Transform a TM attribute name into a maskable integer and back.
8214 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8215 to how the lack of an attribute is treated. */
8218 tm_attr_to_mask (tree attr)
8220 if (attr == NULL)
8221 return 0;
8222 if (is_attribute_p ("transaction_safe", attr))
8223 return TM_ATTR_SAFE;
8224 if (is_attribute_p ("transaction_callable", attr))
8225 return TM_ATTR_CALLABLE;
8226 if (is_attribute_p ("transaction_pure", attr))
8227 return TM_ATTR_PURE;
8228 if (is_attribute_p ("transaction_unsafe", attr))
8229 return TM_ATTR_IRREVOCABLE;
8230 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8231 return TM_ATTR_MAY_CANCEL_OUTER;
8232 return 0;
8235 tree
8236 tm_mask_to_attr (int mask)
8238 const char *str;
8239 switch (mask)
8241 case TM_ATTR_SAFE:
8242 str = "transaction_safe";
8243 break;
8244 case TM_ATTR_CALLABLE:
8245 str = "transaction_callable";
8246 break;
8247 case TM_ATTR_PURE:
8248 str = "transaction_pure";
8249 break;
8250 case TM_ATTR_IRREVOCABLE:
8251 str = "transaction_unsafe";
8252 break;
8253 case TM_ATTR_MAY_CANCEL_OUTER:
8254 str = "transaction_may_cancel_outer";
8255 break;
8256 default:
8257 gcc_unreachable ();
8259 return get_identifier (str);
8262 /* Return the first TM attribute seen in LIST. */
8264 tree
8265 find_tm_attribute (tree list)
8267 for (; list ; list = TREE_CHAIN (list))
8269 tree name = TREE_PURPOSE (list);
8270 if (tm_attr_to_mask (name) != 0)
8271 return name;
8273 return NULL_TREE;
8276 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8277 Here we accept only function types, and verify that none of the other
8278 function TM attributes are also applied. */
8279 /* ??? We need to accept class types for C++, but not C. This greatly
8280 complicates this function, since we can no longer rely on the extra
8281 processing given by function_type_required. */
8283 static tree
8284 handle_tm_attribute (tree *node, tree name, tree args,
8285 int flags, bool *no_add_attrs)
8287 /* Only one path adds the attribute; others don't. */
8288 *no_add_attrs = true;
8290 switch (TREE_CODE (*node))
8292 case RECORD_TYPE:
8293 case UNION_TYPE:
8294 /* Only tm_callable and tm_safe apply to classes. */
8295 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8296 goto ignored;
8297 /* FALLTHRU */
8299 case FUNCTION_TYPE:
8300 case METHOD_TYPE:
8302 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8303 if (old_name == name)
8305 else if (old_name != NULL_TREE)
8306 error ("type was previously declared %qE", old_name);
8307 else
8308 *no_add_attrs = false;
8310 break;
8312 case POINTER_TYPE:
8314 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8315 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8317 tree fn_tmp = TREE_TYPE (*node);
8318 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8319 *node = build_pointer_type (fn_tmp);
8320 break;
8323 /* FALLTHRU */
8325 default:
8326 /* If a function is next, pass it on to be tried next. */
8327 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8328 return tree_cons (name, args, NULL);
8330 ignored:
8331 warning (OPT_Wattributes, "%qE attribute ignored", name);
8332 break;
8335 return NULL_TREE;
8338 /* Handle the TM_WRAP attribute; arguments as in
8339 struct attribute_spec.handler. */
8341 static tree
8342 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8343 int ARG_UNUSED (flags), bool *no_add_attrs)
8345 tree decl = *node;
8347 /* We don't need the attribute even on success, since we
8348 record the entry in an external table. */
8349 *no_add_attrs = true;
8351 if (TREE_CODE (decl) != FUNCTION_DECL)
8352 warning (OPT_Wattributes, "%qE attribute ignored", name);
8353 else
8355 tree wrap_decl = TREE_VALUE (args);
8356 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8357 && TREE_CODE (wrap_decl) != VAR_DECL
8358 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8359 error ("%qE argument not an identifier", name);
8360 else
8362 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8363 wrap_decl = lookup_name (wrap_decl);
8364 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8366 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8367 TREE_TYPE (wrap_decl)))
8368 record_tm_replacement (wrap_decl, decl);
8369 else
8370 error ("%qD is not compatible with %qD", wrap_decl, decl);
8372 else
8373 error ("transaction_wrap argument is not a function");
8377 return NULL_TREE;
8380 /* Ignore the given attribute. Used when this attribute may be usefully
8381 overridden by the target, but is not used generically. */
8383 static tree
8384 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8385 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8386 bool *no_add_attrs)
8388 *no_add_attrs = true;
8389 return NULL_TREE;
8392 /* Handle a "no vops" attribute; arguments as in
8393 struct attribute_spec.handler. */
8395 static tree
8396 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8397 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8398 bool *ARG_UNUSED (no_add_attrs))
8400 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8401 DECL_IS_NOVOPS (*node) = 1;
8402 return NULL_TREE;
8405 /* Handle a "deprecated" attribute; arguments as in
8406 struct attribute_spec.handler. */
8408 static tree
8409 handle_deprecated_attribute (tree *node, tree name,
8410 tree args, int flags,
8411 bool *no_add_attrs)
8413 tree type = NULL_TREE;
8414 int warn = 0;
8415 tree what = NULL_TREE;
8417 if (!args)
8418 *no_add_attrs = true;
8419 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8421 error ("deprecated message is not a string");
8422 *no_add_attrs = true;
8425 if (DECL_P (*node))
8427 tree decl = *node;
8428 type = TREE_TYPE (decl);
8430 if (TREE_CODE (decl) == TYPE_DECL
8431 || TREE_CODE (decl) == PARM_DECL
8432 || TREE_CODE (decl) == VAR_DECL
8433 || TREE_CODE (decl) == FUNCTION_DECL
8434 || TREE_CODE (decl) == FIELD_DECL
8435 || objc_method_decl (TREE_CODE (decl)))
8436 TREE_DEPRECATED (decl) = 1;
8437 else
8438 warn = 1;
8440 else if (TYPE_P (*node))
8442 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8443 *node = build_variant_type_copy (*node);
8444 TREE_DEPRECATED (*node) = 1;
8445 type = *node;
8447 else
8448 warn = 1;
8450 if (warn)
8452 *no_add_attrs = true;
8453 if (type && TYPE_NAME (type))
8455 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8456 what = TYPE_NAME (*node);
8457 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8458 && DECL_NAME (TYPE_NAME (type)))
8459 what = DECL_NAME (TYPE_NAME (type));
8461 if (what)
8462 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8463 else
8464 warning (OPT_Wattributes, "%qE attribute ignored", name);
8467 return NULL_TREE;
8470 /* Handle a "vector_size" attribute; arguments as in
8471 struct attribute_spec.handler. */
8473 static tree
8474 handle_vector_size_attribute (tree *node, tree name, tree args,
8475 int ARG_UNUSED (flags),
8476 bool *no_add_attrs)
8478 unsigned HOST_WIDE_INT vecsize, nunits;
8479 enum machine_mode orig_mode;
8480 tree type = *node, new_type, size;
8482 *no_add_attrs = true;
8484 size = TREE_VALUE (args);
8486 if (!host_integerp (size, 1))
8488 warning (OPT_Wattributes, "%qE attribute ignored", name);
8489 return NULL_TREE;
8492 /* Get the vector size (in bytes). */
8493 vecsize = tree_low_cst (size, 1);
8495 /* We need to provide for vector pointers, vector arrays, and
8496 functions returning vectors. For example:
8498 __attribute__((vector_size(16))) short *foo;
8500 In this case, the mode is SI, but the type being modified is
8501 HI, so we need to look further. */
8503 while (POINTER_TYPE_P (type)
8504 || TREE_CODE (type) == FUNCTION_TYPE
8505 || TREE_CODE (type) == METHOD_TYPE
8506 || TREE_CODE (type) == ARRAY_TYPE
8507 || TREE_CODE (type) == OFFSET_TYPE)
8508 type = TREE_TYPE (type);
8510 /* Get the mode of the type being modified. */
8511 orig_mode = TYPE_MODE (type);
8513 if ((!INTEGRAL_TYPE_P (type)
8514 && !SCALAR_FLOAT_TYPE_P (type)
8515 && !FIXED_POINT_TYPE_P (type))
8516 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8517 && GET_MODE_CLASS (orig_mode) != MODE_INT
8518 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8519 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8520 || TREE_CODE (type) == BOOLEAN_TYPE)
8522 error ("invalid vector type for attribute %qE", name);
8523 return NULL_TREE;
8526 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8528 error ("vector size not an integral multiple of component size");
8529 return NULL;
8532 if (vecsize == 0)
8534 error ("zero vector size");
8535 return NULL;
8538 /* Calculate how many units fit in the vector. */
8539 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8540 if (nunits & (nunits - 1))
8542 error ("number of components of the vector not a power of two");
8543 return NULL_TREE;
8546 new_type = build_vector_type (type, nunits);
8548 /* Build back pointers if needed. */
8549 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8551 return NULL_TREE;
8554 /* Handle the "nonnull" attribute. */
8555 static tree
8556 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8557 tree args, int ARG_UNUSED (flags),
8558 bool *no_add_attrs)
8560 tree type = *node;
8561 unsigned HOST_WIDE_INT attr_arg_num;
8563 /* If no arguments are specified, all pointer arguments should be
8564 non-null. Verify a full prototype is given so that the arguments
8565 will have the correct types when we actually check them later. */
8566 if (!args)
8568 if (!prototype_p (type))
8570 error ("nonnull attribute without arguments on a non-prototype");
8571 *no_add_attrs = true;
8573 return NULL_TREE;
8576 /* Argument list specified. Verify that each argument number references
8577 a pointer argument. */
8578 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8580 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8582 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8584 error ("nonnull argument has invalid operand number (argument %lu)",
8585 (unsigned long) attr_arg_num);
8586 *no_add_attrs = true;
8587 return NULL_TREE;
8590 if (prototype_p (type))
8592 function_args_iterator iter;
8593 tree argument;
8595 function_args_iter_init (&iter, type);
8596 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8598 argument = function_args_iter_cond (&iter);
8599 if (argument == NULL_TREE || ck_num == arg_num)
8600 break;
8603 if (!argument
8604 || TREE_CODE (argument) == VOID_TYPE)
8606 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8607 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8608 *no_add_attrs = true;
8609 return NULL_TREE;
8612 if (TREE_CODE (argument) != POINTER_TYPE)
8614 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8615 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8616 *no_add_attrs = true;
8617 return NULL_TREE;
8622 return NULL_TREE;
8625 /* Check the argument list of a function call for null in argument slots
8626 that are marked as requiring a non-null pointer argument. The NARGS
8627 arguments are passed in the array ARGARRAY.
8630 static void
8631 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8633 tree a;
8634 int i;
8636 attrs = lookup_attribute ("nonnull", attrs);
8637 if (attrs == NULL_TREE)
8638 return;
8640 a = attrs;
8641 /* See if any of the nonnull attributes has no arguments. If so,
8642 then every pointer argument is checked (in which case the check
8643 for pointer type is done in check_nonnull_arg). */
8644 if (TREE_VALUE (a) != NULL_TREE)
8646 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8647 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8649 if (a != NULL_TREE)
8650 for (i = 0; i < nargs; i++)
8651 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8652 i + 1);
8653 else
8655 /* Walk the argument list. If we encounter an argument number we
8656 should check for non-null, do it. */
8657 for (i = 0; i < nargs; i++)
8659 for (a = attrs; ; a = TREE_CHAIN (a))
8661 a = lookup_attribute ("nonnull", a);
8662 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8663 break;
8666 if (a != NULL_TREE)
8667 check_function_arguments_recurse (check_nonnull_arg, NULL,
8668 argarray[i], i + 1);
8673 /* Check that the Nth argument of a function call (counting backwards
8674 from the end) is a (pointer)0. The NARGS arguments are passed in the
8675 array ARGARRAY. */
8677 static void
8678 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8680 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8682 if (attr)
8684 int len = 0;
8685 int pos = 0;
8686 tree sentinel;
8687 function_args_iterator iter;
8688 tree t;
8690 /* Skip over the named arguments. */
8691 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8693 if (len == nargs)
8694 break;
8695 len++;
8698 if (TREE_VALUE (attr))
8700 tree p = TREE_VALUE (TREE_VALUE (attr));
8701 pos = TREE_INT_CST_LOW (p);
8704 /* The sentinel must be one of the varargs, i.e.
8705 in position >= the number of fixed arguments. */
8706 if ((nargs - 1 - pos) < len)
8708 warning (OPT_Wformat_,
8709 "not enough variable arguments to fit a sentinel");
8710 return;
8713 /* Validate the sentinel. */
8714 sentinel = argarray[nargs - 1 - pos];
8715 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8716 || !integer_zerop (sentinel))
8717 /* Although __null (in C++) is only an integer we allow it
8718 nevertheless, as we are guaranteed that it's exactly
8719 as wide as a pointer, and we don't want to force
8720 users to cast the NULL they have written there.
8721 We warn with -Wstrict-null-sentinel, though. */
8722 && (warn_strict_null_sentinel || null_node != sentinel))
8723 warning (OPT_Wformat_, "missing sentinel in function call");
8727 /* Helper for check_function_nonnull; given a list of operands which
8728 must be non-null in ARGS, determine if operand PARAM_NUM should be
8729 checked. */
8731 static bool
8732 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8734 unsigned HOST_WIDE_INT arg_num = 0;
8736 for (; args; args = TREE_CHAIN (args))
8738 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8740 gcc_assert (found);
8742 if (arg_num == param_num)
8743 return true;
8745 return false;
8748 /* Check that the function argument PARAM (which is operand number
8749 PARAM_NUM) is non-null. This is called by check_function_nonnull
8750 via check_function_arguments_recurse. */
8752 static void
8753 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8754 unsigned HOST_WIDE_INT param_num)
8756 /* Just skip checking the argument if it's not a pointer. This can
8757 happen if the "nonnull" attribute was given without an operand
8758 list (which means to check every pointer argument). */
8760 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8761 return;
8763 if (integer_zerop (param))
8764 warning (OPT_Wnonnull, "null argument where non-null required "
8765 "(argument %lu)", (unsigned long) param_num);
8768 /* Helper for nonnull attribute handling; fetch the operand number
8769 from the attribute argument list. */
8771 static bool
8772 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8774 /* Verify the arg number is a constant. */
8775 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8776 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8777 return false;
8779 *valp = TREE_INT_CST_LOW (arg_num_expr);
8780 return true;
8783 /* Handle a "nothrow" attribute; arguments as in
8784 struct attribute_spec.handler. */
8786 static tree
8787 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8788 int ARG_UNUSED (flags), bool *no_add_attrs)
8790 if (TREE_CODE (*node) == FUNCTION_DECL)
8791 TREE_NOTHROW (*node) = 1;
8792 /* ??? TODO: Support types. */
8793 else
8795 warning (OPT_Wattributes, "%qE attribute ignored", name);
8796 *no_add_attrs = true;
8799 return NULL_TREE;
8802 /* Handle a "cleanup" attribute; arguments as in
8803 struct attribute_spec.handler. */
8805 static tree
8806 handle_cleanup_attribute (tree *node, tree name, tree args,
8807 int ARG_UNUSED (flags), bool *no_add_attrs)
8809 tree decl = *node;
8810 tree cleanup_id, cleanup_decl;
8812 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8813 for global destructors in C++. This requires infrastructure that
8814 we don't have generically at the moment. It's also not a feature
8815 we'd be missing too much, since we do have attribute constructor. */
8816 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8818 warning (OPT_Wattributes, "%qE attribute ignored", name);
8819 *no_add_attrs = true;
8820 return NULL_TREE;
8823 /* Verify that the argument is a function in scope. */
8824 /* ??? We could support pointers to functions here as well, if
8825 that was considered desirable. */
8826 cleanup_id = TREE_VALUE (args);
8827 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8829 error ("cleanup argument not an identifier");
8830 *no_add_attrs = true;
8831 return NULL_TREE;
8833 cleanup_decl = lookup_name (cleanup_id);
8834 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8836 error ("cleanup argument not a function");
8837 *no_add_attrs = true;
8838 return NULL_TREE;
8841 /* That the function has proper type is checked with the
8842 eventual call to build_function_call. */
8844 return NULL_TREE;
8847 /* Handle a "warn_unused_result" attribute. No special handling. */
8849 static tree
8850 handle_warn_unused_result_attribute (tree *node, tree name,
8851 tree ARG_UNUSED (args),
8852 int ARG_UNUSED (flags), bool *no_add_attrs)
8854 /* Ignore the attribute for functions not returning any value. */
8855 if (VOID_TYPE_P (TREE_TYPE (*node)))
8857 warning (OPT_Wattributes, "%qE attribute ignored", name);
8858 *no_add_attrs = true;
8861 return NULL_TREE;
8864 /* Handle a "sentinel" attribute. */
8866 static tree
8867 handle_sentinel_attribute (tree *node, tree name, tree args,
8868 int ARG_UNUSED (flags), bool *no_add_attrs)
8870 if (!prototype_p (*node))
8872 warning (OPT_Wattributes,
8873 "%qE attribute requires prototypes with named arguments", name);
8874 *no_add_attrs = true;
8876 else
8878 if (!stdarg_p (*node))
8880 warning (OPT_Wattributes,
8881 "%qE attribute only applies to variadic functions", name);
8882 *no_add_attrs = true;
8886 if (args)
8888 tree position = TREE_VALUE (args);
8890 if (TREE_CODE (position) != INTEGER_CST)
8892 warning (OPT_Wattributes,
8893 "requested position is not an integer constant");
8894 *no_add_attrs = true;
8896 else
8898 if (tree_int_cst_lt (position, integer_zero_node))
8900 warning (OPT_Wattributes,
8901 "requested position is less than zero");
8902 *no_add_attrs = true;
8907 return NULL_TREE;
8910 /* Handle a "type_generic" attribute. */
8912 static tree
8913 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8914 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8915 bool * ARG_UNUSED (no_add_attrs))
8917 /* Ensure we have a function type. */
8918 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8920 /* Ensure we have a variadic function. */
8921 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8923 return NULL_TREE;
8926 /* Handle a "target" attribute. */
8928 static tree
8929 handle_target_attribute (tree *node, tree name, tree args, int flags,
8930 bool *no_add_attrs)
8932 /* Ensure we have a function type. */
8933 if (TREE_CODE (*node) != FUNCTION_DECL)
8935 warning (OPT_Wattributes, "%qE attribute ignored", name);
8936 *no_add_attrs = true;
8938 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8939 flags))
8940 *no_add_attrs = true;
8942 return NULL_TREE;
8945 /* Arguments being collected for optimization. */
8946 typedef const char *const_char_p; /* For DEF_VEC_P. */
8947 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
8950 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8951 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8952 false for #pragma GCC optimize. */
8954 bool
8955 parse_optimize_options (tree args, bool attr_p)
8957 bool ret = true;
8958 unsigned opt_argc;
8959 unsigned i;
8960 int saved_flag_strict_aliasing;
8961 const char **opt_argv;
8962 struct cl_decoded_option *decoded_options;
8963 unsigned int decoded_options_count;
8964 tree ap;
8966 /* Build up argv vector. Just in case the string is stored away, use garbage
8967 collected strings. */
8968 vec_safe_truncate (optimize_args, 0);
8969 vec_safe_push (optimize_args, (const char *) NULL);
8971 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8973 tree value = TREE_VALUE (ap);
8975 if (TREE_CODE (value) == INTEGER_CST)
8977 char buffer[20];
8978 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8979 vec_safe_push (optimize_args, ggc_strdup (buffer));
8982 else if (TREE_CODE (value) == STRING_CST)
8984 /* Split string into multiple substrings. */
8985 size_t len = TREE_STRING_LENGTH (value);
8986 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8987 char *end = p + len;
8988 char *comma;
8989 char *next_p = p;
8991 while (next_p != NULL)
8993 size_t len2;
8994 char *q, *r;
8996 p = next_p;
8997 comma = strchr (p, ',');
8998 if (comma)
9000 len2 = comma - p;
9001 *comma = '\0';
9002 next_p = comma+1;
9004 else
9006 len2 = end - p;
9007 next_p = NULL;
9010 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9012 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9013 options. */
9014 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9016 ret = false;
9017 if (attr_p)
9018 warning (OPT_Wattributes,
9019 "bad option %s to optimize attribute", p);
9020 else
9021 warning (OPT_Wpragmas,
9022 "bad option %s to pragma attribute", p);
9023 continue;
9026 if (*p != '-')
9028 *r++ = '-';
9030 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9031 itself is -Os, and any other switch begins with a -f. */
9032 if ((*p >= '0' && *p <= '9')
9033 || (p[0] == 's' && p[1] == '\0'))
9034 *r++ = 'O';
9035 else if (*p != 'O')
9036 *r++ = 'f';
9039 memcpy (r, p, len2);
9040 r[len2] = '\0';
9041 vec_safe_push (optimize_args, (const char *) q);
9047 opt_argc = optimize_args->length ();
9048 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9050 for (i = 1; i < opt_argc; i++)
9051 opt_argv[i] = (*optimize_args)[i];
9053 saved_flag_strict_aliasing = flag_strict_aliasing;
9055 /* Now parse the options. */
9056 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9057 &decoded_options,
9058 &decoded_options_count);
9059 decode_options (&global_options, &global_options_set,
9060 decoded_options, decoded_options_count,
9061 input_location, global_dc);
9063 targetm.override_options_after_change();
9065 /* Don't allow changing -fstrict-aliasing. */
9066 flag_strict_aliasing = saved_flag_strict_aliasing;
9068 optimize_args->truncate (0);
9069 return ret;
9072 /* For handling "optimize" attribute. arguments as in
9073 struct attribute_spec.handler. */
9075 static tree
9076 handle_optimize_attribute (tree *node, tree name, tree args,
9077 int ARG_UNUSED (flags), bool *no_add_attrs)
9079 /* Ensure we have a function type. */
9080 if (TREE_CODE (*node) != FUNCTION_DECL)
9082 warning (OPT_Wattributes, "%qE attribute ignored", name);
9083 *no_add_attrs = true;
9085 else
9087 struct cl_optimization cur_opts;
9088 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9090 /* Save current options. */
9091 cl_optimization_save (&cur_opts, &global_options);
9093 /* If we previously had some optimization options, use them as the
9094 default. */
9095 if (old_opts)
9096 cl_optimization_restore (&global_options,
9097 TREE_OPTIMIZATION (old_opts));
9099 /* Parse options, and update the vector. */
9100 parse_optimize_options (args, true);
9101 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9102 = build_optimization_node (&global_options);
9104 /* Restore current options. */
9105 cl_optimization_restore (&global_options, &cur_opts);
9108 return NULL_TREE;
9111 /* Handle a "no_split_stack" attribute. */
9113 static tree
9114 handle_no_split_stack_attribute (tree *node, tree name,
9115 tree ARG_UNUSED (args),
9116 int ARG_UNUSED (flags),
9117 bool *no_add_attrs)
9119 tree decl = *node;
9121 if (TREE_CODE (decl) != FUNCTION_DECL)
9123 error_at (DECL_SOURCE_LOCATION (decl),
9124 "%qE attribute applies only to functions", name);
9125 *no_add_attrs = true;
9127 else if (DECL_INITIAL (decl))
9129 error_at (DECL_SOURCE_LOCATION (decl),
9130 "can%'t set %qE attribute after definition", name);
9131 *no_add_attrs = true;
9134 return NULL_TREE;
9137 /* Handle a "returns_nonnull" attribute; arguments as in
9138 struct attribute_spec.handler. */
9140 static tree
9141 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9142 bool *no_add_attrs)
9144 // Even without a prototype we still have a return type we can check.
9145 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9147 error ("returns_nonnull attribute on a function not returning a pointer");
9148 *no_add_attrs = true;
9150 return NULL_TREE;
9154 /* Check for valid arguments being passed to a function with FNTYPE.
9155 There are NARGS arguments in the array ARGARRAY. */
9156 void
9157 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9159 /* Check for null being passed in a pointer argument that must be
9160 non-null. We also need to do this if format checking is enabled. */
9162 if (warn_nonnull)
9163 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9165 /* Check for errors in format strings. */
9167 if (warn_format || warn_suggest_attribute_format)
9168 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9170 if (warn_format)
9171 check_function_sentinel (fntype, nargs, argarray);
9174 /* Generic argument checking recursion routine. PARAM is the argument to
9175 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9176 once the argument is resolved. CTX is context for the callback. */
9177 void
9178 check_function_arguments_recurse (void (*callback)
9179 (void *, tree, unsigned HOST_WIDE_INT),
9180 void *ctx, tree param,
9181 unsigned HOST_WIDE_INT param_num)
9183 if (CONVERT_EXPR_P (param)
9184 && (TYPE_PRECISION (TREE_TYPE (param))
9185 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9187 /* Strip coercion. */
9188 check_function_arguments_recurse (callback, ctx,
9189 TREE_OPERAND (param, 0), param_num);
9190 return;
9193 if (TREE_CODE (param) == CALL_EXPR)
9195 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9196 tree attrs;
9197 bool found_format_arg = false;
9199 /* See if this is a call to a known internationalization function
9200 that modifies a format arg. Such a function may have multiple
9201 format_arg attributes (for example, ngettext). */
9203 for (attrs = TYPE_ATTRIBUTES (type);
9204 attrs;
9205 attrs = TREE_CHAIN (attrs))
9206 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9208 tree inner_arg;
9209 tree format_num_expr;
9210 int format_num;
9211 int i;
9212 call_expr_arg_iterator iter;
9214 /* Extract the argument number, which was previously checked
9215 to be valid. */
9216 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9218 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9219 && !TREE_INT_CST_HIGH (format_num_expr));
9221 format_num = TREE_INT_CST_LOW (format_num_expr);
9223 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9224 inner_arg != 0;
9225 inner_arg = next_call_expr_arg (&iter), i++)
9226 if (i == format_num)
9228 check_function_arguments_recurse (callback, ctx,
9229 inner_arg, param_num);
9230 found_format_arg = true;
9231 break;
9235 /* If we found a format_arg attribute and did a recursive check,
9236 we are done with checking this argument. Otherwise, we continue
9237 and this will be considered a non-literal. */
9238 if (found_format_arg)
9239 return;
9242 if (TREE_CODE (param) == COND_EXPR)
9244 /* Check both halves of the conditional expression. */
9245 check_function_arguments_recurse (callback, ctx,
9246 TREE_OPERAND (param, 1), param_num);
9247 check_function_arguments_recurse (callback, ctx,
9248 TREE_OPERAND (param, 2), param_num);
9249 return;
9252 (*callback) (ctx, param, param_num);
9255 /* Checks for a builtin function FNDECL that the number of arguments
9256 NARGS against the required number REQUIRED and issues an error if
9257 there is a mismatch. Returns true if the number of arguments is
9258 correct, otherwise false. */
9260 static bool
9261 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9263 if (nargs < required)
9265 error_at (input_location,
9266 "not enough arguments to function %qE", fndecl);
9267 return false;
9269 else if (nargs > required)
9271 error_at (input_location,
9272 "too many arguments to function %qE", fndecl);
9273 return false;
9275 return true;
9278 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9279 Returns false if there was an error, otherwise true. */
9281 bool
9282 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9284 if (!DECL_BUILT_IN (fndecl)
9285 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9286 return true;
9288 switch (DECL_FUNCTION_CODE (fndecl))
9290 case BUILT_IN_CONSTANT_P:
9291 return builtin_function_validate_nargs (fndecl, nargs, 1);
9293 case BUILT_IN_ISFINITE:
9294 case BUILT_IN_ISINF:
9295 case BUILT_IN_ISINF_SIGN:
9296 case BUILT_IN_ISNAN:
9297 case BUILT_IN_ISNORMAL:
9298 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9300 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9302 error ("non-floating-point argument in call to "
9303 "function %qE", fndecl);
9304 return false;
9306 return true;
9308 return false;
9310 case BUILT_IN_ISGREATER:
9311 case BUILT_IN_ISGREATEREQUAL:
9312 case BUILT_IN_ISLESS:
9313 case BUILT_IN_ISLESSEQUAL:
9314 case BUILT_IN_ISLESSGREATER:
9315 case BUILT_IN_ISUNORDERED:
9316 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9318 enum tree_code code0, code1;
9319 code0 = TREE_CODE (TREE_TYPE (args[0]));
9320 code1 = TREE_CODE (TREE_TYPE (args[1]));
9321 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9322 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9323 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9325 error ("non-floating-point arguments in call to "
9326 "function %qE", fndecl);
9327 return false;
9329 return true;
9331 return false;
9333 case BUILT_IN_FPCLASSIFY:
9334 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9336 unsigned i;
9338 for (i=0; i<5; i++)
9339 if (TREE_CODE (args[i]) != INTEGER_CST)
9341 error ("non-const integer argument %u in call to function %qE",
9342 i+1, fndecl);
9343 return false;
9346 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9348 error ("non-floating-point argument in call to function %qE",
9349 fndecl);
9350 return false;
9352 return true;
9354 return false;
9356 case BUILT_IN_ASSUME_ALIGNED:
9357 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9359 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9361 error ("non-integer argument 3 in call to function %qE", fndecl);
9362 return false;
9364 return true;
9366 return false;
9368 default:
9369 return true;
9373 /* Function to help qsort sort FIELD_DECLs by name order. */
9376 field_decl_cmp (const void *x_p, const void *y_p)
9378 const tree *const x = (const tree *const) x_p;
9379 const tree *const y = (const tree *const) y_p;
9381 if (DECL_NAME (*x) == DECL_NAME (*y))
9382 /* A nontype is "greater" than a type. */
9383 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9384 if (DECL_NAME (*x) == NULL_TREE)
9385 return -1;
9386 if (DECL_NAME (*y) == NULL_TREE)
9387 return 1;
9388 if (DECL_NAME (*x) < DECL_NAME (*y))
9389 return -1;
9390 return 1;
9393 static struct {
9394 gt_pointer_operator new_value;
9395 void *cookie;
9396 } resort_data;
9398 /* This routine compares two fields like field_decl_cmp but using the
9399 pointer operator in resort_data. */
9401 static int
9402 resort_field_decl_cmp (const void *x_p, const void *y_p)
9404 const tree *const x = (const tree *const) x_p;
9405 const tree *const y = (const tree *const) y_p;
9407 if (DECL_NAME (*x) == DECL_NAME (*y))
9408 /* A nontype is "greater" than a type. */
9409 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9410 if (DECL_NAME (*x) == NULL_TREE)
9411 return -1;
9412 if (DECL_NAME (*y) == NULL_TREE)
9413 return 1;
9415 tree d1 = DECL_NAME (*x);
9416 tree d2 = DECL_NAME (*y);
9417 resort_data.new_value (&d1, resort_data.cookie);
9418 resort_data.new_value (&d2, resort_data.cookie);
9419 if (d1 < d2)
9420 return -1;
9422 return 1;
9425 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9427 void
9428 resort_sorted_fields (void *obj,
9429 void * ARG_UNUSED (orig_obj),
9430 gt_pointer_operator new_value,
9431 void *cookie)
9433 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9434 resort_data.new_value = new_value;
9435 resort_data.cookie = cookie;
9436 qsort (&sf->elts[0], sf->len, sizeof (tree),
9437 resort_field_decl_cmp);
9440 /* Subroutine of c_parse_error.
9441 Return the result of concatenating LHS and RHS. RHS is really
9442 a string literal, its first character is indicated by RHS_START and
9443 RHS_SIZE is its length (including the terminating NUL character).
9445 The caller is responsible for deleting the returned pointer. */
9447 static char *
9448 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9450 const int lhs_size = strlen (lhs);
9451 char *result = XNEWVEC (char, lhs_size + rhs_size);
9452 strncpy (result, lhs, lhs_size);
9453 strncpy (result + lhs_size, rhs_start, rhs_size);
9454 return result;
9457 /* Issue the error given by GMSGID, indicating that it occurred before
9458 TOKEN, which had the associated VALUE. */
9460 void
9461 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9462 tree value, unsigned char token_flags)
9464 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9466 char *message = NULL;
9468 if (token_type == CPP_EOF)
9469 message = catenate_messages (gmsgid, " at end of input");
9470 else if (token_type == CPP_CHAR
9471 || token_type == CPP_WCHAR
9472 || token_type == CPP_CHAR16
9473 || token_type == CPP_CHAR32)
9475 unsigned int val = TREE_INT_CST_LOW (value);
9476 const char *prefix;
9478 switch (token_type)
9480 default:
9481 prefix = "";
9482 break;
9483 case CPP_WCHAR:
9484 prefix = "L";
9485 break;
9486 case CPP_CHAR16:
9487 prefix = "u";
9488 break;
9489 case CPP_CHAR32:
9490 prefix = "U";
9491 break;
9494 if (val <= UCHAR_MAX && ISGRAPH (val))
9495 message = catenate_messages (gmsgid, " before %s'%c'");
9496 else
9497 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9499 error (message, prefix, val);
9500 free (message);
9501 message = NULL;
9503 else if (token_type == CPP_CHAR_USERDEF
9504 || token_type == CPP_WCHAR_USERDEF
9505 || token_type == CPP_CHAR16_USERDEF
9506 || token_type == CPP_CHAR32_USERDEF)
9507 message = catenate_messages (gmsgid,
9508 " before user-defined character literal");
9509 else if (token_type == CPP_STRING_USERDEF
9510 || token_type == CPP_WSTRING_USERDEF
9511 || token_type == CPP_STRING16_USERDEF
9512 || token_type == CPP_STRING32_USERDEF
9513 || token_type == CPP_UTF8STRING_USERDEF)
9514 message = catenate_messages (gmsgid, " before user-defined string literal");
9515 else if (token_type == CPP_STRING
9516 || token_type == CPP_WSTRING
9517 || token_type == CPP_STRING16
9518 || token_type == CPP_STRING32
9519 || token_type == CPP_UTF8STRING)
9520 message = catenate_messages (gmsgid, " before string constant");
9521 else if (token_type == CPP_NUMBER)
9522 message = catenate_messages (gmsgid, " before numeric constant");
9523 else if (token_type == CPP_NAME)
9525 message = catenate_messages (gmsgid, " before %qE");
9526 error (message, value);
9527 free (message);
9528 message = NULL;
9530 else if (token_type == CPP_PRAGMA)
9531 message = catenate_messages (gmsgid, " before %<#pragma%>");
9532 else if (token_type == CPP_PRAGMA_EOL)
9533 message = catenate_messages (gmsgid, " before end of line");
9534 else if (token_type == CPP_DECLTYPE)
9535 message = catenate_messages (gmsgid, " before %<decltype%>");
9536 else if (token_type < N_TTYPES)
9538 message = catenate_messages (gmsgid, " before %qs token");
9539 error (message, cpp_type2name (token_type, token_flags));
9540 free (message);
9541 message = NULL;
9543 else
9544 error (gmsgid);
9546 if (message)
9548 error (message);
9549 free (message);
9551 #undef catenate_messages
9554 /* Mapping for cpp message reasons to the options that enable them. */
9556 struct reason_option_codes_t
9558 const int reason; /* cpplib message reason. */
9559 const int option_code; /* gcc option that controls this message. */
9562 static const struct reason_option_codes_t option_codes[] = {
9563 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9564 {CPP_W_COMMENTS, OPT_Wcomment},
9565 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9566 {CPP_W_MULTICHAR, OPT_Wmultichar},
9567 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9568 {CPP_W_LONG_LONG, OPT_Wlong_long},
9569 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9570 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9571 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9572 {CPP_W_UNDEF, OPT_Wundef},
9573 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9574 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9575 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9576 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9577 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9578 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9579 {CPP_W_DATE_TIME, OPT_Wdate_time},
9580 {CPP_W_NONE, 0}
9583 /* Return the gcc option code associated with the reason for a cpp
9584 message, or 0 if none. */
9586 static int
9587 c_option_controlling_cpp_error (int reason)
9589 const struct reason_option_codes_t *entry;
9591 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9593 if (entry->reason == reason)
9594 return entry->option_code;
9596 return 0;
9599 /* Callback from cpp_error for PFILE to print diagnostics from the
9600 preprocessor. The diagnostic is of type LEVEL, with REASON set
9601 to the reason code if LEVEL is represents a warning, at location
9602 LOCATION unless this is after lexing and the compiler's location
9603 should be used instead, with column number possibly overridden by
9604 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9605 the arguments. Returns true if a diagnostic was emitted, false
9606 otherwise. */
9608 bool
9609 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9610 location_t location, unsigned int column_override,
9611 const char *msg, va_list *ap)
9613 diagnostic_info diagnostic;
9614 diagnostic_t dlevel;
9615 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9616 bool ret;
9618 switch (level)
9620 case CPP_DL_WARNING_SYSHDR:
9621 if (flag_no_output)
9622 return false;
9623 global_dc->dc_warn_system_headers = 1;
9624 /* Fall through. */
9625 case CPP_DL_WARNING:
9626 if (flag_no_output)
9627 return false;
9628 dlevel = DK_WARNING;
9629 break;
9630 case CPP_DL_PEDWARN:
9631 if (flag_no_output && !flag_pedantic_errors)
9632 return false;
9633 dlevel = DK_PEDWARN;
9634 break;
9635 case CPP_DL_ERROR:
9636 dlevel = DK_ERROR;
9637 break;
9638 case CPP_DL_ICE:
9639 dlevel = DK_ICE;
9640 break;
9641 case CPP_DL_NOTE:
9642 dlevel = DK_NOTE;
9643 break;
9644 case CPP_DL_FATAL:
9645 dlevel = DK_FATAL;
9646 break;
9647 default:
9648 gcc_unreachable ();
9650 if (done_lexing)
9651 location = input_location;
9652 diagnostic_set_info_translated (&diagnostic, msg, ap,
9653 location, dlevel);
9654 if (column_override)
9655 diagnostic_override_column (&diagnostic, column_override);
9656 diagnostic_override_option_index (&diagnostic,
9657 c_option_controlling_cpp_error (reason));
9658 ret = report_diagnostic (&diagnostic);
9659 if (level == CPP_DL_WARNING_SYSHDR)
9660 global_dc->dc_warn_system_headers = save_warn_system_headers;
9661 return ret;
9664 /* Convert a character from the host to the target execution character
9665 set. cpplib handles this, mostly. */
9667 HOST_WIDE_INT
9668 c_common_to_target_charset (HOST_WIDE_INT c)
9670 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9671 zero-extended under -fno-signed-char. cpplib insists that characters
9672 and character constants are always unsigned. Hence we must convert
9673 back and forth. */
9674 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9676 uc = cpp_host_to_exec_charset (parse_in, uc);
9678 if (flag_signed_char)
9679 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9680 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9681 else
9682 return uc;
9685 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9686 references with an INDIRECT_REF of a constant at the bottom; much like the
9687 traditional rendering of offsetof as a macro. Return the folded result. */
9689 tree
9690 fold_offsetof_1 (tree expr)
9692 tree base, off, t;
9694 switch (TREE_CODE (expr))
9696 case ERROR_MARK:
9697 return expr;
9699 case VAR_DECL:
9700 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9701 return error_mark_node;
9703 case CALL_EXPR:
9704 case TARGET_EXPR:
9705 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9706 return error_mark_node;
9708 case NOP_EXPR:
9709 case INDIRECT_REF:
9710 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9712 error ("cannot apply %<offsetof%> to a non constant address");
9713 return error_mark_node;
9715 return TREE_OPERAND (expr, 0);
9717 case COMPONENT_REF:
9718 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9719 if (base == error_mark_node)
9720 return base;
9722 t = TREE_OPERAND (expr, 1);
9723 if (DECL_C_BIT_FIELD (t))
9725 error ("attempt to take address of bit-field structure "
9726 "member %qD", t);
9727 return error_mark_node;
9729 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9730 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9732 / BITS_PER_UNIT));
9733 break;
9735 case ARRAY_REF:
9736 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9737 if (base == error_mark_node)
9738 return base;
9740 t = TREE_OPERAND (expr, 1);
9742 /* Check if the offset goes beyond the upper bound of the array. */
9743 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9745 tree upbound = array_ref_up_bound (expr);
9746 if (upbound != NULL_TREE
9747 && TREE_CODE (upbound) == INTEGER_CST
9748 && !tree_int_cst_equal (upbound,
9749 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9751 upbound = size_binop (PLUS_EXPR, upbound,
9752 build_int_cst (TREE_TYPE (upbound), 1));
9753 if (tree_int_cst_lt (upbound, t))
9755 tree v;
9757 for (v = TREE_OPERAND (expr, 0);
9758 TREE_CODE (v) == COMPONENT_REF;
9759 v = TREE_OPERAND (v, 0))
9760 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9761 == RECORD_TYPE)
9763 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9764 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9765 if (TREE_CODE (fld_chain) == FIELD_DECL)
9766 break;
9768 if (fld_chain)
9769 break;
9771 /* Don't warn if the array might be considered a poor
9772 man's flexible array member with a very permissive
9773 definition thereof. */
9774 if (TREE_CODE (v) == ARRAY_REF
9775 || TREE_CODE (v) == COMPONENT_REF)
9776 warning (OPT_Warray_bounds,
9777 "index %E denotes an offset "
9778 "greater than size of %qT",
9779 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9784 t = convert (sizetype, t);
9785 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9786 break;
9788 case COMPOUND_EXPR:
9789 /* Handle static members of volatile structs. */
9790 t = TREE_OPERAND (expr, 1);
9791 gcc_assert (TREE_CODE (t) == VAR_DECL);
9792 return fold_offsetof_1 (t);
9794 default:
9795 gcc_unreachable ();
9798 return fold_build_pointer_plus (base, off);
9801 /* Likewise, but convert it to the return type of offsetof. */
9803 tree
9804 fold_offsetof (tree expr)
9806 return convert (size_type_node, fold_offsetof_1 (expr));
9809 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9810 expression, because B will always be true. */
9812 void
9813 warn_for_omitted_condop (location_t location, tree cond)
9815 if (truth_value_p (TREE_CODE (cond)))
9816 warning_at (location, OPT_Wparentheses,
9817 "the omitted middle operand in ?: will always be %<true%>, "
9818 "suggest explicit middle operand");
9821 /* Give an error for storing into ARG, which is 'const'. USE indicates
9822 how ARG was being used. */
9824 void
9825 readonly_error (location_t loc, tree arg, enum lvalue_use use)
9827 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9828 || use == lv_asm);
9829 /* Using this macro rather than (for example) arrays of messages
9830 ensures that all the format strings are checked at compile
9831 time. */
9832 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9833 : (use == lv_increment ? (I) \
9834 : (use == lv_decrement ? (D) : (AS))))
9835 if (TREE_CODE (arg) == COMPONENT_REF)
9837 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9838 error_at (loc, READONLY_MSG (G_("assignment of member "
9839 "%qD in read-only object"),
9840 G_("increment of member "
9841 "%qD in read-only object"),
9842 G_("decrement of member "
9843 "%qD in read-only object"),
9844 G_("member %qD in read-only object "
9845 "used as %<asm%> output")),
9846 TREE_OPERAND (arg, 1));
9847 else
9848 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
9849 G_("increment of read-only member %qD"),
9850 G_("decrement of read-only member %qD"),
9851 G_("read-only member %qD used as %<asm%> output")),
9852 TREE_OPERAND (arg, 1));
9854 else if (TREE_CODE (arg) == VAR_DECL)
9855 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
9856 G_("increment of read-only variable %qD"),
9857 G_("decrement of read-only variable %qD"),
9858 G_("read-only variable %qD used as %<asm%> output")),
9859 arg);
9860 else if (TREE_CODE (arg) == PARM_DECL)
9861 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
9862 G_("increment of read-only parameter %qD"),
9863 G_("decrement of read-only parameter %qD"),
9864 G_("read-only parameter %qD use as %<asm%> output")),
9865 arg);
9866 else if (TREE_CODE (arg) == RESULT_DECL)
9868 gcc_assert (c_dialect_cxx ());
9869 error_at (loc, READONLY_MSG (G_("assignment of "
9870 "read-only named return value %qD"),
9871 G_("increment of "
9872 "read-only named return value %qD"),
9873 G_("decrement of "
9874 "read-only named return value %qD"),
9875 G_("read-only named return value %qD "
9876 "used as %<asm%>output")),
9877 arg);
9879 else if (TREE_CODE (arg) == FUNCTION_DECL)
9880 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
9881 G_("increment of function %qD"),
9882 G_("decrement of function %qD"),
9883 G_("function %qD used as %<asm%> output")),
9884 arg);
9885 else
9886 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
9887 G_("increment of read-only location %qE"),
9888 G_("decrement of read-only location %qE"),
9889 G_("read-only location %qE used as %<asm%> output")),
9890 arg);
9893 /* Print an error message for an invalid lvalue. USE says
9894 how the lvalue is being used and so selects the error message. LOC
9895 is the location for the error. */
9897 void
9898 lvalue_error (location_t loc, enum lvalue_use use)
9900 switch (use)
9902 case lv_assign:
9903 error_at (loc, "lvalue required as left operand of assignment");
9904 break;
9905 case lv_increment:
9906 error_at (loc, "lvalue required as increment operand");
9907 break;
9908 case lv_decrement:
9909 error_at (loc, "lvalue required as decrement operand");
9910 break;
9911 case lv_addressof:
9912 error_at (loc, "lvalue required as unary %<&%> operand");
9913 break;
9914 case lv_asm:
9915 error_at (loc, "lvalue required in asm statement");
9916 break;
9917 default:
9918 gcc_unreachable ();
9922 /* Print an error message for an invalid indirection of type TYPE.
9923 ERRSTRING is the name of the operator for the indirection. */
9925 void
9926 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9928 switch (errstring)
9930 case RO_NULL:
9931 gcc_assert (c_dialect_cxx ());
9932 error_at (loc, "invalid type argument (have %qT)", type);
9933 break;
9934 case RO_ARRAY_INDEXING:
9935 error_at (loc,
9936 "invalid type argument of array indexing (have %qT)",
9937 type);
9938 break;
9939 case RO_UNARY_STAR:
9940 error_at (loc,
9941 "invalid type argument of unary %<*%> (have %qT)",
9942 type);
9943 break;
9944 case RO_ARROW:
9945 error_at (loc,
9946 "invalid type argument of %<->%> (have %qT)",
9947 type);
9948 break;
9949 case RO_ARROW_STAR:
9950 error_at (loc,
9951 "invalid type argument of %<->*%> (have %qT)",
9952 type);
9953 break;
9954 case RO_IMPLICIT_CONVERSION:
9955 error_at (loc,
9956 "invalid type argument of implicit conversion (have %qT)",
9957 type);
9958 break;
9959 default:
9960 gcc_unreachable ();
9964 /* *PTYPE is an incomplete array. Complete it with a domain based on
9965 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9966 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9967 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9970 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9972 tree maxindex, type, main_type, elt, unqual_elt;
9973 int failure = 0, quals;
9974 hashval_t hashcode = 0;
9975 bool overflow_p = false;
9977 maxindex = size_zero_node;
9978 if (initial_value)
9980 if (TREE_CODE (initial_value) == STRING_CST)
9982 int eltsize
9983 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9984 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9986 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9988 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9990 if (vec_safe_is_empty (v))
9992 if (pedantic)
9993 failure = 3;
9994 maxindex = ssize_int (-1);
9996 else
9998 tree curindex;
9999 unsigned HOST_WIDE_INT cnt;
10000 constructor_elt *ce;
10001 bool fold_p = false;
10003 if ((*v)[0].index)
10004 maxindex = (*v)[0].index, fold_p = true;
10006 curindex = maxindex;
10008 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10010 bool curfold_p = false;
10011 if (ce->index)
10012 curindex = ce->index, curfold_p = true;
10013 else
10015 if (fold_p)
10017 /* Since we treat size types now as ordinary
10018 unsigned types, we need an explicit overflow
10019 check. */
10020 tree orig = curindex;
10021 curindex = fold_convert (sizetype, curindex);
10022 overflow_p |= tree_int_cst_lt (curindex, orig);
10024 curindex = size_binop (PLUS_EXPR, curindex,
10025 size_one_node);
10027 if (tree_int_cst_lt (maxindex, curindex))
10028 maxindex = curindex, fold_p = curfold_p;
10030 if (fold_p)
10032 tree orig = maxindex;
10033 maxindex = fold_convert (sizetype, maxindex);
10034 overflow_p |= tree_int_cst_lt (maxindex, orig);
10038 else
10040 /* Make an error message unless that happened already. */
10041 if (initial_value != error_mark_node)
10042 failure = 1;
10045 else
10047 failure = 2;
10048 if (!do_default)
10049 return failure;
10052 type = *ptype;
10053 elt = TREE_TYPE (type);
10054 quals = TYPE_QUALS (strip_array_types (elt));
10055 if (quals == 0)
10056 unqual_elt = elt;
10057 else
10058 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10060 /* Using build_distinct_type_copy and modifying things afterward instead
10061 of using build_array_type to create a new type preserves all of the
10062 TYPE_LANG_FLAG_? bits that the front end may have set. */
10063 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10064 TREE_TYPE (main_type) = unqual_elt;
10065 TYPE_DOMAIN (main_type)
10066 = build_range_type (TREE_TYPE (maxindex),
10067 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10068 layout_type (main_type);
10070 /* Make sure we have the canonical MAIN_TYPE. */
10071 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10072 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10073 hashcode);
10074 main_type = type_hash_canon (hashcode, main_type);
10076 /* Fix the canonical type. */
10077 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10078 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10079 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10080 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10081 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10082 != TYPE_DOMAIN (main_type)))
10083 TYPE_CANONICAL (main_type)
10084 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10085 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10086 else
10087 TYPE_CANONICAL (main_type) = main_type;
10089 if (quals == 0)
10090 type = main_type;
10091 else
10092 type = c_build_qualified_type (main_type, quals);
10094 if (COMPLETE_TYPE_P (type)
10095 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10096 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10098 error ("size of array is too large");
10099 /* If we proceed with the array type as it is, we'll eventually
10100 crash in tree_low_cst(). */
10101 type = error_mark_node;
10104 *ptype = type;
10105 return failure;
10108 /* Like c_mark_addressable but don't check register qualifier. */
10109 void
10110 c_common_mark_addressable_vec (tree t)
10112 while (handled_component_p (t))
10113 t = TREE_OPERAND (t, 0);
10114 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10115 return;
10116 TREE_ADDRESSABLE (t) = 1;
10121 /* Used to help initialize the builtin-types.def table. When a type of
10122 the correct size doesn't exist, use error_mark_node instead of NULL.
10123 The later results in segfaults even when a decl using the type doesn't
10124 get invoked. */
10126 tree
10127 builtin_type_for_size (int size, bool unsignedp)
10129 tree type = c_common_type_for_size (size, unsignedp);
10130 return type ? type : error_mark_node;
10133 /* A helper function for resolve_overloaded_builtin in resolving the
10134 overloaded __sync_ builtins. Returns a positive power of 2 if the
10135 first operand of PARAMS is a pointer to a supported data type.
10136 Returns 0 if an error is encountered. */
10138 static int
10139 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10141 tree type;
10142 int size;
10144 if (!params)
10146 error ("too few arguments to function %qE", function);
10147 return 0;
10150 type = TREE_TYPE ((*params)[0]);
10151 if (TREE_CODE (type) != POINTER_TYPE)
10152 goto incompatible;
10154 type = TREE_TYPE (type);
10155 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10156 goto incompatible;
10158 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
10159 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10160 return size;
10162 incompatible:
10163 error ("incompatible type for argument %d of %qE", 1, function);
10164 return 0;
10167 /* A helper function for resolve_overloaded_builtin. Adds casts to
10168 PARAMS to make arguments match up with those of FUNCTION. Drops
10169 the variadic arguments at the end. Returns false if some error
10170 was encountered; true on success. */
10172 static bool
10173 sync_resolve_params (location_t loc, tree orig_function, tree function,
10174 vec<tree, va_gc> *params, bool orig_format)
10176 function_args_iterator iter;
10177 tree ptype;
10178 unsigned int parmnum;
10180 function_args_iter_init (&iter, TREE_TYPE (function));
10181 /* We've declared the implementation functions to use "volatile void *"
10182 as the pointer parameter, so we shouldn't get any complaints from the
10183 call to check_function_arguments what ever type the user used. */
10184 function_args_iter_next (&iter);
10185 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10186 ptype = TYPE_MAIN_VARIANT (ptype);
10188 /* For the rest of the values, we need to cast these to FTYPE, so that we
10189 don't get warnings for passing pointer types, etc. */
10190 parmnum = 0;
10191 while (1)
10193 tree val, arg_type;
10195 arg_type = function_args_iter_cond (&iter);
10196 /* XXX void_type_node belies the abstraction. */
10197 if (arg_type == void_type_node)
10198 break;
10200 ++parmnum;
10201 if (params->length () <= parmnum)
10203 error_at (loc, "too few arguments to function %qE", orig_function);
10204 return false;
10207 /* Only convert parameters if arg_type is unsigned integer type with
10208 new format sync routines, i.e. don't attempt to convert pointer
10209 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10210 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10211 kinds). */
10212 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10214 /* Ideally for the first conversion we'd use convert_for_assignment
10215 so that we get warnings for anything that doesn't match the pointer
10216 type. This isn't portable across the C and C++ front ends atm. */
10217 val = (*params)[parmnum];
10218 val = convert (ptype, val);
10219 val = convert (arg_type, val);
10220 (*params)[parmnum] = val;
10223 function_args_iter_next (&iter);
10226 /* __atomic routines are not variadic. */
10227 if (!orig_format && params->length () != parmnum + 1)
10229 error_at (loc, "too many arguments to function %qE", orig_function);
10230 return false;
10233 /* The definition of these primitives is variadic, with the remaining
10234 being "an optional list of variables protected by the memory barrier".
10235 No clue what that's supposed to mean, precisely, but we consider all
10236 call-clobbered variables to be protected so we're safe. */
10237 params->truncate (parmnum + 1);
10239 return true;
10242 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10243 RESULT to make it match the type of the first pointer argument in
10244 PARAMS. */
10246 static tree
10247 sync_resolve_return (tree first_param, tree result, bool orig_format)
10249 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10250 tree rtype = TREE_TYPE (result);
10251 ptype = TYPE_MAIN_VARIANT (ptype);
10253 /* New format doesn't require casting unless the types are the same size. */
10254 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10255 return convert (ptype, result);
10256 else
10257 return result;
10260 /* This function verifies the PARAMS to generic atomic FUNCTION.
10261 It returns the size if all the parameters are the same size, otherwise
10262 0 is returned if the parameters are invalid. */
10264 static int
10265 get_atomic_generic_size (location_t loc, tree function,
10266 vec<tree, va_gc> *params)
10268 unsigned int n_param;
10269 unsigned int n_model;
10270 unsigned int x;
10271 int size_0;
10272 tree type_0;
10274 /* Determine the parameter makeup. */
10275 switch (DECL_FUNCTION_CODE (function))
10277 case BUILT_IN_ATOMIC_EXCHANGE:
10278 n_param = 4;
10279 n_model = 1;
10280 break;
10281 case BUILT_IN_ATOMIC_LOAD:
10282 case BUILT_IN_ATOMIC_STORE:
10283 n_param = 3;
10284 n_model = 1;
10285 break;
10286 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10287 n_param = 6;
10288 n_model = 2;
10289 break;
10290 default:
10291 gcc_unreachable ();
10294 if (vec_safe_length (params) != n_param)
10296 error_at (loc, "incorrect number of arguments to function %qE", function);
10297 return 0;
10300 /* Get type of first parameter, and determine its size. */
10301 type_0 = TREE_TYPE ((*params)[0]);
10302 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10304 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10305 function);
10306 return 0;
10309 /* Types must be compile time constant sizes. */
10310 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10312 error_at (loc,
10313 "argument 1 of %qE must be a pointer to a constant size type",
10314 function);
10315 return 0;
10318 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10320 /* Zero size objects are not allowed. */
10321 if (size_0 == 0)
10323 error_at (loc,
10324 "argument 1 of %qE must be a pointer to a nonzero size object",
10325 function);
10326 return 0;
10329 /* Check each other parameter is a pointer and the same size. */
10330 for (x = 0; x < n_param - n_model; x++)
10332 int size;
10333 tree type = TREE_TYPE ((*params)[x]);
10334 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10335 if (n_param == 6 && x == 3)
10336 continue;
10337 if (!POINTER_TYPE_P (type))
10339 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10340 function);
10341 return 0;
10343 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10344 if (size != size_0)
10346 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10347 function);
10348 return 0;
10352 /* Check memory model parameters for validity. */
10353 for (x = n_param - n_model ; x < n_param; x++)
10355 tree p = (*params)[x];
10356 if (TREE_CODE (p) == INTEGER_CST)
10358 int i = tree_low_cst (p, 1);
10359 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10361 warning_at (loc, OPT_Winvalid_memory_model,
10362 "invalid memory model argument %d of %qE", x + 1,
10363 function);
10366 else
10367 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10369 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10370 function);
10371 return 0;
10375 return size_0;
10379 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10380 at the beginning of the parameter list PARAMS representing the size of the
10381 objects. This is to match the library ABI requirement. LOC is the location
10382 of the function call.
10383 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10384 returned to allow the external call to be constructed. */
10386 static tree
10387 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10388 vec<tree, va_gc> *params)
10390 tree size_node;
10392 /* Insert a SIZE_T parameter as the first param. If there isn't
10393 enough space, allocate a new vector and recursively re-build with that. */
10394 if (!params->space (1))
10396 unsigned int z, len;
10397 vec<tree, va_gc> *v;
10398 tree f;
10400 len = params->length ();
10401 vec_alloc (v, len + 1);
10402 for (z = 0; z < len; z++)
10403 v->quick_push ((*params)[z]);
10404 f = build_function_call_vec (loc, function, v, NULL);
10405 vec_free (v);
10406 return f;
10409 /* Add the size parameter and leave as a function call for processing. */
10410 size_node = build_int_cst (size_type_node, n);
10411 params->quick_insert (0, size_node);
10412 return NULL_TREE;
10416 /* Return whether atomic operations for naturally aligned N-byte
10417 arguments are supported, whether inline or through libatomic. */
10418 static bool
10419 atomic_size_supported_p (int n)
10421 switch (n)
10423 case 1:
10424 case 2:
10425 case 4:
10426 case 8:
10427 return true;
10429 case 16:
10430 return targetm.scalar_mode_supported_p (TImode);
10432 default:
10433 return false;
10437 /* This will process an __atomic_exchange function call, determine whether it
10438 needs to be mapped to the _N variation, or turned into a library call.
10439 LOC is the location of the builtin call.
10440 FUNCTION is the DECL that has been invoked;
10441 PARAMS is the argument list for the call. The return value is non-null
10442 TRUE is returned if it is translated into the proper format for a call to the
10443 external library, and NEW_RETURN is set the tree for that function.
10444 FALSE is returned if processing for the _N variation is required, and
10445 NEW_RETURN is set to the the return value the result is copied into. */
10446 static bool
10447 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10448 vec<tree, va_gc> *params, tree *new_return)
10450 tree p0, p1, p2, p3;
10451 tree I_type, I_type_ptr;
10452 int n = get_atomic_generic_size (loc, function, params);
10454 /* Size of 0 is an error condition. */
10455 if (n == 0)
10457 *new_return = error_mark_node;
10458 return true;
10461 /* If not a lock-free size, change to the library generic format. */
10462 if (!atomic_size_supported_p (n))
10464 *new_return = add_atomic_size_parameter (n, loc, function, params);
10465 return true;
10468 /* Otherwise there is a lockfree match, transform the call from:
10469 void fn(T* mem, T* desired, T* return, model)
10470 into
10471 *return = (T) (fn (In* mem, (In) *desired, model)) */
10473 p0 = (*params)[0];
10474 p1 = (*params)[1];
10475 p2 = (*params)[2];
10476 p3 = (*params)[3];
10478 /* Create pointer to appropriate size. */
10479 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10480 I_type_ptr = build_pointer_type (I_type);
10482 /* Convert object pointer to required type. */
10483 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10484 (*params)[0] = p0;
10485 /* Convert new value to required type, and dereference it. */
10486 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10487 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10488 (*params)[1] = p1;
10490 /* Move memory model to the 3rd position, and end param list. */
10491 (*params)[2] = p3;
10492 params->truncate (3);
10494 /* Convert return pointer and dereference it for later assignment. */
10495 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10497 return false;
10501 /* This will process an __atomic_compare_exchange function call, determine
10502 whether it needs to be mapped to the _N variation, or turned into a lib call.
10503 LOC is the location of the builtin call.
10504 FUNCTION is the DECL that has been invoked;
10505 PARAMS is the argument list for the call. The return value is non-null
10506 TRUE is returned if it is translated into the proper format for a call to the
10507 external library, and NEW_RETURN is set the tree for that function.
10508 FALSE is returned if processing for the _N variation is required. */
10510 static bool
10511 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10512 vec<tree, va_gc> *params,
10513 tree *new_return)
10515 tree p0, p1, p2;
10516 tree I_type, I_type_ptr;
10517 int n = get_atomic_generic_size (loc, function, params);
10519 /* Size of 0 is an error condition. */
10520 if (n == 0)
10522 *new_return = error_mark_node;
10523 return true;
10526 /* If not a lock-free size, change to the library generic format. */
10527 if (!atomic_size_supported_p (n))
10529 /* The library generic format does not have the weak parameter, so
10530 remove it from the param list. Since a parameter has been removed,
10531 we can be sure that there is room for the SIZE_T parameter, meaning
10532 there will not be a recursive rebuilding of the parameter list, so
10533 there is no danger this will be done twice. */
10534 if (n > 0)
10536 (*params)[3] = (*params)[4];
10537 (*params)[4] = (*params)[5];
10538 params->truncate (5);
10540 *new_return = add_atomic_size_parameter (n, loc, function, params);
10541 return true;
10544 /* Otherwise, there is a match, so the call needs to be transformed from:
10545 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10546 into
10547 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10549 p0 = (*params)[0];
10550 p1 = (*params)[1];
10551 p2 = (*params)[2];
10553 /* Create pointer to appropriate size. */
10554 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10555 I_type_ptr = build_pointer_type (I_type);
10557 /* Convert object pointer to required type. */
10558 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10559 (*params)[0] = p0;
10561 /* Convert expected pointer to required type. */
10562 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10563 (*params)[1] = p1;
10565 /* Convert desired value to required type, and dereference it. */
10566 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10567 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10568 (*params)[2] = p2;
10570 /* The rest of the parameters are fine. NULL means no special return value
10571 processing.*/
10572 *new_return = NULL;
10573 return false;
10577 /* This will process an __atomic_load function call, determine whether it
10578 needs to be mapped to the _N variation, or turned into a library call.
10579 LOC is the location of the builtin call.
10580 FUNCTION is the DECL that has been invoked;
10581 PARAMS is the argument list for the call. The return value is non-null
10582 TRUE is returned if it is translated into the proper format for a call to the
10583 external library, and NEW_RETURN is set the tree for that function.
10584 FALSE is returned if processing for the _N variation is required, and
10585 NEW_RETURN is set to the the return value the result is copied into. */
10587 static bool
10588 resolve_overloaded_atomic_load (location_t loc, tree function,
10589 vec<tree, va_gc> *params, tree *new_return)
10591 tree p0, p1, p2;
10592 tree I_type, I_type_ptr;
10593 int n = get_atomic_generic_size (loc, function, params);
10595 /* Size of 0 is an error condition. */
10596 if (n == 0)
10598 *new_return = error_mark_node;
10599 return true;
10602 /* If not a lock-free size, change to the library generic format. */
10603 if (!atomic_size_supported_p (n))
10605 *new_return = add_atomic_size_parameter (n, loc, function, params);
10606 return true;
10609 /* Otherwise, there is a match, so the call needs to be transformed from:
10610 void fn(T* mem, T* return, model)
10611 into
10612 *return = (T) (fn ((In *) mem, model)) */
10614 p0 = (*params)[0];
10615 p1 = (*params)[1];
10616 p2 = (*params)[2];
10618 /* Create pointer to appropriate size. */
10619 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10620 I_type_ptr = build_pointer_type (I_type);
10622 /* Convert object pointer to required type. */
10623 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10624 (*params)[0] = p0;
10626 /* Move memory model to the 2nd position, and end param list. */
10627 (*params)[1] = p2;
10628 params->truncate (2);
10630 /* Convert return pointer and dereference it for later assignment. */
10631 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10633 return false;
10637 /* This will process an __atomic_store function call, determine whether it
10638 needs to be mapped to the _N variation, or turned into a library call.
10639 LOC is the location of the builtin call.
10640 FUNCTION is the DECL that has been invoked;
10641 PARAMS is the argument list for the call. The return value is non-null
10642 TRUE is returned if it is translated into the proper format for a call to the
10643 external library, and NEW_RETURN is set the tree for that function.
10644 FALSE is returned if processing for the _N variation is required, and
10645 NEW_RETURN is set to the the return value the result is copied into. */
10647 static bool
10648 resolve_overloaded_atomic_store (location_t loc, tree function,
10649 vec<tree, va_gc> *params, tree *new_return)
10651 tree p0, p1;
10652 tree I_type, I_type_ptr;
10653 int n = get_atomic_generic_size (loc, function, params);
10655 /* Size of 0 is an error condition. */
10656 if (n == 0)
10658 *new_return = error_mark_node;
10659 return true;
10662 /* If not a lock-free size, change to the library generic format. */
10663 if (!atomic_size_supported_p (n))
10665 *new_return = add_atomic_size_parameter (n, loc, function, params);
10666 return true;
10669 /* Otherwise, there is a match, so the call needs to be transformed from:
10670 void fn(T* mem, T* value, model)
10671 into
10672 fn ((In *) mem, (In) *value, model) */
10674 p0 = (*params)[0];
10675 p1 = (*params)[1];
10677 /* Create pointer to appropriate size. */
10678 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10679 I_type_ptr = build_pointer_type (I_type);
10681 /* Convert object pointer to required type. */
10682 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10683 (*params)[0] = p0;
10685 /* Convert new value to required type, and dereference it. */
10686 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10687 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10688 (*params)[1] = p1;
10690 /* The memory model is in the right spot already. Return is void. */
10691 *new_return = NULL_TREE;
10693 return false;
10697 /* Some builtin functions are placeholders for other expressions. This
10698 function should be called immediately after parsing the call expression
10699 before surrounding code has committed to the type of the expression.
10701 LOC is the location of the builtin call.
10703 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10704 PARAMS is the argument list for the call. The return value is non-null
10705 when expansion is complete, and null if normal processing should
10706 continue. */
10708 tree
10709 resolve_overloaded_builtin (location_t loc, tree function,
10710 vec<tree, va_gc> *params)
10712 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10713 bool orig_format = true;
10714 tree new_return = NULL_TREE;
10716 switch (DECL_BUILT_IN_CLASS (function))
10718 case BUILT_IN_NORMAL:
10719 break;
10720 case BUILT_IN_MD:
10721 if (targetm.resolve_overloaded_builtin)
10722 return targetm.resolve_overloaded_builtin (loc, function, params);
10723 else
10724 return NULL_TREE;
10725 default:
10726 return NULL_TREE;
10729 /* Handle BUILT_IN_NORMAL here. */
10730 switch (orig_code)
10732 case BUILT_IN_ATOMIC_EXCHANGE:
10733 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10734 case BUILT_IN_ATOMIC_LOAD:
10735 case BUILT_IN_ATOMIC_STORE:
10737 /* Handle these 4 together so that they can fall through to the next
10738 case if the call is transformed to an _N variant. */
10739 switch (orig_code)
10741 case BUILT_IN_ATOMIC_EXCHANGE:
10743 if (resolve_overloaded_atomic_exchange (loc, function, params,
10744 &new_return))
10745 return new_return;
10746 /* Change to the _N variant. */
10747 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10748 break;
10751 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10753 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10754 params,
10755 &new_return))
10756 return new_return;
10757 /* Change to the _N variant. */
10758 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10759 break;
10761 case BUILT_IN_ATOMIC_LOAD:
10763 if (resolve_overloaded_atomic_load (loc, function, params,
10764 &new_return))
10765 return new_return;
10766 /* Change to the _N variant. */
10767 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10768 break;
10770 case BUILT_IN_ATOMIC_STORE:
10772 if (resolve_overloaded_atomic_store (loc, function, params,
10773 &new_return))
10774 return new_return;
10775 /* Change to the _N variant. */
10776 orig_code = BUILT_IN_ATOMIC_STORE_N;
10777 break;
10779 default:
10780 gcc_unreachable ();
10782 /* Fallthrough to the normal processing. */
10784 case BUILT_IN_ATOMIC_EXCHANGE_N:
10785 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10786 case BUILT_IN_ATOMIC_LOAD_N:
10787 case BUILT_IN_ATOMIC_STORE_N:
10788 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10789 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10790 case BUILT_IN_ATOMIC_AND_FETCH_N:
10791 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10792 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10793 case BUILT_IN_ATOMIC_OR_FETCH_N:
10794 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10795 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10796 case BUILT_IN_ATOMIC_FETCH_AND_N:
10797 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10798 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10799 case BUILT_IN_ATOMIC_FETCH_OR_N:
10801 orig_format = false;
10802 /* Fallthru for parameter processing. */
10804 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10805 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10806 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10807 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10808 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10809 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10810 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10811 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10812 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10813 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10814 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10815 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10816 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10817 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10818 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10819 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10821 int n = sync_resolve_size (function, params);
10822 tree new_function, first_param, result;
10823 enum built_in_function fncode;
10825 if (n == 0)
10826 return error_mark_node;
10828 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10829 new_function = builtin_decl_explicit (fncode);
10830 if (!sync_resolve_params (loc, function, new_function, params,
10831 orig_format))
10832 return error_mark_node;
10834 first_param = (*params)[0];
10835 result = build_function_call_vec (loc, new_function, params, NULL);
10836 if (result == error_mark_node)
10837 return result;
10838 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10839 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10840 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10841 result = sync_resolve_return (first_param, result, orig_format);
10843 /* If new_return is set, assign function to that expr and cast the
10844 result to void since the generic interface returned void. */
10845 if (new_return)
10847 /* Cast function result from I{1,2,4,8,16} to the required type. */
10848 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10849 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10850 result);
10851 TREE_SIDE_EFFECTS (result) = 1;
10852 protected_set_expr_location (result, loc);
10853 result = convert (void_type_node, result);
10855 return result;
10858 default:
10859 return NULL_TREE;
10863 /* vector_types_compatible_elements_p is used in type checks of vectors
10864 values used as operands of binary operators. Where it returns true, and
10865 the other checks of the caller succeed (being vector types in he first
10866 place, and matching number of elements), we can just treat the types
10867 as essentially the same.
10868 Contrast with vector_targets_convertible_p, which is used for vector
10869 pointer types, and vector_types_convertible_p, which will allow
10870 language-specific matches under the control of flag_lax_vector_conversions,
10871 and might still require a conversion. */
10872 /* True if vector types T1 and T2 can be inputs to the same binary
10873 operator without conversion.
10874 We don't check the overall vector size here because some of our callers
10875 want to give different error messages when the vectors are compatible
10876 except for the element count. */
10878 bool
10879 vector_types_compatible_elements_p (tree t1, tree t2)
10881 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10882 t1 = TREE_TYPE (t1);
10883 t2 = TREE_TYPE (t2);
10885 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10887 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10888 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10889 || c2 == FIXED_POINT_TYPE));
10891 t1 = c_common_signed_type (t1);
10892 t2 = c_common_signed_type (t2);
10893 /* Equality works here because c_common_signed_type uses
10894 TYPE_MAIN_VARIANT. */
10895 if (t1 == t2)
10896 return true;
10897 if (opaque && c1 == c2
10898 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10899 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10900 return true;
10901 return false;
10904 /* Check for missing format attributes on function pointers. LTYPE is
10905 the new type or left-hand side type. RTYPE is the old type or
10906 right-hand side type. Returns TRUE if LTYPE is missing the desired
10907 attribute. */
10909 bool
10910 check_missing_format_attribute (tree ltype, tree rtype)
10912 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10913 tree ra;
10915 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10916 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10917 break;
10918 if (ra)
10920 tree la;
10921 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10922 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10923 break;
10924 return !la;
10926 else
10927 return false;
10930 /* Subscripting with type char is likely to lose on a machine where
10931 chars are signed. So warn on any machine, but optionally. Don't
10932 warn for unsigned char since that type is safe. Don't warn for
10933 signed char because anyone who uses that must have done so
10934 deliberately. Furthermore, we reduce the false positive load by
10935 warning only for non-constant value of type char. */
10937 void
10938 warn_array_subscript_with_type_char (tree index)
10940 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10941 && TREE_CODE (index) != INTEGER_CST)
10942 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10945 /* Implement -Wparentheses for the unexpected C precedence rules, to
10946 cover cases like x + y << z which readers are likely to
10947 misinterpret. We have seen an expression in which CODE is a binary
10948 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10949 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10950 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10951 expression was not formed using a binary or unary operator, or it
10952 was enclosed in parentheses. */
10954 void
10955 warn_about_parentheses (location_t loc, enum tree_code code,
10956 enum tree_code code_left, tree arg_left,
10957 enum tree_code code_right, tree arg_right)
10959 if (!warn_parentheses)
10960 return;
10962 /* This macro tests that the expression ARG with original tree code
10963 CODE appears to be a boolean expression. or the result of folding a
10964 boolean expression. */
10965 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10966 (truth_value_p (TREE_CODE (ARG)) \
10967 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10968 /* Folding may create 0 or 1 integers from other expressions. */ \
10969 || ((CODE) != INTEGER_CST \
10970 && (integer_onep (ARG) || integer_zerop (ARG))))
10972 switch (code)
10974 case LSHIFT_EXPR:
10975 if (code_left == PLUS_EXPR)
10976 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10977 "suggest parentheses around %<+%> inside %<<<%>");
10978 else if (code_right == PLUS_EXPR)
10979 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10980 "suggest parentheses around %<+%> inside %<<<%>");
10981 else if (code_left == MINUS_EXPR)
10982 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10983 "suggest parentheses around %<-%> inside %<<<%>");
10984 else if (code_right == MINUS_EXPR)
10985 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10986 "suggest parentheses around %<-%> inside %<<<%>");
10987 return;
10989 case RSHIFT_EXPR:
10990 if (code_left == PLUS_EXPR)
10991 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10992 "suggest parentheses around %<+%> inside %<>>%>");
10993 else if (code_right == PLUS_EXPR)
10994 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10995 "suggest parentheses around %<+%> inside %<>>%>");
10996 else if (code_left == MINUS_EXPR)
10997 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10998 "suggest parentheses around %<-%> inside %<>>%>");
10999 else if (code_right == MINUS_EXPR)
11000 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11001 "suggest parentheses around %<-%> inside %<>>%>");
11002 return;
11004 case TRUTH_ORIF_EXPR:
11005 if (code_left == TRUTH_ANDIF_EXPR)
11006 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11007 "suggest parentheses around %<&&%> within %<||%>");
11008 else if (code_right == TRUTH_ANDIF_EXPR)
11009 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11010 "suggest parentheses around %<&&%> within %<||%>");
11011 return;
11013 case BIT_IOR_EXPR:
11014 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11015 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11016 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11017 "suggest parentheses around arithmetic in operand of %<|%>");
11018 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11019 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11020 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11021 "suggest parentheses around arithmetic in operand of %<|%>");
11022 /* Check cases like x|y==z */
11023 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11024 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11025 "suggest parentheses around comparison in operand of %<|%>");
11026 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11027 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11028 "suggest parentheses around comparison in operand of %<|%>");
11029 /* Check cases like !x | y */
11030 else if (code_left == TRUTH_NOT_EXPR
11031 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11032 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11033 "suggest parentheses around operand of "
11034 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11035 return;
11037 case BIT_XOR_EXPR:
11038 if (code_left == BIT_AND_EXPR
11039 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11040 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11041 "suggest parentheses around arithmetic in operand of %<^%>");
11042 else if (code_right == BIT_AND_EXPR
11043 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11044 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11045 "suggest parentheses around arithmetic in operand of %<^%>");
11046 /* Check cases like x^y==z */
11047 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11048 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11049 "suggest parentheses around comparison in operand of %<^%>");
11050 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11051 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11052 "suggest parentheses around comparison in operand of %<^%>");
11053 return;
11055 case BIT_AND_EXPR:
11056 if (code_left == PLUS_EXPR)
11057 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11058 "suggest parentheses around %<+%> in operand of %<&%>");
11059 else if (code_right == PLUS_EXPR)
11060 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11061 "suggest parentheses around %<+%> in operand of %<&%>");
11062 else if (code_left == MINUS_EXPR)
11063 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11064 "suggest parentheses around %<-%> in operand of %<&%>");
11065 else if (code_right == MINUS_EXPR)
11066 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11067 "suggest parentheses around %<-%> in operand of %<&%>");
11068 /* Check cases like x&y==z */
11069 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11070 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11071 "suggest parentheses around comparison in operand of %<&%>");
11072 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11073 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11074 "suggest parentheses around comparison in operand of %<&%>");
11075 /* Check cases like !x & y */
11076 else if (code_left == TRUTH_NOT_EXPR
11077 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11078 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11079 "suggest parentheses around operand of "
11080 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11081 return;
11083 case EQ_EXPR:
11084 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11085 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11086 "suggest parentheses around comparison in operand of %<==%>");
11087 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11088 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11089 "suggest parentheses around comparison in operand of %<==%>");
11090 return;
11091 case NE_EXPR:
11092 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11093 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11094 "suggest parentheses around comparison in operand of %<!=%>");
11095 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11096 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11097 "suggest parentheses around comparison in operand of %<!=%>");
11098 return;
11100 default:
11101 if (TREE_CODE_CLASS (code) == tcc_comparison)
11103 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11104 && code_left != NE_EXPR && code_left != EQ_EXPR
11105 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11106 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11107 "comparisons like %<X<=Y<=Z%> do not "
11108 "have their mathematical meaning");
11109 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11110 && code_right != NE_EXPR && code_right != EQ_EXPR
11111 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11112 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11113 "comparisons like %<X<=Y<=Z%> do not "
11114 "have their mathematical meaning");
11116 return;
11118 #undef NOT_A_BOOLEAN_EXPR_P
11121 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11123 void
11124 warn_for_unused_label (tree label)
11126 if (!TREE_USED (label))
11128 if (DECL_INITIAL (label))
11129 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11130 else
11131 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11135 /* Warn for division by zero according to the value of DIVISOR. LOC
11136 is the location of the division operator. */
11138 void
11139 warn_for_div_by_zero (location_t loc, tree divisor)
11141 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11142 about division by zero. Do not issue a warning if DIVISOR has a
11143 floating-point type, since we consider 0.0/0.0 a valid way of
11144 generating a NaN. */
11145 if (c_inhibit_evaluation_warnings == 0
11146 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11147 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11150 /* Subroutine of build_binary_op. Give warnings for comparisons
11151 between signed and unsigned quantities that may fail. Do the
11152 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11153 so that casts will be considered, but default promotions won't
11156 LOCATION is the location of the comparison operator.
11158 The arguments of this function map directly to local variables
11159 of build_binary_op. */
11161 void
11162 warn_for_sign_compare (location_t location,
11163 tree orig_op0, tree orig_op1,
11164 tree op0, tree op1,
11165 tree result_type, enum tree_code resultcode)
11167 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11168 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11169 int unsignedp0, unsignedp1;
11171 /* In C++, check for comparison of different enum types. */
11172 if (c_dialect_cxx()
11173 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11174 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11175 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11176 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11178 warning_at (location,
11179 OPT_Wsign_compare, "comparison between types %qT and %qT",
11180 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11183 /* Do not warn if the comparison is being done in a signed type,
11184 since the signed type will only be chosen if it can represent
11185 all the values of the unsigned type. */
11186 if (!TYPE_UNSIGNED (result_type))
11187 /* OK */;
11188 /* Do not warn if both operands are unsigned. */
11189 else if (op0_signed == op1_signed)
11190 /* OK */;
11191 else
11193 tree sop, uop, base_type;
11194 bool ovf;
11196 if (op0_signed)
11197 sop = orig_op0, uop = orig_op1;
11198 else
11199 sop = orig_op1, uop = orig_op0;
11201 STRIP_TYPE_NOPS (sop);
11202 STRIP_TYPE_NOPS (uop);
11203 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11204 ? TREE_TYPE (result_type) : result_type);
11206 /* Do not warn if the signed quantity is an unsuffixed integer
11207 literal (or some static constant expression involving such
11208 literals or a conditional expression involving such literals)
11209 and it is non-negative. */
11210 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11211 /* OK */;
11212 /* Do not warn if the comparison is an equality operation, the
11213 unsigned quantity is an integral constant, and it would fit
11214 in the result if the result were signed. */
11215 else if (TREE_CODE (uop) == INTEGER_CST
11216 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11217 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11218 /* OK */;
11219 /* In C, do not warn if the unsigned quantity is an enumeration
11220 constant and its maximum value would fit in the result if the
11221 result were signed. */
11222 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11223 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11224 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11225 c_common_signed_type (base_type)))
11226 /* OK */;
11227 else
11228 warning_at (location,
11229 OPT_Wsign_compare,
11230 "comparison between signed and unsigned integer expressions");
11233 /* Warn if two unsigned values are being compared in a size larger
11234 than their original size, and one (and only one) is the result of
11235 a `~' operator. This comparison will always fail.
11237 Also warn if one operand is a constant, and the constant does not
11238 have all bits set that are set in the ~ operand when it is
11239 extended. */
11241 op0 = c_common_get_narrower (op0, &unsignedp0);
11242 op1 = c_common_get_narrower (op1, &unsignedp1);
11244 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11245 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11247 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11248 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11249 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11250 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11252 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11254 tree primop;
11255 HOST_WIDE_INT constant, mask;
11256 int unsignedp;
11257 unsigned int bits;
11259 if (host_integerp (op0, 0))
11261 primop = op1;
11262 unsignedp = unsignedp1;
11263 constant = tree_low_cst (op0, 0);
11265 else
11267 primop = op0;
11268 unsignedp = unsignedp0;
11269 constant = tree_low_cst (op1, 0);
11272 bits = TYPE_PRECISION (TREE_TYPE (primop));
11273 if (bits < TYPE_PRECISION (result_type)
11274 && bits < HOST_BITS_PER_LONG && unsignedp)
11276 mask = (~ (HOST_WIDE_INT) 0) << bits;
11277 if ((mask & constant) != mask)
11279 if (constant == 0)
11280 warning (OPT_Wsign_compare,
11281 "promoted ~unsigned is always non-zero");
11282 else
11283 warning_at (location, OPT_Wsign_compare,
11284 "comparison of promoted ~unsigned with constant");
11288 else if (unsignedp0 && unsignedp1
11289 && (TYPE_PRECISION (TREE_TYPE (op0))
11290 < TYPE_PRECISION (result_type))
11291 && (TYPE_PRECISION (TREE_TYPE (op1))
11292 < TYPE_PRECISION (result_type)))
11293 warning_at (location, OPT_Wsign_compare,
11294 "comparison of promoted ~unsigned with unsigned");
11298 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11299 type via c_common_type. If -Wdouble-promotion is in use, and the
11300 conditions for warning have been met, issue a warning. GMSGID is
11301 the warning message. It must have two %T specifiers for the type
11302 that was converted (generally "float") and the type to which it was
11303 converted (generally "double), respectively. LOC is the location
11304 to which the awrning should refer. */
11306 void
11307 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11308 const char *gmsgid, location_t loc)
11310 tree source_type;
11312 if (!warn_double_promotion)
11313 return;
11314 /* If the conversion will not occur at run-time, there is no need to
11315 warn about it. */
11316 if (c_inhibit_evaluation_warnings)
11317 return;
11318 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11319 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11320 return;
11321 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11322 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11323 source_type = type1;
11324 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11325 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11326 source_type = type2;
11327 else
11328 return;
11329 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11332 /* Setup a TYPE_DECL node as a typedef representation.
11334 X is a TYPE_DECL for a typedef statement. Create a brand new
11335 ..._TYPE node (which will be just a variant of the existing
11336 ..._TYPE node with identical properties) and then install X
11337 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11339 The whole point here is to end up with a situation where each
11340 and every ..._TYPE node the compiler creates will be uniquely
11341 associated with AT MOST one node representing a typedef name.
11342 This way, even though the compiler substitutes corresponding
11343 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11344 early on, later parts of the compiler can always do the reverse
11345 translation and get back the corresponding typedef name. For
11346 example, given:
11348 typedef struct S MY_TYPE;
11349 MY_TYPE object;
11351 Later parts of the compiler might only know that `object' was of
11352 type `struct S' if it were not for code just below. With this
11353 code however, later parts of the compiler see something like:
11355 struct S' == struct S
11356 typedef struct S' MY_TYPE;
11357 struct S' object;
11359 And they can then deduce (from the node for type struct S') that
11360 the original object declaration was:
11362 MY_TYPE object;
11364 Being able to do this is important for proper support of protoize,
11365 and also for generating precise symbolic debugging information
11366 which takes full account of the programmer's (typedef) vocabulary.
11368 Obviously, we don't want to generate a duplicate ..._TYPE node if
11369 the TYPE_DECL node that we are now processing really represents a
11370 standard built-in type. */
11372 void
11373 set_underlying_type (tree x)
11375 if (x == error_mark_node)
11376 return;
11377 if (DECL_IS_BUILTIN (x))
11379 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11380 TYPE_NAME (TREE_TYPE (x)) = x;
11382 else if (TREE_TYPE (x) != error_mark_node
11383 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11385 tree tt = TREE_TYPE (x);
11386 DECL_ORIGINAL_TYPE (x) = tt;
11387 tt = build_variant_type_copy (tt);
11388 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11389 TYPE_NAME (tt) = x;
11390 TREE_USED (tt) = TREE_USED (x);
11391 TREE_TYPE (x) = tt;
11395 /* Record the types used by the current global variable declaration
11396 being parsed, so that we can decide later to emit their debug info.
11397 Those types are in types_used_by_cur_var_decl, and we are going to
11398 store them in the types_used_by_vars_hash hash table.
11399 DECL is the declaration of the global variable that has been parsed. */
11401 void
11402 record_types_used_by_current_var_decl (tree decl)
11404 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11406 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11408 tree type = types_used_by_cur_var_decl->pop ();
11409 types_used_by_var_decl_insert (type, decl);
11413 /* If DECL is a typedef that is declared in the current function,
11414 record it for the purpose of -Wunused-local-typedefs. */
11416 void
11417 record_locally_defined_typedef (tree decl)
11419 struct c_language_function *l;
11421 if (!warn_unused_local_typedefs
11422 || cfun == NULL
11423 /* if this is not a locally defined typedef then we are not
11424 interested. */
11425 || !is_typedef_decl (decl)
11426 || !decl_function_context (decl))
11427 return;
11429 l = (struct c_language_function *) cfun->language;
11430 vec_safe_push (l->local_typedefs, decl);
11433 /* If T is a TYPE_DECL declared locally, mark it as used. */
11435 void
11436 maybe_record_typedef_use (tree t)
11438 if (!is_typedef_decl (t))
11439 return;
11441 TREE_USED (t) = true;
11444 /* Warn if there are some unused locally defined typedefs in the
11445 current function. */
11447 void
11448 maybe_warn_unused_local_typedefs (void)
11450 int i;
11451 tree decl;
11452 /* The number of times we have emitted -Wunused-local-typedefs
11453 warnings. If this is different from errorcount, that means some
11454 unrelated errors have been issued. In which case, we'll avoid
11455 emitting "unused-local-typedefs" warnings. */
11456 static int unused_local_typedefs_warn_count;
11457 struct c_language_function *l;
11459 if (cfun == NULL)
11460 return;
11462 if ((l = (struct c_language_function *) cfun->language) == NULL)
11463 return;
11465 if (warn_unused_local_typedefs
11466 && errorcount == unused_local_typedefs_warn_count)
11468 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11469 if (!TREE_USED (decl))
11470 warning_at (DECL_SOURCE_LOCATION (decl),
11471 OPT_Wunused_local_typedefs,
11472 "typedef %qD locally defined but not used", decl);
11473 unused_local_typedefs_warn_count = errorcount;
11476 vec_free (l->local_typedefs);
11479 /* The C and C++ parsers both use vectors to hold function arguments.
11480 For efficiency, we keep a cache of unused vectors. This is the
11481 cache. */
11483 typedef vec<tree, va_gc> *tree_gc_vec;
11484 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11486 /* Return a new vector from the cache. If the cache is empty,
11487 allocate a new vector. These vectors are GC'ed, so it is OK if the
11488 pointer is not released.. */
11490 vec<tree, va_gc> *
11491 make_tree_vector (void)
11493 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11494 return tree_vector_cache->pop ();
11495 else
11497 /* Passing 0 to vec::alloc returns NULL, and our callers require
11498 that we always return a non-NULL value. The vector code uses
11499 4 when growing a NULL vector, so we do too. */
11500 vec<tree, va_gc> *v;
11501 vec_alloc (v, 4);
11502 return v;
11506 /* Release a vector of trees back to the cache. */
11508 void
11509 release_tree_vector (vec<tree, va_gc> *vec)
11511 if (vec != NULL)
11513 vec->truncate (0);
11514 vec_safe_push (tree_vector_cache, vec);
11518 /* Get a new tree vector holding a single tree. */
11520 vec<tree, va_gc> *
11521 make_tree_vector_single (tree t)
11523 vec<tree, va_gc> *ret = make_tree_vector ();
11524 ret->quick_push (t);
11525 return ret;
11528 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11530 vec<tree, va_gc> *
11531 make_tree_vector_from_list (tree list)
11533 vec<tree, va_gc> *ret = make_tree_vector ();
11534 for (; list; list = TREE_CHAIN (list))
11535 vec_safe_push (ret, TREE_VALUE (list));
11536 return ret;
11539 /* Get a new tree vector which is a copy of an existing one. */
11541 vec<tree, va_gc> *
11542 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11544 vec<tree, va_gc> *ret;
11545 unsigned int ix;
11546 tree t;
11548 ret = make_tree_vector ();
11549 vec_safe_reserve (ret, vec_safe_length (orig));
11550 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11551 ret->quick_push (t);
11552 return ret;
11555 /* Return true if KEYWORD starts a type specifier. */
11557 bool
11558 keyword_begins_type_specifier (enum rid keyword)
11560 switch (keyword)
11562 case RID_AUTO_TYPE:
11563 case RID_INT:
11564 case RID_CHAR:
11565 case RID_FLOAT:
11566 case RID_DOUBLE:
11567 case RID_VOID:
11568 case RID_INT128:
11569 case RID_UNSIGNED:
11570 case RID_LONG:
11571 case RID_SHORT:
11572 case RID_SIGNED:
11573 case RID_DFLOAT32:
11574 case RID_DFLOAT64:
11575 case RID_DFLOAT128:
11576 case RID_FRACT:
11577 case RID_ACCUM:
11578 case RID_BOOL:
11579 case RID_WCHAR:
11580 case RID_CHAR16:
11581 case RID_CHAR32:
11582 case RID_SAT:
11583 case RID_COMPLEX:
11584 case RID_TYPEOF:
11585 case RID_STRUCT:
11586 case RID_CLASS:
11587 case RID_UNION:
11588 case RID_ENUM:
11589 return true;
11590 default:
11591 return false;
11595 /* Return true if KEYWORD names a type qualifier. */
11597 bool
11598 keyword_is_type_qualifier (enum rid keyword)
11600 switch (keyword)
11602 case RID_CONST:
11603 case RID_VOLATILE:
11604 case RID_RESTRICT:
11605 case RID_ATOMIC:
11606 return true;
11607 default:
11608 return false;
11612 /* Return true if KEYWORD names a storage class specifier.
11614 RID_TYPEDEF is not included in this list despite `typedef' being
11615 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11616 such for syntactic convenience only. */
11618 bool
11619 keyword_is_storage_class_specifier (enum rid keyword)
11621 switch (keyword)
11623 case RID_STATIC:
11624 case RID_EXTERN:
11625 case RID_REGISTER:
11626 case RID_AUTO:
11627 case RID_MUTABLE:
11628 case RID_THREAD:
11629 return true;
11630 default:
11631 return false;
11635 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11637 static bool
11638 keyword_is_function_specifier (enum rid keyword)
11640 switch (keyword)
11642 case RID_INLINE:
11643 case RID_NORETURN:
11644 case RID_VIRTUAL:
11645 case RID_EXPLICIT:
11646 return true;
11647 default:
11648 return false;
11652 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11653 declaration-specifier (C99 6.7). */
11655 bool
11656 keyword_is_decl_specifier (enum rid keyword)
11658 if (keyword_is_storage_class_specifier (keyword)
11659 || keyword_is_type_qualifier (keyword)
11660 || keyword_is_function_specifier (keyword))
11661 return true;
11663 switch (keyword)
11665 case RID_TYPEDEF:
11666 case RID_FRIEND:
11667 case RID_CONSTEXPR:
11668 return true;
11669 default:
11670 return false;
11674 /* Initialize language-specific-bits of tree_contains_struct. */
11676 void
11677 c_common_init_ts (void)
11679 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11680 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11681 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11684 /* Build a user-defined numeric literal out of an integer constant type VALUE
11685 with identifier SUFFIX. */
11687 tree
11688 build_userdef_literal (tree suffix_id, tree value,
11689 enum overflow_type overflow, tree num_string)
11691 tree literal = make_node (USERDEF_LITERAL);
11692 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11693 USERDEF_LITERAL_VALUE (literal) = value;
11694 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11695 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11696 return literal;
11699 /* For vector[index], convert the vector to a
11700 pointer of the underlying type. */
11701 void
11702 convert_vector_to_pointer_for_subscript (location_t loc,
11703 tree* vecp, tree index)
11705 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11707 tree type = TREE_TYPE (*vecp);
11708 tree type1;
11710 if (TREE_CODE (index) == INTEGER_CST)
11711 if (!host_integerp (index, 1)
11712 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11713 >= TYPE_VECTOR_SUBPARTS (type)))
11714 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11716 c_common_mark_addressable_vec (*vecp);
11717 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11718 type = build_pointer_type (type);
11719 type1 = build_pointer_type (TREE_TYPE (*vecp));
11720 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11721 *vecp = convert (type, *vecp);
11725 /* Determine which of the operands, if any, is a scalar that needs to be
11726 converted to a vector, for the range of operations. */
11727 enum stv_conv
11728 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11729 bool complain)
11731 tree type0 = TREE_TYPE (op0);
11732 tree type1 = TREE_TYPE (op1);
11733 bool integer_only_op = false;
11734 enum stv_conv ret = stv_firstarg;
11736 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11737 || TREE_CODE (type1) == VECTOR_TYPE);
11738 switch (code)
11740 /* Most GENERIC binary expressions require homogeneous arguments.
11741 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11742 argument that is a vector and a second one that is a scalar, so
11743 we never return stv_secondarg for them. */
11744 case RSHIFT_EXPR:
11745 case LSHIFT_EXPR:
11746 if (TREE_CODE (type0) == INTEGER_TYPE
11747 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11749 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11751 if (complain)
11752 error_at (loc, "conversion of scalar %qT to vector %qT "
11753 "involves truncation", type0, type1);
11754 return stv_error;
11756 else
11757 return stv_firstarg;
11759 break;
11761 case BIT_IOR_EXPR:
11762 case BIT_XOR_EXPR:
11763 case BIT_AND_EXPR:
11764 integer_only_op = true;
11765 /* ... fall through ... */
11767 case VEC_COND_EXPR:
11769 case PLUS_EXPR:
11770 case MINUS_EXPR:
11771 case MULT_EXPR:
11772 case TRUNC_DIV_EXPR:
11773 case CEIL_DIV_EXPR:
11774 case FLOOR_DIV_EXPR:
11775 case ROUND_DIV_EXPR:
11776 case EXACT_DIV_EXPR:
11777 case TRUNC_MOD_EXPR:
11778 case FLOOR_MOD_EXPR:
11779 case RDIV_EXPR:
11780 case EQ_EXPR:
11781 case NE_EXPR:
11782 case LE_EXPR:
11783 case GE_EXPR:
11784 case LT_EXPR:
11785 case GT_EXPR:
11786 /* What about UNLT_EXPR? */
11787 if (TREE_CODE (type0) == VECTOR_TYPE)
11789 tree tmp;
11790 ret = stv_secondarg;
11791 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11792 tmp = type0; type0 = type1; type1 = tmp;
11793 tmp = op0; op0 = op1; op1 = tmp;
11796 if (TREE_CODE (type0) == INTEGER_TYPE
11797 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11799 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11801 if (complain)
11802 error_at (loc, "conversion of scalar %qT to vector %qT "
11803 "involves truncation", type0, type1);
11804 return stv_error;
11806 return ret;
11808 else if (!integer_only_op
11809 /* Allow integer --> real conversion if safe. */
11810 && (TREE_CODE (type0) == REAL_TYPE
11811 || TREE_CODE (type0) == INTEGER_TYPE)
11812 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11814 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11816 if (complain)
11817 error_at (loc, "conversion of scalar %qT to vector %qT "
11818 "involves truncation", type0, type1);
11819 return stv_error;
11821 return ret;
11823 default:
11824 break;
11827 return stv_nothing;
11830 /* Return true iff ALIGN is an integral constant that is a fundamental
11831 alignment, as defined by [basic.align] in the c++-11
11832 specifications.
11834 That is:
11836 [A fundamental alignment is represented by an alignment less than or
11837 equal to the greatest alignment supported by the implementation
11838 in all contexts, which is equal to
11839 alignof(max_align_t)]. */
11841 bool
11842 cxx_fundamental_alignment_p (unsigned align)
11844 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11845 TYPE_ALIGN (long_double_type_node)));
11848 #include "gt-c-family-c-common.h"