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