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