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