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