]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
PR bootstrap/56198
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b0fc3e72 1/* Subroutines shared by all languages that are variants of C.
711789cc 2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
b0fc3e72 3
f12b58b3 4This file is part of GCC.
b0fc3e72 5
f12b58b3 6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
b0fc3e72 10
f12b58b3 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
b0fc3e72 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
b0fc3e72 19
20#include "config.h"
405711de 21#include "system.h"
805e22b2 22#include "coretypes.h"
23#include "tm.h"
e48d0f41 24#include "intl.h"
b0fc3e72 25#include "tree.h"
b0fc3e72 26#include "flags.h"
a3fa7feb 27#include "c-pragma.h"
dc12af01 28#include "ggc.h"
e41f0d80 29#include "c-common.h"
6c536c4f 30#include "c-objc.h"
d8c9779c 31#include "tm_p.h"
4e91a871 32#include "obstack.h"
a654e028 33#include "cpplib.h"
8ee295a7 34#include "target.h"
218e3e4e 35#include "common/common-target.h"
96554925 36#include "langhooks.h"
f3dde807 37#include "tree-inline.h"
69579044 38#include "toplev.h"
7f5f3953 39#include "diagnostic.h"
4ee9c684 40#include "tree-iterator.h"
41#include "hashtab.h"
b55af61c 42#include "tree-mudflap.h"
e08bd2f4 43#include "opts.h"
62eec3b4 44#include "cgraph.h"
b9fc964a 45#include "target-def.h"
fd6f6435 46
90cc7820 47cpp_reader *parse_in; /* Declared in c-pragma.h. */
a654e028 48
72040e7e 49/* The following symbols are subsumed in the c_global_trees array, and
44e9fa65 50 listed here individually for documentation purposes.
72040e7e 51
52 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
53
54 tree short_integer_type_node;
55 tree long_integer_type_node;
56 tree long_long_integer_type_node;
6388cfe2 57 tree int128_integer_type_node;
72040e7e 58
59 tree short_unsigned_type_node;
60 tree long_unsigned_type_node;
61 tree long_long_unsigned_type_node;
6388cfe2 62 tree int128_unsigned_type_node;
72040e7e 63
3c2239cf 64 tree truthvalue_type_node;
65 tree truthvalue_false_node;
66 tree truthvalue_true_node;
72040e7e 67
68 tree ptrdiff_type_node;
69
70 tree unsigned_char_type_node;
71 tree signed_char_type_node;
72 tree wchar_type_node;
72040e7e 73
924bbf02 74 tree char16_type_node;
75 tree char32_type_node;
76
72040e7e 77 tree float_type_node;
78 tree double_type_node;
79 tree long_double_type_node;
80
81 tree complex_integer_type_node;
82 tree complex_float_type_node;
83 tree complex_double_type_node;
84 tree complex_long_double_type_node;
85
c4503c0a 86 tree dfloat32_type_node;
87 tree dfloat64_type_node;
88 tree_dfloat128_type_node;
89
72040e7e 90 tree intQI_type_node;
91 tree intHI_type_node;
92 tree intSI_type_node;
93 tree intDI_type_node;
94 tree intTI_type_node;
95
96 tree unsigned_intQI_type_node;
97 tree unsigned_intHI_type_node;
98 tree unsigned_intSI_type_node;
99 tree unsigned_intDI_type_node;
100 tree unsigned_intTI_type_node;
101
102 tree widest_integer_literal_type_node;
103 tree widest_unsigned_literal_type_node;
104
105 Nodes for types `void *' and `const void *'.
106
107 tree ptr_type_node, const_ptr_type_node;
108
109 Nodes for types `char *' and `const char *'.
110
111 tree string_type_node, const_string_type_node;
112
113 Type `char[SOMENUMBER]'.
114 Used when an array of char is needed and the size is irrelevant.
115
116 tree char_array_type_node;
117
118 Type `int[SOMENUMBER]' or something like it.
119 Used when an array of int needed and the size is irrelevant.
120
121 tree int_array_type_node;
122
123 Type `wchar_t[SOMENUMBER]' or something like it.
124 Used when a wide string literal is created.
125
126 tree wchar_array_type_node;
127
924bbf02 128 Type `char16_t[SOMENUMBER]' or something like it.
129 Used when a UTF-16 string literal is created.
130
131 tree char16_array_type_node;
132
133 Type `char32_t[SOMENUMBER]' or something like it.
134 Used when a UTF-32 string literal is created.
135
136 tree char32_array_type_node;
137
72040e7e 138 Type `int ()' -- used for implicit declaration of functions.
139
140 tree default_function_type;
141
72040e7e 142 A VOID_TYPE node, packaged in a TREE_LIST.
143
144 tree void_list_node;
145
734c98be 146 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
65b7f83f 147 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
148 VAR_DECLS, but C++ does.)
71d9fc9b 149
65b7f83f 150 tree function_name_decl_node;
734c98be 151 tree pretty_function_name_decl_node;
65b7f83f 152 tree c99_function_name_decl_node;
153
154 Stack of nested function name VAR_DECLs.
1cae46be 155
65b7f83f 156 tree saved_function_name_decls;
71d9fc9b 157
72040e7e 158*/
159
160tree c_global_trees[CTI_MAX];
573aba85 161\f
574a6990 162/* Switches common to the C front ends. */
163
f7070933 164/* Nonzero means don't output line number information. */
165
166char flag_no_line_commands;
167
168/* Nonzero causes -E output not to be done, but directives such as
169 #define that have side effects are still obeyed. */
170
171char flag_no_output;
172
173/* Nonzero means dump macros in some fashion. */
174
175char flag_dump_macros;
176
177/* Nonzero means pass #include lines through to the output. */
178
179char flag_dump_includes;
180
d718b525 181/* Nonzero means process PCH files while preprocessing. */
182
183bool flag_pch_preprocess;
184
573aba85 185/* The file name to which we should write a precompiled header, or
186 NULL if no header will be written in this compile. */
187
188const char *pch_file;
189
1ed9d5f5 190/* Nonzero if an ISO standard was selected. It rejects macros in the
191 user's namespace. */
192int flag_iso;
193
574a6990 194/* C/ObjC language option variables. */
195
196
574a6990 197/* Nonzero means allow type mismatches in conditional expressions;
198 just make their values `void'. */
199
200int flag_cond_mismatch;
201
202/* Nonzero means enable C89 Amendment 1 features. */
203
204int flag_isoc94;
205
32074525 206/* Nonzero means use the ISO C99 (or C11) dialect of C. */
574a6990 207
208int flag_isoc99;
209
32074525 210/* Nonzero means use the ISO C11 dialect of C. */
39012afb 211
32074525 212int flag_isoc11;
39012afb 213
8b332087 214/* Nonzero means that we have builtin functions, and main is an int. */
574a6990 215
216int flag_hosted = 1;
217
574a6990 218
219/* ObjC language option variables. */
220
221
574a6990 222/* Tells the compiler that this is a special run. Do not perform any
223 compiling, instead we are to test some platform dependent features
224 and output a C header file with appropriate definitions. */
225
226int print_struct_values;
227
f0b5f617 228/* Tells the compiler what is the constant string class for ObjC. */
574a6990 229
230const char *constant_string_class_name;
231
574a6990 232
233/* C++ language option variables. */
234
235
574a6990 236/* Nonzero means generate separate instantiation control files and
237 juggle them at link time. */
238
239int flag_use_repository;
240
6dcdb5de 241/* The C++ dialect being used. C++98 is the default. */
0fe6eeac 242
6dcdb5de 243enum cxx_dialect cxx_dialect = cxx98;
0fe6eeac 244
9dcd0d49 245/* Maximum template instantiation depth. This limit exists to limit the
8ce59854 246 time it takes to notice excessively recursive template instantiations.
574a6990 247
8ce59854 248 The default is lower than the 1024 recommended by the C++0x standard
249 because G++ runs out of stack before 1024 with highly recursive template
250 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
251
252int max_tinst_depth = 900;
574a6990 253
988fc1d1 254/* The elements of `ridpointers' are identifier nodes for the reserved
255 type names and storage classes. It is indexed by a RID_... value. */
256tree *ridpointers;
257
e60a6f7b 258tree (*make_fname_decl) (location_t, tree, int);
9e5a737d 259
48d94ede 260/* Nonzero means don't warn about problems that occur when the code is
261 executed. */
262int c_inhibit_evaluation_warnings;
e78703c1 263
93be21c0 264/* Whether we are building a boolean conversion inside
265 convert_for_assignment, or some other late binary operation. If
266 build_binary_op is called for C (from code shared by C and C++) in
267 this case, then the operands have already been folded and the
268 result will not be folded again, so C_MAYBE_CONST_EXPR should not
269 be generated. */
270bool in_late_binary_op;
271
7f5f3953 272/* Whether lexing has been completed, so subsequent preprocessor
273 errors should use the compiler's input_location. */
274bool done_lexing = false;
275
2c0e001b 276/* Information about how a function name is generated. */
65b7f83f 277struct fname_var_t
278{
e99c3a1d 279 tree *const decl; /* pointer to the VAR_DECL. */
280 const unsigned rid; /* RID number for the identifier. */
281 const int pretty; /* How pretty is it? */
65b7f83f 282};
283
2c0e001b 284/* The three ways of getting then name of the current function. */
65b7f83f 285
286const struct fname_var_t fname_vars[] =
287{
2c0e001b 288 /* C99 compliant __func__, must be first. */
65b7f83f 289 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
2c0e001b 290 /* GCC __FUNCTION__ compliant. */
65b7f83f 291 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
2c0e001b 292 /* GCC __PRETTY_FUNCTION__ compliant. */
65b7f83f 293 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
294 {NULL, 0, 0},
295};
296
79396169 297/* Global visibility options. */
298struct visibility_flags visibility_options;
299
a75b1c71 300static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
2ca392fd 301static tree check_case_value (tree);
302static bool check_case_bounds (tree, tree, tree *, tree *);
be43ff5a 303
1cae46be 304static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
305static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
306static tree handle_common_attribute (tree *, tree, tree, int, bool *);
307static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
5de92639 308static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
309static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
d413ffdd 310static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
311 int, bool *);
1cae46be 312static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
bdb1f0d1 313static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
7bd95dfd 314static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
1cae46be 315static tree handle_always_inline_attribute (tree *, tree, tree, int,
316 bool *);
1b16fc45 317static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
318static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0cdd9887 319static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
10fc867f 320static tree handle_error_attribute (tree *, tree, tree, int, bool *);
1cae46be 321static tree handle_used_attribute (tree *, tree, tree, int, bool *);
322static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
62eec3b4 323static tree handle_externally_visible_attribute (tree *, tree, tree, int,
324 bool *);
1cae46be 325static tree handle_const_attribute (tree *, tree, tree, int, bool *);
326static tree handle_transparent_union_attribute (tree *, tree, tree,
327 int, bool *);
328static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
329static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
330static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
331static tree handle_section_attribute (tree *, tree, tree, int, bool *);
332static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
333static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
85c0a25c 334static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
335static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
1cae46be 336static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
f4a30bd7 337static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 338static tree handle_visibility_attribute (tree *, tree, tree, int,
339 bool *);
340static tree handle_tls_model_attribute (tree *, tree, tree, int,
341 bool *);
342static tree handle_no_instrument_function_attribute (tree *, tree,
343 tree, int, bool *);
344static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
26d1c5ff 345static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 346static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
347 bool *);
348static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
4c0315d0 349static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
350static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
fc09b200 351static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 352static tree handle_deprecated_attribute (tree *, tree, tree, int,
353 bool *);
354static tree handle_vector_size_attribute (tree *, tree, tree, int,
355 bool *);
356static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
357static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
358static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
8a8cdb8d 359static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
360 bool *);
bf6c8de0 361static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 362static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 363static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
24470055 364static tree handle_target_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 365static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
4c0315d0 366static tree ignore_attribute (tree *, tree, tree, int, bool *);
48b14f50 367static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
8ce86007 368static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
1cae46be 369
d01f58f9 370static void check_function_nonnull (tree, int, tree *);
1cae46be 371static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
372static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
373static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
860251be 374static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 375
5c6e5756 376/* Reserved words. The third field is a mask: keywords are disabled
377 if they match the mask.
378
379 Masks for languages:
380 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
381 C --std=c99: D_CXXONLY | D_OBJC
382 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
383 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
384 C++ --std=c0x: D_CONLY | D_OBJC
385 ObjC++ is like C++ except that D_OBJC is not set
386
387 If -fno-asm is used, D_ASM is added to the mask. If
388 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
389 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 390 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 391
b27e241e 392 Note the complication of the D_CXX_OBJC keywords. These are
393 reserved words such as 'class'. In C++, 'class' is a reserved
394 word. In Objective-C++ it is too. In Objective-C, it is a
395 reserved word too, but only if it follows an '@' sign.
396*/
5c6e5756 397const struct c_common_resword c_common_reswords[] =
398{
83e25171 399 { "_Alignas", RID_ALIGNAS, D_CONLY },
400 { "_Alignof", RID_ALIGNOF, D_CONLY },
5c6e5756 401 { "_Bool", RID_BOOL, D_CONLY },
402 { "_Complex", RID_COMPLEX, 0 },
c1800156 403 { "_Imaginary", RID_IMAGINARY, D_CONLY },
5c6e5756 404 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
405 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
406 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
407 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
408 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
409 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 410 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 411 { "_Noreturn", RID_NORETURN, D_CONLY },
5c6e5756 412 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
413 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
414 { "__alignof", RID_ALIGNOF, 0 },
415 { "__alignof__", RID_ALIGNOF, 0 },
416 { "__asm", RID_ASM, 0 },
417 { "__asm__", RID_ASM, 0 },
418 { "__attribute", RID_ATTRIBUTE, 0 },
419 { "__attribute__", RID_ATTRIBUTE, 0 },
e6014a82 420 { "__bases", RID_BASES, D_CXXONLY },
5c6e5756 421 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 422 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
bf0cb017 423 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
5c6e5756 424 { "__builtin_offsetof", RID_OFFSETOF, 0 },
425 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
426 { "__builtin_va_arg", RID_VA_ARG, 0 },
427 { "__complex", RID_COMPLEX, 0 },
428 { "__complex__", RID_COMPLEX, 0 },
429 { "__const", RID_CONST, 0 },
430 { "__const__", RID_CONST, 0 },
431 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 432 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 433 { "__extension__", RID_EXTENSION, 0 },
434 { "__func__", RID_C99_FUNCTION_NAME, 0 },
435 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
436 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
437 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
438 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
439 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
440 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
441 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
442 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 443 { "__imag", RID_IMAGPART, 0 },
444 { "__imag__", RID_IMAGPART, 0 },
445 { "__inline", RID_INLINE, 0 },
446 { "__inline__", RID_INLINE, 0 },
6388cfe2 447 { "__int128", RID_INT128, 0 },
5c6e5756 448 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
449 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
450 { "__is_class", RID_IS_CLASS, D_CXXONLY },
451 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
452 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
453 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 454 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 455 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 456 { "__is_pod", RID_IS_POD, D_CXXONLY },
457 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
c1c67b4f 458 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
459 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
5c6e5756 460 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 461 { "__label__", RID_LABEL, 0 },
462 { "__null", RID_NULL, 0 },
463 { "__real", RID_REALPART, 0 },
464 { "__real__", RID_REALPART, 0 },
465 { "__restrict", RID_RESTRICT, 0 },
466 { "__restrict__", RID_RESTRICT, 0 },
467 { "__signed", RID_SIGNED, 0 },
468 { "__signed__", RID_SIGNED, 0 },
469 { "__thread", RID_THREAD, 0 },
4c0315d0 470 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
471 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
472 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 473 { "__typeof", RID_TYPEOF, 0 },
474 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 475 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 476 { "__volatile", RID_VOLATILE, 0 },
477 { "__volatile__", RID_VOLATILE, 0 },
ffcdbf9c 478 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
e463efd7 479 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 480 { "asm", RID_ASM, D_ASM },
481 { "auto", RID_AUTO, 0 },
dbd982c9 482 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 483 { "break", RID_BREAK, 0 },
484 { "case", RID_CASE, 0 },
51030405 485 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 486 { "char", RID_CHAR, 0 },
51030405 487 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
488 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
489 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 490 { "const", RID_CONST, 0 },
17814aca 491 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 492 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
493 { "continue", RID_CONTINUE, 0 },
51030405 494 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 495 { "default", RID_DEFAULT, 0 },
51030405 496 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 497 { "do", RID_DO, 0 },
498 { "double", RID_DOUBLE, 0 },
499 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
500 { "else", RID_ELSE, 0 },
501 { "enum", RID_ENUM, 0 },
51030405 502 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
503 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 504 { "extern", RID_EXTERN, 0 },
51030405 505 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 506 { "float", RID_FLOAT, 0 },
507 { "for", RID_FOR, 0 },
51030405 508 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 509 { "goto", RID_GOTO, 0 },
510 { "if", RID_IF, 0 },
511 { "inline", RID_INLINE, D_EXT89 },
512 { "int", RID_INT, 0 },
513 { "long", RID_LONG, 0 },
514 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 515 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
516 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
98fe9664 517 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
6fe11077 518 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
51030405 519 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
520 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
521 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
522 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 523 { "register", RID_REGISTER, 0 },
524 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
525 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
526 { "return", RID_RETURN, 0 },
527 { "short", RID_SHORT, 0 },
528 { "signed", RID_SIGNED, 0 },
529 { "sizeof", RID_SIZEOF, 0 },
530 { "static", RID_STATIC, 0 },
531 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
532 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
533 { "struct", RID_STRUCT, 0 },
534 { "switch", RID_SWITCH, 0 },
51030405 535 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
536 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
3740094c 537 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
51030405 538 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
539 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
540 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 541 { "typedef", RID_TYPEDEF, 0 },
51030405 542 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
543 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 544 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
545 { "union", RID_UNION, 0 },
546 { "unsigned", RID_UNSIGNED, 0 },
51030405 547 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
548 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 549 { "void", RID_VOID, 0 },
550 { "volatile", RID_VOLATILE, 0 },
551 { "wchar_t", RID_WCHAR, D_CXXONLY },
552 { "while", RID_WHILE, 0 },
553 /* These Objective-C keywords are recognized only immediately after
554 an '@'. */
555 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
556 { "defs", RID_AT_DEFS, D_OBJC },
557 { "encode", RID_AT_ENCODE, D_OBJC },
558 { "end", RID_AT_END, D_OBJC },
559 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
560 { "interface", RID_AT_INTERFACE, D_OBJC },
561 { "protocol", RID_AT_PROTOCOL, D_OBJC },
562 { "selector", RID_AT_SELECTOR, D_OBJC },
563 { "finally", RID_AT_FINALLY, D_OBJC },
564 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
069761fb 565 { "optional", RID_AT_OPTIONAL, D_OBJC },
566 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 567 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 568 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 569 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
570 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 571 /* These are recognized only in protocol-qualifier context
572 (see above) */
573 { "bycopy", RID_BYCOPY, D_OBJC },
574 { "byref", RID_BYREF, D_OBJC },
575 { "in", RID_IN, D_OBJC },
576 { "inout", RID_INOUT, D_OBJC },
577 { "oneway", RID_ONEWAY, D_OBJC },
578 { "out", RID_OUT, D_OBJC },
86c110ac 579 /* These are recognized inside a property attribute list */
7590f0e5 580 { "assign", RID_ASSIGN, D_OBJC },
581 { "copy", RID_COPY, D_OBJC },
86c110ac 582 { "getter", RID_GETTER, D_OBJC },
7590f0e5 583 { "nonatomic", RID_NONATOMIC, D_OBJC },
584 { "readonly", RID_READONLY, D_OBJC },
585 { "readwrite", RID_READWRITE, D_OBJC },
586 { "retain", RID_RETAIN, D_OBJC },
86c110ac 587 { "setter", RID_SETTER, D_OBJC },
5c6e5756 588};
589
590const unsigned int num_c_common_reswords =
591 sizeof c_common_reswords / sizeof (struct c_common_resword);
592
f8e93a2e 593/* Table of machine-independent attributes common to all C-like languages. */
594const struct attribute_spec c_common_attribute_table[] =
595{
ac86af5d 596 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
597 affects_type_identity } */
f8e93a2e 598 { "packed", 0, 0, false, false, false,
ac86af5d 599 handle_packed_attribute , false},
f8e93a2e 600 { "nocommon", 0, 0, true, false, false,
ac86af5d 601 handle_nocommon_attribute, false},
f8e93a2e 602 { "common", 0, 0, true, false, false,
ac86af5d 603 handle_common_attribute, false },
f8e93a2e 604 /* FIXME: logically, noreturn attributes should be listed as
605 "false, true, true" and apply to function types. But implementing this
606 would require all the places in the compiler that use TREE_THIS_VOLATILE
607 on a decl to identify non-returning functions to be located and fixed
608 to check the function type instead. */
609 { "noreturn", 0, 0, true, false, false,
ac86af5d 610 handle_noreturn_attribute, false },
f8e93a2e 611 { "volatile", 0, 0, true, false, false,
ac86af5d 612 handle_noreturn_attribute, false },
f8e93a2e 613 { "noinline", 0, 0, true, false, false,
ac86af5d 614 handle_noinline_attribute, false },
bdb1f0d1 615 { "noclone", 0, 0, true, false, false,
ac86af5d 616 handle_noclone_attribute, false },
7bd95dfd 617 { "leaf", 0, 0, true, false, false,
ac86af5d 618 handle_leaf_attribute, false },
f8e93a2e 619 { "always_inline", 0, 0, true, false, false,
ac86af5d 620 handle_always_inline_attribute, false },
541e4101 621 { "gnu_inline", 0, 0, true, false, false,
ac86af5d 622 handle_gnu_inline_attribute, false },
1b16fc45 623 { "artificial", 0, 0, true, false, false,
ac86af5d 624 handle_artificial_attribute, false },
0cdd9887 625 { "flatten", 0, 0, true, false, false,
ac86af5d 626 handle_flatten_attribute, false },
f8e93a2e 627 { "used", 0, 0, true, false, false,
ac86af5d 628 handle_used_attribute, false },
f8e93a2e 629 { "unused", 0, 0, false, false, false,
ac86af5d 630 handle_unused_attribute, false },
62eec3b4 631 { "externally_visible", 0, 0, true, false, false,
ac86af5d 632 handle_externally_visible_attribute, false },
f8e93a2e 633 /* The same comments as for noreturn attributes apply to const ones. */
634 { "const", 0, 0, true, false, false,
ac86af5d 635 handle_const_attribute, false },
f8e93a2e 636 { "transparent_union", 0, 0, false, false, false,
ac86af5d 637 handle_transparent_union_attribute, false },
9af7fd5b 638 { "constructor", 0, 1, true, false, false,
ac86af5d 639 handle_constructor_attribute, false },
9af7fd5b 640 { "destructor", 0, 1, true, false, false,
ac86af5d 641 handle_destructor_attribute, false },
f8e93a2e 642 { "mode", 1, 1, false, true, false,
ac86af5d 643 handle_mode_attribute, false },
f8e93a2e 644 { "section", 1, 1, true, false, false,
ac86af5d 645 handle_section_attribute, false },
f8e93a2e 646 { "aligned", 0, 1, false, false, false,
ac86af5d 647 handle_aligned_attribute, false },
f8e93a2e 648 { "weak", 0, 0, true, false, false,
ac86af5d 649 handle_weak_attribute, false },
85c0a25c 650 { "ifunc", 1, 1, true, false, false,
ac86af5d 651 handle_ifunc_attribute, false },
f8e93a2e 652 { "alias", 1, 1, true, false, false,
ac86af5d 653 handle_alias_attribute, false },
f4a30bd7 654 { "weakref", 0, 1, true, false, false,
ac86af5d 655 handle_weakref_attribute, false },
f8e93a2e 656 { "no_instrument_function", 0, 0, true, false, false,
ac86af5d 657 handle_no_instrument_function_attribute,
658 false },
f8e93a2e 659 { "malloc", 0, 0, true, false, false,
ac86af5d 660 handle_malloc_attribute, false },
26d1c5ff 661 { "returns_twice", 0, 0, true, false, false,
ac86af5d 662 handle_returns_twice_attribute, false },
f8e93a2e 663 { "no_stack_limit", 0, 0, true, false, false,
ac86af5d 664 handle_no_limit_stack_attribute, false },
f8e93a2e 665 { "pure", 0, 0, true, false, false,
ac86af5d 666 handle_pure_attribute, false },
4c0315d0 667 { "transaction_callable", 0, 0, false, true, false,
668 handle_tm_attribute, false },
669 { "transaction_unsafe", 0, 0, false, true, false,
670 handle_tm_attribute, false },
671 { "transaction_safe", 0, 0, false, true, false,
672 handle_tm_attribute, false },
673 { "transaction_may_cancel_outer", 0, 0, false, true, false,
674 handle_tm_attribute, false },
675 /* ??? These two attributes didn't make the transition from the
676 Intel language document to the multi-vendor language document. */
677 { "transaction_pure", 0, 0, false, true, false,
678 handle_tm_attribute, false },
679 { "transaction_wrap", 1, 1, true, false, false,
680 handle_tm_wrap_attribute, false },
fc09b200 681 /* For internal use (marking of builtins) only. The name contains space
682 to prevent its usage in source code. */
683 { "no vops", 0, 0, true, false, false,
ac86af5d 684 handle_novops_attribute, false },
45c4e798 685 { "deprecated", 0, 1, false, false, false,
ac86af5d 686 handle_deprecated_attribute, false },
f8e93a2e 687 { "vector_size", 1, 1, false, true, false,
ac86af5d 688 handle_vector_size_attribute, false },
b212f378 689 { "visibility", 1, 1, false, false, false,
ac86af5d 690 handle_visibility_attribute, false },
24dfead4 691 { "tls_model", 1, 1, true, false, false,
ac86af5d 692 handle_tls_model_attribute, false },
dbf6c367 693 { "nonnull", 0, -1, false, true, true,
ac86af5d 694 handle_nonnull_attribute, false },
fa987697 695 { "nothrow", 0, 0, true, false, false,
ac86af5d 696 handle_nothrow_attribute, false },
697 { "may_alias", 0, 0, false, true, false, NULL, false },
7acb29a3 698 { "cleanup", 1, 1, true, false, false,
ac86af5d 699 handle_cleanup_attribute, false },
8a8cdb8d 700 { "warn_unused_result", 0, 0, false, true, true,
ac86af5d 701 handle_warn_unused_result_attribute, false },
50ca527f 702 { "sentinel", 0, 1, false, true, true,
ac86af5d 703 handle_sentinel_attribute, false },
b5c26b42 704 /* For internal use (marking of builtins) only. The name contains space
705 to prevent its usage in source code. */
706 { "type generic", 0, 0, false, true, true,
ac86af5d 707 handle_type_generic_attribute, false },
4a29c97c 708 { "alloc_size", 1, 2, false, true, true,
ac86af5d 709 handle_alloc_size_attribute, false },
5de92639 710 { "cold", 0, 0, true, false, false,
ac86af5d 711 handle_cold_attribute, false },
5de92639 712 { "hot", 0, 0, true, false, false,
ac86af5d 713 handle_hot_attribute, false },
d413ffdd 714 { "no_address_safety_analysis",
715 0, 0, true, false, false,
716 handle_no_address_safety_analysis_attribute,
717 false },
10fc867f 718 { "warning", 1, 1, true, false, false,
ac86af5d 719 handle_error_attribute, false },
10fc867f 720 { "error", 1, 1, true, false, false,
ac86af5d 721 handle_error_attribute, false },
24470055 722 { "target", 1, -1, true, false, false,
ac86af5d 723 handle_target_attribute, false },
46f8e3b0 724 { "optimize", 1, -1, true, false, false,
ac86af5d 725 handle_optimize_attribute, false },
4c0315d0 726 /* For internal use only. The leading '*' both prevents its usage in
727 source code and signals that it may be overridden by machine tables. */
728 { "*tm regparm", 0, 0, false, true, true,
729 ignore_attribute, false },
48b14f50 730 { "no_split_stack", 0, 0, true, false, false,
ac86af5d 731 handle_no_split_stack_attribute, false },
8ce86007 732 /* For internal use (marking of builtins and runtime functions) only.
733 The name contains space to prevent its usage in source code. */
734 { "fn spec", 1, 1, false, true, true,
ac86af5d 735 handle_fnspec_attribute, false },
736 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 737};
738
739/* Give the specifications for the format attributes, used by C and all
d716ce75 740 descendants. */
f8e93a2e 741
742const struct attribute_spec c_common_format_attribute_table[] =
743{
ac86af5d 744 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
745 affects_type_identity } */
f8e93a2e 746 { "format", 3, 3, false, true, true,
ac86af5d 747 handle_format_attribute, false },
f8e93a2e 748 { "format_arg", 1, 1, false, true, true,
ac86af5d 749 handle_format_arg_attribute, false },
750 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 751};
752
6d5d708e 753/* Return identifier for address space AS. */
34208e18 754
6d5d708e 755const char *
756c_addr_space_name (addr_space_t as)
757{
34208e18 758 int rid = RID_FIRST_ADDR_SPACE + as;
759 gcc_assert (ridpointers [rid]);
760 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 761}
762
2c0e001b 763/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 764
765void
1cae46be 766start_fname_decls (void)
f4e3c278 767{
65b7f83f 768 unsigned ix;
769 tree saved = NULL_TREE;
1cae46be 770
65b7f83f 771 for (ix = 0; fname_vars[ix].decl; ix++)
772 {
773 tree decl = *fname_vars[ix].decl;
f4e3c278 774
65b7f83f 775 if (decl)
776 {
ceb7b692 777 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
778 saved);
65b7f83f 779 *fname_vars[ix].decl = NULL_TREE;
780 }
781 }
782 if (saved || saved_function_name_decls)
783 /* Normally they'll have been NULL, so only push if we've got a
784 stack, or they are non-NULL. */
785 saved_function_name_decls = tree_cons (saved, NULL_TREE,
786 saved_function_name_decls);
787}
788
2363ef00 789/* Finish up the current bindings, adding them into the current function's
790 statement tree. This must be done _before_ finish_stmt_tree is called.
791 If there is no current function, we must be at file scope and no statements
792 are involved. Pop the previous bindings. */
65b7f83f 793
794void
1cae46be 795finish_fname_decls (void)
65b7f83f 796{
797 unsigned ix;
2363ef00 798 tree stmts = NULL_TREE;
65b7f83f 799 tree stack = saved_function_name_decls;
800
801 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 802 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 803
2363ef00 804 if (stmts)
65b7f83f 805 {
2363ef00 806 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 807
2363ef00 808 if (TREE_CODE (*bodyp) == BIND_EXPR)
809 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 810
bc2b76e0 811 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 812 *bodyp = stmts;
65b7f83f 813 }
1cae46be 814
65b7f83f 815 for (ix = 0; fname_vars[ix].decl; ix++)
816 *fname_vars[ix].decl = NULL_TREE;
1cae46be 817
65b7f83f 818 if (stack)
f4e3c278 819 {
2c0e001b 820 /* We had saved values, restore them. */
65b7f83f 821 tree saved;
822
823 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
824 {
825 tree decl = TREE_PURPOSE (saved);
826 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 827
65b7f83f 828 *fname_vars[ix].decl = decl;
829 }
830 stack = TREE_CHAIN (stack);
f4e3c278 831 }
65b7f83f 832 saved_function_name_decls = stack;
833}
834
81010c97 835/* Return the text name of the current function, suitably prettified
5fc7fa69 836 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 837
838const char *
1cae46be 839fname_as_string (int pretty_p)
65b7f83f 840{
9ad4bb1e 841 const char *name = "top level";
5fc7fa69 842 char *namep;
8115b8be 843 int vrb = 2, len;
844 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 845
84166705 846 if (!pretty_p)
9ad4bb1e 847 {
848 name = "";
849 vrb = 0;
850 }
851
852 if (current_function_decl)
dc24ddbd 853 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 854
8115b8be 855 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 856
8115b8be 857 namep = XNEWVEC (char, len);
858 snprintf (namep, len, "\"%s\"", name);
859 strname.text = (unsigned char *) namep;
860 strname.len = len - 1;
5fc7fa69 861
924bbf02 862 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 863 {
864 XDELETEVEC (namep);
865 return (const char *) cstr.text;
5fc7fa69 866 }
5fc7fa69 867
868 return namep;
65b7f83f 869}
870
65b7f83f 871/* Return the VAR_DECL for a const char array naming the current
872 function. If the VAR_DECL has not yet been created, create it
873 now. RID indicates how it should be formatted and IDENTIFIER_NODE
874 ID is its name (unfortunately C and C++ hold the RID values of
875 keywords in different places, so we can't derive RID from ID in
e3b80d49 876 this language independent code. LOC is the location of the
877 function. */
65b7f83f 878
879tree
e3b80d49 880fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 881{
882 unsigned ix;
883 tree decl = NULL_TREE;
884
885 for (ix = 0; fname_vars[ix].decl; ix++)
886 if (fname_vars[ix].rid == rid)
887 break;
888
889 decl = *fname_vars[ix].decl;
890 if (!decl)
f4e3c278 891 {
2222b3c6 892 /* If a tree is built here, it would normally have the lineno of
893 the current statement. Later this tree will be moved to the
894 beginning of the function and this line number will be wrong.
895 To avoid this problem set the lineno to 0 here; that prevents
7299020b 896 it from appearing in the RTL. */
2363ef00 897 tree stmts;
9a6486a6 898 location_t saved_location = input_location;
9a6486a6 899 input_location = UNKNOWN_LOCATION;
1cae46be 900
2363ef00 901 stmts = push_stmt_list ();
e60a6f7b 902 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 903 stmts = pop_stmt_list (stmts);
904 if (!IS_EMPTY_STMT (stmts))
905 saved_function_name_decls
906 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 907 *fname_vars[ix].decl = decl;
9a6486a6 908 input_location = saved_location;
f4e3c278 909 }
65b7f83f 910 if (!ix && !current_function_decl)
e3b80d49 911 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 912
65b7f83f 913 return decl;
f4e3c278 914}
915
070236f0 916/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 917
918tree
1cae46be 919fix_string_type (tree value)
b0fc3e72 920{
070236f0 921 int length = TREE_STRING_LENGTH (value);
922 int nchars;
00d26680 923 tree e_type, i_type, a_type;
924
73be5127 925 /* Compute the number of elements, for the array type. */
924bbf02 926 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
927 {
928 nchars = length;
929 e_type = char_type_node;
930 }
931 else if (TREE_TYPE (value) == char16_array_type_node)
932 {
933 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
934 e_type = char16_type_node;
935 }
936 else if (TREE_TYPE (value) == char32_array_type_node)
937 {
938 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
939 e_type = char32_type_node;
940 }
941 else
942 {
943 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
944 e_type = wchar_type_node;
945 }
b0fc3e72 946
1d752508 947 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
948 limit in C++98 Annex B is very large (65536) and is not normative,
949 so we do not diagnose it (warn_overlength_strings is forced off
950 in c_common_post_options). */
951 if (warn_overlength_strings)
952 {
953 const int nchars_max = flag_isoc99 ? 4095 : 509;
954 const int relevant_std = flag_isoc99 ? 99 : 90;
955 if (nchars - 1 > nchars_max)
956 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
957 separate the %d from the 'C'. 'ISO' should not be
958 translated, but it may be moved after 'C%d' in languages
959 where modifiers follow nouns. */
21ca8540 960 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 961 "string length %qd is greater than the length %qd "
1d752508 962 "ISO C%d compilers are required to support",
963 nchars - 1, nchars_max, relevant_std);
964 }
82cfc7f7 965
390be14e 966 /* Create the array type for the string constant. The ISO C++
967 standard says that a string literal has type `const char[N]' or
968 `const wchar_t[N]'. We use the same logic when invoked as a C
969 front-end with -Wwrite-strings.
970 ??? We should change the type of an expression depending on the
971 state of a warning flag. We should just be warning -- see how
972 this is handled in the C++ front-end for the deprecated implicit
973 conversion from string literals to `char*' or `wchar_t*'.
00d26680 974
975 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
976 array type being the unqualified version of that type.
977 Therefore, if we are constructing an array of const char, we must
978 construct the matching unqualified array type first. The C front
979 end does not require this, but it does no harm, so we do it
980 unconditionally. */
ceb7b692 981 i_type = build_index_type (size_int (nchars - 1));
00d26680 982 a_type = build_array_type (e_type, i_type);
390be14e 983 if (c_dialect_cxx() || warn_write_strings)
aebc8537 984 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 985
00d26680 986 TREE_TYPE (value) = a_type;
b8e3b7ad 987 TREE_CONSTANT (value) = 1;
a814bad5 988 TREE_READONLY (value) = 1;
b0fc3e72 989 TREE_STATIC (value) = 1;
990 return value;
991}
992\f
fc501191 993/* If DISABLE is true, stop issuing warnings. This is used when
994 parsing code that we know will not be executed. This function may
995 be called multiple times, and works as a stack. */
996
997static void
998c_disable_warnings (bool disable)
999{
1000 if (disable)
1001 {
1002 ++c_inhibit_evaluation_warnings;
1003 fold_defer_overflow_warnings ();
1004 }
1005}
1006
1007/* If ENABLE is true, reenable issuing warnings. */
1008
1009static void
1010c_enable_warnings (bool enable)
1011{
1012 if (enable)
1013 {
1014 --c_inhibit_evaluation_warnings;
1015 fold_undefer_and_ignore_overflow_warnings ();
1016 }
1017}
1018
a75b1c71 1019/* Fully fold EXPR, an expression that was not folded (beyond integer
1020 constant expressions and null pointer constants) when being built
1021 up. If IN_INIT, this is in a static initializer and certain
1022 changes are made to the folding done. Clear *MAYBE_CONST if
1023 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1024 expression because it contains an evaluated operator (in C99) or an
1025 operator outside of sizeof returning an integer constant (in C90)
1026 not permitted in constant expressions, or because it contains an
1027 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1028 set to true by callers before calling this function.) Return the
1029 folded expression. Function arguments have already been folded
1030 before calling this function, as have the contents of SAVE_EXPR,
1031 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1032 C_MAYBE_CONST_EXPR. */
1033
1034tree
1035c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1036{
1037 tree ret;
c6418a4e 1038 tree eptype = NULL_TREE;
a75b1c71 1039 bool dummy = true;
1040 bool maybe_const_itself = true;
389dd41b 1041 location_t loc = EXPR_LOCATION (expr);
a75b1c71 1042
1043 /* This function is not relevant to C++ because C++ folds while
1044 parsing, and may need changes to be correct for C++ when C++
1045 stops folding while parsing. */
1046 if (c_dialect_cxx ())
1047 gcc_unreachable ();
1048
1049 if (!maybe_const)
1050 maybe_const = &dummy;
c6418a4e 1051 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1052 {
1053 eptype = TREE_TYPE (expr);
1054 expr = TREE_OPERAND (expr, 0);
1055 }
a75b1c71 1056 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1057 &maybe_const_itself);
c6418a4e 1058 if (eptype)
389dd41b 1059 ret = fold_convert_loc (loc, eptype, ret);
a75b1c71 1060 *maybe_const &= maybe_const_itself;
1061 return ret;
1062}
1063
1064/* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1065 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1066 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1067 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1068 both evaluated and unevaluated subexpressions while
1069 *MAYBE_CONST_ITSELF is carried from only evaluated
1070 subexpressions). */
1071
1072static tree
1073c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1074 bool *maybe_const_itself)
1075{
1076 tree ret = expr;
1077 enum tree_code code = TREE_CODE (expr);
1078 enum tree_code_class kind = TREE_CODE_CLASS (code);
1079 location_t loc = EXPR_LOCATION (expr);
1080 tree op0, op1, op2, op3;
1081 tree orig_op0, orig_op1, orig_op2;
1082 bool op0_const = true, op1_const = true, op2_const = true;
1083 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1084 bool nowarning = TREE_NO_WARNING (expr);
fc501191 1085 bool unused_p;
a75b1c71 1086
1087 /* This function is not relevant to C++ because C++ folds while
1088 parsing, and may need changes to be correct for C++ when C++
1089 stops folding while parsing. */
1090 if (c_dialect_cxx ())
1091 gcc_unreachable ();
1092
1093 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1094 anything else not counted as an expression cannot usefully be
1095 folded further at this point. */
1096 if (!IS_EXPR_CODE_CLASS (kind)
1097 || kind == tcc_statement
1098 || code == SAVE_EXPR)
1099 return expr;
1100
1101 /* Operands of variable-length expressions (function calls) have
1102 already been folded, as have __builtin_* function calls, and such
1103 expressions cannot occur in constant expressions. */
1104 if (kind == tcc_vl_exp)
1105 {
1106 *maybe_const_operands = false;
1107 ret = fold (expr);
1108 goto out;
1109 }
1110
1111 if (code == C_MAYBE_CONST_EXPR)
1112 {
1113 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1114 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1115 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1116 *maybe_const_operands = false;
1117 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1118 *maybe_const_itself = false;
1119 if (pre && !in_init)
1120 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1121 else
1122 ret = inner;
1123 goto out;
1124 }
1125
1126 /* Assignment, increment, decrement, function call and comma
1127 operators, and statement expressions, cannot occur in constant
1128 expressions if evaluated / outside of sizeof. (Function calls
1129 were handled above, though VA_ARG_EXPR is treated like a function
1130 call here, and statement expressions are handled through
1131 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1132 switch (code)
1133 {
1134 case MODIFY_EXPR:
1135 case PREDECREMENT_EXPR:
1136 case PREINCREMENT_EXPR:
1137 case POSTDECREMENT_EXPR:
1138 case POSTINCREMENT_EXPR:
1139 case COMPOUND_EXPR:
1140 *maybe_const_operands = false;
1141 break;
1142
1143 case VA_ARG_EXPR:
1144 case TARGET_EXPR:
1145 case BIND_EXPR:
1146 case OBJ_TYPE_REF:
1147 *maybe_const_operands = false;
1148 ret = fold (expr);
1149 goto out;
1150
1151 default:
1152 break;
1153 }
1154
1155 /* Fold individual tree codes as appropriate. */
1156 switch (code)
1157 {
1158 case COMPOUND_LITERAL_EXPR:
1159 /* Any non-constancy will have been marked in a containing
1160 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1161 goto out;
1162
1163 case COMPONENT_REF:
1164 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1165 op1 = TREE_OPERAND (expr, 1);
1166 op2 = TREE_OPERAND (expr, 2);
1167 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1168 maybe_const_itself);
f59e3889 1169 STRIP_TYPE_NOPS (op0);
a75b1c71 1170 if (op0 != orig_op0)
1171 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1172 if (ret != expr)
1173 {
1174 TREE_READONLY (ret) = TREE_READONLY (expr);
1175 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1176 }
1177 goto out;
1178
1179 case ARRAY_REF:
1180 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1181 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1182 op2 = TREE_OPERAND (expr, 2);
1183 op3 = TREE_OPERAND (expr, 3);
1184 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1185 maybe_const_itself);
f59e3889 1186 STRIP_TYPE_NOPS (op0);
a75b1c71 1187 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1188 maybe_const_itself);
f59e3889 1189 STRIP_TYPE_NOPS (op1);
a75b1c71 1190 op1 = decl_constant_value_for_optimization (op1);
1191 if (op0 != orig_op0 || op1 != orig_op1)
1192 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1193 if (ret != expr)
1194 {
1195 TREE_READONLY (ret) = TREE_READONLY (expr);
1196 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1197 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1198 }
1199 ret = fold (ret);
1200 goto out;
1201
1202 case COMPOUND_EXPR:
1203 case MODIFY_EXPR:
1204 case PREDECREMENT_EXPR:
1205 case PREINCREMENT_EXPR:
1206 case POSTDECREMENT_EXPR:
1207 case POSTINCREMENT_EXPR:
1208 case PLUS_EXPR:
1209 case MINUS_EXPR:
1210 case MULT_EXPR:
1211 case POINTER_PLUS_EXPR:
1212 case TRUNC_DIV_EXPR:
1213 case CEIL_DIV_EXPR:
1214 case FLOOR_DIV_EXPR:
1215 case TRUNC_MOD_EXPR:
1216 case RDIV_EXPR:
1217 case EXACT_DIV_EXPR:
1218 case LSHIFT_EXPR:
1219 case RSHIFT_EXPR:
1220 case BIT_IOR_EXPR:
1221 case BIT_XOR_EXPR:
1222 case BIT_AND_EXPR:
1223 case LT_EXPR:
1224 case LE_EXPR:
1225 case GT_EXPR:
1226 case GE_EXPR:
1227 case EQ_EXPR:
1228 case NE_EXPR:
1229 case COMPLEX_EXPR:
1230 case TRUTH_AND_EXPR:
1231 case TRUTH_OR_EXPR:
1232 case TRUTH_XOR_EXPR:
1233 case UNORDERED_EXPR:
1234 case ORDERED_EXPR:
1235 case UNLT_EXPR:
1236 case UNLE_EXPR:
1237 case UNGT_EXPR:
1238 case UNGE_EXPR:
1239 case UNEQ_EXPR:
1240 /* Binary operations evaluating both arguments (increment and
1241 decrement are binary internally in GCC). */
1242 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1243 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1244 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1245 maybe_const_itself);
f59e3889 1246 STRIP_TYPE_NOPS (op0);
a75b1c71 1247 if (code != MODIFY_EXPR
1248 && code != PREDECREMENT_EXPR
1249 && code != PREINCREMENT_EXPR
1250 && code != POSTDECREMENT_EXPR
1251 && code != POSTINCREMENT_EXPR)
1252 op0 = decl_constant_value_for_optimization (op0);
1253 /* The RHS of a MODIFY_EXPR was fully folded when building that
1254 expression for the sake of conversion warnings. */
1255 if (code != MODIFY_EXPR)
1256 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1257 maybe_const_itself);
f59e3889 1258 STRIP_TYPE_NOPS (op1);
a75b1c71 1259 op1 = decl_constant_value_for_optimization (op1);
1260 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1261 ret = in_init
389dd41b 1262 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1263 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1264 else
1265 ret = fold (expr);
672d914b 1266 if (TREE_OVERFLOW_P (ret)
1267 && !TREE_OVERFLOW_P (op0)
1268 && !TREE_OVERFLOW_P (op1))
1269 overflow_warning (EXPR_LOCATION (expr), ret);
7c834436 1270 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1271 && TREE_CODE (orig_op1) != INTEGER_CST
1272 && TREE_CODE (op1) == INTEGER_CST
1273 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1274 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1275 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1276 && c_inhibit_evaluation_warnings == 0)
1277 {
1278 if (tree_int_cst_sgn (op1) < 0)
1279 warning_at (loc, 0, (code == LSHIFT_EXPR
1280 ? G_("left shift count is negative")
1281 : G_("right shift count is negative")));
1282 else if (compare_tree_int (op1,
1283 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1284 >= 0)
1285 warning_at (loc, 0, (code == LSHIFT_EXPR
1286 ? G_("left shift count >= width of type")
1287 : G_("right shift count >= width of type")));
1288 }
a75b1c71 1289 goto out;
1290
1291 case INDIRECT_REF:
1292 case FIX_TRUNC_EXPR:
1293 case FLOAT_EXPR:
1294 CASE_CONVERT:
2b19dfe4 1295 case VIEW_CONVERT_EXPR:
a75b1c71 1296 case NON_LVALUE_EXPR:
1297 case NEGATE_EXPR:
1298 case BIT_NOT_EXPR:
1299 case TRUTH_NOT_EXPR:
1300 case ADDR_EXPR:
1301 case CONJ_EXPR:
1302 case REALPART_EXPR:
1303 case IMAGPART_EXPR:
1304 /* Unary operations. */
1305 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1306 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1307 maybe_const_itself);
f59e3889 1308 STRIP_TYPE_NOPS (op0);
a75b1c71 1309 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1310 op0 = decl_constant_value_for_optimization (op0);
737a23cc 1311 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1312 not prepared to deal with them if they occur in initializers. */
1313 if (op0 != orig_op0
1314 && code == ADDR_EXPR
1315 && (op1 = get_base_address (op0)) != NULL_TREE
1316 && TREE_CODE (op1) == INDIRECT_REF
1317 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
7549df0d 1318 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
737a23cc 1319 else if (op0 != orig_op0 || in_init)
a75b1c71 1320 ret = in_init
389dd41b 1321 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1322 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
a75b1c71 1323 else
1324 ret = fold (expr);
1325 if (code == INDIRECT_REF
1326 && ret != expr
1327 && TREE_CODE (ret) == INDIRECT_REF)
1328 {
1329 TREE_READONLY (ret) = TREE_READONLY (expr);
1330 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1331 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1332 }
672d914b 1333 switch (code)
1334 {
1335 case FIX_TRUNC_EXPR:
1336 case FLOAT_EXPR:
1337 CASE_CONVERT:
1338 /* Don't warn about explicit conversions. We will already
1339 have warned about suspect implicit conversions. */
1340 break;
1341
1342 default:
1343 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1344 overflow_warning (EXPR_LOCATION (expr), ret);
1345 break;
1346 }
a75b1c71 1347 goto out;
1348
1349 case TRUTH_ANDIF_EXPR:
1350 case TRUTH_ORIF_EXPR:
1351 /* Binary operations not necessarily evaluating both
1352 arguments. */
1353 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1354 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1355 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
f59e3889 1356 STRIP_TYPE_NOPS (op0);
672d914b 1357
1358 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1359 ? truthvalue_false_node
1360 : truthvalue_true_node));
fc501191 1361 c_disable_warnings (unused_p);
a75b1c71 1362 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
f59e3889 1363 STRIP_TYPE_NOPS (op1);
fc501191 1364 c_enable_warnings (unused_p);
672d914b 1365
a75b1c71 1366 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1367 ret = in_init
389dd41b 1368 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1369 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1370 else
1371 ret = fold (expr);
1372 *maybe_const_operands &= op0_const;
1373 *maybe_const_itself &= op0_const_self;
1374 if (!(flag_isoc99
1375 && op0_const
1376 && op0_const_self
1377 && (code == TRUTH_ANDIF_EXPR
1378 ? op0 == truthvalue_false_node
1379 : op0 == truthvalue_true_node)))
1380 *maybe_const_operands &= op1_const;
1381 if (!(op0_const
1382 && op0_const_self
1383 && (code == TRUTH_ANDIF_EXPR
1384 ? op0 == truthvalue_false_node
1385 : op0 == truthvalue_true_node)))
1386 *maybe_const_itself &= op1_const_self;
1387 goto out;
1388
1389 case COND_EXPR:
1390 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1391 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1392 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1393 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
672d914b 1394
f59e3889 1395 STRIP_TYPE_NOPS (op0);
fc501191 1396 c_disable_warnings (op0 == truthvalue_false_node);
a75b1c71 1397 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
f59e3889 1398 STRIP_TYPE_NOPS (op1);
fc501191 1399 c_enable_warnings (op0 == truthvalue_false_node);
672d914b 1400
fc501191 1401 c_disable_warnings (op0 == truthvalue_true_node);
a75b1c71 1402 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
f59e3889 1403 STRIP_TYPE_NOPS (op2);
fc501191 1404 c_enable_warnings (op0 == truthvalue_true_node);
672d914b 1405
a75b1c71 1406 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
389dd41b 1407 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
a75b1c71 1408 else
1409 ret = fold (expr);
1410 *maybe_const_operands &= op0_const;
1411 *maybe_const_itself &= op0_const_self;
1412 if (!(flag_isoc99
1413 && op0_const
1414 && op0_const_self
1415 && op0 == truthvalue_false_node))
1416 *maybe_const_operands &= op1_const;
1417 if (!(op0_const
1418 && op0_const_self
1419 && op0 == truthvalue_false_node))
1420 *maybe_const_itself &= op1_const_self;
1421 if (!(flag_isoc99
1422 && op0_const
1423 && op0_const_self
1424 && op0 == truthvalue_true_node))
1425 *maybe_const_operands &= op2_const;
1426 if (!(op0_const
1427 && op0_const_self
1428 && op0 == truthvalue_true_node))
1429 *maybe_const_itself &= op2_const_self;
1430 goto out;
1431
c6418a4e 1432 case EXCESS_PRECISION_EXPR:
1433 /* Each case where an operand with excess precision may be
1434 encountered must remove the EXCESS_PRECISION_EXPR around
1435 inner operands and possibly put one around the whole
1436 expression or possibly convert to the semantic type (which
1437 c_fully_fold does); we cannot tell at this stage which is
1438 appropriate in any particular case. */
1439 gcc_unreachable ();
1440
a75b1c71 1441 default:
1442 /* Various codes may appear through folding built-in functions
1443 and their arguments. */
1444 goto out;
1445 }
1446
1447 out:
1448 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1449 have been done by this point, so remove them again. */
1450 nowarning |= TREE_NO_WARNING (ret);
1451 STRIP_TYPE_NOPS (ret);
1452 if (nowarning && !TREE_NO_WARNING (ret))
1453 {
1454 if (!CAN_HAVE_LOCATION_P (ret))
1455 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1456 TREE_NO_WARNING (ret) = 1;
1457 }
1458 if (ret != expr)
1459 protected_set_expr_location (ret, loc);
1460 return ret;
1461}
1462
1463/* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1464 return EXP. Otherwise, return either EXP or its known constant
1465 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1466 Is the BLKmode test appropriate? */
1467
1468tree
1469decl_constant_value_for_optimization (tree exp)
1470{
1471 tree ret;
1472
1473 /* This function is only used by C, for c_fully_fold and other
1474 optimization, and may not be correct for C++. */
1475 if (c_dialect_cxx ())
1476 gcc_unreachable ();
1477
1478 if (!optimize
1479 || TREE_CODE (exp) != VAR_DECL
1480 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1481 || DECL_MODE (exp) == BLKmode)
1482 return exp;
1483
1484 ret = decl_constant_value (exp);
1485 /* Avoid unwanted tree sharing between the initializer and current
1486 function's body where the tree can be modified e.g. by the
1487 gimplifier. */
1488 if (ret != exp && TREE_STATIC (exp))
1489 ret = unshare_expr (ret);
1490 return ret;
1491}
1492
2a1736ed 1493/* Print a warning if a constant expression had overflow in folding.
1494 Invoke this function on every expression that the language
1495 requires to be a constant expression.
1496 Note the ANSI C standard says it is erroneous for a
1497 constant expression to overflow. */
b2806639 1498
1499void
1cae46be 1500constant_expression_warning (tree value)
07317e69 1501{
48e1416a 1502 if (warn_overflow && pedantic
07317e69 1503 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1504 || TREE_CODE (value) == FIXED_CST
1505 || TREE_CODE (value) == VECTOR_CST
1506 || TREE_CODE (value) == COMPLEX_CST)
1507 && TREE_OVERFLOW (value))
21ca8540 1508 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1509}
1510
1511/* The same as above but print an unconditional error. */
1512void
1513constant_expression_error (tree value)
b2806639 1514{
837e1122 1515 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1516 || TREE_CODE (value) == FIXED_CST
886cfd4f 1517 || TREE_CODE (value) == VECTOR_CST
837e1122 1518 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1519 && TREE_OVERFLOW (value))
1520 error ("overflow in constant expression");
2a1736ed 1521}
1522
f170d67f 1523/* Print a warning if an expression had overflow in folding and its
1524 operands hadn't.
1525
2a1736ed 1526 Invoke this function on every expression that
1527 (1) appears in the source code, and
f170d67f 1528 (2) is a constant expression that overflowed, and
2a1736ed 1529 (3) is not already checked by convert_and_check;
f170d67f 1530 however, do not invoke this function on operands of explicit casts
1531 or when the expression is the result of an operator and any operand
1532 already overflowed. */
2a1736ed 1533
1534void
e60a6f7b 1535overflow_warning (location_t loc, tree value)
2a1736ed 1536{
48d94ede 1537 if (c_inhibit_evaluation_warnings != 0)
1538 return;
f170d67f 1539
1540 switch (TREE_CODE (value))
886cfd4f 1541 {
f170d67f 1542 case INTEGER_CST:
e60a6f7b 1543 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
f170d67f 1544 break;
48e1416a 1545
f170d67f 1546 case REAL_CST:
e60a6f7b 1547 warning_at (loc, OPT_Woverflow,
1548 "floating point overflow in expression");
f170d67f 1549 break;
48e1416a 1550
9421ebb9 1551 case FIXED_CST:
e60a6f7b 1552 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
9421ebb9 1553 break;
1554
f170d67f 1555 case VECTOR_CST:
e60a6f7b 1556 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
f170d67f 1557 break;
48e1416a 1558
f170d67f 1559 case COMPLEX_CST:
1560 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
e60a6f7b 1561 warning_at (loc, OPT_Woverflow,
1562 "complex integer overflow in expression");
f170d67f 1563 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
e60a6f7b 1564 warning_at (loc, OPT_Woverflow,
1565 "complex floating point overflow in expression");
f170d67f 1566 break;
1567
1568 default:
1569 break;
886cfd4f 1570 }
2a1736ed 1571}
1572
03033af4 1573/* Warn about uses of logical || / && operator in a context where it
1574 is likely that the bitwise equivalent was intended by the
1575 programmer. We have seen an expression in which CODE is a binary
9c20c4fc 1576 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1577 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
b13d1547 1578void
9c20c4fc 1579warn_logical_operator (location_t location, enum tree_code code, tree type,
48e1416a 1580 enum tree_code code_left, tree op_left,
03033af4 1581 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1582{
9c20c4fc 1583 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1584 int in0_p, in1_p, in_p;
1585 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1586 bool strict_overflow_p = false;
1587
03033af4 1588 if (code != TRUTH_ANDIF_EXPR
1589 && code != TRUTH_AND_EXPR
1590 && code != TRUTH_ORIF_EXPR
1591 && code != TRUTH_OR_EXPR)
1592 return;
1593
1594 /* Warn if &&/|| are being used in a context where it is
1595 likely that the bitwise equivalent was intended by the
1596 programmer. That is, an expression such as op && MASK
1597 where op should not be any boolean expression, nor a
1598 constant, and mask seems to be a non-boolean integer constant. */
1599 if (!truth_value_p (code_left)
1600 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1601 && !CONSTANT_CLASS_P (op_left)
1602 && !TREE_NO_WARNING (op_left)
1603 && TREE_CODE (op_right) == INTEGER_CST
1604 && !integer_zerop (op_right)
1605 && !integer_onep (op_right))
b13d1547 1606 {
9c20c4fc 1607 if (or_op)
03033af4 1608 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1609 " applied to non-boolean constant");
1610 else
1611 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1612 " applied to non-boolean constant");
1613 TREE_NO_WARNING (op_left) = true;
9c20c4fc 1614 return;
1615 }
1616
1617 /* We do not warn for constants because they are typical of macro
1618 expansions that test for features. */
1619 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1620 return;
1621
1622 /* This warning only makes sense with logical operands. */
1623 if (!(truth_value_p (TREE_CODE (op_left))
1624 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1625 || !(truth_value_p (TREE_CODE (op_right))
1626 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1627 return;
1628
9c20c4fc 1629
686369e8 1630 /* We first test whether either side separately is trivially true
1631 (with OR) or trivially false (with AND). If so, do not warn.
1632 This is a common idiom for testing ranges of data types in
1633 portable code. */
1634 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1635 if (!lhs)
1636 return;
1637 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1638 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1639
686369e8 1640 /* If this is an OR operation, invert both sides; now, the result
1641 should be always false to get a warning. */
1642 if (or_op)
1643 in0_p = !in0_p;
1644
1645 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
d42e7c5a 1646 if (tem && integer_zerop (tem))
686369e8 1647 return;
1648
1649 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1650 if (!rhs)
1651 return;
1652 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1653 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
48e1416a 1654
686369e8 1655 /* If this is an OR operation, invert both sides; now, the result
1656 should be always false to get a warning. */
9c20c4fc 1657 if (or_op)
686369e8 1658 in1_p = !in1_p;
48e1416a 1659
686369e8 1660 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
d42e7c5a 1661 if (tem && integer_zerop (tem))
686369e8 1662 return;
1663
1664 /* If both expressions have the same operand, if we can merge the
1665 ranges, and if the range test is always false, then warn. */
1666 if (operand_equal_p (lhs, rhs, 0)
9c20c4fc 1667 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1668 in1_p, low1, high1)
389dd41b 1669 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
686369e8 1670 type, lhs, in_p, low, high))
1671 && integer_zerop (tem))
9c20c4fc 1672 {
9c20c4fc 1673 if (or_op)
1674 warning_at (location, OPT_Wlogical_op,
1675 "logical %<or%> "
1676 "of collectively exhaustive tests is always true");
1677 else
1678 warning_at (location, OPT_Wlogical_op,
1679 "logical %<and%> "
1680 "of mutually exclusive tests is always false");
b13d1547 1681 }
1682}
1683
1684
3d177e8c 1685/* Warn if EXP contains any computations whose results are not used.
1686 Return true if a warning is printed; false otherwise. LOCUS is the
1687 (potential) location of the expression. */
1688
1689bool
1690warn_if_unused_value (const_tree exp, location_t locus)
1691{
1692 restart:
1693 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1694 return false;
1695
1696 /* Don't warn about void constructs. This includes casting to void,
1697 void function calls, and statement expressions with a final cast
1698 to void. */
1699 if (VOID_TYPE_P (TREE_TYPE (exp)))
1700 return false;
1701
1702 if (EXPR_HAS_LOCATION (exp))
1703 locus = EXPR_LOCATION (exp);
1704
1705 switch (TREE_CODE (exp))
1706 {
1707 case PREINCREMENT_EXPR:
1708 case POSTINCREMENT_EXPR:
1709 case PREDECREMENT_EXPR:
1710 case POSTDECREMENT_EXPR:
1711 case MODIFY_EXPR:
1712 case INIT_EXPR:
1713 case TARGET_EXPR:
1714 case CALL_EXPR:
1715 case TRY_CATCH_EXPR:
1716 case WITH_CLEANUP_EXPR:
1717 case EXIT_EXPR:
1718 case VA_ARG_EXPR:
1719 return false;
1720
1721 case BIND_EXPR:
1722 /* For a binding, warn if no side effect within it. */
1723 exp = BIND_EXPR_BODY (exp);
1724 goto restart;
1725
1726 case SAVE_EXPR:
1727 case NON_LVALUE_EXPR:
d85dbdb3 1728 case NOP_EXPR:
3d177e8c 1729 exp = TREE_OPERAND (exp, 0);
1730 goto restart;
1731
1732 case TRUTH_ORIF_EXPR:
1733 case TRUTH_ANDIF_EXPR:
1734 /* In && or ||, warn if 2nd operand has no side effect. */
1735 exp = TREE_OPERAND (exp, 1);
1736 goto restart;
1737
1738 case COMPOUND_EXPR:
1739 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1740 return true;
1741 /* Let people do `(foo (), 0)' without a warning. */
1742 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1743 return false;
1744 exp = TREE_OPERAND (exp, 1);
1745 goto restart;
1746
1747 case COND_EXPR:
1748 /* If this is an expression with side effects, don't warn; this
1749 case commonly appears in macro expansions. */
1750 if (TREE_SIDE_EFFECTS (exp))
1751 return false;
1752 goto warn;
1753
1754 case INDIRECT_REF:
1755 /* Don't warn about automatic dereferencing of references, since
1756 the user cannot control it. */
1757 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1758 {
1759 exp = TREE_OPERAND (exp, 0);
1760 goto restart;
1761 }
1762 /* Fall through. */
1763
1764 default:
1765 /* Referencing a volatile value is a side effect, so don't warn. */
1766 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1767 && TREE_THIS_VOLATILE (exp))
1768 return false;
1769
1770 /* If this is an expression which has no operands, there is no value
1771 to be unused. There are no such language-independent codes,
1772 but front ends may define such. */
1773 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1774 return false;
1775
1776 warn:
1777 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1778 }
1779}
1780
1781
bcf22371 1782/* Print a warning about casts that might indicate violation
1783 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 1784 strict aliasing mode is in effect. OTYPE is the original
1785 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 1786
e6fa0ea6 1787bool
1e31ff37 1788strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 1789{
45bb3afb 1790 /* Strip pointer conversion chains and get to the correct original type. */
1791 STRIP_NOPS (expr);
1792 otype = TREE_TYPE (expr);
1793
f06537f2 1794 if (!(flag_strict_aliasing
1795 && POINTER_TYPE_P (type)
1796 && POINTER_TYPE_P (otype)
1797 && !VOID_TYPE_P (TREE_TYPE (type)))
1798 /* If the type we are casting to is a ref-all pointer
1799 dereferencing it is always valid. */
1800 || TYPE_REF_CAN_ALIAS_ALL (type))
e6fa0ea6 1801 return false;
1802
1803 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 1804 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 1805 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 1806 {
1807 /* Casting the address of an object to non void pointer. Warn
1808 if the cast breaks type based aliasing. */
e6fa0ea6 1809 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1810 {
1811 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1812 "might break strict-aliasing rules");
1813 return true;
1814 }
bcf22371 1815 else
1816 {
48e1416a 1817 /* warn_strict_aliasing >= 3. This includes the default (3).
e6fa0ea6 1818 Only warn if the cast is dereferenced immediately. */
32c2fdea 1819 alias_set_type set1 =
e6fa0ea6 1820 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 1821 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 1822
62d823d0 1823 if (set1 != set2 && set2 != 0
1824 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
e6fa0ea6 1825 {
1826 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1827 "pointer will break strict-aliasing rules");
1828 return true;
1829 }
1830 else if (warn_strict_aliasing == 2
879f881c 1831 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 1832 {
1833 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1834 "pointer might break strict-aliasing rules");
1835 return true;
1836 }
bcf22371 1837 }
1838 }
e6fa0ea6 1839 else
1840 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1841 {
1842 /* At this level, warn for any conversions, even if an address is
1843 not taken in the same statement. This will likely produce many
1844 false positives, but could be useful to pinpoint problems that
1845 are not revealed at higher levels. */
32c2fdea 1846 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1847 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1848 if (!COMPLETE_TYPE_P (type)
879f881c 1849 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 1850 {
1851 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1852 "pointer might break strict-aliasing rules");
1853 return true;
1854 }
1855 }
1856
1857 return false;
bcf22371 1858}
1859
f003f5dc 1860/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1861 sizeof as last operand of certain builtins. */
1862
1863void
57f872a2 1864sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
f1f41a6c 1865 vec<tree, va_gc> *params, tree *sizeof_arg,
f003f5dc 1866 bool (*comp_types) (tree, tree))
1867{
1868 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
57f872a2 1869 bool strop = false, cmp = false;
1870 unsigned int idx = ~0;
1871 location_t loc;
f003f5dc 1872
1873 if (TREE_CODE (callee) != FUNCTION_DECL
1874 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
f1f41a6c 1875 || vec_safe_length (params) <= 1)
f003f5dc 1876 return;
1877
f003f5dc 1878 switch (DECL_FUNCTION_CODE (callee))
1879 {
1880 case BUILT_IN_STRNCMP:
1881 case BUILT_IN_STRNCASECMP:
57f872a2 1882 cmp = true;
1883 /* FALLTHRU */
f003f5dc 1884 case BUILT_IN_STRNCPY:
57f872a2 1885 case BUILT_IN_STRNCPY_CHK:
f003f5dc 1886 case BUILT_IN_STRNCAT:
57f872a2 1887 case BUILT_IN_STRNCAT_CHK:
1888 case BUILT_IN_STPNCPY:
1889 case BUILT_IN_STPNCPY_CHK:
f003f5dc 1890 strop = true;
1891 /* FALLTHRU */
1892 case BUILT_IN_MEMCPY:
57f872a2 1893 case BUILT_IN_MEMCPY_CHK:
f003f5dc 1894 case BUILT_IN_MEMMOVE:
57f872a2 1895 case BUILT_IN_MEMMOVE_CHK:
f1f41a6c 1896 if (params->length () < 3)
57f872a2 1897 return;
f1f41a6c 1898 src = (*params)[1];
1899 dest = (*params)[0];
57f872a2 1900 idx = 2;
1901 break;
1902 case BUILT_IN_BCOPY:
f1f41a6c 1903 if (params->length () < 3)
57f872a2 1904 return;
f1f41a6c 1905 src = (*params)[0];
1906 dest = (*params)[1];
57f872a2 1907 idx = 2;
1908 break;
f003f5dc 1909 case BUILT_IN_MEMCMP:
57f872a2 1910 case BUILT_IN_BCMP:
f1f41a6c 1911 if (params->length () < 3)
f003f5dc 1912 return;
f1f41a6c 1913 src = (*params)[1];
1914 dest = (*params)[0];
57f872a2 1915 idx = 2;
1916 cmp = true;
f003f5dc 1917 break;
1918 case BUILT_IN_MEMSET:
57f872a2 1919 case BUILT_IN_MEMSET_CHK:
f1f41a6c 1920 if (params->length () < 3)
f003f5dc 1921 return;
f1f41a6c 1922 dest = (*params)[0];
57f872a2 1923 idx = 2;
1924 break;
1925 case BUILT_IN_BZERO:
f1f41a6c 1926 dest = (*params)[0];
57f872a2 1927 idx = 1;
f003f5dc 1928 break;
1929 case BUILT_IN_STRNDUP:
f1f41a6c 1930 src = (*params)[0];
f003f5dc 1931 strop = true;
57f872a2 1932 idx = 1;
1933 break;
1934 case BUILT_IN_MEMCHR:
f1f41a6c 1935 if (params->length () < 3)
57f872a2 1936 return;
f1f41a6c 1937 src = (*params)[0];
57f872a2 1938 idx = 2;
1939 break;
1940 case BUILT_IN_SNPRINTF:
1941 case BUILT_IN_SNPRINTF_CHK:
1942 case BUILT_IN_VSNPRINTF:
1943 case BUILT_IN_VSNPRINTF_CHK:
f1f41a6c 1944 dest = (*params)[0];
57f872a2 1945 idx = 1;
1946 strop = true;
f003f5dc 1947 break;
1948 default:
1949 break;
1950 }
1951
57f872a2 1952 if (idx >= 3)
1953 return;
1954
1955 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1956 return;
1957
1958 type = TYPE_P (sizeof_arg[idx])
1959 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1960 if (!POINTER_TYPE_P (type))
1961 return;
1962
f003f5dc 1963 if (dest
1964 && (tem = tree_strip_nop_conversions (dest))
1965 && POINTER_TYPE_P (TREE_TYPE (tem))
1966 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1967 return;
1968
1969 if (src
1970 && (tem = tree_strip_nop_conversions (src))
1971 && POINTER_TYPE_P (TREE_TYPE (tem))
1972 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1973 return;
1974
57f872a2 1975 loc = sizeof_arg_loc[idx];
1976
1977 if (dest && !cmp)
f003f5dc 1978 {
57f872a2 1979 if (!TYPE_P (sizeof_arg[idx])
1980 && operand_equal_p (dest, sizeof_arg[idx], 0)
f003f5dc 1981 && comp_types (TREE_TYPE (dest), type))
1982 {
57f872a2 1983 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 1984 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1985 "argument to %<sizeof%> in %qD call is the same "
1986 "expression as the destination; did you mean to "
1987 "remove the addressof?", callee);
1988 else if ((TYPE_PRECISION (TREE_TYPE (type))
1989 == TYPE_PRECISION (char_type_node))
1990 || strop)
1991 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1992 "argument to %<sizeof%> in %qD call is the same "
1993 "expression as the destination; did you mean to "
1994 "provide an explicit length?", callee);
1995 else
1996 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1997 "argument to %<sizeof%> in %qD call is the same "
1998 "expression as the destination; did you mean to "
1999 "dereference it?", callee);
2000 return;
2001 }
2002
2003 if (POINTER_TYPE_P (TREE_TYPE (dest))
2004 && !strop
2005 && comp_types (TREE_TYPE (dest), type)
2006 && !VOID_TYPE_P (TREE_TYPE (type)))
2007 {
2008 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2009 "argument to %<sizeof%> in %qD call is the same "
2010 "pointer type %qT as the destination; expected %qT "
2011 "or an explicit length", callee, TREE_TYPE (dest),
2012 TREE_TYPE (TREE_TYPE (dest)));
2013 return;
2014 }
2015 }
2016
57f872a2 2017 if (src && !cmp)
f003f5dc 2018 {
57f872a2 2019 if (!TYPE_P (sizeof_arg[idx])
2020 && operand_equal_p (src, sizeof_arg[idx], 0)
f003f5dc 2021 && comp_types (TREE_TYPE (src), type))
2022 {
57f872a2 2023 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2024 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2025 "argument to %<sizeof%> in %qD call is the same "
2026 "expression as the source; did you mean to "
2027 "remove the addressof?", callee);
2028 else if ((TYPE_PRECISION (TREE_TYPE (type))
2029 == TYPE_PRECISION (char_type_node))
2030 || strop)
2031 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2032 "argument to %<sizeof%> in %qD call is the same "
2033 "expression as the source; did you mean to "
2034 "provide an explicit length?", callee);
2035 else
2036 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2037 "argument to %<sizeof%> in %qD call is the same "
2038 "expression as the source; did you mean to "
2039 "dereference it?", callee);
2040 return;
2041 }
2042
2043 if (POINTER_TYPE_P (TREE_TYPE (src))
2044 && !strop
2045 && comp_types (TREE_TYPE (src), type)
2046 && !VOID_TYPE_P (TREE_TYPE (type)))
2047 {
2048 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2049 "argument to %<sizeof%> in %qD call is the same "
2050 "pointer type %qT as the source; expected %qT "
2051 "or an explicit length", callee, TREE_TYPE (src),
2052 TREE_TYPE (TREE_TYPE (src)));
2053 return;
2054 }
2055 }
57f872a2 2056
2057 if (dest)
2058 {
2059 if (!TYPE_P (sizeof_arg[idx])
2060 && operand_equal_p (dest, sizeof_arg[idx], 0)
2061 && comp_types (TREE_TYPE (dest), type))
2062 {
2063 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2064 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2065 "argument to %<sizeof%> in %qD call is the same "
2066 "expression as the first source; did you mean to "
2067 "remove the addressof?", callee);
2068 else if ((TYPE_PRECISION (TREE_TYPE (type))
2069 == TYPE_PRECISION (char_type_node))
2070 || strop)
2071 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2072 "argument to %<sizeof%> in %qD call is the same "
2073 "expression as the first source; did you mean to "
2074 "provide an explicit length?", callee);
2075 else
2076 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2077 "argument to %<sizeof%> in %qD call is the same "
2078 "expression as the first source; did you mean to "
2079 "dereference it?", callee);
2080 return;
2081 }
2082
2083 if (POINTER_TYPE_P (TREE_TYPE (dest))
2084 && !strop
2085 && comp_types (TREE_TYPE (dest), type)
2086 && !VOID_TYPE_P (TREE_TYPE (type)))
2087 {
2088 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2089 "argument to %<sizeof%> in %qD call is the same "
2090 "pointer type %qT as the first source; expected %qT "
2091 "or an explicit length", callee, TREE_TYPE (dest),
2092 TREE_TYPE (TREE_TYPE (dest)));
2093 return;
2094 }
2095 }
2096
2097 if (src)
2098 {
2099 if (!TYPE_P (sizeof_arg[idx])
2100 && operand_equal_p (src, sizeof_arg[idx], 0)
2101 && comp_types (TREE_TYPE (src), type))
2102 {
2103 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2104 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2105 "argument to %<sizeof%> in %qD call is the same "
2106 "expression as the second source; did you mean to "
2107 "remove the addressof?", callee);
2108 else if ((TYPE_PRECISION (TREE_TYPE (type))
2109 == TYPE_PRECISION (char_type_node))
2110 || strop)
2111 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2112 "argument to %<sizeof%> in %qD call is the same "
2113 "expression as the second source; did you mean to "
2114 "provide an explicit length?", callee);
2115 else
2116 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2117 "argument to %<sizeof%> in %qD call is the same "
2118 "expression as the second source; did you mean to "
2119 "dereference it?", callee);
2120 return;
2121 }
2122
2123 if (POINTER_TYPE_P (TREE_TYPE (src))
2124 && !strop
2125 && comp_types (TREE_TYPE (src), type)
2126 && !VOID_TYPE_P (TREE_TYPE (type)))
2127 {
2128 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2129 "argument to %<sizeof%> in %qD call is the same "
2130 "pointer type %qT as the second source; expected %qT "
2131 "or an explicit length", callee, TREE_TYPE (src),
2132 TREE_TYPE (TREE_TYPE (src)));
2133 return;
2134 }
2135 }
2136
f003f5dc 2137}
2138
3f08e399 2139/* Warn for unlikely, improbable, or stupid DECL declarations
2140 of `main'. */
2141
2142void
2143check_main_parameter_types (tree decl)
2144{
d0af78c5 2145 function_args_iterator iter;
2146 tree type;
3f08e399 2147 int argct = 0;
2148
d0af78c5 2149 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2150 {
2151 /* XXX void_type_node belies the abstraction. */
2152 if (type == void_type_node || type == error_mark_node )
2153 break;
2154
2155 ++argct;
2156 switch (argct)
2157 {
2158 case 1:
2159 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2160 pedwarn (input_location, OPT_Wmain,
2161 "first argument of %q+D should be %<int%>", decl);
2162 break;
2163
2164 case 2:
2165 if (TREE_CODE (type) != POINTER_TYPE
2166 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2167 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2168 != char_type_node))
2169 pedwarn (input_location, OPT_Wmain,
2170 "second argument of %q+D should be %<char **%>", decl);
2171 break;
2172
2173 case 3:
2174 if (TREE_CODE (type) != POINTER_TYPE
2175 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2176 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2177 != char_type_node))
2178 pedwarn (input_location, OPT_Wmain,
2179 "third argument of %q+D should probably be "
2180 "%<char **%>", decl);
2181 break;
2182 }
2183 }
3f08e399 2184
2185 /* It is intentional that this message does not mention the third
2186 argument because it's only mentioned in an appendix of the
2187 standard. */
2188 if (argct > 0 && (argct < 2 || argct > 3))
d0af78c5 2189 pedwarn (input_location, OPT_Wmain,
2190 "%q+D takes only zero or two arguments", decl);
3f08e399 2191}
2192
ed7c4e62 2193/* True if pointers to distinct types T1 and T2 can be converted to
2194 each other without an explicit cast. Only returns true for opaque
2195 vector types. */
2196bool
2197vector_targets_convertible_p (const_tree t1, const_tree t2)
2198{
2199 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
8d125f7d 2200 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 2201 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2202 return true;
2203
2204 return false;
2205}
2206
546c4794 2207/* True if vector types T1 and T2 can be converted to each other
2208 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2209 can only be converted with -flax-vector-conversions yet that is not
2210 in effect, emit a note telling the user about that option if such
2211 a note has not previously been emitted. */
2212bool
9f627b1a 2213vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 2214{
546c4794 2215 static bool emitted_lax_note = false;
ae6db8ab 2216 bool convertible_lax;
2217
8d125f7d 2218 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 2219 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2220 return true;
2221
2222 convertible_lax =
2223 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2224 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2225 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
2226 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2227 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 2228
2229 if (!convertible_lax || flag_lax_vector_conversions)
2230 return convertible_lax;
2231
2232 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 2233 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 2234 return true;
2235
2236 if (emit_lax_note && !emitted_lax_note)
2237 {
2238 emitted_lax_note = true;
5bcc316e 2239 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 2240 "conversions between vectors with differing "
2241 "element types or numbers of subparts");
2242 }
2243
2244 return false;
8b4b9810 2245}
2246
bf0cb017 2247/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2248 and have vector types, V0 has the same type as V1, and the number of
2249 elements of V0, V1, MASK is the same.
2250
2251 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2252 called with two arguments. In this case implementation passes the
2253 first argument twice in order to share the same tree code. This fact
2254 could enable the mask-values being twice the vector length. This is
2255 an implementation accident and this semantics is not guaranteed to
2256 the user. */
2257tree
2258c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2259{
2260 tree ret;
2261 bool wrap = true;
2262 bool maybe_const = false;
2263 bool two_arguments = false;
2264
2265 if (v1 == NULL_TREE)
2266 {
2267 two_arguments = true;
2268 v1 = v0;
2269 }
2270
2271 if (v0 == error_mark_node || v1 == error_mark_node
2272 || mask == error_mark_node)
2273 return error_mark_node;
2274
2275 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2276 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2277 {
2278 error_at (loc, "__builtin_shuffle last argument must "
2279 "be an integer vector");
2280 return error_mark_node;
2281 }
2282
2283 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2284 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2285 {
2286 error_at (loc, "__builtin_shuffle arguments must be vectors");
2287 return error_mark_node;
2288 }
2289
2290 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2291 {
2292 error_at (loc, "__builtin_shuffle argument vectors must be of "
2293 "the same type");
2294 return error_mark_node;
2295 }
2296
2297 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2298 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2299 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2300 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2301 {
2302 error_at (loc, "__builtin_shuffle number of elements of the "
2303 "argument vector(s) and the mask vector should "
2304 "be the same");
2305 return error_mark_node;
2306 }
2307
2308 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2309 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2310 {
2311 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2312 "must have the same size as inner type of the mask");
2313 return error_mark_node;
2314 }
2315
2316 if (!c_dialect_cxx ())
2317 {
2318 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2319 v0 = c_fully_fold (v0, false, &maybe_const);
2320 wrap &= maybe_const;
2321
2322 if (two_arguments)
2323 v1 = v0 = save_expr (v0);
2324 else
2325 {
2326 v1 = c_fully_fold (v1, false, &maybe_const);
2327 wrap &= maybe_const;
2328 }
2329
2330 mask = c_fully_fold (mask, false, &maybe_const);
2331 wrap &= maybe_const;
2332 }
2333
2334 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2335
2336 if (!c_dialect_cxx () && !wrap)
2337 ret = c_wrap_maybe_const (ret, true);
2338
2339 return ret;
2340}
2341
7f506bca 2342/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2343 to integral type. */
2344
2345static tree
2346c_common_get_narrower (tree op, int *unsignedp_ptr)
2347{
2348 op = get_narrower (op, unsignedp_ptr);
2349
2350 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2351 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2352 {
2353 /* C++0x scoped enumerations don't implicitly convert to integral
2354 type; if we stripped an explicit conversion to a larger type we
2355 need to replace it so common_type will still work. */
a51edb4c 2356 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2357 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 2358 op = fold_convert (type, op);
2359 }
2360 return op;
2361}
2362
2561cea2 2363/* This is a helper function of build_binary_op.
2364
2365 For certain operations if both args were extended from the same
2366 smaller type, do the arithmetic in that type and then extend.
2367
2368 BITWISE indicates a bitwise operation.
2369 For them, this optimization is safe only if
2370 both args are zero-extended or both are sign-extended.
2371 Otherwise, we might change the result.
2372 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 2373 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 2374*/
7f506bca 2375tree
2376shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 2377{
2378 int unsigned0, unsigned1;
2379 tree arg0, arg1;
2380 int uns;
2381 tree type;
2382
2383 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2384 excessive narrowing when we call get_narrower below. For
2385 example, suppose that OP0 is of unsigned int extended
2386 from signed char and that RESULT_TYPE is long long int.
2387 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2388 like
48e1416a 2389
2561cea2 2390 (long long int) (unsigned int) signed_char
2391
2392 which get_narrower would narrow down to
48e1416a 2393
2561cea2 2394 (unsigned int) signed char
48e1416a 2395
2561cea2 2396 If we do not cast OP0 first, get_narrower would return
2397 signed_char, which is inconsistent with the case of the
2398 explicit cast. */
2399 op0 = convert (result_type, op0);
2400 op1 = convert (result_type, op1);
2401
7f506bca 2402 arg0 = c_common_get_narrower (op0, &unsigned0);
2403 arg1 = c_common_get_narrower (op1, &unsigned1);
2561cea2 2404
2405 /* UNS is 1 if the operation to be done is an unsigned one. */
2406 uns = TYPE_UNSIGNED (result_type);
2407
2408 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2409 but it *requires* conversion to FINAL_TYPE. */
48e1416a 2410
2561cea2 2411 if ((TYPE_PRECISION (TREE_TYPE (op0))
2412 == TYPE_PRECISION (TREE_TYPE (arg0)))
2413 && TREE_TYPE (op0) != result_type)
2414 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2415 if ((TYPE_PRECISION (TREE_TYPE (op1))
2416 == TYPE_PRECISION (TREE_TYPE (arg1)))
2417 && TREE_TYPE (op1) != result_type)
2418 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 2419
2561cea2 2420 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 2421
2561cea2 2422 /* For bitwise operations, signedness of nominal type
2423 does not matter. Consider only how operands were extended. */
2424 if (bitwise)
2425 uns = unsigned0;
48e1416a 2426
2561cea2 2427 /* Note that in all three cases below we refrain from optimizing
2428 an unsigned operation on sign-extended args.
2429 That would not be valid. */
48e1416a 2430
2561cea2 2431 /* Both args variable: if both extended in same way
2432 from same width, do it in that width.
2433 Do it unsigned if args were zero-extended. */
2434 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2435 < TYPE_PRECISION (result_type))
2436 && (TYPE_PRECISION (TREE_TYPE (arg1))
2437 == TYPE_PRECISION (TREE_TYPE (arg0)))
2438 && unsigned0 == unsigned1
2439 && (unsigned0 || !uns))
2440 return c_common_signed_or_unsigned_type
2441 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2442
2443 else if (TREE_CODE (arg0) == INTEGER_CST
2444 && (unsigned1 || !uns)
2445 && (TYPE_PRECISION (TREE_TYPE (arg1))
2446 < TYPE_PRECISION (result_type))
2447 && (type
2448 = c_common_signed_or_unsigned_type (unsigned1,
2449 TREE_TYPE (arg1)))
2450 && !POINTER_TYPE_P (type)
2451 && int_fits_type_p (arg0, type))
2452 return type;
2453
2454 else if (TREE_CODE (arg1) == INTEGER_CST
2455 && (unsigned0 || !uns)
2456 && (TYPE_PRECISION (TREE_TYPE (arg0))
2457 < TYPE_PRECISION (result_type))
2458 && (type
2459 = c_common_signed_or_unsigned_type (unsigned0,
2460 TREE_TYPE (arg0)))
2461 && !POINTER_TYPE_P (type)
2462 && int_fits_type_p (arg1, type))
2463 return type;
2464
2465 return result_type;
2466}
2467
7dfa155b 2468/* Checks if expression EXPR of real/integer type cannot be converted
2469 to the real/integer type TYPE. Function returns true when:
2470 * EXPR is a constant which cannot be exactly converted to TYPE
2471 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2472 for EXPR type and TYPE being both integers or both real.
2473 * EXPR is not a constant of real type and TYPE is an integer.
2474 * EXPR is not a constant of integer type which cannot be
2475 exactly converted to real type.
2476 Function allows conversions between types of different signedness and
2477 does not return true in that case. Function can produce signedness
2478 warnings if PRODUCE_WARNS is true. */
2479bool
2480unsafe_conversion_p (tree type, tree expr, bool produce_warns)
d31d55f0 2481{
2482 bool give_warning = false;
27259707 2483 tree expr_type = TREE_TYPE (expr);
8637f1db 2484 location_t loc = EXPR_LOC_OR_HERE (expr);
d31d55f0 2485
7dfa155b 2486 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 2487 {
d31d55f0 2488 /* Warn for real constant that is not an exact integer converted
7dfa155b 2489 to integer type. */
27259707 2490 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2491 && TREE_CODE (type) == INTEGER_TYPE)
2492 {
2493 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2494 give_warning = true;
2495 }
da1fb07b 2496 /* Warn for an integer constant that does not fit into integer type. */
27259707 2497 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2498 && TREE_CODE (type) == INTEGER_TYPE
2499 && !int_fits_type_p (expr, type))
2500 {
2501 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 2502 && tree_int_cst_sgn (expr) < 0)
7dfa155b 2503 {
2504 if (produce_warns)
2505 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2506 " implicitly converted to unsigned type");
2507 }
2508 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2509 {
2510 if (produce_warns)
2511 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2512 " constant value to negative integer");
2513 }
7ee0d227 2514 else
2515 give_warning = true;
7dfa155b 2516 }
d31d55f0 2517 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 2518 {
2519 /* Warn for an integer constant that does not fit into real type. */
2520 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2521 {
2522 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2523 if (!exact_real_truncate (TYPE_MODE (type), &a))
2524 give_warning = true;
2525 }
2526 /* Warn for a real constant that does not fit into a smaller
2527 real type. */
2528 else if (TREE_CODE (expr_type) == REAL_TYPE
2529 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2530 {
2531 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2532 if (!exact_real_truncate (TYPE_MODE (type), &a))
2533 give_warning = true;
2534 }
2535 }
2536 }
2537 else
2538 {
d31d55f0 2539 /* Warn for real types converted to integer types. */
2561cea2 2540 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2541 && TREE_CODE (type) == INTEGER_TYPE)
2542 give_warning = true;
d31d55f0 2543
2561cea2 2544 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2545 && TREE_CODE (type) == INTEGER_TYPE)
2546 {
69609004 2547 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2548 expr = get_unwidened (expr, 0);
2561cea2 2549 expr_type = TREE_TYPE (expr);
69609004 2550
2561cea2 2551 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 2552 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 2553 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2554 || TREE_CODE (expr) == BIT_XOR_EXPR)
2555 {
27259707 2556 /* If both args were extended from a shortest type,
2557 use that type if that is safe. */
48e1416a 2558 expr_type = shorten_binary_op (expr_type,
2559 TREE_OPERAND (expr, 0),
2560 TREE_OPERAND (expr, 1),
2561cea2 2561 /* bitwise */1);
2562
2561cea2 2563 if (TREE_CODE (expr) == BIT_AND_EXPR)
2564 {
2565 tree op0 = TREE_OPERAND (expr, 0);
2566 tree op1 = TREE_OPERAND (expr, 1);
30de145b 2567 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2568 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2569
2570 /* If one of the operands is a non-negative constant
2571 that fits in the target type, then the type of the
2572 other operand does not matter. */
2561cea2 2573 if ((TREE_CODE (op0) == INTEGER_CST
2574 && int_fits_type_p (op0, c_common_signed_type (type))
2575 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2576 || (TREE_CODE (op1) == INTEGER_CST
27259707 2577 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 2578 && int_fits_type_p (op1,
27259707 2579 c_common_unsigned_type (type))))
7dfa155b 2580 return false;
30de145b 2581 /* If constant is unsigned and fits in the target
2582 type, then the result will also fit. */
2583 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 2584 && unsigned0
30de145b 2585 && int_fits_type_p (op0, type))
2586 || (TREE_CODE (op1) == INTEGER_CST
2587 && unsigned1
2588 && int_fits_type_p (op1, type)))
7dfa155b 2589 return false;
2561cea2 2590 }
2591 }
7dfa155b 2592 /* Warn for integer types converted to smaller integer types. */
48e1416a 2593 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
7ee0d227 2594 give_warning = true;
2595
2596 /* When they are the same width but different signedness,
2597 then the value may change. */
7dfa155b 2598 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 2599 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 2600 /* Even when converted to a bigger type, if the type is
2601 unsigned but expr is signed, then negative values
2602 will be changed. */
7dfa155b 2603 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2604 && produce_warns)
200dd99c 2605 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2606 "may change the sign of the result",
2607 type, expr_type);
7dfa155b 2608 }
d31d55f0 2609
2610 /* Warn for integer types converted to real types if and only if
7dfa155b 2611 all the range of values of the integer type cannot be
2612 represented by the real type. */
2561cea2 2613 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2614 && TREE_CODE (type) == REAL_TYPE)
2615 {
4c2cfa81 2616 tree type_low_bound, type_high_bound;
7dfa155b 2617 REAL_VALUE_TYPE real_low_bound, real_high_bound;
4c2cfa81 2618
2619 /* Don't warn about char y = 0xff; float x = (int) y; */
2620 expr = get_unwidened (expr, 0);
2621 expr_type = TREE_TYPE (expr);
2622
7dfa155b 2623 type_low_bound = TYPE_MIN_VALUE (expr_type);
2624 type_high_bound = TYPE_MAX_VALUE (expr_type);
2625 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2626 real_high_bound = real_value_from_int_cst (0, type_high_bound);
d31d55f0 2627
7dfa155b 2628 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2629 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2630 give_warning = true;
2631 }
d31d55f0 2632
2633 /* Warn for real types converted to smaller real types. */
2561cea2 2634 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2635 && TREE_CODE (type) == REAL_TYPE
2636 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2637 give_warning = true;
2638 }
2639
2640 return give_warning;
2641}
2642
2643/* Warns if the conversion of EXPR to TYPE may alter a value.
2644 This is a helper function for warnings_for_convert_and_check. */
2645
2646static void
2647conversion_warning (tree type, tree expr)
2648{
7dfa155b 2649 tree expr_type = TREE_TYPE (expr);
2650 location_t loc = EXPR_LOC_OR_HERE (expr);
d31d55f0 2651
7dfa155b 2652 if (!warn_conversion && !warn_sign_conversion)
2653 return;
d31d55f0 2654
7dfa155b 2655 switch (TREE_CODE (expr))
2656 {
2657 case EQ_EXPR:
2658 case NE_EXPR:
2659 case LE_EXPR:
2660 case GE_EXPR:
2661 case LT_EXPR:
2662 case GT_EXPR:
2663 case TRUTH_ANDIF_EXPR:
2664 case TRUTH_ORIF_EXPR:
2665 case TRUTH_AND_EXPR:
2666 case TRUTH_OR_EXPR:
2667 case TRUTH_XOR_EXPR:
2668 case TRUTH_NOT_EXPR:
2669 /* Conversion from boolean to a signed:1 bit-field (which only
2670 can hold the values 0 and -1) doesn't lose information - but
2671 it does change the value. */
2672 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2673 warning_at (loc, OPT_Wconversion,
2674 "conversion to %qT from boolean expression", type);
2675 return;
2676
2677 case REAL_CST:
2678 case INTEGER_CST:
2679 if (unsafe_conversion_p (type, expr, true))
2680 warning_at (loc, OPT_Wconversion,
2681 "conversion to %qT alters %qT constant value",
2682 type, expr_type);
2683 return;
2684
2685 case COND_EXPR:
2686 {
0e4e775a 2687 /* In case of COND_EXPR, we do not care about the type of
2688 COND_EXPR, only about the conversion of each operand. */
2689 tree op1 = TREE_OPERAND (expr, 1);
2690 tree op2 = TREE_OPERAND (expr, 2);
2691
2692 conversion_warning (type, op1);
2693 conversion_warning (type, op2);
2694 return;
7dfa155b 2695 }
2696
2697 default: /* 'expr' is not a constant. */
2698 if (unsafe_conversion_p (type, expr, true))
2699 warning_at (loc, OPT_Wconversion,
200dd99c 2700 "conversion to %qT from %qT may alter its value",
2701 type, expr_type);
d31d55f0 2702 }
2703}
2704
59dd8856 2705/* Produce warnings after a conversion. RESULT is the result of
2706 converting EXPR to TYPE. This is a helper function for
2707 convert_and_check and cp_convert_and_check. */
2a1736ed 2708
59dd8856 2709void
2710warnings_for_convert_and_check (tree type, tree expr, tree result)
2a1736ed 2711{
61f69bc9 2712 location_t loc = EXPR_LOC_OR_HERE (expr);
2713
da1fb07b 2714 if (TREE_CODE (expr) == INTEGER_CST
2715 && (TREE_CODE (type) == INTEGER_TYPE
2716 || TREE_CODE (type) == ENUMERAL_TYPE)
2717 && !int_fits_type_p (expr, type))
2718 {
d31d55f0 2719 /* Do not diagnose overflow in a constant expression merely
2720 because a conversion overflowed. */
da1fb07b 2721 if (TREE_OVERFLOW (result))
eddad94a 2722 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2723
da1fb07b 2724 if (TYPE_UNSIGNED (type))
d31d55f0 2725 {
da1fb07b 2726 /* This detects cases like converting -129 or 256 to
2727 unsigned char. */
2728 if (!int_fits_type_p (expr, c_common_signed_type (type)))
61f69bc9 2729 warning_at (loc, OPT_Woverflow,
2730 "large integer implicitly truncated to unsigned type");
7ee0d227 2731 else
da1fb07b 2732 conversion_warning (type, expr);
2733 }
48e1416a 2734 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
e0913805 2735 warning (OPT_Woverflow,
2736 "overflow in implicit constant conversion");
2737 /* No warning for converting 0x80000000 to int. */
2738 else if (pedantic
2739 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2740 || TYPE_PRECISION (TREE_TYPE (expr))
2741 != TYPE_PRECISION (type)))
61f69bc9 2742 warning_at (loc, OPT_Woverflow,
2743 "overflow in implicit constant conversion");
e0913805 2744
7ee0d227 2745 else
e0913805 2746 conversion_warning (type, expr);
2a1736ed 2747 }
9421ebb9 2748 else if ((TREE_CODE (result) == INTEGER_CST
2749 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
61f69bc9 2750 warning_at (loc, OPT_Woverflow,
2751 "overflow in implicit constant conversion");
7ee0d227 2752 else
da1fb07b 2753 conversion_warning (type, expr);
59dd8856 2754}
2755
2756
2757/* Convert EXPR to TYPE, warning about conversion problems with constants.
2758 Invoke this function on every expression that is converted implicitly,
2759 i.e. because of language rules and not because of an explicit cast. */
2760
2761tree
2762convert_and_check (tree type, tree expr)
2763{
2764 tree result;
c6418a4e 2765 tree expr_for_warning;
2766
2767 /* Convert from a value with possible excess precision rather than
2768 via the semantic type, but do not warn about values not fitting
2769 exactly in the semantic type. */
2770 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2771 {
2772 tree orig_type = TREE_TYPE (expr);
2773 expr = TREE_OPERAND (expr, 0);
2774 expr_for_warning = convert (orig_type, expr);
2775 if (orig_type == type)
2776 return expr_for_warning;
2777 }
2778 else
2779 expr_for_warning = expr;
59dd8856 2780
2781 if (TREE_TYPE (expr) == type)
2782 return expr;
48e1416a 2783
59dd8856 2784 result = convert (type, expr);
2785
48d94ede 2786 if (c_inhibit_evaluation_warnings == 0
2787 && !TREE_OVERFLOW_P (expr)
2788 && result != error_mark_node)
c6418a4e 2789 warnings_for_convert_and_check (type, expr_for_warning, result);
59dd8856 2790
da1fb07b 2791 return result;
b2806639 2792}
2793\f
4e91a871 2794/* A node in a list that describes references to variables (EXPR), which are
2795 either read accesses if WRITER is zero, or write accesses, in which case
2796 WRITER is the parent of EXPR. */
2797struct tlist
2798{
2799 struct tlist *next;
2800 tree expr, writer;
2801};
2802
2803/* Used to implement a cache the results of a call to verify_tree. We only
2804 use this for SAVE_EXPRs. */
2805struct tlist_cache
2806{
2807 struct tlist_cache *next;
2808 struct tlist *cache_before_sp;
2809 struct tlist *cache_after_sp;
2810 tree expr;
481c6ce6 2811};
2812
4e91a871 2813/* Obstack to use when allocating tlist structures, and corresponding
2814 firstobj. */
2815static struct obstack tlist_obstack;
2816static char *tlist_firstobj = 0;
2817
2818/* Keep track of the identifiers we've warned about, so we can avoid duplicate
2819 warnings. */
2820static struct tlist *warned_ids;
2821/* SAVE_EXPRs need special treatment. We process them only once and then
2822 cache the results. */
2823static struct tlist_cache *save_expr_cache;
2824
1cae46be 2825static void add_tlist (struct tlist **, struct tlist *, tree, int);
2826static void merge_tlist (struct tlist **, struct tlist *, int);
2827static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2828static int warning_candidate_p (tree);
79973b57 2829static bool candidate_equal_p (const_tree, const_tree);
1cae46be 2830static void warn_for_collisions (struct tlist *);
2831static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2832static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 2833
4e91a871 2834/* Create a new struct tlist and fill in its fields. */
2835static struct tlist *
1cae46be 2836new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 2837{
2838 struct tlist *l;
9318f22c 2839 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 2840 l->next = next;
2841 l->expr = t;
2842 l->writer = writer;
2843 return l;
2844}
2845
2846/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2847 is nonnull, we ignore any node we find which has a writer equal to it. */
2848
2849static void
1cae46be 2850add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 2851{
2852 while (add)
2853 {
2854 struct tlist *next = add->next;
84166705 2855 if (!copy)
4e91a871 2856 add->next = *to;
79973b57 2857 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 2858 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2859 add = next;
2860 }
2861}
2862
2863/* Merge the nodes of ADD into TO. This merging process is done so that for
2864 each variable that already exists in TO, no new node is added; however if
2865 there is a write access recorded in ADD, and an occurrence on TO is only
2866 a read access, then the occurrence in TO will be modified to record the
2867 write. */
481c6ce6 2868
2869static void
1cae46be 2870merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 2871{
2872 struct tlist **end = to;
2873
2874 while (*end)
2875 end = &(*end)->next;
2876
2877 while (add)
2878 {
2879 int found = 0;
2880 struct tlist *tmp2;
2881 struct tlist *next = add->next;
2882
2883 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 2884 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 2885 {
2886 found = 1;
84166705 2887 if (!tmp2->writer)
4e91a871 2888 tmp2->writer = add->writer;
2889 }
84166705 2890 if (!found)
4e91a871 2891 {
2892 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2893 end = &(*end)->next;
2894 *end = 0;
2895 }
2896 add = next;
2897 }
2898}
2899
2900/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2901 references in list LIST conflict with it, excluding reads if ONLY writers
2902 is nonzero. */
2903
2904static void
1cae46be 2905warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2906 int only_writes)
4e91a871 2907{
2908 struct tlist *tmp;
2909
2910 /* Avoid duplicate warnings. */
2911 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 2912 if (candidate_equal_p (tmp->expr, written))
4e91a871 2913 return;
2914
2915 while (list)
2916 {
79973b57 2917 if (candidate_equal_p (list->expr, written)
2918 && !candidate_equal_p (list->writer, writer)
2919 && (!only_writes || list->writer))
4e91a871 2920 {
2921 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
8637f1db 2922 warning_at (EXPR_LOC_OR_HERE (writer),
6513b50d 2923 OPT_Wsequence_point, "operation on %qE may be undefined",
2924 list->expr);
4e91a871 2925 }
2926 list = list->next;
2927 }
2928}
2929
2930/* Given a list LIST of references to variables, find whether any of these
2931 can cause conflicts due to missing sequence points. */
2932
2933static void
1cae46be 2934warn_for_collisions (struct tlist *list)
4e91a871 2935{
2936 struct tlist *tmp;
1cae46be 2937
4e91a871 2938 for (tmp = list; tmp; tmp = tmp->next)
2939 {
2940 if (tmp->writer)
2941 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2942 }
2943}
2944
734c98be 2945/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 2946 warnings. */
2947static int
1cae46be 2948warning_candidate_p (tree x)
481c6ce6 2949{
6ef8d12f 2950 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2951 return 0;
2952
027fc6ef 2953 if (TREE_CODE (x) == BLOCK)
2954 return 0;
2955
6ef8d12f 2956 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 2957 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 2958 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2959 return 0;
2960
2961 if (!lvalue_p (x))
2962 return 0;
2963
2964 /* No point to track non-const calls, they will never satisfy
2965 operand_equal_p. */
2966 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2967 return 0;
2968
2969 if (TREE_CODE (x) == STRING_CST)
2970 return 0;
2971
2972 return 1;
79973b57 2973}
2974
2975/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2976static bool
2977candidate_equal_p (const_tree x, const_tree y)
2978{
2979 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 2980}
481c6ce6 2981
4e91a871 2982/* Walk the tree X, and record accesses to variables. If X is written by the
2983 parent tree, WRITER is the parent.
2984 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2985 expression or its only operand forces a sequence point, then everything up
2986 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2987 in PNO_SP.
2988 Once we return, we will have emitted warnings if any subexpression before
2989 such a sequence point could be undefined. On a higher level, however, the
2990 sequence point may not be relevant, and we'll merge the two lists.
2991
2992 Example: (b++, a) + b;
2993 The call that processes the COMPOUND_EXPR will store the increment of B
2994 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2995 processes the PLUS_EXPR will need to merge the two lists so that
2996 eventually, all accesses end up on the same list (and we'll warn about the
2997 unordered subexpressions b++ and b.
2998
2999 A note on merging. If we modify the former example so that our expression
3000 becomes
3001 (b++, b) + a
3002 care must be taken not simply to add all three expressions into the final
3003 PNO_SP list. The function merge_tlist takes care of that by merging the
3004 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3005 way, so that no more than one access to B is recorded. */
481c6ce6 3006
4e91a871 3007static void
1cae46be 3008verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3009 tree writer)
4e91a871 3010{
3011 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3012 enum tree_code code;
ce45a448 3013 enum tree_code_class cl;
481c6ce6 3014
e5b75768 3015 /* X may be NULL if it is the operand of an empty statement expression
3016 ({ }). */
3017 if (x == NULL)
3018 return;
3019
4e91a871 3020 restart:
3021 code = TREE_CODE (x);
e916c70c 3022 cl = TREE_CODE_CLASS (code);
481c6ce6 3023
4e91a871 3024 if (warning_candidate_p (x))
79973b57 3025 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 3026
3027 switch (code)
3028 {
67b28e3e 3029 case CONSTRUCTOR:
3030 return;
3031
4e91a871 3032 case COMPOUND_EXPR:
3033 case TRUTH_ANDIF_EXPR:
3034 case TRUTH_ORIF_EXPR:
3035 tmp_before = tmp_nosp = tmp_list3 = 0;
3036 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3037 warn_for_collisions (tmp_nosp);
3038 merge_tlist (pbefore_sp, tmp_before, 0);
3039 merge_tlist (pbefore_sp, tmp_nosp, 0);
3040 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3041 merge_tlist (pbefore_sp, tmp_list3, 0);
3042 return;
3043
3044 case COND_EXPR:
3045 tmp_before = tmp_list2 = 0;
3046 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3047 warn_for_collisions (tmp_list2);
3048 merge_tlist (pbefore_sp, tmp_before, 0);
3049 merge_tlist (pbefore_sp, tmp_list2, 1);
3050
3051 tmp_list3 = tmp_nosp = 0;
3052 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3053 warn_for_collisions (tmp_nosp);
3054 merge_tlist (pbefore_sp, tmp_list3, 0);
3055
3056 tmp_list3 = tmp_list2 = 0;
3057 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3058 warn_for_collisions (tmp_list2);
3059 merge_tlist (pbefore_sp, tmp_list3, 0);
3060 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3061 two first, to avoid warning for (a ? b++ : b++). */
3062 merge_tlist (&tmp_nosp, tmp_list2, 0);
3063 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3064 return;
3065
481c6ce6 3066 case PREDECREMENT_EXPR:
3067 case PREINCREMENT_EXPR:
3068 case POSTDECREMENT_EXPR:
3069 case POSTINCREMENT_EXPR:
4e91a871 3070 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3071 return;
3072
3073 case MODIFY_EXPR:
3074 tmp_before = tmp_nosp = tmp_list3 = 0;
3075 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3076 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3077 /* Expressions inside the LHS are not ordered wrt. the sequence points
3078 in the RHS. Example:
3079 *a = (a++, 2)
3080 Despite the fact that the modification of "a" is in the before_sp
3081 list (tmp_before), it conflicts with the use of "a" in the LHS.
3082 We can handle this by adding the contents of tmp_list3
3083 to those of tmp_before, and redoing the collision warnings for that
3084 list. */
3085 add_tlist (&tmp_before, tmp_list3, x, 1);
3086 warn_for_collisions (tmp_before);
3087 /* Exclude the LHS itself here; we first have to merge it into the
3088 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3089 didn't exclude the LHS, we'd get it twice, once as a read and once
3090 as a write. */
3091 add_tlist (pno_sp, tmp_list3, x, 0);
3092 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3093
3094 merge_tlist (pbefore_sp, tmp_before, 0);
3095 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3096 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3097 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3098 return;
481c6ce6 3099
3100 case CALL_EXPR:
4e91a871 3101 /* We need to warn about conflicts among arguments and conflicts between
3102 args and the function address. Side effects of the function address,
3103 however, are not ordered by the sequence point of the call. */
c2f47e15 3104 {
3105 call_expr_arg_iterator iter;
3106 tree arg;
48e1416a 3107 tmp_before = tmp_nosp = 0;
c2f47e15 3108 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3109 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3110 {
3111 tmp_list2 = tmp_list3 = 0;
3112 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3113 merge_tlist (&tmp_list3, tmp_list2, 0);
3114 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3115 }
3116 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3117 warn_for_collisions (tmp_before);
3118 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3119 return;
3120 }
481c6ce6 3121
3122 case TREE_LIST:
3123 /* Scan all the list, e.g. indices of multi dimensional array. */
3124 while (x)
3125 {
4e91a871 3126 tmp_before = tmp_nosp = 0;
3127 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3128 merge_tlist (&tmp_nosp, tmp_before, 0);
3129 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 3130 x = TREE_CHAIN (x);
3131 }
4e91a871 3132 return;
481c6ce6 3133
4e91a871 3134 case SAVE_EXPR:
3135 {
3136 struct tlist_cache *t;
3137 for (t = save_expr_cache; t; t = t->next)
79973b57 3138 if (candidate_equal_p (t->expr, x))
4e91a871 3139 break;
481c6ce6 3140
84166705 3141 if (!t)
481c6ce6 3142 {
9318f22c 3143 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 3144 t->next = save_expr_cache;
3145 t->expr = x;
3146 save_expr_cache = t;
3147
3148 tmp_before = tmp_nosp = 0;
3149 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3150 warn_for_collisions (tmp_nosp);
3151
3152 tmp_list3 = 0;
3153 while (tmp_nosp)
3154 {
3155 struct tlist *t = tmp_nosp;
3156 tmp_nosp = t->next;
3157 merge_tlist (&tmp_list3, t, 0);
3158 }
3159 t->cache_before_sp = tmp_before;
3160 t->cache_after_sp = tmp_list3;
481c6ce6 3161 }
4e91a871 3162 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3163 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3164 return;
3165 }
481c6ce6 3166
012916cb 3167 case ADDR_EXPR:
3168 x = TREE_OPERAND (x, 0);
3169 if (DECL_P (x))
3170 return;
3171 writer = 0;
3172 goto restart;
3173
ce45a448 3174 default:
3175 /* For other expressions, simply recurse on their operands.
a0c938f0 3176 Manual tail recursion for unary expressions.
ce45a448 3177 Other non-expressions need not be processed. */
3178 if (cl == tcc_unary)
3179 {
ce45a448 3180 x = TREE_OPERAND (x, 0);
3181 writer = 0;
3182 goto restart;
3183 }
3184 else if (IS_EXPR_CODE_CLASS (cl))
3185 {
3186 int lp;
c2f47e15 3187 int max = TREE_OPERAND_LENGTH (x);
ce45a448 3188 for (lp = 0; lp < max; lp++)
3189 {
3190 tmp_before = tmp_nosp = 0;
3191 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3192 merge_tlist (&tmp_nosp, tmp_before, 0);
3193 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3194 }
3195 }
3196 return;
481c6ce6 3197 }
481c6ce6 3198}
3199
974e2c0c 3200/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 3201 points. */
3202
4b987fac 3203DEBUG_FUNCTION void
1cae46be 3204verify_sequence_points (tree expr)
481c6ce6 3205{
4e91a871 3206 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 3207
4e91a871 3208 warned_ids = 0;
3209 save_expr_cache = 0;
3210 if (tlist_firstobj == 0)
481c6ce6 3211 {
4e91a871 3212 gcc_obstack_init (&tlist_obstack);
4fd61bc6 3213 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 3214 }
3215
4e91a871 3216 verify_tree (expr, &before_sp, &after_sp, 0);
3217 warn_for_collisions (after_sp);
3218 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 3219}
b0fc3e72 3220\f
3221/* Validate the expression after `case' and apply default promotions. */
3222
2ca392fd 3223static tree
1cae46be 3224check_case_value (tree value)
b0fc3e72 3225{
3226 if (value == NULL_TREE)
3227 return value;
3228
b96dc121 3229 if (TREE_CODE (value) == INTEGER_CST)
3230 /* Promote char or short to int. */
3231 value = perform_integral_promotions (value);
3232 else if (value != error_mark_node)
b0fc3e72 3233 {
3234 error ("case label does not reduce to an integer constant");
3235 value = error_mark_node;
3236 }
b0fc3e72 3237
6433f1c2 3238 constant_expression_warning (value);
3239
b0fc3e72 3240 return value;
3241}
3242\f
2ca392fd 3243/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 3244 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 3245 expression.
3246 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 3247 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 3248 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3249 if the case is not a case range.
3250 The caller has to make sure that we are not called with NULL for
5c9dae64 3251 CASE_LOW_P (i.e. the default case).
442e3cb9 3252 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 3253 untouched) or false if the label is out of range. */
3254
3255static bool
3256check_case_bounds (tree type, tree orig_type,
3257 tree *case_low_p, tree *case_high_p)
3258{
3259 tree min_value, max_value;
3260 tree case_low = *case_low_p;
3261 tree case_high = case_high_p ? *case_high_p : case_low;
3262
3263 /* If there was a problem with the original type, do nothing. */
3264 if (orig_type == error_mark_node)
3265 return true;
3266
3267 min_value = TYPE_MIN_VALUE (orig_type);
3268 max_value = TYPE_MAX_VALUE (orig_type);
3269
3270 /* Case label is less than minimum for type. */
3271 if (tree_int_cst_compare (case_low, min_value) < 0
3272 && tree_int_cst_compare (case_high, min_value) < 0)
3273 {
c3ceba8e 3274 warning (0, "case label value is less than minimum value for type");
2ca392fd 3275 return false;
3276 }
b27ac6b5 3277
2ca392fd 3278 /* Case value is greater than maximum for type. */
3279 if (tree_int_cst_compare (case_low, max_value) > 0
3280 && tree_int_cst_compare (case_high, max_value) > 0)
3281 {
c3ceba8e 3282 warning (0, "case label value exceeds maximum value for type");
2ca392fd 3283 return false;
3284 }
3285
3286 /* Saturate lower case label value to minimum. */
3287 if (tree_int_cst_compare (case_high, min_value) >= 0
3288 && tree_int_cst_compare (case_low, min_value) < 0)
3289 {
c3ceba8e 3290 warning (0, "lower value in case label range"
2ca392fd 3291 " less than minimum value for type");
3292 case_low = min_value;
3293 }
b27ac6b5 3294
2ca392fd 3295 /* Saturate upper case label value to maximum. */
3296 if (tree_int_cst_compare (case_low, max_value) <= 0
3297 && tree_int_cst_compare (case_high, max_value) > 0)
3298 {
c3ceba8e 3299 warning (0, "upper value in case label range"
2ca392fd 3300 " exceeds maximum value for type");
3301 case_high = max_value;
3302 }
3303
3304 if (*case_low_p != case_low)
3305 *case_low_p = convert (type, case_low);
3306 if (case_high_p && *case_high_p != case_high)
3307 *case_high_p = convert (type, case_high);
3308
3309 return true;
3310}
3311\f
b0fc3e72 3312/* Return an integer type with BITS bits of precision,
3313 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3314
3315tree
1cae46be 3316c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 3317{
46375237 3318 if (bits == TYPE_PRECISION (integer_type_node))
3319 return unsignedp ? unsigned_type_node : integer_type_node;
3320
bacde65a 3321 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 3322 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3323
bacde65a 3324 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 3325 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3326
bacde65a 3327 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 3328 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3329
bacde65a 3330 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 3331 return (unsignedp ? long_long_unsigned_type_node
3332 : long_long_integer_type_node);
3333
6388cfe2 3334 if (int128_integer_type_node
3335 && bits == TYPE_PRECISION (int128_integer_type_node))
3336 return (unsignedp ? int128_unsigned_type_node
3337 : int128_integer_type_node);
3338
f57fa2ea 3339 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3340 return (unsignedp ? widest_unsigned_literal_type_node
3341 : widest_integer_literal_type_node);
3342
bacde65a 3343 if (bits <= TYPE_PRECISION (intQI_type_node))
3344 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3345
3346 if (bits <= TYPE_PRECISION (intHI_type_node))
3347 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3348
3349 if (bits <= TYPE_PRECISION (intSI_type_node))
3350 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3351
3352 if (bits <= TYPE_PRECISION (intDI_type_node))
3353 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3354
b0fc3e72 3355 return 0;
3356}
3357
9421ebb9 3358/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3359 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3360 and saturating if SATP is nonzero, otherwise not saturating. */
3361
3362tree
3363c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3364 int unsignedp, int satp)
3365{
3366 enum machine_mode mode;
3367 if (ibit == 0)
3368 mode = unsignedp ? UQQmode : QQmode;
3369 else
3370 mode = unsignedp ? UHAmode : HAmode;
3371
3372 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3373 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3374 break;
3375
3376 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3377 {
3378 sorry ("GCC cannot support operators with integer types and "
3379 "fixed-point types that have too many integral and "
3380 "fractional bits together");
3381 return 0;
3382 }
3383
3384 return c_common_type_for_mode (mode, satp);
3385}
3386
5b247e9f 3387/* Used for communication between c_common_type_for_mode and
3388 c_register_builtin_type. */
3389static GTY(()) tree registered_builtin_types;
3390
b0fc3e72 3391/* Return a data type that has machine mode MODE.
3392 If the mode is an integer,
9421ebb9 3393 then UNSIGNEDP selects between signed and unsigned types.
3394 If the mode is a fixed-point mode,
3395 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 3396
3397tree
1cae46be 3398c_common_type_for_mode (enum machine_mode mode, int unsignedp)
b0fc3e72 3399{
5b247e9f 3400 tree t;
3401
46375237 3402 if (mode == TYPE_MODE (integer_type_node))
3403 return unsignedp ? unsigned_type_node : integer_type_node;
3404
b0fc3e72 3405 if (mode == TYPE_MODE (signed_char_type_node))
3406 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3407
3408 if (mode == TYPE_MODE (short_integer_type_node))
3409 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3410
b0fc3e72 3411 if (mode == TYPE_MODE (long_integer_type_node))
3412 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3413
3414 if (mode == TYPE_MODE (long_long_integer_type_node))
3415 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3416
6388cfe2 3417 if (int128_integer_type_node
3418 && mode == TYPE_MODE (int128_integer_type_node))
3419 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3420
f57fa2ea 3421 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3422 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3423 : widest_integer_literal_type_node;
f57fa2ea 3424
88ae7f04 3425 if (mode == QImode)
bacde65a 3426 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3427
88ae7f04 3428 if (mode == HImode)
bacde65a 3429 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3430
88ae7f04 3431 if (mode == SImode)
bacde65a 3432 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3433
88ae7f04 3434 if (mode == DImode)
bacde65a 3435 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3436
cc1cc1c7 3437#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3438 if (mode == TYPE_MODE (intTI_type_node))
3439 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3440#endif
6274009c 3441
b0fc3e72 3442 if (mode == TYPE_MODE (float_type_node))
3443 return float_type_node;
3444
3445 if (mode == TYPE_MODE (double_type_node))
3446 return double_type_node;
3447
3448 if (mode == TYPE_MODE (long_double_type_node))
3449 return long_double_type_node;
3450
545c2bde 3451 if (mode == TYPE_MODE (void_type_node))
3452 return void_type_node;
b27ac6b5 3453
b0fc3e72 3454 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3455 return (unsignedp
3456 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3457 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3458
3459 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3460 return (unsignedp
3461 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3462 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3463
0dfc45b5 3464 if (COMPLEX_MODE_P (mode))
3465 {
3466 enum machine_mode inner_mode;
3467 tree inner_type;
3468
3469 if (mode == TYPE_MODE (complex_float_type_node))
3470 return complex_float_type_node;
3471 if (mode == TYPE_MODE (complex_double_type_node))
3472 return complex_double_type_node;
3473 if (mode == TYPE_MODE (complex_long_double_type_node))
3474 return complex_long_double_type_node;
3475
3476 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3477 return complex_integer_type_node;
3478
3479 inner_mode = GET_MODE_INNER (mode);
3480 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3481 if (inner_type != NULL_TREE)
3482 return build_complex_type (inner_type);
3483 }
3484 else if (VECTOR_MODE_P (mode))
4917c376 3485 {
3486 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3487 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3488 if (inner_type != NULL_TREE)
3489 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 3490 }
e2ea7e3a 3491
c4503c0a 3492 if (mode == TYPE_MODE (dfloat32_type_node))
3493 return dfloat32_type_node;
3494 if (mode == TYPE_MODE (dfloat64_type_node))
3495 return dfloat64_type_node;
3496 if (mode == TYPE_MODE (dfloat128_type_node))
3497 return dfloat128_type_node;
3498
9421ebb9 3499 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3500 {
3501 if (mode == TYPE_MODE (short_fract_type_node))
3502 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3503 if (mode == TYPE_MODE (fract_type_node))
3504 return unsignedp ? sat_fract_type_node : fract_type_node;
3505 if (mode == TYPE_MODE (long_fract_type_node))
3506 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3507 if (mode == TYPE_MODE (long_long_fract_type_node))
3508 return unsignedp ? sat_long_long_fract_type_node
3509 : long_long_fract_type_node;
3510
3511 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3512 return unsignedp ? sat_unsigned_short_fract_type_node
3513 : unsigned_short_fract_type_node;
3514 if (mode == TYPE_MODE (unsigned_fract_type_node))
3515 return unsignedp ? sat_unsigned_fract_type_node
3516 : unsigned_fract_type_node;
3517 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3518 return unsignedp ? sat_unsigned_long_fract_type_node
3519 : unsigned_long_fract_type_node;
3520 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3521 return unsignedp ? sat_unsigned_long_long_fract_type_node
3522 : unsigned_long_long_fract_type_node;
3523
3524 if (mode == TYPE_MODE (short_accum_type_node))
3525 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3526 if (mode == TYPE_MODE (accum_type_node))
3527 return unsignedp ? sat_accum_type_node : accum_type_node;
3528 if (mode == TYPE_MODE (long_accum_type_node))
3529 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3530 if (mode == TYPE_MODE (long_long_accum_type_node))
3531 return unsignedp ? sat_long_long_accum_type_node
3532 : long_long_accum_type_node;
3533
3534 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3535 return unsignedp ? sat_unsigned_short_accum_type_node
3536 : unsigned_short_accum_type_node;
3537 if (mode == TYPE_MODE (unsigned_accum_type_node))
3538 return unsignedp ? sat_unsigned_accum_type_node
3539 : unsigned_accum_type_node;
3540 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3541 return unsignedp ? sat_unsigned_long_accum_type_node
3542 : unsigned_long_accum_type_node;
3543 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3544 return unsignedp ? sat_unsigned_long_long_accum_type_node
3545 : unsigned_long_long_accum_type_node;
3546
3547 if (mode == QQmode)
3548 return unsignedp ? sat_qq_type_node : qq_type_node;
3549 if (mode == HQmode)
3550 return unsignedp ? sat_hq_type_node : hq_type_node;
3551 if (mode == SQmode)
3552 return unsignedp ? sat_sq_type_node : sq_type_node;
3553 if (mode == DQmode)
3554 return unsignedp ? sat_dq_type_node : dq_type_node;
3555 if (mode == TQmode)
3556 return unsignedp ? sat_tq_type_node : tq_type_node;
3557
3558 if (mode == UQQmode)
3559 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3560 if (mode == UHQmode)
3561 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3562 if (mode == USQmode)
3563 return unsignedp ? sat_usq_type_node : usq_type_node;
3564 if (mode == UDQmode)
3565 return unsignedp ? sat_udq_type_node : udq_type_node;
3566 if (mode == UTQmode)
3567 return unsignedp ? sat_utq_type_node : utq_type_node;
3568
3569 if (mode == HAmode)
3570 return unsignedp ? sat_ha_type_node : ha_type_node;
3571 if (mode == SAmode)
3572 return unsignedp ? sat_sa_type_node : sa_type_node;
3573 if (mode == DAmode)
3574 return unsignedp ? sat_da_type_node : da_type_node;
3575 if (mode == TAmode)
3576 return unsignedp ? sat_ta_type_node : ta_type_node;
3577
3578 if (mode == UHAmode)
3579 return unsignedp ? sat_uha_type_node : uha_type_node;
3580 if (mode == USAmode)
3581 return unsignedp ? sat_usa_type_node : usa_type_node;
3582 if (mode == UDAmode)
3583 return unsignedp ? sat_uda_type_node : uda_type_node;
3584 if (mode == UTAmode)
3585 return unsignedp ? sat_uta_type_node : uta_type_node;
3586 }
3587
5b247e9f 3588 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 3589 if (TYPE_MODE (TREE_VALUE (t)) == mode
3590 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 3591 return TREE_VALUE (t);
3592
b0fc3e72 3593 return 0;
3594}
20d39783 3595
11773141 3596tree
3597c_common_unsigned_type (tree type)
3598{
3599 return c_common_signed_or_unsigned_type (1, type);
3600}
3601
20d39783 3602/* Return a signed type the same as TYPE in other respects. */
3603
3604tree
1cae46be 3605c_common_signed_type (tree type)
20d39783 3606{
4070745f 3607 return c_common_signed_or_unsigned_type (0, type);
20d39783 3608}
3609
3610/* Return a type the same as TYPE except unsigned or
3611 signed according to UNSIGNEDP. */
3612
3613tree
1cae46be 3614c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 3615{
7a91101f 3616 tree type1;
20d39783 3617
7a91101f 3618 /* This block of code emulates the behavior of the old
3619 c_common_unsigned_type. In particular, it returns
3620 long_unsigned_type_node if passed a long, even when a int would
3621 have the same size. This is necessary for warnings to work
3622 correctly in archs where sizeof(int) == sizeof(long) */
3623
3624 type1 = TYPE_MAIN_VARIANT (type);
3625 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3626 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3627 if (type1 == integer_type_node || type1 == unsigned_type_node)
3628 return unsignedp ? unsigned_type_node : integer_type_node;
3629 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3630 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3631 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3632 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3633 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3634 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
6388cfe2 3635 if (int128_integer_type_node
3636 && (type1 == int128_integer_type_node
3637 || type1 == int128_unsigned_type_node))
3638 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
7a91101f 3639 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3640 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3641#if HOST_BITS_PER_WIDE_INT >= 64
3642 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3643 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3644#endif
3645 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3646 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3647 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3648 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3649 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3650 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3651 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3652 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3653
9f92e1a2 3654#define C_COMMON_FIXED_TYPES(NAME) \
3655 if (type1 == short_ ## NAME ## _type_node \
3656 || type1 == unsigned_short_ ## NAME ## _type_node) \
3657 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3658 : short_ ## NAME ## _type_node; \
3659 if (type1 == NAME ## _type_node \
3660 || type1 == unsigned_ ## NAME ## _type_node) \
3661 return unsignedp ? unsigned_ ## NAME ## _type_node \
3662 : NAME ## _type_node; \
3663 if (type1 == long_ ## NAME ## _type_node \
3664 || type1 == unsigned_long_ ## NAME ## _type_node) \
3665 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3666 : long_ ## NAME ## _type_node; \
3667 if (type1 == long_long_ ## NAME ## _type_node \
3668 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3669 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3670 : long_long_ ## NAME ## _type_node;
3671
3672#define C_COMMON_FIXED_MODE_TYPES(NAME) \
3673 if (type1 == NAME ## _type_node \
3674 || type1 == u ## NAME ## _type_node) \
3675 return unsignedp ? u ## NAME ## _type_node \
3676 : NAME ## _type_node;
3677
3678#define C_COMMON_FIXED_TYPES_SAT(NAME) \
3679 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3680 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3681 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3682 : sat_ ## short_ ## NAME ## _type_node; \
3683 if (type1 == sat_ ## NAME ## _type_node \
3684 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3685 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3686 : sat_ ## NAME ## _type_node; \
3687 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3688 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3689 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3690 : sat_ ## long_ ## NAME ## _type_node; \
3691 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3692 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3693 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3694 : sat_ ## long_long_ ## NAME ## _type_node;
3695
3696#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3697 if (type1 == sat_ ## NAME ## _type_node \
3698 || type1 == sat_ ## u ## NAME ## _type_node) \
3699 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3700 : sat_ ## NAME ## _type_node;
3701
3702 C_COMMON_FIXED_TYPES (fract);
3703 C_COMMON_FIXED_TYPES_SAT (fract);
3704 C_COMMON_FIXED_TYPES (accum);
3705 C_COMMON_FIXED_TYPES_SAT (accum);
3706
3707 C_COMMON_FIXED_MODE_TYPES (qq);
3708 C_COMMON_FIXED_MODE_TYPES (hq);
3709 C_COMMON_FIXED_MODE_TYPES (sq);
3710 C_COMMON_FIXED_MODE_TYPES (dq);
3711 C_COMMON_FIXED_MODE_TYPES (tq);
3712 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3713 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3714 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3715 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3716 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3717 C_COMMON_FIXED_MODE_TYPES (ha);
3718 C_COMMON_FIXED_MODE_TYPES (sa);
3719 C_COMMON_FIXED_MODE_TYPES (da);
3720 C_COMMON_FIXED_MODE_TYPES (ta);
3721 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3722 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3723 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3724 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 3725
4f7f7efd 3726 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3727 the precision; they have precision set to match their range, but
3728 may use a wider mode to match an ABI. If we change modes, we may
3729 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3730 the precision as well, so as to yield correct results for
3731 bit-field types. C++ does not have these separate bit-field
3732 types, and producing a signed or unsigned variant of an
3733 ENUMERAL_TYPE may cause other problems as well. */
3734
ac265864 3735 if (!INTEGRAL_TYPE_P (type)
3736 || TYPE_UNSIGNED (type) == unsignedp)
3737 return type;
3738
4f7f7efd 3739#define TYPE_OK(node) \
3740 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 3741 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 3742 if (TYPE_OK (signed_char_type_node))
20d39783 3743 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 3744 if (TYPE_OK (integer_type_node))
20d39783 3745 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 3746 if (TYPE_OK (short_integer_type_node))
20d39783 3747 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 3748 if (TYPE_OK (long_integer_type_node))
20d39783 3749 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 3750 if (TYPE_OK (long_long_integer_type_node))
20d39783 3751 return (unsignedp ? long_long_unsigned_type_node
3752 : long_long_integer_type_node);
6388cfe2 3753 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3754 return (unsignedp ? int128_unsigned_type_node
3755 : int128_integer_type_node);
4f7f7efd 3756 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 3757 return (unsignedp ? widest_unsigned_literal_type_node
3758 : widest_integer_literal_type_node);
ef11801e 3759
3760#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 3761 if (TYPE_OK (intTI_type_node))
ef11801e 3762 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3763#endif
4f7f7efd 3764 if (TYPE_OK (intDI_type_node))
ef11801e 3765 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 3766 if (TYPE_OK (intSI_type_node))
ef11801e 3767 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 3768 if (TYPE_OK (intHI_type_node))
ef11801e 3769 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 3770 if (TYPE_OK (intQI_type_node))
ef11801e 3771 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 3772#undef TYPE_OK
ef11801e 3773
0c4abe5b 3774 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 3775}
b268e47e 3776
c0e47fd4 3777/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3778
3779tree
3780c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3781{
3782 /* Extended integer types of the same width as a standard type have
3783 lesser rank, so those of the same width as int promote to int or
3784 unsigned int and are valid for printf formats expecting int or
3785 unsigned int. To avoid such special cases, avoid creating
3786 extended integer types for bit-fields if a standard integer type
3787 is available. */
3788 if (width == TYPE_PRECISION (integer_type_node))
3789 return unsignedp ? unsigned_type_node : integer_type_node;
3790 if (width == TYPE_PRECISION (signed_char_type_node))
3791 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3792 if (width == TYPE_PRECISION (short_integer_type_node))
3793 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3794 if (width == TYPE_PRECISION (long_integer_type_node))
3795 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3796 if (width == TYPE_PRECISION (long_long_integer_type_node))
3797 return (unsignedp ? long_long_unsigned_type_node
3798 : long_long_integer_type_node);
6388cfe2 3799 if (int128_integer_type_node
3800 && width == TYPE_PRECISION (int128_integer_type_node))
3801 return (unsignedp ? int128_unsigned_type_node
3802 : int128_integer_type_node);
c0e47fd4 3803 return build_nonstandard_integer_type (width, unsignedp);
3804}
3805
b268e47e 3806/* The C version of the register_builtin_type langhook. */
3807
3808void
3809c_register_builtin_type (tree type, const char* name)
3810{
3811 tree decl;
3812
e60a6f7b 3813 decl = build_decl (UNKNOWN_LOCATION,
3814 TYPE_DECL, get_identifier (name), type);
b268e47e 3815 DECL_ARTIFICIAL (decl) = 1;
3816 if (!TYPE_NAME (type))
3817 TYPE_NAME (type) = decl;
3818 pushdecl (decl);
5b247e9f 3819
3820 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 3821}
a9b9d10c 3822\f
aff9e656 3823/* Print an error message for invalid operands to arith operation
8e70fb09 3824 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3825 LOCATION is the location of the message. */
b0fc3e72 3826
3827void
8e70fb09 3828binary_op_error (location_t location, enum tree_code code,
3829 tree type0, tree type1)
b0fc3e72 3830{
19cb6b50 3831 const char *opname;
f03946e4 3832
b0fc3e72 3833 switch (code)
3834 {
b0fc3e72 3835 case PLUS_EXPR:
3836 opname = "+"; break;
3837 case MINUS_EXPR:
3838 opname = "-"; break;
3839 case MULT_EXPR:
3840 opname = "*"; break;
3841 case MAX_EXPR:
3842 opname = "max"; break;
3843 case MIN_EXPR:
3844 opname = "min"; break;
3845 case EQ_EXPR:
3846 opname = "=="; break;
3847 case NE_EXPR:
3848 opname = "!="; break;
3849 case LE_EXPR:
3850 opname = "<="; break;
3851 case GE_EXPR:
3852 opname = ">="; break;
3853 case LT_EXPR:
3854 opname = "<"; break;
3855 case GT_EXPR:
3856 opname = ">"; break;
3857 case LSHIFT_EXPR:
3858 opname = "<<"; break;
3859 case RSHIFT_EXPR:
3860 opname = ">>"; break;
3861 case TRUNC_MOD_EXPR:
66618a1e 3862 case FLOOR_MOD_EXPR:
b0fc3e72 3863 opname = "%"; break;
3864 case TRUNC_DIV_EXPR:
66618a1e 3865 case FLOOR_DIV_EXPR:
b0fc3e72 3866 opname = "/"; break;
3867 case BIT_AND_EXPR:
3868 opname = "&"; break;
3869 case BIT_IOR_EXPR:
3870 opname = "|"; break;
3871 case TRUTH_ANDIF_EXPR:
3872 opname = "&&"; break;
3873 case TRUTH_ORIF_EXPR:
3874 opname = "||"; break;
3875 case BIT_XOR_EXPR:
3876 opname = "^"; break;
31f820d2 3877 default:
315ba355 3878 gcc_unreachable ();
b0fc3e72 3879 }
8e70fb09 3880 error_at (location,
3881 "invalid operands to binary %s (have %qT and %qT)", opname,
3882 type0, type1);
b0fc3e72 3883}
3884\f
03fe1dc2 3885/* Given an expression as a tree, return its original type. Do this
3886 by stripping any conversion that preserves the sign and precision. */
3887static tree
3888expr_original_type (tree expr)
3889{
3890 STRIP_SIGN_NOPS (expr);
3891 return TREE_TYPE (expr);
3892}
3893
b0fc3e72 3894/* Subroutine of build_binary_op, used for comparison operations.
3895 See if the operands have both been converted from subword integer types
3896 and, if so, perhaps change them both back to their original type.
5b511807 3897 This function is also responsible for converting the two operands
3898 to the proper common type for comparison.
b0fc3e72 3899
3900 The arguments of this function are all pointers to local variables
3901 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3902 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3903
3904 If this function returns nonzero, it means that the comparison has
3905 a constant value. What this function returns is an expression for
3906 that value. */
3907
3908tree
1cae46be 3909shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3910 enum tree_code *rescode_ptr)
b0fc3e72 3911{
19cb6b50 3912 tree type;
b0fc3e72 3913 tree op0 = *op0_ptr;
3914 tree op1 = *op1_ptr;
3915 int unsignedp0, unsignedp1;
3916 int real1, real2;
3917 tree primop0, primop1;
3918 enum tree_code code = *rescode_ptr;
03fe1dc2 3919 location_t loc = EXPR_LOC_OR_HERE (op0);
b0fc3e72 3920
3921 /* Throw away any conversions to wider types
3922 already present in the operands. */
3923
7f506bca 3924 primop0 = c_common_get_narrower (op0, &unsignedp0);
3925 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 3926
119d06b2 3927 /* If primopN is first sign-extended from primopN's precision to opN's
3928 precision, then zero-extended from opN's precision to
3929 *restype_ptr precision, shortenings might be invalid. */
3930 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3931 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3932 && !unsignedp0
3933 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3934 primop0 = op0;
3935 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3936 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3937 && !unsignedp1
3938 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3939 primop1 = op1;
3940
b0fc3e72 3941 /* Handle the case that OP0 does not *contain* a conversion
3942 but it *requires* conversion to FINAL_TYPE. */
3943
3944 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 3945 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 3946 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 3947 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 3948
3949 /* If one of the operands must be floated, we cannot optimize. */
3950 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3951 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3952
3953 /* If first arg is constant, swap the args (changing operation
2bd278cc 3954 so value is preserved), for canonicalization. Don't do this if
3955 the second arg is 0. */
b0fc3e72 3956
2bd278cc 3957 if (TREE_CONSTANT (primop0)
9421ebb9 3958 && !integer_zerop (primop1) && !real_zerop (primop1)
3959 && !fixed_zerop (primop1))
b0fc3e72 3960 {
19cb6b50 3961 tree tem = primop0;
3962 int temi = unsignedp0;
b0fc3e72 3963 primop0 = primop1;
3964 primop1 = tem;
3965 tem = op0;
3966 op0 = op1;
3967 op1 = tem;
3968 *op0_ptr = op0;
3969 *op1_ptr = op1;
3970 unsignedp0 = unsignedp1;
3971 unsignedp1 = temi;
3972 temi = real1;
3973 real1 = real2;
3974 real2 = temi;
3975
3976 switch (code)
3977 {
3978 case LT_EXPR:
3979 code = GT_EXPR;
3980 break;
3981 case GT_EXPR:
3982 code = LT_EXPR;
3983 break;
3984 case LE_EXPR:
3985 code = GE_EXPR;
3986 break;
3987 case GE_EXPR:
3988 code = LE_EXPR;
3989 break;
31f820d2 3990 default:
3991 break;
b0fc3e72 3992 }
3993 *rescode_ptr = code;
3994 }
3995
3996 /* If comparing an integer against a constant more bits wide,
3997 maybe we can deduce a value of 1 or 0 independent of the data.
3998 Or else truncate the constant now
3999 rather than extend the variable at run time.
4000
4001 This is only interesting if the constant is the wider arg.
4002 Also, it is not safe if the constant is unsigned and the
4003 variable arg is signed, since in this case the variable
4004 would be sign-extended and then regarded as unsigned.
4005 Our technique fails in this case because the lowest/highest
4006 possible unsigned results don't follow naturally from the
4007 lowest/highest possible values of the variable operand.
4008 For just EQ_EXPR and NE_EXPR there is another technique that
4009 could be used: see if the constant can be faithfully represented
4010 in the other operand's type, by truncating it and reextending it
4011 and see if that preserves the constant's value. */
4012
4013 if (!real1 && !real2
9421ebb9 4014 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 4015 && TREE_CODE (primop1) == INTEGER_CST
4016 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4017 {
4018 int min_gt, max_gt, min_lt, max_lt;
4019 tree maxval, minval;
4020 /* 1 if comparison is nominally unsigned. */
78a8ed03 4021 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 4022 tree val;
4023
4070745f 4024 type = c_common_signed_or_unsigned_type (unsignedp0,
4025 TREE_TYPE (primop0));
cda09c61 4026
b0fc3e72 4027 maxval = TYPE_MAX_VALUE (type);
4028 minval = TYPE_MIN_VALUE (type);
4029
4030 if (unsignedp && !unsignedp0)
4070745f 4031 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 4032
4033 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 4034 {
9a5e8086 4035 /* Convert primop1 to target type, but do not introduce
4036 additional overflow. We know primop1 is an int_cst. */
c8110c8f 4037 primop1 = force_fit_type_double (*restype_ptr,
d3237426 4038 tree_to_double_int (primop1),
4039 0, TREE_OVERFLOW (primop1));
18dbec6f 4040 }
b0fc3e72 4041 if (type != *restype_ptr)
4042 {
4043 minval = convert (*restype_ptr, minval);
4044 maxval = convert (*restype_ptr, maxval);
4045 }
4046
4047 if (unsignedp && unsignedp0)
4048 {
4049 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4050 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4051 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4052 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4053 }
4054 else
4055 {
4056 min_gt = INT_CST_LT (primop1, minval);
4057 max_gt = INT_CST_LT (primop1, maxval);
4058 min_lt = INT_CST_LT (minval, primop1);
4059 max_lt = INT_CST_LT (maxval, primop1);
4060 }
4061
4062 val = 0;
4063 /* This used to be a switch, but Genix compiler can't handle that. */
4064 if (code == NE_EXPR)
4065 {
4066 if (max_lt || min_gt)
3c2239cf 4067 val = truthvalue_true_node;
b0fc3e72 4068 }
4069 else if (code == EQ_EXPR)
4070 {
4071 if (max_lt || min_gt)
3c2239cf 4072 val = truthvalue_false_node;
b0fc3e72 4073 }
4074 else if (code == LT_EXPR)
4075 {
4076 if (max_lt)
3c2239cf 4077 val = truthvalue_true_node;
b0fc3e72 4078 if (!min_lt)
3c2239cf 4079 val = truthvalue_false_node;
b0fc3e72 4080 }
4081 else if (code == GT_EXPR)
4082 {
4083 if (min_gt)
3c2239cf 4084 val = truthvalue_true_node;
b0fc3e72 4085 if (!max_gt)
3c2239cf 4086 val = truthvalue_false_node;
b0fc3e72 4087 }
4088 else if (code == LE_EXPR)
4089 {
4090 if (!max_gt)
3c2239cf 4091 val = truthvalue_true_node;
b0fc3e72 4092 if (min_gt)
3c2239cf 4093 val = truthvalue_false_node;
b0fc3e72 4094 }
4095 else if (code == GE_EXPR)
4096 {
4097 if (!min_lt)
3c2239cf 4098 val = truthvalue_true_node;
b0fc3e72 4099 if (max_lt)
3c2239cf 4100 val = truthvalue_false_node;
b0fc3e72 4101 }
4102
4103 /* If primop0 was sign-extended and unsigned comparison specd,
4104 we did a signed comparison above using the signed type bounds.
4105 But the comparison we output must be unsigned.
4106
4107 Also, for inequalities, VAL is no good; but if the signed
4108 comparison had *any* fixed result, it follows that the
4109 unsigned comparison just tests the sign in reverse
4110 (positive values are LE, negative ones GE).
4111 So we can generate an unsigned comparison
4112 against an extreme value of the signed type. */
4113
4114 if (unsignedp && !unsignedp0)
4115 {
4116 if (val != 0)
4117 switch (code)
4118 {
4119 case LT_EXPR:
4120 case GE_EXPR:
4121 primop1 = TYPE_MIN_VALUE (type);
4122 val = 0;
4123 break;
4124
4125 case LE_EXPR:
4126 case GT_EXPR:
4127 primop1 = TYPE_MAX_VALUE (type);
4128 val = 0;
4129 break;
31f820d2 4130
4131 default:
4132 break;
b0fc3e72 4133 }
11773141 4134 type = c_common_unsigned_type (type);
b0fc3e72 4135 }
4136
82e6ef7c 4137 if (TREE_CODE (primop0) != INTEGER_CST
4138 && c_inhibit_evaluation_warnings == 0)
b0fc3e72 4139 {
3c2239cf 4140 if (val == truthvalue_false_node)
03fe1dc2 4141 warning_at (loc, OPT_Wtype_limits,
4142 "comparison is always false due to limited range of data type");
3c2239cf 4143 if (val == truthvalue_true_node)
03fe1dc2 4144 warning_at (loc, OPT_Wtype_limits,
4145 "comparison is always true due to limited range of data type");
b0fc3e72 4146 }
4147
4148 if (val != 0)
4149 {
4150 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4151 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4152 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 4153 return val;
4154 }
4155
4156 /* Value is not predetermined, but do the comparison
4157 in the type of the operand that is not constant.
4158 TYPE is already properly set. */
4159 }
c4503c0a 4160
4161 /* If either arg is decimal float and the other is float, find the
4162 proper common type to use for comparison. */
4163 else if (real1 && real2
4164 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4165 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4166 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4167
b0fc3e72 4168 else if (real1 && real2
2203bd5c 4169 && (TYPE_PRECISION (TREE_TYPE (primop0))
4170 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 4171 type = TREE_TYPE (primop0);
4172
4173 /* If args' natural types are both narrower than nominal type
4174 and both extend in the same manner, compare them
4175 in the type of the wider arg.
4176 Otherwise must actually extend both to the nominal
4177 common type lest different ways of extending
4178 alter the result.
4179 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4180
4181 else if (unsignedp0 == unsignedp1 && real1 == real2
4182 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4183 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4184 {
4185 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4070745f 4186 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 4187 || TYPE_UNSIGNED (*restype_ptr),
4070745f 4188 type);
b0fc3e72 4189 /* Make sure shorter operand is extended the right way
4190 to match the longer operand. */
4070745f 4191 primop0
4192 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4193 TREE_TYPE (primop0)),
4194 primop0);
4195 primop1
4196 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4197 TREE_TYPE (primop1)),
4198 primop1);
b0fc3e72 4199 }
4200 else
4201 {
4202 /* Here we must do the comparison on the nominal type
4203 using the args exactly as we received them. */
4204 type = *restype_ptr;
4205 primop0 = op0;
4206 primop1 = op1;
4207
4208 if (!real1 && !real2 && integer_zerop (primop1)
78a8ed03 4209 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 4210 {
4211 tree value = 0;
03fe1dc2 4212 /* All unsigned values are >= 0, so we warn. However,
4213 if OP0 is a constant that is >= 0, the signedness of
4214 the comparison isn't an issue, so suppress the
4215 warning. */
4216 bool warn =
4217 warn_type_limits && !in_system_header
82e6ef7c 4218 && c_inhibit_evaluation_warnings == 0
03fe1dc2 4219 && !(TREE_CODE (primop0) == INTEGER_CST
4220 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4221 primop0)))
4222 /* Do not warn for enumeration types. */
4223 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4224
b0fc3e72 4225 switch (code)
4226 {
4227 case GE_EXPR:
03fe1dc2 4228 if (warn)
4229 warning_at (loc, OPT_Wtype_limits,
4230 "comparison of unsigned expression >= 0 is always true");
3c2239cf 4231 value = truthvalue_true_node;
b0fc3e72 4232 break;
4233
4234 case LT_EXPR:
03fe1dc2 4235 if (warn)
4236 warning_at (loc, OPT_Wtype_limits,
4237 "comparison of unsigned expression < 0 is always false");
3c2239cf 4238 value = truthvalue_false_node;
31f820d2 4239 break;
4240
4241 default:
4242 break;
b0fc3e72 4243 }
4244
4245 if (value != 0)
4246 {
4247 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4248 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4249 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4250 primop0, value);
b0fc3e72 4251 return value;
4252 }
4253 }
4254 }
4255
4256 *op0_ptr = convert (type, primop0);
4257 *op1_ptr = convert (type, primop1);
4258
3c2239cf 4259 *restype_ptr = truthvalue_type_node;
b0fc3e72 4260
4261 return 0;
4262}
4263\f
1c26100f 4264/* Return a tree for the sum or difference (RESULTCODE says which)
4265 of pointer PTROP and integer INTOP. */
4266
4267tree
389dd41b 4268pointer_int_sum (location_t loc, enum tree_code resultcode,
4269 tree ptrop, tree intop)
1c26100f 4270{
add6ee5e 4271 tree size_exp, ret;
1c26100f 4272
1c26100f 4273 /* The result is a pointer of the same type that is being added. */
1c26100f 4274 tree result_type = TREE_TYPE (ptrop);
4275
4276 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4277 {
29438999 4278 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
8864917d 4279 "pointer of type %<void *%> used in arithmetic");
1c26100f 4280 size_exp = integer_one_node;
4281 }
4282 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4283 {
29438999 4284 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
8864917d 4285 "pointer to a function used in arithmetic");
1c26100f 4286 size_exp = integer_one_node;
4287 }
4288 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4289 {
29438999 4290 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
8864917d 4291 "pointer to member function used in arithmetic");
1c26100f 4292 size_exp = integer_one_node;
4293 }
1c26100f 4294 else
4295 size_exp = size_in_bytes (TREE_TYPE (result_type));
4296
add6ee5e 4297 /* We are manipulating pointer values, so we don't need to warn
4298 about relying on undefined signed overflow. We disable the
4299 warning here because we use integer types so fold won't know that
4300 they are really pointers. */
4301 fold_defer_overflow_warnings ();
4302
1c26100f 4303 /* If what we are about to multiply by the size of the elements
4304 contains a constant term, apply distributive law
4305 and multiply that constant term separately.
4306 This helps produce common subexpressions. */
1c26100f 4307 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 4308 && !TREE_CONSTANT (intop)
1c26100f 4309 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4310 && TREE_CONSTANT (size_exp)
4311 /* If the constant comes from pointer subtraction,
4312 skip this optimization--it would cause an error. */
4313 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4314 /* If the constant is unsigned, and smaller than the pointer size,
4315 then we must skip this optimization. This is because it could cause
4316 an overflow error if the constant is negative but INTOP is not. */
84166705 4317 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 4318 || (TYPE_PRECISION (TREE_TYPE (intop))
4319 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4320 {
4321 enum tree_code subcode = resultcode;
4322 tree int_type = TREE_TYPE (intop);
4323 if (TREE_CODE (intop) == MINUS_EXPR)
4324 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4325 /* Convert both subexpression types to the type of intop,
4326 because weird cases involving pointer arithmetic
4327 can result in a sum or difference with different type args. */
8e70fb09 4328 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4329 subcode, ptrop,
1c26100f 4330 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4331 intop = convert (int_type, TREE_OPERAND (intop, 0));
4332 }
4333
4334 /* Convert the integer argument to a type the same size as sizetype
4335 so the multiply won't overflow spuriously. */
1c26100f 4336 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 4337 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 4338 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 4339 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 4340
4341 /* Replace the integer argument with a suitable product by the object size.
c6feb9f1 4342 Do this multiplication as signed, then convert to the appropriate type
8032877c 4343 for the pointer operation and disregard an overflow that occurred only
c6feb9f1 4344 because of the sign-extension change in the latter conversion. */
4345 {
4346 tree t = build_binary_op (loc,
4347 MULT_EXPR, intop,
4348 convert (TREE_TYPE (intop), size_exp), 1);
4349 intop = convert (sizetype, t);
4350 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4351 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4352 TREE_INT_CST_HIGH (intop));
4353 }
0de36bdb 4354
499e523f 4355 /* Create the sum or difference. */
0de36bdb 4356 if (resultcode == MINUS_EXPR)
389dd41b 4357 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
1c26100f 4358
2cc66f2a 4359 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
add6ee5e 4360
4361 fold_undefer_and_ignore_overflow_warnings ();
4362
4363 return ret;
1c26100f 4364}
4365\f
f59e3889 4366/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4367 and if NON_CONST is known not to be permitted in an evaluated part
4368 of a constant expression. */
4369
4370tree
4371c_wrap_maybe_const (tree expr, bool non_const)
4372{
4373 bool nowarning = TREE_NO_WARNING (expr);
4374 location_t loc = EXPR_LOCATION (expr);
4375
4376 /* This should never be called for C++. */
4377 if (c_dialect_cxx ())
4378 gcc_unreachable ();
4379
4380 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4381 STRIP_TYPE_NOPS (expr);
4382 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4383 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4384 if (nowarning)
4385 TREE_NO_WARNING (expr) = 1;
4386 protected_set_expr_location (expr, loc);
4387
4388 return expr;
4389}
4390
a75b1c71 4391/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4392 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4393 around the SAVE_EXPR if needed so that c_fully_fold does not need
4394 to look inside SAVE_EXPRs. */
4395
4396tree
4397c_save_expr (tree expr)
4398{
4399 bool maybe_const = true;
4400 if (c_dialect_cxx ())
4401 return save_expr (expr);
4402 expr = c_fully_fold (expr, false, &maybe_const);
4403 expr = save_expr (expr);
4404 if (!maybe_const)
f59e3889 4405 expr = c_wrap_maybe_const (expr, true);
a75b1c71 4406 return expr;
4407}
4408
6b68e71a 4409/* Return whether EXPR is a declaration whose address can never be
4410 NULL. */
4411
4412bool
9f627b1a 4413decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 4414{
4415 return (DECL_P (expr)
4416 && (TREE_CODE (expr) == PARM_DECL
4417 || TREE_CODE (expr) == LABEL_DECL
4418 || !DECL_WEAK (expr)));
4419}
4420
b0fc3e72 4421/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 4422 or for an `if' or `while' statement or ?..: exp. It should already
4423 have been validated to be of suitable type; otherwise, a bad
4424 diagnostic may result.
b0fc3e72 4425
8e70fb09 4426 The EXPR is located at LOCATION.
4427
b0fc3e72 4428 This preparation consists of taking the ordinary
4429 representation of an expression expr and producing a valid tree
4430 boolean expression describing whether expr is nonzero. We could
3c2239cf 4431 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 4432 but we optimize comparisons, &&, ||, and !.
4433
3c2239cf 4434 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 4435
4436tree
8e70fb09 4437c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 4438{
b0fc3e72 4439 switch (TREE_CODE (expr))
4440 {
318a728f 4441 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 4442 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4443 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4444 case ORDERED_EXPR: case UNORDERED_EXPR:
ce04dcdc 4445 if (TREE_TYPE (expr) == truthvalue_type_node)
4446 return expr;
e60a6f7b 4447 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
ce04dcdc 4448 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
e60a6f7b 4449 goto ret;
ce04dcdc 4450
b0fc3e72 4451 case TRUTH_ANDIF_EXPR:
4452 case TRUTH_ORIF_EXPR:
4453 case TRUTH_AND_EXPR:
4454 case TRUTH_OR_EXPR:
31f6e93c 4455 case TRUTH_XOR_EXPR:
ce04dcdc 4456 if (TREE_TYPE (expr) == truthvalue_type_node)
4457 return expr;
e60a6f7b 4458 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
48e1416a 4459 c_common_truthvalue_conversion (location,
e60a6f7b 4460 TREE_OPERAND (expr, 0)),
4461 c_common_truthvalue_conversion (location,
4462 TREE_OPERAND (expr, 1)));
4463 goto ret;
3e851b85 4464
f6e28f72 4465 case TRUTH_NOT_EXPR:
ce04dcdc 4466 if (TREE_TYPE (expr) == truthvalue_type_node)
4467 return expr;
e60a6f7b 4468 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4469 c_common_truthvalue_conversion (location,
4470 TREE_OPERAND (expr, 0)));
4471 goto ret;
f6e28f72 4472
b0fc3e72 4473 case ERROR_MARK:
4474 return expr;
4475
4476 case INTEGER_CST:
eddad94a 4477 return integer_zerop (expr) ? truthvalue_false_node
4478 : truthvalue_true_node;
b0fc3e72 4479
4480 case REAL_CST:
5000e21c 4481 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4482 ? truthvalue_true_node
4483 : truthvalue_false_node;
b0fc3e72 4484
9421ebb9 4485 case FIXED_CST:
4486 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4487 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4488 ? truthvalue_true_node
4489 : truthvalue_false_node;
4490
ce04dcdc 4491 case FUNCTION_DECL:
b6889cb0 4492 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
ce04dcdc 4493 /* Fall through. */
4494
b0fc3e72 4495 case ADDR_EXPR:
ce871053 4496 {
ee7d4d6a 4497 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 4498 if (decl_with_nonnull_addr_p (inner))
ce871053 4499 {
6b68e71a 4500 /* Common Ada/Pascal programmer's mistake. */
8e70fb09 4501 warning_at (location,
4502 OPT_Waddress,
4503 "the address of %qD will always evaluate as %<true%>",
4504 inner);
ce871053 4505 return truthvalue_true_node;
4506 }
d473d901 4507 break;
ce871053 4508 }
b0fc3e72 4509
2203bd5c 4510 case COMPLEX_EXPR:
e60a6f7b 4511 expr = build_binary_op (EXPR_LOCATION (expr),
8e70fb09 4512 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 4513 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 4514 c_common_truthvalue_conversion (location,
4515 TREE_OPERAND (expr, 0)),
4516 c_common_truthvalue_conversion (location,
4517 TREE_OPERAND (expr, 1)),
2203bd5c 4518 0);
e60a6f7b 4519 goto ret;
2203bd5c 4520
b0fc3e72 4521 case NEGATE_EXPR:
4522 case ABS_EXPR:
4523 case FLOAT_EXPR:
c6418a4e 4524 case EXCESS_PRECISION_EXPR:
d10cfa8d 4525 /* These don't change whether an object is nonzero or zero. */
8e70fb09 4526 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 4527
4528 case LROTATE_EXPR:
4529 case RROTATE_EXPR:
d10cfa8d 4530 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 4531 we can't ignore them if their second arg has side-effects. */
4532 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
e60a6f7b 4533 {
4534 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4535 TREE_OPERAND (expr, 1),
48e1416a 4536 c_common_truthvalue_conversion
e60a6f7b 4537 (location, TREE_OPERAND (expr, 0)));
4538 goto ret;
4539 }
b0fc3e72 4540 else
8e70fb09 4541 return c_common_truthvalue_conversion (location,
4542 TREE_OPERAND (expr, 0));
73be5127 4543
b0fc3e72 4544 case COND_EXPR:
4545 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 4546 if (c_dialect_cxx ())
e60a6f7b 4547 {
d0389adc 4548 tree op1 = TREE_OPERAND (expr, 1);
4549 tree op2 = TREE_OPERAND (expr, 2);
4550 /* In C++ one of the arms might have void type if it is throw. */
4551 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4552 op1 = c_common_truthvalue_conversion (location, op1);
4553 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4554 op2 = c_common_truthvalue_conversion (location, op2);
389dd41b 4555 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
d0389adc 4556 TREE_OPERAND (expr, 0), op1, op2);
e60a6f7b 4557 goto ret;
4558 }
a75b1c71 4559 else
e60a6f7b 4560 {
4561 /* Folding will happen later for C. */
4562 expr = build3 (COND_EXPR, truthvalue_type_node,
4563 TREE_OPERAND (expr, 0),
4564 c_common_truthvalue_conversion (location,
4565 TREE_OPERAND (expr, 1)),
4566 c_common_truthvalue_conversion (location,
4567 TREE_OPERAND (expr, 2)));
4568 goto ret;
4569 }
b0fc3e72 4570
72dd6141 4571 CASE_CONVERT:
f8913d47 4572 {
4573 tree totype = TREE_TYPE (expr);
4574 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4575
4576 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4577 since that affects how `default_conversion' will behave. */
4578 if (TREE_CODE (totype) == REFERENCE_TYPE
4579 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4580 break;
4581 /* Don't strip a conversion from C++0x scoped enum, since they
4582 don't implicitly convert to other types. */
4583 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4584 && ENUM_IS_SCOPED (fromtype))
4585 break;
4586 /* If this isn't narrowing the argument, we can ignore it. */
4587 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4588 return c_common_truthvalue_conversion (location,
4589 TREE_OPERAND (expr, 0));
4590 }
b0fc3e72 4591 break;
4592
16837b18 4593 case MODIFY_EXPR:
60a0513e 4594 if (!TREE_NO_WARNING (expr)
4595 && warn_parentheses)
4596 {
4597 warning (OPT_Wparentheses,
4598 "suggest parentheses around assignment used as truth value");
4599 TREE_NO_WARNING (expr) = 1;
4600 }
16837b18 4601 break;
73be5127 4602
31f820d2 4603 default:
4604 break;
b0fc3e72 4605 }
4606
2ba726d2 4607 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 4608 {
93be21c0 4609 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
e60a6f7b 4610 expr = (build_binary_op
8e70fb09 4611 (EXPR_LOCATION (expr),
4612 (TREE_SIDE_EFFECTS (expr)
a0748b7d 4613 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 4614 c_common_truthvalue_conversion
4615 (location,
4616 build_unary_op (location, REALPART_EXPR, t, 0)),
4617 c_common_truthvalue_conversion
4618 (location,
4619 build_unary_op (location, IMAGPART_EXPR, t, 0)),
a0748b7d 4620 0));
e60a6f7b 4621 goto ret;
a0748b7d 4622 }
2ba726d2 4623
9421ebb9 4624 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4625 {
4626 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4627 FCONST0 (TYPE_MODE
4628 (TREE_TYPE (expr))));
43158006 4629 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
9421ebb9 4630 }
e60a6f7b 4631 else
4632 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
9421ebb9 4633
e60a6f7b 4634 ret:
4635 protected_set_expr_location (expr, location);
4636 return expr;
b0fc3e72 4637}
4638\f
3237155d 4639static void def_builtin_1 (enum built_in_function fncode,
4640 const char *name,
4641 enum built_in_class fnclass,
4642 tree fntype, tree libtype,
4643 bool both_p, bool fallback_p, bool nonansi_p,
4644 tree fnattrs, bool implicit_p);
0d4238dc 4645
a5b1863e 4646
4647/* Apply the TYPE_QUALS to the new DECL. */
4648
4649void
1cae46be 4650c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 4651{
adfb367f 4652 tree type = TREE_TYPE (decl);
b27ac6b5 4653
e4eabbe4 4654 if (type == error_mark_node)
4655 return;
adfb367f 4656
98a33d9f 4657 if ((type_quals & TYPE_QUAL_CONST)
4658 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4659 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4660 constructor can produce constant init, so rely on cp_finish_decl to
4661 clear TREE_READONLY if the variable has non-constant init. */
a5b1863e 4662 TREE_READONLY (decl) = 1;
4663 if (type_quals & TYPE_QUAL_VOLATILE)
4664 {
4665 TREE_SIDE_EFFECTS (decl) = 1;
4666 TREE_THIS_VOLATILE (decl) = 1;
4667 }
d91a20bc 4668 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 4669 {
adfb367f 4670 while (type && TREE_CODE (type) == ARRAY_TYPE)
4671 /* Allow 'restrict' on arrays of pointers.
4672 FIXME currently we just ignore it. */
4673 type = TREE_TYPE (type);
4674 if (!type
4675 || !POINTER_TYPE_P (type)
4676 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
b0b1af64 4677 error ("invalid use of %<restrict%>");
a5b1863e 4678 }
4679}
4680
4ee9c684 4681/* Hash function for the problem of multiple type definitions in
4682 different files. This must hash all types that will compare
4683 equal via comptypes to the same value. In practice it hashes
0bed3869 4684 on some of the simple stuff and leaves the details to comptypes. */
4ee9c684 4685
4686static hashval_t
4687c_type_hash (const void *p)
4688{
ecf2703d 4689 int n_elements;
4ee9c684 4690 int shift, size;
aae87fc3 4691 const_tree const t = (const_tree) p;
4ee9c684 4692 tree t2;
4693 switch (TREE_CODE (t))
4694 {
fbf0afd1 4695 /* For pointers, hash on pointee type plus some swizzling. */
2363ef00 4696 case POINTER_TYPE:
4697 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4698 /* Hash on number of elements and total size. */
4699 case ENUMERAL_TYPE:
4700 shift = 3;
4701 t2 = TYPE_VALUES (t);
4702 break;
4703 case RECORD_TYPE:
4704 shift = 0;
4705 t2 = TYPE_FIELDS (t);
4706 break;
4707 case QUAL_UNION_TYPE:
4708 shift = 1;
4709 t2 = TYPE_FIELDS (t);
4710 break;
4711 case UNION_TYPE:
4712 shift = 2;
4713 t2 = TYPE_FIELDS (t);
4714 break;
4715 default:
231bd014 4716 gcc_unreachable ();
4ee9c684 4717 }
ecf2703d 4718 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4719 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4720 n_elements = list_length (t2);
78c2e180 4721 /* We might have a VLA here. */
4722 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4723 size = 0;
4724 else
4725 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
ecf2703d 4726 return ((size << 24) | (n_elements << shift));
4ee9c684 4727}
4728
1ecd4018 4729static GTY((param_is (union tree_node))) htab_t type_hash_table;
4730
b5ba9f3a 4731/* Return the typed-based alias set for T, which may be an expression
f7c44134 4732 or a type. Return -1 if we don't do anything special. */
b5ba9f3a 4733
32c2fdea 4734alias_set_type
1cae46be 4735c_common_get_alias_set (tree t)
b5ba9f3a 4736{
be4f2de7 4737 tree u;
4ee9c684 4738 PTR *slot;
1cae46be 4739
e58c17e7 4740 /* For VLAs, use the alias set of the element type rather than the
4741 default of alias set 0 for types compared structurally. */
4742 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4743 {
4744 if (TREE_CODE (t) == ARRAY_TYPE)
4745 return get_alias_set (TREE_TYPE (t));
4746 return -1;
4747 }
4748
be4f2de7 4749 /* Permit type-punning when accessing a union, provided the access
4750 is directly through the union. For example, this code does not
4751 permit taking the address of a union member and then storing
4752 through it. Even the type-punning allowed here is a GCC
4753 extension, albeit a common and useful one; the C standard says
4754 that such accesses have implementation-defined behavior. */
4755 for (u = t;
4756 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4757 u = TREE_OPERAND (u, 0))
4758 if (TREE_CODE (u) == COMPONENT_REF
4759 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4760 return 0;
1e2513d9 4761
9fcc3e54 4762 /* That's all the expressions we handle specially. */
84166705 4763 if (!TYPE_P (t))
9fcc3e54 4764 return -1;
4765
d716ce75 4766 /* The C standard guarantees that any object may be accessed via an
9fcc3e54 4767 lvalue that has character type. */
4768 if (t == char_type_node
4769 || t == signed_char_type_node
4770 || t == unsigned_char_type_node)
f7c44134 4771 return 0;
a5b1863e 4772
1607663f 4773 /* The C standard specifically allows aliasing between signed and
4774 unsigned variants of the same type. We treat the signed
4775 variant as canonical. */
78a8ed03 4776 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 4777 {
4070745f 4778 tree t1 = c_common_signed_type (t);
1607663f 4779
a8868e19 4780 /* t1 == t can happen for boolean nodes which are always unsigned. */
4781 if (t1 != t)
4782 return get_alias_set (t1);
4783 }
1e2513d9 4784
4ee9c684 4785 /* Handle the case of multiple type nodes referring to "the same" type,
4786 which occurs with IMA. These share an alias set. FIXME: Currently only
4787 C90 is handled. (In C99 type compatibility is not transitive, which
4788 complicates things mightily. The alias set splay trees can theoretically
4789 represent this, but insertion is tricky when you consider all the
4790 different orders things might arrive in.) */
4791
4792 if (c_language != clk_c || flag_isoc99)
4793 return -1;
4794
0bed3869 4795 /* Save time if there's only one input file. */
e08bd2f4 4796 if (num_in_fnames == 1)
4ee9c684 4797 return -1;
4798
4799 /* Pointers need special handling if they point to any type that
4800 needs special handling (below). */
4801 if (TREE_CODE (t) == POINTER_TYPE)
4802 {
4803 tree t2;
4804 /* Find bottom type under any nested POINTERs. */
b27ac6b5 4805 for (t2 = TREE_TYPE (t);
af592f67 4806 TREE_CODE (t2) == POINTER_TYPE;
4807 t2 = TREE_TYPE (t2))
4808 ;
b27ac6b5 4809 if (TREE_CODE (t2) != RECORD_TYPE
af592f67 4810 && TREE_CODE (t2) != ENUMERAL_TYPE
4811 && TREE_CODE (t2) != QUAL_UNION_TYPE
4812 && TREE_CODE (t2) != UNION_TYPE)
4813 return -1;
4ee9c684 4814 if (TYPE_SIZE (t2) == 0)
af592f67 4815 return -1;
4ee9c684 4816 }
4817 /* These are the only cases that need special handling. */
b27ac6b5 4818 if (TREE_CODE (t) != RECORD_TYPE
4ee9c684 4819 && TREE_CODE (t) != ENUMERAL_TYPE
4820 && TREE_CODE (t) != QUAL_UNION_TYPE
4821 && TREE_CODE (t) != UNION_TYPE
4822 && TREE_CODE (t) != POINTER_TYPE)
4823 return -1;
4824 /* Undefined? */
4825 if (TYPE_SIZE (t) == 0)
4826 return -1;
4827
b27ac6b5 4828 /* Look up t in hash table. Only one of the compatible types within each
4ee9c684 4829 alias set is recorded in the table. */
4830 if (!type_hash_table)
1ecd4018 4831 type_hash_table = htab_create_ggc (1021, c_type_hash,
4ee9c684 4832 (htab_eq) lang_hooks.types_compatible_p,
4833 NULL);
4834 slot = htab_find_slot (type_hash_table, t, INSERT);
4835 if (*slot != NULL)
ad16cb2c 4836 {
4837 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4838 return TYPE_ALIAS_SET ((tree)*slot);
4839 }
4ee9c684 4840 else
4841 /* Our caller will assign and record (in t) a new alias set; all we need
4842 to do is remember t in the hash table. */
4843 *slot = t;
4844
f7c44134 4845 return -1;
b5ba9f3a 4846}
902b4e01 4847\f
e60a6f7b 4848/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4849 the second parameter indicates which OPERATOR is being applied.
4850 The COMPLAIN flag controls whether we should diagnose possibly
4851 ill-formed constructs or not. LOC is the location of the SIZEOF or
4852 TYPEOF operator. */
908c697e 4853
902b4e01 4854tree
e60a6f7b 4855c_sizeof_or_alignof_type (location_t loc,
4856 tree type, bool is_sizeof, int complain)
902b4e01 4857{
d4c4d95c 4858 const char *op_name;
4859 tree value = NULL;
4860 enum tree_code type_code = TREE_CODE (type);
1cae46be 4861
908c697e 4862 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 4863
d4c4d95c 4864 if (type_code == FUNCTION_TYPE)
902b4e01 4865 {
908c697e 4866 if (is_sizeof)
d4c4d95c 4867 {
d3a4d008 4868 if (complain && (pedantic || warn_pointer_arith))
29438999 4869 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
8864917d 4870 "invalid application of %<sizeof%> to a function type");
ebd21de4 4871 else if (!complain)
4872 return error_mark_node;
d4c4d95c 4873 value = size_one_node;
4874 }
4875 else
83e25171 4876 {
4877 if (complain)
4878 {
4879 if (c_dialect_cxx ())
29438999 4880 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
83e25171 4881 "%<alignof%> applied to a function type");
4882 else
29438999 4883 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
83e25171 4884 "%<_Alignof%> applied to a function type");
4885 }
4886 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4887 }
d4c4d95c 4888 }
4889 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4890 {
1cae46be 4891 if (type_code == VOID_TYPE
d3a4d008 4892 && complain && (pedantic || warn_pointer_arith))
29438999 4893 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
8864917d 4894 "invalid application of %qs to a void type", op_name);
ebd21de4 4895 else if (!complain)
4896 return error_mark_node;
d4c4d95c 4897 value = size_one_node;
902b4e01 4898 }
3df19e1b 4899 else if (!COMPLETE_TYPE_P (type)
4900 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
902b4e01 4901 {
d3a4d008 4902 if (complain)
3df19e1b 4903 error_at (loc, "invalid application of %qs to incomplete type %qT",
e60a6f7b 4904 op_name, type);
9c719c74 4905 return error_mark_node;
902b4e01 4906 }
3df19e1b 4907 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4908 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4909 {
4910 if (complain)
4911 error_at (loc, "invalid application of %qs to array type %qT of "
4912 "incomplete element type", op_name, type);
4913 return error_mark_node;
4914 }
902b4e01 4915 else
d4c4d95c 4916 {
908c697e 4917 if (is_sizeof)
d4c4d95c 4918 /* Convert in case a char is more than one unit. */
389dd41b 4919 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4920 size_int (TYPE_PRECISION (char_type_node)
4921 / BITS_PER_UNIT));
d4c4d95c 4922 else
d37625c0 4923 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 4924 }
902b4e01 4925
5a1fe2db 4926 /* VALUE will have the middle-end integer type sizetype.
4927 However, we should really return a value of type `size_t',
4928 which is just a typedef for an ordinary integer type. */
389dd41b 4929 value = fold_convert_loc (loc, size_type_node, value);
1cae46be 4930
d4c4d95c 4931 return value;
902b4e01 4932}
4933
4934/* Implement the __alignof keyword: Return the minimum required
097b5c8b 4935 alignment of EXPR, measured in bytes. For VAR_DECLs,
4936 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
e60a6f7b 4937 from an "aligned" __attribute__ specification). LOC is the
4938 location of the ALIGNOF operator. */
72040e7e 4939
902b4e01 4940tree
e60a6f7b 4941c_alignof_expr (location_t loc, tree expr)
902b4e01 4942{
4943 tree t;
4944
097b5c8b 4945 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 4946 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 4947
902b4e01 4948 else if (TREE_CODE (expr) == COMPONENT_REF
4949 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4950 {
e60a6f7b 4951 error_at (loc, "%<__alignof%> applied to a bit-field");
902b4e01 4952 t = size_one_node;
4953 }
4954 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 4955 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 4956 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 4957
902b4e01 4958 else if (TREE_CODE (expr) == INDIRECT_REF)
4959 {
4960 tree t = TREE_OPERAND (expr, 0);
4961 tree best = t;
4962 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1cae46be 4963
72dd6141 4964 while (CONVERT_EXPR_P (t)
7cc7e163 4965 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 4966 {
4967 int thisalign;
4968
4969 t = TREE_OPERAND (t, 0);
4970 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4971 if (thisalign > bestalign)
4972 best = t, bestalign = thisalign;
4973 }
e60a6f7b 4974 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
902b4e01 4975 }
4976 else
e60a6f7b 4977 return c_alignof (loc, TREE_TYPE (expr));
902b4e01 4978
389dd41b 4979 return fold_convert_loc (loc, size_type_node, t);
902b4e01 4980}
4981\f
8fe4a266 4982/* Handle C and C++ default attributes. */
4983
4984enum built_in_attribute
4985{
4986#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4987#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
c8010b80 4988#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
8fe4a266 4989#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4990#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
8fe4a266 4991#include "builtin-attrs.def"
4992#undef DEF_ATTR_NULL_TREE
4993#undef DEF_ATTR_INT
c8010b80 4994#undef DEF_ATTR_STRING
8fe4a266 4995#undef DEF_ATTR_IDENT
4996#undef DEF_ATTR_TREE_LIST
8fe4a266 4997 ATTR_LAST
4998};
4999
5000static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5001
1cae46be 5002static void c_init_attributes (void);
8fe4a266 5003
27213ba3 5004enum c_builtin_type
72040e7e 5005{
d2d4bdde 5006#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5007#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5008#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5009#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5010#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5011#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
0a39fd54 5012#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
27213ba3 5013#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5014#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
d2d4bdde 5015#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5016#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5017#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
03901330 5018#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 5019#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5020#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5021 NAME,
d2d4bdde 5022#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5023#include "builtin-types.def"
5024#undef DEF_PRIMITIVE_TYPE
5025#undef DEF_FUNCTION_TYPE_0
5026#undef DEF_FUNCTION_TYPE_1
5027#undef DEF_FUNCTION_TYPE_2
5028#undef DEF_FUNCTION_TYPE_3
5029#undef DEF_FUNCTION_TYPE_4
0a39fd54 5030#undef DEF_FUNCTION_TYPE_5
5031#undef DEF_FUNCTION_TYPE_6
27213ba3 5032#undef DEF_FUNCTION_TYPE_7
d2d4bdde 5033#undef DEF_FUNCTION_TYPE_VAR_0
5034#undef DEF_FUNCTION_TYPE_VAR_1
5035#undef DEF_FUNCTION_TYPE_VAR_2
03901330 5036#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 5037#undef DEF_FUNCTION_TYPE_VAR_4
5038#undef DEF_FUNCTION_TYPE_VAR_5
d2d4bdde 5039#undef DEF_POINTER_TYPE
27213ba3 5040 BT_LAST
5041};
5042
5043typedef enum c_builtin_type builtin_type;
d2d4bdde 5044
27213ba3 5045/* A temporary array for c_common_nodes_and_builtins. Used in
5046 communication with def_fn_type. */
5047static tree builtin_types[(int) BT_LAST + 1];
d2d4bdde 5048
27213ba3 5049/* A helper function for c_common_nodes_and_builtins. Build function type
5050 for DEF with return type RET and N arguments. If VAR is true, then the
5051 function should be variadic after those N arguments.
5052
5053 Takes special care not to ICE if any of the types involved are
5054 error_mark_node, which indicates that said type is not in fact available
5055 (see builtin_type_for_size). In which case the function type as a whole
5056 should be error_mark_node. */
5057
5058static void
5059def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5060{
3a939d12 5061 tree t;
5062 tree *args = XALLOCAVEC (tree, n);
27213ba3 5063 va_list list;
5064 int i;
5065
5066 va_start (list, n);
5067 for (i = 0; i < n; ++i)
5068 {
7d339f93 5069 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 5070 t = builtin_types[a];
5071 if (t == error_mark_node)
5072 goto egress;
3a939d12 5073 args[i] = t;
27213ba3 5074 }
27213ba3 5075
27213ba3 5076 t = builtin_types[ret];
5077 if (t == error_mark_node)
5078 goto egress;
3a939d12 5079 if (var)
5080 t = build_varargs_function_type_array (t, n, args);
5081 else
5082 t = build_function_type_array (t, n, args);
27213ba3 5083
5084 egress:
5085 builtin_types[def] = t;
451c8e2f 5086 va_end (list);
27213ba3 5087}
5088
dce22712 5089/* Build builtin functions common to both C and C++ language
5090 frontends. */
5091
5092static void
5093c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5094{
5095#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5096 builtin_types[ENUM] = VALUE;
5097#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5098 def_fn_type (ENUM, RETURN, 0, 0);
5099#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5100 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5101#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5102 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5103#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5104 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5105#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5106 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5107#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5108 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5109#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5110 ARG6) \
5111 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5112#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5113 ARG6, ARG7) \
5114 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5115#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5116 def_fn_type (ENUM, RETURN, 1, 0);
5117#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5118 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5119#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5120 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5121#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5122 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5123#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5124 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5125#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5126 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5127#define DEF_POINTER_TYPE(ENUM, TYPE) \
5128 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5129
5130#include "builtin-types.def"
5131
5132#undef DEF_PRIMITIVE_TYPE
5133#undef DEF_FUNCTION_TYPE_1
5134#undef DEF_FUNCTION_TYPE_2
5135#undef DEF_FUNCTION_TYPE_3
5136#undef DEF_FUNCTION_TYPE_4
5137#undef DEF_FUNCTION_TYPE_5
5138#undef DEF_FUNCTION_TYPE_6
5139#undef DEF_FUNCTION_TYPE_VAR_0
5140#undef DEF_FUNCTION_TYPE_VAR_1
5141#undef DEF_FUNCTION_TYPE_VAR_2
5142#undef DEF_FUNCTION_TYPE_VAR_3
5143#undef DEF_FUNCTION_TYPE_VAR_4
5144#undef DEF_FUNCTION_TYPE_VAR_5
5145#undef DEF_POINTER_TYPE
5146 builtin_types[(int) BT_LAST] = NULL_TREE;
5147
5148 c_init_attributes ();
5149
5150#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5151 NONANSI_P, ATTRS, IMPLICIT, COND) \
5152 if (NAME && COND) \
5153 def_builtin_1 (ENUM, NAME, CLASS, \
5154 builtin_types[(int) TYPE], \
5155 builtin_types[(int) LIBTYPE], \
5156 BOTH_P, FALLBACK_P, NONANSI_P, \
5157 built_in_attributes[(int) ATTRS], IMPLICIT);
5158#include "builtins.def"
5159#undef DEF_BUILTIN
5160
87eb1c28 5161 targetm.init_builtins ();
5162
471eff36 5163 build_common_builtin_nodes ();
dce22712 5164
dce22712 5165 if (flag_mudflap)
5166 mudflap_init ();
5167}
5168
9e6687c8 5169/* Like get_identifier, but avoid warnings about null arguments when
5170 the argument may be NULL for targets where GCC lacks stdint.h type
5171 information. */
5172
5173static inline tree
5174c_get_ident (const char *id)
5175{
5176 return get_identifier (id);
5177}
5178
27213ba3 5179/* Build tree nodes and builtin functions common to both C and C++ language
5180 frontends. */
5181
5182void
5183c_common_nodes_and_builtins (void)
5184{
924bbf02 5185 int char16_type_size;
5186 int char32_type_size;
174fcc61 5187 int wchar_type_size;
5188 tree array_domain_type;
2d47cc32 5189 tree va_list_ref_type_node;
8a15c04a 5190 tree va_list_arg_type_node;
a66c9326 5191
c38a75b7 5192 build_common_tree_nodes (flag_signed_char, flag_short_double);
e593356b 5193
174fcc61 5194 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 5195 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 5196 record_builtin_type (RID_CHAR, "char", char_type_node);
5197
5198 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5199 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5200 but not C. Are the conditionals here needed? */
c0f19401 5201 if (c_dialect_cxx ())
d946ea19 5202 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 5203 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5204 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5205 record_builtin_type (RID_MAX, "long unsigned int",
5206 long_unsigned_type_node);
6388cfe2 5207 if (int128_integer_type_node != NULL_TREE)
5208 {
5209 record_builtin_type (RID_INT128, "__int128",
5210 int128_integer_type_node);
5211 record_builtin_type (RID_MAX, "__int128 unsigned",
5212 int128_unsigned_type_node);
5213 }
c0f19401 5214 if (c_dialect_cxx ())
174fcc61 5215 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5216 record_builtin_type (RID_MAX, "long long int",
5217 long_long_integer_type_node);
5218 record_builtin_type (RID_MAX, "long long unsigned int",
5219 long_long_unsigned_type_node);
c0f19401 5220 if (c_dialect_cxx ())
174fcc61 5221 record_builtin_type (RID_MAX, "long long unsigned",
5222 long_long_unsigned_type_node);
5223 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5224 record_builtin_type (RID_MAX, "short unsigned int",
5225 short_unsigned_type_node);
c0f19401 5226 if (c_dialect_cxx ())
174fcc61 5227 record_builtin_type (RID_MAX, "unsigned short",
5228 short_unsigned_type_node);
5229
5230 /* Define both `signed char' and `unsigned char'. */
5231 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5232 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5233
771d21fa 5234 /* These are types that c_common_type_for_size and
5235 c_common_type_for_mode use. */
e60a6f7b 5236 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5237 TYPE_DECL, NULL_TREE,
dc24ddbd 5238 intQI_type_node));
e60a6f7b 5239 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5240 TYPE_DECL, NULL_TREE,
dc24ddbd 5241 intHI_type_node));
e60a6f7b 5242 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5243 TYPE_DECL, NULL_TREE,
dc24ddbd 5244 intSI_type_node));
e60a6f7b 5245 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5246 TYPE_DECL, NULL_TREE,
dc24ddbd 5247 intDI_type_node));
174fcc61 5248#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 5249 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5250 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5251 TYPE_DECL,
f1515a39 5252 get_identifier ("__int128_t"),
5253 intTI_type_node));
174fcc61 5254#endif
e60a6f7b 5255 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5256 TYPE_DECL, NULL_TREE,
dc24ddbd 5257 unsigned_intQI_type_node));
e60a6f7b 5258 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5259 TYPE_DECL, NULL_TREE,
dc24ddbd 5260 unsigned_intHI_type_node));
e60a6f7b 5261 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5262 TYPE_DECL, NULL_TREE,
dc24ddbd 5263 unsigned_intSI_type_node));
e60a6f7b 5264 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5265 TYPE_DECL, NULL_TREE,
dc24ddbd 5266 unsigned_intDI_type_node));
174fcc61 5267#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 5268 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5269 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5270 TYPE_DECL,
f1515a39 5271 get_identifier ("__uint128_t"),
5272 unsigned_intTI_type_node));
174fcc61 5273#endif
5274
5275 /* Create the widest literal types. */
5276 widest_integer_literal_type_node
5277 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5278 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5279 TYPE_DECL, NULL_TREE,
dc24ddbd 5280 widest_integer_literal_type_node));
174fcc61 5281
5282 widest_unsigned_literal_type_node
5283 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5284 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5285 TYPE_DECL, NULL_TREE,
dc24ddbd 5286 widest_unsigned_literal_type_node));
174fcc61 5287
654ef926 5288 signed_size_type_node = c_common_signed_type (size_type_node);
174fcc61 5289
73673831 5290 pid_type_node =
5291 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5292
d946ea19 5293 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5294 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 5295 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5296
c4503c0a 5297 /* Only supported decimal floating point extension if the target
5298 actually supports underlying modes. */
48e1416a 5299 if (targetm.scalar_mode_supported_p (SDmode)
c4503c0a 5300 && targetm.scalar_mode_supported_p (DDmode)
5301 && targetm.scalar_mode_supported_p (TDmode))
5302 {
5303 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5304 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5305 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5306 }
5307
9421ebb9 5308 if (targetm.fixed_point_supported_p ())
5309 {
5310 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5311 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5312 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5313 record_builtin_type (RID_MAX, "long long _Fract",
5314 long_long_fract_type_node);
5315 record_builtin_type (RID_MAX, "unsigned short _Fract",
5316 unsigned_short_fract_type_node);
5317 record_builtin_type (RID_MAX, "unsigned _Fract",
5318 unsigned_fract_type_node);
5319 record_builtin_type (RID_MAX, "unsigned long _Fract",
5320 unsigned_long_fract_type_node);
5321 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5322 unsigned_long_long_fract_type_node);
5323 record_builtin_type (RID_MAX, "_Sat short _Fract",
5324 sat_short_fract_type_node);
5325 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5326 record_builtin_type (RID_MAX, "_Sat long _Fract",
5327 sat_long_fract_type_node);
5328 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5329 sat_long_long_fract_type_node);
5330 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5331 sat_unsigned_short_fract_type_node);
5332 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5333 sat_unsigned_fract_type_node);
5334 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5335 sat_unsigned_long_fract_type_node);
5336 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5337 sat_unsigned_long_long_fract_type_node);
5338 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5339 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5340 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5341 record_builtin_type (RID_MAX, "long long _Accum",
5342 long_long_accum_type_node);
5343 record_builtin_type (RID_MAX, "unsigned short _Accum",
5344 unsigned_short_accum_type_node);
5345 record_builtin_type (RID_MAX, "unsigned _Accum",
5346 unsigned_accum_type_node);
5347 record_builtin_type (RID_MAX, "unsigned long _Accum",
5348 unsigned_long_accum_type_node);
5349 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5350 unsigned_long_long_accum_type_node);
5351 record_builtin_type (RID_MAX, "_Sat short _Accum",
5352 sat_short_accum_type_node);
5353 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5354 record_builtin_type (RID_MAX, "_Sat long _Accum",
5355 sat_long_accum_type_node);
5356 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5357 sat_long_long_accum_type_node);
5358 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5359 sat_unsigned_short_accum_type_node);
5360 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5361 sat_unsigned_accum_type_node);
5362 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5363 sat_unsigned_long_accum_type_node);
5364 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5365 sat_unsigned_long_long_accum_type_node);
5366
5367 }
5368
e60a6f7b 5369 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5370 TYPE_DECL,
dc24ddbd 5371 get_identifier ("complex int"),
5372 complex_integer_type_node));
e60a6f7b 5373 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5374 TYPE_DECL,
dc24ddbd 5375 get_identifier ("complex float"),
5376 complex_float_type_node));
e60a6f7b 5377 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5378 TYPE_DECL,
dc24ddbd 5379 get_identifier ("complex double"),
5380 complex_double_type_node));
5381 lang_hooks.decls.pushdecl
e60a6f7b 5382 (build_decl (UNKNOWN_LOCATION,
5383 TYPE_DECL, get_identifier ("complex long double"),
20325f61 5384 complex_long_double_type_node));
174fcc61 5385
e256d445 5386 if (c_dialect_cxx ())
5387 /* For C++, make fileptr_type_node a distinct void * type until
5388 FILE type is defined. */
e086912e 5389 fileptr_type_node = build_variant_type_copy (ptr_type_node);
e256d445 5390
d946ea19 5391 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 5392
6753bca0 5393 /* Set the TYPE_NAME for any variants that were built before
5394 record_builtin_type gave names to the built-in types. */
5395 {
5396 tree void_name = TYPE_NAME (void_type_node);
5397 TYPE_NAME (void_type_node) = NULL_TREE;
5398 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5399 = void_name;
5400 TYPE_NAME (void_type_node) = void_name;
5401 }
5402
fbf0afd1 5403 /* This node must not be shared. */
7c446c95 5404 void_zero_node = make_node (INTEGER_CST);
d2d4bdde 5405 TREE_TYPE (void_zero_node) = void_type_node;
5406
174fcc61 5407 void_list_node = build_void_list_node ();
5408
5409 /* Make a type to be the domain of a few array types
5410 whose domains don't really matter.
5411 200 is small enough that it always fits in size_t
5412 and large enough that it can hold most function names for the
5413 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5414 array_domain_type = build_index_type (size_int (200));
5415
5416 /* Make a type for arrays of characters.
5417 With luck nothing will ever really depend on the length of this
5418 array type. */
5419 char_array_type_node
5420 = build_array_type (char_type_node, array_domain_type);
5421
5422 /* Likewise for arrays of ints. */
5423 int_array_type_node
5424 = build_array_type (integer_type_node, array_domain_type);
5425
d2d4bdde 5426 string_type_node = build_pointer_type (char_type_node);
5427 const_string_type_node
5428 = build_pointer_type (build_qualified_type
5429 (char_type_node, TYPE_QUAL_CONST));
5430
174fcc61 5431 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 5432 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 5433 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5434 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 5435 underlying_wchar_type_node = wchar_type_node;
c0f19401 5436 if (c_dialect_cxx ())
174fcc61 5437 {
78a8ed03 5438 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 5439 wchar_type_node = make_unsigned_type (wchar_type_size);
5440 else
5441 wchar_type_node = make_signed_type (wchar_type_size);
5442 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5443 }
174fcc61 5444
5445 /* This is for wide string constants. */
5446 wchar_array_type_node
5447 = build_array_type (wchar_type_node, array_domain_type);
5448
924bbf02 5449 /* Define 'char16_t'. */
5450 char16_type_node = get_identifier (CHAR16_TYPE);
5451 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5452 char16_type_size = TYPE_PRECISION (char16_type_node);
5453 if (c_dialect_cxx ())
5454 {
5455 char16_type_node = make_unsigned_type (char16_type_size);
5456
543efdbe 5457 if (cxx_dialect >= cxx0x)
924bbf02 5458 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5459 }
5460
5461 /* This is for UTF-16 string constants. */
5462 char16_array_type_node
5463 = build_array_type (char16_type_node, array_domain_type);
5464
5465 /* Define 'char32_t'. */
5466 char32_type_node = get_identifier (CHAR32_TYPE);
5467 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5468 char32_type_size = TYPE_PRECISION (char32_type_node);
5469 if (c_dialect_cxx ())
5470 {
5471 char32_type_node = make_unsigned_type (char32_type_size);
5472
543efdbe 5473 if (cxx_dialect >= cxx0x)
924bbf02 5474 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5475 }
5476
5477 /* This is for UTF-32 string constants. */
5478 char32_array_type_node
5479 = build_array_type (char32_type_node, array_domain_type);
5480
6bf5ed8d 5481 wint_type_node =
5482 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5483
5484 intmax_type_node =
5485 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5486 uintmax_type_node =
5487 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5488
f3449a3c 5489 if (SIG_ATOMIC_TYPE)
5490 sig_atomic_type_node =
9e6687c8 5491 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 5492 if (INT8_TYPE)
5493 int8_type_node =
9e6687c8 5494 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 5495 if (INT16_TYPE)
5496 int16_type_node =
9e6687c8 5497 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 5498 if (INT32_TYPE)
5499 int32_type_node =
9e6687c8 5500 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 5501 if (INT64_TYPE)
5502 int64_type_node =
9e6687c8 5503 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 5504 if (UINT8_TYPE)
5505 uint8_type_node =
9e6687c8 5506 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 5507 if (UINT16_TYPE)
74bdbe96 5508 c_uint16_type_node =
9e6687c8 5509 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 5510 if (UINT32_TYPE)
5511 c_uint32_type_node =
9e6687c8 5512 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 5513 if (UINT64_TYPE)
5514 c_uint64_type_node =
9e6687c8 5515 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 5516 if (INT_LEAST8_TYPE)
5517 int_least8_type_node =
9e6687c8 5518 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 5519 if (INT_LEAST16_TYPE)
5520 int_least16_type_node =
9e6687c8 5521 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 5522 if (INT_LEAST32_TYPE)
5523 int_least32_type_node =
9e6687c8 5524 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 5525 if (INT_LEAST64_TYPE)
5526 int_least64_type_node =
9e6687c8 5527 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 5528 if (UINT_LEAST8_TYPE)
5529 uint_least8_type_node =
9e6687c8 5530 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 5531 if (UINT_LEAST16_TYPE)
5532 uint_least16_type_node =
9e6687c8 5533 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 5534 if (UINT_LEAST32_TYPE)
5535 uint_least32_type_node =
9e6687c8 5536 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 5537 if (UINT_LEAST64_TYPE)
5538 uint_least64_type_node =
9e6687c8 5539 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 5540 if (INT_FAST8_TYPE)
5541 int_fast8_type_node =
9e6687c8 5542 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 5543 if (INT_FAST16_TYPE)
5544 int_fast16_type_node =
9e6687c8 5545 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 5546 if (INT_FAST32_TYPE)
5547 int_fast32_type_node =
9e6687c8 5548 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 5549 if (INT_FAST64_TYPE)
5550 int_fast64_type_node =
9e6687c8 5551 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 5552 if (UINT_FAST8_TYPE)
5553 uint_fast8_type_node =
9e6687c8 5554 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 5555 if (UINT_FAST16_TYPE)
5556 uint_fast16_type_node =
9e6687c8 5557 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 5558 if (UINT_FAST32_TYPE)
5559 uint_fast32_type_node =
9e6687c8 5560 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 5561 if (UINT_FAST64_TYPE)
5562 uint_fast64_type_node =
9e6687c8 5563 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 5564 if (INTPTR_TYPE)
5565 intptr_type_node =
9e6687c8 5566 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 5567 if (UINTPTR_TYPE)
5568 uintptr_type_node =
9e6687c8 5569 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 5570
3a939d12 5571 default_function_type
5572 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6bf5ed8d 5573 ptrdiff_type_node
5574 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
11773141 5575 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 5576
dc24ddbd 5577 lang_hooks.decls.pushdecl
e60a6f7b 5578 (build_decl (UNKNOWN_LOCATION,
5579 TYPE_DECL, get_identifier ("__builtin_va_list"),
20325f61 5580 va_list_type_node));
202d6e5f 5581 if (targetm.enum_va_list_p)
acd6f472 5582 {
5583 int l;
5584 const char *pname;
5585 tree ptype;
5f57a8b1 5586
202d6e5f 5587 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
acd6f472 5588 {
5589 lang_hooks.decls.pushdecl
5590 (build_decl (UNKNOWN_LOCATION,
5591 TYPE_DECL, get_identifier (pname),
5592 ptype));
5593
5594 }
5595 }
8a15c04a 5596
8a15c04a 5597 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 5598 {
5599 va_list_arg_type_node = va_list_ref_type_node =
5600 build_pointer_type (TREE_TYPE (va_list_type_node));
5601 }
8a15c04a 5602 else
2d47cc32 5603 {
5604 va_list_arg_type_node = va_list_type_node;
5605 va_list_ref_type_node = build_reference_type (va_list_type_node);
5606 }
1cae46be 5607
dce22712 5608 if (!flag_preprocess_only)
5609 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 5610
5c62f199 5611 main_identifier_node = get_identifier ("main");
ae84079f 5612
5613 /* Create the built-in __null node. It is important that this is
5614 not shared. */
5615 null_node = make_node (INTEGER_CST);
5616 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
27213ba3 5617
5618 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5619 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 5620}
a66c9326 5621
79b01846 5622/* The number of named compound-literals generated thus far. */
5623static GTY(()) int compound_literal_number;
5624
5625/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5626
5627void
5628set_compound_literal_name (tree decl)
5629{
5630 char *name;
5631 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5632 compound_literal_number);
5633 compound_literal_number++;
5634 DECL_NAME (decl) = get_identifier (name);
5635}
5636
a66c9326 5637tree
e60a6f7b 5638build_va_arg (location_t loc, tree expr, tree type)
a66c9326 5639{
e60a6f7b 5640 expr = build1 (VA_ARG_EXPR, type, expr);
5641 SET_EXPR_LOCATION (expr, loc);
5642 return expr;
a66c9326 5643}
0d4238dc 5644
5645
dd878098 5646/* Linked list of disabled built-in functions. */
5647
5648typedef struct disabled_builtin
5649{
5650 const char *name;
5651 struct disabled_builtin *next;
5652} disabled_builtin;
5653static disabled_builtin *disabled_builtins = NULL;
5654
1cae46be 5655static bool builtin_function_disabled_p (const char *);
dd878098 5656
5657/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5658 begins with "__builtin_", give an error. */
5659
5660void
1cae46be 5661disable_builtin_function (const char *name)
dd878098 5662{
5663 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 5664 error ("cannot disable built-in function %qs", name);
dd878098 5665 else
5666 {
e85905e5 5667 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5668 new_disabled_builtin->name = name;
5669 new_disabled_builtin->next = disabled_builtins;
5670 disabled_builtins = new_disabled_builtin;
dd878098 5671 }
5672}
5673
5674
5675/* Return true if the built-in function NAME has been disabled, false
5676 otherwise. */
5677
5678static bool
1cae46be 5679builtin_function_disabled_p (const char *name)
dd878098 5680{
5681 disabled_builtin *p;
5682 for (p = disabled_builtins; p != NULL; p = p->next)
5683 {
5684 if (strcmp (name, p->name) == 0)
5685 return true;
5686 }
5687 return false;
5688}
5689
5690
3237155d 5691/* Worker for DEF_BUILTIN.
5692 Possibly define a builtin function with one or two names.
5693 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5694 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 5695
3237155d 5696static void
5697def_builtin_1 (enum built_in_function fncode,
5698 const char *name,
5699 enum built_in_class fnclass,
5700 tree fntype, tree libtype,
5701 bool both_p, bool fallback_p, bool nonansi_p,
5702 tree fnattrs, bool implicit_p)
0d4238dc 5703{
3237155d 5704 tree decl;
5705 const char *libname;
5706
27213ba3 5707 if (fntype == error_mark_node)
5708 return;
5709
3237155d 5710 gcc_assert ((!both_p && !fallback_p)
5711 || !strncmp (name, "__builtin_",
5712 strlen ("__builtin_")));
5713
5714 libname = name + strlen ("__builtin_");
54be5d7e 5715 decl = add_builtin_function (name, fntype, fncode, fnclass,
5716 (fallback_p ? libname : NULL),
5717 fnattrs);
b9a16870 5718
5719 set_builtin_decl (fncode, decl, implicit_p);
5720
3237155d 5721 if (both_p
5722 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 5723 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 5724 add_builtin_function (libname, libtype, fncode, fnclass,
5725 NULL, fnattrs);
0d4238dc 5726}
e94026da 5727\f
d7aeef06 5728/* Nonzero if the type T promotes to int. This is (nearly) the
5729 integral promotions defined in ISO C99 6.3.1.1/2. */
5730
5731bool
9f627b1a 5732c_promoting_integer_type_p (const_tree t)
d7aeef06 5733{
5734 switch (TREE_CODE (t))
5735 {
5736 case INTEGER_TYPE:
5737 return (TYPE_MAIN_VARIANT (t) == char_type_node
5738 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5739 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5740 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 5741 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5742 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 5743
5744 case ENUMERAL_TYPE:
5745 /* ??? Technically all enumerations not larger than an int
5746 promote to an int. But this is used along code paths
5747 that only want to notice a size change. */
5748 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5749
5750 case BOOLEAN_TYPE:
5751 return 1;
5752
5753 default:
5754 return 0;
5755 }
5756}
5757
e94026da 5758/* Return 1 if PARMS specifies a fixed number of parameters
5759 and none of their types is affected by default promotions. */
5760
5761int
9f627b1a 5762self_promoting_args_p (const_tree parms)
e94026da 5763{
9f627b1a 5764 const_tree t;
e94026da 5765 for (t = parms; t; t = TREE_CHAIN (t))
5766 {
19cb6b50 5767 tree type = TREE_VALUE (t);
43f74bc4 5768
e1d8e198 5769 if (type == error_mark_node)
5770 continue;
5771
e94026da 5772 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5773 return 0;
5774
5775 if (type == 0)
5776 return 0;
5777
5778 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5779 return 0;
5780
d7aeef06 5781 if (c_promoting_integer_type_p (type))
e94026da 5782 return 0;
5783 }
5784 return 1;
5785}
605fb01e 5786
c10de5e7 5787/* Recursively remove any '*' or '&' operator from TYPE. */
5788tree
5789strip_pointer_operator (tree t)
5790{
5791 while (POINTER_TYPE_P (t))
5792 t = TREE_TYPE (t);
5793 return t;
5794}
5795
57a0ed23 5796/* Recursively remove pointer or array type from TYPE. */
5797tree
5798strip_pointer_or_array_types (tree t)
5799{
5800 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5801 t = TREE_TYPE (t);
5802 return t;
5803}
5804
e41f0d80 5805/* Used to compare case labels. K1 and K2 are actually tree nodes
5806 representing case labels, or NULL_TREE for a `default' label.
5807 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5808 K2, and 0 if K1 and K2 are equal. */
5809
5810int
1cae46be 5811case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 5812{
5813 /* Consider a NULL key (such as arises with a `default' label) to be
5814 smaller than anything else. */
5815 if (!k1)
5816 return k2 ? -1 : 0;
5817 else if (!k2)
5818 return k1 ? 1 : 0;
5819
5820 return tree_int_cst_compare ((tree) k1, (tree) k2);
5821}
5822
e60a6f7b 5823/* Process a case label, located at LOC, for the range LOW_VALUE
5824 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5825 then this case label is actually a `default' label. If only
5826 HIGH_VALUE is NULL_TREE, then case label was declared using the
5827 usual C/C++ syntax, rather than the GNU case range extension.
5828 CASES is a tree containing all the case ranges processed so far;
5829 COND is the condition for the switch-statement itself. Returns the
5830 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5831 is created. */
e41f0d80 5832
5833tree
e60a6f7b 5834c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
2ca392fd 5835 tree low_value, tree high_value)
e41f0d80 5836{
5837 tree type;
5838 tree label;
5839 tree case_label;
5840 splay_tree_node node;
5841
5842 /* Create the LABEL_DECL itself. */
e60a6f7b 5843 label = create_artificial_label (loc);
e41f0d80 5844
5845 /* If there was an error processing the switch condition, bail now
5846 before we get more confused. */
5847 if (!cond || cond == error_mark_node)
4ee9c684 5848 goto error_out;
e41f0d80 5849
1cae46be 5850 if ((low_value && TREE_TYPE (low_value)
5851 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 5852 || (high_value && TREE_TYPE (high_value)
5853 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 5854 {
e60a6f7b 5855 error_at (loc, "pointers are not permitted as case values");
b96dc121 5856 goto error_out;
5857 }
e41f0d80 5858
5859 /* Case ranges are a GNU extension. */
8864917d 5860 if (high_value)
29438999 5861 pedwarn (loc, OPT_Wpedantic,
8864917d 5862 "range expressions in switch statements are non-standard");
e41f0d80 5863
5864 type = TREE_TYPE (cond);
5865 if (low_value)
5866 {
5867 low_value = check_case_value (low_value);
5868 low_value = convert_and_check (type, low_value);
96722196 5869 if (low_value == error_mark_node)
5870 goto error_out;
e41f0d80 5871 }
5872 if (high_value)
5873 {
5874 high_value = check_case_value (high_value);
5875 high_value = convert_and_check (type, high_value);
96722196 5876 if (high_value == error_mark_node)
5877 goto error_out;
e41f0d80 5878 }
5879
96722196 5880 if (low_value && high_value)
5881 {
5882 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 5883 really a case range, even though it was written that way.
5884 Remove the HIGH_VALUE to simplify later processing. */
96722196 5885 if (tree_int_cst_equal (low_value, high_value))
5886 high_value = NULL_TREE;
5887 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 5888 warning_at (loc, 0, "empty range specified");
96722196 5889 }
e41f0d80 5890
2ca392fd 5891 /* See if the case is in range of the type of the original testing
5892 expression. If both low_value and high_value are out of range,
5893 don't insert the case label and return NULL_TREE. */
5894 if (low_value
84166705 5895 && !check_case_bounds (type, orig_type,
5896 &low_value, high_value ? &high_value : NULL))
2ca392fd 5897 return NULL_TREE;
5898
e41f0d80 5899 /* Look up the LOW_VALUE in the table of case labels we already
5900 have. */
5901 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5902 /* If there was not an exact match, check for overlapping ranges.
5903 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5904 that's a `default' label and the only overlap is an exact match. */
5905 if (!node && (low_value || high_value))
5906 {
5907 splay_tree_node low_bound;
5908 splay_tree_node high_bound;
5909
5910 /* Even though there wasn't an exact match, there might be an
5911 overlap between this case range and another case range.
5912 Since we've (inductively) not allowed any overlapping case
5913 ranges, we simply need to find the greatest low case label
5914 that is smaller that LOW_VALUE, and the smallest low case
5915 label that is greater than LOW_VALUE. If there is an overlap
5916 it will occur in one of these two ranges. */
5917 low_bound = splay_tree_predecessor (cases,
5918 (splay_tree_key) low_value);
5919 high_bound = splay_tree_successor (cases,
5920 (splay_tree_key) low_value);
5921
5922 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5923 the LOW_VALUE, so there is no need to check unless the
5924 LOW_BOUND is in fact itself a case range. */
5925 if (low_bound
5926 && CASE_HIGH ((tree) low_bound->value)
5927 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5928 low_value) >= 0)
5929 node = low_bound;
5930 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5931 range is bigger than the low end of the current range, so we
5932 are only interested if the current range is a real range, and
5933 not an ordinary case label. */
1cae46be 5934 else if (high_bound
e41f0d80 5935 && high_value
5936 && (tree_int_cst_compare ((tree) high_bound->key,
5937 high_value)
5938 <= 0))
5939 node = high_bound;
5940 }
5941 /* If there was an overlap, issue an error. */
5942 if (node)
5943 {
eaae3b75 5944 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 5945
5946 if (high_value)
5947 {
e60a6f7b 5948 error_at (loc, "duplicate (or overlapping) case value");
5949 error_at (DECL_SOURCE_LOCATION (duplicate),
5950 "this is the first entry overlapping that value");
e41f0d80 5951 }
5952 else if (low_value)
5953 {
e60a6f7b 5954 error_at (loc, "duplicate case value") ;
5955 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 5956 }
5957 else
5958 {
e60a6f7b 5959 error_at (loc, "multiple default labels in one switch");
5960 error_at (DECL_SOURCE_LOCATION (duplicate),
5961 "this is the first default label");
e41f0d80 5962 }
4ee9c684 5963 goto error_out;
e41f0d80 5964 }
5965
5966 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 5967 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 5968 /* Register this case label in the splay tree. */
1cae46be 5969 splay_tree_insert (cases,
e41f0d80 5970 (splay_tree_key) low_value,
5971 (splay_tree_value) case_label);
5972
5973 return case_label;
4ee9c684 5974
5975 error_out:
daf6dff5 5976 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 5977 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 5978 that just leads to duplicates and thence to failure later on. */
4ee9c684 5979 if (!cases->root)
5980 {
e60a6f7b 5981 tree t = create_artificial_label (loc);
5982 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 5983 }
5984 return error_mark_node;
5985}
5986
5987/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5988 Used to verify that case values match up with enumerator values. */
5989
5990static void
5991match_case_to_enum_1 (tree key, tree type, tree label)
5992{
5993 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5994
5995 /* ??? Not working too hard to print the double-word value.
5996 Should perhaps be done with %lwd in the diagnostic routines? */
5997 if (TREE_INT_CST_HIGH (key) == 0)
5998 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5999 TREE_INT_CST_LOW (key));
6000 else if (!TYPE_UNSIGNED (type)
6001 && TREE_INT_CST_HIGH (key) == -1
6002 && TREE_INT_CST_LOW (key) != 0)
6003 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6004 -TREE_INT_CST_LOW (key));
6005 else
6006 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
7df7561b 6007 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6008 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
4ee9c684 6009
6010 if (TYPE_NAME (type) == 0)
712d2297 6011 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6012 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6013 "case value %qs not in enumerated type",
6014 buf);
4ee9c684 6015 else
712d2297 6016 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6017 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6018 "case value %qs not in enumerated type %qT",
6019 buf, type);
4ee9c684 6020}
6021
359d87c6 6022/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6023 Used to verify that case values match up with enumerator values. */
6024
4ee9c684 6025static int
6026match_case_to_enum (splay_tree_node node, void *data)
6027{
6028 tree label = (tree) node->value;
4fd61bc6 6029 tree type = (tree) data;
4ee9c684 6030
6031 /* Skip default case. */
6032 if (!CASE_LOW (label))
6033 return 0;
6034
359d87c6 6035 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6036 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6037 if (!CASE_LOW_SEEN (label))
4ee9c684 6038 match_case_to_enum_1 (CASE_LOW (label), type, label);
6039 else
359d87c6 6040 CASE_LOW_SEEN (label) = 0;
4ee9c684 6041
359d87c6 6042 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6043 not set, that means that CASE_HIGH did not appear when we did our
6044 enum->case scan. Reset our scratch bit after. */
4ee9c684 6045 if (CASE_HIGH (label))
6046 {
359d87c6 6047 if (!CASE_HIGH_SEEN (label))
6048 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6049 else
6050 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6051 }
6052
6053 return 0;
6054}
6055
e7911019 6056/* Handle -Wswitch*. Called from the front end after parsing the
6057 switch construct. */
6058/* ??? Should probably be somewhere generic, since other languages
6059 besides C and C++ would want this. At the moment, however, C/C++
6060 are the only tree-ssa languages that support enumerations at all,
6061 so the point is moot. */
4ee9c684 6062
e7911019 6063void
6064c_do_switch_warnings (splay_tree cases, location_t switch_location,
6065 tree type, tree cond)
4ee9c684 6066{
b27ac6b5 6067 splay_tree_node default_node;
359d87c6 6068 splay_tree_node node;
6069 tree chain;
4ee9c684 6070
6071 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6072 return;
6073
4ee9c684 6074 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6075 if (!default_node)
5fb6a912 6076 warning_at (switch_location, OPT_Wswitch_default,
6077 "switch missing default case");
4ee9c684 6078
359d87c6 6079 /* From here on, we only care about about enumerated types. */
6080 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6081 return;
6082
561017b5 6083 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6084 if (!warn_switch_enum && !warn_switch)
359d87c6 6085 return;
6086
561017b5 6087 /* Check the cases. Warn about case values which are not members of
6088 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6089 there is no default case, check that exactly all enumeration
6090 literals are covered by the cases. */
6091
359d87c6 6092 /* Clearing COND if it is not an integer constant simplifies
6093 the tests inside the loop below. */
6094 if (TREE_CODE (cond) != INTEGER_CST)
6095 cond = NULL_TREE;
6096
6097 /* The time complexity here is O(N*lg(N)) worst case, but for the
6098 common case of monotonically increasing enumerators, it is
6099 O(N), since the nature of the splay tree will keep the next
6100 element adjacent to the root at all times. */
4ee9c684 6101
359d87c6 6102 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6103 {
6104 tree value = TREE_VALUE (chain);
3f00a6c0 6105 if (TREE_CODE (value) == CONST_DECL)
6106 value = DECL_INITIAL (value);
359d87c6 6107 node = splay_tree_lookup (cases, (splay_tree_key) value);
6108 if (node)
4ee9c684 6109 {
359d87c6 6110 /* Mark the CASE_LOW part of the case entry as seen. */
6111 tree label = (tree) node->value;
6112 CASE_LOW_SEEN (label) = 1;
6113 continue;
6114 }
6115
6116 /* Even though there wasn't an exact match, there might be a
f0b5f617 6117 case range which includes the enumerator's value. */
359d87c6 6118 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6119 if (node && CASE_HIGH ((tree) node->value))
6120 {
6121 tree label = (tree) node->value;
6122 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6123 if (cmp >= 0)
4ee9c684 6124 {
359d87c6 6125 /* If we match the upper bound exactly, mark the CASE_HIGH
6126 part of the case entry as seen. */
6127 if (cmp == 0)
6128 CASE_HIGH_SEEN (label) = 1;
6129 continue;
4ee9c684 6130 }
6131 }
6132
359d87c6 6133 /* We've now determined that this enumerated literal isn't
6134 handled by the case labels of the switch statement. */
4ee9c684 6135
359d87c6 6136 /* If the switch expression is a constant, we only really care
6137 about whether that constant is handled by the switch. */
6138 if (cond && tree_int_cst_compare (cond, value))
6139 continue;
4ee9c684 6140
6cbbbc89 6141 /* If there is a default_node, the only relevant option is
561017b5 6142 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6143 to warn using -Wswitch because -Wswitch is enabled by -Wall
6144 while -Wswitch-enum is explicit. */
561017b5 6145 warning_at (switch_location,
6146 (default_node || !warn_switch
6147 ? OPT_Wswitch_enum
6148 : OPT_Wswitch),
6149 "enumeration value %qE not handled in switch",
6150 TREE_PURPOSE (chain));
4ee9c684 6151 }
359d87c6 6152
6153 /* Warn if there are case expressions that don't correspond to
6154 enumerators. This can occur since C and C++ don't enforce
6155 type-checking of assignments to enumeration variables.
6156
6157 The time complexity here is now always O(N) worst case, since
6158 we should have marked both the lower bound and upper bound of
6159 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6160 above. This scan also resets those fields. */
6cbbbc89 6161
359d87c6 6162 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6163}
6164
9dd48740 6165/* Finish an expression taking the address of LABEL (an
dda49785 6166 IDENTIFIER_NODE). Returns an expression for the address.
6167
6168 LOC is the location for the expression returned. */
d0a47c8d 6169
1cae46be 6170tree
dda49785 6171finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6172{
6173 tree result;
6174
29438999 6175 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6176
9dd48740 6177 if (label == error_mark_node)
6178 return error_mark_node;
6179
d0a47c8d 6180 label = lookup_label (label);
6181 if (label == NULL_TREE)
6182 result = null_pointer_node;
6183 else
6184 {
6185 TREE_USED (label) = 1;
6186 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6187 /* The current function is not necessarily uninlinable.
d0a47c8d 6188 Computed gotos are incompatible with inlining, but the value
6189 here could be used only in a diagnostic, for example. */
dda49785 6190 protected_set_expr_location (result, loc);
d0a47c8d 6191 }
6192
6193 return result;
6194}
4f9a1c9b 6195\f
6196
6197/* Given a boolean expression ARG, return a tree representing an increment
6198 or decrement (as indicated by CODE) of ARG. The front end must check for
6199 invalid cases (e.g., decrement in C++). */
6200tree
1cae46be 6201boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6202{
6203 tree val;
69db191c 6204 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6205
4f9a1c9b 6206 arg = stabilize_reference (arg);
6207 switch (code)
6208 {
6209 case PREINCREMENT_EXPR:
14ae0310 6210 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6211 break;
6212 case POSTINCREMENT_EXPR:
14ae0310 6213 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6214 arg = save_expr (arg);
14ae0310 6215 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6216 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6217 break;
6218 case PREDECREMENT_EXPR:
14ae0310 6219 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6220 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6221 break;
6222 case POSTDECREMENT_EXPR:
14ae0310 6223 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6224 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6225 arg = save_expr (arg);
14ae0310 6226 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6227 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6228 break;
6229 default:
231bd014 6230 gcc_unreachable ();
4f9a1c9b 6231 }
6232 TREE_SIDE_EFFECTS (val) = 1;
6233 return val;
6234}
76a6e674 6235\f
f3449a3c 6236/* Built-in macros for stddef.h and stdint.h, that require macros
6237 defined in this file. */
79cf3ec1 6238void
1cae46be 6239c_stddef_cpp_builtins(void)
1ed9d5f5 6240{
63994318 6241 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6242 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6243 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6244 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6245 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6246 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6247 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6248 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6249 if (SIG_ATOMIC_TYPE)
6250 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6251 if (INT8_TYPE)
6252 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6253 if (INT16_TYPE)
6254 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6255 if (INT32_TYPE)
6256 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6257 if (INT64_TYPE)
6258 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6259 if (UINT8_TYPE)
6260 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6261 if (UINT16_TYPE)
6262 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6263 if (UINT32_TYPE)
6264 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6265 if (UINT64_TYPE)
6266 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6267 if (INT_LEAST8_TYPE)
6268 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6269 if (INT_LEAST16_TYPE)
6270 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6271 if (INT_LEAST32_TYPE)
6272 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6273 if (INT_LEAST64_TYPE)
6274 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6275 if (UINT_LEAST8_TYPE)
6276 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6277 if (UINT_LEAST16_TYPE)
6278 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6279 if (UINT_LEAST32_TYPE)
6280 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6281 if (UINT_LEAST64_TYPE)
6282 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6283 if (INT_FAST8_TYPE)
6284 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6285 if (INT_FAST16_TYPE)
6286 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6287 if (INT_FAST32_TYPE)
6288 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6289 if (INT_FAST64_TYPE)
6290 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6291 if (UINT_FAST8_TYPE)
6292 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6293 if (UINT_FAST16_TYPE)
6294 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6295 if (UINT_FAST32_TYPE)
6296 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6297 if (UINT_FAST64_TYPE)
6298 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6299 if (INTPTR_TYPE)
6300 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6301 if (UINTPTR_TYPE)
6302 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 6303}
6304
7d3b509a 6305static void
1cae46be 6306c_init_attributes (void)
7d3b509a 6307{
6308 /* Fill in the built_in_attributes array. */
7c446c95 6309#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 6310 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 6311#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 6312 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 6313#define DEF_ATTR_STRING(ENUM, VALUE) \
6314 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 6315#define DEF_ATTR_IDENT(ENUM, STRING) \
6316 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6317#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6318 built_in_attributes[(int) ENUM] \
6319 = tree_cons (built_in_attributes[(int) PURPOSE], \
6320 built_in_attributes[(int) VALUE], \
6321 built_in_attributes[(int) CHAIN]);
7d3b509a 6322#include "builtin-attrs.def"
6323#undef DEF_ATTR_NULL_TREE
6324#undef DEF_ATTR_INT
6325#undef DEF_ATTR_IDENT
6326#undef DEF_ATTR_TREE_LIST
76a6e674 6327}
5f3cead1 6328
33199a81 6329/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6330 identifier as an argument, so the front end shouldn't look it up. */
6331
6332bool
47b19b94 6333attribute_takes_identifier_p (const_tree attr_id)
33199a81 6334{
9bf1c74e 6335 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 6336 if (spec == NULL)
6337 /* Unknown attribute that we'll end up ignoring, return true so we
6338 don't complain about an identifier argument. */
6339 return true;
6340 else if (!strcmp ("mode", spec->name)
6341 || !strcmp ("format", spec->name)
6342 || !strcmp ("cleanup", spec->name))
47b19b94 6343 return true;
6344 else
6345 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 6346}
6347
f8e93a2e 6348/* Attribute handlers common to C front ends. */
6349
6350/* Handle a "packed" attribute; arguments as in
6351 struct attribute_spec.handler. */
6352
6353static tree
9a03a746 6354handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 6355 int flags, bool *no_add_attrs)
f8e93a2e 6356{
f40175cb 6357 if (TYPE_P (*node))
f8e93a2e 6358 {
6359 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 6360 *node = build_variant_type_copy (*node);
f40175cb 6361 TYPE_PACKED (*node) = 1;
f8e93a2e 6362 }
6363 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 6364 {
9fd767c5 6365 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6366 /* Still pack bitfields. */
6367 && ! DECL_INITIAL (*node))
c2ab04f9 6368 warning (OPT_Wattributes,
6369 "%qE attribute ignored for field of type %qT",
6370 name, TREE_TYPE (*node));
6371 else
6372 DECL_PACKED (*node) = 1;
6373 }
f8e93a2e 6374 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 6375 used for DECL_REGISTER. It wouldn't mean anything anyway.
6376 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6377 that changes what the typedef is typing. */
f8e93a2e 6378 else
6379 {
9b2d6d13 6380 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6381 *no_add_attrs = true;
6382 }
6383
6384 return NULL_TREE;
6385}
6386
6387/* Handle a "nocommon" attribute; arguments as in
6388 struct attribute_spec.handler. */
6389
6390static tree
1cae46be 6391handle_nocommon_attribute (tree *node, tree name,
9a03a746 6392 tree ARG_UNUSED (args),
6393 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6394{
6395 if (TREE_CODE (*node) == VAR_DECL)
6396 DECL_COMMON (*node) = 0;
6397 else
6398 {
9b2d6d13 6399 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6400 *no_add_attrs = true;
6401 }
6402
6403 return NULL_TREE;
6404}
6405
6406/* Handle a "common" attribute; arguments as in
6407 struct attribute_spec.handler. */
6408
6409static tree
9a03a746 6410handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6411 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6412{
6413 if (TREE_CODE (*node) == VAR_DECL)
6414 DECL_COMMON (*node) = 1;
6415 else
6416 {
9b2d6d13 6417 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6418 *no_add_attrs = true;
6419 }
6420
6421 return NULL_TREE;
6422}
6423
6424/* Handle a "noreturn" attribute; arguments as in
6425 struct attribute_spec.handler. */
6426
6427static tree
9a03a746 6428handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6429 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6430{
6431 tree type = TREE_TYPE (*node);
6432
6433 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 6434 if (TREE_CODE (*node) == FUNCTION_DECL
6435 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 6436 TREE_THIS_VOLATILE (*node) = 1;
6437 else if (TREE_CODE (type) == POINTER_TYPE
6438 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6439 TREE_TYPE (*node)
6440 = build_pointer_type
6441 (build_type_variant (TREE_TYPE (type),
71bda45d 6442 TYPE_READONLY (TREE_TYPE (type)), 1));
f8e93a2e 6443 else
6444 {
9b2d6d13 6445 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6446 *no_add_attrs = true;
6447 }
6448
6449 return NULL_TREE;
6450}
6451
5de92639 6452/* Handle a "hot" and attribute; arguments as in
6453 struct attribute_spec.handler. */
6454
6455static tree
6456handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 6457 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 6458{
758a38ab 6459 if (TREE_CODE (*node) == FUNCTION_DECL
6460 || TREE_CODE (*node) == LABEL_DECL)
5de92639 6461 {
6462 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6463 {
6464 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6465 name, "cold");
6466 *no_add_attrs = true;
6467 }
24470055 6468 /* Most of the rest of the hot processing is done later with
6469 lookup_attribute. */
5de92639 6470 }
6471 else
6472 {
6473 warning (OPT_Wattributes, "%qE attribute ignored", name);
6474 *no_add_attrs = true;
6475 }
6476
6477 return NULL_TREE;
6478}
758a38ab 6479
5de92639 6480/* Handle a "cold" and attribute; arguments as in
6481 struct attribute_spec.handler. */
6482
6483static tree
6484handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6485 int ARG_UNUSED (flags), bool *no_add_attrs)
6486{
758a38ab 6487 if (TREE_CODE (*node) == FUNCTION_DECL
6488 || TREE_CODE (*node) == LABEL_DECL)
5de92639 6489 {
6490 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6491 {
6492 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6493 name, "hot");
6494 *no_add_attrs = true;
6495 }
24470055 6496 /* Most of the rest of the cold processing is done later with
6497 lookup_attribute. */
5de92639 6498 }
6499 else
6500 {
6501 warning (OPT_Wattributes, "%qE attribute ignored", name);
6502 *no_add_attrs = true;
6503 }
6504
6505 return NULL_TREE;
6506}
6507
d413ffdd 6508/* Handle a "no_address_safety_analysis" attribute; arguments as in
6509 struct attribute_spec.handler. */
6510
6511static tree
6512handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6513 bool *no_add_attrs)
6514{
6515 if (TREE_CODE (*node) != FUNCTION_DECL)
6516 {
6517 warning (OPT_Wattributes, "%qE attribute ignored", name);
6518 *no_add_attrs = true;
6519 }
6520
6521 return NULL_TREE;
6522}
6523
f8e93a2e 6524/* Handle a "noinline" attribute; arguments as in
6525 struct attribute_spec.handler. */
6526
6527static tree
1cae46be 6528handle_noinline_attribute (tree *node, tree name,
9a03a746 6529 tree ARG_UNUSED (args),
6530 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6531{
6532 if (TREE_CODE (*node) == FUNCTION_DECL)
6533 DECL_UNINLINABLE (*node) = 1;
6534 else
6535 {
9b2d6d13 6536 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6537 *no_add_attrs = true;
6538 }
6539
6540 return NULL_TREE;
6541}
6542
bdb1f0d1 6543/* Handle a "noclone" attribute; arguments as in
6544 struct attribute_spec.handler. */
6545
6546static tree
6547handle_noclone_attribute (tree *node, tree name,
6548 tree ARG_UNUSED (args),
6549 int ARG_UNUSED (flags), bool *no_add_attrs)
6550{
6551 if (TREE_CODE (*node) != FUNCTION_DECL)
6552 {
6553 warning (OPT_Wattributes, "%qE attribute ignored", name);
6554 *no_add_attrs = true;
6555 }
6556
6557 return NULL_TREE;
6558}
6559
f8e93a2e 6560/* Handle a "always_inline" attribute; arguments as in
6561 struct attribute_spec.handler. */
6562
6563static tree
1cae46be 6564handle_always_inline_attribute (tree *node, tree name,
9a03a746 6565 tree ARG_UNUSED (args),
6566 int ARG_UNUSED (flags),
09347743 6567 bool *no_add_attrs)
f8e93a2e 6568{
6569 if (TREE_CODE (*node) == FUNCTION_DECL)
6570 {
ebb7d626 6571 /* Set the attribute and mark it for disregarding inline
6572 limits. */
6573 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 6574 }
6575 else
6576 {
9b2d6d13 6577 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6578 *no_add_attrs = true;
541e4101 6579 }
6580
6581 return NULL_TREE;
6582}
6583
6584/* Handle a "gnu_inline" attribute; arguments as in
6585 struct attribute_spec.handler. */
6586
6587static tree
6588handle_gnu_inline_attribute (tree *node, tree name,
6589 tree ARG_UNUSED (args),
6590 int ARG_UNUSED (flags),
6591 bool *no_add_attrs)
6592{
6593 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6594 {
6595 /* Do nothing else, just set the attribute. We'll get at
6596 it later with lookup_attribute. */
6597 }
6598 else
6599 {
6600 warning (OPT_Wattributes, "%qE attribute ignored", name);
6601 *no_add_attrs = true;
7bd95dfd 6602 }
6603
6604 return NULL_TREE;
6605}
6606
6607/* Handle a "leaf" attribute; arguments as in
6608 struct attribute_spec.handler. */
6609
6610static tree
6611handle_leaf_attribute (tree *node, tree name,
6612 tree ARG_UNUSED (args),
6613 int ARG_UNUSED (flags), bool *no_add_attrs)
6614{
6615 if (TREE_CODE (*node) != FUNCTION_DECL)
6616 {
6617 warning (OPT_Wattributes, "%qE attribute ignored", name);
6618 *no_add_attrs = true;
6619 }
6620 if (!TREE_PUBLIC (*node))
6621 {
6622 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6623 *no_add_attrs = true;
f8e93a2e 6624 }
6625
6626 return NULL_TREE;
6627}
6628
1b16fc45 6629/* Handle an "artificial" attribute; arguments as in
6630 struct attribute_spec.handler. */
6631
6632static tree
6633handle_artificial_attribute (tree *node, tree name,
6634 tree ARG_UNUSED (args),
6635 int ARG_UNUSED (flags),
6636 bool *no_add_attrs)
6637{
6638 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6639 {
6640 /* Do nothing else, just set the attribute. We'll get at
6641 it later with lookup_attribute. */
6642 }
6643 else
6644 {
6645 warning (OPT_Wattributes, "%qE attribute ignored", name);
6646 *no_add_attrs = true;
6647 }
6648
6649 return NULL_TREE;
6650}
6651
0cdd9887 6652/* Handle a "flatten" attribute; arguments as in
6653 struct attribute_spec.handler. */
6654
6655static tree
6656handle_flatten_attribute (tree *node, tree name,
a0c938f0 6657 tree args ATTRIBUTE_UNUSED,
6658 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 6659{
6660 if (TREE_CODE (*node) == FUNCTION_DECL)
6661 /* Do nothing else, just set the attribute. We'll get at
6662 it later with lookup_attribute. */
6663 ;
6664 else
6665 {
6666 warning (OPT_Wattributes, "%qE attribute ignored", name);
6667 *no_add_attrs = true;
6668 }
6669
6670 return NULL_TREE;
6671}
6672
10fc867f 6673/* Handle a "warning" or "error" attribute; arguments as in
6674 struct attribute_spec.handler. */
6675
6676static tree
6677handle_error_attribute (tree *node, tree name, tree args,
6678 int ARG_UNUSED (flags), bool *no_add_attrs)
6679{
6680 if (TREE_CODE (*node) == FUNCTION_DECL
6681 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6682 /* Do nothing else, just set the attribute. We'll get at
6683 it later with lookup_attribute. */
6684 ;
6685 else
6686 {
6687 warning (OPT_Wattributes, "%qE attribute ignored", name);
6688 *no_add_attrs = true;
6689 }
6690
6691 return NULL_TREE;
6692}
0cdd9887 6693
f8e93a2e 6694/* Handle a "used" attribute; arguments as in
6695 struct attribute_spec.handler. */
6696
6697static tree
9a03a746 6698handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6699 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6700{
d0a31bd8 6701 tree node = *pnode;
6702
6703 if (TREE_CODE (node) == FUNCTION_DECL
a4e3ffad 6704 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6705 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 6706 {
f54ed8bc 6707 TREE_USED (node) = 1;
9423c9b7 6708 DECL_PRESERVE_P (node) = 1;
abc6c64f 6709 if (TREE_CODE (node) == VAR_DECL)
6710 DECL_READ_P (node) = 1;
f54ed8bc 6711 }
f8e93a2e 6712 else
6713 {
9b2d6d13 6714 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6715 *no_add_attrs = true;
6716 }
6717
6718 return NULL_TREE;
6719}
6720
6721/* Handle a "unused" attribute; arguments as in
6722 struct attribute_spec.handler. */
6723
6724static tree
9a03a746 6725handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6726 int flags, bool *no_add_attrs)
f8e93a2e 6727{
6728 if (DECL_P (*node))
6729 {
6730 tree decl = *node;
6731
6732 if (TREE_CODE (decl) == PARM_DECL
6733 || TREE_CODE (decl) == VAR_DECL
6734 || TREE_CODE (decl) == FUNCTION_DECL
6735 || TREE_CODE (decl) == LABEL_DECL
6736 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 6737 {
6738 TREE_USED (decl) = 1;
6739 if (TREE_CODE (decl) == VAR_DECL
6740 || TREE_CODE (decl) == PARM_DECL)
6741 DECL_READ_P (decl) = 1;
6742 }
f8e93a2e 6743 else
6744 {
9b2d6d13 6745 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6746 *no_add_attrs = true;
6747 }
6748 }
6749 else
6750 {
6751 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 6752 *node = build_variant_type_copy (*node);
f8e93a2e 6753 TREE_USED (*node) = 1;
6754 }
6755
6756 return NULL_TREE;
6757}
6758
62eec3b4 6759/* Handle a "externally_visible" attribute; arguments as in
6760 struct attribute_spec.handler. */
6761
6762static tree
6763handle_externally_visible_attribute (tree *pnode, tree name,
6764 tree ARG_UNUSED (args),
6765 int ARG_UNUSED (flags),
6766 bool *no_add_attrs)
6767{
6768 tree node = *pnode;
6769
ba12ea31 6770 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
62eec3b4 6771 {
ba12ea31 6772 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6773 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6774 {
6775 warning (OPT_Wattributes,
6776 "%qE attribute have effect only on public objects", name);
6777 *no_add_attrs = true;
6778 }
62eec3b4 6779 }
62eec3b4 6780 else
6781 {
6782 warning (OPT_Wattributes, "%qE attribute ignored", name);
6783 *no_add_attrs = true;
6784 }
6785
6786 return NULL_TREE;
6787}
6788
f8e93a2e 6789/* Handle a "const" attribute; arguments as in
6790 struct attribute_spec.handler. */
6791
6792static tree
9a03a746 6793handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6794 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6795{
6796 tree type = TREE_TYPE (*node);
6797
6798 /* See FIXME comment on noreturn in c_common_attribute_table. */
6799 if (TREE_CODE (*node) == FUNCTION_DECL)
6800 TREE_READONLY (*node) = 1;
6801 else if (TREE_CODE (type) == POINTER_TYPE
6802 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6803 TREE_TYPE (*node)
6804 = build_pointer_type
6805 (build_type_variant (TREE_TYPE (type), 1,
6806 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6807 else
6808 {
9b2d6d13 6809 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6810 *no_add_attrs = true;
6811 }
6812
6813 return NULL_TREE;
6814}
6815
6816/* Handle a "transparent_union" attribute; arguments as in
6817 struct attribute_spec.handler. */
6818
6819static tree
1cae46be 6820handle_transparent_union_attribute (tree *node, tree name,
9a03a746 6821 tree ARG_UNUSED (args), int flags,
09347743 6822 bool *no_add_attrs)
f8e93a2e 6823{
881eb642 6824 tree type;
03908818 6825
6826 *no_add_attrs = true;
f8e93a2e 6827
ffcdbf9c 6828
6829 if (TREE_CODE (*node) == TYPE_DECL
6830 && ! (flags & ATTR_FLAG_CXX11))
881eb642 6831 node = &TREE_TYPE (*node);
6832 type = *node;
f8e93a2e 6833
03908818 6834 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 6835 {
fca86134 6836 /* Make sure that the first field will work for a transparent union.
6837 If the type isn't complete yet, leave the check to the code in
6838 finish_struct. */
6839 if (TYPE_SIZE (type))
6840 {
6841 tree first = first_field (type);
6842 if (first == NULL_TREE
6843 || DECL_ARTIFICIAL (first)
6844 || TYPE_MODE (type) != DECL_MODE (first))
6845 goto ignored;
6846 }
6847
f8e93a2e 6848 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 6849 {
fca86134 6850 /* If the type isn't complete yet, setting the flag
6851 on a variant wouldn't ever be checked. */
6852 if (!TYPE_SIZE (type))
6853 goto ignored;
6854
6855 /* build_duplicate_type doesn't work for C++. */
6856 if (c_dialect_cxx ())
03908818 6857 goto ignored;
6858
6859 /* A type variant isn't good enough, since we don't a cast
6860 to such a type removed as a no-op. */
6861 *node = type = build_duplicate_type (type);
6862 }
6863
8df5a43d 6864 TYPE_TRANSPARENT_AGGR (type) = 1;
03908818 6865 return NULL_TREE;
f8e93a2e 6866 }
6867
03908818 6868 ignored:
6869 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6870 return NULL_TREE;
6871}
6872
9af7fd5b 6873/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6874 get the requested priority for a constructor or destructor,
6875 possibly issuing diagnostics for invalid or reserved
6876 priorities. */
6877
6878static priority_type
6879get_priority (tree args, bool is_destructor)
6880{
6881 HOST_WIDE_INT pri;
6c181a06 6882 tree arg;
9af7fd5b 6883
6884 if (!args)
6885 return DEFAULT_INIT_PRIORITY;
48e1416a 6886
28fbc04f 6887 if (!SUPPORTS_INIT_PRIORITY)
6888 {
6889 if (is_destructor)
6890 error ("destructor priorities are not supported");
6891 else
6892 error ("constructor priorities are not supported");
6893 return DEFAULT_INIT_PRIORITY;
6894 }
6895
6c181a06 6896 arg = TREE_VALUE (args);
c28ddc97 6897 arg = default_conversion (arg);
6c181a06 6898 if (!host_integerp (arg, /*pos=*/0)
6899 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 6900 goto invalid;
6901
c28ddc97 6902 pri = tree_low_cst (arg, /*pos=*/0);
9af7fd5b 6903 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6904 goto invalid;
6905
6906 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6907 {
6908 if (is_destructor)
6909 warning (0,
6910 "destructor priorities from 0 to %d are reserved "
48e1416a 6911 "for the implementation",
9af7fd5b 6912 MAX_RESERVED_INIT_PRIORITY);
6913 else
6914 warning (0,
6915 "constructor priorities from 0 to %d are reserved "
48e1416a 6916 "for the implementation",
9af7fd5b 6917 MAX_RESERVED_INIT_PRIORITY);
6918 }
6919 return pri;
6920
6921 invalid:
6922 if (is_destructor)
6923 error ("destructor priorities must be integers from 0 to %d inclusive",
6924 MAX_INIT_PRIORITY);
6925 else
6926 error ("constructor priorities must be integers from 0 to %d inclusive",
6927 MAX_INIT_PRIORITY);
6928 return DEFAULT_INIT_PRIORITY;
6929}
6930
f8e93a2e 6931/* Handle a "constructor" attribute; arguments as in
6932 struct attribute_spec.handler. */
6933
6934static tree
9af7fd5b 6935handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 6936 int ARG_UNUSED (flags),
09347743 6937 bool *no_add_attrs)
f8e93a2e 6938{
6939 tree decl = *node;
6940 tree type = TREE_TYPE (decl);
6941
6942 if (TREE_CODE (decl) == FUNCTION_DECL
6943 && TREE_CODE (type) == FUNCTION_TYPE
6944 && decl_function_context (decl) == 0)
6945 {
9af7fd5b 6946 priority_type priority;
f8e93a2e 6947 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 6948 priority = get_priority (args, /*is_destructor=*/false);
6949 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 6950 TREE_USED (decl) = 1;
6951 }
6952 else
6953 {
9b2d6d13 6954 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6955 *no_add_attrs = true;
6956 }
6957
6958 return NULL_TREE;
6959}
6960
6961/* Handle a "destructor" attribute; arguments as in
6962 struct attribute_spec.handler. */
6963
6964static tree
9af7fd5b 6965handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 6966 int ARG_UNUSED (flags),
09347743 6967 bool *no_add_attrs)
f8e93a2e 6968{
6969 tree decl = *node;
6970 tree type = TREE_TYPE (decl);
6971
6972 if (TREE_CODE (decl) == FUNCTION_DECL
6973 && TREE_CODE (type) == FUNCTION_TYPE
6974 && decl_function_context (decl) == 0)
6975 {
9af7fd5b 6976 priority_type priority;
f8e93a2e 6977 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 6978 priority = get_priority (args, /*is_destructor=*/true);
6979 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 6980 TREE_USED (decl) = 1;
6981 }
6982 else
6983 {
9b2d6d13 6984 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6985 *no_add_attrs = true;
6986 }
6987
6988 return NULL_TREE;
6989}
6990
1c58e3f1 6991/* Nonzero if the mode is a valid vector mode for this architecture.
6992 This returns nonzero even if there is no hardware support for the
6993 vector mode, but we can emulate with narrower modes. */
6994
6995static int
6996vector_mode_valid_p (enum machine_mode mode)
6997{
6998 enum mode_class mclass = GET_MODE_CLASS (mode);
6999 enum machine_mode innermode;
7000
7001 /* Doh! What's going on? */
7002 if (mclass != MODE_VECTOR_INT
7003 && mclass != MODE_VECTOR_FLOAT
7004 && mclass != MODE_VECTOR_FRACT
7005 && mclass != MODE_VECTOR_UFRACT
7006 && mclass != MODE_VECTOR_ACCUM
7007 && mclass != MODE_VECTOR_UACCUM)
7008 return 0;
7009
7010 /* Hardware support. Woo hoo! */
7011 if (targetm.vector_mode_supported_p (mode))
7012 return 1;
7013
7014 innermode = GET_MODE_INNER (mode);
7015
7016 /* We should probably return 1 if requesting V4DI and we have no DI,
7017 but we have V2DI, but this is probably very unlikely. */
7018
7019 /* If we have support for the inner mode, we can safely emulate it.
7020 We may not have V2DI, but me can emulate with a pair of DIs. */
7021 return targetm.scalar_mode_supported_p (innermode);
7022}
7023
7024
f8e93a2e 7025/* Handle a "mode" attribute; arguments as in
7026 struct attribute_spec.handler. */
7027
7028static tree
9a03a746 7029handle_mode_attribute (tree *node, tree name, tree args,
7030 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7031{
7032 tree type = *node;
d1dd9ac0 7033 tree ident = TREE_VALUE (args);
f8e93a2e 7034
7035 *no_add_attrs = true;
7036
d1dd9ac0 7037 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7038 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7039 else
7040 {
7041 int j;
d1dd9ac0 7042 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7043 int len = strlen (p);
7044 enum machine_mode mode = VOIDmode;
7045 tree typefm;
b2aef146 7046 bool valid_mode;
f8e93a2e 7047
7048 if (len > 4 && p[0] == '_' && p[1] == '_'
7049 && p[len - 1] == '_' && p[len - 2] == '_')
7050 {
4fd61bc6 7051 char *newp = (char *) alloca (len - 1);
f8e93a2e 7052
7053 strcpy (newp, &p[2]);
7054 newp[len - 4] = '\0';
7055 p = newp;
7056 }
7057
7058 /* Change this type to have a type with the specified mode.
7059 First check for the special modes. */
84166705 7060 if (!strcmp (p, "byte"))
f8e93a2e 7061 mode = byte_mode;
7062 else if (!strcmp (p, "word"))
7063 mode = word_mode;
84166705 7064 else if (!strcmp (p, "pointer"))
f8e93a2e 7065 mode = ptr_mode;
0ef89dfd 7066 else if (!strcmp (p, "libgcc_cmp_return"))
7067 mode = targetm.libgcc_cmp_return_mode ();
7068 else if (!strcmp (p, "libgcc_shift_count"))
7069 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7070 else if (!strcmp (p, "unwind_word"))
7071 mode = targetm.unwind_word_mode ();
f8e93a2e 7072 else
7073 for (j = 0; j < NUM_MACHINE_MODES; j++)
7074 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 7075 {
7076 mode = (enum machine_mode) j;
7077 break;
7078 }
f8e93a2e 7079
7080 if (mode == VOIDmode)
4917c376 7081 {
d1dd9ac0 7082 error ("unknown machine mode %qE", ident);
4917c376 7083 return NULL_TREE;
7084 }
7085
b2aef146 7086 valid_mode = false;
7087 switch (GET_MODE_CLASS (mode))
4917c376 7088 {
b2aef146 7089 case MODE_INT:
7090 case MODE_PARTIAL_INT:
7091 case MODE_FLOAT:
c4503c0a 7092 case MODE_DECIMAL_FLOAT:
9421ebb9 7093 case MODE_FRACT:
7094 case MODE_UFRACT:
7095 case MODE_ACCUM:
7096 case MODE_UACCUM:
b2aef146 7097 valid_mode = targetm.scalar_mode_supported_p (mode);
7098 break;
7099
7100 case MODE_COMPLEX_INT:
7101 case MODE_COMPLEX_FLOAT:
7102 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7103 break;
7104
7105 case MODE_VECTOR_INT:
7106 case MODE_VECTOR_FLOAT:
9421ebb9 7107 case MODE_VECTOR_FRACT:
7108 case MODE_VECTOR_UFRACT:
7109 case MODE_VECTOR_ACCUM:
7110 case MODE_VECTOR_UACCUM:
9b2d6d13 7111 warning (OPT_Wattributes, "specifying vector types with "
7112 "__attribute__ ((mode)) is deprecated");
7113 warning (OPT_Wattributes,
7114 "use __attribute__ ((vector_size)) instead");
b2aef146 7115 valid_mode = vector_mode_valid_p (mode);
7116 break;
4917c376 7117
b2aef146 7118 default:
7119 break;
7120 }
7121 if (!valid_mode)
7122 {
1e5fcbe2 7123 error ("unable to emulate %qs", p);
b2aef146 7124 return NULL_TREE;
7125 }
4917c376 7126
b2aef146 7127 if (POINTER_TYPE_P (type))
ead34f59 7128 {
6d5d708e 7129 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
b2aef146 7130 tree (*fn)(tree, enum machine_mode, bool);
7131
6d5d708e 7132 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 7133 {
1e5fcbe2 7134 error ("invalid pointer mode %qs", p);
ead34f59 7135 return NULL_TREE;
7136 }
7137
a0c938f0 7138 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 7139 fn = build_pointer_type_for_mode;
805e22b2 7140 else
b2aef146 7141 fn = build_reference_type_for_mode;
7142 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 7143 }
b2aef146 7144 else
9421ebb9 7145 {
7146 /* For fixed-point modes, we need to test if the signness of type
7147 and the machine mode are consistent. */
7148 if (ALL_FIXED_POINT_MODE_P (mode)
7149 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7150 {
bf776685 7151 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 7152 return NULL_TREE;
7153 }
7154 /* For fixed-point modes, we need to pass saturating info. */
7155 typefm = lang_hooks.types.type_for_mode (mode,
7156 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7157 : TYPE_UNSIGNED (type));
7158 }
3a648ab9 7159
b2aef146 7160 if (typefm == NULL_TREE)
7161 {
743a6f47 7162 error ("no data type for mode %qs", p);
b2aef146 7163 return NULL_TREE;
7164 }
3a648ab9 7165 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7166 {
7167 /* For enumeral types, copy the precision from the integer
7168 type returned above. If not an INTEGER_TYPE, we can't use
7169 this mode for this type. */
7170 if (TREE_CODE (typefm) != INTEGER_TYPE)
7171 {
743a6f47 7172 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 7173 return NULL_TREE;
7174 }
7175
10080eac 7176 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7177 {
7178 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7179 typefm = type;
7180 }
7181 else
7182 {
7183 /* We cannot build a type variant, as there's code that assumes
7184 that TYPE_MAIN_VARIANT has the same mode. This includes the
7185 debug generators. Instead, create a subrange type. This
7186 results in all of the enumeral values being emitted only once
7187 in the original, and the subtype gets them by reference. */
7188 if (TYPE_UNSIGNED (type))
7189 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7190 else
7191 typefm = make_signed_type (TYPE_PRECISION (typefm));
7192 TREE_TYPE (typefm) = type;
7193 }
3a648ab9 7194 }
4bf450a1 7195 else if (VECTOR_MODE_P (mode)
7196 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7197 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 7198 {
7199 error ("mode %qs applied to inappropriate type", p);
7200 return NULL_TREE;
7201 }
7202
b2aef146 7203 *node = typefm;
f8e93a2e 7204 }
7205
7206 return NULL_TREE;
7207}
7208
7209/* Handle a "section" attribute; arguments as in
7210 struct attribute_spec.handler. */
7211
7212static tree
9a03a746 7213handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7214 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7215{
7216 tree decl = *node;
7217
218e3e4e 7218 if (targetm_common.have_named_sections)
f8e93a2e 7219 {
065efcb1 7220 user_defined_section_attribute = true;
7221
f8e93a2e 7222 if ((TREE_CODE (decl) == FUNCTION_DECL
7223 || TREE_CODE (decl) == VAR_DECL)
7224 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7225 {
7226 if (TREE_CODE (decl) == VAR_DECL
7227 && current_function_decl != NULL_TREE
84166705 7228 && !TREE_STATIC (decl))
f8e93a2e 7229 {
48e1416a 7230 error_at (DECL_SOURCE_LOCATION (decl),
712d2297 7231 "section attribute cannot be specified for "
7232 "local variables");
f8e93a2e 7233 *no_add_attrs = true;
7234 }
7235
7236 /* The decl may have already been given a section attribute
7237 from a previous declaration. Ensure they match. */
7238 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7239 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7240 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7241 {
3cf8b391 7242 error ("section of %q+D conflicts with previous declaration",
7243 *node);
f8e93a2e 7244 *no_add_attrs = true;
7245 }
38475469 7246 else if (TREE_CODE (decl) == VAR_DECL
7247 && !targetm.have_tls && targetm.emutls.tmpl_section
7248 && DECL_THREAD_LOCAL_P (decl))
7249 {
7250 error ("section of %q+D cannot be overridden", *node);
7251 *no_add_attrs = true;
7252 }
f8e93a2e 7253 else
7254 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7255 }
7256 else
7257 {
3cf8b391 7258 error ("section attribute not allowed for %q+D", *node);
f8e93a2e 7259 *no_add_attrs = true;
7260 }
7261 }
7262 else
7263 {
712d2297 7264 error_at (DECL_SOURCE_LOCATION (*node),
7265 "section attributes are not supported for this target");
f8e93a2e 7266 *no_add_attrs = true;
7267 }
7268
7269 return NULL_TREE;
7270}
7271
83e25171 7272/* Check whether ALIGN is a valid user-specified alignment. If so,
7273 return its base-2 log; if not, output an error and return -1. If
7274 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7275 no error. */
7276int
7277check_user_alignment (const_tree align, bool allow_zero)
7278{
7279 int i;
7280
5abaa10a 7281 if (TREE_CODE (align) != INTEGER_CST
7282 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 7283 {
7284 error ("requested alignment is not an integer constant");
7285 return -1;
7286 }
7287 else if (allow_zero && integer_zerop (align))
7288 return -1;
7289 else if ((i = tree_log2 (align)) == -1)
7290 {
7291 error ("requested alignment is not a power of 2");
7292 return -1;
7293 }
7294 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7295 {
7296 error ("requested alignment is too large");
7297 return -1;
7298 }
7299 return i;
7300}
7301
ffcdbf9c 7302/*
7303 If in c++-11, check if the c++-11 alignment constraint with respect
7304 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7305 c++-11 mode, does nothing.
7306
7307 [dcl.align]2/ says:
7308
7309 [* if the constant expression evaluates to a fundamental alignment,
7310 the alignment requirement of the declared entity shall be the
7311 specified fundamental alignment.
7312
7313 * if the constant expression evaluates to an extended alignment
7314 and the implementation supports that alignment in the context
7315 of the declaration, the alignment of the declared entity shall
7316 be that alignment
7317
7318 * if the constant expression evaluates to an extended alignment
7319 and the implementation does not support that alignment in the
7320 context of the declaration, the program is ill-formed]. */
7321
7322static bool
7323check_cxx_fundamental_alignment_constraints (tree node,
7324 unsigned align_log,
7325 int flags)
7326{
7327 bool alignment_too_large_p = false;
7328 unsigned requested_alignment = 1U << align_log;
7329 unsigned max_align = 0;
7330
7331 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7332 || (node == NULL_TREE || node == error_mark_node))
7333 return true;
7334
7335 if (cxx_fundamental_alignment_p (requested_alignment))
7336 return true;
7337
7338 if (DECL_P (node))
7339 {
7340 if (TREE_STATIC (node))
7341 {
7342 /* For file scope variables and static members, the target
7343 supports alignments that are at most
7344 MAX_OFILE_ALIGNMENT. */
7345 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7346 alignment_too_large_p = true;
7347 }
7348 else
7349 {
7350#ifdef BIGGEST_FIELD_ALIGNMENT
7351#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7352#else
7353#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7354#endif
7355 /* For non-static members, the target supports either
7356 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7357 if it is defined or BIGGEST_ALIGNMENT. */
7358 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7359 if (TREE_CODE (node) == FIELD_DECL
7360 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7361 alignment_too_large_p = true;
7362#undef MAX_TARGET_FIELD_ALIGNMENT
7363 /* For stack variables, the target supports at most
7364 MAX_STACK_ALIGNMENT. */
7365 else if (decl_function_context (node) != NULL
7366 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7367 alignment_too_large_p = true;
7368 }
7369 }
7370 else if (TYPE_P (node))
7371 {
7372 /* Let's be liberal for types. */
7373 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7374 alignment_too_large_p = true;
7375 }
7376
7377 if (alignment_too_large_p)
7378 pedwarn (input_location, OPT_Wattributes,
7379 "requested alignment %d is larger than %d",
7380 requested_alignment, max_align);
7381
7382 return !alignment_too_large_p;
7383}
7384
f8e93a2e 7385/* Handle a "aligned" attribute; arguments as in
7386 struct attribute_spec.handler. */
7387
7388static tree
9a03a746 7389handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 7390 int flags, bool *no_add_attrs)
f8e93a2e 7391{
7392 tree decl = NULL_TREE;
7393 tree *type = NULL;
7394 int is_type = 0;
7395 tree align_expr = (args ? TREE_VALUE (args)
02421213 7396 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
f8e93a2e 7397 int i;
7398
7399 if (DECL_P (*node))
7400 {
7401 decl = *node;
7402 type = &TREE_TYPE (decl);
7403 is_type = TREE_CODE (*node) == TYPE_DECL;
7404 }
7405 else if (TYPE_P (*node))
7406 type = node, is_type = 1;
7407
ffcdbf9c 7408 if ((i = check_user_alignment (align_expr, false)) == -1
7409 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 7410 *no_add_attrs = true;
f8e93a2e 7411 else if (is_type)
7412 {
2ec3af9c 7413 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7414 /* OK, modify the type in place. */;
f8e93a2e 7415 /* If we have a TYPE_DECL, then copy the type, so that we
7416 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 7417 else if (decl && TREE_TYPE (decl) != error_mark_node
7418 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 7419 {
7420 tree tt = TREE_TYPE (decl);
e086912e 7421 *type = build_variant_type_copy (*type);
f8e93a2e 7422 DECL_ORIGINAL_TYPE (decl) = tt;
7423 TYPE_NAME (*type) = decl;
7424 TREE_USED (*type) = TREE_USED (decl);
7425 TREE_TYPE (decl) = *type;
7426 }
2ec3af9c 7427 else
e086912e 7428 *type = build_variant_type_copy (*type);
f8e93a2e 7429
7cfdc2f0 7430 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 7431 TYPE_USER_ALIGN (*type) = 1;
7432 }
097b5c8b 7433 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7434 && TREE_CODE (decl) != FIELD_DECL)
7435 {
3cf8b391 7436 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 7437 *no_add_attrs = true;
7438 }
ffcdbf9c 7439 else if (DECL_USER_ALIGN (decl)
7440 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7441 /* C++-11 [dcl.align/4]:
7442
7443 When multiple alignment-specifiers are specified for an
7444 entity, the alignment requirement shall be set to the
7445 strictest specified alignment.
7446
7447 This formally comes from the c++11 specification but we are
7448 doing it for the GNU attribute syntax as well. */
7449 *no_add_attrs = true;
097b5c8b 7450 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 7451 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 7452 {
7453 if (DECL_USER_ALIGN (decl))
7454 error ("alignment for %q+D was previously specified as %d "
7455 "and may not be decreased", decl,
7456 DECL_ALIGN (decl) / BITS_PER_UNIT);
7457 else
7458 error ("alignment for %q+D must be at least %d", decl,
7459 DECL_ALIGN (decl) / BITS_PER_UNIT);
7460 *no_add_attrs = true;
7461 }
f8e93a2e 7462 else
7463 {
7cfdc2f0 7464 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 7465 DECL_USER_ALIGN (decl) = 1;
7466 }
7467
7468 return NULL_TREE;
7469}
7470
7471/* Handle a "weak" attribute; arguments as in
7472 struct attribute_spec.handler. */
7473
7474static tree
f948b309 7475handle_weak_attribute (tree *node, tree name,
9a03a746 7476 tree ARG_UNUSED (args),
7477 int ARG_UNUSED (flags),
7478 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 7479{
f948b309 7480 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 7481 && DECL_DECLARED_INLINE_P (*node))
7482 {
0725e25c 7483 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 7484 *no_add_attrs = true;
7485 }
85c0a25c 7486 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7487 {
7488 error ("indirect function %q+D cannot be declared weak", *node);
7489 *no_add_attrs = true;
7490 return NULL_TREE;
7491 }
059a60f3 7492 else if (TREE_CODE (*node) == FUNCTION_DECL
7493 || TREE_CODE (*node) == VAR_DECL)
f948b309 7494 declare_weak (*node);
7495 else
7496 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7497
7498 return NULL_TREE;
7499}
7500
85c0a25c 7501/* Handle an "alias" or "ifunc" attribute; arguments as in
7502 struct attribute_spec.handler, except that IS_ALIAS tells us
7503 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 7504
7505static tree
85c0a25c 7506handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7507 bool *no_add_attrs)
f8e93a2e 7508{
7509 tree decl = *node;
7510
85c0a25c 7511 if (TREE_CODE (decl) != FUNCTION_DECL
7512 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
9e830260 7513 {
7514 warning (OPT_Wattributes, "%qE attribute ignored", name);
7515 *no_add_attrs = true;
7516 }
7517 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 7518 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 7519 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7520 /* A static variable declaration is always a tentative definition,
7521 but the alias is a non-tentative definition which overrides. */
48e1416a 7522 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 7523 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 7524 {
85c0a25c 7525 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 7526 *no_add_attrs = true;
85c0a25c 7527 return NULL_TREE;
f8e93a2e 7528 }
85c0a25c 7529 else if (!is_alias
7530 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7531 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7532 {
7533 error ("weak %q+D cannot be defined %qE", decl, name);
7534 *no_add_attrs = true;
7535 return NULL_TREE;
7536 }
8c42f0d9 7537
7538 /* Note that the very first time we process a nested declaration,
7539 decl_function_context will not be set. Indeed, *would* never
7540 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7541 we do below. After such frobbery, pushdecl would set the context.
7542 In any case, this is never what we want. */
7543 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 7544 {
7545 tree id;
7546
7547 id = TREE_VALUE (args);
7548 if (TREE_CODE (id) != STRING_CST)
7549 {
85c0a25c 7550 error ("attribute %qE argument not a string", name);
f8e93a2e 7551 *no_add_attrs = true;
7552 return NULL_TREE;
7553 }
7554 id = get_identifier (TREE_STRING_POINTER (id));
7555 /* This counts as a use of the object pointed to. */
7556 TREE_USED (id) = 1;
7557
7558 if (TREE_CODE (decl) == FUNCTION_DECL)
7559 DECL_INITIAL (decl) = error_mark_node;
7560 else
4ee9c684 7561 {
f4a30bd7 7562 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
7563 DECL_EXTERNAL (decl) = 1;
7564 else
7565 DECL_EXTERNAL (decl) = 0;
4ee9c684 7566 TREE_STATIC (decl) = 1;
7567 }
85c0a25c 7568
7569 if (!is_alias)
7570 /* ifuncs are also aliases, so set that attribute too. */
7571 DECL_ATTRIBUTES (decl)
7572 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 7573 }
7574 else
7575 {
9b2d6d13 7576 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7577 *no_add_attrs = true;
7578 }
7579
7580 return NULL_TREE;
7581}
7582
85c0a25c 7583/* Handle an "alias" or "ifunc" attribute; arguments as in
7584 struct attribute_spec.handler. */
7585
7586static tree
7587handle_ifunc_attribute (tree *node, tree name, tree args,
7588 int ARG_UNUSED (flags), bool *no_add_attrs)
7589{
7590 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7591}
7592
7593/* Handle an "alias" or "ifunc" attribute; arguments as in
7594 struct attribute_spec.handler. */
7595
7596static tree
7597handle_alias_attribute (tree *node, tree name, tree args,
7598 int ARG_UNUSED (flags), bool *no_add_attrs)
7599{
7600 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7601}
7602
f4a30bd7 7603/* Handle a "weakref" attribute; arguments as in struct
7604 attribute_spec.handler. */
7605
7606static tree
7607handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7608 int flags, bool *no_add_attrs)
7609{
7610 tree attr = NULL_TREE;
7611
83852912 7612 /* We must ignore the attribute when it is associated with
7613 local-scoped decls, since attribute alias is ignored and many
7614 such symbols do not even have a DECL_WEAK field. */
64a7bd81 7615 if (decl_function_context (*node)
7616 || current_function_decl
7617 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
83852912 7618 {
7619 warning (OPT_Wattributes, "%qE attribute ignored", name);
7620 *no_add_attrs = true;
7621 return NULL_TREE;
7622 }
7623
85c0a25c 7624 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7625 {
7626 error ("indirect function %q+D cannot be declared weakref", *node);
7627 *no_add_attrs = true;
7628 return NULL_TREE;
7629 }
7630
f4a30bd7 7631 /* The idea here is that `weakref("name")' mutates into `weakref,
7632 alias("name")', and weakref without arguments, in turn,
7633 implicitly adds weak. */
7634
7635 if (args)
7636 {
7637 attr = tree_cons (get_identifier ("alias"), args, attr);
7638 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7639
7640 *no_add_attrs = true;
0a3ecdc1 7641
7642 decl_attributes (node, attr, flags);
f4a30bd7 7643 }
7644 else
7645 {
7646 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 7647 error_at (DECL_SOURCE_LOCATION (*node),
7648 "weakref attribute must appear before alias attribute");
f4a30bd7 7649
0a3ecdc1 7650 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7651 and that isn't supported; and because it wants to add it to
7652 the list of weak decls, which isn't helpful. */
7653 DECL_WEAK (*node) = 1;
f4a30bd7 7654 }
7655
f4a30bd7 7656 return NULL_TREE;
7657}
7658
f8e93a2e 7659/* Handle an "visibility" attribute; arguments as in
7660 struct attribute_spec.handler. */
7661
7662static tree
1cae46be 7663handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 7664 int ARG_UNUSED (flags),
4a2849cb 7665 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 7666{
7667 tree decl = *node;
9c40570a 7668 tree id = TREE_VALUE (args);
4a2849cb 7669 enum symbol_visibility vis;
f8e93a2e 7670
b212f378 7671 if (TYPE_P (*node))
7672 {
4a2849cb 7673 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7674 /* OK */;
7675 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7676 {
7677 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7678 name);
7679 return NULL_TREE;
7680 }
7681 else if (TYPE_FIELDS (*node))
7682 {
7683 error ("%qE attribute ignored because %qT is already defined",
7684 name, *node);
7685 return NULL_TREE;
7686 }
b212f378 7687 }
84166705 7688 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 7689 {
9b2d6d13 7690 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 7691 return NULL_TREE;
f8e93a2e 7692 }
f8e93a2e 7693
9c40570a 7694 if (TREE_CODE (id) != STRING_CST)
7695 {
07e3a3d2 7696 error ("visibility argument not a string");
9c40570a 7697 return NULL_TREE;
f8e93a2e 7698 }
b27ac6b5 7699
b212f378 7700 /* If this is a type, set the visibility on the type decl. */
7701 if (TYPE_P (decl))
7702 {
7703 decl = TYPE_NAME (decl);
84166705 7704 if (!decl)
a0c938f0 7705 return NULL_TREE;
e147aab3 7706 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7707 {
9b2d6d13 7708 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 7709 name);
7710 return NULL_TREE;
7711 }
b212f378 7712 }
f8e93a2e 7713
9c40570a 7714 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 7715 vis = VISIBILITY_DEFAULT;
9c40570a 7716 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 7717 vis = VISIBILITY_INTERNAL;
9c40570a 7718 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 7719 vis = VISIBILITY_HIDDEN;
9c40570a 7720 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 7721 vis = VISIBILITY_PROTECTED;
9c40570a 7722 else
4a2849cb 7723 {
7724 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7725 vis = VISIBILITY_DEFAULT;
7726 }
7727
7728 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 7729 && vis != DECL_VISIBILITY (decl))
7730 {
7731 tree attributes = (TYPE_P (*node)
7732 ? TYPE_ATTRIBUTES (*node)
7733 : DECL_ATTRIBUTES (decl));
7734 if (lookup_attribute ("visibility", attributes))
7735 error ("%qD redeclared with different visibility", decl);
7736 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7737 && lookup_attribute ("dllimport", attributes))
7738 error ("%qD was declared %qs which implies default visibility",
7739 decl, "dllimport");
7740 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7741 && lookup_attribute ("dllexport", attributes))
7742 error ("%qD was declared %qs which implies default visibility",
7743 decl, "dllexport");
7744 }
4a2849cb 7745
7746 DECL_VISIBILITY (decl) = vis;
b212f378 7747 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7748
4a2849cb 7749 /* Go ahead and attach the attribute to the node as well. This is needed
7750 so we can determine whether we have VISIBILITY_DEFAULT because the
7751 visibility was not specified, or because it was explicitly overridden
7752 from the containing scope. */
9c40570a 7753
f8e93a2e 7754 return NULL_TREE;
7755}
7756
3aa0c315 7757/* Determine the ELF symbol visibility for DECL, which is either a
7758 variable or a function. It is an error to use this function if a
7759 definition of DECL is not available in this translation unit.
7760 Returns true if the final visibility has been determined by this
7761 function; false if the caller is free to make additional
7762 modifications. */
7763
7764bool
7765c_determine_visibility (tree decl)
7766{
1d8fc210 7767 gcc_assert (TREE_CODE (decl) == VAR_DECL
7768 || TREE_CODE (decl) == FUNCTION_DECL);
3aa0c315 7769
7770 /* If the user explicitly specified the visibility with an
7771 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 7772 the processing of the attribute. We check for an explicit
7773 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7774 to distinguish the use of an attribute from the use of a "#pragma
7775 GCC visibility push(...)"; in the latter case we still want other
7776 considerations to be able to overrule the #pragma. */
7777 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7778 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7779 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7780 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 7781 return true;
7782
4a2849cb 7783 /* Set default visibility to whatever the user supplied with
7784 visibility_specified depending on #pragma GCC visibility. */
7785 if (!DECL_VISIBILITY_SPECIFIED (decl))
7786 {
2d9d8740 7787 if (visibility_options.inpragma
7788 || DECL_VISIBILITY (decl) != default_visibility)
7789 {
7790 DECL_VISIBILITY (decl) = default_visibility;
7791 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7792 /* If visibility changed and DECL already has DECL_RTL, ensure
7793 symbol flags are updated. */
7794 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7795 || TREE_CODE (decl) == FUNCTION_DECL)
7796 && DECL_RTL_SET_P (decl))
7797 make_decl_rtl (decl);
7798 }
4a2849cb 7799 }
3aa0c315 7800 return false;
7801}
7802
24dfead4 7803/* Handle an "tls_model" attribute; arguments as in
7804 struct attribute_spec.handler. */
7805
7806static tree
1cae46be 7807handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 7808 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 7809{
1b53eb20 7810 tree id;
24dfead4 7811 tree decl = *node;
1b53eb20 7812 enum tls_model kind;
24dfead4 7813
1b53eb20 7814 *no_add_attrs = true;
7815
c34f8a78 7816 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
24dfead4 7817 {
9b2d6d13 7818 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 7819 return NULL_TREE;
24dfead4 7820 }
24dfead4 7821
1b53eb20 7822 kind = DECL_TLS_MODEL (decl);
7823 id = TREE_VALUE (args);
7824 if (TREE_CODE (id) != STRING_CST)
7825 {
7826 error ("tls_model argument not a string");
7827 return NULL_TREE;
24dfead4 7828 }
7829
1b53eb20 7830 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7831 kind = TLS_MODEL_LOCAL_EXEC;
7832 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7833 kind = TLS_MODEL_INITIAL_EXEC;
7834 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7835 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7836 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7837 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7838 else
7839 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7840
7841 DECL_TLS_MODEL (decl) = kind;
24dfead4 7842 return NULL_TREE;
7843}
7844
f8e93a2e 7845/* Handle a "no_instrument_function" attribute; arguments as in
7846 struct attribute_spec.handler. */
7847
7848static tree
1cae46be 7849handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 7850 tree ARG_UNUSED (args),
7851 int ARG_UNUSED (flags),
09347743 7852 bool *no_add_attrs)
f8e93a2e 7853{
7854 tree decl = *node;
7855
7856 if (TREE_CODE (decl) != FUNCTION_DECL)
7857 {
712d2297 7858 error_at (DECL_SOURCE_LOCATION (decl),
7859 "%qE attribute applies only to functions", name);
f8e93a2e 7860 *no_add_attrs = true;
7861 }
7862 else if (DECL_INITIAL (decl))
7863 {
712d2297 7864 error_at (DECL_SOURCE_LOCATION (decl),
7865 "can%'t set %qE attribute after definition", name);
f8e93a2e 7866 *no_add_attrs = true;
7867 }
7868 else
7869 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7870
7871 return NULL_TREE;
7872}
7873
7874/* Handle a "malloc" attribute; arguments as in
7875 struct attribute_spec.handler. */
7876
7877static tree
9a03a746 7878handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7879 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7880{
a5147fca 7881 if (TREE_CODE (*node) == FUNCTION_DECL
7882 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 7883 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 7884 else
7885 {
9b2d6d13 7886 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7887 *no_add_attrs = true;
7888 }
7889
7890 return NULL_TREE;
7891}
7892
4a29c97c 7893/* Handle a "alloc_size" attribute; arguments as in
7894 struct attribute_spec.handler. */
7895
7896static tree
7897handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7898 int ARG_UNUSED (flags), bool *no_add_attrs)
7899{
2802826e 7900 unsigned arg_count = type_num_arguments (*node);
4a29c97c 7901 for (; args; args = TREE_CHAIN (args))
7902 {
7903 tree position = TREE_VALUE (args);
7904
7905 if (TREE_CODE (position) != INTEGER_CST
48e1416a 7906 || TREE_INT_CST_HIGH (position)
4a29c97c 7907 || TREE_INT_CST_LOW (position) < 1
7908 || TREE_INT_CST_LOW (position) > arg_count )
7909 {
48e1416a 7910 warning (OPT_Wattributes,
4a29c97c 7911 "alloc_size parameter outside range");
7912 *no_add_attrs = true;
7913 return NULL_TREE;
7914 }
7915 }
7916 return NULL_TREE;
7917}
7918
8ce86007 7919/* Handle a "fn spec" attribute; arguments as in
7920 struct attribute_spec.handler. */
7921
7922static tree
7923handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7924 tree args, int ARG_UNUSED (flags),
7925 bool *no_add_attrs ATTRIBUTE_UNUSED)
7926{
7927 gcc_assert (args
7928 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7929 && !TREE_CHAIN (args));
7930 return NULL_TREE;
7931}
7932
26d1c5ff 7933/* Handle a "returns_twice" attribute; arguments as in
7934 struct attribute_spec.handler. */
7935
7936static tree
7937handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7938 int ARG_UNUSED (flags), bool *no_add_attrs)
7939{
7940 if (TREE_CODE (*node) == FUNCTION_DECL)
7941 DECL_IS_RETURNS_TWICE (*node) = 1;
7942 else
7943 {
9b2d6d13 7944 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 7945 *no_add_attrs = true;
7946 }
7947
7948 return NULL_TREE;
7949}
7950
f8e93a2e 7951/* Handle a "no_limit_stack" attribute; arguments as in
7952 struct attribute_spec.handler. */
7953
7954static tree
1cae46be 7955handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 7956 tree ARG_UNUSED (args),
7957 int ARG_UNUSED (flags),
09347743 7958 bool *no_add_attrs)
f8e93a2e 7959{
7960 tree decl = *node;
7961
7962 if (TREE_CODE (decl) != FUNCTION_DECL)
7963 {
712d2297 7964 error_at (DECL_SOURCE_LOCATION (decl),
7965 "%qE attribute applies only to functions", name);
f8e93a2e 7966 *no_add_attrs = true;
7967 }
7968 else if (DECL_INITIAL (decl))
7969 {
712d2297 7970 error_at (DECL_SOURCE_LOCATION (decl),
7971 "can%'t set %qE attribute after definition", name);
f8e93a2e 7972 *no_add_attrs = true;
7973 }
7974 else
7975 DECL_NO_LIMIT_STACK (decl) = 1;
7976
7977 return NULL_TREE;
7978}
7979
7980/* Handle a "pure" attribute; arguments as in
7981 struct attribute_spec.handler. */
7982
7983static tree
9a03a746 7984handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7985 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7986{
7987 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 7988 DECL_PURE_P (*node) = 1;
f8e93a2e 7989 /* ??? TODO: Support types. */
7990 else
7991 {
9b2d6d13 7992 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7993 *no_add_attrs = true;
7994 }
7995
7996 return NULL_TREE;
7997}
7998
4c0315d0 7999/* Digest an attribute list destined for a transactional memory statement.
8000 ALLOWED is the set of attributes that are allowed for this statement;
8001 return the attribute we parsed. Multiple attributes are never allowed. */
8002
8003int
8004parse_tm_stmt_attr (tree attrs, int allowed)
8005{
8006 tree a_seen = NULL;
8007 int m_seen = 0;
8008
8009 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8010 {
8011 tree a = TREE_PURPOSE (attrs);
8012 int m = 0;
8013
8014 if (is_attribute_p ("outer", a))
8015 m = TM_STMT_ATTR_OUTER;
8016
8017 if ((m & allowed) == 0)
8018 {
8019 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8020 continue;
8021 }
8022
8023 if (m_seen == 0)
8024 {
8025 a_seen = a;
8026 m_seen = m;
8027 }
8028 else if (m_seen == m)
8029 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8030 else
8031 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8032 }
8033
8034 return m_seen;
8035}
8036
8037/* Transform a TM attribute name into a maskable integer and back.
8038 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8039 to how the lack of an attribute is treated. */
8040
8041int
8042tm_attr_to_mask (tree attr)
8043{
8044 if (attr == NULL)
8045 return 0;
8046 if (is_attribute_p ("transaction_safe", attr))
8047 return TM_ATTR_SAFE;
8048 if (is_attribute_p ("transaction_callable", attr))
8049 return TM_ATTR_CALLABLE;
8050 if (is_attribute_p ("transaction_pure", attr))
8051 return TM_ATTR_PURE;
8052 if (is_attribute_p ("transaction_unsafe", attr))
8053 return TM_ATTR_IRREVOCABLE;
8054 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8055 return TM_ATTR_MAY_CANCEL_OUTER;
8056 return 0;
8057}
8058
8059tree
8060tm_mask_to_attr (int mask)
8061{
8062 const char *str;
8063 switch (mask)
8064 {
8065 case TM_ATTR_SAFE:
8066 str = "transaction_safe";
8067 break;
8068 case TM_ATTR_CALLABLE:
8069 str = "transaction_callable";
8070 break;
8071 case TM_ATTR_PURE:
8072 str = "transaction_pure";
8073 break;
8074 case TM_ATTR_IRREVOCABLE:
8075 str = "transaction_unsafe";
8076 break;
8077 case TM_ATTR_MAY_CANCEL_OUTER:
8078 str = "transaction_may_cancel_outer";
8079 break;
8080 default:
8081 gcc_unreachable ();
8082 }
8083 return get_identifier (str);
8084}
8085
8086/* Return the first TM attribute seen in LIST. */
8087
8088tree
8089find_tm_attribute (tree list)
8090{
8091 for (; list ; list = TREE_CHAIN (list))
8092 {
8093 tree name = TREE_PURPOSE (list);
8094 if (tm_attr_to_mask (name) != 0)
8095 return name;
8096 }
8097 return NULL_TREE;
8098}
8099
8100/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8101 Here we accept only function types, and verify that none of the other
8102 function TM attributes are also applied. */
8103/* ??? We need to accept class types for C++, but not C. This greatly
8104 complicates this function, since we can no longer rely on the extra
8105 processing given by function_type_required. */
8106
8107static tree
8108handle_tm_attribute (tree *node, tree name, tree args,
8109 int flags, bool *no_add_attrs)
8110{
8111 /* Only one path adds the attribute; others don't. */
8112 *no_add_attrs = true;
8113
8114 switch (TREE_CODE (*node))
8115 {
8116 case RECORD_TYPE:
8117 case UNION_TYPE:
8118 /* Only tm_callable and tm_safe apply to classes. */
8119 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8120 goto ignored;
8121 /* FALLTHRU */
8122
8123 case FUNCTION_TYPE:
8124 case METHOD_TYPE:
8125 {
8126 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8127 if (old_name == name)
8128 ;
8129 else if (old_name != NULL_TREE)
8130 error ("type was previously declared %qE", old_name);
8131 else
8132 *no_add_attrs = false;
8133 }
8134 break;
8135
8136 case POINTER_TYPE:
8137 {
8138 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8139 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8140 {
8141 tree fn_tmp = TREE_TYPE (*node);
8142 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8143 *node = build_pointer_type (fn_tmp);
8144 break;
8145 }
8146 }
8147 /* FALLTHRU */
8148
8149 default:
8150 /* If a function is next, pass it on to be tried next. */
8151 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8152 return tree_cons (name, args, NULL);
8153
8154 ignored:
8155 warning (OPT_Wattributes, "%qE attribute ignored", name);
8156 break;
8157 }
8158
8159 return NULL_TREE;
8160}
8161
8162/* Handle the TM_WRAP attribute; arguments as in
8163 struct attribute_spec.handler. */
8164
8165static tree
8166handle_tm_wrap_attribute (tree *node, tree name, tree args,
8167 int ARG_UNUSED (flags), bool *no_add_attrs)
8168{
8169 tree decl = *node;
8170
8171 /* We don't need the attribute even on success, since we
8172 record the entry in an external table. */
8173 *no_add_attrs = true;
8174
8175 if (TREE_CODE (decl) != FUNCTION_DECL)
8176 warning (OPT_Wattributes, "%qE attribute ignored", name);
8177 else
8178 {
8179 tree wrap_decl = TREE_VALUE (args);
8180 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8181 && TREE_CODE (wrap_decl) != VAR_DECL
8182 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8183 error ("%qE argument not an identifier", name);
8184 else
8185 {
8186 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8187 wrap_decl = lookup_name (wrap_decl);
8188 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8189 {
8190 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8191 TREE_TYPE (wrap_decl)))
8192 record_tm_replacement (wrap_decl, decl);
8193 else
8194 error ("%qD is not compatible with %qD", wrap_decl, decl);
8195 }
8196 else
8197 error ("transaction_wrap argument is not a function");
8198 }
8199 }
8200
8201 return NULL_TREE;
8202}
8203
8204/* Ignore the given attribute. Used when this attribute may be usefully
8205 overridden by the target, but is not used generically. */
8206
8207static tree
8208ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8209 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8210 bool *no_add_attrs)
8211{
8212 *no_add_attrs = true;
8213 return NULL_TREE;
8214}
8215
fc09b200 8216/* Handle a "no vops" attribute; arguments as in
8217 struct attribute_spec.handler. */
8218
8219static tree
8220handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8221 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8222 bool *ARG_UNUSED (no_add_attrs))
8223{
8224 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8225 DECL_IS_NOVOPS (*node) = 1;
8226 return NULL_TREE;
8227}
8228
f8e93a2e 8229/* Handle a "deprecated" attribute; arguments as in
8230 struct attribute_spec.handler. */
1cae46be 8231
f8e93a2e 8232static tree
1cae46be 8233handle_deprecated_attribute (tree *node, tree name,
45c4e798 8234 tree args, int flags,
09347743 8235 bool *no_add_attrs)
f8e93a2e 8236{
8237 tree type = NULL_TREE;
8238 int warn = 0;
782858b8 8239 tree what = NULL_TREE;
1cae46be 8240
45c4e798 8241 if (!args)
8242 *no_add_attrs = true;
8243 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8244 {
8245 error ("deprecated message is not a string");
8246 *no_add_attrs = true;
8247 }
8248
f8e93a2e 8249 if (DECL_P (*node))
8250 {
8251 tree decl = *node;
8252 type = TREE_TYPE (decl);
1cae46be 8253
f8e93a2e 8254 if (TREE_CODE (decl) == TYPE_DECL
8255 || TREE_CODE (decl) == PARM_DECL
8256 || TREE_CODE (decl) == VAR_DECL
8257 || TREE_CODE (decl) == FUNCTION_DECL
40c8d1dd 8258 || TREE_CODE (decl) == FIELD_DECL
8259 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 8260 TREE_DEPRECATED (decl) = 1;
8261 else
8262 warn = 1;
8263 }
8264 else if (TYPE_P (*node))
8265 {
8266 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 8267 *node = build_variant_type_copy (*node);
f8e93a2e 8268 TREE_DEPRECATED (*node) = 1;
8269 type = *node;
8270 }
8271 else
8272 warn = 1;
1cae46be 8273
f8e93a2e 8274 if (warn)
8275 {
8276 *no_add_attrs = true;
8277 if (type && TYPE_NAME (type))
8278 {
8279 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 8280 what = TYPE_NAME (*node);
f8e93a2e 8281 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8282 && DECL_NAME (TYPE_NAME (type)))
782858b8 8283 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 8284 }
8285 if (what)
9b2d6d13 8286 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 8287 else
9b2d6d13 8288 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8289 }
8290
8291 return NULL_TREE;
8292}
8293
f8e93a2e 8294/* Handle a "vector_size" attribute; arguments as in
8295 struct attribute_spec.handler. */
8296
8297static tree
1cae46be 8298handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 8299 int ARG_UNUSED (flags),
09347743 8300 bool *no_add_attrs)
f8e93a2e 8301{
8302 unsigned HOST_WIDE_INT vecsize, nunits;
83e2a11b 8303 enum machine_mode orig_mode;
4917c376 8304 tree type = *node, new_type, size;
f8e93a2e 8305
8306 *no_add_attrs = true;
8307
4917c376 8308 size = TREE_VALUE (args);
4917c376 8309
84166705 8310 if (!host_integerp (size, 1))
f8e93a2e 8311 {
9b2d6d13 8312 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8313 return NULL_TREE;
8314 }
8315
8316 /* Get the vector size (in bytes). */
4917c376 8317 vecsize = tree_low_cst (size, 1);
f8e93a2e 8318
8319 /* We need to provide for vector pointers, vector arrays, and
8320 functions returning vectors. For example:
8321
8322 __attribute__((vector_size(16))) short *foo;
8323
8324 In this case, the mode is SI, but the type being modified is
8325 HI, so we need to look further. */
8326
8327 while (POINTER_TYPE_P (type)
8328 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 8329 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 8330 || TREE_CODE (type) == ARRAY_TYPE
8331 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 8332 type = TREE_TYPE (type);
8333
8334 /* Get the mode of the type being modified. */
8335 orig_mode = TYPE_MODE (type);
8336
2cb0e5d9 8337 if ((!INTEGRAL_TYPE_P (type)
8338 && !SCALAR_FLOAT_TYPE_P (type)
8339 && !FIXED_POINT_TYPE_P (type))
cee7491d 8340 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 8341 && GET_MODE_CLASS (orig_mode) != MODE_INT
8342 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
7ec31215 8343 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8344 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 8345 {
782858b8 8346 error ("invalid vector type for attribute %qE", name);
f8e93a2e 8347 return NULL_TREE;
8348 }
8349
39cc3e6d 8350 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8351 {
8352 error ("vector size not an integral multiple of component size");
8353 return NULL;
8354 }
8355
8356 if (vecsize == 0)
8357 {
8358 error ("zero vector size");
8359 return NULL;
8360 }
8361
f8e93a2e 8362 /* Calculate how many units fit in the vector. */
8363 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
83e2a11b 8364 if (nunits & (nunits - 1))
f8e93a2e 8365 {
83e2a11b 8366 error ("number of components of the vector not a power of two");
f8e93a2e 8367 return NULL_TREE;
8368 }
8369
83e2a11b 8370 new_type = build_vector_type (type, nunits);
f8e93a2e 8371
8372 /* Build back pointers if needed. */
d991e6e8 8373 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 8374
8375 return NULL_TREE;
8376}
8377
dbf6c367 8378/* Handle the "nonnull" attribute. */
8379static tree
9a03a746 8380handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8381 tree args, int ARG_UNUSED (flags),
09347743 8382 bool *no_add_attrs)
dbf6c367 8383{
8384 tree type = *node;
8385 unsigned HOST_WIDE_INT attr_arg_num;
8386
8387 /* If no arguments are specified, all pointer arguments should be
d716ce75 8388 non-null. Verify a full prototype is given so that the arguments
dbf6c367 8389 will have the correct types when we actually check them later. */
84166705 8390 if (!args)
dbf6c367 8391 {
a36cf284 8392 if (!prototype_p (type))
dbf6c367 8393 {
8394 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 8395 *no_add_attrs = true;
dbf6c367 8396 }
8397 return NULL_TREE;
8398 }
8399
8400 /* Argument list specified. Verify that each argument number references
8401 a pointer argument. */
8402 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8403 {
4ee9c684 8404 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 8405
84166705 8406 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
dbf6c367 8407 {
07e3a3d2 8408 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 8409 (unsigned long) attr_arg_num);
8410 *no_add_attrs = true;
8411 return NULL_TREE;
8412 }
8413
d0af78c5 8414 if (prototype_p (type))
dbf6c367 8415 {
d0af78c5 8416 function_args_iterator iter;
8417 tree argument;
8418
8419 function_args_iter_init (&iter, type);
8420 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 8421 {
d0af78c5 8422 argument = function_args_iter_cond (&iter);
8423 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 8424 break;
dbf6c367 8425 }
8426
84166705 8427 if (!argument
d0af78c5 8428 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 8429 {
07e3a3d2 8430 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 8431 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8432 *no_add_attrs = true;
8433 return NULL_TREE;
8434 }
8435
d0af78c5 8436 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 8437 {
07e3a3d2 8438 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 8439 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8440 *no_add_attrs = true;
8441 return NULL_TREE;
8442 }
8443 }
8444 }
8445
8446 return NULL_TREE;
8447}
8448
8449/* Check the argument list of a function call for null in argument slots
d01f58f9 8450 that are marked as requiring a non-null pointer argument. The NARGS
8451 arguments are passed in the array ARGARRAY.
8452*/
dbf6c367 8453
8454static void
d01f58f9 8455check_function_nonnull (tree attrs, int nargs, tree *argarray)
dbf6c367 8456{
9ca77b08 8457 tree a;
d01f58f9 8458 int i;
dbf6c367 8459
9ca77b08 8460 attrs = lookup_attribute ("nonnull", attrs);
8461 if (attrs == NULL_TREE)
8462 return;
8463
8464 a = attrs;
8465 /* See if any of the nonnull attributes has no arguments. If so,
8466 then every pointer argument is checked (in which case the check
8467 for pointer type is done in check_nonnull_arg). */
8468 if (TREE_VALUE (a) != NULL_TREE)
8469 do
8470 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8471 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8472
8473 if (a != NULL_TREE)
8474 for (i = 0; i < nargs; i++)
8475 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8476 i + 1);
8477 else
dbf6c367 8478 {
9ca77b08 8479 /* Walk the argument list. If we encounter an argument number we
8480 should check for non-null, do it. */
8481 for (i = 0; i < nargs; i++)
dbf6c367 8482 {
9ca77b08 8483 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 8484 {
9ca77b08 8485 a = lookup_attribute ("nonnull", a);
8486 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8487 break;
4ee9c684 8488 }
9ca77b08 8489
8490 if (a != NULL_TREE)
8491 check_function_arguments_recurse (check_nonnull_arg, NULL,
8492 argarray[i], i + 1);
dbf6c367 8493 }
8494 }
8495}
8496
50ca527f 8497/* Check that the Nth argument of a function call (counting backwards
d01f58f9 8498 from the end) is a (pointer)0. The NARGS arguments are passed in the
8499 array ARGARRAY. */
bf6c8de0 8500
8501static void
774e9d58 8502check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 8503{
774e9d58 8504 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 8505
8506 if (attr)
8507 {
d01f58f9 8508 int len = 0;
8509 int pos = 0;
8510 tree sentinel;
774e9d58 8511 function_args_iterator iter;
8512 tree t;
a0c938f0 8513
d01f58f9 8514 /* Skip over the named arguments. */
774e9d58 8515 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 8516 {
774e9d58 8517 if (len == nargs)
8518 break;
d01f58f9 8519 len++;
8520 }
50ca527f 8521
d01f58f9 8522 if (TREE_VALUE (attr))
8523 {
8524 tree p = TREE_VALUE (TREE_VALUE (attr));
8525 pos = TREE_INT_CST_LOW (p);
8526 }
50ca527f 8527
d01f58f9 8528 /* The sentinel must be one of the varargs, i.e.
8529 in position >= the number of fixed arguments. */
8530 if ((nargs - 1 - pos) < len)
8531 {
77a357e3 8532 warning (OPT_Wformat_,
d01f58f9 8533 "not enough variable arguments to fit a sentinel");
8534 return;
bf6c8de0 8535 }
d01f58f9 8536
8537 /* Validate the sentinel. */
8538 sentinel = argarray[nargs - 1 - pos];
8539 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8540 || !integer_zerop (sentinel))
8541 /* Although __null (in C++) is only an integer we allow it
8542 nevertheless, as we are guaranteed that it's exactly
8543 as wide as a pointer, and we don't want to force
8544 users to cast the NULL they have written there.
8545 We warn with -Wstrict-null-sentinel, though. */
8546 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 8547 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 8548 }
8549}
8550
dbf6c367 8551/* Helper for check_function_nonnull; given a list of operands which
8552 must be non-null in ARGS, determine if operand PARAM_NUM should be
8553 checked. */
8554
8555static bool
1cae46be 8556nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 8557{
4ee9c684 8558 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 8559
8560 for (; args; args = TREE_CHAIN (args))
8561 {
231bd014 8562 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8563
8564 gcc_assert (found);
dbf6c367 8565
8566 if (arg_num == param_num)
8567 return true;
8568 }
8569 return false;
8570}
8571
8572/* Check that the function argument PARAM (which is operand number
8573 PARAM_NUM) is non-null. This is called by check_function_nonnull
8574 via check_function_arguments_recurse. */
8575
8576static void
9a03a746 8577check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
1cae46be 8578 unsigned HOST_WIDE_INT param_num)
dbf6c367 8579{
8580 /* Just skip checking the argument if it's not a pointer. This can
8581 happen if the "nonnull" attribute was given without an operand
8582 list (which means to check every pointer argument). */
8583
8584 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8585 return;
8586
8587 if (integer_zerop (param))
155b601b 8588 warning (OPT_Wnonnull, "null argument where non-null required "
8589 "(argument %lu)", (unsigned long) param_num);
dbf6c367 8590}
8591
8592/* Helper for nonnull attribute handling; fetch the operand number
8593 from the attribute argument list. */
8594
8595static bool
1cae46be 8596get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 8597{
67409385 8598 /* Verify the arg number is a constant. */
dbf6c367 8599 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8600 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8601 return false;
8602
8603 *valp = TREE_INT_CST_LOW (arg_num_expr);
8604 return true;
8605}
fa987697 8606
8607/* Handle a "nothrow" attribute; arguments as in
8608 struct attribute_spec.handler. */
8609
8610static tree
9a03a746 8611handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8612 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 8613{
8614 if (TREE_CODE (*node) == FUNCTION_DECL)
8615 TREE_NOTHROW (*node) = 1;
8616 /* ??? TODO: Support types. */
8617 else
8618 {
9b2d6d13 8619 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 8620 *no_add_attrs = true;
8621 }
8622
8623 return NULL_TREE;
8624}
7acb29a3 8625
8626/* Handle a "cleanup" attribute; arguments as in
8627 struct attribute_spec.handler. */
8628
8629static tree
1cae46be 8630handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 8631 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 8632{
8633 tree decl = *node;
8634 tree cleanup_id, cleanup_decl;
8635
8636 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8637 for global destructors in C++. This requires infrastructure that
8638 we don't have generically at the moment. It's also not a feature
8639 we'd be missing too much, since we do have attribute constructor. */
8640 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8641 {
9b2d6d13 8642 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 8643 *no_add_attrs = true;
8644 return NULL_TREE;
8645 }
8646
8647 /* Verify that the argument is a function in scope. */
8648 /* ??? We could support pointers to functions here as well, if
8649 that was considered desirable. */
8650 cleanup_id = TREE_VALUE (args);
8651 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8652 {
07e3a3d2 8653 error ("cleanup argument not an identifier");
7acb29a3 8654 *no_add_attrs = true;
8655 return NULL_TREE;
8656 }
d1c41717 8657 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 8658 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8659 {
07e3a3d2 8660 error ("cleanup argument not a function");
7acb29a3 8661 *no_add_attrs = true;
8662 return NULL_TREE;
8663 }
8664
1cae46be 8665 /* That the function has proper type is checked with the
7acb29a3 8666 eventual call to build_function_call. */
8667
8668 return NULL_TREE;
8669}
8a8cdb8d 8670
8671/* Handle a "warn_unused_result" attribute. No special handling. */
8672
8673static tree
8674handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 8675 tree ARG_UNUSED (args),
8676 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 8677{
8678 /* Ignore the attribute for functions not returning any value. */
8679 if (VOID_TYPE_P (TREE_TYPE (*node)))
8680 {
9b2d6d13 8681 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 8682 *no_add_attrs = true;
8683 }
8684
8685 return NULL_TREE;
8686}
bf6c8de0 8687
8688/* Handle a "sentinel" attribute. */
8689
8690static tree
50ca527f 8691handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 8692 int ARG_UNUSED (flags), bool *no_add_attrs)
8693{
a36cf284 8694 if (!prototype_p (*node))
bf6c8de0 8695 {
9b2d6d13 8696 warning (OPT_Wattributes,
8697 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 8698 *no_add_attrs = true;
bf6c8de0 8699 }
50ca527f 8700 else
8701 {
c33080b9 8702 if (!stdarg_p (*node))
a0c938f0 8703 {
9b2d6d13 8704 warning (OPT_Wattributes,
8705 "%qE attribute only applies to variadic functions", name);
50ca527f 8706 *no_add_attrs = true;
8707 }
8708 }
a0c938f0 8709
50ca527f 8710 if (args)
bf6c8de0 8711 {
50ca527f 8712 tree position = TREE_VALUE (args);
8713
50ca527f 8714 if (TREE_CODE (position) != INTEGER_CST)
a0c938f0 8715 {
48e1416a 8716 warning (OPT_Wattributes,
01b54db5 8717 "requested position is not an integer constant");
50ca527f 8718 *no_add_attrs = true;
8719 }
8720 else
a0c938f0 8721 {
50ca527f 8722 if (tree_int_cst_lt (position, integer_zero_node))
8723 {
01b54db5 8724 warning (OPT_Wattributes,
8725 "requested position is less than zero");
50ca527f 8726 *no_add_attrs = true;
8727 }
8728 }
bf6c8de0 8729 }
a0c938f0 8730
bf6c8de0 8731 return NULL_TREE;
8732}
b5c26b42 8733
8734/* Handle a "type_generic" attribute. */
8735
8736static tree
8737handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8738 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8739 bool * ARG_UNUSED (no_add_attrs))
8740{
19fbe3a4 8741 /* Ensure we have a function type. */
8742 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 8743
19fbe3a4 8744 /* Ensure we have a variadic function. */
c33080b9 8745 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 8746
8747 return NULL_TREE;
8748}
46f8e3b0 8749
24470055 8750/* Handle a "target" attribute. */
46f8e3b0 8751
8752static tree
24470055 8753handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 8754 bool *no_add_attrs)
8755{
8756 /* Ensure we have a function type. */
8757 if (TREE_CODE (*node) != FUNCTION_DECL)
8758 {
8759 warning (OPT_Wattributes, "%qE attribute ignored", name);
8760 *no_add_attrs = true;
8761 }
46f8e3b0 8762 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 8763 flags))
46f8e3b0 8764 *no_add_attrs = true;
8765
8766 return NULL_TREE;
8767}
8768
8769/* Arguments being collected for optimization. */
8770typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 8771static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 8772
8773
8774/* Inner function to convert a TREE_LIST to argv string to parse the optimize
8775 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8776 false for #pragma GCC optimize. */
8777
8778bool
8779parse_optimize_options (tree args, bool attr_p)
8780{
8781 bool ret = true;
8782 unsigned opt_argc;
8783 unsigned i;
2becf397 8784 int saved_flag_strict_aliasing;
46f8e3b0 8785 const char **opt_argv;
615ef0bb 8786 struct cl_decoded_option *decoded_options;
8787 unsigned int decoded_options_count;
46f8e3b0 8788 tree ap;
8789
8790 /* Build up argv vector. Just in case the string is stored away, use garbage
8791 collected strings. */
f1f41a6c 8792 vec_safe_truncate (optimize_args, 0);
8793 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 8794
8795 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8796 {
8797 tree value = TREE_VALUE (ap);
8798
8799 if (TREE_CODE (value) == INTEGER_CST)
8800 {
8801 char buffer[20];
8802 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 8803 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 8804 }
8805
8806 else if (TREE_CODE (value) == STRING_CST)
8807 {
8808 /* Split string into multiple substrings. */
8809 size_t len = TREE_STRING_LENGTH (value);
8810 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8811 char *end = p + len;
8812 char *comma;
8813 char *next_p = p;
8814
8815 while (next_p != NULL)
8816 {
8817 size_t len2;
8818 char *q, *r;
8819
8820 p = next_p;
8821 comma = strchr (p, ',');
8822 if (comma)
8823 {
8824 len2 = comma - p;
8825 *comma = '\0';
8826 next_p = comma+1;
8827 }
8828 else
8829 {
8830 len2 = end - p;
8831 next_p = NULL;
8832 }
8833
ba72912a 8834 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 8835
8836 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
8837 options. */
8838 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
8839 {
8840 ret = false;
8841 if (attr_p)
8842 warning (OPT_Wattributes,
ebd7c4c1 8843 "bad option %s to optimize attribute", p);
46f8e3b0 8844 else
8845 warning (OPT_Wpragmas,
e44b0a1f 8846 "bad option %s to pragma attribute", p);
46f8e3b0 8847 continue;
8848 }
8849
8850 if (*p != '-')
8851 {
8852 *r++ = '-';
8853
8854 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
8855 itself is -Os, and any other switch begins with a -f. */
8856 if ((*p >= '0' && *p <= '9')
8857 || (p[0] == 's' && p[1] == '\0'))
8858 *r++ = 'O';
8859 else if (*p != 'O')
8860 *r++ = 'f';
8861 }
8862
8863 memcpy (r, p, len2);
8864 r[len2] = '\0';
f1f41a6c 8865 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 8866 }
8867
8868 }
8869 }
8870
f1f41a6c 8871 opt_argc = optimize_args->length ();
46f8e3b0 8872 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
8873
8874 for (i = 1; i < opt_argc; i++)
f1f41a6c 8875 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 8876
2becf397 8877 saved_flag_strict_aliasing = flag_strict_aliasing;
8878
46f8e3b0 8879 /* Now parse the options. */
f3f006ad 8880 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
8881 &decoded_options,
8882 &decoded_options_count);
8883 decode_options (&global_options, &global_options_set,
3c6c0e40 8884 decoded_options, decoded_options_count,
8885 input_location, global_dc);
46f8e3b0 8886
4bec06b3 8887 targetm.override_options_after_change();
8888
2becf397 8889 /* Don't allow changing -fstrict-aliasing. */
8890 flag_strict_aliasing = saved_flag_strict_aliasing;
8891
f1f41a6c 8892 optimize_args->truncate (0);
46f8e3b0 8893 return ret;
8894}
8895
8896/* For handling "optimize" attribute. arguments as in
8897 struct attribute_spec.handler. */
8898
8899static tree
8900handle_optimize_attribute (tree *node, tree name, tree args,
8901 int ARG_UNUSED (flags), bool *no_add_attrs)
8902{
8903 /* Ensure we have a function type. */
8904 if (TREE_CODE (*node) != FUNCTION_DECL)
8905 {
8906 warning (OPT_Wattributes, "%qE attribute ignored", name);
8907 *no_add_attrs = true;
8908 }
8909 else
8910 {
8911 struct cl_optimization cur_opts;
8912 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
8913
8914 /* Save current options. */
2c5d2e39 8915 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 8916
8917 /* If we previously had some optimization options, use them as the
8918 default. */
8919 if (old_opts)
2c5d2e39 8920 cl_optimization_restore (&global_options,
8921 TREE_OPTIMIZATION (old_opts));
46f8e3b0 8922
8923 /* Parse options, and update the vector. */
8924 parse_optimize_options (args, true);
8925 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
8926 = build_optimization_node ();
8927
8928 /* Restore current options. */
2c5d2e39 8929 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 8930 }
8931
8932 return NULL_TREE;
8933}
48b14f50 8934
8935/* Handle a "no_split_stack" attribute. */
8936
8937static tree
8938handle_no_split_stack_attribute (tree *node, tree name,
8939 tree ARG_UNUSED (args),
8940 int ARG_UNUSED (flags),
8941 bool *no_add_attrs)
8942{
8943 tree decl = *node;
8944
8945 if (TREE_CODE (decl) != FUNCTION_DECL)
8946 {
8947 error_at (DECL_SOURCE_LOCATION (decl),
8948 "%qE attribute applies only to functions", name);
8949 *no_add_attrs = true;
8950 }
8951 else if (DECL_INITIAL (decl))
8952 {
8953 error_at (DECL_SOURCE_LOCATION (decl),
8954 "can%'t set %qE attribute after definition", name);
8955 *no_add_attrs = true;
8956 }
8957
8958 return NULL_TREE;
8959}
dbf6c367 8960\f
774e9d58 8961/* Check for valid arguments being passed to a function with FNTYPE.
8962 There are NARGS arguments in the array ARGARRAY. */
dbf6c367 8963void
774e9d58 8964check_function_arguments (const_tree fntype, int nargs, tree *argarray)
dbf6c367 8965{
8966 /* Check for null being passed in a pointer argument that must be
8967 non-null. We also need to do this if format checking is enabled. */
8968
8969 if (warn_nonnull)
774e9d58 8970 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 8971
8972 /* Check for errors in format strings. */
8973
068bea1e 8974 if (warn_format || warn_suggest_attribute_format)
774e9d58 8975 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 8976
8977 if (warn_format)
774e9d58 8978 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 8979}
8980
8981/* Generic argument checking recursion routine. PARAM is the argument to
8982 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
8983 once the argument is resolved. CTX is context for the callback. */
8984void
1cae46be 8985check_function_arguments_recurse (void (*callback)
8986 (void *, tree, unsigned HOST_WIDE_INT),
8987 void *ctx, tree param,
8988 unsigned HOST_WIDE_INT param_num)
dbf6c367 8989{
72dd6141 8990 if (CONVERT_EXPR_P (param)
c44afe23 8991 && (TYPE_PRECISION (TREE_TYPE (param))
8992 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 8993 {
8994 /* Strip coercion. */
8995 check_function_arguments_recurse (callback, ctx,
4ee9c684 8996 TREE_OPERAND (param, 0), param_num);
dbf6c367 8997 return;
8998 }
8999
9000 if (TREE_CODE (param) == CALL_EXPR)
9001 {
c2f47e15 9002 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 9003 tree attrs;
9004 bool found_format_arg = false;
9005
9006 /* See if this is a call to a known internationalization function
9007 that modifies a format arg. Such a function may have multiple
9008 format_arg attributes (for example, ngettext). */
9009
9010 for (attrs = TYPE_ATTRIBUTES (type);
9011 attrs;
9012 attrs = TREE_CHAIN (attrs))
9013 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9014 {
c2f47e15 9015 tree inner_arg;
dbf6c367 9016 tree format_num_expr;
9017 int format_num;
9018 int i;
c2f47e15 9019 call_expr_arg_iterator iter;
dbf6c367 9020
9021 /* Extract the argument number, which was previously checked
9022 to be valid. */
9023 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
dbf6c367 9024
231bd014 9025 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9026 && !TREE_INT_CST_HIGH (format_num_expr));
dbf6c367 9027
9028 format_num = TREE_INT_CST_LOW (format_num_expr);
9029
c2f47e15 9030 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9031 inner_arg != 0;
9032 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 9033 if (i == format_num)
9034 {
9035 check_function_arguments_recurse (callback, ctx,
c2f47e15 9036 inner_arg, param_num);
dbf6c367 9037 found_format_arg = true;
9038 break;
9039 }
9040 }
9041
9042 /* If we found a format_arg attribute and did a recursive check,
9043 we are done with checking this argument. Otherwise, we continue
9044 and this will be considered a non-literal. */
9045 if (found_format_arg)
9046 return;
9047 }
9048
9049 if (TREE_CODE (param) == COND_EXPR)
9050 {
9051 /* Check both halves of the conditional expression. */
9052 check_function_arguments_recurse (callback, ctx,
4ee9c684 9053 TREE_OPERAND (param, 1), param_num);
dbf6c367 9054 check_function_arguments_recurse (callback, ctx,
4ee9c684 9055 TREE_OPERAND (param, 2), param_num);
dbf6c367 9056 return;
9057 }
9058
9059 (*callback) (ctx, param, param_num);
9060}
1f3233d1 9061
60cce472 9062/* Checks for a builtin function FNDECL that the number of arguments
9063 NARGS against the required number REQUIRED and issues an error if
9064 there is a mismatch. Returns true if the number of arguments is
9065 correct, otherwise false. */
d43cee80 9066
9067static bool
60cce472 9068builtin_function_validate_nargs (tree fndecl, int nargs, int required)
d43cee80 9069{
9070 if (nargs < required)
9071 {
60cce472 9072 error_at (input_location,
9073 "not enough arguments to function %qE", fndecl);
d43cee80 9074 return false;
9075 }
9076 else if (nargs > required)
9077 {
60cce472 9078 error_at (input_location,
9079 "too many arguments to function %qE", fndecl);
d43cee80 9080 return false;
9081 }
9082 return true;
9083}
9084
9085/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9086 Returns false if there was an error, otherwise true. */
9087
9088bool
9089check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9090{
9091 if (!DECL_BUILT_IN (fndecl)
9092 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9093 return true;
9094
9095 switch (DECL_FUNCTION_CODE (fndecl))
9096 {
9097 case BUILT_IN_CONSTANT_P:
60cce472 9098 return builtin_function_validate_nargs (fndecl, nargs, 1);
d43cee80 9099
9100 case BUILT_IN_ISFINITE:
9101 case BUILT_IN_ISINF:
c319d56a 9102 case BUILT_IN_ISINF_SIGN:
d43cee80 9103 case BUILT_IN_ISNAN:
9104 case BUILT_IN_ISNORMAL:
60cce472 9105 if (builtin_function_validate_nargs (fndecl, nargs, 1))
d43cee80 9106 {
9107 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9108 {
9109 error ("non-floating-point argument in call to "
9110 "function %qE", fndecl);
9111 return false;
9112 }
9113 return true;
9114 }
9115 return false;
9116
9117 case BUILT_IN_ISGREATER:
9118 case BUILT_IN_ISGREATEREQUAL:
9119 case BUILT_IN_ISLESS:
9120 case BUILT_IN_ISLESSEQUAL:
9121 case BUILT_IN_ISLESSGREATER:
9122 case BUILT_IN_ISUNORDERED:
60cce472 9123 if (builtin_function_validate_nargs (fndecl, nargs, 2))
d43cee80 9124 {
9125 enum tree_code code0, code1;
9126 code0 = TREE_CODE (TREE_TYPE (args[0]));
9127 code1 = TREE_CODE (TREE_TYPE (args[1]));
9128 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9129 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9130 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9131 {
9132 error ("non-floating-point arguments in call to "
9133 "function %qE", fndecl);
9134 return false;
9135 }
9136 return true;
9137 }
9138 return false;
9139
19fbe3a4 9140 case BUILT_IN_FPCLASSIFY:
60cce472 9141 if (builtin_function_validate_nargs (fndecl, nargs, 6))
19fbe3a4 9142 {
9143 unsigned i;
48e1416a 9144
19fbe3a4 9145 for (i=0; i<5; i++)
9146 if (TREE_CODE (args[i]) != INTEGER_CST)
9147 {
9148 error ("non-const integer argument %u in call to function %qE",
9149 i+1, fndecl);
9150 return false;
9151 }
9152
9153 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9154 {
9155 error ("non-floating-point argument in call to function %qE",
9156 fndecl);
9157 return false;
9158 }
9159 return true;
9160 }
9161 return false;
9162
fca0886c 9163 case BUILT_IN_ASSUME_ALIGNED:
9164 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9165 {
9166 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9167 {
9168 error ("non-integer argument 3 in call to function %qE", fndecl);
9169 return false;
9170 }
9171 return true;
9172 }
9173 return false;
9174
d43cee80 9175 default:
9176 return true;
9177 }
9178}
9179
860251be 9180/* Function to help qsort sort FIELD_DECLs by name order. */
9181
9182int
9183field_decl_cmp (const void *x_p, const void *y_p)
9184{
4fd61bc6 9185 const tree *const x = (const tree *const) x_p;
9186 const tree *const y = (const tree *const) y_p;
9187
860251be 9188 if (DECL_NAME (*x) == DECL_NAME (*y))
9189 /* A nontype is "greater" than a type. */
9190 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9191 if (DECL_NAME (*x) == NULL_TREE)
9192 return -1;
9193 if (DECL_NAME (*y) == NULL_TREE)
9194 return 1;
9195 if (DECL_NAME (*x) < DECL_NAME (*y))
9196 return -1;
9197 return 1;
9198}
9199
9200static struct {
9201 gt_pointer_operator new_value;
9202 void *cookie;
9203} resort_data;
9204
9205/* This routine compares two fields like field_decl_cmp but using the
9206pointer operator in resort_data. */
9207
9208static int
9209resort_field_decl_cmp (const void *x_p, const void *y_p)
9210{
4fd61bc6 9211 const tree *const x = (const tree *const) x_p;
9212 const tree *const y = (const tree *const) y_p;
860251be 9213
9214 if (DECL_NAME (*x) == DECL_NAME (*y))
9215 /* A nontype is "greater" than a type. */
9216 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9217 if (DECL_NAME (*x) == NULL_TREE)
9218 return -1;
9219 if (DECL_NAME (*y) == NULL_TREE)
9220 return 1;
9221 {
9222 tree d1 = DECL_NAME (*x);
9223 tree d2 = DECL_NAME (*y);
9224 resort_data.new_value (&d1, resort_data.cookie);
9225 resort_data.new_value (&d2, resort_data.cookie);
9226 if (d1 < d2)
9227 return -1;
9228 }
9229 return 1;
9230}
9231
9232/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9233
9234void
9235resort_sorted_fields (void *obj,
9a03a746 9236 void * ARG_UNUSED (orig_obj),
4ee9c684 9237 gt_pointer_operator new_value,
9238 void *cookie)
860251be 9239{
9a03a746 9240 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 9241 resort_data.new_value = new_value;
9242 resort_data.cookie = cookie;
9243 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 9244 resort_field_decl_cmp);
860251be 9245}
9246
209c9752 9247/* Subroutine of c_parse_error.
9248 Return the result of concatenating LHS and RHS. RHS is really
9249 a string literal, its first character is indicated by RHS_START and
cfee01e3 9250 RHS_SIZE is its length (including the terminating NUL character).
209c9752 9251
9252 The caller is responsible for deleting the returned pointer. */
9253
9254static char *
9255catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9256{
9257 const int lhs_size = strlen (lhs);
9258 char *result = XNEWVEC (char, lhs_size + rhs_size);
9259 strncpy (result, lhs, lhs_size);
9260 strncpy (result + lhs_size, rhs_start, rhs_size);
9261 return result;
9262}
9263
380c6697 9264/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 9265 TOKEN, which had the associated VALUE. */
9266
9267void
48e1416a 9268c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 9269 tree value, unsigned char token_flags)
92b128ed 9270{
209c9752 9271#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9272
9273 char *message = NULL;
92b128ed 9274
ba99525e 9275 if (token_type == CPP_EOF)
380c6697 9276 message = catenate_messages (gmsgid, " at end of input");
48e1416a 9277 else if (token_type == CPP_CHAR
9278 || token_type == CPP_WCHAR
ba99525e 9279 || token_type == CPP_CHAR16
9280 || token_type == CPP_CHAR32)
92b128ed 9281 {
9282 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 9283 const char *prefix;
9284
ba99525e 9285 switch (token_type)
924bbf02 9286 {
9287 default:
9288 prefix = "";
9289 break;
9290 case CPP_WCHAR:
9291 prefix = "L";
9292 break;
9293 case CPP_CHAR16:
9294 prefix = "u";
9295 break;
9296 case CPP_CHAR32:
9297 prefix = "U";
9298 break;
9299 }
9300
92b128ed 9301 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 9302 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 9303 else
a0c938f0 9304 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 9305
924bbf02 9306 error (message, prefix, val);
209c9752 9307 free (message);
9308 message = NULL;
92b128ed 9309 }
48e1416a 9310 else if (token_type == CPP_STRING
9311 || token_type == CPP_WSTRING
ba99525e 9312 || token_type == CPP_STRING16
538ba11a 9313 || token_type == CPP_STRING32
9314 || token_type == CPP_UTF8STRING)
380c6697 9315 message = catenate_messages (gmsgid, " before string constant");
ba99525e 9316 else if (token_type == CPP_NUMBER)
380c6697 9317 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 9318 else if (token_type == CPP_NAME)
209c9752 9319 {
380c6697 9320 message = catenate_messages (gmsgid, " before %qE");
782858b8 9321 error (message, value);
209c9752 9322 free (message);
9323 message = NULL;
9324 }
ba99525e 9325 else if (token_type == CPP_PRAGMA)
b75b98aa 9326 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 9327 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 9328 message = catenate_messages (gmsgid, " before end of line");
07b8f133 9329 else if (token_type == CPP_DECLTYPE)
9330 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 9331 else if (token_type < N_TTYPES)
209c9752 9332 {
380c6697 9333 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 9334 error (message, cpp_type2name (token_type, token_flags));
209c9752 9335 free (message);
9336 message = NULL;
9337 }
92b128ed 9338 else
380c6697 9339 error (gmsgid);
209c9752 9340
9341 if (message)
9342 {
9343 error (message);
9344 free (message);
9345 }
a0c938f0 9346#undef catenate_messages
92b128ed 9347}
9348
3a79f5da 9349/* Mapping for cpp message reasons to the options that enable them. */
9350
9351struct reason_option_codes_t
9352{
9353 const int reason; /* cpplib message reason. */
9354 const int option_code; /* gcc option that controls this message. */
9355};
9356
9357static const struct reason_option_codes_t option_codes[] = {
9358 {CPP_W_DEPRECATED, OPT_Wdeprecated},
67089c6b 9359 {CPP_W_COMMENTS, OPT_Wcomment},
3a79f5da 9360 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9361 {CPP_W_MULTICHAR, OPT_Wmultichar},
9362 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9363 {CPP_W_LONG_LONG, OPT_Wlong_long},
9364 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9365 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9366 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9367 {CPP_W_UNDEF, OPT_Wundef},
9368 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9369 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9370 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9371 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9372 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
76d340ac 9373 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
3a79f5da 9374 {CPP_W_NONE, 0}
9375};
9376
9377/* Return the gcc option code associated with the reason for a cpp
9378 message, or 0 if none. */
9379
9380static int
9381c_option_controlling_cpp_error (int reason)
9382{
9383 const struct reason_option_codes_t *entry;
9384
9385 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9386 {
9387 if (entry->reason == reason)
9388 return entry->option_code;
9389 }
9390 return 0;
9391}
9392
7f5f3953 9393/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 9394 preprocessor. The diagnostic is of type LEVEL, with REASON set
9395 to the reason code if LEVEL is represents a warning, at location
7f5f3953 9396 LOCATION unless this is after lexing and the compiler's location
9397 should be used instead, with column number possibly overridden by
9398 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9399 the arguments. Returns true if a diagnostic was emitted, false
9400 otherwise. */
9401
9402bool
3a79f5da 9403c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
7f5f3953 9404 location_t location, unsigned int column_override,
9405 const char *msg, va_list *ap)
9406{
9407 diagnostic_info diagnostic;
9408 diagnostic_t dlevel;
5ae82d58 9409 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 9410 bool ret;
9411
9412 switch (level)
9413 {
9414 case CPP_DL_WARNING_SYSHDR:
9415 if (flag_no_output)
9416 return false;
5ae82d58 9417 global_dc->dc_warn_system_headers = 1;
7f5f3953 9418 /* Fall through. */
9419 case CPP_DL_WARNING:
9420 if (flag_no_output)
9421 return false;
9422 dlevel = DK_WARNING;
9423 break;
9424 case CPP_DL_PEDWARN:
9425 if (flag_no_output && !flag_pedantic_errors)
9426 return false;
9427 dlevel = DK_PEDWARN;
9428 break;
9429 case CPP_DL_ERROR:
9430 dlevel = DK_ERROR;
9431 break;
9432 case CPP_DL_ICE:
9433 dlevel = DK_ICE;
9434 break;
9435 case CPP_DL_NOTE:
9436 dlevel = DK_NOTE;
9437 break;
ff903809 9438 case CPP_DL_FATAL:
9439 dlevel = DK_FATAL;
9440 break;
7f5f3953 9441 default:
9442 gcc_unreachable ();
9443 }
9444 if (done_lexing)
9445 location = input_location;
9446 diagnostic_set_info_translated (&diagnostic, msg, ap,
9447 location, dlevel);
9448 if (column_override)
9449 diagnostic_override_column (&diagnostic, column_override);
3a79f5da 9450 diagnostic_override_option_index (&diagnostic,
9451 c_option_controlling_cpp_error (reason));
7f5f3953 9452 ret = report_diagnostic (&diagnostic);
9453 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 9454 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 9455 return ret;
9456}
9457
624d37a6 9458/* Convert a character from the host to the target execution character
9459 set. cpplib handles this, mostly. */
9460
9461HOST_WIDE_INT
9462c_common_to_target_charset (HOST_WIDE_INT c)
9463{
9464 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9465 zero-extended under -fno-signed-char. cpplib insists that characters
9466 and character constants are always unsigned. Hence we must convert
9467 back and forth. */
9468 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9469
9470 uc = cpp_host_to_exec_charset (parse_in, uc);
9471
9472 if (flag_signed_char)
9473 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9474 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9475 else
9476 return uc;
9477}
9478
7549df0d 9479/* Fold an offsetof-like expression. EXPR is a nested sequence of component
9480 references with an INDIRECT_REF of a constant at the bottom; much like the
9481 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 9482
7549df0d 9483tree
9484fold_offsetof_1 (tree expr)
af28855b 9485{
af28855b 9486 tree base, off, t;
9487
9488 switch (TREE_CODE (expr))
9489 {
9490 case ERROR_MARK:
9491 return expr;
9492
6b11d2e3 9493 case VAR_DECL:
9494 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9495 return error_mark_node;
9496
d897f7c2 9497 case CALL_EXPR:
cf1a89a3 9498 case TARGET_EXPR:
d897f7c2 9499 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9500 return error_mark_node;
9501
d897f7c2 9502 case NOP_EXPR:
9503 case INDIRECT_REF:
7549df0d 9504 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 9505 {
9506 error ("cannot apply %<offsetof%> to a non constant address");
9507 return error_mark_node;
9508 }
7549df0d 9509 return TREE_OPERAND (expr, 0);
d897f7c2 9510
af28855b 9511 case COMPONENT_REF:
7549df0d 9512 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 9513 if (base == error_mark_node)
9514 return base;
9515
9516 t = TREE_OPERAND (expr, 1);
9517 if (DECL_C_BIT_FIELD (t))
9518 {
9519 error ("attempt to take address of bit-field structure "
782858b8 9520 "member %qD", t);
af28855b 9521 return error_mark_node;
9522 }
389dd41b 9523 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9524 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9525 1)
9526 / BITS_PER_UNIT));
af28855b 9527 break;
9528
9529 case ARRAY_REF:
7549df0d 9530 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 9531 if (base == error_mark_node)
9532 return base;
9533
9534 t = TREE_OPERAND (expr, 1);
64ed018c 9535
9536 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 9537 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 9538 {
9539 tree upbound = array_ref_up_bound (expr);
9540 if (upbound != NULL_TREE
9541 && TREE_CODE (upbound) == INTEGER_CST
9542 && !tree_int_cst_equal (upbound,
9543 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9544 {
9545 upbound = size_binop (PLUS_EXPR, upbound,
9546 build_int_cst (TREE_TYPE (upbound), 1));
9547 if (tree_int_cst_lt (upbound, t))
9548 {
9549 tree v;
9550
9551 for (v = TREE_OPERAND (expr, 0);
9552 TREE_CODE (v) == COMPONENT_REF;
9553 v = TREE_OPERAND (v, 0))
9554 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9555 == RECORD_TYPE)
9556 {
1767a056 9557 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9558 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 9559 if (TREE_CODE (fld_chain) == FIELD_DECL)
9560 break;
9561
9562 if (fld_chain)
9563 break;
9564 }
9565 /* Don't warn if the array might be considered a poor
9566 man's flexible array member with a very permissive
9567 definition thereof. */
9568 if (TREE_CODE (v) == ARRAY_REF
9569 || TREE_CODE (v) == COMPONENT_REF)
9570 warning (OPT_Warray_bounds,
9571 "index %E denotes an offset "
9572 "greater than size of %qT",
9573 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9574 }
9575 }
9576 }
7549df0d 9577
9578 t = convert (sizetype, t);
9579 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 9580 break;
9581
ede90cc2 9582 case COMPOUND_EXPR:
9583 /* Handle static members of volatile structs. */
9584 t = TREE_OPERAND (expr, 1);
9585 gcc_assert (TREE_CODE (t) == VAR_DECL);
7549df0d 9586 return fold_offsetof_1 (t);
ede90cc2 9587
af28855b 9588 default:
231bd014 9589 gcc_unreachable ();
af28855b 9590 }
9591
7549df0d 9592 return fold_build_pointer_plus (base, off);
af28855b 9593}
9594
7549df0d 9595/* Likewise, but convert it to the return type of offsetof. */
9596
af28855b 9597tree
7549df0d 9598fold_offsetof (tree expr)
af28855b 9599{
7549df0d 9600 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 9601}
9602
b9bdfa0b 9603/* Warn for A ?: C expressions (with B omitted) where A is a boolean
9604 expression, because B will always be true. */
9605
9606void
9607warn_for_omitted_condop (location_t location, tree cond)
9608{
9609 if (truth_value_p (TREE_CODE (cond)))
9610 warning_at (location, OPT_Wparentheses,
9611 "the omitted middle operand in ?: will always be %<true%>, "
9612 "suggest explicit middle operand");
9613}
9614
a1f90215 9615/* Give an error for storing into ARG, which is 'const'. USE indicates
9616 how ARG was being used. */
9617
9618void
9619readonly_error (tree arg, enum lvalue_use use)
9620{
9621 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9622 || use == lv_asm);
9623 /* Using this macro rather than (for example) arrays of messages
9624 ensures that all the format strings are checked at compile
9625 time. */
9626#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9627 : (use == lv_increment ? (I) \
9628 : (use == lv_decrement ? (D) : (AS))))
9629 if (TREE_CODE (arg) == COMPONENT_REF)
9630 {
9631 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9632 error (READONLY_MSG (G_("assignment of member "
9633 "%qD in read-only object"),
9634 G_("increment of member "
9635 "%qD in read-only object"),
9636 G_("decrement of member "
9637 "%qD in read-only object"),
9638 G_("member %qD in read-only object "
9639 "used as %<asm%> output")),
9640 TREE_OPERAND (arg, 1));
9641 else
9642 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9643 G_("increment of read-only member %qD"),
9644 G_("decrement of read-only member %qD"),
9645 G_("read-only member %qD used as %<asm%> output")),
9646 TREE_OPERAND (arg, 1));
9647 }
9648 else if (TREE_CODE (arg) == VAR_DECL)
9649 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9650 G_("increment of read-only variable %qD"),
9651 G_("decrement of read-only variable %qD"),
9652 G_("read-only variable %qD used as %<asm%> output")),
9653 arg);
9654 else if (TREE_CODE (arg) == PARM_DECL)
9655 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9656 G_("increment of read-only parameter %qD"),
9657 G_("decrement of read-only parameter %qD"),
9658 G_("read-only parameter %qD use as %<asm%> output")),
9659 arg);
9660 else if (TREE_CODE (arg) == RESULT_DECL)
9661 {
9662 gcc_assert (c_dialect_cxx ());
9663 error (READONLY_MSG (G_("assignment of "
9664 "read-only named return value %qD"),
9665 G_("increment of "
9666 "read-only named return value %qD"),
9667 G_("decrement of "
9668 "read-only named return value %qD"),
9669 G_("read-only named return value %qD "
9670 "used as %<asm%>output")),
9671 arg);
9672 }
9673 else if (TREE_CODE (arg) == FUNCTION_DECL)
9674 error (READONLY_MSG (G_("assignment of function %qD"),
9675 G_("increment of function %qD"),
9676 G_("decrement of function %qD"),
9677 G_("function %qD used as %<asm%> output")),
9678 arg);
9679 else
9680 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9681 G_("increment of read-only location %qE"),
9682 G_("decrement of read-only location %qE"),
9683 G_("read-only location %qE used as %<asm%> output")),
9684 arg);
9685}
9686
e35976b1 9687/* Print an error message for an invalid lvalue. USE says
fdd84b77 9688 how the lvalue is being used and so selects the error message. LOC
9689 is the location for the error. */
ab6bb714 9690
e35976b1 9691void
fdd84b77 9692lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 9693{
e35976b1 9694 switch (use)
ab6bb714 9695 {
e35976b1 9696 case lv_assign:
fdd84b77 9697 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 9698 break;
9699 case lv_increment:
fdd84b77 9700 error_at (loc, "lvalue required as increment operand");
e35976b1 9701 break;
9702 case lv_decrement:
fdd84b77 9703 error_at (loc, "lvalue required as decrement operand");
e35976b1 9704 break;
9705 case lv_addressof:
fdd84b77 9706 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 9707 break;
9708 case lv_asm:
fdd84b77 9709 error_at (loc, "lvalue required in asm statement");
e35976b1 9710 break;
9711 default:
9712 gcc_unreachable ();
ab6bb714 9713 }
ab6bb714 9714}
b1bbc8e5 9715
9716/* Print an error message for an invalid indirection of type TYPE.
9717 ERRSTRING is the name of the operator for the indirection. */
9718
9719void
9720invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9721{
9722 switch (errstring)
9723 {
9724 case RO_NULL:
9725 gcc_assert (c_dialect_cxx ());
9726 error_at (loc, "invalid type argument (have %qT)", type);
9727 break;
9728 case RO_ARRAY_INDEXING:
9729 error_at (loc,
9730 "invalid type argument of array indexing (have %qT)",
9731 type);
9732 break;
9733 case RO_UNARY_STAR:
9734 error_at (loc,
9735 "invalid type argument of unary %<*%> (have %qT)",
9736 type);
9737 break;
9738 case RO_ARROW:
9739 error_at (loc,
9740 "invalid type argument of %<->%> (have %qT)",
9741 type);
9742 break;
9743 case RO_IMPLICIT_CONVERSION:
9744 error_at (loc,
9745 "invalid type argument of implicit conversion (have %qT)",
9746 type);
9747 break;
9748 default:
9749 gcc_unreachable ();
9750 }
9751}
c271bdb2 9752\f
9753/* *PTYPE is an incomplete array. Complete it with a domain based on
9754 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9755 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9756 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9757
9758int
9759complete_array_type (tree *ptype, tree initial_value, bool do_default)
9760{
9761 tree maxindex, type, main_type, elt, unqual_elt;
9762 int failure = 0, quals;
6753bca0 9763 hashval_t hashcode = 0;
c271bdb2 9764
9765 maxindex = size_zero_node;
9766 if (initial_value)
9767 {
9768 if (TREE_CODE (initial_value) == STRING_CST)
9769 {
9770 int eltsize
9771 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9772 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9773 }
9774 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9775 {
f1f41a6c 9776 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 9777
f1f41a6c 9778 if (vec_safe_is_empty (v))
c271bdb2 9779 {
9780 if (pedantic)
9781 failure = 3;
7542c3b4 9782 maxindex = ssize_int (-1);
c271bdb2 9783 }
9784 else
9785 {
9786 tree curindex;
c75b4594 9787 unsigned HOST_WIDE_INT cnt;
9788 constructor_elt *ce;
cee43f7e 9789 bool fold_p = false;
c271bdb2 9790
f1f41a6c 9791 if ((*v)[0].index)
389dd41b 9792 maxindex = fold_convert_loc (input_location, sizetype,
f1f41a6c 9793 (*v)[0].index);
c271bdb2 9794 curindex = maxindex;
9795
f1f41a6c 9796 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 9797 {
cee43f7e 9798 bool curfold_p = false;
c75b4594 9799 if (ce->index)
cee43f7e 9800 curindex = ce->index, curfold_p = true;
c271bdb2 9801 else
cee43f7e 9802 {
9803 if (fold_p)
9804 curindex = fold_convert (sizetype, curindex);
389dd41b 9805 curindex = size_binop (PLUS_EXPR, curindex,
9806 size_one_node);
cee43f7e 9807 }
c271bdb2 9808 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 9809 maxindex = curindex, fold_p = curfold_p;
c271bdb2 9810 }
cee43f7e 9811 if (fold_p)
9812 maxindex = fold_convert (sizetype, maxindex);
c271bdb2 9813 }
9814 }
9815 else
9816 {
9817 /* Make an error message unless that happened already. */
9818 if (initial_value != error_mark_node)
9819 failure = 1;
9820 }
9821 }
9822 else
9823 {
9824 failure = 2;
9825 if (!do_default)
9826 return failure;
9827 }
9828
9829 type = *ptype;
9830 elt = TREE_TYPE (type);
9831 quals = TYPE_QUALS (strip_array_types (elt));
9832 if (quals == 0)
9833 unqual_elt = elt;
9834 else
6d5d708e 9835 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 9836
9837 /* Using build_distinct_type_copy and modifying things afterward instead
9838 of using build_array_type to create a new type preserves all of the
9839 TYPE_LANG_FLAG_? bits that the front end may have set. */
9840 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9841 TREE_TYPE (main_type) = unqual_elt;
783bb57e 9842 TYPE_DOMAIN (main_type)
9843 = build_range_type (TREE_TYPE (maxindex),
9844 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 9845 layout_type (main_type);
9846
6753bca0 9847 /* Make sure we have the canonical MAIN_TYPE. */
9848 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 9849 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 9850 hashcode);
9851 main_type = type_hash_canon (hashcode, main_type);
9852
796735dc 9853 /* Fix the canonical type. */
9854 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
9855 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
9856 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
9857 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
9858 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
9859 != TYPE_DOMAIN (main_type)))
48e1416a 9860 TYPE_CANONICAL (main_type)
796735dc 9861 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
9862 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
9863 else
9864 TYPE_CANONICAL (main_type) = main_type;
9865
c271bdb2 9866 if (quals == 0)
9867 type = main_type;
9868 else
9869 type = c_build_qualified_type (main_type, quals);
9870
4f5b8f2a 9871 if (COMPLETE_TYPE_P (type)
9872 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9873 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9874 {
9875 error ("size of array is too large");
9876 /* If we proceed with the array type as it is, we'll eventually
9877 crash in tree_low_cst(). */
9878 type = error_mark_node;
9879 }
9880
c271bdb2 9881 *ptype = type;
9882 return failure;
9883}
ab6bb714 9884
93426222 9885/* Like c_mark_addressable but don't check register qualifier. */
9886void
9887c_common_mark_addressable_vec (tree t)
9888{
9889 while (handled_component_p (t))
9890 t = TREE_OPERAND (t, 0);
9891 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9892 return;
9893 TREE_ADDRESSABLE (t) = 1;
9894}
9895
9896
b6a5fc45 9897\f
9898/* Used to help initialize the builtin-types.def table. When a type of
9899 the correct size doesn't exist, use error_mark_node instead of NULL.
9900 The later results in segfaults even when a decl using the type doesn't
9901 get invoked. */
9902
9903tree
9904builtin_type_for_size (int size, bool unsignedp)
9905{
a51edb4c 9906 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 9907 return type ? type : error_mark_node;
9908}
9909
9910/* A helper function for resolve_overloaded_builtin in resolving the
9911 overloaded __sync_ builtins. Returns a positive power of 2 if the
9912 first operand of PARAMS is a pointer to a supported data type.
9913 Returns 0 if an error is encountered. */
9914
9915static int
f1f41a6c 9916sync_resolve_size (tree function, vec<tree, va_gc> *params)
b6a5fc45 9917{
9918 tree type;
9919 int size;
9920
f1f41a6c 9921 if (!params)
b6a5fc45 9922 {
9923 error ("too few arguments to function %qE", function);
9924 return 0;
9925 }
9926
f1f41a6c 9927 type = TREE_TYPE ((*params)[0]);
b6a5fc45 9928 if (TREE_CODE (type) != POINTER_TYPE)
9929 goto incompatible;
9930
9931 type = TREE_TYPE (type);
9932 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9933 goto incompatible;
9934
9935 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
27213ba3 9936 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 9937 return size;
9938
9939 incompatible:
9940 error ("incompatible type for argument %d of %qE", 1, function);
9941 return 0;
9942}
9943
a0c938f0 9944/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 9945 PARAMS to make arguments match up with those of FUNCTION. Drops
9946 the variadic arguments at the end. Returns false if some error
9947 was encountered; true on success. */
9948
9949static bool
1cd6e20d 9950sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 9951 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 9952{
d0af78c5 9953 function_args_iterator iter;
b6a5fc45 9954 tree ptype;
b9c74b4d 9955 unsigned int parmnum;
b6a5fc45 9956
d0af78c5 9957 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 9958 /* We've declared the implementation functions to use "volatile void *"
9959 as the pointer parameter, so we shouldn't get any complaints from the
9960 call to check_function_arguments what ever type the user used. */
d0af78c5 9961 function_args_iter_next (&iter);
f1f41a6c 9962 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b6a5fc45 9963
9964 /* For the rest of the values, we need to cast these to FTYPE, so that we
9965 don't get warnings for passing pointer types, etc. */
b9c74b4d 9966 parmnum = 0;
d0af78c5 9967 while (1)
b6a5fc45 9968 {
d0af78c5 9969 tree val, arg_type;
9970
9971 arg_type = function_args_iter_cond (&iter);
9972 /* XXX void_type_node belies the abstraction. */
9973 if (arg_type == void_type_node)
9974 break;
b6a5fc45 9975
b9c74b4d 9976 ++parmnum;
f1f41a6c 9977 if (params->length () <= parmnum)
b6a5fc45 9978 {
1cd6e20d 9979 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 9980 return false;
9981 }
9982
0f6a7cb7 9983 /* Only convert parameters if arg_type is unsigned integer type with
9984 new format sync routines, i.e. don't attempt to convert pointer
9985 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
9986 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
9987 kinds). */
9988 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 9989 {
9990 /* Ideally for the first conversion we'd use convert_for_assignment
9991 so that we get warnings for anything that doesn't match the pointer
9992 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 9993 val = (*params)[parmnum];
1cd6e20d 9994 val = convert (ptype, val);
9995 val = convert (arg_type, val);
f1f41a6c 9996 (*params)[parmnum] = val;
1cd6e20d 9997 }
b6a5fc45 9998
d0af78c5 9999 function_args_iter_next (&iter);
b6a5fc45 10000 }
10001
1cd6e20d 10002 /* __atomic routines are not variadic. */
f1f41a6c 10003 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 10004 {
10005 error_at (loc, "too many arguments to function %qE", orig_function);
10006 return false;
10007 }
10008
b6a5fc45 10009 /* The definition of these primitives is variadic, with the remaining
10010 being "an optional list of variables protected by the memory barrier".
10011 No clue what that's supposed to mean, precisely, but we consider all
10012 call-clobbered variables to be protected so we're safe. */
f1f41a6c 10013 params->truncate (parmnum + 1);
b6a5fc45 10014
10015 return true;
10016}
10017
a0c938f0 10018/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 10019 RESULT to make it match the type of the first pointer argument in
10020 PARAMS. */
10021
10022static tree
1cd6e20d 10023sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 10024{
b9c74b4d 10025 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 10026 tree rtype = TREE_TYPE (result);
10080eac 10027 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 10028
10029 /* New format doesn't require casting unless the types are the same size. */
10030 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10031 return convert (ptype, result);
10032 else
10033 return result;
10034}
10035
10036/* This function verifies the PARAMS to generic atomic FUNCTION.
10037 It returns the size if all the parameters are the same size, otherwise
10038 0 is returned if the parameters are invalid. */
10039
10040static int
f1f41a6c 10041get_atomic_generic_size (location_t loc, tree function,
10042 vec<tree, va_gc> *params)
1cd6e20d 10043{
10044 unsigned int n_param;
10045 unsigned int n_model;
10046 unsigned int x;
10047 int size_0;
10048 tree type_0;
10049
10050 /* Determine the parameter makeup. */
10051 switch (DECL_FUNCTION_CODE (function))
10052 {
10053 case BUILT_IN_ATOMIC_EXCHANGE:
10054 n_param = 4;
10055 n_model = 1;
10056 break;
10057 case BUILT_IN_ATOMIC_LOAD:
10058 case BUILT_IN_ATOMIC_STORE:
10059 n_param = 3;
10060 n_model = 1;
10061 break;
10062 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10063 n_param = 6;
10064 n_model = 2;
10065 break;
10066 default:
1d581089 10067 gcc_unreachable ();
1cd6e20d 10068 }
10069
f1f41a6c 10070 if (vec_safe_length (params) != n_param)
1cd6e20d 10071 {
10072 error_at (loc, "incorrect number of arguments to function %qE", function);
10073 return 0;
10074 }
10075
10076 /* Get type of first parameter, and determine its size. */
f1f41a6c 10077 type_0 = TREE_TYPE ((*params)[0]);
1d581089 10078 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10079 {
10080 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10081 function);
10082 return 0;
10083 }
10084
10085 /* Types must be compile time constant sizes. */
10086 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 10087 {
1d581089 10088 error_at (loc,
10089 "argument 1 of %qE must be a pointer to a constant size type",
10090 function);
1cd6e20d 10091 return 0;
10092 }
1d581089 10093
1cd6e20d 10094 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10095
1d581089 10096 /* Zero size objects are not allowed. */
10097 if (size_0 == 0)
10098 {
10099 error_at (loc,
10100 "argument 1 of %qE must be a pointer to a nonzero size object",
10101 function);
10102 return 0;
10103 }
10104
1cd6e20d 10105 /* Check each other parameter is a pointer and the same size. */
10106 for (x = 0; x < n_param - n_model; x++)
10107 {
10108 int size;
f1f41a6c 10109 tree type = TREE_TYPE ((*params)[x]);
1cd6e20d 10110 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */
10111 if (n_param == 6 && x == 3)
10112 continue;
10113 if (!POINTER_TYPE_P (type))
10114 {
10115 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10116 function);
10117 return 0;
10118 }
10119 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10120 if (size != size_0)
10121 {
10122 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10123 function);
10124 return 0;
10125 }
10126 }
10127
10128 /* Check memory model parameters for validity. */
10129 for (x = n_param - n_model ; x < n_param; x++)
10130 {
f1f41a6c 10131 tree p = (*params)[x];
1cd6e20d 10132 if (TREE_CODE (p) == INTEGER_CST)
10133 {
10134 int i = tree_low_cst (p, 1);
72d65da9 10135 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
1cd6e20d 10136 {
10137 warning_at (loc, OPT_Winvalid_memory_model,
10138 "invalid memory model argument %d of %qE", x + 1,
10139 function);
1cd6e20d 10140 }
10141 }
10142 else
10143 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10144 {
10145 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10146 function);
10147 return 0;
10148 }
10149 }
10150
10151 return size_0;
10152}
10153
10154
10155/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10156 at the beginning of the parameter list PARAMS representing the size of the
10157 objects. This is to match the library ABI requirement. LOC is the location
10158 of the function call.
10159 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10160 returned to allow the external call to be constructed. */
10161
10162static tree
10163add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 10164 vec<tree, va_gc> *params)
1cd6e20d 10165{
10166 tree size_node;
10167
10168 /* Insert a SIZE_T parameter as the first param. If there isn't
10169 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 10170 if (!params->space (1))
1cd6e20d 10171 {
10172 unsigned int z, len;
f1f41a6c 10173 vec<tree, va_gc> *v;
1cd6e20d 10174 tree f;
10175
f1f41a6c 10176 len = params->length ();
10177 vec_alloc (v, len + 1);
1cd6e20d 10178 for (z = 0; z < len; z++)
f1f41a6c 10179 v->quick_push ((*params)[z]);
10180 f = build_function_call_vec (loc, function, v, NULL);
10181 vec_free (v);
1cd6e20d 10182 return f;
10183 }
10184
10185 /* Add the size parameter and leave as a function call for processing. */
10186 size_node = build_int_cst (size_type_node, n);
f1f41a6c 10187 params->quick_insert (0, size_node);
1cd6e20d 10188 return NULL_TREE;
10189}
10190
10191
10192/* This will process an __atomic_exchange function call, determine whether it
10193 needs to be mapped to the _N variation, or turned into a library call.
10194 LOC is the location of the builtin call.
10195 FUNCTION is the DECL that has been invoked;
10196 PARAMS is the argument list for the call. The return value is non-null
10197 TRUE is returned if it is translated into the proper format for a call to the
10198 external library, and NEW_RETURN is set the tree for that function.
10199 FALSE is returned if processing for the _N variation is required, and
10200 NEW_RETURN is set to the the return value the result is copied into. */
10201static bool
10202resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 10203 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 10204{
10205 tree p0, p1, p2, p3;
10206 tree I_type, I_type_ptr;
10207 int n = get_atomic_generic_size (loc, function, params);
10208
1d581089 10209 /* Size of 0 is an error condition. */
10210 if (n == 0)
10211 {
10212 *new_return = error_mark_node;
10213 return true;
10214 }
10215
1cd6e20d 10216 /* If not a lock-free size, change to the library generic format. */
10217 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10218 {
10219 *new_return = add_atomic_size_parameter (n, loc, function, params);
10220 return true;
10221 }
10222
10223 /* Otherwise there is a lockfree match, transform the call from:
10224 void fn(T* mem, T* desired, T* return, model)
10225 into
10226 *return = (T) (fn (In* mem, (In) *desired, model)) */
10227
f1f41a6c 10228 p0 = (*params)[0];
10229 p1 = (*params)[1];
10230 p2 = (*params)[2];
10231 p3 = (*params)[3];
1cd6e20d 10232
10233 /* Create pointer to appropriate size. */
10234 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10235 I_type_ptr = build_pointer_type (I_type);
10236
10237 /* Convert object pointer to required type. */
10238 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 10239 (*params)[0] = p0;
1cd6e20d 10240 /* Convert new value to required type, and dereference it. */
10241 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10242 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 10243 (*params)[1] = p1;
1cd6e20d 10244
10245 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 10246 (*params)[2] = p3;
10247 params->truncate (3);
1cd6e20d 10248
10249 /* Convert return pointer and dereference it for later assignment. */
10250 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10251
10252 return false;
b6a5fc45 10253}
10254
1cd6e20d 10255
10256/* This will process an __atomic_compare_exchange function call, determine
10257 whether it needs to be mapped to the _N variation, or turned into a lib call.
10258 LOC is the location of the builtin call.
10259 FUNCTION is the DECL that has been invoked;
10260 PARAMS is the argument list for the call. The return value is non-null
10261 TRUE is returned if it is translated into the proper format for a call to the
10262 external library, and NEW_RETURN is set the tree for that function.
10263 FALSE is returned if processing for the _N variation is required. */
10264
10265static bool
10266resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 10267 vec<tree, va_gc> *params,
1cd6e20d 10268 tree *new_return)
10269{
10270 tree p0, p1, p2;
10271 tree I_type, I_type_ptr;
10272 int n = get_atomic_generic_size (loc, function, params);
10273
1d581089 10274 /* Size of 0 is an error condition. */
10275 if (n == 0)
10276 {
10277 *new_return = error_mark_node;
10278 return true;
10279 }
10280
1cd6e20d 10281 /* If not a lock-free size, change to the library generic format. */
10282 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10283 {
10284 /* The library generic format does not have the weak parameter, so
10285 remove it from the param list. Since a parameter has been removed,
10286 we can be sure that there is room for the SIZE_T parameter, meaning
10287 there will not be a recursive rebuilding of the parameter list, so
10288 there is no danger this will be done twice. */
10289 if (n > 0)
10290 {
f1f41a6c 10291 (*params)[3] = (*params)[4];
10292 (*params)[4] = (*params)[5];
10293 params->truncate (5);
1cd6e20d 10294 }
10295 *new_return = add_atomic_size_parameter (n, loc, function, params);
10296 return true;
10297 }
10298
10299 /* Otherwise, there is a match, so the call needs to be transformed from:
10300 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10301 into
10302 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10303
f1f41a6c 10304 p0 = (*params)[0];
10305 p1 = (*params)[1];
10306 p2 = (*params)[2];
1cd6e20d 10307
10308 /* Create pointer to appropriate size. */
10309 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10310 I_type_ptr = build_pointer_type (I_type);
10311
10312 /* Convert object pointer to required type. */
10313 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 10314 (*params)[0] = p0;
1cd6e20d 10315
10316 /* Convert expected pointer to required type. */
10317 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 10318 (*params)[1] = p1;
1cd6e20d 10319
10320 /* Convert desired value to required type, and dereference it. */
10321 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10322 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 10323 (*params)[2] = p2;
1cd6e20d 10324
10325 /* The rest of the parameters are fine. NULL means no special return value
10326 processing.*/
10327 *new_return = NULL;
10328 return false;
10329}
10330
10331
10332/* This will process an __atomic_load function call, determine whether it
10333 needs to be mapped to the _N variation, or turned into a library call.
10334 LOC is the location of the builtin call.
10335 FUNCTION is the DECL that has been invoked;
10336 PARAMS is the argument list for the call. The return value is non-null
10337 TRUE is returned if it is translated into the proper format for a call to the
10338 external library, and NEW_RETURN is set the tree for that function.
10339 FALSE is returned if processing for the _N variation is required, and
10340 NEW_RETURN is set to the the return value the result is copied into. */
10341
10342static bool
10343resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 10344 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 10345{
10346 tree p0, p1, p2;
10347 tree I_type, I_type_ptr;
10348 int n = get_atomic_generic_size (loc, function, params);
10349
1d581089 10350 /* Size of 0 is an error condition. */
10351 if (n == 0)
10352 {
10353 *new_return = error_mark_node;
10354 return true;
10355 }
10356
1cd6e20d 10357 /* If not a lock-free size, change to the library generic format. */
10358 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10359 {
10360 *new_return = add_atomic_size_parameter (n, loc, function, params);
10361 return true;
10362 }
10363
10364 /* Otherwise, there is a match, so the call needs to be transformed from:
10365 void fn(T* mem, T* return, model)
10366 into
10367 *return = (T) (fn ((In *) mem, model)) */
10368
f1f41a6c 10369 p0 = (*params)[0];
10370 p1 = (*params)[1];
10371 p2 = (*params)[2];
1cd6e20d 10372
10373 /* Create pointer to appropriate size. */
10374 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10375 I_type_ptr = build_pointer_type (I_type);
10376
10377 /* Convert object pointer to required type. */
10378 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 10379 (*params)[0] = p0;
1cd6e20d 10380
10381 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 10382 (*params)[1] = p2;
10383 params->truncate (2);
1cd6e20d 10384
10385 /* Convert return pointer and dereference it for later assignment. */
10386 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10387
10388 return false;
10389}
10390
10391
10392/* This will process an __atomic_store function call, determine whether it
10393 needs to be mapped to the _N variation, or turned into a library call.
10394 LOC is the location of the builtin call.
10395 FUNCTION is the DECL that has been invoked;
10396 PARAMS is the argument list for the call. The return value is non-null
10397 TRUE is returned if it is translated into the proper format for a call to the
10398 external library, and NEW_RETURN is set the tree for that function.
10399 FALSE is returned if processing for the _N variation is required, and
10400 NEW_RETURN is set to the the return value the result is copied into. */
10401
10402static bool
10403resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 10404 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 10405{
10406 tree p0, p1;
10407 tree I_type, I_type_ptr;
10408 int n = get_atomic_generic_size (loc, function, params);
10409
1d581089 10410 /* Size of 0 is an error condition. */
10411 if (n == 0)
10412 {
10413 *new_return = error_mark_node;
10414 return true;
10415 }
10416
1cd6e20d 10417 /* If not a lock-free size, change to the library generic format. */
10418 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10419 {
10420 *new_return = add_atomic_size_parameter (n, loc, function, params);
10421 return true;
10422 }
10423
10424 /* Otherwise, there is a match, so the call needs to be transformed from:
10425 void fn(T* mem, T* value, model)
10426 into
10427 fn ((In *) mem, (In) *value, model) */
10428
f1f41a6c 10429 p0 = (*params)[0];
10430 p1 = (*params)[1];
1cd6e20d 10431
10432 /* Create pointer to appropriate size. */
10433 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10434 I_type_ptr = build_pointer_type (I_type);
10435
10436 /* Convert object pointer to required type. */
10437 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 10438 (*params)[0] = p0;
1cd6e20d 10439
10440 /* Convert new value to required type, and dereference it. */
10441 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10442 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 10443 (*params)[1] = p1;
1cd6e20d 10444
10445 /* The memory model is in the right spot already. Return is void. */
10446 *new_return = NULL_TREE;
10447
10448 return false;
10449}
10450
10451
b6a5fc45 10452/* Some builtin functions are placeholders for other expressions. This
10453 function should be called immediately after parsing the call expression
10454 before surrounding code has committed to the type of the expression.
10455
e60a6f7b 10456 LOC is the location of the builtin call.
10457
b6a5fc45 10458 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10459 PARAMS is the argument list for the call. The return value is non-null
10460 when expansion is complete, and null if normal processing should
10461 continue. */
10462
10463tree
f1f41a6c 10464resolve_overloaded_builtin (location_t loc, tree function,
10465 vec<tree, va_gc> *params)
b6a5fc45 10466{
10467 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
1cd6e20d 10468 bool orig_format = true;
10469 tree new_return = NULL_TREE;
10470
65441f6f 10471 switch (DECL_BUILT_IN_CLASS (function))
10472 {
10473 case BUILT_IN_NORMAL:
10474 break;
10475 case BUILT_IN_MD:
10476 if (targetm.resolve_overloaded_builtin)
e60a6f7b 10477 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 10478 else
a0c938f0 10479 return NULL_TREE;
65441f6f 10480 default:
10481 return NULL_TREE;
10482 }
a0c938f0 10483
65441f6f 10484 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 10485 switch (orig_code)
10486 {
1cd6e20d 10487 case BUILT_IN_ATOMIC_EXCHANGE:
10488 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10489 case BUILT_IN_ATOMIC_LOAD:
10490 case BUILT_IN_ATOMIC_STORE:
10491 {
10492 /* Handle these 4 together so that they can fall through to the next
10493 case if the call is transformed to an _N variant. */
10494 switch (orig_code)
10495 {
10496 case BUILT_IN_ATOMIC_EXCHANGE:
10497 {
10498 if (resolve_overloaded_atomic_exchange (loc, function, params,
10499 &new_return))
10500 return new_return;
10501 /* Change to the _N variant. */
10502 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10503 break;
10504 }
10505
10506 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10507 {
10508 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10509 params,
10510 &new_return))
10511 return new_return;
10512 /* Change to the _N variant. */
10513 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10514 break;
10515 }
10516 case BUILT_IN_ATOMIC_LOAD:
10517 {
10518 if (resolve_overloaded_atomic_load (loc, function, params,
10519 &new_return))
10520 return new_return;
10521 /* Change to the _N variant. */
10522 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10523 break;
10524 }
10525 case BUILT_IN_ATOMIC_STORE:
10526 {
10527 if (resolve_overloaded_atomic_store (loc, function, params,
10528 &new_return))
10529 return new_return;
10530 /* Change to the _N variant. */
10531 orig_code = BUILT_IN_ATOMIC_STORE_N;
10532 break;
10533 }
10534 default:
10535 gcc_unreachable ();
10536 }
10537 /* Fallthrough to the normal processing. */
10538 }
10539 case BUILT_IN_ATOMIC_EXCHANGE_N:
10540 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10541 case BUILT_IN_ATOMIC_LOAD_N:
10542 case BUILT_IN_ATOMIC_STORE_N:
10543 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10544 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10545 case BUILT_IN_ATOMIC_AND_FETCH_N:
10546 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10547 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10548 case BUILT_IN_ATOMIC_OR_FETCH_N:
10549 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10550 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10551 case BUILT_IN_ATOMIC_FETCH_AND_N:
10552 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10553 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10554 case BUILT_IN_ATOMIC_FETCH_OR_N:
10555 {
10556 orig_format = false;
10557 /* Fallthru for parameter processing. */
10558 }
2797f13a 10559 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10560 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10561 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10562 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10563 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10564 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10565 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10566 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10567 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10568 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10569 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10570 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10571 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10572 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10573 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10574 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 10575 {
10576 int n = sync_resolve_size (function, params);
b9c74b4d 10577 tree new_function, first_param, result;
b9a16870 10578 enum built_in_function fncode;
b6a5fc45 10579
10580 if (n == 0)
10581 return error_mark_node;
10582
b9a16870 10583 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10584 new_function = builtin_decl_explicit (fncode);
1cd6e20d 10585 if (!sync_resolve_params (loc, function, new_function, params,
10586 orig_format))
b6a5fc45 10587 return error_mark_node;
10588
f1f41a6c 10589 first_param = (*params)[0];
e60a6f7b 10590 result = build_function_call_vec (loc, new_function, params, NULL);
1cd6e20d 10591 if (result == error_mark_node)
10592 return result;
2797f13a 10593 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 10594 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10595 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10596 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 10597
1cd6e20d 10598 /* If new_return is set, assign function to that expr and cast the
10599 result to void since the generic interface returned void. */
10600 if (new_return)
10601 {
10602 /* Cast function result from I{1,2,4,8,16} to the required type. */
10603 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10604 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10605 result);
10606 TREE_SIDE_EFFECTS (result) = 1;
10607 protected_set_expr_location (result, loc);
10608 result = convert (void_type_node, result);
10609 }
b6a5fc45 10610 return result;
10611 }
10612
10613 default:
65441f6f 10614 return NULL_TREE;
b6a5fc45 10615 }
10616}
10617
491255f5 10618/* Ignoring their sign, return true if two scalar types are the same. */
10619bool
10620same_scalar_type_ignoring_signedness (tree t1, tree t2)
10621{
10622 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10623
9421ebb9 10624 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10625 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10626 || c2 == FIXED_POINT_TYPE));
491255f5 10627
10628 /* Equality works here because c_common_signed_type uses
10629 TYPE_MAIN_VARIANT. */
11773141 10630 return c_common_signed_type (t1)
10631 == c_common_signed_type (t2);
491255f5 10632}
10633
be7350e7 10634/* Check for missing format attributes on function pointers. LTYPE is
10635 the new type or left-hand side type. RTYPE is the old type or
10636 right-hand side type. Returns TRUE if LTYPE is missing the desired
10637 attribute. */
10638
10639bool
10640check_missing_format_attribute (tree ltype, tree rtype)
10641{
10642 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10643 tree ra;
10644
10645 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10646 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10647 break;
10648 if (ra)
10649 {
10650 tree la;
10651 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10652 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10653 break;
10654 return !la;
10655 }
10656 else
10657 return false;
10658}
10659
2840aae4 10660/* Subscripting with type char is likely to lose on a machine where
10661 chars are signed. So warn on any machine, but optionally. Don't
10662 warn for unsigned char since that type is safe. Don't warn for
10663 signed char because anyone who uses that must have done so
10664 deliberately. Furthermore, we reduce the false positive load by
10665 warning only for non-constant value of type char. */
10666
10667void
10668warn_array_subscript_with_type_char (tree index)
10669{
10670 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10671 && TREE_CODE (index) != INTEGER_CST)
10672 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10673}
10674
e534436e 10675/* Implement -Wparentheses for the unexpected C precedence rules, to
10676 cover cases like x + y << z which readers are likely to
10677 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 10678 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10679 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10680 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10681 expression was not formed using a binary or unary operator, or it
10682 was enclosed in parentheses. */
e534436e 10683
10684void
b0e7825e 10685warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 10686 enum tree_code code_left, tree arg_left,
82012ffe 10687 enum tree_code code_right, tree arg_right)
e534436e 10688{
10689 if (!warn_parentheses)
10690 return;
10691
82012ffe 10692 /* This macro tests that the expression ARG with original tree code
10693 CODE appears to be a boolean expression. or the result of folding a
10694 boolean expression. */
10695#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10696 (truth_value_p (TREE_CODE (ARG)) \
10697 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10698 /* Folding may create 0 or 1 integers from other expressions. */ \
10699 || ((CODE) != INTEGER_CST \
10700 && (integer_onep (ARG) || integer_zerop (ARG))))
10701
48e1416a 10702 switch (code)
e534436e 10703 {
82012ffe 10704 case LSHIFT_EXPR:
b0e7825e 10705 if (code_left == PLUS_EXPR)
10706 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10707 "suggest parentheses around %<+%> inside %<<<%>");
10708 else if (code_right == PLUS_EXPR)
10709 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10710 "suggest parentheses around %<+%> inside %<<<%>");
10711 else if (code_left == MINUS_EXPR)
10712 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10713 "suggest parentheses around %<-%> inside %<<<%>");
10714 else if (code_right == MINUS_EXPR)
10715 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10716 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 10717 return;
e534436e 10718
82012ffe 10719 case RSHIFT_EXPR:
b0e7825e 10720 if (code_left == PLUS_EXPR)
10721 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10722 "suggest parentheses around %<+%> inside %<>>%>");
10723 else if (code_right == PLUS_EXPR)
10724 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10725 "suggest parentheses around %<+%> inside %<>>%>");
10726 else if (code_left == MINUS_EXPR)
10727 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10728 "suggest parentheses around %<-%> inside %<>>%>");
10729 else if (code_right == MINUS_EXPR)
10730 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10731 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 10732 return;
e534436e 10733
82012ffe 10734 case TRUTH_ORIF_EXPR:
b0e7825e 10735 if (code_left == TRUTH_ANDIF_EXPR)
10736 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10737 "suggest parentheses around %<&&%> within %<||%>");
10738 else if (code_right == TRUTH_ANDIF_EXPR)
10739 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10740 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 10741 return;
10742
10743 case BIT_IOR_EXPR:
e534436e 10744 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 10745 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
10746 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10747 "suggest parentheses around arithmetic in operand of %<|%>");
10748 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10749 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10750 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 10751 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 10752 /* Check cases like x|y==z */
b0e7825e 10753 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10754 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10755 "suggest parentheses around comparison in operand of %<|%>");
10756 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10757 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 10758 "suggest parentheses around comparison in operand of %<|%>");
10759 /* Check cases like !x | y */
10760 else if (code_left == TRUTH_NOT_EXPR
10761 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 10762 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10763 "suggest parentheses around operand of "
10764 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 10765 return;
e534436e 10766
82012ffe 10767 case BIT_XOR_EXPR:
e534436e 10768 if (code_left == BIT_AND_EXPR
b0e7825e 10769 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
10770 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10771 "suggest parentheses around arithmetic in operand of %<^%>");
10772 else if (code_right == BIT_AND_EXPR
10773 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10774 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 10775 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 10776 /* Check cases like x^y==z */
b0e7825e 10777 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10778 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10779 "suggest parentheses around comparison in operand of %<^%>");
10780 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10781 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 10782 "suggest parentheses around comparison in operand of %<^%>");
10783 return;
e534436e 10784
82012ffe 10785 case BIT_AND_EXPR:
b0e7825e 10786 if (code_left == PLUS_EXPR)
10787 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10788 "suggest parentheses around %<+%> in operand of %<&%>");
10789 else if (code_right == PLUS_EXPR)
10790 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 10791 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 10792 else if (code_left == MINUS_EXPR)
10793 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10794 "suggest parentheses around %<-%> in operand of %<&%>");
10795 else if (code_right == MINUS_EXPR)
10796 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 10797 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 10798 /* Check cases like x&y==z */
b0e7825e 10799 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10800 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10801 "suggest parentheses around comparison in operand of %<&%>");
10802 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10803 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 10804 "suggest parentheses around comparison in operand of %<&%>");
10805 /* Check cases like !x & y */
10806 else if (code_left == TRUTH_NOT_EXPR
10807 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 10808 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10809 "suggest parentheses around operand of "
10810 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 10811 return;
e534436e 10812
82012ffe 10813 case EQ_EXPR:
b0e7825e 10814 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10815 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10816 "suggest parentheses around comparison in operand of %<==%>");
10817 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10818 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 10819 "suggest parentheses around comparison in operand of %<==%>");
10820 return;
10821 case NE_EXPR:
b0e7825e 10822 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10823 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10824 "suggest parentheses around comparison in operand of %<!=%>");
10825 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10826 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 10827 "suggest parentheses around comparison in operand of %<!=%>");
10828 return;
10829
10830 default:
b0e7825e 10831 if (TREE_CODE_CLASS (code) == tcc_comparison)
10832 {
10833 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 10834 && code_left != NE_EXPR && code_left != EQ_EXPR
10835 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 10836 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10837 "comparisons like %<X<=Y<=Z%> do not "
10838 "have their mathematical meaning");
10839 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 10840 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 10841 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
10842 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10843 "comparisons like %<X<=Y<=Z%> do not "
10844 "have their mathematical meaning");
10845 }
82012ffe 10846 return;
6ce0c450 10847 }
82012ffe 10848#undef NOT_A_BOOLEAN_EXPR_P
e534436e 10849}
10850
92fccaaa 10851/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
10852
10853void
10854warn_for_unused_label (tree label)
10855{
10856 if (!TREE_USED (label))
10857 {
10858 if (DECL_INITIAL (label))
10859 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
10860 else
10861 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
10862 }
10863}
2840aae4 10864
b6889cb0 10865/* Warn for division by zero according to the value of DIVISOR. LOC
10866 is the location of the division operator. */
f092582b 10867
10868void
b6889cb0 10869warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 10870{
9421ebb9 10871 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
10872 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 10873 floating-point type, since we consider 0.0/0.0 a valid way of
10874 generating a NaN. */
48d94ede 10875 if (c_inhibit_evaluation_warnings == 0
9421ebb9 10876 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 10877 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 10878}
10879
13869a99 10880/* Subroutine of build_binary_op. Give warnings for comparisons
10881 between signed and unsigned quantities that may fail. Do the
10882 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
10883 so that casts will be considered, but default promotions won't
8e70fb09 10884 be.
10885
10886 LOCATION is the location of the comparison operator.
13869a99 10887
10888 The arguments of this function map directly to local variables
10889 of build_binary_op. */
10890
48e1416a 10891void
8e70fb09 10892warn_for_sign_compare (location_t location,
48e1416a 10893 tree orig_op0, tree orig_op1,
10894 tree op0, tree op1,
13869a99 10895 tree result_type, enum tree_code resultcode)
10896{
10897 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
10898 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
10899 int unsignedp0, unsignedp1;
48e1416a 10900
13869a99 10901 /* In C++, check for comparison of different enum types. */
10902 if (c_dialect_cxx()
10903 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
10904 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
10905 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 10906 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 10907 {
8e70fb09 10908 warning_at (location,
10909 OPT_Wsign_compare, "comparison between types %qT and %qT",
10910 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 10911 }
10912
10913 /* Do not warn if the comparison is being done in a signed type,
10914 since the signed type will only be chosen if it can represent
10915 all the values of the unsigned type. */
10916 if (!TYPE_UNSIGNED (result_type))
10917 /* OK */;
10918 /* Do not warn if both operands are unsigned. */
10919 else if (op0_signed == op1_signed)
10920 /* OK */;
10921 else
10922 {
895b662f 10923 tree sop, uop, base_type;
13869a99 10924 bool ovf;
895b662f 10925
13869a99 10926 if (op0_signed)
10927 sop = orig_op0, uop = orig_op1;
48e1416a 10928 else
13869a99 10929 sop = orig_op1, uop = orig_op0;
10930
48e1416a 10931 STRIP_TYPE_NOPS (sop);
13869a99 10932 STRIP_TYPE_NOPS (uop);
895b662f 10933 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
10934 ? TREE_TYPE (result_type) : result_type);
13869a99 10935
10936 /* Do not warn if the signed quantity is an unsuffixed integer
10937 literal (or some static constant expression involving such
10938 literals or a conditional expression involving such literals)
10939 and it is non-negative. */
10940 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
10941 /* OK */;
10942 /* Do not warn if the comparison is an equality operation, the
10943 unsigned quantity is an integral constant, and it would fit
10944 in the result if the result were signed. */
10945 else if (TREE_CODE (uop) == INTEGER_CST
10946 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 10947 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 10948 /* OK */;
10949 /* In C, do not warn if the unsigned quantity is an enumeration
10950 constant and its maximum value would fit in the result if the
10951 result were signed. */
10952 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
10953 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
10954 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 10955 c_common_signed_type (base_type)))
13869a99 10956 /* OK */;
48e1416a 10957 else
8e70fb09 10958 warning_at (location,
48e1416a 10959 OPT_Wsign_compare,
8e70fb09 10960 "comparison between signed and unsigned integer expressions");
13869a99 10961 }
48e1416a 10962
13869a99 10963 /* Warn if two unsigned values are being compared in a size larger
10964 than their original size, and one (and only one) is the result of
10965 a `~' operator. This comparison will always fail.
48e1416a 10966
13869a99 10967 Also warn if one operand is a constant, and the constant does not
10968 have all bits set that are set in the ~ operand when it is
10969 extended. */
10970
7f506bca 10971 op0 = c_common_get_narrower (op0, &unsignedp0);
10972 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 10973
13869a99 10974 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
10975 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
10976 {
10977 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 10978 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 10979 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 10980 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 10981
10982 if (host_integerp (op0, 0) || host_integerp (op1, 0))
10983 {
10984 tree primop;
10985 HOST_WIDE_INT constant, mask;
10986 int unsignedp;
10987 unsigned int bits;
48e1416a 10988
13869a99 10989 if (host_integerp (op0, 0))
10990 {
10991 primop = op1;
10992 unsignedp = unsignedp1;
10993 constant = tree_low_cst (op0, 0);
10994 }
10995 else
10996 {
10997 primop = op0;
10998 unsignedp = unsignedp0;
10999 constant = tree_low_cst (op1, 0);
11000 }
48e1416a 11001
13869a99 11002 bits = TYPE_PRECISION (TREE_TYPE (primop));
11003 if (bits < TYPE_PRECISION (result_type)
11004 && bits < HOST_BITS_PER_LONG && unsignedp)
11005 {
11006 mask = (~ (HOST_WIDE_INT) 0) << bits;
11007 if ((mask & constant) != mask)
11008 {
11009 if (constant == 0)
48e1416a 11010 warning (OPT_Wsign_compare,
13869a99 11011 "promoted ~unsigned is always non-zero");
11012 else
48e1416a 11013 warning_at (location, OPT_Wsign_compare,
8e70fb09 11014 "comparison of promoted ~unsigned with constant");
13869a99 11015 }
11016 }
11017 }
11018 else if (unsignedp0 && unsignedp1
11019 && (TYPE_PRECISION (TREE_TYPE (op0))
11020 < TYPE_PRECISION (result_type))
11021 && (TYPE_PRECISION (TREE_TYPE (op1))
11022 < TYPE_PRECISION (result_type)))
8e70fb09 11023 warning_at (location, OPT_Wsign_compare,
13869a99 11024 "comparison of promoted ~unsigned with unsigned");
11025 }
11026}
11027
5ba33bf4 11028/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11029 type via c_common_type. If -Wdouble-promotion is in use, and the
11030 conditions for warning have been met, issue a warning. GMSGID is
11031 the warning message. It must have two %T specifiers for the type
11032 that was converted (generally "float") and the type to which it was
11033 converted (generally "double), respectively. LOC is the location
11034 to which the awrning should refer. */
11035
11036void
11037do_warn_double_promotion (tree result_type, tree type1, tree type2,
11038 const char *gmsgid, location_t loc)
11039{
11040 tree source_type;
11041
11042 if (!warn_double_promotion)
11043 return;
11044 /* If the conversion will not occur at run-time, there is no need to
11045 warn about it. */
11046 if (c_inhibit_evaluation_warnings)
11047 return;
11048 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11049 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11050 return;
11051 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11052 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11053 source_type = type1;
11054 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11055 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11056 source_type = type2;
11057 else
11058 return;
11059 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11060}
11061
41771881 11062/* Setup a TYPE_DECL node as a typedef representation.
11063
11064 X is a TYPE_DECL for a typedef statement. Create a brand new
11065 ..._TYPE node (which will be just a variant of the existing
11066 ..._TYPE node with identical properties) and then install X
11067 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11068
11069 The whole point here is to end up with a situation where each
11070 and every ..._TYPE node the compiler creates will be uniquely
11071 associated with AT MOST one node representing a typedef name.
11072 This way, even though the compiler substitutes corresponding
11073 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11074 early on, later parts of the compiler can always do the reverse
11075 translation and get back the corresponding typedef name. For
11076 example, given:
11077
11078 typedef struct S MY_TYPE;
11079 MY_TYPE object;
11080
11081 Later parts of the compiler might only know that `object' was of
11082 type `struct S' if it were not for code just below. With this
11083 code however, later parts of the compiler see something like:
11084
11085 struct S' == struct S
11086 typedef struct S' MY_TYPE;
11087 struct S' object;
11088
11089 And they can then deduce (from the node for type struct S') that
11090 the original object declaration was:
11091
11092 MY_TYPE object;
11093
11094 Being able to do this is important for proper support of protoize,
11095 and also for generating precise symbolic debugging information
11096 which takes full account of the programmer's (typedef) vocabulary.
11097
11098 Obviously, we don't want to generate a duplicate ..._TYPE node if
11099 the TYPE_DECL node that we are now processing really represents a
11100 standard built-in type. */
11101
11102void
11103set_underlying_type (tree x)
11104{
11105 if (x == error_mark_node)
11106 return;
11107 if (DECL_IS_BUILTIN (x))
11108 {
11109 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11110 TYPE_NAME (TREE_TYPE (x)) = x;
11111 }
11112 else if (TREE_TYPE (x) != error_mark_node
11113 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11114 {
11115 tree tt = TREE_TYPE (x);
11116 DECL_ORIGINAL_TYPE (x) = tt;
11117 tt = build_variant_type_copy (tt);
11118 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11119 TYPE_NAME (tt) = x;
11120 TREE_USED (tt) = TREE_USED (x);
11121 TREE_TYPE (x) = tt;
11122 }
11123}
11124
1a4c44c5 11125/* Record the types used by the current global variable declaration
11126 being parsed, so that we can decide later to emit their debug info.
11127 Those types are in types_used_by_cur_var_decl, and we are going to
11128 store them in the types_used_by_vars_hash hash table.
11129 DECL is the declaration of the global variable that has been parsed. */
11130
11131void
11132record_types_used_by_current_var_decl (tree decl)
11133{
11134 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11135
f1f41a6c 11136 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 11137 {
f1f41a6c 11138 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 11139 types_used_by_var_decl_insert (type, decl);
1a4c44c5 11140 }
11141}
11142
a4e3ffad 11143/* If DECL is a typedef that is declared in the current function,
11144 record it for the purpose of -Wunused-local-typedefs. */
11145
11146void
11147record_locally_defined_typedef (tree decl)
11148{
11149 struct c_language_function *l;
11150
11151 if (!warn_unused_local_typedefs
11152 || cfun == NULL
11153 /* if this is not a locally defined typedef then we are not
11154 interested. */
11155 || !is_typedef_decl (decl)
11156 || !decl_function_context (decl))
11157 return;
11158
11159 l = (struct c_language_function *) cfun->language;
f1f41a6c 11160 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 11161}
11162
11163/* If T is a TYPE_DECL declared locally, mark it as used. */
11164
11165void
11166maybe_record_typedef_use (tree t)
11167{
11168 if (!is_typedef_decl (t))
11169 return;
11170
11171 TREE_USED (t) = true;
11172}
11173
11174/* Warn if there are some unused locally defined typedefs in the
11175 current function. */
11176
11177void
11178maybe_warn_unused_local_typedefs (void)
11179{
11180 int i;
11181 tree decl;
11182 /* The number of times we have emitted -Wunused-local-typedefs
11183 warnings. If this is different from errorcount, that means some
11184 unrelated errors have been issued. In which case, we'll avoid
11185 emitting "unused-local-typedefs" warnings. */
11186 static int unused_local_typedefs_warn_count;
11187 struct c_language_function *l;
11188
11189 if (cfun == NULL)
11190 return;
11191
11192 if ((l = (struct c_language_function *) cfun->language) == NULL)
11193 return;
11194
11195 if (warn_unused_local_typedefs
11196 && errorcount == unused_local_typedefs_warn_count)
11197 {
f1f41a6c 11198 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 11199 if (!TREE_USED (decl))
11200 warning_at (DECL_SOURCE_LOCATION (decl),
11201 OPT_Wunused_local_typedefs,
11202 "typedef %qD locally defined but not used", decl);
11203 unused_local_typedefs_warn_count = errorcount;
11204 }
11205
f1f41a6c 11206 vec_free (l->local_typedefs);
a4e3ffad 11207}
11208
f352a3fb 11209/* The C and C++ parsers both use vectors to hold function arguments.
11210 For efficiency, we keep a cache of unused vectors. This is the
11211 cache. */
11212
f1f41a6c 11213typedef vec<tree, va_gc> *tree_gc_vec;
11214static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 11215
11216/* Return a new vector from the cache. If the cache is empty,
11217 allocate a new vector. These vectors are GC'ed, so it is OK if the
11218 pointer is not released.. */
11219
f1f41a6c 11220vec<tree, va_gc> *
f352a3fb 11221make_tree_vector (void)
11222{
f1f41a6c 11223 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11224 return tree_vector_cache->pop ();
f352a3fb 11225 else
11226 {
f1f41a6c 11227 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 11228 that we always return a non-NULL value. The vector code uses
11229 4 when growing a NULL vector, so we do too. */
f1f41a6c 11230 vec<tree, va_gc> *v;
11231 vec_alloc (v, 4);
11232 return v;
f352a3fb 11233 }
11234}
11235
11236/* Release a vector of trees back to the cache. */
11237
11238void
f1f41a6c 11239release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 11240{
11241 if (vec != NULL)
11242 {
f1f41a6c 11243 vec->truncate (0);
11244 vec_safe_push (tree_vector_cache, vec);
f352a3fb 11245 }
11246}
11247
11248/* Get a new tree vector holding a single tree. */
11249
f1f41a6c 11250vec<tree, va_gc> *
f352a3fb 11251make_tree_vector_single (tree t)
11252{
f1f41a6c 11253 vec<tree, va_gc> *ret = make_tree_vector ();
11254 ret->quick_push (t);
f352a3fb 11255 return ret;
11256}
11257
c66c81be 11258/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11259
f1f41a6c 11260vec<tree, va_gc> *
c66c81be 11261make_tree_vector_from_list (tree list)
11262{
f1f41a6c 11263 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 11264 for (; list; list = TREE_CHAIN (list))
f1f41a6c 11265 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 11266 return ret;
11267}
11268
f352a3fb 11269/* Get a new tree vector which is a copy of an existing one. */
11270
f1f41a6c 11271vec<tree, va_gc> *
11272make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 11273{
f1f41a6c 11274 vec<tree, va_gc> *ret;
f352a3fb 11275 unsigned int ix;
11276 tree t;
11277
11278 ret = make_tree_vector ();
f1f41a6c 11279 vec_safe_reserve (ret, vec_safe_length (orig));
11280 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11281 ret->quick_push (t);
f352a3fb 11282 return ret;
11283}
11284
a9ffdd35 11285/* Return true if KEYWORD starts a type specifier. */
11286
11287bool
11288keyword_begins_type_specifier (enum rid keyword)
11289{
11290 switch (keyword)
11291 {
11292 case RID_INT:
11293 case RID_CHAR:
11294 case RID_FLOAT:
11295 case RID_DOUBLE:
11296 case RID_VOID:
11297 case RID_INT128:
11298 case RID_UNSIGNED:
11299 case RID_LONG:
11300 case RID_SHORT:
11301 case RID_SIGNED:
11302 case RID_DFLOAT32:
11303 case RID_DFLOAT64:
11304 case RID_DFLOAT128:
11305 case RID_FRACT:
11306 case RID_ACCUM:
11307 case RID_BOOL:
11308 case RID_WCHAR:
11309 case RID_CHAR16:
11310 case RID_CHAR32:
11311 case RID_SAT:
11312 case RID_COMPLEX:
11313 case RID_TYPEOF:
11314 case RID_STRUCT:
11315 case RID_CLASS:
11316 case RID_UNION:
11317 case RID_ENUM:
11318 return true;
11319 default:
11320 return false;
11321 }
11322}
11323
11324/* Return true if KEYWORD names a type qualifier. */
11325
11326bool
11327keyword_is_type_qualifier (enum rid keyword)
11328{
11329 switch (keyword)
11330 {
11331 case RID_CONST:
11332 case RID_VOLATILE:
11333 case RID_RESTRICT:
11334 return true;
11335 default:
11336 return false;
11337 }
11338}
11339
11340/* Return true if KEYWORD names a storage class specifier.
11341
11342 RID_TYPEDEF is not included in this list despite `typedef' being
11343 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11344 such for syntactic convenience only. */
11345
11346bool
11347keyword_is_storage_class_specifier (enum rid keyword)
11348{
11349 switch (keyword)
11350 {
11351 case RID_STATIC:
11352 case RID_EXTERN:
11353 case RID_REGISTER:
11354 case RID_AUTO:
11355 case RID_MUTABLE:
11356 case RID_THREAD:
11357 return true;
11358 default:
11359 return false;
11360 }
11361}
11362
fad3f658 11363/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11364
11365static bool
11366keyword_is_function_specifier (enum rid keyword)
11367{
11368 switch (keyword)
11369 {
11370 case RID_INLINE:
985c6e3a 11371 case RID_NORETURN:
fad3f658 11372 case RID_VIRTUAL:
11373 case RID_EXPLICIT:
11374 return true;
11375 default:
11376 return false;
11377 }
11378}
11379
11380/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11381 declaration-specifier (C99 6.7). */
11382
11383bool
11384keyword_is_decl_specifier (enum rid keyword)
11385{
11386 if (keyword_is_storage_class_specifier (keyword)
11387 || keyword_is_type_qualifier (keyword)
11388 || keyword_is_function_specifier (keyword))
11389 return true;
11390
11391 switch (keyword)
11392 {
11393 case RID_TYPEDEF:
11394 case RID_FRIEND:
11395 case RID_CONSTEXPR:
11396 return true;
11397 default:
11398 return false;
11399 }
11400}
11401
9b88d08d 11402/* Initialize language-specific-bits of tree_contains_struct. */
11403
11404void
11405c_common_init_ts (void)
11406{
11407 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11408 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11409}
11410
244db24d 11411/* Build a user-defined numeric literal out of an integer constant type VALUE
11412 with identifier SUFFIX. */
11413
11414tree
324ca377 11415build_userdef_literal (tree suffix_id, tree value,
11416 enum overflow_type overflow, tree num_string)
244db24d 11417{
11418 tree literal = make_node (USERDEF_LITERAL);
11419 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11420 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 11421 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 11422 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11423 return literal;
11424}
11425
7059d45d 11426/* For vector[index], convert the vector to a
11427 pointer of the underlying type. */
11428void
11429convert_vector_to_pointer_for_subscript (location_t loc,
11430 tree* vecp, tree index)
11431{
11432 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11433 {
11434 tree type = TREE_TYPE (*vecp);
11435 tree type1;
11436
11437 if (TREE_CODE (index) == INTEGER_CST)
11438 if (!host_integerp (index, 1)
11439 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11440 >= TYPE_VECTOR_SUBPARTS (type)))
11441 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11442
11443 c_common_mark_addressable_vec (*vecp);
11444 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11445 type = build_pointer_type (type);
11446 type1 = build_pointer_type (TREE_TYPE (*vecp));
11447 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11448 *vecp = convert (type, *vecp);
11449 }
11450}
11451
41ed701a 11452/* Determine which of the operands, if any, is a scalar that needs to be
11453 converted to a vector, for the range of operations. */
11454enum stv_conv
11455scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11456 bool complain)
11457{
11458 tree type0 = TREE_TYPE (op0);
11459 tree type1 = TREE_TYPE (op1);
11460 bool integer_only_op = false;
11461 enum stv_conv ret = stv_firstarg;
11462
11463 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11464 || TREE_CODE (type1) == VECTOR_TYPE);
11465 switch (code)
11466 {
11467 /* Most GENERIC binary expressions require homogeneous arguments.
11468 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11469 argument that is a vector and a second one that is a scalar, so
11470 we never return stv_secondarg for them. */
11471 case RSHIFT_EXPR:
11472 case LSHIFT_EXPR:
11473 if (TREE_CODE (type0) == INTEGER_TYPE
11474 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11475 {
11476 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11477 {
11478 if (complain)
11479 error_at (loc, "conversion of scalar %qT to vector %qT "
11480 "involves truncation", type0, type1);
11481 return stv_error;
11482 }
11483 else
11484 return stv_firstarg;
11485 }
11486 break;
11487
11488 case BIT_IOR_EXPR:
11489 case BIT_XOR_EXPR:
11490 case BIT_AND_EXPR:
11491 integer_only_op = true;
11492 /* ... fall through ... */
11493
7b463b19 11494 case VEC_COND_EXPR:
11495
41ed701a 11496 case PLUS_EXPR:
11497 case MINUS_EXPR:
11498 case MULT_EXPR:
11499 case TRUNC_DIV_EXPR:
11500 case CEIL_DIV_EXPR:
11501 case FLOOR_DIV_EXPR:
11502 case ROUND_DIV_EXPR:
11503 case EXACT_DIV_EXPR:
11504 case TRUNC_MOD_EXPR:
11505 case FLOOR_MOD_EXPR:
11506 case RDIV_EXPR:
11507 case EQ_EXPR:
11508 case NE_EXPR:
11509 case LE_EXPR:
11510 case GE_EXPR:
11511 case LT_EXPR:
11512 case GT_EXPR:
11513 /* What about UNLT_EXPR? */
11514 if (TREE_CODE (type0) == VECTOR_TYPE)
11515 {
11516 tree tmp;
11517 ret = stv_secondarg;
11518 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11519 tmp = type0; type0 = type1; type1 = tmp;
11520 tmp = op0; op0 = op1; op1 = tmp;
11521 }
11522
11523 if (TREE_CODE (type0) == INTEGER_TYPE
11524 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11525 {
11526 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11527 {
11528 if (complain)
11529 error_at (loc, "conversion of scalar %qT to vector %qT "
11530 "involves truncation", type0, type1);
11531 return stv_error;
11532 }
11533 return ret;
11534 }
11535 else if (!integer_only_op
11536 /* Allow integer --> real conversion if safe. */
11537 && (TREE_CODE (type0) == REAL_TYPE
11538 || TREE_CODE (type0) == INTEGER_TYPE)
11539 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11540 {
11541 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11542 {
11543 if (complain)
11544 error_at (loc, "conversion of scalar %qT to vector %qT "
11545 "involves truncation", type0, type1);
11546 return stv_error;
11547 }
11548 return ret;
11549 }
11550 default:
11551 break;
11552 }
11553
11554 return stv_nothing;
11555}
11556
ffcdbf9c 11557/* Return true iff ALIGN is an integral constant that is a fundamental
11558 alignment, as defined by [basic.align] in the c++-11
11559 specifications.
11560
11561 That is:
11562
11563 [A fundamental alignment is represented by an alignment less than or
11564 equal to the greatest alignment supported by the implementation
11565 in all contexts, which is equal to
11566 alignof(max_align_t)]. */
11567
11568bool
11569cxx_fundamental_alignment_p (unsigned align)
11570{
11571 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11572 TYPE_ALIGN (long_double_type_node)));
11573}
11574
7bedc3a0 11575#include "gt-c-family-c-common.h"