acpi: Narrow workaround for broken interrupt settings
[dragonfly.git] / contrib / gcc-4.7 / gcc / c-family / c-common.c
blob82a2b8d6cd22b19f130a329c2bbeb48abe81430e
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "intl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "output.h"
30 #include "c-pragma.h"
31 #include "ggc.h"
32 #include "c-common.h"
33 #include "c-objc.h"
34 #include "tm_p.h"
35 #include "obstack.h"
36 #include "cpplib.h"
37 #include "target.h"
38 #include "common/common-target.h"
39 #include "langhooks.h"
40 #include "tree-inline.h"
41 #include "toplev.h"
42 #include "diagnostic.h"
43 #include "tree-iterator.h"
44 #include "hashtab.h"
45 #include "tree-mudflap.h"
46 #include "opts.h"
47 #include "cgraph.h"
48 #include "target-def.h"
49 #include "libfuncs.h"
51 cpp_reader *parse_in; /* Declared in c-pragma.h. */
53 /* The following symbols are subsumed in the c_global_trees array, and
54 listed here individually for documentation purposes.
56 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
58 tree short_integer_type_node;
59 tree long_integer_type_node;
60 tree long_long_integer_type_node;
61 tree int128_integer_type_node;
63 tree short_unsigned_type_node;
64 tree long_unsigned_type_node;
65 tree long_long_unsigned_type_node;
66 tree int128_unsigned_type_node;
68 tree truthvalue_type_node;
69 tree truthvalue_false_node;
70 tree truthvalue_true_node;
72 tree ptrdiff_type_node;
74 tree unsigned_char_type_node;
75 tree signed_char_type_node;
76 tree wchar_type_node;
78 tree char16_type_node;
79 tree char32_type_node;
81 tree float_type_node;
82 tree double_type_node;
83 tree long_double_type_node;
85 tree complex_integer_type_node;
86 tree complex_float_type_node;
87 tree complex_double_type_node;
88 tree complex_long_double_type_node;
90 tree dfloat32_type_node;
91 tree dfloat64_type_node;
92 tree_dfloat128_type_node;
94 tree intQI_type_node;
95 tree intHI_type_node;
96 tree intSI_type_node;
97 tree intDI_type_node;
98 tree intTI_type_node;
100 tree unsigned_intQI_type_node;
101 tree unsigned_intHI_type_node;
102 tree unsigned_intSI_type_node;
103 tree unsigned_intDI_type_node;
104 tree unsigned_intTI_type_node;
106 tree widest_integer_literal_type_node;
107 tree widest_unsigned_literal_type_node;
109 Nodes for types `void *' and `const void *'.
111 tree ptr_type_node, const_ptr_type_node;
113 Nodes for types `char *' and `const char *'.
115 tree string_type_node, const_string_type_node;
117 Type `char[SOMENUMBER]'.
118 Used when an array of char is needed and the size is irrelevant.
120 tree char_array_type_node;
122 Type `int[SOMENUMBER]' or something like it.
123 Used when an array of int needed and the size is irrelevant.
125 tree int_array_type_node;
127 Type `wchar_t[SOMENUMBER]' or something like it.
128 Used when a wide string literal is created.
130 tree wchar_array_type_node;
132 Type `char16_t[SOMENUMBER]' or something like it.
133 Used when a UTF-16 string literal is created.
135 tree char16_array_type_node;
137 Type `char32_t[SOMENUMBER]' or something like it.
138 Used when a UTF-32 string literal is created.
140 tree char32_array_type_node;
142 Type `int ()' -- used for implicit declaration of functions.
144 tree default_function_type;
146 A VOID_TYPE node, packaged in a TREE_LIST.
148 tree void_list_node;
150 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
151 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
152 VAR_DECLS, but C++ does.)
154 tree function_name_decl_node;
155 tree pretty_function_name_decl_node;
156 tree c99_function_name_decl_node;
158 Stack of nested function name VAR_DECLs.
160 tree saved_function_name_decls;
164 tree c_global_trees[CTI_MAX];
166 /* Switches common to the C front ends. */
168 /* Nonzero means don't output line number information. */
170 char flag_no_line_commands;
172 /* Nonzero causes -E output not to be done, but directives such as
173 #define that have side effects are still obeyed. */
175 char flag_no_output;
177 /* Nonzero means dump macros in some fashion. */
179 char flag_dump_macros;
181 /* Nonzero means pass #include lines through to the output. */
183 char flag_dump_includes;
185 /* Nonzero means process PCH files while preprocessing. */
187 bool flag_pch_preprocess;
189 /* The file name to which we should write a precompiled header, or
190 NULL if no header will be written in this compile. */
192 const char *pch_file;
194 /* Nonzero if an ISO standard was selected. It rejects macros in the
195 user's namespace. */
196 int flag_iso;
198 /* Warn about #pragma directives that are not recognized. */
200 int warn_unknown_pragmas; /* Tri state variable. */
202 /* Warn about format/argument anomalies in calls to formatted I/O functions
203 (*printf, *scanf, strftime, strfmon, etc.). */
205 int warn_format;
207 /* C/ObjC language option variables. */
210 /* Nonzero means allow type mismatches in conditional expressions;
211 just make their values `void'. */
213 int flag_cond_mismatch;
215 /* Nonzero means enable C89 Amendment 1 features. */
217 int flag_isoc94;
219 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
221 int flag_isoc99;
223 /* Nonzero means use the ISO C11 dialect of C. */
225 int flag_isoc11;
227 /* Nonzero means that we have builtin functions, and main is an int. */
229 int flag_hosted = 1;
232 /* ObjC language option variables. */
235 /* Tells the compiler that this is a special run. Do not perform any
236 compiling, instead we are to test some platform dependent features
237 and output a C header file with appropriate definitions. */
239 int print_struct_values;
241 /* Tells the compiler what is the constant string class for ObjC. */
243 const char *constant_string_class_name;
246 /* C++ language option variables. */
249 /* Nonzero means generate separate instantiation control files and
250 juggle them at link time. */
252 int flag_use_repository;
254 /* The C++ dialect being used. C++98 is the default. */
256 enum cxx_dialect cxx_dialect = cxx98;
258 /* Maximum template instantiation depth. This limit exists to limit the
259 time it takes to notice excessively recursive template instantiations.
261 The default is lower than the 1024 recommended by the C++0x standard
262 because G++ runs out of stack before 1024 with highly recursive template
263 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
265 int max_tinst_depth = 900;
267 /* The elements of `ridpointers' are identifier nodes for the reserved
268 type names and storage classes. It is indexed by a RID_... value. */
269 tree *ridpointers;
271 tree (*make_fname_decl) (location_t, tree, int);
273 /* Nonzero means don't warn about problems that occur when the code is
274 executed. */
275 int c_inhibit_evaluation_warnings;
277 /* Whether we are building a boolean conversion inside
278 convert_for_assignment, or some other late binary operation. If
279 build_binary_op is called for C (from code shared by C and C++) in
280 this case, then the operands have already been folded and the
281 result will not be folded again, so C_MAYBE_CONST_EXPR should not
282 be generated. */
283 bool in_late_binary_op;
285 /* Whether lexing has been completed, so subsequent preprocessor
286 errors should use the compiler's input_location. */
287 bool done_lexing = false;
289 /* Information about how a function name is generated. */
290 struct fname_var_t
292 tree *const decl; /* pointer to the VAR_DECL. */
293 const unsigned rid; /* RID number for the identifier. */
294 const int pretty; /* How pretty is it? */
297 /* The three ways of getting then name of the current function. */
299 const struct fname_var_t fname_vars[] =
301 /* C99 compliant __func__, must be first. */
302 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
303 /* GCC __FUNCTION__ compliant. */
304 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
305 /* GCC __PRETTY_FUNCTION__ compliant. */
306 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
307 {NULL, 0, 0},
310 /* Global visibility options. */
311 struct visibility_flags visibility_options;
313 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
314 static tree check_case_value (tree);
315 static bool check_case_bounds (tree, tree, tree *, tree *);
317 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_always_inline_attribute (tree *, tree, tree, int,
327 bool *);
328 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
335 bool *);
336 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_transparent_union_attribute (tree *, tree, tree,
338 int, bool *);
339 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
345 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
346 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
349 static tree handle_visibility_attribute (tree *, tree, tree, int,
350 bool *);
351 static tree handle_tls_model_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_no_instrument_function_attribute (tree *, tree,
354 tree, int, bool *);
355 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_deprecated_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_vector_size_attribute (tree *, tree, tree, int,
366 bool *);
367 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
371 bool *);
372 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
377 static tree ignore_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
381 static void check_function_nonnull (tree, int, tree *);
382 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
383 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
384 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
385 static int resort_field_decl_cmp (const void *, const void *);
387 /* Reserved words. The third field is a mask: keywords are disabled
388 if they match the mask.
390 Masks for languages:
391 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
392 C --std=c99: D_CXXONLY | D_OBJC
393 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
394 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
395 C++ --std=c0x: D_CONLY | D_OBJC
396 ObjC++ is like C++ except that D_OBJC is not set
398 If -fno-asm is used, D_ASM is added to the mask. If
399 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
400 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
401 In C with -Wc++-compat, we warn if D_CXXWARN is set.
403 Note the complication of the D_CXX_OBJC keywords. These are
404 reserved words such as 'class'. In C++, 'class' is a reserved
405 word. In Objective-C++ it is too. In Objective-C, it is a
406 reserved word too, but only if it follows an '@' sign.
408 const struct c_common_resword c_common_reswords[] =
410 { "_Alignas", RID_ALIGNAS, D_CONLY },
411 { "_Alignof", RID_ALIGNOF, D_CONLY },
412 { "_Bool", RID_BOOL, D_CONLY },
413 { "_Complex", RID_COMPLEX, 0 },
414 { "_Imaginary", RID_IMAGINARY, D_CONLY },
415 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
416 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
417 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
418 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
419 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
420 { "_Sat", RID_SAT, D_CONLY | D_EXT },
421 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
422 { "_Noreturn", RID_NORETURN, D_CONLY },
423 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
424 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
425 { "__alignof", RID_ALIGNOF, 0 },
426 { "__alignof__", RID_ALIGNOF, 0 },
427 { "__asm", RID_ASM, 0 },
428 { "__asm__", RID_ASM, 0 },
429 { "__attribute", RID_ATTRIBUTE, 0 },
430 { "__attribute__", RID_ATTRIBUTE, 0 },
431 { "__bases", RID_BASES, D_CXXONLY },
432 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
433 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
434 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, D_CONLY },
435 { "__builtin_offsetof", RID_OFFSETOF, 0 },
436 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
437 { "__builtin_va_arg", RID_VA_ARG, 0 },
438 { "__complex", RID_COMPLEX, 0 },
439 { "__complex__", RID_COMPLEX, 0 },
440 { "__const", RID_CONST, 0 },
441 { "__const__", RID_CONST, 0 },
442 { "__decltype", RID_DECLTYPE, D_CXXONLY },
443 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
444 { "__extension__", RID_EXTENSION, 0 },
445 { "__func__", RID_C99_FUNCTION_NAME, 0 },
446 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
447 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
448 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
449 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
450 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
451 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
452 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
453 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
454 { "__imag", RID_IMAGPART, 0 },
455 { "__imag__", RID_IMAGPART, 0 },
456 { "__inline", RID_INLINE, 0 },
457 { "__inline__", RID_INLINE, 0 },
458 { "__int128", RID_INT128, 0 },
459 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
460 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
461 { "__is_class", RID_IS_CLASS, D_CXXONLY },
462 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
463 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
464 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
465 { "__is_final", RID_IS_FINAL, D_CXXONLY },
466 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
467 { "__is_pod", RID_IS_POD, D_CXXONLY },
468 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
469 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
470 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
471 { "__is_union", RID_IS_UNION, D_CXXONLY },
472 { "__label__", RID_LABEL, 0 },
473 { "__null", RID_NULL, 0 },
474 { "__real", RID_REALPART, 0 },
475 { "__real__", RID_REALPART, 0 },
476 { "__restrict", RID_RESTRICT, 0 },
477 { "__restrict__", RID_RESTRICT, 0 },
478 { "__signed", RID_SIGNED, 0 },
479 { "__signed__", RID_SIGNED, 0 },
480 { "__thread", RID_THREAD, 0 },
481 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
482 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
483 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
484 { "__typeof", RID_TYPEOF, 0 },
485 { "__typeof__", RID_TYPEOF, 0 },
486 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
487 { "__volatile", RID_VOLATILE, 0 },
488 { "__volatile__", RID_VOLATILE, 0 },
489 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
490 { "asm", RID_ASM, D_ASM },
491 { "auto", RID_AUTO, 0 },
492 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
493 { "break", RID_BREAK, 0 },
494 { "case", RID_CASE, 0 },
495 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
496 { "char", RID_CHAR, 0 },
497 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
498 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
499 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
500 { "const", RID_CONST, 0 },
501 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
502 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
503 { "continue", RID_CONTINUE, 0 },
504 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
505 { "default", RID_DEFAULT, 0 },
506 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
507 { "do", RID_DO, 0 },
508 { "double", RID_DOUBLE, 0 },
509 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
510 { "else", RID_ELSE, 0 },
511 { "enum", RID_ENUM, 0 },
512 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
513 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
514 { "extern", RID_EXTERN, 0 },
515 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
516 { "float", RID_FLOAT, 0 },
517 { "for", RID_FOR, 0 },
518 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
519 { "goto", RID_GOTO, 0 },
520 { "if", RID_IF, 0 },
521 { "inline", RID_INLINE, D_EXT89 },
522 { "int", RID_INT, 0 },
523 { "long", RID_LONG, 0 },
524 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
525 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
526 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
527 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
528 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
529 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
530 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
531 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
532 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
533 { "register", RID_REGISTER, 0 },
534 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
535 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
536 { "return", RID_RETURN, 0 },
537 { "short", RID_SHORT, 0 },
538 { "signed", RID_SIGNED, 0 },
539 { "sizeof", RID_SIZEOF, 0 },
540 { "static", RID_STATIC, 0 },
541 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
542 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
543 { "struct", RID_STRUCT, 0 },
544 { "switch", RID_SWITCH, 0 },
545 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
546 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
547 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
548 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
549 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
550 { "typedef", RID_TYPEDEF, 0 },
551 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
552 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
553 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
554 { "union", RID_UNION, 0 },
555 { "unsigned", RID_UNSIGNED, 0 },
556 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
557 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
558 { "void", RID_VOID, 0 },
559 { "volatile", RID_VOLATILE, 0 },
560 { "wchar_t", RID_WCHAR, D_CXXONLY },
561 { "while", RID_WHILE, 0 },
562 /* These Objective-C keywords are recognized only immediately after
563 an '@'. */
564 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
565 { "defs", RID_AT_DEFS, D_OBJC },
566 { "encode", RID_AT_ENCODE, D_OBJC },
567 { "end", RID_AT_END, D_OBJC },
568 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
569 { "interface", RID_AT_INTERFACE, D_OBJC },
570 { "protocol", RID_AT_PROTOCOL, D_OBJC },
571 { "selector", RID_AT_SELECTOR, D_OBJC },
572 { "finally", RID_AT_FINALLY, D_OBJC },
573 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
574 { "optional", RID_AT_OPTIONAL, D_OBJC },
575 { "required", RID_AT_REQUIRED, D_OBJC },
576 { "property", RID_AT_PROPERTY, D_OBJC },
577 { "package", RID_AT_PACKAGE, D_OBJC },
578 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
579 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
580 /* These are recognized only in protocol-qualifier context
581 (see above) */
582 { "bycopy", RID_BYCOPY, D_OBJC },
583 { "byref", RID_BYREF, D_OBJC },
584 { "in", RID_IN, D_OBJC },
585 { "inout", RID_INOUT, D_OBJC },
586 { "oneway", RID_ONEWAY, D_OBJC },
587 { "out", RID_OUT, D_OBJC },
588 /* These are recognized inside a property attribute list */
589 { "assign", RID_ASSIGN, D_OBJC },
590 { "copy", RID_COPY, D_OBJC },
591 { "getter", RID_GETTER, D_OBJC },
592 { "nonatomic", RID_NONATOMIC, D_OBJC },
593 { "readonly", RID_READONLY, D_OBJC },
594 { "readwrite", RID_READWRITE, D_OBJC },
595 { "retain", RID_RETAIN, D_OBJC },
596 { "setter", RID_SETTER, D_OBJC },
599 const unsigned int num_c_common_reswords =
600 sizeof c_common_reswords / sizeof (struct c_common_resword);
602 /* Table of machine-independent attributes common to all C-like languages. */
603 const struct attribute_spec c_common_attribute_table[] =
605 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
606 affects_type_identity } */
607 { "packed", 0, 0, false, false, false,
608 handle_packed_attribute , false},
609 { "nocommon", 0, 0, true, false, false,
610 handle_nocommon_attribute, false},
611 { "common", 0, 0, true, false, false,
612 handle_common_attribute, false },
613 /* FIXME: logically, noreturn attributes should be listed as
614 "false, true, true" and apply to function types. But implementing this
615 would require all the places in the compiler that use TREE_THIS_VOLATILE
616 on a decl to identify non-returning functions to be located and fixed
617 to check the function type instead. */
618 { "noreturn", 0, 0, true, false, false,
619 handle_noreturn_attribute, false },
620 { "volatile", 0, 0, true, false, false,
621 handle_noreturn_attribute, false },
622 { "noinline", 0, 0, true, false, false,
623 handle_noinline_attribute, false },
624 { "noclone", 0, 0, true, false, false,
625 handle_noclone_attribute, false },
626 { "leaf", 0, 0, true, false, false,
627 handle_leaf_attribute, false },
628 { "always_inline", 0, 0, true, false, false,
629 handle_always_inline_attribute, false },
630 { "gnu_inline", 0, 0, true, false, false,
631 handle_gnu_inline_attribute, false },
632 { "artificial", 0, 0, true, false, false,
633 handle_artificial_attribute, false },
634 { "flatten", 0, 0, true, false, false,
635 handle_flatten_attribute, false },
636 { "used", 0, 0, true, false, false,
637 handle_used_attribute, false },
638 { "unused", 0, 0, false, false, false,
639 handle_unused_attribute, false },
640 { "externally_visible", 0, 0, true, false, false,
641 handle_externally_visible_attribute, false },
642 /* The same comments as for noreturn attributes apply to const ones. */
643 { "const", 0, 0, true, false, false,
644 handle_const_attribute, false },
645 { "transparent_union", 0, 0, false, false, false,
646 handle_transparent_union_attribute, false },
647 { "constructor", 0, 1, true, false, false,
648 handle_constructor_attribute, false },
649 { "destructor", 0, 1, true, false, false,
650 handle_destructor_attribute, false },
651 { "mode", 1, 1, false, true, false,
652 handle_mode_attribute, false },
653 { "section", 1, 1, true, false, false,
654 handle_section_attribute, false },
655 { "aligned", 0, 1, false, false, false,
656 handle_aligned_attribute, false },
657 { "weak", 0, 0, true, false, false,
658 handle_weak_attribute, false },
659 { "ifunc", 1, 1, true, false, false,
660 handle_ifunc_attribute, false },
661 { "alias", 1, 1, true, false, false,
662 handle_alias_attribute, false },
663 { "weakref", 0, 1, true, false, false,
664 handle_weakref_attribute, false },
665 { "no_instrument_function", 0, 0, true, false, false,
666 handle_no_instrument_function_attribute,
667 false },
668 { "malloc", 0, 0, true, false, false,
669 handle_malloc_attribute, false },
670 { "returns_twice", 0, 0, true, false, false,
671 handle_returns_twice_attribute, false },
672 { "no_stack_limit", 0, 0, true, false, false,
673 handle_no_limit_stack_attribute, false },
674 { "pure", 0, 0, true, false, false,
675 handle_pure_attribute, false },
676 { "transaction_callable", 0, 0, false, true, false,
677 handle_tm_attribute, false },
678 { "transaction_unsafe", 0, 0, false, true, false,
679 handle_tm_attribute, false },
680 { "transaction_safe", 0, 0, false, true, false,
681 handle_tm_attribute, false },
682 { "transaction_may_cancel_outer", 0, 0, false, true, false,
683 handle_tm_attribute, false },
684 /* ??? These two attributes didn't make the transition from the
685 Intel language document to the multi-vendor language document. */
686 { "transaction_pure", 0, 0, false, true, false,
687 handle_tm_attribute, false },
688 { "transaction_wrap", 1, 1, true, false, false,
689 handle_tm_wrap_attribute, false },
690 /* For internal use (marking of builtins) only. The name contains space
691 to prevent its usage in source code. */
692 { "no vops", 0, 0, true, false, false,
693 handle_novops_attribute, false },
694 { "deprecated", 0, 1, false, false, false,
695 handle_deprecated_attribute, false },
696 { "vector_size", 1, 1, false, true, false,
697 handle_vector_size_attribute, false },
698 { "visibility", 1, 1, false, false, false,
699 handle_visibility_attribute, false },
700 { "tls_model", 1, 1, true, false, false,
701 handle_tls_model_attribute, false },
702 { "nonnull", 0, -1, false, true, true,
703 handle_nonnull_attribute, false },
704 { "nothrow", 0, 0, true, false, false,
705 handle_nothrow_attribute, false },
706 { "may_alias", 0, 0, false, true, false, NULL, false },
707 { "cleanup", 1, 1, true, false, false,
708 handle_cleanup_attribute, false },
709 { "warn_unused_result", 0, 0, false, true, true,
710 handle_warn_unused_result_attribute, false },
711 { "sentinel", 0, 1, false, true, true,
712 handle_sentinel_attribute, false },
713 /* For internal use (marking of builtins) only. The name contains space
714 to prevent its usage in source code. */
715 { "type generic", 0, 0, false, true, true,
716 handle_type_generic_attribute, false },
717 { "alloc_size", 1, 2, false, true, true,
718 handle_alloc_size_attribute, false },
719 { "cold", 0, 0, true, false, false,
720 handle_cold_attribute, false },
721 { "hot", 0, 0, true, false, false,
722 handle_hot_attribute, false },
723 { "warning", 1, 1, true, false, false,
724 handle_error_attribute, false },
725 { "error", 1, 1, true, false, false,
726 handle_error_attribute, false },
727 { "target", 1, -1, true, false, false,
728 handle_target_attribute, false },
729 { "optimize", 1, -1, true, false, false,
730 handle_optimize_attribute, false },
731 /* For internal use only. The leading '*' both prevents its usage in
732 source code and signals that it may be overridden by machine tables. */
733 { "*tm regparm", 0, 0, false, true, true,
734 ignore_attribute, false },
735 { "no_split_stack", 0, 0, true, false, false,
736 handle_no_split_stack_attribute, false },
737 /* For internal use (marking of builtins and runtime functions) only.
738 The name contains space to prevent its usage in source code. */
739 { "fn spec", 1, 1, false, true, true,
740 handle_fnspec_attribute, false },
741 { NULL, 0, 0, false, false, false, NULL, false }
744 /* Give the specifications for the format attributes, used by C and all
745 descendants. */
747 const struct attribute_spec c_common_format_attribute_table[] =
749 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
750 affects_type_identity } */
751 { "format", 3, 3, false, true, true,
752 handle_format_attribute, false },
753 { "format_arg", 1, 1, false, true, true,
754 handle_format_arg_attribute, false },
755 { NULL, 0, 0, false, false, false, NULL, false }
758 /* Return identifier for address space AS. */
760 const char *
761 c_addr_space_name (addr_space_t as)
763 int rid = RID_FIRST_ADDR_SPACE + as;
764 gcc_assert (ridpointers [rid]);
765 return IDENTIFIER_POINTER (ridpointers [rid]);
768 /* Push current bindings for the function name VAR_DECLS. */
770 void
771 start_fname_decls (void)
773 unsigned ix;
774 tree saved = NULL_TREE;
776 for (ix = 0; fname_vars[ix].decl; ix++)
778 tree decl = *fname_vars[ix].decl;
780 if (decl)
782 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
783 saved);
784 *fname_vars[ix].decl = NULL_TREE;
787 if (saved || saved_function_name_decls)
788 /* Normally they'll have been NULL, so only push if we've got a
789 stack, or they are non-NULL. */
790 saved_function_name_decls = tree_cons (saved, NULL_TREE,
791 saved_function_name_decls);
794 /* Finish up the current bindings, adding them into the current function's
795 statement tree. This must be done _before_ finish_stmt_tree is called.
796 If there is no current function, we must be at file scope and no statements
797 are involved. Pop the previous bindings. */
799 void
800 finish_fname_decls (void)
802 unsigned ix;
803 tree stmts = NULL_TREE;
804 tree stack = saved_function_name_decls;
806 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
807 append_to_statement_list (TREE_VALUE (stack), &stmts);
809 if (stmts)
811 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
813 if (TREE_CODE (*bodyp) == BIND_EXPR)
814 bodyp = &BIND_EXPR_BODY (*bodyp);
816 append_to_statement_list_force (*bodyp, &stmts);
817 *bodyp = stmts;
820 for (ix = 0; fname_vars[ix].decl; ix++)
821 *fname_vars[ix].decl = NULL_TREE;
823 if (stack)
825 /* We had saved values, restore them. */
826 tree saved;
828 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
830 tree decl = TREE_PURPOSE (saved);
831 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
833 *fname_vars[ix].decl = decl;
835 stack = TREE_CHAIN (stack);
837 saved_function_name_decls = stack;
840 /* Return the text name of the current function, suitably prettified
841 by PRETTY_P. Return string must be freed by caller. */
843 const char *
844 fname_as_string (int pretty_p)
846 const char *name = "top level";
847 char *namep;
848 int vrb = 2, len;
849 cpp_string cstr = { 0, 0 }, strname;
851 if (!pretty_p)
853 name = "";
854 vrb = 0;
857 if (current_function_decl)
858 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
860 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
862 namep = XNEWVEC (char, len);
863 snprintf (namep, len, "\"%s\"", name);
864 strname.text = (unsigned char *) namep;
865 strname.len = len - 1;
867 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
869 XDELETEVEC (namep);
870 return (const char *) cstr.text;
873 return namep;
876 /* Return the VAR_DECL for a const char array naming the current
877 function. If the VAR_DECL has not yet been created, create it
878 now. RID indicates how it should be formatted and IDENTIFIER_NODE
879 ID is its name (unfortunately C and C++ hold the RID values of
880 keywords in different places, so we can't derive RID from ID in
881 this language independent code. LOC is the location of the
882 function. */
884 tree
885 fname_decl (location_t loc, unsigned int rid, tree id)
887 unsigned ix;
888 tree decl = NULL_TREE;
890 for (ix = 0; fname_vars[ix].decl; ix++)
891 if (fname_vars[ix].rid == rid)
892 break;
894 decl = *fname_vars[ix].decl;
895 if (!decl)
897 /* If a tree is built here, it would normally have the lineno of
898 the current statement. Later this tree will be moved to the
899 beginning of the function and this line number will be wrong.
900 To avoid this problem set the lineno to 0 here; that prevents
901 it from appearing in the RTL. */
902 tree stmts;
903 location_t saved_location = input_location;
904 input_location = UNKNOWN_LOCATION;
906 stmts = push_stmt_list ();
907 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
908 stmts = pop_stmt_list (stmts);
909 if (!IS_EMPTY_STMT (stmts))
910 saved_function_name_decls
911 = tree_cons (decl, stmts, saved_function_name_decls);
912 *fname_vars[ix].decl = decl;
913 input_location = saved_location;
915 if (!ix && !current_function_decl)
916 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
918 return decl;
921 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
923 tree
924 fix_string_type (tree value)
926 int length = TREE_STRING_LENGTH (value);
927 int nchars;
928 tree e_type, i_type, a_type;
930 /* Compute the number of elements, for the array type. */
931 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
933 nchars = length;
934 e_type = char_type_node;
936 else if (TREE_TYPE (value) == char16_array_type_node)
938 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
939 e_type = char16_type_node;
941 else if (TREE_TYPE (value) == char32_array_type_node)
943 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
944 e_type = char32_type_node;
946 else
948 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
949 e_type = wchar_type_node;
952 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
953 limit in C++98 Annex B is very large (65536) and is not normative,
954 so we do not diagnose it (warn_overlength_strings is forced off
955 in c_common_post_options). */
956 if (warn_overlength_strings)
958 const int nchars_max = flag_isoc99 ? 4095 : 509;
959 const int relevant_std = flag_isoc99 ? 99 : 90;
960 if (nchars - 1 > nchars_max)
961 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
962 separate the %d from the 'C'. 'ISO' should not be
963 translated, but it may be moved after 'C%d' in languages
964 where modifiers follow nouns. */
965 pedwarn (input_location, OPT_Woverlength_strings,
966 "string length %qd is greater than the length %qd "
967 "ISO C%d compilers are required to support",
968 nchars - 1, nchars_max, relevant_std);
971 /* Create the array type for the string constant. The ISO C++
972 standard says that a string literal has type `const char[N]' or
973 `const wchar_t[N]'. We use the same logic when invoked as a C
974 front-end with -Wwrite-strings.
975 ??? We should change the type of an expression depending on the
976 state of a warning flag. We should just be warning -- see how
977 this is handled in the C++ front-end for the deprecated implicit
978 conversion from string literals to `char*' or `wchar_t*'.
980 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
981 array type being the unqualified version of that type.
982 Therefore, if we are constructing an array of const char, we must
983 construct the matching unqualified array type first. The C front
984 end does not require this, but it does no harm, so we do it
985 unconditionally. */
986 i_type = build_index_type (size_int (nchars - 1));
987 a_type = build_array_type (e_type, i_type);
988 if (c_dialect_cxx() || warn_write_strings)
989 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
991 TREE_TYPE (value) = a_type;
992 TREE_CONSTANT (value) = 1;
993 TREE_READONLY (value) = 1;
994 TREE_STATIC (value) = 1;
995 return value;
998 /* If DISABLE is true, stop issuing warnings. This is used when
999 parsing code that we know will not be executed. This function may
1000 be called multiple times, and works as a stack. */
1002 static void
1003 c_disable_warnings (bool disable)
1005 if (disable)
1007 ++c_inhibit_evaluation_warnings;
1008 fold_defer_overflow_warnings ();
1012 /* If ENABLE is true, reenable issuing warnings. */
1014 static void
1015 c_enable_warnings (bool enable)
1017 if (enable)
1019 --c_inhibit_evaluation_warnings;
1020 fold_undefer_and_ignore_overflow_warnings ();
1024 /* Fully fold EXPR, an expression that was not folded (beyond integer
1025 constant expressions and null pointer constants) when being built
1026 up. If IN_INIT, this is in a static initializer and certain
1027 changes are made to the folding done. Clear *MAYBE_CONST if
1028 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1029 expression because it contains an evaluated operator (in C99) or an
1030 operator outside of sizeof returning an integer constant (in C90)
1031 not permitted in constant expressions, or because it contains an
1032 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1033 set to true by callers before calling this function.) Return the
1034 folded expression. Function arguments have already been folded
1035 before calling this function, as have the contents of SAVE_EXPR,
1036 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1037 C_MAYBE_CONST_EXPR. */
1039 tree
1040 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1042 tree ret;
1043 tree eptype = NULL_TREE;
1044 bool dummy = true;
1045 bool maybe_const_itself = true;
1046 location_t loc = EXPR_LOCATION (expr);
1048 /* This function is not relevant to C++ because C++ folds while
1049 parsing, and may need changes to be correct for C++ when C++
1050 stops folding while parsing. */
1051 if (c_dialect_cxx ())
1052 gcc_unreachable ();
1054 if (!maybe_const)
1055 maybe_const = &dummy;
1056 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1058 eptype = TREE_TYPE (expr);
1059 expr = TREE_OPERAND (expr, 0);
1061 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1062 &maybe_const_itself);
1063 if (eptype)
1064 ret = fold_convert_loc (loc, eptype, ret);
1065 *maybe_const &= maybe_const_itself;
1066 return ret;
1069 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1070 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1071 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1072 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1073 both evaluated and unevaluated subexpressions while
1074 *MAYBE_CONST_ITSELF is carried from only evaluated
1075 subexpressions). */
1077 static tree
1078 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1079 bool *maybe_const_itself)
1081 tree ret = expr;
1082 enum tree_code code = TREE_CODE (expr);
1083 enum tree_code_class kind = TREE_CODE_CLASS (code);
1084 location_t loc = EXPR_LOCATION (expr);
1085 tree op0, op1, op2, op3;
1086 tree orig_op0, orig_op1, orig_op2;
1087 bool op0_const = true, op1_const = true, op2_const = true;
1088 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1089 bool nowarning = TREE_NO_WARNING (expr);
1090 bool unused_p;
1092 /* This function is not relevant to C++ because C++ folds while
1093 parsing, and may need changes to be correct for C++ when C++
1094 stops folding while parsing. */
1095 if (c_dialect_cxx ())
1096 gcc_unreachable ();
1098 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1099 anything else not counted as an expression cannot usefully be
1100 folded further at this point. */
1101 if (!IS_EXPR_CODE_CLASS (kind)
1102 || kind == tcc_statement
1103 || code == SAVE_EXPR)
1104 return expr;
1106 /* Operands of variable-length expressions (function calls) have
1107 already been folded, as have __builtin_* function calls, and such
1108 expressions cannot occur in constant expressions. */
1109 if (kind == tcc_vl_exp)
1111 *maybe_const_operands = false;
1112 ret = fold (expr);
1113 goto out;
1116 if (code == C_MAYBE_CONST_EXPR)
1118 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1119 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1120 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1121 *maybe_const_operands = false;
1122 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1123 *maybe_const_itself = false;
1124 if (pre && !in_init)
1125 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1126 else
1127 ret = inner;
1128 goto out;
1131 /* Assignment, increment, decrement, function call and comma
1132 operators, and statement expressions, cannot occur in constant
1133 expressions if evaluated / outside of sizeof. (Function calls
1134 were handled above, though VA_ARG_EXPR is treated like a function
1135 call here, and statement expressions are handled through
1136 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1137 switch (code)
1139 case MODIFY_EXPR:
1140 case PREDECREMENT_EXPR:
1141 case PREINCREMENT_EXPR:
1142 case POSTDECREMENT_EXPR:
1143 case POSTINCREMENT_EXPR:
1144 case COMPOUND_EXPR:
1145 *maybe_const_operands = false;
1146 break;
1148 case VA_ARG_EXPR:
1149 case TARGET_EXPR:
1150 case BIND_EXPR:
1151 case OBJ_TYPE_REF:
1152 *maybe_const_operands = false;
1153 ret = fold (expr);
1154 goto out;
1156 default:
1157 break;
1160 /* Fold individual tree codes as appropriate. */
1161 switch (code)
1163 case COMPOUND_LITERAL_EXPR:
1164 /* Any non-constancy will have been marked in a containing
1165 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1166 goto out;
1168 case COMPONENT_REF:
1169 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1170 op1 = TREE_OPERAND (expr, 1);
1171 op2 = TREE_OPERAND (expr, 2);
1172 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1173 maybe_const_itself);
1174 STRIP_TYPE_NOPS (op0);
1175 if (op0 != orig_op0)
1176 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1177 if (ret != expr)
1179 TREE_READONLY (ret) = TREE_READONLY (expr);
1180 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1182 goto out;
1184 case ARRAY_REF:
1185 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1186 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1187 op2 = TREE_OPERAND (expr, 2);
1188 op3 = TREE_OPERAND (expr, 3);
1189 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1190 maybe_const_itself);
1191 STRIP_TYPE_NOPS (op0);
1192 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1193 maybe_const_itself);
1194 STRIP_TYPE_NOPS (op1);
1195 op1 = decl_constant_value_for_optimization (op1);
1196 if (op0 != orig_op0 || op1 != orig_op1)
1197 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1198 if (ret != expr)
1200 TREE_READONLY (ret) = TREE_READONLY (expr);
1201 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1202 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1204 ret = fold (ret);
1205 goto out;
1207 case COMPOUND_EXPR:
1208 case MODIFY_EXPR:
1209 case PREDECREMENT_EXPR:
1210 case PREINCREMENT_EXPR:
1211 case POSTDECREMENT_EXPR:
1212 case POSTINCREMENT_EXPR:
1213 case PLUS_EXPR:
1214 case MINUS_EXPR:
1215 case MULT_EXPR:
1216 case POINTER_PLUS_EXPR:
1217 case TRUNC_DIV_EXPR:
1218 case CEIL_DIV_EXPR:
1219 case FLOOR_DIV_EXPR:
1220 case TRUNC_MOD_EXPR:
1221 case RDIV_EXPR:
1222 case EXACT_DIV_EXPR:
1223 case LSHIFT_EXPR:
1224 case RSHIFT_EXPR:
1225 case BIT_IOR_EXPR:
1226 case BIT_XOR_EXPR:
1227 case BIT_AND_EXPR:
1228 case LT_EXPR:
1229 case LE_EXPR:
1230 case GT_EXPR:
1231 case GE_EXPR:
1232 case EQ_EXPR:
1233 case NE_EXPR:
1234 case COMPLEX_EXPR:
1235 case TRUTH_AND_EXPR:
1236 case TRUTH_OR_EXPR:
1237 case TRUTH_XOR_EXPR:
1238 case UNORDERED_EXPR:
1239 case ORDERED_EXPR:
1240 case UNLT_EXPR:
1241 case UNLE_EXPR:
1242 case UNGT_EXPR:
1243 case UNGE_EXPR:
1244 case UNEQ_EXPR:
1245 /* Binary operations evaluating both arguments (increment and
1246 decrement are binary internally in GCC). */
1247 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1248 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1249 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1250 maybe_const_itself);
1251 STRIP_TYPE_NOPS (op0);
1252 if (code != MODIFY_EXPR
1253 && code != PREDECREMENT_EXPR
1254 && code != PREINCREMENT_EXPR
1255 && code != POSTDECREMENT_EXPR
1256 && code != POSTINCREMENT_EXPR)
1257 op0 = decl_constant_value_for_optimization (op0);
1258 /* The RHS of a MODIFY_EXPR was fully folded when building that
1259 expression for the sake of conversion warnings. */
1260 if (code != MODIFY_EXPR)
1261 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1262 maybe_const_itself);
1263 STRIP_TYPE_NOPS (op1);
1264 op1 = decl_constant_value_for_optimization (op1);
1265 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1266 ret = in_init
1267 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1268 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1269 else
1270 ret = fold (expr);
1271 if (TREE_OVERFLOW_P (ret)
1272 && !TREE_OVERFLOW_P (op0)
1273 && !TREE_OVERFLOW_P (op1))
1274 overflow_warning (EXPR_LOCATION (expr), ret);
1275 goto out;
1277 case INDIRECT_REF:
1278 case FIX_TRUNC_EXPR:
1279 case FLOAT_EXPR:
1280 CASE_CONVERT:
1281 case VIEW_CONVERT_EXPR:
1282 case NON_LVALUE_EXPR:
1283 case NEGATE_EXPR:
1284 case BIT_NOT_EXPR:
1285 case TRUTH_NOT_EXPR:
1286 case ADDR_EXPR:
1287 case CONJ_EXPR:
1288 case REALPART_EXPR:
1289 case IMAGPART_EXPR:
1290 /* Unary operations. */
1291 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1292 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1293 maybe_const_itself);
1294 STRIP_TYPE_NOPS (op0);
1295 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1296 op0 = decl_constant_value_for_optimization (op0);
1297 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1298 not prepared to deal with them if they occur in initializers. */
1299 if (op0 != orig_op0
1300 && code == ADDR_EXPR
1301 && (op1 = get_base_address (op0)) != NULL_TREE
1302 && TREE_CODE (op1) == INDIRECT_REF
1303 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1304 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1305 else if (op0 != orig_op0 || in_init)
1306 ret = in_init
1307 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1308 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1309 else
1310 ret = fold (expr);
1311 if (code == INDIRECT_REF
1312 && ret != expr
1313 && TREE_CODE (ret) == INDIRECT_REF)
1315 TREE_READONLY (ret) = TREE_READONLY (expr);
1316 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1317 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1319 switch (code)
1321 case FIX_TRUNC_EXPR:
1322 case FLOAT_EXPR:
1323 CASE_CONVERT:
1324 /* Don't warn about explicit conversions. We will already
1325 have warned about suspect implicit conversions. */
1326 break;
1328 default:
1329 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1330 overflow_warning (EXPR_LOCATION (expr), ret);
1331 break;
1333 goto out;
1335 case TRUTH_ANDIF_EXPR:
1336 case TRUTH_ORIF_EXPR:
1337 /* Binary operations not necessarily evaluating both
1338 arguments. */
1339 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1340 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1341 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1342 STRIP_TYPE_NOPS (op0);
1344 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1345 ? truthvalue_false_node
1346 : truthvalue_true_node));
1347 c_disable_warnings (unused_p);
1348 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1349 STRIP_TYPE_NOPS (op1);
1350 c_enable_warnings (unused_p);
1352 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1353 ret = in_init
1354 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1355 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1356 else
1357 ret = fold (expr);
1358 *maybe_const_operands &= op0_const;
1359 *maybe_const_itself &= op0_const_self;
1360 if (!(flag_isoc99
1361 && op0_const
1362 && op0_const_self
1363 && (code == TRUTH_ANDIF_EXPR
1364 ? op0 == truthvalue_false_node
1365 : op0 == truthvalue_true_node)))
1366 *maybe_const_operands &= op1_const;
1367 if (!(op0_const
1368 && op0_const_self
1369 && (code == TRUTH_ANDIF_EXPR
1370 ? op0 == truthvalue_false_node
1371 : op0 == truthvalue_true_node)))
1372 *maybe_const_itself &= op1_const_self;
1373 goto out;
1375 case COND_EXPR:
1376 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1377 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1378 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1379 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1381 STRIP_TYPE_NOPS (op0);
1382 c_disable_warnings (op0 == truthvalue_false_node);
1383 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1384 STRIP_TYPE_NOPS (op1);
1385 c_enable_warnings (op0 == truthvalue_false_node);
1387 c_disable_warnings (op0 == truthvalue_true_node);
1388 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1389 STRIP_TYPE_NOPS (op2);
1390 c_enable_warnings (op0 == truthvalue_true_node);
1392 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1393 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1394 else
1395 ret = fold (expr);
1396 *maybe_const_operands &= op0_const;
1397 *maybe_const_itself &= op0_const_self;
1398 if (!(flag_isoc99
1399 && op0_const
1400 && op0_const_self
1401 && op0 == truthvalue_false_node))
1402 *maybe_const_operands &= op1_const;
1403 if (!(op0_const
1404 && op0_const_self
1405 && op0 == truthvalue_false_node))
1406 *maybe_const_itself &= op1_const_self;
1407 if (!(flag_isoc99
1408 && op0_const
1409 && op0_const_self
1410 && op0 == truthvalue_true_node))
1411 *maybe_const_operands &= op2_const;
1412 if (!(op0_const
1413 && op0_const_self
1414 && op0 == truthvalue_true_node))
1415 *maybe_const_itself &= op2_const_self;
1416 goto out;
1418 case EXCESS_PRECISION_EXPR:
1419 /* Each case where an operand with excess precision may be
1420 encountered must remove the EXCESS_PRECISION_EXPR around
1421 inner operands and possibly put one around the whole
1422 expression or possibly convert to the semantic type (which
1423 c_fully_fold does); we cannot tell at this stage which is
1424 appropriate in any particular case. */
1425 gcc_unreachable ();
1427 default:
1428 /* Various codes may appear through folding built-in functions
1429 and their arguments. */
1430 goto out;
1433 out:
1434 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1435 have been done by this point, so remove them again. */
1436 nowarning |= TREE_NO_WARNING (ret);
1437 STRIP_TYPE_NOPS (ret);
1438 if (nowarning && !TREE_NO_WARNING (ret))
1440 if (!CAN_HAVE_LOCATION_P (ret))
1441 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1442 TREE_NO_WARNING (ret) = 1;
1444 if (ret != expr)
1445 protected_set_expr_location (ret, loc);
1446 return ret;
1449 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1450 return EXP. Otherwise, return either EXP or its known constant
1451 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1452 Is the BLKmode test appropriate? */
1454 tree
1455 decl_constant_value_for_optimization (tree exp)
1457 tree ret;
1459 /* This function is only used by C, for c_fully_fold and other
1460 optimization, and may not be correct for C++. */
1461 if (c_dialect_cxx ())
1462 gcc_unreachable ();
1464 if (!optimize
1465 || TREE_CODE (exp) != VAR_DECL
1466 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1467 || DECL_MODE (exp) == BLKmode)
1468 return exp;
1470 ret = decl_constant_value (exp);
1471 /* Avoid unwanted tree sharing between the initializer and current
1472 function's body where the tree can be modified e.g. by the
1473 gimplifier. */
1474 if (ret != exp && TREE_STATIC (exp))
1475 ret = unshare_expr (ret);
1476 return ret;
1479 /* Print a warning if a constant expression had overflow in folding.
1480 Invoke this function on every expression that the language
1481 requires to be a constant expression.
1482 Note the ANSI C standard says it is erroneous for a
1483 constant expression to overflow. */
1485 void
1486 constant_expression_warning (tree value)
1488 if (warn_overflow && pedantic
1489 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1490 || TREE_CODE (value) == FIXED_CST
1491 || TREE_CODE (value) == VECTOR_CST
1492 || TREE_CODE (value) == COMPLEX_CST)
1493 && TREE_OVERFLOW (value))
1494 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1497 /* The same as above but print an unconditional error. */
1498 void
1499 constant_expression_error (tree value)
1501 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1502 || TREE_CODE (value) == FIXED_CST
1503 || TREE_CODE (value) == VECTOR_CST
1504 || TREE_CODE (value) == COMPLEX_CST)
1505 && TREE_OVERFLOW (value))
1506 error ("overflow in constant expression");
1509 /* Print a warning if an expression had overflow in folding and its
1510 operands hadn't.
1512 Invoke this function on every expression that
1513 (1) appears in the source code, and
1514 (2) is a constant expression that overflowed, and
1515 (3) is not already checked by convert_and_check;
1516 however, do not invoke this function on operands of explicit casts
1517 or when the expression is the result of an operator and any operand
1518 already overflowed. */
1520 void
1521 overflow_warning (location_t loc, tree value)
1523 if (c_inhibit_evaluation_warnings != 0)
1524 return;
1526 switch (TREE_CODE (value))
1528 case INTEGER_CST:
1529 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1530 break;
1532 case REAL_CST:
1533 warning_at (loc, OPT_Woverflow,
1534 "floating point overflow in expression");
1535 break;
1537 case FIXED_CST:
1538 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1539 break;
1541 case VECTOR_CST:
1542 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1543 break;
1545 case COMPLEX_CST:
1546 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1547 warning_at (loc, OPT_Woverflow,
1548 "complex integer overflow in expression");
1549 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1550 warning_at (loc, OPT_Woverflow,
1551 "complex floating point overflow in expression");
1552 break;
1554 default:
1555 break;
1559 /* Warn about uses of logical || / && operator in a context where it
1560 is likely that the bitwise equivalent was intended by the
1561 programmer. We have seen an expression in which CODE is a binary
1562 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1563 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1564 void
1565 warn_logical_operator (location_t location, enum tree_code code, tree type,
1566 enum tree_code code_left, tree op_left,
1567 enum tree_code ARG_UNUSED (code_right), tree op_right)
1569 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1570 int in0_p, in1_p, in_p;
1571 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1572 bool strict_overflow_p = false;
1574 if (code != TRUTH_ANDIF_EXPR
1575 && code != TRUTH_AND_EXPR
1576 && code != TRUTH_ORIF_EXPR
1577 && code != TRUTH_OR_EXPR)
1578 return;
1580 /* Warn if &&/|| are being used in a context where it is
1581 likely that the bitwise equivalent was intended by the
1582 programmer. That is, an expression such as op && MASK
1583 where op should not be any boolean expression, nor a
1584 constant, and mask seems to be a non-boolean integer constant. */
1585 if (!truth_value_p (code_left)
1586 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1587 && !CONSTANT_CLASS_P (op_left)
1588 && !TREE_NO_WARNING (op_left)
1589 && TREE_CODE (op_right) == INTEGER_CST
1590 && !integer_zerop (op_right)
1591 && !integer_onep (op_right))
1593 if (or_op)
1594 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1595 " applied to non-boolean constant");
1596 else
1597 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1598 " applied to non-boolean constant");
1599 TREE_NO_WARNING (op_left) = true;
1600 return;
1603 /* We do not warn for constants because they are typical of macro
1604 expansions that test for features. */
1605 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1606 return;
1608 /* This warning only makes sense with logical operands. */
1609 if (!(truth_value_p (TREE_CODE (op_left))
1610 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1611 || !(truth_value_p (TREE_CODE (op_right))
1612 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1613 return;
1615 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1616 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1618 if (lhs && TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1619 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1621 if (rhs && TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1622 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1624 /* If this is an OR operation, invert both sides; we will invert
1625 again at the end. */
1626 if (or_op)
1627 in0_p = !in0_p, in1_p = !in1_p;
1629 /* If both expressions are the same, if we can merge the ranges, and we
1630 can build the range test, return it or it inverted. */
1631 if (lhs && rhs && operand_equal_p (lhs, rhs, 0)
1632 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1633 in1_p, low1, high1)
1634 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1635 type, lhs, in_p, low, high)))
1637 if (TREE_CODE (tem) != INTEGER_CST)
1638 return;
1640 if (or_op)
1641 warning_at (location, OPT_Wlogical_op,
1642 "logical %<or%> "
1643 "of collectively exhaustive tests is always true");
1644 else
1645 warning_at (location, OPT_Wlogical_op,
1646 "logical %<and%> "
1647 "of mutually exclusive tests is always false");
1652 /* Print a warning about casts that might indicate violation
1653 of strict aliasing rules if -Wstrict-aliasing is used and
1654 strict aliasing mode is in effect. OTYPE is the original
1655 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1657 bool
1658 strict_aliasing_warning (tree otype, tree type, tree expr)
1660 /* Strip pointer conversion chains and get to the correct original type. */
1661 STRIP_NOPS (expr);
1662 otype = TREE_TYPE (expr);
1664 if (!(flag_strict_aliasing
1665 && POINTER_TYPE_P (type)
1666 && POINTER_TYPE_P (otype)
1667 && !VOID_TYPE_P (TREE_TYPE (type)))
1668 /* If the type we are casting to is a ref-all pointer
1669 dereferencing it is always valid. */
1670 || TYPE_REF_CAN_ALIAS_ALL (type))
1671 return false;
1673 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1674 && (DECL_P (TREE_OPERAND (expr, 0))
1675 || handled_component_p (TREE_OPERAND (expr, 0))))
1677 /* Casting the address of an object to non void pointer. Warn
1678 if the cast breaks type based aliasing. */
1679 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1681 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1682 "might break strict-aliasing rules");
1683 return true;
1685 else
1687 /* warn_strict_aliasing >= 3. This includes the default (3).
1688 Only warn if the cast is dereferenced immediately. */
1689 alias_set_type set1 =
1690 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1691 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1693 if (set1 != set2 && set2 != 0
1694 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1696 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1697 "pointer will break strict-aliasing rules");
1698 return true;
1700 else if (warn_strict_aliasing == 2
1701 && !alias_sets_must_conflict_p (set1, set2))
1703 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1704 "pointer might break strict-aliasing rules");
1705 return true;
1709 else
1710 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1712 /* At this level, warn for any conversions, even if an address is
1713 not taken in the same statement. This will likely produce many
1714 false positives, but could be useful to pinpoint problems that
1715 are not revealed at higher levels. */
1716 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1717 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1718 if (!COMPLETE_TYPE_P (type)
1719 || !alias_sets_must_conflict_p (set1, set2))
1721 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1722 "pointer might break strict-aliasing rules");
1723 return true;
1727 return false;
1730 /* Warn for unlikely, improbable, or stupid DECL declarations
1731 of `main'. */
1733 void
1734 check_main_parameter_types (tree decl)
1736 function_args_iterator iter;
1737 tree type;
1738 int argct = 0;
1740 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1742 /* XXX void_type_node belies the abstraction. */
1743 if (type == void_type_node || type == error_mark_node )
1744 break;
1746 ++argct;
1747 switch (argct)
1749 case 1:
1750 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1751 pedwarn (input_location, OPT_Wmain,
1752 "first argument of %q+D should be %<int%>", decl);
1753 break;
1755 case 2:
1756 if (TREE_CODE (type) != POINTER_TYPE
1757 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1758 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1759 != char_type_node))
1760 pedwarn (input_location, OPT_Wmain,
1761 "second argument of %q+D should be %<char **%>", decl);
1762 break;
1764 case 3:
1765 if (TREE_CODE (type) != POINTER_TYPE
1766 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1767 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1768 != char_type_node))
1769 pedwarn (input_location, OPT_Wmain,
1770 "third argument of %q+D should probably be "
1771 "%<char **%>", decl);
1772 break;
1776 /* It is intentional that this message does not mention the third
1777 argument because it's only mentioned in an appendix of the
1778 standard. */
1779 if (argct > 0 && (argct < 2 || argct > 3))
1780 pedwarn (input_location, OPT_Wmain,
1781 "%q+D takes only zero or two arguments", decl);
1784 /* True if pointers to distinct types T1 and T2 can be converted to
1785 each other without an explicit cast. Only returns true for opaque
1786 vector types. */
1787 bool
1788 vector_targets_convertible_p (const_tree t1, const_tree t2)
1790 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
1791 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1792 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1793 return true;
1795 return false;
1798 /* True if vector types T1 and T2 can be converted to each other
1799 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1800 can only be converted with -flax-vector-conversions yet that is not
1801 in effect, emit a note telling the user about that option if such
1802 a note has not previously been emitted. */
1803 bool
1804 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1806 static bool emitted_lax_note = false;
1807 bool convertible_lax;
1809 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1810 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1811 return true;
1813 convertible_lax =
1814 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1815 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
1816 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1817 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1818 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1820 if (!convertible_lax || flag_lax_vector_conversions)
1821 return convertible_lax;
1823 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1824 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1825 return true;
1827 if (emit_lax_note && !emitted_lax_note)
1829 emitted_lax_note = true;
1830 inform (input_location, "use -flax-vector-conversions to permit "
1831 "conversions between vectors with differing "
1832 "element types or numbers of subparts");
1835 return false;
1838 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1839 to integral type. */
1841 static tree
1842 c_common_get_narrower (tree op, int *unsignedp_ptr)
1844 op = get_narrower (op, unsignedp_ptr);
1846 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1847 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1849 /* C++0x scoped enumerations don't implicitly convert to integral
1850 type; if we stripped an explicit conversion to a larger type we
1851 need to replace it so common_type will still work. */
1852 tree type = (lang_hooks.types.type_for_size
1853 (TYPE_PRECISION (TREE_TYPE (op)),
1854 TYPE_UNSIGNED (TREE_TYPE (op))));
1855 op = fold_convert (type, op);
1857 return op;
1860 /* This is a helper function of build_binary_op.
1862 For certain operations if both args were extended from the same
1863 smaller type, do the arithmetic in that type and then extend.
1865 BITWISE indicates a bitwise operation.
1866 For them, this optimization is safe only if
1867 both args are zero-extended or both are sign-extended.
1868 Otherwise, we might change the result.
1869 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1870 but calculated in (unsigned short) it would be (unsigned short)-1.
1872 tree
1873 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1875 int unsigned0, unsigned1;
1876 tree arg0, arg1;
1877 int uns;
1878 tree type;
1880 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1881 excessive narrowing when we call get_narrower below. For
1882 example, suppose that OP0 is of unsigned int extended
1883 from signed char and that RESULT_TYPE is long long int.
1884 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1885 like
1887 (long long int) (unsigned int) signed_char
1889 which get_narrower would narrow down to
1891 (unsigned int) signed char
1893 If we do not cast OP0 first, get_narrower would return
1894 signed_char, which is inconsistent with the case of the
1895 explicit cast. */
1896 op0 = convert (result_type, op0);
1897 op1 = convert (result_type, op1);
1899 arg0 = c_common_get_narrower (op0, &unsigned0);
1900 arg1 = c_common_get_narrower (op1, &unsigned1);
1902 /* UNS is 1 if the operation to be done is an unsigned one. */
1903 uns = TYPE_UNSIGNED (result_type);
1905 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1906 but it *requires* conversion to FINAL_TYPE. */
1908 if ((TYPE_PRECISION (TREE_TYPE (op0))
1909 == TYPE_PRECISION (TREE_TYPE (arg0)))
1910 && TREE_TYPE (op0) != result_type)
1911 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1912 if ((TYPE_PRECISION (TREE_TYPE (op1))
1913 == TYPE_PRECISION (TREE_TYPE (arg1)))
1914 && TREE_TYPE (op1) != result_type)
1915 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1917 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1919 /* For bitwise operations, signedness of nominal type
1920 does not matter. Consider only how operands were extended. */
1921 if (bitwise)
1922 uns = unsigned0;
1924 /* Note that in all three cases below we refrain from optimizing
1925 an unsigned operation on sign-extended args.
1926 That would not be valid. */
1928 /* Both args variable: if both extended in same way
1929 from same width, do it in that width.
1930 Do it unsigned if args were zero-extended. */
1931 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1932 < TYPE_PRECISION (result_type))
1933 && (TYPE_PRECISION (TREE_TYPE (arg1))
1934 == TYPE_PRECISION (TREE_TYPE (arg0)))
1935 && unsigned0 == unsigned1
1936 && (unsigned0 || !uns))
1937 return c_common_signed_or_unsigned_type
1938 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1940 else if (TREE_CODE (arg0) == INTEGER_CST
1941 && (unsigned1 || !uns)
1942 && (TYPE_PRECISION (TREE_TYPE (arg1))
1943 < TYPE_PRECISION (result_type))
1944 && (type
1945 = c_common_signed_or_unsigned_type (unsigned1,
1946 TREE_TYPE (arg1)))
1947 && !POINTER_TYPE_P (type)
1948 && int_fits_type_p (arg0, type))
1949 return type;
1951 else if (TREE_CODE (arg1) == INTEGER_CST
1952 && (unsigned0 || !uns)
1953 && (TYPE_PRECISION (TREE_TYPE (arg0))
1954 < TYPE_PRECISION (result_type))
1955 && (type
1956 = c_common_signed_or_unsigned_type (unsigned0,
1957 TREE_TYPE (arg0)))
1958 && !POINTER_TYPE_P (type)
1959 && int_fits_type_p (arg1, type))
1960 return type;
1962 return result_type;
1965 /* Checks if expression EXPR of real/integer type cannot be converted
1966 to the real/integer type TYPE. Function returns true when:
1967 * EXPR is a constant which cannot be exactly converted to TYPE
1968 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1969 for EXPR type and TYPE being both integers or both real.
1970 * EXPR is not a constant of real type and TYPE is an integer.
1971 * EXPR is not a constant of integer type which cannot be
1972 exactly converted to real type.
1973 Function allows conversions between types of different signedness and
1974 does not return true in that case. Function can produce signedness
1975 warnings if PRODUCE_WARNS is true. */
1976 bool
1977 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
1979 bool give_warning = false;
1980 tree expr_type = TREE_TYPE (expr);
1981 location_t loc = EXPR_LOC_OR_HERE (expr);
1983 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1985 /* Warn for real constant that is not an exact integer converted
1986 to integer type. */
1987 if (TREE_CODE (expr_type) == REAL_TYPE
1988 && TREE_CODE (type) == INTEGER_TYPE)
1990 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1991 give_warning = true;
1993 /* Warn for an integer constant that does not fit into integer type. */
1994 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1995 && TREE_CODE (type) == INTEGER_TYPE
1996 && !int_fits_type_p (expr, type))
1998 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1999 && tree_int_cst_sgn (expr) < 0)
2001 if (produce_warns)
2002 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2003 " implicitly converted to unsigned type");
2005 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2007 if (produce_warns)
2008 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2009 " constant value to negative integer");
2011 else
2012 give_warning = true;
2014 else if (TREE_CODE (type) == REAL_TYPE)
2016 /* Warn for an integer constant that does not fit into real type. */
2017 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2019 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2020 if (!exact_real_truncate (TYPE_MODE (type), &a))
2021 give_warning = true;
2023 /* Warn for a real constant that does not fit into a smaller
2024 real type. */
2025 else if (TREE_CODE (expr_type) == REAL_TYPE
2026 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2028 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2029 if (!exact_real_truncate (TYPE_MODE (type), &a))
2030 give_warning = true;
2034 else
2036 /* Warn for real types converted to integer types. */
2037 if (TREE_CODE (expr_type) == REAL_TYPE
2038 && TREE_CODE (type) == INTEGER_TYPE)
2039 give_warning = true;
2041 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2042 && TREE_CODE (type) == INTEGER_TYPE)
2044 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2045 expr = get_unwidened (expr, 0);
2046 expr_type = TREE_TYPE (expr);
2048 /* Don't warn for short y; short x = ((int)y & 0xff); */
2049 if (TREE_CODE (expr) == BIT_AND_EXPR
2050 || TREE_CODE (expr) == BIT_IOR_EXPR
2051 || TREE_CODE (expr) == BIT_XOR_EXPR)
2053 /* If both args were extended from a shortest type,
2054 use that type if that is safe. */
2055 expr_type = shorten_binary_op (expr_type,
2056 TREE_OPERAND (expr, 0),
2057 TREE_OPERAND (expr, 1),
2058 /* bitwise */1);
2060 if (TREE_CODE (expr) == BIT_AND_EXPR)
2062 tree op0 = TREE_OPERAND (expr, 0);
2063 tree op1 = TREE_OPERAND (expr, 1);
2064 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2065 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2067 /* If one of the operands is a non-negative constant
2068 that fits in the target type, then the type of the
2069 other operand does not matter. */
2070 if ((TREE_CODE (op0) == INTEGER_CST
2071 && int_fits_type_p (op0, c_common_signed_type (type))
2072 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2073 || (TREE_CODE (op1) == INTEGER_CST
2074 && int_fits_type_p (op1, c_common_signed_type (type))
2075 && int_fits_type_p (op1,
2076 c_common_unsigned_type (type))))
2077 return false;
2078 /* If constant is unsigned and fits in the target
2079 type, then the result will also fit. */
2080 else if ((TREE_CODE (op0) == INTEGER_CST
2081 && unsigned0
2082 && int_fits_type_p (op0, type))
2083 || (TREE_CODE (op1) == INTEGER_CST
2084 && unsigned1
2085 && int_fits_type_p (op1, type)))
2086 return false;
2089 /* Warn for integer types converted to smaller integer types. */
2090 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2091 give_warning = true;
2093 /* When they are the same width but different signedness,
2094 then the value may change. */
2095 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2096 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2097 /* Even when converted to a bigger type, if the type is
2098 unsigned but expr is signed, then negative values
2099 will be changed. */
2100 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2101 && produce_warns)
2102 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2103 "may change the sign of the result",
2104 type, expr_type);
2107 /* Warn for integer types converted to real types if and only if
2108 all the range of values of the integer type cannot be
2109 represented by the real type. */
2110 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2111 && TREE_CODE (type) == REAL_TYPE)
2113 tree type_low_bound, type_high_bound;
2114 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2116 /* Don't warn about char y = 0xff; float x = (int) y; */
2117 expr = get_unwidened (expr, 0);
2118 expr_type = TREE_TYPE (expr);
2120 type_low_bound = TYPE_MIN_VALUE (expr_type);
2121 type_high_bound = TYPE_MAX_VALUE (expr_type);
2122 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2123 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2125 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2126 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2127 give_warning = true;
2130 /* Warn for real types converted to smaller real types. */
2131 else if (TREE_CODE (expr_type) == REAL_TYPE
2132 && TREE_CODE (type) == REAL_TYPE
2133 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2134 give_warning = true;
2137 return give_warning;
2140 /* Warns if the conversion of EXPR to TYPE may alter a value.
2141 This is a helper function for warnings_for_convert_and_check. */
2143 static void
2144 conversion_warning (tree type, tree expr)
2146 tree expr_type = TREE_TYPE (expr);
2147 location_t loc = EXPR_LOC_OR_HERE (expr);
2149 if (!warn_conversion && !warn_sign_conversion)
2150 return;
2152 switch (TREE_CODE (expr))
2154 case EQ_EXPR:
2155 case NE_EXPR:
2156 case LE_EXPR:
2157 case GE_EXPR:
2158 case LT_EXPR:
2159 case GT_EXPR:
2160 case TRUTH_ANDIF_EXPR:
2161 case TRUTH_ORIF_EXPR:
2162 case TRUTH_AND_EXPR:
2163 case TRUTH_OR_EXPR:
2164 case TRUTH_XOR_EXPR:
2165 case TRUTH_NOT_EXPR:
2166 /* Conversion from boolean to a signed:1 bit-field (which only
2167 can hold the values 0 and -1) doesn't lose information - but
2168 it does change the value. */
2169 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2170 warning_at (loc, OPT_Wconversion,
2171 "conversion to %qT from boolean expression", type);
2172 return;
2174 case REAL_CST:
2175 case INTEGER_CST:
2176 if (unsafe_conversion_p (type, expr, true))
2177 warning_at (loc, OPT_Wconversion,
2178 "conversion to %qT alters %qT constant value",
2179 type, expr_type);
2180 return;
2182 case COND_EXPR:
2184 /* In case of COND_EXPR, if both operands are constants or
2185 COND_EXPR, then we do not care about the type of COND_EXPR,
2186 only about the conversion of each operand. */
2187 tree op1 = TREE_OPERAND (expr, 1);
2188 tree op2 = TREE_OPERAND (expr, 2);
2190 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
2191 || TREE_CODE (op1) == COND_EXPR)
2192 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
2193 || TREE_CODE (op2) == COND_EXPR))
2195 conversion_warning (type, op1);
2196 conversion_warning (type, op2);
2197 return;
2199 /* Fall through. */
2202 default: /* 'expr' is not a constant. */
2203 if (unsafe_conversion_p (type, expr, true))
2204 warning_at (loc, OPT_Wconversion,
2205 "conversion to %qT from %qT may alter its value",
2206 type, expr_type);
2210 /* Produce warnings after a conversion. RESULT is the result of
2211 converting EXPR to TYPE. This is a helper function for
2212 convert_and_check and cp_convert_and_check. */
2214 void
2215 warnings_for_convert_and_check (tree type, tree expr, tree result)
2217 if (TREE_CODE (expr) == INTEGER_CST
2218 && (TREE_CODE (type) == INTEGER_TYPE
2219 || TREE_CODE (type) == ENUMERAL_TYPE)
2220 && !int_fits_type_p (expr, type))
2222 /* Do not diagnose overflow in a constant expression merely
2223 because a conversion overflowed. */
2224 if (TREE_OVERFLOW (result))
2225 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2227 if (TYPE_UNSIGNED (type))
2229 /* This detects cases like converting -129 or 256 to
2230 unsigned char. */
2231 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2232 warning (OPT_Woverflow,
2233 "large integer implicitly truncated to unsigned type");
2234 else
2235 conversion_warning (type, expr);
2237 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2238 warning (OPT_Woverflow,
2239 "overflow in implicit constant conversion");
2240 /* No warning for converting 0x80000000 to int. */
2241 else if (pedantic
2242 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2243 || TYPE_PRECISION (TREE_TYPE (expr))
2244 != TYPE_PRECISION (type)))
2245 warning (OPT_Woverflow,
2246 "overflow in implicit constant conversion");
2248 else
2249 conversion_warning (type, expr);
2251 else if ((TREE_CODE (result) == INTEGER_CST
2252 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2253 warning (OPT_Woverflow,
2254 "overflow in implicit constant conversion");
2255 else
2256 conversion_warning (type, expr);
2260 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2261 Invoke this function on every expression that is converted implicitly,
2262 i.e. because of language rules and not because of an explicit cast. */
2264 tree
2265 convert_and_check (tree type, tree expr)
2267 tree result;
2268 tree expr_for_warning;
2270 /* Convert from a value with possible excess precision rather than
2271 via the semantic type, but do not warn about values not fitting
2272 exactly in the semantic type. */
2273 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2275 tree orig_type = TREE_TYPE (expr);
2276 expr = TREE_OPERAND (expr, 0);
2277 expr_for_warning = convert (orig_type, expr);
2278 if (orig_type == type)
2279 return expr_for_warning;
2281 else
2282 expr_for_warning = expr;
2284 if (TREE_TYPE (expr) == type)
2285 return expr;
2287 result = convert (type, expr);
2289 if (c_inhibit_evaluation_warnings == 0
2290 && !TREE_OVERFLOW_P (expr)
2291 && result != error_mark_node)
2292 warnings_for_convert_and_check (type, expr_for_warning, result);
2294 return result;
2297 /* A node in a list that describes references to variables (EXPR), which are
2298 either read accesses if WRITER is zero, or write accesses, in which case
2299 WRITER is the parent of EXPR. */
2300 struct tlist
2302 struct tlist *next;
2303 tree expr, writer;
2306 /* Used to implement a cache the results of a call to verify_tree. We only
2307 use this for SAVE_EXPRs. */
2308 struct tlist_cache
2310 struct tlist_cache *next;
2311 struct tlist *cache_before_sp;
2312 struct tlist *cache_after_sp;
2313 tree expr;
2316 /* Obstack to use when allocating tlist structures, and corresponding
2317 firstobj. */
2318 static struct obstack tlist_obstack;
2319 static char *tlist_firstobj = 0;
2321 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2322 warnings. */
2323 static struct tlist *warned_ids;
2324 /* SAVE_EXPRs need special treatment. We process them only once and then
2325 cache the results. */
2326 static struct tlist_cache *save_expr_cache;
2328 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2329 static void merge_tlist (struct tlist **, struct tlist *, int);
2330 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2331 static int warning_candidate_p (tree);
2332 static bool candidate_equal_p (const_tree, const_tree);
2333 static void warn_for_collisions (struct tlist *);
2334 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2335 static struct tlist *new_tlist (struct tlist *, tree, tree);
2337 /* Create a new struct tlist and fill in its fields. */
2338 static struct tlist *
2339 new_tlist (struct tlist *next, tree t, tree writer)
2341 struct tlist *l;
2342 l = XOBNEW (&tlist_obstack, struct tlist);
2343 l->next = next;
2344 l->expr = t;
2345 l->writer = writer;
2346 return l;
2349 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2350 is nonnull, we ignore any node we find which has a writer equal to it. */
2352 static void
2353 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2355 while (add)
2357 struct tlist *next = add->next;
2358 if (!copy)
2359 add->next = *to;
2360 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2361 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2362 add = next;
2366 /* Merge the nodes of ADD into TO. This merging process is done so that for
2367 each variable that already exists in TO, no new node is added; however if
2368 there is a write access recorded in ADD, and an occurrence on TO is only
2369 a read access, then the occurrence in TO will be modified to record the
2370 write. */
2372 static void
2373 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2375 struct tlist **end = to;
2377 while (*end)
2378 end = &(*end)->next;
2380 while (add)
2382 int found = 0;
2383 struct tlist *tmp2;
2384 struct tlist *next = add->next;
2386 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2387 if (candidate_equal_p (tmp2->expr, add->expr))
2389 found = 1;
2390 if (!tmp2->writer)
2391 tmp2->writer = add->writer;
2393 if (!found)
2395 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2396 end = &(*end)->next;
2397 *end = 0;
2399 add = next;
2403 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2404 references in list LIST conflict with it, excluding reads if ONLY writers
2405 is nonzero. */
2407 static void
2408 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2409 int only_writes)
2411 struct tlist *tmp;
2413 /* Avoid duplicate warnings. */
2414 for (tmp = warned_ids; tmp; tmp = tmp->next)
2415 if (candidate_equal_p (tmp->expr, written))
2416 return;
2418 while (list)
2420 if (candidate_equal_p (list->expr, written)
2421 && !candidate_equal_p (list->writer, writer)
2422 && (!only_writes || list->writer))
2424 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2425 warning_at (EXPR_LOC_OR_HERE (writer),
2426 OPT_Wsequence_point, "operation on %qE may be undefined",
2427 list->expr);
2429 list = list->next;
2433 /* Given a list LIST of references to variables, find whether any of these
2434 can cause conflicts due to missing sequence points. */
2436 static void
2437 warn_for_collisions (struct tlist *list)
2439 struct tlist *tmp;
2441 for (tmp = list; tmp; tmp = tmp->next)
2443 if (tmp->writer)
2444 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2448 /* Return nonzero if X is a tree that can be verified by the sequence point
2449 warnings. */
2450 static int
2451 warning_candidate_p (tree x)
2453 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2454 return 0;
2456 if (TREE_CODE (x) == BLOCK)
2457 return 0;
2459 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2460 (lvalue_p) crash on TRY/CATCH. */
2461 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2462 return 0;
2464 if (!lvalue_p (x))
2465 return 0;
2467 /* No point to track non-const calls, they will never satisfy
2468 operand_equal_p. */
2469 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2470 return 0;
2472 if (TREE_CODE (x) == STRING_CST)
2473 return 0;
2475 return 1;
2478 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2479 static bool
2480 candidate_equal_p (const_tree x, const_tree y)
2482 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2485 /* Walk the tree X, and record accesses to variables. If X is written by the
2486 parent tree, WRITER is the parent.
2487 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2488 expression or its only operand forces a sequence point, then everything up
2489 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2490 in PNO_SP.
2491 Once we return, we will have emitted warnings if any subexpression before
2492 such a sequence point could be undefined. On a higher level, however, the
2493 sequence point may not be relevant, and we'll merge the two lists.
2495 Example: (b++, a) + b;
2496 The call that processes the COMPOUND_EXPR will store the increment of B
2497 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2498 processes the PLUS_EXPR will need to merge the two lists so that
2499 eventually, all accesses end up on the same list (and we'll warn about the
2500 unordered subexpressions b++ and b.
2502 A note on merging. If we modify the former example so that our expression
2503 becomes
2504 (b++, b) + a
2505 care must be taken not simply to add all three expressions into the final
2506 PNO_SP list. The function merge_tlist takes care of that by merging the
2507 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2508 way, so that no more than one access to B is recorded. */
2510 static void
2511 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2512 tree writer)
2514 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2515 enum tree_code code;
2516 enum tree_code_class cl;
2518 /* X may be NULL if it is the operand of an empty statement expression
2519 ({ }). */
2520 if (x == NULL)
2521 return;
2523 restart:
2524 code = TREE_CODE (x);
2525 cl = TREE_CODE_CLASS (code);
2527 if (warning_candidate_p (x))
2528 *pno_sp = new_tlist (*pno_sp, x, writer);
2530 switch (code)
2532 case CONSTRUCTOR:
2533 return;
2535 case COMPOUND_EXPR:
2536 case TRUTH_ANDIF_EXPR:
2537 case TRUTH_ORIF_EXPR:
2538 tmp_before = tmp_nosp = tmp_list3 = 0;
2539 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2540 warn_for_collisions (tmp_nosp);
2541 merge_tlist (pbefore_sp, tmp_before, 0);
2542 merge_tlist (pbefore_sp, tmp_nosp, 0);
2543 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2544 merge_tlist (pbefore_sp, tmp_list3, 0);
2545 return;
2547 case COND_EXPR:
2548 tmp_before = tmp_list2 = 0;
2549 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2550 warn_for_collisions (tmp_list2);
2551 merge_tlist (pbefore_sp, tmp_before, 0);
2552 merge_tlist (pbefore_sp, tmp_list2, 1);
2554 tmp_list3 = tmp_nosp = 0;
2555 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2556 warn_for_collisions (tmp_nosp);
2557 merge_tlist (pbefore_sp, tmp_list3, 0);
2559 tmp_list3 = tmp_list2 = 0;
2560 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2561 warn_for_collisions (tmp_list2);
2562 merge_tlist (pbefore_sp, tmp_list3, 0);
2563 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2564 two first, to avoid warning for (a ? b++ : b++). */
2565 merge_tlist (&tmp_nosp, tmp_list2, 0);
2566 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2567 return;
2569 case PREDECREMENT_EXPR:
2570 case PREINCREMENT_EXPR:
2571 case POSTDECREMENT_EXPR:
2572 case POSTINCREMENT_EXPR:
2573 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2574 return;
2576 case MODIFY_EXPR:
2577 tmp_before = tmp_nosp = tmp_list3 = 0;
2578 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2579 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2580 /* Expressions inside the LHS are not ordered wrt. the sequence points
2581 in the RHS. Example:
2582 *a = (a++, 2)
2583 Despite the fact that the modification of "a" is in the before_sp
2584 list (tmp_before), it conflicts with the use of "a" in the LHS.
2585 We can handle this by adding the contents of tmp_list3
2586 to those of tmp_before, and redoing the collision warnings for that
2587 list. */
2588 add_tlist (&tmp_before, tmp_list3, x, 1);
2589 warn_for_collisions (tmp_before);
2590 /* Exclude the LHS itself here; we first have to merge it into the
2591 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2592 didn't exclude the LHS, we'd get it twice, once as a read and once
2593 as a write. */
2594 add_tlist (pno_sp, tmp_list3, x, 0);
2595 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2597 merge_tlist (pbefore_sp, tmp_before, 0);
2598 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2599 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2600 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2601 return;
2603 case CALL_EXPR:
2604 /* We need to warn about conflicts among arguments and conflicts between
2605 args and the function address. Side effects of the function address,
2606 however, are not ordered by the sequence point of the call. */
2608 call_expr_arg_iterator iter;
2609 tree arg;
2610 tmp_before = tmp_nosp = 0;
2611 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2612 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2614 tmp_list2 = tmp_list3 = 0;
2615 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2616 merge_tlist (&tmp_list3, tmp_list2, 0);
2617 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2619 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2620 warn_for_collisions (tmp_before);
2621 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2622 return;
2625 case TREE_LIST:
2626 /* Scan all the list, e.g. indices of multi dimensional array. */
2627 while (x)
2629 tmp_before = tmp_nosp = 0;
2630 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2631 merge_tlist (&tmp_nosp, tmp_before, 0);
2632 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2633 x = TREE_CHAIN (x);
2635 return;
2637 case SAVE_EXPR:
2639 struct tlist_cache *t;
2640 for (t = save_expr_cache; t; t = t->next)
2641 if (candidate_equal_p (t->expr, x))
2642 break;
2644 if (!t)
2646 t = XOBNEW (&tlist_obstack, struct tlist_cache);
2647 t->next = save_expr_cache;
2648 t->expr = x;
2649 save_expr_cache = t;
2651 tmp_before = tmp_nosp = 0;
2652 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2653 warn_for_collisions (tmp_nosp);
2655 tmp_list3 = 0;
2656 while (tmp_nosp)
2658 struct tlist *t = tmp_nosp;
2659 tmp_nosp = t->next;
2660 merge_tlist (&tmp_list3, t, 0);
2662 t->cache_before_sp = tmp_before;
2663 t->cache_after_sp = tmp_list3;
2665 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2666 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2667 return;
2670 case ADDR_EXPR:
2671 x = TREE_OPERAND (x, 0);
2672 if (DECL_P (x))
2673 return;
2674 writer = 0;
2675 goto restart;
2677 default:
2678 /* For other expressions, simply recurse on their operands.
2679 Manual tail recursion for unary expressions.
2680 Other non-expressions need not be processed. */
2681 if (cl == tcc_unary)
2683 x = TREE_OPERAND (x, 0);
2684 writer = 0;
2685 goto restart;
2687 else if (IS_EXPR_CODE_CLASS (cl))
2689 int lp;
2690 int max = TREE_OPERAND_LENGTH (x);
2691 for (lp = 0; lp < max; lp++)
2693 tmp_before = tmp_nosp = 0;
2694 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2695 merge_tlist (&tmp_nosp, tmp_before, 0);
2696 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2699 return;
2703 /* Try to warn for undefined behavior in EXPR due to missing sequence
2704 points. */
2706 DEBUG_FUNCTION void
2707 verify_sequence_points (tree expr)
2709 struct tlist *before_sp = 0, *after_sp = 0;
2711 warned_ids = 0;
2712 save_expr_cache = 0;
2713 if (tlist_firstobj == 0)
2715 gcc_obstack_init (&tlist_obstack);
2716 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2719 verify_tree (expr, &before_sp, &after_sp, 0);
2720 warn_for_collisions (after_sp);
2721 obstack_free (&tlist_obstack, tlist_firstobj);
2724 /* Validate the expression after `case' and apply default promotions. */
2726 static tree
2727 check_case_value (tree value)
2729 if (value == NULL_TREE)
2730 return value;
2732 if (TREE_CODE (value) == INTEGER_CST)
2733 /* Promote char or short to int. */
2734 value = perform_integral_promotions (value);
2735 else if (value != error_mark_node)
2737 error ("case label does not reduce to an integer constant");
2738 value = error_mark_node;
2741 constant_expression_warning (value);
2743 return value;
2746 /* See if the case values LOW and HIGH are in the range of the original
2747 type (i.e. before the default conversion to int) of the switch testing
2748 expression.
2749 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2750 the type before promoting it. CASE_LOW_P is a pointer to the lower
2751 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2752 if the case is not a case range.
2753 The caller has to make sure that we are not called with NULL for
2754 CASE_LOW_P (i.e. the default case).
2755 Returns true if the case label is in range of ORIG_TYPE (saturated or
2756 untouched) or false if the label is out of range. */
2758 static bool
2759 check_case_bounds (tree type, tree orig_type,
2760 tree *case_low_p, tree *case_high_p)
2762 tree min_value, max_value;
2763 tree case_low = *case_low_p;
2764 tree case_high = case_high_p ? *case_high_p : case_low;
2766 /* If there was a problem with the original type, do nothing. */
2767 if (orig_type == error_mark_node)
2768 return true;
2770 min_value = TYPE_MIN_VALUE (orig_type);
2771 max_value = TYPE_MAX_VALUE (orig_type);
2773 /* Case label is less than minimum for type. */
2774 if (tree_int_cst_compare (case_low, min_value) < 0
2775 && tree_int_cst_compare (case_high, min_value) < 0)
2777 warning (0, "case label value is less than minimum value for type");
2778 return false;
2781 /* Case value is greater than maximum for type. */
2782 if (tree_int_cst_compare (case_low, max_value) > 0
2783 && tree_int_cst_compare (case_high, max_value) > 0)
2785 warning (0, "case label value exceeds maximum value for type");
2786 return false;
2789 /* Saturate lower case label value to minimum. */
2790 if (tree_int_cst_compare (case_high, min_value) >= 0
2791 && tree_int_cst_compare (case_low, min_value) < 0)
2793 warning (0, "lower value in case label range"
2794 " less than minimum value for type");
2795 case_low = min_value;
2798 /* Saturate upper case label value to maximum. */
2799 if (tree_int_cst_compare (case_low, max_value) <= 0
2800 && tree_int_cst_compare (case_high, max_value) > 0)
2802 warning (0, "upper value in case label range"
2803 " exceeds maximum value for type");
2804 case_high = max_value;
2807 if (*case_low_p != case_low)
2808 *case_low_p = convert (type, case_low);
2809 if (case_high_p && *case_high_p != case_high)
2810 *case_high_p = convert (type, case_high);
2812 return true;
2815 /* Return an integer type with BITS bits of precision,
2816 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2818 tree
2819 c_common_type_for_size (unsigned int bits, int unsignedp)
2821 if (bits == TYPE_PRECISION (integer_type_node))
2822 return unsignedp ? unsigned_type_node : integer_type_node;
2824 if (bits == TYPE_PRECISION (signed_char_type_node))
2825 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2827 if (bits == TYPE_PRECISION (short_integer_type_node))
2828 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2830 if (bits == TYPE_PRECISION (long_integer_type_node))
2831 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2833 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2834 return (unsignedp ? long_long_unsigned_type_node
2835 : long_long_integer_type_node);
2837 if (int128_integer_type_node
2838 && bits == TYPE_PRECISION (int128_integer_type_node))
2839 return (unsignedp ? int128_unsigned_type_node
2840 : int128_integer_type_node);
2842 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2843 return (unsignedp ? widest_unsigned_literal_type_node
2844 : widest_integer_literal_type_node);
2846 if (bits <= TYPE_PRECISION (intQI_type_node))
2847 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2849 if (bits <= TYPE_PRECISION (intHI_type_node))
2850 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2852 if (bits <= TYPE_PRECISION (intSI_type_node))
2853 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2855 if (bits <= TYPE_PRECISION (intDI_type_node))
2856 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2858 return 0;
2861 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2862 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2863 and saturating if SATP is nonzero, otherwise not saturating. */
2865 tree
2866 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2867 int unsignedp, int satp)
2869 enum machine_mode mode;
2870 if (ibit == 0)
2871 mode = unsignedp ? UQQmode : QQmode;
2872 else
2873 mode = unsignedp ? UHAmode : HAmode;
2875 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2876 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2877 break;
2879 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2881 sorry ("GCC cannot support operators with integer types and "
2882 "fixed-point types that have too many integral and "
2883 "fractional bits together");
2884 return 0;
2887 return c_common_type_for_mode (mode, satp);
2890 /* Used for communication between c_common_type_for_mode and
2891 c_register_builtin_type. */
2892 static GTY(()) tree registered_builtin_types;
2894 /* Return a data type that has machine mode MODE.
2895 If the mode is an integer,
2896 then UNSIGNEDP selects between signed and unsigned types.
2897 If the mode is a fixed-point mode,
2898 then UNSIGNEDP selects between saturating and nonsaturating types. */
2900 tree
2901 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
2903 tree t;
2905 if (mode == TYPE_MODE (integer_type_node))
2906 return unsignedp ? unsigned_type_node : integer_type_node;
2908 if (mode == TYPE_MODE (signed_char_type_node))
2909 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2911 if (mode == TYPE_MODE (short_integer_type_node))
2912 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2914 if (mode == TYPE_MODE (long_integer_type_node))
2915 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2917 if (mode == TYPE_MODE (long_long_integer_type_node))
2918 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2920 if (int128_integer_type_node
2921 && mode == TYPE_MODE (int128_integer_type_node))
2922 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
2924 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2925 return unsignedp ? widest_unsigned_literal_type_node
2926 : widest_integer_literal_type_node;
2928 if (mode == QImode)
2929 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2931 if (mode == HImode)
2932 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2934 if (mode == SImode)
2935 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2937 if (mode == DImode)
2938 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2940 #if HOST_BITS_PER_WIDE_INT >= 64
2941 if (mode == TYPE_MODE (intTI_type_node))
2942 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2943 #endif
2945 if (mode == TYPE_MODE (float_type_node))
2946 return float_type_node;
2948 if (mode == TYPE_MODE (double_type_node))
2949 return double_type_node;
2951 if (mode == TYPE_MODE (long_double_type_node))
2952 return long_double_type_node;
2954 if (mode == TYPE_MODE (void_type_node))
2955 return void_type_node;
2957 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2958 return (unsignedp
2959 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2960 : make_signed_type (GET_MODE_PRECISION (mode)));
2962 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2963 return (unsignedp
2964 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2965 : make_signed_type (GET_MODE_PRECISION (mode)));
2967 if (COMPLEX_MODE_P (mode))
2969 enum machine_mode inner_mode;
2970 tree inner_type;
2972 if (mode == TYPE_MODE (complex_float_type_node))
2973 return complex_float_type_node;
2974 if (mode == TYPE_MODE (complex_double_type_node))
2975 return complex_double_type_node;
2976 if (mode == TYPE_MODE (complex_long_double_type_node))
2977 return complex_long_double_type_node;
2979 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2980 return complex_integer_type_node;
2982 inner_mode = GET_MODE_INNER (mode);
2983 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2984 if (inner_type != NULL_TREE)
2985 return build_complex_type (inner_type);
2987 else if (VECTOR_MODE_P (mode))
2989 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2990 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2991 if (inner_type != NULL_TREE)
2992 return build_vector_type_for_mode (inner_type, mode);
2995 if (mode == TYPE_MODE (dfloat32_type_node))
2996 return dfloat32_type_node;
2997 if (mode == TYPE_MODE (dfloat64_type_node))
2998 return dfloat64_type_node;
2999 if (mode == TYPE_MODE (dfloat128_type_node))
3000 return dfloat128_type_node;
3002 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3004 if (mode == TYPE_MODE (short_fract_type_node))
3005 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3006 if (mode == TYPE_MODE (fract_type_node))
3007 return unsignedp ? sat_fract_type_node : fract_type_node;
3008 if (mode == TYPE_MODE (long_fract_type_node))
3009 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3010 if (mode == TYPE_MODE (long_long_fract_type_node))
3011 return unsignedp ? sat_long_long_fract_type_node
3012 : long_long_fract_type_node;
3014 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3015 return unsignedp ? sat_unsigned_short_fract_type_node
3016 : unsigned_short_fract_type_node;
3017 if (mode == TYPE_MODE (unsigned_fract_type_node))
3018 return unsignedp ? sat_unsigned_fract_type_node
3019 : unsigned_fract_type_node;
3020 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3021 return unsignedp ? sat_unsigned_long_fract_type_node
3022 : unsigned_long_fract_type_node;
3023 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3024 return unsignedp ? sat_unsigned_long_long_fract_type_node
3025 : unsigned_long_long_fract_type_node;
3027 if (mode == TYPE_MODE (short_accum_type_node))
3028 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3029 if (mode == TYPE_MODE (accum_type_node))
3030 return unsignedp ? sat_accum_type_node : accum_type_node;
3031 if (mode == TYPE_MODE (long_accum_type_node))
3032 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3033 if (mode == TYPE_MODE (long_long_accum_type_node))
3034 return unsignedp ? sat_long_long_accum_type_node
3035 : long_long_accum_type_node;
3037 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3038 return unsignedp ? sat_unsigned_short_accum_type_node
3039 : unsigned_short_accum_type_node;
3040 if (mode == TYPE_MODE (unsigned_accum_type_node))
3041 return unsignedp ? sat_unsigned_accum_type_node
3042 : unsigned_accum_type_node;
3043 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3044 return unsignedp ? sat_unsigned_long_accum_type_node
3045 : unsigned_long_accum_type_node;
3046 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3047 return unsignedp ? sat_unsigned_long_long_accum_type_node
3048 : unsigned_long_long_accum_type_node;
3050 if (mode == QQmode)
3051 return unsignedp ? sat_qq_type_node : qq_type_node;
3052 if (mode == HQmode)
3053 return unsignedp ? sat_hq_type_node : hq_type_node;
3054 if (mode == SQmode)
3055 return unsignedp ? sat_sq_type_node : sq_type_node;
3056 if (mode == DQmode)
3057 return unsignedp ? sat_dq_type_node : dq_type_node;
3058 if (mode == TQmode)
3059 return unsignedp ? sat_tq_type_node : tq_type_node;
3061 if (mode == UQQmode)
3062 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3063 if (mode == UHQmode)
3064 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3065 if (mode == USQmode)
3066 return unsignedp ? sat_usq_type_node : usq_type_node;
3067 if (mode == UDQmode)
3068 return unsignedp ? sat_udq_type_node : udq_type_node;
3069 if (mode == UTQmode)
3070 return unsignedp ? sat_utq_type_node : utq_type_node;
3072 if (mode == HAmode)
3073 return unsignedp ? sat_ha_type_node : ha_type_node;
3074 if (mode == SAmode)
3075 return unsignedp ? sat_sa_type_node : sa_type_node;
3076 if (mode == DAmode)
3077 return unsignedp ? sat_da_type_node : da_type_node;
3078 if (mode == TAmode)
3079 return unsignedp ? sat_ta_type_node : ta_type_node;
3081 if (mode == UHAmode)
3082 return unsignedp ? sat_uha_type_node : uha_type_node;
3083 if (mode == USAmode)
3084 return unsignedp ? sat_usa_type_node : usa_type_node;
3085 if (mode == UDAmode)
3086 return unsignedp ? sat_uda_type_node : uda_type_node;
3087 if (mode == UTAmode)
3088 return unsignedp ? sat_uta_type_node : uta_type_node;
3091 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3092 if (TYPE_MODE (TREE_VALUE (t)) == mode
3093 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3094 return TREE_VALUE (t);
3096 return 0;
3099 tree
3100 c_common_unsigned_type (tree type)
3102 return c_common_signed_or_unsigned_type (1, type);
3105 /* Return a signed type the same as TYPE in other respects. */
3107 tree
3108 c_common_signed_type (tree type)
3110 return c_common_signed_or_unsigned_type (0, type);
3113 /* Return a type the same as TYPE except unsigned or
3114 signed according to UNSIGNEDP. */
3116 tree
3117 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3119 tree type1;
3121 /* This block of code emulates the behavior of the old
3122 c_common_unsigned_type. In particular, it returns
3123 long_unsigned_type_node if passed a long, even when a int would
3124 have the same size. This is necessary for warnings to work
3125 correctly in archs where sizeof(int) == sizeof(long) */
3127 type1 = TYPE_MAIN_VARIANT (type);
3128 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3129 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3130 if (type1 == integer_type_node || type1 == unsigned_type_node)
3131 return unsignedp ? unsigned_type_node : integer_type_node;
3132 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3133 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3134 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3135 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3136 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3137 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3138 if (int128_integer_type_node
3139 && (type1 == int128_integer_type_node
3140 || type1 == int128_unsigned_type_node))
3141 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3142 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3143 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3144 #if HOST_BITS_PER_WIDE_INT >= 64
3145 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3146 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3147 #endif
3148 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3149 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3150 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3151 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3152 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3153 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3154 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3155 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3157 #define C_COMMON_FIXED_TYPES(NAME) \
3158 if (type1 == short_ ## NAME ## _type_node \
3159 || type1 == unsigned_short_ ## NAME ## _type_node) \
3160 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3161 : short_ ## NAME ## _type_node; \
3162 if (type1 == NAME ## _type_node \
3163 || type1 == unsigned_ ## NAME ## _type_node) \
3164 return unsignedp ? unsigned_ ## NAME ## _type_node \
3165 : NAME ## _type_node; \
3166 if (type1 == long_ ## NAME ## _type_node \
3167 || type1 == unsigned_long_ ## NAME ## _type_node) \
3168 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3169 : long_ ## NAME ## _type_node; \
3170 if (type1 == long_long_ ## NAME ## _type_node \
3171 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3172 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3173 : long_long_ ## NAME ## _type_node;
3175 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3176 if (type1 == NAME ## _type_node \
3177 || type1 == u ## NAME ## _type_node) \
3178 return unsignedp ? u ## NAME ## _type_node \
3179 : NAME ## _type_node;
3181 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3182 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3183 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3184 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3185 : sat_ ## short_ ## NAME ## _type_node; \
3186 if (type1 == sat_ ## NAME ## _type_node \
3187 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3188 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3189 : sat_ ## NAME ## _type_node; \
3190 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3191 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3192 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3193 : sat_ ## long_ ## NAME ## _type_node; \
3194 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3195 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3196 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3197 : sat_ ## long_long_ ## NAME ## _type_node;
3199 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3200 if (type1 == sat_ ## NAME ## _type_node \
3201 || type1 == sat_ ## u ## NAME ## _type_node) \
3202 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3203 : sat_ ## NAME ## _type_node;
3205 C_COMMON_FIXED_TYPES (fract);
3206 C_COMMON_FIXED_TYPES_SAT (fract);
3207 C_COMMON_FIXED_TYPES (accum);
3208 C_COMMON_FIXED_TYPES_SAT (accum);
3210 C_COMMON_FIXED_MODE_TYPES (qq);
3211 C_COMMON_FIXED_MODE_TYPES (hq);
3212 C_COMMON_FIXED_MODE_TYPES (sq);
3213 C_COMMON_FIXED_MODE_TYPES (dq);
3214 C_COMMON_FIXED_MODE_TYPES (tq);
3215 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3216 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3217 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3218 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3219 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3220 C_COMMON_FIXED_MODE_TYPES (ha);
3221 C_COMMON_FIXED_MODE_TYPES (sa);
3222 C_COMMON_FIXED_MODE_TYPES (da);
3223 C_COMMON_FIXED_MODE_TYPES (ta);
3224 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3225 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3226 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3227 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3229 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3230 the precision; they have precision set to match their range, but
3231 may use a wider mode to match an ABI. If we change modes, we may
3232 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3233 the precision as well, so as to yield correct results for
3234 bit-field types. C++ does not have these separate bit-field
3235 types, and producing a signed or unsigned variant of an
3236 ENUMERAL_TYPE may cause other problems as well. */
3238 if (!INTEGRAL_TYPE_P (type)
3239 || TYPE_UNSIGNED (type) == unsignedp)
3240 return type;
3242 #define TYPE_OK(node) \
3243 (TYPE_MODE (type) == TYPE_MODE (node) \
3244 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3245 if (TYPE_OK (signed_char_type_node))
3246 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3247 if (TYPE_OK (integer_type_node))
3248 return unsignedp ? unsigned_type_node : integer_type_node;
3249 if (TYPE_OK (short_integer_type_node))
3250 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3251 if (TYPE_OK (long_integer_type_node))
3252 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3253 if (TYPE_OK (long_long_integer_type_node))
3254 return (unsignedp ? long_long_unsigned_type_node
3255 : long_long_integer_type_node);
3256 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3257 return (unsignedp ? int128_unsigned_type_node
3258 : int128_integer_type_node);
3259 if (TYPE_OK (widest_integer_literal_type_node))
3260 return (unsignedp ? widest_unsigned_literal_type_node
3261 : widest_integer_literal_type_node);
3263 #if HOST_BITS_PER_WIDE_INT >= 64
3264 if (TYPE_OK (intTI_type_node))
3265 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3266 #endif
3267 if (TYPE_OK (intDI_type_node))
3268 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3269 if (TYPE_OK (intSI_type_node))
3270 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3271 if (TYPE_OK (intHI_type_node))
3272 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3273 if (TYPE_OK (intQI_type_node))
3274 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3275 #undef TYPE_OK
3277 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3280 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3282 tree
3283 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3285 /* Extended integer types of the same width as a standard type have
3286 lesser rank, so those of the same width as int promote to int or
3287 unsigned int and are valid for printf formats expecting int or
3288 unsigned int. To avoid such special cases, avoid creating
3289 extended integer types for bit-fields if a standard integer type
3290 is available. */
3291 if (width == TYPE_PRECISION (integer_type_node))
3292 return unsignedp ? unsigned_type_node : integer_type_node;
3293 if (width == TYPE_PRECISION (signed_char_type_node))
3294 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3295 if (width == TYPE_PRECISION (short_integer_type_node))
3296 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3297 if (width == TYPE_PRECISION (long_integer_type_node))
3298 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3299 if (width == TYPE_PRECISION (long_long_integer_type_node))
3300 return (unsignedp ? long_long_unsigned_type_node
3301 : long_long_integer_type_node);
3302 if (int128_integer_type_node
3303 && width == TYPE_PRECISION (int128_integer_type_node))
3304 return (unsignedp ? int128_unsigned_type_node
3305 : int128_integer_type_node);
3306 return build_nonstandard_integer_type (width, unsignedp);
3309 /* The C version of the register_builtin_type langhook. */
3311 void
3312 c_register_builtin_type (tree type, const char* name)
3314 tree decl;
3316 decl = build_decl (UNKNOWN_LOCATION,
3317 TYPE_DECL, get_identifier (name), type);
3318 DECL_ARTIFICIAL (decl) = 1;
3319 if (!TYPE_NAME (type))
3320 TYPE_NAME (type) = decl;
3321 pushdecl (decl);
3323 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3326 /* Print an error message for invalid operands to arith operation
3327 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3328 LOCATION is the location of the message. */
3330 void
3331 binary_op_error (location_t location, enum tree_code code,
3332 tree type0, tree type1)
3334 const char *opname;
3336 switch (code)
3338 case PLUS_EXPR:
3339 opname = "+"; break;
3340 case MINUS_EXPR:
3341 opname = "-"; break;
3342 case MULT_EXPR:
3343 opname = "*"; break;
3344 case MAX_EXPR:
3345 opname = "max"; break;
3346 case MIN_EXPR:
3347 opname = "min"; break;
3348 case EQ_EXPR:
3349 opname = "=="; break;
3350 case NE_EXPR:
3351 opname = "!="; break;
3352 case LE_EXPR:
3353 opname = "<="; break;
3354 case GE_EXPR:
3355 opname = ">="; break;
3356 case LT_EXPR:
3357 opname = "<"; break;
3358 case GT_EXPR:
3359 opname = ">"; break;
3360 case LSHIFT_EXPR:
3361 opname = "<<"; break;
3362 case RSHIFT_EXPR:
3363 opname = ">>"; break;
3364 case TRUNC_MOD_EXPR:
3365 case FLOOR_MOD_EXPR:
3366 opname = "%"; break;
3367 case TRUNC_DIV_EXPR:
3368 case FLOOR_DIV_EXPR:
3369 opname = "/"; break;
3370 case BIT_AND_EXPR:
3371 opname = "&"; break;
3372 case BIT_IOR_EXPR:
3373 opname = "|"; break;
3374 case TRUTH_ANDIF_EXPR:
3375 opname = "&&"; break;
3376 case TRUTH_ORIF_EXPR:
3377 opname = "||"; break;
3378 case BIT_XOR_EXPR:
3379 opname = "^"; break;
3380 default:
3381 gcc_unreachable ();
3383 error_at (location,
3384 "invalid operands to binary %s (have %qT and %qT)", opname,
3385 type0, type1);
3388 /* Subroutine of build_binary_op, used for comparison operations.
3389 See if the operands have both been converted from subword integer types
3390 and, if so, perhaps change them both back to their original type.
3391 This function is also responsible for converting the two operands
3392 to the proper common type for comparison.
3394 The arguments of this function are all pointers to local variables
3395 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3396 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3398 If this function returns nonzero, it means that the comparison has
3399 a constant value. What this function returns is an expression for
3400 that value. */
3402 tree
3403 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3404 enum tree_code *rescode_ptr)
3406 tree type;
3407 tree op0 = *op0_ptr;
3408 tree op1 = *op1_ptr;
3409 int unsignedp0, unsignedp1;
3410 int real1, real2;
3411 tree primop0, primop1;
3412 enum tree_code code = *rescode_ptr;
3414 /* Throw away any conversions to wider types
3415 already present in the operands. */
3417 primop0 = c_common_get_narrower (op0, &unsignedp0);
3418 primop1 = c_common_get_narrower (op1, &unsignedp1);
3420 /* If primopN is first sign-extended from primopN's precision to opN's
3421 precision, then zero-extended from opN's precision to
3422 *restype_ptr precision, shortenings might be invalid. */
3423 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3424 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3425 && !unsignedp0
3426 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3427 primop0 = op0;
3428 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3429 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3430 && !unsignedp1
3431 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3432 primop1 = op1;
3434 /* Handle the case that OP0 does not *contain* a conversion
3435 but it *requires* conversion to FINAL_TYPE. */
3437 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3438 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3439 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3440 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3442 /* If one of the operands must be floated, we cannot optimize. */
3443 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3444 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3446 /* If first arg is constant, swap the args (changing operation
3447 so value is preserved), for canonicalization. Don't do this if
3448 the second arg is 0. */
3450 if (TREE_CONSTANT (primop0)
3451 && !integer_zerop (primop1) && !real_zerop (primop1)
3452 && !fixed_zerop (primop1))
3454 tree tem = primop0;
3455 int temi = unsignedp0;
3456 primop0 = primop1;
3457 primop1 = tem;
3458 tem = op0;
3459 op0 = op1;
3460 op1 = tem;
3461 *op0_ptr = op0;
3462 *op1_ptr = op1;
3463 unsignedp0 = unsignedp1;
3464 unsignedp1 = temi;
3465 temi = real1;
3466 real1 = real2;
3467 real2 = temi;
3469 switch (code)
3471 case LT_EXPR:
3472 code = GT_EXPR;
3473 break;
3474 case GT_EXPR:
3475 code = LT_EXPR;
3476 break;
3477 case LE_EXPR:
3478 code = GE_EXPR;
3479 break;
3480 case GE_EXPR:
3481 code = LE_EXPR;
3482 break;
3483 default:
3484 break;
3486 *rescode_ptr = code;
3489 /* If comparing an integer against a constant more bits wide,
3490 maybe we can deduce a value of 1 or 0 independent of the data.
3491 Or else truncate the constant now
3492 rather than extend the variable at run time.
3494 This is only interesting if the constant is the wider arg.
3495 Also, it is not safe if the constant is unsigned and the
3496 variable arg is signed, since in this case the variable
3497 would be sign-extended and then regarded as unsigned.
3498 Our technique fails in this case because the lowest/highest
3499 possible unsigned results don't follow naturally from the
3500 lowest/highest possible values of the variable operand.
3501 For just EQ_EXPR and NE_EXPR there is another technique that
3502 could be used: see if the constant can be faithfully represented
3503 in the other operand's type, by truncating it and reextending it
3504 and see if that preserves the constant's value. */
3506 if (!real1 && !real2
3507 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3508 && TREE_CODE (primop1) == INTEGER_CST
3509 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3511 int min_gt, max_gt, min_lt, max_lt;
3512 tree maxval, minval;
3513 /* 1 if comparison is nominally unsigned. */
3514 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3515 tree val;
3517 type = c_common_signed_or_unsigned_type (unsignedp0,
3518 TREE_TYPE (primop0));
3520 maxval = TYPE_MAX_VALUE (type);
3521 minval = TYPE_MIN_VALUE (type);
3523 if (unsignedp && !unsignedp0)
3524 *restype_ptr = c_common_signed_type (*restype_ptr);
3526 if (TREE_TYPE (primop1) != *restype_ptr)
3528 /* Convert primop1 to target type, but do not introduce
3529 additional overflow. We know primop1 is an int_cst. */
3530 primop1 = force_fit_type_double (*restype_ptr,
3531 tree_to_double_int (primop1),
3532 0, TREE_OVERFLOW (primop1));
3534 if (type != *restype_ptr)
3536 minval = convert (*restype_ptr, minval);
3537 maxval = convert (*restype_ptr, maxval);
3540 if (unsignedp && unsignedp0)
3542 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3543 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3544 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3545 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3547 else
3549 min_gt = INT_CST_LT (primop1, minval);
3550 max_gt = INT_CST_LT (primop1, maxval);
3551 min_lt = INT_CST_LT (minval, primop1);
3552 max_lt = INT_CST_LT (maxval, primop1);
3555 val = 0;
3556 /* This used to be a switch, but Genix compiler can't handle that. */
3557 if (code == NE_EXPR)
3559 if (max_lt || min_gt)
3560 val = truthvalue_true_node;
3562 else if (code == EQ_EXPR)
3564 if (max_lt || min_gt)
3565 val = truthvalue_false_node;
3567 else if (code == LT_EXPR)
3569 if (max_lt)
3570 val = truthvalue_true_node;
3571 if (!min_lt)
3572 val = truthvalue_false_node;
3574 else if (code == GT_EXPR)
3576 if (min_gt)
3577 val = truthvalue_true_node;
3578 if (!max_gt)
3579 val = truthvalue_false_node;
3581 else if (code == LE_EXPR)
3583 if (!max_gt)
3584 val = truthvalue_true_node;
3585 if (min_gt)
3586 val = truthvalue_false_node;
3588 else if (code == GE_EXPR)
3590 if (!min_lt)
3591 val = truthvalue_true_node;
3592 if (max_lt)
3593 val = truthvalue_false_node;
3596 /* If primop0 was sign-extended and unsigned comparison specd,
3597 we did a signed comparison above using the signed type bounds.
3598 But the comparison we output must be unsigned.
3600 Also, for inequalities, VAL is no good; but if the signed
3601 comparison had *any* fixed result, it follows that the
3602 unsigned comparison just tests the sign in reverse
3603 (positive values are LE, negative ones GE).
3604 So we can generate an unsigned comparison
3605 against an extreme value of the signed type. */
3607 if (unsignedp && !unsignedp0)
3609 if (val != 0)
3610 switch (code)
3612 case LT_EXPR:
3613 case GE_EXPR:
3614 primop1 = TYPE_MIN_VALUE (type);
3615 val = 0;
3616 break;
3618 case LE_EXPR:
3619 case GT_EXPR:
3620 primop1 = TYPE_MAX_VALUE (type);
3621 val = 0;
3622 break;
3624 default:
3625 break;
3627 type = c_common_unsigned_type (type);
3630 if (TREE_CODE (primop0) != INTEGER_CST)
3632 if (val == truthvalue_false_node)
3633 warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type");
3634 if (val == truthvalue_true_node)
3635 warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type");
3638 if (val != 0)
3640 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3641 if (TREE_SIDE_EFFECTS (primop0))
3642 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3643 return val;
3646 /* Value is not predetermined, but do the comparison
3647 in the type of the operand that is not constant.
3648 TYPE is already properly set. */
3651 /* If either arg is decimal float and the other is float, find the
3652 proper common type to use for comparison. */
3653 else if (real1 && real2
3654 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3655 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3656 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3658 else if (real1 && real2
3659 && (TYPE_PRECISION (TREE_TYPE (primop0))
3660 == TYPE_PRECISION (TREE_TYPE (primop1))))
3661 type = TREE_TYPE (primop0);
3663 /* If args' natural types are both narrower than nominal type
3664 and both extend in the same manner, compare them
3665 in the type of the wider arg.
3666 Otherwise must actually extend both to the nominal
3667 common type lest different ways of extending
3668 alter the result.
3669 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3671 else if (unsignedp0 == unsignedp1 && real1 == real2
3672 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3673 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3675 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3676 type = c_common_signed_or_unsigned_type (unsignedp0
3677 || TYPE_UNSIGNED (*restype_ptr),
3678 type);
3679 /* Make sure shorter operand is extended the right way
3680 to match the longer operand. */
3681 primop0
3682 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3683 TREE_TYPE (primop0)),
3684 primop0);
3685 primop1
3686 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3687 TREE_TYPE (primop1)),
3688 primop1);
3690 else
3692 /* Here we must do the comparison on the nominal type
3693 using the args exactly as we received them. */
3694 type = *restype_ptr;
3695 primop0 = op0;
3696 primop1 = op1;
3698 if (!real1 && !real2 && integer_zerop (primop1)
3699 && TYPE_UNSIGNED (*restype_ptr))
3701 tree value = 0;
3702 switch (code)
3704 case GE_EXPR:
3705 /* All unsigned values are >= 0, so we warn. However,
3706 if OP0 is a constant that is >= 0, the signedness of
3707 the comparison isn't an issue, so suppress the
3708 warning. */
3709 if (warn_type_limits && !in_system_header
3710 && !(TREE_CODE (primop0) == INTEGER_CST
3711 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3712 primop0))))
3713 warning (OPT_Wtype_limits,
3714 "comparison of unsigned expression >= 0 is always true");
3715 value = truthvalue_true_node;
3716 break;
3718 case LT_EXPR:
3719 if (warn_type_limits && !in_system_header
3720 && !(TREE_CODE (primop0) == INTEGER_CST
3721 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3722 primop0))))
3723 warning (OPT_Wtype_limits,
3724 "comparison of unsigned expression < 0 is always false");
3725 value = truthvalue_false_node;
3726 break;
3728 default:
3729 break;
3732 if (value != 0)
3734 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3735 if (TREE_SIDE_EFFECTS (primop0))
3736 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3737 primop0, value);
3738 return value;
3743 *op0_ptr = convert (type, primop0);
3744 *op1_ptr = convert (type, primop1);
3746 *restype_ptr = truthvalue_type_node;
3748 return 0;
3751 /* Return a tree for the sum or difference (RESULTCODE says which)
3752 of pointer PTROP and integer INTOP. */
3754 tree
3755 pointer_int_sum (location_t loc, enum tree_code resultcode,
3756 tree ptrop, tree intop)
3758 tree size_exp, ret;
3760 /* The result is a pointer of the same type that is being added. */
3761 tree result_type = TREE_TYPE (ptrop);
3763 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3765 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3766 "pointer of type %<void *%> used in arithmetic");
3767 size_exp = integer_one_node;
3769 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3771 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3772 "pointer to a function used in arithmetic");
3773 size_exp = integer_one_node;
3775 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3777 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3778 "pointer to member function used in arithmetic");
3779 size_exp = integer_one_node;
3781 else
3782 size_exp = size_in_bytes (TREE_TYPE (result_type));
3784 /* We are manipulating pointer values, so we don't need to warn
3785 about relying on undefined signed overflow. We disable the
3786 warning here because we use integer types so fold won't know that
3787 they are really pointers. */
3788 fold_defer_overflow_warnings ();
3790 /* If what we are about to multiply by the size of the elements
3791 contains a constant term, apply distributive law
3792 and multiply that constant term separately.
3793 This helps produce common subexpressions. */
3794 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3795 && !TREE_CONSTANT (intop)
3796 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3797 && TREE_CONSTANT (size_exp)
3798 /* If the constant comes from pointer subtraction,
3799 skip this optimization--it would cause an error. */
3800 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3801 /* If the constant is unsigned, and smaller than the pointer size,
3802 then we must skip this optimization. This is because it could cause
3803 an overflow error if the constant is negative but INTOP is not. */
3804 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3805 || (TYPE_PRECISION (TREE_TYPE (intop))
3806 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3808 enum tree_code subcode = resultcode;
3809 tree int_type = TREE_TYPE (intop);
3810 if (TREE_CODE (intop) == MINUS_EXPR)
3811 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3812 /* Convert both subexpression types to the type of intop,
3813 because weird cases involving pointer arithmetic
3814 can result in a sum or difference with different type args. */
3815 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3816 subcode, ptrop,
3817 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3818 intop = convert (int_type, TREE_OPERAND (intop, 0));
3821 /* Convert the integer argument to a type the same size as sizetype
3822 so the multiply won't overflow spuriously. */
3823 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3824 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3825 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3826 TYPE_UNSIGNED (sizetype)), intop);
3828 /* Replace the integer argument with a suitable product by the object size.
3829 Do this multiplication as signed, then convert to the appropriate type
3830 for the pointer operation and disregard an overflow that occured only
3831 because of the sign-extension change in the latter conversion. */
3833 tree t = build_binary_op (loc,
3834 MULT_EXPR, intop,
3835 convert (TREE_TYPE (intop), size_exp), 1);
3836 intop = convert (sizetype, t);
3837 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3838 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
3839 TREE_INT_CST_HIGH (intop));
3842 /* Create the sum or difference. */
3843 if (resultcode == MINUS_EXPR)
3844 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3846 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3848 fold_undefer_and_ignore_overflow_warnings ();
3850 return ret;
3853 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3854 and if NON_CONST is known not to be permitted in an evaluated part
3855 of a constant expression. */
3857 tree
3858 c_wrap_maybe_const (tree expr, bool non_const)
3860 bool nowarning = TREE_NO_WARNING (expr);
3861 location_t loc = EXPR_LOCATION (expr);
3863 /* This should never be called for C++. */
3864 if (c_dialect_cxx ())
3865 gcc_unreachable ();
3867 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3868 STRIP_TYPE_NOPS (expr);
3869 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3870 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3871 if (nowarning)
3872 TREE_NO_WARNING (expr) = 1;
3873 protected_set_expr_location (expr, loc);
3875 return expr;
3878 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
3879 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
3880 around the SAVE_EXPR if needed so that c_fully_fold does not need
3881 to look inside SAVE_EXPRs. */
3883 tree
3884 c_save_expr (tree expr)
3886 bool maybe_const = true;
3887 if (c_dialect_cxx ())
3888 return save_expr (expr);
3889 expr = c_fully_fold (expr, false, &maybe_const);
3890 expr = save_expr (expr);
3891 if (!maybe_const)
3892 expr = c_wrap_maybe_const (expr, true);
3893 return expr;
3896 /* Return whether EXPR is a declaration whose address can never be
3897 NULL. */
3899 bool
3900 decl_with_nonnull_addr_p (const_tree expr)
3902 return (DECL_P (expr)
3903 && (TREE_CODE (expr) == PARM_DECL
3904 || TREE_CODE (expr) == LABEL_DECL
3905 || !DECL_WEAK (expr)));
3908 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3909 or for an `if' or `while' statement or ?..: exp. It should already
3910 have been validated to be of suitable type; otherwise, a bad
3911 diagnostic may result.
3913 The EXPR is located at LOCATION.
3915 This preparation consists of taking the ordinary
3916 representation of an expression expr and producing a valid tree
3917 boolean expression describing whether expr is nonzero. We could
3918 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3919 but we optimize comparisons, &&, ||, and !.
3921 The resulting type should always be `truthvalue_type_node'. */
3923 tree
3924 c_common_truthvalue_conversion (location_t location, tree expr)
3926 switch (TREE_CODE (expr))
3928 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3929 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3930 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3931 case ORDERED_EXPR: case UNORDERED_EXPR:
3932 if (TREE_TYPE (expr) == truthvalue_type_node)
3933 return expr;
3934 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3935 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3936 goto ret;
3938 case TRUTH_ANDIF_EXPR:
3939 case TRUTH_ORIF_EXPR:
3940 case TRUTH_AND_EXPR:
3941 case TRUTH_OR_EXPR:
3942 case TRUTH_XOR_EXPR:
3943 if (TREE_TYPE (expr) == truthvalue_type_node)
3944 return expr;
3945 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3946 c_common_truthvalue_conversion (location,
3947 TREE_OPERAND (expr, 0)),
3948 c_common_truthvalue_conversion (location,
3949 TREE_OPERAND (expr, 1)));
3950 goto ret;
3952 case TRUTH_NOT_EXPR:
3953 if (TREE_TYPE (expr) == truthvalue_type_node)
3954 return expr;
3955 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3956 c_common_truthvalue_conversion (location,
3957 TREE_OPERAND (expr, 0)));
3958 goto ret;
3960 case ERROR_MARK:
3961 return expr;
3963 case INTEGER_CST:
3964 return integer_zerop (expr) ? truthvalue_false_node
3965 : truthvalue_true_node;
3967 case REAL_CST:
3968 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3969 ? truthvalue_true_node
3970 : truthvalue_false_node;
3972 case FIXED_CST:
3973 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3974 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3975 ? truthvalue_true_node
3976 : truthvalue_false_node;
3978 case FUNCTION_DECL:
3979 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
3980 /* Fall through. */
3982 case ADDR_EXPR:
3984 tree inner = TREE_OPERAND (expr, 0);
3985 if (decl_with_nonnull_addr_p (inner))
3987 /* Common Ada/Pascal programmer's mistake. */
3988 warning_at (location,
3989 OPT_Waddress,
3990 "the address of %qD will always evaluate as %<true%>",
3991 inner);
3992 return truthvalue_true_node;
3994 break;
3997 case COMPLEX_EXPR:
3998 expr = build_binary_op (EXPR_LOCATION (expr),
3999 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4000 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4001 c_common_truthvalue_conversion (location,
4002 TREE_OPERAND (expr, 0)),
4003 c_common_truthvalue_conversion (location,
4004 TREE_OPERAND (expr, 1)),
4006 goto ret;
4008 case NEGATE_EXPR:
4009 case ABS_EXPR:
4010 case FLOAT_EXPR:
4011 case EXCESS_PRECISION_EXPR:
4012 /* These don't change whether an object is nonzero or zero. */
4013 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4015 case LROTATE_EXPR:
4016 case RROTATE_EXPR:
4017 /* These don't change whether an object is zero or nonzero, but
4018 we can't ignore them if their second arg has side-effects. */
4019 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4021 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4022 TREE_OPERAND (expr, 1),
4023 c_common_truthvalue_conversion
4024 (location, TREE_OPERAND (expr, 0)));
4025 goto ret;
4027 else
4028 return c_common_truthvalue_conversion (location,
4029 TREE_OPERAND (expr, 0));
4031 case COND_EXPR:
4032 /* Distribute the conversion into the arms of a COND_EXPR. */
4033 if (c_dialect_cxx ())
4035 tree op1 = TREE_OPERAND (expr, 1);
4036 tree op2 = TREE_OPERAND (expr, 2);
4037 /* In C++ one of the arms might have void type if it is throw. */
4038 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4039 op1 = c_common_truthvalue_conversion (location, op1);
4040 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4041 op2 = c_common_truthvalue_conversion (location, op2);
4042 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4043 TREE_OPERAND (expr, 0), op1, op2);
4044 goto ret;
4046 else
4048 /* Folding will happen later for C. */
4049 expr = build3 (COND_EXPR, truthvalue_type_node,
4050 TREE_OPERAND (expr, 0),
4051 c_common_truthvalue_conversion (location,
4052 TREE_OPERAND (expr, 1)),
4053 c_common_truthvalue_conversion (location,
4054 TREE_OPERAND (expr, 2)));
4055 goto ret;
4058 CASE_CONVERT:
4060 tree totype = TREE_TYPE (expr);
4061 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4063 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4064 since that affects how `default_conversion' will behave. */
4065 if (TREE_CODE (totype) == REFERENCE_TYPE
4066 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4067 break;
4068 /* Don't strip a conversion from C++0x scoped enum, since they
4069 don't implicitly convert to other types. */
4070 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4071 && ENUM_IS_SCOPED (fromtype))
4072 break;
4073 /* If this isn't narrowing the argument, we can ignore it. */
4074 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4075 return c_common_truthvalue_conversion (location,
4076 TREE_OPERAND (expr, 0));
4078 break;
4080 case MODIFY_EXPR:
4081 if (!TREE_NO_WARNING (expr)
4082 && warn_parentheses)
4084 warning (OPT_Wparentheses,
4085 "suggest parentheses around assignment used as truth value");
4086 TREE_NO_WARNING (expr) = 1;
4088 break;
4090 default:
4091 break;
4094 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4096 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4097 expr = (build_binary_op
4098 (EXPR_LOCATION (expr),
4099 (TREE_SIDE_EFFECTS (expr)
4100 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4101 c_common_truthvalue_conversion
4102 (location,
4103 build_unary_op (location, REALPART_EXPR, t, 0)),
4104 c_common_truthvalue_conversion
4105 (location,
4106 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4107 0));
4108 goto ret;
4111 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4113 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4114 FCONST0 (TYPE_MODE
4115 (TREE_TYPE (expr))));
4116 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4118 else
4119 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4121 ret:
4122 protected_set_expr_location (expr, location);
4123 return expr;
4126 static void def_builtin_1 (enum built_in_function fncode,
4127 const char *name,
4128 enum built_in_class fnclass,
4129 tree fntype, tree libtype,
4130 bool both_p, bool fallback_p, bool nonansi_p,
4131 tree fnattrs, bool implicit_p);
4134 /* Apply the TYPE_QUALS to the new DECL. */
4136 void
4137 c_apply_type_quals_to_decl (int type_quals, tree decl)
4139 tree type = TREE_TYPE (decl);
4141 if (type == error_mark_node)
4142 return;
4144 if ((type_quals & TYPE_QUAL_CONST)
4145 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4146 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4147 constructor can produce constant init, so rely on cp_finish_decl to
4148 clear TREE_READONLY if the variable has non-constant init. */
4149 TREE_READONLY (decl) = 1;
4150 if (type_quals & TYPE_QUAL_VOLATILE)
4152 TREE_SIDE_EFFECTS (decl) = 1;
4153 TREE_THIS_VOLATILE (decl) = 1;
4155 if (type_quals & TYPE_QUAL_RESTRICT)
4157 while (type && TREE_CODE (type) == ARRAY_TYPE)
4158 /* Allow 'restrict' on arrays of pointers.
4159 FIXME currently we just ignore it. */
4160 type = TREE_TYPE (type);
4161 if (!type
4162 || !POINTER_TYPE_P (type)
4163 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4164 error ("invalid use of %<restrict%>");
4168 /* Hash function for the problem of multiple type definitions in
4169 different files. This must hash all types that will compare
4170 equal via comptypes to the same value. In practice it hashes
4171 on some of the simple stuff and leaves the details to comptypes. */
4173 static hashval_t
4174 c_type_hash (const void *p)
4176 int n_elements;
4177 int shift, size;
4178 const_tree const t = (const_tree) p;
4179 tree t2;
4180 switch (TREE_CODE (t))
4182 /* For pointers, hash on pointee type plus some swizzling. */
4183 case POINTER_TYPE:
4184 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4185 /* Hash on number of elements and total size. */
4186 case ENUMERAL_TYPE:
4187 shift = 3;
4188 t2 = TYPE_VALUES (t);
4189 break;
4190 case RECORD_TYPE:
4191 shift = 0;
4192 t2 = TYPE_FIELDS (t);
4193 break;
4194 case QUAL_UNION_TYPE:
4195 shift = 1;
4196 t2 = TYPE_FIELDS (t);
4197 break;
4198 case UNION_TYPE:
4199 shift = 2;
4200 t2 = TYPE_FIELDS (t);
4201 break;
4202 default:
4203 gcc_unreachable ();
4205 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4206 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4207 n_elements = list_length (t2);
4208 /* We might have a VLA here. */
4209 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4210 size = 0;
4211 else
4212 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4213 return ((size << 24) | (n_elements << shift));
4216 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4218 /* Return the typed-based alias set for T, which may be an expression
4219 or a type. Return -1 if we don't do anything special. */
4221 alias_set_type
4222 c_common_get_alias_set (tree t)
4224 tree u;
4225 PTR *slot;
4227 /* For VLAs, use the alias set of the element type rather than the
4228 default of alias set 0 for types compared structurally. */
4229 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4231 if (TREE_CODE (t) == ARRAY_TYPE)
4232 return get_alias_set (TREE_TYPE (t));
4233 return -1;
4236 /* Permit type-punning when accessing a union, provided the access
4237 is directly through the union. For example, this code does not
4238 permit taking the address of a union member and then storing
4239 through it. Even the type-punning allowed here is a GCC
4240 extension, albeit a common and useful one; the C standard says
4241 that such accesses have implementation-defined behavior. */
4242 for (u = t;
4243 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4244 u = TREE_OPERAND (u, 0))
4245 if (TREE_CODE (u) == COMPONENT_REF
4246 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4247 return 0;
4249 /* That's all the expressions we handle specially. */
4250 if (!TYPE_P (t))
4251 return -1;
4253 /* The C standard guarantees that any object may be accessed via an
4254 lvalue that has character type. */
4255 if (t == char_type_node
4256 || t == signed_char_type_node
4257 || t == unsigned_char_type_node)
4258 return 0;
4260 /* The C standard specifically allows aliasing between signed and
4261 unsigned variants of the same type. We treat the signed
4262 variant as canonical. */
4263 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4265 tree t1 = c_common_signed_type (t);
4267 /* t1 == t can happen for boolean nodes which are always unsigned. */
4268 if (t1 != t)
4269 return get_alias_set (t1);
4272 /* Handle the case of multiple type nodes referring to "the same" type,
4273 which occurs with IMA. These share an alias set. FIXME: Currently only
4274 C90 is handled. (In C99 type compatibility is not transitive, which
4275 complicates things mightily. The alias set splay trees can theoretically
4276 represent this, but insertion is tricky when you consider all the
4277 different orders things might arrive in.) */
4279 if (c_language != clk_c || flag_isoc99)
4280 return -1;
4282 /* Save time if there's only one input file. */
4283 if (num_in_fnames == 1)
4284 return -1;
4286 /* Pointers need special handling if they point to any type that
4287 needs special handling (below). */
4288 if (TREE_CODE (t) == POINTER_TYPE)
4290 tree t2;
4291 /* Find bottom type under any nested POINTERs. */
4292 for (t2 = TREE_TYPE (t);
4293 TREE_CODE (t2) == POINTER_TYPE;
4294 t2 = TREE_TYPE (t2))
4296 if (TREE_CODE (t2) != RECORD_TYPE
4297 && TREE_CODE (t2) != ENUMERAL_TYPE
4298 && TREE_CODE (t2) != QUAL_UNION_TYPE
4299 && TREE_CODE (t2) != UNION_TYPE)
4300 return -1;
4301 if (TYPE_SIZE (t2) == 0)
4302 return -1;
4304 /* These are the only cases that need special handling. */
4305 if (TREE_CODE (t) != RECORD_TYPE
4306 && TREE_CODE (t) != ENUMERAL_TYPE
4307 && TREE_CODE (t) != QUAL_UNION_TYPE
4308 && TREE_CODE (t) != UNION_TYPE
4309 && TREE_CODE (t) != POINTER_TYPE)
4310 return -1;
4311 /* Undefined? */
4312 if (TYPE_SIZE (t) == 0)
4313 return -1;
4315 /* Look up t in hash table. Only one of the compatible types within each
4316 alias set is recorded in the table. */
4317 if (!type_hash_table)
4318 type_hash_table = htab_create_ggc (1021, c_type_hash,
4319 (htab_eq) lang_hooks.types_compatible_p,
4320 NULL);
4321 slot = htab_find_slot (type_hash_table, t, INSERT);
4322 if (*slot != NULL)
4324 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4325 return TYPE_ALIAS_SET ((tree)*slot);
4327 else
4328 /* Our caller will assign and record (in t) a new alias set; all we need
4329 to do is remember t in the hash table. */
4330 *slot = t;
4332 return -1;
4335 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4336 the second parameter indicates which OPERATOR is being applied.
4337 The COMPLAIN flag controls whether we should diagnose possibly
4338 ill-formed constructs or not. LOC is the location of the SIZEOF or
4339 TYPEOF operator. */
4341 tree
4342 c_sizeof_or_alignof_type (location_t loc,
4343 tree type, bool is_sizeof, int complain)
4345 const char *op_name;
4346 tree value = NULL;
4347 enum tree_code type_code = TREE_CODE (type);
4349 op_name = is_sizeof ? "sizeof" : "__alignof__";
4351 if (type_code == FUNCTION_TYPE)
4353 if (is_sizeof)
4355 if (complain && (pedantic || warn_pointer_arith))
4356 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4357 "invalid application of %<sizeof%> to a function type");
4358 else if (!complain)
4359 return error_mark_node;
4360 value = size_one_node;
4362 else
4364 if (complain)
4366 if (c_dialect_cxx ())
4367 pedwarn (loc, OPT_pedantic, "ISO C++ does not permit "
4368 "%<alignof%> applied to a function type");
4369 else
4370 pedwarn (loc, OPT_pedantic, "ISO C does not permit "
4371 "%<_Alignof%> applied to a function type");
4373 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4376 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4378 if (type_code == VOID_TYPE
4379 && complain && (pedantic || warn_pointer_arith))
4380 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4381 "invalid application of %qs to a void type", op_name);
4382 else if (!complain)
4383 return error_mark_node;
4384 value = size_one_node;
4386 else if (!COMPLETE_TYPE_P (type)
4387 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4389 if (complain)
4390 error_at (loc, "invalid application of %qs to incomplete type %qT",
4391 op_name, type);
4392 return error_mark_node;
4394 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4395 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4397 if (complain)
4398 error_at (loc, "invalid application of %qs to array type %qT of "
4399 "incomplete element type", op_name, type);
4400 return error_mark_node;
4402 else
4404 if (is_sizeof)
4405 /* Convert in case a char is more than one unit. */
4406 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4407 size_int (TYPE_PRECISION (char_type_node)
4408 / BITS_PER_UNIT));
4409 else
4410 value = size_int (TYPE_ALIGN_UNIT (type));
4413 /* VALUE will have an integer type with TYPE_IS_SIZETYPE set.
4414 TYPE_IS_SIZETYPE means that certain things (like overflow) will
4415 never happen. However, this node should really have type
4416 `size_t', which is just a typedef for an ordinary integer type. */
4417 value = fold_convert_loc (loc, size_type_node, value);
4418 gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));
4420 return value;
4423 /* Implement the __alignof keyword: Return the minimum required
4424 alignment of EXPR, measured in bytes. For VAR_DECLs,
4425 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4426 from an "aligned" __attribute__ specification). LOC is the
4427 location of the ALIGNOF operator. */
4429 tree
4430 c_alignof_expr (location_t loc, tree expr)
4432 tree t;
4434 if (VAR_OR_FUNCTION_DECL_P (expr))
4435 t = size_int (DECL_ALIGN_UNIT (expr));
4437 else if (TREE_CODE (expr) == COMPONENT_REF
4438 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4440 error_at (loc, "%<__alignof%> applied to a bit-field");
4441 t = size_one_node;
4443 else if (TREE_CODE (expr) == COMPONENT_REF
4444 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4445 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4447 else if (TREE_CODE (expr) == INDIRECT_REF)
4449 tree t = TREE_OPERAND (expr, 0);
4450 tree best = t;
4451 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4453 while (CONVERT_EXPR_P (t)
4454 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4456 int thisalign;
4458 t = TREE_OPERAND (t, 0);
4459 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4460 if (thisalign > bestalign)
4461 best = t, bestalign = thisalign;
4463 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4465 else
4466 return c_alignof (loc, TREE_TYPE (expr));
4468 return fold_convert_loc (loc, size_type_node, t);
4471 /* Handle C and C++ default attributes. */
4473 enum built_in_attribute
4475 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4476 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4477 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4478 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4479 #include "builtin-attrs.def"
4480 #undef DEF_ATTR_NULL_TREE
4481 #undef DEF_ATTR_INT
4482 #undef DEF_ATTR_IDENT
4483 #undef DEF_ATTR_TREE_LIST
4484 ATTR_LAST
4487 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4489 static void c_init_attributes (void);
4491 enum c_builtin_type
4493 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4494 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4495 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4496 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4497 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4498 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4499 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4500 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4501 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4502 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4503 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4504 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4505 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4506 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4507 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4508 NAME,
4509 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4510 #include "builtin-types.def"
4511 #undef DEF_PRIMITIVE_TYPE
4512 #undef DEF_FUNCTION_TYPE_0
4513 #undef DEF_FUNCTION_TYPE_1
4514 #undef DEF_FUNCTION_TYPE_2
4515 #undef DEF_FUNCTION_TYPE_3
4516 #undef DEF_FUNCTION_TYPE_4
4517 #undef DEF_FUNCTION_TYPE_5
4518 #undef DEF_FUNCTION_TYPE_6
4519 #undef DEF_FUNCTION_TYPE_7
4520 #undef DEF_FUNCTION_TYPE_VAR_0
4521 #undef DEF_FUNCTION_TYPE_VAR_1
4522 #undef DEF_FUNCTION_TYPE_VAR_2
4523 #undef DEF_FUNCTION_TYPE_VAR_3
4524 #undef DEF_FUNCTION_TYPE_VAR_4
4525 #undef DEF_FUNCTION_TYPE_VAR_5
4526 #undef DEF_POINTER_TYPE
4527 BT_LAST
4530 typedef enum c_builtin_type builtin_type;
4532 /* A temporary array for c_common_nodes_and_builtins. Used in
4533 communication with def_fn_type. */
4534 static tree builtin_types[(int) BT_LAST + 1];
4536 /* A helper function for c_common_nodes_and_builtins. Build function type
4537 for DEF with return type RET and N arguments. If VAR is true, then the
4538 function should be variadic after those N arguments.
4540 Takes special care not to ICE if any of the types involved are
4541 error_mark_node, which indicates that said type is not in fact available
4542 (see builtin_type_for_size). In which case the function type as a whole
4543 should be error_mark_node. */
4545 static void
4546 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4548 tree t;
4549 tree *args = XALLOCAVEC (tree, n);
4550 va_list list;
4551 int i;
4553 va_start (list, n);
4554 for (i = 0; i < n; ++i)
4556 builtin_type a = (builtin_type) va_arg (list, int);
4557 t = builtin_types[a];
4558 if (t == error_mark_node)
4559 goto egress;
4560 args[i] = t;
4563 t = builtin_types[ret];
4564 if (t == error_mark_node)
4565 goto egress;
4566 if (var)
4567 t = build_varargs_function_type_array (t, n, args);
4568 else
4569 t = build_function_type_array (t, n, args);
4571 egress:
4572 builtin_types[def] = t;
4573 va_end (list);
4576 /* Build builtin functions common to both C and C++ language
4577 frontends. */
4579 static void
4580 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4582 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4583 builtin_types[ENUM] = VALUE;
4584 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4585 def_fn_type (ENUM, RETURN, 0, 0);
4586 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4587 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4588 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4589 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4590 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4591 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4592 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4593 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4594 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4595 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4596 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4597 ARG6) \
4598 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4599 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4600 ARG6, ARG7) \
4601 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4602 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4603 def_fn_type (ENUM, RETURN, 1, 0);
4604 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4605 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4606 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4607 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4608 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4609 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4610 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4611 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4612 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4613 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4614 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4615 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4617 #include "builtin-types.def"
4619 #undef DEF_PRIMITIVE_TYPE
4620 #undef DEF_FUNCTION_TYPE_1
4621 #undef DEF_FUNCTION_TYPE_2
4622 #undef DEF_FUNCTION_TYPE_3
4623 #undef DEF_FUNCTION_TYPE_4
4624 #undef DEF_FUNCTION_TYPE_5
4625 #undef DEF_FUNCTION_TYPE_6
4626 #undef DEF_FUNCTION_TYPE_VAR_0
4627 #undef DEF_FUNCTION_TYPE_VAR_1
4628 #undef DEF_FUNCTION_TYPE_VAR_2
4629 #undef DEF_FUNCTION_TYPE_VAR_3
4630 #undef DEF_FUNCTION_TYPE_VAR_4
4631 #undef DEF_FUNCTION_TYPE_VAR_5
4632 #undef DEF_POINTER_TYPE
4633 builtin_types[(int) BT_LAST] = NULL_TREE;
4635 c_init_attributes ();
4637 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4638 NONANSI_P, ATTRS, IMPLICIT, COND) \
4639 if (NAME && COND) \
4640 def_builtin_1 (ENUM, NAME, CLASS, \
4641 builtin_types[(int) TYPE], \
4642 builtin_types[(int) LIBTYPE], \
4643 BOTH_P, FALLBACK_P, NONANSI_P, \
4644 built_in_attributes[(int) ATTRS], IMPLICIT);
4645 #include "builtins.def"
4646 #undef DEF_BUILTIN
4648 targetm.init_builtins ();
4650 build_common_builtin_nodes ();
4652 if (flag_mudflap)
4653 mudflap_init ();
4656 /* Like get_identifier, but avoid warnings about null arguments when
4657 the argument may be NULL for targets where GCC lacks stdint.h type
4658 information. */
4660 static inline tree
4661 c_get_ident (const char *id)
4663 return get_identifier (id);
4666 /* Build tree nodes and builtin functions common to both C and C++ language
4667 frontends. */
4669 void
4670 c_common_nodes_and_builtins (void)
4672 int char16_type_size;
4673 int char32_type_size;
4674 int wchar_type_size;
4675 tree array_domain_type;
4676 tree va_list_ref_type_node;
4677 tree va_list_arg_type_node;
4679 build_common_tree_nodes (flag_signed_char, flag_short_double);
4681 /* Define `int' and `char' first so that dbx will output them first. */
4682 record_builtin_type (RID_INT, NULL, integer_type_node);
4683 record_builtin_type (RID_CHAR, "char", char_type_node);
4685 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4686 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4687 but not C. Are the conditionals here needed? */
4688 if (c_dialect_cxx ())
4689 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4690 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4691 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4692 record_builtin_type (RID_MAX, "long unsigned int",
4693 long_unsigned_type_node);
4694 if (int128_integer_type_node != NULL_TREE)
4696 record_builtin_type (RID_INT128, "__int128",
4697 int128_integer_type_node);
4698 record_builtin_type (RID_MAX, "__int128 unsigned",
4699 int128_unsigned_type_node);
4701 if (c_dialect_cxx ())
4702 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4703 record_builtin_type (RID_MAX, "long long int",
4704 long_long_integer_type_node);
4705 record_builtin_type (RID_MAX, "long long unsigned int",
4706 long_long_unsigned_type_node);
4707 if (c_dialect_cxx ())
4708 record_builtin_type (RID_MAX, "long long unsigned",
4709 long_long_unsigned_type_node);
4710 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4711 record_builtin_type (RID_MAX, "short unsigned int",
4712 short_unsigned_type_node);
4713 if (c_dialect_cxx ())
4714 record_builtin_type (RID_MAX, "unsigned short",
4715 short_unsigned_type_node);
4717 /* Define both `signed char' and `unsigned char'. */
4718 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4719 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4721 /* These are types that c_common_type_for_size and
4722 c_common_type_for_mode use. */
4723 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4724 TYPE_DECL, NULL_TREE,
4725 intQI_type_node));
4726 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4727 TYPE_DECL, NULL_TREE,
4728 intHI_type_node));
4729 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4730 TYPE_DECL, NULL_TREE,
4731 intSI_type_node));
4732 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4733 TYPE_DECL, NULL_TREE,
4734 intDI_type_node));
4735 #if HOST_BITS_PER_WIDE_INT >= 64
4736 if (targetm.scalar_mode_supported_p (TImode))
4737 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4738 TYPE_DECL,
4739 get_identifier ("__int128_t"),
4740 intTI_type_node));
4741 #endif
4742 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4743 TYPE_DECL, NULL_TREE,
4744 unsigned_intQI_type_node));
4745 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4746 TYPE_DECL, NULL_TREE,
4747 unsigned_intHI_type_node));
4748 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4749 TYPE_DECL, NULL_TREE,
4750 unsigned_intSI_type_node));
4751 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4752 TYPE_DECL, NULL_TREE,
4753 unsigned_intDI_type_node));
4754 #if HOST_BITS_PER_WIDE_INT >= 64
4755 if (targetm.scalar_mode_supported_p (TImode))
4756 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4757 TYPE_DECL,
4758 get_identifier ("__uint128_t"),
4759 unsigned_intTI_type_node));
4760 #endif
4762 /* Create the widest literal types. */
4763 widest_integer_literal_type_node
4764 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
4765 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4766 TYPE_DECL, NULL_TREE,
4767 widest_integer_literal_type_node));
4769 widest_unsigned_literal_type_node
4770 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
4771 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4772 TYPE_DECL, NULL_TREE,
4773 widest_unsigned_literal_type_node));
4775 signed_size_type_node = c_common_signed_type (size_type_node);
4777 pid_type_node =
4778 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4780 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4781 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4782 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4784 /* Only supported decimal floating point extension if the target
4785 actually supports underlying modes. */
4786 if (targetm.scalar_mode_supported_p (SDmode)
4787 && targetm.scalar_mode_supported_p (DDmode)
4788 && targetm.scalar_mode_supported_p (TDmode))
4790 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4791 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4792 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4795 if (targetm.fixed_point_supported_p ())
4797 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4798 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4799 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4800 record_builtin_type (RID_MAX, "long long _Fract",
4801 long_long_fract_type_node);
4802 record_builtin_type (RID_MAX, "unsigned short _Fract",
4803 unsigned_short_fract_type_node);
4804 record_builtin_type (RID_MAX, "unsigned _Fract",
4805 unsigned_fract_type_node);
4806 record_builtin_type (RID_MAX, "unsigned long _Fract",
4807 unsigned_long_fract_type_node);
4808 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4809 unsigned_long_long_fract_type_node);
4810 record_builtin_type (RID_MAX, "_Sat short _Fract",
4811 sat_short_fract_type_node);
4812 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4813 record_builtin_type (RID_MAX, "_Sat long _Fract",
4814 sat_long_fract_type_node);
4815 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4816 sat_long_long_fract_type_node);
4817 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4818 sat_unsigned_short_fract_type_node);
4819 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4820 sat_unsigned_fract_type_node);
4821 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4822 sat_unsigned_long_fract_type_node);
4823 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4824 sat_unsigned_long_long_fract_type_node);
4825 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4826 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4827 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4828 record_builtin_type (RID_MAX, "long long _Accum",
4829 long_long_accum_type_node);
4830 record_builtin_type (RID_MAX, "unsigned short _Accum",
4831 unsigned_short_accum_type_node);
4832 record_builtin_type (RID_MAX, "unsigned _Accum",
4833 unsigned_accum_type_node);
4834 record_builtin_type (RID_MAX, "unsigned long _Accum",
4835 unsigned_long_accum_type_node);
4836 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4837 unsigned_long_long_accum_type_node);
4838 record_builtin_type (RID_MAX, "_Sat short _Accum",
4839 sat_short_accum_type_node);
4840 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4841 record_builtin_type (RID_MAX, "_Sat long _Accum",
4842 sat_long_accum_type_node);
4843 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4844 sat_long_long_accum_type_node);
4845 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4846 sat_unsigned_short_accum_type_node);
4847 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4848 sat_unsigned_accum_type_node);
4849 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4850 sat_unsigned_long_accum_type_node);
4851 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4852 sat_unsigned_long_long_accum_type_node);
4856 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4857 TYPE_DECL,
4858 get_identifier ("complex int"),
4859 complex_integer_type_node));
4860 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4861 TYPE_DECL,
4862 get_identifier ("complex float"),
4863 complex_float_type_node));
4864 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4865 TYPE_DECL,
4866 get_identifier ("complex double"),
4867 complex_double_type_node));
4868 lang_hooks.decls.pushdecl
4869 (build_decl (UNKNOWN_LOCATION,
4870 TYPE_DECL, get_identifier ("complex long double"),
4871 complex_long_double_type_node));
4873 if (c_dialect_cxx ())
4874 /* For C++, make fileptr_type_node a distinct void * type until
4875 FILE type is defined. */
4876 fileptr_type_node = build_variant_type_copy (ptr_type_node);
4878 record_builtin_type (RID_VOID, NULL, void_type_node);
4880 /* Set the TYPE_NAME for any variants that were built before
4881 record_builtin_type gave names to the built-in types. */
4883 tree void_name = TYPE_NAME (void_type_node);
4884 TYPE_NAME (void_type_node) = NULL_TREE;
4885 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4886 = void_name;
4887 TYPE_NAME (void_type_node) = void_name;
4890 /* This node must not be shared. */
4891 void_zero_node = make_node (INTEGER_CST);
4892 TREE_TYPE (void_zero_node) = void_type_node;
4894 void_list_node = build_void_list_node ();
4896 /* Make a type to be the domain of a few array types
4897 whose domains don't really matter.
4898 200 is small enough that it always fits in size_t
4899 and large enough that it can hold most function names for the
4900 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4901 array_domain_type = build_index_type (size_int (200));
4903 /* Make a type for arrays of characters.
4904 With luck nothing will ever really depend on the length of this
4905 array type. */
4906 char_array_type_node
4907 = build_array_type (char_type_node, array_domain_type);
4909 /* Likewise for arrays of ints. */
4910 int_array_type_node
4911 = build_array_type (integer_type_node, array_domain_type);
4913 string_type_node = build_pointer_type (char_type_node);
4914 const_string_type_node
4915 = build_pointer_type (build_qualified_type
4916 (char_type_node, TYPE_QUAL_CONST));
4918 /* This is special for C++ so functions can be overloaded. */
4919 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4920 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4921 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4922 underlying_wchar_type_node = wchar_type_node;
4923 if (c_dialect_cxx ())
4925 if (TYPE_UNSIGNED (wchar_type_node))
4926 wchar_type_node = make_unsigned_type (wchar_type_size);
4927 else
4928 wchar_type_node = make_signed_type (wchar_type_size);
4929 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4932 /* This is for wide string constants. */
4933 wchar_array_type_node
4934 = build_array_type (wchar_type_node, array_domain_type);
4936 /* Define 'char16_t'. */
4937 char16_type_node = get_identifier (CHAR16_TYPE);
4938 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4939 char16_type_size = TYPE_PRECISION (char16_type_node);
4940 if (c_dialect_cxx ())
4942 char16_type_node = make_unsigned_type (char16_type_size);
4944 if (cxx_dialect == cxx0x)
4945 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4948 /* This is for UTF-16 string constants. */
4949 char16_array_type_node
4950 = build_array_type (char16_type_node, array_domain_type);
4952 /* Define 'char32_t'. */
4953 char32_type_node = get_identifier (CHAR32_TYPE);
4954 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4955 char32_type_size = TYPE_PRECISION (char32_type_node);
4956 if (c_dialect_cxx ())
4958 char32_type_node = make_unsigned_type (char32_type_size);
4960 if (cxx_dialect == cxx0x)
4961 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4964 /* This is for UTF-32 string constants. */
4965 char32_array_type_node
4966 = build_array_type (char32_type_node, array_domain_type);
4968 wint_type_node =
4969 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4971 intmax_type_node =
4972 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4973 uintmax_type_node =
4974 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4976 if (SIG_ATOMIC_TYPE)
4977 sig_atomic_type_node =
4978 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4979 if (INT8_TYPE)
4980 int8_type_node =
4981 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4982 if (INT16_TYPE)
4983 int16_type_node =
4984 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4985 if (INT32_TYPE)
4986 int32_type_node =
4987 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4988 if (INT64_TYPE)
4989 int64_type_node =
4990 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4991 if (UINT8_TYPE)
4992 uint8_type_node =
4993 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4994 if (UINT16_TYPE)
4995 uint16_type_node =
4996 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4997 if (UINT32_TYPE)
4998 c_uint32_type_node =
4999 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5000 if (UINT64_TYPE)
5001 c_uint64_type_node =
5002 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5003 if (INT_LEAST8_TYPE)
5004 int_least8_type_node =
5005 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5006 if (INT_LEAST16_TYPE)
5007 int_least16_type_node =
5008 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5009 if (INT_LEAST32_TYPE)
5010 int_least32_type_node =
5011 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5012 if (INT_LEAST64_TYPE)
5013 int_least64_type_node =
5014 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5015 if (UINT_LEAST8_TYPE)
5016 uint_least8_type_node =
5017 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5018 if (UINT_LEAST16_TYPE)
5019 uint_least16_type_node =
5020 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5021 if (UINT_LEAST32_TYPE)
5022 uint_least32_type_node =
5023 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5024 if (UINT_LEAST64_TYPE)
5025 uint_least64_type_node =
5026 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5027 if (INT_FAST8_TYPE)
5028 int_fast8_type_node =
5029 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5030 if (INT_FAST16_TYPE)
5031 int_fast16_type_node =
5032 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5033 if (INT_FAST32_TYPE)
5034 int_fast32_type_node =
5035 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5036 if (INT_FAST64_TYPE)
5037 int_fast64_type_node =
5038 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5039 if (UINT_FAST8_TYPE)
5040 uint_fast8_type_node =
5041 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5042 if (UINT_FAST16_TYPE)
5043 uint_fast16_type_node =
5044 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5045 if (UINT_FAST32_TYPE)
5046 uint_fast32_type_node =
5047 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5048 if (UINT_FAST64_TYPE)
5049 uint_fast64_type_node =
5050 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5051 if (INTPTR_TYPE)
5052 intptr_type_node =
5053 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5054 if (UINTPTR_TYPE)
5055 uintptr_type_node =
5056 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5058 default_function_type
5059 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5060 ptrdiff_type_node
5061 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5062 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5064 lang_hooks.decls.pushdecl
5065 (build_decl (UNKNOWN_LOCATION,
5066 TYPE_DECL, get_identifier ("__builtin_va_list"),
5067 va_list_type_node));
5068 if (targetm.enum_va_list_p)
5070 int l;
5071 const char *pname;
5072 tree ptype;
5074 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5076 lang_hooks.decls.pushdecl
5077 (build_decl (UNKNOWN_LOCATION,
5078 TYPE_DECL, get_identifier (pname),
5079 ptype));
5084 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5086 va_list_arg_type_node = va_list_ref_type_node =
5087 build_pointer_type (TREE_TYPE (va_list_type_node));
5089 else
5091 va_list_arg_type_node = va_list_type_node;
5092 va_list_ref_type_node = build_reference_type (va_list_type_node);
5095 if (!flag_preprocess_only)
5096 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5098 main_identifier_node = get_identifier ("main");
5100 /* Create the built-in __null node. It is important that this is
5101 not shared. */
5102 null_node = make_node (INTEGER_CST);
5103 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5105 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5106 memset (builtin_types, 0, sizeof (builtin_types));
5109 /* The number of named compound-literals generated thus far. */
5110 static GTY(()) int compound_literal_number;
5112 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5114 void
5115 set_compound_literal_name (tree decl)
5117 char *name;
5118 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5119 compound_literal_number);
5120 compound_literal_number++;
5121 DECL_NAME (decl) = get_identifier (name);
5124 tree
5125 build_va_arg (location_t loc, tree expr, tree type)
5127 expr = build1 (VA_ARG_EXPR, type, expr);
5128 SET_EXPR_LOCATION (expr, loc);
5129 return expr;
5133 /* Linked list of disabled built-in functions. */
5135 typedef struct disabled_builtin
5137 const char *name;
5138 struct disabled_builtin *next;
5139 } disabled_builtin;
5140 static disabled_builtin *disabled_builtins = NULL;
5142 static bool builtin_function_disabled_p (const char *);
5144 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5145 begins with "__builtin_", give an error. */
5147 void
5148 disable_builtin_function (const char *name)
5150 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5151 error ("cannot disable built-in function %qs", name);
5152 else
5154 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5155 new_disabled_builtin->name = name;
5156 new_disabled_builtin->next = disabled_builtins;
5157 disabled_builtins = new_disabled_builtin;
5162 /* Return true if the built-in function NAME has been disabled, false
5163 otherwise. */
5165 static bool
5166 builtin_function_disabled_p (const char *name)
5168 disabled_builtin *p;
5169 for (p = disabled_builtins; p != NULL; p = p->next)
5171 if (strcmp (name, p->name) == 0)
5172 return true;
5174 return false;
5178 /* Worker for DEF_BUILTIN.
5179 Possibly define a builtin function with one or two names.
5180 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5181 nonansi_p and flag_no_nonansi_builtin. */
5183 static void
5184 def_builtin_1 (enum built_in_function fncode,
5185 const char *name,
5186 enum built_in_class fnclass,
5187 tree fntype, tree libtype,
5188 bool both_p, bool fallback_p, bool nonansi_p,
5189 tree fnattrs, bool implicit_p)
5191 tree decl;
5192 const char *libname;
5194 if (fntype == error_mark_node)
5195 return;
5197 gcc_assert ((!both_p && !fallback_p)
5198 || !strncmp (name, "__builtin_",
5199 strlen ("__builtin_")));
5201 libname = name + strlen ("__builtin_");
5202 decl = add_builtin_function (name, fntype, fncode, fnclass,
5203 (fallback_p ? libname : NULL),
5204 fnattrs);
5206 set_builtin_decl (fncode, decl, implicit_p);
5208 if (both_p
5209 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5210 && !(nonansi_p && flag_no_nonansi_builtin))
5211 add_builtin_function (libname, libtype, fncode, fnclass,
5212 NULL, fnattrs);
5215 /* Nonzero if the type T promotes to int. This is (nearly) the
5216 integral promotions defined in ISO C99 6.3.1.1/2. */
5218 bool
5219 c_promoting_integer_type_p (const_tree t)
5221 switch (TREE_CODE (t))
5223 case INTEGER_TYPE:
5224 return (TYPE_MAIN_VARIANT (t) == char_type_node
5225 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5226 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5227 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5228 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5229 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5231 case ENUMERAL_TYPE:
5232 /* ??? Technically all enumerations not larger than an int
5233 promote to an int. But this is used along code paths
5234 that only want to notice a size change. */
5235 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5237 case BOOLEAN_TYPE:
5238 return 1;
5240 default:
5241 return 0;
5245 /* Return 1 if PARMS specifies a fixed number of parameters
5246 and none of their types is affected by default promotions. */
5249 self_promoting_args_p (const_tree parms)
5251 const_tree t;
5252 for (t = parms; t; t = TREE_CHAIN (t))
5254 tree type = TREE_VALUE (t);
5256 if (type == error_mark_node)
5257 continue;
5259 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5260 return 0;
5262 if (type == 0)
5263 return 0;
5265 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5266 return 0;
5268 if (c_promoting_integer_type_p (type))
5269 return 0;
5271 return 1;
5274 /* Recursively remove any '*' or '&' operator from TYPE. */
5275 tree
5276 strip_pointer_operator (tree t)
5278 while (POINTER_TYPE_P (t))
5279 t = TREE_TYPE (t);
5280 return t;
5283 /* Recursively remove pointer or array type from TYPE. */
5284 tree
5285 strip_pointer_or_array_types (tree t)
5287 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5288 t = TREE_TYPE (t);
5289 return t;
5292 /* Used to compare case labels. K1 and K2 are actually tree nodes
5293 representing case labels, or NULL_TREE for a `default' label.
5294 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5295 K2, and 0 if K1 and K2 are equal. */
5298 case_compare (splay_tree_key k1, splay_tree_key k2)
5300 /* Consider a NULL key (such as arises with a `default' label) to be
5301 smaller than anything else. */
5302 if (!k1)
5303 return k2 ? -1 : 0;
5304 else if (!k2)
5305 return k1 ? 1 : 0;
5307 return tree_int_cst_compare ((tree) k1, (tree) k2);
5310 /* Process a case label, located at LOC, for the range LOW_VALUE
5311 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5312 then this case label is actually a `default' label. If only
5313 HIGH_VALUE is NULL_TREE, then case label was declared using the
5314 usual C/C++ syntax, rather than the GNU case range extension.
5315 CASES is a tree containing all the case ranges processed so far;
5316 COND is the condition for the switch-statement itself. Returns the
5317 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5318 is created. */
5320 tree
5321 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5322 tree low_value, tree high_value)
5324 tree type;
5325 tree label;
5326 tree case_label;
5327 splay_tree_node node;
5329 /* Create the LABEL_DECL itself. */
5330 label = create_artificial_label (loc);
5332 /* If there was an error processing the switch condition, bail now
5333 before we get more confused. */
5334 if (!cond || cond == error_mark_node)
5335 goto error_out;
5337 if ((low_value && TREE_TYPE (low_value)
5338 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5339 || (high_value && TREE_TYPE (high_value)
5340 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5342 error_at (loc, "pointers are not permitted as case values");
5343 goto error_out;
5346 /* Case ranges are a GNU extension. */
5347 if (high_value)
5348 pedwarn (loc, OPT_pedantic,
5349 "range expressions in switch statements are non-standard");
5351 type = TREE_TYPE (cond);
5352 if (low_value)
5354 low_value = check_case_value (low_value);
5355 low_value = convert_and_check (type, low_value);
5356 if (low_value == error_mark_node)
5357 goto error_out;
5359 if (high_value)
5361 high_value = check_case_value (high_value);
5362 high_value = convert_and_check (type, high_value);
5363 if (high_value == error_mark_node)
5364 goto error_out;
5367 if (low_value && high_value)
5369 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5370 really a case range, even though it was written that way.
5371 Remove the HIGH_VALUE to simplify later processing. */
5372 if (tree_int_cst_equal (low_value, high_value))
5373 high_value = NULL_TREE;
5374 else if (!tree_int_cst_lt (low_value, high_value))
5375 warning_at (loc, 0, "empty range specified");
5378 /* See if the case is in range of the type of the original testing
5379 expression. If both low_value and high_value are out of range,
5380 don't insert the case label and return NULL_TREE. */
5381 if (low_value
5382 && !check_case_bounds (type, orig_type,
5383 &low_value, high_value ? &high_value : NULL))
5384 return NULL_TREE;
5386 /* Look up the LOW_VALUE in the table of case labels we already
5387 have. */
5388 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5389 /* If there was not an exact match, check for overlapping ranges.
5390 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5391 that's a `default' label and the only overlap is an exact match. */
5392 if (!node && (low_value || high_value))
5394 splay_tree_node low_bound;
5395 splay_tree_node high_bound;
5397 /* Even though there wasn't an exact match, there might be an
5398 overlap between this case range and another case range.
5399 Since we've (inductively) not allowed any overlapping case
5400 ranges, we simply need to find the greatest low case label
5401 that is smaller that LOW_VALUE, and the smallest low case
5402 label that is greater than LOW_VALUE. If there is an overlap
5403 it will occur in one of these two ranges. */
5404 low_bound = splay_tree_predecessor (cases,
5405 (splay_tree_key) low_value);
5406 high_bound = splay_tree_successor (cases,
5407 (splay_tree_key) low_value);
5409 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5410 the LOW_VALUE, so there is no need to check unless the
5411 LOW_BOUND is in fact itself a case range. */
5412 if (low_bound
5413 && CASE_HIGH ((tree) low_bound->value)
5414 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5415 low_value) >= 0)
5416 node = low_bound;
5417 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5418 range is bigger than the low end of the current range, so we
5419 are only interested if the current range is a real range, and
5420 not an ordinary case label. */
5421 else if (high_bound
5422 && high_value
5423 && (tree_int_cst_compare ((tree) high_bound->key,
5424 high_value)
5425 <= 0))
5426 node = high_bound;
5428 /* If there was an overlap, issue an error. */
5429 if (node)
5431 tree duplicate = CASE_LABEL ((tree) node->value);
5433 if (high_value)
5435 error_at (loc, "duplicate (or overlapping) case value");
5436 error_at (DECL_SOURCE_LOCATION (duplicate),
5437 "this is the first entry overlapping that value");
5439 else if (low_value)
5441 error_at (loc, "duplicate case value") ;
5442 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5444 else
5446 error_at (loc, "multiple default labels in one switch");
5447 error_at (DECL_SOURCE_LOCATION (duplicate),
5448 "this is the first default label");
5450 goto error_out;
5453 /* Add a CASE_LABEL to the statement-tree. */
5454 case_label = add_stmt (build_case_label (low_value, high_value, label));
5455 /* Register this case label in the splay tree. */
5456 splay_tree_insert (cases,
5457 (splay_tree_key) low_value,
5458 (splay_tree_value) case_label);
5460 return case_label;
5462 error_out:
5463 /* Add a label so that the back-end doesn't think that the beginning of
5464 the switch is unreachable. Note that we do not add a case label, as
5465 that just leads to duplicates and thence to failure later on. */
5466 if (!cases->root)
5468 tree t = create_artificial_label (loc);
5469 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5471 return error_mark_node;
5474 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5475 Used to verify that case values match up with enumerator values. */
5477 static void
5478 match_case_to_enum_1 (tree key, tree type, tree label)
5480 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5482 /* ??? Not working too hard to print the double-word value.
5483 Should perhaps be done with %lwd in the diagnostic routines? */
5484 if (TREE_INT_CST_HIGH (key) == 0)
5485 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5486 TREE_INT_CST_LOW (key));
5487 else if (!TYPE_UNSIGNED (type)
5488 && TREE_INT_CST_HIGH (key) == -1
5489 && TREE_INT_CST_LOW (key) != 0)
5490 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
5491 -TREE_INT_CST_LOW (key));
5492 else
5493 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5494 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5495 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
5497 if (TYPE_NAME (type) == 0)
5498 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5499 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5500 "case value %qs not in enumerated type",
5501 buf);
5502 else
5503 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5504 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5505 "case value %qs not in enumerated type %qT",
5506 buf, type);
5509 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
5510 Used to verify that case values match up with enumerator values. */
5512 static int
5513 match_case_to_enum (splay_tree_node node, void *data)
5515 tree label = (tree) node->value;
5516 tree type = (tree) data;
5518 /* Skip default case. */
5519 if (!CASE_LOW (label))
5520 return 0;
5522 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
5523 when we did our enum->case scan. Reset our scratch bit after. */
5524 if (!CASE_LOW_SEEN (label))
5525 match_case_to_enum_1 (CASE_LOW (label), type, label);
5526 else
5527 CASE_LOW_SEEN (label) = 0;
5529 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
5530 not set, that means that CASE_HIGH did not appear when we did our
5531 enum->case scan. Reset our scratch bit after. */
5532 if (CASE_HIGH (label))
5534 if (!CASE_HIGH_SEEN (label))
5535 match_case_to_enum_1 (CASE_HIGH (label), type, label);
5536 else
5537 CASE_HIGH_SEEN (label) = 0;
5540 return 0;
5543 /* Handle -Wswitch*. Called from the front end after parsing the
5544 switch construct. */
5545 /* ??? Should probably be somewhere generic, since other languages
5546 besides C and C++ would want this. At the moment, however, C/C++
5547 are the only tree-ssa languages that support enumerations at all,
5548 so the point is moot. */
5550 void
5551 c_do_switch_warnings (splay_tree cases, location_t switch_location,
5552 tree type, tree cond)
5554 splay_tree_node default_node;
5555 splay_tree_node node;
5556 tree chain;
5558 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5559 return;
5561 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
5562 if (!default_node)
5563 warning_at (switch_location, OPT_Wswitch_default,
5564 "switch missing default case");
5566 /* From here on, we only care about about enumerated types. */
5567 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
5568 return;
5570 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
5571 if (!warn_switch_enum && !warn_switch)
5572 return;
5574 /* Check the cases. Warn about case values which are not members of
5575 the enumerated type. For -Wswitch-enum, or for -Wswitch when
5576 there is no default case, check that exactly all enumeration
5577 literals are covered by the cases. */
5579 /* Clearing COND if it is not an integer constant simplifies
5580 the tests inside the loop below. */
5581 if (TREE_CODE (cond) != INTEGER_CST)
5582 cond = NULL_TREE;
5584 /* The time complexity here is O(N*lg(N)) worst case, but for the
5585 common case of monotonically increasing enumerators, it is
5586 O(N), since the nature of the splay tree will keep the next
5587 element adjacent to the root at all times. */
5589 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
5591 tree value = TREE_VALUE (chain);
5592 if (TREE_CODE (value) == CONST_DECL)
5593 value = DECL_INITIAL (value);
5594 node = splay_tree_lookup (cases, (splay_tree_key) value);
5595 if (node)
5597 /* Mark the CASE_LOW part of the case entry as seen. */
5598 tree label = (tree) node->value;
5599 CASE_LOW_SEEN (label) = 1;
5600 continue;
5603 /* Even though there wasn't an exact match, there might be a
5604 case range which includes the enumerator's value. */
5605 node = splay_tree_predecessor (cases, (splay_tree_key) value);
5606 if (node && CASE_HIGH ((tree) node->value))
5608 tree label = (tree) node->value;
5609 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
5610 if (cmp >= 0)
5612 /* If we match the upper bound exactly, mark the CASE_HIGH
5613 part of the case entry as seen. */
5614 if (cmp == 0)
5615 CASE_HIGH_SEEN (label) = 1;
5616 continue;
5620 /* We've now determined that this enumerated literal isn't
5621 handled by the case labels of the switch statement. */
5623 /* If the switch expression is a constant, we only really care
5624 about whether that constant is handled by the switch. */
5625 if (cond && tree_int_cst_compare (cond, value))
5626 continue;
5628 /* If there is a default_node, the only relevant option is
5629 Wswitch-enum. Otherwise, if both are enabled then we prefer
5630 to warn using -Wswitch because -Wswitch is enabled by -Wall
5631 while -Wswitch-enum is explicit. */
5632 warning_at (switch_location,
5633 (default_node || !warn_switch
5634 ? OPT_Wswitch_enum
5635 : OPT_Wswitch),
5636 "enumeration value %qE not handled in switch",
5637 TREE_PURPOSE (chain));
5640 /* Warn if there are case expressions that don't correspond to
5641 enumerators. This can occur since C and C++ don't enforce
5642 type-checking of assignments to enumeration variables.
5644 The time complexity here is now always O(N) worst case, since
5645 we should have marked both the lower bound and upper bound of
5646 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
5647 above. This scan also resets those fields. */
5649 splay_tree_foreach (cases, match_case_to_enum, type);
5652 /* Finish an expression taking the address of LABEL (an
5653 IDENTIFIER_NODE). Returns an expression for the address.
5655 LOC is the location for the expression returned. */
5657 tree
5658 finish_label_address_expr (tree label, location_t loc)
5660 tree result;
5662 pedwarn (input_location, OPT_pedantic, "taking the address of a label is non-standard");
5664 if (label == error_mark_node)
5665 return error_mark_node;
5667 label = lookup_label (label);
5668 if (label == NULL_TREE)
5669 result = null_pointer_node;
5670 else
5672 TREE_USED (label) = 1;
5673 result = build1 (ADDR_EXPR, ptr_type_node, label);
5674 /* The current function is not necessarily uninlinable.
5675 Computed gotos are incompatible with inlining, but the value
5676 here could be used only in a diagnostic, for example. */
5677 protected_set_expr_location (result, loc);
5680 return result;
5684 /* Given a boolean expression ARG, return a tree representing an increment
5685 or decrement (as indicated by CODE) of ARG. The front end must check for
5686 invalid cases (e.g., decrement in C++). */
5687 tree
5688 boolean_increment (enum tree_code code, tree arg)
5690 tree val;
5691 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5693 arg = stabilize_reference (arg);
5694 switch (code)
5696 case PREINCREMENT_EXPR:
5697 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5698 break;
5699 case POSTINCREMENT_EXPR:
5700 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5701 arg = save_expr (arg);
5702 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5703 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5704 break;
5705 case PREDECREMENT_EXPR:
5706 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5707 invert_truthvalue_loc (input_location, arg));
5708 break;
5709 case POSTDECREMENT_EXPR:
5710 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5711 invert_truthvalue_loc (input_location, arg));
5712 arg = save_expr (arg);
5713 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5714 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5715 break;
5716 default:
5717 gcc_unreachable ();
5719 TREE_SIDE_EFFECTS (val) = 1;
5720 return val;
5723 /* Built-in macros for stddef.h and stdint.h, that require macros
5724 defined in this file. */
5725 void
5726 c_stddef_cpp_builtins(void)
5728 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5729 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5730 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5731 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5732 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5733 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5734 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5735 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5736 if (SIG_ATOMIC_TYPE)
5737 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5738 if (INT8_TYPE)
5739 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5740 if (INT16_TYPE)
5741 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5742 if (INT32_TYPE)
5743 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5744 if (INT64_TYPE)
5745 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5746 if (UINT8_TYPE)
5747 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5748 if (UINT16_TYPE)
5749 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5750 if (UINT32_TYPE)
5751 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5752 if (UINT64_TYPE)
5753 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5754 if (INT_LEAST8_TYPE)
5755 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5756 if (INT_LEAST16_TYPE)
5757 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5758 if (INT_LEAST32_TYPE)
5759 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5760 if (INT_LEAST64_TYPE)
5761 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5762 if (UINT_LEAST8_TYPE)
5763 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5764 if (UINT_LEAST16_TYPE)
5765 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5766 if (UINT_LEAST32_TYPE)
5767 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5768 if (UINT_LEAST64_TYPE)
5769 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5770 if (INT_FAST8_TYPE)
5771 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5772 if (INT_FAST16_TYPE)
5773 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5774 if (INT_FAST32_TYPE)
5775 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5776 if (INT_FAST64_TYPE)
5777 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5778 if (UINT_FAST8_TYPE)
5779 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5780 if (UINT_FAST16_TYPE)
5781 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5782 if (UINT_FAST32_TYPE)
5783 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5784 if (UINT_FAST64_TYPE)
5785 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5786 if (INTPTR_TYPE)
5787 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5788 if (UINTPTR_TYPE)
5789 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5792 static void
5793 c_init_attributes (void)
5795 /* Fill in the built_in_attributes array. */
5796 #define DEF_ATTR_NULL_TREE(ENUM) \
5797 built_in_attributes[(int) ENUM] = NULL_TREE;
5798 #define DEF_ATTR_INT(ENUM, VALUE) \
5799 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5800 #define DEF_ATTR_IDENT(ENUM, STRING) \
5801 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5802 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5803 built_in_attributes[(int) ENUM] \
5804 = tree_cons (built_in_attributes[(int) PURPOSE], \
5805 built_in_attributes[(int) VALUE], \
5806 built_in_attributes[(int) CHAIN]);
5807 #include "builtin-attrs.def"
5808 #undef DEF_ATTR_NULL_TREE
5809 #undef DEF_ATTR_INT
5810 #undef DEF_ATTR_IDENT
5811 #undef DEF_ATTR_TREE_LIST
5814 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
5815 identifier as an argument, so the front end shouldn't look it up. */
5817 bool
5818 attribute_takes_identifier_p (const_tree attr_id)
5820 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
5821 if (spec == NULL)
5822 /* Unknown attribute that we'll end up ignoring, return true so we
5823 don't complain about an identifier argument. */
5824 return true;
5825 else if (!strcmp ("mode", spec->name)
5826 || !strcmp ("format", spec->name)
5827 || !strcmp ("cleanup", spec->name))
5828 return true;
5829 else
5830 return targetm.attribute_takes_identifier_p (attr_id);
5833 /* Attribute handlers common to C front ends. */
5835 /* Handle a "packed" attribute; arguments as in
5836 struct attribute_spec.handler. */
5838 static tree
5839 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5840 int flags, bool *no_add_attrs)
5842 if (TYPE_P (*node))
5844 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5845 *node = build_variant_type_copy (*node);
5846 TYPE_PACKED (*node) = 1;
5848 else if (TREE_CODE (*node) == FIELD_DECL)
5850 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
5851 /* Still pack bitfields. */
5852 && ! DECL_INITIAL (*node))
5853 warning (OPT_Wattributes,
5854 "%qE attribute ignored for field of type %qT",
5855 name, TREE_TYPE (*node));
5856 else
5857 DECL_PACKED (*node) = 1;
5859 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
5860 used for DECL_REGISTER. It wouldn't mean anything anyway.
5861 We can't set DECL_PACKED on the type of a TYPE_DECL, because
5862 that changes what the typedef is typing. */
5863 else
5865 warning (OPT_Wattributes, "%qE attribute ignored", name);
5866 *no_add_attrs = true;
5869 return NULL_TREE;
5872 /* Handle a "nocommon" attribute; arguments as in
5873 struct attribute_spec.handler. */
5875 static tree
5876 handle_nocommon_attribute (tree *node, tree name,
5877 tree ARG_UNUSED (args),
5878 int ARG_UNUSED (flags), bool *no_add_attrs)
5880 if (TREE_CODE (*node) == VAR_DECL)
5881 DECL_COMMON (*node) = 0;
5882 else
5884 warning (OPT_Wattributes, "%qE attribute ignored", name);
5885 *no_add_attrs = true;
5888 return NULL_TREE;
5891 /* Handle a "common" attribute; arguments as in
5892 struct attribute_spec.handler. */
5894 static tree
5895 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5896 int ARG_UNUSED (flags), bool *no_add_attrs)
5898 if (TREE_CODE (*node) == VAR_DECL)
5899 DECL_COMMON (*node) = 1;
5900 else
5902 warning (OPT_Wattributes, "%qE attribute ignored", name);
5903 *no_add_attrs = true;
5906 return NULL_TREE;
5909 /* Handle a "noreturn" attribute; arguments as in
5910 struct attribute_spec.handler. */
5912 static tree
5913 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5914 int ARG_UNUSED (flags), bool *no_add_attrs)
5916 tree type = TREE_TYPE (*node);
5918 /* See FIXME comment in c_common_attribute_table. */
5919 if (TREE_CODE (*node) == FUNCTION_DECL
5920 || objc_method_decl (TREE_CODE (*node)))
5921 TREE_THIS_VOLATILE (*node) = 1;
5922 else if (TREE_CODE (type) == POINTER_TYPE
5923 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5924 TREE_TYPE (*node)
5925 = build_pointer_type
5926 (build_type_variant (TREE_TYPE (type),
5927 TYPE_READONLY (TREE_TYPE (type)), 1));
5928 else
5930 warning (OPT_Wattributes, "%qE attribute ignored", name);
5931 *no_add_attrs = true;
5934 return NULL_TREE;
5937 /* Handle a "hot" and attribute; arguments as in
5938 struct attribute_spec.handler. */
5940 static tree
5941 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5942 int ARG_UNUSED (flags), bool *no_add_attrs)
5944 if (TREE_CODE (*node) == FUNCTION_DECL)
5946 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
5948 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5949 name, "cold");
5950 *no_add_attrs = true;
5952 /* Most of the rest of the hot processing is done later with
5953 lookup_attribute. */
5955 else
5957 warning (OPT_Wattributes, "%qE attribute ignored", name);
5958 *no_add_attrs = true;
5961 return NULL_TREE;
5963 /* Handle a "cold" and attribute; arguments as in
5964 struct attribute_spec.handler. */
5966 static tree
5967 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5968 int ARG_UNUSED (flags), bool *no_add_attrs)
5970 if (TREE_CODE (*node) == FUNCTION_DECL)
5972 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
5974 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5975 name, "hot");
5976 *no_add_attrs = true;
5978 /* Most of the rest of the cold processing is done later with
5979 lookup_attribute. */
5981 else
5983 warning (OPT_Wattributes, "%qE attribute ignored", name);
5984 *no_add_attrs = true;
5987 return NULL_TREE;
5990 /* Handle a "noinline" attribute; arguments as in
5991 struct attribute_spec.handler. */
5993 static tree
5994 handle_noinline_attribute (tree *node, tree name,
5995 tree ARG_UNUSED (args),
5996 int ARG_UNUSED (flags), bool *no_add_attrs)
5998 if (TREE_CODE (*node) == FUNCTION_DECL)
5999 DECL_UNINLINABLE (*node) = 1;
6000 else
6002 warning (OPT_Wattributes, "%qE attribute ignored", name);
6003 *no_add_attrs = true;
6006 return NULL_TREE;
6009 /* Handle a "noclone" attribute; arguments as in
6010 struct attribute_spec.handler. */
6012 static tree
6013 handle_noclone_attribute (tree *node, tree name,
6014 tree ARG_UNUSED (args),
6015 int ARG_UNUSED (flags), bool *no_add_attrs)
6017 if (TREE_CODE (*node) != FUNCTION_DECL)
6019 warning (OPT_Wattributes, "%qE attribute ignored", name);
6020 *no_add_attrs = true;
6023 return NULL_TREE;
6026 /* Handle a "always_inline" attribute; arguments as in
6027 struct attribute_spec.handler. */
6029 static tree
6030 handle_always_inline_attribute (tree *node, tree name,
6031 tree ARG_UNUSED (args),
6032 int ARG_UNUSED (flags),
6033 bool *no_add_attrs)
6035 if (TREE_CODE (*node) == FUNCTION_DECL)
6037 /* Set the attribute and mark it for disregarding inline
6038 limits. */
6039 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6041 else
6043 warning (OPT_Wattributes, "%qE attribute ignored", name);
6044 *no_add_attrs = true;
6047 return NULL_TREE;
6050 /* Handle a "gnu_inline" attribute; arguments as in
6051 struct attribute_spec.handler. */
6053 static tree
6054 handle_gnu_inline_attribute (tree *node, tree name,
6055 tree ARG_UNUSED (args),
6056 int ARG_UNUSED (flags),
6057 bool *no_add_attrs)
6059 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6061 /* Do nothing else, just set the attribute. We'll get at
6062 it later with lookup_attribute. */
6064 else
6066 warning (OPT_Wattributes, "%qE attribute ignored", name);
6067 *no_add_attrs = true;
6070 return NULL_TREE;
6073 /* Handle a "leaf" attribute; arguments as in
6074 struct attribute_spec.handler. */
6076 static tree
6077 handle_leaf_attribute (tree *node, tree name,
6078 tree ARG_UNUSED (args),
6079 int ARG_UNUSED (flags), bool *no_add_attrs)
6081 if (TREE_CODE (*node) != FUNCTION_DECL)
6083 warning (OPT_Wattributes, "%qE attribute ignored", name);
6084 *no_add_attrs = true;
6086 if (!TREE_PUBLIC (*node))
6088 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6089 *no_add_attrs = true;
6092 return NULL_TREE;
6095 /* Handle an "artificial" attribute; arguments as in
6096 struct attribute_spec.handler. */
6098 static tree
6099 handle_artificial_attribute (tree *node, tree name,
6100 tree ARG_UNUSED (args),
6101 int ARG_UNUSED (flags),
6102 bool *no_add_attrs)
6104 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6106 /* Do nothing else, just set the attribute. We'll get at
6107 it later with lookup_attribute. */
6109 else
6111 warning (OPT_Wattributes, "%qE attribute ignored", name);
6112 *no_add_attrs = true;
6115 return NULL_TREE;
6118 /* Handle a "flatten" attribute; arguments as in
6119 struct attribute_spec.handler. */
6121 static tree
6122 handle_flatten_attribute (tree *node, tree name,
6123 tree args ATTRIBUTE_UNUSED,
6124 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6126 if (TREE_CODE (*node) == FUNCTION_DECL)
6127 /* Do nothing else, just set the attribute. We'll get at
6128 it later with lookup_attribute. */
6130 else
6132 warning (OPT_Wattributes, "%qE attribute ignored", name);
6133 *no_add_attrs = true;
6136 return NULL_TREE;
6139 /* Handle a "warning" or "error" attribute; arguments as in
6140 struct attribute_spec.handler. */
6142 static tree
6143 handle_error_attribute (tree *node, tree name, tree args,
6144 int ARG_UNUSED (flags), bool *no_add_attrs)
6146 if (TREE_CODE (*node) == FUNCTION_DECL
6147 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6148 /* Do nothing else, just set the attribute. We'll get at
6149 it later with lookup_attribute. */
6151 else
6153 warning (OPT_Wattributes, "%qE attribute ignored", name);
6154 *no_add_attrs = true;
6157 return NULL_TREE;
6160 /* Handle a "used" attribute; arguments as in
6161 struct attribute_spec.handler. */
6163 static tree
6164 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6165 int ARG_UNUSED (flags), bool *no_add_attrs)
6167 tree node = *pnode;
6169 if (TREE_CODE (node) == FUNCTION_DECL
6170 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6171 || (TREE_CODE (node) == TYPE_DECL))
6173 TREE_USED (node) = 1;
6174 DECL_PRESERVE_P (node) = 1;
6175 if (TREE_CODE (node) == VAR_DECL)
6176 DECL_READ_P (node) = 1;
6178 else
6180 warning (OPT_Wattributes, "%qE attribute ignored", name);
6181 *no_add_attrs = true;
6184 return NULL_TREE;
6187 /* Handle a "unused" attribute; arguments as in
6188 struct attribute_spec.handler. */
6190 static tree
6191 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6192 int flags, bool *no_add_attrs)
6194 if (DECL_P (*node))
6196 tree decl = *node;
6198 if (TREE_CODE (decl) == PARM_DECL
6199 || TREE_CODE (decl) == VAR_DECL
6200 || TREE_CODE (decl) == FUNCTION_DECL
6201 || TREE_CODE (decl) == LABEL_DECL
6202 || TREE_CODE (decl) == TYPE_DECL)
6204 TREE_USED (decl) = 1;
6205 if (TREE_CODE (decl) == VAR_DECL
6206 || TREE_CODE (decl) == PARM_DECL)
6207 DECL_READ_P (decl) = 1;
6209 else
6211 warning (OPT_Wattributes, "%qE attribute ignored", name);
6212 *no_add_attrs = true;
6215 else
6217 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6218 *node = build_variant_type_copy (*node);
6219 TREE_USED (*node) = 1;
6222 return NULL_TREE;
6225 /* Handle a "externally_visible" attribute; arguments as in
6226 struct attribute_spec.handler. */
6228 static tree
6229 handle_externally_visible_attribute (tree *pnode, tree name,
6230 tree ARG_UNUSED (args),
6231 int ARG_UNUSED (flags),
6232 bool *no_add_attrs)
6234 tree node = *pnode;
6236 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6238 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6239 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6241 warning (OPT_Wattributes,
6242 "%qE attribute have effect only on public objects", name);
6243 *no_add_attrs = true;
6246 else
6248 warning (OPT_Wattributes, "%qE attribute ignored", name);
6249 *no_add_attrs = true;
6252 return NULL_TREE;
6255 /* Handle a "const" attribute; arguments as in
6256 struct attribute_spec.handler. */
6258 static tree
6259 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6260 int ARG_UNUSED (flags), bool *no_add_attrs)
6262 tree type = TREE_TYPE (*node);
6264 /* See FIXME comment on noreturn in c_common_attribute_table. */
6265 if (TREE_CODE (*node) == FUNCTION_DECL)
6266 TREE_READONLY (*node) = 1;
6267 else if (TREE_CODE (type) == POINTER_TYPE
6268 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6269 TREE_TYPE (*node)
6270 = build_pointer_type
6271 (build_type_variant (TREE_TYPE (type), 1,
6272 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6273 else
6275 warning (OPT_Wattributes, "%qE attribute ignored", name);
6276 *no_add_attrs = true;
6279 return NULL_TREE;
6282 /* Handle a "transparent_union" attribute; arguments as in
6283 struct attribute_spec.handler. */
6285 static tree
6286 handle_transparent_union_attribute (tree *node, tree name,
6287 tree ARG_UNUSED (args), int flags,
6288 bool *no_add_attrs)
6290 tree type;
6292 *no_add_attrs = true;
6294 if (TREE_CODE (*node) == TYPE_DECL)
6295 node = &TREE_TYPE (*node);
6296 type = *node;
6298 if (TREE_CODE (type) == UNION_TYPE)
6300 /* Make sure that the first field will work for a transparent union.
6301 If the type isn't complete yet, leave the check to the code in
6302 finish_struct. */
6303 if (TYPE_SIZE (type))
6305 tree first = first_field (type);
6306 if (first == NULL_TREE
6307 || DECL_ARTIFICIAL (first)
6308 || TYPE_MODE (type) != DECL_MODE (first))
6309 goto ignored;
6312 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6314 /* If the type isn't complete yet, setting the flag
6315 on a variant wouldn't ever be checked. */
6316 if (!TYPE_SIZE (type))
6317 goto ignored;
6319 /* build_duplicate_type doesn't work for C++. */
6320 if (c_dialect_cxx ())
6321 goto ignored;
6323 /* A type variant isn't good enough, since we don't a cast
6324 to such a type removed as a no-op. */
6325 *node = type = build_duplicate_type (type);
6328 TYPE_TRANSPARENT_AGGR (type) = 1;
6329 return NULL_TREE;
6332 ignored:
6333 warning (OPT_Wattributes, "%qE attribute ignored", name);
6334 return NULL_TREE;
6337 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6338 get the requested priority for a constructor or destructor,
6339 possibly issuing diagnostics for invalid or reserved
6340 priorities. */
6342 static priority_type
6343 get_priority (tree args, bool is_destructor)
6345 HOST_WIDE_INT pri;
6346 tree arg;
6348 if (!args)
6349 return DEFAULT_INIT_PRIORITY;
6351 if (!SUPPORTS_INIT_PRIORITY)
6353 if (is_destructor)
6354 error ("destructor priorities are not supported");
6355 else
6356 error ("constructor priorities are not supported");
6357 return DEFAULT_INIT_PRIORITY;
6360 arg = TREE_VALUE (args);
6361 if (!host_integerp (arg, /*pos=*/0)
6362 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6363 goto invalid;
6365 pri = tree_low_cst (TREE_VALUE (args), /*pos=*/0);
6366 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6367 goto invalid;
6369 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6371 if (is_destructor)
6372 warning (0,
6373 "destructor priorities from 0 to %d are reserved "
6374 "for the implementation",
6375 MAX_RESERVED_INIT_PRIORITY);
6376 else
6377 warning (0,
6378 "constructor priorities from 0 to %d are reserved "
6379 "for the implementation",
6380 MAX_RESERVED_INIT_PRIORITY);
6382 return pri;
6384 invalid:
6385 if (is_destructor)
6386 error ("destructor priorities must be integers from 0 to %d inclusive",
6387 MAX_INIT_PRIORITY);
6388 else
6389 error ("constructor priorities must be integers from 0 to %d inclusive",
6390 MAX_INIT_PRIORITY);
6391 return DEFAULT_INIT_PRIORITY;
6394 /* Handle a "constructor" attribute; arguments as in
6395 struct attribute_spec.handler. */
6397 static tree
6398 handle_constructor_attribute (tree *node, tree name, tree args,
6399 int ARG_UNUSED (flags),
6400 bool *no_add_attrs)
6402 tree decl = *node;
6403 tree type = TREE_TYPE (decl);
6405 if (TREE_CODE (decl) == FUNCTION_DECL
6406 && TREE_CODE (type) == FUNCTION_TYPE
6407 && decl_function_context (decl) == 0)
6409 priority_type priority;
6410 DECL_STATIC_CONSTRUCTOR (decl) = 1;
6411 priority = get_priority (args, /*is_destructor=*/false);
6412 SET_DECL_INIT_PRIORITY (decl, priority);
6413 TREE_USED (decl) = 1;
6415 else
6417 warning (OPT_Wattributes, "%qE attribute ignored", name);
6418 *no_add_attrs = true;
6421 return NULL_TREE;
6424 /* Handle a "destructor" attribute; arguments as in
6425 struct attribute_spec.handler. */
6427 static tree
6428 handle_destructor_attribute (tree *node, tree name, tree args,
6429 int ARG_UNUSED (flags),
6430 bool *no_add_attrs)
6432 tree decl = *node;
6433 tree type = TREE_TYPE (decl);
6435 if (TREE_CODE (decl) == FUNCTION_DECL
6436 && TREE_CODE (type) == FUNCTION_TYPE
6437 && decl_function_context (decl) == 0)
6439 priority_type priority;
6440 DECL_STATIC_DESTRUCTOR (decl) = 1;
6441 priority = get_priority (args, /*is_destructor=*/true);
6442 SET_DECL_FINI_PRIORITY (decl, priority);
6443 TREE_USED (decl) = 1;
6445 else
6447 warning (OPT_Wattributes, "%qE attribute ignored", name);
6448 *no_add_attrs = true;
6451 return NULL_TREE;
6454 /* Nonzero if the mode is a valid vector mode for this architecture.
6455 This returns nonzero even if there is no hardware support for the
6456 vector mode, but we can emulate with narrower modes. */
6458 static int
6459 vector_mode_valid_p (enum machine_mode mode)
6461 enum mode_class mclass = GET_MODE_CLASS (mode);
6462 enum machine_mode innermode;
6464 /* Doh! What's going on? */
6465 if (mclass != MODE_VECTOR_INT
6466 && mclass != MODE_VECTOR_FLOAT
6467 && mclass != MODE_VECTOR_FRACT
6468 && mclass != MODE_VECTOR_UFRACT
6469 && mclass != MODE_VECTOR_ACCUM
6470 && mclass != MODE_VECTOR_UACCUM)
6471 return 0;
6473 /* Hardware support. Woo hoo! */
6474 if (targetm.vector_mode_supported_p (mode))
6475 return 1;
6477 innermode = GET_MODE_INNER (mode);
6479 /* We should probably return 1 if requesting V4DI and we have no DI,
6480 but we have V2DI, but this is probably very unlikely. */
6482 /* If we have support for the inner mode, we can safely emulate it.
6483 We may not have V2DI, but me can emulate with a pair of DIs. */
6484 return targetm.scalar_mode_supported_p (innermode);
6488 /* Handle a "mode" attribute; arguments as in
6489 struct attribute_spec.handler. */
6491 static tree
6492 handle_mode_attribute (tree *node, tree name, tree args,
6493 int ARG_UNUSED (flags), bool *no_add_attrs)
6495 tree type = *node;
6496 tree ident = TREE_VALUE (args);
6498 *no_add_attrs = true;
6500 if (TREE_CODE (ident) != IDENTIFIER_NODE)
6501 warning (OPT_Wattributes, "%qE attribute ignored", name);
6502 else
6504 int j;
6505 const char *p = IDENTIFIER_POINTER (ident);
6506 int len = strlen (p);
6507 enum machine_mode mode = VOIDmode;
6508 tree typefm;
6509 bool valid_mode;
6511 if (len > 4 && p[0] == '_' && p[1] == '_'
6512 && p[len - 1] == '_' && p[len - 2] == '_')
6514 char *newp = (char *) alloca (len - 1);
6516 strcpy (newp, &p[2]);
6517 newp[len - 4] = '\0';
6518 p = newp;
6521 /* Change this type to have a type with the specified mode.
6522 First check for the special modes. */
6523 if (!strcmp (p, "byte"))
6524 mode = byte_mode;
6525 else if (!strcmp (p, "word"))
6526 mode = word_mode;
6527 else if (!strcmp (p, "pointer"))
6528 mode = ptr_mode;
6529 else if (!strcmp (p, "libgcc_cmp_return"))
6530 mode = targetm.libgcc_cmp_return_mode ();
6531 else if (!strcmp (p, "libgcc_shift_count"))
6532 mode = targetm.libgcc_shift_count_mode ();
6533 else if (!strcmp (p, "unwind_word"))
6534 mode = targetm.unwind_word_mode ();
6535 else
6536 for (j = 0; j < NUM_MACHINE_MODES; j++)
6537 if (!strcmp (p, GET_MODE_NAME (j)))
6539 mode = (enum machine_mode) j;
6540 break;
6543 if (mode == VOIDmode)
6545 error ("unknown machine mode %qE", ident);
6546 return NULL_TREE;
6549 valid_mode = false;
6550 switch (GET_MODE_CLASS (mode))
6552 case MODE_INT:
6553 case MODE_PARTIAL_INT:
6554 case MODE_FLOAT:
6555 case MODE_DECIMAL_FLOAT:
6556 case MODE_FRACT:
6557 case MODE_UFRACT:
6558 case MODE_ACCUM:
6559 case MODE_UACCUM:
6560 valid_mode = targetm.scalar_mode_supported_p (mode);
6561 break;
6563 case MODE_COMPLEX_INT:
6564 case MODE_COMPLEX_FLOAT:
6565 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
6566 break;
6568 case MODE_VECTOR_INT:
6569 case MODE_VECTOR_FLOAT:
6570 case MODE_VECTOR_FRACT:
6571 case MODE_VECTOR_UFRACT:
6572 case MODE_VECTOR_ACCUM:
6573 case MODE_VECTOR_UACCUM:
6574 warning (OPT_Wattributes, "specifying vector types with "
6575 "__attribute__ ((mode)) is deprecated");
6576 warning (OPT_Wattributes,
6577 "use __attribute__ ((vector_size)) instead");
6578 valid_mode = vector_mode_valid_p (mode);
6579 break;
6581 default:
6582 break;
6584 if (!valid_mode)
6586 error ("unable to emulate %qs", p);
6587 return NULL_TREE;
6590 if (POINTER_TYPE_P (type))
6592 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
6593 tree (*fn)(tree, enum machine_mode, bool);
6595 if (!targetm.addr_space.valid_pointer_mode (mode, as))
6597 error ("invalid pointer mode %qs", p);
6598 return NULL_TREE;
6601 if (TREE_CODE (type) == POINTER_TYPE)
6602 fn = build_pointer_type_for_mode;
6603 else
6604 fn = build_reference_type_for_mode;
6605 typefm = fn (TREE_TYPE (type), mode, false);
6607 else
6609 /* For fixed-point modes, we need to test if the signness of type
6610 and the machine mode are consistent. */
6611 if (ALL_FIXED_POINT_MODE_P (mode)
6612 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
6614 error ("signedness of type and machine mode %qs don%'t match", p);
6615 return NULL_TREE;
6617 /* For fixed-point modes, we need to pass saturating info. */
6618 typefm = lang_hooks.types.type_for_mode (mode,
6619 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
6620 : TYPE_UNSIGNED (type));
6623 if (typefm == NULL_TREE)
6625 error ("no data type for mode %qs", p);
6626 return NULL_TREE;
6628 else if (TREE_CODE (type) == ENUMERAL_TYPE)
6630 /* For enumeral types, copy the precision from the integer
6631 type returned above. If not an INTEGER_TYPE, we can't use
6632 this mode for this type. */
6633 if (TREE_CODE (typefm) != INTEGER_TYPE)
6635 error ("cannot use mode %qs for enumeral types", p);
6636 return NULL_TREE;
6639 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
6641 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
6642 typefm = type;
6644 else
6646 /* We cannot build a type variant, as there's code that assumes
6647 that TYPE_MAIN_VARIANT has the same mode. This includes the
6648 debug generators. Instead, create a subrange type. This
6649 results in all of the enumeral values being emitted only once
6650 in the original, and the subtype gets them by reference. */
6651 if (TYPE_UNSIGNED (type))
6652 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
6653 else
6654 typefm = make_signed_type (TYPE_PRECISION (typefm));
6655 TREE_TYPE (typefm) = type;
6658 else if (VECTOR_MODE_P (mode)
6659 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
6660 : TREE_CODE (type) != TREE_CODE (typefm))
6662 error ("mode %qs applied to inappropriate type", p);
6663 return NULL_TREE;
6666 *node = typefm;
6669 return NULL_TREE;
6672 /* Handle a "section" attribute; arguments as in
6673 struct attribute_spec.handler. */
6675 static tree
6676 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6677 int ARG_UNUSED (flags), bool *no_add_attrs)
6679 tree decl = *node;
6681 if (targetm_common.have_named_sections)
6683 user_defined_section_attribute = true;
6685 if ((TREE_CODE (decl) == FUNCTION_DECL
6686 || TREE_CODE (decl) == VAR_DECL)
6687 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6689 if (TREE_CODE (decl) == VAR_DECL
6690 && current_function_decl != NULL_TREE
6691 && !TREE_STATIC (decl))
6693 error_at (DECL_SOURCE_LOCATION (decl),
6694 "section attribute cannot be specified for "
6695 "local variables");
6696 *no_add_attrs = true;
6699 /* The decl may have already been given a section attribute
6700 from a previous declaration. Ensure they match. */
6701 else if (DECL_SECTION_NAME (decl) != NULL_TREE
6702 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
6703 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
6705 error ("section of %q+D conflicts with previous declaration",
6706 *node);
6707 *no_add_attrs = true;
6709 else if (TREE_CODE (decl) == VAR_DECL
6710 && !targetm.have_tls && targetm.emutls.tmpl_section
6711 && DECL_THREAD_LOCAL_P (decl))
6713 error ("section of %q+D cannot be overridden", *node);
6714 *no_add_attrs = true;
6716 else
6717 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
6719 else
6721 error ("section attribute not allowed for %q+D", *node);
6722 *no_add_attrs = true;
6725 else
6727 error_at (DECL_SOURCE_LOCATION (*node),
6728 "section attributes are not supported for this target");
6729 *no_add_attrs = true;
6732 return NULL_TREE;
6735 /* Check whether ALIGN is a valid user-specified alignment. If so,
6736 return its base-2 log; if not, output an error and return -1. If
6737 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
6738 no error. */
6740 check_user_alignment (const_tree align, bool allow_zero)
6742 int i;
6744 if (TREE_CODE (align) != INTEGER_CST
6745 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
6747 error ("requested alignment is not an integer constant");
6748 return -1;
6750 else if (allow_zero && integer_zerop (align))
6751 return -1;
6752 else if ((i = tree_log2 (align)) == -1)
6754 error ("requested alignment is not a power of 2");
6755 return -1;
6757 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
6759 error ("requested alignment is too large");
6760 return -1;
6762 return i;
6765 /* Handle a "aligned" attribute; arguments as in
6766 struct attribute_spec.handler. */
6768 static tree
6769 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6770 int flags, bool *no_add_attrs)
6772 tree decl = NULL_TREE;
6773 tree *type = NULL;
6774 int is_type = 0;
6775 tree align_expr = (args ? TREE_VALUE (args)
6776 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
6777 int i;
6779 if (DECL_P (*node))
6781 decl = *node;
6782 type = &TREE_TYPE (decl);
6783 is_type = TREE_CODE (*node) == TYPE_DECL;
6785 else if (TYPE_P (*node))
6786 type = node, is_type = 1;
6788 if ((i = check_user_alignment (align_expr, false)) == -1)
6789 *no_add_attrs = true;
6790 else if (is_type)
6792 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6793 /* OK, modify the type in place. */;
6794 /* If we have a TYPE_DECL, then copy the type, so that we
6795 don't accidentally modify a builtin type. See pushdecl. */
6796 else if (decl && TREE_TYPE (decl) != error_mark_node
6797 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
6799 tree tt = TREE_TYPE (decl);
6800 *type = build_variant_type_copy (*type);
6801 DECL_ORIGINAL_TYPE (decl) = tt;
6802 TYPE_NAME (*type) = decl;
6803 TREE_USED (*type) = TREE_USED (decl);
6804 TREE_TYPE (decl) = *type;
6806 else
6807 *type = build_variant_type_copy (*type);
6809 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
6810 TYPE_USER_ALIGN (*type) = 1;
6812 else if (! VAR_OR_FUNCTION_DECL_P (decl)
6813 && TREE_CODE (decl) != FIELD_DECL)
6815 error ("alignment may not be specified for %q+D", decl);
6816 *no_add_attrs = true;
6818 else if (TREE_CODE (decl) == FUNCTION_DECL
6819 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
6821 if (DECL_USER_ALIGN (decl))
6822 error ("alignment for %q+D was previously specified as %d "
6823 "and may not be decreased", decl,
6824 DECL_ALIGN (decl) / BITS_PER_UNIT);
6825 else
6826 error ("alignment for %q+D must be at least %d", decl,
6827 DECL_ALIGN (decl) / BITS_PER_UNIT);
6828 *no_add_attrs = true;
6830 else
6832 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
6833 DECL_USER_ALIGN (decl) = 1;
6836 return NULL_TREE;
6839 /* Handle a "weak" attribute; arguments as in
6840 struct attribute_spec.handler. */
6842 static tree
6843 handle_weak_attribute (tree *node, tree name,
6844 tree ARG_UNUSED (args),
6845 int ARG_UNUSED (flags),
6846 bool * ARG_UNUSED (no_add_attrs))
6848 if (TREE_CODE (*node) == FUNCTION_DECL
6849 && DECL_DECLARED_INLINE_P (*node))
6851 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6852 *no_add_attrs = true;
6854 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6856 error ("indirect function %q+D cannot be declared weak", *node);
6857 *no_add_attrs = true;
6858 return NULL_TREE;
6860 else if (TREE_CODE (*node) == FUNCTION_DECL
6861 || TREE_CODE (*node) == VAR_DECL)
6862 declare_weak (*node);
6863 else
6864 warning (OPT_Wattributes, "%qE attribute ignored", name);
6866 return NULL_TREE;
6869 /* Handle an "alias" or "ifunc" attribute; arguments as in
6870 struct attribute_spec.handler, except that IS_ALIAS tells us
6871 whether this is an alias as opposed to ifunc attribute. */
6873 static tree
6874 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
6875 bool *no_add_attrs)
6877 tree decl = *node;
6879 if (TREE_CODE (decl) != FUNCTION_DECL
6880 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
6882 warning (OPT_Wattributes, "%qE attribute ignored", name);
6883 *no_add_attrs = true;
6885 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
6886 || (TREE_CODE (decl) != FUNCTION_DECL
6887 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
6888 /* A static variable declaration is always a tentative definition,
6889 but the alias is a non-tentative definition which overrides. */
6890 || (TREE_CODE (decl) != FUNCTION_DECL
6891 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
6893 error ("%q+D defined both normally and as %qE attribute", decl, name);
6894 *no_add_attrs = true;
6895 return NULL_TREE;
6897 else if (!is_alias
6898 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
6899 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
6901 error ("weak %q+D cannot be defined %qE", decl, name);
6902 *no_add_attrs = true;
6903 return NULL_TREE;
6906 /* Note that the very first time we process a nested declaration,
6907 decl_function_context will not be set. Indeed, *would* never
6908 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
6909 we do below. After such frobbery, pushdecl would set the context.
6910 In any case, this is never what we want. */
6911 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
6913 tree id;
6915 id = TREE_VALUE (args);
6916 if (TREE_CODE (id) != STRING_CST)
6918 error ("attribute %qE argument not a string", name);
6919 *no_add_attrs = true;
6920 return NULL_TREE;
6922 id = get_identifier (TREE_STRING_POINTER (id));
6923 /* This counts as a use of the object pointed to. */
6924 TREE_USED (id) = 1;
6926 if (TREE_CODE (decl) == FUNCTION_DECL)
6927 DECL_INITIAL (decl) = error_mark_node;
6928 else
6930 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6931 DECL_EXTERNAL (decl) = 1;
6932 else
6933 DECL_EXTERNAL (decl) = 0;
6934 TREE_STATIC (decl) = 1;
6937 if (!is_alias)
6938 /* ifuncs are also aliases, so set that attribute too. */
6939 DECL_ATTRIBUTES (decl)
6940 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
6942 else
6944 warning (OPT_Wattributes, "%qE attribute ignored", name);
6945 *no_add_attrs = true;
6948 return NULL_TREE;
6951 /* Handle an "alias" or "ifunc" attribute; arguments as in
6952 struct attribute_spec.handler. */
6954 static tree
6955 handle_ifunc_attribute (tree *node, tree name, tree args,
6956 int ARG_UNUSED (flags), bool *no_add_attrs)
6958 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
6961 /* Handle an "alias" or "ifunc" attribute; arguments as in
6962 struct attribute_spec.handler. */
6964 static tree
6965 handle_alias_attribute (tree *node, tree name, tree args,
6966 int ARG_UNUSED (flags), bool *no_add_attrs)
6968 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
6971 /* Handle a "weakref" attribute; arguments as in struct
6972 attribute_spec.handler. */
6974 static tree
6975 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6976 int flags, bool *no_add_attrs)
6978 tree attr = NULL_TREE;
6980 /* We must ignore the attribute when it is associated with
6981 local-scoped decls, since attribute alias is ignored and many
6982 such symbols do not even have a DECL_WEAK field. */
6983 if (decl_function_context (*node)
6984 || current_function_decl
6985 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
6987 warning (OPT_Wattributes, "%qE attribute ignored", name);
6988 *no_add_attrs = true;
6989 return NULL_TREE;
6992 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6994 error ("indirect function %q+D cannot be declared weakref", *node);
6995 *no_add_attrs = true;
6996 return NULL_TREE;
6999 /* The idea here is that `weakref("name")' mutates into `weakref,
7000 alias("name")', and weakref without arguments, in turn,
7001 implicitly adds weak. */
7003 if (args)
7005 attr = tree_cons (get_identifier ("alias"), args, attr);
7006 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7008 *no_add_attrs = true;
7010 decl_attributes (node, attr, flags);
7012 else
7014 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7015 error_at (DECL_SOURCE_LOCATION (*node),
7016 "weakref attribute must appear before alias attribute");
7018 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7019 and that isn't supported; and because it wants to add it to
7020 the list of weak decls, which isn't helpful. */
7021 DECL_WEAK (*node) = 1;
7024 return NULL_TREE;
7027 /* Handle an "visibility" attribute; arguments as in
7028 struct attribute_spec.handler. */
7030 static tree
7031 handle_visibility_attribute (tree *node, tree name, tree args,
7032 int ARG_UNUSED (flags),
7033 bool *ARG_UNUSED (no_add_attrs))
7035 tree decl = *node;
7036 tree id = TREE_VALUE (args);
7037 enum symbol_visibility vis;
7039 if (TYPE_P (*node))
7041 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7042 /* OK */;
7043 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7045 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7046 name);
7047 return NULL_TREE;
7049 else if (TYPE_FIELDS (*node))
7051 error ("%qE attribute ignored because %qT is already defined",
7052 name, *node);
7053 return NULL_TREE;
7056 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7058 warning (OPT_Wattributes, "%qE attribute ignored", name);
7059 return NULL_TREE;
7062 if (TREE_CODE (id) != STRING_CST)
7064 error ("visibility argument not a string");
7065 return NULL_TREE;
7068 /* If this is a type, set the visibility on the type decl. */
7069 if (TYPE_P (decl))
7071 decl = TYPE_NAME (decl);
7072 if (!decl)
7073 return NULL_TREE;
7074 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7076 warning (OPT_Wattributes, "%qE attribute ignored on types",
7077 name);
7078 return NULL_TREE;
7082 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7083 vis = VISIBILITY_DEFAULT;
7084 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7085 vis = VISIBILITY_INTERNAL;
7086 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7087 vis = VISIBILITY_HIDDEN;
7088 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7089 vis = VISIBILITY_PROTECTED;
7090 else
7092 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7093 vis = VISIBILITY_DEFAULT;
7096 if (DECL_VISIBILITY_SPECIFIED (decl)
7097 && vis != DECL_VISIBILITY (decl))
7099 tree attributes = (TYPE_P (*node)
7100 ? TYPE_ATTRIBUTES (*node)
7101 : DECL_ATTRIBUTES (decl));
7102 if (lookup_attribute ("visibility", attributes))
7103 error ("%qD redeclared with different visibility", decl);
7104 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7105 && lookup_attribute ("dllimport", attributes))
7106 error ("%qD was declared %qs which implies default visibility",
7107 decl, "dllimport");
7108 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7109 && lookup_attribute ("dllexport", attributes))
7110 error ("%qD was declared %qs which implies default visibility",
7111 decl, "dllexport");
7114 DECL_VISIBILITY (decl) = vis;
7115 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7117 /* Go ahead and attach the attribute to the node as well. This is needed
7118 so we can determine whether we have VISIBILITY_DEFAULT because the
7119 visibility was not specified, or because it was explicitly overridden
7120 from the containing scope. */
7122 return NULL_TREE;
7125 /* Determine the ELF symbol visibility for DECL, which is either a
7126 variable or a function. It is an error to use this function if a
7127 definition of DECL is not available in this translation unit.
7128 Returns true if the final visibility has been determined by this
7129 function; false if the caller is free to make additional
7130 modifications. */
7132 bool
7133 c_determine_visibility (tree decl)
7135 gcc_assert (TREE_CODE (decl) == VAR_DECL
7136 || TREE_CODE (decl) == FUNCTION_DECL);
7138 /* If the user explicitly specified the visibility with an
7139 attribute, honor that. DECL_VISIBILITY will have been set during
7140 the processing of the attribute. We check for an explicit
7141 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7142 to distinguish the use of an attribute from the use of a "#pragma
7143 GCC visibility push(...)"; in the latter case we still want other
7144 considerations to be able to overrule the #pragma. */
7145 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7146 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7147 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7148 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7149 return true;
7151 /* Set default visibility to whatever the user supplied with
7152 visibility_specified depending on #pragma GCC visibility. */
7153 if (!DECL_VISIBILITY_SPECIFIED (decl))
7155 if (visibility_options.inpragma
7156 || DECL_VISIBILITY (decl) != default_visibility)
7158 DECL_VISIBILITY (decl) = default_visibility;
7159 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7160 /* If visibility changed and DECL already has DECL_RTL, ensure
7161 symbol flags are updated. */
7162 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7163 || TREE_CODE (decl) == FUNCTION_DECL)
7164 && DECL_RTL_SET_P (decl))
7165 make_decl_rtl (decl);
7168 return false;
7171 /* Handle an "tls_model" attribute; arguments as in
7172 struct attribute_spec.handler. */
7174 static tree
7175 handle_tls_model_attribute (tree *node, tree name, tree args,
7176 int ARG_UNUSED (flags), bool *no_add_attrs)
7178 tree id;
7179 tree decl = *node;
7180 enum tls_model kind;
7182 *no_add_attrs = true;
7184 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7186 warning (OPT_Wattributes, "%qE attribute ignored", name);
7187 return NULL_TREE;
7190 kind = DECL_TLS_MODEL (decl);
7191 id = TREE_VALUE (args);
7192 if (TREE_CODE (id) != STRING_CST)
7194 error ("tls_model argument not a string");
7195 return NULL_TREE;
7198 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7199 kind = TLS_MODEL_LOCAL_EXEC;
7200 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7201 kind = TLS_MODEL_INITIAL_EXEC;
7202 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7203 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7204 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7205 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7206 else
7207 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7209 DECL_TLS_MODEL (decl) = kind;
7210 return NULL_TREE;
7213 /* Handle a "no_instrument_function" attribute; arguments as in
7214 struct attribute_spec.handler. */
7216 static tree
7217 handle_no_instrument_function_attribute (tree *node, tree name,
7218 tree ARG_UNUSED (args),
7219 int ARG_UNUSED (flags),
7220 bool *no_add_attrs)
7222 tree decl = *node;
7224 if (TREE_CODE (decl) != FUNCTION_DECL)
7226 error_at (DECL_SOURCE_LOCATION (decl),
7227 "%qE attribute applies only to functions", name);
7228 *no_add_attrs = true;
7230 else if (DECL_INITIAL (decl))
7232 error_at (DECL_SOURCE_LOCATION (decl),
7233 "can%'t set %qE attribute after definition", name);
7234 *no_add_attrs = true;
7236 else
7237 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7239 return NULL_TREE;
7242 /* Handle a "malloc" attribute; arguments as in
7243 struct attribute_spec.handler. */
7245 static tree
7246 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7247 int ARG_UNUSED (flags), bool *no_add_attrs)
7249 if (TREE_CODE (*node) == FUNCTION_DECL
7250 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7251 DECL_IS_MALLOC (*node) = 1;
7252 else
7254 warning (OPT_Wattributes, "%qE attribute ignored", name);
7255 *no_add_attrs = true;
7258 return NULL_TREE;
7261 /* Handle a "alloc_size" attribute; arguments as in
7262 struct attribute_spec.handler. */
7264 static tree
7265 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7266 int ARG_UNUSED (flags), bool *no_add_attrs)
7268 unsigned arg_count = type_num_arguments (*node);
7269 for (; args; args = TREE_CHAIN (args))
7271 tree position = TREE_VALUE (args);
7273 if (TREE_CODE (position) != INTEGER_CST
7274 || TREE_INT_CST_HIGH (position)
7275 || TREE_INT_CST_LOW (position) < 1
7276 || TREE_INT_CST_LOW (position) > arg_count )
7278 warning (OPT_Wattributes,
7279 "alloc_size parameter outside range");
7280 *no_add_attrs = true;
7281 return NULL_TREE;
7284 return NULL_TREE;
7287 /* Handle a "fn spec" attribute; arguments as in
7288 struct attribute_spec.handler. */
7290 static tree
7291 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7292 tree args, int ARG_UNUSED (flags),
7293 bool *no_add_attrs ATTRIBUTE_UNUSED)
7295 gcc_assert (args
7296 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7297 && !TREE_CHAIN (args));
7298 return NULL_TREE;
7301 /* Handle a "returns_twice" attribute; arguments as in
7302 struct attribute_spec.handler. */
7304 static tree
7305 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7306 int ARG_UNUSED (flags), bool *no_add_attrs)
7308 if (TREE_CODE (*node) == FUNCTION_DECL)
7309 DECL_IS_RETURNS_TWICE (*node) = 1;
7310 else
7312 warning (OPT_Wattributes, "%qE attribute ignored", name);
7313 *no_add_attrs = true;
7316 return NULL_TREE;
7319 /* Handle a "no_limit_stack" attribute; arguments as in
7320 struct attribute_spec.handler. */
7322 static tree
7323 handle_no_limit_stack_attribute (tree *node, tree name,
7324 tree ARG_UNUSED (args),
7325 int ARG_UNUSED (flags),
7326 bool *no_add_attrs)
7328 tree decl = *node;
7330 if (TREE_CODE (decl) != FUNCTION_DECL)
7332 error_at (DECL_SOURCE_LOCATION (decl),
7333 "%qE attribute applies only to functions", name);
7334 *no_add_attrs = true;
7336 else if (DECL_INITIAL (decl))
7338 error_at (DECL_SOURCE_LOCATION (decl),
7339 "can%'t set %qE attribute after definition", name);
7340 *no_add_attrs = true;
7342 else
7343 DECL_NO_LIMIT_STACK (decl) = 1;
7345 return NULL_TREE;
7348 /* Handle a "pure" attribute; arguments as in
7349 struct attribute_spec.handler. */
7351 static tree
7352 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7353 int ARG_UNUSED (flags), bool *no_add_attrs)
7355 if (TREE_CODE (*node) == FUNCTION_DECL)
7356 DECL_PURE_P (*node) = 1;
7357 /* ??? TODO: Support types. */
7358 else
7360 warning (OPT_Wattributes, "%qE attribute ignored", name);
7361 *no_add_attrs = true;
7364 return NULL_TREE;
7367 /* Digest an attribute list destined for a transactional memory statement.
7368 ALLOWED is the set of attributes that are allowed for this statement;
7369 return the attribute we parsed. Multiple attributes are never allowed. */
7372 parse_tm_stmt_attr (tree attrs, int allowed)
7374 tree a_seen = NULL;
7375 int m_seen = 0;
7377 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
7379 tree a = TREE_PURPOSE (attrs);
7380 int m = 0;
7382 if (is_attribute_p ("outer", a))
7383 m = TM_STMT_ATTR_OUTER;
7385 if ((m & allowed) == 0)
7387 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
7388 continue;
7391 if (m_seen == 0)
7393 a_seen = a;
7394 m_seen = m;
7396 else if (m_seen == m)
7397 warning (OPT_Wattributes, "%qE attribute duplicated", a);
7398 else
7399 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
7402 return m_seen;
7405 /* Transform a TM attribute name into a maskable integer and back.
7406 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
7407 to how the lack of an attribute is treated. */
7410 tm_attr_to_mask (tree attr)
7412 if (attr == NULL)
7413 return 0;
7414 if (is_attribute_p ("transaction_safe", attr))
7415 return TM_ATTR_SAFE;
7416 if (is_attribute_p ("transaction_callable", attr))
7417 return TM_ATTR_CALLABLE;
7418 if (is_attribute_p ("transaction_pure", attr))
7419 return TM_ATTR_PURE;
7420 if (is_attribute_p ("transaction_unsafe", attr))
7421 return TM_ATTR_IRREVOCABLE;
7422 if (is_attribute_p ("transaction_may_cancel_outer", attr))
7423 return TM_ATTR_MAY_CANCEL_OUTER;
7424 return 0;
7427 tree
7428 tm_mask_to_attr (int mask)
7430 const char *str;
7431 switch (mask)
7433 case TM_ATTR_SAFE:
7434 str = "transaction_safe";
7435 break;
7436 case TM_ATTR_CALLABLE:
7437 str = "transaction_callable";
7438 break;
7439 case TM_ATTR_PURE:
7440 str = "transaction_pure";
7441 break;
7442 case TM_ATTR_IRREVOCABLE:
7443 str = "transaction_unsafe";
7444 break;
7445 case TM_ATTR_MAY_CANCEL_OUTER:
7446 str = "transaction_may_cancel_outer";
7447 break;
7448 default:
7449 gcc_unreachable ();
7451 return get_identifier (str);
7454 /* Return the first TM attribute seen in LIST. */
7456 tree
7457 find_tm_attribute (tree list)
7459 for (; list ; list = TREE_CHAIN (list))
7461 tree name = TREE_PURPOSE (list);
7462 if (tm_attr_to_mask (name) != 0)
7463 return name;
7465 return NULL_TREE;
7468 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
7469 Here we accept only function types, and verify that none of the other
7470 function TM attributes are also applied. */
7471 /* ??? We need to accept class types for C++, but not C. This greatly
7472 complicates this function, since we can no longer rely on the extra
7473 processing given by function_type_required. */
7475 static tree
7476 handle_tm_attribute (tree *node, tree name, tree args,
7477 int flags, bool *no_add_attrs)
7479 /* Only one path adds the attribute; others don't. */
7480 *no_add_attrs = true;
7482 switch (TREE_CODE (*node))
7484 case RECORD_TYPE:
7485 case UNION_TYPE:
7486 /* Only tm_callable and tm_safe apply to classes. */
7487 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
7488 goto ignored;
7489 /* FALLTHRU */
7491 case FUNCTION_TYPE:
7492 case METHOD_TYPE:
7494 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
7495 if (old_name == name)
7497 else if (old_name != NULL_TREE)
7498 error ("type was previously declared %qE", old_name);
7499 else
7500 *no_add_attrs = false;
7502 break;
7504 case POINTER_TYPE:
7506 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
7507 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
7509 tree fn_tmp = TREE_TYPE (*node);
7510 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
7511 *node = build_pointer_type (fn_tmp);
7512 break;
7515 /* FALLTHRU */
7517 default:
7518 /* If a function is next, pass it on to be tried next. */
7519 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
7520 return tree_cons (name, args, NULL);
7522 ignored:
7523 warning (OPT_Wattributes, "%qE attribute ignored", name);
7524 break;
7527 return NULL_TREE;
7530 /* Handle the TM_WRAP attribute; arguments as in
7531 struct attribute_spec.handler. */
7533 static tree
7534 handle_tm_wrap_attribute (tree *node, tree name, tree args,
7535 int ARG_UNUSED (flags), bool *no_add_attrs)
7537 tree decl = *node;
7539 /* We don't need the attribute even on success, since we
7540 record the entry in an external table. */
7541 *no_add_attrs = true;
7543 if (TREE_CODE (decl) != FUNCTION_DECL)
7544 warning (OPT_Wattributes, "%qE attribute ignored", name);
7545 else
7547 tree wrap_decl = TREE_VALUE (args);
7548 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
7549 && TREE_CODE (wrap_decl) != VAR_DECL
7550 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
7551 error ("%qE argument not an identifier", name);
7552 else
7554 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
7555 wrap_decl = lookup_name (wrap_decl);
7556 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
7558 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
7559 TREE_TYPE (wrap_decl)))
7560 record_tm_replacement (wrap_decl, decl);
7561 else
7562 error ("%qD is not compatible with %qD", wrap_decl, decl);
7564 else
7565 error ("transaction_wrap argument is not a function");
7569 return NULL_TREE;
7572 /* Ignore the given attribute. Used when this attribute may be usefully
7573 overridden by the target, but is not used generically. */
7575 static tree
7576 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
7577 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7578 bool *no_add_attrs)
7580 *no_add_attrs = true;
7581 return NULL_TREE;
7584 /* Handle a "no vops" attribute; arguments as in
7585 struct attribute_spec.handler. */
7587 static tree
7588 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
7589 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7590 bool *ARG_UNUSED (no_add_attrs))
7592 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
7593 DECL_IS_NOVOPS (*node) = 1;
7594 return NULL_TREE;
7597 /* Handle a "deprecated" attribute; arguments as in
7598 struct attribute_spec.handler. */
7600 static tree
7601 handle_deprecated_attribute (tree *node, tree name,
7602 tree args, int flags,
7603 bool *no_add_attrs)
7605 tree type = NULL_TREE;
7606 int warn = 0;
7607 tree what = NULL_TREE;
7609 if (!args)
7610 *no_add_attrs = true;
7611 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7613 error ("deprecated message is not a string");
7614 *no_add_attrs = true;
7617 if (DECL_P (*node))
7619 tree decl = *node;
7620 type = TREE_TYPE (decl);
7622 if (TREE_CODE (decl) == TYPE_DECL
7623 || TREE_CODE (decl) == PARM_DECL
7624 || TREE_CODE (decl) == VAR_DECL
7625 || TREE_CODE (decl) == FUNCTION_DECL
7626 || TREE_CODE (decl) == FIELD_DECL
7627 || objc_method_decl (TREE_CODE (decl)))
7628 TREE_DEPRECATED (decl) = 1;
7629 else
7630 warn = 1;
7632 else if (TYPE_P (*node))
7634 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7635 *node = build_variant_type_copy (*node);
7636 TREE_DEPRECATED (*node) = 1;
7637 type = *node;
7639 else
7640 warn = 1;
7642 if (warn)
7644 *no_add_attrs = true;
7645 if (type && TYPE_NAME (type))
7647 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
7648 what = TYPE_NAME (*node);
7649 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7650 && DECL_NAME (TYPE_NAME (type)))
7651 what = DECL_NAME (TYPE_NAME (type));
7653 if (what)
7654 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
7655 else
7656 warning (OPT_Wattributes, "%qE attribute ignored", name);
7659 return NULL_TREE;
7662 /* Handle a "vector_size" attribute; arguments as in
7663 struct attribute_spec.handler. */
7665 static tree
7666 handle_vector_size_attribute (tree *node, tree name, tree args,
7667 int ARG_UNUSED (flags),
7668 bool *no_add_attrs)
7670 unsigned HOST_WIDE_INT vecsize, nunits;
7671 enum machine_mode orig_mode;
7672 tree type = *node, new_type, size;
7674 *no_add_attrs = true;
7676 size = TREE_VALUE (args);
7678 if (!host_integerp (size, 1))
7680 warning (OPT_Wattributes, "%qE attribute ignored", name);
7681 return NULL_TREE;
7684 /* Get the vector size (in bytes). */
7685 vecsize = tree_low_cst (size, 1);
7687 /* We need to provide for vector pointers, vector arrays, and
7688 functions returning vectors. For example:
7690 __attribute__((vector_size(16))) short *foo;
7692 In this case, the mode is SI, but the type being modified is
7693 HI, so we need to look further. */
7695 while (POINTER_TYPE_P (type)
7696 || TREE_CODE (type) == FUNCTION_TYPE
7697 || TREE_CODE (type) == METHOD_TYPE
7698 || TREE_CODE (type) == ARRAY_TYPE
7699 || TREE_CODE (type) == OFFSET_TYPE)
7700 type = TREE_TYPE (type);
7702 /* Get the mode of the type being modified. */
7703 orig_mode = TYPE_MODE (type);
7705 if ((!INTEGRAL_TYPE_P (type)
7706 && !SCALAR_FLOAT_TYPE_P (type)
7707 && !FIXED_POINT_TYPE_P (type))
7708 || (!SCALAR_FLOAT_MODE_P (orig_mode)
7709 && GET_MODE_CLASS (orig_mode) != MODE_INT
7710 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
7711 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
7712 || TREE_CODE (type) == BOOLEAN_TYPE)
7714 error ("invalid vector type for attribute %qE", name);
7715 return NULL_TREE;
7718 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
7720 error ("vector size not an integral multiple of component size");
7721 return NULL;
7724 if (vecsize == 0)
7726 error ("zero vector size");
7727 return NULL;
7730 /* Calculate how many units fit in the vector. */
7731 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
7732 if (nunits & (nunits - 1))
7734 error ("number of components of the vector not a power of two");
7735 return NULL_TREE;
7738 new_type = build_vector_type (type, nunits);
7740 /* Build back pointers if needed. */
7741 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
7743 return NULL_TREE;
7746 /* Handle the "nonnull" attribute. */
7747 static tree
7748 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
7749 tree args, int ARG_UNUSED (flags),
7750 bool *no_add_attrs)
7752 tree type = *node;
7753 unsigned HOST_WIDE_INT attr_arg_num;
7755 /* If no arguments are specified, all pointer arguments should be
7756 non-null. Verify a full prototype is given so that the arguments
7757 will have the correct types when we actually check them later. */
7758 if (!args)
7760 if (!prototype_p (type))
7762 error ("nonnull attribute without arguments on a non-prototype");
7763 *no_add_attrs = true;
7765 return NULL_TREE;
7768 /* Argument list specified. Verify that each argument number references
7769 a pointer argument. */
7770 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
7772 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
7774 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
7776 error ("nonnull argument has invalid operand number (argument %lu)",
7777 (unsigned long) attr_arg_num);
7778 *no_add_attrs = true;
7779 return NULL_TREE;
7782 if (prototype_p (type))
7784 function_args_iterator iter;
7785 tree argument;
7787 function_args_iter_init (&iter, type);
7788 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
7790 argument = function_args_iter_cond (&iter);
7791 if (argument == NULL_TREE || ck_num == arg_num)
7792 break;
7795 if (!argument
7796 || TREE_CODE (argument) == VOID_TYPE)
7798 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
7799 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7800 *no_add_attrs = true;
7801 return NULL_TREE;
7804 if (TREE_CODE (argument) != POINTER_TYPE)
7806 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
7807 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7808 *no_add_attrs = true;
7809 return NULL_TREE;
7814 return NULL_TREE;
7817 /* Check the argument list of a function call for null in argument slots
7818 that are marked as requiring a non-null pointer argument. The NARGS
7819 arguments are passed in the array ARGARRAY.
7822 static void
7823 check_function_nonnull (tree attrs, int nargs, tree *argarray)
7825 tree a, args;
7826 int i;
7828 for (a = attrs; a; a = TREE_CHAIN (a))
7830 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
7832 args = TREE_VALUE (a);
7834 /* Walk the argument list. If we encounter an argument number we
7835 should check for non-null, do it. If the attribute has no args,
7836 then every pointer argument is checked (in which case the check
7837 for pointer type is done in check_nonnull_arg). */
7838 for (i = 0; i < nargs; i++)
7840 if (!args || nonnull_check_p (args, i + 1))
7841 check_function_arguments_recurse (check_nonnull_arg, NULL,
7842 argarray[i],
7843 i + 1);
7849 /* Check that the Nth argument of a function call (counting backwards
7850 from the end) is a (pointer)0. The NARGS arguments are passed in the
7851 array ARGARRAY. */
7853 static void
7854 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
7856 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
7858 if (attr)
7860 int len = 0;
7861 int pos = 0;
7862 tree sentinel;
7863 function_args_iterator iter;
7864 tree t;
7866 /* Skip over the named arguments. */
7867 FOREACH_FUNCTION_ARGS (fntype, t, iter)
7869 if (len == nargs)
7870 break;
7871 len++;
7874 if (TREE_VALUE (attr))
7876 tree p = TREE_VALUE (TREE_VALUE (attr));
7877 pos = TREE_INT_CST_LOW (p);
7880 /* The sentinel must be one of the varargs, i.e.
7881 in position >= the number of fixed arguments. */
7882 if ((nargs - 1 - pos) < len)
7884 warning (OPT_Wformat,
7885 "not enough variable arguments to fit a sentinel");
7886 return;
7889 /* Validate the sentinel. */
7890 sentinel = argarray[nargs - 1 - pos];
7891 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
7892 || !integer_zerop (sentinel))
7893 /* Although __null (in C++) is only an integer we allow it
7894 nevertheless, as we are guaranteed that it's exactly
7895 as wide as a pointer, and we don't want to force
7896 users to cast the NULL they have written there.
7897 We warn with -Wstrict-null-sentinel, though. */
7898 && (warn_strict_null_sentinel || null_node != sentinel))
7899 warning (OPT_Wformat, "missing sentinel in function call");
7903 /* Helper for check_function_nonnull; given a list of operands which
7904 must be non-null in ARGS, determine if operand PARAM_NUM should be
7905 checked. */
7907 static bool
7908 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
7910 unsigned HOST_WIDE_INT arg_num = 0;
7912 for (; args; args = TREE_CHAIN (args))
7914 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
7916 gcc_assert (found);
7918 if (arg_num == param_num)
7919 return true;
7921 return false;
7924 /* Check that the function argument PARAM (which is operand number
7925 PARAM_NUM) is non-null. This is called by check_function_nonnull
7926 via check_function_arguments_recurse. */
7928 static void
7929 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
7930 unsigned HOST_WIDE_INT param_num)
7932 /* Just skip checking the argument if it's not a pointer. This can
7933 happen if the "nonnull" attribute was given without an operand
7934 list (which means to check every pointer argument). */
7936 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
7937 return;
7939 if (integer_zerop (param))
7940 warning (OPT_Wnonnull, "null argument where non-null required "
7941 "(argument %lu)", (unsigned long) param_num);
7944 /* Helper for nonnull attribute handling; fetch the operand number
7945 from the attribute argument list. */
7947 static bool
7948 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
7950 /* Verify the arg number is a constant. */
7951 if (TREE_CODE (arg_num_expr) != INTEGER_CST
7952 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
7953 return false;
7955 *valp = TREE_INT_CST_LOW (arg_num_expr);
7956 return true;
7959 /* Handle a "nothrow" attribute; arguments as in
7960 struct attribute_spec.handler. */
7962 static tree
7963 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7964 int ARG_UNUSED (flags), bool *no_add_attrs)
7966 if (TREE_CODE (*node) == FUNCTION_DECL)
7967 TREE_NOTHROW (*node) = 1;
7968 /* ??? TODO: Support types. */
7969 else
7971 warning (OPT_Wattributes, "%qE attribute ignored", name);
7972 *no_add_attrs = true;
7975 return NULL_TREE;
7978 /* Handle a "cleanup" attribute; arguments as in
7979 struct attribute_spec.handler. */
7981 static tree
7982 handle_cleanup_attribute (tree *node, tree name, tree args,
7983 int ARG_UNUSED (flags), bool *no_add_attrs)
7985 tree decl = *node;
7986 tree cleanup_id, cleanup_decl;
7988 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
7989 for global destructors in C++. This requires infrastructure that
7990 we don't have generically at the moment. It's also not a feature
7991 we'd be missing too much, since we do have attribute constructor. */
7992 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
7994 warning (OPT_Wattributes, "%qE attribute ignored", name);
7995 *no_add_attrs = true;
7996 return NULL_TREE;
7999 /* Verify that the argument is a function in scope. */
8000 /* ??? We could support pointers to functions here as well, if
8001 that was considered desirable. */
8002 cleanup_id = TREE_VALUE (args);
8003 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8005 error ("cleanup argument not an identifier");
8006 *no_add_attrs = true;
8007 return NULL_TREE;
8009 cleanup_decl = lookup_name (cleanup_id);
8010 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8012 error ("cleanup argument not a function");
8013 *no_add_attrs = true;
8014 return NULL_TREE;
8017 /* That the function has proper type is checked with the
8018 eventual call to build_function_call. */
8020 return NULL_TREE;
8023 /* Handle a "warn_unused_result" attribute. No special handling. */
8025 static tree
8026 handle_warn_unused_result_attribute (tree *node, tree name,
8027 tree ARG_UNUSED (args),
8028 int ARG_UNUSED (flags), bool *no_add_attrs)
8030 /* Ignore the attribute for functions not returning any value. */
8031 if (VOID_TYPE_P (TREE_TYPE (*node)))
8033 warning (OPT_Wattributes, "%qE attribute ignored", name);
8034 *no_add_attrs = true;
8037 return NULL_TREE;
8040 /* Handle a "sentinel" attribute. */
8042 static tree
8043 handle_sentinel_attribute (tree *node, tree name, tree args,
8044 int ARG_UNUSED (flags), bool *no_add_attrs)
8046 if (!prototype_p (*node))
8048 warning (OPT_Wattributes,
8049 "%qE attribute requires prototypes with named arguments", name);
8050 *no_add_attrs = true;
8052 else
8054 if (!stdarg_p (*node))
8056 warning (OPT_Wattributes,
8057 "%qE attribute only applies to variadic functions", name);
8058 *no_add_attrs = true;
8062 if (args)
8064 tree position = TREE_VALUE (args);
8066 if (TREE_CODE (position) != INTEGER_CST)
8068 warning (OPT_Wattributes,
8069 "requested position is not an integer constant");
8070 *no_add_attrs = true;
8072 else
8074 if (tree_int_cst_lt (position, integer_zero_node))
8076 warning (OPT_Wattributes,
8077 "requested position is less than zero");
8078 *no_add_attrs = true;
8083 return NULL_TREE;
8086 /* Handle a "type_generic" attribute. */
8088 static tree
8089 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8090 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8091 bool * ARG_UNUSED (no_add_attrs))
8093 /* Ensure we have a function type. */
8094 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8096 /* Ensure we have a variadic function. */
8097 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8099 return NULL_TREE;
8102 /* Handle a "target" attribute. */
8104 static tree
8105 handle_target_attribute (tree *node, tree name, tree args, int flags,
8106 bool *no_add_attrs)
8108 /* Ensure we have a function type. */
8109 if (TREE_CODE (*node) != FUNCTION_DECL)
8111 warning (OPT_Wattributes, "%qE attribute ignored", name);
8112 *no_add_attrs = true;
8114 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8115 flags))
8116 *no_add_attrs = true;
8118 return NULL_TREE;
8121 /* Arguments being collected for optimization. */
8122 typedef const char *const_char_p; /* For DEF_VEC_P. */
8123 DEF_VEC_P(const_char_p);
8124 DEF_VEC_ALLOC_P(const_char_p, gc);
8125 static GTY(()) VEC(const_char_p, gc) *optimize_args;
8128 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8129 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8130 false for #pragma GCC optimize. */
8132 bool
8133 parse_optimize_options (tree args, bool attr_p)
8135 bool ret = true;
8136 unsigned opt_argc;
8137 unsigned i;
8138 int saved_flag_strict_aliasing;
8139 const char **opt_argv;
8140 struct cl_decoded_option *decoded_options;
8141 unsigned int decoded_options_count;
8142 tree ap;
8144 /* Build up argv vector. Just in case the string is stored away, use garbage
8145 collected strings. */
8146 VEC_truncate (const_char_p, optimize_args, 0);
8147 VEC_safe_push (const_char_p, gc, optimize_args, NULL);
8149 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8151 tree value = TREE_VALUE (ap);
8153 if (TREE_CODE (value) == INTEGER_CST)
8155 char buffer[20];
8156 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8157 VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer));
8160 else if (TREE_CODE (value) == STRING_CST)
8162 /* Split string into multiple substrings. */
8163 size_t len = TREE_STRING_LENGTH (value);
8164 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8165 char *end = p + len;
8166 char *comma;
8167 char *next_p = p;
8169 while (next_p != NULL)
8171 size_t len2;
8172 char *q, *r;
8174 p = next_p;
8175 comma = strchr (p, ',');
8176 if (comma)
8178 len2 = comma - p;
8179 *comma = '\0';
8180 next_p = comma+1;
8182 else
8184 len2 = end - p;
8185 next_p = NULL;
8188 r = q = (char *) ggc_alloc_atomic (len2 + 3);
8190 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
8191 options. */
8192 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
8194 ret = false;
8195 if (attr_p)
8196 warning (OPT_Wattributes,
8197 "bad option %s to optimize attribute", p);
8198 else
8199 warning (OPT_Wpragmas,
8200 "bad option %s to pragma attribute", p);
8201 continue;
8204 if (*p != '-')
8206 *r++ = '-';
8208 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
8209 itself is -Os, and any other switch begins with a -f. */
8210 if ((*p >= '0' && *p <= '9')
8211 || (p[0] == 's' && p[1] == '\0'))
8212 *r++ = 'O';
8213 else if (*p != 'O')
8214 *r++ = 'f';
8217 memcpy (r, p, len2);
8218 r[len2] = '\0';
8219 VEC_safe_push (const_char_p, gc, optimize_args, q);
8225 opt_argc = VEC_length (const_char_p, optimize_args);
8226 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
8228 for (i = 1; i < opt_argc; i++)
8229 opt_argv[i] = VEC_index (const_char_p, optimize_args, i);
8231 saved_flag_strict_aliasing = flag_strict_aliasing;
8233 /* Now parse the options. */
8234 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
8235 &decoded_options,
8236 &decoded_options_count);
8237 decode_options (&global_options, &global_options_set,
8238 decoded_options, decoded_options_count,
8239 input_location, global_dc);
8241 targetm.override_options_after_change();
8243 /* Don't allow changing -fstrict-aliasing. */
8244 flag_strict_aliasing = saved_flag_strict_aliasing;
8246 VEC_truncate (const_char_p, optimize_args, 0);
8247 return ret;
8250 /* For handling "optimize" attribute. arguments as in
8251 struct attribute_spec.handler. */
8253 static tree
8254 handle_optimize_attribute (tree *node, tree name, tree args,
8255 int ARG_UNUSED (flags), bool *no_add_attrs)
8257 /* Ensure we have a function type. */
8258 if (TREE_CODE (*node) != FUNCTION_DECL)
8260 warning (OPT_Wattributes, "%qE attribute ignored", name);
8261 *no_add_attrs = true;
8263 else
8265 struct cl_optimization cur_opts;
8266 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
8268 /* Save current options. */
8269 cl_optimization_save (&cur_opts, &global_options);
8271 /* If we previously had some optimization options, use them as the
8272 default. */
8273 if (old_opts)
8274 cl_optimization_restore (&global_options,
8275 TREE_OPTIMIZATION (old_opts));
8277 /* Parse options, and update the vector. */
8278 parse_optimize_options (args, true);
8279 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
8280 = build_optimization_node ();
8282 /* Restore current options. */
8283 cl_optimization_restore (&global_options, &cur_opts);
8286 return NULL_TREE;
8289 /* Handle a "no_split_stack" attribute. */
8291 static tree
8292 handle_no_split_stack_attribute (tree *node, tree name,
8293 tree ARG_UNUSED (args),
8294 int ARG_UNUSED (flags),
8295 bool *no_add_attrs)
8297 tree decl = *node;
8299 if (TREE_CODE (decl) != FUNCTION_DECL)
8301 error_at (DECL_SOURCE_LOCATION (decl),
8302 "%qE attribute applies only to functions", name);
8303 *no_add_attrs = true;
8305 else if (DECL_INITIAL (decl))
8307 error_at (DECL_SOURCE_LOCATION (decl),
8308 "can%'t set %qE attribute after definition", name);
8309 *no_add_attrs = true;
8312 return NULL_TREE;
8315 /* Check for valid arguments being passed to a function with FNTYPE.
8316 There are NARGS arguments in the array ARGARRAY. */
8317 void
8318 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
8320 /* Check for null being passed in a pointer argument that must be
8321 non-null. We also need to do this if format checking is enabled. */
8323 if (warn_nonnull)
8324 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8326 /* Check for errors in format strings. */
8328 if (warn_format || warn_missing_format_attribute)
8329 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8331 if (warn_format)
8332 check_function_sentinel (fntype, nargs, argarray);
8335 /* Generic argument checking recursion routine. PARAM is the argument to
8336 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
8337 once the argument is resolved. CTX is context for the callback. */
8338 void
8339 check_function_arguments_recurse (void (*callback)
8340 (void *, tree, unsigned HOST_WIDE_INT),
8341 void *ctx, tree param,
8342 unsigned HOST_WIDE_INT param_num)
8344 if (CONVERT_EXPR_P (param)
8345 && (TYPE_PRECISION (TREE_TYPE (param))
8346 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
8348 /* Strip coercion. */
8349 check_function_arguments_recurse (callback, ctx,
8350 TREE_OPERAND (param, 0), param_num);
8351 return;
8354 if (TREE_CODE (param) == CALL_EXPR)
8356 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
8357 tree attrs;
8358 bool found_format_arg = false;
8360 /* See if this is a call to a known internationalization function
8361 that modifies a format arg. Such a function may have multiple
8362 format_arg attributes (for example, ngettext). */
8364 for (attrs = TYPE_ATTRIBUTES (type);
8365 attrs;
8366 attrs = TREE_CHAIN (attrs))
8367 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
8369 tree inner_arg;
8370 tree format_num_expr;
8371 int format_num;
8372 int i;
8373 call_expr_arg_iterator iter;
8375 /* Extract the argument number, which was previously checked
8376 to be valid. */
8377 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
8379 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
8380 && !TREE_INT_CST_HIGH (format_num_expr));
8382 format_num = TREE_INT_CST_LOW (format_num_expr);
8384 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
8385 inner_arg != 0;
8386 inner_arg = next_call_expr_arg (&iter), i++)
8387 if (i == format_num)
8389 check_function_arguments_recurse (callback, ctx,
8390 inner_arg, param_num);
8391 found_format_arg = true;
8392 break;
8396 /* If we found a format_arg attribute and did a recursive check,
8397 we are done with checking this argument. Otherwise, we continue
8398 and this will be considered a non-literal. */
8399 if (found_format_arg)
8400 return;
8403 if (TREE_CODE (param) == COND_EXPR)
8405 /* Check both halves of the conditional expression. */
8406 check_function_arguments_recurse (callback, ctx,
8407 TREE_OPERAND (param, 1), param_num);
8408 check_function_arguments_recurse (callback, ctx,
8409 TREE_OPERAND (param, 2), param_num);
8410 return;
8413 (*callback) (ctx, param, param_num);
8416 /* Checks for a builtin function FNDECL that the number of arguments
8417 NARGS against the required number REQUIRED and issues an error if
8418 there is a mismatch. Returns true if the number of arguments is
8419 correct, otherwise false. */
8421 static bool
8422 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
8424 if (nargs < required)
8426 error_at (input_location,
8427 "not enough arguments to function %qE", fndecl);
8428 return false;
8430 else if (nargs > required)
8432 error_at (input_location,
8433 "too many arguments to function %qE", fndecl);
8434 return false;
8436 return true;
8439 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
8440 Returns false if there was an error, otherwise true. */
8442 bool
8443 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
8445 if (!DECL_BUILT_IN (fndecl)
8446 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
8447 return true;
8449 switch (DECL_FUNCTION_CODE (fndecl))
8451 case BUILT_IN_CONSTANT_P:
8452 return builtin_function_validate_nargs (fndecl, nargs, 1);
8454 case BUILT_IN_ISFINITE:
8455 case BUILT_IN_ISINF:
8456 case BUILT_IN_ISINF_SIGN:
8457 case BUILT_IN_ISNAN:
8458 case BUILT_IN_ISNORMAL:
8459 if (builtin_function_validate_nargs (fndecl, nargs, 1))
8461 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
8463 error ("non-floating-point argument in call to "
8464 "function %qE", fndecl);
8465 return false;
8467 return true;
8469 return false;
8471 case BUILT_IN_ISGREATER:
8472 case BUILT_IN_ISGREATEREQUAL:
8473 case BUILT_IN_ISLESS:
8474 case BUILT_IN_ISLESSEQUAL:
8475 case BUILT_IN_ISLESSGREATER:
8476 case BUILT_IN_ISUNORDERED:
8477 if (builtin_function_validate_nargs (fndecl, nargs, 2))
8479 enum tree_code code0, code1;
8480 code0 = TREE_CODE (TREE_TYPE (args[0]));
8481 code1 = TREE_CODE (TREE_TYPE (args[1]));
8482 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
8483 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8484 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
8486 error ("non-floating-point arguments in call to "
8487 "function %qE", fndecl);
8488 return false;
8490 return true;
8492 return false;
8494 case BUILT_IN_FPCLASSIFY:
8495 if (builtin_function_validate_nargs (fndecl, nargs, 6))
8497 unsigned i;
8499 for (i=0; i<5; i++)
8500 if (TREE_CODE (args[i]) != INTEGER_CST)
8502 error ("non-const integer argument %u in call to function %qE",
8503 i+1, fndecl);
8504 return false;
8507 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
8509 error ("non-floating-point argument in call to function %qE",
8510 fndecl);
8511 return false;
8513 return true;
8515 return false;
8517 case BUILT_IN_ASSUME_ALIGNED:
8518 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
8520 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
8522 error ("non-integer argument 3 in call to function %qE", fndecl);
8523 return false;
8525 return true;
8527 return false;
8529 default:
8530 return true;
8534 /* Function to help qsort sort FIELD_DECLs by name order. */
8537 field_decl_cmp (const void *x_p, const void *y_p)
8539 const tree *const x = (const tree *const) x_p;
8540 const tree *const y = (const tree *const) y_p;
8542 if (DECL_NAME (*x) == DECL_NAME (*y))
8543 /* A nontype is "greater" than a type. */
8544 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8545 if (DECL_NAME (*x) == NULL_TREE)
8546 return -1;
8547 if (DECL_NAME (*y) == NULL_TREE)
8548 return 1;
8549 if (DECL_NAME (*x) < DECL_NAME (*y))
8550 return -1;
8551 return 1;
8554 static struct {
8555 gt_pointer_operator new_value;
8556 void *cookie;
8557 } resort_data;
8559 /* This routine compares two fields like field_decl_cmp but using the
8560 pointer operator in resort_data. */
8562 static int
8563 resort_field_decl_cmp (const void *x_p, const void *y_p)
8565 const tree *const x = (const tree *const) x_p;
8566 const tree *const y = (const tree *const) y_p;
8568 if (DECL_NAME (*x) == DECL_NAME (*y))
8569 /* A nontype is "greater" than a type. */
8570 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8571 if (DECL_NAME (*x) == NULL_TREE)
8572 return -1;
8573 if (DECL_NAME (*y) == NULL_TREE)
8574 return 1;
8576 tree d1 = DECL_NAME (*x);
8577 tree d2 = DECL_NAME (*y);
8578 resort_data.new_value (&d1, resort_data.cookie);
8579 resort_data.new_value (&d2, resort_data.cookie);
8580 if (d1 < d2)
8581 return -1;
8583 return 1;
8586 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8588 void
8589 resort_sorted_fields (void *obj,
8590 void * ARG_UNUSED (orig_obj),
8591 gt_pointer_operator new_value,
8592 void *cookie)
8594 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8595 resort_data.new_value = new_value;
8596 resort_data.cookie = cookie;
8597 qsort (&sf->elts[0], sf->len, sizeof (tree),
8598 resort_field_decl_cmp);
8601 /* Subroutine of c_parse_error.
8602 Return the result of concatenating LHS and RHS. RHS is really
8603 a string literal, its first character is indicated by RHS_START and
8604 RHS_SIZE is its length (including the terminating NUL character).
8606 The caller is responsible for deleting the returned pointer. */
8608 static char *
8609 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
8611 const int lhs_size = strlen (lhs);
8612 char *result = XNEWVEC (char, lhs_size + rhs_size);
8613 strncpy (result, lhs, lhs_size);
8614 strncpy (result + lhs_size, rhs_start, rhs_size);
8615 return result;
8618 /* Issue the error given by GMSGID, indicating that it occurred before
8619 TOKEN, which had the associated VALUE. */
8621 void
8622 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
8623 tree value, unsigned char token_flags)
8625 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
8627 char *message = NULL;
8629 if (token_type == CPP_EOF)
8630 message = catenate_messages (gmsgid, " at end of input");
8631 else if (token_type == CPP_CHAR
8632 || token_type == CPP_WCHAR
8633 || token_type == CPP_CHAR16
8634 || token_type == CPP_CHAR32)
8636 unsigned int val = TREE_INT_CST_LOW (value);
8637 const char *prefix;
8639 switch (token_type)
8641 default:
8642 prefix = "";
8643 break;
8644 case CPP_WCHAR:
8645 prefix = "L";
8646 break;
8647 case CPP_CHAR16:
8648 prefix = "u";
8649 break;
8650 case CPP_CHAR32:
8651 prefix = "U";
8652 break;
8655 if (val <= UCHAR_MAX && ISGRAPH (val))
8656 message = catenate_messages (gmsgid, " before %s'%c'");
8657 else
8658 message = catenate_messages (gmsgid, " before %s'\\x%x'");
8660 error (message, prefix, val);
8661 free (message);
8662 message = NULL;
8664 else if (token_type == CPP_STRING
8665 || token_type == CPP_WSTRING
8666 || token_type == CPP_STRING16
8667 || token_type == CPP_STRING32
8668 || token_type == CPP_UTF8STRING)
8669 message = catenate_messages (gmsgid, " before string constant");
8670 else if (token_type == CPP_NUMBER)
8671 message = catenate_messages (gmsgid, " before numeric constant");
8672 else if (token_type == CPP_NAME)
8674 message = catenate_messages (gmsgid, " before %qE");
8675 error (message, value);
8676 free (message);
8677 message = NULL;
8679 else if (token_type == CPP_PRAGMA)
8680 message = catenate_messages (gmsgid, " before %<#pragma%>");
8681 else if (token_type == CPP_PRAGMA_EOL)
8682 message = catenate_messages (gmsgid, " before end of line");
8683 else if (token_type == CPP_DECLTYPE)
8684 message = catenate_messages (gmsgid, " before %<decltype%>");
8685 else if (token_type < N_TTYPES)
8687 message = catenate_messages (gmsgid, " before %qs token");
8688 error (message, cpp_type2name (token_type, token_flags));
8689 free (message);
8690 message = NULL;
8692 else
8693 error (gmsgid);
8695 if (message)
8697 error (message);
8698 free (message);
8700 #undef catenate_messages
8703 /* Mapping for cpp message reasons to the options that enable them. */
8705 struct reason_option_codes_t
8707 const int reason; /* cpplib message reason. */
8708 const int option_code; /* gcc option that controls this message. */
8711 static const struct reason_option_codes_t option_codes[] = {
8712 {CPP_W_DEPRECATED, OPT_Wdeprecated},
8713 {CPP_W_COMMENTS, OPT_Wcomment},
8714 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
8715 {CPP_W_MULTICHAR, OPT_Wmultichar},
8716 {CPP_W_TRADITIONAL, OPT_Wtraditional},
8717 {CPP_W_LONG_LONG, OPT_Wlong_long},
8718 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
8719 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
8720 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
8721 {CPP_W_UNDEF, OPT_Wundef},
8722 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
8723 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
8724 {CPP_W_NORMALIZE, OPT_Wnormalized_},
8725 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
8726 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
8727 {CPP_W_NONE, 0}
8730 /* Return the gcc option code associated with the reason for a cpp
8731 message, or 0 if none. */
8733 static int
8734 c_option_controlling_cpp_error (int reason)
8736 const struct reason_option_codes_t *entry;
8738 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
8740 if (entry->reason == reason)
8741 return entry->option_code;
8743 return 0;
8746 /* Callback from cpp_error for PFILE to print diagnostics from the
8747 preprocessor. The diagnostic is of type LEVEL, with REASON set
8748 to the reason code if LEVEL is represents a warning, at location
8749 LOCATION unless this is after lexing and the compiler's location
8750 should be used instead, with column number possibly overridden by
8751 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
8752 the arguments. Returns true if a diagnostic was emitted, false
8753 otherwise. */
8755 bool
8756 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
8757 location_t location, unsigned int column_override,
8758 const char *msg, va_list *ap)
8760 diagnostic_info diagnostic;
8761 diagnostic_t dlevel;
8762 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
8763 bool ret;
8765 switch (level)
8767 case CPP_DL_WARNING_SYSHDR:
8768 if (flag_no_output)
8769 return false;
8770 global_dc->dc_warn_system_headers = 1;
8771 /* Fall through. */
8772 case CPP_DL_WARNING:
8773 if (flag_no_output)
8774 return false;
8775 dlevel = DK_WARNING;
8776 break;
8777 case CPP_DL_PEDWARN:
8778 if (flag_no_output && !flag_pedantic_errors)
8779 return false;
8780 dlevel = DK_PEDWARN;
8781 break;
8782 case CPP_DL_ERROR:
8783 dlevel = DK_ERROR;
8784 break;
8785 case CPP_DL_ICE:
8786 dlevel = DK_ICE;
8787 break;
8788 case CPP_DL_NOTE:
8789 dlevel = DK_NOTE;
8790 break;
8791 case CPP_DL_FATAL:
8792 dlevel = DK_FATAL;
8793 break;
8794 default:
8795 gcc_unreachable ();
8797 if (done_lexing)
8798 location = input_location;
8799 diagnostic_set_info_translated (&diagnostic, msg, ap,
8800 location, dlevel);
8801 if (column_override)
8802 diagnostic_override_column (&diagnostic, column_override);
8803 diagnostic_override_option_index (&diagnostic,
8804 c_option_controlling_cpp_error (reason));
8805 ret = report_diagnostic (&diagnostic);
8806 if (level == CPP_DL_WARNING_SYSHDR)
8807 global_dc->dc_warn_system_headers = save_warn_system_headers;
8808 return ret;
8811 /* Convert a character from the host to the target execution character
8812 set. cpplib handles this, mostly. */
8814 HOST_WIDE_INT
8815 c_common_to_target_charset (HOST_WIDE_INT c)
8817 /* Character constants in GCC proper are sign-extended under -fsigned-char,
8818 zero-extended under -fno-signed-char. cpplib insists that characters
8819 and character constants are always unsigned. Hence we must convert
8820 back and forth. */
8821 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
8823 uc = cpp_host_to_exec_charset (parse_in, uc);
8825 if (flag_signed_char)
8826 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
8827 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
8828 else
8829 return uc;
8832 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
8833 references with an INDIRECT_REF of a constant at the bottom; much like the
8834 traditional rendering of offsetof as a macro. Return the folded result. */
8836 tree
8837 fold_offsetof_1 (tree expr)
8839 tree base, off, t;
8841 switch (TREE_CODE (expr))
8843 case ERROR_MARK:
8844 return expr;
8846 case VAR_DECL:
8847 error ("cannot apply %<offsetof%> to static data member %qD", expr);
8848 return error_mark_node;
8850 case CALL_EXPR:
8851 case TARGET_EXPR:
8852 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
8853 return error_mark_node;
8855 case NOP_EXPR:
8856 case INDIRECT_REF:
8857 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
8859 error ("cannot apply %<offsetof%> to a non constant address");
8860 return error_mark_node;
8862 return TREE_OPERAND (expr, 0);
8864 case COMPONENT_REF:
8865 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
8866 if (base == error_mark_node)
8867 return base;
8869 t = TREE_OPERAND (expr, 1);
8870 if (DECL_C_BIT_FIELD (t))
8872 error ("attempt to take address of bit-field structure "
8873 "member %qD", t);
8874 return error_mark_node;
8876 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
8877 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
8879 / BITS_PER_UNIT));
8880 break;
8882 case ARRAY_REF:
8883 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
8884 if (base == error_mark_node)
8885 return base;
8887 t = TREE_OPERAND (expr, 1);
8889 /* Check if the offset goes beyond the upper bound of the array. */
8890 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
8892 tree upbound = array_ref_up_bound (expr);
8893 if (upbound != NULL_TREE
8894 && TREE_CODE (upbound) == INTEGER_CST
8895 && !tree_int_cst_equal (upbound,
8896 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
8898 upbound = size_binop (PLUS_EXPR, upbound,
8899 build_int_cst (TREE_TYPE (upbound), 1));
8900 if (tree_int_cst_lt (upbound, t))
8902 tree v;
8904 for (v = TREE_OPERAND (expr, 0);
8905 TREE_CODE (v) == COMPONENT_REF;
8906 v = TREE_OPERAND (v, 0))
8907 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
8908 == RECORD_TYPE)
8910 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
8911 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
8912 if (TREE_CODE (fld_chain) == FIELD_DECL)
8913 break;
8915 if (fld_chain)
8916 break;
8918 /* Don't warn if the array might be considered a poor
8919 man's flexible array member with a very permissive
8920 definition thereof. */
8921 if (TREE_CODE (v) == ARRAY_REF
8922 || TREE_CODE (v) == COMPONENT_REF)
8923 warning (OPT_Warray_bounds,
8924 "index %E denotes an offset "
8925 "greater than size of %qT",
8926 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
8931 t = convert (sizetype, t);
8932 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
8933 break;
8935 case COMPOUND_EXPR:
8936 /* Handle static members of volatile structs. */
8937 t = TREE_OPERAND (expr, 1);
8938 gcc_assert (TREE_CODE (t) == VAR_DECL);
8939 return fold_offsetof_1 (t);
8941 default:
8942 gcc_unreachable ();
8945 return fold_build_pointer_plus (base, off);
8948 /* Likewise, but convert it to the return type of offsetof. */
8950 tree
8951 fold_offsetof (tree expr)
8953 return convert (size_type_node, fold_offsetof_1 (expr));
8956 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
8957 expression, because B will always be true. */
8959 void
8960 warn_for_omitted_condop (location_t location, tree cond)
8962 if (truth_value_p (TREE_CODE (cond)))
8963 warning_at (location, OPT_Wparentheses,
8964 "the omitted middle operand in ?: will always be %<true%>, "
8965 "suggest explicit middle operand");
8968 /* Give an error for storing into ARG, which is 'const'. USE indicates
8969 how ARG was being used. */
8971 void
8972 readonly_error (tree arg, enum lvalue_use use)
8974 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
8975 || use == lv_asm);
8976 /* Using this macro rather than (for example) arrays of messages
8977 ensures that all the format strings are checked at compile
8978 time. */
8979 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
8980 : (use == lv_increment ? (I) \
8981 : (use == lv_decrement ? (D) : (AS))))
8982 if (TREE_CODE (arg) == COMPONENT_REF)
8984 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
8985 error (READONLY_MSG (G_("assignment of member "
8986 "%qD in read-only object"),
8987 G_("increment of member "
8988 "%qD in read-only object"),
8989 G_("decrement of member "
8990 "%qD in read-only object"),
8991 G_("member %qD in read-only object "
8992 "used as %<asm%> output")),
8993 TREE_OPERAND (arg, 1));
8994 else
8995 error (READONLY_MSG (G_("assignment of read-only member %qD"),
8996 G_("increment of read-only member %qD"),
8997 G_("decrement of read-only member %qD"),
8998 G_("read-only member %qD used as %<asm%> output")),
8999 TREE_OPERAND (arg, 1));
9001 else if (TREE_CODE (arg) == VAR_DECL)
9002 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9003 G_("increment of read-only variable %qD"),
9004 G_("decrement of read-only variable %qD"),
9005 G_("read-only variable %qD used as %<asm%> output")),
9006 arg);
9007 else if (TREE_CODE (arg) == PARM_DECL)
9008 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9009 G_("increment of read-only parameter %qD"),
9010 G_("decrement of read-only parameter %qD"),
9011 G_("read-only parameter %qD use as %<asm%> output")),
9012 arg);
9013 else if (TREE_CODE (arg) == RESULT_DECL)
9015 gcc_assert (c_dialect_cxx ());
9016 error (READONLY_MSG (G_("assignment of "
9017 "read-only named return value %qD"),
9018 G_("increment of "
9019 "read-only named return value %qD"),
9020 G_("decrement of "
9021 "read-only named return value %qD"),
9022 G_("read-only named return value %qD "
9023 "used as %<asm%>output")),
9024 arg);
9026 else if (TREE_CODE (arg) == FUNCTION_DECL)
9027 error (READONLY_MSG (G_("assignment of function %qD"),
9028 G_("increment of function %qD"),
9029 G_("decrement of function %qD"),
9030 G_("function %qD used as %<asm%> output")),
9031 arg);
9032 else
9033 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9034 G_("increment of read-only location %qE"),
9035 G_("decrement of read-only location %qE"),
9036 G_("read-only location %qE used as %<asm%> output")),
9037 arg);
9040 /* Print an error message for an invalid lvalue. USE says
9041 how the lvalue is being used and so selects the error message. LOC
9042 is the location for the error. */
9044 void
9045 lvalue_error (location_t loc, enum lvalue_use use)
9047 switch (use)
9049 case lv_assign:
9050 error_at (loc, "lvalue required as left operand of assignment");
9051 break;
9052 case lv_increment:
9053 error_at (loc, "lvalue required as increment operand");
9054 break;
9055 case lv_decrement:
9056 error_at (loc, "lvalue required as decrement operand");
9057 break;
9058 case lv_addressof:
9059 error_at (loc, "lvalue required as unary %<&%> operand");
9060 break;
9061 case lv_asm:
9062 error_at (loc, "lvalue required in asm statement");
9063 break;
9064 default:
9065 gcc_unreachable ();
9069 /* Print an error message for an invalid indirection of type TYPE.
9070 ERRSTRING is the name of the operator for the indirection. */
9072 void
9073 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9075 switch (errstring)
9077 case RO_NULL:
9078 gcc_assert (c_dialect_cxx ());
9079 error_at (loc, "invalid type argument (have %qT)", type);
9080 break;
9081 case RO_ARRAY_INDEXING:
9082 error_at (loc,
9083 "invalid type argument of array indexing (have %qT)",
9084 type);
9085 break;
9086 case RO_UNARY_STAR:
9087 error_at (loc,
9088 "invalid type argument of unary %<*%> (have %qT)",
9089 type);
9090 break;
9091 case RO_ARROW:
9092 error_at (loc,
9093 "invalid type argument of %<->%> (have %qT)",
9094 type);
9095 break;
9096 case RO_IMPLICIT_CONVERSION:
9097 error_at (loc,
9098 "invalid type argument of implicit conversion (have %qT)",
9099 type);
9100 break;
9101 default:
9102 gcc_unreachable ();
9106 /* *PTYPE is an incomplete array. Complete it with a domain based on
9107 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9108 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9109 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9112 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9114 tree maxindex, type, main_type, elt, unqual_elt;
9115 int failure = 0, quals;
9116 hashval_t hashcode = 0;
9118 maxindex = size_zero_node;
9119 if (initial_value)
9121 if (TREE_CODE (initial_value) == STRING_CST)
9123 int eltsize
9124 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9125 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9127 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9129 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
9131 if (VEC_empty (constructor_elt, v))
9133 if (pedantic)
9134 failure = 3;
9135 maxindex = ssize_int (-1);
9137 else
9139 tree curindex;
9140 unsigned HOST_WIDE_INT cnt;
9141 constructor_elt *ce;
9142 bool fold_p = false;
9144 if (VEC_index (constructor_elt, v, 0)->index)
9145 maxindex = fold_convert_loc (input_location, sizetype,
9146 VEC_index (constructor_elt,
9147 v, 0)->index);
9148 curindex = maxindex;
9150 for (cnt = 1;
9151 VEC_iterate (constructor_elt, v, cnt, ce);
9152 cnt++)
9154 bool curfold_p = false;
9155 if (ce->index)
9156 curindex = ce->index, curfold_p = true;
9157 else
9159 if (fold_p)
9160 curindex = fold_convert (sizetype, curindex);
9161 curindex = size_binop (PLUS_EXPR, curindex,
9162 size_one_node);
9164 if (tree_int_cst_lt (maxindex, curindex))
9165 maxindex = curindex, fold_p = curfold_p;
9167 if (fold_p)
9168 maxindex = fold_convert (sizetype, maxindex);
9171 else
9173 /* Make an error message unless that happened already. */
9174 if (initial_value != error_mark_node)
9175 failure = 1;
9178 else
9180 failure = 2;
9181 if (!do_default)
9182 return failure;
9185 type = *ptype;
9186 elt = TREE_TYPE (type);
9187 quals = TYPE_QUALS (strip_array_types (elt));
9188 if (quals == 0)
9189 unqual_elt = elt;
9190 else
9191 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
9193 /* Using build_distinct_type_copy and modifying things afterward instead
9194 of using build_array_type to create a new type preserves all of the
9195 TYPE_LANG_FLAG_? bits that the front end may have set. */
9196 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9197 TREE_TYPE (main_type) = unqual_elt;
9198 TYPE_DOMAIN (main_type)
9199 = build_range_type (TREE_TYPE (maxindex),
9200 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
9201 layout_type (main_type);
9203 /* Make sure we have the canonical MAIN_TYPE. */
9204 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
9205 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
9206 hashcode);
9207 main_type = type_hash_canon (hashcode, main_type);
9209 /* Fix the canonical type. */
9210 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
9211 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
9212 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
9213 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
9214 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
9215 != TYPE_DOMAIN (main_type)))
9216 TYPE_CANONICAL (main_type)
9217 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
9218 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
9219 else
9220 TYPE_CANONICAL (main_type) = main_type;
9222 if (quals == 0)
9223 type = main_type;
9224 else
9225 type = c_build_qualified_type (main_type, quals);
9227 if (COMPLETE_TYPE_P (type)
9228 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9229 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9231 error ("size of array is too large");
9232 /* If we proceed with the array type as it is, we'll eventually
9233 crash in tree_low_cst(). */
9234 type = error_mark_node;
9237 *ptype = type;
9238 return failure;
9241 /* Like c_mark_addressable but don't check register qualifier. */
9242 void
9243 c_common_mark_addressable_vec (tree t)
9245 while (handled_component_p (t))
9246 t = TREE_OPERAND (t, 0);
9247 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9248 return;
9249 TREE_ADDRESSABLE (t) = 1;
9254 /* Used to help initialize the builtin-types.def table. When a type of
9255 the correct size doesn't exist, use error_mark_node instead of NULL.
9256 The later results in segfaults even when a decl using the type doesn't
9257 get invoked. */
9259 tree
9260 builtin_type_for_size (int size, bool unsignedp)
9262 tree type = lang_hooks.types.type_for_size (size, unsignedp);
9263 return type ? type : error_mark_node;
9266 /* A helper function for resolve_overloaded_builtin in resolving the
9267 overloaded __sync_ builtins. Returns a positive power of 2 if the
9268 first operand of PARAMS is a pointer to a supported data type.
9269 Returns 0 if an error is encountered. */
9271 static int
9272 sync_resolve_size (tree function, VEC(tree,gc) *params)
9274 tree type;
9275 int size;
9277 if (VEC_empty (tree, params))
9279 error ("too few arguments to function %qE", function);
9280 return 0;
9283 type = TREE_TYPE (VEC_index (tree, params, 0));
9284 if (TREE_CODE (type) != POINTER_TYPE)
9285 goto incompatible;
9287 type = TREE_TYPE (type);
9288 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9289 goto incompatible;
9291 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
9292 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
9293 return size;
9295 incompatible:
9296 error ("incompatible type for argument %d of %qE", 1, function);
9297 return 0;
9300 /* A helper function for resolve_overloaded_builtin. Adds casts to
9301 PARAMS to make arguments match up with those of FUNCTION. Drops
9302 the variadic arguments at the end. Returns false if some error
9303 was encountered; true on success. */
9305 static bool
9306 sync_resolve_params (location_t loc, tree orig_function, tree function,
9307 VEC(tree, gc) *params, bool orig_format)
9309 function_args_iterator iter;
9310 tree ptype;
9311 unsigned int parmnum;
9313 function_args_iter_init (&iter, TREE_TYPE (function));
9314 /* We've declared the implementation functions to use "volatile void *"
9315 as the pointer parameter, so we shouldn't get any complaints from the
9316 call to check_function_arguments what ever type the user used. */
9317 function_args_iter_next (&iter);
9318 ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
9320 /* For the rest of the values, we need to cast these to FTYPE, so that we
9321 don't get warnings for passing pointer types, etc. */
9322 parmnum = 0;
9323 while (1)
9325 tree val, arg_type;
9327 arg_type = function_args_iter_cond (&iter);
9328 /* XXX void_type_node belies the abstraction. */
9329 if (arg_type == void_type_node)
9330 break;
9332 ++parmnum;
9333 if (VEC_length (tree, params) <= parmnum)
9335 error_at (loc, "too few arguments to function %qE", orig_function);
9336 return false;
9339 /* Only convert parameters if arg_type is unsigned integer type with
9340 new format sync routines, i.e. don't attempt to convert pointer
9341 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
9342 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
9343 kinds). */
9344 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
9346 /* Ideally for the first conversion we'd use convert_for_assignment
9347 so that we get warnings for anything that doesn't match the pointer
9348 type. This isn't portable across the C and C++ front ends atm. */
9349 val = VEC_index (tree, params, parmnum);
9350 val = convert (ptype, val);
9351 val = convert (arg_type, val);
9352 VEC_replace (tree, params, parmnum, val);
9355 function_args_iter_next (&iter);
9358 /* __atomic routines are not variadic. */
9359 if (!orig_format && VEC_length (tree, params) != parmnum + 1)
9361 error_at (loc, "too many arguments to function %qE", orig_function);
9362 return false;
9365 /* The definition of these primitives is variadic, with the remaining
9366 being "an optional list of variables protected by the memory barrier".
9367 No clue what that's supposed to mean, precisely, but we consider all
9368 call-clobbered variables to be protected so we're safe. */
9369 VEC_truncate (tree, params, parmnum + 1);
9371 return true;
9374 /* A helper function for resolve_overloaded_builtin. Adds a cast to
9375 RESULT to make it match the type of the first pointer argument in
9376 PARAMS. */
9378 static tree
9379 sync_resolve_return (tree first_param, tree result, bool orig_format)
9381 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
9382 tree rtype = TREE_TYPE (result);
9383 ptype = TYPE_MAIN_VARIANT (ptype);
9385 /* New format doesn't require casting unless the types are the same size. */
9386 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
9387 return convert (ptype, result);
9388 else
9389 return result;
9392 /* This function verifies the PARAMS to generic atomic FUNCTION.
9393 It returns the size if all the parameters are the same size, otherwise
9394 0 is returned if the parameters are invalid. */
9396 static int
9397 get_atomic_generic_size (location_t loc, tree function, VEC(tree,gc) *params)
9399 unsigned int n_param;
9400 unsigned int n_model;
9401 unsigned int x;
9402 int size_0;
9403 tree type_0;
9405 /* Determine the parameter makeup. */
9406 switch (DECL_FUNCTION_CODE (function))
9408 case BUILT_IN_ATOMIC_EXCHANGE:
9409 n_param = 4;
9410 n_model = 1;
9411 break;
9412 case BUILT_IN_ATOMIC_LOAD:
9413 case BUILT_IN_ATOMIC_STORE:
9414 n_param = 3;
9415 n_model = 1;
9416 break;
9417 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9418 n_param = 6;
9419 n_model = 2;
9420 break;
9421 default:
9422 gcc_unreachable ();
9425 if (VEC_length (tree, params) != n_param)
9427 error_at (loc, "incorrect number of arguments to function %qE", function);
9428 return 0;
9431 /* Get type of first parameter, and determine its size. */
9432 type_0 = TREE_TYPE (VEC_index (tree, params, 0));
9433 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
9435 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
9436 function);
9437 return 0;
9440 /* Types must be compile time constant sizes. */
9441 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
9443 error_at (loc,
9444 "argument 1 of %qE must be a pointer to a constant size type",
9445 function);
9446 return 0;
9449 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
9451 /* Zero size objects are not allowed. */
9452 if (size_0 == 0)
9454 error_at (loc,
9455 "argument 1 of %qE must be a pointer to a nonzero size object",
9456 function);
9457 return 0;
9460 /* Check each other parameter is a pointer and the same size. */
9461 for (x = 0; x < n_param - n_model; x++)
9463 int size;
9464 tree type = TREE_TYPE (VEC_index (tree, params, x));
9465 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */
9466 if (n_param == 6 && x == 3)
9467 continue;
9468 if (!POINTER_TYPE_P (type))
9470 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
9471 function);
9472 return 0;
9474 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
9475 if (size != size_0)
9477 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
9478 function);
9479 return 0;
9483 /* Check memory model parameters for validity. */
9484 for (x = n_param - n_model ; x < n_param; x++)
9486 tree p = VEC_index (tree, params, x);
9487 if (TREE_CODE (p) == INTEGER_CST)
9489 int i = tree_low_cst (p, 1);
9490 if (i < 0 || i >= MEMMODEL_LAST)
9492 warning_at (loc, OPT_Winvalid_memory_model,
9493 "invalid memory model argument %d of %qE", x + 1,
9494 function);
9497 else
9498 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
9500 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
9501 function);
9502 return 0;
9506 return size_0;
9510 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
9511 at the beginning of the parameter list PARAMS representing the size of the
9512 objects. This is to match the library ABI requirement. LOC is the location
9513 of the function call.
9514 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
9515 returned to allow the external call to be constructed. */
9517 static tree
9518 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9519 VEC(tree,gc) *params)
9521 tree size_node;
9523 /* Insert a SIZE_T parameter as the first param. If there isn't
9524 enough space, allocate a new vector and recursively re-build with that. */
9525 if (!VEC_space (tree, params, 1))
9527 unsigned int z, len;
9528 VEC(tree,gc) *vec;
9529 tree f;
9531 len = VEC_length (tree, params);
9532 vec = VEC_alloc (tree, gc, len + 1);
9533 for (z = 0; z < len; z++)
9534 VEC_quick_push (tree, vec, VEC_index (tree, params, z));
9535 f = build_function_call_vec (loc, function, vec, NULL);
9536 VEC_free (tree, gc, vec);
9537 return f;
9540 /* Add the size parameter and leave as a function call for processing. */
9541 size_node = build_int_cst (size_type_node, n);
9542 VEC_quick_insert (tree, params, 0, size_node);
9543 return NULL_TREE;
9547 /* This will process an __atomic_exchange function call, determine whether it
9548 needs to be mapped to the _N variation, or turned into a library call.
9549 LOC is the location of the builtin call.
9550 FUNCTION is the DECL that has been invoked;
9551 PARAMS is the argument list for the call. The return value is non-null
9552 TRUE is returned if it is translated into the proper format for a call to the
9553 external library, and NEW_RETURN is set the tree for that function.
9554 FALSE is returned if processing for the _N variation is required, and
9555 NEW_RETURN is set to the the return value the result is copied into. */
9556 static bool
9557 resolve_overloaded_atomic_exchange (location_t loc, tree function,
9558 VEC(tree,gc) *params, tree *new_return)
9560 tree p0, p1, p2, p3;
9561 tree I_type, I_type_ptr;
9562 int n = get_atomic_generic_size (loc, function, params);
9564 /* Size of 0 is an error condition. */
9565 if (n == 0)
9567 *new_return = error_mark_node;
9568 return true;
9571 /* If not a lock-free size, change to the library generic format. */
9572 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9574 *new_return = add_atomic_size_parameter (n, loc, function, params);
9575 return true;
9578 /* Otherwise there is a lockfree match, transform the call from:
9579 void fn(T* mem, T* desired, T* return, model)
9580 into
9581 *return = (T) (fn (In* mem, (In) *desired, model)) */
9583 p0 = VEC_index (tree, params, 0);
9584 p1 = VEC_index (tree, params, 1);
9585 p2 = VEC_index (tree, params, 2);
9586 p3 = VEC_index (tree, params, 3);
9588 /* Create pointer to appropriate size. */
9589 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9590 I_type_ptr = build_pointer_type (I_type);
9592 /* Convert object pointer to required type. */
9593 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9594 VEC_replace (tree, params, 0, p0);
9595 /* Convert new value to required type, and dereference it. */
9596 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
9597 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9598 VEC_replace (tree, params, 1, p1);
9600 /* Move memory model to the 3rd position, and end param list. */
9601 VEC_replace (tree, params, 2, p3);
9602 VEC_truncate (tree, params, 3);
9604 /* Convert return pointer and dereference it for later assignment. */
9605 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
9607 return false;
9611 /* This will process an __atomic_compare_exchange function call, determine
9612 whether it needs to be mapped to the _N variation, or turned into a lib call.
9613 LOC is the location of the builtin call.
9614 FUNCTION is the DECL that has been invoked;
9615 PARAMS is the argument list for the call. The return value is non-null
9616 TRUE is returned if it is translated into the proper format for a call to the
9617 external library, and NEW_RETURN is set the tree for that function.
9618 FALSE is returned if processing for the _N variation is required. */
9620 static bool
9621 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9622 VEC(tree,gc) *params,
9623 tree *new_return)
9625 tree p0, p1, p2;
9626 tree I_type, I_type_ptr;
9627 int n = get_atomic_generic_size (loc, function, params);
9629 /* Size of 0 is an error condition. */
9630 if (n == 0)
9632 *new_return = error_mark_node;
9633 return true;
9636 /* If not a lock-free size, change to the library generic format. */
9637 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9639 /* The library generic format does not have the weak parameter, so
9640 remove it from the param list. Since a parameter has been removed,
9641 we can be sure that there is room for the SIZE_T parameter, meaning
9642 there will not be a recursive rebuilding of the parameter list, so
9643 there is no danger this will be done twice. */
9644 if (n > 0)
9646 VEC_replace (tree, params, 3, VEC_index (tree, params, 4));
9647 VEC_replace (tree, params, 4, VEC_index (tree, params, 5));
9648 VEC_truncate (tree, params, 5);
9650 *new_return = add_atomic_size_parameter (n, loc, function, params);
9651 return true;
9654 /* Otherwise, there is a match, so the call needs to be transformed from:
9655 bool fn(T* mem, T* desired, T* return, weak, success, failure)
9656 into
9657 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
9659 p0 = VEC_index (tree, params, 0);
9660 p1 = VEC_index (tree, params, 1);
9661 p2 = VEC_index (tree, params, 2);
9663 /* Create pointer to appropriate size. */
9664 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9665 I_type_ptr = build_pointer_type (I_type);
9667 /* Convert object pointer to required type. */
9668 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9669 VEC_replace (tree, params, 0, p0);
9671 /* Convert expected pointer to required type. */
9672 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9673 VEC_replace (tree, params, 1, p1);
9675 /* Convert desired value to required type, and dereference it. */
9676 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
9677 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9678 VEC_replace (tree, params, 2, p2);
9680 /* The rest of the parameters are fine. NULL means no special return value
9681 processing.*/
9682 *new_return = NULL;
9683 return false;
9687 /* This will process an __atomic_load function call, determine whether it
9688 needs to be mapped to the _N variation, or turned into a library call.
9689 LOC is the location of the builtin call.
9690 FUNCTION is the DECL that has been invoked;
9691 PARAMS is the argument list for the call. The return value is non-null
9692 TRUE is returned if it is translated into the proper format for a call to the
9693 external library, and NEW_RETURN is set the tree for that function.
9694 FALSE is returned if processing for the _N variation is required, and
9695 NEW_RETURN is set to the the return value the result is copied into. */
9697 static bool
9698 resolve_overloaded_atomic_load (location_t loc, tree function,
9699 VEC(tree,gc) *params, tree *new_return)
9701 tree p0, p1, p2;
9702 tree I_type, I_type_ptr;
9703 int n = get_atomic_generic_size (loc, function, params);
9705 /* Size of 0 is an error condition. */
9706 if (n == 0)
9708 *new_return = error_mark_node;
9709 return true;
9712 /* If not a lock-free size, change to the library generic format. */
9713 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9715 *new_return = add_atomic_size_parameter (n, loc, function, params);
9716 return true;
9719 /* Otherwise, there is a match, so the call needs to be transformed from:
9720 void fn(T* mem, T* return, model)
9721 into
9722 *return = (T) (fn ((In *) mem, model)) */
9724 p0 = VEC_index (tree, params, 0);
9725 p1 = VEC_index (tree, params, 1);
9726 p2 = VEC_index (tree, params, 2);
9728 /* Create pointer to appropriate size. */
9729 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9730 I_type_ptr = build_pointer_type (I_type);
9732 /* Convert object pointer to required type. */
9733 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9734 VEC_replace (tree, params, 0, p0);
9736 /* Move memory model to the 2nd position, and end param list. */
9737 VEC_replace (tree, params, 1, p2);
9738 VEC_truncate (tree, params, 2);
9740 /* Convert return pointer and dereference it for later assignment. */
9741 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
9743 return false;
9747 /* This will process an __atomic_store function call, determine whether it
9748 needs to be mapped to the _N variation, or turned into a library call.
9749 LOC is the location of the builtin call.
9750 FUNCTION is the DECL that has been invoked;
9751 PARAMS is the argument list for the call. The return value is non-null
9752 TRUE is returned if it is translated into the proper format for a call to the
9753 external library, and NEW_RETURN is set the tree for that function.
9754 FALSE is returned if processing for the _N variation is required, and
9755 NEW_RETURN is set to the the return value the result is copied into. */
9757 static bool
9758 resolve_overloaded_atomic_store (location_t loc, tree function,
9759 VEC(tree,gc) *params, tree *new_return)
9761 tree p0, p1;
9762 tree I_type, I_type_ptr;
9763 int n = get_atomic_generic_size (loc, function, params);
9765 /* Size of 0 is an error condition. */
9766 if (n == 0)
9768 *new_return = error_mark_node;
9769 return true;
9772 /* If not a lock-free size, change to the library generic format. */
9773 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9775 *new_return = add_atomic_size_parameter (n, loc, function, params);
9776 return true;
9779 /* Otherwise, there is a match, so the call needs to be transformed from:
9780 void fn(T* mem, T* value, model)
9781 into
9782 fn ((In *) mem, (In) *value, model) */
9784 p0 = VEC_index (tree, params, 0);
9785 p1 = VEC_index (tree, params, 1);
9787 /* Create pointer to appropriate size. */
9788 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9789 I_type_ptr = build_pointer_type (I_type);
9791 /* Convert object pointer to required type. */
9792 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9793 VEC_replace (tree, params, 0, p0);
9795 /* Convert new value to required type, and dereference it. */
9796 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
9797 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9798 VEC_replace (tree, params, 1, p1);
9800 /* The memory model is in the right spot already. Return is void. */
9801 *new_return = NULL_TREE;
9803 return false;
9807 /* Some builtin functions are placeholders for other expressions. This
9808 function should be called immediately after parsing the call expression
9809 before surrounding code has committed to the type of the expression.
9811 LOC is the location of the builtin call.
9813 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
9814 PARAMS is the argument list for the call. The return value is non-null
9815 when expansion is complete, and null if normal processing should
9816 continue. */
9818 tree
9819 resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params)
9821 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
9822 bool orig_format = true;
9823 tree new_return = NULL_TREE;
9825 switch (DECL_BUILT_IN_CLASS (function))
9827 case BUILT_IN_NORMAL:
9828 break;
9829 case BUILT_IN_MD:
9830 if (targetm.resolve_overloaded_builtin)
9831 return targetm.resolve_overloaded_builtin (loc, function, params);
9832 else
9833 return NULL_TREE;
9834 default:
9835 return NULL_TREE;
9838 /* Handle BUILT_IN_NORMAL here. */
9839 switch (orig_code)
9841 case BUILT_IN_ATOMIC_EXCHANGE:
9842 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9843 case BUILT_IN_ATOMIC_LOAD:
9844 case BUILT_IN_ATOMIC_STORE:
9846 /* Handle these 4 together so that they can fall through to the next
9847 case if the call is transformed to an _N variant. */
9848 switch (orig_code)
9850 case BUILT_IN_ATOMIC_EXCHANGE:
9852 if (resolve_overloaded_atomic_exchange (loc, function, params,
9853 &new_return))
9854 return new_return;
9855 /* Change to the _N variant. */
9856 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
9857 break;
9860 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9862 if (resolve_overloaded_atomic_compare_exchange (loc, function,
9863 params,
9864 &new_return))
9865 return new_return;
9866 /* Change to the _N variant. */
9867 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
9868 break;
9870 case BUILT_IN_ATOMIC_LOAD:
9872 if (resolve_overloaded_atomic_load (loc, function, params,
9873 &new_return))
9874 return new_return;
9875 /* Change to the _N variant. */
9876 orig_code = BUILT_IN_ATOMIC_LOAD_N;
9877 break;
9879 case BUILT_IN_ATOMIC_STORE:
9881 if (resolve_overloaded_atomic_store (loc, function, params,
9882 &new_return))
9883 return new_return;
9884 /* Change to the _N variant. */
9885 orig_code = BUILT_IN_ATOMIC_STORE_N;
9886 break;
9888 default:
9889 gcc_unreachable ();
9891 /* Fallthrough to the normal processing. */
9893 case BUILT_IN_ATOMIC_EXCHANGE_N:
9894 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
9895 case BUILT_IN_ATOMIC_LOAD_N:
9896 case BUILT_IN_ATOMIC_STORE_N:
9897 case BUILT_IN_ATOMIC_ADD_FETCH_N:
9898 case BUILT_IN_ATOMIC_SUB_FETCH_N:
9899 case BUILT_IN_ATOMIC_AND_FETCH_N:
9900 case BUILT_IN_ATOMIC_NAND_FETCH_N:
9901 case BUILT_IN_ATOMIC_XOR_FETCH_N:
9902 case BUILT_IN_ATOMIC_OR_FETCH_N:
9903 case BUILT_IN_ATOMIC_FETCH_ADD_N:
9904 case BUILT_IN_ATOMIC_FETCH_SUB_N:
9905 case BUILT_IN_ATOMIC_FETCH_AND_N:
9906 case BUILT_IN_ATOMIC_FETCH_NAND_N:
9907 case BUILT_IN_ATOMIC_FETCH_XOR_N:
9908 case BUILT_IN_ATOMIC_FETCH_OR_N:
9910 orig_format = false;
9911 /* Fallthru for parameter processing. */
9913 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
9914 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
9915 case BUILT_IN_SYNC_FETCH_AND_OR_N:
9916 case BUILT_IN_SYNC_FETCH_AND_AND_N:
9917 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
9918 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
9919 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
9920 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
9921 case BUILT_IN_SYNC_OR_AND_FETCH_N:
9922 case BUILT_IN_SYNC_AND_AND_FETCH_N:
9923 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
9924 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
9925 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
9926 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
9927 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
9928 case BUILT_IN_SYNC_LOCK_RELEASE_N:
9930 int n = sync_resolve_size (function, params);
9931 tree new_function, first_param, result;
9932 enum built_in_function fncode;
9934 if (n == 0)
9935 return error_mark_node;
9937 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
9938 new_function = builtin_decl_explicit (fncode);
9939 if (!sync_resolve_params (loc, function, new_function, params,
9940 orig_format))
9941 return error_mark_node;
9943 first_param = VEC_index (tree, params, 0);
9944 result = build_function_call_vec (loc, new_function, params, NULL);
9945 if (result == error_mark_node)
9946 return result;
9947 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
9948 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
9949 && orig_code != BUILT_IN_ATOMIC_STORE_N)
9950 result = sync_resolve_return (first_param, result, orig_format);
9952 /* If new_return is set, assign function to that expr and cast the
9953 result to void since the generic interface returned void. */
9954 if (new_return)
9956 /* Cast function result from I{1,2,4,8,16} to the required type. */
9957 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
9958 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
9959 result);
9960 TREE_SIDE_EFFECTS (result) = 1;
9961 protected_set_expr_location (result, loc);
9962 result = convert (void_type_node, result);
9964 return result;
9967 default:
9968 return NULL_TREE;
9972 /* Ignoring their sign, return true if two scalar types are the same. */
9973 bool
9974 same_scalar_type_ignoring_signedness (tree t1, tree t2)
9976 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
9978 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
9979 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
9980 || c2 == FIXED_POINT_TYPE));
9982 /* Equality works here because c_common_signed_type uses
9983 TYPE_MAIN_VARIANT. */
9984 return c_common_signed_type (t1)
9985 == c_common_signed_type (t2);
9988 /* Check for missing format attributes on function pointers. LTYPE is
9989 the new type or left-hand side type. RTYPE is the old type or
9990 right-hand side type. Returns TRUE if LTYPE is missing the desired
9991 attribute. */
9993 bool
9994 check_missing_format_attribute (tree ltype, tree rtype)
9996 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
9997 tree ra;
9999 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10000 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10001 break;
10002 if (ra)
10004 tree la;
10005 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10006 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10007 break;
10008 return !la;
10010 else
10011 return false;
10014 /* Subscripting with type char is likely to lose on a machine where
10015 chars are signed. So warn on any machine, but optionally. Don't
10016 warn for unsigned char since that type is safe. Don't warn for
10017 signed char because anyone who uses that must have done so
10018 deliberately. Furthermore, we reduce the false positive load by
10019 warning only for non-constant value of type char. */
10021 void
10022 warn_array_subscript_with_type_char (tree index)
10024 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10025 && TREE_CODE (index) != INTEGER_CST)
10026 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10029 /* Implement -Wparentheses for the unexpected C precedence rules, to
10030 cover cases like x + y << z which readers are likely to
10031 misinterpret. We have seen an expression in which CODE is a binary
10032 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10033 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10034 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10035 expression was not formed using a binary or unary operator, or it
10036 was enclosed in parentheses. */
10038 void
10039 warn_about_parentheses (enum tree_code code,
10040 enum tree_code code_left, tree arg_left,
10041 enum tree_code code_right, tree arg_right)
10043 if (!warn_parentheses)
10044 return;
10046 /* This macro tests that the expression ARG with original tree code
10047 CODE appears to be a boolean expression. or the result of folding a
10048 boolean expression. */
10049 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10050 (truth_value_p (TREE_CODE (ARG)) \
10051 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10052 /* Folding may create 0 or 1 integers from other expressions. */ \
10053 || ((CODE) != INTEGER_CST \
10054 && (integer_onep (ARG) || integer_zerop (ARG))))
10056 switch (code)
10058 case LSHIFT_EXPR:
10059 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10060 warning (OPT_Wparentheses,
10061 "suggest parentheses around %<+%> inside %<<<%>");
10062 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10063 warning (OPT_Wparentheses,
10064 "suggest parentheses around %<-%> inside %<<<%>");
10065 return;
10067 case RSHIFT_EXPR:
10068 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10069 warning (OPT_Wparentheses,
10070 "suggest parentheses around %<+%> inside %<>>%>");
10071 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10072 warning (OPT_Wparentheses,
10073 "suggest parentheses around %<-%> inside %<>>%>");
10074 return;
10076 case TRUTH_ORIF_EXPR:
10077 if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR)
10078 warning (OPT_Wparentheses,
10079 "suggest parentheses around %<&&%> within %<||%>");
10080 return;
10082 case BIT_IOR_EXPR:
10083 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
10084 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
10085 || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10086 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10087 warning (OPT_Wparentheses,
10088 "suggest parentheses around arithmetic in operand of %<|%>");
10089 /* Check cases like x|y==z */
10090 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10091 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10092 warning (OPT_Wparentheses,
10093 "suggest parentheses around comparison in operand of %<|%>");
10094 /* Check cases like !x | y */
10095 else if (code_left == TRUTH_NOT_EXPR
10096 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10097 warning (OPT_Wparentheses, "suggest parentheses around operand of "
10098 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
10099 return;
10101 case BIT_XOR_EXPR:
10102 if (code_left == BIT_AND_EXPR
10103 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
10104 || code_right == BIT_AND_EXPR
10105 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10106 warning (OPT_Wparentheses,
10107 "suggest parentheses around arithmetic in operand of %<^%>");
10108 /* Check cases like x^y==z */
10109 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10110 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10111 warning (OPT_Wparentheses,
10112 "suggest parentheses around comparison in operand of %<^%>");
10113 return;
10115 case BIT_AND_EXPR:
10116 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10117 warning (OPT_Wparentheses,
10118 "suggest parentheses around %<+%> in operand of %<&%>");
10119 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10120 warning (OPT_Wparentheses,
10121 "suggest parentheses around %<-%> in operand of %<&%>");
10122 /* Check cases like x&y==z */
10123 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10124 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10125 warning (OPT_Wparentheses,
10126 "suggest parentheses around comparison in operand of %<&%>");
10127 /* Check cases like !x & y */
10128 else if (code_left == TRUTH_NOT_EXPR
10129 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10130 warning (OPT_Wparentheses, "suggest parentheses around operand of "
10131 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
10132 return;
10134 case EQ_EXPR:
10135 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10136 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10137 warning (OPT_Wparentheses,
10138 "suggest parentheses around comparison in operand of %<==%>");
10139 return;
10140 case NE_EXPR:
10141 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10142 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10143 warning (OPT_Wparentheses,
10144 "suggest parentheses around comparison in operand of %<!=%>");
10145 return;
10147 default:
10148 if (TREE_CODE_CLASS (code) == tcc_comparison
10149 && ((TREE_CODE_CLASS (code_left) == tcc_comparison
10150 && code_left != NE_EXPR && code_left != EQ_EXPR
10151 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
10152 || (TREE_CODE_CLASS (code_right) == tcc_comparison
10153 && code_right != NE_EXPR && code_right != EQ_EXPR
10154 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
10155 warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
10156 "have their mathematical meaning");
10157 return;
10159 #undef NOT_A_BOOLEAN_EXPR_P
10162 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
10164 void
10165 warn_for_unused_label (tree label)
10167 if (!TREE_USED (label))
10169 if (DECL_INITIAL (label))
10170 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
10171 else
10172 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
10176 /* Warn for division by zero according to the value of DIVISOR. LOC
10177 is the location of the division operator. */
10179 void
10180 warn_for_div_by_zero (location_t loc, tree divisor)
10182 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
10183 about division by zero. Do not issue a warning if DIVISOR has a
10184 floating-point type, since we consider 0.0/0.0 a valid way of
10185 generating a NaN. */
10186 if (c_inhibit_evaluation_warnings == 0
10187 && (integer_zerop (divisor) || fixed_zerop (divisor)))
10188 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
10191 /* Subroutine of build_binary_op. Give warnings for comparisons
10192 between signed and unsigned quantities that may fail. Do the
10193 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
10194 so that casts will be considered, but default promotions won't
10197 LOCATION is the location of the comparison operator.
10199 The arguments of this function map directly to local variables
10200 of build_binary_op. */
10202 void
10203 warn_for_sign_compare (location_t location,
10204 tree orig_op0, tree orig_op1,
10205 tree op0, tree op1,
10206 tree result_type, enum tree_code resultcode)
10208 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
10209 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
10210 int unsignedp0, unsignedp1;
10212 /* In C++, check for comparison of different enum types. */
10213 if (c_dialect_cxx()
10214 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
10215 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
10216 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
10217 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
10219 warning_at (location,
10220 OPT_Wsign_compare, "comparison between types %qT and %qT",
10221 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
10224 /* Do not warn if the comparison is being done in a signed type,
10225 since the signed type will only be chosen if it can represent
10226 all the values of the unsigned type. */
10227 if (!TYPE_UNSIGNED (result_type))
10228 /* OK */;
10229 /* Do not warn if both operands are unsigned. */
10230 else if (op0_signed == op1_signed)
10231 /* OK */;
10232 else
10234 tree sop, uop, base_type;
10235 bool ovf;
10237 if (op0_signed)
10238 sop = orig_op0, uop = orig_op1;
10239 else
10240 sop = orig_op1, uop = orig_op0;
10242 STRIP_TYPE_NOPS (sop);
10243 STRIP_TYPE_NOPS (uop);
10244 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
10245 ? TREE_TYPE (result_type) : result_type);
10247 /* Do not warn if the signed quantity is an unsuffixed integer
10248 literal (or some static constant expression involving such
10249 literals or a conditional expression involving such literals)
10250 and it is non-negative. */
10251 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
10252 /* OK */;
10253 /* Do not warn if the comparison is an equality operation, the
10254 unsigned quantity is an integral constant, and it would fit
10255 in the result if the result were signed. */
10256 else if (TREE_CODE (uop) == INTEGER_CST
10257 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
10258 && int_fits_type_p (uop, c_common_signed_type (base_type)))
10259 /* OK */;
10260 /* In C, do not warn if the unsigned quantity is an enumeration
10261 constant and its maximum value would fit in the result if the
10262 result were signed. */
10263 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
10264 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
10265 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
10266 c_common_signed_type (base_type)))
10267 /* OK */;
10268 else
10269 warning_at (location,
10270 OPT_Wsign_compare,
10271 "comparison between signed and unsigned integer expressions");
10274 /* Warn if two unsigned values are being compared in a size larger
10275 than their original size, and one (and only one) is the result of
10276 a `~' operator. This comparison will always fail.
10278 Also warn if one operand is a constant, and the constant does not
10279 have all bits set that are set in the ~ operand when it is
10280 extended. */
10282 op0 = c_common_get_narrower (op0, &unsignedp0);
10283 op1 = c_common_get_narrower (op1, &unsignedp1);
10285 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
10286 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
10288 if (TREE_CODE (op0) == BIT_NOT_EXPR)
10289 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
10290 if (TREE_CODE (op1) == BIT_NOT_EXPR)
10291 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
10293 if (host_integerp (op0, 0) || host_integerp (op1, 0))
10295 tree primop;
10296 HOST_WIDE_INT constant, mask;
10297 int unsignedp;
10298 unsigned int bits;
10300 if (host_integerp (op0, 0))
10302 primop = op1;
10303 unsignedp = unsignedp1;
10304 constant = tree_low_cst (op0, 0);
10306 else
10308 primop = op0;
10309 unsignedp = unsignedp0;
10310 constant = tree_low_cst (op1, 0);
10313 bits = TYPE_PRECISION (TREE_TYPE (primop));
10314 if (bits < TYPE_PRECISION (result_type)
10315 && bits < HOST_BITS_PER_LONG && unsignedp)
10317 mask = (~ (HOST_WIDE_INT) 0) << bits;
10318 if ((mask & constant) != mask)
10320 if (constant == 0)
10321 warning (OPT_Wsign_compare,
10322 "promoted ~unsigned is always non-zero");
10323 else
10324 warning_at (location, OPT_Wsign_compare,
10325 "comparison of promoted ~unsigned with constant");
10329 else if (unsignedp0 && unsignedp1
10330 && (TYPE_PRECISION (TREE_TYPE (op0))
10331 < TYPE_PRECISION (result_type))
10332 && (TYPE_PRECISION (TREE_TYPE (op1))
10333 < TYPE_PRECISION (result_type)))
10334 warning_at (location, OPT_Wsign_compare,
10335 "comparison of promoted ~unsigned with unsigned");
10339 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
10340 type via c_common_type. If -Wdouble-promotion is in use, and the
10341 conditions for warning have been met, issue a warning. GMSGID is
10342 the warning message. It must have two %T specifiers for the type
10343 that was converted (generally "float") and the type to which it was
10344 converted (generally "double), respectively. LOC is the location
10345 to which the awrning should refer. */
10347 void
10348 do_warn_double_promotion (tree result_type, tree type1, tree type2,
10349 const char *gmsgid, location_t loc)
10351 tree source_type;
10353 if (!warn_double_promotion)
10354 return;
10355 /* If the conversion will not occur at run-time, there is no need to
10356 warn about it. */
10357 if (c_inhibit_evaluation_warnings)
10358 return;
10359 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
10360 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
10361 return;
10362 if (TYPE_MAIN_VARIANT (type1) == float_type_node
10363 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
10364 source_type = type1;
10365 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
10366 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
10367 source_type = type2;
10368 else
10369 return;
10370 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
10373 /* Setup a TYPE_DECL node as a typedef representation.
10375 X is a TYPE_DECL for a typedef statement. Create a brand new
10376 ..._TYPE node (which will be just a variant of the existing
10377 ..._TYPE node with identical properties) and then install X
10378 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
10380 The whole point here is to end up with a situation where each
10381 and every ..._TYPE node the compiler creates will be uniquely
10382 associated with AT MOST one node representing a typedef name.
10383 This way, even though the compiler substitutes corresponding
10384 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
10385 early on, later parts of the compiler can always do the reverse
10386 translation and get back the corresponding typedef name. For
10387 example, given:
10389 typedef struct S MY_TYPE;
10390 MY_TYPE object;
10392 Later parts of the compiler might only know that `object' was of
10393 type `struct S' if it were not for code just below. With this
10394 code however, later parts of the compiler see something like:
10396 struct S' == struct S
10397 typedef struct S' MY_TYPE;
10398 struct S' object;
10400 And they can then deduce (from the node for type struct S') that
10401 the original object declaration was:
10403 MY_TYPE object;
10405 Being able to do this is important for proper support of protoize,
10406 and also for generating precise symbolic debugging information
10407 which takes full account of the programmer's (typedef) vocabulary.
10409 Obviously, we don't want to generate a duplicate ..._TYPE node if
10410 the TYPE_DECL node that we are now processing really represents a
10411 standard built-in type. */
10413 void
10414 set_underlying_type (tree x)
10416 if (x == error_mark_node)
10417 return;
10418 if (DECL_IS_BUILTIN (x))
10420 if (TYPE_NAME (TREE_TYPE (x)) == 0)
10421 TYPE_NAME (TREE_TYPE (x)) = x;
10423 else if (TREE_TYPE (x) != error_mark_node
10424 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
10426 tree tt = TREE_TYPE (x);
10427 DECL_ORIGINAL_TYPE (x) = tt;
10428 tt = build_variant_type_copy (tt);
10429 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
10430 TYPE_NAME (tt) = x;
10431 TREE_USED (tt) = TREE_USED (x);
10432 TREE_TYPE (x) = tt;
10436 /* Record the types used by the current global variable declaration
10437 being parsed, so that we can decide later to emit their debug info.
10438 Those types are in types_used_by_cur_var_decl, and we are going to
10439 store them in the types_used_by_vars_hash hash table.
10440 DECL is the declaration of the global variable that has been parsed. */
10442 void
10443 record_types_used_by_current_var_decl (tree decl)
10445 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
10447 while (!VEC_empty (tree, types_used_by_cur_var_decl))
10449 tree type = VEC_pop (tree, types_used_by_cur_var_decl);
10450 types_used_by_var_decl_insert (type, decl);
10454 /* If DECL is a typedef that is declared in the current function,
10455 record it for the purpose of -Wunused-local-typedefs. */
10457 void
10458 record_locally_defined_typedef (tree decl)
10460 struct c_language_function *l;
10462 if (!warn_unused_local_typedefs
10463 || cfun == NULL
10464 /* if this is not a locally defined typedef then we are not
10465 interested. */
10466 || !is_typedef_decl (decl)
10467 || !decl_function_context (decl))
10468 return;
10470 l = (struct c_language_function *) cfun->language;
10471 VEC_safe_push (tree, gc, l->local_typedefs, decl);
10474 /* If T is a TYPE_DECL declared locally, mark it as used. */
10476 void
10477 maybe_record_typedef_use (tree t)
10479 if (!is_typedef_decl (t))
10480 return;
10482 TREE_USED (t) = true;
10485 /* Warn if there are some unused locally defined typedefs in the
10486 current function. */
10488 void
10489 maybe_warn_unused_local_typedefs (void)
10491 int i;
10492 tree decl;
10493 /* The number of times we have emitted -Wunused-local-typedefs
10494 warnings. If this is different from errorcount, that means some
10495 unrelated errors have been issued. In which case, we'll avoid
10496 emitting "unused-local-typedefs" warnings. */
10497 static int unused_local_typedefs_warn_count;
10498 struct c_language_function *l;
10500 if (cfun == NULL)
10501 return;
10503 if ((l = (struct c_language_function *) cfun->language) == NULL)
10504 return;
10506 if (warn_unused_local_typedefs
10507 && errorcount == unused_local_typedefs_warn_count)
10509 FOR_EACH_VEC_ELT (tree, l->local_typedefs, i, decl)
10510 if (!TREE_USED (decl))
10511 warning_at (DECL_SOURCE_LOCATION (decl),
10512 OPT_Wunused_local_typedefs,
10513 "typedef %qD locally defined but not used", decl);
10514 unused_local_typedefs_warn_count = errorcount;
10517 if (l->local_typedefs)
10519 VEC_free (tree, gc, l->local_typedefs);
10520 l->local_typedefs = NULL;
10524 /* The C and C++ parsers both use vectors to hold function arguments.
10525 For efficiency, we keep a cache of unused vectors. This is the
10526 cache. */
10528 typedef VEC(tree,gc)* tree_gc_vec;
10529 DEF_VEC_P(tree_gc_vec);
10530 DEF_VEC_ALLOC_P(tree_gc_vec,gc);
10531 static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache;
10533 /* Return a new vector from the cache. If the cache is empty,
10534 allocate a new vector. These vectors are GC'ed, so it is OK if the
10535 pointer is not released.. */
10537 VEC(tree,gc) *
10538 make_tree_vector (void)
10540 if (!VEC_empty (tree_gc_vec, tree_vector_cache))
10541 return VEC_pop (tree_gc_vec, tree_vector_cache);
10542 else
10544 /* Passing 0 to VEC_alloc returns NULL, and our callers require
10545 that we always return a non-NULL value. The vector code uses
10546 4 when growing a NULL vector, so we do too. */
10547 return VEC_alloc (tree, gc, 4);
10551 /* Release a vector of trees back to the cache. */
10553 void
10554 release_tree_vector (VEC(tree,gc) *vec)
10556 if (vec != NULL)
10558 VEC_truncate (tree, vec, 0);
10559 VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec);
10563 /* Get a new tree vector holding a single tree. */
10565 VEC(tree,gc) *
10566 make_tree_vector_single (tree t)
10568 VEC(tree,gc) *ret = make_tree_vector ();
10569 VEC_quick_push (tree, ret, t);
10570 return ret;
10573 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
10575 VEC(tree,gc) *
10576 make_tree_vector_from_list (tree list)
10578 VEC(tree,gc) *ret = make_tree_vector ();
10579 for (; list; list = TREE_CHAIN (list))
10580 VEC_safe_push (tree, gc, ret, TREE_VALUE (list));
10581 return ret;
10584 /* Get a new tree vector which is a copy of an existing one. */
10586 VEC(tree,gc) *
10587 make_tree_vector_copy (const VEC(tree,gc) *orig)
10589 VEC(tree,gc) *ret;
10590 unsigned int ix;
10591 tree t;
10593 ret = make_tree_vector ();
10594 VEC_reserve (tree, gc, ret, VEC_length (tree, orig));
10595 FOR_EACH_VEC_ELT (tree, orig, ix, t)
10596 VEC_quick_push (tree, ret, t);
10597 return ret;
10600 /* Return true if KEYWORD starts a type specifier. */
10602 bool
10603 keyword_begins_type_specifier (enum rid keyword)
10605 switch (keyword)
10607 case RID_INT:
10608 case RID_CHAR:
10609 case RID_FLOAT:
10610 case RID_DOUBLE:
10611 case RID_VOID:
10612 case RID_INT128:
10613 case RID_UNSIGNED:
10614 case RID_LONG:
10615 case RID_SHORT:
10616 case RID_SIGNED:
10617 case RID_DFLOAT32:
10618 case RID_DFLOAT64:
10619 case RID_DFLOAT128:
10620 case RID_FRACT:
10621 case RID_ACCUM:
10622 case RID_BOOL:
10623 case RID_WCHAR:
10624 case RID_CHAR16:
10625 case RID_CHAR32:
10626 case RID_SAT:
10627 case RID_COMPLEX:
10628 case RID_TYPEOF:
10629 case RID_STRUCT:
10630 case RID_CLASS:
10631 case RID_UNION:
10632 case RID_ENUM:
10633 return true;
10634 default:
10635 return false;
10639 /* Return true if KEYWORD names a type qualifier. */
10641 bool
10642 keyword_is_type_qualifier (enum rid keyword)
10644 switch (keyword)
10646 case RID_CONST:
10647 case RID_VOLATILE:
10648 case RID_RESTRICT:
10649 return true;
10650 default:
10651 return false;
10655 /* Return true if KEYWORD names a storage class specifier.
10657 RID_TYPEDEF is not included in this list despite `typedef' being
10658 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
10659 such for syntactic convenience only. */
10661 bool
10662 keyword_is_storage_class_specifier (enum rid keyword)
10664 switch (keyword)
10666 case RID_STATIC:
10667 case RID_EXTERN:
10668 case RID_REGISTER:
10669 case RID_AUTO:
10670 case RID_MUTABLE:
10671 case RID_THREAD:
10672 return true;
10673 default:
10674 return false;
10678 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
10680 static bool
10681 keyword_is_function_specifier (enum rid keyword)
10683 switch (keyword)
10685 case RID_INLINE:
10686 case RID_NORETURN:
10687 case RID_VIRTUAL:
10688 case RID_EXPLICIT:
10689 return true;
10690 default:
10691 return false;
10695 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
10696 declaration-specifier (C99 6.7). */
10698 bool
10699 keyword_is_decl_specifier (enum rid keyword)
10701 if (keyword_is_storage_class_specifier (keyword)
10702 || keyword_is_type_qualifier (keyword)
10703 || keyword_is_function_specifier (keyword))
10704 return true;
10706 switch (keyword)
10708 case RID_TYPEDEF:
10709 case RID_FRIEND:
10710 case RID_CONSTEXPR:
10711 return true;
10712 default:
10713 return false;
10717 /* Initialize language-specific-bits of tree_contains_struct. */
10719 void
10720 c_common_init_ts (void)
10722 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
10723 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
10726 /* Build a user-defined numeric literal out of an integer constant type VALUE
10727 with identifier SUFFIX. */
10729 tree
10730 build_userdef_literal (tree suffix_id, tree value, tree num_string)
10732 tree literal = make_node (USERDEF_LITERAL);
10733 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
10734 USERDEF_LITERAL_VALUE (literal) = value;
10735 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
10736 return literal;
10739 #include "gt-c-family-c-common.h"