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