* Makefile.in (C_COMMON_OBJS): Depend on c-cilkplus.o.
[official-gcc.git] / gcc / c-family / c-common.c
blobd7d5cb20a17788530ab29d2db85febc35493b589
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++98 is the default. */
244 enum cxx_dialect cxx_dialect = cxx98;
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_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
478 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
479 { "__is_union", RID_IS_UNION, D_CXXONLY },
480 { "__label__", RID_LABEL, 0 },
481 { "__null", RID_NULL, 0 },
482 { "__real", RID_REALPART, 0 },
483 { "__real__", RID_REALPART, 0 },
484 { "__restrict", RID_RESTRICT, 0 },
485 { "__restrict__", RID_RESTRICT, 0 },
486 { "__signed", RID_SIGNED, 0 },
487 { "__signed__", RID_SIGNED, 0 },
488 { "__thread", RID_THREAD, 0 },
489 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
490 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
491 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
492 { "__typeof", RID_TYPEOF, 0 },
493 { "__typeof__", RID_TYPEOF, 0 },
494 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
495 { "__volatile", RID_VOLATILE, 0 },
496 { "__volatile__", RID_VOLATILE, 0 },
497 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
498 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
499 { "asm", RID_ASM, D_ASM },
500 { "auto", RID_AUTO, 0 },
501 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
502 { "break", RID_BREAK, 0 },
503 { "case", RID_CASE, 0 },
504 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
505 { "char", RID_CHAR, 0 },
506 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
507 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
508 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
509 { "const", RID_CONST, 0 },
510 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
511 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
512 { "continue", RID_CONTINUE, 0 },
513 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
514 { "default", RID_DEFAULT, 0 },
515 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
516 { "do", RID_DO, 0 },
517 { "double", RID_DOUBLE, 0 },
518 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
519 { "else", RID_ELSE, 0 },
520 { "enum", RID_ENUM, 0 },
521 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
522 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
523 { "extern", RID_EXTERN, 0 },
524 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
525 { "float", RID_FLOAT, 0 },
526 { "for", RID_FOR, 0 },
527 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
528 { "goto", RID_GOTO, 0 },
529 { "if", RID_IF, 0 },
530 { "inline", RID_INLINE, D_EXT89 },
531 { "int", RID_INT, 0 },
532 { "long", RID_LONG, 0 },
533 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
534 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
535 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
536 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
537 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
538 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
539 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
540 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
541 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
542 { "register", RID_REGISTER, 0 },
543 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
544 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
545 { "return", RID_RETURN, 0 },
546 { "short", RID_SHORT, 0 },
547 { "signed", RID_SIGNED, 0 },
548 { "sizeof", RID_SIZEOF, 0 },
549 { "static", RID_STATIC, 0 },
550 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
551 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
552 { "struct", RID_STRUCT, 0 },
553 { "switch", RID_SWITCH, 0 },
554 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
555 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
556 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
557 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
558 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
559 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
560 { "typedef", RID_TYPEDEF, 0 },
561 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
562 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
563 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
564 { "union", RID_UNION, 0 },
565 { "unsigned", RID_UNSIGNED, 0 },
566 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
567 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
568 { "void", RID_VOID, 0 },
569 { "volatile", RID_VOLATILE, 0 },
570 { "wchar_t", RID_WCHAR, D_CXXONLY },
571 { "while", RID_WHILE, 0 },
572 /* These Objective-C keywords are recognized only immediately after
573 an '@'. */
574 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
575 { "defs", RID_AT_DEFS, D_OBJC },
576 { "encode", RID_AT_ENCODE, D_OBJC },
577 { "end", RID_AT_END, D_OBJC },
578 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
579 { "interface", RID_AT_INTERFACE, D_OBJC },
580 { "protocol", RID_AT_PROTOCOL, D_OBJC },
581 { "selector", RID_AT_SELECTOR, D_OBJC },
582 { "finally", RID_AT_FINALLY, D_OBJC },
583 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
584 { "optional", RID_AT_OPTIONAL, D_OBJC },
585 { "required", RID_AT_REQUIRED, D_OBJC },
586 { "property", RID_AT_PROPERTY, D_OBJC },
587 { "package", RID_AT_PACKAGE, D_OBJC },
588 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
589 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
590 /* These are recognized only in protocol-qualifier context
591 (see above) */
592 { "bycopy", RID_BYCOPY, D_OBJC },
593 { "byref", RID_BYREF, D_OBJC },
594 { "in", RID_IN, D_OBJC },
595 { "inout", RID_INOUT, D_OBJC },
596 { "oneway", RID_ONEWAY, D_OBJC },
597 { "out", RID_OUT, D_OBJC },
598 /* These are recognized inside a property attribute list */
599 { "assign", RID_ASSIGN, D_OBJC },
600 { "copy", RID_COPY, D_OBJC },
601 { "getter", RID_GETTER, D_OBJC },
602 { "nonatomic", RID_NONATOMIC, D_OBJC },
603 { "readonly", RID_READONLY, D_OBJC },
604 { "readwrite", RID_READWRITE, D_OBJC },
605 { "retain", RID_RETAIN, D_OBJC },
606 { "setter", RID_SETTER, D_OBJC },
609 const unsigned int num_c_common_reswords =
610 sizeof c_common_reswords / sizeof (struct c_common_resword);
612 /* Table of machine-independent attributes common to all C-like languages. */
613 const struct attribute_spec c_common_attribute_table[] =
615 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
616 affects_type_identity } */
617 { "packed", 0, 0, false, false, false,
618 handle_packed_attribute , false},
619 { "nocommon", 0, 0, true, false, false,
620 handle_nocommon_attribute, false},
621 { "common", 0, 0, true, false, false,
622 handle_common_attribute, false },
623 /* FIXME: logically, noreturn attributes should be listed as
624 "false, true, true" and apply to function types. But implementing this
625 would require all the places in the compiler that use TREE_THIS_VOLATILE
626 on a decl to identify non-returning functions to be located and fixed
627 to check the function type instead. */
628 { "noreturn", 0, 0, true, false, false,
629 handle_noreturn_attribute, false },
630 { "volatile", 0, 0, true, false, false,
631 handle_noreturn_attribute, false },
632 { "noinline", 0, 0, true, false, false,
633 handle_noinline_attribute, false },
634 { "noclone", 0, 0, true, false, false,
635 handle_noclone_attribute, false },
636 { "leaf", 0, 0, true, false, false,
637 handle_leaf_attribute, false },
638 { "always_inline", 0, 0, true, false, false,
639 handle_always_inline_attribute, false },
640 { "gnu_inline", 0, 0, true, false, false,
641 handle_gnu_inline_attribute, false },
642 { "artificial", 0, 0, true, false, false,
643 handle_artificial_attribute, false },
644 { "flatten", 0, 0, true, false, false,
645 handle_flatten_attribute, false },
646 { "used", 0, 0, true, false, false,
647 handle_used_attribute, false },
648 { "unused", 0, 0, false, false, false,
649 handle_unused_attribute, false },
650 { "externally_visible", 0, 0, true, false, false,
651 handle_externally_visible_attribute, false },
652 /* The same comments as for noreturn attributes apply to const ones. */
653 { "const", 0, 0, true, false, false,
654 handle_const_attribute, false },
655 { "transparent_union", 0, 0, false, false, false,
656 handle_transparent_union_attribute, false },
657 { "constructor", 0, 1, true, false, false,
658 handle_constructor_attribute, false },
659 { "destructor", 0, 1, true, false, false,
660 handle_destructor_attribute, false },
661 { "mode", 1, 1, false, true, false,
662 handle_mode_attribute, false },
663 { "section", 1, 1, true, false, false,
664 handle_section_attribute, false },
665 { "aligned", 0, 1, false, false, false,
666 handle_aligned_attribute, false },
667 { "weak", 0, 0, true, false, false,
668 handle_weak_attribute, false },
669 { "ifunc", 1, 1, true, false, false,
670 handle_ifunc_attribute, false },
671 { "alias", 1, 1, true, false, false,
672 handle_alias_attribute, false },
673 { "weakref", 0, 1, true, false, false,
674 handle_weakref_attribute, false },
675 { "no_instrument_function", 0, 0, true, false, false,
676 handle_no_instrument_function_attribute,
677 false },
678 { "malloc", 0, 0, true, false, false,
679 handle_malloc_attribute, false },
680 { "returns_twice", 0, 0, true, false, false,
681 handle_returns_twice_attribute, false },
682 { "no_stack_limit", 0, 0, true, false, false,
683 handle_no_limit_stack_attribute, false },
684 { "pure", 0, 0, true, false, false,
685 handle_pure_attribute, false },
686 { "transaction_callable", 0, 0, false, true, false,
687 handle_tm_attribute, false },
688 { "transaction_unsafe", 0, 0, false, true, false,
689 handle_tm_attribute, false },
690 { "transaction_safe", 0, 0, false, true, false,
691 handle_tm_attribute, false },
692 { "transaction_may_cancel_outer", 0, 0, false, true, false,
693 handle_tm_attribute, false },
694 /* ??? These two attributes didn't make the transition from the
695 Intel language document to the multi-vendor language document. */
696 { "transaction_pure", 0, 0, false, true, false,
697 handle_tm_attribute, false },
698 { "transaction_wrap", 1, 1, true, false, false,
699 handle_tm_wrap_attribute, false },
700 /* For internal use (marking of builtins) only. The name contains space
701 to prevent its usage in source code. */
702 { "no vops", 0, 0, true, false, false,
703 handle_novops_attribute, false },
704 { "deprecated", 0, 1, false, false, false,
705 handle_deprecated_attribute, false },
706 { "vector_size", 1, 1, false, true, false,
707 handle_vector_size_attribute, false },
708 { "visibility", 1, 1, false, false, false,
709 handle_visibility_attribute, false },
710 { "tls_model", 1, 1, true, false, false,
711 handle_tls_model_attribute, false },
712 { "nonnull", 0, -1, false, true, true,
713 handle_nonnull_attribute, false },
714 { "nothrow", 0, 0, true, false, false,
715 handle_nothrow_attribute, false },
716 { "may_alias", 0, 0, false, true, false, NULL, false },
717 { "cleanup", 1, 1, true, false, false,
718 handle_cleanup_attribute, false },
719 { "warn_unused_result", 0, 0, false, true, true,
720 handle_warn_unused_result_attribute, false },
721 { "sentinel", 0, 1, false, true, true,
722 handle_sentinel_attribute, false },
723 /* For internal use (marking of builtins) only. The name contains space
724 to prevent its usage in source code. */
725 { "type generic", 0, 0, false, true, true,
726 handle_type_generic_attribute, false },
727 { "alloc_size", 1, 2, false, true, true,
728 handle_alloc_size_attribute, false },
729 { "cold", 0, 0, true, false, false,
730 handle_cold_attribute, false },
731 { "hot", 0, 0, true, false, false,
732 handle_hot_attribute, false },
733 { "no_address_safety_analysis",
734 0, 0, true, false, false,
735 handle_no_address_safety_analysis_attribute,
736 false },
737 { "no_sanitize_address", 0, 0, true, false, false,
738 handle_no_sanitize_address_attribute,
739 false },
740 { "no_sanitize_undefined", 0, 0, true, false, false,
741 handle_no_sanitize_undefined_attribute,
742 false },
743 { "warning", 1, 1, true, false, false,
744 handle_error_attribute, false },
745 { "error", 1, 1, true, false, false,
746 handle_error_attribute, false },
747 { "target", 1, -1, true, false, false,
748 handle_target_attribute, false },
749 { "optimize", 1, -1, true, false, false,
750 handle_optimize_attribute, false },
751 /* For internal use only. The leading '*' both prevents its usage in
752 source code and signals that it may be overridden by machine tables. */
753 { "*tm regparm", 0, 0, false, true, true,
754 ignore_attribute, false },
755 { "no_split_stack", 0, 0, true, false, false,
756 handle_no_split_stack_attribute, false },
757 /* For internal use (marking of builtins and runtime functions) only.
758 The name contains space to prevent its usage in source code. */
759 { "fn spec", 1, 1, false, true, true,
760 handle_fnspec_attribute, false },
761 { "warn_unused", 0, 0, false, false, false,
762 handle_warn_unused_attribute, false },
763 { "returns_nonnull", 0, 0, false, true, true,
764 handle_returns_nonnull_attribute, false },
765 { "omp declare simd", 0, -1, true, false, false,
766 handle_omp_declare_simd_attribute, false },
767 { "omp declare target", 0, 0, true, false, false,
768 handle_omp_declare_target_attribute, false },
769 { "bnd_variable_size", 0, 0, true, false, false,
770 handle_bnd_variable_size_attribute, false },
771 { "bnd_legacy", 0, 0, true, false, false,
772 handle_bnd_legacy, false },
773 { NULL, 0, 0, false, false, false, NULL, false }
776 /* Give the specifications for the format attributes, used by C and all
777 descendants. */
779 const struct attribute_spec c_common_format_attribute_table[] =
781 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
782 affects_type_identity } */
783 { "format", 3, 3, false, true, true,
784 handle_format_attribute, false },
785 { "format_arg", 1, 1, false, true, true,
786 handle_format_arg_attribute, false },
787 { NULL, 0, 0, false, false, false, NULL, false }
790 /* Return identifier for address space AS. */
792 const char *
793 c_addr_space_name (addr_space_t as)
795 int rid = RID_FIRST_ADDR_SPACE + as;
796 gcc_assert (ridpointers [rid]);
797 return IDENTIFIER_POINTER (ridpointers [rid]);
800 /* Push current bindings for the function name VAR_DECLS. */
802 void
803 start_fname_decls (void)
805 unsigned ix;
806 tree saved = NULL_TREE;
808 for (ix = 0; fname_vars[ix].decl; ix++)
810 tree decl = *fname_vars[ix].decl;
812 if (decl)
814 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
815 saved);
816 *fname_vars[ix].decl = NULL_TREE;
819 if (saved || saved_function_name_decls)
820 /* Normally they'll have been NULL, so only push if we've got a
821 stack, or they are non-NULL. */
822 saved_function_name_decls = tree_cons (saved, NULL_TREE,
823 saved_function_name_decls);
826 /* Finish up the current bindings, adding them into the current function's
827 statement tree. This must be done _before_ finish_stmt_tree is called.
828 If there is no current function, we must be at file scope and no statements
829 are involved. Pop the previous bindings. */
831 void
832 finish_fname_decls (void)
834 unsigned ix;
835 tree stmts = NULL_TREE;
836 tree stack = saved_function_name_decls;
838 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
839 append_to_statement_list (TREE_VALUE (stack), &stmts);
841 if (stmts)
843 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
845 if (TREE_CODE (*bodyp) == BIND_EXPR)
846 bodyp = &BIND_EXPR_BODY (*bodyp);
848 append_to_statement_list_force (*bodyp, &stmts);
849 *bodyp = stmts;
852 for (ix = 0; fname_vars[ix].decl; ix++)
853 *fname_vars[ix].decl = NULL_TREE;
855 if (stack)
857 /* We had saved values, restore them. */
858 tree saved;
860 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
862 tree decl = TREE_PURPOSE (saved);
863 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
865 *fname_vars[ix].decl = decl;
867 stack = TREE_CHAIN (stack);
869 saved_function_name_decls = stack;
872 /* Return the text name of the current function, suitably prettified
873 by PRETTY_P. Return string must be freed by caller. */
875 const char *
876 fname_as_string (int pretty_p)
878 const char *name = "top level";
879 char *namep;
880 int vrb = 2, len;
881 cpp_string cstr = { 0, 0 }, strname;
883 if (!pretty_p)
885 name = "";
886 vrb = 0;
889 if (current_function_decl)
890 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
892 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
894 namep = XNEWVEC (char, len);
895 snprintf (namep, len, "\"%s\"", name);
896 strname.text = (unsigned char *) namep;
897 strname.len = len - 1;
899 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
901 XDELETEVEC (namep);
902 return (const char *) cstr.text;
905 return namep;
908 /* Return the VAR_DECL for a const char array naming the current
909 function. If the VAR_DECL has not yet been created, create it
910 now. RID indicates how it should be formatted and IDENTIFIER_NODE
911 ID is its name (unfortunately C and C++ hold the RID values of
912 keywords in different places, so we can't derive RID from ID in
913 this language independent code. LOC is the location of the
914 function. */
916 tree
917 fname_decl (location_t loc, unsigned int rid, tree id)
919 unsigned ix;
920 tree decl = NULL_TREE;
922 for (ix = 0; fname_vars[ix].decl; ix++)
923 if (fname_vars[ix].rid == rid)
924 break;
926 decl = *fname_vars[ix].decl;
927 if (!decl)
929 /* If a tree is built here, it would normally have the lineno of
930 the current statement. Later this tree will be moved to the
931 beginning of the function and this line number will be wrong.
932 To avoid this problem set the lineno to 0 here; that prevents
933 it from appearing in the RTL. */
934 tree stmts;
935 location_t saved_location = input_location;
936 input_location = UNKNOWN_LOCATION;
938 stmts = push_stmt_list ();
939 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
940 stmts = pop_stmt_list (stmts);
941 if (!IS_EMPTY_STMT (stmts))
942 saved_function_name_decls
943 = tree_cons (decl, stmts, saved_function_name_decls);
944 *fname_vars[ix].decl = decl;
945 input_location = saved_location;
947 if (!ix && !current_function_decl)
948 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
950 return decl;
953 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
955 tree
956 fix_string_type (tree value)
958 int length = TREE_STRING_LENGTH (value);
959 int nchars;
960 tree e_type, i_type, a_type;
962 /* Compute the number of elements, for the array type. */
963 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
965 nchars = length;
966 e_type = char_type_node;
968 else if (TREE_TYPE (value) == char16_array_type_node)
970 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
971 e_type = char16_type_node;
973 else if (TREE_TYPE (value) == char32_array_type_node)
975 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
976 e_type = char32_type_node;
978 else
980 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
981 e_type = wchar_type_node;
984 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
985 limit in C++98 Annex B is very large (65536) and is not normative,
986 so we do not diagnose it (warn_overlength_strings is forced off
987 in c_common_post_options). */
988 if (warn_overlength_strings)
990 const int nchars_max = flag_isoc99 ? 4095 : 509;
991 const int relevant_std = flag_isoc99 ? 99 : 90;
992 if (nchars - 1 > nchars_max)
993 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
994 separate the %d from the 'C'. 'ISO' should not be
995 translated, but it may be moved after 'C%d' in languages
996 where modifiers follow nouns. */
997 pedwarn (input_location, OPT_Woverlength_strings,
998 "string length %qd is greater than the length %qd "
999 "ISO C%d compilers are required to support",
1000 nchars - 1, nchars_max, relevant_std);
1003 /* Create the array type for the string constant. The ISO C++
1004 standard says that a string literal has type `const char[N]' or
1005 `const wchar_t[N]'. We use the same logic when invoked as a C
1006 front-end with -Wwrite-strings.
1007 ??? We should change the type of an expression depending on the
1008 state of a warning flag. We should just be warning -- see how
1009 this is handled in the C++ front-end for the deprecated implicit
1010 conversion from string literals to `char*' or `wchar_t*'.
1012 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1013 array type being the unqualified version of that type.
1014 Therefore, if we are constructing an array of const char, we must
1015 construct the matching unqualified array type first. The C front
1016 end does not require this, but it does no harm, so we do it
1017 unconditionally. */
1018 i_type = build_index_type (size_int (nchars - 1));
1019 a_type = build_array_type (e_type, i_type);
1020 if (c_dialect_cxx() || warn_write_strings)
1021 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1023 TREE_TYPE (value) = a_type;
1024 TREE_CONSTANT (value) = 1;
1025 TREE_READONLY (value) = 1;
1026 TREE_STATIC (value) = 1;
1027 return value;
1030 /* If DISABLE is true, stop issuing warnings. This is used when
1031 parsing code that we know will not be executed. This function may
1032 be called multiple times, and works as a stack. */
1034 static void
1035 c_disable_warnings (bool disable)
1037 if (disable)
1039 ++c_inhibit_evaluation_warnings;
1040 fold_defer_overflow_warnings ();
1044 /* If ENABLE is true, reenable issuing warnings. */
1046 static void
1047 c_enable_warnings (bool enable)
1049 if (enable)
1051 --c_inhibit_evaluation_warnings;
1052 fold_undefer_and_ignore_overflow_warnings ();
1056 /* Fully fold EXPR, an expression that was not folded (beyond integer
1057 constant expressions and null pointer constants) when being built
1058 up. If IN_INIT, this is in a static initializer and certain
1059 changes are made to the folding done. Clear *MAYBE_CONST if
1060 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1061 expression because it contains an evaluated operator (in C99) or an
1062 operator outside of sizeof returning an integer constant (in C90)
1063 not permitted in constant expressions, or because it contains an
1064 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1065 set to true by callers before calling this function.) Return the
1066 folded expression. Function arguments have already been folded
1067 before calling this function, as have the contents of SAVE_EXPR,
1068 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1069 C_MAYBE_CONST_EXPR. */
1071 tree
1072 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1074 tree ret;
1075 tree eptype = NULL_TREE;
1076 bool dummy = true;
1077 bool maybe_const_itself = true;
1078 location_t loc = EXPR_LOCATION (expr);
1080 /* This function is not relevant to C++ because C++ folds while
1081 parsing, and may need changes to be correct for C++ when C++
1082 stops folding while parsing. */
1083 if (c_dialect_cxx ())
1084 gcc_unreachable ();
1086 if (!maybe_const)
1087 maybe_const = &dummy;
1088 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1090 eptype = TREE_TYPE (expr);
1091 expr = TREE_OPERAND (expr, 0);
1093 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1094 &maybe_const_itself);
1095 if (eptype)
1096 ret = fold_convert_loc (loc, eptype, ret);
1097 *maybe_const &= maybe_const_itself;
1098 return ret;
1101 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1102 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1103 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1104 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1105 both evaluated and unevaluated subexpressions while
1106 *MAYBE_CONST_ITSELF is carried from only evaluated
1107 subexpressions). */
1109 static tree
1110 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1111 bool *maybe_const_itself)
1113 tree ret = expr;
1114 enum tree_code code = TREE_CODE (expr);
1115 enum tree_code_class kind = TREE_CODE_CLASS (code);
1116 location_t loc = EXPR_LOCATION (expr);
1117 tree op0, op1, op2, op3;
1118 tree orig_op0, orig_op1, orig_op2;
1119 bool op0_const = true, op1_const = true, op2_const = true;
1120 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1121 bool nowarning = TREE_NO_WARNING (expr);
1122 bool unused_p;
1124 /* This function is not relevant to C++ because C++ folds while
1125 parsing, and may need changes to be correct for C++ when C++
1126 stops folding while parsing. */
1127 if (c_dialect_cxx ())
1128 gcc_unreachable ();
1130 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1131 anything else not counted as an expression cannot usefully be
1132 folded further at this point. */
1133 if (!IS_EXPR_CODE_CLASS (kind)
1134 || kind == tcc_statement
1135 || code == SAVE_EXPR)
1136 return expr;
1138 /* Operands of variable-length expressions (function calls) have
1139 already been folded, as have __builtin_* function calls, and such
1140 expressions cannot occur in constant expressions. */
1141 if (kind == tcc_vl_exp)
1143 *maybe_const_operands = false;
1144 ret = fold (expr);
1145 goto out;
1148 if (code == C_MAYBE_CONST_EXPR)
1150 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1151 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1152 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1153 *maybe_const_operands = false;
1154 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1155 *maybe_const_itself = false;
1156 if (pre && !in_init)
1157 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1158 else
1159 ret = inner;
1160 goto out;
1163 /* Assignment, increment, decrement, function call and comma
1164 operators, and statement expressions, cannot occur in constant
1165 expressions if evaluated / outside of sizeof. (Function calls
1166 were handled above, though VA_ARG_EXPR is treated like a function
1167 call here, and statement expressions are handled through
1168 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1169 switch (code)
1171 case MODIFY_EXPR:
1172 case PREDECREMENT_EXPR:
1173 case PREINCREMENT_EXPR:
1174 case POSTDECREMENT_EXPR:
1175 case POSTINCREMENT_EXPR:
1176 case COMPOUND_EXPR:
1177 *maybe_const_operands = false;
1178 break;
1180 case VA_ARG_EXPR:
1181 case TARGET_EXPR:
1182 case BIND_EXPR:
1183 case OBJ_TYPE_REF:
1184 *maybe_const_operands = false;
1185 ret = fold (expr);
1186 goto out;
1188 default:
1189 break;
1192 /* Fold individual tree codes as appropriate. */
1193 switch (code)
1195 case COMPOUND_LITERAL_EXPR:
1196 /* Any non-constancy will have been marked in a containing
1197 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1198 goto out;
1200 case COMPONENT_REF:
1201 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1202 op1 = TREE_OPERAND (expr, 1);
1203 op2 = TREE_OPERAND (expr, 2);
1204 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1205 maybe_const_itself);
1206 STRIP_TYPE_NOPS (op0);
1207 if (op0 != orig_op0)
1208 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1209 if (ret != expr)
1211 TREE_READONLY (ret) = TREE_READONLY (expr);
1212 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1214 goto out;
1216 case ARRAY_REF:
1217 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1218 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1219 op2 = TREE_OPERAND (expr, 2);
1220 op3 = TREE_OPERAND (expr, 3);
1221 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1222 maybe_const_itself);
1223 STRIP_TYPE_NOPS (op0);
1224 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1225 maybe_const_itself);
1226 STRIP_TYPE_NOPS (op1);
1227 op1 = decl_constant_value_for_optimization (op1);
1228 if (op0 != orig_op0 || op1 != orig_op1)
1229 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1230 if (ret != expr)
1232 TREE_READONLY (ret) = TREE_READONLY (expr);
1233 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1234 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1236 ret = fold (ret);
1237 goto out;
1239 case COMPOUND_EXPR:
1240 case MODIFY_EXPR:
1241 case PREDECREMENT_EXPR:
1242 case PREINCREMENT_EXPR:
1243 case POSTDECREMENT_EXPR:
1244 case POSTINCREMENT_EXPR:
1245 case PLUS_EXPR:
1246 case MINUS_EXPR:
1247 case MULT_EXPR:
1248 case POINTER_PLUS_EXPR:
1249 case TRUNC_DIV_EXPR:
1250 case CEIL_DIV_EXPR:
1251 case FLOOR_DIV_EXPR:
1252 case TRUNC_MOD_EXPR:
1253 case RDIV_EXPR:
1254 case EXACT_DIV_EXPR:
1255 case LSHIFT_EXPR:
1256 case RSHIFT_EXPR:
1257 case BIT_IOR_EXPR:
1258 case BIT_XOR_EXPR:
1259 case BIT_AND_EXPR:
1260 case LT_EXPR:
1261 case LE_EXPR:
1262 case GT_EXPR:
1263 case GE_EXPR:
1264 case EQ_EXPR:
1265 case NE_EXPR:
1266 case COMPLEX_EXPR:
1267 case TRUTH_AND_EXPR:
1268 case TRUTH_OR_EXPR:
1269 case TRUTH_XOR_EXPR:
1270 case UNORDERED_EXPR:
1271 case ORDERED_EXPR:
1272 case UNLT_EXPR:
1273 case UNLE_EXPR:
1274 case UNGT_EXPR:
1275 case UNGE_EXPR:
1276 case UNEQ_EXPR:
1277 /* Binary operations evaluating both arguments (increment and
1278 decrement are binary internally in GCC). */
1279 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1280 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1281 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1282 maybe_const_itself);
1283 STRIP_TYPE_NOPS (op0);
1284 if (code != MODIFY_EXPR
1285 && code != PREDECREMENT_EXPR
1286 && code != PREINCREMENT_EXPR
1287 && code != POSTDECREMENT_EXPR
1288 && code != POSTINCREMENT_EXPR)
1289 op0 = decl_constant_value_for_optimization (op0);
1290 /* The RHS of a MODIFY_EXPR was fully folded when building that
1291 expression for the sake of conversion warnings. */
1292 if (code != MODIFY_EXPR)
1293 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1294 maybe_const_itself);
1295 STRIP_TYPE_NOPS (op1);
1296 op1 = decl_constant_value_for_optimization (op1);
1297 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1298 ret = in_init
1299 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1300 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1301 else
1302 ret = fold (expr);
1303 if (TREE_OVERFLOW_P (ret)
1304 && !TREE_OVERFLOW_P (op0)
1305 && !TREE_OVERFLOW_P (op1))
1306 overflow_warning (EXPR_LOCATION (expr), ret);
1307 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1308 && TREE_CODE (orig_op1) != INTEGER_CST
1309 && TREE_CODE (op1) == INTEGER_CST
1310 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1311 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1312 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1313 && c_inhibit_evaluation_warnings == 0)
1315 if (tree_int_cst_sgn (op1) < 0)
1316 warning_at (loc, 0, (code == LSHIFT_EXPR
1317 ? G_("left shift count is negative")
1318 : G_("right shift count is negative")));
1319 else if (compare_tree_int (op1,
1320 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1321 >= 0)
1322 warning_at (loc, 0, (code == LSHIFT_EXPR
1323 ? G_("left shift count >= width of type")
1324 : G_("right shift count >= width of type")));
1326 goto out;
1328 case INDIRECT_REF:
1329 case FIX_TRUNC_EXPR:
1330 case FLOAT_EXPR:
1331 CASE_CONVERT:
1332 case VIEW_CONVERT_EXPR:
1333 case NON_LVALUE_EXPR:
1334 case NEGATE_EXPR:
1335 case BIT_NOT_EXPR:
1336 case TRUTH_NOT_EXPR:
1337 case ADDR_EXPR:
1338 case CONJ_EXPR:
1339 case REALPART_EXPR:
1340 case IMAGPART_EXPR:
1341 /* Unary operations. */
1342 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1343 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1344 maybe_const_itself);
1345 STRIP_TYPE_NOPS (op0);
1346 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1347 op0 = decl_constant_value_for_optimization (op0);
1348 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1349 not prepared to deal with them if they occur in initializers. */
1350 if (op0 != orig_op0
1351 && code == ADDR_EXPR
1352 && (op1 = get_base_address (op0)) != NULL_TREE
1353 && TREE_CODE (op1) == INDIRECT_REF
1354 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1355 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1356 else if (op0 != orig_op0 || in_init)
1357 ret = in_init
1358 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1359 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1360 else
1361 ret = fold (expr);
1362 if (code == INDIRECT_REF
1363 && ret != expr
1364 && TREE_CODE (ret) == INDIRECT_REF)
1366 TREE_READONLY (ret) = TREE_READONLY (expr);
1367 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1368 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1370 switch (code)
1372 case FIX_TRUNC_EXPR:
1373 case FLOAT_EXPR:
1374 CASE_CONVERT:
1375 /* Don't warn about explicit conversions. We will already
1376 have warned about suspect implicit conversions. */
1377 break;
1379 default:
1380 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1381 overflow_warning (EXPR_LOCATION (expr), ret);
1382 break;
1384 goto out;
1386 case TRUTH_ANDIF_EXPR:
1387 case TRUTH_ORIF_EXPR:
1388 /* Binary operations not necessarily evaluating both
1389 arguments. */
1390 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1391 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1392 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1393 STRIP_TYPE_NOPS (op0);
1395 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1396 ? truthvalue_false_node
1397 : truthvalue_true_node));
1398 c_disable_warnings (unused_p);
1399 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1400 STRIP_TYPE_NOPS (op1);
1401 c_enable_warnings (unused_p);
1403 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1404 ret = in_init
1405 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1406 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1407 else
1408 ret = fold (expr);
1409 *maybe_const_operands &= op0_const;
1410 *maybe_const_itself &= op0_const_self;
1411 if (!(flag_isoc99
1412 && op0_const
1413 && op0_const_self
1414 && (code == TRUTH_ANDIF_EXPR
1415 ? op0 == truthvalue_false_node
1416 : op0 == truthvalue_true_node)))
1417 *maybe_const_operands &= op1_const;
1418 if (!(op0_const
1419 && op0_const_self
1420 && (code == TRUTH_ANDIF_EXPR
1421 ? op0 == truthvalue_false_node
1422 : op0 == truthvalue_true_node)))
1423 *maybe_const_itself &= op1_const_self;
1424 goto out;
1426 case COND_EXPR:
1427 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1428 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1429 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1430 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1432 STRIP_TYPE_NOPS (op0);
1433 c_disable_warnings (op0 == truthvalue_false_node);
1434 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1435 STRIP_TYPE_NOPS (op1);
1436 c_enable_warnings (op0 == truthvalue_false_node);
1438 c_disable_warnings (op0 == truthvalue_true_node);
1439 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1440 STRIP_TYPE_NOPS (op2);
1441 c_enable_warnings (op0 == truthvalue_true_node);
1443 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1444 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1445 else
1446 ret = fold (expr);
1447 *maybe_const_operands &= op0_const;
1448 *maybe_const_itself &= op0_const_self;
1449 if (!(flag_isoc99
1450 && op0_const
1451 && op0_const_self
1452 && op0 == truthvalue_false_node))
1453 *maybe_const_operands &= op1_const;
1454 if (!(op0_const
1455 && op0_const_self
1456 && op0 == truthvalue_false_node))
1457 *maybe_const_itself &= op1_const_self;
1458 if (!(flag_isoc99
1459 && op0_const
1460 && op0_const_self
1461 && op0 == truthvalue_true_node))
1462 *maybe_const_operands &= op2_const;
1463 if (!(op0_const
1464 && op0_const_self
1465 && op0 == truthvalue_true_node))
1466 *maybe_const_itself &= op2_const_self;
1467 goto out;
1469 case EXCESS_PRECISION_EXPR:
1470 /* Each case where an operand with excess precision may be
1471 encountered must remove the EXCESS_PRECISION_EXPR around
1472 inner operands and possibly put one around the whole
1473 expression or possibly convert to the semantic type (which
1474 c_fully_fold does); we cannot tell at this stage which is
1475 appropriate in any particular case. */
1476 gcc_unreachable ();
1478 default:
1479 /* Various codes may appear through folding built-in functions
1480 and their arguments. */
1481 goto out;
1484 out:
1485 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1486 have been done by this point, so remove them again. */
1487 nowarning |= TREE_NO_WARNING (ret);
1488 STRIP_TYPE_NOPS (ret);
1489 if (nowarning && !TREE_NO_WARNING (ret))
1491 if (!CAN_HAVE_LOCATION_P (ret))
1492 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1493 TREE_NO_WARNING (ret) = 1;
1495 if (ret != expr)
1496 protected_set_expr_location (ret, loc);
1497 return ret;
1500 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1501 return EXP. Otherwise, return either EXP or its known constant
1502 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1503 Is the BLKmode test appropriate? */
1505 tree
1506 decl_constant_value_for_optimization (tree exp)
1508 tree ret;
1510 /* This function is only used by C, for c_fully_fold and other
1511 optimization, and may not be correct for C++. */
1512 if (c_dialect_cxx ())
1513 gcc_unreachable ();
1515 if (!optimize
1516 || TREE_CODE (exp) != VAR_DECL
1517 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1518 || DECL_MODE (exp) == BLKmode)
1519 return exp;
1521 ret = decl_constant_value (exp);
1522 /* Avoid unwanted tree sharing between the initializer and current
1523 function's body where the tree can be modified e.g. by the
1524 gimplifier. */
1525 if (ret != exp && TREE_STATIC (exp))
1526 ret = unshare_expr (ret);
1527 return ret;
1530 /* Print a warning if a constant expression had overflow in folding.
1531 Invoke this function on every expression that the language
1532 requires to be a constant expression.
1533 Note the ANSI C standard says it is erroneous for a
1534 constant expression to overflow. */
1536 void
1537 constant_expression_warning (tree value)
1539 if (warn_overflow && pedantic
1540 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1541 || TREE_CODE (value) == FIXED_CST
1542 || TREE_CODE (value) == VECTOR_CST
1543 || TREE_CODE (value) == COMPLEX_CST)
1544 && TREE_OVERFLOW (value))
1545 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1548 /* The same as above but print an unconditional error. */
1549 void
1550 constant_expression_error (tree value)
1552 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1553 || TREE_CODE (value) == FIXED_CST
1554 || TREE_CODE (value) == VECTOR_CST
1555 || TREE_CODE (value) == COMPLEX_CST)
1556 && TREE_OVERFLOW (value))
1557 error ("overflow in constant expression");
1560 /* Print a warning if an expression had overflow in folding and its
1561 operands hadn't.
1563 Invoke this function on every expression that
1564 (1) appears in the source code, and
1565 (2) is a constant expression that overflowed, and
1566 (3) is not already checked by convert_and_check;
1567 however, do not invoke this function on operands of explicit casts
1568 or when the expression is the result of an operator and any operand
1569 already overflowed. */
1571 void
1572 overflow_warning (location_t loc, tree value)
1574 if (c_inhibit_evaluation_warnings != 0)
1575 return;
1577 switch (TREE_CODE (value))
1579 case INTEGER_CST:
1580 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1581 break;
1583 case REAL_CST:
1584 warning_at (loc, OPT_Woverflow,
1585 "floating point overflow in expression");
1586 break;
1588 case FIXED_CST:
1589 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1590 break;
1592 case VECTOR_CST:
1593 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1594 break;
1596 case COMPLEX_CST:
1597 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1598 warning_at (loc, OPT_Woverflow,
1599 "complex integer overflow in expression");
1600 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1601 warning_at (loc, OPT_Woverflow,
1602 "complex floating point overflow in expression");
1603 break;
1605 default:
1606 break;
1610 /* Warn about uses of logical || / && operator in a context where it
1611 is likely that the bitwise equivalent was intended by the
1612 programmer. We have seen an expression in which CODE is a binary
1613 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1614 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1615 void
1616 warn_logical_operator (location_t location, enum tree_code code, tree type,
1617 enum tree_code code_left, tree op_left,
1618 enum tree_code ARG_UNUSED (code_right), tree op_right)
1620 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1621 int in0_p, in1_p, in_p;
1622 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1623 bool strict_overflow_p = false;
1625 if (code != TRUTH_ANDIF_EXPR
1626 && code != TRUTH_AND_EXPR
1627 && code != TRUTH_ORIF_EXPR
1628 && code != TRUTH_OR_EXPR)
1629 return;
1631 /* Warn if &&/|| are being used in a context where it is
1632 likely that the bitwise equivalent was intended by the
1633 programmer. That is, an expression such as op && MASK
1634 where op should not be any boolean expression, nor a
1635 constant, and mask seems to be a non-boolean integer constant. */
1636 if (!truth_value_p (code_left)
1637 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1638 && !CONSTANT_CLASS_P (op_left)
1639 && !TREE_NO_WARNING (op_left)
1640 && TREE_CODE (op_right) == INTEGER_CST
1641 && !integer_zerop (op_right)
1642 && !integer_onep (op_right))
1644 if (or_op)
1645 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1646 " applied to non-boolean constant");
1647 else
1648 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1649 " applied to non-boolean constant");
1650 TREE_NO_WARNING (op_left) = true;
1651 return;
1654 /* We do not warn for constants because they are typical of macro
1655 expansions that test for features. */
1656 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1657 return;
1659 /* This warning only makes sense with logical operands. */
1660 if (!(truth_value_p (TREE_CODE (op_left))
1661 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1662 || !(truth_value_p (TREE_CODE (op_right))
1663 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1664 return;
1667 /* We first test whether either side separately is trivially true
1668 (with OR) or trivially false (with AND). If so, do not warn.
1669 This is a common idiom for testing ranges of data types in
1670 portable code. */
1671 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1672 if (!lhs)
1673 return;
1674 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1675 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1677 /* If this is an OR operation, invert both sides; now, the result
1678 should be always false to get a warning. */
1679 if (or_op)
1680 in0_p = !in0_p;
1682 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1683 if (tem && integer_zerop (tem))
1684 return;
1686 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1687 if (!rhs)
1688 return;
1689 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1690 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1692 /* If this is an OR operation, invert both sides; now, the result
1693 should be always false to get a warning. */
1694 if (or_op)
1695 in1_p = !in1_p;
1697 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1698 if (tem && integer_zerop (tem))
1699 return;
1701 /* If both expressions have the same operand, if we can merge the
1702 ranges, and if the range test is always false, then warn. */
1703 if (operand_equal_p (lhs, rhs, 0)
1704 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1705 in1_p, low1, high1)
1706 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1707 type, lhs, in_p, low, high))
1708 && integer_zerop (tem))
1710 if (or_op)
1711 warning_at (location, OPT_Wlogical_op,
1712 "logical %<or%> "
1713 "of collectively exhaustive tests is always true");
1714 else
1715 warning_at (location, OPT_Wlogical_op,
1716 "logical %<and%> "
1717 "of mutually exclusive tests is always false");
1722 /* Warn if EXP contains any computations whose results are not used.
1723 Return true if a warning is printed; false otherwise. LOCUS is the
1724 (potential) location of the expression. */
1726 bool
1727 warn_if_unused_value (const_tree exp, location_t locus)
1729 restart:
1730 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1731 return false;
1733 /* Don't warn about void constructs. This includes casting to void,
1734 void function calls, and statement expressions with a final cast
1735 to void. */
1736 if (VOID_TYPE_P (TREE_TYPE (exp)))
1737 return false;
1739 if (EXPR_HAS_LOCATION (exp))
1740 locus = EXPR_LOCATION (exp);
1742 switch (TREE_CODE (exp))
1744 case PREINCREMENT_EXPR:
1745 case POSTINCREMENT_EXPR:
1746 case PREDECREMENT_EXPR:
1747 case POSTDECREMENT_EXPR:
1748 case MODIFY_EXPR:
1749 case INIT_EXPR:
1750 case TARGET_EXPR:
1751 case CALL_EXPR:
1752 case TRY_CATCH_EXPR:
1753 case WITH_CLEANUP_EXPR:
1754 case EXIT_EXPR:
1755 case VA_ARG_EXPR:
1756 return false;
1758 case BIND_EXPR:
1759 /* For a binding, warn if no side effect within it. */
1760 exp = BIND_EXPR_BODY (exp);
1761 goto restart;
1763 case SAVE_EXPR:
1764 case NON_LVALUE_EXPR:
1765 case NOP_EXPR:
1766 exp = TREE_OPERAND (exp, 0);
1767 goto restart;
1769 case TRUTH_ORIF_EXPR:
1770 case TRUTH_ANDIF_EXPR:
1771 /* In && or ||, warn if 2nd operand has no side effect. */
1772 exp = TREE_OPERAND (exp, 1);
1773 goto restart;
1775 case COMPOUND_EXPR:
1776 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1777 return true;
1778 /* Let people do `(foo (), 0)' without a warning. */
1779 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1780 return false;
1781 exp = TREE_OPERAND (exp, 1);
1782 goto restart;
1784 case COND_EXPR:
1785 /* If this is an expression with side effects, don't warn; this
1786 case commonly appears in macro expansions. */
1787 if (TREE_SIDE_EFFECTS (exp))
1788 return false;
1789 goto warn;
1791 case INDIRECT_REF:
1792 /* Don't warn about automatic dereferencing of references, since
1793 the user cannot control it. */
1794 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1796 exp = TREE_OPERAND (exp, 0);
1797 goto restart;
1799 /* Fall through. */
1801 default:
1802 /* Referencing a volatile value is a side effect, so don't warn. */
1803 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1804 && TREE_THIS_VOLATILE (exp))
1805 return false;
1807 /* If this is an expression which has no operands, there is no value
1808 to be unused. There are no such language-independent codes,
1809 but front ends may define such. */
1810 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1811 return false;
1813 warn:
1814 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1819 /* Print a warning about casts that might indicate violation
1820 of strict aliasing rules if -Wstrict-aliasing is used and
1821 strict aliasing mode is in effect. OTYPE is the original
1822 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1824 bool
1825 strict_aliasing_warning (tree otype, tree type, tree expr)
1827 /* Strip pointer conversion chains and get to the correct original type. */
1828 STRIP_NOPS (expr);
1829 otype = TREE_TYPE (expr);
1831 if (!(flag_strict_aliasing
1832 && POINTER_TYPE_P (type)
1833 && POINTER_TYPE_P (otype)
1834 && !VOID_TYPE_P (TREE_TYPE (type)))
1835 /* If the type we are casting to is a ref-all pointer
1836 dereferencing it is always valid. */
1837 || TYPE_REF_CAN_ALIAS_ALL (type))
1838 return false;
1840 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1841 && (DECL_P (TREE_OPERAND (expr, 0))
1842 || handled_component_p (TREE_OPERAND (expr, 0))))
1844 /* Casting the address of an object to non void pointer. Warn
1845 if the cast breaks type based aliasing. */
1846 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1848 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1849 "might break strict-aliasing rules");
1850 return true;
1852 else
1854 /* warn_strict_aliasing >= 3. This includes the default (3).
1855 Only warn if the cast is dereferenced immediately. */
1856 alias_set_type set1 =
1857 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1858 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1860 if (set1 != set2 && set2 != 0
1861 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1863 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1864 "pointer will break strict-aliasing rules");
1865 return true;
1867 else if (warn_strict_aliasing == 2
1868 && !alias_sets_must_conflict_p (set1, set2))
1870 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1871 "pointer might break strict-aliasing rules");
1872 return true;
1876 else
1877 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1879 /* At this level, warn for any conversions, even if an address is
1880 not taken in the same statement. This will likely produce many
1881 false positives, but could be useful to pinpoint problems that
1882 are not revealed at higher levels. */
1883 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1884 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1885 if (!COMPLETE_TYPE_P (type)
1886 || !alias_sets_must_conflict_p (set1, set2))
1888 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1889 "pointer might break strict-aliasing rules");
1890 return true;
1894 return false;
1897 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1898 sizeof as last operand of certain builtins. */
1900 void
1901 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1902 vec<tree, va_gc> *params, tree *sizeof_arg,
1903 bool (*comp_types) (tree, tree))
1905 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1906 bool strop = false, cmp = false;
1907 unsigned int idx = ~0;
1908 location_t loc;
1910 if (TREE_CODE (callee) != FUNCTION_DECL
1911 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1912 || vec_safe_length (params) <= 1)
1913 return;
1915 switch (DECL_FUNCTION_CODE (callee))
1917 case BUILT_IN_STRNCMP:
1918 case BUILT_IN_STRNCASECMP:
1919 cmp = true;
1920 /* FALLTHRU */
1921 case BUILT_IN_STRNCPY:
1922 case BUILT_IN_STRNCPY_CHK:
1923 case BUILT_IN_STRNCAT:
1924 case BUILT_IN_STRNCAT_CHK:
1925 case BUILT_IN_STPNCPY:
1926 case BUILT_IN_STPNCPY_CHK:
1927 strop = true;
1928 /* FALLTHRU */
1929 case BUILT_IN_MEMCPY:
1930 case BUILT_IN_MEMCPY_CHK:
1931 case BUILT_IN_MEMMOVE:
1932 case BUILT_IN_MEMMOVE_CHK:
1933 if (params->length () < 3)
1934 return;
1935 src = (*params)[1];
1936 dest = (*params)[0];
1937 idx = 2;
1938 break;
1939 case BUILT_IN_BCOPY:
1940 if (params->length () < 3)
1941 return;
1942 src = (*params)[0];
1943 dest = (*params)[1];
1944 idx = 2;
1945 break;
1946 case BUILT_IN_MEMCMP:
1947 case BUILT_IN_BCMP:
1948 if (params->length () < 3)
1949 return;
1950 src = (*params)[1];
1951 dest = (*params)[0];
1952 idx = 2;
1953 cmp = true;
1954 break;
1955 case BUILT_IN_MEMSET:
1956 case BUILT_IN_MEMSET_CHK:
1957 if (params->length () < 3)
1958 return;
1959 dest = (*params)[0];
1960 idx = 2;
1961 break;
1962 case BUILT_IN_BZERO:
1963 dest = (*params)[0];
1964 idx = 1;
1965 break;
1966 case BUILT_IN_STRNDUP:
1967 src = (*params)[0];
1968 strop = true;
1969 idx = 1;
1970 break;
1971 case BUILT_IN_MEMCHR:
1972 if (params->length () < 3)
1973 return;
1974 src = (*params)[0];
1975 idx = 2;
1976 break;
1977 case BUILT_IN_SNPRINTF:
1978 case BUILT_IN_SNPRINTF_CHK:
1979 case BUILT_IN_VSNPRINTF:
1980 case BUILT_IN_VSNPRINTF_CHK:
1981 dest = (*params)[0];
1982 idx = 1;
1983 strop = true;
1984 break;
1985 default:
1986 break;
1989 if (idx >= 3)
1990 return;
1992 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1993 return;
1995 type = TYPE_P (sizeof_arg[idx])
1996 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1997 if (!POINTER_TYPE_P (type))
1998 return;
2000 if (dest
2001 && (tem = tree_strip_nop_conversions (dest))
2002 && POINTER_TYPE_P (TREE_TYPE (tem))
2003 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2004 return;
2006 if (src
2007 && (tem = tree_strip_nop_conversions (src))
2008 && POINTER_TYPE_P (TREE_TYPE (tem))
2009 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2010 return;
2012 loc = sizeof_arg_loc[idx];
2014 if (dest && !cmp)
2016 if (!TYPE_P (sizeof_arg[idx])
2017 && operand_equal_p (dest, sizeof_arg[idx], 0)
2018 && comp_types (TREE_TYPE (dest), type))
2020 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2021 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2022 "argument to %<sizeof%> in %qD call is the same "
2023 "expression as the destination; did you mean to "
2024 "remove the addressof?", callee);
2025 else if ((TYPE_PRECISION (TREE_TYPE (type))
2026 == TYPE_PRECISION (char_type_node))
2027 || strop)
2028 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2029 "argument to %<sizeof%> in %qD call is the same "
2030 "expression as the destination; did you mean to "
2031 "provide an explicit length?", callee);
2032 else
2033 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2034 "argument to %<sizeof%> in %qD call is the same "
2035 "expression as the destination; did you mean to "
2036 "dereference it?", callee);
2037 return;
2040 if (POINTER_TYPE_P (TREE_TYPE (dest))
2041 && !strop
2042 && comp_types (TREE_TYPE (dest), type)
2043 && !VOID_TYPE_P (TREE_TYPE (type)))
2045 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2046 "argument to %<sizeof%> in %qD call is the same "
2047 "pointer type %qT as the destination; expected %qT "
2048 "or an explicit length", callee, TREE_TYPE (dest),
2049 TREE_TYPE (TREE_TYPE (dest)));
2050 return;
2054 if (src && !cmp)
2056 if (!TYPE_P (sizeof_arg[idx])
2057 && operand_equal_p (src, sizeof_arg[idx], 0)
2058 && comp_types (TREE_TYPE (src), type))
2060 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2061 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2062 "argument to %<sizeof%> in %qD call is the same "
2063 "expression as the source; did you mean to "
2064 "remove the addressof?", callee);
2065 else if ((TYPE_PRECISION (TREE_TYPE (type))
2066 == TYPE_PRECISION (char_type_node))
2067 || strop)
2068 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2069 "argument to %<sizeof%> in %qD call is the same "
2070 "expression as the source; did you mean to "
2071 "provide an explicit length?", callee);
2072 else
2073 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2074 "argument to %<sizeof%> in %qD call is the same "
2075 "expression as the source; did you mean to "
2076 "dereference it?", callee);
2077 return;
2080 if (POINTER_TYPE_P (TREE_TYPE (src))
2081 && !strop
2082 && comp_types (TREE_TYPE (src), type)
2083 && !VOID_TYPE_P (TREE_TYPE (type)))
2085 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2086 "argument to %<sizeof%> in %qD call is the same "
2087 "pointer type %qT as the source; expected %qT "
2088 "or an explicit length", callee, TREE_TYPE (src),
2089 TREE_TYPE (TREE_TYPE (src)));
2090 return;
2094 if (dest)
2096 if (!TYPE_P (sizeof_arg[idx])
2097 && operand_equal_p (dest, sizeof_arg[idx], 0)
2098 && comp_types (TREE_TYPE (dest), type))
2100 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2101 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2102 "argument to %<sizeof%> in %qD call is the same "
2103 "expression as the first source; did you mean to "
2104 "remove the addressof?", callee);
2105 else if ((TYPE_PRECISION (TREE_TYPE (type))
2106 == TYPE_PRECISION (char_type_node))
2107 || strop)
2108 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2109 "argument to %<sizeof%> in %qD call is the same "
2110 "expression as the first source; did you mean to "
2111 "provide an explicit length?", callee);
2112 else
2113 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2114 "argument to %<sizeof%> in %qD call is the same "
2115 "expression as the first source; did you mean to "
2116 "dereference it?", callee);
2117 return;
2120 if (POINTER_TYPE_P (TREE_TYPE (dest))
2121 && !strop
2122 && comp_types (TREE_TYPE (dest), type)
2123 && !VOID_TYPE_P (TREE_TYPE (type)))
2125 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2126 "argument to %<sizeof%> in %qD call is the same "
2127 "pointer type %qT as the first source; expected %qT "
2128 "or an explicit length", callee, TREE_TYPE (dest),
2129 TREE_TYPE (TREE_TYPE (dest)));
2130 return;
2134 if (src)
2136 if (!TYPE_P (sizeof_arg[idx])
2137 && operand_equal_p (src, sizeof_arg[idx], 0)
2138 && comp_types (TREE_TYPE (src), type))
2140 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2141 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2142 "argument to %<sizeof%> in %qD call is the same "
2143 "expression as the second source; did you mean to "
2144 "remove the addressof?", callee);
2145 else if ((TYPE_PRECISION (TREE_TYPE (type))
2146 == TYPE_PRECISION (char_type_node))
2147 || strop)
2148 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2149 "argument to %<sizeof%> in %qD call is the same "
2150 "expression as the second source; did you mean to "
2151 "provide an explicit length?", callee);
2152 else
2153 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2154 "argument to %<sizeof%> in %qD call is the same "
2155 "expression as the second source; did you mean to "
2156 "dereference it?", callee);
2157 return;
2160 if (POINTER_TYPE_P (TREE_TYPE (src))
2161 && !strop
2162 && comp_types (TREE_TYPE (src), type)
2163 && !VOID_TYPE_P (TREE_TYPE (type)))
2165 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2166 "argument to %<sizeof%> in %qD call is the same "
2167 "pointer type %qT as the second source; expected %qT "
2168 "or an explicit length", callee, TREE_TYPE (src),
2169 TREE_TYPE (TREE_TYPE (src)));
2170 return;
2176 /* Warn for unlikely, improbable, or stupid DECL declarations
2177 of `main'. */
2179 void
2180 check_main_parameter_types (tree decl)
2182 function_args_iterator iter;
2183 tree type;
2184 int argct = 0;
2186 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2188 /* XXX void_type_node belies the abstraction. */
2189 if (type == void_type_node || type == error_mark_node )
2190 break;
2192 ++argct;
2193 switch (argct)
2195 case 1:
2196 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2197 pedwarn (input_location, OPT_Wmain,
2198 "first argument of %q+D should be %<int%>", decl);
2199 break;
2201 case 2:
2202 if (TREE_CODE (type) != POINTER_TYPE
2203 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2204 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2205 != char_type_node))
2206 pedwarn (input_location, OPT_Wmain,
2207 "second argument of %q+D should be %<char **%>", decl);
2208 break;
2210 case 3:
2211 if (TREE_CODE (type) != POINTER_TYPE
2212 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2213 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2214 != char_type_node))
2215 pedwarn (input_location, OPT_Wmain,
2216 "third argument of %q+D should probably be "
2217 "%<char **%>", decl);
2218 break;
2222 /* It is intentional that this message does not mention the third
2223 argument because it's only mentioned in an appendix of the
2224 standard. */
2225 if (argct > 0 && (argct < 2 || argct > 3))
2226 pedwarn (input_location, OPT_Wmain,
2227 "%q+D takes only zero or two arguments", decl);
2230 /* vector_targets_convertible_p is used for vector pointer types. The
2231 callers perform various checks that the qualifiers are satisfactory,
2232 while OTOH vector_targets_convertible_p ignores the number of elements
2233 in the vectors. That's fine with vector pointers as we can consider,
2234 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2235 and that does not require and conversion of the pointer values.
2236 In contrast, vector_types_convertible_p and
2237 vector_types_compatible_elements_p are used for vector value types. */
2238 /* True if pointers to distinct types T1 and T2 can be converted to
2239 each other without an explicit cast. Only returns true for opaque
2240 vector types. */
2241 bool
2242 vector_targets_convertible_p (const_tree t1, const_tree t2)
2244 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2245 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2246 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2247 return true;
2249 return false;
2252 /* vector_types_convertible_p is used for vector value types.
2253 It could in principle call vector_targets_convertible_p as a subroutine,
2254 but then the check for vector type would be duplicated with its callers,
2255 and also the purpose of vector_targets_convertible_p would become
2256 muddled.
2257 Where vector_types_convertible_p returns true, a conversion might still be
2258 needed to make the types match.
2259 In contrast, vector_targets_convertible_p is used for vector pointer
2260 values, and vector_types_compatible_elements_p is used specifically
2261 in the context for binary operators, as a check if use is possible without
2262 conversion. */
2263 /* True if vector types T1 and T2 can be converted to each other
2264 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2265 can only be converted with -flax-vector-conversions yet that is not
2266 in effect, emit a note telling the user about that option if such
2267 a note has not previously been emitted. */
2268 bool
2269 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2271 static bool emitted_lax_note = false;
2272 bool convertible_lax;
2274 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2275 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2276 return true;
2278 convertible_lax =
2279 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2280 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2281 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2282 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2283 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2285 if (!convertible_lax || flag_lax_vector_conversions)
2286 return convertible_lax;
2288 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2289 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2290 return true;
2292 if (emit_lax_note && !emitted_lax_note)
2294 emitted_lax_note = true;
2295 inform (input_location, "use -flax-vector-conversions to permit "
2296 "conversions between vectors with differing "
2297 "element types or numbers of subparts");
2300 return false;
2303 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2304 and have vector types, V0 has the same type as V1, and the number of
2305 elements of V0, V1, MASK is the same.
2307 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2308 called with two arguments. In this case implementation passes the
2309 first argument twice in order to share the same tree code. This fact
2310 could enable the mask-values being twice the vector length. This is
2311 an implementation accident and this semantics is not guaranteed to
2312 the user. */
2313 tree
2314 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2315 bool complain)
2317 tree ret;
2318 bool wrap = true;
2319 bool maybe_const = false;
2320 bool two_arguments = false;
2322 if (v1 == NULL_TREE)
2324 two_arguments = true;
2325 v1 = v0;
2328 if (v0 == error_mark_node || v1 == error_mark_node
2329 || mask == error_mark_node)
2330 return error_mark_node;
2332 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2333 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2335 if (complain)
2336 error_at (loc, "__builtin_shuffle last argument must "
2337 "be an integer vector");
2338 return error_mark_node;
2341 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2342 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2344 if (complain)
2345 error_at (loc, "__builtin_shuffle arguments must be vectors");
2346 return error_mark_node;
2349 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2351 if (complain)
2352 error_at (loc, "__builtin_shuffle argument vectors must be of "
2353 "the same type");
2354 return error_mark_node;
2357 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2358 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2359 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2360 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2362 if (complain)
2363 error_at (loc, "__builtin_shuffle number of elements of the "
2364 "argument vector(s) and the mask vector should "
2365 "be the same");
2366 return error_mark_node;
2369 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2370 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2372 if (complain)
2373 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2374 "must have the same size as inner type of the mask");
2375 return error_mark_node;
2378 if (!c_dialect_cxx ())
2380 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2381 v0 = c_fully_fold (v0, false, &maybe_const);
2382 wrap &= maybe_const;
2384 if (two_arguments)
2385 v1 = v0 = save_expr (v0);
2386 else
2388 v1 = c_fully_fold (v1, false, &maybe_const);
2389 wrap &= maybe_const;
2392 mask = c_fully_fold (mask, false, &maybe_const);
2393 wrap &= maybe_const;
2395 else if (two_arguments)
2396 v1 = v0 = save_expr (v0);
2398 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2400 if (!c_dialect_cxx () && !wrap)
2401 ret = c_wrap_maybe_const (ret, true);
2403 return ret;
2406 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2407 to integral type. */
2409 static tree
2410 c_common_get_narrower (tree op, int *unsignedp_ptr)
2412 op = get_narrower (op, unsignedp_ptr);
2414 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2415 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2417 /* C++0x scoped enumerations don't implicitly convert to integral
2418 type; if we stripped an explicit conversion to a larger type we
2419 need to replace it so common_type will still work. */
2420 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2421 TYPE_UNSIGNED (TREE_TYPE (op)));
2422 op = fold_convert (type, op);
2424 return op;
2427 /* This is a helper function of build_binary_op.
2429 For certain operations if both args were extended from the same
2430 smaller type, do the arithmetic in that type and then extend.
2432 BITWISE indicates a bitwise operation.
2433 For them, this optimization is safe only if
2434 both args are zero-extended or both are sign-extended.
2435 Otherwise, we might change the result.
2436 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2437 but calculated in (unsigned short) it would be (unsigned short)-1.
2439 tree
2440 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2442 int unsigned0, unsigned1;
2443 tree arg0, arg1;
2444 int uns;
2445 tree type;
2447 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2448 excessive narrowing when we call get_narrower below. For
2449 example, suppose that OP0 is of unsigned int extended
2450 from signed char and that RESULT_TYPE is long long int.
2451 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2452 like
2454 (long long int) (unsigned int) signed_char
2456 which get_narrower would narrow down to
2458 (unsigned int) signed char
2460 If we do not cast OP0 first, get_narrower would return
2461 signed_char, which is inconsistent with the case of the
2462 explicit cast. */
2463 op0 = convert (result_type, op0);
2464 op1 = convert (result_type, op1);
2466 arg0 = c_common_get_narrower (op0, &unsigned0);
2467 arg1 = c_common_get_narrower (op1, &unsigned1);
2469 /* UNS is 1 if the operation to be done is an unsigned one. */
2470 uns = TYPE_UNSIGNED (result_type);
2472 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2473 but it *requires* conversion to FINAL_TYPE. */
2475 if ((TYPE_PRECISION (TREE_TYPE (op0))
2476 == TYPE_PRECISION (TREE_TYPE (arg0)))
2477 && TREE_TYPE (op0) != result_type)
2478 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2479 if ((TYPE_PRECISION (TREE_TYPE (op1))
2480 == TYPE_PRECISION (TREE_TYPE (arg1)))
2481 && TREE_TYPE (op1) != result_type)
2482 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2484 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2486 /* For bitwise operations, signedness of nominal type
2487 does not matter. Consider only how operands were extended. */
2488 if (bitwise)
2489 uns = unsigned0;
2491 /* Note that in all three cases below we refrain from optimizing
2492 an unsigned operation on sign-extended args.
2493 That would not be valid. */
2495 /* Both args variable: if both extended in same way
2496 from same width, do it in that width.
2497 Do it unsigned if args were zero-extended. */
2498 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2499 < TYPE_PRECISION (result_type))
2500 && (TYPE_PRECISION (TREE_TYPE (arg1))
2501 == TYPE_PRECISION (TREE_TYPE (arg0)))
2502 && unsigned0 == unsigned1
2503 && (unsigned0 || !uns))
2504 return c_common_signed_or_unsigned_type
2505 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2507 else if (TREE_CODE (arg0) == INTEGER_CST
2508 && (unsigned1 || !uns)
2509 && (TYPE_PRECISION (TREE_TYPE (arg1))
2510 < TYPE_PRECISION (result_type))
2511 && (type
2512 = c_common_signed_or_unsigned_type (unsigned1,
2513 TREE_TYPE (arg1)))
2514 && !POINTER_TYPE_P (type)
2515 && int_fits_type_p (arg0, type))
2516 return type;
2518 else if (TREE_CODE (arg1) == INTEGER_CST
2519 && (unsigned0 || !uns)
2520 && (TYPE_PRECISION (TREE_TYPE (arg0))
2521 < TYPE_PRECISION (result_type))
2522 && (type
2523 = c_common_signed_or_unsigned_type (unsigned0,
2524 TREE_TYPE (arg0)))
2525 && !POINTER_TYPE_P (type)
2526 && int_fits_type_p (arg1, type))
2527 return type;
2529 return result_type;
2532 /* Checks if expression EXPR of real/integer type cannot be converted
2533 to the real/integer type TYPE. Function returns true when:
2534 * EXPR is a constant which cannot be exactly converted to TYPE
2535 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2536 for EXPR type and TYPE being both integers or both real.
2537 * EXPR is not a constant of real type and TYPE is an integer.
2538 * EXPR is not a constant of integer type which cannot be
2539 exactly converted to real type.
2540 Function allows conversions between types of different signedness and
2541 does not return true in that case. Function can produce signedness
2542 warnings if PRODUCE_WARNS is true. */
2543 bool
2544 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2546 bool give_warning = false;
2547 tree expr_type = TREE_TYPE (expr);
2548 location_t loc = EXPR_LOC_OR_HERE (expr);
2550 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2552 /* Warn for real constant that is not an exact integer converted
2553 to integer type. */
2554 if (TREE_CODE (expr_type) == REAL_TYPE
2555 && TREE_CODE (type) == INTEGER_TYPE)
2557 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2558 give_warning = true;
2560 /* Warn for an integer constant that does not fit into integer type. */
2561 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2562 && TREE_CODE (type) == INTEGER_TYPE
2563 && !int_fits_type_p (expr, type))
2565 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2566 && tree_int_cst_sgn (expr) < 0)
2568 if (produce_warns)
2569 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2570 " implicitly converted to unsigned type");
2572 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2574 if (produce_warns)
2575 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2576 " constant value to negative integer");
2578 else
2579 give_warning = true;
2581 else if (TREE_CODE (type) == REAL_TYPE)
2583 /* Warn for an integer constant that does not fit into real type. */
2584 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2586 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2587 if (!exact_real_truncate (TYPE_MODE (type), &a))
2588 give_warning = true;
2590 /* Warn for a real constant that does not fit into a smaller
2591 real type. */
2592 else if (TREE_CODE (expr_type) == REAL_TYPE
2593 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2595 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2596 if (!exact_real_truncate (TYPE_MODE (type), &a))
2597 give_warning = true;
2601 else
2603 /* Warn for real types converted to integer types. */
2604 if (TREE_CODE (expr_type) == REAL_TYPE
2605 && TREE_CODE (type) == INTEGER_TYPE)
2606 give_warning = true;
2608 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2609 && TREE_CODE (type) == INTEGER_TYPE)
2611 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2612 expr = get_unwidened (expr, 0);
2613 expr_type = TREE_TYPE (expr);
2615 /* Don't warn for short y; short x = ((int)y & 0xff); */
2616 if (TREE_CODE (expr) == BIT_AND_EXPR
2617 || TREE_CODE (expr) == BIT_IOR_EXPR
2618 || TREE_CODE (expr) == BIT_XOR_EXPR)
2620 /* If both args were extended from a shortest type,
2621 use that type if that is safe. */
2622 expr_type = shorten_binary_op (expr_type,
2623 TREE_OPERAND (expr, 0),
2624 TREE_OPERAND (expr, 1),
2625 /* bitwise */1);
2627 if (TREE_CODE (expr) == BIT_AND_EXPR)
2629 tree op0 = TREE_OPERAND (expr, 0);
2630 tree op1 = TREE_OPERAND (expr, 1);
2631 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2632 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2634 /* If one of the operands is a non-negative constant
2635 that fits in the target type, then the type of the
2636 other operand does not matter. */
2637 if ((TREE_CODE (op0) == INTEGER_CST
2638 && int_fits_type_p (op0, c_common_signed_type (type))
2639 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2640 || (TREE_CODE (op1) == INTEGER_CST
2641 && int_fits_type_p (op1, c_common_signed_type (type))
2642 && int_fits_type_p (op1,
2643 c_common_unsigned_type (type))))
2644 return false;
2645 /* If constant is unsigned and fits in the target
2646 type, then the result will also fit. */
2647 else if ((TREE_CODE (op0) == INTEGER_CST
2648 && unsigned0
2649 && int_fits_type_p (op0, type))
2650 || (TREE_CODE (op1) == INTEGER_CST
2651 && unsigned1
2652 && int_fits_type_p (op1, type)))
2653 return false;
2656 /* Warn for integer types converted to smaller integer types. */
2657 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2658 give_warning = true;
2660 /* When they are the same width but different signedness,
2661 then the value may change. */
2662 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2663 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2664 /* Even when converted to a bigger type, if the type is
2665 unsigned but expr is signed, then negative values
2666 will be changed. */
2667 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2668 && produce_warns)
2669 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2670 "may change the sign of the result",
2671 type, expr_type);
2674 /* Warn for integer types converted to real types if and only if
2675 all the range of values of the integer type cannot be
2676 represented by the real type. */
2677 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2678 && TREE_CODE (type) == REAL_TYPE)
2680 tree type_low_bound, type_high_bound;
2681 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2683 /* Don't warn about char y = 0xff; float x = (int) y; */
2684 expr = get_unwidened (expr, 0);
2685 expr_type = TREE_TYPE (expr);
2687 type_low_bound = TYPE_MIN_VALUE (expr_type);
2688 type_high_bound = TYPE_MAX_VALUE (expr_type);
2689 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2690 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2692 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2693 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2694 give_warning = true;
2697 /* Warn for real types converted to smaller real types. */
2698 else if (TREE_CODE (expr_type) == REAL_TYPE
2699 && TREE_CODE (type) == REAL_TYPE
2700 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2701 give_warning = true;
2704 return give_warning;
2707 /* Warns if the conversion of EXPR to TYPE may alter a value.
2708 This is a helper function for warnings_for_convert_and_check. */
2710 static void
2711 conversion_warning (tree type, tree expr)
2713 tree expr_type = TREE_TYPE (expr);
2714 location_t loc = EXPR_LOC_OR_HERE (expr);
2716 if (!warn_conversion && !warn_sign_conversion)
2717 return;
2719 switch (TREE_CODE (expr))
2721 case EQ_EXPR:
2722 case NE_EXPR:
2723 case LE_EXPR:
2724 case GE_EXPR:
2725 case LT_EXPR:
2726 case GT_EXPR:
2727 case TRUTH_ANDIF_EXPR:
2728 case TRUTH_ORIF_EXPR:
2729 case TRUTH_AND_EXPR:
2730 case TRUTH_OR_EXPR:
2731 case TRUTH_XOR_EXPR:
2732 case TRUTH_NOT_EXPR:
2733 /* Conversion from boolean to a signed:1 bit-field (which only
2734 can hold the values 0 and -1) doesn't lose information - but
2735 it does change the value. */
2736 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2737 warning_at (loc, OPT_Wconversion,
2738 "conversion to %qT from boolean expression", type);
2739 return;
2741 case REAL_CST:
2742 case INTEGER_CST:
2743 if (unsafe_conversion_p (type, expr, true))
2744 warning_at (loc, OPT_Wconversion,
2745 "conversion to %qT alters %qT constant value",
2746 type, expr_type);
2747 return;
2749 case COND_EXPR:
2751 /* In case of COND_EXPR, we do not care about the type of
2752 COND_EXPR, only about the conversion of each operand. */
2753 tree op1 = TREE_OPERAND (expr, 1);
2754 tree op2 = TREE_OPERAND (expr, 2);
2756 conversion_warning (type, op1);
2757 conversion_warning (type, op2);
2758 return;
2761 default: /* 'expr' is not a constant. */
2762 if (unsafe_conversion_p (type, expr, true))
2763 warning_at (loc, OPT_Wconversion,
2764 "conversion to %qT from %qT may alter its value",
2765 type, expr_type);
2769 /* Produce warnings after a conversion. RESULT is the result of
2770 converting EXPR to TYPE. This is a helper function for
2771 convert_and_check and cp_convert_and_check. */
2773 void
2774 warnings_for_convert_and_check (tree type, tree expr, tree result)
2776 location_t loc = EXPR_LOC_OR_HERE (expr);
2778 if (TREE_CODE (expr) == INTEGER_CST
2779 && (TREE_CODE (type) == INTEGER_TYPE
2780 || TREE_CODE (type) == ENUMERAL_TYPE)
2781 && !int_fits_type_p (expr, type))
2783 /* Do not diagnose overflow in a constant expression merely
2784 because a conversion overflowed. */
2785 if (TREE_OVERFLOW (result))
2786 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2788 if (TYPE_UNSIGNED (type))
2790 /* This detects cases like converting -129 or 256 to
2791 unsigned char. */
2792 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2793 warning_at (loc, OPT_Woverflow,
2794 "large integer implicitly truncated to unsigned type");
2795 else
2796 conversion_warning (type, expr);
2798 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2799 warning (OPT_Woverflow,
2800 "overflow in implicit constant conversion");
2801 /* No warning for converting 0x80000000 to int. */
2802 else if (pedantic
2803 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2804 || TYPE_PRECISION (TREE_TYPE (expr))
2805 != TYPE_PRECISION (type)))
2806 warning_at (loc, OPT_Woverflow,
2807 "overflow in implicit constant conversion");
2809 else
2810 conversion_warning (type, expr);
2812 else if ((TREE_CODE (result) == INTEGER_CST
2813 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2814 warning_at (loc, OPT_Woverflow,
2815 "overflow in implicit constant conversion");
2816 else
2817 conversion_warning (type, expr);
2821 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2822 Invoke this function on every expression that is converted implicitly,
2823 i.e. because of language rules and not because of an explicit cast. */
2825 tree
2826 convert_and_check (tree type, tree expr)
2828 tree result;
2829 tree expr_for_warning;
2831 /* Convert from a value with possible excess precision rather than
2832 via the semantic type, but do not warn about values not fitting
2833 exactly in the semantic type. */
2834 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2836 tree orig_type = TREE_TYPE (expr);
2837 expr = TREE_OPERAND (expr, 0);
2838 expr_for_warning = convert (orig_type, expr);
2839 if (orig_type == type)
2840 return expr_for_warning;
2842 else
2843 expr_for_warning = expr;
2845 if (TREE_TYPE (expr) == type)
2846 return expr;
2848 result = convert (type, expr);
2850 if (c_inhibit_evaluation_warnings == 0
2851 && !TREE_OVERFLOW_P (expr)
2852 && result != error_mark_node)
2853 warnings_for_convert_and_check (type, expr_for_warning, result);
2855 return result;
2858 /* A node in a list that describes references to variables (EXPR), which are
2859 either read accesses if WRITER is zero, or write accesses, in which case
2860 WRITER is the parent of EXPR. */
2861 struct tlist
2863 struct tlist *next;
2864 tree expr, writer;
2867 /* Used to implement a cache the results of a call to verify_tree. We only
2868 use this for SAVE_EXPRs. */
2869 struct tlist_cache
2871 struct tlist_cache *next;
2872 struct tlist *cache_before_sp;
2873 struct tlist *cache_after_sp;
2874 tree expr;
2877 /* Obstack to use when allocating tlist structures, and corresponding
2878 firstobj. */
2879 static struct obstack tlist_obstack;
2880 static char *tlist_firstobj = 0;
2882 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2883 warnings. */
2884 static struct tlist *warned_ids;
2885 /* SAVE_EXPRs need special treatment. We process them only once and then
2886 cache the results. */
2887 static struct tlist_cache *save_expr_cache;
2889 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2890 static void merge_tlist (struct tlist **, struct tlist *, int);
2891 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2892 static int warning_candidate_p (tree);
2893 static bool candidate_equal_p (const_tree, const_tree);
2894 static void warn_for_collisions (struct tlist *);
2895 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2896 static struct tlist *new_tlist (struct tlist *, tree, tree);
2898 /* Create a new struct tlist and fill in its fields. */
2899 static struct tlist *
2900 new_tlist (struct tlist *next, tree t, tree writer)
2902 struct tlist *l;
2903 l = XOBNEW (&tlist_obstack, struct tlist);
2904 l->next = next;
2905 l->expr = t;
2906 l->writer = writer;
2907 return l;
2910 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2911 is nonnull, we ignore any node we find which has a writer equal to it. */
2913 static void
2914 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2916 while (add)
2918 struct tlist *next = add->next;
2919 if (!copy)
2920 add->next = *to;
2921 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2922 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2923 add = next;
2927 /* Merge the nodes of ADD into TO. This merging process is done so that for
2928 each variable that already exists in TO, no new node is added; however if
2929 there is a write access recorded in ADD, and an occurrence on TO is only
2930 a read access, then the occurrence in TO will be modified to record the
2931 write. */
2933 static void
2934 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2936 struct tlist **end = to;
2938 while (*end)
2939 end = &(*end)->next;
2941 while (add)
2943 int found = 0;
2944 struct tlist *tmp2;
2945 struct tlist *next = add->next;
2947 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2948 if (candidate_equal_p (tmp2->expr, add->expr))
2950 found = 1;
2951 if (!tmp2->writer)
2952 tmp2->writer = add->writer;
2954 if (!found)
2956 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2957 end = &(*end)->next;
2958 *end = 0;
2960 add = next;
2964 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2965 references in list LIST conflict with it, excluding reads if ONLY writers
2966 is nonzero. */
2968 static void
2969 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2970 int only_writes)
2972 struct tlist *tmp;
2974 /* Avoid duplicate warnings. */
2975 for (tmp = warned_ids; tmp; tmp = tmp->next)
2976 if (candidate_equal_p (tmp->expr, written))
2977 return;
2979 while (list)
2981 if (candidate_equal_p (list->expr, written)
2982 && !candidate_equal_p (list->writer, writer)
2983 && (!only_writes || list->writer))
2985 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2986 warning_at (EXPR_LOC_OR_HERE (writer),
2987 OPT_Wsequence_point, "operation on %qE may be undefined",
2988 list->expr);
2990 list = list->next;
2994 /* Given a list LIST of references to variables, find whether any of these
2995 can cause conflicts due to missing sequence points. */
2997 static void
2998 warn_for_collisions (struct tlist *list)
3000 struct tlist *tmp;
3002 for (tmp = list; tmp; tmp = tmp->next)
3004 if (tmp->writer)
3005 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3009 /* Return nonzero if X is a tree that can be verified by the sequence point
3010 warnings. */
3011 static int
3012 warning_candidate_p (tree x)
3014 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3015 return 0;
3017 if (TREE_CODE (x) == BLOCK)
3018 return 0;
3020 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3021 (lvalue_p) crash on TRY/CATCH. */
3022 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3023 return 0;
3025 if (!lvalue_p (x))
3026 return 0;
3028 /* No point to track non-const calls, they will never satisfy
3029 operand_equal_p. */
3030 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3031 return 0;
3033 if (TREE_CODE (x) == STRING_CST)
3034 return 0;
3036 return 1;
3039 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3040 static bool
3041 candidate_equal_p (const_tree x, const_tree y)
3043 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3046 /* Walk the tree X, and record accesses to variables. If X is written by the
3047 parent tree, WRITER is the parent.
3048 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3049 expression or its only operand forces a sequence point, then everything up
3050 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3051 in PNO_SP.
3052 Once we return, we will have emitted warnings if any subexpression before
3053 such a sequence point could be undefined. On a higher level, however, the
3054 sequence point may not be relevant, and we'll merge the two lists.
3056 Example: (b++, a) + b;
3057 The call that processes the COMPOUND_EXPR will store the increment of B
3058 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3059 processes the PLUS_EXPR will need to merge the two lists so that
3060 eventually, all accesses end up on the same list (and we'll warn about the
3061 unordered subexpressions b++ and b.
3063 A note on merging. If we modify the former example so that our expression
3064 becomes
3065 (b++, b) + a
3066 care must be taken not simply to add all three expressions into the final
3067 PNO_SP list. The function merge_tlist takes care of that by merging the
3068 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3069 way, so that no more than one access to B is recorded. */
3071 static void
3072 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3073 tree writer)
3075 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3076 enum tree_code code;
3077 enum tree_code_class cl;
3079 /* X may be NULL if it is the operand of an empty statement expression
3080 ({ }). */
3081 if (x == NULL)
3082 return;
3084 restart:
3085 code = TREE_CODE (x);
3086 cl = TREE_CODE_CLASS (code);
3088 if (warning_candidate_p (x))
3089 *pno_sp = new_tlist (*pno_sp, x, writer);
3091 switch (code)
3093 case CONSTRUCTOR:
3094 case SIZEOF_EXPR:
3095 return;
3097 case COMPOUND_EXPR:
3098 case TRUTH_ANDIF_EXPR:
3099 case TRUTH_ORIF_EXPR:
3100 tmp_before = tmp_nosp = tmp_list3 = 0;
3101 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3102 warn_for_collisions (tmp_nosp);
3103 merge_tlist (pbefore_sp, tmp_before, 0);
3104 merge_tlist (pbefore_sp, tmp_nosp, 0);
3105 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3106 merge_tlist (pbefore_sp, tmp_list3, 0);
3107 return;
3109 case COND_EXPR:
3110 tmp_before = tmp_list2 = 0;
3111 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3112 warn_for_collisions (tmp_list2);
3113 merge_tlist (pbefore_sp, tmp_before, 0);
3114 merge_tlist (pbefore_sp, tmp_list2, 1);
3116 tmp_list3 = tmp_nosp = 0;
3117 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3118 warn_for_collisions (tmp_nosp);
3119 merge_tlist (pbefore_sp, tmp_list3, 0);
3121 tmp_list3 = tmp_list2 = 0;
3122 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3123 warn_for_collisions (tmp_list2);
3124 merge_tlist (pbefore_sp, tmp_list3, 0);
3125 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3126 two first, to avoid warning for (a ? b++ : b++). */
3127 merge_tlist (&tmp_nosp, tmp_list2, 0);
3128 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3129 return;
3131 case PREDECREMENT_EXPR:
3132 case PREINCREMENT_EXPR:
3133 case POSTDECREMENT_EXPR:
3134 case POSTINCREMENT_EXPR:
3135 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3136 return;
3138 case MODIFY_EXPR:
3139 tmp_before = tmp_nosp = tmp_list3 = 0;
3140 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3141 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3142 /* Expressions inside the LHS are not ordered wrt. the sequence points
3143 in the RHS. Example:
3144 *a = (a++, 2)
3145 Despite the fact that the modification of "a" is in the before_sp
3146 list (tmp_before), it conflicts with the use of "a" in the LHS.
3147 We can handle this by adding the contents of tmp_list3
3148 to those of tmp_before, and redoing the collision warnings for that
3149 list. */
3150 add_tlist (&tmp_before, tmp_list3, x, 1);
3151 warn_for_collisions (tmp_before);
3152 /* Exclude the LHS itself here; we first have to merge it into the
3153 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3154 didn't exclude the LHS, we'd get it twice, once as a read and once
3155 as a write. */
3156 add_tlist (pno_sp, tmp_list3, x, 0);
3157 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3159 merge_tlist (pbefore_sp, tmp_before, 0);
3160 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3161 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3162 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3163 return;
3165 case CALL_EXPR:
3166 /* We need to warn about conflicts among arguments and conflicts between
3167 args and the function address. Side effects of the function address,
3168 however, are not ordered by the sequence point of the call. */
3170 call_expr_arg_iterator iter;
3171 tree arg;
3172 tmp_before = tmp_nosp = 0;
3173 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3174 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3176 tmp_list2 = tmp_list3 = 0;
3177 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3178 merge_tlist (&tmp_list3, tmp_list2, 0);
3179 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3181 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3182 warn_for_collisions (tmp_before);
3183 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3184 return;
3187 case TREE_LIST:
3188 /* Scan all the list, e.g. indices of multi dimensional array. */
3189 while (x)
3191 tmp_before = tmp_nosp = 0;
3192 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3193 merge_tlist (&tmp_nosp, tmp_before, 0);
3194 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3195 x = TREE_CHAIN (x);
3197 return;
3199 case SAVE_EXPR:
3201 struct tlist_cache *t;
3202 for (t = save_expr_cache; t; t = t->next)
3203 if (candidate_equal_p (t->expr, x))
3204 break;
3206 if (!t)
3208 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3209 t->next = save_expr_cache;
3210 t->expr = x;
3211 save_expr_cache = t;
3213 tmp_before = tmp_nosp = 0;
3214 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3215 warn_for_collisions (tmp_nosp);
3217 tmp_list3 = 0;
3218 while (tmp_nosp)
3220 struct tlist *t = tmp_nosp;
3221 tmp_nosp = t->next;
3222 merge_tlist (&tmp_list3, t, 0);
3224 t->cache_before_sp = tmp_before;
3225 t->cache_after_sp = tmp_list3;
3227 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3228 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3229 return;
3232 case ADDR_EXPR:
3233 x = TREE_OPERAND (x, 0);
3234 if (DECL_P (x))
3235 return;
3236 writer = 0;
3237 goto restart;
3239 default:
3240 /* For other expressions, simply recurse on their operands.
3241 Manual tail recursion for unary expressions.
3242 Other non-expressions need not be processed. */
3243 if (cl == tcc_unary)
3245 x = TREE_OPERAND (x, 0);
3246 writer = 0;
3247 goto restart;
3249 else if (IS_EXPR_CODE_CLASS (cl))
3251 int lp;
3252 int max = TREE_OPERAND_LENGTH (x);
3253 for (lp = 0; lp < max; lp++)
3255 tmp_before = tmp_nosp = 0;
3256 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3257 merge_tlist (&tmp_nosp, tmp_before, 0);
3258 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3261 return;
3265 /* Try to warn for undefined behavior in EXPR due to missing sequence
3266 points. */
3268 DEBUG_FUNCTION void
3269 verify_sequence_points (tree expr)
3271 struct tlist *before_sp = 0, *after_sp = 0;
3273 warned_ids = 0;
3274 save_expr_cache = 0;
3275 if (tlist_firstobj == 0)
3277 gcc_obstack_init (&tlist_obstack);
3278 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3281 verify_tree (expr, &before_sp, &after_sp, 0);
3282 warn_for_collisions (after_sp);
3283 obstack_free (&tlist_obstack, tlist_firstobj);
3286 /* Validate the expression after `case' and apply default promotions. */
3288 static tree
3289 check_case_value (tree value)
3291 if (value == NULL_TREE)
3292 return value;
3294 if (TREE_CODE (value) == INTEGER_CST)
3295 /* Promote char or short to int. */
3296 value = perform_integral_promotions (value);
3297 else if (value != error_mark_node)
3299 error ("case label does not reduce to an integer constant");
3300 value = error_mark_node;
3303 constant_expression_warning (value);
3305 return value;
3308 /* See if the case values LOW and HIGH are in the range of the original
3309 type (i.e. before the default conversion to int) of the switch testing
3310 expression.
3311 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3312 the type before promoting it. CASE_LOW_P is a pointer to the lower
3313 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3314 if the case is not a case range.
3315 The caller has to make sure that we are not called with NULL for
3316 CASE_LOW_P (i.e. the default case).
3317 Returns true if the case label is in range of ORIG_TYPE (saturated or
3318 untouched) or false if the label is out of range. */
3320 static bool
3321 check_case_bounds (tree type, tree orig_type,
3322 tree *case_low_p, tree *case_high_p)
3324 tree min_value, max_value;
3325 tree case_low = *case_low_p;
3326 tree case_high = case_high_p ? *case_high_p : case_low;
3328 /* If there was a problem with the original type, do nothing. */
3329 if (orig_type == error_mark_node)
3330 return true;
3332 min_value = TYPE_MIN_VALUE (orig_type);
3333 max_value = TYPE_MAX_VALUE (orig_type);
3335 /* Case label is less than minimum for type. */
3336 if (tree_int_cst_compare (case_low, min_value) < 0
3337 && tree_int_cst_compare (case_high, min_value) < 0)
3339 warning (0, "case label value is less than minimum value for type");
3340 return false;
3343 /* Case value is greater than maximum for type. */
3344 if (tree_int_cst_compare (case_low, max_value) > 0
3345 && tree_int_cst_compare (case_high, max_value) > 0)
3347 warning (0, "case label value exceeds maximum value for type");
3348 return false;
3351 /* Saturate lower case label value to minimum. */
3352 if (tree_int_cst_compare (case_high, min_value) >= 0
3353 && tree_int_cst_compare (case_low, min_value) < 0)
3355 warning (0, "lower value in case label range"
3356 " less than minimum value for type");
3357 case_low = min_value;
3360 /* Saturate upper case label value to maximum. */
3361 if (tree_int_cst_compare (case_low, max_value) <= 0
3362 && tree_int_cst_compare (case_high, max_value) > 0)
3364 warning (0, "upper value in case label range"
3365 " exceeds maximum value for type");
3366 case_high = max_value;
3369 if (*case_low_p != case_low)
3370 *case_low_p = convert (type, case_low);
3371 if (case_high_p && *case_high_p != case_high)
3372 *case_high_p = convert (type, case_high);
3374 return true;
3377 /* Return an integer type with BITS bits of precision,
3378 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3380 tree
3381 c_common_type_for_size (unsigned int bits, int unsignedp)
3383 if (bits == TYPE_PRECISION (integer_type_node))
3384 return unsignedp ? unsigned_type_node : integer_type_node;
3386 if (bits == TYPE_PRECISION (signed_char_type_node))
3387 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3389 if (bits == TYPE_PRECISION (short_integer_type_node))
3390 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3392 if (bits == TYPE_PRECISION (long_integer_type_node))
3393 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3395 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3396 return (unsignedp ? long_long_unsigned_type_node
3397 : long_long_integer_type_node);
3399 if (int128_integer_type_node
3400 && bits == TYPE_PRECISION (int128_integer_type_node))
3401 return (unsignedp ? int128_unsigned_type_node
3402 : int128_integer_type_node);
3404 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3405 return (unsignedp ? widest_unsigned_literal_type_node
3406 : widest_integer_literal_type_node);
3408 if (bits <= TYPE_PRECISION (intQI_type_node))
3409 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3411 if (bits <= TYPE_PRECISION (intHI_type_node))
3412 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3414 if (bits <= TYPE_PRECISION (intSI_type_node))
3415 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3417 if (bits <= TYPE_PRECISION (intDI_type_node))
3418 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3420 return 0;
3423 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3424 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3425 and saturating if SATP is nonzero, otherwise not saturating. */
3427 tree
3428 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3429 int unsignedp, int satp)
3431 enum machine_mode mode;
3432 if (ibit == 0)
3433 mode = unsignedp ? UQQmode : QQmode;
3434 else
3435 mode = unsignedp ? UHAmode : HAmode;
3437 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3438 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3439 break;
3441 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3443 sorry ("GCC cannot support operators with integer types and "
3444 "fixed-point types that have too many integral and "
3445 "fractional bits together");
3446 return 0;
3449 return c_common_type_for_mode (mode, satp);
3452 /* Used for communication between c_common_type_for_mode and
3453 c_register_builtin_type. */
3454 static GTY(()) tree registered_builtin_types;
3456 /* Return a data type that has machine mode MODE.
3457 If the mode is an integer,
3458 then UNSIGNEDP selects between signed and unsigned types.
3459 If the mode is a fixed-point mode,
3460 then UNSIGNEDP selects between saturating and nonsaturating types. */
3462 tree
3463 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3465 tree t;
3467 if (mode == TYPE_MODE (integer_type_node))
3468 return unsignedp ? unsigned_type_node : integer_type_node;
3470 if (mode == TYPE_MODE (signed_char_type_node))
3471 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3473 if (mode == TYPE_MODE (short_integer_type_node))
3474 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3476 if (mode == TYPE_MODE (long_integer_type_node))
3477 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3479 if (mode == TYPE_MODE (long_long_integer_type_node))
3480 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3482 if (int128_integer_type_node
3483 && mode == TYPE_MODE (int128_integer_type_node))
3484 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3486 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3487 return unsignedp ? widest_unsigned_literal_type_node
3488 : widest_integer_literal_type_node;
3490 if (mode == QImode)
3491 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3493 if (mode == HImode)
3494 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3496 if (mode == SImode)
3497 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3499 if (mode == DImode)
3500 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3502 #if HOST_BITS_PER_WIDE_INT >= 64
3503 if (mode == TYPE_MODE (intTI_type_node))
3504 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3505 #endif
3507 if (mode == TYPE_MODE (float_type_node))
3508 return float_type_node;
3510 if (mode == TYPE_MODE (double_type_node))
3511 return double_type_node;
3513 if (mode == TYPE_MODE (long_double_type_node))
3514 return long_double_type_node;
3516 if (mode == TYPE_MODE (void_type_node))
3517 return void_type_node;
3519 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3520 return (unsignedp
3521 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3522 : make_signed_type (GET_MODE_PRECISION (mode)));
3524 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3525 return (unsignedp
3526 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3527 : make_signed_type (GET_MODE_PRECISION (mode)));
3529 if (COMPLEX_MODE_P (mode))
3531 enum machine_mode inner_mode;
3532 tree inner_type;
3534 if (mode == TYPE_MODE (complex_float_type_node))
3535 return complex_float_type_node;
3536 if (mode == TYPE_MODE (complex_double_type_node))
3537 return complex_double_type_node;
3538 if (mode == TYPE_MODE (complex_long_double_type_node))
3539 return complex_long_double_type_node;
3541 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3542 return complex_integer_type_node;
3544 inner_mode = GET_MODE_INNER (mode);
3545 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3546 if (inner_type != NULL_TREE)
3547 return build_complex_type (inner_type);
3549 else if (VECTOR_MODE_P (mode))
3551 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3552 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3553 if (inner_type != NULL_TREE)
3554 return build_vector_type_for_mode (inner_type, mode);
3557 if (mode == TYPE_MODE (dfloat32_type_node))
3558 return dfloat32_type_node;
3559 if (mode == TYPE_MODE (dfloat64_type_node))
3560 return dfloat64_type_node;
3561 if (mode == TYPE_MODE (dfloat128_type_node))
3562 return dfloat128_type_node;
3564 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3566 if (mode == TYPE_MODE (short_fract_type_node))
3567 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3568 if (mode == TYPE_MODE (fract_type_node))
3569 return unsignedp ? sat_fract_type_node : fract_type_node;
3570 if (mode == TYPE_MODE (long_fract_type_node))
3571 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3572 if (mode == TYPE_MODE (long_long_fract_type_node))
3573 return unsignedp ? sat_long_long_fract_type_node
3574 : long_long_fract_type_node;
3576 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3577 return unsignedp ? sat_unsigned_short_fract_type_node
3578 : unsigned_short_fract_type_node;
3579 if (mode == TYPE_MODE (unsigned_fract_type_node))
3580 return unsignedp ? sat_unsigned_fract_type_node
3581 : unsigned_fract_type_node;
3582 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3583 return unsignedp ? sat_unsigned_long_fract_type_node
3584 : unsigned_long_fract_type_node;
3585 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3586 return unsignedp ? sat_unsigned_long_long_fract_type_node
3587 : unsigned_long_long_fract_type_node;
3589 if (mode == TYPE_MODE (short_accum_type_node))
3590 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3591 if (mode == TYPE_MODE (accum_type_node))
3592 return unsignedp ? sat_accum_type_node : accum_type_node;
3593 if (mode == TYPE_MODE (long_accum_type_node))
3594 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3595 if (mode == TYPE_MODE (long_long_accum_type_node))
3596 return unsignedp ? sat_long_long_accum_type_node
3597 : long_long_accum_type_node;
3599 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3600 return unsignedp ? sat_unsigned_short_accum_type_node
3601 : unsigned_short_accum_type_node;
3602 if (mode == TYPE_MODE (unsigned_accum_type_node))
3603 return unsignedp ? sat_unsigned_accum_type_node
3604 : unsigned_accum_type_node;
3605 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3606 return unsignedp ? sat_unsigned_long_accum_type_node
3607 : unsigned_long_accum_type_node;
3608 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3609 return unsignedp ? sat_unsigned_long_long_accum_type_node
3610 : unsigned_long_long_accum_type_node;
3612 if (mode == QQmode)
3613 return unsignedp ? sat_qq_type_node : qq_type_node;
3614 if (mode == HQmode)
3615 return unsignedp ? sat_hq_type_node : hq_type_node;
3616 if (mode == SQmode)
3617 return unsignedp ? sat_sq_type_node : sq_type_node;
3618 if (mode == DQmode)
3619 return unsignedp ? sat_dq_type_node : dq_type_node;
3620 if (mode == TQmode)
3621 return unsignedp ? sat_tq_type_node : tq_type_node;
3623 if (mode == UQQmode)
3624 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3625 if (mode == UHQmode)
3626 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3627 if (mode == USQmode)
3628 return unsignedp ? sat_usq_type_node : usq_type_node;
3629 if (mode == UDQmode)
3630 return unsignedp ? sat_udq_type_node : udq_type_node;
3631 if (mode == UTQmode)
3632 return unsignedp ? sat_utq_type_node : utq_type_node;
3634 if (mode == HAmode)
3635 return unsignedp ? sat_ha_type_node : ha_type_node;
3636 if (mode == SAmode)
3637 return unsignedp ? sat_sa_type_node : sa_type_node;
3638 if (mode == DAmode)
3639 return unsignedp ? sat_da_type_node : da_type_node;
3640 if (mode == TAmode)
3641 return unsignedp ? sat_ta_type_node : ta_type_node;
3643 if (mode == UHAmode)
3644 return unsignedp ? sat_uha_type_node : uha_type_node;
3645 if (mode == USAmode)
3646 return unsignedp ? sat_usa_type_node : usa_type_node;
3647 if (mode == UDAmode)
3648 return unsignedp ? sat_uda_type_node : uda_type_node;
3649 if (mode == UTAmode)
3650 return unsignedp ? sat_uta_type_node : uta_type_node;
3653 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3654 if (TYPE_MODE (TREE_VALUE (t)) == mode
3655 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3656 return TREE_VALUE (t);
3658 return 0;
3661 tree
3662 c_common_unsigned_type (tree type)
3664 return c_common_signed_or_unsigned_type (1, type);
3667 /* Return a signed type the same as TYPE in other respects. */
3669 tree
3670 c_common_signed_type (tree type)
3672 return c_common_signed_or_unsigned_type (0, type);
3675 /* Return a type the same as TYPE except unsigned or
3676 signed according to UNSIGNEDP. */
3678 tree
3679 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3681 tree type1;
3683 /* This block of code emulates the behavior of the old
3684 c_common_unsigned_type. In particular, it returns
3685 long_unsigned_type_node if passed a long, even when a int would
3686 have the same size. This is necessary for warnings to work
3687 correctly in archs where sizeof(int) == sizeof(long) */
3689 type1 = TYPE_MAIN_VARIANT (type);
3690 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3691 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3692 if (type1 == integer_type_node || type1 == unsigned_type_node)
3693 return unsignedp ? unsigned_type_node : integer_type_node;
3694 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3695 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3696 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3697 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3698 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3699 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3700 if (int128_integer_type_node
3701 && (type1 == int128_integer_type_node
3702 || type1 == int128_unsigned_type_node))
3703 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3704 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3705 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3706 #if HOST_BITS_PER_WIDE_INT >= 64
3707 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3708 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3709 #endif
3710 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3711 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3712 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3713 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3714 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3715 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3716 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3717 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3719 #define C_COMMON_FIXED_TYPES(NAME) \
3720 if (type1 == short_ ## NAME ## _type_node \
3721 || type1 == unsigned_short_ ## NAME ## _type_node) \
3722 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3723 : short_ ## NAME ## _type_node; \
3724 if (type1 == NAME ## _type_node \
3725 || type1 == unsigned_ ## NAME ## _type_node) \
3726 return unsignedp ? unsigned_ ## NAME ## _type_node \
3727 : NAME ## _type_node; \
3728 if (type1 == long_ ## NAME ## _type_node \
3729 || type1 == unsigned_long_ ## NAME ## _type_node) \
3730 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3731 : long_ ## NAME ## _type_node; \
3732 if (type1 == long_long_ ## NAME ## _type_node \
3733 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3734 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3735 : long_long_ ## NAME ## _type_node;
3737 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3738 if (type1 == NAME ## _type_node \
3739 || type1 == u ## NAME ## _type_node) \
3740 return unsignedp ? u ## NAME ## _type_node \
3741 : NAME ## _type_node;
3743 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3744 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3745 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3746 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3747 : sat_ ## short_ ## NAME ## _type_node; \
3748 if (type1 == sat_ ## NAME ## _type_node \
3749 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3750 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3751 : sat_ ## NAME ## _type_node; \
3752 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3753 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3754 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3755 : sat_ ## long_ ## NAME ## _type_node; \
3756 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3757 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3758 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3759 : sat_ ## long_long_ ## NAME ## _type_node;
3761 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3762 if (type1 == sat_ ## NAME ## _type_node \
3763 || type1 == sat_ ## u ## NAME ## _type_node) \
3764 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3765 : sat_ ## NAME ## _type_node;
3767 C_COMMON_FIXED_TYPES (fract);
3768 C_COMMON_FIXED_TYPES_SAT (fract);
3769 C_COMMON_FIXED_TYPES (accum);
3770 C_COMMON_FIXED_TYPES_SAT (accum);
3772 C_COMMON_FIXED_MODE_TYPES (qq);
3773 C_COMMON_FIXED_MODE_TYPES (hq);
3774 C_COMMON_FIXED_MODE_TYPES (sq);
3775 C_COMMON_FIXED_MODE_TYPES (dq);
3776 C_COMMON_FIXED_MODE_TYPES (tq);
3777 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3778 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3779 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3780 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3781 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3782 C_COMMON_FIXED_MODE_TYPES (ha);
3783 C_COMMON_FIXED_MODE_TYPES (sa);
3784 C_COMMON_FIXED_MODE_TYPES (da);
3785 C_COMMON_FIXED_MODE_TYPES (ta);
3786 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3787 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3788 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3789 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3791 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3792 the precision; they have precision set to match their range, but
3793 may use a wider mode to match an ABI. If we change modes, we may
3794 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3795 the precision as well, so as to yield correct results for
3796 bit-field types. C++ does not have these separate bit-field
3797 types, and producing a signed or unsigned variant of an
3798 ENUMERAL_TYPE may cause other problems as well. */
3800 if (!INTEGRAL_TYPE_P (type)
3801 || TYPE_UNSIGNED (type) == unsignedp)
3802 return type;
3804 #define TYPE_OK(node) \
3805 (TYPE_MODE (type) == TYPE_MODE (node) \
3806 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3807 if (TYPE_OK (signed_char_type_node))
3808 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3809 if (TYPE_OK (integer_type_node))
3810 return unsignedp ? unsigned_type_node : integer_type_node;
3811 if (TYPE_OK (short_integer_type_node))
3812 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3813 if (TYPE_OK (long_integer_type_node))
3814 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3815 if (TYPE_OK (long_long_integer_type_node))
3816 return (unsignedp ? long_long_unsigned_type_node
3817 : long_long_integer_type_node);
3818 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3819 return (unsignedp ? int128_unsigned_type_node
3820 : int128_integer_type_node);
3821 if (TYPE_OK (widest_integer_literal_type_node))
3822 return (unsignedp ? widest_unsigned_literal_type_node
3823 : widest_integer_literal_type_node);
3825 #if HOST_BITS_PER_WIDE_INT >= 64
3826 if (TYPE_OK (intTI_type_node))
3827 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3828 #endif
3829 if (TYPE_OK (intDI_type_node))
3830 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3831 if (TYPE_OK (intSI_type_node))
3832 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3833 if (TYPE_OK (intHI_type_node))
3834 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3835 if (TYPE_OK (intQI_type_node))
3836 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3837 #undef TYPE_OK
3839 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3842 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3844 tree
3845 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3847 /* Extended integer types of the same width as a standard type have
3848 lesser rank, so those of the same width as int promote to int or
3849 unsigned int and are valid for printf formats expecting int or
3850 unsigned int. To avoid such special cases, avoid creating
3851 extended integer types for bit-fields if a standard integer type
3852 is available. */
3853 if (width == TYPE_PRECISION (integer_type_node))
3854 return unsignedp ? unsigned_type_node : integer_type_node;
3855 if (width == TYPE_PRECISION (signed_char_type_node))
3856 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3857 if (width == TYPE_PRECISION (short_integer_type_node))
3858 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3859 if (width == TYPE_PRECISION (long_integer_type_node))
3860 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3861 if (width == TYPE_PRECISION (long_long_integer_type_node))
3862 return (unsignedp ? long_long_unsigned_type_node
3863 : long_long_integer_type_node);
3864 if (int128_integer_type_node
3865 && width == TYPE_PRECISION (int128_integer_type_node))
3866 return (unsignedp ? int128_unsigned_type_node
3867 : int128_integer_type_node);
3868 return build_nonstandard_integer_type (width, unsignedp);
3871 /* The C version of the register_builtin_type langhook. */
3873 void
3874 c_register_builtin_type (tree type, const char* name)
3876 tree decl;
3878 decl = build_decl (UNKNOWN_LOCATION,
3879 TYPE_DECL, get_identifier (name), type);
3880 DECL_ARTIFICIAL (decl) = 1;
3881 if (!TYPE_NAME (type))
3882 TYPE_NAME (type) = decl;
3883 pushdecl (decl);
3885 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3888 /* Print an error message for invalid operands to arith operation
3889 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3890 LOCATION is the location of the message. */
3892 void
3893 binary_op_error (location_t location, enum tree_code code,
3894 tree type0, tree type1)
3896 const char *opname;
3898 switch (code)
3900 case PLUS_EXPR:
3901 opname = "+"; break;
3902 case MINUS_EXPR:
3903 opname = "-"; break;
3904 case MULT_EXPR:
3905 opname = "*"; break;
3906 case MAX_EXPR:
3907 opname = "max"; break;
3908 case MIN_EXPR:
3909 opname = "min"; break;
3910 case EQ_EXPR:
3911 opname = "=="; break;
3912 case NE_EXPR:
3913 opname = "!="; break;
3914 case LE_EXPR:
3915 opname = "<="; break;
3916 case GE_EXPR:
3917 opname = ">="; break;
3918 case LT_EXPR:
3919 opname = "<"; break;
3920 case GT_EXPR:
3921 opname = ">"; break;
3922 case LSHIFT_EXPR:
3923 opname = "<<"; break;
3924 case RSHIFT_EXPR:
3925 opname = ">>"; break;
3926 case TRUNC_MOD_EXPR:
3927 case FLOOR_MOD_EXPR:
3928 opname = "%"; break;
3929 case TRUNC_DIV_EXPR:
3930 case FLOOR_DIV_EXPR:
3931 opname = "/"; break;
3932 case BIT_AND_EXPR:
3933 opname = "&"; break;
3934 case BIT_IOR_EXPR:
3935 opname = "|"; break;
3936 case TRUTH_ANDIF_EXPR:
3937 opname = "&&"; break;
3938 case TRUTH_ORIF_EXPR:
3939 opname = "||"; break;
3940 case BIT_XOR_EXPR:
3941 opname = "^"; break;
3942 default:
3943 gcc_unreachable ();
3945 error_at (location,
3946 "invalid operands to binary %s (have %qT and %qT)", opname,
3947 type0, type1);
3950 /* Given an expression as a tree, return its original type. Do this
3951 by stripping any conversion that preserves the sign and precision. */
3952 static tree
3953 expr_original_type (tree expr)
3955 STRIP_SIGN_NOPS (expr);
3956 return TREE_TYPE (expr);
3959 /* Subroutine of build_binary_op, used for comparison operations.
3960 See if the operands have both been converted from subword integer types
3961 and, if so, perhaps change them both back to their original type.
3962 This function is also responsible for converting the two operands
3963 to the proper common type for comparison.
3965 The arguments of this function are all pointers to local variables
3966 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3967 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3969 If this function returns nonzero, it means that the comparison has
3970 a constant value. What this function returns is an expression for
3971 that value. */
3973 tree
3974 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3975 enum tree_code *rescode_ptr)
3977 tree type;
3978 tree op0 = *op0_ptr;
3979 tree op1 = *op1_ptr;
3980 int unsignedp0, unsignedp1;
3981 int real1, real2;
3982 tree primop0, primop1;
3983 enum tree_code code = *rescode_ptr;
3984 location_t loc = EXPR_LOC_OR_HERE (op0);
3986 /* Throw away any conversions to wider types
3987 already present in the operands. */
3989 primop0 = c_common_get_narrower (op0, &unsignedp0);
3990 primop1 = c_common_get_narrower (op1, &unsignedp1);
3992 /* If primopN is first sign-extended from primopN's precision to opN's
3993 precision, then zero-extended from opN's precision to
3994 *restype_ptr precision, shortenings might be invalid. */
3995 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3996 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3997 && !unsignedp0
3998 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3999 primop0 = op0;
4000 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4001 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4002 && !unsignedp1
4003 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4004 primop1 = op1;
4006 /* Handle the case that OP0 does not *contain* a conversion
4007 but it *requires* conversion to FINAL_TYPE. */
4009 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4010 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4011 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4012 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4014 /* If one of the operands must be floated, we cannot optimize. */
4015 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4016 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4018 /* If first arg is constant, swap the args (changing operation
4019 so value is preserved), for canonicalization. Don't do this if
4020 the second arg is 0. */
4022 if (TREE_CONSTANT (primop0)
4023 && !integer_zerop (primop1) && !real_zerop (primop1)
4024 && !fixed_zerop (primop1))
4026 tree tem = primop0;
4027 int temi = unsignedp0;
4028 primop0 = primop1;
4029 primop1 = tem;
4030 tem = op0;
4031 op0 = op1;
4032 op1 = tem;
4033 *op0_ptr = op0;
4034 *op1_ptr = op1;
4035 unsignedp0 = unsignedp1;
4036 unsignedp1 = temi;
4037 temi = real1;
4038 real1 = real2;
4039 real2 = temi;
4041 switch (code)
4043 case LT_EXPR:
4044 code = GT_EXPR;
4045 break;
4046 case GT_EXPR:
4047 code = LT_EXPR;
4048 break;
4049 case LE_EXPR:
4050 code = GE_EXPR;
4051 break;
4052 case GE_EXPR:
4053 code = LE_EXPR;
4054 break;
4055 default:
4056 break;
4058 *rescode_ptr = code;
4061 /* If comparing an integer against a constant more bits wide,
4062 maybe we can deduce a value of 1 or 0 independent of the data.
4063 Or else truncate the constant now
4064 rather than extend the variable at run time.
4066 This is only interesting if the constant is the wider arg.
4067 Also, it is not safe if the constant is unsigned and the
4068 variable arg is signed, since in this case the variable
4069 would be sign-extended and then regarded as unsigned.
4070 Our technique fails in this case because the lowest/highest
4071 possible unsigned results don't follow naturally from the
4072 lowest/highest possible values of the variable operand.
4073 For just EQ_EXPR and NE_EXPR there is another technique that
4074 could be used: see if the constant can be faithfully represented
4075 in the other operand's type, by truncating it and reextending it
4076 and see if that preserves the constant's value. */
4078 if (!real1 && !real2
4079 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4080 && TREE_CODE (primop1) == INTEGER_CST
4081 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4083 int min_gt, max_gt, min_lt, max_lt;
4084 tree maxval, minval;
4085 /* 1 if comparison is nominally unsigned. */
4086 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4087 tree val;
4089 type = c_common_signed_or_unsigned_type (unsignedp0,
4090 TREE_TYPE (primop0));
4092 maxval = TYPE_MAX_VALUE (type);
4093 minval = TYPE_MIN_VALUE (type);
4095 if (unsignedp && !unsignedp0)
4096 *restype_ptr = c_common_signed_type (*restype_ptr);
4098 if (TREE_TYPE (primop1) != *restype_ptr)
4100 /* Convert primop1 to target type, but do not introduce
4101 additional overflow. We know primop1 is an int_cst. */
4102 primop1 = force_fit_type_double (*restype_ptr,
4103 tree_to_double_int (primop1),
4104 0, TREE_OVERFLOW (primop1));
4106 if (type != *restype_ptr)
4108 minval = convert (*restype_ptr, minval);
4109 maxval = convert (*restype_ptr, maxval);
4112 if (unsignedp && unsignedp0)
4114 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4115 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4116 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4117 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4119 else
4121 min_gt = INT_CST_LT (primop1, minval);
4122 max_gt = INT_CST_LT (primop1, maxval);
4123 min_lt = INT_CST_LT (minval, primop1);
4124 max_lt = INT_CST_LT (maxval, primop1);
4127 val = 0;
4128 /* This used to be a switch, but Genix compiler can't handle that. */
4129 if (code == NE_EXPR)
4131 if (max_lt || min_gt)
4132 val = truthvalue_true_node;
4134 else if (code == EQ_EXPR)
4136 if (max_lt || min_gt)
4137 val = truthvalue_false_node;
4139 else if (code == LT_EXPR)
4141 if (max_lt)
4142 val = truthvalue_true_node;
4143 if (!min_lt)
4144 val = truthvalue_false_node;
4146 else if (code == GT_EXPR)
4148 if (min_gt)
4149 val = truthvalue_true_node;
4150 if (!max_gt)
4151 val = truthvalue_false_node;
4153 else if (code == LE_EXPR)
4155 if (!max_gt)
4156 val = truthvalue_true_node;
4157 if (min_gt)
4158 val = truthvalue_false_node;
4160 else if (code == GE_EXPR)
4162 if (!min_lt)
4163 val = truthvalue_true_node;
4164 if (max_lt)
4165 val = truthvalue_false_node;
4168 /* If primop0 was sign-extended and unsigned comparison specd,
4169 we did a signed comparison above using the signed type bounds.
4170 But the comparison we output must be unsigned.
4172 Also, for inequalities, VAL is no good; but if the signed
4173 comparison had *any* fixed result, it follows that the
4174 unsigned comparison just tests the sign in reverse
4175 (positive values are LE, negative ones GE).
4176 So we can generate an unsigned comparison
4177 against an extreme value of the signed type. */
4179 if (unsignedp && !unsignedp0)
4181 if (val != 0)
4182 switch (code)
4184 case LT_EXPR:
4185 case GE_EXPR:
4186 primop1 = TYPE_MIN_VALUE (type);
4187 val = 0;
4188 break;
4190 case LE_EXPR:
4191 case GT_EXPR:
4192 primop1 = TYPE_MAX_VALUE (type);
4193 val = 0;
4194 break;
4196 default:
4197 break;
4199 type = c_common_unsigned_type (type);
4202 if (TREE_CODE (primop0) != INTEGER_CST
4203 && c_inhibit_evaluation_warnings == 0)
4205 if (val == truthvalue_false_node)
4206 warning_at (loc, OPT_Wtype_limits,
4207 "comparison is always false due to limited range of data type");
4208 if (val == truthvalue_true_node)
4209 warning_at (loc, OPT_Wtype_limits,
4210 "comparison is always true due to limited range of data type");
4213 if (val != 0)
4215 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4216 if (TREE_SIDE_EFFECTS (primop0))
4217 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4218 return val;
4221 /* Value is not predetermined, but do the comparison
4222 in the type of the operand that is not constant.
4223 TYPE is already properly set. */
4226 /* If either arg is decimal float and the other is float, find the
4227 proper common type to use for comparison. */
4228 else if (real1 && real2
4229 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4230 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4231 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4233 else if (real1 && real2
4234 && (TYPE_PRECISION (TREE_TYPE (primop0))
4235 == TYPE_PRECISION (TREE_TYPE (primop1))))
4236 type = TREE_TYPE (primop0);
4238 /* If args' natural types are both narrower than nominal type
4239 and both extend in the same manner, compare them
4240 in the type of the wider arg.
4241 Otherwise must actually extend both to the nominal
4242 common type lest different ways of extending
4243 alter the result.
4244 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4246 else if (unsignedp0 == unsignedp1 && real1 == real2
4247 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4248 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4250 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4251 type = c_common_signed_or_unsigned_type (unsignedp0
4252 || TYPE_UNSIGNED (*restype_ptr),
4253 type);
4254 /* Make sure shorter operand is extended the right way
4255 to match the longer operand. */
4256 primop0
4257 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4258 TREE_TYPE (primop0)),
4259 primop0);
4260 primop1
4261 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4262 TREE_TYPE (primop1)),
4263 primop1);
4265 else
4267 /* Here we must do the comparison on the nominal type
4268 using the args exactly as we received them. */
4269 type = *restype_ptr;
4270 primop0 = op0;
4271 primop1 = op1;
4273 if (!real1 && !real2 && integer_zerop (primop1)
4274 && TYPE_UNSIGNED (*restype_ptr))
4276 tree value = 0;
4277 /* All unsigned values are >= 0, so we warn. However,
4278 if OP0 is a constant that is >= 0, the signedness of
4279 the comparison isn't an issue, so suppress the
4280 warning. */
4281 bool warn =
4282 warn_type_limits && !in_system_header
4283 && c_inhibit_evaluation_warnings == 0
4284 && !(TREE_CODE (primop0) == INTEGER_CST
4285 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4286 primop0)))
4287 /* Do not warn for enumeration types. */
4288 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4290 switch (code)
4292 case GE_EXPR:
4293 if (warn)
4294 warning_at (loc, OPT_Wtype_limits,
4295 "comparison of unsigned expression >= 0 is always true");
4296 value = truthvalue_true_node;
4297 break;
4299 case LT_EXPR:
4300 if (warn)
4301 warning_at (loc, OPT_Wtype_limits,
4302 "comparison of unsigned expression < 0 is always false");
4303 value = truthvalue_false_node;
4304 break;
4306 default:
4307 break;
4310 if (value != 0)
4312 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4313 if (TREE_SIDE_EFFECTS (primop0))
4314 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4315 primop0, value);
4316 return value;
4321 *op0_ptr = convert (type, primop0);
4322 *op1_ptr = convert (type, primop1);
4324 *restype_ptr = truthvalue_type_node;
4326 return 0;
4329 /* Return a tree for the sum or difference (RESULTCODE says which)
4330 of pointer PTROP and integer INTOP. */
4332 tree
4333 pointer_int_sum (location_t loc, enum tree_code resultcode,
4334 tree ptrop, tree intop, bool complain)
4336 tree size_exp, ret;
4338 /* The result is a pointer of the same type that is being added. */
4339 tree result_type = TREE_TYPE (ptrop);
4341 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4343 if (complain && warn_pointer_arith)
4344 pedwarn (loc, OPT_Wpointer_arith,
4345 "pointer of type %<void *%> used in arithmetic");
4346 else if (!complain)
4347 return error_mark_node;
4348 size_exp = integer_one_node;
4350 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4352 if (complain && warn_pointer_arith)
4353 pedwarn (loc, OPT_Wpointer_arith,
4354 "pointer to a function used in arithmetic");
4355 else if (!complain)
4356 return error_mark_node;
4357 size_exp = integer_one_node;
4359 else
4360 size_exp = size_in_bytes (TREE_TYPE (result_type));
4362 /* We are manipulating pointer values, so we don't need to warn
4363 about relying on undefined signed overflow. We disable the
4364 warning here because we use integer types so fold won't know that
4365 they are really pointers. */
4366 fold_defer_overflow_warnings ();
4368 /* If what we are about to multiply by the size of the elements
4369 contains a constant term, apply distributive law
4370 and multiply that constant term separately.
4371 This helps produce common subexpressions. */
4372 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4373 && !TREE_CONSTANT (intop)
4374 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4375 && TREE_CONSTANT (size_exp)
4376 /* If the constant comes from pointer subtraction,
4377 skip this optimization--it would cause an error. */
4378 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4379 /* If the constant is unsigned, and smaller than the pointer size,
4380 then we must skip this optimization. This is because it could cause
4381 an overflow error if the constant is negative but INTOP is not. */
4382 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4383 || (TYPE_PRECISION (TREE_TYPE (intop))
4384 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4386 enum tree_code subcode = resultcode;
4387 tree int_type = TREE_TYPE (intop);
4388 if (TREE_CODE (intop) == MINUS_EXPR)
4389 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4390 /* Convert both subexpression types to the type of intop,
4391 because weird cases involving pointer arithmetic
4392 can result in a sum or difference with different type args. */
4393 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4394 subcode, ptrop,
4395 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4396 intop = convert (int_type, TREE_OPERAND (intop, 0));
4399 /* Convert the integer argument to a type the same size as sizetype
4400 so the multiply won't overflow spuriously. */
4401 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4402 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4403 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4404 TYPE_UNSIGNED (sizetype)), intop);
4406 /* Replace the integer argument with a suitable product by the object size.
4407 Do this multiplication as signed, then convert to the appropriate type
4408 for the pointer operation and disregard an overflow that occurred only
4409 because of the sign-extension change in the latter conversion. */
4411 tree t = build_binary_op (loc,
4412 MULT_EXPR, intop,
4413 convert (TREE_TYPE (intop), size_exp), 1);
4414 intop = convert (sizetype, t);
4415 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4416 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4417 TREE_INT_CST_HIGH (intop));
4420 /* Create the sum or difference. */
4421 if (resultcode == MINUS_EXPR)
4422 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4424 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4426 fold_undefer_and_ignore_overflow_warnings ();
4428 return ret;
4431 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4432 and if NON_CONST is known not to be permitted in an evaluated part
4433 of a constant expression. */
4435 tree
4436 c_wrap_maybe_const (tree expr, bool non_const)
4438 bool nowarning = TREE_NO_WARNING (expr);
4439 location_t loc = EXPR_LOCATION (expr);
4441 /* This should never be called for C++. */
4442 if (c_dialect_cxx ())
4443 gcc_unreachable ();
4445 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4446 STRIP_TYPE_NOPS (expr);
4447 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4448 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4449 if (nowarning)
4450 TREE_NO_WARNING (expr) = 1;
4451 protected_set_expr_location (expr, loc);
4453 return expr;
4456 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4457 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4458 around the SAVE_EXPR if needed so that c_fully_fold does not need
4459 to look inside SAVE_EXPRs. */
4461 tree
4462 c_save_expr (tree expr)
4464 bool maybe_const = true;
4465 if (c_dialect_cxx ())
4466 return save_expr (expr);
4467 expr = c_fully_fold (expr, false, &maybe_const);
4468 expr = save_expr (expr);
4469 if (!maybe_const)
4470 expr = c_wrap_maybe_const (expr, true);
4471 return expr;
4474 /* Return whether EXPR is a declaration whose address can never be
4475 NULL. */
4477 bool
4478 decl_with_nonnull_addr_p (const_tree expr)
4480 return (DECL_P (expr)
4481 && (TREE_CODE (expr) == PARM_DECL
4482 || TREE_CODE (expr) == LABEL_DECL
4483 || !DECL_WEAK (expr)));
4486 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4487 or for an `if' or `while' statement or ?..: exp. It should already
4488 have been validated to be of suitable type; otherwise, a bad
4489 diagnostic may result.
4491 The EXPR is located at LOCATION.
4493 This preparation consists of taking the ordinary
4494 representation of an expression expr and producing a valid tree
4495 boolean expression describing whether expr is nonzero. We could
4496 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4497 but we optimize comparisons, &&, ||, and !.
4499 The resulting type should always be `truthvalue_type_node'. */
4501 tree
4502 c_common_truthvalue_conversion (location_t location, tree expr)
4504 switch (TREE_CODE (expr))
4506 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4507 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4508 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4509 case ORDERED_EXPR: case UNORDERED_EXPR:
4510 if (TREE_TYPE (expr) == truthvalue_type_node)
4511 return expr;
4512 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4513 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4514 goto ret;
4516 case TRUTH_ANDIF_EXPR:
4517 case TRUTH_ORIF_EXPR:
4518 case TRUTH_AND_EXPR:
4519 case TRUTH_OR_EXPR:
4520 case TRUTH_XOR_EXPR:
4521 if (TREE_TYPE (expr) == truthvalue_type_node)
4522 return expr;
4523 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4524 c_common_truthvalue_conversion (location,
4525 TREE_OPERAND (expr, 0)),
4526 c_common_truthvalue_conversion (location,
4527 TREE_OPERAND (expr, 1)));
4528 goto ret;
4530 case TRUTH_NOT_EXPR:
4531 if (TREE_TYPE (expr) == truthvalue_type_node)
4532 return expr;
4533 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4534 c_common_truthvalue_conversion (location,
4535 TREE_OPERAND (expr, 0)));
4536 goto ret;
4538 case ERROR_MARK:
4539 return expr;
4541 case INTEGER_CST:
4542 return integer_zerop (expr) ? truthvalue_false_node
4543 : truthvalue_true_node;
4545 case REAL_CST:
4546 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4547 ? truthvalue_true_node
4548 : truthvalue_false_node;
4550 case FIXED_CST:
4551 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4552 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4553 ? truthvalue_true_node
4554 : truthvalue_false_node;
4556 case FUNCTION_DECL:
4557 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4558 /* Fall through. */
4560 case ADDR_EXPR:
4562 tree inner = TREE_OPERAND (expr, 0);
4563 if (decl_with_nonnull_addr_p (inner))
4565 /* Common Ada/Pascal programmer's mistake. */
4566 warning_at (location,
4567 OPT_Waddress,
4568 "the address of %qD will always evaluate as %<true%>",
4569 inner);
4570 return truthvalue_true_node;
4572 break;
4575 case COMPLEX_EXPR:
4576 expr = build_binary_op (EXPR_LOCATION (expr),
4577 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4578 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4579 c_common_truthvalue_conversion (location,
4580 TREE_OPERAND (expr, 0)),
4581 c_common_truthvalue_conversion (location,
4582 TREE_OPERAND (expr, 1)),
4584 goto ret;
4586 case NEGATE_EXPR:
4587 case ABS_EXPR:
4588 case FLOAT_EXPR:
4589 case EXCESS_PRECISION_EXPR:
4590 /* These don't change whether an object is nonzero or zero. */
4591 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4593 case LROTATE_EXPR:
4594 case RROTATE_EXPR:
4595 /* These don't change whether an object is zero or nonzero, but
4596 we can't ignore them if their second arg has side-effects. */
4597 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4599 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4600 TREE_OPERAND (expr, 1),
4601 c_common_truthvalue_conversion
4602 (location, TREE_OPERAND (expr, 0)));
4603 goto ret;
4605 else
4606 return c_common_truthvalue_conversion (location,
4607 TREE_OPERAND (expr, 0));
4609 case COND_EXPR:
4610 /* Distribute the conversion into the arms of a COND_EXPR. */
4611 if (c_dialect_cxx ())
4613 tree op1 = TREE_OPERAND (expr, 1);
4614 tree op2 = TREE_OPERAND (expr, 2);
4615 /* In C++ one of the arms might have void type if it is throw. */
4616 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4617 op1 = c_common_truthvalue_conversion (location, op1);
4618 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4619 op2 = c_common_truthvalue_conversion (location, op2);
4620 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4621 TREE_OPERAND (expr, 0), op1, op2);
4622 goto ret;
4624 else
4626 /* Folding will happen later for C. */
4627 expr = build3 (COND_EXPR, truthvalue_type_node,
4628 TREE_OPERAND (expr, 0),
4629 c_common_truthvalue_conversion (location,
4630 TREE_OPERAND (expr, 1)),
4631 c_common_truthvalue_conversion (location,
4632 TREE_OPERAND (expr, 2)));
4633 goto ret;
4636 CASE_CONVERT:
4638 tree totype = TREE_TYPE (expr);
4639 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4641 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4642 since that affects how `default_conversion' will behave. */
4643 if (TREE_CODE (totype) == REFERENCE_TYPE
4644 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4645 break;
4646 /* Don't strip a conversion from C++0x scoped enum, since they
4647 don't implicitly convert to other types. */
4648 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4649 && ENUM_IS_SCOPED (fromtype))
4650 break;
4651 /* If this isn't narrowing the argument, we can ignore it. */
4652 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4653 return c_common_truthvalue_conversion (location,
4654 TREE_OPERAND (expr, 0));
4656 break;
4658 case MODIFY_EXPR:
4659 if (!TREE_NO_WARNING (expr)
4660 && warn_parentheses)
4662 warning (OPT_Wparentheses,
4663 "suggest parentheses around assignment used as truth value");
4664 TREE_NO_WARNING (expr) = 1;
4666 break;
4668 default:
4669 break;
4672 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4674 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4675 expr = (build_binary_op
4676 (EXPR_LOCATION (expr),
4677 (TREE_SIDE_EFFECTS (expr)
4678 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4679 c_common_truthvalue_conversion
4680 (location,
4681 build_unary_op (location, REALPART_EXPR, t, 0)),
4682 c_common_truthvalue_conversion
4683 (location,
4684 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4685 0));
4686 goto ret;
4689 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4691 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4692 FCONST0 (TYPE_MODE
4693 (TREE_TYPE (expr))));
4694 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4696 else
4697 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4699 ret:
4700 protected_set_expr_location (expr, location);
4701 return expr;
4704 static void def_builtin_1 (enum built_in_function fncode,
4705 const char *name,
4706 enum built_in_class fnclass,
4707 tree fntype, tree libtype,
4708 bool both_p, bool fallback_p, bool nonansi_p,
4709 tree fnattrs, bool implicit_p);
4712 /* Apply the TYPE_QUALS to the new DECL. */
4714 void
4715 c_apply_type_quals_to_decl (int type_quals, tree decl)
4717 tree type = TREE_TYPE (decl);
4719 if (type == error_mark_node)
4720 return;
4722 if ((type_quals & TYPE_QUAL_CONST)
4723 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4724 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4725 constructor can produce constant init, so rely on cp_finish_decl to
4726 clear TREE_READONLY if the variable has non-constant init. */
4727 TREE_READONLY (decl) = 1;
4728 if (type_quals & TYPE_QUAL_VOLATILE)
4730 TREE_SIDE_EFFECTS (decl) = 1;
4731 TREE_THIS_VOLATILE (decl) = 1;
4733 if (type_quals & TYPE_QUAL_RESTRICT)
4735 while (type && TREE_CODE (type) == ARRAY_TYPE)
4736 /* Allow 'restrict' on arrays of pointers.
4737 FIXME currently we just ignore it. */
4738 type = TREE_TYPE (type);
4739 if (!type
4740 || !POINTER_TYPE_P (type)
4741 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4742 error ("invalid use of %<restrict%>");
4746 /* Hash function for the problem of multiple type definitions in
4747 different files. This must hash all types that will compare
4748 equal via comptypes to the same value. In practice it hashes
4749 on some of the simple stuff and leaves the details to comptypes. */
4751 static hashval_t
4752 c_type_hash (const void *p)
4754 int n_elements;
4755 int shift, size;
4756 const_tree const t = (const_tree) p;
4757 tree t2;
4758 switch (TREE_CODE (t))
4760 /* For pointers, hash on pointee type plus some swizzling. */
4761 case POINTER_TYPE:
4762 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4763 /* Hash on number of elements and total size. */
4764 case ENUMERAL_TYPE:
4765 shift = 3;
4766 t2 = TYPE_VALUES (t);
4767 break;
4768 case RECORD_TYPE:
4769 shift = 0;
4770 t2 = TYPE_FIELDS (t);
4771 break;
4772 case QUAL_UNION_TYPE:
4773 shift = 1;
4774 t2 = TYPE_FIELDS (t);
4775 break;
4776 case UNION_TYPE:
4777 shift = 2;
4778 t2 = TYPE_FIELDS (t);
4779 break;
4780 default:
4781 gcc_unreachable ();
4783 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4784 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4785 n_elements = list_length (t2);
4786 /* We might have a VLA here. */
4787 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4788 size = 0;
4789 else
4790 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4791 return ((size << 24) | (n_elements << shift));
4794 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4796 /* Return the typed-based alias set for T, which may be an expression
4797 or a type. Return -1 if we don't do anything special. */
4799 alias_set_type
4800 c_common_get_alias_set (tree t)
4802 tree u;
4803 PTR *slot;
4805 /* For VLAs, use the alias set of the element type rather than the
4806 default of alias set 0 for types compared structurally. */
4807 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4809 if (TREE_CODE (t) == ARRAY_TYPE)
4810 return get_alias_set (TREE_TYPE (t));
4811 return -1;
4814 /* Permit type-punning when accessing a union, provided the access
4815 is directly through the union. For example, this code does not
4816 permit taking the address of a union member and then storing
4817 through it. Even the type-punning allowed here is a GCC
4818 extension, albeit a common and useful one; the C standard says
4819 that such accesses have implementation-defined behavior. */
4820 for (u = t;
4821 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4822 u = TREE_OPERAND (u, 0))
4823 if (TREE_CODE (u) == COMPONENT_REF
4824 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4825 return 0;
4827 /* That's all the expressions we handle specially. */
4828 if (!TYPE_P (t))
4829 return -1;
4831 /* The C standard guarantees that any object may be accessed via an
4832 lvalue that has character type. */
4833 if (t == char_type_node
4834 || t == signed_char_type_node
4835 || t == unsigned_char_type_node)
4836 return 0;
4838 /* The C standard specifically allows aliasing between signed and
4839 unsigned variants of the same type. We treat the signed
4840 variant as canonical. */
4841 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4843 tree t1 = c_common_signed_type (t);
4845 /* t1 == t can happen for boolean nodes which are always unsigned. */
4846 if (t1 != t)
4847 return get_alias_set (t1);
4850 /* Handle the case of multiple type nodes referring to "the same" type,
4851 which occurs with IMA. These share an alias set. FIXME: Currently only
4852 C90 is handled. (In C99 type compatibility is not transitive, which
4853 complicates things mightily. The alias set splay trees can theoretically
4854 represent this, but insertion is tricky when you consider all the
4855 different orders things might arrive in.) */
4857 if (c_language != clk_c || flag_isoc99)
4858 return -1;
4860 /* Save time if there's only one input file. */
4861 if (num_in_fnames == 1)
4862 return -1;
4864 /* Pointers need special handling if they point to any type that
4865 needs special handling (below). */
4866 if (TREE_CODE (t) == POINTER_TYPE)
4868 tree t2;
4869 /* Find bottom type under any nested POINTERs. */
4870 for (t2 = TREE_TYPE (t);
4871 TREE_CODE (t2) == POINTER_TYPE;
4872 t2 = TREE_TYPE (t2))
4874 if (TREE_CODE (t2) != RECORD_TYPE
4875 && TREE_CODE (t2) != ENUMERAL_TYPE
4876 && TREE_CODE (t2) != QUAL_UNION_TYPE
4877 && TREE_CODE (t2) != UNION_TYPE)
4878 return -1;
4879 if (TYPE_SIZE (t2) == 0)
4880 return -1;
4882 /* These are the only cases that need special handling. */
4883 if (TREE_CODE (t) != RECORD_TYPE
4884 && TREE_CODE (t) != ENUMERAL_TYPE
4885 && TREE_CODE (t) != QUAL_UNION_TYPE
4886 && TREE_CODE (t) != UNION_TYPE
4887 && TREE_CODE (t) != POINTER_TYPE)
4888 return -1;
4889 /* Undefined? */
4890 if (TYPE_SIZE (t) == 0)
4891 return -1;
4893 /* Look up t in hash table. Only one of the compatible types within each
4894 alias set is recorded in the table. */
4895 if (!type_hash_table)
4896 type_hash_table = htab_create_ggc (1021, c_type_hash,
4897 (htab_eq) lang_hooks.types_compatible_p,
4898 NULL);
4899 slot = htab_find_slot (type_hash_table, t, INSERT);
4900 if (*slot != NULL)
4902 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4903 return TYPE_ALIAS_SET ((tree)*slot);
4905 else
4906 /* Our caller will assign and record (in t) a new alias set; all we need
4907 to do is remember t in the hash table. */
4908 *slot = t;
4910 return -1;
4913 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4914 the second parameter indicates which OPERATOR is being applied.
4915 The COMPLAIN flag controls whether we should diagnose possibly
4916 ill-formed constructs or not. LOC is the location of the SIZEOF or
4917 TYPEOF operator. */
4919 tree
4920 c_sizeof_or_alignof_type (location_t loc,
4921 tree type, bool is_sizeof, int complain)
4923 const char *op_name;
4924 tree value = NULL;
4925 enum tree_code type_code = TREE_CODE (type);
4927 op_name = is_sizeof ? "sizeof" : "__alignof__";
4929 if (type_code == FUNCTION_TYPE)
4931 if (is_sizeof)
4933 if (complain && warn_pointer_arith)
4934 pedwarn (loc, OPT_Wpointer_arith,
4935 "invalid application of %<sizeof%> to a function type");
4936 else if (!complain)
4937 return error_mark_node;
4938 value = size_one_node;
4940 else
4942 if (complain)
4944 if (c_dialect_cxx ())
4945 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4946 "%<alignof%> applied to a function type");
4947 else
4948 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4949 "%<_Alignof%> applied to a function type");
4951 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4954 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4956 if (type_code == VOID_TYPE
4957 && complain && warn_pointer_arith)
4958 pedwarn (loc, OPT_Wpointer_arith,
4959 "invalid application of %qs to a void type", op_name);
4960 else if (!complain)
4961 return error_mark_node;
4962 value = size_one_node;
4964 else if (!COMPLETE_TYPE_P (type)
4965 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4967 if (complain)
4968 error_at (loc, "invalid application of %qs to incomplete type %qT",
4969 op_name, type);
4970 return error_mark_node;
4972 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4973 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4975 if (complain)
4976 error_at (loc, "invalid application of %qs to array type %qT of "
4977 "incomplete element type", op_name, type);
4978 return error_mark_node;
4980 else
4982 if (is_sizeof)
4983 /* Convert in case a char is more than one unit. */
4984 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4985 size_int (TYPE_PRECISION (char_type_node)
4986 / BITS_PER_UNIT));
4987 else
4988 value = size_int (TYPE_ALIGN_UNIT (type));
4991 /* VALUE will have the middle-end integer type sizetype.
4992 However, we should really return a value of type `size_t',
4993 which is just a typedef for an ordinary integer type. */
4994 value = fold_convert_loc (loc, size_type_node, value);
4996 return value;
4999 /* Implement the __alignof keyword: Return the minimum required
5000 alignment of EXPR, measured in bytes. For VAR_DECLs,
5001 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5002 from an "aligned" __attribute__ specification). LOC is the
5003 location of the ALIGNOF operator. */
5005 tree
5006 c_alignof_expr (location_t loc, tree expr)
5008 tree t;
5010 if (VAR_OR_FUNCTION_DECL_P (expr))
5011 t = size_int (DECL_ALIGN_UNIT (expr));
5013 else if (TREE_CODE (expr) == COMPONENT_REF
5014 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5016 error_at (loc, "%<__alignof%> applied to a bit-field");
5017 t = size_one_node;
5019 else if (TREE_CODE (expr) == COMPONENT_REF
5020 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5021 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5023 else if (TREE_CODE (expr) == INDIRECT_REF)
5025 tree t = TREE_OPERAND (expr, 0);
5026 tree best = t;
5027 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5029 while (CONVERT_EXPR_P (t)
5030 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5032 int thisalign;
5034 t = TREE_OPERAND (t, 0);
5035 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5036 if (thisalign > bestalign)
5037 best = t, bestalign = thisalign;
5039 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5041 else
5042 return c_alignof (loc, TREE_TYPE (expr));
5044 return fold_convert_loc (loc, size_type_node, t);
5047 /* Handle C and C++ default attributes. */
5049 enum built_in_attribute
5051 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5052 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5053 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5054 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5055 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5056 #include "builtin-attrs.def"
5057 #undef DEF_ATTR_NULL_TREE
5058 #undef DEF_ATTR_INT
5059 #undef DEF_ATTR_STRING
5060 #undef DEF_ATTR_IDENT
5061 #undef DEF_ATTR_TREE_LIST
5062 ATTR_LAST
5065 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5067 static void c_init_attributes (void);
5069 enum c_builtin_type
5071 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5072 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5073 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5074 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5075 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5076 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5077 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5078 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5079 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5080 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5081 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5082 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5083 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5084 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5085 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5086 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5087 NAME,
5088 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5089 #include "builtin-types.def"
5090 #undef DEF_PRIMITIVE_TYPE
5091 #undef DEF_FUNCTION_TYPE_0
5092 #undef DEF_FUNCTION_TYPE_1
5093 #undef DEF_FUNCTION_TYPE_2
5094 #undef DEF_FUNCTION_TYPE_3
5095 #undef DEF_FUNCTION_TYPE_4
5096 #undef DEF_FUNCTION_TYPE_5
5097 #undef DEF_FUNCTION_TYPE_6
5098 #undef DEF_FUNCTION_TYPE_7
5099 #undef DEF_FUNCTION_TYPE_8
5100 #undef DEF_FUNCTION_TYPE_VAR_0
5101 #undef DEF_FUNCTION_TYPE_VAR_1
5102 #undef DEF_FUNCTION_TYPE_VAR_2
5103 #undef DEF_FUNCTION_TYPE_VAR_3
5104 #undef DEF_FUNCTION_TYPE_VAR_4
5105 #undef DEF_FUNCTION_TYPE_VAR_5
5106 #undef DEF_POINTER_TYPE
5107 BT_LAST
5110 typedef enum c_builtin_type builtin_type;
5112 /* A temporary array for c_common_nodes_and_builtins. Used in
5113 communication with def_fn_type. */
5114 static tree builtin_types[(int) BT_LAST + 1];
5116 /* A helper function for c_common_nodes_and_builtins. Build function type
5117 for DEF with return type RET and N arguments. If VAR is true, then the
5118 function should be variadic after those N arguments.
5120 Takes special care not to ICE if any of the types involved are
5121 error_mark_node, which indicates that said type is not in fact available
5122 (see builtin_type_for_size). In which case the function type as a whole
5123 should be error_mark_node. */
5125 static void
5126 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5128 tree t;
5129 tree *args = XALLOCAVEC (tree, n);
5130 va_list list;
5131 int i;
5133 va_start (list, n);
5134 for (i = 0; i < n; ++i)
5136 builtin_type a = (builtin_type) va_arg (list, int);
5137 t = builtin_types[a];
5138 if (t == error_mark_node)
5139 goto egress;
5140 args[i] = t;
5143 t = builtin_types[ret];
5144 if (t == error_mark_node)
5145 goto egress;
5146 if (var)
5147 t = build_varargs_function_type_array (t, n, args);
5148 else
5149 t = build_function_type_array (t, n, args);
5151 egress:
5152 builtin_types[def] = t;
5153 va_end (list);
5156 /* Build builtin functions common to both C and C++ language
5157 frontends. */
5159 static void
5160 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5162 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5163 builtin_types[ENUM] = VALUE;
5164 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5165 def_fn_type (ENUM, RETURN, 0, 0);
5166 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5167 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5168 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5169 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5170 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5171 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5172 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5173 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5174 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5175 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5176 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5177 ARG6) \
5178 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5179 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5180 ARG6, ARG7) \
5181 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5182 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5183 ARG6, ARG7, ARG8) \
5184 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5185 ARG7, ARG8);
5186 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5187 def_fn_type (ENUM, RETURN, 1, 0);
5188 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5189 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5190 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5191 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5192 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5193 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5194 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5195 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5196 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5197 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5198 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5199 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5201 #include "builtin-types.def"
5203 #undef DEF_PRIMITIVE_TYPE
5204 #undef DEF_FUNCTION_TYPE_1
5205 #undef DEF_FUNCTION_TYPE_2
5206 #undef DEF_FUNCTION_TYPE_3
5207 #undef DEF_FUNCTION_TYPE_4
5208 #undef DEF_FUNCTION_TYPE_5
5209 #undef DEF_FUNCTION_TYPE_6
5210 #undef DEF_FUNCTION_TYPE_VAR_0
5211 #undef DEF_FUNCTION_TYPE_VAR_1
5212 #undef DEF_FUNCTION_TYPE_VAR_2
5213 #undef DEF_FUNCTION_TYPE_VAR_3
5214 #undef DEF_FUNCTION_TYPE_VAR_4
5215 #undef DEF_FUNCTION_TYPE_VAR_5
5216 #undef DEF_POINTER_TYPE
5217 builtin_types[(int) BT_LAST] = NULL_TREE;
5219 c_init_attributes ();
5221 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5222 NONANSI_P, ATTRS, IMPLICIT, COND) \
5223 if (NAME && COND) \
5224 def_builtin_1 (ENUM, NAME, CLASS, \
5225 builtin_types[(int) TYPE], \
5226 builtin_types[(int) LIBTYPE], \
5227 BOTH_P, FALLBACK_P, NONANSI_P, \
5228 built_in_attributes[(int) ATTRS], IMPLICIT);
5229 #include "builtins.def"
5230 #undef DEF_BUILTIN
5232 targetm.init_builtins ();
5234 build_common_builtin_nodes ();
5236 if (flag_enable_cilkplus)
5237 cilk_init_builtins ();
5240 /* Like get_identifier, but avoid warnings about null arguments when
5241 the argument may be NULL for targets where GCC lacks stdint.h type
5242 information. */
5244 static inline tree
5245 c_get_ident (const char *id)
5247 return get_identifier (id);
5250 /* Build tree nodes and builtin functions common to both C and C++ language
5251 frontends. */
5253 void
5254 c_common_nodes_and_builtins (void)
5256 int char16_type_size;
5257 int char32_type_size;
5258 int wchar_type_size;
5259 tree array_domain_type;
5260 tree va_list_ref_type_node;
5261 tree va_list_arg_type_node;
5263 build_common_tree_nodes (flag_signed_char, flag_short_double);
5265 /* Define `int' and `char' first so that dbx will output them first. */
5266 record_builtin_type (RID_INT, NULL, integer_type_node);
5267 record_builtin_type (RID_CHAR, "char", char_type_node);
5269 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5270 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5271 but not C. Are the conditionals here needed? */
5272 if (c_dialect_cxx ())
5273 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5274 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5275 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5276 record_builtin_type (RID_MAX, "long unsigned int",
5277 long_unsigned_type_node);
5278 if (int128_integer_type_node != NULL_TREE)
5280 record_builtin_type (RID_INT128, "__int128",
5281 int128_integer_type_node);
5282 record_builtin_type (RID_MAX, "__int128 unsigned",
5283 int128_unsigned_type_node);
5285 if (c_dialect_cxx ())
5286 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5287 record_builtin_type (RID_MAX, "long long int",
5288 long_long_integer_type_node);
5289 record_builtin_type (RID_MAX, "long long unsigned int",
5290 long_long_unsigned_type_node);
5291 if (c_dialect_cxx ())
5292 record_builtin_type (RID_MAX, "long long unsigned",
5293 long_long_unsigned_type_node);
5294 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5295 record_builtin_type (RID_MAX, "short unsigned int",
5296 short_unsigned_type_node);
5297 if (c_dialect_cxx ())
5298 record_builtin_type (RID_MAX, "unsigned short",
5299 short_unsigned_type_node);
5301 /* Define both `signed char' and `unsigned char'. */
5302 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5303 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5305 /* These are types that c_common_type_for_size and
5306 c_common_type_for_mode use. */
5307 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5308 TYPE_DECL, NULL_TREE,
5309 intQI_type_node));
5310 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5311 TYPE_DECL, NULL_TREE,
5312 intHI_type_node));
5313 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5314 TYPE_DECL, NULL_TREE,
5315 intSI_type_node));
5316 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5317 TYPE_DECL, NULL_TREE,
5318 intDI_type_node));
5319 #if HOST_BITS_PER_WIDE_INT >= 64
5320 if (targetm.scalar_mode_supported_p (TImode))
5321 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5322 TYPE_DECL,
5323 get_identifier ("__int128_t"),
5324 intTI_type_node));
5325 #endif
5326 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5327 TYPE_DECL, NULL_TREE,
5328 unsigned_intQI_type_node));
5329 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5330 TYPE_DECL, NULL_TREE,
5331 unsigned_intHI_type_node));
5332 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5333 TYPE_DECL, NULL_TREE,
5334 unsigned_intSI_type_node));
5335 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5336 TYPE_DECL, NULL_TREE,
5337 unsigned_intDI_type_node));
5338 #if HOST_BITS_PER_WIDE_INT >= 64
5339 if (targetm.scalar_mode_supported_p (TImode))
5340 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5341 TYPE_DECL,
5342 get_identifier ("__uint128_t"),
5343 unsigned_intTI_type_node));
5344 #endif
5346 /* Create the widest literal types. */
5347 widest_integer_literal_type_node
5348 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5349 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5350 TYPE_DECL, NULL_TREE,
5351 widest_integer_literal_type_node));
5353 widest_unsigned_literal_type_node
5354 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5355 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5356 TYPE_DECL, NULL_TREE,
5357 widest_unsigned_literal_type_node));
5359 signed_size_type_node = c_common_signed_type (size_type_node);
5361 pid_type_node =
5362 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5364 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5365 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5366 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5368 /* Only supported decimal floating point extension if the target
5369 actually supports underlying modes. */
5370 if (targetm.scalar_mode_supported_p (SDmode)
5371 && targetm.scalar_mode_supported_p (DDmode)
5372 && targetm.scalar_mode_supported_p (TDmode))
5374 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5375 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5376 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5379 if (targetm.fixed_point_supported_p ())
5381 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5382 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5383 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5384 record_builtin_type (RID_MAX, "long long _Fract",
5385 long_long_fract_type_node);
5386 record_builtin_type (RID_MAX, "unsigned short _Fract",
5387 unsigned_short_fract_type_node);
5388 record_builtin_type (RID_MAX, "unsigned _Fract",
5389 unsigned_fract_type_node);
5390 record_builtin_type (RID_MAX, "unsigned long _Fract",
5391 unsigned_long_fract_type_node);
5392 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5393 unsigned_long_long_fract_type_node);
5394 record_builtin_type (RID_MAX, "_Sat short _Fract",
5395 sat_short_fract_type_node);
5396 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5397 record_builtin_type (RID_MAX, "_Sat long _Fract",
5398 sat_long_fract_type_node);
5399 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5400 sat_long_long_fract_type_node);
5401 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5402 sat_unsigned_short_fract_type_node);
5403 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5404 sat_unsigned_fract_type_node);
5405 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5406 sat_unsigned_long_fract_type_node);
5407 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5408 sat_unsigned_long_long_fract_type_node);
5409 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5410 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5411 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5412 record_builtin_type (RID_MAX, "long long _Accum",
5413 long_long_accum_type_node);
5414 record_builtin_type (RID_MAX, "unsigned short _Accum",
5415 unsigned_short_accum_type_node);
5416 record_builtin_type (RID_MAX, "unsigned _Accum",
5417 unsigned_accum_type_node);
5418 record_builtin_type (RID_MAX, "unsigned long _Accum",
5419 unsigned_long_accum_type_node);
5420 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5421 unsigned_long_long_accum_type_node);
5422 record_builtin_type (RID_MAX, "_Sat short _Accum",
5423 sat_short_accum_type_node);
5424 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5425 record_builtin_type (RID_MAX, "_Sat long _Accum",
5426 sat_long_accum_type_node);
5427 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5428 sat_long_long_accum_type_node);
5429 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5430 sat_unsigned_short_accum_type_node);
5431 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5432 sat_unsigned_accum_type_node);
5433 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5434 sat_unsigned_long_accum_type_node);
5435 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5436 sat_unsigned_long_long_accum_type_node);
5440 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5441 TYPE_DECL,
5442 get_identifier ("complex int"),
5443 complex_integer_type_node));
5444 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5445 TYPE_DECL,
5446 get_identifier ("complex float"),
5447 complex_float_type_node));
5448 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5449 TYPE_DECL,
5450 get_identifier ("complex double"),
5451 complex_double_type_node));
5452 lang_hooks.decls.pushdecl
5453 (build_decl (UNKNOWN_LOCATION,
5454 TYPE_DECL, get_identifier ("complex long double"),
5455 complex_long_double_type_node));
5457 if (c_dialect_cxx ())
5458 /* For C++, make fileptr_type_node a distinct void * type until
5459 FILE type is defined. */
5460 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5462 record_builtin_type (RID_VOID, NULL, void_type_node);
5464 /* Set the TYPE_NAME for any variants that were built before
5465 record_builtin_type gave names to the built-in types. */
5467 tree void_name = TYPE_NAME (void_type_node);
5468 TYPE_NAME (void_type_node) = NULL_TREE;
5469 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5470 = void_name;
5471 TYPE_NAME (void_type_node) = void_name;
5474 /* This node must not be shared. */
5475 void_zero_node = make_node (INTEGER_CST);
5476 TREE_TYPE (void_zero_node) = void_type_node;
5478 void_list_node = build_void_list_node ();
5480 /* Make a type to be the domain of a few array types
5481 whose domains don't really matter.
5482 200 is small enough that it always fits in size_t
5483 and large enough that it can hold most function names for the
5484 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5485 array_domain_type = build_index_type (size_int (200));
5487 /* Make a type for arrays of characters.
5488 With luck nothing will ever really depend on the length of this
5489 array type. */
5490 char_array_type_node
5491 = build_array_type (char_type_node, array_domain_type);
5493 /* Likewise for arrays of ints. */
5494 int_array_type_node
5495 = build_array_type (integer_type_node, array_domain_type);
5497 string_type_node = build_pointer_type (char_type_node);
5498 const_string_type_node
5499 = build_pointer_type (build_qualified_type
5500 (char_type_node, TYPE_QUAL_CONST));
5502 /* This is special for C++ so functions can be overloaded. */
5503 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5504 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5505 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5506 underlying_wchar_type_node = wchar_type_node;
5507 if (c_dialect_cxx ())
5509 if (TYPE_UNSIGNED (wchar_type_node))
5510 wchar_type_node = make_unsigned_type (wchar_type_size);
5511 else
5512 wchar_type_node = make_signed_type (wchar_type_size);
5513 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5516 /* This is for wide string constants. */
5517 wchar_array_type_node
5518 = build_array_type (wchar_type_node, array_domain_type);
5520 /* Define 'char16_t'. */
5521 char16_type_node = get_identifier (CHAR16_TYPE);
5522 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5523 char16_type_size = TYPE_PRECISION (char16_type_node);
5524 if (c_dialect_cxx ())
5526 char16_type_node = make_unsigned_type (char16_type_size);
5528 if (cxx_dialect >= cxx11)
5529 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5532 /* This is for UTF-16 string constants. */
5533 char16_array_type_node
5534 = build_array_type (char16_type_node, array_domain_type);
5536 /* Define 'char32_t'. */
5537 char32_type_node = get_identifier (CHAR32_TYPE);
5538 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5539 char32_type_size = TYPE_PRECISION (char32_type_node);
5540 if (c_dialect_cxx ())
5542 char32_type_node = make_unsigned_type (char32_type_size);
5544 if (cxx_dialect >= cxx11)
5545 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5548 /* This is for UTF-32 string constants. */
5549 char32_array_type_node
5550 = build_array_type (char32_type_node, array_domain_type);
5552 wint_type_node =
5553 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5555 intmax_type_node =
5556 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5557 uintmax_type_node =
5558 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5560 if (SIG_ATOMIC_TYPE)
5561 sig_atomic_type_node =
5562 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5563 if (INT8_TYPE)
5564 int8_type_node =
5565 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5566 if (INT16_TYPE)
5567 int16_type_node =
5568 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5569 if (INT32_TYPE)
5570 int32_type_node =
5571 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5572 if (INT64_TYPE)
5573 int64_type_node =
5574 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5575 if (UINT8_TYPE)
5576 uint8_type_node =
5577 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5578 if (UINT16_TYPE)
5579 c_uint16_type_node =
5580 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5581 if (UINT32_TYPE)
5582 c_uint32_type_node =
5583 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5584 if (UINT64_TYPE)
5585 c_uint64_type_node =
5586 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5587 if (INT_LEAST8_TYPE)
5588 int_least8_type_node =
5589 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5590 if (INT_LEAST16_TYPE)
5591 int_least16_type_node =
5592 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5593 if (INT_LEAST32_TYPE)
5594 int_least32_type_node =
5595 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5596 if (INT_LEAST64_TYPE)
5597 int_least64_type_node =
5598 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5599 if (UINT_LEAST8_TYPE)
5600 uint_least8_type_node =
5601 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5602 if (UINT_LEAST16_TYPE)
5603 uint_least16_type_node =
5604 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5605 if (UINT_LEAST32_TYPE)
5606 uint_least32_type_node =
5607 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5608 if (UINT_LEAST64_TYPE)
5609 uint_least64_type_node =
5610 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5611 if (INT_FAST8_TYPE)
5612 int_fast8_type_node =
5613 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5614 if (INT_FAST16_TYPE)
5615 int_fast16_type_node =
5616 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5617 if (INT_FAST32_TYPE)
5618 int_fast32_type_node =
5619 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5620 if (INT_FAST64_TYPE)
5621 int_fast64_type_node =
5622 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5623 if (UINT_FAST8_TYPE)
5624 uint_fast8_type_node =
5625 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5626 if (UINT_FAST16_TYPE)
5627 uint_fast16_type_node =
5628 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5629 if (UINT_FAST32_TYPE)
5630 uint_fast32_type_node =
5631 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5632 if (UINT_FAST64_TYPE)
5633 uint_fast64_type_node =
5634 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5635 if (INTPTR_TYPE)
5636 intptr_type_node =
5637 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5638 if (UINTPTR_TYPE)
5639 uintptr_type_node =
5640 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5642 default_function_type
5643 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5644 ptrdiff_type_node
5645 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5646 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5648 lang_hooks.decls.pushdecl
5649 (build_decl (UNKNOWN_LOCATION,
5650 TYPE_DECL, get_identifier ("__builtin_va_list"),
5651 va_list_type_node));
5652 if (targetm.enum_va_list_p)
5654 int l;
5655 const char *pname;
5656 tree ptype;
5658 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5660 lang_hooks.decls.pushdecl
5661 (build_decl (UNKNOWN_LOCATION,
5662 TYPE_DECL, get_identifier (pname),
5663 ptype));
5668 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5670 va_list_arg_type_node = va_list_ref_type_node =
5671 build_pointer_type (TREE_TYPE (va_list_type_node));
5673 else
5675 va_list_arg_type_node = va_list_type_node;
5676 va_list_ref_type_node = build_reference_type (va_list_type_node);
5679 if (!flag_preprocess_only)
5680 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5682 main_identifier_node = get_identifier ("main");
5684 /* Create the built-in __null node. It is important that this is
5685 not shared. */
5686 null_node = make_node (INTEGER_CST);
5687 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5689 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5690 memset (builtin_types, 0, sizeof (builtin_types));
5693 /* The number of named compound-literals generated thus far. */
5694 static GTY(()) int compound_literal_number;
5696 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5698 void
5699 set_compound_literal_name (tree decl)
5701 char *name;
5702 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5703 compound_literal_number);
5704 compound_literal_number++;
5705 DECL_NAME (decl) = get_identifier (name);
5708 tree
5709 build_va_arg (location_t loc, tree expr, tree type)
5711 expr = build1 (VA_ARG_EXPR, type, expr);
5712 SET_EXPR_LOCATION (expr, loc);
5713 return expr;
5717 /* Linked list of disabled built-in functions. */
5719 typedef struct disabled_builtin
5721 const char *name;
5722 struct disabled_builtin *next;
5723 } disabled_builtin;
5724 static disabled_builtin *disabled_builtins = NULL;
5726 static bool builtin_function_disabled_p (const char *);
5728 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5729 begins with "__builtin_", give an error. */
5731 void
5732 disable_builtin_function (const char *name)
5734 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5735 error ("cannot disable built-in function %qs", name);
5736 else
5738 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5739 new_disabled_builtin->name = name;
5740 new_disabled_builtin->next = disabled_builtins;
5741 disabled_builtins = new_disabled_builtin;
5746 /* Return true if the built-in function NAME has been disabled, false
5747 otherwise. */
5749 static bool
5750 builtin_function_disabled_p (const char *name)
5752 disabled_builtin *p;
5753 for (p = disabled_builtins; p != NULL; p = p->next)
5755 if (strcmp (name, p->name) == 0)
5756 return true;
5758 return false;
5762 /* Worker for DEF_BUILTIN.
5763 Possibly define a builtin function with one or two names.
5764 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5765 nonansi_p and flag_no_nonansi_builtin. */
5767 static void
5768 def_builtin_1 (enum built_in_function fncode,
5769 const char *name,
5770 enum built_in_class fnclass,
5771 tree fntype, tree libtype,
5772 bool both_p, bool fallback_p, bool nonansi_p,
5773 tree fnattrs, bool implicit_p)
5775 tree decl;
5776 const char *libname;
5778 if (fntype == error_mark_node)
5779 return;
5781 gcc_assert ((!both_p && !fallback_p)
5782 || !strncmp (name, "__builtin_",
5783 strlen ("__builtin_")));
5785 libname = name + strlen ("__builtin_");
5786 decl = add_builtin_function (name, fntype, fncode, fnclass,
5787 (fallback_p ? libname : NULL),
5788 fnattrs);
5790 set_builtin_decl (fncode, decl, implicit_p);
5792 if (both_p
5793 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5794 && !(nonansi_p && flag_no_nonansi_builtin))
5795 add_builtin_function (libname, libtype, fncode, fnclass,
5796 NULL, fnattrs);
5799 /* Nonzero if the type T promotes to int. This is (nearly) the
5800 integral promotions defined in ISO C99 6.3.1.1/2. */
5802 bool
5803 c_promoting_integer_type_p (const_tree t)
5805 switch (TREE_CODE (t))
5807 case INTEGER_TYPE:
5808 return (TYPE_MAIN_VARIANT (t) == char_type_node
5809 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5810 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5811 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5812 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5813 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5815 case ENUMERAL_TYPE:
5816 /* ??? Technically all enumerations not larger than an int
5817 promote to an int. But this is used along code paths
5818 that only want to notice a size change. */
5819 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5821 case BOOLEAN_TYPE:
5822 return 1;
5824 default:
5825 return 0;
5829 /* Return 1 if PARMS specifies a fixed number of parameters
5830 and none of their types is affected by default promotions. */
5833 self_promoting_args_p (const_tree parms)
5835 const_tree t;
5836 for (t = parms; t; t = TREE_CHAIN (t))
5838 tree type = TREE_VALUE (t);
5840 if (type == error_mark_node)
5841 continue;
5843 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5844 return 0;
5846 if (type == 0)
5847 return 0;
5849 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5850 return 0;
5852 if (c_promoting_integer_type_p (type))
5853 return 0;
5855 return 1;
5858 /* Recursively remove any '*' or '&' operator from TYPE. */
5859 tree
5860 strip_pointer_operator (tree t)
5862 while (POINTER_TYPE_P (t))
5863 t = TREE_TYPE (t);
5864 return t;
5867 /* Recursively remove pointer or array type from TYPE. */
5868 tree
5869 strip_pointer_or_array_types (tree t)
5871 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5872 t = TREE_TYPE (t);
5873 return t;
5876 /* Used to compare case labels. K1 and K2 are actually tree nodes
5877 representing case labels, or NULL_TREE for a `default' label.
5878 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5879 K2, and 0 if K1 and K2 are equal. */
5882 case_compare (splay_tree_key k1, splay_tree_key k2)
5884 /* Consider a NULL key (such as arises with a `default' label) to be
5885 smaller than anything else. */
5886 if (!k1)
5887 return k2 ? -1 : 0;
5888 else if (!k2)
5889 return k1 ? 1 : 0;
5891 return tree_int_cst_compare ((tree) k1, (tree) k2);
5894 /* Process a case label, located at LOC, for the range LOW_VALUE
5895 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5896 then this case label is actually a `default' label. If only
5897 HIGH_VALUE is NULL_TREE, then case label was declared using the
5898 usual C/C++ syntax, rather than the GNU case range extension.
5899 CASES is a tree containing all the case ranges processed so far;
5900 COND is the condition for the switch-statement itself. Returns the
5901 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5902 is created. */
5904 tree
5905 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5906 tree low_value, tree high_value)
5908 tree type;
5909 tree label;
5910 tree case_label;
5911 splay_tree_node node;
5913 /* Create the LABEL_DECL itself. */
5914 label = create_artificial_label (loc);
5916 /* If there was an error processing the switch condition, bail now
5917 before we get more confused. */
5918 if (!cond || cond == error_mark_node)
5919 goto error_out;
5921 if ((low_value && TREE_TYPE (low_value)
5922 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5923 || (high_value && TREE_TYPE (high_value)
5924 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5926 error_at (loc, "pointers are not permitted as case values");
5927 goto error_out;
5930 /* Case ranges are a GNU extension. */
5931 if (high_value)
5932 pedwarn (loc, OPT_Wpedantic,
5933 "range expressions in switch statements are non-standard");
5935 type = TREE_TYPE (cond);
5936 if (low_value)
5938 low_value = check_case_value (low_value);
5939 low_value = convert_and_check (type, low_value);
5940 if (low_value == error_mark_node)
5941 goto error_out;
5943 if (high_value)
5945 high_value = check_case_value (high_value);
5946 high_value = convert_and_check (type, high_value);
5947 if (high_value == error_mark_node)
5948 goto error_out;
5951 if (low_value && high_value)
5953 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5954 really a case range, even though it was written that way.
5955 Remove the HIGH_VALUE to simplify later processing. */
5956 if (tree_int_cst_equal (low_value, high_value))
5957 high_value = NULL_TREE;
5958 else if (!tree_int_cst_lt (low_value, high_value))
5959 warning_at (loc, 0, "empty range specified");
5962 /* See if the case is in range of the type of the original testing
5963 expression. If both low_value and high_value are out of range,
5964 don't insert the case label and return NULL_TREE. */
5965 if (low_value
5966 && !check_case_bounds (type, orig_type,
5967 &low_value, high_value ? &high_value : NULL))
5968 return NULL_TREE;
5970 /* Look up the LOW_VALUE in the table of case labels we already
5971 have. */
5972 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5973 /* If there was not an exact match, check for overlapping ranges.
5974 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5975 that's a `default' label and the only overlap is an exact match. */
5976 if (!node && (low_value || high_value))
5978 splay_tree_node low_bound;
5979 splay_tree_node high_bound;
5981 /* Even though there wasn't an exact match, there might be an
5982 overlap between this case range and another case range.
5983 Since we've (inductively) not allowed any overlapping case
5984 ranges, we simply need to find the greatest low case label
5985 that is smaller that LOW_VALUE, and the smallest low case
5986 label that is greater than LOW_VALUE. If there is an overlap
5987 it will occur in one of these two ranges. */
5988 low_bound = splay_tree_predecessor (cases,
5989 (splay_tree_key) low_value);
5990 high_bound = splay_tree_successor (cases,
5991 (splay_tree_key) low_value);
5993 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5994 the LOW_VALUE, so there is no need to check unless the
5995 LOW_BOUND is in fact itself a case range. */
5996 if (low_bound
5997 && CASE_HIGH ((tree) low_bound->value)
5998 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5999 low_value) >= 0)
6000 node = low_bound;
6001 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6002 range is bigger than the low end of the current range, so we
6003 are only interested if the current range is a real range, and
6004 not an ordinary case label. */
6005 else if (high_bound
6006 && high_value
6007 && (tree_int_cst_compare ((tree) high_bound->key,
6008 high_value)
6009 <= 0))
6010 node = high_bound;
6012 /* If there was an overlap, issue an error. */
6013 if (node)
6015 tree duplicate = CASE_LABEL ((tree) node->value);
6017 if (high_value)
6019 error_at (loc, "duplicate (or overlapping) case value");
6020 error_at (DECL_SOURCE_LOCATION (duplicate),
6021 "this is the first entry overlapping that value");
6023 else if (low_value)
6025 error_at (loc, "duplicate case value") ;
6026 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6028 else
6030 error_at (loc, "multiple default labels in one switch");
6031 error_at (DECL_SOURCE_LOCATION (duplicate),
6032 "this is the first default label");
6034 goto error_out;
6037 /* Add a CASE_LABEL to the statement-tree. */
6038 case_label = add_stmt (build_case_label (low_value, high_value, label));
6039 /* Register this case label in the splay tree. */
6040 splay_tree_insert (cases,
6041 (splay_tree_key) low_value,
6042 (splay_tree_value) case_label);
6044 return case_label;
6046 error_out:
6047 /* Add a label so that the back-end doesn't think that the beginning of
6048 the switch is unreachable. Note that we do not add a case label, as
6049 that just leads to duplicates and thence to failure later on. */
6050 if (!cases->root)
6052 tree t = create_artificial_label (loc);
6053 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6055 return error_mark_node;
6058 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6059 Used to verify that case values match up with enumerator values. */
6061 static void
6062 match_case_to_enum_1 (tree key, tree type, tree label)
6064 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6066 /* ??? Not working too hard to print the double-word value.
6067 Should perhaps be done with %lwd in the diagnostic routines? */
6068 if (TREE_INT_CST_HIGH (key) == 0)
6069 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6070 TREE_INT_CST_LOW (key));
6071 else if (!TYPE_UNSIGNED (type)
6072 && TREE_INT_CST_HIGH (key) == -1
6073 && TREE_INT_CST_LOW (key) != 0)
6074 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6075 -TREE_INT_CST_LOW (key));
6076 else
6077 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6078 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6079 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6081 if (TYPE_NAME (type) == 0)
6082 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6083 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6084 "case value %qs not in enumerated type",
6085 buf);
6086 else
6087 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6088 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6089 "case value %qs not in enumerated type %qT",
6090 buf, type);
6093 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6094 Used to verify that case values match up with enumerator values. */
6096 static int
6097 match_case_to_enum (splay_tree_node node, void *data)
6099 tree label = (tree) node->value;
6100 tree type = (tree) data;
6102 /* Skip default case. */
6103 if (!CASE_LOW (label))
6104 return 0;
6106 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6107 when we did our enum->case scan. Reset our scratch bit after. */
6108 if (!CASE_LOW_SEEN (label))
6109 match_case_to_enum_1 (CASE_LOW (label), type, label);
6110 else
6111 CASE_LOW_SEEN (label) = 0;
6113 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6114 not set, that means that CASE_HIGH did not appear when we did our
6115 enum->case scan. Reset our scratch bit after. */
6116 if (CASE_HIGH (label))
6118 if (!CASE_HIGH_SEEN (label))
6119 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6120 else
6121 CASE_HIGH_SEEN (label) = 0;
6124 return 0;
6127 /* Handle -Wswitch*. Called from the front end after parsing the
6128 switch construct. */
6129 /* ??? Should probably be somewhere generic, since other languages
6130 besides C and C++ would want this. At the moment, however, C/C++
6131 are the only tree-ssa languages that support enumerations at all,
6132 so the point is moot. */
6134 void
6135 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6136 tree type, tree cond)
6138 splay_tree_node default_node;
6139 splay_tree_node node;
6140 tree chain;
6142 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6143 return;
6145 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6146 if (!default_node)
6147 warning_at (switch_location, OPT_Wswitch_default,
6148 "switch missing default case");
6150 /* From here on, we only care about about enumerated types. */
6151 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6152 return;
6154 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6155 if (!warn_switch_enum && !warn_switch)
6156 return;
6158 /* Check the cases. Warn about case values which are not members of
6159 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6160 there is no default case, check that exactly all enumeration
6161 literals are covered by the cases. */
6163 /* Clearing COND if it is not an integer constant simplifies
6164 the tests inside the loop below. */
6165 if (TREE_CODE (cond) != INTEGER_CST)
6166 cond = NULL_TREE;
6168 /* The time complexity here is O(N*lg(N)) worst case, but for the
6169 common case of monotonically increasing enumerators, it is
6170 O(N), since the nature of the splay tree will keep the next
6171 element adjacent to the root at all times. */
6173 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6175 tree value = TREE_VALUE (chain);
6176 if (TREE_CODE (value) == CONST_DECL)
6177 value = DECL_INITIAL (value);
6178 node = splay_tree_lookup (cases, (splay_tree_key) value);
6179 if (node)
6181 /* Mark the CASE_LOW part of the case entry as seen. */
6182 tree label = (tree) node->value;
6183 CASE_LOW_SEEN (label) = 1;
6184 continue;
6187 /* Even though there wasn't an exact match, there might be a
6188 case range which includes the enumerator's value. */
6189 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6190 if (node && CASE_HIGH ((tree) node->value))
6192 tree label = (tree) node->value;
6193 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6194 if (cmp >= 0)
6196 /* If we match the upper bound exactly, mark the CASE_HIGH
6197 part of the case entry as seen. */
6198 if (cmp == 0)
6199 CASE_HIGH_SEEN (label) = 1;
6200 continue;
6204 /* We've now determined that this enumerated literal isn't
6205 handled by the case labels of the switch statement. */
6207 /* If the switch expression is a constant, we only really care
6208 about whether that constant is handled by the switch. */
6209 if (cond && tree_int_cst_compare (cond, value))
6210 continue;
6212 /* If there is a default_node, the only relevant option is
6213 Wswitch-enum. Otherwise, if both are enabled then we prefer
6214 to warn using -Wswitch because -Wswitch is enabled by -Wall
6215 while -Wswitch-enum is explicit. */
6216 warning_at (switch_location,
6217 (default_node || !warn_switch
6218 ? OPT_Wswitch_enum
6219 : OPT_Wswitch),
6220 "enumeration value %qE not handled in switch",
6221 TREE_PURPOSE (chain));
6224 /* Warn if there are case expressions that don't correspond to
6225 enumerators. This can occur since C and C++ don't enforce
6226 type-checking of assignments to enumeration variables.
6228 The time complexity here is now always O(N) worst case, since
6229 we should have marked both the lower bound and upper bound of
6230 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6231 above. This scan also resets those fields. */
6233 splay_tree_foreach (cases, match_case_to_enum, type);
6236 /* Finish an expression taking the address of LABEL (an
6237 IDENTIFIER_NODE). Returns an expression for the address.
6239 LOC is the location for the expression returned. */
6241 tree
6242 finish_label_address_expr (tree label, location_t loc)
6244 tree result;
6246 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6248 if (label == error_mark_node)
6249 return error_mark_node;
6251 label = lookup_label (label);
6252 if (label == NULL_TREE)
6253 result = null_pointer_node;
6254 else
6256 TREE_USED (label) = 1;
6257 result = build1 (ADDR_EXPR, ptr_type_node, label);
6258 /* The current function is not necessarily uninlinable.
6259 Computed gotos are incompatible with inlining, but the value
6260 here could be used only in a diagnostic, for example. */
6261 protected_set_expr_location (result, loc);
6264 return result;
6268 /* Given a boolean expression ARG, return a tree representing an increment
6269 or decrement (as indicated by CODE) of ARG. The front end must check for
6270 invalid cases (e.g., decrement in C++). */
6271 tree
6272 boolean_increment (enum tree_code code, tree arg)
6274 tree val;
6275 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6277 arg = stabilize_reference (arg);
6278 switch (code)
6280 case PREINCREMENT_EXPR:
6281 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6282 break;
6283 case POSTINCREMENT_EXPR:
6284 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6285 arg = save_expr (arg);
6286 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6287 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6288 break;
6289 case PREDECREMENT_EXPR:
6290 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6291 invert_truthvalue_loc (input_location, arg));
6292 break;
6293 case POSTDECREMENT_EXPR:
6294 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6295 invert_truthvalue_loc (input_location, arg));
6296 arg = save_expr (arg);
6297 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6298 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6299 break;
6300 default:
6301 gcc_unreachable ();
6303 TREE_SIDE_EFFECTS (val) = 1;
6304 return val;
6307 /* Built-in macros for stddef.h and stdint.h, that require macros
6308 defined in this file. */
6309 void
6310 c_stddef_cpp_builtins(void)
6312 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6313 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6314 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6315 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6316 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6317 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6318 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6319 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6320 if (SIG_ATOMIC_TYPE)
6321 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6322 if (INT8_TYPE)
6323 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6324 if (INT16_TYPE)
6325 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6326 if (INT32_TYPE)
6327 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6328 if (INT64_TYPE)
6329 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6330 if (UINT8_TYPE)
6331 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6332 if (UINT16_TYPE)
6333 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6334 if (UINT32_TYPE)
6335 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6336 if (UINT64_TYPE)
6337 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6338 if (INT_LEAST8_TYPE)
6339 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6340 if (INT_LEAST16_TYPE)
6341 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6342 if (INT_LEAST32_TYPE)
6343 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6344 if (INT_LEAST64_TYPE)
6345 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6346 if (UINT_LEAST8_TYPE)
6347 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6348 if (UINT_LEAST16_TYPE)
6349 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6350 if (UINT_LEAST32_TYPE)
6351 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6352 if (UINT_LEAST64_TYPE)
6353 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6354 if (INT_FAST8_TYPE)
6355 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6356 if (INT_FAST16_TYPE)
6357 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6358 if (INT_FAST32_TYPE)
6359 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6360 if (INT_FAST64_TYPE)
6361 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6362 if (UINT_FAST8_TYPE)
6363 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6364 if (UINT_FAST16_TYPE)
6365 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6366 if (UINT_FAST32_TYPE)
6367 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6368 if (UINT_FAST64_TYPE)
6369 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6370 if (INTPTR_TYPE)
6371 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6372 if (UINTPTR_TYPE)
6373 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6376 static void
6377 c_init_attributes (void)
6379 /* Fill in the built_in_attributes array. */
6380 #define DEF_ATTR_NULL_TREE(ENUM) \
6381 built_in_attributes[(int) ENUM] = NULL_TREE;
6382 #define DEF_ATTR_INT(ENUM, VALUE) \
6383 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6384 #define DEF_ATTR_STRING(ENUM, VALUE) \
6385 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6386 #define DEF_ATTR_IDENT(ENUM, STRING) \
6387 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6388 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6389 built_in_attributes[(int) ENUM] \
6390 = tree_cons (built_in_attributes[(int) PURPOSE], \
6391 built_in_attributes[(int) VALUE], \
6392 built_in_attributes[(int) CHAIN]);
6393 #include "builtin-attrs.def"
6394 #undef DEF_ATTR_NULL_TREE
6395 #undef DEF_ATTR_INT
6396 #undef DEF_ATTR_IDENT
6397 #undef DEF_ATTR_TREE_LIST
6400 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6401 identifier as an argument, so the front end shouldn't look it up. */
6403 bool
6404 attribute_takes_identifier_p (const_tree attr_id)
6406 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6407 if (spec == NULL)
6408 /* Unknown attribute that we'll end up ignoring, return true so we
6409 don't complain about an identifier argument. */
6410 return true;
6411 else if (!strcmp ("mode", spec->name)
6412 || !strcmp ("format", spec->name)
6413 || !strcmp ("cleanup", spec->name))
6414 return true;
6415 else
6416 return targetm.attribute_takes_identifier_p (attr_id);
6419 /* Attribute handlers common to C front ends. */
6421 /* Handle a "packed" attribute; arguments as in
6422 struct attribute_spec.handler. */
6424 static tree
6425 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6426 int flags, bool *no_add_attrs)
6428 if (TYPE_P (*node))
6430 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6431 *node = build_variant_type_copy (*node);
6432 TYPE_PACKED (*node) = 1;
6434 else if (TREE_CODE (*node) == FIELD_DECL)
6436 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6437 /* Still pack bitfields. */
6438 && ! DECL_INITIAL (*node))
6439 warning (OPT_Wattributes,
6440 "%qE attribute ignored for field of type %qT",
6441 name, TREE_TYPE (*node));
6442 else
6443 DECL_PACKED (*node) = 1;
6445 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6446 used for DECL_REGISTER. It wouldn't mean anything anyway.
6447 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6448 that changes what the typedef is typing. */
6449 else
6451 warning (OPT_Wattributes, "%qE attribute ignored", name);
6452 *no_add_attrs = true;
6455 return NULL_TREE;
6458 /* Handle a "nocommon" attribute; arguments as in
6459 struct attribute_spec.handler. */
6461 static tree
6462 handle_nocommon_attribute (tree *node, tree name,
6463 tree ARG_UNUSED (args),
6464 int ARG_UNUSED (flags), bool *no_add_attrs)
6466 if (TREE_CODE (*node) == VAR_DECL)
6467 DECL_COMMON (*node) = 0;
6468 else
6470 warning (OPT_Wattributes, "%qE attribute ignored", name);
6471 *no_add_attrs = true;
6474 return NULL_TREE;
6477 /* Handle a "common" attribute; arguments as in
6478 struct attribute_spec.handler. */
6480 static tree
6481 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6482 int ARG_UNUSED (flags), bool *no_add_attrs)
6484 if (TREE_CODE (*node) == VAR_DECL)
6485 DECL_COMMON (*node) = 1;
6486 else
6488 warning (OPT_Wattributes, "%qE attribute ignored", name);
6489 *no_add_attrs = true;
6492 return NULL_TREE;
6495 /* Handle a "noreturn" attribute; arguments as in
6496 struct attribute_spec.handler. */
6498 static tree
6499 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6500 int ARG_UNUSED (flags), bool *no_add_attrs)
6502 tree type = TREE_TYPE (*node);
6504 /* See FIXME comment in c_common_attribute_table. */
6505 if (TREE_CODE (*node) == FUNCTION_DECL
6506 || objc_method_decl (TREE_CODE (*node)))
6507 TREE_THIS_VOLATILE (*node) = 1;
6508 else if (TREE_CODE (type) == POINTER_TYPE
6509 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6510 TREE_TYPE (*node)
6511 = build_pointer_type
6512 (build_type_variant (TREE_TYPE (type),
6513 TYPE_READONLY (TREE_TYPE (type)), 1));
6514 else
6516 warning (OPT_Wattributes, "%qE attribute ignored", name);
6517 *no_add_attrs = true;
6520 return NULL_TREE;
6523 /* Handle a "hot" and attribute; arguments as in
6524 struct attribute_spec.handler. */
6526 static tree
6527 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6528 int ARG_UNUSED (flags), bool *no_add_attrs)
6530 if (TREE_CODE (*node) == FUNCTION_DECL
6531 || TREE_CODE (*node) == LABEL_DECL)
6533 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6535 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6536 name, "cold");
6537 *no_add_attrs = true;
6539 /* Most of the rest of the hot processing is done later with
6540 lookup_attribute. */
6542 else
6544 warning (OPT_Wattributes, "%qE attribute ignored", name);
6545 *no_add_attrs = true;
6548 return NULL_TREE;
6551 /* Handle a "cold" and attribute; arguments as in
6552 struct attribute_spec.handler. */
6554 static tree
6555 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6556 int ARG_UNUSED (flags), bool *no_add_attrs)
6558 if (TREE_CODE (*node) == FUNCTION_DECL
6559 || TREE_CODE (*node) == LABEL_DECL)
6561 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6563 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6564 name, "hot");
6565 *no_add_attrs = true;
6567 /* Most of the rest of the cold processing is done later with
6568 lookup_attribute. */
6570 else
6572 warning (OPT_Wattributes, "%qE attribute ignored", name);
6573 *no_add_attrs = true;
6576 return NULL_TREE;
6579 /* Handle a "no_sanitize_address" attribute; arguments as in
6580 struct attribute_spec.handler. */
6582 static tree
6583 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6584 bool *no_add_attrs)
6586 if (TREE_CODE (*node) != FUNCTION_DECL)
6588 warning (OPT_Wattributes, "%qE attribute ignored", name);
6589 *no_add_attrs = true;
6592 return NULL_TREE;
6595 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6596 struct attribute_spec.handler. */
6598 static tree
6599 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6600 bool *no_add_attrs)
6602 if (TREE_CODE (*node) != FUNCTION_DECL)
6603 warning (OPT_Wattributes, "%qE attribute ignored", name);
6604 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6605 DECL_ATTRIBUTES (*node)
6606 = tree_cons (get_identifier ("no_sanitize_address"),
6607 NULL_TREE, DECL_ATTRIBUTES (*node));
6608 *no_add_attrs = true;
6609 return NULL_TREE;
6612 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6613 struct attribute_spec.handler. */
6615 static tree
6616 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6617 bool *no_add_attrs)
6619 if (TREE_CODE (*node) != FUNCTION_DECL)
6621 warning (OPT_Wattributes, "%qE attribute ignored", name);
6622 *no_add_attrs = true;
6625 return NULL_TREE;
6628 /* Handle a "noinline" attribute; arguments as in
6629 struct attribute_spec.handler. */
6631 static tree
6632 handle_noinline_attribute (tree *node, tree name,
6633 tree ARG_UNUSED (args),
6634 int ARG_UNUSED (flags), bool *no_add_attrs)
6636 if (TREE_CODE (*node) == FUNCTION_DECL)
6637 DECL_UNINLINABLE (*node) = 1;
6638 else
6640 warning (OPT_Wattributes, "%qE attribute ignored", name);
6641 *no_add_attrs = true;
6644 return NULL_TREE;
6647 /* Handle a "noclone" attribute; arguments as in
6648 struct attribute_spec.handler. */
6650 static tree
6651 handle_noclone_attribute (tree *node, tree name,
6652 tree ARG_UNUSED (args),
6653 int ARG_UNUSED (flags), bool *no_add_attrs)
6655 if (TREE_CODE (*node) != FUNCTION_DECL)
6657 warning (OPT_Wattributes, "%qE attribute ignored", name);
6658 *no_add_attrs = true;
6661 return NULL_TREE;
6664 /* Handle a "always_inline" attribute; arguments as in
6665 struct attribute_spec.handler. */
6667 static tree
6668 handle_always_inline_attribute (tree *node, tree name,
6669 tree ARG_UNUSED (args),
6670 int ARG_UNUSED (flags),
6671 bool *no_add_attrs)
6673 if (TREE_CODE (*node) == FUNCTION_DECL)
6675 /* Set the attribute and mark it for disregarding inline
6676 limits. */
6677 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6679 else
6681 warning (OPT_Wattributes, "%qE attribute ignored", name);
6682 *no_add_attrs = true;
6685 return NULL_TREE;
6688 /* Handle a "gnu_inline" attribute; arguments as in
6689 struct attribute_spec.handler. */
6691 static tree
6692 handle_gnu_inline_attribute (tree *node, tree name,
6693 tree ARG_UNUSED (args),
6694 int ARG_UNUSED (flags),
6695 bool *no_add_attrs)
6697 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6699 /* Do nothing else, just set the attribute. We'll get at
6700 it later with lookup_attribute. */
6702 else
6704 warning (OPT_Wattributes, "%qE attribute ignored", name);
6705 *no_add_attrs = true;
6708 return NULL_TREE;
6711 /* Handle a "leaf" attribute; arguments as in
6712 struct attribute_spec.handler. */
6714 static tree
6715 handle_leaf_attribute (tree *node, tree name,
6716 tree ARG_UNUSED (args),
6717 int ARG_UNUSED (flags), bool *no_add_attrs)
6719 if (TREE_CODE (*node) != FUNCTION_DECL)
6721 warning (OPT_Wattributes, "%qE attribute ignored", name);
6722 *no_add_attrs = true;
6724 if (!TREE_PUBLIC (*node))
6726 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6727 *no_add_attrs = true;
6730 return NULL_TREE;
6733 /* Handle an "artificial" attribute; arguments as in
6734 struct attribute_spec.handler. */
6736 static tree
6737 handle_artificial_attribute (tree *node, tree name,
6738 tree ARG_UNUSED (args),
6739 int ARG_UNUSED (flags),
6740 bool *no_add_attrs)
6742 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6744 /* Do nothing else, just set the attribute. We'll get at
6745 it later with lookup_attribute. */
6747 else
6749 warning (OPT_Wattributes, "%qE attribute ignored", name);
6750 *no_add_attrs = true;
6753 return NULL_TREE;
6756 /* Handle a "flatten" attribute; arguments as in
6757 struct attribute_spec.handler. */
6759 static tree
6760 handle_flatten_attribute (tree *node, tree name,
6761 tree args ATTRIBUTE_UNUSED,
6762 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6764 if (TREE_CODE (*node) == FUNCTION_DECL)
6765 /* Do nothing else, just set the attribute. We'll get at
6766 it later with lookup_attribute. */
6768 else
6770 warning (OPT_Wattributes, "%qE attribute ignored", name);
6771 *no_add_attrs = true;
6774 return NULL_TREE;
6777 /* Handle a "warning" or "error" attribute; arguments as in
6778 struct attribute_spec.handler. */
6780 static tree
6781 handle_error_attribute (tree *node, tree name, tree args,
6782 int ARG_UNUSED (flags), bool *no_add_attrs)
6784 if (TREE_CODE (*node) == FUNCTION_DECL
6785 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6786 /* Do nothing else, just set the attribute. We'll get at
6787 it later with lookup_attribute. */
6789 else
6791 warning (OPT_Wattributes, "%qE attribute ignored", name);
6792 *no_add_attrs = true;
6795 return NULL_TREE;
6798 /* Handle a "used" attribute; arguments as in
6799 struct attribute_spec.handler. */
6801 static tree
6802 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6803 int ARG_UNUSED (flags), bool *no_add_attrs)
6805 tree node = *pnode;
6807 if (TREE_CODE (node) == FUNCTION_DECL
6808 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6809 || (TREE_CODE (node) == TYPE_DECL))
6811 TREE_USED (node) = 1;
6812 DECL_PRESERVE_P (node) = 1;
6813 if (TREE_CODE (node) == VAR_DECL)
6814 DECL_READ_P (node) = 1;
6816 else
6818 warning (OPT_Wattributes, "%qE attribute ignored", name);
6819 *no_add_attrs = true;
6822 return NULL_TREE;
6825 /* Handle a "unused" attribute; arguments as in
6826 struct attribute_spec.handler. */
6828 static tree
6829 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6830 int flags, bool *no_add_attrs)
6832 if (DECL_P (*node))
6834 tree decl = *node;
6836 if (TREE_CODE (decl) == PARM_DECL
6837 || TREE_CODE (decl) == VAR_DECL
6838 || TREE_CODE (decl) == FUNCTION_DECL
6839 || TREE_CODE (decl) == LABEL_DECL
6840 || TREE_CODE (decl) == TYPE_DECL)
6842 TREE_USED (decl) = 1;
6843 if (TREE_CODE (decl) == VAR_DECL
6844 || TREE_CODE (decl) == PARM_DECL)
6845 DECL_READ_P (decl) = 1;
6847 else
6849 warning (OPT_Wattributes, "%qE attribute ignored", name);
6850 *no_add_attrs = true;
6853 else
6855 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6856 *node = build_variant_type_copy (*node);
6857 TREE_USED (*node) = 1;
6860 return NULL_TREE;
6863 /* Handle a "externally_visible" attribute; arguments as in
6864 struct attribute_spec.handler. */
6866 static tree
6867 handle_externally_visible_attribute (tree *pnode, tree name,
6868 tree ARG_UNUSED (args),
6869 int ARG_UNUSED (flags),
6870 bool *no_add_attrs)
6872 tree node = *pnode;
6874 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6876 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6877 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6879 warning (OPT_Wattributes,
6880 "%qE attribute have effect only on public objects", name);
6881 *no_add_attrs = true;
6884 else
6886 warning (OPT_Wattributes, "%qE attribute ignored", name);
6887 *no_add_attrs = true;
6890 return NULL_TREE;
6893 /* Handle a "const" attribute; arguments as in
6894 struct attribute_spec.handler. */
6896 static tree
6897 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6898 int ARG_UNUSED (flags), bool *no_add_attrs)
6900 tree type = TREE_TYPE (*node);
6902 /* See FIXME comment on noreturn in c_common_attribute_table. */
6903 if (TREE_CODE (*node) == FUNCTION_DECL)
6904 TREE_READONLY (*node) = 1;
6905 else if (TREE_CODE (type) == POINTER_TYPE
6906 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6907 TREE_TYPE (*node)
6908 = build_pointer_type
6909 (build_type_variant (TREE_TYPE (type), 1,
6910 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6911 else
6913 warning (OPT_Wattributes, "%qE attribute ignored", name);
6914 *no_add_attrs = true;
6917 return NULL_TREE;
6920 /* Handle a "transparent_union" attribute; arguments as in
6921 struct attribute_spec.handler. */
6923 static tree
6924 handle_transparent_union_attribute (tree *node, tree name,
6925 tree ARG_UNUSED (args), int flags,
6926 bool *no_add_attrs)
6928 tree type;
6930 *no_add_attrs = true;
6933 if (TREE_CODE (*node) == TYPE_DECL
6934 && ! (flags & ATTR_FLAG_CXX11))
6935 node = &TREE_TYPE (*node);
6936 type = *node;
6938 if (TREE_CODE (type) == UNION_TYPE)
6940 /* Make sure that the first field will work for a transparent union.
6941 If the type isn't complete yet, leave the check to the code in
6942 finish_struct. */
6943 if (TYPE_SIZE (type))
6945 tree first = first_field (type);
6946 if (first == NULL_TREE
6947 || DECL_ARTIFICIAL (first)
6948 || TYPE_MODE (type) != DECL_MODE (first))
6949 goto ignored;
6952 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6954 /* If the type isn't complete yet, setting the flag
6955 on a variant wouldn't ever be checked. */
6956 if (!TYPE_SIZE (type))
6957 goto ignored;
6959 /* build_duplicate_type doesn't work for C++. */
6960 if (c_dialect_cxx ())
6961 goto ignored;
6963 /* A type variant isn't good enough, since we don't a cast
6964 to such a type removed as a no-op. */
6965 *node = type = build_duplicate_type (type);
6968 TYPE_TRANSPARENT_AGGR (type) = 1;
6969 return NULL_TREE;
6972 ignored:
6973 warning (OPT_Wattributes, "%qE attribute ignored", name);
6974 return NULL_TREE;
6977 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6978 get the requested priority for a constructor or destructor,
6979 possibly issuing diagnostics for invalid or reserved
6980 priorities. */
6982 static priority_type
6983 get_priority (tree args, bool is_destructor)
6985 HOST_WIDE_INT pri;
6986 tree arg;
6988 if (!args)
6989 return DEFAULT_INIT_PRIORITY;
6991 if (!SUPPORTS_INIT_PRIORITY)
6993 if (is_destructor)
6994 error ("destructor priorities are not supported");
6995 else
6996 error ("constructor priorities are not supported");
6997 return DEFAULT_INIT_PRIORITY;
7000 arg = TREE_VALUE (args);
7001 arg = default_conversion (arg);
7002 if (!host_integerp (arg, /*pos=*/0)
7003 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7004 goto invalid;
7006 pri = tree_low_cst (arg, /*pos=*/0);
7007 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7008 goto invalid;
7010 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7012 if (is_destructor)
7013 warning (0,
7014 "destructor priorities from 0 to %d are reserved "
7015 "for the implementation",
7016 MAX_RESERVED_INIT_PRIORITY);
7017 else
7018 warning (0,
7019 "constructor priorities from 0 to %d are reserved "
7020 "for the implementation",
7021 MAX_RESERVED_INIT_PRIORITY);
7023 return pri;
7025 invalid:
7026 if (is_destructor)
7027 error ("destructor priorities must be integers from 0 to %d inclusive",
7028 MAX_INIT_PRIORITY);
7029 else
7030 error ("constructor priorities must be integers from 0 to %d inclusive",
7031 MAX_INIT_PRIORITY);
7032 return DEFAULT_INIT_PRIORITY;
7035 /* Handle a "constructor" attribute; arguments as in
7036 struct attribute_spec.handler. */
7038 static tree
7039 handle_constructor_attribute (tree *node, tree name, tree args,
7040 int ARG_UNUSED (flags),
7041 bool *no_add_attrs)
7043 tree decl = *node;
7044 tree type = TREE_TYPE (decl);
7046 if (TREE_CODE (decl) == FUNCTION_DECL
7047 && TREE_CODE (type) == FUNCTION_TYPE
7048 && decl_function_context (decl) == 0)
7050 priority_type priority;
7051 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7052 priority = get_priority (args, /*is_destructor=*/false);
7053 SET_DECL_INIT_PRIORITY (decl, priority);
7054 TREE_USED (decl) = 1;
7056 else
7058 warning (OPT_Wattributes, "%qE attribute ignored", name);
7059 *no_add_attrs = true;
7062 return NULL_TREE;
7065 /* Handle a "destructor" attribute; arguments as in
7066 struct attribute_spec.handler. */
7068 static tree
7069 handle_destructor_attribute (tree *node, tree name, tree args,
7070 int ARG_UNUSED (flags),
7071 bool *no_add_attrs)
7073 tree decl = *node;
7074 tree type = TREE_TYPE (decl);
7076 if (TREE_CODE (decl) == FUNCTION_DECL
7077 && TREE_CODE (type) == FUNCTION_TYPE
7078 && decl_function_context (decl) == 0)
7080 priority_type priority;
7081 DECL_STATIC_DESTRUCTOR (decl) = 1;
7082 priority = get_priority (args, /*is_destructor=*/true);
7083 SET_DECL_FINI_PRIORITY (decl, priority);
7084 TREE_USED (decl) = 1;
7086 else
7088 warning (OPT_Wattributes, "%qE attribute ignored", name);
7089 *no_add_attrs = true;
7092 return NULL_TREE;
7095 /* Nonzero if the mode is a valid vector mode for this architecture.
7096 This returns nonzero even if there is no hardware support for the
7097 vector mode, but we can emulate with narrower modes. */
7099 static int
7100 vector_mode_valid_p (enum machine_mode mode)
7102 enum mode_class mclass = GET_MODE_CLASS (mode);
7103 enum machine_mode innermode;
7105 /* Doh! What's going on? */
7106 if (mclass != MODE_VECTOR_INT
7107 && mclass != MODE_VECTOR_FLOAT
7108 && mclass != MODE_VECTOR_FRACT
7109 && mclass != MODE_VECTOR_UFRACT
7110 && mclass != MODE_VECTOR_ACCUM
7111 && mclass != MODE_VECTOR_UACCUM)
7112 return 0;
7114 /* Hardware support. Woo hoo! */
7115 if (targetm.vector_mode_supported_p (mode))
7116 return 1;
7118 innermode = GET_MODE_INNER (mode);
7120 /* We should probably return 1 if requesting V4DI and we have no DI,
7121 but we have V2DI, but this is probably very unlikely. */
7123 /* If we have support for the inner mode, we can safely emulate it.
7124 We may not have V2DI, but me can emulate with a pair of DIs. */
7125 return targetm.scalar_mode_supported_p (innermode);
7129 /* Handle a "mode" attribute; arguments as in
7130 struct attribute_spec.handler. */
7132 static tree
7133 handle_mode_attribute (tree *node, tree name, tree args,
7134 int ARG_UNUSED (flags), bool *no_add_attrs)
7136 tree type = *node;
7137 tree ident = TREE_VALUE (args);
7139 *no_add_attrs = true;
7141 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7142 warning (OPT_Wattributes, "%qE attribute ignored", name);
7143 else
7145 int j;
7146 const char *p = IDENTIFIER_POINTER (ident);
7147 int len = strlen (p);
7148 enum machine_mode mode = VOIDmode;
7149 tree typefm;
7150 bool valid_mode;
7152 if (len > 4 && p[0] == '_' && p[1] == '_'
7153 && p[len - 1] == '_' && p[len - 2] == '_')
7155 char *newp = (char *) alloca (len - 1);
7157 strcpy (newp, &p[2]);
7158 newp[len - 4] = '\0';
7159 p = newp;
7162 /* Change this type to have a type with the specified mode.
7163 First check for the special modes. */
7164 if (!strcmp (p, "byte"))
7165 mode = byte_mode;
7166 else if (!strcmp (p, "word"))
7167 mode = word_mode;
7168 else if (!strcmp (p, "pointer"))
7169 mode = ptr_mode;
7170 else if (!strcmp (p, "libgcc_cmp_return"))
7171 mode = targetm.libgcc_cmp_return_mode ();
7172 else if (!strcmp (p, "libgcc_shift_count"))
7173 mode = targetm.libgcc_shift_count_mode ();
7174 else if (!strcmp (p, "unwind_word"))
7175 mode = targetm.unwind_word_mode ();
7176 else
7177 for (j = 0; j < NUM_MACHINE_MODES; j++)
7178 if (!strcmp (p, GET_MODE_NAME (j)))
7180 mode = (enum machine_mode) j;
7181 break;
7184 if (mode == VOIDmode)
7186 error ("unknown machine mode %qE", ident);
7187 return NULL_TREE;
7190 valid_mode = false;
7191 switch (GET_MODE_CLASS (mode))
7193 case MODE_INT:
7194 case MODE_PARTIAL_INT:
7195 case MODE_FLOAT:
7196 case MODE_DECIMAL_FLOAT:
7197 case MODE_FRACT:
7198 case MODE_UFRACT:
7199 case MODE_ACCUM:
7200 case MODE_UACCUM:
7201 valid_mode = targetm.scalar_mode_supported_p (mode);
7202 break;
7204 case MODE_COMPLEX_INT:
7205 case MODE_COMPLEX_FLOAT:
7206 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7207 break;
7209 case MODE_VECTOR_INT:
7210 case MODE_VECTOR_FLOAT:
7211 case MODE_VECTOR_FRACT:
7212 case MODE_VECTOR_UFRACT:
7213 case MODE_VECTOR_ACCUM:
7214 case MODE_VECTOR_UACCUM:
7215 warning (OPT_Wattributes, "specifying vector types with "
7216 "__attribute__ ((mode)) is deprecated");
7217 warning (OPT_Wattributes,
7218 "use __attribute__ ((vector_size)) instead");
7219 valid_mode = vector_mode_valid_p (mode);
7220 break;
7222 default:
7223 break;
7225 if (!valid_mode)
7227 error ("unable to emulate %qs", p);
7228 return NULL_TREE;
7231 if (POINTER_TYPE_P (type))
7233 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7234 tree (*fn)(tree, enum machine_mode, bool);
7236 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7238 error ("invalid pointer mode %qs", p);
7239 return NULL_TREE;
7242 if (TREE_CODE (type) == POINTER_TYPE)
7243 fn = build_pointer_type_for_mode;
7244 else
7245 fn = build_reference_type_for_mode;
7246 typefm = fn (TREE_TYPE (type), mode, false);
7248 else
7250 /* For fixed-point modes, we need to test if the signness of type
7251 and the machine mode are consistent. */
7252 if (ALL_FIXED_POINT_MODE_P (mode)
7253 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7255 error ("signedness of type and machine mode %qs don%'t match", p);
7256 return NULL_TREE;
7258 /* For fixed-point modes, we need to pass saturating info. */
7259 typefm = lang_hooks.types.type_for_mode (mode,
7260 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7261 : TYPE_UNSIGNED (type));
7264 if (typefm == NULL_TREE)
7266 error ("no data type for mode %qs", p);
7267 return NULL_TREE;
7269 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7271 /* For enumeral types, copy the precision from the integer
7272 type returned above. If not an INTEGER_TYPE, we can't use
7273 this mode for this type. */
7274 if (TREE_CODE (typefm) != INTEGER_TYPE)
7276 error ("cannot use mode %qs for enumeral types", p);
7277 return NULL_TREE;
7280 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7282 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7283 typefm = type;
7285 else
7287 /* We cannot build a type variant, as there's code that assumes
7288 that TYPE_MAIN_VARIANT has the same mode. This includes the
7289 debug generators. Instead, create a subrange type. This
7290 results in all of the enumeral values being emitted only once
7291 in the original, and the subtype gets them by reference. */
7292 if (TYPE_UNSIGNED (type))
7293 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7294 else
7295 typefm = make_signed_type (TYPE_PRECISION (typefm));
7296 TREE_TYPE (typefm) = type;
7299 else if (VECTOR_MODE_P (mode)
7300 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7301 : TREE_CODE (type) != TREE_CODE (typefm))
7303 error ("mode %qs applied to inappropriate type", p);
7304 return NULL_TREE;
7307 *node = typefm;
7310 return NULL_TREE;
7313 /* Handle a "section" attribute; arguments as in
7314 struct attribute_spec.handler. */
7316 static tree
7317 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7318 int ARG_UNUSED (flags), bool *no_add_attrs)
7320 tree decl = *node;
7322 if (targetm_common.have_named_sections)
7324 user_defined_section_attribute = true;
7326 if ((TREE_CODE (decl) == FUNCTION_DECL
7327 || TREE_CODE (decl) == VAR_DECL)
7328 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7330 if (TREE_CODE (decl) == VAR_DECL
7331 && current_function_decl != NULL_TREE
7332 && !TREE_STATIC (decl))
7334 error_at (DECL_SOURCE_LOCATION (decl),
7335 "section attribute cannot be specified for "
7336 "local variables");
7337 *no_add_attrs = true;
7340 /* The decl may have already been given a section attribute
7341 from a previous declaration. Ensure they match. */
7342 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7343 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7344 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7346 error ("section of %q+D conflicts with previous declaration",
7347 *node);
7348 *no_add_attrs = true;
7350 else if (TREE_CODE (decl) == VAR_DECL
7351 && !targetm.have_tls && targetm.emutls.tmpl_section
7352 && DECL_THREAD_LOCAL_P (decl))
7354 error ("section of %q+D cannot be overridden", *node);
7355 *no_add_attrs = true;
7357 else
7358 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7360 else
7362 error ("section attribute not allowed for %q+D", *node);
7363 *no_add_attrs = true;
7366 else
7368 error_at (DECL_SOURCE_LOCATION (*node),
7369 "section attributes are not supported for this target");
7370 *no_add_attrs = true;
7373 return NULL_TREE;
7376 /* Check whether ALIGN is a valid user-specified alignment. If so,
7377 return its base-2 log; if not, output an error and return -1. If
7378 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7379 no error. */
7381 check_user_alignment (const_tree align, bool allow_zero)
7383 int i;
7385 if (TREE_CODE (align) != INTEGER_CST
7386 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7388 error ("requested alignment is not an integer constant");
7389 return -1;
7391 else if (allow_zero && integer_zerop (align))
7392 return -1;
7393 else if (tree_int_cst_sgn (align) == -1
7394 || (i = tree_log2 (align)) == -1)
7396 error ("requested alignment is not a positive power of 2");
7397 return -1;
7399 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7401 error ("requested alignment is too large");
7402 return -1;
7404 return i;
7408 If in c++-11, check if the c++-11 alignment constraint with respect
7409 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7410 c++-11 mode, does nothing.
7412 [dcl.align]2/ says:
7414 [* if the constant expression evaluates to a fundamental alignment,
7415 the alignment requirement of the declared entity shall be the
7416 specified fundamental alignment.
7418 * if the constant expression evaluates to an extended alignment
7419 and the implementation supports that alignment in the context
7420 of the declaration, the alignment of the declared entity shall
7421 be that alignment
7423 * if the constant expression evaluates to an extended alignment
7424 and the implementation does not support that alignment in the
7425 context of the declaration, the program is ill-formed]. */
7427 static bool
7428 check_cxx_fundamental_alignment_constraints (tree node,
7429 unsigned align_log,
7430 int flags)
7432 bool alignment_too_large_p = false;
7433 unsigned requested_alignment = 1U << align_log;
7434 unsigned max_align = 0;
7436 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7437 || (node == NULL_TREE || node == error_mark_node))
7438 return true;
7440 if (cxx_fundamental_alignment_p (requested_alignment))
7441 return true;
7443 if (DECL_P (node))
7445 if (TREE_STATIC (node))
7447 /* For file scope variables and static members, the target
7448 supports alignments that are at most
7449 MAX_OFILE_ALIGNMENT. */
7450 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7451 alignment_too_large_p = true;
7453 else
7455 #ifdef BIGGEST_FIELD_ALIGNMENT
7456 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7457 #else
7458 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7459 #endif
7460 /* For non-static members, the target supports either
7461 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7462 if it is defined or BIGGEST_ALIGNMENT. */
7463 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7464 if (TREE_CODE (node) == FIELD_DECL
7465 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7466 alignment_too_large_p = true;
7467 #undef MAX_TARGET_FIELD_ALIGNMENT
7468 /* For stack variables, the target supports at most
7469 MAX_STACK_ALIGNMENT. */
7470 else if (decl_function_context (node) != NULL
7471 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7472 alignment_too_large_p = true;
7475 else if (TYPE_P (node))
7477 /* Let's be liberal for types. */
7478 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7479 alignment_too_large_p = true;
7482 if (alignment_too_large_p)
7483 pedwarn (input_location, OPT_Wattributes,
7484 "requested alignment %d is larger than %d",
7485 requested_alignment, max_align);
7487 return !alignment_too_large_p;
7490 /* Handle a "aligned" attribute; arguments as in
7491 struct attribute_spec.handler. */
7493 static tree
7494 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7495 int flags, bool *no_add_attrs)
7497 tree decl = NULL_TREE;
7498 tree *type = NULL;
7499 int is_type = 0;
7500 tree align_expr = (args ? TREE_VALUE (args)
7501 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7502 int i;
7504 if (DECL_P (*node))
7506 decl = *node;
7507 type = &TREE_TYPE (decl);
7508 is_type = TREE_CODE (*node) == TYPE_DECL;
7510 else if (TYPE_P (*node))
7511 type = node, is_type = 1;
7513 if ((i = check_user_alignment (align_expr, false)) == -1
7514 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7515 *no_add_attrs = true;
7516 else if (is_type)
7518 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7519 /* OK, modify the type in place. */;
7520 /* If we have a TYPE_DECL, then copy the type, so that we
7521 don't accidentally modify a builtin type. See pushdecl. */
7522 else if (decl && TREE_TYPE (decl) != error_mark_node
7523 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7525 tree tt = TREE_TYPE (decl);
7526 *type = build_variant_type_copy (*type);
7527 DECL_ORIGINAL_TYPE (decl) = tt;
7528 TYPE_NAME (*type) = decl;
7529 TREE_USED (*type) = TREE_USED (decl);
7530 TREE_TYPE (decl) = *type;
7532 else
7533 *type = build_variant_type_copy (*type);
7535 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7536 TYPE_USER_ALIGN (*type) = 1;
7538 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7539 && TREE_CODE (decl) != FIELD_DECL)
7541 error ("alignment may not be specified for %q+D", decl);
7542 *no_add_attrs = true;
7544 else if (DECL_USER_ALIGN (decl)
7545 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7546 /* C++-11 [dcl.align/4]:
7548 When multiple alignment-specifiers are specified for an
7549 entity, the alignment requirement shall be set to the
7550 strictest specified alignment.
7552 This formally comes from the c++11 specification but we are
7553 doing it for the GNU attribute syntax as well. */
7554 *no_add_attrs = true;
7555 else if (TREE_CODE (decl) == FUNCTION_DECL
7556 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7558 if (DECL_USER_ALIGN (decl))
7559 error ("alignment for %q+D was previously specified as %d "
7560 "and may not be decreased", decl,
7561 DECL_ALIGN (decl) / BITS_PER_UNIT);
7562 else
7563 error ("alignment for %q+D must be at least %d", decl,
7564 DECL_ALIGN (decl) / BITS_PER_UNIT);
7565 *no_add_attrs = true;
7567 else
7569 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7570 DECL_USER_ALIGN (decl) = 1;
7573 return NULL_TREE;
7576 /* Handle a "weak" attribute; arguments as in
7577 struct attribute_spec.handler. */
7579 static tree
7580 handle_weak_attribute (tree *node, tree name,
7581 tree ARG_UNUSED (args),
7582 int ARG_UNUSED (flags),
7583 bool * ARG_UNUSED (no_add_attrs))
7585 if (TREE_CODE (*node) == FUNCTION_DECL
7586 && DECL_DECLARED_INLINE_P (*node))
7588 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7589 *no_add_attrs = true;
7591 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7593 error ("indirect function %q+D cannot be declared weak", *node);
7594 *no_add_attrs = true;
7595 return NULL_TREE;
7597 else if (TREE_CODE (*node) == FUNCTION_DECL
7598 || TREE_CODE (*node) == VAR_DECL)
7599 declare_weak (*node);
7600 else
7601 warning (OPT_Wattributes, "%qE attribute ignored", name);
7603 return NULL_TREE;
7606 /* Handle an "alias" or "ifunc" attribute; arguments as in
7607 struct attribute_spec.handler, except that IS_ALIAS tells us
7608 whether this is an alias as opposed to ifunc attribute. */
7610 static tree
7611 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7612 bool *no_add_attrs)
7614 tree decl = *node;
7616 if (TREE_CODE (decl) != FUNCTION_DECL
7617 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7619 warning (OPT_Wattributes, "%qE attribute ignored", name);
7620 *no_add_attrs = true;
7622 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7623 || (TREE_CODE (decl) != FUNCTION_DECL
7624 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7625 /* A static variable declaration is always a tentative definition,
7626 but the alias is a non-tentative definition which overrides. */
7627 || (TREE_CODE (decl) != FUNCTION_DECL
7628 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7630 error ("%q+D defined both normally and as %qE attribute", decl, name);
7631 *no_add_attrs = true;
7632 return NULL_TREE;
7634 else if (!is_alias
7635 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7636 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7638 error ("weak %q+D cannot be defined %qE", decl, name);
7639 *no_add_attrs = true;
7640 return NULL_TREE;
7643 /* Note that the very first time we process a nested declaration,
7644 decl_function_context will not be set. Indeed, *would* never
7645 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7646 we do below. After such frobbery, pushdecl would set the context.
7647 In any case, this is never what we want. */
7648 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7650 tree id;
7652 id = TREE_VALUE (args);
7653 if (TREE_CODE (id) != STRING_CST)
7655 error ("attribute %qE argument not a string", name);
7656 *no_add_attrs = true;
7657 return NULL_TREE;
7659 id = get_identifier (TREE_STRING_POINTER (id));
7660 /* This counts as a use of the object pointed to. */
7661 TREE_USED (id) = 1;
7663 if (TREE_CODE (decl) == FUNCTION_DECL)
7664 DECL_INITIAL (decl) = error_mark_node;
7665 else
7666 TREE_STATIC (decl) = 1;
7668 if (!is_alias)
7669 /* ifuncs are also aliases, so set that attribute too. */
7670 DECL_ATTRIBUTES (decl)
7671 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7673 else
7675 warning (OPT_Wattributes, "%qE attribute ignored", name);
7676 *no_add_attrs = true;
7679 return NULL_TREE;
7682 /* Handle an "alias" or "ifunc" attribute; arguments as in
7683 struct attribute_spec.handler. */
7685 static tree
7686 handle_ifunc_attribute (tree *node, tree name, tree args,
7687 int ARG_UNUSED (flags), bool *no_add_attrs)
7689 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7692 /* Handle an "alias" or "ifunc" attribute; arguments as in
7693 struct attribute_spec.handler. */
7695 static tree
7696 handle_alias_attribute (tree *node, tree name, tree args,
7697 int ARG_UNUSED (flags), bool *no_add_attrs)
7699 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7702 /* Handle a "weakref" attribute; arguments as in struct
7703 attribute_spec.handler. */
7705 static tree
7706 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7707 int flags, bool *no_add_attrs)
7709 tree attr = NULL_TREE;
7711 /* We must ignore the attribute when it is associated with
7712 local-scoped decls, since attribute alias is ignored and many
7713 such symbols do not even have a DECL_WEAK field. */
7714 if (decl_function_context (*node)
7715 || current_function_decl
7716 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7718 warning (OPT_Wattributes, "%qE attribute ignored", name);
7719 *no_add_attrs = true;
7720 return NULL_TREE;
7723 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7725 error ("indirect function %q+D cannot be declared weakref", *node);
7726 *no_add_attrs = true;
7727 return NULL_TREE;
7730 /* The idea here is that `weakref("name")' mutates into `weakref,
7731 alias("name")', and weakref without arguments, in turn,
7732 implicitly adds weak. */
7734 if (args)
7736 attr = tree_cons (get_identifier ("alias"), args, attr);
7737 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7739 *no_add_attrs = true;
7741 decl_attributes (node, attr, flags);
7743 else
7745 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7746 error_at (DECL_SOURCE_LOCATION (*node),
7747 "weakref attribute must appear before alias attribute");
7749 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7750 and that isn't supported; and because it wants to add it to
7751 the list of weak decls, which isn't helpful. */
7752 DECL_WEAK (*node) = 1;
7755 return NULL_TREE;
7758 /* Handle an "visibility" attribute; arguments as in
7759 struct attribute_spec.handler. */
7761 static tree
7762 handle_visibility_attribute (tree *node, tree name, tree args,
7763 int ARG_UNUSED (flags),
7764 bool *ARG_UNUSED (no_add_attrs))
7766 tree decl = *node;
7767 tree id = TREE_VALUE (args);
7768 enum symbol_visibility vis;
7770 if (TYPE_P (*node))
7772 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7773 /* OK */;
7774 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7776 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7777 name);
7778 return NULL_TREE;
7780 else if (TYPE_FIELDS (*node))
7782 error ("%qE attribute ignored because %qT is already defined",
7783 name, *node);
7784 return NULL_TREE;
7787 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7789 warning (OPT_Wattributes, "%qE attribute ignored", name);
7790 return NULL_TREE;
7793 if (TREE_CODE (id) != STRING_CST)
7795 error ("visibility argument not a string");
7796 return NULL_TREE;
7799 /* If this is a type, set the visibility on the type decl. */
7800 if (TYPE_P (decl))
7802 decl = TYPE_NAME (decl);
7803 if (!decl)
7804 return NULL_TREE;
7805 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7807 warning (OPT_Wattributes, "%qE attribute ignored on types",
7808 name);
7809 return NULL_TREE;
7813 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7814 vis = VISIBILITY_DEFAULT;
7815 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7816 vis = VISIBILITY_INTERNAL;
7817 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7818 vis = VISIBILITY_HIDDEN;
7819 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7820 vis = VISIBILITY_PROTECTED;
7821 else
7823 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7824 vis = VISIBILITY_DEFAULT;
7827 if (DECL_VISIBILITY_SPECIFIED (decl)
7828 && vis != DECL_VISIBILITY (decl))
7830 tree attributes = (TYPE_P (*node)
7831 ? TYPE_ATTRIBUTES (*node)
7832 : DECL_ATTRIBUTES (decl));
7833 if (lookup_attribute ("visibility", attributes))
7834 error ("%qD redeclared with different visibility", decl);
7835 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7836 && lookup_attribute ("dllimport", attributes))
7837 error ("%qD was declared %qs which implies default visibility",
7838 decl, "dllimport");
7839 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7840 && lookup_attribute ("dllexport", attributes))
7841 error ("%qD was declared %qs which implies default visibility",
7842 decl, "dllexport");
7845 DECL_VISIBILITY (decl) = vis;
7846 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7848 /* Go ahead and attach the attribute to the node as well. This is needed
7849 so we can determine whether we have VISIBILITY_DEFAULT because the
7850 visibility was not specified, or because it was explicitly overridden
7851 from the containing scope. */
7853 return NULL_TREE;
7856 /* Determine the ELF symbol visibility for DECL, which is either a
7857 variable or a function. It is an error to use this function if a
7858 definition of DECL is not available in this translation unit.
7859 Returns true if the final visibility has been determined by this
7860 function; false if the caller is free to make additional
7861 modifications. */
7863 bool
7864 c_determine_visibility (tree decl)
7866 gcc_assert (TREE_CODE (decl) == VAR_DECL
7867 || TREE_CODE (decl) == FUNCTION_DECL);
7869 /* If the user explicitly specified the visibility with an
7870 attribute, honor that. DECL_VISIBILITY will have been set during
7871 the processing of the attribute. We check for an explicit
7872 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7873 to distinguish the use of an attribute from the use of a "#pragma
7874 GCC visibility push(...)"; in the latter case we still want other
7875 considerations to be able to overrule the #pragma. */
7876 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7877 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7878 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7879 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7880 return true;
7882 /* Set default visibility to whatever the user supplied with
7883 visibility_specified depending on #pragma GCC visibility. */
7884 if (!DECL_VISIBILITY_SPECIFIED (decl))
7886 if (visibility_options.inpragma
7887 || DECL_VISIBILITY (decl) != default_visibility)
7889 DECL_VISIBILITY (decl) = default_visibility;
7890 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7891 /* If visibility changed and DECL already has DECL_RTL, ensure
7892 symbol flags are updated. */
7893 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7894 || TREE_CODE (decl) == FUNCTION_DECL)
7895 && DECL_RTL_SET_P (decl))
7896 make_decl_rtl (decl);
7899 return false;
7902 /* Handle an "tls_model" attribute; arguments as in
7903 struct attribute_spec.handler. */
7905 static tree
7906 handle_tls_model_attribute (tree *node, tree name, tree args,
7907 int ARG_UNUSED (flags), bool *no_add_attrs)
7909 tree id;
7910 tree decl = *node;
7911 enum tls_model kind;
7913 *no_add_attrs = true;
7915 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7917 warning (OPT_Wattributes, "%qE attribute ignored", name);
7918 return NULL_TREE;
7921 kind = DECL_TLS_MODEL (decl);
7922 id = TREE_VALUE (args);
7923 if (TREE_CODE (id) != STRING_CST)
7925 error ("tls_model argument not a string");
7926 return NULL_TREE;
7929 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7930 kind = TLS_MODEL_LOCAL_EXEC;
7931 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7932 kind = TLS_MODEL_INITIAL_EXEC;
7933 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7934 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7935 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7936 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7937 else
7938 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7940 DECL_TLS_MODEL (decl) = kind;
7941 return NULL_TREE;
7944 /* Handle a "no_instrument_function" attribute; arguments as in
7945 struct attribute_spec.handler. */
7947 static tree
7948 handle_no_instrument_function_attribute (tree *node, tree name,
7949 tree ARG_UNUSED (args),
7950 int ARG_UNUSED (flags),
7951 bool *no_add_attrs)
7953 tree decl = *node;
7955 if (TREE_CODE (decl) != FUNCTION_DECL)
7957 error_at (DECL_SOURCE_LOCATION (decl),
7958 "%qE attribute applies only to functions", name);
7959 *no_add_attrs = true;
7961 else if (DECL_INITIAL (decl))
7963 error_at (DECL_SOURCE_LOCATION (decl),
7964 "can%'t set %qE attribute after definition", name);
7965 *no_add_attrs = true;
7967 else
7968 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7970 return NULL_TREE;
7973 /* Handle a "malloc" attribute; arguments as in
7974 struct attribute_spec.handler. */
7976 static tree
7977 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7978 int ARG_UNUSED (flags), bool *no_add_attrs)
7980 if (TREE_CODE (*node) == FUNCTION_DECL
7981 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7982 DECL_IS_MALLOC (*node) = 1;
7983 else
7985 warning (OPT_Wattributes, "%qE attribute ignored", name);
7986 *no_add_attrs = true;
7989 return NULL_TREE;
7992 /* Handle a "alloc_size" attribute; arguments as in
7993 struct attribute_spec.handler. */
7995 static tree
7996 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7997 int ARG_UNUSED (flags), bool *no_add_attrs)
7999 unsigned arg_count = type_num_arguments (*node);
8000 for (; args; args = TREE_CHAIN (args))
8002 tree position = TREE_VALUE (args);
8004 if (TREE_CODE (position) != INTEGER_CST
8005 || TREE_INT_CST_HIGH (position)
8006 || TREE_INT_CST_LOW (position) < 1
8007 || TREE_INT_CST_LOW (position) > arg_count )
8009 warning (OPT_Wattributes,
8010 "alloc_size parameter outside range");
8011 *no_add_attrs = true;
8012 return NULL_TREE;
8015 return NULL_TREE;
8018 /* Handle a "fn spec" attribute; arguments as in
8019 struct attribute_spec.handler. */
8021 static tree
8022 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8023 tree args, int ARG_UNUSED (flags),
8024 bool *no_add_attrs ATTRIBUTE_UNUSED)
8026 gcc_assert (args
8027 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8028 && !TREE_CHAIN (args));
8029 return NULL_TREE;
8032 /* Handle a "bnd_variable_size" attribute; arguments as in
8033 struct attribute_spec.handler. */
8035 static tree
8036 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8037 int ARG_UNUSED (flags), bool *no_add_attrs)
8039 if (TREE_CODE (*node) != FIELD_DECL)
8041 warning (OPT_Wattributes, "%qE attribute ignored", name);
8042 *no_add_attrs = true;
8045 return NULL_TREE;
8048 /* Handle a "bnd_legacy" attribute; arguments as in
8049 struct attribute_spec.handler. */
8051 static tree
8052 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8053 int ARG_UNUSED (flags), bool *no_add_attrs)
8055 if (TREE_CODE (*node) != FUNCTION_DECL)
8057 warning (OPT_Wattributes, "%qE attribute ignored", name);
8058 *no_add_attrs = true;
8061 return NULL_TREE;
8064 /* Handle a "warn_unused" attribute; arguments as in
8065 struct attribute_spec.handler. */
8067 static tree
8068 handle_warn_unused_attribute (tree *node, tree name,
8069 tree args ATTRIBUTE_UNUSED,
8070 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8072 if (TYPE_P (*node))
8073 /* Do nothing else, just set the attribute. We'll get at
8074 it later with lookup_attribute. */
8076 else
8078 warning (OPT_Wattributes, "%qE attribute ignored", name);
8079 *no_add_attrs = true;
8082 return NULL_TREE;
8085 /* Handle an "omp declare simd" attribute; arguments as in
8086 struct attribute_spec.handler. */
8088 static tree
8089 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8091 return NULL_TREE;
8094 /* Handle an "omp declare target" attribute; arguments as in
8095 struct attribute_spec.handler. */
8097 static tree
8098 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8100 return NULL_TREE;
8103 /* Handle a "returns_twice" attribute; arguments as in
8104 struct attribute_spec.handler. */
8106 static tree
8107 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8108 int ARG_UNUSED (flags), bool *no_add_attrs)
8110 if (TREE_CODE (*node) == FUNCTION_DECL)
8111 DECL_IS_RETURNS_TWICE (*node) = 1;
8112 else
8114 warning (OPT_Wattributes, "%qE attribute ignored", name);
8115 *no_add_attrs = true;
8118 return NULL_TREE;
8121 /* Handle a "no_limit_stack" attribute; arguments as in
8122 struct attribute_spec.handler. */
8124 static tree
8125 handle_no_limit_stack_attribute (tree *node, tree name,
8126 tree ARG_UNUSED (args),
8127 int ARG_UNUSED (flags),
8128 bool *no_add_attrs)
8130 tree decl = *node;
8132 if (TREE_CODE (decl) != FUNCTION_DECL)
8134 error_at (DECL_SOURCE_LOCATION (decl),
8135 "%qE attribute applies only to functions", name);
8136 *no_add_attrs = true;
8138 else if (DECL_INITIAL (decl))
8140 error_at (DECL_SOURCE_LOCATION (decl),
8141 "can%'t set %qE attribute after definition", name);
8142 *no_add_attrs = true;
8144 else
8145 DECL_NO_LIMIT_STACK (decl) = 1;
8147 return NULL_TREE;
8150 /* Handle a "pure" attribute; arguments as in
8151 struct attribute_spec.handler. */
8153 static tree
8154 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8155 int ARG_UNUSED (flags), bool *no_add_attrs)
8157 if (TREE_CODE (*node) == FUNCTION_DECL)
8158 DECL_PURE_P (*node) = 1;
8159 /* ??? TODO: Support types. */
8160 else
8162 warning (OPT_Wattributes, "%qE attribute ignored", name);
8163 *no_add_attrs = true;
8166 return NULL_TREE;
8169 /* Digest an attribute list destined for a transactional memory statement.
8170 ALLOWED is the set of attributes that are allowed for this statement;
8171 return the attribute we parsed. Multiple attributes are never allowed. */
8174 parse_tm_stmt_attr (tree attrs, int allowed)
8176 tree a_seen = NULL;
8177 int m_seen = 0;
8179 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8181 tree a = TREE_PURPOSE (attrs);
8182 int m = 0;
8184 if (is_attribute_p ("outer", a))
8185 m = TM_STMT_ATTR_OUTER;
8187 if ((m & allowed) == 0)
8189 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8190 continue;
8193 if (m_seen == 0)
8195 a_seen = a;
8196 m_seen = m;
8198 else if (m_seen == m)
8199 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8200 else
8201 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8204 return m_seen;
8207 /* Transform a TM attribute name into a maskable integer and back.
8208 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8209 to how the lack of an attribute is treated. */
8212 tm_attr_to_mask (tree attr)
8214 if (attr == NULL)
8215 return 0;
8216 if (is_attribute_p ("transaction_safe", attr))
8217 return TM_ATTR_SAFE;
8218 if (is_attribute_p ("transaction_callable", attr))
8219 return TM_ATTR_CALLABLE;
8220 if (is_attribute_p ("transaction_pure", attr))
8221 return TM_ATTR_PURE;
8222 if (is_attribute_p ("transaction_unsafe", attr))
8223 return TM_ATTR_IRREVOCABLE;
8224 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8225 return TM_ATTR_MAY_CANCEL_OUTER;
8226 return 0;
8229 tree
8230 tm_mask_to_attr (int mask)
8232 const char *str;
8233 switch (mask)
8235 case TM_ATTR_SAFE:
8236 str = "transaction_safe";
8237 break;
8238 case TM_ATTR_CALLABLE:
8239 str = "transaction_callable";
8240 break;
8241 case TM_ATTR_PURE:
8242 str = "transaction_pure";
8243 break;
8244 case TM_ATTR_IRREVOCABLE:
8245 str = "transaction_unsafe";
8246 break;
8247 case TM_ATTR_MAY_CANCEL_OUTER:
8248 str = "transaction_may_cancel_outer";
8249 break;
8250 default:
8251 gcc_unreachable ();
8253 return get_identifier (str);
8256 /* Return the first TM attribute seen in LIST. */
8258 tree
8259 find_tm_attribute (tree list)
8261 for (; list ; list = TREE_CHAIN (list))
8263 tree name = TREE_PURPOSE (list);
8264 if (tm_attr_to_mask (name) != 0)
8265 return name;
8267 return NULL_TREE;
8270 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8271 Here we accept only function types, and verify that none of the other
8272 function TM attributes are also applied. */
8273 /* ??? We need to accept class types for C++, but not C. This greatly
8274 complicates this function, since we can no longer rely on the extra
8275 processing given by function_type_required. */
8277 static tree
8278 handle_tm_attribute (tree *node, tree name, tree args,
8279 int flags, bool *no_add_attrs)
8281 /* Only one path adds the attribute; others don't. */
8282 *no_add_attrs = true;
8284 switch (TREE_CODE (*node))
8286 case RECORD_TYPE:
8287 case UNION_TYPE:
8288 /* Only tm_callable and tm_safe apply to classes. */
8289 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8290 goto ignored;
8291 /* FALLTHRU */
8293 case FUNCTION_TYPE:
8294 case METHOD_TYPE:
8296 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8297 if (old_name == name)
8299 else if (old_name != NULL_TREE)
8300 error ("type was previously declared %qE", old_name);
8301 else
8302 *no_add_attrs = false;
8304 break;
8306 case POINTER_TYPE:
8308 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8309 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8311 tree fn_tmp = TREE_TYPE (*node);
8312 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8313 *node = build_pointer_type (fn_tmp);
8314 break;
8317 /* FALLTHRU */
8319 default:
8320 /* If a function is next, pass it on to be tried next. */
8321 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8322 return tree_cons (name, args, NULL);
8324 ignored:
8325 warning (OPT_Wattributes, "%qE attribute ignored", name);
8326 break;
8329 return NULL_TREE;
8332 /* Handle the TM_WRAP attribute; arguments as in
8333 struct attribute_spec.handler. */
8335 static tree
8336 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8337 int ARG_UNUSED (flags), bool *no_add_attrs)
8339 tree decl = *node;
8341 /* We don't need the attribute even on success, since we
8342 record the entry in an external table. */
8343 *no_add_attrs = true;
8345 if (TREE_CODE (decl) != FUNCTION_DECL)
8346 warning (OPT_Wattributes, "%qE attribute ignored", name);
8347 else
8349 tree wrap_decl = TREE_VALUE (args);
8350 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8351 && TREE_CODE (wrap_decl) != VAR_DECL
8352 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8353 error ("%qE argument not an identifier", name);
8354 else
8356 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8357 wrap_decl = lookup_name (wrap_decl);
8358 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8360 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8361 TREE_TYPE (wrap_decl)))
8362 record_tm_replacement (wrap_decl, decl);
8363 else
8364 error ("%qD is not compatible with %qD", wrap_decl, decl);
8366 else
8367 error ("transaction_wrap argument is not a function");
8371 return NULL_TREE;
8374 /* Ignore the given attribute. Used when this attribute may be usefully
8375 overridden by the target, but is not used generically. */
8377 static tree
8378 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8379 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8380 bool *no_add_attrs)
8382 *no_add_attrs = true;
8383 return NULL_TREE;
8386 /* Handle a "no vops" attribute; arguments as in
8387 struct attribute_spec.handler. */
8389 static tree
8390 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8391 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8392 bool *ARG_UNUSED (no_add_attrs))
8394 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8395 DECL_IS_NOVOPS (*node) = 1;
8396 return NULL_TREE;
8399 /* Handle a "deprecated" attribute; arguments as in
8400 struct attribute_spec.handler. */
8402 static tree
8403 handle_deprecated_attribute (tree *node, tree name,
8404 tree args, int flags,
8405 bool *no_add_attrs)
8407 tree type = NULL_TREE;
8408 int warn = 0;
8409 tree what = NULL_TREE;
8411 if (!args)
8412 *no_add_attrs = true;
8413 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8415 error ("deprecated message is not a string");
8416 *no_add_attrs = true;
8419 if (DECL_P (*node))
8421 tree decl = *node;
8422 type = TREE_TYPE (decl);
8424 if (TREE_CODE (decl) == TYPE_DECL
8425 || TREE_CODE (decl) == PARM_DECL
8426 || TREE_CODE (decl) == VAR_DECL
8427 || TREE_CODE (decl) == FUNCTION_DECL
8428 || TREE_CODE (decl) == FIELD_DECL
8429 || objc_method_decl (TREE_CODE (decl)))
8430 TREE_DEPRECATED (decl) = 1;
8431 else
8432 warn = 1;
8434 else if (TYPE_P (*node))
8436 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8437 *node = build_variant_type_copy (*node);
8438 TREE_DEPRECATED (*node) = 1;
8439 type = *node;
8441 else
8442 warn = 1;
8444 if (warn)
8446 *no_add_attrs = true;
8447 if (type && TYPE_NAME (type))
8449 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8450 what = TYPE_NAME (*node);
8451 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8452 && DECL_NAME (TYPE_NAME (type)))
8453 what = DECL_NAME (TYPE_NAME (type));
8455 if (what)
8456 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8457 else
8458 warning (OPT_Wattributes, "%qE attribute ignored", name);
8461 return NULL_TREE;
8464 /* Handle a "vector_size" attribute; arguments as in
8465 struct attribute_spec.handler. */
8467 static tree
8468 handle_vector_size_attribute (tree *node, tree name, tree args,
8469 int ARG_UNUSED (flags),
8470 bool *no_add_attrs)
8472 unsigned HOST_WIDE_INT vecsize, nunits;
8473 enum machine_mode orig_mode;
8474 tree type = *node, new_type, size;
8476 *no_add_attrs = true;
8478 size = TREE_VALUE (args);
8480 if (!host_integerp (size, 1))
8482 warning (OPT_Wattributes, "%qE attribute ignored", name);
8483 return NULL_TREE;
8486 /* Get the vector size (in bytes). */
8487 vecsize = tree_low_cst (size, 1);
8489 /* We need to provide for vector pointers, vector arrays, and
8490 functions returning vectors. For example:
8492 __attribute__((vector_size(16))) short *foo;
8494 In this case, the mode is SI, but the type being modified is
8495 HI, so we need to look further. */
8497 while (POINTER_TYPE_P (type)
8498 || TREE_CODE (type) == FUNCTION_TYPE
8499 || TREE_CODE (type) == METHOD_TYPE
8500 || TREE_CODE (type) == ARRAY_TYPE
8501 || TREE_CODE (type) == OFFSET_TYPE)
8502 type = TREE_TYPE (type);
8504 /* Get the mode of the type being modified. */
8505 orig_mode = TYPE_MODE (type);
8507 if ((!INTEGRAL_TYPE_P (type)
8508 && !SCALAR_FLOAT_TYPE_P (type)
8509 && !FIXED_POINT_TYPE_P (type))
8510 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8511 && GET_MODE_CLASS (orig_mode) != MODE_INT
8512 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8513 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8514 || TREE_CODE (type) == BOOLEAN_TYPE)
8516 error ("invalid vector type for attribute %qE", name);
8517 return NULL_TREE;
8520 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8522 error ("vector size not an integral multiple of component size");
8523 return NULL;
8526 if (vecsize == 0)
8528 error ("zero vector size");
8529 return NULL;
8532 /* Calculate how many units fit in the vector. */
8533 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8534 if (nunits & (nunits - 1))
8536 error ("number of components of the vector not a power of two");
8537 return NULL_TREE;
8540 new_type = build_vector_type (type, nunits);
8542 /* Build back pointers if needed. */
8543 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8545 return NULL_TREE;
8548 /* Handle the "nonnull" attribute. */
8549 static tree
8550 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8551 tree args, int ARG_UNUSED (flags),
8552 bool *no_add_attrs)
8554 tree type = *node;
8555 unsigned HOST_WIDE_INT attr_arg_num;
8557 /* If no arguments are specified, all pointer arguments should be
8558 non-null. Verify a full prototype is given so that the arguments
8559 will have the correct types when we actually check them later. */
8560 if (!args)
8562 if (!prototype_p (type))
8564 error ("nonnull attribute without arguments on a non-prototype");
8565 *no_add_attrs = true;
8567 return NULL_TREE;
8570 /* Argument list specified. Verify that each argument number references
8571 a pointer argument. */
8572 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8574 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8576 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8578 error ("nonnull argument has invalid operand number (argument %lu)",
8579 (unsigned long) attr_arg_num);
8580 *no_add_attrs = true;
8581 return NULL_TREE;
8584 if (prototype_p (type))
8586 function_args_iterator iter;
8587 tree argument;
8589 function_args_iter_init (&iter, type);
8590 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8592 argument = function_args_iter_cond (&iter);
8593 if (argument == NULL_TREE || ck_num == arg_num)
8594 break;
8597 if (!argument
8598 || TREE_CODE (argument) == VOID_TYPE)
8600 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8601 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8602 *no_add_attrs = true;
8603 return NULL_TREE;
8606 if (TREE_CODE (argument) != POINTER_TYPE)
8608 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8609 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8610 *no_add_attrs = true;
8611 return NULL_TREE;
8616 return NULL_TREE;
8619 /* Check the argument list of a function call for null in argument slots
8620 that are marked as requiring a non-null pointer argument. The NARGS
8621 arguments are passed in the array ARGARRAY.
8624 static void
8625 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8627 tree a;
8628 int i;
8630 attrs = lookup_attribute ("nonnull", attrs);
8631 if (attrs == NULL_TREE)
8632 return;
8634 a = attrs;
8635 /* See if any of the nonnull attributes has no arguments. If so,
8636 then every pointer argument is checked (in which case the check
8637 for pointer type is done in check_nonnull_arg). */
8638 if (TREE_VALUE (a) != NULL_TREE)
8640 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8641 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8643 if (a != NULL_TREE)
8644 for (i = 0; i < nargs; i++)
8645 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8646 i + 1);
8647 else
8649 /* Walk the argument list. If we encounter an argument number we
8650 should check for non-null, do it. */
8651 for (i = 0; i < nargs; i++)
8653 for (a = attrs; ; a = TREE_CHAIN (a))
8655 a = lookup_attribute ("nonnull", a);
8656 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8657 break;
8660 if (a != NULL_TREE)
8661 check_function_arguments_recurse (check_nonnull_arg, NULL,
8662 argarray[i], i + 1);
8667 /* Check that the Nth argument of a function call (counting backwards
8668 from the end) is a (pointer)0. The NARGS arguments are passed in the
8669 array ARGARRAY. */
8671 static void
8672 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8674 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8676 if (attr)
8678 int len = 0;
8679 int pos = 0;
8680 tree sentinel;
8681 function_args_iterator iter;
8682 tree t;
8684 /* Skip over the named arguments. */
8685 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8687 if (len == nargs)
8688 break;
8689 len++;
8692 if (TREE_VALUE (attr))
8694 tree p = TREE_VALUE (TREE_VALUE (attr));
8695 pos = TREE_INT_CST_LOW (p);
8698 /* The sentinel must be one of the varargs, i.e.
8699 in position >= the number of fixed arguments. */
8700 if ((nargs - 1 - pos) < len)
8702 warning (OPT_Wformat_,
8703 "not enough variable arguments to fit a sentinel");
8704 return;
8707 /* Validate the sentinel. */
8708 sentinel = argarray[nargs - 1 - pos];
8709 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8710 || !integer_zerop (sentinel))
8711 /* Although __null (in C++) is only an integer we allow it
8712 nevertheless, as we are guaranteed that it's exactly
8713 as wide as a pointer, and we don't want to force
8714 users to cast the NULL they have written there.
8715 We warn with -Wstrict-null-sentinel, though. */
8716 && (warn_strict_null_sentinel || null_node != sentinel))
8717 warning (OPT_Wformat_, "missing sentinel in function call");
8721 /* Helper for check_function_nonnull; given a list of operands which
8722 must be non-null in ARGS, determine if operand PARAM_NUM should be
8723 checked. */
8725 static bool
8726 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8728 unsigned HOST_WIDE_INT arg_num = 0;
8730 for (; args; args = TREE_CHAIN (args))
8732 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8734 gcc_assert (found);
8736 if (arg_num == param_num)
8737 return true;
8739 return false;
8742 /* Check that the function argument PARAM (which is operand number
8743 PARAM_NUM) is non-null. This is called by check_function_nonnull
8744 via check_function_arguments_recurse. */
8746 static void
8747 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8748 unsigned HOST_WIDE_INT param_num)
8750 /* Just skip checking the argument if it's not a pointer. This can
8751 happen if the "nonnull" attribute was given without an operand
8752 list (which means to check every pointer argument). */
8754 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8755 return;
8757 if (integer_zerop (param))
8758 warning (OPT_Wnonnull, "null argument where non-null required "
8759 "(argument %lu)", (unsigned long) param_num);
8762 /* Helper for nonnull attribute handling; fetch the operand number
8763 from the attribute argument list. */
8765 static bool
8766 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8768 /* Verify the arg number is a constant. */
8769 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8770 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8771 return false;
8773 *valp = TREE_INT_CST_LOW (arg_num_expr);
8774 return true;
8777 /* Handle a "nothrow" attribute; arguments as in
8778 struct attribute_spec.handler. */
8780 static tree
8781 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8782 int ARG_UNUSED (flags), bool *no_add_attrs)
8784 if (TREE_CODE (*node) == FUNCTION_DECL)
8785 TREE_NOTHROW (*node) = 1;
8786 /* ??? TODO: Support types. */
8787 else
8789 warning (OPT_Wattributes, "%qE attribute ignored", name);
8790 *no_add_attrs = true;
8793 return NULL_TREE;
8796 /* Handle a "cleanup" attribute; arguments as in
8797 struct attribute_spec.handler. */
8799 static tree
8800 handle_cleanup_attribute (tree *node, tree name, tree args,
8801 int ARG_UNUSED (flags), bool *no_add_attrs)
8803 tree decl = *node;
8804 tree cleanup_id, cleanup_decl;
8806 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8807 for global destructors in C++. This requires infrastructure that
8808 we don't have generically at the moment. It's also not a feature
8809 we'd be missing too much, since we do have attribute constructor. */
8810 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8812 warning (OPT_Wattributes, "%qE attribute ignored", name);
8813 *no_add_attrs = true;
8814 return NULL_TREE;
8817 /* Verify that the argument is a function in scope. */
8818 /* ??? We could support pointers to functions here as well, if
8819 that was considered desirable. */
8820 cleanup_id = TREE_VALUE (args);
8821 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8823 error ("cleanup argument not an identifier");
8824 *no_add_attrs = true;
8825 return NULL_TREE;
8827 cleanup_decl = lookup_name (cleanup_id);
8828 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8830 error ("cleanup argument not a function");
8831 *no_add_attrs = true;
8832 return NULL_TREE;
8835 /* That the function has proper type is checked with the
8836 eventual call to build_function_call. */
8838 return NULL_TREE;
8841 /* Handle a "warn_unused_result" attribute. No special handling. */
8843 static tree
8844 handle_warn_unused_result_attribute (tree *node, tree name,
8845 tree ARG_UNUSED (args),
8846 int ARG_UNUSED (flags), bool *no_add_attrs)
8848 /* Ignore the attribute for functions not returning any value. */
8849 if (VOID_TYPE_P (TREE_TYPE (*node)))
8851 warning (OPT_Wattributes, "%qE attribute ignored", name);
8852 *no_add_attrs = true;
8855 return NULL_TREE;
8858 /* Handle a "sentinel" attribute. */
8860 static tree
8861 handle_sentinel_attribute (tree *node, tree name, tree args,
8862 int ARG_UNUSED (flags), bool *no_add_attrs)
8864 if (!prototype_p (*node))
8866 warning (OPT_Wattributes,
8867 "%qE attribute requires prototypes with named arguments", name);
8868 *no_add_attrs = true;
8870 else
8872 if (!stdarg_p (*node))
8874 warning (OPT_Wattributes,
8875 "%qE attribute only applies to variadic functions", name);
8876 *no_add_attrs = true;
8880 if (args)
8882 tree position = TREE_VALUE (args);
8884 if (TREE_CODE (position) != INTEGER_CST)
8886 warning (OPT_Wattributes,
8887 "requested position is not an integer constant");
8888 *no_add_attrs = true;
8890 else
8892 if (tree_int_cst_lt (position, integer_zero_node))
8894 warning (OPT_Wattributes,
8895 "requested position is less than zero");
8896 *no_add_attrs = true;
8901 return NULL_TREE;
8904 /* Handle a "type_generic" attribute. */
8906 static tree
8907 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8908 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8909 bool * ARG_UNUSED (no_add_attrs))
8911 /* Ensure we have a function type. */
8912 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8914 /* Ensure we have a variadic function. */
8915 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8917 return NULL_TREE;
8920 /* Handle a "target" attribute. */
8922 static tree
8923 handle_target_attribute (tree *node, tree name, tree args, int flags,
8924 bool *no_add_attrs)
8926 /* Ensure we have a function type. */
8927 if (TREE_CODE (*node) != FUNCTION_DECL)
8929 warning (OPT_Wattributes, "%qE attribute ignored", name);
8930 *no_add_attrs = true;
8932 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8933 flags))
8934 *no_add_attrs = true;
8936 return NULL_TREE;
8939 /* Arguments being collected for optimization. */
8940 typedef const char *const_char_p; /* For DEF_VEC_P. */
8941 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
8944 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8945 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8946 false for #pragma GCC optimize. */
8948 bool
8949 parse_optimize_options (tree args, bool attr_p)
8951 bool ret = true;
8952 unsigned opt_argc;
8953 unsigned i;
8954 int saved_flag_strict_aliasing;
8955 const char **opt_argv;
8956 struct cl_decoded_option *decoded_options;
8957 unsigned int decoded_options_count;
8958 tree ap;
8960 /* Build up argv vector. Just in case the string is stored away, use garbage
8961 collected strings. */
8962 vec_safe_truncate (optimize_args, 0);
8963 vec_safe_push (optimize_args, (const char *) NULL);
8965 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8967 tree value = TREE_VALUE (ap);
8969 if (TREE_CODE (value) == INTEGER_CST)
8971 char buffer[20];
8972 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8973 vec_safe_push (optimize_args, ggc_strdup (buffer));
8976 else if (TREE_CODE (value) == STRING_CST)
8978 /* Split string into multiple substrings. */
8979 size_t len = TREE_STRING_LENGTH (value);
8980 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8981 char *end = p + len;
8982 char *comma;
8983 char *next_p = p;
8985 while (next_p != NULL)
8987 size_t len2;
8988 char *q, *r;
8990 p = next_p;
8991 comma = strchr (p, ',');
8992 if (comma)
8994 len2 = comma - p;
8995 *comma = '\0';
8996 next_p = comma+1;
8998 else
9000 len2 = end - p;
9001 next_p = NULL;
9004 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9006 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9007 options. */
9008 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9010 ret = false;
9011 if (attr_p)
9012 warning (OPT_Wattributes,
9013 "bad option %s to optimize attribute", p);
9014 else
9015 warning (OPT_Wpragmas,
9016 "bad option %s to pragma attribute", p);
9017 continue;
9020 if (*p != '-')
9022 *r++ = '-';
9024 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9025 itself is -Os, and any other switch begins with a -f. */
9026 if ((*p >= '0' && *p <= '9')
9027 || (p[0] == 's' && p[1] == '\0'))
9028 *r++ = 'O';
9029 else if (*p != 'O')
9030 *r++ = 'f';
9033 memcpy (r, p, len2);
9034 r[len2] = '\0';
9035 vec_safe_push (optimize_args, (const char *) q);
9041 opt_argc = optimize_args->length ();
9042 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9044 for (i = 1; i < opt_argc; i++)
9045 opt_argv[i] = (*optimize_args)[i];
9047 saved_flag_strict_aliasing = flag_strict_aliasing;
9049 /* Now parse the options. */
9050 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9051 &decoded_options,
9052 &decoded_options_count);
9053 decode_options (&global_options, &global_options_set,
9054 decoded_options, decoded_options_count,
9055 input_location, global_dc);
9057 targetm.override_options_after_change();
9059 /* Don't allow changing -fstrict-aliasing. */
9060 flag_strict_aliasing = saved_flag_strict_aliasing;
9062 optimize_args->truncate (0);
9063 return ret;
9066 /* For handling "optimize" attribute. arguments as in
9067 struct attribute_spec.handler. */
9069 static tree
9070 handle_optimize_attribute (tree *node, tree name, tree args,
9071 int ARG_UNUSED (flags), bool *no_add_attrs)
9073 /* Ensure we have a function type. */
9074 if (TREE_CODE (*node) != FUNCTION_DECL)
9076 warning (OPT_Wattributes, "%qE attribute ignored", name);
9077 *no_add_attrs = true;
9079 else
9081 struct cl_optimization cur_opts;
9082 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9084 /* Save current options. */
9085 cl_optimization_save (&cur_opts, &global_options);
9087 /* If we previously had some optimization options, use them as the
9088 default. */
9089 if (old_opts)
9090 cl_optimization_restore (&global_options,
9091 TREE_OPTIMIZATION (old_opts));
9093 /* Parse options, and update the vector. */
9094 parse_optimize_options (args, true);
9095 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9096 = build_optimization_node (&global_options);
9098 /* Restore current options. */
9099 cl_optimization_restore (&global_options, &cur_opts);
9102 return NULL_TREE;
9105 /* Handle a "no_split_stack" attribute. */
9107 static tree
9108 handle_no_split_stack_attribute (tree *node, tree name,
9109 tree ARG_UNUSED (args),
9110 int ARG_UNUSED (flags),
9111 bool *no_add_attrs)
9113 tree decl = *node;
9115 if (TREE_CODE (decl) != FUNCTION_DECL)
9117 error_at (DECL_SOURCE_LOCATION (decl),
9118 "%qE attribute applies only to functions", name);
9119 *no_add_attrs = true;
9121 else if (DECL_INITIAL (decl))
9123 error_at (DECL_SOURCE_LOCATION (decl),
9124 "can%'t set %qE attribute after definition", name);
9125 *no_add_attrs = true;
9128 return NULL_TREE;
9131 /* Handle a "returns_nonnull" attribute; arguments as in
9132 struct attribute_spec.handler. */
9134 static tree
9135 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9136 bool *no_add_attrs)
9138 // Even without a prototype we still have a return type we can check.
9139 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9141 error ("returns_nonnull attribute on a function not returning a pointer");
9142 *no_add_attrs = true;
9144 return NULL_TREE;
9148 /* Check for valid arguments being passed to a function with FNTYPE.
9149 There are NARGS arguments in the array ARGARRAY. */
9150 void
9151 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9153 /* Check for null being passed in a pointer argument that must be
9154 non-null. We also need to do this if format checking is enabled. */
9156 if (warn_nonnull)
9157 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9159 /* Check for errors in format strings. */
9161 if (warn_format || warn_suggest_attribute_format)
9162 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9164 if (warn_format)
9165 check_function_sentinel (fntype, nargs, argarray);
9168 /* Generic argument checking recursion routine. PARAM is the argument to
9169 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9170 once the argument is resolved. CTX is context for the callback. */
9171 void
9172 check_function_arguments_recurse (void (*callback)
9173 (void *, tree, unsigned HOST_WIDE_INT),
9174 void *ctx, tree param,
9175 unsigned HOST_WIDE_INT param_num)
9177 if (CONVERT_EXPR_P (param)
9178 && (TYPE_PRECISION (TREE_TYPE (param))
9179 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9181 /* Strip coercion. */
9182 check_function_arguments_recurse (callback, ctx,
9183 TREE_OPERAND (param, 0), param_num);
9184 return;
9187 if (TREE_CODE (param) == CALL_EXPR)
9189 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9190 tree attrs;
9191 bool found_format_arg = false;
9193 /* See if this is a call to a known internationalization function
9194 that modifies a format arg. Such a function may have multiple
9195 format_arg attributes (for example, ngettext). */
9197 for (attrs = TYPE_ATTRIBUTES (type);
9198 attrs;
9199 attrs = TREE_CHAIN (attrs))
9200 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9202 tree inner_arg;
9203 tree format_num_expr;
9204 int format_num;
9205 int i;
9206 call_expr_arg_iterator iter;
9208 /* Extract the argument number, which was previously checked
9209 to be valid. */
9210 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9212 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9213 && !TREE_INT_CST_HIGH (format_num_expr));
9215 format_num = TREE_INT_CST_LOW (format_num_expr);
9217 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9218 inner_arg != 0;
9219 inner_arg = next_call_expr_arg (&iter), i++)
9220 if (i == format_num)
9222 check_function_arguments_recurse (callback, ctx,
9223 inner_arg, param_num);
9224 found_format_arg = true;
9225 break;
9229 /* If we found a format_arg attribute and did a recursive check,
9230 we are done with checking this argument. Otherwise, we continue
9231 and this will be considered a non-literal. */
9232 if (found_format_arg)
9233 return;
9236 if (TREE_CODE (param) == COND_EXPR)
9238 /* Check both halves of the conditional expression. */
9239 check_function_arguments_recurse (callback, ctx,
9240 TREE_OPERAND (param, 1), param_num);
9241 check_function_arguments_recurse (callback, ctx,
9242 TREE_OPERAND (param, 2), param_num);
9243 return;
9246 (*callback) (ctx, param, param_num);
9249 /* Checks for a builtin function FNDECL that the number of arguments
9250 NARGS against the required number REQUIRED and issues an error if
9251 there is a mismatch. Returns true if the number of arguments is
9252 correct, otherwise false. */
9254 static bool
9255 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9257 if (nargs < required)
9259 error_at (input_location,
9260 "not enough arguments to function %qE", fndecl);
9261 return false;
9263 else if (nargs > required)
9265 error_at (input_location,
9266 "too many arguments to function %qE", fndecl);
9267 return false;
9269 return true;
9272 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9273 Returns false if there was an error, otherwise true. */
9275 bool
9276 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9278 if (!DECL_BUILT_IN (fndecl)
9279 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9280 return true;
9282 switch (DECL_FUNCTION_CODE (fndecl))
9284 case BUILT_IN_CONSTANT_P:
9285 return builtin_function_validate_nargs (fndecl, nargs, 1);
9287 case BUILT_IN_ISFINITE:
9288 case BUILT_IN_ISINF:
9289 case BUILT_IN_ISINF_SIGN:
9290 case BUILT_IN_ISNAN:
9291 case BUILT_IN_ISNORMAL:
9292 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9294 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9296 error ("non-floating-point argument in call to "
9297 "function %qE", fndecl);
9298 return false;
9300 return true;
9302 return false;
9304 case BUILT_IN_ISGREATER:
9305 case BUILT_IN_ISGREATEREQUAL:
9306 case BUILT_IN_ISLESS:
9307 case BUILT_IN_ISLESSEQUAL:
9308 case BUILT_IN_ISLESSGREATER:
9309 case BUILT_IN_ISUNORDERED:
9310 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9312 enum tree_code code0, code1;
9313 code0 = TREE_CODE (TREE_TYPE (args[0]));
9314 code1 = TREE_CODE (TREE_TYPE (args[1]));
9315 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9316 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9317 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9319 error ("non-floating-point arguments in call to "
9320 "function %qE", fndecl);
9321 return false;
9323 return true;
9325 return false;
9327 case BUILT_IN_FPCLASSIFY:
9328 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9330 unsigned i;
9332 for (i=0; i<5; i++)
9333 if (TREE_CODE (args[i]) != INTEGER_CST)
9335 error ("non-const integer argument %u in call to function %qE",
9336 i+1, fndecl);
9337 return false;
9340 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9342 error ("non-floating-point argument in call to function %qE",
9343 fndecl);
9344 return false;
9346 return true;
9348 return false;
9350 case BUILT_IN_ASSUME_ALIGNED:
9351 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9353 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9355 error ("non-integer argument 3 in call to function %qE", fndecl);
9356 return false;
9358 return true;
9360 return false;
9362 default:
9363 return true;
9367 /* Function to help qsort sort FIELD_DECLs by name order. */
9370 field_decl_cmp (const void *x_p, const void *y_p)
9372 const tree *const x = (const tree *const) x_p;
9373 const tree *const y = (const tree *const) y_p;
9375 if (DECL_NAME (*x) == DECL_NAME (*y))
9376 /* A nontype is "greater" than a type. */
9377 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9378 if (DECL_NAME (*x) == NULL_TREE)
9379 return -1;
9380 if (DECL_NAME (*y) == NULL_TREE)
9381 return 1;
9382 if (DECL_NAME (*x) < DECL_NAME (*y))
9383 return -1;
9384 return 1;
9387 static struct {
9388 gt_pointer_operator new_value;
9389 void *cookie;
9390 } resort_data;
9392 /* This routine compares two fields like field_decl_cmp but using the
9393 pointer operator in resort_data. */
9395 static int
9396 resort_field_decl_cmp (const void *x_p, const void *y_p)
9398 const tree *const x = (const tree *const) x_p;
9399 const tree *const y = (const tree *const) y_p;
9401 if (DECL_NAME (*x) == DECL_NAME (*y))
9402 /* A nontype is "greater" than a type. */
9403 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9404 if (DECL_NAME (*x) == NULL_TREE)
9405 return -1;
9406 if (DECL_NAME (*y) == NULL_TREE)
9407 return 1;
9409 tree d1 = DECL_NAME (*x);
9410 tree d2 = DECL_NAME (*y);
9411 resort_data.new_value (&d1, resort_data.cookie);
9412 resort_data.new_value (&d2, resort_data.cookie);
9413 if (d1 < d2)
9414 return -1;
9416 return 1;
9419 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9421 void
9422 resort_sorted_fields (void *obj,
9423 void * ARG_UNUSED (orig_obj),
9424 gt_pointer_operator new_value,
9425 void *cookie)
9427 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9428 resort_data.new_value = new_value;
9429 resort_data.cookie = cookie;
9430 qsort (&sf->elts[0], sf->len, sizeof (tree),
9431 resort_field_decl_cmp);
9434 /* Subroutine of c_parse_error.
9435 Return the result of concatenating LHS and RHS. RHS is really
9436 a string literal, its first character is indicated by RHS_START and
9437 RHS_SIZE is its length (including the terminating NUL character).
9439 The caller is responsible for deleting the returned pointer. */
9441 static char *
9442 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9444 const int lhs_size = strlen (lhs);
9445 char *result = XNEWVEC (char, lhs_size + rhs_size);
9446 strncpy (result, lhs, lhs_size);
9447 strncpy (result + lhs_size, rhs_start, rhs_size);
9448 return result;
9451 /* Issue the error given by GMSGID, indicating that it occurred before
9452 TOKEN, which had the associated VALUE. */
9454 void
9455 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9456 tree value, unsigned char token_flags)
9458 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9460 char *message = NULL;
9462 if (token_type == CPP_EOF)
9463 message = catenate_messages (gmsgid, " at end of input");
9464 else if (token_type == CPP_CHAR
9465 || token_type == CPP_WCHAR
9466 || token_type == CPP_CHAR16
9467 || token_type == CPP_CHAR32)
9469 unsigned int val = TREE_INT_CST_LOW (value);
9470 const char *prefix;
9472 switch (token_type)
9474 default:
9475 prefix = "";
9476 break;
9477 case CPP_WCHAR:
9478 prefix = "L";
9479 break;
9480 case CPP_CHAR16:
9481 prefix = "u";
9482 break;
9483 case CPP_CHAR32:
9484 prefix = "U";
9485 break;
9488 if (val <= UCHAR_MAX && ISGRAPH (val))
9489 message = catenate_messages (gmsgid, " before %s'%c'");
9490 else
9491 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9493 error (message, prefix, val);
9494 free (message);
9495 message = NULL;
9497 else if (token_type == CPP_CHAR_USERDEF
9498 || token_type == CPP_WCHAR_USERDEF
9499 || token_type == CPP_CHAR16_USERDEF
9500 || token_type == CPP_CHAR32_USERDEF)
9501 message = catenate_messages (gmsgid,
9502 " before user-defined character literal");
9503 else if (token_type == CPP_STRING_USERDEF
9504 || token_type == CPP_WSTRING_USERDEF
9505 || token_type == CPP_STRING16_USERDEF
9506 || token_type == CPP_STRING32_USERDEF
9507 || token_type == CPP_UTF8STRING_USERDEF)
9508 message = catenate_messages (gmsgid, " before user-defined string literal");
9509 else if (token_type == CPP_STRING
9510 || token_type == CPP_WSTRING
9511 || token_type == CPP_STRING16
9512 || token_type == CPP_STRING32
9513 || token_type == CPP_UTF8STRING)
9514 message = catenate_messages (gmsgid, " before string constant");
9515 else if (token_type == CPP_NUMBER)
9516 message = catenate_messages (gmsgid, " before numeric constant");
9517 else if (token_type == CPP_NAME)
9519 message = catenate_messages (gmsgid, " before %qE");
9520 error (message, value);
9521 free (message);
9522 message = NULL;
9524 else if (token_type == CPP_PRAGMA)
9525 message = catenate_messages (gmsgid, " before %<#pragma%>");
9526 else if (token_type == CPP_PRAGMA_EOL)
9527 message = catenate_messages (gmsgid, " before end of line");
9528 else if (token_type == CPP_DECLTYPE)
9529 message = catenate_messages (gmsgid, " before %<decltype%>");
9530 else if (token_type < N_TTYPES)
9532 message = catenate_messages (gmsgid, " before %qs token");
9533 error (message, cpp_type2name (token_type, token_flags));
9534 free (message);
9535 message = NULL;
9537 else
9538 error (gmsgid);
9540 if (message)
9542 error (message);
9543 free (message);
9545 #undef catenate_messages
9548 /* Mapping for cpp message reasons to the options that enable them. */
9550 struct reason_option_codes_t
9552 const int reason; /* cpplib message reason. */
9553 const int option_code; /* gcc option that controls this message. */
9556 static const struct reason_option_codes_t option_codes[] = {
9557 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9558 {CPP_W_COMMENTS, OPT_Wcomment},
9559 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9560 {CPP_W_MULTICHAR, OPT_Wmultichar},
9561 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9562 {CPP_W_LONG_LONG, OPT_Wlong_long},
9563 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9564 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9565 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9566 {CPP_W_UNDEF, OPT_Wundef},
9567 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9568 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9569 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9570 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9571 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9572 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9573 {CPP_W_DATE_TIME, OPT_Wdate_time},
9574 {CPP_W_NONE, 0}
9577 /* Return the gcc option code associated with the reason for a cpp
9578 message, or 0 if none. */
9580 static int
9581 c_option_controlling_cpp_error (int reason)
9583 const struct reason_option_codes_t *entry;
9585 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9587 if (entry->reason == reason)
9588 return entry->option_code;
9590 return 0;
9593 /* Callback from cpp_error for PFILE to print diagnostics from the
9594 preprocessor. The diagnostic is of type LEVEL, with REASON set
9595 to the reason code if LEVEL is represents a warning, at location
9596 LOCATION unless this is after lexing and the compiler's location
9597 should be used instead, with column number possibly overridden by
9598 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9599 the arguments. Returns true if a diagnostic was emitted, false
9600 otherwise. */
9602 bool
9603 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9604 location_t location, unsigned int column_override,
9605 const char *msg, va_list *ap)
9607 diagnostic_info diagnostic;
9608 diagnostic_t dlevel;
9609 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9610 bool ret;
9612 switch (level)
9614 case CPP_DL_WARNING_SYSHDR:
9615 if (flag_no_output)
9616 return false;
9617 global_dc->dc_warn_system_headers = 1;
9618 /* Fall through. */
9619 case CPP_DL_WARNING:
9620 if (flag_no_output)
9621 return false;
9622 dlevel = DK_WARNING;
9623 break;
9624 case CPP_DL_PEDWARN:
9625 if (flag_no_output && !flag_pedantic_errors)
9626 return false;
9627 dlevel = DK_PEDWARN;
9628 break;
9629 case CPP_DL_ERROR:
9630 dlevel = DK_ERROR;
9631 break;
9632 case CPP_DL_ICE:
9633 dlevel = DK_ICE;
9634 break;
9635 case CPP_DL_NOTE:
9636 dlevel = DK_NOTE;
9637 break;
9638 case CPP_DL_FATAL:
9639 dlevel = DK_FATAL;
9640 break;
9641 default:
9642 gcc_unreachable ();
9644 if (done_lexing)
9645 location = input_location;
9646 diagnostic_set_info_translated (&diagnostic, msg, ap,
9647 location, dlevel);
9648 if (column_override)
9649 diagnostic_override_column (&diagnostic, column_override);
9650 diagnostic_override_option_index (&diagnostic,
9651 c_option_controlling_cpp_error (reason));
9652 ret = report_diagnostic (&diagnostic);
9653 if (level == CPP_DL_WARNING_SYSHDR)
9654 global_dc->dc_warn_system_headers = save_warn_system_headers;
9655 return ret;
9658 /* Convert a character from the host to the target execution character
9659 set. cpplib handles this, mostly. */
9661 HOST_WIDE_INT
9662 c_common_to_target_charset (HOST_WIDE_INT c)
9664 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9665 zero-extended under -fno-signed-char. cpplib insists that characters
9666 and character constants are always unsigned. Hence we must convert
9667 back and forth. */
9668 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9670 uc = cpp_host_to_exec_charset (parse_in, uc);
9672 if (flag_signed_char)
9673 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9674 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9675 else
9676 return uc;
9679 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9680 references with an INDIRECT_REF of a constant at the bottom; much like the
9681 traditional rendering of offsetof as a macro. Return the folded result. */
9683 tree
9684 fold_offsetof_1 (tree expr)
9686 tree base, off, t;
9688 switch (TREE_CODE (expr))
9690 case ERROR_MARK:
9691 return expr;
9693 case VAR_DECL:
9694 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9695 return error_mark_node;
9697 case CALL_EXPR:
9698 case TARGET_EXPR:
9699 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9700 return error_mark_node;
9702 case NOP_EXPR:
9703 case INDIRECT_REF:
9704 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9706 error ("cannot apply %<offsetof%> to a non constant address");
9707 return error_mark_node;
9709 return TREE_OPERAND (expr, 0);
9711 case COMPONENT_REF:
9712 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9713 if (base == error_mark_node)
9714 return base;
9716 t = TREE_OPERAND (expr, 1);
9717 if (DECL_C_BIT_FIELD (t))
9719 error ("attempt to take address of bit-field structure "
9720 "member %qD", t);
9721 return error_mark_node;
9723 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9724 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9726 / BITS_PER_UNIT));
9727 break;
9729 case ARRAY_REF:
9730 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9731 if (base == error_mark_node)
9732 return base;
9734 t = TREE_OPERAND (expr, 1);
9736 /* Check if the offset goes beyond the upper bound of the array. */
9737 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9739 tree upbound = array_ref_up_bound (expr);
9740 if (upbound != NULL_TREE
9741 && TREE_CODE (upbound) == INTEGER_CST
9742 && !tree_int_cst_equal (upbound,
9743 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9745 upbound = size_binop (PLUS_EXPR, upbound,
9746 build_int_cst (TREE_TYPE (upbound), 1));
9747 if (tree_int_cst_lt (upbound, t))
9749 tree v;
9751 for (v = TREE_OPERAND (expr, 0);
9752 TREE_CODE (v) == COMPONENT_REF;
9753 v = TREE_OPERAND (v, 0))
9754 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9755 == RECORD_TYPE)
9757 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9758 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9759 if (TREE_CODE (fld_chain) == FIELD_DECL)
9760 break;
9762 if (fld_chain)
9763 break;
9765 /* Don't warn if the array might be considered a poor
9766 man's flexible array member with a very permissive
9767 definition thereof. */
9768 if (TREE_CODE (v) == ARRAY_REF
9769 || TREE_CODE (v) == COMPONENT_REF)
9770 warning (OPT_Warray_bounds,
9771 "index %E denotes an offset "
9772 "greater than size of %qT",
9773 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9778 t = convert (sizetype, t);
9779 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9780 break;
9782 case COMPOUND_EXPR:
9783 /* Handle static members of volatile structs. */
9784 t = TREE_OPERAND (expr, 1);
9785 gcc_assert (TREE_CODE (t) == VAR_DECL);
9786 return fold_offsetof_1 (t);
9788 default:
9789 gcc_unreachable ();
9792 return fold_build_pointer_plus (base, off);
9795 /* Likewise, but convert it to the return type of offsetof. */
9797 tree
9798 fold_offsetof (tree expr)
9800 return convert (size_type_node, fold_offsetof_1 (expr));
9803 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9804 expression, because B will always be true. */
9806 void
9807 warn_for_omitted_condop (location_t location, tree cond)
9809 if (truth_value_p (TREE_CODE (cond)))
9810 warning_at (location, OPT_Wparentheses,
9811 "the omitted middle operand in ?: will always be %<true%>, "
9812 "suggest explicit middle operand");
9815 /* Give an error for storing into ARG, which is 'const'. USE indicates
9816 how ARG was being used. */
9818 void
9819 readonly_error (location_t loc, tree arg, enum lvalue_use use)
9821 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9822 || use == lv_asm);
9823 /* Using this macro rather than (for example) arrays of messages
9824 ensures that all the format strings are checked at compile
9825 time. */
9826 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9827 : (use == lv_increment ? (I) \
9828 : (use == lv_decrement ? (D) : (AS))))
9829 if (TREE_CODE (arg) == COMPONENT_REF)
9831 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9832 error_at (loc, READONLY_MSG (G_("assignment of member "
9833 "%qD in read-only object"),
9834 G_("increment of member "
9835 "%qD in read-only object"),
9836 G_("decrement of member "
9837 "%qD in read-only object"),
9838 G_("member %qD in read-only object "
9839 "used as %<asm%> output")),
9840 TREE_OPERAND (arg, 1));
9841 else
9842 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
9843 G_("increment of read-only member %qD"),
9844 G_("decrement of read-only member %qD"),
9845 G_("read-only member %qD used as %<asm%> output")),
9846 TREE_OPERAND (arg, 1));
9848 else if (TREE_CODE (arg) == VAR_DECL)
9849 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
9850 G_("increment of read-only variable %qD"),
9851 G_("decrement of read-only variable %qD"),
9852 G_("read-only variable %qD used as %<asm%> output")),
9853 arg);
9854 else if (TREE_CODE (arg) == PARM_DECL)
9855 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
9856 G_("increment of read-only parameter %qD"),
9857 G_("decrement of read-only parameter %qD"),
9858 G_("read-only parameter %qD use as %<asm%> output")),
9859 arg);
9860 else if (TREE_CODE (arg) == RESULT_DECL)
9862 gcc_assert (c_dialect_cxx ());
9863 error_at (loc, READONLY_MSG (G_("assignment of "
9864 "read-only named return value %qD"),
9865 G_("increment of "
9866 "read-only named return value %qD"),
9867 G_("decrement of "
9868 "read-only named return value %qD"),
9869 G_("read-only named return value %qD "
9870 "used as %<asm%>output")),
9871 arg);
9873 else if (TREE_CODE (arg) == FUNCTION_DECL)
9874 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
9875 G_("increment of function %qD"),
9876 G_("decrement of function %qD"),
9877 G_("function %qD used as %<asm%> output")),
9878 arg);
9879 else
9880 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
9881 G_("increment of read-only location %qE"),
9882 G_("decrement of read-only location %qE"),
9883 G_("read-only location %qE used as %<asm%> output")),
9884 arg);
9887 /* Print an error message for an invalid lvalue. USE says
9888 how the lvalue is being used and so selects the error message. LOC
9889 is the location for the error. */
9891 void
9892 lvalue_error (location_t loc, enum lvalue_use use)
9894 switch (use)
9896 case lv_assign:
9897 error_at (loc, "lvalue required as left operand of assignment");
9898 break;
9899 case lv_increment:
9900 error_at (loc, "lvalue required as increment operand");
9901 break;
9902 case lv_decrement:
9903 error_at (loc, "lvalue required as decrement operand");
9904 break;
9905 case lv_addressof:
9906 error_at (loc, "lvalue required as unary %<&%> operand");
9907 break;
9908 case lv_asm:
9909 error_at (loc, "lvalue required in asm statement");
9910 break;
9911 default:
9912 gcc_unreachable ();
9916 /* Print an error message for an invalid indirection of type TYPE.
9917 ERRSTRING is the name of the operator for the indirection. */
9919 void
9920 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9922 switch (errstring)
9924 case RO_NULL:
9925 gcc_assert (c_dialect_cxx ());
9926 error_at (loc, "invalid type argument (have %qT)", type);
9927 break;
9928 case RO_ARRAY_INDEXING:
9929 error_at (loc,
9930 "invalid type argument of array indexing (have %qT)",
9931 type);
9932 break;
9933 case RO_UNARY_STAR:
9934 error_at (loc,
9935 "invalid type argument of unary %<*%> (have %qT)",
9936 type);
9937 break;
9938 case RO_ARROW:
9939 error_at (loc,
9940 "invalid type argument of %<->%> (have %qT)",
9941 type);
9942 break;
9943 case RO_ARROW_STAR:
9944 error_at (loc,
9945 "invalid type argument of %<->*%> (have %qT)",
9946 type);
9947 break;
9948 case RO_IMPLICIT_CONVERSION:
9949 error_at (loc,
9950 "invalid type argument of implicit conversion (have %qT)",
9951 type);
9952 break;
9953 default:
9954 gcc_unreachable ();
9958 /* *PTYPE is an incomplete array. Complete it with a domain based on
9959 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9960 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9961 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9964 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9966 tree maxindex, type, main_type, elt, unqual_elt;
9967 int failure = 0, quals;
9968 hashval_t hashcode = 0;
9969 bool overflow_p = false;
9971 maxindex = size_zero_node;
9972 if (initial_value)
9974 if (TREE_CODE (initial_value) == STRING_CST)
9976 int eltsize
9977 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9978 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9980 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9982 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9984 if (vec_safe_is_empty (v))
9986 if (pedantic)
9987 failure = 3;
9988 maxindex = ssize_int (-1);
9990 else
9992 tree curindex;
9993 unsigned HOST_WIDE_INT cnt;
9994 constructor_elt *ce;
9995 bool fold_p = false;
9997 if ((*v)[0].index)
9998 maxindex = (*v)[0].index, fold_p = true;
10000 curindex = maxindex;
10002 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10004 bool curfold_p = false;
10005 if (ce->index)
10006 curindex = ce->index, curfold_p = true;
10007 else
10009 if (fold_p)
10011 /* Since we treat size types now as ordinary
10012 unsigned types, we need an explicit overflow
10013 check. */
10014 tree orig = curindex;
10015 curindex = fold_convert (sizetype, curindex);
10016 overflow_p |= tree_int_cst_lt (curindex, orig);
10018 curindex = size_binop (PLUS_EXPR, curindex,
10019 size_one_node);
10021 if (tree_int_cst_lt (maxindex, curindex))
10022 maxindex = curindex, fold_p = curfold_p;
10024 if (fold_p)
10026 tree orig = maxindex;
10027 maxindex = fold_convert (sizetype, maxindex);
10028 overflow_p |= tree_int_cst_lt (maxindex, orig);
10032 else
10034 /* Make an error message unless that happened already. */
10035 if (initial_value != error_mark_node)
10036 failure = 1;
10039 else
10041 failure = 2;
10042 if (!do_default)
10043 return failure;
10046 type = *ptype;
10047 elt = TREE_TYPE (type);
10048 quals = TYPE_QUALS (strip_array_types (elt));
10049 if (quals == 0)
10050 unqual_elt = elt;
10051 else
10052 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10054 /* Using build_distinct_type_copy and modifying things afterward instead
10055 of using build_array_type to create a new type preserves all of the
10056 TYPE_LANG_FLAG_? bits that the front end may have set. */
10057 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10058 TREE_TYPE (main_type) = unqual_elt;
10059 TYPE_DOMAIN (main_type)
10060 = build_range_type (TREE_TYPE (maxindex),
10061 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10062 layout_type (main_type);
10064 /* Make sure we have the canonical MAIN_TYPE. */
10065 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10066 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10067 hashcode);
10068 main_type = type_hash_canon (hashcode, main_type);
10070 /* Fix the canonical type. */
10071 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10072 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10073 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10074 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10075 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10076 != TYPE_DOMAIN (main_type)))
10077 TYPE_CANONICAL (main_type)
10078 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10079 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10080 else
10081 TYPE_CANONICAL (main_type) = main_type;
10083 if (quals == 0)
10084 type = main_type;
10085 else
10086 type = c_build_qualified_type (main_type, quals);
10088 if (COMPLETE_TYPE_P (type)
10089 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10090 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10092 error ("size of array is too large");
10093 /* If we proceed with the array type as it is, we'll eventually
10094 crash in tree_low_cst(). */
10095 type = error_mark_node;
10098 *ptype = type;
10099 return failure;
10102 /* Like c_mark_addressable but don't check register qualifier. */
10103 void
10104 c_common_mark_addressable_vec (tree t)
10106 while (handled_component_p (t))
10107 t = TREE_OPERAND (t, 0);
10108 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10109 return;
10110 TREE_ADDRESSABLE (t) = 1;
10115 /* Used to help initialize the builtin-types.def table. When a type of
10116 the correct size doesn't exist, use error_mark_node instead of NULL.
10117 The later results in segfaults even when a decl using the type doesn't
10118 get invoked. */
10120 tree
10121 builtin_type_for_size (int size, bool unsignedp)
10123 tree type = c_common_type_for_size (size, unsignedp);
10124 return type ? type : error_mark_node;
10127 /* A helper function for resolve_overloaded_builtin in resolving the
10128 overloaded __sync_ builtins. Returns a positive power of 2 if the
10129 first operand of PARAMS is a pointer to a supported data type.
10130 Returns 0 if an error is encountered. */
10132 static int
10133 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10135 tree type;
10136 int size;
10138 if (!params)
10140 error ("too few arguments to function %qE", function);
10141 return 0;
10144 type = TREE_TYPE ((*params)[0]);
10145 if (TREE_CODE (type) != POINTER_TYPE)
10146 goto incompatible;
10148 type = TREE_TYPE (type);
10149 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10150 goto incompatible;
10152 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
10153 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10154 return size;
10156 incompatible:
10157 error ("incompatible type for argument %d of %qE", 1, function);
10158 return 0;
10161 /* A helper function for resolve_overloaded_builtin. Adds casts to
10162 PARAMS to make arguments match up with those of FUNCTION. Drops
10163 the variadic arguments at the end. Returns false if some error
10164 was encountered; true on success. */
10166 static bool
10167 sync_resolve_params (location_t loc, tree orig_function, tree function,
10168 vec<tree, va_gc> *params, bool orig_format)
10170 function_args_iterator iter;
10171 tree ptype;
10172 unsigned int parmnum;
10174 function_args_iter_init (&iter, TREE_TYPE (function));
10175 /* We've declared the implementation functions to use "volatile void *"
10176 as the pointer parameter, so we shouldn't get any complaints from the
10177 call to check_function_arguments what ever type the user used. */
10178 function_args_iter_next (&iter);
10179 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10180 ptype = TYPE_MAIN_VARIANT (ptype);
10182 /* For the rest of the values, we need to cast these to FTYPE, so that we
10183 don't get warnings for passing pointer types, etc. */
10184 parmnum = 0;
10185 while (1)
10187 tree val, arg_type;
10189 arg_type = function_args_iter_cond (&iter);
10190 /* XXX void_type_node belies the abstraction. */
10191 if (arg_type == void_type_node)
10192 break;
10194 ++parmnum;
10195 if (params->length () <= parmnum)
10197 error_at (loc, "too few arguments to function %qE", orig_function);
10198 return false;
10201 /* Only convert parameters if arg_type is unsigned integer type with
10202 new format sync routines, i.e. don't attempt to convert pointer
10203 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10204 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10205 kinds). */
10206 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10208 /* Ideally for the first conversion we'd use convert_for_assignment
10209 so that we get warnings for anything that doesn't match the pointer
10210 type. This isn't portable across the C and C++ front ends atm. */
10211 val = (*params)[parmnum];
10212 val = convert (ptype, val);
10213 val = convert (arg_type, val);
10214 (*params)[parmnum] = val;
10217 function_args_iter_next (&iter);
10220 /* __atomic routines are not variadic. */
10221 if (!orig_format && params->length () != parmnum + 1)
10223 error_at (loc, "too many arguments to function %qE", orig_function);
10224 return false;
10227 /* The definition of these primitives is variadic, with the remaining
10228 being "an optional list of variables protected by the memory barrier".
10229 No clue what that's supposed to mean, precisely, but we consider all
10230 call-clobbered variables to be protected so we're safe. */
10231 params->truncate (parmnum + 1);
10233 return true;
10236 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10237 RESULT to make it match the type of the first pointer argument in
10238 PARAMS. */
10240 static tree
10241 sync_resolve_return (tree first_param, tree result, bool orig_format)
10243 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10244 tree rtype = TREE_TYPE (result);
10245 ptype = TYPE_MAIN_VARIANT (ptype);
10247 /* New format doesn't require casting unless the types are the same size. */
10248 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10249 return convert (ptype, result);
10250 else
10251 return result;
10254 /* This function verifies the PARAMS to generic atomic FUNCTION.
10255 It returns the size if all the parameters are the same size, otherwise
10256 0 is returned if the parameters are invalid. */
10258 static int
10259 get_atomic_generic_size (location_t loc, tree function,
10260 vec<tree, va_gc> *params)
10262 unsigned int n_param;
10263 unsigned int n_model;
10264 unsigned int x;
10265 int size_0;
10266 tree type_0;
10268 /* Determine the parameter makeup. */
10269 switch (DECL_FUNCTION_CODE (function))
10271 case BUILT_IN_ATOMIC_EXCHANGE:
10272 n_param = 4;
10273 n_model = 1;
10274 break;
10275 case BUILT_IN_ATOMIC_LOAD:
10276 case BUILT_IN_ATOMIC_STORE:
10277 n_param = 3;
10278 n_model = 1;
10279 break;
10280 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10281 n_param = 6;
10282 n_model = 2;
10283 break;
10284 default:
10285 gcc_unreachable ();
10288 if (vec_safe_length (params) != n_param)
10290 error_at (loc, "incorrect number of arguments to function %qE", function);
10291 return 0;
10294 /* Get type of first parameter, and determine its size. */
10295 type_0 = TREE_TYPE ((*params)[0]);
10296 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10298 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10299 function);
10300 return 0;
10303 /* Types must be compile time constant sizes. */
10304 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10306 error_at (loc,
10307 "argument 1 of %qE must be a pointer to a constant size type",
10308 function);
10309 return 0;
10312 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10314 /* Zero size objects are not allowed. */
10315 if (size_0 == 0)
10317 error_at (loc,
10318 "argument 1 of %qE must be a pointer to a nonzero size object",
10319 function);
10320 return 0;
10323 /* Check each other parameter is a pointer and the same size. */
10324 for (x = 0; x < n_param - n_model; x++)
10326 int size;
10327 tree type = TREE_TYPE ((*params)[x]);
10328 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10329 if (n_param == 6 && x == 3)
10330 continue;
10331 if (!POINTER_TYPE_P (type))
10333 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10334 function);
10335 return 0;
10337 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10338 if (size != size_0)
10340 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10341 function);
10342 return 0;
10346 /* Check memory model parameters for validity. */
10347 for (x = n_param - n_model ; x < n_param; x++)
10349 tree p = (*params)[x];
10350 if (TREE_CODE (p) == INTEGER_CST)
10352 int i = tree_low_cst (p, 1);
10353 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10355 warning_at (loc, OPT_Winvalid_memory_model,
10356 "invalid memory model argument %d of %qE", x + 1,
10357 function);
10360 else
10361 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10363 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10364 function);
10365 return 0;
10369 return size_0;
10373 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10374 at the beginning of the parameter list PARAMS representing the size of the
10375 objects. This is to match the library ABI requirement. LOC is the location
10376 of the function call.
10377 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10378 returned to allow the external call to be constructed. */
10380 static tree
10381 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10382 vec<tree, va_gc> *params)
10384 tree size_node;
10386 /* Insert a SIZE_T parameter as the first param. If there isn't
10387 enough space, allocate a new vector and recursively re-build with that. */
10388 if (!params->space (1))
10390 unsigned int z, len;
10391 vec<tree, va_gc> *v;
10392 tree f;
10394 len = params->length ();
10395 vec_alloc (v, len + 1);
10396 for (z = 0; z < len; z++)
10397 v->quick_push ((*params)[z]);
10398 f = build_function_call_vec (loc, function, v, NULL);
10399 vec_free (v);
10400 return f;
10403 /* Add the size parameter and leave as a function call for processing. */
10404 size_node = build_int_cst (size_type_node, n);
10405 params->quick_insert (0, size_node);
10406 return NULL_TREE;
10410 /* Return whether atomic operations for naturally aligned N-byte
10411 arguments are supported, whether inline or through libatomic. */
10412 static bool
10413 atomic_size_supported_p (int n)
10415 switch (n)
10417 case 1:
10418 case 2:
10419 case 4:
10420 case 8:
10421 return true;
10423 case 16:
10424 return targetm.scalar_mode_supported_p (TImode);
10426 default:
10427 return false;
10431 /* This will process an __atomic_exchange function call, determine whether it
10432 needs to be mapped to the _N variation, or turned into a library call.
10433 LOC is the location of the builtin call.
10434 FUNCTION is the DECL that has been invoked;
10435 PARAMS is the argument list for the call. The return value is non-null
10436 TRUE is returned if it is translated into the proper format for a call to the
10437 external library, and NEW_RETURN is set the tree for that function.
10438 FALSE is returned if processing for the _N variation is required, and
10439 NEW_RETURN is set to the the return value the result is copied into. */
10440 static bool
10441 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10442 vec<tree, va_gc> *params, tree *new_return)
10444 tree p0, p1, p2, p3;
10445 tree I_type, I_type_ptr;
10446 int n = get_atomic_generic_size (loc, function, params);
10448 /* Size of 0 is an error condition. */
10449 if (n == 0)
10451 *new_return = error_mark_node;
10452 return true;
10455 /* If not a lock-free size, change to the library generic format. */
10456 if (!atomic_size_supported_p (n))
10458 *new_return = add_atomic_size_parameter (n, loc, function, params);
10459 return true;
10462 /* Otherwise there is a lockfree match, transform the call from:
10463 void fn(T* mem, T* desired, T* return, model)
10464 into
10465 *return = (T) (fn (In* mem, (In) *desired, model)) */
10467 p0 = (*params)[0];
10468 p1 = (*params)[1];
10469 p2 = (*params)[2];
10470 p3 = (*params)[3];
10472 /* Create pointer to appropriate size. */
10473 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10474 I_type_ptr = build_pointer_type (I_type);
10476 /* Convert object pointer to required type. */
10477 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10478 (*params)[0] = p0;
10479 /* Convert new value to required type, and dereference it. */
10480 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10481 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10482 (*params)[1] = p1;
10484 /* Move memory model to the 3rd position, and end param list. */
10485 (*params)[2] = p3;
10486 params->truncate (3);
10488 /* Convert return pointer and dereference it for later assignment. */
10489 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10491 return false;
10495 /* This will process an __atomic_compare_exchange function call, determine
10496 whether it needs to be mapped to the _N variation, or turned into a lib call.
10497 LOC is the location of the builtin call.
10498 FUNCTION is the DECL that has been invoked;
10499 PARAMS is the argument list for the call. The return value is non-null
10500 TRUE is returned if it is translated into the proper format for a call to the
10501 external library, and NEW_RETURN is set the tree for that function.
10502 FALSE is returned if processing for the _N variation is required. */
10504 static bool
10505 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10506 vec<tree, va_gc> *params,
10507 tree *new_return)
10509 tree p0, p1, p2;
10510 tree I_type, I_type_ptr;
10511 int n = get_atomic_generic_size (loc, function, params);
10513 /* Size of 0 is an error condition. */
10514 if (n == 0)
10516 *new_return = error_mark_node;
10517 return true;
10520 /* If not a lock-free size, change to the library generic format. */
10521 if (!atomic_size_supported_p (n))
10523 /* The library generic format does not have the weak parameter, so
10524 remove it from the param list. Since a parameter has been removed,
10525 we can be sure that there is room for the SIZE_T parameter, meaning
10526 there will not be a recursive rebuilding of the parameter list, so
10527 there is no danger this will be done twice. */
10528 if (n > 0)
10530 (*params)[3] = (*params)[4];
10531 (*params)[4] = (*params)[5];
10532 params->truncate (5);
10534 *new_return = add_atomic_size_parameter (n, loc, function, params);
10535 return true;
10538 /* Otherwise, there is a match, so the call needs to be transformed from:
10539 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10540 into
10541 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10543 p0 = (*params)[0];
10544 p1 = (*params)[1];
10545 p2 = (*params)[2];
10547 /* Create pointer to appropriate size. */
10548 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10549 I_type_ptr = build_pointer_type (I_type);
10551 /* Convert object pointer to required type. */
10552 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10553 (*params)[0] = p0;
10555 /* Convert expected pointer to required type. */
10556 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10557 (*params)[1] = p1;
10559 /* Convert desired value to required type, and dereference it. */
10560 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10561 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10562 (*params)[2] = p2;
10564 /* The rest of the parameters are fine. NULL means no special return value
10565 processing.*/
10566 *new_return = NULL;
10567 return false;
10571 /* This will process an __atomic_load function call, determine whether it
10572 needs to be mapped to the _N variation, or turned into a library call.
10573 LOC is the location of the builtin call.
10574 FUNCTION is the DECL that has been invoked;
10575 PARAMS is the argument list for the call. The return value is non-null
10576 TRUE is returned if it is translated into the proper format for a call to the
10577 external library, and NEW_RETURN is set the tree for that function.
10578 FALSE is returned if processing for the _N variation is required, and
10579 NEW_RETURN is set to the the return value the result is copied into. */
10581 static bool
10582 resolve_overloaded_atomic_load (location_t loc, tree function,
10583 vec<tree, va_gc> *params, tree *new_return)
10585 tree p0, p1, p2;
10586 tree I_type, I_type_ptr;
10587 int n = get_atomic_generic_size (loc, function, params);
10589 /* Size of 0 is an error condition. */
10590 if (n == 0)
10592 *new_return = error_mark_node;
10593 return true;
10596 /* If not a lock-free size, change to the library generic format. */
10597 if (!atomic_size_supported_p (n))
10599 *new_return = add_atomic_size_parameter (n, loc, function, params);
10600 return true;
10603 /* Otherwise, there is a match, so the call needs to be transformed from:
10604 void fn(T* mem, T* return, model)
10605 into
10606 *return = (T) (fn ((In *) mem, model)) */
10608 p0 = (*params)[0];
10609 p1 = (*params)[1];
10610 p2 = (*params)[2];
10612 /* Create pointer to appropriate size. */
10613 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10614 I_type_ptr = build_pointer_type (I_type);
10616 /* Convert object pointer to required type. */
10617 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10618 (*params)[0] = p0;
10620 /* Move memory model to the 2nd position, and end param list. */
10621 (*params)[1] = p2;
10622 params->truncate (2);
10624 /* Convert return pointer and dereference it for later assignment. */
10625 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10627 return false;
10631 /* This will process an __atomic_store function call, determine whether it
10632 needs to be mapped to the _N variation, or turned into a library call.
10633 LOC is the location of the builtin call.
10634 FUNCTION is the DECL that has been invoked;
10635 PARAMS is the argument list for the call. The return value is non-null
10636 TRUE is returned if it is translated into the proper format for a call to the
10637 external library, and NEW_RETURN is set the tree for that function.
10638 FALSE is returned if processing for the _N variation is required, and
10639 NEW_RETURN is set to the the return value the result is copied into. */
10641 static bool
10642 resolve_overloaded_atomic_store (location_t loc, tree function,
10643 vec<tree, va_gc> *params, tree *new_return)
10645 tree p0, p1;
10646 tree I_type, I_type_ptr;
10647 int n = get_atomic_generic_size (loc, function, params);
10649 /* Size of 0 is an error condition. */
10650 if (n == 0)
10652 *new_return = error_mark_node;
10653 return true;
10656 /* If not a lock-free size, change to the library generic format. */
10657 if (!atomic_size_supported_p (n))
10659 *new_return = add_atomic_size_parameter (n, loc, function, params);
10660 return true;
10663 /* Otherwise, there is a match, so the call needs to be transformed from:
10664 void fn(T* mem, T* value, model)
10665 into
10666 fn ((In *) mem, (In) *value, model) */
10668 p0 = (*params)[0];
10669 p1 = (*params)[1];
10671 /* Create pointer to appropriate size. */
10672 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10673 I_type_ptr = build_pointer_type (I_type);
10675 /* Convert object pointer to required type. */
10676 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10677 (*params)[0] = p0;
10679 /* Convert new value to required type, and dereference it. */
10680 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10681 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10682 (*params)[1] = p1;
10684 /* The memory model is in the right spot already. Return is void. */
10685 *new_return = NULL_TREE;
10687 return false;
10691 /* Some builtin functions are placeholders for other expressions. This
10692 function should be called immediately after parsing the call expression
10693 before surrounding code has committed to the type of the expression.
10695 LOC is the location of the builtin call.
10697 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10698 PARAMS is the argument list for the call. The return value is non-null
10699 when expansion is complete, and null if normal processing should
10700 continue. */
10702 tree
10703 resolve_overloaded_builtin (location_t loc, tree function,
10704 vec<tree, va_gc> *params)
10706 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10707 bool orig_format = true;
10708 tree new_return = NULL_TREE;
10710 switch (DECL_BUILT_IN_CLASS (function))
10712 case BUILT_IN_NORMAL:
10713 break;
10714 case BUILT_IN_MD:
10715 if (targetm.resolve_overloaded_builtin)
10716 return targetm.resolve_overloaded_builtin (loc, function, params);
10717 else
10718 return NULL_TREE;
10719 default:
10720 return NULL_TREE;
10723 /* Handle BUILT_IN_NORMAL here. */
10724 switch (orig_code)
10726 case BUILT_IN_ATOMIC_EXCHANGE:
10727 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10728 case BUILT_IN_ATOMIC_LOAD:
10729 case BUILT_IN_ATOMIC_STORE:
10731 /* Handle these 4 together so that they can fall through to the next
10732 case if the call is transformed to an _N variant. */
10733 switch (orig_code)
10735 case BUILT_IN_ATOMIC_EXCHANGE:
10737 if (resolve_overloaded_atomic_exchange (loc, function, params,
10738 &new_return))
10739 return new_return;
10740 /* Change to the _N variant. */
10741 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10742 break;
10745 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10747 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10748 params,
10749 &new_return))
10750 return new_return;
10751 /* Change to the _N variant. */
10752 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10753 break;
10755 case BUILT_IN_ATOMIC_LOAD:
10757 if (resolve_overloaded_atomic_load (loc, function, params,
10758 &new_return))
10759 return new_return;
10760 /* Change to the _N variant. */
10761 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10762 break;
10764 case BUILT_IN_ATOMIC_STORE:
10766 if (resolve_overloaded_atomic_store (loc, function, params,
10767 &new_return))
10768 return new_return;
10769 /* Change to the _N variant. */
10770 orig_code = BUILT_IN_ATOMIC_STORE_N;
10771 break;
10773 default:
10774 gcc_unreachable ();
10776 /* Fallthrough to the normal processing. */
10778 case BUILT_IN_ATOMIC_EXCHANGE_N:
10779 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10780 case BUILT_IN_ATOMIC_LOAD_N:
10781 case BUILT_IN_ATOMIC_STORE_N:
10782 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10783 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10784 case BUILT_IN_ATOMIC_AND_FETCH_N:
10785 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10786 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10787 case BUILT_IN_ATOMIC_OR_FETCH_N:
10788 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10789 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10790 case BUILT_IN_ATOMIC_FETCH_AND_N:
10791 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10792 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10793 case BUILT_IN_ATOMIC_FETCH_OR_N:
10795 orig_format = false;
10796 /* Fallthru for parameter processing. */
10798 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10799 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10800 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10801 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10802 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10803 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10804 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10805 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10806 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10807 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10808 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10809 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10810 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10811 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10812 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10813 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10815 int n = sync_resolve_size (function, params);
10816 tree new_function, first_param, result;
10817 enum built_in_function fncode;
10819 if (n == 0)
10820 return error_mark_node;
10822 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10823 new_function = builtin_decl_explicit (fncode);
10824 if (!sync_resolve_params (loc, function, new_function, params,
10825 orig_format))
10826 return error_mark_node;
10828 first_param = (*params)[0];
10829 result = build_function_call_vec (loc, new_function, params, NULL);
10830 if (result == error_mark_node)
10831 return result;
10832 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10833 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10834 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10835 result = sync_resolve_return (first_param, result, orig_format);
10837 /* If new_return is set, assign function to that expr and cast the
10838 result to void since the generic interface returned void. */
10839 if (new_return)
10841 /* Cast function result from I{1,2,4,8,16} to the required type. */
10842 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10843 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10844 result);
10845 TREE_SIDE_EFFECTS (result) = 1;
10846 protected_set_expr_location (result, loc);
10847 result = convert (void_type_node, result);
10849 return result;
10852 default:
10853 return NULL_TREE;
10857 /* vector_types_compatible_elements_p is used in type checks of vectors
10858 values used as operands of binary operators. Where it returns true, and
10859 the other checks of the caller succeed (being vector types in he first
10860 place, and matching number of elements), we can just treat the types
10861 as essentially the same.
10862 Contrast with vector_targets_convertible_p, which is used for vector
10863 pointer types, and vector_types_convertible_p, which will allow
10864 language-specific matches under the control of flag_lax_vector_conversions,
10865 and might still require a conversion. */
10866 /* True if vector types T1 and T2 can be inputs to the same binary
10867 operator without conversion.
10868 We don't check the overall vector size here because some of our callers
10869 want to give different error messages when the vectors are compatible
10870 except for the element count. */
10872 bool
10873 vector_types_compatible_elements_p (tree t1, tree t2)
10875 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10876 t1 = TREE_TYPE (t1);
10877 t2 = TREE_TYPE (t2);
10879 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10881 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10882 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10883 || c2 == FIXED_POINT_TYPE));
10885 t1 = c_common_signed_type (t1);
10886 t2 = c_common_signed_type (t2);
10887 /* Equality works here because c_common_signed_type uses
10888 TYPE_MAIN_VARIANT. */
10889 if (t1 == t2)
10890 return true;
10891 if (opaque && c1 == c2
10892 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10893 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10894 return true;
10895 return false;
10898 /* Check for missing format attributes on function pointers. LTYPE is
10899 the new type or left-hand side type. RTYPE is the old type or
10900 right-hand side type. Returns TRUE if LTYPE is missing the desired
10901 attribute. */
10903 bool
10904 check_missing_format_attribute (tree ltype, tree rtype)
10906 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10907 tree ra;
10909 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10910 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10911 break;
10912 if (ra)
10914 tree la;
10915 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10916 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10917 break;
10918 return !la;
10920 else
10921 return false;
10924 /* Subscripting with type char is likely to lose on a machine where
10925 chars are signed. So warn on any machine, but optionally. Don't
10926 warn for unsigned char since that type is safe. Don't warn for
10927 signed char because anyone who uses that must have done so
10928 deliberately. Furthermore, we reduce the false positive load by
10929 warning only for non-constant value of type char. */
10931 void
10932 warn_array_subscript_with_type_char (tree index)
10934 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10935 && TREE_CODE (index) != INTEGER_CST)
10936 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10939 /* Implement -Wparentheses for the unexpected C precedence rules, to
10940 cover cases like x + y << z which readers are likely to
10941 misinterpret. We have seen an expression in which CODE is a binary
10942 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10943 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10944 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10945 expression was not formed using a binary or unary operator, or it
10946 was enclosed in parentheses. */
10948 void
10949 warn_about_parentheses (location_t loc, enum tree_code code,
10950 enum tree_code code_left, tree arg_left,
10951 enum tree_code code_right, tree arg_right)
10953 if (!warn_parentheses)
10954 return;
10956 /* This macro tests that the expression ARG with original tree code
10957 CODE appears to be a boolean expression. or the result of folding a
10958 boolean expression. */
10959 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10960 (truth_value_p (TREE_CODE (ARG)) \
10961 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10962 /* Folding may create 0 or 1 integers from other expressions. */ \
10963 || ((CODE) != INTEGER_CST \
10964 && (integer_onep (ARG) || integer_zerop (ARG))))
10966 switch (code)
10968 case LSHIFT_EXPR:
10969 if (code_left == PLUS_EXPR)
10970 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10971 "suggest parentheses around %<+%> inside %<<<%>");
10972 else if (code_right == PLUS_EXPR)
10973 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10974 "suggest parentheses around %<+%> inside %<<<%>");
10975 else if (code_left == MINUS_EXPR)
10976 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10977 "suggest parentheses around %<-%> inside %<<<%>");
10978 else if (code_right == MINUS_EXPR)
10979 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10980 "suggest parentheses around %<-%> inside %<<<%>");
10981 return;
10983 case RSHIFT_EXPR:
10984 if (code_left == PLUS_EXPR)
10985 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10986 "suggest parentheses around %<+%> inside %<>>%>");
10987 else if (code_right == PLUS_EXPR)
10988 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10989 "suggest parentheses around %<+%> inside %<>>%>");
10990 else if (code_left == MINUS_EXPR)
10991 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10992 "suggest parentheses around %<-%> inside %<>>%>");
10993 else if (code_right == MINUS_EXPR)
10994 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10995 "suggest parentheses around %<-%> inside %<>>%>");
10996 return;
10998 case TRUTH_ORIF_EXPR:
10999 if (code_left == TRUTH_ANDIF_EXPR)
11000 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11001 "suggest parentheses around %<&&%> within %<||%>");
11002 else if (code_right == TRUTH_ANDIF_EXPR)
11003 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11004 "suggest parentheses around %<&&%> within %<||%>");
11005 return;
11007 case BIT_IOR_EXPR:
11008 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11009 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11010 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11011 "suggest parentheses around arithmetic in operand of %<|%>");
11012 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11013 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11014 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11015 "suggest parentheses around arithmetic in operand of %<|%>");
11016 /* Check cases like x|y==z */
11017 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11018 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11019 "suggest parentheses around comparison in operand of %<|%>");
11020 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11021 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11022 "suggest parentheses around comparison in operand of %<|%>");
11023 /* Check cases like !x | y */
11024 else if (code_left == TRUTH_NOT_EXPR
11025 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11026 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11027 "suggest parentheses around operand of "
11028 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11029 return;
11031 case BIT_XOR_EXPR:
11032 if (code_left == BIT_AND_EXPR
11033 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11034 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11035 "suggest parentheses around arithmetic in operand of %<^%>");
11036 else if (code_right == BIT_AND_EXPR
11037 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11038 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11039 "suggest parentheses around arithmetic in operand of %<^%>");
11040 /* Check cases like x^y==z */
11041 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11042 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11043 "suggest parentheses around comparison in operand of %<^%>");
11044 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11045 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11046 "suggest parentheses around comparison in operand of %<^%>");
11047 return;
11049 case BIT_AND_EXPR:
11050 if (code_left == PLUS_EXPR)
11051 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11052 "suggest parentheses around %<+%> in operand of %<&%>");
11053 else if (code_right == PLUS_EXPR)
11054 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11055 "suggest parentheses around %<+%> in operand of %<&%>");
11056 else if (code_left == MINUS_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 == MINUS_EXPR)
11060 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11061 "suggest parentheses around %<-%> in operand of %<&%>");
11062 /* Check cases like x&y==z */
11063 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11064 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11065 "suggest parentheses around comparison in operand of %<&%>");
11066 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11067 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11068 "suggest parentheses around comparison in operand of %<&%>");
11069 /* Check cases like !x & y */
11070 else if (code_left == TRUTH_NOT_EXPR
11071 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11072 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11073 "suggest parentheses around operand of "
11074 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11075 return;
11077 case EQ_EXPR:
11078 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11079 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11080 "suggest parentheses around comparison in operand of %<==%>");
11081 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11082 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11083 "suggest parentheses around comparison in operand of %<==%>");
11084 return;
11085 case NE_EXPR:
11086 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11087 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11088 "suggest parentheses around comparison in operand of %<!=%>");
11089 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11090 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11091 "suggest parentheses around comparison in operand of %<!=%>");
11092 return;
11094 default:
11095 if (TREE_CODE_CLASS (code) == tcc_comparison)
11097 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11098 && code_left != NE_EXPR && code_left != EQ_EXPR
11099 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11100 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11101 "comparisons like %<X<=Y<=Z%> do not "
11102 "have their mathematical meaning");
11103 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11104 && code_right != NE_EXPR && code_right != EQ_EXPR
11105 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11106 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11107 "comparisons like %<X<=Y<=Z%> do not "
11108 "have their mathematical meaning");
11110 return;
11112 #undef NOT_A_BOOLEAN_EXPR_P
11115 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11117 void
11118 warn_for_unused_label (tree label)
11120 if (!TREE_USED (label))
11122 if (DECL_INITIAL (label))
11123 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11124 else
11125 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11129 /* Warn for division by zero according to the value of DIVISOR. LOC
11130 is the location of the division operator. */
11132 void
11133 warn_for_div_by_zero (location_t loc, tree divisor)
11135 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11136 about division by zero. Do not issue a warning if DIVISOR has a
11137 floating-point type, since we consider 0.0/0.0 a valid way of
11138 generating a NaN. */
11139 if (c_inhibit_evaluation_warnings == 0
11140 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11141 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11144 /* Subroutine of build_binary_op. Give warnings for comparisons
11145 between signed and unsigned quantities that may fail. Do the
11146 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11147 so that casts will be considered, but default promotions won't
11150 LOCATION is the location of the comparison operator.
11152 The arguments of this function map directly to local variables
11153 of build_binary_op. */
11155 void
11156 warn_for_sign_compare (location_t location,
11157 tree orig_op0, tree orig_op1,
11158 tree op0, tree op1,
11159 tree result_type, enum tree_code resultcode)
11161 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11162 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11163 int unsignedp0, unsignedp1;
11165 /* In C++, check for comparison of different enum types. */
11166 if (c_dialect_cxx()
11167 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11168 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11169 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11170 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11172 warning_at (location,
11173 OPT_Wsign_compare, "comparison between types %qT and %qT",
11174 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11177 /* Do not warn if the comparison is being done in a signed type,
11178 since the signed type will only be chosen if it can represent
11179 all the values of the unsigned type. */
11180 if (!TYPE_UNSIGNED (result_type))
11181 /* OK */;
11182 /* Do not warn if both operands are unsigned. */
11183 else if (op0_signed == op1_signed)
11184 /* OK */;
11185 else
11187 tree sop, uop, base_type;
11188 bool ovf;
11190 if (op0_signed)
11191 sop = orig_op0, uop = orig_op1;
11192 else
11193 sop = orig_op1, uop = orig_op0;
11195 STRIP_TYPE_NOPS (sop);
11196 STRIP_TYPE_NOPS (uop);
11197 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11198 ? TREE_TYPE (result_type) : result_type);
11200 /* Do not warn if the signed quantity is an unsuffixed integer
11201 literal (or some static constant expression involving such
11202 literals or a conditional expression involving such literals)
11203 and it is non-negative. */
11204 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11205 /* OK */;
11206 /* Do not warn if the comparison is an equality operation, the
11207 unsigned quantity is an integral constant, and it would fit
11208 in the result if the result were signed. */
11209 else if (TREE_CODE (uop) == INTEGER_CST
11210 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11211 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11212 /* OK */;
11213 /* In C, do not warn if the unsigned quantity is an enumeration
11214 constant and its maximum value would fit in the result if the
11215 result were signed. */
11216 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11217 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11218 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11219 c_common_signed_type (base_type)))
11220 /* OK */;
11221 else
11222 warning_at (location,
11223 OPT_Wsign_compare,
11224 "comparison between signed and unsigned integer expressions");
11227 /* Warn if two unsigned values are being compared in a size larger
11228 than their original size, and one (and only one) is the result of
11229 a `~' operator. This comparison will always fail.
11231 Also warn if one operand is a constant, and the constant does not
11232 have all bits set that are set in the ~ operand when it is
11233 extended. */
11235 op0 = c_common_get_narrower (op0, &unsignedp0);
11236 op1 = c_common_get_narrower (op1, &unsignedp1);
11238 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11239 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11241 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11242 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11243 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11244 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11246 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11248 tree primop;
11249 HOST_WIDE_INT constant, mask;
11250 int unsignedp;
11251 unsigned int bits;
11253 if (host_integerp (op0, 0))
11255 primop = op1;
11256 unsignedp = unsignedp1;
11257 constant = tree_low_cst (op0, 0);
11259 else
11261 primop = op0;
11262 unsignedp = unsignedp0;
11263 constant = tree_low_cst (op1, 0);
11266 bits = TYPE_PRECISION (TREE_TYPE (primop));
11267 if (bits < TYPE_PRECISION (result_type)
11268 && bits < HOST_BITS_PER_LONG && unsignedp)
11270 mask = (~ (HOST_WIDE_INT) 0) << bits;
11271 if ((mask & constant) != mask)
11273 if (constant == 0)
11274 warning (OPT_Wsign_compare,
11275 "promoted ~unsigned is always non-zero");
11276 else
11277 warning_at (location, OPT_Wsign_compare,
11278 "comparison of promoted ~unsigned with constant");
11282 else if (unsignedp0 && unsignedp1
11283 && (TYPE_PRECISION (TREE_TYPE (op0))
11284 < TYPE_PRECISION (result_type))
11285 && (TYPE_PRECISION (TREE_TYPE (op1))
11286 < TYPE_PRECISION (result_type)))
11287 warning_at (location, OPT_Wsign_compare,
11288 "comparison of promoted ~unsigned with unsigned");
11292 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11293 type via c_common_type. If -Wdouble-promotion is in use, and the
11294 conditions for warning have been met, issue a warning. GMSGID is
11295 the warning message. It must have two %T specifiers for the type
11296 that was converted (generally "float") and the type to which it was
11297 converted (generally "double), respectively. LOC is the location
11298 to which the awrning should refer. */
11300 void
11301 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11302 const char *gmsgid, location_t loc)
11304 tree source_type;
11306 if (!warn_double_promotion)
11307 return;
11308 /* If the conversion will not occur at run-time, there is no need to
11309 warn about it. */
11310 if (c_inhibit_evaluation_warnings)
11311 return;
11312 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11313 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11314 return;
11315 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11316 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11317 source_type = type1;
11318 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11319 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11320 source_type = type2;
11321 else
11322 return;
11323 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11326 /* Setup a TYPE_DECL node as a typedef representation.
11328 X is a TYPE_DECL for a typedef statement. Create a brand new
11329 ..._TYPE node (which will be just a variant of the existing
11330 ..._TYPE node with identical properties) and then install X
11331 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11333 The whole point here is to end up with a situation where each
11334 and every ..._TYPE node the compiler creates will be uniquely
11335 associated with AT MOST one node representing a typedef name.
11336 This way, even though the compiler substitutes corresponding
11337 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11338 early on, later parts of the compiler can always do the reverse
11339 translation and get back the corresponding typedef name. For
11340 example, given:
11342 typedef struct S MY_TYPE;
11343 MY_TYPE object;
11345 Later parts of the compiler might only know that `object' was of
11346 type `struct S' if it were not for code just below. With this
11347 code however, later parts of the compiler see something like:
11349 struct S' == struct S
11350 typedef struct S' MY_TYPE;
11351 struct S' object;
11353 And they can then deduce (from the node for type struct S') that
11354 the original object declaration was:
11356 MY_TYPE object;
11358 Being able to do this is important for proper support of protoize,
11359 and also for generating precise symbolic debugging information
11360 which takes full account of the programmer's (typedef) vocabulary.
11362 Obviously, we don't want to generate a duplicate ..._TYPE node if
11363 the TYPE_DECL node that we are now processing really represents a
11364 standard built-in type. */
11366 void
11367 set_underlying_type (tree x)
11369 if (x == error_mark_node)
11370 return;
11371 if (DECL_IS_BUILTIN (x))
11373 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11374 TYPE_NAME (TREE_TYPE (x)) = x;
11376 else if (TREE_TYPE (x) != error_mark_node
11377 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11379 tree tt = TREE_TYPE (x);
11380 DECL_ORIGINAL_TYPE (x) = tt;
11381 tt = build_variant_type_copy (tt);
11382 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11383 TYPE_NAME (tt) = x;
11384 TREE_USED (tt) = TREE_USED (x);
11385 TREE_TYPE (x) = tt;
11389 /* Record the types used by the current global variable declaration
11390 being parsed, so that we can decide later to emit their debug info.
11391 Those types are in types_used_by_cur_var_decl, and we are going to
11392 store them in the types_used_by_vars_hash hash table.
11393 DECL is the declaration of the global variable that has been parsed. */
11395 void
11396 record_types_used_by_current_var_decl (tree decl)
11398 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11400 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11402 tree type = types_used_by_cur_var_decl->pop ();
11403 types_used_by_var_decl_insert (type, decl);
11407 /* If DECL is a typedef that is declared in the current function,
11408 record it for the purpose of -Wunused-local-typedefs. */
11410 void
11411 record_locally_defined_typedef (tree decl)
11413 struct c_language_function *l;
11415 if (!warn_unused_local_typedefs
11416 || cfun == NULL
11417 /* if this is not a locally defined typedef then we are not
11418 interested. */
11419 || !is_typedef_decl (decl)
11420 || !decl_function_context (decl))
11421 return;
11423 l = (struct c_language_function *) cfun->language;
11424 vec_safe_push (l->local_typedefs, decl);
11427 /* If T is a TYPE_DECL declared locally, mark it as used. */
11429 void
11430 maybe_record_typedef_use (tree t)
11432 if (!is_typedef_decl (t))
11433 return;
11435 TREE_USED (t) = true;
11438 /* Warn if there are some unused locally defined typedefs in the
11439 current function. */
11441 void
11442 maybe_warn_unused_local_typedefs (void)
11444 int i;
11445 tree decl;
11446 /* The number of times we have emitted -Wunused-local-typedefs
11447 warnings. If this is different from errorcount, that means some
11448 unrelated errors have been issued. In which case, we'll avoid
11449 emitting "unused-local-typedefs" warnings. */
11450 static int unused_local_typedefs_warn_count;
11451 struct c_language_function *l;
11453 if (cfun == NULL)
11454 return;
11456 if ((l = (struct c_language_function *) cfun->language) == NULL)
11457 return;
11459 if (warn_unused_local_typedefs
11460 && errorcount == unused_local_typedefs_warn_count)
11462 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11463 if (!TREE_USED (decl))
11464 warning_at (DECL_SOURCE_LOCATION (decl),
11465 OPT_Wunused_local_typedefs,
11466 "typedef %qD locally defined but not used", decl);
11467 unused_local_typedefs_warn_count = errorcount;
11470 vec_free (l->local_typedefs);
11473 /* The C and C++ parsers both use vectors to hold function arguments.
11474 For efficiency, we keep a cache of unused vectors. This is the
11475 cache. */
11477 typedef vec<tree, va_gc> *tree_gc_vec;
11478 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11480 /* Return a new vector from the cache. If the cache is empty,
11481 allocate a new vector. These vectors are GC'ed, so it is OK if the
11482 pointer is not released.. */
11484 vec<tree, va_gc> *
11485 make_tree_vector (void)
11487 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11488 return tree_vector_cache->pop ();
11489 else
11491 /* Passing 0 to vec::alloc returns NULL, and our callers require
11492 that we always return a non-NULL value. The vector code uses
11493 4 when growing a NULL vector, so we do too. */
11494 vec<tree, va_gc> *v;
11495 vec_alloc (v, 4);
11496 return v;
11500 /* Release a vector of trees back to the cache. */
11502 void
11503 release_tree_vector (vec<tree, va_gc> *vec)
11505 if (vec != NULL)
11507 vec->truncate (0);
11508 vec_safe_push (tree_vector_cache, vec);
11512 /* Get a new tree vector holding a single tree. */
11514 vec<tree, va_gc> *
11515 make_tree_vector_single (tree t)
11517 vec<tree, va_gc> *ret = make_tree_vector ();
11518 ret->quick_push (t);
11519 return ret;
11522 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11524 vec<tree, va_gc> *
11525 make_tree_vector_from_list (tree list)
11527 vec<tree, va_gc> *ret = make_tree_vector ();
11528 for (; list; list = TREE_CHAIN (list))
11529 vec_safe_push (ret, TREE_VALUE (list));
11530 return ret;
11533 /* Get a new tree vector which is a copy of an existing one. */
11535 vec<tree, va_gc> *
11536 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11538 vec<tree, va_gc> *ret;
11539 unsigned int ix;
11540 tree t;
11542 ret = make_tree_vector ();
11543 vec_safe_reserve (ret, vec_safe_length (orig));
11544 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11545 ret->quick_push (t);
11546 return ret;
11549 /* Return true if KEYWORD starts a type specifier. */
11551 bool
11552 keyword_begins_type_specifier (enum rid keyword)
11554 switch (keyword)
11556 case RID_AUTO_TYPE:
11557 case RID_INT:
11558 case RID_CHAR:
11559 case RID_FLOAT:
11560 case RID_DOUBLE:
11561 case RID_VOID:
11562 case RID_INT128:
11563 case RID_UNSIGNED:
11564 case RID_LONG:
11565 case RID_SHORT:
11566 case RID_SIGNED:
11567 case RID_DFLOAT32:
11568 case RID_DFLOAT64:
11569 case RID_DFLOAT128:
11570 case RID_FRACT:
11571 case RID_ACCUM:
11572 case RID_BOOL:
11573 case RID_WCHAR:
11574 case RID_CHAR16:
11575 case RID_CHAR32:
11576 case RID_SAT:
11577 case RID_COMPLEX:
11578 case RID_TYPEOF:
11579 case RID_STRUCT:
11580 case RID_CLASS:
11581 case RID_UNION:
11582 case RID_ENUM:
11583 return true;
11584 default:
11585 return false;
11589 /* Return true if KEYWORD names a type qualifier. */
11591 bool
11592 keyword_is_type_qualifier (enum rid keyword)
11594 switch (keyword)
11596 case RID_CONST:
11597 case RID_VOLATILE:
11598 case RID_RESTRICT:
11599 case RID_ATOMIC:
11600 return true;
11601 default:
11602 return false;
11606 /* Return true if KEYWORD names a storage class specifier.
11608 RID_TYPEDEF is not included in this list despite `typedef' being
11609 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11610 such for syntactic convenience only. */
11612 bool
11613 keyword_is_storage_class_specifier (enum rid keyword)
11615 switch (keyword)
11617 case RID_STATIC:
11618 case RID_EXTERN:
11619 case RID_REGISTER:
11620 case RID_AUTO:
11621 case RID_MUTABLE:
11622 case RID_THREAD:
11623 return true;
11624 default:
11625 return false;
11629 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11631 static bool
11632 keyword_is_function_specifier (enum rid keyword)
11634 switch (keyword)
11636 case RID_INLINE:
11637 case RID_NORETURN:
11638 case RID_VIRTUAL:
11639 case RID_EXPLICIT:
11640 return true;
11641 default:
11642 return false;
11646 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11647 declaration-specifier (C99 6.7). */
11649 bool
11650 keyword_is_decl_specifier (enum rid keyword)
11652 if (keyword_is_storage_class_specifier (keyword)
11653 || keyword_is_type_qualifier (keyword)
11654 || keyword_is_function_specifier (keyword))
11655 return true;
11657 switch (keyword)
11659 case RID_TYPEDEF:
11660 case RID_FRIEND:
11661 case RID_CONSTEXPR:
11662 return true;
11663 default:
11664 return false;
11668 /* Initialize language-specific-bits of tree_contains_struct. */
11670 void
11671 c_common_init_ts (void)
11673 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11674 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11675 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11678 /* Build a user-defined numeric literal out of an integer constant type VALUE
11679 with identifier SUFFIX. */
11681 tree
11682 build_userdef_literal (tree suffix_id, tree value,
11683 enum overflow_type overflow, tree num_string)
11685 tree literal = make_node (USERDEF_LITERAL);
11686 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11687 USERDEF_LITERAL_VALUE (literal) = value;
11688 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11689 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11690 return literal;
11693 /* For vector[index], convert the vector to a
11694 pointer of the underlying type. */
11695 void
11696 convert_vector_to_pointer_for_subscript (location_t loc,
11697 tree* vecp, tree index)
11699 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11701 tree type = TREE_TYPE (*vecp);
11702 tree type1;
11704 if (TREE_CODE (index) == INTEGER_CST)
11705 if (!host_integerp (index, 1)
11706 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11707 >= TYPE_VECTOR_SUBPARTS (type)))
11708 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11710 c_common_mark_addressable_vec (*vecp);
11711 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11712 type = build_pointer_type (type);
11713 type1 = build_pointer_type (TREE_TYPE (*vecp));
11714 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11715 *vecp = convert (type, *vecp);
11719 /* Determine which of the operands, if any, is a scalar that needs to be
11720 converted to a vector, for the range of operations. */
11721 enum stv_conv
11722 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11723 bool complain)
11725 tree type0 = TREE_TYPE (op0);
11726 tree type1 = TREE_TYPE (op1);
11727 bool integer_only_op = false;
11728 enum stv_conv ret = stv_firstarg;
11730 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11731 || TREE_CODE (type1) == VECTOR_TYPE);
11732 switch (code)
11734 /* Most GENERIC binary expressions require homogeneous arguments.
11735 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11736 argument that is a vector and a second one that is a scalar, so
11737 we never return stv_secondarg for them. */
11738 case RSHIFT_EXPR:
11739 case LSHIFT_EXPR:
11740 if (TREE_CODE (type0) == INTEGER_TYPE
11741 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11743 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11745 if (complain)
11746 error_at (loc, "conversion of scalar %qT to vector %qT "
11747 "involves truncation", type0, type1);
11748 return stv_error;
11750 else
11751 return stv_firstarg;
11753 break;
11755 case BIT_IOR_EXPR:
11756 case BIT_XOR_EXPR:
11757 case BIT_AND_EXPR:
11758 integer_only_op = true;
11759 /* ... fall through ... */
11761 case VEC_COND_EXPR:
11763 case PLUS_EXPR:
11764 case MINUS_EXPR:
11765 case MULT_EXPR:
11766 case TRUNC_DIV_EXPR:
11767 case CEIL_DIV_EXPR:
11768 case FLOOR_DIV_EXPR:
11769 case ROUND_DIV_EXPR:
11770 case EXACT_DIV_EXPR:
11771 case TRUNC_MOD_EXPR:
11772 case FLOOR_MOD_EXPR:
11773 case RDIV_EXPR:
11774 case EQ_EXPR:
11775 case NE_EXPR:
11776 case LE_EXPR:
11777 case GE_EXPR:
11778 case LT_EXPR:
11779 case GT_EXPR:
11780 /* What about UNLT_EXPR? */
11781 if (TREE_CODE (type0) == VECTOR_TYPE)
11783 tree tmp;
11784 ret = stv_secondarg;
11785 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11786 tmp = type0; type0 = type1; type1 = tmp;
11787 tmp = op0; op0 = op1; op1 = tmp;
11790 if (TREE_CODE (type0) == INTEGER_TYPE
11791 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11793 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11795 if (complain)
11796 error_at (loc, "conversion of scalar %qT to vector %qT "
11797 "involves truncation", type0, type1);
11798 return stv_error;
11800 return ret;
11802 else if (!integer_only_op
11803 /* Allow integer --> real conversion if safe. */
11804 && (TREE_CODE (type0) == REAL_TYPE
11805 || TREE_CODE (type0) == INTEGER_TYPE)
11806 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11808 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11810 if (complain)
11811 error_at (loc, "conversion of scalar %qT to vector %qT "
11812 "involves truncation", type0, type1);
11813 return stv_error;
11815 return ret;
11817 default:
11818 break;
11821 return stv_nothing;
11824 /* Return true iff ALIGN is an integral constant that is a fundamental
11825 alignment, as defined by [basic.align] in the c++-11
11826 specifications.
11828 That is:
11830 [A fundamental alignment is represented by an alignment less than or
11831 equal to the greatest alignment supported by the implementation
11832 in all contexts, which is equal to
11833 alignof(max_align_t)]. */
11835 bool
11836 cxx_fundamental_alignment_p (unsigned align)
11838 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11839 TYPE_ALIGN (long_double_type_node)));
11842 #include "gt-c-family-c-common.h"