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