Partial implementation of C++11 thread_local.
[official-gcc.git] / gcc / c-family / c-common.c
blobbb18c39cd6ba1ba8c62ba14f2429525939b0f244
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 "c-pragma.h"
30 #include "ggc.h"
31 #include "c-common.h"
32 #include "c-objc.h"
33 #include "tm_p.h"
34 #include "obstack.h"
35 #include "cpplib.h"
36 #include "target.h"
37 #include "common/common-target.h"
38 #include "langhooks.h"
39 #include "tree-inline.h"
40 #include "toplev.h"
41 #include "diagnostic.h"
42 #include "tree-iterator.h"
43 #include "hashtab.h"
44 #include "tree-mudflap.h"
45 #include "opts.h"
46 #include "cgraph.h"
47 #include "target-def.h"
49 cpp_reader *parse_in; /* Declared in c-pragma.h. */
51 /* The following symbols are subsumed in the c_global_trees array, and
52 listed here individually for documentation purposes.
54 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
56 tree short_integer_type_node;
57 tree long_integer_type_node;
58 tree long_long_integer_type_node;
59 tree int128_integer_type_node;
61 tree short_unsigned_type_node;
62 tree long_unsigned_type_node;
63 tree long_long_unsigned_type_node;
64 tree int128_unsigned_type_node;
66 tree truthvalue_type_node;
67 tree truthvalue_false_node;
68 tree truthvalue_true_node;
70 tree ptrdiff_type_node;
72 tree unsigned_char_type_node;
73 tree signed_char_type_node;
74 tree wchar_type_node;
76 tree char16_type_node;
77 tree char32_type_node;
79 tree float_type_node;
80 tree double_type_node;
81 tree long_double_type_node;
83 tree complex_integer_type_node;
84 tree complex_float_type_node;
85 tree complex_double_type_node;
86 tree complex_long_double_type_node;
88 tree dfloat32_type_node;
89 tree dfloat64_type_node;
90 tree_dfloat128_type_node;
92 tree intQI_type_node;
93 tree intHI_type_node;
94 tree intSI_type_node;
95 tree intDI_type_node;
96 tree intTI_type_node;
98 tree unsigned_intQI_type_node;
99 tree unsigned_intHI_type_node;
100 tree unsigned_intSI_type_node;
101 tree unsigned_intDI_type_node;
102 tree unsigned_intTI_type_node;
104 tree widest_integer_literal_type_node;
105 tree widest_unsigned_literal_type_node;
107 Nodes for types `void *' and `const void *'.
109 tree ptr_type_node, const_ptr_type_node;
111 Nodes for types `char *' and `const char *'.
113 tree string_type_node, const_string_type_node;
115 Type `char[SOMENUMBER]'.
116 Used when an array of char is needed and the size is irrelevant.
118 tree char_array_type_node;
120 Type `int[SOMENUMBER]' or something like it.
121 Used when an array of int needed and the size is irrelevant.
123 tree int_array_type_node;
125 Type `wchar_t[SOMENUMBER]' or something like it.
126 Used when a wide string literal is created.
128 tree wchar_array_type_node;
130 Type `char16_t[SOMENUMBER]' or something like it.
131 Used when a UTF-16 string literal is created.
133 tree char16_array_type_node;
135 Type `char32_t[SOMENUMBER]' or something like it.
136 Used when a UTF-32 string literal is created.
138 tree char32_array_type_node;
140 Type `int ()' -- used for implicit declaration of functions.
142 tree default_function_type;
144 A VOID_TYPE node, packaged in a TREE_LIST.
146 tree void_list_node;
148 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
149 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
150 VAR_DECLS, but C++ does.)
152 tree function_name_decl_node;
153 tree pretty_function_name_decl_node;
154 tree c99_function_name_decl_node;
156 Stack of nested function name VAR_DECLs.
158 tree saved_function_name_decls;
162 tree c_global_trees[CTI_MAX];
164 /* Switches common to the C front ends. */
166 /* Nonzero means don't output line number information. */
168 char flag_no_line_commands;
170 /* Nonzero causes -E output not to be done, but directives such as
171 #define that have side effects are still obeyed. */
173 char flag_no_output;
175 /* Nonzero means dump macros in some fashion. */
177 char flag_dump_macros;
179 /* Nonzero means pass #include lines through to the output. */
181 char flag_dump_includes;
183 /* Nonzero means process PCH files while preprocessing. */
185 bool flag_pch_preprocess;
187 /* The file name to which we should write a precompiled header, or
188 NULL if no header will be written in this compile. */
190 const char *pch_file;
192 /* Nonzero if an ISO standard was selected. It rejects macros in the
193 user's namespace. */
194 int flag_iso;
196 /* Warn about #pragma directives that are not recognized. */
198 int warn_unknown_pragmas; /* Tri state variable. */
200 /* Warn about format/argument anomalies in calls to formatted I/O functions
201 (*printf, *scanf, strftime, strfmon, etc.). */
203 int warn_format;
205 /* C/ObjC language option variables. */
208 /* Nonzero means allow type mismatches in conditional expressions;
209 just make their values `void'. */
211 int flag_cond_mismatch;
213 /* Nonzero means enable C89 Amendment 1 features. */
215 int flag_isoc94;
217 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
219 int flag_isoc99;
221 /* Nonzero means use the ISO C11 dialect of C. */
223 int flag_isoc11;
225 /* Nonzero means that we have builtin functions, and main is an int. */
227 int flag_hosted = 1;
230 /* ObjC language option variables. */
233 /* Tells the compiler that this is a special run. Do not perform any
234 compiling, instead we are to test some platform dependent features
235 and output a C header file with appropriate definitions. */
237 int print_struct_values;
239 /* Tells the compiler what is the constant string class for ObjC. */
241 const char *constant_string_class_name;
244 /* C++ language option variables. */
247 /* Nonzero means generate separate instantiation control files and
248 juggle them at link time. */
250 int flag_use_repository;
252 /* The C++ dialect being used. C++98 is the default. */
254 enum cxx_dialect cxx_dialect = cxx98;
256 /* Maximum template instantiation depth. This limit exists to limit the
257 time it takes to notice excessively recursive template instantiations.
259 The default is lower than the 1024 recommended by the C++0x standard
260 because G++ runs out of stack before 1024 with highly recursive template
261 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
263 int max_tinst_depth = 900;
265 /* The elements of `ridpointers' are identifier nodes for the reserved
266 type names and storage classes. It is indexed by a RID_... value. */
267 tree *ridpointers;
269 tree (*make_fname_decl) (location_t, tree, int);
271 /* Nonzero means don't warn about problems that occur when the code is
272 executed. */
273 int c_inhibit_evaluation_warnings;
275 /* Whether we are building a boolean conversion inside
276 convert_for_assignment, or some other late binary operation. If
277 build_binary_op is called for C (from code shared by C and C++) in
278 this case, then the operands have already been folded and the
279 result will not be folded again, so C_MAYBE_CONST_EXPR should not
280 be generated. */
281 bool in_late_binary_op;
283 /* Whether lexing has been completed, so subsequent preprocessor
284 errors should use the compiler's input_location. */
285 bool done_lexing = false;
287 /* Information about how a function name is generated. */
288 struct fname_var_t
290 tree *const decl; /* pointer to the VAR_DECL. */
291 const unsigned rid; /* RID number for the identifier. */
292 const int pretty; /* How pretty is it? */
295 /* The three ways of getting then name of the current function. */
297 const struct fname_var_t fname_vars[] =
299 /* C99 compliant __func__, must be first. */
300 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
301 /* GCC __FUNCTION__ compliant. */
302 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
303 /* GCC __PRETTY_FUNCTION__ compliant. */
304 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
305 {NULL, 0, 0},
308 /* Global visibility options. */
309 struct visibility_flags visibility_options;
311 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
312 static tree check_case_value (tree);
313 static bool check_case_bounds (tree, tree, tree *, tree *);
315 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_always_inline_attribute (tree *, tree, tree, int,
325 bool *);
326 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
333 bool *);
334 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_transparent_union_attribute (tree *, tree, tree,
336 int, bool *);
337 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
343 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
344 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
347 static tree handle_visibility_attribute (tree *, tree, tree, int,
348 bool *);
349 static tree handle_tls_model_attribute (tree *, tree, tree, int,
350 bool *);
351 static tree handle_no_instrument_function_attribute (tree *, tree,
352 tree, int, bool *);
353 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
356 bool *);
357 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_deprecated_attribute (tree *, tree, tree, int,
362 bool *);
363 static tree handle_vector_size_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
369 bool *);
370 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
375 static tree ignore_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
379 static void check_function_nonnull (tree, int, tree *);
380 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
381 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
382 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
383 static int resort_field_decl_cmp (const void *, const void *);
385 /* Reserved words. The third field is a mask: keywords are disabled
386 if they match the mask.
388 Masks for languages:
389 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
390 C --std=c99: D_CXXONLY | D_OBJC
391 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
392 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
393 C++ --std=c0x: D_CONLY | D_OBJC
394 ObjC++ is like C++ except that D_OBJC is not set
396 If -fno-asm is used, D_ASM is added to the mask. If
397 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
398 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
399 In C with -Wc++-compat, we warn if D_CXXWARN is set.
401 Note the complication of the D_CXX_OBJC keywords. These are
402 reserved words such as 'class'. In C++, 'class' is a reserved
403 word. In Objective-C++ it is too. In Objective-C, it is a
404 reserved word too, but only if it follows an '@' sign.
406 const struct c_common_resword c_common_reswords[] =
408 { "_Alignas", RID_ALIGNAS, D_CONLY },
409 { "_Alignof", RID_ALIGNOF, D_CONLY },
410 { "_Bool", RID_BOOL, D_CONLY },
411 { "_Complex", RID_COMPLEX, 0 },
412 { "_Imaginary", RID_IMAGINARY, D_CONLY },
413 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
414 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
415 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
416 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
417 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
418 { "_Sat", RID_SAT, D_CONLY | D_EXT },
419 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
420 { "_Noreturn", RID_NORETURN, D_CONLY },
421 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
422 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
423 { "__alignof", RID_ALIGNOF, 0 },
424 { "__alignof__", RID_ALIGNOF, 0 },
425 { "__asm", RID_ASM, 0 },
426 { "__asm__", RID_ASM, 0 },
427 { "__attribute", RID_ATTRIBUTE, 0 },
428 { "__attribute__", RID_ATTRIBUTE, 0 },
429 { "__bases", RID_BASES, D_CXXONLY },
430 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
431 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
432 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
433 { "__builtin_offsetof", RID_OFFSETOF, 0 },
434 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
435 { "__builtin_va_arg", RID_VA_ARG, 0 },
436 { "__complex", RID_COMPLEX, 0 },
437 { "__complex__", RID_COMPLEX, 0 },
438 { "__const", RID_CONST, 0 },
439 { "__const__", RID_CONST, 0 },
440 { "__decltype", RID_DECLTYPE, D_CXXONLY },
441 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
442 { "__extension__", RID_EXTENSION, 0 },
443 { "__func__", RID_C99_FUNCTION_NAME, 0 },
444 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
445 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
446 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
447 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
448 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
449 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
450 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
451 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
452 { "__imag", RID_IMAGPART, 0 },
453 { "__imag__", RID_IMAGPART, 0 },
454 { "__inline", RID_INLINE, 0 },
455 { "__inline__", RID_INLINE, 0 },
456 { "__int128", RID_INT128, 0 },
457 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
458 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
459 { "__is_class", RID_IS_CLASS, D_CXXONLY },
460 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
461 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
462 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
463 { "__is_final", RID_IS_FINAL, D_CXXONLY },
464 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
465 { "__is_pod", RID_IS_POD, D_CXXONLY },
466 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
467 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
468 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
469 { "__is_union", RID_IS_UNION, D_CXXONLY },
470 { "__label__", RID_LABEL, 0 },
471 { "__null", RID_NULL, 0 },
472 { "__real", RID_REALPART, 0 },
473 { "__real__", RID_REALPART, 0 },
474 { "__restrict", RID_RESTRICT, 0 },
475 { "__restrict__", RID_RESTRICT, 0 },
476 { "__signed", RID_SIGNED, 0 },
477 { "__signed__", RID_SIGNED, 0 },
478 { "__thread", RID_THREAD, 0 },
479 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
480 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
481 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
482 { "__typeof", RID_TYPEOF, 0 },
483 { "__typeof__", RID_TYPEOF, 0 },
484 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
485 { "__volatile", RID_VOLATILE, 0 },
486 { "__volatile__", RID_VOLATILE, 0 },
487 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
488 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
489 { "asm", RID_ASM, D_ASM },
490 { "auto", RID_AUTO, 0 },
491 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
492 { "break", RID_BREAK, 0 },
493 { "case", RID_CASE, 0 },
494 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
495 { "char", RID_CHAR, 0 },
496 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
497 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
498 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
499 { "const", RID_CONST, 0 },
500 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
501 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
502 { "continue", RID_CONTINUE, 0 },
503 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
504 { "default", RID_DEFAULT, 0 },
505 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
506 { "do", RID_DO, 0 },
507 { "double", RID_DOUBLE, 0 },
508 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
509 { "else", RID_ELSE, 0 },
510 { "enum", RID_ENUM, 0 },
511 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
512 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
513 { "extern", RID_EXTERN, 0 },
514 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
515 { "float", RID_FLOAT, 0 },
516 { "for", RID_FOR, 0 },
517 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
518 { "goto", RID_GOTO, 0 },
519 { "if", RID_IF, 0 },
520 { "inline", RID_INLINE, D_EXT89 },
521 { "int", RID_INT, 0 },
522 { "long", RID_LONG, 0 },
523 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
524 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
525 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
526 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
527 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
528 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
529 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
530 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
531 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
532 { "register", RID_REGISTER, 0 },
533 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
534 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
535 { "return", RID_RETURN, 0 },
536 { "short", RID_SHORT, 0 },
537 { "signed", RID_SIGNED, 0 },
538 { "sizeof", RID_SIZEOF, 0 },
539 { "static", RID_STATIC, 0 },
540 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
541 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
542 { "struct", RID_STRUCT, 0 },
543 { "switch", RID_SWITCH, 0 },
544 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
545 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
546 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | 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;
1616 /* We first test whether either side separately is trivially true
1617 (with OR) or trivially false (with AND). If so, do not warn.
1618 This is a common idiom for testing ranges of data types in
1619 portable code. */
1620 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1621 if (!lhs)
1622 return;
1623 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1624 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1626 /* If this is an OR operation, invert both sides; now, the result
1627 should be always false to get a warning. */
1628 if (or_op)
1629 in0_p = !in0_p;
1631 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1632 if (tem && integer_zerop (tem))
1633 return;
1635 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1636 if (!rhs)
1637 return;
1638 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1639 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1641 /* If this is an OR operation, invert both sides; now, the result
1642 should be always false to get a warning. */
1643 if (or_op)
1644 in1_p = !in1_p;
1646 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1647 if (tem && integer_zerop (tem))
1648 return;
1650 /* If both expressions have the same operand, if we can merge the
1651 ranges, and if the range test is always false, then warn. */
1652 if (operand_equal_p (lhs, rhs, 0)
1653 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1654 in1_p, low1, high1)
1655 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1656 type, lhs, in_p, low, high))
1657 && integer_zerop (tem))
1659 if (or_op)
1660 warning_at (location, OPT_Wlogical_op,
1661 "logical %<or%> "
1662 "of collectively exhaustive tests is always true");
1663 else
1664 warning_at (location, OPT_Wlogical_op,
1665 "logical %<and%> "
1666 "of mutually exclusive tests is always false");
1671 /* Warn if EXP contains any computations whose results are not used.
1672 Return true if a warning is printed; false otherwise. LOCUS is the
1673 (potential) location of the expression. */
1675 bool
1676 warn_if_unused_value (const_tree exp, location_t locus)
1678 restart:
1679 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1680 return false;
1682 /* Don't warn about void constructs. This includes casting to void,
1683 void function calls, and statement expressions with a final cast
1684 to void. */
1685 if (VOID_TYPE_P (TREE_TYPE (exp)))
1686 return false;
1688 if (EXPR_HAS_LOCATION (exp))
1689 locus = EXPR_LOCATION (exp);
1691 switch (TREE_CODE (exp))
1693 case PREINCREMENT_EXPR:
1694 case POSTINCREMENT_EXPR:
1695 case PREDECREMENT_EXPR:
1696 case POSTDECREMENT_EXPR:
1697 case MODIFY_EXPR:
1698 case INIT_EXPR:
1699 case TARGET_EXPR:
1700 case CALL_EXPR:
1701 case TRY_CATCH_EXPR:
1702 case WITH_CLEANUP_EXPR:
1703 case EXIT_EXPR:
1704 case VA_ARG_EXPR:
1705 return false;
1707 case BIND_EXPR:
1708 /* For a binding, warn if no side effect within it. */
1709 exp = BIND_EXPR_BODY (exp);
1710 goto restart;
1712 case SAVE_EXPR:
1713 case NON_LVALUE_EXPR:
1714 case NOP_EXPR:
1715 exp = TREE_OPERAND (exp, 0);
1716 goto restart;
1718 case TRUTH_ORIF_EXPR:
1719 case TRUTH_ANDIF_EXPR:
1720 /* In && or ||, warn if 2nd operand has no side effect. */
1721 exp = TREE_OPERAND (exp, 1);
1722 goto restart;
1724 case COMPOUND_EXPR:
1725 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1726 return true;
1727 /* Let people do `(foo (), 0)' without a warning. */
1728 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1729 return false;
1730 exp = TREE_OPERAND (exp, 1);
1731 goto restart;
1733 case COND_EXPR:
1734 /* If this is an expression with side effects, don't warn; this
1735 case commonly appears in macro expansions. */
1736 if (TREE_SIDE_EFFECTS (exp))
1737 return false;
1738 goto warn;
1740 case INDIRECT_REF:
1741 /* Don't warn about automatic dereferencing of references, since
1742 the user cannot control it. */
1743 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1745 exp = TREE_OPERAND (exp, 0);
1746 goto restart;
1748 /* Fall through. */
1750 default:
1751 /* Referencing a volatile value is a side effect, so don't warn. */
1752 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1753 && TREE_THIS_VOLATILE (exp))
1754 return false;
1756 /* If this is an expression which has no operands, there is no value
1757 to be unused. There are no such language-independent codes,
1758 but front ends may define such. */
1759 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1760 return false;
1762 warn:
1763 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1768 /* Print a warning about casts that might indicate violation
1769 of strict aliasing rules if -Wstrict-aliasing is used and
1770 strict aliasing mode is in effect. OTYPE is the original
1771 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1773 bool
1774 strict_aliasing_warning (tree otype, tree type, tree expr)
1776 /* Strip pointer conversion chains and get to the correct original type. */
1777 STRIP_NOPS (expr);
1778 otype = TREE_TYPE (expr);
1780 if (!(flag_strict_aliasing
1781 && POINTER_TYPE_P (type)
1782 && POINTER_TYPE_P (otype)
1783 && !VOID_TYPE_P (TREE_TYPE (type)))
1784 /* If the type we are casting to is a ref-all pointer
1785 dereferencing it is always valid. */
1786 || TYPE_REF_CAN_ALIAS_ALL (type))
1787 return false;
1789 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1790 && (DECL_P (TREE_OPERAND (expr, 0))
1791 || handled_component_p (TREE_OPERAND (expr, 0))))
1793 /* Casting the address of an object to non void pointer. Warn
1794 if the cast breaks type based aliasing. */
1795 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1797 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1798 "might break strict-aliasing rules");
1799 return true;
1801 else
1803 /* warn_strict_aliasing >= 3. This includes the default (3).
1804 Only warn if the cast is dereferenced immediately. */
1805 alias_set_type set1 =
1806 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1807 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1809 if (set1 != set2 && set2 != 0
1810 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1812 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1813 "pointer will break strict-aliasing rules");
1814 return true;
1816 else if (warn_strict_aliasing == 2
1817 && !alias_sets_must_conflict_p (set1, set2))
1819 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1820 "pointer might break strict-aliasing rules");
1821 return true;
1825 else
1826 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1828 /* At this level, warn for any conversions, even if an address is
1829 not taken in the same statement. This will likely produce many
1830 false positives, but could be useful to pinpoint problems that
1831 are not revealed at higher levels. */
1832 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1833 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1834 if (!COMPLETE_TYPE_P (type)
1835 || !alias_sets_must_conflict_p (set1, set2))
1837 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1838 "pointer might break strict-aliasing rules");
1839 return true;
1843 return false;
1846 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1847 sizeof as last operand of certain builtins. */
1849 void
1850 sizeof_pointer_memaccess_warning (location_t loc, tree callee,
1851 VEC(tree, gc) *params, tree sizeof_arg,
1852 bool (*comp_types) (tree, tree))
1854 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1855 bool strop = false;
1857 if (TREE_CODE (callee) != FUNCTION_DECL
1858 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1859 || sizeof_arg == error_mark_node
1860 || VEC_length (tree, params) <= 1)
1861 return;
1863 type = TYPE_P (sizeof_arg) ? sizeof_arg : TREE_TYPE (sizeof_arg);
1864 if (!POINTER_TYPE_P (type))
1865 return;
1867 switch (DECL_FUNCTION_CODE (callee))
1869 case BUILT_IN_STRNCMP:
1870 case BUILT_IN_STRNCASECMP:
1871 case BUILT_IN_STRNCPY:
1872 case BUILT_IN_STRNCAT:
1873 strop = true;
1874 /* FALLTHRU */
1875 case BUILT_IN_MEMCPY:
1876 case BUILT_IN_MEMMOVE:
1877 case BUILT_IN_MEMCMP:
1878 if (VEC_length (tree, params) < 3)
1879 return;
1880 src = VEC_index (tree, params, 1);
1881 dest = VEC_index (tree, params, 0);
1882 break;
1883 case BUILT_IN_MEMSET:
1884 if (VEC_length (tree, params) < 3)
1885 return;
1886 dest = VEC_index (tree, params, 0);
1887 break;
1888 case BUILT_IN_STRNDUP:
1889 src = VEC_index (tree, params, 0);
1890 strop = true;
1891 break;
1892 default:
1893 break;
1896 if (dest
1897 && (tem = tree_strip_nop_conversions (dest))
1898 && POINTER_TYPE_P (TREE_TYPE (tem))
1899 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1900 return;
1902 if (src
1903 && (tem = tree_strip_nop_conversions (src))
1904 && POINTER_TYPE_P (TREE_TYPE (tem))
1905 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1906 return;
1908 if (dest)
1910 if (!TYPE_P (sizeof_arg)
1911 && operand_equal_p (dest, sizeof_arg, 0)
1912 && comp_types (TREE_TYPE (dest), type))
1914 if (TREE_CODE (sizeof_arg) == ADDR_EXPR && !strop)
1915 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1916 "argument to %<sizeof%> in %qD call is the same "
1917 "expression as the destination; did you mean to "
1918 "remove the addressof?", callee);
1919 else if ((TYPE_PRECISION (TREE_TYPE (type))
1920 == TYPE_PRECISION (char_type_node))
1921 || strop)
1922 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1923 "argument to %<sizeof%> in %qD call is the same "
1924 "expression as the destination; did you mean to "
1925 "provide an explicit length?", callee);
1926 else
1927 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1928 "argument to %<sizeof%> in %qD call is the same "
1929 "expression as the destination; did you mean to "
1930 "dereference it?", callee);
1931 return;
1934 if (POINTER_TYPE_P (TREE_TYPE (dest))
1935 && !strop
1936 && comp_types (TREE_TYPE (dest), type)
1937 && !VOID_TYPE_P (TREE_TYPE (type)))
1939 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1940 "argument to %<sizeof%> in %qD call is the same "
1941 "pointer type %qT as the destination; expected %qT "
1942 "or an explicit length", callee, TREE_TYPE (dest),
1943 TREE_TYPE (TREE_TYPE (dest)));
1944 return;
1948 if (src)
1950 if (!TYPE_P (sizeof_arg)
1951 && operand_equal_p (src, sizeof_arg, 0)
1952 && comp_types (TREE_TYPE (src), type))
1954 if (TREE_CODE (sizeof_arg) == ADDR_EXPR && !strop)
1955 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1956 "argument to %<sizeof%> in %qD call is the same "
1957 "expression as the source; did you mean to "
1958 "remove the addressof?", callee);
1959 else if ((TYPE_PRECISION (TREE_TYPE (type))
1960 == TYPE_PRECISION (char_type_node))
1961 || strop)
1962 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1963 "argument to %<sizeof%> in %qD call is the same "
1964 "expression as the source; did you mean to "
1965 "provide an explicit length?", callee);
1966 else
1967 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1968 "argument to %<sizeof%> in %qD call is the same "
1969 "expression as the source; did you mean to "
1970 "dereference it?", callee);
1971 return;
1974 if (POINTER_TYPE_P (TREE_TYPE (src))
1975 && !strop
1976 && comp_types (TREE_TYPE (src), type)
1977 && !VOID_TYPE_P (TREE_TYPE (type)))
1979 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1980 "argument to %<sizeof%> in %qD call is the same "
1981 "pointer type %qT as the source; expected %qT "
1982 "or an explicit length", callee, TREE_TYPE (src),
1983 TREE_TYPE (TREE_TYPE (src)));
1984 return;
1989 /* Warn for unlikely, improbable, or stupid DECL declarations
1990 of `main'. */
1992 void
1993 check_main_parameter_types (tree decl)
1995 function_args_iterator iter;
1996 tree type;
1997 int argct = 0;
1999 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2001 /* XXX void_type_node belies the abstraction. */
2002 if (type == void_type_node || type == error_mark_node )
2003 break;
2005 ++argct;
2006 switch (argct)
2008 case 1:
2009 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2010 pedwarn (input_location, OPT_Wmain,
2011 "first argument of %q+D should be %<int%>", decl);
2012 break;
2014 case 2:
2015 if (TREE_CODE (type) != POINTER_TYPE
2016 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2017 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2018 != char_type_node))
2019 pedwarn (input_location, OPT_Wmain,
2020 "second argument of %q+D should be %<char **%>", decl);
2021 break;
2023 case 3:
2024 if (TREE_CODE (type) != POINTER_TYPE
2025 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2026 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2027 != char_type_node))
2028 pedwarn (input_location, OPT_Wmain,
2029 "third argument of %q+D should probably be "
2030 "%<char **%>", decl);
2031 break;
2035 /* It is intentional that this message does not mention the third
2036 argument because it's only mentioned in an appendix of the
2037 standard. */
2038 if (argct > 0 && (argct < 2 || argct > 3))
2039 pedwarn (input_location, OPT_Wmain,
2040 "%q+D takes only zero or two arguments", decl);
2043 /* True if pointers to distinct types T1 and T2 can be converted to
2044 each other without an explicit cast. Only returns true for opaque
2045 vector types. */
2046 bool
2047 vector_targets_convertible_p (const_tree t1, const_tree t2)
2049 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2050 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2051 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2052 return true;
2054 return false;
2057 /* True if vector types T1 and T2 can be converted to each other
2058 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2059 can only be converted with -flax-vector-conversions yet that is not
2060 in effect, emit a note telling the user about that option if such
2061 a note has not previously been emitted. */
2062 bool
2063 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2065 static bool emitted_lax_note = false;
2066 bool convertible_lax;
2068 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2069 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2070 return true;
2072 convertible_lax =
2073 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2074 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2075 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
2076 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2077 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2079 if (!convertible_lax || flag_lax_vector_conversions)
2080 return convertible_lax;
2082 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2083 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2084 return true;
2086 if (emit_lax_note && !emitted_lax_note)
2088 emitted_lax_note = true;
2089 inform (input_location, "use -flax-vector-conversions to permit "
2090 "conversions between vectors with differing "
2091 "element types or numbers of subparts");
2094 return false;
2097 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2098 and have vector types, V0 has the same type as V1, and the number of
2099 elements of V0, V1, MASK is the same.
2101 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2102 called with two arguments. In this case implementation passes the
2103 first argument twice in order to share the same tree code. This fact
2104 could enable the mask-values being twice the vector length. This is
2105 an implementation accident and this semantics is not guaranteed to
2106 the user. */
2107 tree
2108 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2110 tree ret;
2111 bool wrap = true;
2112 bool maybe_const = false;
2113 bool two_arguments = false;
2115 if (v1 == NULL_TREE)
2117 two_arguments = true;
2118 v1 = v0;
2121 if (v0 == error_mark_node || v1 == error_mark_node
2122 || mask == error_mark_node)
2123 return error_mark_node;
2125 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2126 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2128 error_at (loc, "__builtin_shuffle last argument must "
2129 "be an integer vector");
2130 return error_mark_node;
2133 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2134 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2136 error_at (loc, "__builtin_shuffle arguments must be vectors");
2137 return error_mark_node;
2140 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2142 error_at (loc, "__builtin_shuffle argument vectors must be of "
2143 "the same type");
2144 return error_mark_node;
2147 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2148 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2149 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2150 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2152 error_at (loc, "__builtin_shuffle number of elements of the "
2153 "argument vector(s) and the mask vector should "
2154 "be the same");
2155 return error_mark_node;
2158 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2159 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2161 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2162 "must have the same size as inner type of the mask");
2163 return error_mark_node;
2166 if (!c_dialect_cxx ())
2168 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2169 v0 = c_fully_fold (v0, false, &maybe_const);
2170 wrap &= maybe_const;
2172 if (two_arguments)
2173 v1 = v0 = save_expr (v0);
2174 else
2176 v1 = c_fully_fold (v1, false, &maybe_const);
2177 wrap &= maybe_const;
2180 mask = c_fully_fold (mask, false, &maybe_const);
2181 wrap &= maybe_const;
2184 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2186 if (!c_dialect_cxx () && !wrap)
2187 ret = c_wrap_maybe_const (ret, true);
2189 return ret;
2192 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2193 to integral type. */
2195 static tree
2196 c_common_get_narrower (tree op, int *unsignedp_ptr)
2198 op = get_narrower (op, unsignedp_ptr);
2200 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2201 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2203 /* C++0x scoped enumerations don't implicitly convert to integral
2204 type; if we stripped an explicit conversion to a larger type we
2205 need to replace it so common_type will still work. */
2206 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2207 TYPE_UNSIGNED (TREE_TYPE (op)));
2208 op = fold_convert (type, op);
2210 return op;
2213 /* This is a helper function of build_binary_op.
2215 For certain operations if both args were extended from the same
2216 smaller type, do the arithmetic in that type and then extend.
2218 BITWISE indicates a bitwise operation.
2219 For them, this optimization is safe only if
2220 both args are zero-extended or both are sign-extended.
2221 Otherwise, we might change the result.
2222 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2223 but calculated in (unsigned short) it would be (unsigned short)-1.
2225 tree
2226 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2228 int unsigned0, unsigned1;
2229 tree arg0, arg1;
2230 int uns;
2231 tree type;
2233 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2234 excessive narrowing when we call get_narrower below. For
2235 example, suppose that OP0 is of unsigned int extended
2236 from signed char and that RESULT_TYPE is long long int.
2237 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2238 like
2240 (long long int) (unsigned int) signed_char
2242 which get_narrower would narrow down to
2244 (unsigned int) signed char
2246 If we do not cast OP0 first, get_narrower would return
2247 signed_char, which is inconsistent with the case of the
2248 explicit cast. */
2249 op0 = convert (result_type, op0);
2250 op1 = convert (result_type, op1);
2252 arg0 = c_common_get_narrower (op0, &unsigned0);
2253 arg1 = c_common_get_narrower (op1, &unsigned1);
2255 /* UNS is 1 if the operation to be done is an unsigned one. */
2256 uns = TYPE_UNSIGNED (result_type);
2258 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2259 but it *requires* conversion to FINAL_TYPE. */
2261 if ((TYPE_PRECISION (TREE_TYPE (op0))
2262 == TYPE_PRECISION (TREE_TYPE (arg0)))
2263 && TREE_TYPE (op0) != result_type)
2264 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2265 if ((TYPE_PRECISION (TREE_TYPE (op1))
2266 == TYPE_PRECISION (TREE_TYPE (arg1)))
2267 && TREE_TYPE (op1) != result_type)
2268 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2270 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2272 /* For bitwise operations, signedness of nominal type
2273 does not matter. Consider only how operands were extended. */
2274 if (bitwise)
2275 uns = unsigned0;
2277 /* Note that in all three cases below we refrain from optimizing
2278 an unsigned operation on sign-extended args.
2279 That would not be valid. */
2281 /* Both args variable: if both extended in same way
2282 from same width, do it in that width.
2283 Do it unsigned if args were zero-extended. */
2284 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2285 < TYPE_PRECISION (result_type))
2286 && (TYPE_PRECISION (TREE_TYPE (arg1))
2287 == TYPE_PRECISION (TREE_TYPE (arg0)))
2288 && unsigned0 == unsigned1
2289 && (unsigned0 || !uns))
2290 return c_common_signed_or_unsigned_type
2291 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2293 else if (TREE_CODE (arg0) == INTEGER_CST
2294 && (unsigned1 || !uns)
2295 && (TYPE_PRECISION (TREE_TYPE (arg1))
2296 < TYPE_PRECISION (result_type))
2297 && (type
2298 = c_common_signed_or_unsigned_type (unsigned1,
2299 TREE_TYPE (arg1)))
2300 && !POINTER_TYPE_P (type)
2301 && int_fits_type_p (arg0, type))
2302 return type;
2304 else if (TREE_CODE (arg1) == INTEGER_CST
2305 && (unsigned0 || !uns)
2306 && (TYPE_PRECISION (TREE_TYPE (arg0))
2307 < TYPE_PRECISION (result_type))
2308 && (type
2309 = c_common_signed_or_unsigned_type (unsigned0,
2310 TREE_TYPE (arg0)))
2311 && !POINTER_TYPE_P (type)
2312 && int_fits_type_p (arg1, type))
2313 return type;
2315 return result_type;
2318 /* Checks if expression EXPR of real/integer type cannot be converted
2319 to the real/integer type TYPE. Function returns true when:
2320 * EXPR is a constant which cannot be exactly converted to TYPE
2321 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2322 for EXPR type and TYPE being both integers or both real.
2323 * EXPR is not a constant of real type and TYPE is an integer.
2324 * EXPR is not a constant of integer type which cannot be
2325 exactly converted to real type.
2326 Function allows conversions between types of different signedness and
2327 does not return true in that case. Function can produce signedness
2328 warnings if PRODUCE_WARNS is true. */
2329 bool
2330 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2332 bool give_warning = false;
2333 tree expr_type = TREE_TYPE (expr);
2334 location_t loc = EXPR_LOC_OR_HERE (expr);
2336 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2338 /* Warn for real constant that is not an exact integer converted
2339 to integer type. */
2340 if (TREE_CODE (expr_type) == REAL_TYPE
2341 && TREE_CODE (type) == INTEGER_TYPE)
2343 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2344 give_warning = true;
2346 /* Warn for an integer constant that does not fit into integer type. */
2347 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2348 && TREE_CODE (type) == INTEGER_TYPE
2349 && !int_fits_type_p (expr, type))
2351 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2352 && tree_int_cst_sgn (expr) < 0)
2354 if (produce_warns)
2355 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2356 " implicitly converted to unsigned type");
2358 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2360 if (produce_warns)
2361 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2362 " constant value to negative integer");
2364 else
2365 give_warning = true;
2367 else if (TREE_CODE (type) == REAL_TYPE)
2369 /* Warn for an integer constant that does not fit into real type. */
2370 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2372 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2373 if (!exact_real_truncate (TYPE_MODE (type), &a))
2374 give_warning = true;
2376 /* Warn for a real constant that does not fit into a smaller
2377 real type. */
2378 else if (TREE_CODE (expr_type) == REAL_TYPE
2379 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2381 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2382 if (!exact_real_truncate (TYPE_MODE (type), &a))
2383 give_warning = true;
2387 else
2389 /* Warn for real types converted to integer types. */
2390 if (TREE_CODE (expr_type) == REAL_TYPE
2391 && TREE_CODE (type) == INTEGER_TYPE)
2392 give_warning = true;
2394 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2395 && TREE_CODE (type) == INTEGER_TYPE)
2397 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2398 expr = get_unwidened (expr, 0);
2399 expr_type = TREE_TYPE (expr);
2401 /* Don't warn for short y; short x = ((int)y & 0xff); */
2402 if (TREE_CODE (expr) == BIT_AND_EXPR
2403 || TREE_CODE (expr) == BIT_IOR_EXPR
2404 || TREE_CODE (expr) == BIT_XOR_EXPR)
2406 /* If both args were extended from a shortest type,
2407 use that type if that is safe. */
2408 expr_type = shorten_binary_op (expr_type,
2409 TREE_OPERAND (expr, 0),
2410 TREE_OPERAND (expr, 1),
2411 /* bitwise */1);
2413 if (TREE_CODE (expr) == BIT_AND_EXPR)
2415 tree op0 = TREE_OPERAND (expr, 0);
2416 tree op1 = TREE_OPERAND (expr, 1);
2417 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2418 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2420 /* If one of the operands is a non-negative constant
2421 that fits in the target type, then the type of the
2422 other operand does not matter. */
2423 if ((TREE_CODE (op0) == INTEGER_CST
2424 && int_fits_type_p (op0, c_common_signed_type (type))
2425 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2426 || (TREE_CODE (op1) == INTEGER_CST
2427 && int_fits_type_p (op1, c_common_signed_type (type))
2428 && int_fits_type_p (op1,
2429 c_common_unsigned_type (type))))
2430 return false;
2431 /* If constant is unsigned and fits in the target
2432 type, then the result will also fit. */
2433 else if ((TREE_CODE (op0) == INTEGER_CST
2434 && unsigned0
2435 && int_fits_type_p (op0, type))
2436 || (TREE_CODE (op1) == INTEGER_CST
2437 && unsigned1
2438 && int_fits_type_p (op1, type)))
2439 return false;
2442 /* Warn for integer types converted to smaller integer types. */
2443 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2444 give_warning = true;
2446 /* When they are the same width but different signedness,
2447 then the value may change. */
2448 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2449 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2450 /* Even when converted to a bigger type, if the type is
2451 unsigned but expr is signed, then negative values
2452 will be changed. */
2453 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2454 && produce_warns)
2455 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2456 "may change the sign of the result",
2457 type, expr_type);
2460 /* Warn for integer types converted to real types if and only if
2461 all the range of values of the integer type cannot be
2462 represented by the real type. */
2463 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2464 && TREE_CODE (type) == REAL_TYPE)
2466 tree type_low_bound, type_high_bound;
2467 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2469 /* Don't warn about char y = 0xff; float x = (int) y; */
2470 expr = get_unwidened (expr, 0);
2471 expr_type = TREE_TYPE (expr);
2473 type_low_bound = TYPE_MIN_VALUE (expr_type);
2474 type_high_bound = TYPE_MAX_VALUE (expr_type);
2475 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2476 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2478 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2479 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2480 give_warning = true;
2483 /* Warn for real types converted to smaller real types. */
2484 else if (TREE_CODE (expr_type) == REAL_TYPE
2485 && TREE_CODE (type) == REAL_TYPE
2486 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2487 give_warning = true;
2490 return give_warning;
2493 /* Warns if the conversion of EXPR to TYPE may alter a value.
2494 This is a helper function for warnings_for_convert_and_check. */
2496 static void
2497 conversion_warning (tree type, tree expr)
2499 tree expr_type = TREE_TYPE (expr);
2500 location_t loc = EXPR_LOC_OR_HERE (expr);
2502 if (!warn_conversion && !warn_sign_conversion)
2503 return;
2505 switch (TREE_CODE (expr))
2507 case EQ_EXPR:
2508 case NE_EXPR:
2509 case LE_EXPR:
2510 case GE_EXPR:
2511 case LT_EXPR:
2512 case GT_EXPR:
2513 case TRUTH_ANDIF_EXPR:
2514 case TRUTH_ORIF_EXPR:
2515 case TRUTH_AND_EXPR:
2516 case TRUTH_OR_EXPR:
2517 case TRUTH_XOR_EXPR:
2518 case TRUTH_NOT_EXPR:
2519 /* Conversion from boolean to a signed:1 bit-field (which only
2520 can hold the values 0 and -1) doesn't lose information - but
2521 it does change the value. */
2522 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2523 warning_at (loc, OPT_Wconversion,
2524 "conversion to %qT from boolean expression", type);
2525 return;
2527 case REAL_CST:
2528 case INTEGER_CST:
2529 if (unsafe_conversion_p (type, expr, true))
2530 warning_at (loc, OPT_Wconversion,
2531 "conversion to %qT alters %qT constant value",
2532 type, expr_type);
2533 return;
2535 case COND_EXPR:
2537 /* In case of COND_EXPR, if both operands are constants or
2538 COND_EXPR, then we do not care about the type of COND_EXPR,
2539 only about the conversion of each operand. */
2540 tree op1 = TREE_OPERAND (expr, 1);
2541 tree op2 = TREE_OPERAND (expr, 2);
2543 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
2544 || TREE_CODE (op1) == COND_EXPR)
2545 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
2546 || TREE_CODE (op2) == COND_EXPR))
2548 conversion_warning (type, op1);
2549 conversion_warning (type, op2);
2550 return;
2552 /* Fall through. */
2555 default: /* 'expr' is not a constant. */
2556 if (unsafe_conversion_p (type, expr, true))
2557 warning_at (loc, OPT_Wconversion,
2558 "conversion to %qT from %qT may alter its value",
2559 type, expr_type);
2563 /* Produce warnings after a conversion. RESULT is the result of
2564 converting EXPR to TYPE. This is a helper function for
2565 convert_and_check and cp_convert_and_check. */
2567 void
2568 warnings_for_convert_and_check (tree type, tree expr, tree result)
2570 location_t loc = EXPR_LOC_OR_HERE (expr);
2572 if (TREE_CODE (expr) == INTEGER_CST
2573 && (TREE_CODE (type) == INTEGER_TYPE
2574 || TREE_CODE (type) == ENUMERAL_TYPE)
2575 && !int_fits_type_p (expr, type))
2577 /* Do not diagnose overflow in a constant expression merely
2578 because a conversion overflowed. */
2579 if (TREE_OVERFLOW (result))
2580 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2582 if (TYPE_UNSIGNED (type))
2584 /* This detects cases like converting -129 or 256 to
2585 unsigned char. */
2586 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2587 warning_at (loc, OPT_Woverflow,
2588 "large integer implicitly truncated to unsigned type");
2589 else
2590 conversion_warning (type, expr);
2592 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2593 warning (OPT_Woverflow,
2594 "overflow in implicit constant conversion");
2595 /* No warning for converting 0x80000000 to int. */
2596 else if (pedantic
2597 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2598 || TYPE_PRECISION (TREE_TYPE (expr))
2599 != TYPE_PRECISION (type)))
2600 warning_at (loc, OPT_Woverflow,
2601 "overflow in implicit constant conversion");
2603 else
2604 conversion_warning (type, expr);
2606 else if ((TREE_CODE (result) == INTEGER_CST
2607 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2608 warning_at (loc, OPT_Woverflow,
2609 "overflow in implicit constant conversion");
2610 else
2611 conversion_warning (type, expr);
2615 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2616 Invoke this function on every expression that is converted implicitly,
2617 i.e. because of language rules and not because of an explicit cast. */
2619 tree
2620 convert_and_check (tree type, tree expr)
2622 tree result;
2623 tree expr_for_warning;
2625 /* Convert from a value with possible excess precision rather than
2626 via the semantic type, but do not warn about values not fitting
2627 exactly in the semantic type. */
2628 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2630 tree orig_type = TREE_TYPE (expr);
2631 expr = TREE_OPERAND (expr, 0);
2632 expr_for_warning = convert (orig_type, expr);
2633 if (orig_type == type)
2634 return expr_for_warning;
2636 else
2637 expr_for_warning = expr;
2639 if (TREE_TYPE (expr) == type)
2640 return expr;
2642 result = convert (type, expr);
2644 if (c_inhibit_evaluation_warnings == 0
2645 && !TREE_OVERFLOW_P (expr)
2646 && result != error_mark_node)
2647 warnings_for_convert_and_check (type, expr_for_warning, result);
2649 return result;
2652 /* A node in a list that describes references to variables (EXPR), which are
2653 either read accesses if WRITER is zero, or write accesses, in which case
2654 WRITER is the parent of EXPR. */
2655 struct tlist
2657 struct tlist *next;
2658 tree expr, writer;
2661 /* Used to implement a cache the results of a call to verify_tree. We only
2662 use this for SAVE_EXPRs. */
2663 struct tlist_cache
2665 struct tlist_cache *next;
2666 struct tlist *cache_before_sp;
2667 struct tlist *cache_after_sp;
2668 tree expr;
2671 /* Obstack to use when allocating tlist structures, and corresponding
2672 firstobj. */
2673 static struct obstack tlist_obstack;
2674 static char *tlist_firstobj = 0;
2676 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2677 warnings. */
2678 static struct tlist *warned_ids;
2679 /* SAVE_EXPRs need special treatment. We process them only once and then
2680 cache the results. */
2681 static struct tlist_cache *save_expr_cache;
2683 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2684 static void merge_tlist (struct tlist **, struct tlist *, int);
2685 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2686 static int warning_candidate_p (tree);
2687 static bool candidate_equal_p (const_tree, const_tree);
2688 static void warn_for_collisions (struct tlist *);
2689 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2690 static struct tlist *new_tlist (struct tlist *, tree, tree);
2692 /* Create a new struct tlist and fill in its fields. */
2693 static struct tlist *
2694 new_tlist (struct tlist *next, tree t, tree writer)
2696 struct tlist *l;
2697 l = XOBNEW (&tlist_obstack, struct tlist);
2698 l->next = next;
2699 l->expr = t;
2700 l->writer = writer;
2701 return l;
2704 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2705 is nonnull, we ignore any node we find which has a writer equal to it. */
2707 static void
2708 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2710 while (add)
2712 struct tlist *next = add->next;
2713 if (!copy)
2714 add->next = *to;
2715 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2716 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2717 add = next;
2721 /* Merge the nodes of ADD into TO. This merging process is done so that for
2722 each variable that already exists in TO, no new node is added; however if
2723 there is a write access recorded in ADD, and an occurrence on TO is only
2724 a read access, then the occurrence in TO will be modified to record the
2725 write. */
2727 static void
2728 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2730 struct tlist **end = to;
2732 while (*end)
2733 end = &(*end)->next;
2735 while (add)
2737 int found = 0;
2738 struct tlist *tmp2;
2739 struct tlist *next = add->next;
2741 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2742 if (candidate_equal_p (tmp2->expr, add->expr))
2744 found = 1;
2745 if (!tmp2->writer)
2746 tmp2->writer = add->writer;
2748 if (!found)
2750 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2751 end = &(*end)->next;
2752 *end = 0;
2754 add = next;
2758 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2759 references in list LIST conflict with it, excluding reads if ONLY writers
2760 is nonzero. */
2762 static void
2763 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2764 int only_writes)
2766 struct tlist *tmp;
2768 /* Avoid duplicate warnings. */
2769 for (tmp = warned_ids; tmp; tmp = tmp->next)
2770 if (candidate_equal_p (tmp->expr, written))
2771 return;
2773 while (list)
2775 if (candidate_equal_p (list->expr, written)
2776 && !candidate_equal_p (list->writer, writer)
2777 && (!only_writes || list->writer))
2779 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2780 warning_at (EXPR_LOC_OR_HERE (writer),
2781 OPT_Wsequence_point, "operation on %qE may be undefined",
2782 list->expr);
2784 list = list->next;
2788 /* Given a list LIST of references to variables, find whether any of these
2789 can cause conflicts due to missing sequence points. */
2791 static void
2792 warn_for_collisions (struct tlist *list)
2794 struct tlist *tmp;
2796 for (tmp = list; tmp; tmp = tmp->next)
2798 if (tmp->writer)
2799 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2803 /* Return nonzero if X is a tree that can be verified by the sequence point
2804 warnings. */
2805 static int
2806 warning_candidate_p (tree x)
2808 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2809 return 0;
2811 if (TREE_CODE (x) == BLOCK)
2812 return 0;
2814 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2815 (lvalue_p) crash on TRY/CATCH. */
2816 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2817 return 0;
2819 if (!lvalue_p (x))
2820 return 0;
2822 /* No point to track non-const calls, they will never satisfy
2823 operand_equal_p. */
2824 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2825 return 0;
2827 if (TREE_CODE (x) == STRING_CST)
2828 return 0;
2830 return 1;
2833 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2834 static bool
2835 candidate_equal_p (const_tree x, const_tree y)
2837 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2840 /* Walk the tree X, and record accesses to variables. If X is written by the
2841 parent tree, WRITER is the parent.
2842 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2843 expression or its only operand forces a sequence point, then everything up
2844 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2845 in PNO_SP.
2846 Once we return, we will have emitted warnings if any subexpression before
2847 such a sequence point could be undefined. On a higher level, however, the
2848 sequence point may not be relevant, and we'll merge the two lists.
2850 Example: (b++, a) + b;
2851 The call that processes the COMPOUND_EXPR will store the increment of B
2852 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2853 processes the PLUS_EXPR will need to merge the two lists so that
2854 eventually, all accesses end up on the same list (and we'll warn about the
2855 unordered subexpressions b++ and b.
2857 A note on merging. If we modify the former example so that our expression
2858 becomes
2859 (b++, b) + a
2860 care must be taken not simply to add all three expressions into the final
2861 PNO_SP list. The function merge_tlist takes care of that by merging the
2862 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2863 way, so that no more than one access to B is recorded. */
2865 static void
2866 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2867 tree writer)
2869 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2870 enum tree_code code;
2871 enum tree_code_class cl;
2873 /* X may be NULL if it is the operand of an empty statement expression
2874 ({ }). */
2875 if (x == NULL)
2876 return;
2878 restart:
2879 code = TREE_CODE (x);
2880 cl = TREE_CODE_CLASS (code);
2882 if (warning_candidate_p (x))
2883 *pno_sp = new_tlist (*pno_sp, x, writer);
2885 switch (code)
2887 case CONSTRUCTOR:
2888 return;
2890 case COMPOUND_EXPR:
2891 case TRUTH_ANDIF_EXPR:
2892 case TRUTH_ORIF_EXPR:
2893 tmp_before = tmp_nosp = tmp_list3 = 0;
2894 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2895 warn_for_collisions (tmp_nosp);
2896 merge_tlist (pbefore_sp, tmp_before, 0);
2897 merge_tlist (pbefore_sp, tmp_nosp, 0);
2898 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2899 merge_tlist (pbefore_sp, tmp_list3, 0);
2900 return;
2902 case COND_EXPR:
2903 tmp_before = tmp_list2 = 0;
2904 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2905 warn_for_collisions (tmp_list2);
2906 merge_tlist (pbefore_sp, tmp_before, 0);
2907 merge_tlist (pbefore_sp, tmp_list2, 1);
2909 tmp_list3 = tmp_nosp = 0;
2910 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2911 warn_for_collisions (tmp_nosp);
2912 merge_tlist (pbefore_sp, tmp_list3, 0);
2914 tmp_list3 = tmp_list2 = 0;
2915 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2916 warn_for_collisions (tmp_list2);
2917 merge_tlist (pbefore_sp, tmp_list3, 0);
2918 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2919 two first, to avoid warning for (a ? b++ : b++). */
2920 merge_tlist (&tmp_nosp, tmp_list2, 0);
2921 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2922 return;
2924 case PREDECREMENT_EXPR:
2925 case PREINCREMENT_EXPR:
2926 case POSTDECREMENT_EXPR:
2927 case POSTINCREMENT_EXPR:
2928 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2929 return;
2931 case MODIFY_EXPR:
2932 tmp_before = tmp_nosp = tmp_list3 = 0;
2933 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2934 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2935 /* Expressions inside the LHS are not ordered wrt. the sequence points
2936 in the RHS. Example:
2937 *a = (a++, 2)
2938 Despite the fact that the modification of "a" is in the before_sp
2939 list (tmp_before), it conflicts with the use of "a" in the LHS.
2940 We can handle this by adding the contents of tmp_list3
2941 to those of tmp_before, and redoing the collision warnings for that
2942 list. */
2943 add_tlist (&tmp_before, tmp_list3, x, 1);
2944 warn_for_collisions (tmp_before);
2945 /* Exclude the LHS itself here; we first have to merge it into the
2946 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2947 didn't exclude the LHS, we'd get it twice, once as a read and once
2948 as a write. */
2949 add_tlist (pno_sp, tmp_list3, x, 0);
2950 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2952 merge_tlist (pbefore_sp, tmp_before, 0);
2953 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2954 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2955 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2956 return;
2958 case CALL_EXPR:
2959 /* We need to warn about conflicts among arguments and conflicts between
2960 args and the function address. Side effects of the function address,
2961 however, are not ordered by the sequence point of the call. */
2963 call_expr_arg_iterator iter;
2964 tree arg;
2965 tmp_before = tmp_nosp = 0;
2966 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2967 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2969 tmp_list2 = tmp_list3 = 0;
2970 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2971 merge_tlist (&tmp_list3, tmp_list2, 0);
2972 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2974 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2975 warn_for_collisions (tmp_before);
2976 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2977 return;
2980 case TREE_LIST:
2981 /* Scan all the list, e.g. indices of multi dimensional array. */
2982 while (x)
2984 tmp_before = tmp_nosp = 0;
2985 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2986 merge_tlist (&tmp_nosp, tmp_before, 0);
2987 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2988 x = TREE_CHAIN (x);
2990 return;
2992 case SAVE_EXPR:
2994 struct tlist_cache *t;
2995 for (t = save_expr_cache; t; t = t->next)
2996 if (candidate_equal_p (t->expr, x))
2997 break;
2999 if (!t)
3001 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3002 t->next = save_expr_cache;
3003 t->expr = x;
3004 save_expr_cache = t;
3006 tmp_before = tmp_nosp = 0;
3007 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3008 warn_for_collisions (tmp_nosp);
3010 tmp_list3 = 0;
3011 while (tmp_nosp)
3013 struct tlist *t = tmp_nosp;
3014 tmp_nosp = t->next;
3015 merge_tlist (&tmp_list3, t, 0);
3017 t->cache_before_sp = tmp_before;
3018 t->cache_after_sp = tmp_list3;
3020 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3021 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3022 return;
3025 case ADDR_EXPR:
3026 x = TREE_OPERAND (x, 0);
3027 if (DECL_P (x))
3028 return;
3029 writer = 0;
3030 goto restart;
3032 default:
3033 /* For other expressions, simply recurse on their operands.
3034 Manual tail recursion for unary expressions.
3035 Other non-expressions need not be processed. */
3036 if (cl == tcc_unary)
3038 x = TREE_OPERAND (x, 0);
3039 writer = 0;
3040 goto restart;
3042 else if (IS_EXPR_CODE_CLASS (cl))
3044 int lp;
3045 int max = TREE_OPERAND_LENGTH (x);
3046 for (lp = 0; lp < max; lp++)
3048 tmp_before = tmp_nosp = 0;
3049 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3050 merge_tlist (&tmp_nosp, tmp_before, 0);
3051 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3054 return;
3058 /* Try to warn for undefined behavior in EXPR due to missing sequence
3059 points. */
3061 DEBUG_FUNCTION void
3062 verify_sequence_points (tree expr)
3064 struct tlist *before_sp = 0, *after_sp = 0;
3066 warned_ids = 0;
3067 save_expr_cache = 0;
3068 if (tlist_firstobj == 0)
3070 gcc_obstack_init (&tlist_obstack);
3071 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3074 verify_tree (expr, &before_sp, &after_sp, 0);
3075 warn_for_collisions (after_sp);
3076 obstack_free (&tlist_obstack, tlist_firstobj);
3079 /* Validate the expression after `case' and apply default promotions. */
3081 static tree
3082 check_case_value (tree value)
3084 if (value == NULL_TREE)
3085 return value;
3087 if (TREE_CODE (value) == INTEGER_CST)
3088 /* Promote char or short to int. */
3089 value = perform_integral_promotions (value);
3090 else if (value != error_mark_node)
3092 error ("case label does not reduce to an integer constant");
3093 value = error_mark_node;
3096 constant_expression_warning (value);
3098 return value;
3101 /* See if the case values LOW and HIGH are in the range of the original
3102 type (i.e. before the default conversion to int) of the switch testing
3103 expression.
3104 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3105 the type before promoting it. CASE_LOW_P is a pointer to the lower
3106 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3107 if the case is not a case range.
3108 The caller has to make sure that we are not called with NULL for
3109 CASE_LOW_P (i.e. the default case).
3110 Returns true if the case label is in range of ORIG_TYPE (saturated or
3111 untouched) or false if the label is out of range. */
3113 static bool
3114 check_case_bounds (tree type, tree orig_type,
3115 tree *case_low_p, tree *case_high_p)
3117 tree min_value, max_value;
3118 tree case_low = *case_low_p;
3119 tree case_high = case_high_p ? *case_high_p : case_low;
3121 /* If there was a problem with the original type, do nothing. */
3122 if (orig_type == error_mark_node)
3123 return true;
3125 min_value = TYPE_MIN_VALUE (orig_type);
3126 max_value = TYPE_MAX_VALUE (orig_type);
3128 /* Case label is less than minimum for type. */
3129 if (tree_int_cst_compare (case_low, min_value) < 0
3130 && tree_int_cst_compare (case_high, min_value) < 0)
3132 warning (0, "case label value is less than minimum value for type");
3133 return false;
3136 /* Case value is greater than maximum for type. */
3137 if (tree_int_cst_compare (case_low, max_value) > 0
3138 && tree_int_cst_compare (case_high, max_value) > 0)
3140 warning (0, "case label value exceeds maximum value for type");
3141 return false;
3144 /* Saturate lower case label value to minimum. */
3145 if (tree_int_cst_compare (case_high, min_value) >= 0
3146 && tree_int_cst_compare (case_low, min_value) < 0)
3148 warning (0, "lower value in case label range"
3149 " less than minimum value for type");
3150 case_low = min_value;
3153 /* Saturate upper case label value to maximum. */
3154 if (tree_int_cst_compare (case_low, max_value) <= 0
3155 && tree_int_cst_compare (case_high, max_value) > 0)
3157 warning (0, "upper value in case label range"
3158 " exceeds maximum value for type");
3159 case_high = max_value;
3162 if (*case_low_p != case_low)
3163 *case_low_p = convert (type, case_low);
3164 if (case_high_p && *case_high_p != case_high)
3165 *case_high_p = convert (type, case_high);
3167 return true;
3170 /* Return an integer type with BITS bits of precision,
3171 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3173 tree
3174 c_common_type_for_size (unsigned int bits, int unsignedp)
3176 if (bits == TYPE_PRECISION (integer_type_node))
3177 return unsignedp ? unsigned_type_node : integer_type_node;
3179 if (bits == TYPE_PRECISION (signed_char_type_node))
3180 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3182 if (bits == TYPE_PRECISION (short_integer_type_node))
3183 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3185 if (bits == TYPE_PRECISION (long_integer_type_node))
3186 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3188 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3189 return (unsignedp ? long_long_unsigned_type_node
3190 : long_long_integer_type_node);
3192 if (int128_integer_type_node
3193 && bits == TYPE_PRECISION (int128_integer_type_node))
3194 return (unsignedp ? int128_unsigned_type_node
3195 : int128_integer_type_node);
3197 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3198 return (unsignedp ? widest_unsigned_literal_type_node
3199 : widest_integer_literal_type_node);
3201 if (bits <= TYPE_PRECISION (intQI_type_node))
3202 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3204 if (bits <= TYPE_PRECISION (intHI_type_node))
3205 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3207 if (bits <= TYPE_PRECISION (intSI_type_node))
3208 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3210 if (bits <= TYPE_PRECISION (intDI_type_node))
3211 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3213 return 0;
3216 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3217 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3218 and saturating if SATP is nonzero, otherwise not saturating. */
3220 tree
3221 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3222 int unsignedp, int satp)
3224 enum machine_mode mode;
3225 if (ibit == 0)
3226 mode = unsignedp ? UQQmode : QQmode;
3227 else
3228 mode = unsignedp ? UHAmode : HAmode;
3230 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3231 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3232 break;
3234 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3236 sorry ("GCC cannot support operators with integer types and "
3237 "fixed-point types that have too many integral and "
3238 "fractional bits together");
3239 return 0;
3242 return c_common_type_for_mode (mode, satp);
3245 /* Used for communication between c_common_type_for_mode and
3246 c_register_builtin_type. */
3247 static GTY(()) tree registered_builtin_types;
3249 /* Return a data type that has machine mode MODE.
3250 If the mode is an integer,
3251 then UNSIGNEDP selects between signed and unsigned types.
3252 If the mode is a fixed-point mode,
3253 then UNSIGNEDP selects between saturating and nonsaturating types. */
3255 tree
3256 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3258 tree t;
3260 if (mode == TYPE_MODE (integer_type_node))
3261 return unsignedp ? unsigned_type_node : integer_type_node;
3263 if (mode == TYPE_MODE (signed_char_type_node))
3264 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3266 if (mode == TYPE_MODE (short_integer_type_node))
3267 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3269 if (mode == TYPE_MODE (long_integer_type_node))
3270 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3272 if (mode == TYPE_MODE (long_long_integer_type_node))
3273 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3275 if (int128_integer_type_node
3276 && mode == TYPE_MODE (int128_integer_type_node))
3277 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3279 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3280 return unsignedp ? widest_unsigned_literal_type_node
3281 : widest_integer_literal_type_node;
3283 if (mode == QImode)
3284 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3286 if (mode == HImode)
3287 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3289 if (mode == SImode)
3290 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3292 if (mode == DImode)
3293 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3295 #if HOST_BITS_PER_WIDE_INT >= 64
3296 if (mode == TYPE_MODE (intTI_type_node))
3297 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3298 #endif
3300 if (mode == TYPE_MODE (float_type_node))
3301 return float_type_node;
3303 if (mode == TYPE_MODE (double_type_node))
3304 return double_type_node;
3306 if (mode == TYPE_MODE (long_double_type_node))
3307 return long_double_type_node;
3309 if (mode == TYPE_MODE (void_type_node))
3310 return void_type_node;
3312 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3313 return (unsignedp
3314 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3315 : make_signed_type (GET_MODE_PRECISION (mode)));
3317 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3318 return (unsignedp
3319 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3320 : make_signed_type (GET_MODE_PRECISION (mode)));
3322 if (COMPLEX_MODE_P (mode))
3324 enum machine_mode inner_mode;
3325 tree inner_type;
3327 if (mode == TYPE_MODE (complex_float_type_node))
3328 return complex_float_type_node;
3329 if (mode == TYPE_MODE (complex_double_type_node))
3330 return complex_double_type_node;
3331 if (mode == TYPE_MODE (complex_long_double_type_node))
3332 return complex_long_double_type_node;
3334 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3335 return complex_integer_type_node;
3337 inner_mode = GET_MODE_INNER (mode);
3338 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3339 if (inner_type != NULL_TREE)
3340 return build_complex_type (inner_type);
3342 else if (VECTOR_MODE_P (mode))
3344 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3345 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3346 if (inner_type != NULL_TREE)
3347 return build_vector_type_for_mode (inner_type, mode);
3350 if (mode == TYPE_MODE (dfloat32_type_node))
3351 return dfloat32_type_node;
3352 if (mode == TYPE_MODE (dfloat64_type_node))
3353 return dfloat64_type_node;
3354 if (mode == TYPE_MODE (dfloat128_type_node))
3355 return dfloat128_type_node;
3357 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3359 if (mode == TYPE_MODE (short_fract_type_node))
3360 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3361 if (mode == TYPE_MODE (fract_type_node))
3362 return unsignedp ? sat_fract_type_node : fract_type_node;
3363 if (mode == TYPE_MODE (long_fract_type_node))
3364 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3365 if (mode == TYPE_MODE (long_long_fract_type_node))
3366 return unsignedp ? sat_long_long_fract_type_node
3367 : long_long_fract_type_node;
3369 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3370 return unsignedp ? sat_unsigned_short_fract_type_node
3371 : unsigned_short_fract_type_node;
3372 if (mode == TYPE_MODE (unsigned_fract_type_node))
3373 return unsignedp ? sat_unsigned_fract_type_node
3374 : unsigned_fract_type_node;
3375 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3376 return unsignedp ? sat_unsigned_long_fract_type_node
3377 : unsigned_long_fract_type_node;
3378 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3379 return unsignedp ? sat_unsigned_long_long_fract_type_node
3380 : unsigned_long_long_fract_type_node;
3382 if (mode == TYPE_MODE (short_accum_type_node))
3383 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3384 if (mode == TYPE_MODE (accum_type_node))
3385 return unsignedp ? sat_accum_type_node : accum_type_node;
3386 if (mode == TYPE_MODE (long_accum_type_node))
3387 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3388 if (mode == TYPE_MODE (long_long_accum_type_node))
3389 return unsignedp ? sat_long_long_accum_type_node
3390 : long_long_accum_type_node;
3392 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3393 return unsignedp ? sat_unsigned_short_accum_type_node
3394 : unsigned_short_accum_type_node;
3395 if (mode == TYPE_MODE (unsigned_accum_type_node))
3396 return unsignedp ? sat_unsigned_accum_type_node
3397 : unsigned_accum_type_node;
3398 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3399 return unsignedp ? sat_unsigned_long_accum_type_node
3400 : unsigned_long_accum_type_node;
3401 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3402 return unsignedp ? sat_unsigned_long_long_accum_type_node
3403 : unsigned_long_long_accum_type_node;
3405 if (mode == QQmode)
3406 return unsignedp ? sat_qq_type_node : qq_type_node;
3407 if (mode == HQmode)
3408 return unsignedp ? sat_hq_type_node : hq_type_node;
3409 if (mode == SQmode)
3410 return unsignedp ? sat_sq_type_node : sq_type_node;
3411 if (mode == DQmode)
3412 return unsignedp ? sat_dq_type_node : dq_type_node;
3413 if (mode == TQmode)
3414 return unsignedp ? sat_tq_type_node : tq_type_node;
3416 if (mode == UQQmode)
3417 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3418 if (mode == UHQmode)
3419 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3420 if (mode == USQmode)
3421 return unsignedp ? sat_usq_type_node : usq_type_node;
3422 if (mode == UDQmode)
3423 return unsignedp ? sat_udq_type_node : udq_type_node;
3424 if (mode == UTQmode)
3425 return unsignedp ? sat_utq_type_node : utq_type_node;
3427 if (mode == HAmode)
3428 return unsignedp ? sat_ha_type_node : ha_type_node;
3429 if (mode == SAmode)
3430 return unsignedp ? sat_sa_type_node : sa_type_node;
3431 if (mode == DAmode)
3432 return unsignedp ? sat_da_type_node : da_type_node;
3433 if (mode == TAmode)
3434 return unsignedp ? sat_ta_type_node : ta_type_node;
3436 if (mode == UHAmode)
3437 return unsignedp ? sat_uha_type_node : uha_type_node;
3438 if (mode == USAmode)
3439 return unsignedp ? sat_usa_type_node : usa_type_node;
3440 if (mode == UDAmode)
3441 return unsignedp ? sat_uda_type_node : uda_type_node;
3442 if (mode == UTAmode)
3443 return unsignedp ? sat_uta_type_node : uta_type_node;
3446 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3447 if (TYPE_MODE (TREE_VALUE (t)) == mode
3448 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3449 return TREE_VALUE (t);
3451 return 0;
3454 tree
3455 c_common_unsigned_type (tree type)
3457 return c_common_signed_or_unsigned_type (1, type);
3460 /* Return a signed type the same as TYPE in other respects. */
3462 tree
3463 c_common_signed_type (tree type)
3465 return c_common_signed_or_unsigned_type (0, type);
3468 /* Return a type the same as TYPE except unsigned or
3469 signed according to UNSIGNEDP. */
3471 tree
3472 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3474 tree type1;
3476 /* This block of code emulates the behavior of the old
3477 c_common_unsigned_type. In particular, it returns
3478 long_unsigned_type_node if passed a long, even when a int would
3479 have the same size. This is necessary for warnings to work
3480 correctly in archs where sizeof(int) == sizeof(long) */
3482 type1 = TYPE_MAIN_VARIANT (type);
3483 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3484 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3485 if (type1 == integer_type_node || type1 == unsigned_type_node)
3486 return unsignedp ? unsigned_type_node : integer_type_node;
3487 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3488 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3489 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3490 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3491 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3492 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3493 if (int128_integer_type_node
3494 && (type1 == int128_integer_type_node
3495 || type1 == int128_unsigned_type_node))
3496 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3497 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3498 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3499 #if HOST_BITS_PER_WIDE_INT >= 64
3500 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3501 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3502 #endif
3503 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3504 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3505 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3506 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3507 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3508 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3509 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3510 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3512 #define C_COMMON_FIXED_TYPES(NAME) \
3513 if (type1 == short_ ## NAME ## _type_node \
3514 || type1 == unsigned_short_ ## NAME ## _type_node) \
3515 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3516 : short_ ## NAME ## _type_node; \
3517 if (type1 == NAME ## _type_node \
3518 || type1 == unsigned_ ## NAME ## _type_node) \
3519 return unsignedp ? unsigned_ ## NAME ## _type_node \
3520 : NAME ## _type_node; \
3521 if (type1 == long_ ## NAME ## _type_node \
3522 || type1 == unsigned_long_ ## NAME ## _type_node) \
3523 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3524 : long_ ## NAME ## _type_node; \
3525 if (type1 == long_long_ ## NAME ## _type_node \
3526 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3527 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3528 : long_long_ ## NAME ## _type_node;
3530 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3531 if (type1 == NAME ## _type_node \
3532 || type1 == u ## NAME ## _type_node) \
3533 return unsignedp ? u ## NAME ## _type_node \
3534 : NAME ## _type_node;
3536 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3537 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3538 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3539 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3540 : sat_ ## short_ ## NAME ## _type_node; \
3541 if (type1 == sat_ ## NAME ## _type_node \
3542 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3543 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3544 : sat_ ## NAME ## _type_node; \
3545 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3546 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3547 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3548 : sat_ ## long_ ## NAME ## _type_node; \
3549 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3550 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3551 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3552 : sat_ ## long_long_ ## NAME ## _type_node;
3554 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3555 if (type1 == sat_ ## NAME ## _type_node \
3556 || type1 == sat_ ## u ## NAME ## _type_node) \
3557 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3558 : sat_ ## NAME ## _type_node;
3560 C_COMMON_FIXED_TYPES (fract);
3561 C_COMMON_FIXED_TYPES_SAT (fract);
3562 C_COMMON_FIXED_TYPES (accum);
3563 C_COMMON_FIXED_TYPES_SAT (accum);
3565 C_COMMON_FIXED_MODE_TYPES (qq);
3566 C_COMMON_FIXED_MODE_TYPES (hq);
3567 C_COMMON_FIXED_MODE_TYPES (sq);
3568 C_COMMON_FIXED_MODE_TYPES (dq);
3569 C_COMMON_FIXED_MODE_TYPES (tq);
3570 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3571 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3572 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3573 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3574 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3575 C_COMMON_FIXED_MODE_TYPES (ha);
3576 C_COMMON_FIXED_MODE_TYPES (sa);
3577 C_COMMON_FIXED_MODE_TYPES (da);
3578 C_COMMON_FIXED_MODE_TYPES (ta);
3579 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3580 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3581 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3582 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3584 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3585 the precision; they have precision set to match their range, but
3586 may use a wider mode to match an ABI. If we change modes, we may
3587 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3588 the precision as well, so as to yield correct results for
3589 bit-field types. C++ does not have these separate bit-field
3590 types, and producing a signed or unsigned variant of an
3591 ENUMERAL_TYPE may cause other problems as well. */
3593 if (!INTEGRAL_TYPE_P (type)
3594 || TYPE_UNSIGNED (type) == unsignedp)
3595 return type;
3597 #define TYPE_OK(node) \
3598 (TYPE_MODE (type) == TYPE_MODE (node) \
3599 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3600 if (TYPE_OK (signed_char_type_node))
3601 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3602 if (TYPE_OK (integer_type_node))
3603 return unsignedp ? unsigned_type_node : integer_type_node;
3604 if (TYPE_OK (short_integer_type_node))
3605 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3606 if (TYPE_OK (long_integer_type_node))
3607 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3608 if (TYPE_OK (long_long_integer_type_node))
3609 return (unsignedp ? long_long_unsigned_type_node
3610 : long_long_integer_type_node);
3611 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3612 return (unsignedp ? int128_unsigned_type_node
3613 : int128_integer_type_node);
3614 if (TYPE_OK (widest_integer_literal_type_node))
3615 return (unsignedp ? widest_unsigned_literal_type_node
3616 : widest_integer_literal_type_node);
3618 #if HOST_BITS_PER_WIDE_INT >= 64
3619 if (TYPE_OK (intTI_type_node))
3620 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3621 #endif
3622 if (TYPE_OK (intDI_type_node))
3623 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3624 if (TYPE_OK (intSI_type_node))
3625 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3626 if (TYPE_OK (intHI_type_node))
3627 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3628 if (TYPE_OK (intQI_type_node))
3629 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3630 #undef TYPE_OK
3632 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3635 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3637 tree
3638 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3640 /* Extended integer types of the same width as a standard type have
3641 lesser rank, so those of the same width as int promote to int or
3642 unsigned int and are valid for printf formats expecting int or
3643 unsigned int. To avoid such special cases, avoid creating
3644 extended integer types for bit-fields if a standard integer type
3645 is available. */
3646 if (width == TYPE_PRECISION (integer_type_node))
3647 return unsignedp ? unsigned_type_node : integer_type_node;
3648 if (width == TYPE_PRECISION (signed_char_type_node))
3649 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3650 if (width == TYPE_PRECISION (short_integer_type_node))
3651 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3652 if (width == TYPE_PRECISION (long_integer_type_node))
3653 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3654 if (width == TYPE_PRECISION (long_long_integer_type_node))
3655 return (unsignedp ? long_long_unsigned_type_node
3656 : long_long_integer_type_node);
3657 if (int128_integer_type_node
3658 && width == TYPE_PRECISION (int128_integer_type_node))
3659 return (unsignedp ? int128_unsigned_type_node
3660 : int128_integer_type_node);
3661 return build_nonstandard_integer_type (width, unsignedp);
3664 /* The C version of the register_builtin_type langhook. */
3666 void
3667 c_register_builtin_type (tree type, const char* name)
3669 tree decl;
3671 decl = build_decl (UNKNOWN_LOCATION,
3672 TYPE_DECL, get_identifier (name), type);
3673 DECL_ARTIFICIAL (decl) = 1;
3674 if (!TYPE_NAME (type))
3675 TYPE_NAME (type) = decl;
3676 pushdecl (decl);
3678 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3681 /* Print an error message for invalid operands to arith operation
3682 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3683 LOCATION is the location of the message. */
3685 void
3686 binary_op_error (location_t location, enum tree_code code,
3687 tree type0, tree type1)
3689 const char *opname;
3691 switch (code)
3693 case PLUS_EXPR:
3694 opname = "+"; break;
3695 case MINUS_EXPR:
3696 opname = "-"; break;
3697 case MULT_EXPR:
3698 opname = "*"; break;
3699 case MAX_EXPR:
3700 opname = "max"; break;
3701 case MIN_EXPR:
3702 opname = "min"; break;
3703 case EQ_EXPR:
3704 opname = "=="; break;
3705 case NE_EXPR:
3706 opname = "!="; break;
3707 case LE_EXPR:
3708 opname = "<="; break;
3709 case GE_EXPR:
3710 opname = ">="; break;
3711 case LT_EXPR:
3712 opname = "<"; break;
3713 case GT_EXPR:
3714 opname = ">"; break;
3715 case LSHIFT_EXPR:
3716 opname = "<<"; break;
3717 case RSHIFT_EXPR:
3718 opname = ">>"; break;
3719 case TRUNC_MOD_EXPR:
3720 case FLOOR_MOD_EXPR:
3721 opname = "%"; break;
3722 case TRUNC_DIV_EXPR:
3723 case FLOOR_DIV_EXPR:
3724 opname = "/"; break;
3725 case BIT_AND_EXPR:
3726 opname = "&"; break;
3727 case BIT_IOR_EXPR:
3728 opname = "|"; break;
3729 case TRUTH_ANDIF_EXPR:
3730 opname = "&&"; break;
3731 case TRUTH_ORIF_EXPR:
3732 opname = "||"; break;
3733 case BIT_XOR_EXPR:
3734 opname = "^"; break;
3735 default:
3736 gcc_unreachable ();
3738 error_at (location,
3739 "invalid operands to binary %s (have %qT and %qT)", opname,
3740 type0, type1);
3743 /* Given an expression as a tree, return its original type. Do this
3744 by stripping any conversion that preserves the sign and precision. */
3745 static tree
3746 expr_original_type (tree expr)
3748 STRIP_SIGN_NOPS (expr);
3749 return TREE_TYPE (expr);
3752 /* Subroutine of build_binary_op, used for comparison operations.
3753 See if the operands have both been converted from subword integer types
3754 and, if so, perhaps change them both back to their original type.
3755 This function is also responsible for converting the two operands
3756 to the proper common type for comparison.
3758 The arguments of this function are all pointers to local variables
3759 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3760 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3762 If this function returns nonzero, it means that the comparison has
3763 a constant value. What this function returns is an expression for
3764 that value. */
3766 tree
3767 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3768 enum tree_code *rescode_ptr)
3770 tree type;
3771 tree op0 = *op0_ptr;
3772 tree op1 = *op1_ptr;
3773 int unsignedp0, unsignedp1;
3774 int real1, real2;
3775 tree primop0, primop1;
3776 enum tree_code code = *rescode_ptr;
3777 location_t loc = EXPR_LOC_OR_HERE (op0);
3779 /* Throw away any conversions to wider types
3780 already present in the operands. */
3782 primop0 = c_common_get_narrower (op0, &unsignedp0);
3783 primop1 = c_common_get_narrower (op1, &unsignedp1);
3785 /* If primopN is first sign-extended from primopN's precision to opN's
3786 precision, then zero-extended from opN's precision to
3787 *restype_ptr precision, shortenings might be invalid. */
3788 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3789 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3790 && !unsignedp0
3791 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3792 primop0 = op0;
3793 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3794 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3795 && !unsignedp1
3796 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3797 primop1 = op1;
3799 /* Handle the case that OP0 does not *contain* a conversion
3800 but it *requires* conversion to FINAL_TYPE. */
3802 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3803 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3804 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3805 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3807 /* If one of the operands must be floated, we cannot optimize. */
3808 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3809 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3811 /* If first arg is constant, swap the args (changing operation
3812 so value is preserved), for canonicalization. Don't do this if
3813 the second arg is 0. */
3815 if (TREE_CONSTANT (primop0)
3816 && !integer_zerop (primop1) && !real_zerop (primop1)
3817 && !fixed_zerop (primop1))
3819 tree tem = primop0;
3820 int temi = unsignedp0;
3821 primop0 = primop1;
3822 primop1 = tem;
3823 tem = op0;
3824 op0 = op1;
3825 op1 = tem;
3826 *op0_ptr = op0;
3827 *op1_ptr = op1;
3828 unsignedp0 = unsignedp1;
3829 unsignedp1 = temi;
3830 temi = real1;
3831 real1 = real2;
3832 real2 = temi;
3834 switch (code)
3836 case LT_EXPR:
3837 code = GT_EXPR;
3838 break;
3839 case GT_EXPR:
3840 code = LT_EXPR;
3841 break;
3842 case LE_EXPR:
3843 code = GE_EXPR;
3844 break;
3845 case GE_EXPR:
3846 code = LE_EXPR;
3847 break;
3848 default:
3849 break;
3851 *rescode_ptr = code;
3854 /* If comparing an integer against a constant more bits wide,
3855 maybe we can deduce a value of 1 or 0 independent of the data.
3856 Or else truncate the constant now
3857 rather than extend the variable at run time.
3859 This is only interesting if the constant is the wider arg.
3860 Also, it is not safe if the constant is unsigned and the
3861 variable arg is signed, since in this case the variable
3862 would be sign-extended and then regarded as unsigned.
3863 Our technique fails in this case because the lowest/highest
3864 possible unsigned results don't follow naturally from the
3865 lowest/highest possible values of the variable operand.
3866 For just EQ_EXPR and NE_EXPR there is another technique that
3867 could be used: see if the constant can be faithfully represented
3868 in the other operand's type, by truncating it and reextending it
3869 and see if that preserves the constant's value. */
3871 if (!real1 && !real2
3872 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3873 && TREE_CODE (primop1) == INTEGER_CST
3874 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3876 int min_gt, max_gt, min_lt, max_lt;
3877 tree maxval, minval;
3878 /* 1 if comparison is nominally unsigned. */
3879 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3880 tree val;
3882 type = c_common_signed_or_unsigned_type (unsignedp0,
3883 TREE_TYPE (primop0));
3885 maxval = TYPE_MAX_VALUE (type);
3886 minval = TYPE_MIN_VALUE (type);
3888 if (unsignedp && !unsignedp0)
3889 *restype_ptr = c_common_signed_type (*restype_ptr);
3891 if (TREE_TYPE (primop1) != *restype_ptr)
3893 /* Convert primop1 to target type, but do not introduce
3894 additional overflow. We know primop1 is an int_cst. */
3895 primop1 = force_fit_type_double (*restype_ptr,
3896 tree_to_double_int (primop1),
3897 0, TREE_OVERFLOW (primop1));
3899 if (type != *restype_ptr)
3901 minval = convert (*restype_ptr, minval);
3902 maxval = convert (*restype_ptr, maxval);
3905 if (unsignedp && unsignedp0)
3907 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3908 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3909 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3910 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3912 else
3914 min_gt = INT_CST_LT (primop1, minval);
3915 max_gt = INT_CST_LT (primop1, maxval);
3916 min_lt = INT_CST_LT (minval, primop1);
3917 max_lt = INT_CST_LT (maxval, primop1);
3920 val = 0;
3921 /* This used to be a switch, but Genix compiler can't handle that. */
3922 if (code == NE_EXPR)
3924 if (max_lt || min_gt)
3925 val = truthvalue_true_node;
3927 else if (code == EQ_EXPR)
3929 if (max_lt || min_gt)
3930 val = truthvalue_false_node;
3932 else if (code == LT_EXPR)
3934 if (max_lt)
3935 val = truthvalue_true_node;
3936 if (!min_lt)
3937 val = truthvalue_false_node;
3939 else if (code == GT_EXPR)
3941 if (min_gt)
3942 val = truthvalue_true_node;
3943 if (!max_gt)
3944 val = truthvalue_false_node;
3946 else if (code == LE_EXPR)
3948 if (!max_gt)
3949 val = truthvalue_true_node;
3950 if (min_gt)
3951 val = truthvalue_false_node;
3953 else if (code == GE_EXPR)
3955 if (!min_lt)
3956 val = truthvalue_true_node;
3957 if (max_lt)
3958 val = truthvalue_false_node;
3961 /* If primop0 was sign-extended and unsigned comparison specd,
3962 we did a signed comparison above using the signed type bounds.
3963 But the comparison we output must be unsigned.
3965 Also, for inequalities, VAL is no good; but if the signed
3966 comparison had *any* fixed result, it follows that the
3967 unsigned comparison just tests the sign in reverse
3968 (positive values are LE, negative ones GE).
3969 So we can generate an unsigned comparison
3970 against an extreme value of the signed type. */
3972 if (unsignedp && !unsignedp0)
3974 if (val != 0)
3975 switch (code)
3977 case LT_EXPR:
3978 case GE_EXPR:
3979 primop1 = TYPE_MIN_VALUE (type);
3980 val = 0;
3981 break;
3983 case LE_EXPR:
3984 case GT_EXPR:
3985 primop1 = TYPE_MAX_VALUE (type);
3986 val = 0;
3987 break;
3989 default:
3990 break;
3992 type = c_common_unsigned_type (type);
3995 if (TREE_CODE (primop0) != INTEGER_CST
3996 && c_inhibit_evaluation_warnings == 0)
3998 if (val == truthvalue_false_node)
3999 warning_at (loc, OPT_Wtype_limits,
4000 "comparison is always false due to limited range of data type");
4001 if (val == truthvalue_true_node)
4002 warning_at (loc, OPT_Wtype_limits,
4003 "comparison is always true due to limited range of data type");
4006 if (val != 0)
4008 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4009 if (TREE_SIDE_EFFECTS (primop0))
4010 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4011 return val;
4014 /* Value is not predetermined, but do the comparison
4015 in the type of the operand that is not constant.
4016 TYPE is already properly set. */
4019 /* If either arg is decimal float and the other is float, find the
4020 proper common type to use for comparison. */
4021 else if (real1 && real2
4022 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4023 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4024 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4026 else if (real1 && real2
4027 && (TYPE_PRECISION (TREE_TYPE (primop0))
4028 == TYPE_PRECISION (TREE_TYPE (primop1))))
4029 type = TREE_TYPE (primop0);
4031 /* If args' natural types are both narrower than nominal type
4032 and both extend in the same manner, compare them
4033 in the type of the wider arg.
4034 Otherwise must actually extend both to the nominal
4035 common type lest different ways of extending
4036 alter the result.
4037 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4039 else if (unsignedp0 == unsignedp1 && real1 == real2
4040 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4041 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4043 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4044 type = c_common_signed_or_unsigned_type (unsignedp0
4045 || TYPE_UNSIGNED (*restype_ptr),
4046 type);
4047 /* Make sure shorter operand is extended the right way
4048 to match the longer operand. */
4049 primop0
4050 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4051 TREE_TYPE (primop0)),
4052 primop0);
4053 primop1
4054 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4055 TREE_TYPE (primop1)),
4056 primop1);
4058 else
4060 /* Here we must do the comparison on the nominal type
4061 using the args exactly as we received them. */
4062 type = *restype_ptr;
4063 primop0 = op0;
4064 primop1 = op1;
4066 if (!real1 && !real2 && integer_zerop (primop1)
4067 && TYPE_UNSIGNED (*restype_ptr))
4069 tree value = 0;
4070 /* All unsigned values are >= 0, so we warn. However,
4071 if OP0 is a constant that is >= 0, the signedness of
4072 the comparison isn't an issue, so suppress the
4073 warning. */
4074 bool warn =
4075 warn_type_limits && !in_system_header
4076 && c_inhibit_evaluation_warnings == 0
4077 && !(TREE_CODE (primop0) == INTEGER_CST
4078 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4079 primop0)))
4080 /* Do not warn for enumeration types. */
4081 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4083 switch (code)
4085 case GE_EXPR:
4086 if (warn)
4087 warning_at (loc, OPT_Wtype_limits,
4088 "comparison of unsigned expression >= 0 is always true");
4089 value = truthvalue_true_node;
4090 break;
4092 case LT_EXPR:
4093 if (warn)
4094 warning_at (loc, OPT_Wtype_limits,
4095 "comparison of unsigned expression < 0 is always false");
4096 value = truthvalue_false_node;
4097 break;
4099 default:
4100 break;
4103 if (value != 0)
4105 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4106 if (TREE_SIDE_EFFECTS (primop0))
4107 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4108 primop0, value);
4109 return value;
4114 *op0_ptr = convert (type, primop0);
4115 *op1_ptr = convert (type, primop1);
4117 *restype_ptr = truthvalue_type_node;
4119 return 0;
4122 /* Return a tree for the sum or difference (RESULTCODE says which)
4123 of pointer PTROP and integer INTOP. */
4125 tree
4126 pointer_int_sum (location_t loc, enum tree_code resultcode,
4127 tree ptrop, tree intop)
4129 tree size_exp, ret;
4131 /* The result is a pointer of the same type that is being added. */
4132 tree result_type = TREE_TYPE (ptrop);
4134 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4136 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4137 "pointer of type %<void *%> used in arithmetic");
4138 size_exp = integer_one_node;
4140 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4142 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4143 "pointer to a function used in arithmetic");
4144 size_exp = integer_one_node;
4146 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4148 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4149 "pointer to member function used in arithmetic");
4150 size_exp = integer_one_node;
4152 else
4153 size_exp = size_in_bytes (TREE_TYPE (result_type));
4155 /* We are manipulating pointer values, so we don't need to warn
4156 about relying on undefined signed overflow. We disable the
4157 warning here because we use integer types so fold won't know that
4158 they are really pointers. */
4159 fold_defer_overflow_warnings ();
4161 /* If what we are about to multiply by the size of the elements
4162 contains a constant term, apply distributive law
4163 and multiply that constant term separately.
4164 This helps produce common subexpressions. */
4165 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4166 && !TREE_CONSTANT (intop)
4167 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4168 && TREE_CONSTANT (size_exp)
4169 /* If the constant comes from pointer subtraction,
4170 skip this optimization--it would cause an error. */
4171 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4172 /* If the constant is unsigned, and smaller than the pointer size,
4173 then we must skip this optimization. This is because it could cause
4174 an overflow error if the constant is negative but INTOP is not. */
4175 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4176 || (TYPE_PRECISION (TREE_TYPE (intop))
4177 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4179 enum tree_code subcode = resultcode;
4180 tree int_type = TREE_TYPE (intop);
4181 if (TREE_CODE (intop) == MINUS_EXPR)
4182 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4183 /* Convert both subexpression types to the type of intop,
4184 because weird cases involving pointer arithmetic
4185 can result in a sum or difference with different type args. */
4186 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4187 subcode, ptrop,
4188 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4189 intop = convert (int_type, TREE_OPERAND (intop, 0));
4192 /* Convert the integer argument to a type the same size as sizetype
4193 so the multiply won't overflow spuriously. */
4194 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4195 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4196 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4197 TYPE_UNSIGNED (sizetype)), intop);
4199 /* Replace the integer argument with a suitable product by the object size.
4200 Do this multiplication as signed, then convert to the appropriate type
4201 for the pointer operation and disregard an overflow that occurred only
4202 because of the sign-extension change in the latter conversion. */
4204 tree t = build_binary_op (loc,
4205 MULT_EXPR, intop,
4206 convert (TREE_TYPE (intop), size_exp), 1);
4207 intop = convert (sizetype, t);
4208 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4209 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4210 TREE_INT_CST_HIGH (intop));
4213 /* Create the sum or difference. */
4214 if (resultcode == MINUS_EXPR)
4215 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4217 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4219 fold_undefer_and_ignore_overflow_warnings ();
4221 return ret;
4224 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4225 and if NON_CONST is known not to be permitted in an evaluated part
4226 of a constant expression. */
4228 tree
4229 c_wrap_maybe_const (tree expr, bool non_const)
4231 bool nowarning = TREE_NO_WARNING (expr);
4232 location_t loc = EXPR_LOCATION (expr);
4234 /* This should never be called for C++. */
4235 if (c_dialect_cxx ())
4236 gcc_unreachable ();
4238 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4239 STRIP_TYPE_NOPS (expr);
4240 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4241 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4242 if (nowarning)
4243 TREE_NO_WARNING (expr) = 1;
4244 protected_set_expr_location (expr, loc);
4246 return expr;
4249 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4250 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4251 around the SAVE_EXPR if needed so that c_fully_fold does not need
4252 to look inside SAVE_EXPRs. */
4254 tree
4255 c_save_expr (tree expr)
4257 bool maybe_const = true;
4258 if (c_dialect_cxx ())
4259 return save_expr (expr);
4260 expr = c_fully_fold (expr, false, &maybe_const);
4261 expr = save_expr (expr);
4262 if (!maybe_const)
4263 expr = c_wrap_maybe_const (expr, true);
4264 return expr;
4267 /* Return whether EXPR is a declaration whose address can never be
4268 NULL. */
4270 bool
4271 decl_with_nonnull_addr_p (const_tree expr)
4273 return (DECL_P (expr)
4274 && (TREE_CODE (expr) == PARM_DECL
4275 || TREE_CODE (expr) == LABEL_DECL
4276 || !DECL_WEAK (expr)));
4279 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4280 or for an `if' or `while' statement or ?..: exp. It should already
4281 have been validated to be of suitable type; otherwise, a bad
4282 diagnostic may result.
4284 The EXPR is located at LOCATION.
4286 This preparation consists of taking the ordinary
4287 representation of an expression expr and producing a valid tree
4288 boolean expression describing whether expr is nonzero. We could
4289 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4290 but we optimize comparisons, &&, ||, and !.
4292 The resulting type should always be `truthvalue_type_node'. */
4294 tree
4295 c_common_truthvalue_conversion (location_t location, tree expr)
4297 switch (TREE_CODE (expr))
4299 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4300 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4301 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4302 case ORDERED_EXPR: case UNORDERED_EXPR:
4303 if (TREE_TYPE (expr) == truthvalue_type_node)
4304 return expr;
4305 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4306 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4307 goto ret;
4309 case TRUTH_ANDIF_EXPR:
4310 case TRUTH_ORIF_EXPR:
4311 case TRUTH_AND_EXPR:
4312 case TRUTH_OR_EXPR:
4313 case TRUTH_XOR_EXPR:
4314 if (TREE_TYPE (expr) == truthvalue_type_node)
4315 return expr;
4316 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4317 c_common_truthvalue_conversion (location,
4318 TREE_OPERAND (expr, 0)),
4319 c_common_truthvalue_conversion (location,
4320 TREE_OPERAND (expr, 1)));
4321 goto ret;
4323 case TRUTH_NOT_EXPR:
4324 if (TREE_TYPE (expr) == truthvalue_type_node)
4325 return expr;
4326 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4327 c_common_truthvalue_conversion (location,
4328 TREE_OPERAND (expr, 0)));
4329 goto ret;
4331 case ERROR_MARK:
4332 return expr;
4334 case INTEGER_CST:
4335 return integer_zerop (expr) ? truthvalue_false_node
4336 : truthvalue_true_node;
4338 case REAL_CST:
4339 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4340 ? truthvalue_true_node
4341 : truthvalue_false_node;
4343 case FIXED_CST:
4344 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4345 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4346 ? truthvalue_true_node
4347 : truthvalue_false_node;
4349 case FUNCTION_DECL:
4350 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4351 /* Fall through. */
4353 case ADDR_EXPR:
4355 tree inner = TREE_OPERAND (expr, 0);
4356 if (decl_with_nonnull_addr_p (inner))
4358 /* Common Ada/Pascal programmer's mistake. */
4359 warning_at (location,
4360 OPT_Waddress,
4361 "the address of %qD will always evaluate as %<true%>",
4362 inner);
4363 return truthvalue_true_node;
4365 break;
4368 case COMPLEX_EXPR:
4369 expr = build_binary_op (EXPR_LOCATION (expr),
4370 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4371 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4372 c_common_truthvalue_conversion (location,
4373 TREE_OPERAND (expr, 0)),
4374 c_common_truthvalue_conversion (location,
4375 TREE_OPERAND (expr, 1)),
4377 goto ret;
4379 case NEGATE_EXPR:
4380 case ABS_EXPR:
4381 case FLOAT_EXPR:
4382 case EXCESS_PRECISION_EXPR:
4383 /* These don't change whether an object is nonzero or zero. */
4384 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4386 case LROTATE_EXPR:
4387 case RROTATE_EXPR:
4388 /* These don't change whether an object is zero or nonzero, but
4389 we can't ignore them if their second arg has side-effects. */
4390 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4392 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4393 TREE_OPERAND (expr, 1),
4394 c_common_truthvalue_conversion
4395 (location, TREE_OPERAND (expr, 0)));
4396 goto ret;
4398 else
4399 return c_common_truthvalue_conversion (location,
4400 TREE_OPERAND (expr, 0));
4402 case COND_EXPR:
4403 /* Distribute the conversion into the arms of a COND_EXPR. */
4404 if (c_dialect_cxx ())
4406 tree op1 = TREE_OPERAND (expr, 1);
4407 tree op2 = TREE_OPERAND (expr, 2);
4408 /* In C++ one of the arms might have void type if it is throw. */
4409 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4410 op1 = c_common_truthvalue_conversion (location, op1);
4411 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4412 op2 = c_common_truthvalue_conversion (location, op2);
4413 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4414 TREE_OPERAND (expr, 0), op1, op2);
4415 goto ret;
4417 else
4419 /* Folding will happen later for C. */
4420 expr = build3 (COND_EXPR, truthvalue_type_node,
4421 TREE_OPERAND (expr, 0),
4422 c_common_truthvalue_conversion (location,
4423 TREE_OPERAND (expr, 1)),
4424 c_common_truthvalue_conversion (location,
4425 TREE_OPERAND (expr, 2)));
4426 goto ret;
4429 CASE_CONVERT:
4431 tree totype = TREE_TYPE (expr);
4432 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4434 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4435 since that affects how `default_conversion' will behave. */
4436 if (TREE_CODE (totype) == REFERENCE_TYPE
4437 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4438 break;
4439 /* Don't strip a conversion from C++0x scoped enum, since they
4440 don't implicitly convert to other types. */
4441 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4442 && ENUM_IS_SCOPED (fromtype))
4443 break;
4444 /* If this isn't narrowing the argument, we can ignore it. */
4445 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4446 return c_common_truthvalue_conversion (location,
4447 TREE_OPERAND (expr, 0));
4449 break;
4451 case MODIFY_EXPR:
4452 if (!TREE_NO_WARNING (expr)
4453 && warn_parentheses)
4455 warning (OPT_Wparentheses,
4456 "suggest parentheses around assignment used as truth value");
4457 TREE_NO_WARNING (expr) = 1;
4459 break;
4461 default:
4462 break;
4465 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4467 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4468 expr = (build_binary_op
4469 (EXPR_LOCATION (expr),
4470 (TREE_SIDE_EFFECTS (expr)
4471 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4472 c_common_truthvalue_conversion
4473 (location,
4474 build_unary_op (location, REALPART_EXPR, t, 0)),
4475 c_common_truthvalue_conversion
4476 (location,
4477 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4478 0));
4479 goto ret;
4482 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4484 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4485 FCONST0 (TYPE_MODE
4486 (TREE_TYPE (expr))));
4487 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4489 else
4490 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4492 ret:
4493 protected_set_expr_location (expr, location);
4494 return expr;
4497 static void def_builtin_1 (enum built_in_function fncode,
4498 const char *name,
4499 enum built_in_class fnclass,
4500 tree fntype, tree libtype,
4501 bool both_p, bool fallback_p, bool nonansi_p,
4502 tree fnattrs, bool implicit_p);
4505 /* Apply the TYPE_QUALS to the new DECL. */
4507 void
4508 c_apply_type_quals_to_decl (int type_quals, tree decl)
4510 tree type = TREE_TYPE (decl);
4512 if (type == error_mark_node)
4513 return;
4515 if ((type_quals & TYPE_QUAL_CONST)
4516 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4517 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4518 constructor can produce constant init, so rely on cp_finish_decl to
4519 clear TREE_READONLY if the variable has non-constant init. */
4520 TREE_READONLY (decl) = 1;
4521 if (type_quals & TYPE_QUAL_VOLATILE)
4523 TREE_SIDE_EFFECTS (decl) = 1;
4524 TREE_THIS_VOLATILE (decl) = 1;
4526 if (type_quals & TYPE_QUAL_RESTRICT)
4528 while (type && TREE_CODE (type) == ARRAY_TYPE)
4529 /* Allow 'restrict' on arrays of pointers.
4530 FIXME currently we just ignore it. */
4531 type = TREE_TYPE (type);
4532 if (!type
4533 || !POINTER_TYPE_P (type)
4534 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4535 error ("invalid use of %<restrict%>");
4539 /* Hash function for the problem of multiple type definitions in
4540 different files. This must hash all types that will compare
4541 equal via comptypes to the same value. In practice it hashes
4542 on some of the simple stuff and leaves the details to comptypes. */
4544 static hashval_t
4545 c_type_hash (const void *p)
4547 int n_elements;
4548 int shift, size;
4549 const_tree const t = (const_tree) p;
4550 tree t2;
4551 switch (TREE_CODE (t))
4553 /* For pointers, hash on pointee type plus some swizzling. */
4554 case POINTER_TYPE:
4555 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4556 /* Hash on number of elements and total size. */
4557 case ENUMERAL_TYPE:
4558 shift = 3;
4559 t2 = TYPE_VALUES (t);
4560 break;
4561 case RECORD_TYPE:
4562 shift = 0;
4563 t2 = TYPE_FIELDS (t);
4564 break;
4565 case QUAL_UNION_TYPE:
4566 shift = 1;
4567 t2 = TYPE_FIELDS (t);
4568 break;
4569 case UNION_TYPE:
4570 shift = 2;
4571 t2 = TYPE_FIELDS (t);
4572 break;
4573 default:
4574 gcc_unreachable ();
4576 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4577 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4578 n_elements = list_length (t2);
4579 /* We might have a VLA here. */
4580 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4581 size = 0;
4582 else
4583 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4584 return ((size << 24) | (n_elements << shift));
4587 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4589 /* Return the typed-based alias set for T, which may be an expression
4590 or a type. Return -1 if we don't do anything special. */
4592 alias_set_type
4593 c_common_get_alias_set (tree t)
4595 tree u;
4596 PTR *slot;
4598 /* For VLAs, use the alias set of the element type rather than the
4599 default of alias set 0 for types compared structurally. */
4600 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4602 if (TREE_CODE (t) == ARRAY_TYPE)
4603 return get_alias_set (TREE_TYPE (t));
4604 return -1;
4607 /* Permit type-punning when accessing a union, provided the access
4608 is directly through the union. For example, this code does not
4609 permit taking the address of a union member and then storing
4610 through it. Even the type-punning allowed here is a GCC
4611 extension, albeit a common and useful one; the C standard says
4612 that such accesses have implementation-defined behavior. */
4613 for (u = t;
4614 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4615 u = TREE_OPERAND (u, 0))
4616 if (TREE_CODE (u) == COMPONENT_REF
4617 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4618 return 0;
4620 /* That's all the expressions we handle specially. */
4621 if (!TYPE_P (t))
4622 return -1;
4624 /* The C standard guarantees that any object may be accessed via an
4625 lvalue that has character type. */
4626 if (t == char_type_node
4627 || t == signed_char_type_node
4628 || t == unsigned_char_type_node)
4629 return 0;
4631 /* The C standard specifically allows aliasing between signed and
4632 unsigned variants of the same type. We treat the signed
4633 variant as canonical. */
4634 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4636 tree t1 = c_common_signed_type (t);
4638 /* t1 == t can happen for boolean nodes which are always unsigned. */
4639 if (t1 != t)
4640 return get_alias_set (t1);
4643 /* Handle the case of multiple type nodes referring to "the same" type,
4644 which occurs with IMA. These share an alias set. FIXME: Currently only
4645 C90 is handled. (In C99 type compatibility is not transitive, which
4646 complicates things mightily. The alias set splay trees can theoretically
4647 represent this, but insertion is tricky when you consider all the
4648 different orders things might arrive in.) */
4650 if (c_language != clk_c || flag_isoc99)
4651 return -1;
4653 /* Save time if there's only one input file. */
4654 if (num_in_fnames == 1)
4655 return -1;
4657 /* Pointers need special handling if they point to any type that
4658 needs special handling (below). */
4659 if (TREE_CODE (t) == POINTER_TYPE)
4661 tree t2;
4662 /* Find bottom type under any nested POINTERs. */
4663 for (t2 = TREE_TYPE (t);
4664 TREE_CODE (t2) == POINTER_TYPE;
4665 t2 = TREE_TYPE (t2))
4667 if (TREE_CODE (t2) != RECORD_TYPE
4668 && TREE_CODE (t2) != ENUMERAL_TYPE
4669 && TREE_CODE (t2) != QUAL_UNION_TYPE
4670 && TREE_CODE (t2) != UNION_TYPE)
4671 return -1;
4672 if (TYPE_SIZE (t2) == 0)
4673 return -1;
4675 /* These are the only cases that need special handling. */
4676 if (TREE_CODE (t) != RECORD_TYPE
4677 && TREE_CODE (t) != ENUMERAL_TYPE
4678 && TREE_CODE (t) != QUAL_UNION_TYPE
4679 && TREE_CODE (t) != UNION_TYPE
4680 && TREE_CODE (t) != POINTER_TYPE)
4681 return -1;
4682 /* Undefined? */
4683 if (TYPE_SIZE (t) == 0)
4684 return -1;
4686 /* Look up t in hash table. Only one of the compatible types within each
4687 alias set is recorded in the table. */
4688 if (!type_hash_table)
4689 type_hash_table = htab_create_ggc (1021, c_type_hash,
4690 (htab_eq) lang_hooks.types_compatible_p,
4691 NULL);
4692 slot = htab_find_slot (type_hash_table, t, INSERT);
4693 if (*slot != NULL)
4695 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4696 return TYPE_ALIAS_SET ((tree)*slot);
4698 else
4699 /* Our caller will assign and record (in t) a new alias set; all we need
4700 to do is remember t in the hash table. */
4701 *slot = t;
4703 return -1;
4706 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4707 the second parameter indicates which OPERATOR is being applied.
4708 The COMPLAIN flag controls whether we should diagnose possibly
4709 ill-formed constructs or not. LOC is the location of the SIZEOF or
4710 TYPEOF operator. */
4712 tree
4713 c_sizeof_or_alignof_type (location_t loc,
4714 tree type, bool is_sizeof, int complain)
4716 const char *op_name;
4717 tree value = NULL;
4718 enum tree_code type_code = TREE_CODE (type);
4720 op_name = is_sizeof ? "sizeof" : "__alignof__";
4722 if (type_code == FUNCTION_TYPE)
4724 if (is_sizeof)
4726 if (complain && (pedantic || warn_pointer_arith))
4727 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4728 "invalid application of %<sizeof%> to a function type");
4729 else if (!complain)
4730 return error_mark_node;
4731 value = size_one_node;
4733 else
4735 if (complain)
4737 if (c_dialect_cxx ())
4738 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4739 "%<alignof%> applied to a function type");
4740 else
4741 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4742 "%<_Alignof%> applied to a function type");
4744 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4747 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4749 if (type_code == VOID_TYPE
4750 && complain && (pedantic || warn_pointer_arith))
4751 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4752 "invalid application of %qs to a void type", op_name);
4753 else if (!complain)
4754 return error_mark_node;
4755 value = size_one_node;
4757 else if (!COMPLETE_TYPE_P (type)
4758 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4760 if (complain)
4761 error_at (loc, "invalid application of %qs to incomplete type %qT",
4762 op_name, type);
4763 return error_mark_node;
4765 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4766 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4768 if (complain)
4769 error_at (loc, "invalid application of %qs to array type %qT of "
4770 "incomplete element type", op_name, type);
4771 return error_mark_node;
4773 else
4775 if (is_sizeof)
4776 /* Convert in case a char is more than one unit. */
4777 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4778 size_int (TYPE_PRECISION (char_type_node)
4779 / BITS_PER_UNIT));
4780 else
4781 value = size_int (TYPE_ALIGN_UNIT (type));
4784 /* VALUE will have the middle-end integer type sizetype.
4785 However, we should really return a value of type `size_t',
4786 which is just a typedef for an ordinary integer type. */
4787 value = fold_convert_loc (loc, size_type_node, value);
4789 return value;
4792 /* Implement the __alignof keyword: Return the minimum required
4793 alignment of EXPR, measured in bytes. For VAR_DECLs,
4794 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4795 from an "aligned" __attribute__ specification). LOC is the
4796 location of the ALIGNOF operator. */
4798 tree
4799 c_alignof_expr (location_t loc, tree expr)
4801 tree t;
4803 if (VAR_OR_FUNCTION_DECL_P (expr))
4804 t = size_int (DECL_ALIGN_UNIT (expr));
4806 else if (TREE_CODE (expr) == COMPONENT_REF
4807 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4809 error_at (loc, "%<__alignof%> applied to a bit-field");
4810 t = size_one_node;
4812 else if (TREE_CODE (expr) == COMPONENT_REF
4813 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4814 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4816 else if (TREE_CODE (expr) == INDIRECT_REF)
4818 tree t = TREE_OPERAND (expr, 0);
4819 tree best = t;
4820 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4822 while (CONVERT_EXPR_P (t)
4823 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4825 int thisalign;
4827 t = TREE_OPERAND (t, 0);
4828 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4829 if (thisalign > bestalign)
4830 best = t, bestalign = thisalign;
4832 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4834 else
4835 return c_alignof (loc, TREE_TYPE (expr));
4837 return fold_convert_loc (loc, size_type_node, t);
4840 /* Handle C and C++ default attributes. */
4842 enum built_in_attribute
4844 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4845 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4846 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
4847 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4848 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4849 #include "builtin-attrs.def"
4850 #undef DEF_ATTR_NULL_TREE
4851 #undef DEF_ATTR_INT
4852 #undef DEF_ATTR_STRING
4853 #undef DEF_ATTR_IDENT
4854 #undef DEF_ATTR_TREE_LIST
4855 ATTR_LAST
4858 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4860 static void c_init_attributes (void);
4862 enum c_builtin_type
4864 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4865 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4866 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4867 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4868 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4869 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4870 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4871 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4872 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4873 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4874 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4875 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4876 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4877 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4878 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4879 NAME,
4880 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4881 #include "builtin-types.def"
4882 #undef DEF_PRIMITIVE_TYPE
4883 #undef DEF_FUNCTION_TYPE_0
4884 #undef DEF_FUNCTION_TYPE_1
4885 #undef DEF_FUNCTION_TYPE_2
4886 #undef DEF_FUNCTION_TYPE_3
4887 #undef DEF_FUNCTION_TYPE_4
4888 #undef DEF_FUNCTION_TYPE_5
4889 #undef DEF_FUNCTION_TYPE_6
4890 #undef DEF_FUNCTION_TYPE_7
4891 #undef DEF_FUNCTION_TYPE_VAR_0
4892 #undef DEF_FUNCTION_TYPE_VAR_1
4893 #undef DEF_FUNCTION_TYPE_VAR_2
4894 #undef DEF_FUNCTION_TYPE_VAR_3
4895 #undef DEF_FUNCTION_TYPE_VAR_4
4896 #undef DEF_FUNCTION_TYPE_VAR_5
4897 #undef DEF_POINTER_TYPE
4898 BT_LAST
4901 typedef enum c_builtin_type builtin_type;
4903 /* A temporary array for c_common_nodes_and_builtins. Used in
4904 communication with def_fn_type. */
4905 static tree builtin_types[(int) BT_LAST + 1];
4907 /* A helper function for c_common_nodes_and_builtins. Build function type
4908 for DEF with return type RET and N arguments. If VAR is true, then the
4909 function should be variadic after those N arguments.
4911 Takes special care not to ICE if any of the types involved are
4912 error_mark_node, which indicates that said type is not in fact available
4913 (see builtin_type_for_size). In which case the function type as a whole
4914 should be error_mark_node. */
4916 static void
4917 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4919 tree t;
4920 tree *args = XALLOCAVEC (tree, n);
4921 va_list list;
4922 int i;
4924 va_start (list, n);
4925 for (i = 0; i < n; ++i)
4927 builtin_type a = (builtin_type) va_arg (list, int);
4928 t = builtin_types[a];
4929 if (t == error_mark_node)
4930 goto egress;
4931 args[i] = t;
4934 t = builtin_types[ret];
4935 if (t == error_mark_node)
4936 goto egress;
4937 if (var)
4938 t = build_varargs_function_type_array (t, n, args);
4939 else
4940 t = build_function_type_array (t, n, args);
4942 egress:
4943 builtin_types[def] = t;
4944 va_end (list);
4947 /* Build builtin functions common to both C and C++ language
4948 frontends. */
4950 static void
4951 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4953 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4954 builtin_types[ENUM] = VALUE;
4955 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4956 def_fn_type (ENUM, RETURN, 0, 0);
4957 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4958 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4959 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4960 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4961 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4962 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4963 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4964 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4965 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4966 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4967 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4968 ARG6) \
4969 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4970 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4971 ARG6, ARG7) \
4972 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4973 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4974 def_fn_type (ENUM, RETURN, 1, 0);
4975 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4976 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4977 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4978 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4979 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4980 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4981 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4982 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4983 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4984 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4985 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4986 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4988 #include "builtin-types.def"
4990 #undef DEF_PRIMITIVE_TYPE
4991 #undef DEF_FUNCTION_TYPE_1
4992 #undef DEF_FUNCTION_TYPE_2
4993 #undef DEF_FUNCTION_TYPE_3
4994 #undef DEF_FUNCTION_TYPE_4
4995 #undef DEF_FUNCTION_TYPE_5
4996 #undef DEF_FUNCTION_TYPE_6
4997 #undef DEF_FUNCTION_TYPE_VAR_0
4998 #undef DEF_FUNCTION_TYPE_VAR_1
4999 #undef DEF_FUNCTION_TYPE_VAR_2
5000 #undef DEF_FUNCTION_TYPE_VAR_3
5001 #undef DEF_FUNCTION_TYPE_VAR_4
5002 #undef DEF_FUNCTION_TYPE_VAR_5
5003 #undef DEF_POINTER_TYPE
5004 builtin_types[(int) BT_LAST] = NULL_TREE;
5006 c_init_attributes ();
5008 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5009 NONANSI_P, ATTRS, IMPLICIT, COND) \
5010 if (NAME && COND) \
5011 def_builtin_1 (ENUM, NAME, CLASS, \
5012 builtin_types[(int) TYPE], \
5013 builtin_types[(int) LIBTYPE], \
5014 BOTH_P, FALLBACK_P, NONANSI_P, \
5015 built_in_attributes[(int) ATTRS], IMPLICIT);
5016 #include "builtins.def"
5017 #undef DEF_BUILTIN
5019 targetm.init_builtins ();
5021 build_common_builtin_nodes ();
5023 if (flag_mudflap)
5024 mudflap_init ();
5027 /* Like get_identifier, but avoid warnings about null arguments when
5028 the argument may be NULL for targets where GCC lacks stdint.h type
5029 information. */
5031 static inline tree
5032 c_get_ident (const char *id)
5034 return get_identifier (id);
5037 /* Build tree nodes and builtin functions common to both C and C++ language
5038 frontends. */
5040 void
5041 c_common_nodes_and_builtins (void)
5043 int char16_type_size;
5044 int char32_type_size;
5045 int wchar_type_size;
5046 tree array_domain_type;
5047 tree va_list_ref_type_node;
5048 tree va_list_arg_type_node;
5050 build_common_tree_nodes (flag_signed_char, flag_short_double);
5052 /* Define `int' and `char' first so that dbx will output them first. */
5053 record_builtin_type (RID_INT, NULL, integer_type_node);
5054 record_builtin_type (RID_CHAR, "char", char_type_node);
5056 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5057 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5058 but not C. Are the conditionals here needed? */
5059 if (c_dialect_cxx ())
5060 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5061 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5062 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5063 record_builtin_type (RID_MAX, "long unsigned int",
5064 long_unsigned_type_node);
5065 if (int128_integer_type_node != NULL_TREE)
5067 record_builtin_type (RID_INT128, "__int128",
5068 int128_integer_type_node);
5069 record_builtin_type (RID_MAX, "__int128 unsigned",
5070 int128_unsigned_type_node);
5072 if (c_dialect_cxx ())
5073 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5074 record_builtin_type (RID_MAX, "long long int",
5075 long_long_integer_type_node);
5076 record_builtin_type (RID_MAX, "long long unsigned int",
5077 long_long_unsigned_type_node);
5078 if (c_dialect_cxx ())
5079 record_builtin_type (RID_MAX, "long long unsigned",
5080 long_long_unsigned_type_node);
5081 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5082 record_builtin_type (RID_MAX, "short unsigned int",
5083 short_unsigned_type_node);
5084 if (c_dialect_cxx ())
5085 record_builtin_type (RID_MAX, "unsigned short",
5086 short_unsigned_type_node);
5088 /* Define both `signed char' and `unsigned char'. */
5089 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5090 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5092 /* These are types that c_common_type_for_size and
5093 c_common_type_for_mode use. */
5094 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5095 TYPE_DECL, NULL_TREE,
5096 intQI_type_node));
5097 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5098 TYPE_DECL, NULL_TREE,
5099 intHI_type_node));
5100 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5101 TYPE_DECL, NULL_TREE,
5102 intSI_type_node));
5103 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5104 TYPE_DECL, NULL_TREE,
5105 intDI_type_node));
5106 #if HOST_BITS_PER_WIDE_INT >= 64
5107 if (targetm.scalar_mode_supported_p (TImode))
5108 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5109 TYPE_DECL,
5110 get_identifier ("__int128_t"),
5111 intTI_type_node));
5112 #endif
5113 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5114 TYPE_DECL, NULL_TREE,
5115 unsigned_intQI_type_node));
5116 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5117 TYPE_DECL, NULL_TREE,
5118 unsigned_intHI_type_node));
5119 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5120 TYPE_DECL, NULL_TREE,
5121 unsigned_intSI_type_node));
5122 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5123 TYPE_DECL, NULL_TREE,
5124 unsigned_intDI_type_node));
5125 #if HOST_BITS_PER_WIDE_INT >= 64
5126 if (targetm.scalar_mode_supported_p (TImode))
5127 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5128 TYPE_DECL,
5129 get_identifier ("__uint128_t"),
5130 unsigned_intTI_type_node));
5131 #endif
5133 /* Create the widest literal types. */
5134 widest_integer_literal_type_node
5135 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5136 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5137 TYPE_DECL, NULL_TREE,
5138 widest_integer_literal_type_node));
5140 widest_unsigned_literal_type_node
5141 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5142 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5143 TYPE_DECL, NULL_TREE,
5144 widest_unsigned_literal_type_node));
5146 signed_size_type_node = c_common_signed_type (size_type_node);
5148 pid_type_node =
5149 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5151 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5152 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5153 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5155 /* Only supported decimal floating point extension if the target
5156 actually supports underlying modes. */
5157 if (targetm.scalar_mode_supported_p (SDmode)
5158 && targetm.scalar_mode_supported_p (DDmode)
5159 && targetm.scalar_mode_supported_p (TDmode))
5161 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5162 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5163 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5166 if (targetm.fixed_point_supported_p ())
5168 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5169 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5170 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5171 record_builtin_type (RID_MAX, "long long _Fract",
5172 long_long_fract_type_node);
5173 record_builtin_type (RID_MAX, "unsigned short _Fract",
5174 unsigned_short_fract_type_node);
5175 record_builtin_type (RID_MAX, "unsigned _Fract",
5176 unsigned_fract_type_node);
5177 record_builtin_type (RID_MAX, "unsigned long _Fract",
5178 unsigned_long_fract_type_node);
5179 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5180 unsigned_long_long_fract_type_node);
5181 record_builtin_type (RID_MAX, "_Sat short _Fract",
5182 sat_short_fract_type_node);
5183 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5184 record_builtin_type (RID_MAX, "_Sat long _Fract",
5185 sat_long_fract_type_node);
5186 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5187 sat_long_long_fract_type_node);
5188 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5189 sat_unsigned_short_fract_type_node);
5190 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5191 sat_unsigned_fract_type_node);
5192 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5193 sat_unsigned_long_fract_type_node);
5194 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5195 sat_unsigned_long_long_fract_type_node);
5196 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5197 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5198 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5199 record_builtin_type (RID_MAX, "long long _Accum",
5200 long_long_accum_type_node);
5201 record_builtin_type (RID_MAX, "unsigned short _Accum",
5202 unsigned_short_accum_type_node);
5203 record_builtin_type (RID_MAX, "unsigned _Accum",
5204 unsigned_accum_type_node);
5205 record_builtin_type (RID_MAX, "unsigned long _Accum",
5206 unsigned_long_accum_type_node);
5207 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5208 unsigned_long_long_accum_type_node);
5209 record_builtin_type (RID_MAX, "_Sat short _Accum",
5210 sat_short_accum_type_node);
5211 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5212 record_builtin_type (RID_MAX, "_Sat long _Accum",
5213 sat_long_accum_type_node);
5214 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5215 sat_long_long_accum_type_node);
5216 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5217 sat_unsigned_short_accum_type_node);
5218 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5219 sat_unsigned_accum_type_node);
5220 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5221 sat_unsigned_long_accum_type_node);
5222 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5223 sat_unsigned_long_long_accum_type_node);
5227 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5228 TYPE_DECL,
5229 get_identifier ("complex int"),
5230 complex_integer_type_node));
5231 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5232 TYPE_DECL,
5233 get_identifier ("complex float"),
5234 complex_float_type_node));
5235 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5236 TYPE_DECL,
5237 get_identifier ("complex double"),
5238 complex_double_type_node));
5239 lang_hooks.decls.pushdecl
5240 (build_decl (UNKNOWN_LOCATION,
5241 TYPE_DECL, get_identifier ("complex long double"),
5242 complex_long_double_type_node));
5244 if (c_dialect_cxx ())
5245 /* For C++, make fileptr_type_node a distinct void * type until
5246 FILE type is defined. */
5247 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5249 record_builtin_type (RID_VOID, NULL, void_type_node);
5251 /* Set the TYPE_NAME for any variants that were built before
5252 record_builtin_type gave names to the built-in types. */
5254 tree void_name = TYPE_NAME (void_type_node);
5255 TYPE_NAME (void_type_node) = NULL_TREE;
5256 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5257 = void_name;
5258 TYPE_NAME (void_type_node) = void_name;
5261 /* This node must not be shared. */
5262 void_zero_node = make_node (INTEGER_CST);
5263 TREE_TYPE (void_zero_node) = void_type_node;
5265 void_list_node = build_void_list_node ();
5267 /* Make a type to be the domain of a few array types
5268 whose domains don't really matter.
5269 200 is small enough that it always fits in size_t
5270 and large enough that it can hold most function names for the
5271 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5272 array_domain_type = build_index_type (size_int (200));
5274 /* Make a type for arrays of characters.
5275 With luck nothing will ever really depend on the length of this
5276 array type. */
5277 char_array_type_node
5278 = build_array_type (char_type_node, array_domain_type);
5280 /* Likewise for arrays of ints. */
5281 int_array_type_node
5282 = build_array_type (integer_type_node, array_domain_type);
5284 string_type_node = build_pointer_type (char_type_node);
5285 const_string_type_node
5286 = build_pointer_type (build_qualified_type
5287 (char_type_node, TYPE_QUAL_CONST));
5289 /* This is special for C++ so functions can be overloaded. */
5290 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5291 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5292 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5293 underlying_wchar_type_node = wchar_type_node;
5294 if (c_dialect_cxx ())
5296 if (TYPE_UNSIGNED (wchar_type_node))
5297 wchar_type_node = make_unsigned_type (wchar_type_size);
5298 else
5299 wchar_type_node = make_signed_type (wchar_type_size);
5300 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5303 /* This is for wide string constants. */
5304 wchar_array_type_node
5305 = build_array_type (wchar_type_node, array_domain_type);
5307 /* Define 'char16_t'. */
5308 char16_type_node = get_identifier (CHAR16_TYPE);
5309 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5310 char16_type_size = TYPE_PRECISION (char16_type_node);
5311 if (c_dialect_cxx ())
5313 char16_type_node = make_unsigned_type (char16_type_size);
5315 if (cxx_dialect >= cxx0x)
5316 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5319 /* This is for UTF-16 string constants. */
5320 char16_array_type_node
5321 = build_array_type (char16_type_node, array_domain_type);
5323 /* Define 'char32_t'. */
5324 char32_type_node = get_identifier (CHAR32_TYPE);
5325 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5326 char32_type_size = TYPE_PRECISION (char32_type_node);
5327 if (c_dialect_cxx ())
5329 char32_type_node = make_unsigned_type (char32_type_size);
5331 if (cxx_dialect >= cxx0x)
5332 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5335 /* This is for UTF-32 string constants. */
5336 char32_array_type_node
5337 = build_array_type (char32_type_node, array_domain_type);
5339 wint_type_node =
5340 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5342 intmax_type_node =
5343 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5344 uintmax_type_node =
5345 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5347 if (SIG_ATOMIC_TYPE)
5348 sig_atomic_type_node =
5349 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5350 if (INT8_TYPE)
5351 int8_type_node =
5352 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5353 if (INT16_TYPE)
5354 int16_type_node =
5355 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5356 if (INT32_TYPE)
5357 int32_type_node =
5358 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5359 if (INT64_TYPE)
5360 int64_type_node =
5361 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5362 if (UINT8_TYPE)
5363 uint8_type_node =
5364 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5365 if (UINT16_TYPE)
5366 c_uint16_type_node =
5367 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5368 if (UINT32_TYPE)
5369 c_uint32_type_node =
5370 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5371 if (UINT64_TYPE)
5372 c_uint64_type_node =
5373 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5374 if (INT_LEAST8_TYPE)
5375 int_least8_type_node =
5376 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5377 if (INT_LEAST16_TYPE)
5378 int_least16_type_node =
5379 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5380 if (INT_LEAST32_TYPE)
5381 int_least32_type_node =
5382 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5383 if (INT_LEAST64_TYPE)
5384 int_least64_type_node =
5385 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5386 if (UINT_LEAST8_TYPE)
5387 uint_least8_type_node =
5388 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5389 if (UINT_LEAST16_TYPE)
5390 uint_least16_type_node =
5391 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5392 if (UINT_LEAST32_TYPE)
5393 uint_least32_type_node =
5394 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5395 if (UINT_LEAST64_TYPE)
5396 uint_least64_type_node =
5397 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5398 if (INT_FAST8_TYPE)
5399 int_fast8_type_node =
5400 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5401 if (INT_FAST16_TYPE)
5402 int_fast16_type_node =
5403 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5404 if (INT_FAST32_TYPE)
5405 int_fast32_type_node =
5406 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5407 if (INT_FAST64_TYPE)
5408 int_fast64_type_node =
5409 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5410 if (UINT_FAST8_TYPE)
5411 uint_fast8_type_node =
5412 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5413 if (UINT_FAST16_TYPE)
5414 uint_fast16_type_node =
5415 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5416 if (UINT_FAST32_TYPE)
5417 uint_fast32_type_node =
5418 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5419 if (UINT_FAST64_TYPE)
5420 uint_fast64_type_node =
5421 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5422 if (INTPTR_TYPE)
5423 intptr_type_node =
5424 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5425 if (UINTPTR_TYPE)
5426 uintptr_type_node =
5427 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5429 default_function_type
5430 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5431 ptrdiff_type_node
5432 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5433 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5435 lang_hooks.decls.pushdecl
5436 (build_decl (UNKNOWN_LOCATION,
5437 TYPE_DECL, get_identifier ("__builtin_va_list"),
5438 va_list_type_node));
5439 if (targetm.enum_va_list_p)
5441 int l;
5442 const char *pname;
5443 tree ptype;
5445 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5447 lang_hooks.decls.pushdecl
5448 (build_decl (UNKNOWN_LOCATION,
5449 TYPE_DECL, get_identifier (pname),
5450 ptype));
5455 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5457 va_list_arg_type_node = va_list_ref_type_node =
5458 build_pointer_type (TREE_TYPE (va_list_type_node));
5460 else
5462 va_list_arg_type_node = va_list_type_node;
5463 va_list_ref_type_node = build_reference_type (va_list_type_node);
5466 if (!flag_preprocess_only)
5467 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5469 main_identifier_node = get_identifier ("main");
5471 /* Create the built-in __null node. It is important that this is
5472 not shared. */
5473 null_node = make_node (INTEGER_CST);
5474 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5476 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5477 memset (builtin_types, 0, sizeof (builtin_types));
5480 /* The number of named compound-literals generated thus far. */
5481 static GTY(()) int compound_literal_number;
5483 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5485 void
5486 set_compound_literal_name (tree decl)
5488 char *name;
5489 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5490 compound_literal_number);
5491 compound_literal_number++;
5492 DECL_NAME (decl) = get_identifier (name);
5495 tree
5496 build_va_arg (location_t loc, tree expr, tree type)
5498 expr = build1 (VA_ARG_EXPR, type, expr);
5499 SET_EXPR_LOCATION (expr, loc);
5500 return expr;
5504 /* Linked list of disabled built-in functions. */
5506 typedef struct disabled_builtin
5508 const char *name;
5509 struct disabled_builtin *next;
5510 } disabled_builtin;
5511 static disabled_builtin *disabled_builtins = NULL;
5513 static bool builtin_function_disabled_p (const char *);
5515 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5516 begins with "__builtin_", give an error. */
5518 void
5519 disable_builtin_function (const char *name)
5521 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5522 error ("cannot disable built-in function %qs", name);
5523 else
5525 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5526 new_disabled_builtin->name = name;
5527 new_disabled_builtin->next = disabled_builtins;
5528 disabled_builtins = new_disabled_builtin;
5533 /* Return true if the built-in function NAME has been disabled, false
5534 otherwise. */
5536 static bool
5537 builtin_function_disabled_p (const char *name)
5539 disabled_builtin *p;
5540 for (p = disabled_builtins; p != NULL; p = p->next)
5542 if (strcmp (name, p->name) == 0)
5543 return true;
5545 return false;
5549 /* Worker for DEF_BUILTIN.
5550 Possibly define a builtin function with one or two names.
5551 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5552 nonansi_p and flag_no_nonansi_builtin. */
5554 static void
5555 def_builtin_1 (enum built_in_function fncode,
5556 const char *name,
5557 enum built_in_class fnclass,
5558 tree fntype, tree libtype,
5559 bool both_p, bool fallback_p, bool nonansi_p,
5560 tree fnattrs, bool implicit_p)
5562 tree decl;
5563 const char *libname;
5565 if (fntype == error_mark_node)
5566 return;
5568 gcc_assert ((!both_p && !fallback_p)
5569 || !strncmp (name, "__builtin_",
5570 strlen ("__builtin_")));
5572 libname = name + strlen ("__builtin_");
5573 decl = add_builtin_function (name, fntype, fncode, fnclass,
5574 (fallback_p ? libname : NULL),
5575 fnattrs);
5577 set_builtin_decl (fncode, decl, implicit_p);
5579 if (both_p
5580 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5581 && !(nonansi_p && flag_no_nonansi_builtin))
5582 add_builtin_function (libname, libtype, fncode, fnclass,
5583 NULL, fnattrs);
5586 /* Nonzero if the type T promotes to int. This is (nearly) the
5587 integral promotions defined in ISO C99 6.3.1.1/2. */
5589 bool
5590 c_promoting_integer_type_p (const_tree t)
5592 switch (TREE_CODE (t))
5594 case INTEGER_TYPE:
5595 return (TYPE_MAIN_VARIANT (t) == char_type_node
5596 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5597 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5598 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5599 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5600 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5602 case ENUMERAL_TYPE:
5603 /* ??? Technically all enumerations not larger than an int
5604 promote to an int. But this is used along code paths
5605 that only want to notice a size change. */
5606 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5608 case BOOLEAN_TYPE:
5609 return 1;
5611 default:
5612 return 0;
5616 /* Return 1 if PARMS specifies a fixed number of parameters
5617 and none of their types is affected by default promotions. */
5620 self_promoting_args_p (const_tree parms)
5622 const_tree t;
5623 for (t = parms; t; t = TREE_CHAIN (t))
5625 tree type = TREE_VALUE (t);
5627 if (type == error_mark_node)
5628 continue;
5630 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5631 return 0;
5633 if (type == 0)
5634 return 0;
5636 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5637 return 0;
5639 if (c_promoting_integer_type_p (type))
5640 return 0;
5642 return 1;
5645 /* Recursively remove any '*' or '&' operator from TYPE. */
5646 tree
5647 strip_pointer_operator (tree t)
5649 while (POINTER_TYPE_P (t))
5650 t = TREE_TYPE (t);
5651 return t;
5654 /* Recursively remove pointer or array type from TYPE. */
5655 tree
5656 strip_pointer_or_array_types (tree t)
5658 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5659 t = TREE_TYPE (t);
5660 return t;
5663 /* Used to compare case labels. K1 and K2 are actually tree nodes
5664 representing case labels, or NULL_TREE for a `default' label.
5665 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5666 K2, and 0 if K1 and K2 are equal. */
5669 case_compare (splay_tree_key k1, splay_tree_key k2)
5671 /* Consider a NULL key (such as arises with a `default' label) to be
5672 smaller than anything else. */
5673 if (!k1)
5674 return k2 ? -1 : 0;
5675 else if (!k2)
5676 return k1 ? 1 : 0;
5678 return tree_int_cst_compare ((tree) k1, (tree) k2);
5681 /* Process a case label, located at LOC, for the range LOW_VALUE
5682 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5683 then this case label is actually a `default' label. If only
5684 HIGH_VALUE is NULL_TREE, then case label was declared using the
5685 usual C/C++ syntax, rather than the GNU case range extension.
5686 CASES is a tree containing all the case ranges processed so far;
5687 COND is the condition for the switch-statement itself. Returns the
5688 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5689 is created. */
5691 tree
5692 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5693 tree low_value, tree high_value)
5695 tree type;
5696 tree label;
5697 tree case_label;
5698 splay_tree_node node;
5700 /* Create the LABEL_DECL itself. */
5701 label = create_artificial_label (loc);
5703 /* If there was an error processing the switch condition, bail now
5704 before we get more confused. */
5705 if (!cond || cond == error_mark_node)
5706 goto error_out;
5708 if ((low_value && TREE_TYPE (low_value)
5709 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5710 || (high_value && TREE_TYPE (high_value)
5711 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5713 error_at (loc, "pointers are not permitted as case values");
5714 goto error_out;
5717 /* Case ranges are a GNU extension. */
5718 if (high_value)
5719 pedwarn (loc, OPT_Wpedantic,
5720 "range expressions in switch statements are non-standard");
5722 type = TREE_TYPE (cond);
5723 if (low_value)
5725 low_value = check_case_value (low_value);
5726 low_value = convert_and_check (type, low_value);
5727 if (low_value == error_mark_node)
5728 goto error_out;
5730 if (high_value)
5732 high_value = check_case_value (high_value);
5733 high_value = convert_and_check (type, high_value);
5734 if (high_value == error_mark_node)
5735 goto error_out;
5738 if (low_value && high_value)
5740 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5741 really a case range, even though it was written that way.
5742 Remove the HIGH_VALUE to simplify later processing. */
5743 if (tree_int_cst_equal (low_value, high_value))
5744 high_value = NULL_TREE;
5745 else if (!tree_int_cst_lt (low_value, high_value))
5746 warning_at (loc, 0, "empty range specified");
5749 /* See if the case is in range of the type of the original testing
5750 expression. If both low_value and high_value are out of range,
5751 don't insert the case label and return NULL_TREE. */
5752 if (low_value
5753 && !check_case_bounds (type, orig_type,
5754 &low_value, high_value ? &high_value : NULL))
5755 return NULL_TREE;
5757 /* Look up the LOW_VALUE in the table of case labels we already
5758 have. */
5759 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5760 /* If there was not an exact match, check for overlapping ranges.
5761 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5762 that's a `default' label and the only overlap is an exact match. */
5763 if (!node && (low_value || high_value))
5765 splay_tree_node low_bound;
5766 splay_tree_node high_bound;
5768 /* Even though there wasn't an exact match, there might be an
5769 overlap between this case range and another case range.
5770 Since we've (inductively) not allowed any overlapping case
5771 ranges, we simply need to find the greatest low case label
5772 that is smaller that LOW_VALUE, and the smallest low case
5773 label that is greater than LOW_VALUE. If there is an overlap
5774 it will occur in one of these two ranges. */
5775 low_bound = splay_tree_predecessor (cases,
5776 (splay_tree_key) low_value);
5777 high_bound = splay_tree_successor (cases,
5778 (splay_tree_key) low_value);
5780 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5781 the LOW_VALUE, so there is no need to check unless the
5782 LOW_BOUND is in fact itself a case range. */
5783 if (low_bound
5784 && CASE_HIGH ((tree) low_bound->value)
5785 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5786 low_value) >= 0)
5787 node = low_bound;
5788 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5789 range is bigger than the low end of the current range, so we
5790 are only interested if the current range is a real range, and
5791 not an ordinary case label. */
5792 else if (high_bound
5793 && high_value
5794 && (tree_int_cst_compare ((tree) high_bound->key,
5795 high_value)
5796 <= 0))
5797 node = high_bound;
5799 /* If there was an overlap, issue an error. */
5800 if (node)
5802 tree duplicate = CASE_LABEL ((tree) node->value);
5804 if (high_value)
5806 error_at (loc, "duplicate (or overlapping) case value");
5807 error_at (DECL_SOURCE_LOCATION (duplicate),
5808 "this is the first entry overlapping that value");
5810 else if (low_value)
5812 error_at (loc, "duplicate case value") ;
5813 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5815 else
5817 error_at (loc, "multiple default labels in one switch");
5818 error_at (DECL_SOURCE_LOCATION (duplicate),
5819 "this is the first default label");
5821 goto error_out;
5824 /* Add a CASE_LABEL to the statement-tree. */
5825 case_label = add_stmt (build_case_label (low_value, high_value, label));
5826 /* Register this case label in the splay tree. */
5827 splay_tree_insert (cases,
5828 (splay_tree_key) low_value,
5829 (splay_tree_value) case_label);
5831 return case_label;
5833 error_out:
5834 /* Add a label so that the back-end doesn't think that the beginning of
5835 the switch is unreachable. Note that we do not add a case label, as
5836 that just leads to duplicates and thence to failure later on. */
5837 if (!cases->root)
5839 tree t = create_artificial_label (loc);
5840 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5842 return error_mark_node;
5845 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5846 Used to verify that case values match up with enumerator values. */
5848 static void
5849 match_case_to_enum_1 (tree key, tree type, tree label)
5851 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5853 /* ??? Not working too hard to print the double-word value.
5854 Should perhaps be done with %lwd in the diagnostic routines? */
5855 if (TREE_INT_CST_HIGH (key) == 0)
5856 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5857 TREE_INT_CST_LOW (key));
5858 else if (!TYPE_UNSIGNED (type)
5859 && TREE_INT_CST_HIGH (key) == -1
5860 && TREE_INT_CST_LOW (key) != 0)
5861 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
5862 -TREE_INT_CST_LOW (key));
5863 else
5864 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5865 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5866 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
5868 if (TYPE_NAME (type) == 0)
5869 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5870 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5871 "case value %qs not in enumerated type",
5872 buf);
5873 else
5874 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5875 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5876 "case value %qs not in enumerated type %qT",
5877 buf, type);
5880 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
5881 Used to verify that case values match up with enumerator values. */
5883 static int
5884 match_case_to_enum (splay_tree_node node, void *data)
5886 tree label = (tree) node->value;
5887 tree type = (tree) data;
5889 /* Skip default case. */
5890 if (!CASE_LOW (label))
5891 return 0;
5893 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
5894 when we did our enum->case scan. Reset our scratch bit after. */
5895 if (!CASE_LOW_SEEN (label))
5896 match_case_to_enum_1 (CASE_LOW (label), type, label);
5897 else
5898 CASE_LOW_SEEN (label) = 0;
5900 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
5901 not set, that means that CASE_HIGH did not appear when we did our
5902 enum->case scan. Reset our scratch bit after. */
5903 if (CASE_HIGH (label))
5905 if (!CASE_HIGH_SEEN (label))
5906 match_case_to_enum_1 (CASE_HIGH (label), type, label);
5907 else
5908 CASE_HIGH_SEEN (label) = 0;
5911 return 0;
5914 /* Handle -Wswitch*. Called from the front end after parsing the
5915 switch construct. */
5916 /* ??? Should probably be somewhere generic, since other languages
5917 besides C and C++ would want this. At the moment, however, C/C++
5918 are the only tree-ssa languages that support enumerations at all,
5919 so the point is moot. */
5921 void
5922 c_do_switch_warnings (splay_tree cases, location_t switch_location,
5923 tree type, tree cond)
5925 splay_tree_node default_node;
5926 splay_tree_node node;
5927 tree chain;
5929 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5930 return;
5932 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
5933 if (!default_node)
5934 warning_at (switch_location, OPT_Wswitch_default,
5935 "switch missing default case");
5937 /* From here on, we only care about about enumerated types. */
5938 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
5939 return;
5941 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
5942 if (!warn_switch_enum && !warn_switch)
5943 return;
5945 /* Check the cases. Warn about case values which are not members of
5946 the enumerated type. For -Wswitch-enum, or for -Wswitch when
5947 there is no default case, check that exactly all enumeration
5948 literals are covered by the cases. */
5950 /* Clearing COND if it is not an integer constant simplifies
5951 the tests inside the loop below. */
5952 if (TREE_CODE (cond) != INTEGER_CST)
5953 cond = NULL_TREE;
5955 /* The time complexity here is O(N*lg(N)) worst case, but for the
5956 common case of monotonically increasing enumerators, it is
5957 O(N), since the nature of the splay tree will keep the next
5958 element adjacent to the root at all times. */
5960 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
5962 tree value = TREE_VALUE (chain);
5963 if (TREE_CODE (value) == CONST_DECL)
5964 value = DECL_INITIAL (value);
5965 node = splay_tree_lookup (cases, (splay_tree_key) value);
5966 if (node)
5968 /* Mark the CASE_LOW part of the case entry as seen. */
5969 tree label = (tree) node->value;
5970 CASE_LOW_SEEN (label) = 1;
5971 continue;
5974 /* Even though there wasn't an exact match, there might be a
5975 case range which includes the enumerator's value. */
5976 node = splay_tree_predecessor (cases, (splay_tree_key) value);
5977 if (node && CASE_HIGH ((tree) node->value))
5979 tree label = (tree) node->value;
5980 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
5981 if (cmp >= 0)
5983 /* If we match the upper bound exactly, mark the CASE_HIGH
5984 part of the case entry as seen. */
5985 if (cmp == 0)
5986 CASE_HIGH_SEEN (label) = 1;
5987 continue;
5991 /* We've now determined that this enumerated literal isn't
5992 handled by the case labels of the switch statement. */
5994 /* If the switch expression is a constant, we only really care
5995 about whether that constant is handled by the switch. */
5996 if (cond && tree_int_cst_compare (cond, value))
5997 continue;
5999 /* If there is a default_node, the only relevant option is
6000 Wswitch-enum. Otherwise, if both are enabled then we prefer
6001 to warn using -Wswitch because -Wswitch is enabled by -Wall
6002 while -Wswitch-enum is explicit. */
6003 warning_at (switch_location,
6004 (default_node || !warn_switch
6005 ? OPT_Wswitch_enum
6006 : OPT_Wswitch),
6007 "enumeration value %qE not handled in switch",
6008 TREE_PURPOSE (chain));
6011 /* Warn if there are case expressions that don't correspond to
6012 enumerators. This can occur since C and C++ don't enforce
6013 type-checking of assignments to enumeration variables.
6015 The time complexity here is now always O(N) worst case, since
6016 we should have marked both the lower bound and upper bound of
6017 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6018 above. This scan also resets those fields. */
6020 splay_tree_foreach (cases, match_case_to_enum, type);
6023 /* Finish an expression taking the address of LABEL (an
6024 IDENTIFIER_NODE). Returns an expression for the address.
6026 LOC is the location for the expression returned. */
6028 tree
6029 finish_label_address_expr (tree label, location_t loc)
6031 tree result;
6033 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6035 if (label == error_mark_node)
6036 return error_mark_node;
6038 label = lookup_label (label);
6039 if (label == NULL_TREE)
6040 result = null_pointer_node;
6041 else
6043 TREE_USED (label) = 1;
6044 result = build1 (ADDR_EXPR, ptr_type_node, label);
6045 /* The current function is not necessarily uninlinable.
6046 Computed gotos are incompatible with inlining, but the value
6047 here could be used only in a diagnostic, for example. */
6048 protected_set_expr_location (result, loc);
6051 return result;
6055 /* Given a boolean expression ARG, return a tree representing an increment
6056 or decrement (as indicated by CODE) of ARG. The front end must check for
6057 invalid cases (e.g., decrement in C++). */
6058 tree
6059 boolean_increment (enum tree_code code, tree arg)
6061 tree val;
6062 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6064 arg = stabilize_reference (arg);
6065 switch (code)
6067 case PREINCREMENT_EXPR:
6068 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6069 break;
6070 case POSTINCREMENT_EXPR:
6071 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6072 arg = save_expr (arg);
6073 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6074 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6075 break;
6076 case PREDECREMENT_EXPR:
6077 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6078 invert_truthvalue_loc (input_location, arg));
6079 break;
6080 case POSTDECREMENT_EXPR:
6081 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6082 invert_truthvalue_loc (input_location, arg));
6083 arg = save_expr (arg);
6084 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6085 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6086 break;
6087 default:
6088 gcc_unreachable ();
6090 TREE_SIDE_EFFECTS (val) = 1;
6091 return val;
6094 /* Built-in macros for stddef.h and stdint.h, that require macros
6095 defined in this file. */
6096 void
6097 c_stddef_cpp_builtins(void)
6099 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6100 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6101 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6102 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6103 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6104 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6105 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6106 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6107 if (SIG_ATOMIC_TYPE)
6108 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6109 if (INT8_TYPE)
6110 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6111 if (INT16_TYPE)
6112 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6113 if (INT32_TYPE)
6114 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6115 if (INT64_TYPE)
6116 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6117 if (UINT8_TYPE)
6118 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6119 if (UINT16_TYPE)
6120 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6121 if (UINT32_TYPE)
6122 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6123 if (UINT64_TYPE)
6124 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6125 if (INT_LEAST8_TYPE)
6126 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6127 if (INT_LEAST16_TYPE)
6128 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6129 if (INT_LEAST32_TYPE)
6130 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6131 if (INT_LEAST64_TYPE)
6132 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6133 if (UINT_LEAST8_TYPE)
6134 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6135 if (UINT_LEAST16_TYPE)
6136 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6137 if (UINT_LEAST32_TYPE)
6138 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6139 if (UINT_LEAST64_TYPE)
6140 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6141 if (INT_FAST8_TYPE)
6142 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6143 if (INT_FAST16_TYPE)
6144 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6145 if (INT_FAST32_TYPE)
6146 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6147 if (INT_FAST64_TYPE)
6148 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6149 if (UINT_FAST8_TYPE)
6150 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6151 if (UINT_FAST16_TYPE)
6152 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6153 if (UINT_FAST32_TYPE)
6154 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6155 if (UINT_FAST64_TYPE)
6156 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6157 if (INTPTR_TYPE)
6158 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6159 if (UINTPTR_TYPE)
6160 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6163 static void
6164 c_init_attributes (void)
6166 /* Fill in the built_in_attributes array. */
6167 #define DEF_ATTR_NULL_TREE(ENUM) \
6168 built_in_attributes[(int) ENUM] = NULL_TREE;
6169 #define DEF_ATTR_INT(ENUM, VALUE) \
6170 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6171 #define DEF_ATTR_STRING(ENUM, VALUE) \
6172 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6173 #define DEF_ATTR_IDENT(ENUM, STRING) \
6174 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6175 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6176 built_in_attributes[(int) ENUM] \
6177 = tree_cons (built_in_attributes[(int) PURPOSE], \
6178 built_in_attributes[(int) VALUE], \
6179 built_in_attributes[(int) CHAIN]);
6180 #include "builtin-attrs.def"
6181 #undef DEF_ATTR_NULL_TREE
6182 #undef DEF_ATTR_INT
6183 #undef DEF_ATTR_IDENT
6184 #undef DEF_ATTR_TREE_LIST
6187 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6188 identifier as an argument, so the front end shouldn't look it up. */
6190 bool
6191 attribute_takes_identifier_p (const_tree attr_id)
6193 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6194 if (spec == NULL)
6195 /* Unknown attribute that we'll end up ignoring, return true so we
6196 don't complain about an identifier argument. */
6197 return true;
6198 else if (!strcmp ("mode", spec->name)
6199 || !strcmp ("format", spec->name)
6200 || !strcmp ("cleanup", spec->name))
6201 return true;
6202 else
6203 return targetm.attribute_takes_identifier_p (attr_id);
6206 /* Attribute handlers common to C front ends. */
6208 /* Handle a "packed" attribute; arguments as in
6209 struct attribute_spec.handler. */
6211 static tree
6212 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6213 int flags, bool *no_add_attrs)
6215 if (TYPE_P (*node))
6217 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6218 *node = build_variant_type_copy (*node);
6219 TYPE_PACKED (*node) = 1;
6221 else if (TREE_CODE (*node) == FIELD_DECL)
6223 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6224 /* Still pack bitfields. */
6225 && ! DECL_INITIAL (*node))
6226 warning (OPT_Wattributes,
6227 "%qE attribute ignored for field of type %qT",
6228 name, TREE_TYPE (*node));
6229 else
6230 DECL_PACKED (*node) = 1;
6232 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6233 used for DECL_REGISTER. It wouldn't mean anything anyway.
6234 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6235 that changes what the typedef is typing. */
6236 else
6238 warning (OPT_Wattributes, "%qE attribute ignored", name);
6239 *no_add_attrs = true;
6242 return NULL_TREE;
6245 /* Handle a "nocommon" attribute; arguments as in
6246 struct attribute_spec.handler. */
6248 static tree
6249 handle_nocommon_attribute (tree *node, tree name,
6250 tree ARG_UNUSED (args),
6251 int ARG_UNUSED (flags), bool *no_add_attrs)
6253 if (TREE_CODE (*node) == VAR_DECL)
6254 DECL_COMMON (*node) = 0;
6255 else
6257 warning (OPT_Wattributes, "%qE attribute ignored", name);
6258 *no_add_attrs = true;
6261 return NULL_TREE;
6264 /* Handle a "common" attribute; arguments as in
6265 struct attribute_spec.handler. */
6267 static tree
6268 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6269 int ARG_UNUSED (flags), bool *no_add_attrs)
6271 if (TREE_CODE (*node) == VAR_DECL)
6272 DECL_COMMON (*node) = 1;
6273 else
6275 warning (OPT_Wattributes, "%qE attribute ignored", name);
6276 *no_add_attrs = true;
6279 return NULL_TREE;
6282 /* Handle a "noreturn" attribute; arguments as in
6283 struct attribute_spec.handler. */
6285 static tree
6286 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6287 int ARG_UNUSED (flags), bool *no_add_attrs)
6289 tree type = TREE_TYPE (*node);
6291 /* See FIXME comment in c_common_attribute_table. */
6292 if (TREE_CODE (*node) == FUNCTION_DECL
6293 || objc_method_decl (TREE_CODE (*node)))
6294 TREE_THIS_VOLATILE (*node) = 1;
6295 else if (TREE_CODE (type) == POINTER_TYPE
6296 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6297 TREE_TYPE (*node)
6298 = build_pointer_type
6299 (build_type_variant (TREE_TYPE (type),
6300 TYPE_READONLY (TREE_TYPE (type)), 1));
6301 else
6303 warning (OPT_Wattributes, "%qE attribute ignored", name);
6304 *no_add_attrs = true;
6307 return NULL_TREE;
6310 /* Handle a "hot" and attribute; arguments as in
6311 struct attribute_spec.handler. */
6313 static tree
6314 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6315 int ARG_UNUSED (flags), bool *no_add_attrs)
6317 if (TREE_CODE (*node) == FUNCTION_DECL
6318 || TREE_CODE (*node) == LABEL_DECL)
6320 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6322 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6323 name, "cold");
6324 *no_add_attrs = true;
6326 /* Most of the rest of the hot processing is done later with
6327 lookup_attribute. */
6329 else
6331 warning (OPT_Wattributes, "%qE attribute ignored", name);
6332 *no_add_attrs = true;
6335 return NULL_TREE;
6338 /* Handle a "cold" and attribute; arguments as in
6339 struct attribute_spec.handler. */
6341 static tree
6342 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6343 int ARG_UNUSED (flags), bool *no_add_attrs)
6345 if (TREE_CODE (*node) == FUNCTION_DECL
6346 || TREE_CODE (*node) == LABEL_DECL)
6348 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6350 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6351 name, "hot");
6352 *no_add_attrs = true;
6354 /* Most of the rest of the cold processing is done later with
6355 lookup_attribute. */
6357 else
6359 warning (OPT_Wattributes, "%qE attribute ignored", name);
6360 *no_add_attrs = true;
6363 return NULL_TREE;
6366 /* Handle a "noinline" attribute; arguments as in
6367 struct attribute_spec.handler. */
6369 static tree
6370 handle_noinline_attribute (tree *node, tree name,
6371 tree ARG_UNUSED (args),
6372 int ARG_UNUSED (flags), bool *no_add_attrs)
6374 if (TREE_CODE (*node) == FUNCTION_DECL)
6375 DECL_UNINLINABLE (*node) = 1;
6376 else
6378 warning (OPT_Wattributes, "%qE attribute ignored", name);
6379 *no_add_attrs = true;
6382 return NULL_TREE;
6385 /* Handle a "noclone" attribute; arguments as in
6386 struct attribute_spec.handler. */
6388 static tree
6389 handle_noclone_attribute (tree *node, tree name,
6390 tree ARG_UNUSED (args),
6391 int ARG_UNUSED (flags), bool *no_add_attrs)
6393 if (TREE_CODE (*node) != FUNCTION_DECL)
6395 warning (OPT_Wattributes, "%qE attribute ignored", name);
6396 *no_add_attrs = true;
6399 return NULL_TREE;
6402 /* Handle a "always_inline" attribute; arguments as in
6403 struct attribute_spec.handler. */
6405 static tree
6406 handle_always_inline_attribute (tree *node, tree name,
6407 tree ARG_UNUSED (args),
6408 int ARG_UNUSED (flags),
6409 bool *no_add_attrs)
6411 if (TREE_CODE (*node) == FUNCTION_DECL)
6413 /* Set the attribute and mark it for disregarding inline
6414 limits. */
6415 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6417 else
6419 warning (OPT_Wattributes, "%qE attribute ignored", name);
6420 *no_add_attrs = true;
6423 return NULL_TREE;
6426 /* Handle a "gnu_inline" attribute; arguments as in
6427 struct attribute_spec.handler. */
6429 static tree
6430 handle_gnu_inline_attribute (tree *node, tree name,
6431 tree ARG_UNUSED (args),
6432 int ARG_UNUSED (flags),
6433 bool *no_add_attrs)
6435 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6437 /* Do nothing else, just set the attribute. We'll get at
6438 it later with lookup_attribute. */
6440 else
6442 warning (OPT_Wattributes, "%qE attribute ignored", name);
6443 *no_add_attrs = true;
6446 return NULL_TREE;
6449 /* Handle a "leaf" attribute; arguments as in
6450 struct attribute_spec.handler. */
6452 static tree
6453 handle_leaf_attribute (tree *node, tree name,
6454 tree ARG_UNUSED (args),
6455 int ARG_UNUSED (flags), bool *no_add_attrs)
6457 if (TREE_CODE (*node) != FUNCTION_DECL)
6459 warning (OPT_Wattributes, "%qE attribute ignored", name);
6460 *no_add_attrs = true;
6462 if (!TREE_PUBLIC (*node))
6464 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6465 *no_add_attrs = true;
6468 return NULL_TREE;
6471 /* Handle an "artificial" attribute; arguments as in
6472 struct attribute_spec.handler. */
6474 static tree
6475 handle_artificial_attribute (tree *node, tree name,
6476 tree ARG_UNUSED (args),
6477 int ARG_UNUSED (flags),
6478 bool *no_add_attrs)
6480 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6482 /* Do nothing else, just set the attribute. We'll get at
6483 it later with lookup_attribute. */
6485 else
6487 warning (OPT_Wattributes, "%qE attribute ignored", name);
6488 *no_add_attrs = true;
6491 return NULL_TREE;
6494 /* Handle a "flatten" attribute; arguments as in
6495 struct attribute_spec.handler. */
6497 static tree
6498 handle_flatten_attribute (tree *node, tree name,
6499 tree args ATTRIBUTE_UNUSED,
6500 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6502 if (TREE_CODE (*node) == FUNCTION_DECL)
6503 /* Do nothing else, just set the attribute. We'll get at
6504 it later with lookup_attribute. */
6506 else
6508 warning (OPT_Wattributes, "%qE attribute ignored", name);
6509 *no_add_attrs = true;
6512 return NULL_TREE;
6515 /* Handle a "warning" or "error" attribute; arguments as in
6516 struct attribute_spec.handler. */
6518 static tree
6519 handle_error_attribute (tree *node, tree name, tree args,
6520 int ARG_UNUSED (flags), bool *no_add_attrs)
6522 if (TREE_CODE (*node) == FUNCTION_DECL
6523 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6524 /* Do nothing else, just set the attribute. We'll get at
6525 it later with lookup_attribute. */
6527 else
6529 warning (OPT_Wattributes, "%qE attribute ignored", name);
6530 *no_add_attrs = true;
6533 return NULL_TREE;
6536 /* Handle a "used" attribute; arguments as in
6537 struct attribute_spec.handler. */
6539 static tree
6540 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6541 int ARG_UNUSED (flags), bool *no_add_attrs)
6543 tree node = *pnode;
6545 if (TREE_CODE (node) == FUNCTION_DECL
6546 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6547 || (TREE_CODE (node) == TYPE_DECL))
6549 TREE_USED (node) = 1;
6550 DECL_PRESERVE_P (node) = 1;
6551 if (TREE_CODE (node) == VAR_DECL)
6552 DECL_READ_P (node) = 1;
6554 else
6556 warning (OPT_Wattributes, "%qE attribute ignored", name);
6557 *no_add_attrs = true;
6560 return NULL_TREE;
6563 /* Handle a "unused" attribute; arguments as in
6564 struct attribute_spec.handler. */
6566 static tree
6567 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6568 int flags, bool *no_add_attrs)
6570 if (DECL_P (*node))
6572 tree decl = *node;
6574 if (TREE_CODE (decl) == PARM_DECL
6575 || TREE_CODE (decl) == VAR_DECL
6576 || TREE_CODE (decl) == FUNCTION_DECL
6577 || TREE_CODE (decl) == LABEL_DECL
6578 || TREE_CODE (decl) == TYPE_DECL)
6580 TREE_USED (decl) = 1;
6581 if (TREE_CODE (decl) == VAR_DECL
6582 || TREE_CODE (decl) == PARM_DECL)
6583 DECL_READ_P (decl) = 1;
6585 else
6587 warning (OPT_Wattributes, "%qE attribute ignored", name);
6588 *no_add_attrs = true;
6591 else
6593 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6594 *node = build_variant_type_copy (*node);
6595 TREE_USED (*node) = 1;
6598 return NULL_TREE;
6601 /* Handle a "externally_visible" attribute; arguments as in
6602 struct attribute_spec.handler. */
6604 static tree
6605 handle_externally_visible_attribute (tree *pnode, tree name,
6606 tree ARG_UNUSED (args),
6607 int ARG_UNUSED (flags),
6608 bool *no_add_attrs)
6610 tree node = *pnode;
6612 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6614 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6615 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6617 warning (OPT_Wattributes,
6618 "%qE attribute have effect only on public objects", name);
6619 *no_add_attrs = true;
6622 else
6624 warning (OPT_Wattributes, "%qE attribute ignored", name);
6625 *no_add_attrs = true;
6628 return NULL_TREE;
6631 /* Handle a "const" attribute; arguments as in
6632 struct attribute_spec.handler. */
6634 static tree
6635 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6636 int ARG_UNUSED (flags), bool *no_add_attrs)
6638 tree type = TREE_TYPE (*node);
6640 /* See FIXME comment on noreturn in c_common_attribute_table. */
6641 if (TREE_CODE (*node) == FUNCTION_DECL)
6642 TREE_READONLY (*node) = 1;
6643 else if (TREE_CODE (type) == POINTER_TYPE
6644 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6645 TREE_TYPE (*node)
6646 = build_pointer_type
6647 (build_type_variant (TREE_TYPE (type), 1,
6648 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6649 else
6651 warning (OPT_Wattributes, "%qE attribute ignored", name);
6652 *no_add_attrs = true;
6655 return NULL_TREE;
6658 /* Handle a "transparent_union" attribute; arguments as in
6659 struct attribute_spec.handler. */
6661 static tree
6662 handle_transparent_union_attribute (tree *node, tree name,
6663 tree ARG_UNUSED (args), int flags,
6664 bool *no_add_attrs)
6666 tree type;
6668 *no_add_attrs = true;
6671 if (TREE_CODE (*node) == TYPE_DECL
6672 && ! (flags & ATTR_FLAG_CXX11))
6673 node = &TREE_TYPE (*node);
6674 type = *node;
6676 if (TREE_CODE (type) == UNION_TYPE)
6678 /* Make sure that the first field will work for a transparent union.
6679 If the type isn't complete yet, leave the check to the code in
6680 finish_struct. */
6681 if (TYPE_SIZE (type))
6683 tree first = first_field (type);
6684 if (first == NULL_TREE
6685 || DECL_ARTIFICIAL (first)
6686 || TYPE_MODE (type) != DECL_MODE (first))
6687 goto ignored;
6690 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6692 /* If the type isn't complete yet, setting the flag
6693 on a variant wouldn't ever be checked. */
6694 if (!TYPE_SIZE (type))
6695 goto ignored;
6697 /* build_duplicate_type doesn't work for C++. */
6698 if (c_dialect_cxx ())
6699 goto ignored;
6701 /* A type variant isn't good enough, since we don't a cast
6702 to such a type removed as a no-op. */
6703 *node = type = build_duplicate_type (type);
6706 TYPE_TRANSPARENT_AGGR (type) = 1;
6707 return NULL_TREE;
6710 ignored:
6711 warning (OPT_Wattributes, "%qE attribute ignored", name);
6712 return NULL_TREE;
6715 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6716 get the requested priority for a constructor or destructor,
6717 possibly issuing diagnostics for invalid or reserved
6718 priorities. */
6720 static priority_type
6721 get_priority (tree args, bool is_destructor)
6723 HOST_WIDE_INT pri;
6724 tree arg;
6726 if (!args)
6727 return DEFAULT_INIT_PRIORITY;
6729 if (!SUPPORTS_INIT_PRIORITY)
6731 if (is_destructor)
6732 error ("destructor priorities are not supported");
6733 else
6734 error ("constructor priorities are not supported");
6735 return DEFAULT_INIT_PRIORITY;
6738 arg = TREE_VALUE (args);
6739 arg = default_conversion (arg);
6740 if (!host_integerp (arg, /*pos=*/0)
6741 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6742 goto invalid;
6744 pri = tree_low_cst (arg, /*pos=*/0);
6745 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6746 goto invalid;
6748 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6750 if (is_destructor)
6751 warning (0,
6752 "destructor priorities from 0 to %d are reserved "
6753 "for the implementation",
6754 MAX_RESERVED_INIT_PRIORITY);
6755 else
6756 warning (0,
6757 "constructor priorities from 0 to %d are reserved "
6758 "for the implementation",
6759 MAX_RESERVED_INIT_PRIORITY);
6761 return pri;
6763 invalid:
6764 if (is_destructor)
6765 error ("destructor priorities must be integers from 0 to %d inclusive",
6766 MAX_INIT_PRIORITY);
6767 else
6768 error ("constructor priorities must be integers from 0 to %d inclusive",
6769 MAX_INIT_PRIORITY);
6770 return DEFAULT_INIT_PRIORITY;
6773 /* Handle a "constructor" attribute; arguments as in
6774 struct attribute_spec.handler. */
6776 static tree
6777 handle_constructor_attribute (tree *node, tree name, tree args,
6778 int ARG_UNUSED (flags),
6779 bool *no_add_attrs)
6781 tree decl = *node;
6782 tree type = TREE_TYPE (decl);
6784 if (TREE_CODE (decl) == FUNCTION_DECL
6785 && TREE_CODE (type) == FUNCTION_TYPE
6786 && decl_function_context (decl) == 0)
6788 priority_type priority;
6789 DECL_STATIC_CONSTRUCTOR (decl) = 1;
6790 priority = get_priority (args, /*is_destructor=*/false);
6791 SET_DECL_INIT_PRIORITY (decl, priority);
6792 TREE_USED (decl) = 1;
6794 else
6796 warning (OPT_Wattributes, "%qE attribute ignored", name);
6797 *no_add_attrs = true;
6800 return NULL_TREE;
6803 /* Handle a "destructor" attribute; arguments as in
6804 struct attribute_spec.handler. */
6806 static tree
6807 handle_destructor_attribute (tree *node, tree name, tree args,
6808 int ARG_UNUSED (flags),
6809 bool *no_add_attrs)
6811 tree decl = *node;
6812 tree type = TREE_TYPE (decl);
6814 if (TREE_CODE (decl) == FUNCTION_DECL
6815 && TREE_CODE (type) == FUNCTION_TYPE
6816 && decl_function_context (decl) == 0)
6818 priority_type priority;
6819 DECL_STATIC_DESTRUCTOR (decl) = 1;
6820 priority = get_priority (args, /*is_destructor=*/true);
6821 SET_DECL_FINI_PRIORITY (decl, priority);
6822 TREE_USED (decl) = 1;
6824 else
6826 warning (OPT_Wattributes, "%qE attribute ignored", name);
6827 *no_add_attrs = true;
6830 return NULL_TREE;
6833 /* Nonzero if the mode is a valid vector mode for this architecture.
6834 This returns nonzero even if there is no hardware support for the
6835 vector mode, but we can emulate with narrower modes. */
6837 static int
6838 vector_mode_valid_p (enum machine_mode mode)
6840 enum mode_class mclass = GET_MODE_CLASS (mode);
6841 enum machine_mode innermode;
6843 /* Doh! What's going on? */
6844 if (mclass != MODE_VECTOR_INT
6845 && mclass != MODE_VECTOR_FLOAT
6846 && mclass != MODE_VECTOR_FRACT
6847 && mclass != MODE_VECTOR_UFRACT
6848 && mclass != MODE_VECTOR_ACCUM
6849 && mclass != MODE_VECTOR_UACCUM)
6850 return 0;
6852 /* Hardware support. Woo hoo! */
6853 if (targetm.vector_mode_supported_p (mode))
6854 return 1;
6856 innermode = GET_MODE_INNER (mode);
6858 /* We should probably return 1 if requesting V4DI and we have no DI,
6859 but we have V2DI, but this is probably very unlikely. */
6861 /* If we have support for the inner mode, we can safely emulate it.
6862 We may not have V2DI, but me can emulate with a pair of DIs. */
6863 return targetm.scalar_mode_supported_p (innermode);
6867 /* Handle a "mode" attribute; arguments as in
6868 struct attribute_spec.handler. */
6870 static tree
6871 handle_mode_attribute (tree *node, tree name, tree args,
6872 int ARG_UNUSED (flags), bool *no_add_attrs)
6874 tree type = *node;
6875 tree ident = TREE_VALUE (args);
6877 *no_add_attrs = true;
6879 if (TREE_CODE (ident) != IDENTIFIER_NODE)
6880 warning (OPT_Wattributes, "%qE attribute ignored", name);
6881 else
6883 int j;
6884 const char *p = IDENTIFIER_POINTER (ident);
6885 int len = strlen (p);
6886 enum machine_mode mode = VOIDmode;
6887 tree typefm;
6888 bool valid_mode;
6890 if (len > 4 && p[0] == '_' && p[1] == '_'
6891 && p[len - 1] == '_' && p[len - 2] == '_')
6893 char *newp = (char *) alloca (len - 1);
6895 strcpy (newp, &p[2]);
6896 newp[len - 4] = '\0';
6897 p = newp;
6900 /* Change this type to have a type with the specified mode.
6901 First check for the special modes. */
6902 if (!strcmp (p, "byte"))
6903 mode = byte_mode;
6904 else if (!strcmp (p, "word"))
6905 mode = word_mode;
6906 else if (!strcmp (p, "pointer"))
6907 mode = ptr_mode;
6908 else if (!strcmp (p, "libgcc_cmp_return"))
6909 mode = targetm.libgcc_cmp_return_mode ();
6910 else if (!strcmp (p, "libgcc_shift_count"))
6911 mode = targetm.libgcc_shift_count_mode ();
6912 else if (!strcmp (p, "unwind_word"))
6913 mode = targetm.unwind_word_mode ();
6914 else
6915 for (j = 0; j < NUM_MACHINE_MODES; j++)
6916 if (!strcmp (p, GET_MODE_NAME (j)))
6918 mode = (enum machine_mode) j;
6919 break;
6922 if (mode == VOIDmode)
6924 error ("unknown machine mode %qE", ident);
6925 return NULL_TREE;
6928 valid_mode = false;
6929 switch (GET_MODE_CLASS (mode))
6931 case MODE_INT:
6932 case MODE_PARTIAL_INT:
6933 case MODE_FLOAT:
6934 case MODE_DECIMAL_FLOAT:
6935 case MODE_FRACT:
6936 case MODE_UFRACT:
6937 case MODE_ACCUM:
6938 case MODE_UACCUM:
6939 valid_mode = targetm.scalar_mode_supported_p (mode);
6940 break;
6942 case MODE_COMPLEX_INT:
6943 case MODE_COMPLEX_FLOAT:
6944 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
6945 break;
6947 case MODE_VECTOR_INT:
6948 case MODE_VECTOR_FLOAT:
6949 case MODE_VECTOR_FRACT:
6950 case MODE_VECTOR_UFRACT:
6951 case MODE_VECTOR_ACCUM:
6952 case MODE_VECTOR_UACCUM:
6953 warning (OPT_Wattributes, "specifying vector types with "
6954 "__attribute__ ((mode)) is deprecated");
6955 warning (OPT_Wattributes,
6956 "use __attribute__ ((vector_size)) instead");
6957 valid_mode = vector_mode_valid_p (mode);
6958 break;
6960 default:
6961 break;
6963 if (!valid_mode)
6965 error ("unable to emulate %qs", p);
6966 return NULL_TREE;
6969 if (POINTER_TYPE_P (type))
6971 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
6972 tree (*fn)(tree, enum machine_mode, bool);
6974 if (!targetm.addr_space.valid_pointer_mode (mode, as))
6976 error ("invalid pointer mode %qs", p);
6977 return NULL_TREE;
6980 if (TREE_CODE (type) == POINTER_TYPE)
6981 fn = build_pointer_type_for_mode;
6982 else
6983 fn = build_reference_type_for_mode;
6984 typefm = fn (TREE_TYPE (type), mode, false);
6986 else
6988 /* For fixed-point modes, we need to test if the signness of type
6989 and the machine mode are consistent. */
6990 if (ALL_FIXED_POINT_MODE_P (mode)
6991 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
6993 error ("signedness of type and machine mode %qs don%'t match", p);
6994 return NULL_TREE;
6996 /* For fixed-point modes, we need to pass saturating info. */
6997 typefm = lang_hooks.types.type_for_mode (mode,
6998 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
6999 : TYPE_UNSIGNED (type));
7002 if (typefm == NULL_TREE)
7004 error ("no data type for mode %qs", p);
7005 return NULL_TREE;
7007 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7009 /* For enumeral types, copy the precision from the integer
7010 type returned above. If not an INTEGER_TYPE, we can't use
7011 this mode for this type. */
7012 if (TREE_CODE (typefm) != INTEGER_TYPE)
7014 error ("cannot use mode %qs for enumeral types", p);
7015 return NULL_TREE;
7018 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7020 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7021 typefm = type;
7023 else
7025 /* We cannot build a type variant, as there's code that assumes
7026 that TYPE_MAIN_VARIANT has the same mode. This includes the
7027 debug generators. Instead, create a subrange type. This
7028 results in all of the enumeral values being emitted only once
7029 in the original, and the subtype gets them by reference. */
7030 if (TYPE_UNSIGNED (type))
7031 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7032 else
7033 typefm = make_signed_type (TYPE_PRECISION (typefm));
7034 TREE_TYPE (typefm) = type;
7037 else if (VECTOR_MODE_P (mode)
7038 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7039 : TREE_CODE (type) != TREE_CODE (typefm))
7041 error ("mode %qs applied to inappropriate type", p);
7042 return NULL_TREE;
7045 *node = typefm;
7048 return NULL_TREE;
7051 /* Handle a "section" attribute; arguments as in
7052 struct attribute_spec.handler. */
7054 static tree
7055 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7056 int ARG_UNUSED (flags), bool *no_add_attrs)
7058 tree decl = *node;
7060 if (targetm_common.have_named_sections)
7062 user_defined_section_attribute = true;
7064 if ((TREE_CODE (decl) == FUNCTION_DECL
7065 || TREE_CODE (decl) == VAR_DECL)
7066 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7068 if (TREE_CODE (decl) == VAR_DECL
7069 && current_function_decl != NULL_TREE
7070 && !TREE_STATIC (decl))
7072 error_at (DECL_SOURCE_LOCATION (decl),
7073 "section attribute cannot be specified for "
7074 "local variables");
7075 *no_add_attrs = true;
7078 /* The decl may have already been given a section attribute
7079 from a previous declaration. Ensure they match. */
7080 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7081 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7082 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7084 error ("section of %q+D conflicts with previous declaration",
7085 *node);
7086 *no_add_attrs = true;
7088 else if (TREE_CODE (decl) == VAR_DECL
7089 && !targetm.have_tls && targetm.emutls.tmpl_section
7090 && DECL_THREAD_LOCAL_P (decl))
7092 error ("section of %q+D cannot be overridden", *node);
7093 *no_add_attrs = true;
7095 else
7096 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7098 else
7100 error ("section attribute not allowed for %q+D", *node);
7101 *no_add_attrs = true;
7104 else
7106 error_at (DECL_SOURCE_LOCATION (*node),
7107 "section attributes are not supported for this target");
7108 *no_add_attrs = true;
7111 return NULL_TREE;
7114 /* Check whether ALIGN is a valid user-specified alignment. If so,
7115 return its base-2 log; if not, output an error and return -1. If
7116 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7117 no error. */
7119 check_user_alignment (const_tree align, bool allow_zero)
7121 int i;
7123 if (!INTEGRAL_TYPE_P (TREE_TYPE (align))
7124 || TREE_CODE (align) != INTEGER_CST)
7126 error ("requested alignment is not an integer constant");
7127 return -1;
7129 else if (allow_zero && integer_zerop (align))
7130 return -1;
7131 else if ((i = tree_log2 (align)) == -1)
7133 error ("requested alignment is not a power of 2");
7134 return -1;
7136 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7138 error ("requested alignment is too large");
7139 return -1;
7141 return i;
7145 If in c++-11, check if the c++-11 alignment constraint with respect
7146 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7147 c++-11 mode, does nothing.
7149 [dcl.align]2/ says:
7151 [* if the constant expression evaluates to a fundamental alignment,
7152 the alignment requirement of the declared entity shall be the
7153 specified fundamental alignment.
7155 * if the constant expression evaluates to an extended alignment
7156 and the implementation supports that alignment in the context
7157 of the declaration, the alignment of the declared entity shall
7158 be that alignment
7160 * if the constant expression evaluates to an extended alignment
7161 and the implementation does not support that alignment in the
7162 context of the declaration, the program is ill-formed]. */
7164 static bool
7165 check_cxx_fundamental_alignment_constraints (tree node,
7166 unsigned align_log,
7167 int flags)
7169 bool alignment_too_large_p = false;
7170 unsigned requested_alignment = 1U << align_log;
7171 unsigned max_align = 0;
7173 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7174 || (node == NULL_TREE || node == error_mark_node))
7175 return true;
7177 if (cxx_fundamental_alignment_p (requested_alignment))
7178 return true;
7180 if (DECL_P (node))
7182 if (TREE_STATIC (node))
7184 /* For file scope variables and static members, the target
7185 supports alignments that are at most
7186 MAX_OFILE_ALIGNMENT. */
7187 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7188 alignment_too_large_p = true;
7190 else
7192 #ifdef BIGGEST_FIELD_ALIGNMENT
7193 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7194 #else
7195 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7196 #endif
7197 /* For non-static members, the target supports either
7198 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7199 if it is defined or BIGGEST_ALIGNMENT. */
7200 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7201 if (TREE_CODE (node) == FIELD_DECL
7202 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7203 alignment_too_large_p = true;
7204 #undef MAX_TARGET_FIELD_ALIGNMENT
7205 /* For stack variables, the target supports at most
7206 MAX_STACK_ALIGNMENT. */
7207 else if (decl_function_context (node) != NULL
7208 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7209 alignment_too_large_p = true;
7212 else if (TYPE_P (node))
7214 /* Let's be liberal for types. */
7215 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7216 alignment_too_large_p = true;
7219 if (alignment_too_large_p)
7220 pedwarn (input_location, OPT_Wattributes,
7221 "requested alignment %d is larger than %d",
7222 requested_alignment, max_align);
7224 return !alignment_too_large_p;
7227 /* Handle a "aligned" attribute; arguments as in
7228 struct attribute_spec.handler. */
7230 static tree
7231 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7232 int flags, bool *no_add_attrs)
7234 tree decl = NULL_TREE;
7235 tree *type = NULL;
7236 int is_type = 0;
7237 tree align_expr = (args ? TREE_VALUE (args)
7238 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7239 int i;
7241 if (DECL_P (*node))
7243 decl = *node;
7244 type = &TREE_TYPE (decl);
7245 is_type = TREE_CODE (*node) == TYPE_DECL;
7247 else if (TYPE_P (*node))
7248 type = node, is_type = 1;
7250 if ((i = check_user_alignment (align_expr, false)) == -1
7251 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7252 *no_add_attrs = true;
7253 else if (is_type)
7255 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7256 /* OK, modify the type in place. */;
7257 /* If we have a TYPE_DECL, then copy the type, so that we
7258 don't accidentally modify a builtin type. See pushdecl. */
7259 else if (decl && TREE_TYPE (decl) != error_mark_node
7260 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7262 tree tt = TREE_TYPE (decl);
7263 *type = build_variant_type_copy (*type);
7264 DECL_ORIGINAL_TYPE (decl) = tt;
7265 TYPE_NAME (*type) = decl;
7266 TREE_USED (*type) = TREE_USED (decl);
7267 TREE_TYPE (decl) = *type;
7269 else
7270 *type = build_variant_type_copy (*type);
7272 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7273 TYPE_USER_ALIGN (*type) = 1;
7275 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7276 && TREE_CODE (decl) != FIELD_DECL)
7278 error ("alignment may not be specified for %q+D", decl);
7279 *no_add_attrs = true;
7281 else if (DECL_USER_ALIGN (decl)
7282 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7283 /* C++-11 [dcl.align/4]:
7285 When multiple alignment-specifiers are specified for an
7286 entity, the alignment requirement shall be set to the
7287 strictest specified alignment.
7289 This formally comes from the c++11 specification but we are
7290 doing it for the GNU attribute syntax as well. */
7291 *no_add_attrs = true;
7292 else if (TREE_CODE (decl) == FUNCTION_DECL
7293 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7295 if (DECL_USER_ALIGN (decl))
7296 error ("alignment for %q+D was previously specified as %d "
7297 "and may not be decreased", decl,
7298 DECL_ALIGN (decl) / BITS_PER_UNIT);
7299 else
7300 error ("alignment for %q+D must be at least %d", decl,
7301 DECL_ALIGN (decl) / BITS_PER_UNIT);
7302 *no_add_attrs = true;
7304 else
7306 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7307 DECL_USER_ALIGN (decl) = 1;
7310 return NULL_TREE;
7313 /* Handle a "weak" attribute; arguments as in
7314 struct attribute_spec.handler. */
7316 static tree
7317 handle_weak_attribute (tree *node, tree name,
7318 tree ARG_UNUSED (args),
7319 int ARG_UNUSED (flags),
7320 bool * ARG_UNUSED (no_add_attrs))
7322 if (TREE_CODE (*node) == FUNCTION_DECL
7323 && DECL_DECLARED_INLINE_P (*node))
7325 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7326 *no_add_attrs = true;
7328 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7330 error ("indirect function %q+D cannot be declared weak", *node);
7331 *no_add_attrs = true;
7332 return NULL_TREE;
7334 else if (TREE_CODE (*node) == FUNCTION_DECL
7335 || TREE_CODE (*node) == VAR_DECL)
7336 declare_weak (*node);
7337 else
7338 warning (OPT_Wattributes, "%qE attribute ignored", name);
7340 return NULL_TREE;
7343 /* Handle an "alias" or "ifunc" attribute; arguments as in
7344 struct attribute_spec.handler, except that IS_ALIAS tells us
7345 whether this is an alias as opposed to ifunc attribute. */
7347 static tree
7348 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7349 bool *no_add_attrs)
7351 tree decl = *node;
7353 if (TREE_CODE (decl) != FUNCTION_DECL
7354 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7356 warning (OPT_Wattributes, "%qE attribute ignored", name);
7357 *no_add_attrs = true;
7359 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7360 || (TREE_CODE (decl) != FUNCTION_DECL
7361 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7362 /* A static variable declaration is always a tentative definition,
7363 but the alias is a non-tentative definition which overrides. */
7364 || (TREE_CODE (decl) != FUNCTION_DECL
7365 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7367 error ("%q+D defined both normally and as %qE attribute", decl, name);
7368 *no_add_attrs = true;
7369 return NULL_TREE;
7371 else if (!is_alias
7372 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7373 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7375 error ("weak %q+D cannot be defined %qE", decl, name);
7376 *no_add_attrs = true;
7377 return NULL_TREE;
7380 /* Note that the very first time we process a nested declaration,
7381 decl_function_context will not be set. Indeed, *would* never
7382 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7383 we do below. After such frobbery, pushdecl would set the context.
7384 In any case, this is never what we want. */
7385 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7387 tree id;
7389 id = TREE_VALUE (args);
7390 if (TREE_CODE (id) != STRING_CST)
7392 error ("attribute %qE argument not a string", name);
7393 *no_add_attrs = true;
7394 return NULL_TREE;
7396 id = get_identifier (TREE_STRING_POINTER (id));
7397 /* This counts as a use of the object pointed to. */
7398 TREE_USED (id) = 1;
7400 if (TREE_CODE (decl) == FUNCTION_DECL)
7401 DECL_INITIAL (decl) = error_mark_node;
7402 else
7404 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
7405 DECL_EXTERNAL (decl) = 1;
7406 else
7407 DECL_EXTERNAL (decl) = 0;
7408 TREE_STATIC (decl) = 1;
7411 if (!is_alias)
7412 /* ifuncs are also aliases, so set that attribute too. */
7413 DECL_ATTRIBUTES (decl)
7414 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7416 else
7418 warning (OPT_Wattributes, "%qE attribute ignored", name);
7419 *no_add_attrs = true;
7422 return NULL_TREE;
7425 /* Handle an "alias" or "ifunc" attribute; arguments as in
7426 struct attribute_spec.handler. */
7428 static tree
7429 handle_ifunc_attribute (tree *node, tree name, tree args,
7430 int ARG_UNUSED (flags), bool *no_add_attrs)
7432 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7435 /* Handle an "alias" or "ifunc" attribute; arguments as in
7436 struct attribute_spec.handler. */
7438 static tree
7439 handle_alias_attribute (tree *node, tree name, tree args,
7440 int ARG_UNUSED (flags), bool *no_add_attrs)
7442 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7445 /* Handle a "weakref" attribute; arguments as in struct
7446 attribute_spec.handler. */
7448 static tree
7449 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7450 int flags, bool *no_add_attrs)
7452 tree attr = NULL_TREE;
7454 /* We must ignore the attribute when it is associated with
7455 local-scoped decls, since attribute alias is ignored and many
7456 such symbols do not even have a DECL_WEAK field. */
7457 if (decl_function_context (*node)
7458 || current_function_decl
7459 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7461 warning (OPT_Wattributes, "%qE attribute ignored", name);
7462 *no_add_attrs = true;
7463 return NULL_TREE;
7466 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7468 error ("indirect function %q+D cannot be declared weakref", *node);
7469 *no_add_attrs = true;
7470 return NULL_TREE;
7473 /* The idea here is that `weakref("name")' mutates into `weakref,
7474 alias("name")', and weakref without arguments, in turn,
7475 implicitly adds weak. */
7477 if (args)
7479 attr = tree_cons (get_identifier ("alias"), args, attr);
7480 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7482 *no_add_attrs = true;
7484 decl_attributes (node, attr, flags);
7486 else
7488 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7489 error_at (DECL_SOURCE_LOCATION (*node),
7490 "weakref attribute must appear before alias attribute");
7492 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7493 and that isn't supported; and because it wants to add it to
7494 the list of weak decls, which isn't helpful. */
7495 DECL_WEAK (*node) = 1;
7498 return NULL_TREE;
7501 /* Handle an "visibility" attribute; arguments as in
7502 struct attribute_spec.handler. */
7504 static tree
7505 handle_visibility_attribute (tree *node, tree name, tree args,
7506 int ARG_UNUSED (flags),
7507 bool *ARG_UNUSED (no_add_attrs))
7509 tree decl = *node;
7510 tree id = TREE_VALUE (args);
7511 enum symbol_visibility vis;
7513 if (TYPE_P (*node))
7515 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7516 /* OK */;
7517 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7519 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7520 name);
7521 return NULL_TREE;
7523 else if (TYPE_FIELDS (*node))
7525 error ("%qE attribute ignored because %qT is already defined",
7526 name, *node);
7527 return NULL_TREE;
7530 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7532 warning (OPT_Wattributes, "%qE attribute ignored", name);
7533 return NULL_TREE;
7536 if (TREE_CODE (id) != STRING_CST)
7538 error ("visibility argument not a string");
7539 return NULL_TREE;
7542 /* If this is a type, set the visibility on the type decl. */
7543 if (TYPE_P (decl))
7545 decl = TYPE_NAME (decl);
7546 if (!decl)
7547 return NULL_TREE;
7548 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7550 warning (OPT_Wattributes, "%qE attribute ignored on types",
7551 name);
7552 return NULL_TREE;
7556 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7557 vis = VISIBILITY_DEFAULT;
7558 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7559 vis = VISIBILITY_INTERNAL;
7560 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7561 vis = VISIBILITY_HIDDEN;
7562 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7563 vis = VISIBILITY_PROTECTED;
7564 else
7566 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7567 vis = VISIBILITY_DEFAULT;
7570 if (DECL_VISIBILITY_SPECIFIED (decl)
7571 && vis != DECL_VISIBILITY (decl))
7573 tree attributes = (TYPE_P (*node)
7574 ? TYPE_ATTRIBUTES (*node)
7575 : DECL_ATTRIBUTES (decl));
7576 if (lookup_attribute ("visibility", attributes))
7577 error ("%qD redeclared with different visibility", decl);
7578 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7579 && lookup_attribute ("dllimport", attributes))
7580 error ("%qD was declared %qs which implies default visibility",
7581 decl, "dllimport");
7582 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7583 && lookup_attribute ("dllexport", attributes))
7584 error ("%qD was declared %qs which implies default visibility",
7585 decl, "dllexport");
7588 DECL_VISIBILITY (decl) = vis;
7589 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7591 /* Go ahead and attach the attribute to the node as well. This is needed
7592 so we can determine whether we have VISIBILITY_DEFAULT because the
7593 visibility was not specified, or because it was explicitly overridden
7594 from the containing scope. */
7596 return NULL_TREE;
7599 /* Determine the ELF symbol visibility for DECL, which is either a
7600 variable or a function. It is an error to use this function if a
7601 definition of DECL is not available in this translation unit.
7602 Returns true if the final visibility has been determined by this
7603 function; false if the caller is free to make additional
7604 modifications. */
7606 bool
7607 c_determine_visibility (tree decl)
7609 gcc_assert (TREE_CODE (decl) == VAR_DECL
7610 || TREE_CODE (decl) == FUNCTION_DECL);
7612 /* If the user explicitly specified the visibility with an
7613 attribute, honor that. DECL_VISIBILITY will have been set during
7614 the processing of the attribute. We check for an explicit
7615 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7616 to distinguish the use of an attribute from the use of a "#pragma
7617 GCC visibility push(...)"; in the latter case we still want other
7618 considerations to be able to overrule the #pragma. */
7619 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7620 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7621 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7622 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7623 return true;
7625 /* Set default visibility to whatever the user supplied with
7626 visibility_specified depending on #pragma GCC visibility. */
7627 if (!DECL_VISIBILITY_SPECIFIED (decl))
7629 if (visibility_options.inpragma
7630 || DECL_VISIBILITY (decl) != default_visibility)
7632 DECL_VISIBILITY (decl) = default_visibility;
7633 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7634 /* If visibility changed and DECL already has DECL_RTL, ensure
7635 symbol flags are updated. */
7636 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7637 || TREE_CODE (decl) == FUNCTION_DECL)
7638 && DECL_RTL_SET_P (decl))
7639 make_decl_rtl (decl);
7642 return false;
7645 /* Handle an "tls_model" attribute; arguments as in
7646 struct attribute_spec.handler. */
7648 static tree
7649 handle_tls_model_attribute (tree *node, tree name, tree args,
7650 int ARG_UNUSED (flags), bool *no_add_attrs)
7652 tree id;
7653 tree decl = *node;
7654 enum tls_model kind;
7656 *no_add_attrs = true;
7658 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7660 warning (OPT_Wattributes, "%qE attribute ignored", name);
7661 return NULL_TREE;
7664 kind = DECL_TLS_MODEL (decl);
7665 id = TREE_VALUE (args);
7666 if (TREE_CODE (id) != STRING_CST)
7668 error ("tls_model argument not a string");
7669 return NULL_TREE;
7672 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7673 kind = TLS_MODEL_LOCAL_EXEC;
7674 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7675 kind = TLS_MODEL_INITIAL_EXEC;
7676 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7677 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7678 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7679 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7680 else
7681 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7683 DECL_TLS_MODEL (decl) = kind;
7684 return NULL_TREE;
7687 /* Handle a "no_instrument_function" attribute; arguments as in
7688 struct attribute_spec.handler. */
7690 static tree
7691 handle_no_instrument_function_attribute (tree *node, tree name,
7692 tree ARG_UNUSED (args),
7693 int ARG_UNUSED (flags),
7694 bool *no_add_attrs)
7696 tree decl = *node;
7698 if (TREE_CODE (decl) != FUNCTION_DECL)
7700 error_at (DECL_SOURCE_LOCATION (decl),
7701 "%qE attribute applies only to functions", name);
7702 *no_add_attrs = true;
7704 else if (DECL_INITIAL (decl))
7706 error_at (DECL_SOURCE_LOCATION (decl),
7707 "can%'t set %qE attribute after definition", name);
7708 *no_add_attrs = true;
7710 else
7711 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7713 return NULL_TREE;
7716 /* Handle a "malloc" attribute; arguments as in
7717 struct attribute_spec.handler. */
7719 static tree
7720 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7721 int ARG_UNUSED (flags), bool *no_add_attrs)
7723 if (TREE_CODE (*node) == FUNCTION_DECL
7724 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7725 DECL_IS_MALLOC (*node) = 1;
7726 else
7728 warning (OPT_Wattributes, "%qE attribute ignored", name);
7729 *no_add_attrs = true;
7732 return NULL_TREE;
7735 /* Handle a "alloc_size" attribute; arguments as in
7736 struct attribute_spec.handler. */
7738 static tree
7739 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7740 int ARG_UNUSED (flags), bool *no_add_attrs)
7742 unsigned arg_count = type_num_arguments (*node);
7743 for (; args; args = TREE_CHAIN (args))
7745 tree position = TREE_VALUE (args);
7747 if (TREE_CODE (position) != INTEGER_CST
7748 || TREE_INT_CST_HIGH (position)
7749 || TREE_INT_CST_LOW (position) < 1
7750 || TREE_INT_CST_LOW (position) > arg_count )
7752 warning (OPT_Wattributes,
7753 "alloc_size parameter outside range");
7754 *no_add_attrs = true;
7755 return NULL_TREE;
7758 return NULL_TREE;
7761 /* Handle a "fn spec" attribute; arguments as in
7762 struct attribute_spec.handler. */
7764 static tree
7765 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7766 tree args, int ARG_UNUSED (flags),
7767 bool *no_add_attrs ATTRIBUTE_UNUSED)
7769 gcc_assert (args
7770 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7771 && !TREE_CHAIN (args));
7772 return NULL_TREE;
7775 /* Handle a "returns_twice" attribute; arguments as in
7776 struct attribute_spec.handler. */
7778 static tree
7779 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7780 int ARG_UNUSED (flags), bool *no_add_attrs)
7782 if (TREE_CODE (*node) == FUNCTION_DECL)
7783 DECL_IS_RETURNS_TWICE (*node) = 1;
7784 else
7786 warning (OPT_Wattributes, "%qE attribute ignored", name);
7787 *no_add_attrs = true;
7790 return NULL_TREE;
7793 /* Handle a "no_limit_stack" attribute; arguments as in
7794 struct attribute_spec.handler. */
7796 static tree
7797 handle_no_limit_stack_attribute (tree *node, tree name,
7798 tree ARG_UNUSED (args),
7799 int ARG_UNUSED (flags),
7800 bool *no_add_attrs)
7802 tree decl = *node;
7804 if (TREE_CODE (decl) != FUNCTION_DECL)
7806 error_at (DECL_SOURCE_LOCATION (decl),
7807 "%qE attribute applies only to functions", name);
7808 *no_add_attrs = true;
7810 else if (DECL_INITIAL (decl))
7812 error_at (DECL_SOURCE_LOCATION (decl),
7813 "can%'t set %qE attribute after definition", name);
7814 *no_add_attrs = true;
7816 else
7817 DECL_NO_LIMIT_STACK (decl) = 1;
7819 return NULL_TREE;
7822 /* Handle a "pure" attribute; arguments as in
7823 struct attribute_spec.handler. */
7825 static tree
7826 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7827 int ARG_UNUSED (flags), bool *no_add_attrs)
7829 if (TREE_CODE (*node) == FUNCTION_DECL)
7830 DECL_PURE_P (*node) = 1;
7831 /* ??? TODO: Support types. */
7832 else
7834 warning (OPT_Wattributes, "%qE attribute ignored", name);
7835 *no_add_attrs = true;
7838 return NULL_TREE;
7841 /* Digest an attribute list destined for a transactional memory statement.
7842 ALLOWED is the set of attributes that are allowed for this statement;
7843 return the attribute we parsed. Multiple attributes are never allowed. */
7846 parse_tm_stmt_attr (tree attrs, int allowed)
7848 tree a_seen = NULL;
7849 int m_seen = 0;
7851 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
7853 tree a = TREE_PURPOSE (attrs);
7854 int m = 0;
7856 if (is_attribute_p ("outer", a))
7857 m = TM_STMT_ATTR_OUTER;
7859 if ((m & allowed) == 0)
7861 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
7862 continue;
7865 if (m_seen == 0)
7867 a_seen = a;
7868 m_seen = m;
7870 else if (m_seen == m)
7871 warning (OPT_Wattributes, "%qE attribute duplicated", a);
7872 else
7873 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
7876 return m_seen;
7879 /* Transform a TM attribute name into a maskable integer and back.
7880 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
7881 to how the lack of an attribute is treated. */
7884 tm_attr_to_mask (tree attr)
7886 if (attr == NULL)
7887 return 0;
7888 if (is_attribute_p ("transaction_safe", attr))
7889 return TM_ATTR_SAFE;
7890 if (is_attribute_p ("transaction_callable", attr))
7891 return TM_ATTR_CALLABLE;
7892 if (is_attribute_p ("transaction_pure", attr))
7893 return TM_ATTR_PURE;
7894 if (is_attribute_p ("transaction_unsafe", attr))
7895 return TM_ATTR_IRREVOCABLE;
7896 if (is_attribute_p ("transaction_may_cancel_outer", attr))
7897 return TM_ATTR_MAY_CANCEL_OUTER;
7898 return 0;
7901 tree
7902 tm_mask_to_attr (int mask)
7904 const char *str;
7905 switch (mask)
7907 case TM_ATTR_SAFE:
7908 str = "transaction_safe";
7909 break;
7910 case TM_ATTR_CALLABLE:
7911 str = "transaction_callable";
7912 break;
7913 case TM_ATTR_PURE:
7914 str = "transaction_pure";
7915 break;
7916 case TM_ATTR_IRREVOCABLE:
7917 str = "transaction_unsafe";
7918 break;
7919 case TM_ATTR_MAY_CANCEL_OUTER:
7920 str = "transaction_may_cancel_outer";
7921 break;
7922 default:
7923 gcc_unreachable ();
7925 return get_identifier (str);
7928 /* Return the first TM attribute seen in LIST. */
7930 tree
7931 find_tm_attribute (tree list)
7933 for (; list ; list = TREE_CHAIN (list))
7935 tree name = TREE_PURPOSE (list);
7936 if (tm_attr_to_mask (name) != 0)
7937 return name;
7939 return NULL_TREE;
7942 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
7943 Here we accept only function types, and verify that none of the other
7944 function TM attributes are also applied. */
7945 /* ??? We need to accept class types for C++, but not C. This greatly
7946 complicates this function, since we can no longer rely on the extra
7947 processing given by function_type_required. */
7949 static tree
7950 handle_tm_attribute (tree *node, tree name, tree args,
7951 int flags, bool *no_add_attrs)
7953 /* Only one path adds the attribute; others don't. */
7954 *no_add_attrs = true;
7956 switch (TREE_CODE (*node))
7958 case RECORD_TYPE:
7959 case UNION_TYPE:
7960 /* Only tm_callable and tm_safe apply to classes. */
7961 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
7962 goto ignored;
7963 /* FALLTHRU */
7965 case FUNCTION_TYPE:
7966 case METHOD_TYPE:
7968 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
7969 if (old_name == name)
7971 else if (old_name != NULL_TREE)
7972 error ("type was previously declared %qE", old_name);
7973 else
7974 *no_add_attrs = false;
7976 break;
7978 case POINTER_TYPE:
7980 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
7981 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
7983 tree fn_tmp = TREE_TYPE (*node);
7984 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
7985 *node = build_pointer_type (fn_tmp);
7986 break;
7989 /* FALLTHRU */
7991 default:
7992 /* If a function is next, pass it on to be tried next. */
7993 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
7994 return tree_cons (name, args, NULL);
7996 ignored:
7997 warning (OPT_Wattributes, "%qE attribute ignored", name);
7998 break;
8001 return NULL_TREE;
8004 /* Handle the TM_WRAP attribute; arguments as in
8005 struct attribute_spec.handler. */
8007 static tree
8008 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8009 int ARG_UNUSED (flags), bool *no_add_attrs)
8011 tree decl = *node;
8013 /* We don't need the attribute even on success, since we
8014 record the entry in an external table. */
8015 *no_add_attrs = true;
8017 if (TREE_CODE (decl) != FUNCTION_DECL)
8018 warning (OPT_Wattributes, "%qE attribute ignored", name);
8019 else
8021 tree wrap_decl = TREE_VALUE (args);
8022 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8023 && TREE_CODE (wrap_decl) != VAR_DECL
8024 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8025 error ("%qE argument not an identifier", name);
8026 else
8028 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8029 wrap_decl = lookup_name (wrap_decl);
8030 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8032 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8033 TREE_TYPE (wrap_decl)))
8034 record_tm_replacement (wrap_decl, decl);
8035 else
8036 error ("%qD is not compatible with %qD", wrap_decl, decl);
8038 else
8039 error ("transaction_wrap argument is not a function");
8043 return NULL_TREE;
8046 /* Ignore the given attribute. Used when this attribute may be usefully
8047 overridden by the target, but is not used generically. */
8049 static tree
8050 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8051 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8052 bool *no_add_attrs)
8054 *no_add_attrs = true;
8055 return NULL_TREE;
8058 /* Handle a "no vops" attribute; arguments as in
8059 struct attribute_spec.handler. */
8061 static tree
8062 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8063 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8064 bool *ARG_UNUSED (no_add_attrs))
8066 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8067 DECL_IS_NOVOPS (*node) = 1;
8068 return NULL_TREE;
8071 /* Handle a "deprecated" attribute; arguments as in
8072 struct attribute_spec.handler. */
8074 static tree
8075 handle_deprecated_attribute (tree *node, tree name,
8076 tree args, int flags,
8077 bool *no_add_attrs)
8079 tree type = NULL_TREE;
8080 int warn = 0;
8081 tree what = NULL_TREE;
8083 if (!args)
8084 *no_add_attrs = true;
8085 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8087 error ("deprecated message is not a string");
8088 *no_add_attrs = true;
8091 if (DECL_P (*node))
8093 tree decl = *node;
8094 type = TREE_TYPE (decl);
8096 if (TREE_CODE (decl) == TYPE_DECL
8097 || TREE_CODE (decl) == PARM_DECL
8098 || TREE_CODE (decl) == VAR_DECL
8099 || TREE_CODE (decl) == FUNCTION_DECL
8100 || TREE_CODE (decl) == FIELD_DECL
8101 || objc_method_decl (TREE_CODE (decl)))
8102 TREE_DEPRECATED (decl) = 1;
8103 else
8104 warn = 1;
8106 else if (TYPE_P (*node))
8108 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8109 *node = build_variant_type_copy (*node);
8110 TREE_DEPRECATED (*node) = 1;
8111 type = *node;
8113 else
8114 warn = 1;
8116 if (warn)
8118 *no_add_attrs = true;
8119 if (type && TYPE_NAME (type))
8121 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8122 what = TYPE_NAME (*node);
8123 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8124 && DECL_NAME (TYPE_NAME (type)))
8125 what = DECL_NAME (TYPE_NAME (type));
8127 if (what)
8128 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8129 else
8130 warning (OPT_Wattributes, "%qE attribute ignored", name);
8133 return NULL_TREE;
8136 /* Handle a "vector_size" attribute; arguments as in
8137 struct attribute_spec.handler. */
8139 static tree
8140 handle_vector_size_attribute (tree *node, tree name, tree args,
8141 int ARG_UNUSED (flags),
8142 bool *no_add_attrs)
8144 unsigned HOST_WIDE_INT vecsize, nunits;
8145 enum machine_mode orig_mode;
8146 tree type = *node, new_type, size;
8148 *no_add_attrs = true;
8150 size = TREE_VALUE (args);
8152 if (!host_integerp (size, 1))
8154 warning (OPT_Wattributes, "%qE attribute ignored", name);
8155 return NULL_TREE;
8158 /* Get the vector size (in bytes). */
8159 vecsize = tree_low_cst (size, 1);
8161 /* We need to provide for vector pointers, vector arrays, and
8162 functions returning vectors. For example:
8164 __attribute__((vector_size(16))) short *foo;
8166 In this case, the mode is SI, but the type being modified is
8167 HI, so we need to look further. */
8169 while (POINTER_TYPE_P (type)
8170 || TREE_CODE (type) == FUNCTION_TYPE
8171 || TREE_CODE (type) == METHOD_TYPE
8172 || TREE_CODE (type) == ARRAY_TYPE
8173 || TREE_CODE (type) == OFFSET_TYPE)
8174 type = TREE_TYPE (type);
8176 /* Get the mode of the type being modified. */
8177 orig_mode = TYPE_MODE (type);
8179 if ((!INTEGRAL_TYPE_P (type)
8180 && !SCALAR_FLOAT_TYPE_P (type)
8181 && !FIXED_POINT_TYPE_P (type))
8182 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8183 && GET_MODE_CLASS (orig_mode) != MODE_INT
8184 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8185 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8186 || TREE_CODE (type) == BOOLEAN_TYPE)
8188 error ("invalid vector type for attribute %qE", name);
8189 return NULL_TREE;
8192 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8194 error ("vector size not an integral multiple of component size");
8195 return NULL;
8198 if (vecsize == 0)
8200 error ("zero vector size");
8201 return NULL;
8204 /* Calculate how many units fit in the vector. */
8205 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8206 if (nunits & (nunits - 1))
8208 error ("number of components of the vector not a power of two");
8209 return NULL_TREE;
8212 new_type = build_vector_type (type, nunits);
8214 /* Build back pointers if needed. */
8215 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8217 return NULL_TREE;
8220 /* Handle the "nonnull" attribute. */
8221 static tree
8222 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8223 tree args, int ARG_UNUSED (flags),
8224 bool *no_add_attrs)
8226 tree type = *node;
8227 unsigned HOST_WIDE_INT attr_arg_num;
8229 /* If no arguments are specified, all pointer arguments should be
8230 non-null. Verify a full prototype is given so that the arguments
8231 will have the correct types when we actually check them later. */
8232 if (!args)
8234 if (!prototype_p (type))
8236 error ("nonnull attribute without arguments on a non-prototype");
8237 *no_add_attrs = true;
8239 return NULL_TREE;
8242 /* Argument list specified. Verify that each argument number references
8243 a pointer argument. */
8244 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8246 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8248 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8250 error ("nonnull argument has invalid operand number (argument %lu)",
8251 (unsigned long) attr_arg_num);
8252 *no_add_attrs = true;
8253 return NULL_TREE;
8256 if (prototype_p (type))
8258 function_args_iterator iter;
8259 tree argument;
8261 function_args_iter_init (&iter, type);
8262 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8264 argument = function_args_iter_cond (&iter);
8265 if (argument == NULL_TREE || ck_num == arg_num)
8266 break;
8269 if (!argument
8270 || TREE_CODE (argument) == VOID_TYPE)
8272 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8273 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8274 *no_add_attrs = true;
8275 return NULL_TREE;
8278 if (TREE_CODE (argument) != POINTER_TYPE)
8280 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8281 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8282 *no_add_attrs = true;
8283 return NULL_TREE;
8288 return NULL_TREE;
8291 /* Check the argument list of a function call for null in argument slots
8292 that are marked as requiring a non-null pointer argument. The NARGS
8293 arguments are passed in the array ARGARRAY.
8296 static void
8297 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8299 tree a;
8300 int i;
8302 attrs = lookup_attribute ("nonnull", attrs);
8303 if (attrs == NULL_TREE)
8304 return;
8306 a = attrs;
8307 /* See if any of the nonnull attributes has no arguments. If so,
8308 then every pointer argument is checked (in which case the check
8309 for pointer type is done in check_nonnull_arg). */
8310 if (TREE_VALUE (a) != NULL_TREE)
8312 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8313 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8315 if (a != NULL_TREE)
8316 for (i = 0; i < nargs; i++)
8317 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8318 i + 1);
8319 else
8321 /* Walk the argument list. If we encounter an argument number we
8322 should check for non-null, do it. */
8323 for (i = 0; i < nargs; i++)
8325 for (a = attrs; ; a = TREE_CHAIN (a))
8327 a = lookup_attribute ("nonnull", a);
8328 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8329 break;
8332 if (a != NULL_TREE)
8333 check_function_arguments_recurse (check_nonnull_arg, NULL,
8334 argarray[i], i + 1);
8339 /* Check that the Nth argument of a function call (counting backwards
8340 from the end) is a (pointer)0. The NARGS arguments are passed in the
8341 array ARGARRAY. */
8343 static void
8344 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8346 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8348 if (attr)
8350 int len = 0;
8351 int pos = 0;
8352 tree sentinel;
8353 function_args_iterator iter;
8354 tree t;
8356 /* Skip over the named arguments. */
8357 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8359 if (len == nargs)
8360 break;
8361 len++;
8364 if (TREE_VALUE (attr))
8366 tree p = TREE_VALUE (TREE_VALUE (attr));
8367 pos = TREE_INT_CST_LOW (p);
8370 /* The sentinel must be one of the varargs, i.e.
8371 in position >= the number of fixed arguments. */
8372 if ((nargs - 1 - pos) < len)
8374 warning (OPT_Wformat,
8375 "not enough variable arguments to fit a sentinel");
8376 return;
8379 /* Validate the sentinel. */
8380 sentinel = argarray[nargs - 1 - pos];
8381 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8382 || !integer_zerop (sentinel))
8383 /* Although __null (in C++) is only an integer we allow it
8384 nevertheless, as we are guaranteed that it's exactly
8385 as wide as a pointer, and we don't want to force
8386 users to cast the NULL they have written there.
8387 We warn with -Wstrict-null-sentinel, though. */
8388 && (warn_strict_null_sentinel || null_node != sentinel))
8389 warning (OPT_Wformat, "missing sentinel in function call");
8393 /* Helper for check_function_nonnull; given a list of operands which
8394 must be non-null in ARGS, determine if operand PARAM_NUM should be
8395 checked. */
8397 static bool
8398 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8400 unsigned HOST_WIDE_INT arg_num = 0;
8402 for (; args; args = TREE_CHAIN (args))
8404 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8406 gcc_assert (found);
8408 if (arg_num == param_num)
8409 return true;
8411 return false;
8414 /* Check that the function argument PARAM (which is operand number
8415 PARAM_NUM) is non-null. This is called by check_function_nonnull
8416 via check_function_arguments_recurse. */
8418 static void
8419 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8420 unsigned HOST_WIDE_INT param_num)
8422 /* Just skip checking the argument if it's not a pointer. This can
8423 happen if the "nonnull" attribute was given without an operand
8424 list (which means to check every pointer argument). */
8426 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8427 return;
8429 if (integer_zerop (param))
8430 warning (OPT_Wnonnull, "null argument where non-null required "
8431 "(argument %lu)", (unsigned long) param_num);
8434 /* Helper for nonnull attribute handling; fetch the operand number
8435 from the attribute argument list. */
8437 static bool
8438 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8440 /* Verify the arg number is a constant. */
8441 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8442 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8443 return false;
8445 *valp = TREE_INT_CST_LOW (arg_num_expr);
8446 return true;
8449 /* Handle a "nothrow" attribute; arguments as in
8450 struct attribute_spec.handler. */
8452 static tree
8453 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8454 int ARG_UNUSED (flags), bool *no_add_attrs)
8456 if (TREE_CODE (*node) == FUNCTION_DECL)
8457 TREE_NOTHROW (*node) = 1;
8458 /* ??? TODO: Support types. */
8459 else
8461 warning (OPT_Wattributes, "%qE attribute ignored", name);
8462 *no_add_attrs = true;
8465 return NULL_TREE;
8468 /* Handle a "cleanup" attribute; arguments as in
8469 struct attribute_spec.handler. */
8471 static tree
8472 handle_cleanup_attribute (tree *node, tree name, tree args,
8473 int ARG_UNUSED (flags), bool *no_add_attrs)
8475 tree decl = *node;
8476 tree cleanup_id, cleanup_decl;
8478 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8479 for global destructors in C++. This requires infrastructure that
8480 we don't have generically at the moment. It's also not a feature
8481 we'd be missing too much, since we do have attribute constructor. */
8482 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8484 warning (OPT_Wattributes, "%qE attribute ignored", name);
8485 *no_add_attrs = true;
8486 return NULL_TREE;
8489 /* Verify that the argument is a function in scope. */
8490 /* ??? We could support pointers to functions here as well, if
8491 that was considered desirable. */
8492 cleanup_id = TREE_VALUE (args);
8493 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8495 error ("cleanup argument not an identifier");
8496 *no_add_attrs = true;
8497 return NULL_TREE;
8499 cleanup_decl = lookup_name (cleanup_id);
8500 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8502 error ("cleanup argument not a function");
8503 *no_add_attrs = true;
8504 return NULL_TREE;
8507 /* That the function has proper type is checked with the
8508 eventual call to build_function_call. */
8510 return NULL_TREE;
8513 /* Handle a "warn_unused_result" attribute. No special handling. */
8515 static tree
8516 handle_warn_unused_result_attribute (tree *node, tree name,
8517 tree ARG_UNUSED (args),
8518 int ARG_UNUSED (flags), bool *no_add_attrs)
8520 /* Ignore the attribute for functions not returning any value. */
8521 if (VOID_TYPE_P (TREE_TYPE (*node)))
8523 warning (OPT_Wattributes, "%qE attribute ignored", name);
8524 *no_add_attrs = true;
8527 return NULL_TREE;
8530 /* Handle a "sentinel" attribute. */
8532 static tree
8533 handle_sentinel_attribute (tree *node, tree name, tree args,
8534 int ARG_UNUSED (flags), bool *no_add_attrs)
8536 if (!prototype_p (*node))
8538 warning (OPT_Wattributes,
8539 "%qE attribute requires prototypes with named arguments", name);
8540 *no_add_attrs = true;
8542 else
8544 if (!stdarg_p (*node))
8546 warning (OPT_Wattributes,
8547 "%qE attribute only applies to variadic functions", name);
8548 *no_add_attrs = true;
8552 if (args)
8554 tree position = TREE_VALUE (args);
8556 if (TREE_CODE (position) != INTEGER_CST)
8558 warning (OPT_Wattributes,
8559 "requested position is not an integer constant");
8560 *no_add_attrs = true;
8562 else
8564 if (tree_int_cst_lt (position, integer_zero_node))
8566 warning (OPT_Wattributes,
8567 "requested position is less than zero");
8568 *no_add_attrs = true;
8573 return NULL_TREE;
8576 /* Handle a "type_generic" attribute. */
8578 static tree
8579 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8580 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8581 bool * ARG_UNUSED (no_add_attrs))
8583 /* Ensure we have a function type. */
8584 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8586 /* Ensure we have a variadic function. */
8587 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8589 return NULL_TREE;
8592 /* Handle a "target" attribute. */
8594 static tree
8595 handle_target_attribute (tree *node, tree name, tree args, int flags,
8596 bool *no_add_attrs)
8598 /* Ensure we have a function type. */
8599 if (TREE_CODE (*node) != FUNCTION_DECL)
8601 warning (OPT_Wattributes, "%qE attribute ignored", name);
8602 *no_add_attrs = true;
8604 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8605 flags))
8606 *no_add_attrs = true;
8608 return NULL_TREE;
8611 /* Arguments being collected for optimization. */
8612 typedef const char *const_char_p; /* For DEF_VEC_P. */
8613 DEF_VEC_P(const_char_p);
8614 DEF_VEC_ALLOC_P(const_char_p, gc);
8615 static GTY(()) VEC(const_char_p, gc) *optimize_args;
8618 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8619 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8620 false for #pragma GCC optimize. */
8622 bool
8623 parse_optimize_options (tree args, bool attr_p)
8625 bool ret = true;
8626 unsigned opt_argc;
8627 unsigned i;
8628 int saved_flag_strict_aliasing;
8629 const char **opt_argv;
8630 struct cl_decoded_option *decoded_options;
8631 unsigned int decoded_options_count;
8632 tree ap;
8634 /* Build up argv vector. Just in case the string is stored away, use garbage
8635 collected strings. */
8636 VEC_truncate (const_char_p, optimize_args, 0);
8637 VEC_safe_push (const_char_p, gc, optimize_args, NULL);
8639 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8641 tree value = TREE_VALUE (ap);
8643 if (TREE_CODE (value) == INTEGER_CST)
8645 char buffer[20];
8646 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8647 VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer));
8650 else if (TREE_CODE (value) == STRING_CST)
8652 /* Split string into multiple substrings. */
8653 size_t len = TREE_STRING_LENGTH (value);
8654 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8655 char *end = p + len;
8656 char *comma;
8657 char *next_p = p;
8659 while (next_p != NULL)
8661 size_t len2;
8662 char *q, *r;
8664 p = next_p;
8665 comma = strchr (p, ',');
8666 if (comma)
8668 len2 = comma - p;
8669 *comma = '\0';
8670 next_p = comma+1;
8672 else
8674 len2 = end - p;
8675 next_p = NULL;
8678 r = q = (char *) ggc_alloc_atomic (len2 + 3);
8680 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
8681 options. */
8682 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
8684 ret = false;
8685 if (attr_p)
8686 warning (OPT_Wattributes,
8687 "bad option %s to optimize attribute", p);
8688 else
8689 warning (OPT_Wpragmas,
8690 "bad option %s to pragma attribute", p);
8691 continue;
8694 if (*p != '-')
8696 *r++ = '-';
8698 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
8699 itself is -Os, and any other switch begins with a -f. */
8700 if ((*p >= '0' && *p <= '9')
8701 || (p[0] == 's' && p[1] == '\0'))
8702 *r++ = 'O';
8703 else if (*p != 'O')
8704 *r++ = 'f';
8707 memcpy (r, p, len2);
8708 r[len2] = '\0';
8709 VEC_safe_push (const_char_p, gc, optimize_args, q);
8715 opt_argc = VEC_length (const_char_p, optimize_args);
8716 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
8718 for (i = 1; i < opt_argc; i++)
8719 opt_argv[i] = VEC_index (const_char_p, optimize_args, i);
8721 saved_flag_strict_aliasing = flag_strict_aliasing;
8723 /* Now parse the options. */
8724 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
8725 &decoded_options,
8726 &decoded_options_count);
8727 decode_options (&global_options, &global_options_set,
8728 decoded_options, decoded_options_count,
8729 input_location, global_dc);
8731 targetm.override_options_after_change();
8733 /* Don't allow changing -fstrict-aliasing. */
8734 flag_strict_aliasing = saved_flag_strict_aliasing;
8736 VEC_truncate (const_char_p, optimize_args, 0);
8737 return ret;
8740 /* For handling "optimize" attribute. arguments as in
8741 struct attribute_spec.handler. */
8743 static tree
8744 handle_optimize_attribute (tree *node, tree name, tree args,
8745 int ARG_UNUSED (flags), bool *no_add_attrs)
8747 /* Ensure we have a function type. */
8748 if (TREE_CODE (*node) != FUNCTION_DECL)
8750 warning (OPT_Wattributes, "%qE attribute ignored", name);
8751 *no_add_attrs = true;
8753 else
8755 struct cl_optimization cur_opts;
8756 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
8758 /* Save current options. */
8759 cl_optimization_save (&cur_opts, &global_options);
8761 /* If we previously had some optimization options, use them as the
8762 default. */
8763 if (old_opts)
8764 cl_optimization_restore (&global_options,
8765 TREE_OPTIMIZATION (old_opts));
8767 /* Parse options, and update the vector. */
8768 parse_optimize_options (args, true);
8769 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
8770 = build_optimization_node ();
8772 /* Restore current options. */
8773 cl_optimization_restore (&global_options, &cur_opts);
8776 return NULL_TREE;
8779 /* Handle a "no_split_stack" attribute. */
8781 static tree
8782 handle_no_split_stack_attribute (tree *node, tree name,
8783 tree ARG_UNUSED (args),
8784 int ARG_UNUSED (flags),
8785 bool *no_add_attrs)
8787 tree decl = *node;
8789 if (TREE_CODE (decl) != FUNCTION_DECL)
8791 error_at (DECL_SOURCE_LOCATION (decl),
8792 "%qE attribute applies only to functions", name);
8793 *no_add_attrs = true;
8795 else if (DECL_INITIAL (decl))
8797 error_at (DECL_SOURCE_LOCATION (decl),
8798 "can%'t set %qE attribute after definition", name);
8799 *no_add_attrs = true;
8802 return NULL_TREE;
8805 /* Check for valid arguments being passed to a function with FNTYPE.
8806 There are NARGS arguments in the array ARGARRAY. */
8807 void
8808 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
8810 /* Check for null being passed in a pointer argument that must be
8811 non-null. We also need to do this if format checking is enabled. */
8813 if (warn_nonnull)
8814 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8816 /* Check for errors in format strings. */
8818 if (warn_format || warn_suggest_attribute_format)
8819 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8821 if (warn_format)
8822 check_function_sentinel (fntype, nargs, argarray);
8825 /* Generic argument checking recursion routine. PARAM is the argument to
8826 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
8827 once the argument is resolved. CTX is context for the callback. */
8828 void
8829 check_function_arguments_recurse (void (*callback)
8830 (void *, tree, unsigned HOST_WIDE_INT),
8831 void *ctx, tree param,
8832 unsigned HOST_WIDE_INT param_num)
8834 if (CONVERT_EXPR_P (param)
8835 && (TYPE_PRECISION (TREE_TYPE (param))
8836 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
8838 /* Strip coercion. */
8839 check_function_arguments_recurse (callback, ctx,
8840 TREE_OPERAND (param, 0), param_num);
8841 return;
8844 if (TREE_CODE (param) == CALL_EXPR)
8846 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
8847 tree attrs;
8848 bool found_format_arg = false;
8850 /* See if this is a call to a known internationalization function
8851 that modifies a format arg. Such a function may have multiple
8852 format_arg attributes (for example, ngettext). */
8854 for (attrs = TYPE_ATTRIBUTES (type);
8855 attrs;
8856 attrs = TREE_CHAIN (attrs))
8857 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
8859 tree inner_arg;
8860 tree format_num_expr;
8861 int format_num;
8862 int i;
8863 call_expr_arg_iterator iter;
8865 /* Extract the argument number, which was previously checked
8866 to be valid. */
8867 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
8869 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
8870 && !TREE_INT_CST_HIGH (format_num_expr));
8872 format_num = TREE_INT_CST_LOW (format_num_expr);
8874 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
8875 inner_arg != 0;
8876 inner_arg = next_call_expr_arg (&iter), i++)
8877 if (i == format_num)
8879 check_function_arguments_recurse (callback, ctx,
8880 inner_arg, param_num);
8881 found_format_arg = true;
8882 break;
8886 /* If we found a format_arg attribute and did a recursive check,
8887 we are done with checking this argument. Otherwise, we continue
8888 and this will be considered a non-literal. */
8889 if (found_format_arg)
8890 return;
8893 if (TREE_CODE (param) == COND_EXPR)
8895 /* Check both halves of the conditional expression. */
8896 check_function_arguments_recurse (callback, ctx,
8897 TREE_OPERAND (param, 1), param_num);
8898 check_function_arguments_recurse (callback, ctx,
8899 TREE_OPERAND (param, 2), param_num);
8900 return;
8903 (*callback) (ctx, param, param_num);
8906 /* Checks for a builtin function FNDECL that the number of arguments
8907 NARGS against the required number REQUIRED and issues an error if
8908 there is a mismatch. Returns true if the number of arguments is
8909 correct, otherwise false. */
8911 static bool
8912 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
8914 if (nargs < required)
8916 error_at (input_location,
8917 "not enough arguments to function %qE", fndecl);
8918 return false;
8920 else if (nargs > required)
8922 error_at (input_location,
8923 "too many arguments to function %qE", fndecl);
8924 return false;
8926 return true;
8929 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
8930 Returns false if there was an error, otherwise true. */
8932 bool
8933 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
8935 if (!DECL_BUILT_IN (fndecl)
8936 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
8937 return true;
8939 switch (DECL_FUNCTION_CODE (fndecl))
8941 case BUILT_IN_CONSTANT_P:
8942 return builtin_function_validate_nargs (fndecl, nargs, 1);
8944 case BUILT_IN_ISFINITE:
8945 case BUILT_IN_ISINF:
8946 case BUILT_IN_ISINF_SIGN:
8947 case BUILT_IN_ISNAN:
8948 case BUILT_IN_ISNORMAL:
8949 if (builtin_function_validate_nargs (fndecl, nargs, 1))
8951 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
8953 error ("non-floating-point argument in call to "
8954 "function %qE", fndecl);
8955 return false;
8957 return true;
8959 return false;
8961 case BUILT_IN_ISGREATER:
8962 case BUILT_IN_ISGREATEREQUAL:
8963 case BUILT_IN_ISLESS:
8964 case BUILT_IN_ISLESSEQUAL:
8965 case BUILT_IN_ISLESSGREATER:
8966 case BUILT_IN_ISUNORDERED:
8967 if (builtin_function_validate_nargs (fndecl, nargs, 2))
8969 enum tree_code code0, code1;
8970 code0 = TREE_CODE (TREE_TYPE (args[0]));
8971 code1 = TREE_CODE (TREE_TYPE (args[1]));
8972 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
8973 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8974 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
8976 error ("non-floating-point arguments in call to "
8977 "function %qE", fndecl);
8978 return false;
8980 return true;
8982 return false;
8984 case BUILT_IN_FPCLASSIFY:
8985 if (builtin_function_validate_nargs (fndecl, nargs, 6))
8987 unsigned i;
8989 for (i=0; i<5; i++)
8990 if (TREE_CODE (args[i]) != INTEGER_CST)
8992 error ("non-const integer argument %u in call to function %qE",
8993 i+1, fndecl);
8994 return false;
8997 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
8999 error ("non-floating-point argument in call to function %qE",
9000 fndecl);
9001 return false;
9003 return true;
9005 return false;
9007 case BUILT_IN_ASSUME_ALIGNED:
9008 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9010 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9012 error ("non-integer argument 3 in call to function %qE", fndecl);
9013 return false;
9015 return true;
9017 return false;
9019 default:
9020 return true;
9024 /* Function to help qsort sort FIELD_DECLs by name order. */
9027 field_decl_cmp (const void *x_p, const void *y_p)
9029 const tree *const x = (const tree *const) x_p;
9030 const tree *const y = (const tree *const) y_p;
9032 if (DECL_NAME (*x) == DECL_NAME (*y))
9033 /* A nontype is "greater" than a type. */
9034 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9035 if (DECL_NAME (*x) == NULL_TREE)
9036 return -1;
9037 if (DECL_NAME (*y) == NULL_TREE)
9038 return 1;
9039 if (DECL_NAME (*x) < DECL_NAME (*y))
9040 return -1;
9041 return 1;
9044 static struct {
9045 gt_pointer_operator new_value;
9046 void *cookie;
9047 } resort_data;
9049 /* This routine compares two fields like field_decl_cmp but using the
9050 pointer operator in resort_data. */
9052 static int
9053 resort_field_decl_cmp (const void *x_p, const void *y_p)
9055 const tree *const x = (const tree *const) x_p;
9056 const tree *const y = (const tree *const) y_p;
9058 if (DECL_NAME (*x) == DECL_NAME (*y))
9059 /* A nontype is "greater" than a type. */
9060 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9061 if (DECL_NAME (*x) == NULL_TREE)
9062 return -1;
9063 if (DECL_NAME (*y) == NULL_TREE)
9064 return 1;
9066 tree d1 = DECL_NAME (*x);
9067 tree d2 = DECL_NAME (*y);
9068 resort_data.new_value (&d1, resort_data.cookie);
9069 resort_data.new_value (&d2, resort_data.cookie);
9070 if (d1 < d2)
9071 return -1;
9073 return 1;
9076 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9078 void
9079 resort_sorted_fields (void *obj,
9080 void * ARG_UNUSED (orig_obj),
9081 gt_pointer_operator new_value,
9082 void *cookie)
9084 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9085 resort_data.new_value = new_value;
9086 resort_data.cookie = cookie;
9087 qsort (&sf->elts[0], sf->len, sizeof (tree),
9088 resort_field_decl_cmp);
9091 /* Subroutine of c_parse_error.
9092 Return the result of concatenating LHS and RHS. RHS is really
9093 a string literal, its first character is indicated by RHS_START and
9094 RHS_SIZE is its length (including the terminating NUL character).
9096 The caller is responsible for deleting the returned pointer. */
9098 static char *
9099 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9101 const int lhs_size = strlen (lhs);
9102 char *result = XNEWVEC (char, lhs_size + rhs_size);
9103 strncpy (result, lhs, lhs_size);
9104 strncpy (result + lhs_size, rhs_start, rhs_size);
9105 return result;
9108 /* Issue the error given by GMSGID, indicating that it occurred before
9109 TOKEN, which had the associated VALUE. */
9111 void
9112 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9113 tree value, unsigned char token_flags)
9115 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9117 char *message = NULL;
9119 if (token_type == CPP_EOF)
9120 message = catenate_messages (gmsgid, " at end of input");
9121 else if (token_type == CPP_CHAR
9122 || token_type == CPP_WCHAR
9123 || token_type == CPP_CHAR16
9124 || token_type == CPP_CHAR32)
9126 unsigned int val = TREE_INT_CST_LOW (value);
9127 const char *prefix;
9129 switch (token_type)
9131 default:
9132 prefix = "";
9133 break;
9134 case CPP_WCHAR:
9135 prefix = "L";
9136 break;
9137 case CPP_CHAR16:
9138 prefix = "u";
9139 break;
9140 case CPP_CHAR32:
9141 prefix = "U";
9142 break;
9145 if (val <= UCHAR_MAX && ISGRAPH (val))
9146 message = catenate_messages (gmsgid, " before %s'%c'");
9147 else
9148 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9150 error (message, prefix, val);
9151 free (message);
9152 message = NULL;
9154 else if (token_type == CPP_STRING
9155 || token_type == CPP_WSTRING
9156 || token_type == CPP_STRING16
9157 || token_type == CPP_STRING32
9158 || token_type == CPP_UTF8STRING)
9159 message = catenate_messages (gmsgid, " before string constant");
9160 else if (token_type == CPP_NUMBER)
9161 message = catenate_messages (gmsgid, " before numeric constant");
9162 else if (token_type == CPP_NAME)
9164 message = catenate_messages (gmsgid, " before %qE");
9165 error (message, value);
9166 free (message);
9167 message = NULL;
9169 else if (token_type == CPP_PRAGMA)
9170 message = catenate_messages (gmsgid, " before %<#pragma%>");
9171 else if (token_type == CPP_PRAGMA_EOL)
9172 message = catenate_messages (gmsgid, " before end of line");
9173 else if (token_type == CPP_DECLTYPE)
9174 message = catenate_messages (gmsgid, " before %<decltype%>");
9175 else if (token_type < N_TTYPES)
9177 message = catenate_messages (gmsgid, " before %qs token");
9178 error (message, cpp_type2name (token_type, token_flags));
9179 free (message);
9180 message = NULL;
9182 else
9183 error (gmsgid);
9185 if (message)
9187 error (message);
9188 free (message);
9190 #undef catenate_messages
9193 /* Mapping for cpp message reasons to the options that enable them. */
9195 struct reason_option_codes_t
9197 const int reason; /* cpplib message reason. */
9198 const int option_code; /* gcc option that controls this message. */
9201 static const struct reason_option_codes_t option_codes[] = {
9202 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9203 {CPP_W_COMMENTS, OPT_Wcomment},
9204 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9205 {CPP_W_MULTICHAR, OPT_Wmultichar},
9206 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9207 {CPP_W_LONG_LONG, OPT_Wlong_long},
9208 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9209 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9210 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9211 {CPP_W_UNDEF, OPT_Wundef},
9212 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9213 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9214 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9215 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9216 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9217 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9218 {CPP_W_NONE, 0}
9221 /* Return the gcc option code associated with the reason for a cpp
9222 message, or 0 if none. */
9224 static int
9225 c_option_controlling_cpp_error (int reason)
9227 const struct reason_option_codes_t *entry;
9229 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9231 if (entry->reason == reason)
9232 return entry->option_code;
9234 return 0;
9237 /* Callback from cpp_error for PFILE to print diagnostics from the
9238 preprocessor. The diagnostic is of type LEVEL, with REASON set
9239 to the reason code if LEVEL is represents a warning, at location
9240 LOCATION unless this is after lexing and the compiler's location
9241 should be used instead, with column number possibly overridden by
9242 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9243 the arguments. Returns true if a diagnostic was emitted, false
9244 otherwise. */
9246 bool
9247 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9248 location_t location, unsigned int column_override,
9249 const char *msg, va_list *ap)
9251 diagnostic_info diagnostic;
9252 diagnostic_t dlevel;
9253 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9254 bool ret;
9256 switch (level)
9258 case CPP_DL_WARNING_SYSHDR:
9259 if (flag_no_output)
9260 return false;
9261 global_dc->dc_warn_system_headers = 1;
9262 /* Fall through. */
9263 case CPP_DL_WARNING:
9264 if (flag_no_output)
9265 return false;
9266 dlevel = DK_WARNING;
9267 break;
9268 case CPP_DL_PEDWARN:
9269 if (flag_no_output && !flag_pedantic_errors)
9270 return false;
9271 dlevel = DK_PEDWARN;
9272 break;
9273 case CPP_DL_ERROR:
9274 dlevel = DK_ERROR;
9275 break;
9276 case CPP_DL_ICE:
9277 dlevel = DK_ICE;
9278 break;
9279 case CPP_DL_NOTE:
9280 dlevel = DK_NOTE;
9281 break;
9282 case CPP_DL_FATAL:
9283 dlevel = DK_FATAL;
9284 break;
9285 default:
9286 gcc_unreachable ();
9288 if (done_lexing)
9289 location = input_location;
9290 diagnostic_set_info_translated (&diagnostic, msg, ap,
9291 location, dlevel);
9292 if (column_override)
9293 diagnostic_override_column (&diagnostic, column_override);
9294 diagnostic_override_option_index (&diagnostic,
9295 c_option_controlling_cpp_error (reason));
9296 ret = report_diagnostic (&diagnostic);
9297 if (level == CPP_DL_WARNING_SYSHDR)
9298 global_dc->dc_warn_system_headers = save_warn_system_headers;
9299 return ret;
9302 /* Convert a character from the host to the target execution character
9303 set. cpplib handles this, mostly. */
9305 HOST_WIDE_INT
9306 c_common_to_target_charset (HOST_WIDE_INT c)
9308 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9309 zero-extended under -fno-signed-char. cpplib insists that characters
9310 and character constants are always unsigned. Hence we must convert
9311 back and forth. */
9312 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9314 uc = cpp_host_to_exec_charset (parse_in, uc);
9316 if (flag_signed_char)
9317 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9318 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9319 else
9320 return uc;
9323 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9324 references with an INDIRECT_REF of a constant at the bottom; much like the
9325 traditional rendering of offsetof as a macro. Return the folded result. */
9327 tree
9328 fold_offsetof_1 (tree expr)
9330 tree base, off, t;
9332 switch (TREE_CODE (expr))
9334 case ERROR_MARK:
9335 return expr;
9337 case VAR_DECL:
9338 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9339 return error_mark_node;
9341 case CALL_EXPR:
9342 case TARGET_EXPR:
9343 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9344 return error_mark_node;
9346 case NOP_EXPR:
9347 case INDIRECT_REF:
9348 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9350 error ("cannot apply %<offsetof%> to a non constant address");
9351 return error_mark_node;
9353 return TREE_OPERAND (expr, 0);
9355 case COMPONENT_REF:
9356 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9357 if (base == error_mark_node)
9358 return base;
9360 t = TREE_OPERAND (expr, 1);
9361 if (DECL_C_BIT_FIELD (t))
9363 error ("attempt to take address of bit-field structure "
9364 "member %qD", t);
9365 return error_mark_node;
9367 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9368 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9370 / BITS_PER_UNIT));
9371 break;
9373 case ARRAY_REF:
9374 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9375 if (base == error_mark_node)
9376 return base;
9378 t = TREE_OPERAND (expr, 1);
9380 /* Check if the offset goes beyond the upper bound of the array. */
9381 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9383 tree upbound = array_ref_up_bound (expr);
9384 if (upbound != NULL_TREE
9385 && TREE_CODE (upbound) == INTEGER_CST
9386 && !tree_int_cst_equal (upbound,
9387 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9389 upbound = size_binop (PLUS_EXPR, upbound,
9390 build_int_cst (TREE_TYPE (upbound), 1));
9391 if (tree_int_cst_lt (upbound, t))
9393 tree v;
9395 for (v = TREE_OPERAND (expr, 0);
9396 TREE_CODE (v) == COMPONENT_REF;
9397 v = TREE_OPERAND (v, 0))
9398 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9399 == RECORD_TYPE)
9401 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9402 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9403 if (TREE_CODE (fld_chain) == FIELD_DECL)
9404 break;
9406 if (fld_chain)
9407 break;
9409 /* Don't warn if the array might be considered a poor
9410 man's flexible array member with a very permissive
9411 definition thereof. */
9412 if (TREE_CODE (v) == ARRAY_REF
9413 || TREE_CODE (v) == COMPONENT_REF)
9414 warning (OPT_Warray_bounds,
9415 "index %E denotes an offset "
9416 "greater than size of %qT",
9417 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9422 t = convert (sizetype, t);
9423 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9424 break;
9426 case COMPOUND_EXPR:
9427 /* Handle static members of volatile structs. */
9428 t = TREE_OPERAND (expr, 1);
9429 gcc_assert (TREE_CODE (t) == VAR_DECL);
9430 return fold_offsetof_1 (t);
9432 default:
9433 gcc_unreachable ();
9436 return fold_build_pointer_plus (base, off);
9439 /* Likewise, but convert it to the return type of offsetof. */
9441 tree
9442 fold_offsetof (tree expr)
9444 return convert (size_type_node, fold_offsetof_1 (expr));
9447 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9448 expression, because B will always be true. */
9450 void
9451 warn_for_omitted_condop (location_t location, tree cond)
9453 if (truth_value_p (TREE_CODE (cond)))
9454 warning_at (location, OPT_Wparentheses,
9455 "the omitted middle operand in ?: will always be %<true%>, "
9456 "suggest explicit middle operand");
9459 /* Give an error for storing into ARG, which is 'const'. USE indicates
9460 how ARG was being used. */
9462 void
9463 readonly_error (tree arg, enum lvalue_use use)
9465 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9466 || use == lv_asm);
9467 /* Using this macro rather than (for example) arrays of messages
9468 ensures that all the format strings are checked at compile
9469 time. */
9470 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9471 : (use == lv_increment ? (I) \
9472 : (use == lv_decrement ? (D) : (AS))))
9473 if (TREE_CODE (arg) == COMPONENT_REF)
9475 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9476 error (READONLY_MSG (G_("assignment of member "
9477 "%qD in read-only object"),
9478 G_("increment of member "
9479 "%qD in read-only object"),
9480 G_("decrement of member "
9481 "%qD in read-only object"),
9482 G_("member %qD in read-only object "
9483 "used as %<asm%> output")),
9484 TREE_OPERAND (arg, 1));
9485 else
9486 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9487 G_("increment of read-only member %qD"),
9488 G_("decrement of read-only member %qD"),
9489 G_("read-only member %qD used as %<asm%> output")),
9490 TREE_OPERAND (arg, 1));
9492 else if (TREE_CODE (arg) == VAR_DECL)
9493 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9494 G_("increment of read-only variable %qD"),
9495 G_("decrement of read-only variable %qD"),
9496 G_("read-only variable %qD used as %<asm%> output")),
9497 arg);
9498 else if (TREE_CODE (arg) == PARM_DECL)
9499 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9500 G_("increment of read-only parameter %qD"),
9501 G_("decrement of read-only parameter %qD"),
9502 G_("read-only parameter %qD use as %<asm%> output")),
9503 arg);
9504 else if (TREE_CODE (arg) == RESULT_DECL)
9506 gcc_assert (c_dialect_cxx ());
9507 error (READONLY_MSG (G_("assignment of "
9508 "read-only named return value %qD"),
9509 G_("increment of "
9510 "read-only named return value %qD"),
9511 G_("decrement of "
9512 "read-only named return value %qD"),
9513 G_("read-only named return value %qD "
9514 "used as %<asm%>output")),
9515 arg);
9517 else if (TREE_CODE (arg) == FUNCTION_DECL)
9518 error (READONLY_MSG (G_("assignment of function %qD"),
9519 G_("increment of function %qD"),
9520 G_("decrement of function %qD"),
9521 G_("function %qD used as %<asm%> output")),
9522 arg);
9523 else
9524 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9525 G_("increment of read-only location %qE"),
9526 G_("decrement of read-only location %qE"),
9527 G_("read-only location %qE used as %<asm%> output")),
9528 arg);
9531 /* Print an error message for an invalid lvalue. USE says
9532 how the lvalue is being used and so selects the error message. LOC
9533 is the location for the error. */
9535 void
9536 lvalue_error (location_t loc, enum lvalue_use use)
9538 switch (use)
9540 case lv_assign:
9541 error_at (loc, "lvalue required as left operand of assignment");
9542 break;
9543 case lv_increment:
9544 error_at (loc, "lvalue required as increment operand");
9545 break;
9546 case lv_decrement:
9547 error_at (loc, "lvalue required as decrement operand");
9548 break;
9549 case lv_addressof:
9550 error_at (loc, "lvalue required as unary %<&%> operand");
9551 break;
9552 case lv_asm:
9553 error_at (loc, "lvalue required in asm statement");
9554 break;
9555 default:
9556 gcc_unreachable ();
9560 /* Print an error message for an invalid indirection of type TYPE.
9561 ERRSTRING is the name of the operator for the indirection. */
9563 void
9564 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9566 switch (errstring)
9568 case RO_NULL:
9569 gcc_assert (c_dialect_cxx ());
9570 error_at (loc, "invalid type argument (have %qT)", type);
9571 break;
9572 case RO_ARRAY_INDEXING:
9573 error_at (loc,
9574 "invalid type argument of array indexing (have %qT)",
9575 type);
9576 break;
9577 case RO_UNARY_STAR:
9578 error_at (loc,
9579 "invalid type argument of unary %<*%> (have %qT)",
9580 type);
9581 break;
9582 case RO_ARROW:
9583 error_at (loc,
9584 "invalid type argument of %<->%> (have %qT)",
9585 type);
9586 break;
9587 case RO_IMPLICIT_CONVERSION:
9588 error_at (loc,
9589 "invalid type argument of implicit conversion (have %qT)",
9590 type);
9591 break;
9592 default:
9593 gcc_unreachable ();
9597 /* *PTYPE is an incomplete array. Complete it with a domain based on
9598 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9599 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9600 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9603 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9605 tree maxindex, type, main_type, elt, unqual_elt;
9606 int failure = 0, quals;
9607 hashval_t hashcode = 0;
9609 maxindex = size_zero_node;
9610 if (initial_value)
9612 if (TREE_CODE (initial_value) == STRING_CST)
9614 int eltsize
9615 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9616 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9618 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9620 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
9622 if (VEC_empty (constructor_elt, v))
9624 if (pedantic)
9625 failure = 3;
9626 maxindex = ssize_int (-1);
9628 else
9630 tree curindex;
9631 unsigned HOST_WIDE_INT cnt;
9632 constructor_elt *ce;
9633 bool fold_p = false;
9635 if (VEC_index (constructor_elt, v, 0).index)
9636 maxindex = fold_convert_loc (input_location, sizetype,
9637 VEC_index (constructor_elt,
9638 v, 0).index);
9639 curindex = maxindex;
9641 for (cnt = 1;
9642 VEC_iterate (constructor_elt, v, cnt, ce);
9643 cnt++)
9645 bool curfold_p = false;
9646 if (ce->index)
9647 curindex = ce->index, curfold_p = true;
9648 else
9650 if (fold_p)
9651 curindex = fold_convert (sizetype, curindex);
9652 curindex = size_binop (PLUS_EXPR, curindex,
9653 size_one_node);
9655 if (tree_int_cst_lt (maxindex, curindex))
9656 maxindex = curindex, fold_p = curfold_p;
9658 if (fold_p)
9659 maxindex = fold_convert (sizetype, maxindex);
9662 else
9664 /* Make an error message unless that happened already. */
9665 if (initial_value != error_mark_node)
9666 failure = 1;
9669 else
9671 failure = 2;
9672 if (!do_default)
9673 return failure;
9676 type = *ptype;
9677 elt = TREE_TYPE (type);
9678 quals = TYPE_QUALS (strip_array_types (elt));
9679 if (quals == 0)
9680 unqual_elt = elt;
9681 else
9682 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
9684 /* Using build_distinct_type_copy and modifying things afterward instead
9685 of using build_array_type to create a new type preserves all of the
9686 TYPE_LANG_FLAG_? bits that the front end may have set. */
9687 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9688 TREE_TYPE (main_type) = unqual_elt;
9689 TYPE_DOMAIN (main_type)
9690 = build_range_type (TREE_TYPE (maxindex),
9691 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
9692 layout_type (main_type);
9694 /* Make sure we have the canonical MAIN_TYPE. */
9695 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
9696 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
9697 hashcode);
9698 main_type = type_hash_canon (hashcode, main_type);
9700 /* Fix the canonical type. */
9701 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
9702 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
9703 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
9704 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
9705 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
9706 != TYPE_DOMAIN (main_type)))
9707 TYPE_CANONICAL (main_type)
9708 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
9709 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
9710 else
9711 TYPE_CANONICAL (main_type) = main_type;
9713 if (quals == 0)
9714 type = main_type;
9715 else
9716 type = c_build_qualified_type (main_type, quals);
9718 if (COMPLETE_TYPE_P (type)
9719 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9720 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9722 error ("size of array is too large");
9723 /* If we proceed with the array type as it is, we'll eventually
9724 crash in tree_low_cst(). */
9725 type = error_mark_node;
9728 *ptype = type;
9729 return failure;
9732 /* Like c_mark_addressable but don't check register qualifier. */
9733 void
9734 c_common_mark_addressable_vec (tree t)
9736 while (handled_component_p (t))
9737 t = TREE_OPERAND (t, 0);
9738 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9739 return;
9740 TREE_ADDRESSABLE (t) = 1;
9745 /* Used to help initialize the builtin-types.def table. When a type of
9746 the correct size doesn't exist, use error_mark_node instead of NULL.
9747 The later results in segfaults even when a decl using the type doesn't
9748 get invoked. */
9750 tree
9751 builtin_type_for_size (int size, bool unsignedp)
9753 tree type = c_common_type_for_size (size, unsignedp);
9754 return type ? type : error_mark_node;
9757 /* A helper function for resolve_overloaded_builtin in resolving the
9758 overloaded __sync_ builtins. Returns a positive power of 2 if the
9759 first operand of PARAMS is a pointer to a supported data type.
9760 Returns 0 if an error is encountered. */
9762 static int
9763 sync_resolve_size (tree function, VEC(tree,gc) *params)
9765 tree type;
9766 int size;
9768 if (VEC_empty (tree, params))
9770 error ("too few arguments to function %qE", function);
9771 return 0;
9774 type = TREE_TYPE (VEC_index (tree, params, 0));
9775 if (TREE_CODE (type) != POINTER_TYPE)
9776 goto incompatible;
9778 type = TREE_TYPE (type);
9779 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9780 goto incompatible;
9782 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
9783 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
9784 return size;
9786 incompatible:
9787 error ("incompatible type for argument %d of %qE", 1, function);
9788 return 0;
9791 /* A helper function for resolve_overloaded_builtin. Adds casts to
9792 PARAMS to make arguments match up with those of FUNCTION. Drops
9793 the variadic arguments at the end. Returns false if some error
9794 was encountered; true on success. */
9796 static bool
9797 sync_resolve_params (location_t loc, tree orig_function, tree function,
9798 VEC(tree, gc) *params, bool orig_format)
9800 function_args_iterator iter;
9801 tree ptype;
9802 unsigned int parmnum;
9804 function_args_iter_init (&iter, TREE_TYPE (function));
9805 /* We've declared the implementation functions to use "volatile void *"
9806 as the pointer parameter, so we shouldn't get any complaints from the
9807 call to check_function_arguments what ever type the user used. */
9808 function_args_iter_next (&iter);
9809 ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
9811 /* For the rest of the values, we need to cast these to FTYPE, so that we
9812 don't get warnings for passing pointer types, etc. */
9813 parmnum = 0;
9814 while (1)
9816 tree val, arg_type;
9818 arg_type = function_args_iter_cond (&iter);
9819 /* XXX void_type_node belies the abstraction. */
9820 if (arg_type == void_type_node)
9821 break;
9823 ++parmnum;
9824 if (VEC_length (tree, params) <= parmnum)
9826 error_at (loc, "too few arguments to function %qE", orig_function);
9827 return false;
9830 /* Only convert parameters if arg_type is unsigned integer type with
9831 new format sync routines, i.e. don't attempt to convert pointer
9832 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
9833 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
9834 kinds). */
9835 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
9837 /* Ideally for the first conversion we'd use convert_for_assignment
9838 so that we get warnings for anything that doesn't match the pointer
9839 type. This isn't portable across the C and C++ front ends atm. */
9840 val = VEC_index (tree, params, parmnum);
9841 val = convert (ptype, val);
9842 val = convert (arg_type, val);
9843 VEC_replace (tree, params, parmnum, val);
9846 function_args_iter_next (&iter);
9849 /* __atomic routines are not variadic. */
9850 if (!orig_format && VEC_length (tree, params) != parmnum + 1)
9852 error_at (loc, "too many arguments to function %qE", orig_function);
9853 return false;
9856 /* The definition of these primitives is variadic, with the remaining
9857 being "an optional list of variables protected by the memory barrier".
9858 No clue what that's supposed to mean, precisely, but we consider all
9859 call-clobbered variables to be protected so we're safe. */
9860 VEC_truncate (tree, params, parmnum + 1);
9862 return true;
9865 /* A helper function for resolve_overloaded_builtin. Adds a cast to
9866 RESULT to make it match the type of the first pointer argument in
9867 PARAMS. */
9869 static tree
9870 sync_resolve_return (tree first_param, tree result, bool orig_format)
9872 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
9873 tree rtype = TREE_TYPE (result);
9874 ptype = TYPE_MAIN_VARIANT (ptype);
9876 /* New format doesn't require casting unless the types are the same size. */
9877 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
9878 return convert (ptype, result);
9879 else
9880 return result;
9883 /* This function verifies the PARAMS to generic atomic FUNCTION.
9884 It returns the size if all the parameters are the same size, otherwise
9885 0 is returned if the parameters are invalid. */
9887 static int
9888 get_atomic_generic_size (location_t loc, tree function, VEC(tree,gc) *params)
9890 unsigned int n_param;
9891 unsigned int n_model;
9892 unsigned int x;
9893 int size_0;
9894 tree type_0;
9896 /* Determine the parameter makeup. */
9897 switch (DECL_FUNCTION_CODE (function))
9899 case BUILT_IN_ATOMIC_EXCHANGE:
9900 n_param = 4;
9901 n_model = 1;
9902 break;
9903 case BUILT_IN_ATOMIC_LOAD:
9904 case BUILT_IN_ATOMIC_STORE:
9905 n_param = 3;
9906 n_model = 1;
9907 break;
9908 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9909 n_param = 6;
9910 n_model = 2;
9911 break;
9912 default:
9913 gcc_unreachable ();
9916 if (VEC_length (tree, params) != n_param)
9918 error_at (loc, "incorrect number of arguments to function %qE", function);
9919 return 0;
9922 /* Get type of first parameter, and determine its size. */
9923 type_0 = TREE_TYPE (VEC_index (tree, params, 0));
9924 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
9926 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
9927 function);
9928 return 0;
9931 /* Types must be compile time constant sizes. */
9932 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
9934 error_at (loc,
9935 "argument 1 of %qE must be a pointer to a constant size type",
9936 function);
9937 return 0;
9940 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
9942 /* Zero size objects are not allowed. */
9943 if (size_0 == 0)
9945 error_at (loc,
9946 "argument 1 of %qE must be a pointer to a nonzero size object",
9947 function);
9948 return 0;
9951 /* Check each other parameter is a pointer and the same size. */
9952 for (x = 0; x < n_param - n_model; x++)
9954 int size;
9955 tree type = TREE_TYPE (VEC_index (tree, params, x));
9956 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */
9957 if (n_param == 6 && x == 3)
9958 continue;
9959 if (!POINTER_TYPE_P (type))
9961 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
9962 function);
9963 return 0;
9965 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
9966 if (size != size_0)
9968 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
9969 function);
9970 return 0;
9974 /* Check memory model parameters for validity. */
9975 for (x = n_param - n_model ; x < n_param; x++)
9977 tree p = VEC_index (tree, params, x);
9978 if (TREE_CODE (p) == INTEGER_CST)
9980 int i = tree_low_cst (p, 1);
9981 if (i < 0 || i >= MEMMODEL_LAST)
9983 warning_at (loc, OPT_Winvalid_memory_model,
9984 "invalid memory model argument %d of %qE", x + 1,
9985 function);
9988 else
9989 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
9991 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
9992 function);
9993 return 0;
9997 return size_0;
10001 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10002 at the beginning of the parameter list PARAMS representing the size of the
10003 objects. This is to match the library ABI requirement. LOC is the location
10004 of the function call.
10005 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10006 returned to allow the external call to be constructed. */
10008 static tree
10009 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10010 VEC(tree,gc) *params)
10012 tree size_node;
10014 /* Insert a SIZE_T parameter as the first param. If there isn't
10015 enough space, allocate a new vector and recursively re-build with that. */
10016 if (!VEC_space (tree, params, 1))
10018 unsigned int z, len;
10019 VEC(tree,gc) *vec;
10020 tree f;
10022 len = VEC_length (tree, params);
10023 vec = VEC_alloc (tree, gc, len + 1);
10024 for (z = 0; z < len; z++)
10025 VEC_quick_push (tree, vec, VEC_index (tree, params, z));
10026 f = build_function_call_vec (loc, function, vec, NULL);
10027 VEC_free (tree, gc, vec);
10028 return f;
10031 /* Add the size parameter and leave as a function call for processing. */
10032 size_node = build_int_cst (size_type_node, n);
10033 VEC_quick_insert (tree, params, 0, size_node);
10034 return NULL_TREE;
10038 /* This will process an __atomic_exchange function call, determine whether it
10039 needs to be mapped to the _N variation, or turned into a library call.
10040 LOC is the location of the builtin call.
10041 FUNCTION is the DECL that has been invoked;
10042 PARAMS is the argument list for the call. The return value is non-null
10043 TRUE is returned if it is translated into the proper format for a call to the
10044 external library, and NEW_RETURN is set the tree for that function.
10045 FALSE is returned if processing for the _N variation is required, and
10046 NEW_RETURN is set to the the return value the result is copied into. */
10047 static bool
10048 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10049 VEC(tree,gc) *params, tree *new_return)
10051 tree p0, p1, p2, p3;
10052 tree I_type, I_type_ptr;
10053 int n = get_atomic_generic_size (loc, function, params);
10055 /* Size of 0 is an error condition. */
10056 if (n == 0)
10058 *new_return = error_mark_node;
10059 return true;
10062 /* If not a lock-free size, change to the library generic format. */
10063 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10065 *new_return = add_atomic_size_parameter (n, loc, function, params);
10066 return true;
10069 /* Otherwise there is a lockfree match, transform the call from:
10070 void fn(T* mem, T* desired, T* return, model)
10071 into
10072 *return = (T) (fn (In* mem, (In) *desired, model)) */
10074 p0 = VEC_index (tree, params, 0);
10075 p1 = VEC_index (tree, params, 1);
10076 p2 = VEC_index (tree, params, 2);
10077 p3 = VEC_index (tree, params, 3);
10079 /* Create pointer to appropriate size. */
10080 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10081 I_type_ptr = build_pointer_type (I_type);
10083 /* Convert object pointer to required type. */
10084 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10085 VEC_replace (tree, params, 0, p0);
10086 /* Convert new value to required type, and dereference it. */
10087 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10088 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10089 VEC_replace (tree, params, 1, p1);
10091 /* Move memory model to the 3rd position, and end param list. */
10092 VEC_replace (tree, params, 2, p3);
10093 VEC_truncate (tree, params, 3);
10095 /* Convert return pointer and dereference it for later assignment. */
10096 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10098 return false;
10102 /* This will process an __atomic_compare_exchange function call, determine
10103 whether it needs to be mapped to the _N variation, or turned into a lib call.
10104 LOC is the location of the builtin call.
10105 FUNCTION is the DECL that has been invoked;
10106 PARAMS is the argument list for the call. The return value is non-null
10107 TRUE is returned if it is translated into the proper format for a call to the
10108 external library, and NEW_RETURN is set the tree for that function.
10109 FALSE is returned if processing for the _N variation is required. */
10111 static bool
10112 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10113 VEC(tree,gc) *params,
10114 tree *new_return)
10116 tree p0, p1, p2;
10117 tree I_type, I_type_ptr;
10118 int n = get_atomic_generic_size (loc, function, params);
10120 /* Size of 0 is an error condition. */
10121 if (n == 0)
10123 *new_return = error_mark_node;
10124 return true;
10127 /* If not a lock-free size, change to the library generic format. */
10128 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10130 /* The library generic format does not have the weak parameter, so
10131 remove it from the param list. Since a parameter has been removed,
10132 we can be sure that there is room for the SIZE_T parameter, meaning
10133 there will not be a recursive rebuilding of the parameter list, so
10134 there is no danger this will be done twice. */
10135 if (n > 0)
10137 VEC_replace (tree, params, 3, VEC_index (tree, params, 4));
10138 VEC_replace (tree, params, 4, VEC_index (tree, params, 5));
10139 VEC_truncate (tree, params, 5);
10141 *new_return = add_atomic_size_parameter (n, loc, function, params);
10142 return true;
10145 /* Otherwise, there is a match, so the call needs to be transformed from:
10146 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10147 into
10148 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10150 p0 = VEC_index (tree, params, 0);
10151 p1 = VEC_index (tree, params, 1);
10152 p2 = VEC_index (tree, params, 2);
10154 /* Create pointer to appropriate size. */
10155 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10156 I_type_ptr = build_pointer_type (I_type);
10158 /* Convert object pointer to required type. */
10159 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10160 VEC_replace (tree, params, 0, p0);
10162 /* Convert expected pointer to required type. */
10163 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10164 VEC_replace (tree, params, 1, p1);
10166 /* Convert desired value to required type, and dereference it. */
10167 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10168 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10169 VEC_replace (tree, params, 2, p2);
10171 /* The rest of the parameters are fine. NULL means no special return value
10172 processing.*/
10173 *new_return = NULL;
10174 return false;
10178 /* This will process an __atomic_load function call, determine whether it
10179 needs to be mapped to the _N variation, or turned into a library call.
10180 LOC is the location of the builtin call.
10181 FUNCTION is the DECL that has been invoked;
10182 PARAMS is the argument list for the call. The return value is non-null
10183 TRUE is returned if it is translated into the proper format for a call to the
10184 external library, and NEW_RETURN is set the tree for that function.
10185 FALSE is returned if processing for the _N variation is required, and
10186 NEW_RETURN is set to the the return value the result is copied into. */
10188 static bool
10189 resolve_overloaded_atomic_load (location_t loc, tree function,
10190 VEC(tree,gc) *params, tree *new_return)
10192 tree p0, p1, p2;
10193 tree I_type, I_type_ptr;
10194 int n = get_atomic_generic_size (loc, function, params);
10196 /* Size of 0 is an error condition. */
10197 if (n == 0)
10199 *new_return = error_mark_node;
10200 return true;
10203 /* If not a lock-free size, change to the library generic format. */
10204 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10206 *new_return = add_atomic_size_parameter (n, loc, function, params);
10207 return true;
10210 /* Otherwise, there is a match, so the call needs to be transformed from:
10211 void fn(T* mem, T* return, model)
10212 into
10213 *return = (T) (fn ((In *) mem, model)) */
10215 p0 = VEC_index (tree, params, 0);
10216 p1 = VEC_index (tree, params, 1);
10217 p2 = VEC_index (tree, params, 2);
10219 /* Create pointer to appropriate size. */
10220 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10221 I_type_ptr = build_pointer_type (I_type);
10223 /* Convert object pointer to required type. */
10224 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10225 VEC_replace (tree, params, 0, p0);
10227 /* Move memory model to the 2nd position, and end param list. */
10228 VEC_replace (tree, params, 1, p2);
10229 VEC_truncate (tree, params, 2);
10231 /* Convert return pointer and dereference it for later assignment. */
10232 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10234 return false;
10238 /* This will process an __atomic_store function call, determine whether it
10239 needs to be mapped to the _N variation, or turned into a library call.
10240 LOC is the location of the builtin call.
10241 FUNCTION is the DECL that has been invoked;
10242 PARAMS is the argument list for the call. The return value is non-null
10243 TRUE is returned if it is translated into the proper format for a call to the
10244 external library, and NEW_RETURN is set the tree for that function.
10245 FALSE is returned if processing for the _N variation is required, and
10246 NEW_RETURN is set to the the return value the result is copied into. */
10248 static bool
10249 resolve_overloaded_atomic_store (location_t loc, tree function,
10250 VEC(tree,gc) *params, tree *new_return)
10252 tree p0, p1;
10253 tree I_type, I_type_ptr;
10254 int n = get_atomic_generic_size (loc, function, params);
10256 /* Size of 0 is an error condition. */
10257 if (n == 0)
10259 *new_return = error_mark_node;
10260 return true;
10263 /* If not a lock-free size, change to the library generic format. */
10264 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10266 *new_return = add_atomic_size_parameter (n, loc, function, params);
10267 return true;
10270 /* Otherwise, there is a match, so the call needs to be transformed from:
10271 void fn(T* mem, T* value, model)
10272 into
10273 fn ((In *) mem, (In) *value, model) */
10275 p0 = VEC_index (tree, params, 0);
10276 p1 = VEC_index (tree, params, 1);
10278 /* Create pointer to appropriate size. */
10279 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10280 I_type_ptr = build_pointer_type (I_type);
10282 /* Convert object pointer to required type. */
10283 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10284 VEC_replace (tree, params, 0, p0);
10286 /* Convert new value to required type, and dereference it. */
10287 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10288 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10289 VEC_replace (tree, params, 1, p1);
10291 /* The memory model is in the right spot already. Return is void. */
10292 *new_return = NULL_TREE;
10294 return false;
10298 /* Some builtin functions are placeholders for other expressions. This
10299 function should be called immediately after parsing the call expression
10300 before surrounding code has committed to the type of the expression.
10302 LOC is the location of the builtin call.
10304 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10305 PARAMS is the argument list for the call. The return value is non-null
10306 when expansion is complete, and null if normal processing should
10307 continue. */
10309 tree
10310 resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params)
10312 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10313 bool orig_format = true;
10314 tree new_return = NULL_TREE;
10316 switch (DECL_BUILT_IN_CLASS (function))
10318 case BUILT_IN_NORMAL:
10319 break;
10320 case BUILT_IN_MD:
10321 if (targetm.resolve_overloaded_builtin)
10322 return targetm.resolve_overloaded_builtin (loc, function, params);
10323 else
10324 return NULL_TREE;
10325 default:
10326 return NULL_TREE;
10329 /* Handle BUILT_IN_NORMAL here. */
10330 switch (orig_code)
10332 case BUILT_IN_ATOMIC_EXCHANGE:
10333 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10334 case BUILT_IN_ATOMIC_LOAD:
10335 case BUILT_IN_ATOMIC_STORE:
10337 /* Handle these 4 together so that they can fall through to the next
10338 case if the call is transformed to an _N variant. */
10339 switch (orig_code)
10341 case BUILT_IN_ATOMIC_EXCHANGE:
10343 if (resolve_overloaded_atomic_exchange (loc, function, params,
10344 &new_return))
10345 return new_return;
10346 /* Change to the _N variant. */
10347 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10348 break;
10351 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10353 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10354 params,
10355 &new_return))
10356 return new_return;
10357 /* Change to the _N variant. */
10358 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10359 break;
10361 case BUILT_IN_ATOMIC_LOAD:
10363 if (resolve_overloaded_atomic_load (loc, function, params,
10364 &new_return))
10365 return new_return;
10366 /* Change to the _N variant. */
10367 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10368 break;
10370 case BUILT_IN_ATOMIC_STORE:
10372 if (resolve_overloaded_atomic_store (loc, function, params,
10373 &new_return))
10374 return new_return;
10375 /* Change to the _N variant. */
10376 orig_code = BUILT_IN_ATOMIC_STORE_N;
10377 break;
10379 default:
10380 gcc_unreachable ();
10382 /* Fallthrough to the normal processing. */
10384 case BUILT_IN_ATOMIC_EXCHANGE_N:
10385 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10386 case BUILT_IN_ATOMIC_LOAD_N:
10387 case BUILT_IN_ATOMIC_STORE_N:
10388 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10389 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10390 case BUILT_IN_ATOMIC_AND_FETCH_N:
10391 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10392 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10393 case BUILT_IN_ATOMIC_OR_FETCH_N:
10394 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10395 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10396 case BUILT_IN_ATOMIC_FETCH_AND_N:
10397 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10398 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10399 case BUILT_IN_ATOMIC_FETCH_OR_N:
10401 orig_format = false;
10402 /* Fallthru for parameter processing. */
10404 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10405 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10406 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10407 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10408 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10409 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10410 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10411 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10412 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10413 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10414 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10415 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10416 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10417 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10418 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10419 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10421 int n = sync_resolve_size (function, params);
10422 tree new_function, first_param, result;
10423 enum built_in_function fncode;
10425 if (n == 0)
10426 return error_mark_node;
10428 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10429 new_function = builtin_decl_explicit (fncode);
10430 if (!sync_resolve_params (loc, function, new_function, params,
10431 orig_format))
10432 return error_mark_node;
10434 first_param = VEC_index (tree, params, 0);
10435 result = build_function_call_vec (loc, new_function, params, NULL);
10436 if (result == error_mark_node)
10437 return result;
10438 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10439 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10440 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10441 result = sync_resolve_return (first_param, result, orig_format);
10443 /* If new_return is set, assign function to that expr and cast the
10444 result to void since the generic interface returned void. */
10445 if (new_return)
10447 /* Cast function result from I{1,2,4,8,16} to the required type. */
10448 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10449 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10450 result);
10451 TREE_SIDE_EFFECTS (result) = 1;
10452 protected_set_expr_location (result, loc);
10453 result = convert (void_type_node, result);
10455 return result;
10458 default:
10459 return NULL_TREE;
10463 /* Ignoring their sign, return true if two scalar types are the same. */
10464 bool
10465 same_scalar_type_ignoring_signedness (tree t1, tree t2)
10467 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10469 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10470 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10471 || c2 == FIXED_POINT_TYPE));
10473 /* Equality works here because c_common_signed_type uses
10474 TYPE_MAIN_VARIANT. */
10475 return c_common_signed_type (t1)
10476 == c_common_signed_type (t2);
10479 /* Check for missing format attributes on function pointers. LTYPE is
10480 the new type or left-hand side type. RTYPE is the old type or
10481 right-hand side type. Returns TRUE if LTYPE is missing the desired
10482 attribute. */
10484 bool
10485 check_missing_format_attribute (tree ltype, tree rtype)
10487 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10488 tree ra;
10490 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10491 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10492 break;
10493 if (ra)
10495 tree la;
10496 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10497 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10498 break;
10499 return !la;
10501 else
10502 return false;
10505 /* Subscripting with type char is likely to lose on a machine where
10506 chars are signed. So warn on any machine, but optionally. Don't
10507 warn for unsigned char since that type is safe. Don't warn for
10508 signed char because anyone who uses that must have done so
10509 deliberately. Furthermore, we reduce the false positive load by
10510 warning only for non-constant value of type char. */
10512 void
10513 warn_array_subscript_with_type_char (tree index)
10515 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10516 && TREE_CODE (index) != INTEGER_CST)
10517 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10520 /* Implement -Wparentheses for the unexpected C precedence rules, to
10521 cover cases like x + y << z which readers are likely to
10522 misinterpret. We have seen an expression in which CODE is a binary
10523 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10524 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10525 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10526 expression was not formed using a binary or unary operator, or it
10527 was enclosed in parentheses. */
10529 void
10530 warn_about_parentheses (enum tree_code code,
10531 enum tree_code code_left, tree arg_left,
10532 enum tree_code code_right, tree arg_right)
10534 if (!warn_parentheses)
10535 return;
10537 /* This macro tests that the expression ARG with original tree code
10538 CODE appears to be a boolean expression. or the result of folding a
10539 boolean expression. */
10540 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10541 (truth_value_p (TREE_CODE (ARG)) \
10542 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10543 /* Folding may create 0 or 1 integers from other expressions. */ \
10544 || ((CODE) != INTEGER_CST \
10545 && (integer_onep (ARG) || integer_zerop (ARG))))
10547 switch (code)
10549 case LSHIFT_EXPR:
10550 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10551 warning (OPT_Wparentheses,
10552 "suggest parentheses around %<+%> inside %<<<%>");
10553 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10554 warning (OPT_Wparentheses,
10555 "suggest parentheses around %<-%> inside %<<<%>");
10556 return;
10558 case RSHIFT_EXPR:
10559 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10560 warning (OPT_Wparentheses,
10561 "suggest parentheses around %<+%> inside %<>>%>");
10562 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10563 warning (OPT_Wparentheses,
10564 "suggest parentheses around %<-%> inside %<>>%>");
10565 return;
10567 case TRUTH_ORIF_EXPR:
10568 if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR)
10569 warning (OPT_Wparentheses,
10570 "suggest parentheses around %<&&%> within %<||%>");
10571 return;
10573 case BIT_IOR_EXPR:
10574 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
10575 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
10576 || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10577 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10578 warning (OPT_Wparentheses,
10579 "suggest parentheses around arithmetic in operand of %<|%>");
10580 /* Check cases like x|y==z */
10581 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10582 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10583 warning (OPT_Wparentheses,
10584 "suggest parentheses around comparison in operand of %<|%>");
10585 /* Check cases like !x | y */
10586 else if (code_left == TRUTH_NOT_EXPR
10587 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10588 warning (OPT_Wparentheses, "suggest parentheses around operand of "
10589 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
10590 return;
10592 case BIT_XOR_EXPR:
10593 if (code_left == BIT_AND_EXPR
10594 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
10595 || code_right == BIT_AND_EXPR
10596 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10597 warning (OPT_Wparentheses,
10598 "suggest parentheses around arithmetic in operand of %<^%>");
10599 /* Check cases like x^y==z */
10600 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10601 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10602 warning (OPT_Wparentheses,
10603 "suggest parentheses around comparison in operand of %<^%>");
10604 return;
10606 case BIT_AND_EXPR:
10607 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10608 warning (OPT_Wparentheses,
10609 "suggest parentheses around %<+%> in operand of %<&%>");
10610 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10611 warning (OPT_Wparentheses,
10612 "suggest parentheses around %<-%> in operand of %<&%>");
10613 /* Check cases like x&y==z */
10614 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10615 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10616 warning (OPT_Wparentheses,
10617 "suggest parentheses around comparison in operand of %<&%>");
10618 /* Check cases like !x & y */
10619 else if (code_left == TRUTH_NOT_EXPR
10620 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10621 warning (OPT_Wparentheses, "suggest parentheses around operand of "
10622 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
10623 return;
10625 case EQ_EXPR:
10626 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10627 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10628 warning (OPT_Wparentheses,
10629 "suggest parentheses around comparison in operand of %<==%>");
10630 return;
10631 case NE_EXPR:
10632 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10633 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10634 warning (OPT_Wparentheses,
10635 "suggest parentheses around comparison in operand of %<!=%>");
10636 return;
10638 default:
10639 if (TREE_CODE_CLASS (code) == tcc_comparison
10640 && ((TREE_CODE_CLASS (code_left) == tcc_comparison
10641 && code_left != NE_EXPR && code_left != EQ_EXPR
10642 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
10643 || (TREE_CODE_CLASS (code_right) == tcc_comparison
10644 && code_right != NE_EXPR && code_right != EQ_EXPR
10645 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
10646 warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
10647 "have their mathematical meaning");
10648 return;
10650 #undef NOT_A_BOOLEAN_EXPR_P
10653 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
10655 void
10656 warn_for_unused_label (tree label)
10658 if (!TREE_USED (label))
10660 if (DECL_INITIAL (label))
10661 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
10662 else
10663 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
10667 /* Warn for division by zero according to the value of DIVISOR. LOC
10668 is the location of the division operator. */
10670 void
10671 warn_for_div_by_zero (location_t loc, tree divisor)
10673 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
10674 about division by zero. Do not issue a warning if DIVISOR has a
10675 floating-point type, since we consider 0.0/0.0 a valid way of
10676 generating a NaN. */
10677 if (c_inhibit_evaluation_warnings == 0
10678 && (integer_zerop (divisor) || fixed_zerop (divisor)))
10679 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
10682 /* Subroutine of build_binary_op. Give warnings for comparisons
10683 between signed and unsigned quantities that may fail. Do the
10684 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
10685 so that casts will be considered, but default promotions won't
10688 LOCATION is the location of the comparison operator.
10690 The arguments of this function map directly to local variables
10691 of build_binary_op. */
10693 void
10694 warn_for_sign_compare (location_t location,
10695 tree orig_op0, tree orig_op1,
10696 tree op0, tree op1,
10697 tree result_type, enum tree_code resultcode)
10699 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
10700 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
10701 int unsignedp0, unsignedp1;
10703 /* In C++, check for comparison of different enum types. */
10704 if (c_dialect_cxx()
10705 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
10706 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
10707 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
10708 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
10710 warning_at (location,
10711 OPT_Wsign_compare, "comparison between types %qT and %qT",
10712 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
10715 /* Do not warn if the comparison is being done in a signed type,
10716 since the signed type will only be chosen if it can represent
10717 all the values of the unsigned type. */
10718 if (!TYPE_UNSIGNED (result_type))
10719 /* OK */;
10720 /* Do not warn if both operands are unsigned. */
10721 else if (op0_signed == op1_signed)
10722 /* OK */;
10723 else
10725 tree sop, uop, base_type;
10726 bool ovf;
10728 if (op0_signed)
10729 sop = orig_op0, uop = orig_op1;
10730 else
10731 sop = orig_op1, uop = orig_op0;
10733 STRIP_TYPE_NOPS (sop);
10734 STRIP_TYPE_NOPS (uop);
10735 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
10736 ? TREE_TYPE (result_type) : result_type);
10738 /* Do not warn if the signed quantity is an unsuffixed integer
10739 literal (or some static constant expression involving such
10740 literals or a conditional expression involving such literals)
10741 and it is non-negative. */
10742 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
10743 /* OK */;
10744 /* Do not warn if the comparison is an equality operation, the
10745 unsigned quantity is an integral constant, and it would fit
10746 in the result if the result were signed. */
10747 else if (TREE_CODE (uop) == INTEGER_CST
10748 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
10749 && int_fits_type_p (uop, c_common_signed_type (base_type)))
10750 /* OK */;
10751 /* In C, do not warn if the unsigned quantity is an enumeration
10752 constant and its maximum value would fit in the result if the
10753 result were signed. */
10754 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
10755 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
10756 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
10757 c_common_signed_type (base_type)))
10758 /* OK */;
10759 else
10760 warning_at (location,
10761 OPT_Wsign_compare,
10762 "comparison between signed and unsigned integer expressions");
10765 /* Warn if two unsigned values are being compared in a size larger
10766 than their original size, and one (and only one) is the result of
10767 a `~' operator. This comparison will always fail.
10769 Also warn if one operand is a constant, and the constant does not
10770 have all bits set that are set in the ~ operand when it is
10771 extended. */
10773 op0 = c_common_get_narrower (op0, &unsignedp0);
10774 op1 = c_common_get_narrower (op1, &unsignedp1);
10776 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
10777 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
10779 if (TREE_CODE (op0) == BIT_NOT_EXPR)
10780 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
10781 if (TREE_CODE (op1) == BIT_NOT_EXPR)
10782 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
10784 if (host_integerp (op0, 0) || host_integerp (op1, 0))
10786 tree primop;
10787 HOST_WIDE_INT constant, mask;
10788 int unsignedp;
10789 unsigned int bits;
10791 if (host_integerp (op0, 0))
10793 primop = op1;
10794 unsignedp = unsignedp1;
10795 constant = tree_low_cst (op0, 0);
10797 else
10799 primop = op0;
10800 unsignedp = unsignedp0;
10801 constant = tree_low_cst (op1, 0);
10804 bits = TYPE_PRECISION (TREE_TYPE (primop));
10805 if (bits < TYPE_PRECISION (result_type)
10806 && bits < HOST_BITS_PER_LONG && unsignedp)
10808 mask = (~ (HOST_WIDE_INT) 0) << bits;
10809 if ((mask & constant) != mask)
10811 if (constant == 0)
10812 warning (OPT_Wsign_compare,
10813 "promoted ~unsigned is always non-zero");
10814 else
10815 warning_at (location, OPT_Wsign_compare,
10816 "comparison of promoted ~unsigned with constant");
10820 else if (unsignedp0 && unsignedp1
10821 && (TYPE_PRECISION (TREE_TYPE (op0))
10822 < TYPE_PRECISION (result_type))
10823 && (TYPE_PRECISION (TREE_TYPE (op1))
10824 < TYPE_PRECISION (result_type)))
10825 warning_at (location, OPT_Wsign_compare,
10826 "comparison of promoted ~unsigned with unsigned");
10830 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
10831 type via c_common_type. If -Wdouble-promotion is in use, and the
10832 conditions for warning have been met, issue a warning. GMSGID is
10833 the warning message. It must have two %T specifiers for the type
10834 that was converted (generally "float") and the type to which it was
10835 converted (generally "double), respectively. LOC is the location
10836 to which the awrning should refer. */
10838 void
10839 do_warn_double_promotion (tree result_type, tree type1, tree type2,
10840 const char *gmsgid, location_t loc)
10842 tree source_type;
10844 if (!warn_double_promotion)
10845 return;
10846 /* If the conversion will not occur at run-time, there is no need to
10847 warn about it. */
10848 if (c_inhibit_evaluation_warnings)
10849 return;
10850 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
10851 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
10852 return;
10853 if (TYPE_MAIN_VARIANT (type1) == float_type_node
10854 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
10855 source_type = type1;
10856 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
10857 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
10858 source_type = type2;
10859 else
10860 return;
10861 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
10864 /* Setup a TYPE_DECL node as a typedef representation.
10866 X is a TYPE_DECL for a typedef statement. Create a brand new
10867 ..._TYPE node (which will be just a variant of the existing
10868 ..._TYPE node with identical properties) and then install X
10869 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
10871 The whole point here is to end up with a situation where each
10872 and every ..._TYPE node the compiler creates will be uniquely
10873 associated with AT MOST one node representing a typedef name.
10874 This way, even though the compiler substitutes corresponding
10875 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
10876 early on, later parts of the compiler can always do the reverse
10877 translation and get back the corresponding typedef name. For
10878 example, given:
10880 typedef struct S MY_TYPE;
10881 MY_TYPE object;
10883 Later parts of the compiler might only know that `object' was of
10884 type `struct S' if it were not for code just below. With this
10885 code however, later parts of the compiler see something like:
10887 struct S' == struct S
10888 typedef struct S' MY_TYPE;
10889 struct S' object;
10891 And they can then deduce (from the node for type struct S') that
10892 the original object declaration was:
10894 MY_TYPE object;
10896 Being able to do this is important for proper support of protoize,
10897 and also for generating precise symbolic debugging information
10898 which takes full account of the programmer's (typedef) vocabulary.
10900 Obviously, we don't want to generate a duplicate ..._TYPE node if
10901 the TYPE_DECL node that we are now processing really represents a
10902 standard built-in type. */
10904 void
10905 set_underlying_type (tree x)
10907 if (x == error_mark_node)
10908 return;
10909 if (DECL_IS_BUILTIN (x))
10911 if (TYPE_NAME (TREE_TYPE (x)) == 0)
10912 TYPE_NAME (TREE_TYPE (x)) = x;
10914 else if (TREE_TYPE (x) != error_mark_node
10915 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
10917 tree tt = TREE_TYPE (x);
10918 DECL_ORIGINAL_TYPE (x) = tt;
10919 tt = build_variant_type_copy (tt);
10920 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
10921 TYPE_NAME (tt) = x;
10922 TREE_USED (tt) = TREE_USED (x);
10923 TREE_TYPE (x) = tt;
10927 /* Record the types used by the current global variable declaration
10928 being parsed, so that we can decide later to emit their debug info.
10929 Those types are in types_used_by_cur_var_decl, and we are going to
10930 store them in the types_used_by_vars_hash hash table.
10931 DECL is the declaration of the global variable that has been parsed. */
10933 void
10934 record_types_used_by_current_var_decl (tree decl)
10936 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
10938 while (!VEC_empty (tree, types_used_by_cur_var_decl))
10940 tree type = VEC_pop (tree, types_used_by_cur_var_decl);
10941 types_used_by_var_decl_insert (type, decl);
10945 /* If DECL is a typedef that is declared in the current function,
10946 record it for the purpose of -Wunused-local-typedefs. */
10948 void
10949 record_locally_defined_typedef (tree decl)
10951 struct c_language_function *l;
10953 if (!warn_unused_local_typedefs
10954 || cfun == NULL
10955 /* if this is not a locally defined typedef then we are not
10956 interested. */
10957 || !is_typedef_decl (decl)
10958 || !decl_function_context (decl))
10959 return;
10961 l = (struct c_language_function *) cfun->language;
10962 VEC_safe_push (tree, gc, l->local_typedefs, decl);
10965 /* If T is a TYPE_DECL declared locally, mark it as used. */
10967 void
10968 maybe_record_typedef_use (tree t)
10970 if (!is_typedef_decl (t))
10971 return;
10973 TREE_USED (t) = true;
10976 /* Warn if there are some unused locally defined typedefs in the
10977 current function. */
10979 void
10980 maybe_warn_unused_local_typedefs (void)
10982 int i;
10983 tree decl;
10984 /* The number of times we have emitted -Wunused-local-typedefs
10985 warnings. If this is different from errorcount, that means some
10986 unrelated errors have been issued. In which case, we'll avoid
10987 emitting "unused-local-typedefs" warnings. */
10988 static int unused_local_typedefs_warn_count;
10989 struct c_language_function *l;
10991 if (cfun == NULL)
10992 return;
10994 if ((l = (struct c_language_function *) cfun->language) == NULL)
10995 return;
10997 if (warn_unused_local_typedefs
10998 && errorcount == unused_local_typedefs_warn_count)
11000 FOR_EACH_VEC_ELT (tree, l->local_typedefs, i, decl)
11001 if (!TREE_USED (decl))
11002 warning_at (DECL_SOURCE_LOCATION (decl),
11003 OPT_Wunused_local_typedefs,
11004 "typedef %qD locally defined but not used", decl);
11005 unused_local_typedefs_warn_count = errorcount;
11008 if (l->local_typedefs)
11010 VEC_free (tree, gc, l->local_typedefs);
11011 l->local_typedefs = NULL;
11015 /* The C and C++ parsers both use vectors to hold function arguments.
11016 For efficiency, we keep a cache of unused vectors. This is the
11017 cache. */
11019 typedef VEC(tree,gc)* tree_gc_vec;
11020 DEF_VEC_P(tree_gc_vec);
11021 DEF_VEC_ALLOC_P(tree_gc_vec,gc);
11022 static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache;
11024 /* Return a new vector from the cache. If the cache is empty,
11025 allocate a new vector. These vectors are GC'ed, so it is OK if the
11026 pointer is not released.. */
11028 VEC(tree,gc) *
11029 make_tree_vector (void)
11031 if (!VEC_empty (tree_gc_vec, tree_vector_cache))
11032 return VEC_pop (tree_gc_vec, tree_vector_cache);
11033 else
11035 /* Passing 0 to VEC_alloc returns NULL, and our callers require
11036 that we always return a non-NULL value. The vector code uses
11037 4 when growing a NULL vector, so we do too. */
11038 return VEC_alloc (tree, gc, 4);
11042 /* Release a vector of trees back to the cache. */
11044 void
11045 release_tree_vector (VEC(tree,gc) *vec)
11047 if (vec != NULL)
11049 VEC_truncate (tree, vec, 0);
11050 VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec);
11054 /* Get a new tree vector holding a single tree. */
11056 VEC(tree,gc) *
11057 make_tree_vector_single (tree t)
11059 VEC(tree,gc) *ret = make_tree_vector ();
11060 VEC_quick_push (tree, ret, t);
11061 return ret;
11064 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11066 VEC(tree,gc) *
11067 make_tree_vector_from_list (tree list)
11069 VEC(tree,gc) *ret = make_tree_vector ();
11070 for (; list; list = TREE_CHAIN (list))
11071 VEC_safe_push (tree, gc, ret, TREE_VALUE (list));
11072 return ret;
11075 /* Get a new tree vector which is a copy of an existing one. */
11077 VEC(tree,gc) *
11078 make_tree_vector_copy (const VEC(tree,gc) *orig)
11080 VEC(tree,gc) *ret;
11081 unsigned int ix;
11082 tree t;
11084 ret = make_tree_vector ();
11085 VEC_reserve (tree, gc, ret, VEC_length (tree, orig));
11086 FOR_EACH_VEC_ELT (tree, orig, ix, t)
11087 VEC_quick_push (tree, ret, t);
11088 return ret;
11091 /* Return true if KEYWORD starts a type specifier. */
11093 bool
11094 keyword_begins_type_specifier (enum rid keyword)
11096 switch (keyword)
11098 case RID_INT:
11099 case RID_CHAR:
11100 case RID_FLOAT:
11101 case RID_DOUBLE:
11102 case RID_VOID:
11103 case RID_INT128:
11104 case RID_UNSIGNED:
11105 case RID_LONG:
11106 case RID_SHORT:
11107 case RID_SIGNED:
11108 case RID_DFLOAT32:
11109 case RID_DFLOAT64:
11110 case RID_DFLOAT128:
11111 case RID_FRACT:
11112 case RID_ACCUM:
11113 case RID_BOOL:
11114 case RID_WCHAR:
11115 case RID_CHAR16:
11116 case RID_CHAR32:
11117 case RID_SAT:
11118 case RID_COMPLEX:
11119 case RID_TYPEOF:
11120 case RID_STRUCT:
11121 case RID_CLASS:
11122 case RID_UNION:
11123 case RID_ENUM:
11124 return true;
11125 default:
11126 return false;
11130 /* Return true if KEYWORD names a type qualifier. */
11132 bool
11133 keyword_is_type_qualifier (enum rid keyword)
11135 switch (keyword)
11137 case RID_CONST:
11138 case RID_VOLATILE:
11139 case RID_RESTRICT:
11140 return true;
11141 default:
11142 return false;
11146 /* Return true if KEYWORD names a storage class specifier.
11148 RID_TYPEDEF is not included in this list despite `typedef' being
11149 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11150 such for syntactic convenience only. */
11152 bool
11153 keyword_is_storage_class_specifier (enum rid keyword)
11155 switch (keyword)
11157 case RID_STATIC:
11158 case RID_EXTERN:
11159 case RID_REGISTER:
11160 case RID_AUTO:
11161 case RID_MUTABLE:
11162 case RID_THREAD:
11163 return true;
11164 default:
11165 return false;
11169 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11171 static bool
11172 keyword_is_function_specifier (enum rid keyword)
11174 switch (keyword)
11176 case RID_INLINE:
11177 case RID_NORETURN:
11178 case RID_VIRTUAL:
11179 case RID_EXPLICIT:
11180 return true;
11181 default:
11182 return false;
11186 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11187 declaration-specifier (C99 6.7). */
11189 bool
11190 keyword_is_decl_specifier (enum rid keyword)
11192 if (keyword_is_storage_class_specifier (keyword)
11193 || keyword_is_type_qualifier (keyword)
11194 || keyword_is_function_specifier (keyword))
11195 return true;
11197 switch (keyword)
11199 case RID_TYPEDEF:
11200 case RID_FRIEND:
11201 case RID_CONSTEXPR:
11202 return true;
11203 default:
11204 return false;
11208 /* Initialize language-specific-bits of tree_contains_struct. */
11210 void
11211 c_common_init_ts (void)
11213 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11214 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11217 /* Build a user-defined numeric literal out of an integer constant type VALUE
11218 with identifier SUFFIX. */
11220 tree
11221 build_userdef_literal (tree suffix_id, tree value, tree num_string)
11223 tree literal = make_node (USERDEF_LITERAL);
11224 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11225 USERDEF_LITERAL_VALUE (literal) = value;
11226 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11227 return literal;
11230 /* For vector[index], convert the vector to a
11231 pointer of the underlying type. */
11232 void
11233 convert_vector_to_pointer_for_subscript (location_t loc,
11234 tree* vecp, tree index)
11236 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11238 tree type = TREE_TYPE (*vecp);
11239 tree type1;
11241 if (TREE_CODE (index) == INTEGER_CST)
11242 if (!host_integerp (index, 1)
11243 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11244 >= TYPE_VECTOR_SUBPARTS (type)))
11245 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11247 c_common_mark_addressable_vec (*vecp);
11248 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11249 type = build_pointer_type (type);
11250 type1 = build_pointer_type (TREE_TYPE (*vecp));
11251 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11252 *vecp = convert (type, *vecp);
11256 /* Return true iff ALIGN is an integral constant that is a fundamental
11257 alignment, as defined by [basic.align] in the c++-11
11258 specifications.
11260 That is:
11262 [A fundamental alignment is represented by an alignment less than or
11263 equal to the greatest alignment supported by the implementation
11264 in all contexts, which is equal to
11265 alignof(max_align_t)]. */
11267 bool
11268 cxx_fundamental_alignment_p (unsigned align)
11270 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11271 TYPE_ALIGN (long_double_type_node)));
11274 #include "gt-c-family-c-common.h"