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