]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
Handle restrict pointer references as restrict in AA
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
5624e564 2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
b30f223b 3
1322177d 4This file is part of GCC.
b30f223b 5
1322177d
LB
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
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
b30f223b 10
1322177d
LB
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.
b30f223b
RS
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
b30f223b 19
40e23961
MC
20#define GCC_C_COMMON_C
21
b30f223b 22#include "config.h"
670ee920 23#include "system.h"
4977bab6 24#include "coretypes.h"
b559c810 25#include "c-common.h"
4977bab6 26#include "tm.h"
d9b2742a 27#include "intl.h"
b30f223b 28#include "tree.h"
d8a2d370
DN
29#include "fold-const.h"
30#include "stor-layout.h"
31#include "calls.h"
32#include "stringpool.h"
33#include "attribs.h"
34#include "varasm.h"
35#include "trans-mem.h"
b30f223b 36#include "flags.h"
e2af664c 37#include "c-pragma.h"
61d3ce20 38#include "c-objc.h"
7bdb32b9 39#include "tm_p.h"
235cfbc4 40#include "obstack.h"
c8724862 41#include "cpplib.h"
12a68f1f 42#include "target.h"
677f3fa8 43#include "common/common-target.h"
7afff7cf 44#include "langhooks.h"
d57a4b98 45#include "tree-inline.h"
5f1989e6 46#include "toplev.h"
148e4216 47#include "diagnostic.h"
6de9cd9a 48#include "tree-iterator.h"
d974312d 49#include "opts.h"
c582198b 50#include "hard-reg-set.h"
c582198b 51#include "function.h"
ce91e74c 52#include "cgraph.h"
45b0be94 53#include "gimplify.h"
807e902e 54#include "wide-int-print.h"
aa7da51a 55#include "gimple-expr.h"
cb60f38d 56
81a75f0f 57cpp_reader *parse_in; /* Declared in c-pragma.h. */
c8724862 58
8fba1830
BRF
59/* Mode used to build pointers (VOIDmode means ptr_mode). */
60
61machine_mode c_default_pointer_mode = VOIDmode;
62
7f4edbcb 63/* The following symbols are subsumed in the c_global_trees array, and
d125d268 64 listed here individually for documentation purposes.
7f4edbcb
BS
65
66 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
67
68 tree short_integer_type_node;
69 tree long_integer_type_node;
70 tree long_long_integer_type_node;
71
72 tree short_unsigned_type_node;
73 tree long_unsigned_type_node;
74 tree long_long_unsigned_type_node;
75
de7df9eb
JM
76 tree truthvalue_type_node;
77 tree truthvalue_false_node;
78 tree truthvalue_true_node;
7f4edbcb
BS
79
80 tree ptrdiff_type_node;
81
82 tree unsigned_char_type_node;
83 tree signed_char_type_node;
84 tree wchar_type_node;
7f4edbcb 85
b6baa67d
KVH
86 tree char16_type_node;
87 tree char32_type_node;
88
7f4edbcb
BS
89 tree float_type_node;
90 tree double_type_node;
91 tree long_double_type_node;
92
93 tree complex_integer_type_node;
94 tree complex_float_type_node;
95 tree complex_double_type_node;
96 tree complex_long_double_type_node;
97
9a8ce21f
JG
98 tree dfloat32_type_node;
99 tree dfloat64_type_node;
100 tree_dfloat128_type_node;
101
7f4edbcb
BS
102 tree intQI_type_node;
103 tree intHI_type_node;
104 tree intSI_type_node;
105 tree intDI_type_node;
106 tree intTI_type_node;
107
108 tree unsigned_intQI_type_node;
109 tree unsigned_intHI_type_node;
110 tree unsigned_intSI_type_node;
111 tree unsigned_intDI_type_node;
112 tree unsigned_intTI_type_node;
113
114 tree widest_integer_literal_type_node;
115 tree widest_unsigned_literal_type_node;
116
117 Nodes for types `void *' and `const void *'.
118
119 tree ptr_type_node, const_ptr_type_node;
120
121 Nodes for types `char *' and `const char *'.
122
123 tree string_type_node, const_string_type_node;
124
125 Type `char[SOMENUMBER]'.
126 Used when an array of char is needed and the size is irrelevant.
127
128 tree char_array_type_node;
129
7f4edbcb
BS
130 Type `wchar_t[SOMENUMBER]' or something like it.
131 Used when a wide string literal is created.
132
133 tree wchar_array_type_node;
134
b6baa67d
KVH
135 Type `char16_t[SOMENUMBER]' or something like it.
136 Used when a UTF-16 string literal is created.
137
138 tree char16_array_type_node;
139
140 Type `char32_t[SOMENUMBER]' or something like it.
141 Used when a UTF-32 string literal is created.
142
143 tree char32_array_type_node;
144
7f4edbcb
BS
145 Type `int ()' -- used for implicit declaration of functions.
146
147 tree default_function_type;
148
7f4edbcb
BS
149 A VOID_TYPE node, packaged in a TREE_LIST.
150
151 tree void_list_node;
152
684d9f3b 153 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
0ba8a114
NS
154 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
155 VAR_DECLS, but C++ does.)
63ad61ed 156
0ba8a114 157 tree function_name_decl_node;
684d9f3b 158 tree pretty_function_name_decl_node;
0ba8a114
NS
159 tree c99_function_name_decl_node;
160
161 Stack of nested function name VAR_DECLs.
35b1a6fa 162
0ba8a114 163 tree saved_function_name_decls;
63ad61ed 164
7f4edbcb
BS
165*/
166
167tree c_global_trees[CTI_MAX];
17211ab5 168\f
4078b403
NB
169/* Switches common to the C front ends. */
170
63973df3
NB
171/* Nonzero means don't output line number information. */
172
173char flag_no_line_commands;
174
175/* Nonzero causes -E output not to be done, but directives such as
176 #define that have side effects are still obeyed. */
177
178char flag_no_output;
179
180/* Nonzero means dump macros in some fashion. */
181
182char flag_dump_macros;
183
184/* Nonzero means pass #include lines through to the output. */
185
186char flag_dump_includes;
187
c0d578e6
GK
188/* Nonzero means process PCH files while preprocessing. */
189
190bool flag_pch_preprocess;
191
17211ab5
GK
192/* The file name to which we should write a precompiled header, or
193 NULL if no header will be written in this compile. */
194
195const char *pch_file;
196
3df89291
NB
197/* Nonzero if an ISO standard was selected. It rejects macros in the
198 user's namespace. */
199int flag_iso;
200
4078b403
NB
201/* C/ObjC language option variables. */
202
203
4078b403
NB
204/* Nonzero means allow type mismatches in conditional expressions;
205 just make their values `void'. */
206
207int flag_cond_mismatch;
208
209/* Nonzero means enable C89 Amendment 1 features. */
210
211int flag_isoc94;
212
48b0b196 213/* Nonzero means use the ISO C99 (or C11) dialect of C. */
4078b403
NB
214
215int flag_isoc99;
216
48b0b196 217/* Nonzero means use the ISO C11 dialect of C. */
2778d766 218
48b0b196 219int flag_isoc11;
2778d766 220
6614fd40 221/* Nonzero means that we have builtin functions, and main is an int. */
4078b403
NB
222
223int flag_hosted = 1;
224
4078b403
NB
225
226/* ObjC language option variables. */
227
228
4078b403
NB
229/* Tells the compiler that this is a special run. Do not perform any
230 compiling, instead we are to test some platform dependent features
231 and output a C header file with appropriate definitions. */
232
233int print_struct_values;
234
fa10beec 235/* Tells the compiler what is the constant string class for ObjC. */
4078b403
NB
236
237const char *constant_string_class_name;
238
4078b403
NB
239
240/* C++ language option variables. */
241
242
4078b403
NB
243/* Nonzero means generate separate instantiation control files and
244 juggle them at link time. */
245
246int flag_use_repository;
247
129211bc 248/* The C++ dialect being used. Default set in c_common_post_options. */
966541e3 249
129211bc 250enum cxx_dialect cxx_dialect = cxx_unset;
966541e3 251
4afe7ad7 252/* Maximum template instantiation depth. This limit exists to limit the
3ff60975 253 time it takes to notice excessively recursive template instantiations.
4078b403 254
3ff60975
JM
255 The default is lower than the 1024 recommended by the C++0x standard
256 because G++ runs out of stack before 1024 with highly recursive template
257 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
258
259int max_tinst_depth = 900;
4078b403 260
f09f1de5
MM
261/* The elements of `ridpointers' are identifier nodes for the reserved
262 type names and storage classes. It is indexed by a RID_... value. */
263tree *ridpointers;
264
c2255bc4 265tree (*make_fname_decl) (location_t, tree, int);
2ce07e2d 266
7d882b83
ILT
267/* Nonzero means don't warn about problems that occur when the code is
268 executed. */
269int c_inhibit_evaluation_warnings;
e78a3b42 270
5386338c
JM
271/* Whether we are building a boolean conversion inside
272 convert_for_assignment, or some other late binary operation. If
273 build_binary_op is called for C (from code shared by C and C++) in
274 this case, then the operands have already been folded and the
275 result will not be folded again, so C_MAYBE_CONST_EXPR should not
276 be generated. */
277bool in_late_binary_op;
278
148e4216
JM
279/* Whether lexing has been completed, so subsequent preprocessor
280 errors should use the compiler's input_location. */
281bool done_lexing = false;
282
ec5c56db 283/* Information about how a function name is generated. */
0ba8a114
NS
284struct fname_var_t
285{
8b60264b
KG
286 tree *const decl; /* pointer to the VAR_DECL. */
287 const unsigned rid; /* RID number for the identifier. */
288 const int pretty; /* How pretty is it? */
0ba8a114
NS
289};
290
ec5c56db 291/* The three ways of getting then name of the current function. */
0ba8a114
NS
292
293const struct fname_var_t fname_vars[] =
294{
ec5c56db 295 /* C99 compliant __func__, must be first. */
0ba8a114 296 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
ec5c56db 297 /* GCC __FUNCTION__ compliant. */
0ba8a114 298 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
ec5c56db 299 /* GCC __PRETTY_FUNCTION__ compliant. */
0ba8a114
NS
300 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
301 {NULL, 0, 0},
302};
303
c98cd5bf
JM
304/* Global visibility options. */
305struct visibility_flags visibility_options;
306
3aa3c9fc 307static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
62e4eb35 308static tree check_case_value (location_t, tree);
b155cfd9
MP
309static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
310 bool *);
4724b3de 311
35b1a6fa
AJ
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 *);
52bf96d2
JH
316static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
317static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
e664c61c
KS
318static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
319 int, bool *);
77bc5132
JJ
320static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
321 int, bool *);
ce6923c5
MP
322static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
323 bool *);
5434dc07 324static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 325static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
86631ea3 326static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
185c9e56 327static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
46a4da10 328static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
329static tree handle_always_inline_attribute (tree *, tree, tree, int,
330 bool *);
d752cfdb
JJ
331static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
332static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0691d1d4 333static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
d2af6a68 334static tree handle_error_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
335static tree handle_used_attribute (tree *, tree, tree, int, bool *);
336static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
ce91e74c
JH
337static tree handle_externally_visible_attribute (tree *, tree, tree, int,
338 bool *);
7861b648
AK
339static tree handle_no_reorder_attribute (tree *, tree, tree, int,
340 bool *);
35b1a6fa
AJ
341static tree handle_const_attribute (tree *, tree, tree, int, bool *);
342static tree handle_transparent_union_attribute (tree *, tree, tree,
343 int, bool *);
344static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
345static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
346static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
347static tree handle_section_attribute (tree *, tree, tree, int, bool *);
348static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
349static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4bb794e2 350static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
ba885ec5
NS
351static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
352static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 353static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
a0203ca7 354static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
35b1a6fa
AJ
355static tree handle_visibility_attribute (tree *, tree, tree, int,
356 bool *);
357static tree handle_tls_model_attribute (tree *, tree, tree, int,
358 bool *);
359static tree handle_no_instrument_function_attribute (tree *, tree,
360 tree, int, bool *);
361static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
6e9a3221 362static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
363static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
364 bool *);
365static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
0a35513e
AH
366static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
367static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
dcd6de6d 368static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
369static tree handle_deprecated_attribute (tree *, tree, tree, int,
370 bool *);
371static tree handle_vector_size_attribute (tree *, tree, tree, int,
372 bool *);
373static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
374static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
375static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
72954a4f
JM
376static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
377 bool *);
3d091dac 378static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5d32c25 379static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
51bc54a6 380static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
8fcbce72
JJ
381static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
382static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
5779e713 383static tree handle_target_attribute (tree *, tree, tree, int, bool *);
ab442df7 384static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
0a35513e 385static tree ignore_attribute (tree *, tree, tree, int, bool *);
7458026b 386static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
0b7b376d 387static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
2a99e5e6 388static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
826cacfe 389static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
390static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
391 bool *);
392static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
393 bool *);
976d5a22 394static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
d5e254e1
IE
395static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
396static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
397static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
35b1a6fa 398
94a0dd7b 399static void check_function_nonnull (tree, int, tree *);
35b1a6fa
AJ
400static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
401static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
402static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
d07605f5 403static int resort_field_decl_cmp (const void *, const void *);
b34c7881 404
eea1139b
ILT
405/* Reserved words. The third field is a mask: keywords are disabled
406 if they match the mask.
407
408 Masks for languages:
409 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
410 C --std=c99: D_CXXONLY | D_OBJC
411 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
412 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
413 C++ --std=c0x: D_CONLY | D_OBJC
414 ObjC++ is like C++ except that D_OBJC is not set
415
416 If -fno-asm is used, D_ASM is added to the mask. If
417 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
418 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
1973201f 419 In C with -Wc++-compat, we warn if D_CXXWARN is set.
eea1139b 420
1973201f
NP
421 Note the complication of the D_CXX_OBJC keywords. These are
422 reserved words such as 'class'. In C++, 'class' is a reserved
423 word. In Objective-C++ it is too. In Objective-C, it is a
424 reserved word too, but only if it follows an '@' sign.
425*/
eea1139b
ILT
426const struct c_common_resword c_common_reswords[] =
427{
d19fa6b5
JM
428 { "_Alignas", RID_ALIGNAS, D_CONLY },
429 { "_Alignof", RID_ALIGNOF, D_CONLY },
267bac10 430 { "_Atomic", RID_ATOMIC, D_CONLY },
eea1139b
ILT
431 { "_Bool", RID_BOOL, D_CONLY },
432 { "_Complex", RID_COMPLEX, 0 },
939b37da
BI
433 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
434 { "_Cilk_sync", RID_CILK_SYNC, 0 },
9a771876 435 { "_Cilk_for", RID_CILK_FOR, 0 },
fa5da7de 436 { "_Imaginary", RID_IMAGINARY, D_CONLY },
eea1139b
ILT
437 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
438 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
439 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
440 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
441 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
442 { "_Sat", RID_SAT, D_CONLY | D_EXT },
32912286 443 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
bbceee64 444 { "_Noreturn", RID_NORETURN, D_CONLY },
433cc7b0 445 { "_Generic", RID_GENERIC, D_CONLY },
582d9b50 446 { "_Thread_local", RID_THREAD, D_CONLY },
eea1139b
ILT
447 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
448 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
449 { "__alignof", RID_ALIGNOF, 0 },
450 { "__alignof__", RID_ALIGNOF, 0 },
451 { "__asm", RID_ASM, 0 },
452 { "__asm__", RID_ASM, 0 },
453 { "__attribute", RID_ATTRIBUTE, 0 },
454 { "__attribute__", RID_ATTRIBUTE, 0 },
38b7bc7f 455 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
4daba884 456 { "__bases", RID_BASES, D_CXXONLY },
74893f25
RH
457 { "__builtin_call_with_static_chain",
458 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
eea1139b 459 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
d4a83c10 460 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
9e1a8dd1 461 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
eea1139b
ILT
462 { "__builtin_offsetof", RID_OFFSETOF, 0 },
463 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
464 { "__builtin_va_arg", RID_VA_ARG, 0 },
465 { "__complex", RID_COMPLEX, 0 },
466 { "__complex__", RID_COMPLEX, 0 },
467 { "__const", RID_CONST, 0 },
468 { "__const__", RID_CONST, 0 },
469 { "__decltype", RID_DECLTYPE, D_CXXONLY },
4daba884 470 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
eea1139b
ILT
471 { "__extension__", RID_EXTENSION, 0 },
472 { "__func__", RID_C99_FUNCTION_NAME, 0 },
473 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
474 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
475 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
476 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
477 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
478 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
479 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
480 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
3c0d13bf
PC
481 { "__imag", RID_IMAGPART, 0 },
482 { "__imag__", RID_IMAGPART, 0 },
483 { "__inline", RID_INLINE, 0 },
484 { "__inline__", RID_INLINE, 0 },
eea1139b
ILT
485 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
486 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
487 { "__is_class", RID_IS_CLASS, D_CXXONLY },
eea1139b
ILT
488 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
489 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
b3908fcc 490 { "__is_final", RID_IS_FINAL, D_CXXONLY },
3c0d13bf 491 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
eea1139b
ILT
492 { "__is_pod", RID_IS_POD, D_CXXONLY },
493 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
971e17ff 494 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c32097d8
JM
495 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
496 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
dd5d5481
JM
497 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
498 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
b752325e 499 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
eea1139b 500 { "__is_union", RID_IS_UNION, D_CXXONLY },
eea1139b
ILT
501 { "__label__", RID_LABEL, 0 },
502 { "__null", RID_NULL, 0 },
503 { "__real", RID_REALPART, 0 },
504 { "__real__", RID_REALPART, 0 },
505 { "__restrict", RID_RESTRICT, 0 },
506 { "__restrict__", RID_RESTRICT, 0 },
507 { "__signed", RID_SIGNED, 0 },
508 { "__signed__", RID_SIGNED, 0 },
509 { "__thread", RID_THREAD, 0 },
0a35513e
AH
510 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
511 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
512 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
eea1139b
ILT
513 { "__typeof", RID_TYPEOF, 0 },
514 { "__typeof__", RID_TYPEOF, 0 },
3c0d13bf 515 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
eea1139b
ILT
516 { "__volatile", RID_VOLATILE, 0 },
517 { "__volatile__", RID_VOLATILE, 0 },
36a85135
JM
518 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
519 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
520 { "asm", RID_ASM, D_ASM },
521 { "auto", RID_AUTO, 0 },
03c3034e 522 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
523 { "break", RID_BREAK, 0 },
524 { "case", RID_CASE, 0 },
2696a995 525 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
eea1139b 526 { "char", RID_CHAR, 0 },
36a85135
JM
527 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
528 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995 529 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
eea1139b 530 { "const", RID_CONST, 0 },
36a85135 531 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
532 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
533 { "continue", RID_CONTINUE, 0 },
36a85135 534 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b 535 { "default", RID_DEFAULT, 0 },
2696a995 536 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
537 { "do", RID_DO, 0 },
538 { "double", RID_DOUBLE, 0 },
539 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
540 { "else", RID_ELSE, 0 },
541 { "enum", RID_ENUM, 0 },
2696a995
KG
542 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
543 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
eea1139b 544 { "extern", RID_EXTERN, 0 },
2696a995 545 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
546 { "float", RID_FLOAT, 0 },
547 { "for", RID_FOR, 0 },
2696a995 548 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
549 { "goto", RID_GOTO, 0 },
550 { "if", RID_IF, 0 },
551 { "inline", RID_INLINE, D_EXT89 },
552 { "int", RID_INT, 0 },
553 { "long", RID_LONG, 0 },
554 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
2696a995
KG
555 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
556 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
36a85135
JM
557 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
558 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995
KG
559 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
560 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
561 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
562 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
eea1139b
ILT
563 { "register", RID_REGISTER, 0 },
564 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
565 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
566 { "return", RID_RETURN, 0 },
567 { "short", RID_SHORT, 0 },
568 { "signed", RID_SIGNED, 0 },
569 { "sizeof", RID_SIZEOF, 0 },
570 { "static", RID_STATIC, 0 },
36a85135 571 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
572 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
573 { "struct", RID_STRUCT, 0 },
574 { "switch", RID_SWITCH, 0 },
2696a995
KG
575 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
576 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
36a85135 577 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995
KG
578 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
579 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
580 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
eea1139b 581 { "typedef", RID_TYPEDEF, 0 },
2696a995
KG
582 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
583 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
584 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
585 { "union", RID_UNION, 0 },
586 { "unsigned", RID_UNSIGNED, 0 },
2696a995
KG
587 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
588 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
589 { "void", RID_VOID, 0 },
590 { "volatile", RID_VOLATILE, 0 },
591 { "wchar_t", RID_WCHAR, D_CXXONLY },
592 { "while", RID_WHILE, 0 },
971e17ff
AS
593
594 /* Concepts-related keywords */
595 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
596 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
597
eea1139b
ILT
598 /* These Objective-C keywords are recognized only immediately after
599 an '@'. */
600 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
601 { "defs", RID_AT_DEFS, D_OBJC },
602 { "encode", RID_AT_ENCODE, D_OBJC },
603 { "end", RID_AT_END, D_OBJC },
604 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
605 { "interface", RID_AT_INTERFACE, D_OBJC },
606 { "protocol", RID_AT_PROTOCOL, D_OBJC },
607 { "selector", RID_AT_SELECTOR, D_OBJC },
608 { "finally", RID_AT_FINALLY, D_OBJC },
609 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
92902b1b
IS
610 { "optional", RID_AT_OPTIONAL, D_OBJC },
611 { "required", RID_AT_REQUIRED, D_OBJC },
668ea4b1 612 { "property", RID_AT_PROPERTY, D_OBJC },
c37d8c30 613 { "package", RID_AT_PACKAGE, D_OBJC },
da57d1b9
NP
614 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
615 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
eea1139b
ILT
616 /* These are recognized only in protocol-qualifier context
617 (see above) */
618 { "bycopy", RID_BYCOPY, D_OBJC },
619 { "byref", RID_BYREF, D_OBJC },
620 { "in", RID_IN, D_OBJC },
621 { "inout", RID_INOUT, D_OBJC },
622 { "oneway", RID_ONEWAY, D_OBJC },
623 { "out", RID_OUT, D_OBJC },
668ea4b1 624 /* These are recognized inside a property attribute list */
200290f2
NP
625 { "assign", RID_ASSIGN, D_OBJC },
626 { "copy", RID_COPY, D_OBJC },
668ea4b1 627 { "getter", RID_GETTER, D_OBJC },
200290f2
NP
628 { "nonatomic", RID_NONATOMIC, D_OBJC },
629 { "readonly", RID_READONLY, D_OBJC },
630 { "readwrite", RID_READWRITE, D_OBJC },
631 { "retain", RID_RETAIN, D_OBJC },
668ea4b1 632 { "setter", RID_SETTER, D_OBJC },
eea1139b
ILT
633};
634
635const unsigned int num_c_common_reswords =
636 sizeof c_common_reswords / sizeof (struct c_common_resword);
637
d5e254e1
IE
638/* Table of machine-independent attributes common to all C-like languages.
639
640 All attributes referencing arguments should be additionally processed
641 in chkp_copy_function_type_adding_bounds for correct instrumentation
642 by Pointer Bounds Checker.
643 Current list of processed common attributes: nonnull. */
349ae713
NB
644const struct attribute_spec c_common_attribute_table[] =
645{
62d784f7
KT
646 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
647 affects_type_identity } */
349ae713 648 { "packed", 0, 0, false, false, false,
62d784f7 649 handle_packed_attribute , false},
349ae713 650 { "nocommon", 0, 0, true, false, false,
62d784f7 651 handle_nocommon_attribute, false},
349ae713 652 { "common", 0, 0, true, false, false,
62d784f7 653 handle_common_attribute, false },
349ae713
NB
654 /* FIXME: logically, noreturn attributes should be listed as
655 "false, true, true" and apply to function types. But implementing this
656 would require all the places in the compiler that use TREE_THIS_VOLATILE
657 on a decl to identify non-returning functions to be located and fixed
658 to check the function type instead. */
659 { "noreturn", 0, 0, true, false, false,
62d784f7 660 handle_noreturn_attribute, false },
349ae713 661 { "volatile", 0, 0, true, false, false,
62d784f7 662 handle_noreturn_attribute, false },
5434dc07
MD
663 { "stack_protect", 0, 0, true, false, false,
664 handle_stack_protect_attribute, false },
349ae713 665 { "noinline", 0, 0, true, false, false,
62d784f7 666 handle_noinline_attribute, false },
86631ea3 667 { "noclone", 0, 0, true, false, false,
62d784f7 668 handle_noclone_attribute, false },
185c9e56
ML
669 { "no_icf", 0, 0, true, false, false,
670 handle_noicf_attribute, false },
46a4da10 671 { "leaf", 0, 0, true, false, false,
62d784f7 672 handle_leaf_attribute, false },
349ae713 673 { "always_inline", 0, 0, true, false, false,
62d784f7 674 handle_always_inline_attribute, false },
4eb7fd83 675 { "gnu_inline", 0, 0, true, false, false,
62d784f7 676 handle_gnu_inline_attribute, false },
d752cfdb 677 { "artificial", 0, 0, true, false, false,
62d784f7 678 handle_artificial_attribute, false },
0691d1d4 679 { "flatten", 0, 0, true, false, false,
62d784f7 680 handle_flatten_attribute, false },
349ae713 681 { "used", 0, 0, true, false, false,
62d784f7 682 handle_used_attribute, false },
349ae713 683 { "unused", 0, 0, false, false, false,
62d784f7 684 handle_unused_attribute, false },
ce91e74c 685 { "externally_visible", 0, 0, true, false, false,
62d784f7 686 handle_externally_visible_attribute, false },
7861b648
AK
687 { "no_reorder", 0, 0, true, false, false,
688 handle_no_reorder_attribute, false },
349ae713
NB
689 /* The same comments as for noreturn attributes apply to const ones. */
690 { "const", 0, 0, true, false, false,
62d784f7 691 handle_const_attribute, false },
349ae713 692 { "transparent_union", 0, 0, false, false, false,
62d784f7 693 handle_transparent_union_attribute, false },
fc8600f9 694 { "constructor", 0, 1, true, false, false,
62d784f7 695 handle_constructor_attribute, false },
fc8600f9 696 { "destructor", 0, 1, true, false, false,
62d784f7 697 handle_destructor_attribute, false },
349ae713 698 { "mode", 1, 1, false, true, false,
62d784f7 699 handle_mode_attribute, false },
349ae713 700 { "section", 1, 1, true, false, false,
62d784f7 701 handle_section_attribute, false },
349ae713 702 { "aligned", 0, 1, false, false, false,
62d784f7 703 handle_aligned_attribute, false },
349ae713 704 { "weak", 0, 0, true, false, false,
62d784f7 705 handle_weak_attribute, false },
4bb794e2
ST
706 { "noplt", 0, 0, true, false, false,
707 handle_noplt_attribute, false },
ba885ec5 708 { "ifunc", 1, 1, true, false, false,
62d784f7 709 handle_ifunc_attribute, false },
349ae713 710 { "alias", 1, 1, true, false, false,
62d784f7 711 handle_alias_attribute, false },
a0203ca7 712 { "weakref", 0, 1, true, false, false,
62d784f7 713 handle_weakref_attribute, false },
349ae713 714 { "no_instrument_function", 0, 0, true, false, false,
62d784f7
KT
715 handle_no_instrument_function_attribute,
716 false },
349ae713 717 { "malloc", 0, 0, true, false, false,
62d784f7 718 handle_malloc_attribute, false },
6e9a3221 719 { "returns_twice", 0, 0, true, false, false,
62d784f7 720 handle_returns_twice_attribute, false },
349ae713 721 { "no_stack_limit", 0, 0, true, false, false,
62d784f7 722 handle_no_limit_stack_attribute, false },
349ae713 723 { "pure", 0, 0, true, false, false,
62d784f7 724 handle_pure_attribute, false },
0a35513e
AH
725 { "transaction_callable", 0, 0, false, true, false,
726 handle_tm_attribute, false },
727 { "transaction_unsafe", 0, 0, false, true, false,
728 handle_tm_attribute, false },
729 { "transaction_safe", 0, 0, false, true, false,
730 handle_tm_attribute, false },
731 { "transaction_may_cancel_outer", 0, 0, false, true, false,
732 handle_tm_attribute, false },
733 /* ??? These two attributes didn't make the transition from the
734 Intel language document to the multi-vendor language document. */
735 { "transaction_pure", 0, 0, false, true, false,
736 handle_tm_attribute, false },
737 { "transaction_wrap", 1, 1, true, false, false,
738 handle_tm_wrap_attribute, false },
dcd6de6d
ZD
739 /* For internal use (marking of builtins) only. The name contains space
740 to prevent its usage in source code. */
741 { "no vops", 0, 0, true, false, false,
62d784f7 742 handle_novops_attribute, false },
9b86d6bb 743 { "deprecated", 0, 1, false, false, false,
62d784f7 744 handle_deprecated_attribute, false },
349ae713 745 { "vector_size", 1, 1, false, true, false,
62d784f7 746 handle_vector_size_attribute, false },
d7afec4b 747 { "visibility", 1, 1, false, false, false,
62d784f7 748 handle_visibility_attribute, false },
dce81a1a 749 { "tls_model", 1, 1, true, false, false,
62d784f7 750 handle_tls_model_attribute, false },
b34c7881 751 { "nonnull", 0, -1, false, true, true,
62d784f7 752 handle_nonnull_attribute, false },
39f2f3c8 753 { "nothrow", 0, 0, true, false, false,
62d784f7
KT
754 handle_nothrow_attribute, false },
755 { "may_alias", 0, 0, false, true, false, NULL, false },
0bfa5f65 756 { "cleanup", 1, 1, true, false, false,
62d784f7 757 handle_cleanup_attribute, false },
72954a4f 758 { "warn_unused_result", 0, 0, false, true, true,
62d784f7 759 handle_warn_unused_result_attribute, false },
254986c7 760 { "sentinel", 0, 1, false, true, true,
62d784f7 761 handle_sentinel_attribute, false },
b5d32c25
KG
762 /* For internal use (marking of builtins) only. The name contains space
763 to prevent its usage in source code. */
764 { "type generic", 0, 0, false, true, true,
62d784f7 765 handle_type_generic_attribute, false },
51bc54a6 766 { "alloc_size", 1, 2, false, true, true,
62d784f7 767 handle_alloc_size_attribute, false },
52bf96d2 768 { "cold", 0, 0, true, false, false,
62d784f7 769 handle_cold_attribute, false },
52bf96d2 770 { "hot", 0, 0, true, false, false,
62d784f7 771 handle_hot_attribute, false },
77bc5132
JJ
772 { "no_address_safety_analysis",
773 0, 0, true, false, false,
774 handle_no_address_safety_analysis_attribute,
775 false },
e664c61c
KS
776 { "no_sanitize_address", 0, 0, true, false, false,
777 handle_no_sanitize_address_attribute,
778 false },
de35aa66
MS
779 { "no_sanitize_thread", 0, 0, true, false, false,
780 handle_no_sanitize_address_attribute,
781 false },
ce6923c5
MP
782 { "no_sanitize_undefined", 0, 0, true, false, false,
783 handle_no_sanitize_undefined_attribute,
784 false },
d2af6a68 785 { "warning", 1, 1, true, false, false,
62d784f7 786 handle_error_attribute, false },
d2af6a68 787 { "error", 1, 1, true, false, false,
62d784f7 788 handle_error_attribute, false },
5779e713 789 { "target", 1, -1, true, false, false,
62d784f7 790 handle_target_attribute, false },
ab442df7 791 { "optimize", 1, -1, true, false, false,
62d784f7 792 handle_optimize_attribute, false },
0a35513e
AH
793 /* For internal use only. The leading '*' both prevents its usage in
794 source code and signals that it may be overridden by machine tables. */
795 { "*tm regparm", 0, 0, false, true, true,
796 ignore_attribute, false },
7458026b 797 { "no_split_stack", 0, 0, true, false, false,
62d784f7 798 handle_no_split_stack_attribute, false },
0b7b376d
RG
799 /* For internal use (marking of builtins and runtime functions) only.
800 The name contains space to prevent its usage in source code. */
801 { "fn spec", 1, 1, false, true, true,
62d784f7 802 handle_fnspec_attribute, false },
2a99e5e6
LL
803 { "warn_unused", 0, 0, false, false, false,
804 handle_warn_unused_attribute, false },
826cacfe
MG
805 { "returns_nonnull", 0, 0, false, true, true,
806 handle_returns_nonnull_attribute, false },
acf0174b
JJ
807 { "omp declare simd", 0, -1, true, false, false,
808 handle_omp_declare_simd_attribute, false },
41958c28
BI
809 { "cilk simd function", 0, -1, true, false, false,
810 handle_omp_declare_simd_attribute, false },
acf0174b
JJ
811 { "omp declare target", 0, 0, true, false, false,
812 handle_omp_declare_target_attribute, false },
8fcbce72
JJ
813 { "alloc_align", 1, 1, false, true, true,
814 handle_alloc_align_attribute, false },
815 { "assume_aligned", 1, 2, false, true, true,
816 handle_assume_aligned_attribute, false },
976d5a22
TT
817 { "designated_init", 0, 0, false, true, false,
818 handle_designated_init_attribute, false },
d5e254e1
IE
819 { "bnd_variable_size", 0, 0, true, false, false,
820 handle_bnd_variable_size_attribute, false },
821 { "bnd_legacy", 0, 0, true, false, false,
822 handle_bnd_legacy, false },
823 { "bnd_instrument", 0, 0, true, false, false,
824 handle_bnd_instrument, false },
62d784f7 825 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
826};
827
828/* Give the specifications for the format attributes, used by C and all
d5e254e1 829 descendants.
349ae713 830
d5e254e1
IE
831 All attributes referencing arguments should be additionally processed
832 in chkp_copy_function_type_adding_bounds for correct instrumentation
833 by Pointer Bounds Checker.
834 Current list of processed format attributes: format, format_arg. */
349ae713
NB
835const struct attribute_spec c_common_format_attribute_table[] =
836{
62d784f7
KT
837 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
838 affects_type_identity } */
349ae713 839 { "format", 3, 3, false, true, true,
62d784f7 840 handle_format_attribute, false },
349ae713 841 { "format_arg", 1, 1, false, true, true,
62d784f7
KT
842 handle_format_arg_attribute, false },
843 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
844};
845
36c5e70a 846/* Return identifier for address space AS. */
3ef0694c 847
36c5e70a
BE
848const char *
849c_addr_space_name (addr_space_t as)
850{
3ef0694c
UW
851 int rid = RID_FIRST_ADDR_SPACE + as;
852 gcc_assert (ridpointers [rid]);
853 return IDENTIFIER_POINTER (ridpointers [rid]);
36c5e70a
BE
854}
855
ec5c56db 856/* Push current bindings for the function name VAR_DECLS. */
7da551a2
RS
857
858void
35b1a6fa 859start_fname_decls (void)
7da551a2 860{
0ba8a114
NS
861 unsigned ix;
862 tree saved = NULL_TREE;
35b1a6fa 863
0ba8a114
NS
864 for (ix = 0; fname_vars[ix].decl; ix++)
865 {
866 tree decl = *fname_vars[ix].decl;
7da551a2 867
0ba8a114
NS
868 if (decl)
869 {
c62c040f
RG
870 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
871 saved);
0ba8a114
NS
872 *fname_vars[ix].decl = NULL_TREE;
873 }
874 }
875 if (saved || saved_function_name_decls)
876 /* Normally they'll have been NULL, so only push if we've got a
877 stack, or they are non-NULL. */
878 saved_function_name_decls = tree_cons (saved, NULL_TREE,
879 saved_function_name_decls);
880}
881
325c3691
RH
882/* Finish up the current bindings, adding them into the current function's
883 statement tree. This must be done _before_ finish_stmt_tree is called.
884 If there is no current function, we must be at file scope and no statements
885 are involved. Pop the previous bindings. */
0ba8a114
NS
886
887void
35b1a6fa 888finish_fname_decls (void)
0ba8a114
NS
889{
890 unsigned ix;
325c3691 891 tree stmts = NULL_TREE;
0ba8a114
NS
892 tree stack = saved_function_name_decls;
893
894 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
325c3691 895 append_to_statement_list (TREE_VALUE (stack), &stmts);
35b1a6fa 896
325c3691 897 if (stmts)
0ba8a114 898 {
325c3691 899 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
bfaba7a9 900
325c3691
RH
901 if (TREE_CODE (*bodyp) == BIND_EXPR)
902 bodyp = &BIND_EXPR_BODY (*bodyp);
6cce57b0 903
86ad3aa9 904 append_to_statement_list_force (*bodyp, &stmts);
325c3691 905 *bodyp = stmts;
0ba8a114 906 }
35b1a6fa 907
0ba8a114
NS
908 for (ix = 0; fname_vars[ix].decl; ix++)
909 *fname_vars[ix].decl = NULL_TREE;
35b1a6fa 910
0ba8a114 911 if (stack)
7da551a2 912 {
ec5c56db 913 /* We had saved values, restore them. */
0ba8a114
NS
914 tree saved;
915
916 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
917 {
918 tree decl = TREE_PURPOSE (saved);
919 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
35b1a6fa 920
0ba8a114
NS
921 *fname_vars[ix].decl = decl;
922 }
923 stack = TREE_CHAIN (stack);
7da551a2 924 }
0ba8a114
NS
925 saved_function_name_decls = stack;
926}
927
6cce57b0 928/* Return the text name of the current function, suitably prettified
0d0bc036 929 by PRETTY_P. Return string must be freed by caller. */
0ba8a114
NS
930
931const char *
35b1a6fa 932fname_as_string (int pretty_p)
0ba8a114 933{
47ab33b2 934 const char *name = "top level";
0d0bc036 935 char *namep;
46c2514e
TT
936 int vrb = 2, len;
937 cpp_string cstr = { 0, 0 }, strname;
47ab33b2 938
3f75a254 939 if (!pretty_p)
47ab33b2
MA
940 {
941 name = "";
942 vrb = 0;
943 }
944
945 if (current_function_decl)
ae2bcd98 946 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
47ab33b2 947
46c2514e 948 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
0d0bc036 949
46c2514e
TT
950 namep = XNEWVEC (char, len);
951 snprintf (namep, len, "\"%s\"", name);
952 strname.text = (unsigned char *) namep;
953 strname.len = len - 1;
0d0bc036 954
b6baa67d 955 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
46c2514e
TT
956 {
957 XDELETEVEC (namep);
958 return (const char *) cstr.text;
0d0bc036 959 }
0d0bc036
AH
960
961 return namep;
0ba8a114
NS
962}
963
0ba8a114
NS
964/* Return the VAR_DECL for a const char array naming the current
965 function. If the VAR_DECL has not yet been created, create it
966 now. RID indicates how it should be formatted and IDENTIFIER_NODE
967 ID is its name (unfortunately C and C++ hold the RID values of
968 keywords in different places, so we can't derive RID from ID in
3ba09659
AH
969 this language independent code. LOC is the location of the
970 function. */
0ba8a114
NS
971
972tree
3ba09659 973fname_decl (location_t loc, unsigned int rid, tree id)
0ba8a114
NS
974{
975 unsigned ix;
976 tree decl = NULL_TREE;
977
978 for (ix = 0; fname_vars[ix].decl; ix++)
979 if (fname_vars[ix].rid == rid)
980 break;
981
982 decl = *fname_vars[ix].decl;
983 if (!decl)
7da551a2 984 {
8d3e27d1
DJ
985 /* If a tree is built here, it would normally have the lineno of
986 the current statement. Later this tree will be moved to the
987 beginning of the function and this line number will be wrong.
988 To avoid this problem set the lineno to 0 here; that prevents
4b7e68e7 989 it from appearing in the RTL. */
325c3691 990 tree stmts;
3c20847b 991 location_t saved_location = input_location;
3c20847b 992 input_location = UNKNOWN_LOCATION;
35b1a6fa 993
325c3691 994 stmts = push_stmt_list ();
c2255bc4 995 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
325c3691
RH
996 stmts = pop_stmt_list (stmts);
997 if (!IS_EMPTY_STMT (stmts))
998 saved_function_name_decls
999 = tree_cons (decl, stmts, saved_function_name_decls);
0ba8a114 1000 *fname_vars[ix].decl = decl;
3c20847b 1001 input_location = saved_location;
7da551a2 1002 }
0ba8a114 1003 if (!ix && !current_function_decl)
3ba09659 1004 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
6cce57b0 1005
0ba8a114 1006 return decl;
7da551a2
RS
1007}
1008
b84a3874 1009/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b30f223b
RS
1010
1011tree
35b1a6fa 1012fix_string_type (tree value)
b30f223b 1013{
b84a3874
RH
1014 int length = TREE_STRING_LENGTH (value);
1015 int nchars;
c162c75e
MA
1016 tree e_type, i_type, a_type;
1017
b57062ca 1018 /* Compute the number of elements, for the array type. */
b6baa67d
KVH
1019 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1020 {
1021 nchars = length;
1022 e_type = char_type_node;
1023 }
1024 else if (TREE_TYPE (value) == char16_array_type_node)
1025 {
1026 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1027 e_type = char16_type_node;
1028 }
1029 else if (TREE_TYPE (value) == char32_array_type_node)
1030 {
1031 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1032 e_type = char32_type_node;
1033 }
1034 else
1035 {
1036 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1037 e_type = wchar_type_node;
1038 }
b30f223b 1039
89a42ac8
ZW
1040 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1041 limit in C++98 Annex B is very large (65536) and is not normative,
1042 so we do not diagnose it (warn_overlength_strings is forced off
1043 in c_common_post_options). */
1044 if (warn_overlength_strings)
1045 {
1046 const int nchars_max = flag_isoc99 ? 4095 : 509;
1047 const int relevant_std = flag_isoc99 ? 99 : 90;
1048 if (nchars - 1 > nchars_max)
1049 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1050 separate the %d from the 'C'. 'ISO' should not be
1051 translated, but it may be moved after 'C%d' in languages
1052 where modifiers follow nouns. */
509c9d60 1053 pedwarn (input_location, OPT_Woverlength_strings,
fcf73884 1054 "string length %qd is greater than the length %qd "
89a42ac8
ZW
1055 "ISO C%d compilers are required to support",
1056 nchars - 1, nchars_max, relevant_std);
1057 }
1326a48b 1058
cfb10bd3
GDR
1059 /* Create the array type for the string constant. The ISO C++
1060 standard says that a string literal has type `const char[N]' or
1061 `const wchar_t[N]'. We use the same logic when invoked as a C
1062 front-end with -Wwrite-strings.
1063 ??? We should change the type of an expression depending on the
1064 state of a warning flag. We should just be warning -- see how
1065 this is handled in the C++ front-end for the deprecated implicit
1066 conversion from string literals to `char*' or `wchar_t*'.
c162c75e
MA
1067
1068 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1069 array type being the unqualified version of that type.
1070 Therefore, if we are constructing an array of const char, we must
1071 construct the matching unqualified array type first. The C front
1072 end does not require this, but it does no harm, so we do it
1073 unconditionally. */
c62c040f 1074 i_type = build_index_type (size_int (nchars - 1));
c162c75e 1075 a_type = build_array_type (e_type, i_type);
cfb10bd3 1076 if (c_dialect_cxx() || warn_write_strings)
46df2823 1077 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
d9cf7c82 1078
c162c75e 1079 TREE_TYPE (value) = a_type;
ccd4c832 1080 TREE_CONSTANT (value) = 1;
3521b33c 1081 TREE_READONLY (value) = 1;
b30f223b
RS
1082 TREE_STATIC (value) = 1;
1083 return value;
1084}
1085\f
02614448
ILT
1086/* If DISABLE is true, stop issuing warnings. This is used when
1087 parsing code that we know will not be executed. This function may
1088 be called multiple times, and works as a stack. */
1089
1090static void
1091c_disable_warnings (bool disable)
1092{
1093 if (disable)
1094 {
1095 ++c_inhibit_evaluation_warnings;
1096 fold_defer_overflow_warnings ();
1097 }
1098}
1099
1100/* If ENABLE is true, reenable issuing warnings. */
1101
1102static void
1103c_enable_warnings (bool enable)
1104{
1105 if (enable)
1106 {
1107 --c_inhibit_evaluation_warnings;
1108 fold_undefer_and_ignore_overflow_warnings ();
1109 }
1110}
1111
928c19bb
JM
1112/* Fully fold EXPR, an expression that was not folded (beyond integer
1113 constant expressions and null pointer constants) when being built
1114 up. If IN_INIT, this is in a static initializer and certain
1115 changes are made to the folding done. Clear *MAYBE_CONST if
1116 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1117 expression because it contains an evaluated operator (in C99) or an
1118 operator outside of sizeof returning an integer constant (in C90)
1119 not permitted in constant expressions, or because it contains an
1120 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1121 set to true by callers before calling this function.) Return the
1122 folded expression. Function arguments have already been folded
1123 before calling this function, as have the contents of SAVE_EXPR,
1124 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1125 C_MAYBE_CONST_EXPR. */
1126
1127tree
1128c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1129{
1130 tree ret;
8ce94e44 1131 tree eptype = NULL_TREE;
928c19bb
JM
1132 bool dummy = true;
1133 bool maybe_const_itself = true;
db3927fb 1134 location_t loc = EXPR_LOCATION (expr);
928c19bb
JM
1135
1136 /* This function is not relevant to C++ because C++ folds while
1137 parsing, and may need changes to be correct for C++ when C++
1138 stops folding while parsing. */
1139 if (c_dialect_cxx ())
1140 gcc_unreachable ();
1141
1142 if (!maybe_const)
1143 maybe_const = &dummy;
8ce94e44
JM
1144 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1145 {
1146 eptype = TREE_TYPE (expr);
1147 expr = TREE_OPERAND (expr, 0);
1148 }
928c19bb 1149 ret = c_fully_fold_internal (expr, in_init, maybe_const,
3aa3c9fc 1150 &maybe_const_itself, false);
8ce94e44 1151 if (eptype)
db3927fb 1152 ret = fold_convert_loc (loc, eptype, ret);
928c19bb
JM
1153 *maybe_const &= maybe_const_itself;
1154 return ret;
1155}
1156
1157/* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1158 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1159 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1160 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1161 both evaluated and unevaluated subexpressions while
1162 *MAYBE_CONST_ITSELF is carried from only evaluated
3aa3c9fc
MP
1163 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1164 with integer constant operands, and if any of the operands doesn't
1165 get folded to an integer constant, don't fold the expression itself. */
928c19bb
JM
1166
1167static tree
1168c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
3aa3c9fc 1169 bool *maybe_const_itself, bool for_int_const)
928c19bb
JM
1170{
1171 tree ret = expr;
1172 enum tree_code code = TREE_CODE (expr);
1173 enum tree_code_class kind = TREE_CODE_CLASS (code);
1174 location_t loc = EXPR_LOCATION (expr);
1175 tree op0, op1, op2, op3;
1176 tree orig_op0, orig_op1, orig_op2;
1177 bool op0_const = true, op1_const = true, op2_const = true;
1178 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1179 bool nowarning = TREE_NO_WARNING (expr);
02614448 1180 bool unused_p;
928c19bb
JM
1181
1182 /* This function is not relevant to C++ because C++ folds while
1183 parsing, and may need changes to be correct for C++ when C++
1184 stops folding while parsing. */
1185 if (c_dialect_cxx ())
1186 gcc_unreachable ();
1187
1188 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1189 anything else not counted as an expression cannot usefully be
1190 folded further at this point. */
1191 if (!IS_EXPR_CODE_CLASS (kind)
1192 || kind == tcc_statement
1193 || code == SAVE_EXPR)
1194 return expr;
1195
1196 /* Operands of variable-length expressions (function calls) have
1197 already been folded, as have __builtin_* function calls, and such
1198 expressions cannot occur in constant expressions. */
1199 if (kind == tcc_vl_exp)
1200 {
1201 *maybe_const_operands = false;
1202 ret = fold (expr);
1203 goto out;
1204 }
1205
1206 if (code == C_MAYBE_CONST_EXPR)
1207 {
1208 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1209 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1210 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1211 *maybe_const_operands = false;
1212 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
3aa3c9fc
MP
1213 {
1214 *maybe_const_itself = false;
1215 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1216 maybe_const_itself, true);
1217 }
928c19bb
JM
1218 if (pre && !in_init)
1219 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1220 else
1221 ret = inner;
1222 goto out;
1223 }
1224
1225 /* Assignment, increment, decrement, function call and comma
1226 operators, and statement expressions, cannot occur in constant
1227 expressions if evaluated / outside of sizeof. (Function calls
1228 were handled above, though VA_ARG_EXPR is treated like a function
1229 call here, and statement expressions are handled through
1230 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1231 switch (code)
1232 {
1233 case MODIFY_EXPR:
1234 case PREDECREMENT_EXPR:
1235 case PREINCREMENT_EXPR:
1236 case POSTDECREMENT_EXPR:
1237 case POSTINCREMENT_EXPR:
1238 case COMPOUND_EXPR:
1239 *maybe_const_operands = false;
1240 break;
1241
1242 case VA_ARG_EXPR:
1243 case TARGET_EXPR:
1244 case BIND_EXPR:
1245 case OBJ_TYPE_REF:
1246 *maybe_const_operands = false;
1247 ret = fold (expr);
1248 goto out;
1249
1250 default:
1251 break;
1252 }
1253
1254 /* Fold individual tree codes as appropriate. */
1255 switch (code)
1256 {
1257 case COMPOUND_LITERAL_EXPR:
1258 /* Any non-constancy will have been marked in a containing
1259 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1260 goto out;
1261
1262 case COMPONENT_REF:
1263 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1264 op1 = TREE_OPERAND (expr, 1);
1265 op2 = TREE_OPERAND (expr, 2);
1266 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
3aa3c9fc 1267 maybe_const_itself, for_int_const);
e5a94231 1268 STRIP_TYPE_NOPS (op0);
928c19bb
JM
1269 if (op0 != orig_op0)
1270 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1271 if (ret != expr)
1272 {
1273 TREE_READONLY (ret) = TREE_READONLY (expr);
1274 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1275 }
1276 goto out;
1277
1278 case ARRAY_REF:
1279 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1280 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1281 op2 = TREE_OPERAND (expr, 2);
1282 op3 = TREE_OPERAND (expr, 3);
1283 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
3aa3c9fc 1284 maybe_const_itself, for_int_const);
e5a94231 1285 STRIP_TYPE_NOPS (op0);
928c19bb 1286 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
3aa3c9fc 1287 maybe_const_itself, for_int_const);
e5a94231 1288 STRIP_TYPE_NOPS (op1);
928c19bb
JM
1289 op1 = decl_constant_value_for_optimization (op1);
1290 if (op0 != orig_op0 || op1 != orig_op1)
1291 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1292 if (ret != expr)
1293 {
1294 TREE_READONLY (ret) = TREE_READONLY (expr);
1295 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1296 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1297 }
1298 ret = fold (ret);
1299 goto out;
1300
1301 case COMPOUND_EXPR:
1302 case MODIFY_EXPR:
1303 case PREDECREMENT_EXPR:
1304 case PREINCREMENT_EXPR:
1305 case POSTDECREMENT_EXPR:
1306 case POSTINCREMENT_EXPR:
1307 case PLUS_EXPR:
1308 case MINUS_EXPR:
1309 case MULT_EXPR:
1310 case POINTER_PLUS_EXPR:
1311 case TRUNC_DIV_EXPR:
1312 case CEIL_DIV_EXPR:
1313 case FLOOR_DIV_EXPR:
1314 case TRUNC_MOD_EXPR:
1315 case RDIV_EXPR:
1316 case EXACT_DIV_EXPR:
1317 case LSHIFT_EXPR:
1318 case RSHIFT_EXPR:
1319 case BIT_IOR_EXPR:
1320 case BIT_XOR_EXPR:
1321 case BIT_AND_EXPR:
1322 case LT_EXPR:
1323 case LE_EXPR:
1324 case GT_EXPR:
1325 case GE_EXPR:
1326 case EQ_EXPR:
1327 case NE_EXPR:
1328 case COMPLEX_EXPR:
1329 case TRUTH_AND_EXPR:
1330 case TRUTH_OR_EXPR:
1331 case TRUTH_XOR_EXPR:
1332 case UNORDERED_EXPR:
1333 case ORDERED_EXPR:
1334 case UNLT_EXPR:
1335 case UNLE_EXPR:
1336 case UNGT_EXPR:
1337 case UNGE_EXPR:
1338 case UNEQ_EXPR:
1339 /* Binary operations evaluating both arguments (increment and
1340 decrement are binary internally in GCC). */
1341 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1342 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1343 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
3aa3c9fc 1344 maybe_const_itself, for_int_const);
e5a94231 1345 STRIP_TYPE_NOPS (op0);
928c19bb
JM
1346 if (code != MODIFY_EXPR
1347 && code != PREDECREMENT_EXPR
1348 && code != PREINCREMENT_EXPR
1349 && code != POSTDECREMENT_EXPR
1350 && code != POSTINCREMENT_EXPR)
1351 op0 = decl_constant_value_for_optimization (op0);
1352 /* The RHS of a MODIFY_EXPR was fully folded when building that
1353 expression for the sake of conversion warnings. */
1354 if (code != MODIFY_EXPR)
1355 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
3aa3c9fc 1356 maybe_const_itself, for_int_const);
e5a94231 1357 STRIP_TYPE_NOPS (op1);
928c19bb 1358 op1 = decl_constant_value_for_optimization (op1);
3aa3c9fc
MP
1359
1360 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1361 || TREE_CODE (op1) != INTEGER_CST))
1362 goto out;
1363
928c19bb
JM
1364 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1365 ret = in_init
db3927fb
AH
1366 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1367 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
928c19bb
JM
1368 else
1369 ret = fold (expr);
f5178456
RS
1370 if (TREE_OVERFLOW_P (ret)
1371 && !TREE_OVERFLOW_P (op0)
1372 && !TREE_OVERFLOW_P (op1))
451b5e48 1373 overflow_warning (EXPR_LOC_OR_LOC (expr, input_location), ret);
0173bd2a
MP
1374 if (code == LSHIFT_EXPR
1375 && TREE_CODE (orig_op0) != INTEGER_CST
1376 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1377 && TREE_CODE (op0) == INTEGER_CST
1378 && c_inhibit_evaluation_warnings == 0
1379 && tree_int_cst_sgn (op0) < 0)
1380 warning_at (loc, OPT_Wshift_negative_value,
1381 "left shift of negative value");
cc83c823
JJ
1382 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1383 && TREE_CODE (orig_op1) != INTEGER_CST
1384 && TREE_CODE (op1) == INTEGER_CST
1385 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1386 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1387 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1388 && c_inhibit_evaluation_warnings == 0)
1389 {
1390 if (tree_int_cst_sgn (op1) < 0)
4853031e
MP
1391 warning_at (loc, OPT_Wshift_count_negative,
1392 (code == LSHIFT_EXPR
1393 ? G_("left shift count is negative")
1394 : G_("right shift count is negative")));
cc83c823
JJ
1395 else if (compare_tree_int (op1,
1396 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1397 >= 0)
4853031e
MP
1398 warning_at (loc, OPT_Wshift_count_overflow,
1399 (code == LSHIFT_EXPR
1400 ? G_("left shift count >= width of type")
1401 : G_("right shift count >= width of type")));
cc83c823 1402 }
451b5e48
MP
1403 if (code == LSHIFT_EXPR
1404 /* If either OP0 has been folded to INTEGER_CST... */
1405 && ((TREE_CODE (orig_op0) != INTEGER_CST
1406 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1407 && TREE_CODE (op0) == INTEGER_CST)
1408 /* ...or if OP1 has been folded to INTEGER_CST... */
1409 || (TREE_CODE (orig_op1) != INTEGER_CST
1410 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1411 && TREE_CODE (op1) == INTEGER_CST))
1412 && c_inhibit_evaluation_warnings == 0)
1413 /* ...then maybe we can detect an overflow. */
1414 maybe_warn_shift_overflow (loc, op0, op1);
4a9a42ab
MP
1415 if ((code == TRUNC_DIV_EXPR
1416 || code == CEIL_DIV_EXPR
1417 || code == FLOOR_DIV_EXPR
1418 || code == EXACT_DIV_EXPR
1419 || code == TRUNC_MOD_EXPR)
1420 && TREE_CODE (orig_op1) != INTEGER_CST
1421 && TREE_CODE (op1) == INTEGER_CST
1422 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1423 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1424 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1425 warn_for_div_by_zero (loc, op1);
928c19bb
JM
1426 goto out;
1427
1428 case INDIRECT_REF:
1429 case FIX_TRUNC_EXPR:
1430 case FLOAT_EXPR:
1431 CASE_CONVERT:
2ce064c3 1432 case ADDR_SPACE_CONVERT_EXPR:
982d62f6 1433 case VIEW_CONVERT_EXPR:
928c19bb
JM
1434 case NON_LVALUE_EXPR:
1435 case NEGATE_EXPR:
1436 case BIT_NOT_EXPR:
1437 case TRUTH_NOT_EXPR:
1438 case ADDR_EXPR:
1439 case CONJ_EXPR:
1440 case REALPART_EXPR:
1441 case IMAGPART_EXPR:
1442 /* Unary operations. */
1443 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1444 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
3aa3c9fc 1445 maybe_const_itself, for_int_const);
e5a94231 1446 STRIP_TYPE_NOPS (op0);
928c19bb
JM
1447 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1448 op0 = decl_constant_value_for_optimization (op0);
3aa3c9fc
MP
1449
1450 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1451 goto out;
1452
693ddb1b
EB
1453 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1454 not prepared to deal with them if they occur in initializers. */
1455 if (op0 != orig_op0
1456 && code == ADDR_EXPR
1457 && (op1 = get_base_address (op0)) != NULL_TREE
22d03525 1458 && INDIRECT_REF_P (op1)
693ddb1b 1459 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
cf9e9959 1460 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
693ddb1b 1461 else if (op0 != orig_op0 || in_init)
928c19bb 1462 ret = in_init
db3927fb
AH
1463 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1464 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
928c19bb
JM
1465 else
1466 ret = fold (expr);
1467 if (code == INDIRECT_REF
1468 && ret != expr
22d03525 1469 && INDIRECT_REF_P (ret))
928c19bb
JM
1470 {
1471 TREE_READONLY (ret) = TREE_READONLY (expr);
1472 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1473 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1474 }
f5178456
RS
1475 switch (code)
1476 {
1477 case FIX_TRUNC_EXPR:
1478 case FLOAT_EXPR:
1479 CASE_CONVERT:
1480 /* Don't warn about explicit conversions. We will already
1481 have warned about suspect implicit conversions. */
1482 break;
1483
1484 default:
1485 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1486 overflow_warning (EXPR_LOCATION (expr), ret);
1487 break;
1488 }
928c19bb
JM
1489 goto out;
1490
1491 case TRUTH_ANDIF_EXPR:
1492 case TRUTH_ORIF_EXPR:
1493 /* Binary operations not necessarily evaluating both
1494 arguments. */
1495 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1496 orig_op1 = op1 = TREE_OPERAND (expr, 1);
3aa3c9fc
MP
1497 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1498 for_int_const);
e5a94231 1499 STRIP_TYPE_NOPS (op0);
f5178456
RS
1500
1501 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1502 ? truthvalue_false_node
1503 : truthvalue_true_node));
02614448 1504 c_disable_warnings (unused_p);
3aa3c9fc
MP
1505 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1506 for_int_const);
e5a94231 1507 STRIP_TYPE_NOPS (op1);
02614448 1508 c_enable_warnings (unused_p);
f5178456 1509
3aa3c9fc
MP
1510 if (for_int_const
1511 && (TREE_CODE (op0) != INTEGER_CST
1512 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1513 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1514 goto out;
1515
928c19bb
JM
1516 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1517 ret = in_init
db3927fb
AH
1518 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1519 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
928c19bb
JM
1520 else
1521 ret = fold (expr);
1522 *maybe_const_operands &= op0_const;
1523 *maybe_const_itself &= op0_const_self;
1524 if (!(flag_isoc99
1525 && op0_const
1526 && op0_const_self
1527 && (code == TRUTH_ANDIF_EXPR
1528 ? op0 == truthvalue_false_node
1529 : op0 == truthvalue_true_node)))
1530 *maybe_const_operands &= op1_const;
1531 if (!(op0_const
1532 && op0_const_self
1533 && (code == TRUTH_ANDIF_EXPR
1534 ? op0 == truthvalue_false_node
1535 : op0 == truthvalue_true_node)))
1536 *maybe_const_itself &= op1_const_self;
1537 goto out;
1538
1539 case COND_EXPR:
1540 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1541 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1542 orig_op2 = op2 = TREE_OPERAND (expr, 2);
3aa3c9fc
MP
1543 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1544 for_int_const);
f5178456 1545
e5a94231 1546 STRIP_TYPE_NOPS (op0);
02614448 1547 c_disable_warnings (op0 == truthvalue_false_node);
3aa3c9fc
MP
1548 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1549 for_int_const);
e5a94231 1550 STRIP_TYPE_NOPS (op1);
02614448 1551 c_enable_warnings (op0 == truthvalue_false_node);
f5178456 1552
02614448 1553 c_disable_warnings (op0 == truthvalue_true_node);
3aa3c9fc
MP
1554 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1555 for_int_const);
e5a94231 1556 STRIP_TYPE_NOPS (op2);
02614448 1557 c_enable_warnings (op0 == truthvalue_true_node);
f5178456 1558
3aa3c9fc
MP
1559 if (for_int_const
1560 && (TREE_CODE (op0) != INTEGER_CST
1561 /* Only the evaluated operand must be an INTEGER_CST. */
1562 || (op0 == truthvalue_true_node
1563 ? TREE_CODE (op1) != INTEGER_CST
1564 : TREE_CODE (op2) != INTEGER_CST)))
1565 goto out;
1566
928c19bb 1567 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
db3927fb 1568 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
928c19bb
JM
1569 else
1570 ret = fold (expr);
1571 *maybe_const_operands &= op0_const;
1572 *maybe_const_itself &= op0_const_self;
1573 if (!(flag_isoc99
1574 && op0_const
1575 && op0_const_self
1576 && op0 == truthvalue_false_node))
1577 *maybe_const_operands &= op1_const;
1578 if (!(op0_const
1579 && op0_const_self
1580 && op0 == truthvalue_false_node))
1581 *maybe_const_itself &= op1_const_self;
1582 if (!(flag_isoc99
1583 && op0_const
1584 && op0_const_self
1585 && op0 == truthvalue_true_node))
1586 *maybe_const_operands &= op2_const;
1587 if (!(op0_const
1588 && op0_const_self
1589 && op0 == truthvalue_true_node))
1590 *maybe_const_itself &= op2_const_self;
1591 goto out;
1592
8ce94e44
JM
1593 case EXCESS_PRECISION_EXPR:
1594 /* Each case where an operand with excess precision may be
1595 encountered must remove the EXCESS_PRECISION_EXPR around
1596 inner operands and possibly put one around the whole
1597 expression or possibly convert to the semantic type (which
1598 c_fully_fold does); we cannot tell at this stage which is
1599 appropriate in any particular case. */
1600 gcc_unreachable ();
1601
928c19bb
JM
1602 default:
1603 /* Various codes may appear through folding built-in functions
1604 and their arguments. */
1605 goto out;
1606 }
1607
1608 out:
1609 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1610 have been done by this point, so remove them again. */
1611 nowarning |= TREE_NO_WARNING (ret);
1612 STRIP_TYPE_NOPS (ret);
1613 if (nowarning && !TREE_NO_WARNING (ret))
1614 {
1615 if (!CAN_HAVE_LOCATION_P (ret))
1616 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1617 TREE_NO_WARNING (ret) = 1;
1618 }
1619 if (ret != expr)
1620 protected_set_expr_location (ret, loc);
1621 return ret;
1622}
1623
1624/* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1625 return EXP. Otherwise, return either EXP or its known constant
1626 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1627 Is the BLKmode test appropriate? */
1628
1629tree
1630decl_constant_value_for_optimization (tree exp)
1631{
1632 tree ret;
1633
1634 /* This function is only used by C, for c_fully_fold and other
1635 optimization, and may not be correct for C++. */
1636 if (c_dialect_cxx ())
1637 gcc_unreachable ();
1638
1639 if (!optimize
0ae9bd27 1640 || !VAR_P (exp)
928c19bb
JM
1641 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1642 || DECL_MODE (exp) == BLKmode)
1643 return exp;
1644
1645 ret = decl_constant_value (exp);
1646 /* Avoid unwanted tree sharing between the initializer and current
1647 function's body where the tree can be modified e.g. by the
1648 gimplifier. */
1649 if (ret != exp && TREE_STATIC (exp))
1650 ret = unshare_expr (ret);
1651 return ret;
1652}
1653
d74154d5
RS
1654/* Print a warning if a constant expression had overflow in folding.
1655 Invoke this function on every expression that the language
1656 requires to be a constant expression.
1657 Note the ANSI C standard says it is erroneous for a
1658 constant expression to overflow. */
96571883
BK
1659
1660void
35b1a6fa 1661constant_expression_warning (tree value)
393eda6a 1662{
b8698a0f 1663 if (warn_overflow && pedantic
393eda6a
MLI
1664 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1665 || TREE_CODE (value) == FIXED_CST
1666 || TREE_CODE (value) == VECTOR_CST
1667 || TREE_CODE (value) == COMPLEX_CST)
1668 && TREE_OVERFLOW (value))
509c9d60 1669 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
393eda6a
MLI
1670}
1671
1672/* The same as above but print an unconditional error. */
1673void
1674constant_expression_error (tree value)
96571883 1675{
c05f751c 1676 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
ab22c1fa 1677 || TREE_CODE (value) == FIXED_CST
69ef87e2 1678 || TREE_CODE (value) == VECTOR_CST
c05f751c 1679 || TREE_CODE (value) == COMPLEX_CST)
393eda6a
MLI
1680 && TREE_OVERFLOW (value))
1681 error ("overflow in constant expression");
d74154d5
RS
1682}
1683
59c0753d
MLI
1684/* Print a warning if an expression had overflow in folding and its
1685 operands hadn't.
1686
d74154d5
RS
1687 Invoke this function on every expression that
1688 (1) appears in the source code, and
59c0753d 1689 (2) is a constant expression that overflowed, and
d74154d5 1690 (3) is not already checked by convert_and_check;
59c0753d
MLI
1691 however, do not invoke this function on operands of explicit casts
1692 or when the expression is the result of an operator and any operand
1693 already overflowed. */
d74154d5
RS
1694
1695void
c2255bc4 1696overflow_warning (location_t loc, tree value)
d74154d5 1697{
7d882b83
ILT
1698 if (c_inhibit_evaluation_warnings != 0)
1699 return;
59c0753d
MLI
1700
1701 switch (TREE_CODE (value))
69ef87e2 1702 {
59c0753d 1703 case INTEGER_CST:
c2255bc4 1704 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
59c0753d 1705 break;
b8698a0f 1706
59c0753d 1707 case REAL_CST:
c2255bc4
AH
1708 warning_at (loc, OPT_Woverflow,
1709 "floating point overflow in expression");
59c0753d 1710 break;
b8698a0f 1711
ab22c1fa 1712 case FIXED_CST:
c2255bc4 1713 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
ab22c1fa
CF
1714 break;
1715
59c0753d 1716 case VECTOR_CST:
c2255bc4 1717 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
59c0753d 1718 break;
b8698a0f 1719
59c0753d
MLI
1720 case COMPLEX_CST:
1721 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
c2255bc4
AH
1722 warning_at (loc, OPT_Woverflow,
1723 "complex integer overflow in expression");
59c0753d 1724 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
c2255bc4
AH
1725 warning_at (loc, OPT_Woverflow,
1726 "complex floating point overflow in expression");
59c0753d
MLI
1727 break;
1728
1729 default:
1730 break;
69ef87e2 1731 }
d74154d5
RS
1732}
1733
ca409efd
MLI
1734/* Warn about uses of logical || / && operator in a context where it
1735 is likely that the bitwise equivalent was intended by the
1736 programmer. We have seen an expression in which CODE is a binary
a243fb4a
MLI
1737 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1738 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
63a08740 1739void
a243fb4a 1740warn_logical_operator (location_t location, enum tree_code code, tree type,
b8698a0f 1741 enum tree_code code_left, tree op_left,
ca409efd 1742 enum tree_code ARG_UNUSED (code_right), tree op_right)
63a08740 1743{
a243fb4a
MLI
1744 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1745 int in0_p, in1_p, in_p;
1746 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1747 bool strict_overflow_p = false;
1748
ca409efd
MLI
1749 if (code != TRUTH_ANDIF_EXPR
1750 && code != TRUTH_AND_EXPR
1751 && code != TRUTH_ORIF_EXPR
1752 && code != TRUTH_OR_EXPR)
1753 return;
1754
b8787813
MP
1755 /* We don't want to warn if either operand comes from a macro
1756 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1757 see PR61534. */
1758 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1759 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1760 return;
1761
ca409efd
MLI
1762 /* Warn if &&/|| are being used in a context where it is
1763 likely that the bitwise equivalent was intended by the
1764 programmer. That is, an expression such as op && MASK
1765 where op should not be any boolean expression, nor a
1766 constant, and mask seems to be a non-boolean integer constant. */
1767 if (!truth_value_p (code_left)
1768 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1769 && !CONSTANT_CLASS_P (op_left)
1770 && !TREE_NO_WARNING (op_left)
1771 && TREE_CODE (op_right) == INTEGER_CST
1772 && !integer_zerop (op_right)
1773 && !integer_onep (op_right))
63a08740 1774 {
a243fb4a 1775 if (or_op)
ca409efd
MLI
1776 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1777 " applied to non-boolean constant");
1778 else
1779 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1780 " applied to non-boolean constant");
1781 TREE_NO_WARNING (op_left) = true;
a243fb4a
MLI
1782 return;
1783 }
1784
1785 /* We do not warn for constants because they are typical of macro
1786 expansions that test for features. */
1787 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1788 return;
1789
1790 /* This warning only makes sense with logical operands. */
1791 if (!(truth_value_p (TREE_CODE (op_left))
1792 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1793 || !(truth_value_p (TREE_CODE (op_right))
1794 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1795 return;
1796
3c9aabbd
MG
1797 /* The range computations only work with scalars. */
1798 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1799 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1800 return;
a243fb4a 1801
f2c4a785
MLI
1802 /* We first test whether either side separately is trivially true
1803 (with OR) or trivially false (with AND). If so, do not warn.
1804 This is a common idiom for testing ranges of data types in
1805 portable code. */
1806 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1807 if (!lhs)
1808 return;
1809 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
a243fb4a
MLI
1810 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1811
f2c4a785
MLI
1812 /* If this is an OR operation, invert both sides; now, the result
1813 should be always false to get a warning. */
1814 if (or_op)
1815 in0_p = !in0_p;
1816
1817 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1e537948 1818 if (tem && integer_zerop (tem))
f2c4a785
MLI
1819 return;
1820
1821 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1822 if (!rhs)
1823 return;
1824 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
a243fb4a 1825 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
b8698a0f 1826
f2c4a785
MLI
1827 /* If this is an OR operation, invert both sides; now, the result
1828 should be always false to get a warning. */
a243fb4a 1829 if (or_op)
f2c4a785 1830 in1_p = !in1_p;
b8698a0f 1831
f2c4a785 1832 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1e537948 1833 if (tem && integer_zerop (tem))
f2c4a785
MLI
1834 return;
1835
1836 /* If both expressions have the same operand, if we can merge the
8c2b7f79 1837 ranges, ... */
f2c4a785 1838 if (operand_equal_p (lhs, rhs, 0)
a243fb4a 1839 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
8c2b7f79 1840 in1_p, low1, high1))
a243fb4a 1841 {
8c2b7f79
MP
1842 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1843 /* ... and if the range test is always false, then warn. */
1844 if (tem && integer_zerop (tem))
1845 {
1846 if (or_op)
1847 warning_at (location, OPT_Wlogical_op,
1848 "logical %<or%> of collectively exhaustive tests is "
1849 "always true");
1850 else
1851 warning_at (location, OPT_Wlogical_op,
1852 "logical %<and%> of mutually exclusive tests is "
1853 "always false");
1854 }
1855 /* Or warn if the operands have exactly the same range, e.g.
1856 A > 0 && A > 0. */
a68ae2e1
MP
1857 else if (tree_int_cst_equal (low0, low1)
1858 && tree_int_cst_equal (high0, high1))
8c2b7f79
MP
1859 {
1860 if (or_op)
1861 warning_at (location, OPT_Wlogical_op,
1862 "logical %<or%> of equal expressions");
1863 else
1864 warning_at (location, OPT_Wlogical_op,
1865 "logical %<and%> of equal expressions");
1866 }
63a08740
DM
1867 }
1868}
1869
05b28fd6
MP
1870/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1871 with constant indices. */
1872
1873static tree
1874find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1875{
1876 tree expr = *expr_p;
1877
1878 if ((TREE_CODE (expr) == ARRAY_REF
1879 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1880 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1881 {
1882 *(bool *) data = true;
1883 *walk_subtrees = 0;
1884 }
1885
1886 return NULL_TREE;
1887}
1888
1889/* Warn if a self-comparison always evaluates to true or false. LOC
1890 is the location of the comparison with code CODE, LHS and RHS are
1891 operands of the comparison. */
1892
1893void
1894warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1895{
1896 if (TREE_CODE_CLASS (code) != tcc_comparison)
1897 return;
1898
f2afe6dd
MP
1899 /* Don't warn for various macro expansions. */
1900 if (from_macro_expansion_at (loc)
1901 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1902 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1903 return;
1904
05b28fd6
MP
1905 /* We do not warn for constants because they are typical of macro
1906 expansions that test for features, sizeof, and similar. */
1907 if (CONSTANT_CLASS_P (lhs) || CONSTANT_CLASS_P (rhs))
1908 return;
1909
1910 /* Don't warn for e.g.
1911 HOST_WIDE_INT n;
1912 ...
1913 if (n == (long) n) ...
1914 */
1915 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1916 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1917 return;
1918
173864e8
MP
1919 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1920 It could be a NaN, and NaN never compares equal to anything, even
1921 itself. */
1922 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1923 return;
1924
05b28fd6
MP
1925 if (operand_equal_p (lhs, rhs, 0))
1926 {
1927 /* Don't warn about array references with constant indices;
1928 these are likely to come from a macro. */
1929 bool found = false;
1930 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1931 &found);
1932 if (found)
1933 return;
1934 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1935 || code == GE_EXPR || code == UNLE_EXPR
1936 || code == UNGE_EXPR || code == UNEQ_EXPR);
1937 if (always_true)
1938 warning_at (loc, OPT_Wtautological_compare,
1939 "self-comparison always evaluates to true");
1940 else
1941 warning_at (loc, OPT_Wtautological_compare,
1942 "self-comparison always evaluates to false");
1943 }
1944}
1945
742938c9
MP
1946/* Warn about logical not used on the left hand side operand of a comparison.
1947 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
59ea0364 1948 Do not warn if RHS is of a boolean type. */
742938c9
MP
1949
1950void
1951warn_logical_not_parentheses (location_t location, enum tree_code code,
59ea0364 1952 tree rhs)
742938c9 1953{
59ea0364
MP
1954 if (TREE_CODE_CLASS (code) != tcc_comparison
1955 || TREE_TYPE (rhs) == NULL_TREE
1956 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
742938c9
MP
1957 return;
1958
7ccb1a11
JJ
1959 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1960 !(x == 0) or !(y != 0). */
1961 if ((code == EQ_EXPR || code == NE_EXPR)
1962 && integer_zerop (rhs))
1963 return;
1964
742938c9
MP
1965 warning_at (location, OPT_Wlogical_not_parentheses,
1966 "logical not is only applied to the left hand side of "
1967 "comparison");
1968}
63a08740 1969
fd4116f4
MLI
1970/* Warn if EXP contains any computations whose results are not used.
1971 Return true if a warning is printed; false otherwise. LOCUS is the
1972 (potential) location of the expression. */
1973
1974bool
1975warn_if_unused_value (const_tree exp, location_t locus)
1976{
1977 restart:
1978 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1979 return false;
1980
1981 /* Don't warn about void constructs. This includes casting to void,
1982 void function calls, and statement expressions with a final cast
1983 to void. */
1984 if (VOID_TYPE_P (TREE_TYPE (exp)))
1985 return false;
1986
1987 if (EXPR_HAS_LOCATION (exp))
1988 locus = EXPR_LOCATION (exp);
1989
1990 switch (TREE_CODE (exp))
1991 {
1992 case PREINCREMENT_EXPR:
1993 case POSTINCREMENT_EXPR:
1994 case PREDECREMENT_EXPR:
1995 case POSTDECREMENT_EXPR:
1996 case MODIFY_EXPR:
1997 case INIT_EXPR:
1998 case TARGET_EXPR:
1999 case CALL_EXPR:
2000 case TRY_CATCH_EXPR:
2001 case WITH_CLEANUP_EXPR:
2002 case EXIT_EXPR:
2003 case VA_ARG_EXPR:
2004 return false;
2005
2006 case BIND_EXPR:
2007 /* For a binding, warn if no side effect within it. */
2008 exp = BIND_EXPR_BODY (exp);
2009 goto restart;
2010
2011 case SAVE_EXPR:
2012 case NON_LVALUE_EXPR:
007a787d 2013 case NOP_EXPR:
fd4116f4
MLI
2014 exp = TREE_OPERAND (exp, 0);
2015 goto restart;
2016
2017 case TRUTH_ORIF_EXPR:
2018 case TRUTH_ANDIF_EXPR:
2019 /* In && or ||, warn if 2nd operand has no side effect. */
2020 exp = TREE_OPERAND (exp, 1);
2021 goto restart;
2022
2023 case COMPOUND_EXPR:
2024 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
2025 return true;
2026 /* Let people do `(foo (), 0)' without a warning. */
2027 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
2028 return false;
2029 exp = TREE_OPERAND (exp, 1);
2030 goto restart;
2031
2032 case COND_EXPR:
2033 /* If this is an expression with side effects, don't warn; this
2034 case commonly appears in macro expansions. */
2035 if (TREE_SIDE_EFFECTS (exp))
2036 return false;
2037 goto warn;
2038
2039 case INDIRECT_REF:
2040 /* Don't warn about automatic dereferencing of references, since
2041 the user cannot control it. */
2042 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
2043 {
2044 exp = TREE_OPERAND (exp, 0);
2045 goto restart;
2046 }
2047 /* Fall through. */
2048
2049 default:
2050 /* Referencing a volatile value is a side effect, so don't warn. */
2051 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
2052 && TREE_THIS_VOLATILE (exp))
2053 return false;
2054
2055 /* If this is an expression which has no operands, there is no value
2056 to be unused. There are no such language-independent codes,
2057 but front ends may define such. */
2058 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
2059 return false;
2060
2061 warn:
2062 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
2063 }
2064}
2065
2066
de9c56a4
RG
2067/* Print a warning about casts that might indicate violation
2068 of strict aliasing rules if -Wstrict-aliasing is used and
3f0a2a47
DM
2069 strict aliasing mode is in effect. OTYPE is the original
2070 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
de9c56a4 2071
79bedddc 2072bool
3f0a2a47 2073strict_aliasing_warning (tree otype, tree type, tree expr)
de9c56a4 2074{
255d3827
RG
2075 /* Strip pointer conversion chains and get to the correct original type. */
2076 STRIP_NOPS (expr);
2077 otype = TREE_TYPE (expr);
2078
ac7ee6ad
RG
2079 if (!(flag_strict_aliasing
2080 && POINTER_TYPE_P (type)
2081 && POINTER_TYPE_P (otype)
2082 && !VOID_TYPE_P (TREE_TYPE (type)))
2083 /* If the type we are casting to is a ref-all pointer
2084 dereferencing it is always valid. */
2085 || TYPE_REF_CAN_ALIAS_ALL (type))
79bedddc
SR
2086 return false;
2087
2088 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
de9c56a4 2089 && (DECL_P (TREE_OPERAND (expr, 0))
79bedddc 2090 || handled_component_p (TREE_OPERAND (expr, 0))))
de9c56a4
RG
2091 {
2092 /* Casting the address of an object to non void pointer. Warn
2093 if the cast breaks type based aliasing. */
79bedddc
SR
2094 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2095 {
2096 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2097 "might break strict-aliasing rules");
2098 return true;
2099 }
de9c56a4
RG
2100 else
2101 {
b8698a0f 2102 /* warn_strict_aliasing >= 3. This includes the default (3).
79bedddc 2103 Only warn if the cast is dereferenced immediately. */
4862826d 2104 alias_set_type set1 =
79bedddc 2105 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
4862826d 2106 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
de9c56a4 2107
4653cae5
RG
2108 if (set1 != set2 && set2 != 0
2109 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
79bedddc
SR
2110 {
2111 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2112 "pointer will break strict-aliasing rules");
2113 return true;
2114 }
2115 else if (warn_strict_aliasing == 2
836f7794 2116 && !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
2117 {
2118 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2119 "pointer might break strict-aliasing rules");
2120 return true;
2121 }
de9c56a4
RG
2122 }
2123 }
79bedddc
SR
2124 else
2125 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2126 {
2127 /* At this level, warn for any conversions, even if an address is
2128 not taken in the same statement. This will likely produce many
2129 false positives, but could be useful to pinpoint problems that
2130 are not revealed at higher levels. */
4862826d
ILT
2131 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2132 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2133 if (!COMPLETE_TYPE_P (type)
836f7794 2134 || !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
2135 {
2136 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2137 "pointer might break strict-aliasing rules");
2138 return true;
2139 }
2140 }
2141
2142 return false;
de9c56a4
RG
2143}
2144
1a4049e7
JJ
2145/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2146 sizeof as last operand of certain builtins. */
2147
2148void
3a785c97 2149sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
9771b263 2150 vec<tree, va_gc> *params, tree *sizeof_arg,
1a4049e7
JJ
2151 bool (*comp_types) (tree, tree))
2152{
2153 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
3a785c97
JJ
2154 bool strop = false, cmp = false;
2155 unsigned int idx = ~0;
2156 location_t loc;
1a4049e7
JJ
2157
2158 if (TREE_CODE (callee) != FUNCTION_DECL
2159 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
9771b263 2160 || vec_safe_length (params) <= 1)
1a4049e7
JJ
2161 return;
2162
1a4049e7
JJ
2163 switch (DECL_FUNCTION_CODE (callee))
2164 {
2165 case BUILT_IN_STRNCMP:
2166 case BUILT_IN_STRNCASECMP:
3a785c97
JJ
2167 cmp = true;
2168 /* FALLTHRU */
1a4049e7 2169 case BUILT_IN_STRNCPY:
3a785c97 2170 case BUILT_IN_STRNCPY_CHK:
1a4049e7 2171 case BUILT_IN_STRNCAT:
3a785c97
JJ
2172 case BUILT_IN_STRNCAT_CHK:
2173 case BUILT_IN_STPNCPY:
2174 case BUILT_IN_STPNCPY_CHK:
1a4049e7
JJ
2175 strop = true;
2176 /* FALLTHRU */
2177 case BUILT_IN_MEMCPY:
3a785c97 2178 case BUILT_IN_MEMCPY_CHK:
1a4049e7 2179 case BUILT_IN_MEMMOVE:
3a785c97 2180 case BUILT_IN_MEMMOVE_CHK:
9771b263 2181 if (params->length () < 3)
3a785c97 2182 return;
9771b263
DN
2183 src = (*params)[1];
2184 dest = (*params)[0];
3a785c97
JJ
2185 idx = 2;
2186 break;
2187 case BUILT_IN_BCOPY:
9771b263 2188 if (params->length () < 3)
3a785c97 2189 return;
9771b263
DN
2190 src = (*params)[0];
2191 dest = (*params)[1];
3a785c97
JJ
2192 idx = 2;
2193 break;
1a4049e7 2194 case BUILT_IN_MEMCMP:
3a785c97 2195 case BUILT_IN_BCMP:
9771b263 2196 if (params->length () < 3)
1a4049e7 2197 return;
9771b263
DN
2198 src = (*params)[1];
2199 dest = (*params)[0];
3a785c97
JJ
2200 idx = 2;
2201 cmp = true;
1a4049e7
JJ
2202 break;
2203 case BUILT_IN_MEMSET:
3a785c97 2204 case BUILT_IN_MEMSET_CHK:
9771b263 2205 if (params->length () < 3)
1a4049e7 2206 return;
9771b263 2207 dest = (*params)[0];
3a785c97
JJ
2208 idx = 2;
2209 break;
2210 case BUILT_IN_BZERO:
9771b263 2211 dest = (*params)[0];
3a785c97 2212 idx = 1;
1a4049e7
JJ
2213 break;
2214 case BUILT_IN_STRNDUP:
9771b263 2215 src = (*params)[0];
1a4049e7 2216 strop = true;
3a785c97
JJ
2217 idx = 1;
2218 break;
2219 case BUILT_IN_MEMCHR:
9771b263 2220 if (params->length () < 3)
3a785c97 2221 return;
9771b263 2222 src = (*params)[0];
3a785c97
JJ
2223 idx = 2;
2224 break;
2225 case BUILT_IN_SNPRINTF:
2226 case BUILT_IN_SNPRINTF_CHK:
2227 case BUILT_IN_VSNPRINTF:
2228 case BUILT_IN_VSNPRINTF_CHK:
9771b263 2229 dest = (*params)[0];
3a785c97
JJ
2230 idx = 1;
2231 strop = true;
1a4049e7
JJ
2232 break;
2233 default:
2234 break;
2235 }
2236
3a785c97
JJ
2237 if (idx >= 3)
2238 return;
2239
2240 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2241 return;
2242
2243 type = TYPE_P (sizeof_arg[idx])
2244 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2245 if (!POINTER_TYPE_P (type))
2246 return;
2247
1a4049e7
JJ
2248 if (dest
2249 && (tem = tree_strip_nop_conversions (dest))
2250 && POINTER_TYPE_P (TREE_TYPE (tem))
2251 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2252 return;
2253
2254 if (src
2255 && (tem = tree_strip_nop_conversions (src))
2256 && POINTER_TYPE_P (TREE_TYPE (tem))
2257 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2258 return;
2259
3a785c97
JJ
2260 loc = sizeof_arg_loc[idx];
2261
2262 if (dest && !cmp)
1a4049e7 2263 {
3a785c97
JJ
2264 if (!TYPE_P (sizeof_arg[idx])
2265 && operand_equal_p (dest, sizeof_arg[idx], 0)
1a4049e7
JJ
2266 && comp_types (TREE_TYPE (dest), type))
2267 {
3a785c97 2268 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
2269 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2270 "argument to %<sizeof%> in %qD call is the same "
2271 "expression as the destination; did you mean to "
2272 "remove the addressof?", callee);
2273 else if ((TYPE_PRECISION (TREE_TYPE (type))
2274 == TYPE_PRECISION (char_type_node))
2275 || strop)
2276 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2277 "argument to %<sizeof%> in %qD call is the same "
2278 "expression as the destination; did you mean to "
2279 "provide an explicit length?", callee);
2280 else
2281 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2282 "argument to %<sizeof%> in %qD call is the same "
2283 "expression as the destination; did you mean to "
2284 "dereference it?", callee);
2285 return;
2286 }
2287
2288 if (POINTER_TYPE_P (TREE_TYPE (dest))
2289 && !strop
2290 && comp_types (TREE_TYPE (dest), type)
2291 && !VOID_TYPE_P (TREE_TYPE (type)))
2292 {
2293 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2294 "argument to %<sizeof%> in %qD call is the same "
2295 "pointer type %qT as the destination; expected %qT "
2296 "or an explicit length", callee, TREE_TYPE (dest),
2297 TREE_TYPE (TREE_TYPE (dest)));
2298 return;
2299 }
2300 }
2301
3a785c97 2302 if (src && !cmp)
1a4049e7 2303 {
3a785c97
JJ
2304 if (!TYPE_P (sizeof_arg[idx])
2305 && operand_equal_p (src, sizeof_arg[idx], 0)
1a4049e7
JJ
2306 && comp_types (TREE_TYPE (src), type))
2307 {
3a785c97 2308 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
2309 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2310 "argument to %<sizeof%> in %qD call is the same "
2311 "expression as the source; did you mean to "
2312 "remove the addressof?", callee);
2313 else if ((TYPE_PRECISION (TREE_TYPE (type))
2314 == TYPE_PRECISION (char_type_node))
2315 || strop)
2316 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2317 "argument to %<sizeof%> in %qD call is the same "
2318 "expression as the source; did you mean to "
2319 "provide an explicit length?", callee);
2320 else
2321 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2322 "argument to %<sizeof%> in %qD call is the same "
2323 "expression as the source; did you mean to "
2324 "dereference it?", callee);
2325 return;
2326 }
2327
2328 if (POINTER_TYPE_P (TREE_TYPE (src))
2329 && !strop
2330 && comp_types (TREE_TYPE (src), type)
2331 && !VOID_TYPE_P (TREE_TYPE (type)))
2332 {
2333 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2334 "argument to %<sizeof%> in %qD call is the same "
2335 "pointer type %qT as the source; expected %qT "
2336 "or an explicit length", callee, TREE_TYPE (src),
2337 TREE_TYPE (TREE_TYPE (src)));
2338 return;
2339 }
2340 }
3a785c97
JJ
2341
2342 if (dest)
2343 {
2344 if (!TYPE_P (sizeof_arg[idx])
2345 && operand_equal_p (dest, sizeof_arg[idx], 0)
2346 && comp_types (TREE_TYPE (dest), type))
2347 {
2348 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2349 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2350 "argument to %<sizeof%> in %qD call is the same "
2351 "expression as the first source; did you mean to "
2352 "remove the addressof?", callee);
2353 else if ((TYPE_PRECISION (TREE_TYPE (type))
2354 == TYPE_PRECISION (char_type_node))
2355 || strop)
2356 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2357 "argument to %<sizeof%> in %qD call is the same "
2358 "expression as the first source; did you mean to "
2359 "provide an explicit length?", callee);
2360 else
2361 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2362 "argument to %<sizeof%> in %qD call is the same "
2363 "expression as the first source; did you mean to "
2364 "dereference it?", callee);
2365 return;
2366 }
2367
2368 if (POINTER_TYPE_P (TREE_TYPE (dest))
2369 && !strop
2370 && comp_types (TREE_TYPE (dest), type)
2371 && !VOID_TYPE_P (TREE_TYPE (type)))
2372 {
2373 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2374 "argument to %<sizeof%> in %qD call is the same "
2375 "pointer type %qT as the first source; expected %qT "
2376 "or an explicit length", callee, TREE_TYPE (dest),
2377 TREE_TYPE (TREE_TYPE (dest)));
2378 return;
2379 }
2380 }
2381
2382 if (src)
2383 {
2384 if (!TYPE_P (sizeof_arg[idx])
2385 && operand_equal_p (src, sizeof_arg[idx], 0)
2386 && comp_types (TREE_TYPE (src), type))
2387 {
2388 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2389 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2390 "argument to %<sizeof%> in %qD call is the same "
2391 "expression as the second source; did you mean to "
2392 "remove the addressof?", callee);
2393 else if ((TYPE_PRECISION (TREE_TYPE (type))
2394 == TYPE_PRECISION (char_type_node))
2395 || strop)
2396 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2397 "argument to %<sizeof%> in %qD call is the same "
2398 "expression as the second source; did you mean to "
2399 "provide an explicit length?", callee);
2400 else
2401 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2402 "argument to %<sizeof%> in %qD call is the same "
2403 "expression as the second source; did you mean to "
2404 "dereference it?", callee);
2405 return;
2406 }
2407
2408 if (POINTER_TYPE_P (TREE_TYPE (src))
2409 && !strop
2410 && comp_types (TREE_TYPE (src), type)
2411 && !VOID_TYPE_P (TREE_TYPE (type)))
2412 {
2413 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2414 "argument to %<sizeof%> in %qD call is the same "
2415 "pointer type %qT as the second source; expected %qT "
2416 "or an explicit length", callee, TREE_TYPE (src),
2417 TREE_TYPE (TREE_TYPE (src)));
2418 return;
2419 }
2420 }
2421
1a4049e7
JJ
2422}
2423
a1e45ff0
DM
2424/* Warn for unlikely, improbable, or stupid DECL declarations
2425 of `main'. */
2426
2427void
2428check_main_parameter_types (tree decl)
2429{
e19a18d4
NF
2430 function_args_iterator iter;
2431 tree type;
a1e45ff0
DM
2432 int argct = 0;
2433
e19a18d4
NF
2434 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2435 {
2436 /* XXX void_type_node belies the abstraction. */
2437 if (type == void_type_node || type == error_mark_node )
2438 break;
2439
f827930a
MP
2440 tree t = type;
2441 if (TYPE_ATOMIC (t))
2442 pedwarn (input_location, OPT_Wmain,
2443 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2444 type, decl);
2445 while (POINTER_TYPE_P (t))
2446 {
2447 t = TREE_TYPE (t);
2448 if (TYPE_ATOMIC (t))
2449 pedwarn (input_location, OPT_Wmain,
2450 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2451 type, decl);
2452 }
2453
e19a18d4
NF
2454 ++argct;
2455 switch (argct)
2456 {
2457 case 1:
2458 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2459 pedwarn (input_location, OPT_Wmain,
2460 "first argument of %q+D should be %<int%>", decl);
2461 break;
2462
2463 case 2:
2464 if (TREE_CODE (type) != POINTER_TYPE
2465 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2466 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2467 != char_type_node))
2468 pedwarn (input_location, OPT_Wmain,
2469 "second argument of %q+D should be %<char **%>", decl);
2470 break;
2471
2472 case 3:
2473 if (TREE_CODE (type) != POINTER_TYPE
2474 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2475 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2476 != char_type_node))
2477 pedwarn (input_location, OPT_Wmain,
2478 "third argument of %q+D should probably be "
2479 "%<char **%>", decl);
2480 break;
2481 }
2482 }
a1e45ff0
DM
2483
2484 /* It is intentional that this message does not mention the third
2485 argument because it's only mentioned in an appendix of the
2486 standard. */
2487 if (argct > 0 && (argct < 2 || argct > 3))
e19a18d4
NF
2488 pedwarn (input_location, OPT_Wmain,
2489 "%q+D takes only zero or two arguments", decl);
38e514c0
MP
2490
2491 if (stdarg_p (TREE_TYPE (decl)))
2492 pedwarn (input_location, OPT_Wmain,
2493 "%q+D declared as variadic function", decl);
a1e45ff0
DM
2494}
2495
0af94e6f
JR
2496/* vector_targets_convertible_p is used for vector pointer types. The
2497 callers perform various checks that the qualifiers are satisfactory,
2498 while OTOH vector_targets_convertible_p ignores the number of elements
2499 in the vectors. That's fine with vector pointers as we can consider,
2500 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2501 and that does not require and conversion of the pointer values.
2502 In contrast, vector_types_convertible_p and
2503 vector_types_compatible_elements_p are used for vector value types. */
f83c7f63
DJ
2504/* True if pointers to distinct types T1 and T2 can be converted to
2505 each other without an explicit cast. Only returns true for opaque
2506 vector types. */
2507bool
2508vector_targets_convertible_p (const_tree t1, const_tree t2)
2509{
31521951 2510 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
b6fc2cdb 2511 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
f83c7f63
DJ
2512 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2513 return true;
2514
2515 return false;
2516}
2517
0af94e6f
JR
2518/* vector_types_convertible_p is used for vector value types.
2519 It could in principle call vector_targets_convertible_p as a subroutine,
2520 but then the check for vector type would be duplicated with its callers,
2521 and also the purpose of vector_targets_convertible_p would become
2522 muddled.
2523 Where vector_types_convertible_p returns true, a conversion might still be
2524 needed to make the types match.
2525 In contrast, vector_targets_convertible_p is used for vector pointer
2526 values, and vector_types_compatible_elements_p is used specifically
2527 in the context for binary operators, as a check if use is possible without
2528 conversion. */
00c8e9f6
MS
2529/* True if vector types T1 and T2 can be converted to each other
2530 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2531 can only be converted with -flax-vector-conversions yet that is not
2532 in effect, emit a note telling the user about that option if such
2533 a note has not previously been emitted. */
2534bool
58f9752a 2535vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
cc27e657 2536{
00c8e9f6 2537 static bool emitted_lax_note = false;
14e765da
JM
2538 bool convertible_lax;
2539
b6fc2cdb 2540 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
14e765da
JM
2541 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2542 return true;
2543
2544 convertible_lax =
2545 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2546 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
a5e0cd1d 2547 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
14e765da
JM
2548 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2549 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
00c8e9f6
MS
2550
2551 if (!convertible_lax || flag_lax_vector_conversions)
2552 return convertible_lax;
2553
2554 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
cf7bc668 2555 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
00c8e9f6
MS
2556 return true;
2557
2558 if (emit_lax_note && !emitted_lax_note)
2559 {
2560 emitted_lax_note = true;
1f5b3869 2561 inform (input_location, "use -flax-vector-conversions to permit "
00c8e9f6
MS
2562 "conversions between vectors with differing "
2563 "element types or numbers of subparts");
2564 }
2565
2566 return false;
cc27e657
PB
2567}
2568
9e1a8dd1
RR
2569/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2570 and have vector types, V0 has the same type as V1, and the number of
2571 elements of V0, V1, MASK is the same.
2572
2573 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2574 called with two arguments. In this case implementation passes the
2575 first argument twice in order to share the same tree code. This fact
2576 could enable the mask-values being twice the vector length. This is
2577 an implementation accident and this semantics is not guaranteed to
2578 the user. */
2579tree
bedc293e
MG
2580c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2581 bool complain)
9e1a8dd1
RR
2582{
2583 tree ret;
2584 bool wrap = true;
2585 bool maybe_const = false;
2586 bool two_arguments = false;
2587
2588 if (v1 == NULL_TREE)
2589 {
2590 two_arguments = true;
2591 v1 = v0;
2592 }
2593
2594 if (v0 == error_mark_node || v1 == error_mark_node
2595 || mask == error_mark_node)
2596 return error_mark_node;
2597
31521951 2598 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
9e1a8dd1 2599 {
bedc293e
MG
2600 if (complain)
2601 error_at (loc, "__builtin_shuffle last argument must "
2602 "be an integer vector");
9e1a8dd1
RR
2603 return error_mark_node;
2604 }
2605
31521951
MP
2606 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2607 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
9e1a8dd1 2608 {
bedc293e
MG
2609 if (complain)
2610 error_at (loc, "__builtin_shuffle arguments must be vectors");
9e1a8dd1
RR
2611 return error_mark_node;
2612 }
2613
2614 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2615 {
bedc293e
MG
2616 if (complain)
2617 error_at (loc, "__builtin_shuffle argument vectors must be of "
2618 "the same type");
9e1a8dd1
RR
2619 return error_mark_node;
2620 }
2621
2622 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2623 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2624 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2625 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2626 {
bedc293e
MG
2627 if (complain)
2628 error_at (loc, "__builtin_shuffle number of elements of the "
2629 "argument vector(s) and the mask vector should "
2630 "be the same");
9e1a8dd1
RR
2631 return error_mark_node;
2632 }
2633
2634 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2635 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2636 {
bedc293e
MG
2637 if (complain)
2638 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2639 "must have the same size as inner type of the mask");
9e1a8dd1
RR
2640 return error_mark_node;
2641 }
2642
2643 if (!c_dialect_cxx ())
2644 {
2645 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2646 v0 = c_fully_fold (v0, false, &maybe_const);
2647 wrap &= maybe_const;
2648
2649 if (two_arguments)
2650 v1 = v0 = save_expr (v0);
2651 else
2652 {
2653 v1 = c_fully_fold (v1, false, &maybe_const);
2654 wrap &= maybe_const;
2655 }
2656
2657 mask = c_fully_fold (mask, false, &maybe_const);
2658 wrap &= maybe_const;
2659 }
bedc293e
MG
2660 else if (two_arguments)
2661 v1 = v0 = save_expr (v0);
9e1a8dd1
RR
2662
2663 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2664
2665 if (!c_dialect_cxx () && !wrap)
2666 ret = c_wrap_maybe_const (ret, true);
2667
2668 return ret;
2669}
2670
828fb3ba
JM
2671/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2672 to integral type. */
2673
2674static tree
2675c_common_get_narrower (tree op, int *unsignedp_ptr)
2676{
2677 op = get_narrower (op, unsignedp_ptr);
2678
2679 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2680 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2681 {
2682 /* C++0x scoped enumerations don't implicitly convert to integral
2683 type; if we stripped an explicit conversion to a larger type we
2684 need to replace it so common_type will still work. */
21fa2faf
RG
2685 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2686 TYPE_UNSIGNED (TREE_TYPE (op)));
828fb3ba
JM
2687 op = fold_convert (type, op);
2688 }
2689 return op;
2690}
2691
6715192c
MLI
2692/* This is a helper function of build_binary_op.
2693
2694 For certain operations if both args were extended from the same
2695 smaller type, do the arithmetic in that type and then extend.
2696
2697 BITWISE indicates a bitwise operation.
2698 For them, this optimization is safe only if
2699 both args are zero-extended or both are sign-extended.
2700 Otherwise, we might change the result.
2701 Eg, (short)-1 | (unsigned short)-1 is (int)-1
b8698a0f 2702 but calculated in (unsigned short) it would be (unsigned short)-1.
6715192c 2703*/
828fb3ba
JM
2704tree
2705shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
6715192c
MLI
2706{
2707 int unsigned0, unsigned1;
2708 tree arg0, arg1;
2709 int uns;
2710 tree type;
2711
2712 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2713 excessive narrowing when we call get_narrower below. For
2714 example, suppose that OP0 is of unsigned int extended
2715 from signed char and that RESULT_TYPE is long long int.
2716 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2717 like
b8698a0f 2718
6715192c
MLI
2719 (long long int) (unsigned int) signed_char
2720
2721 which get_narrower would narrow down to
b8698a0f 2722
6715192c 2723 (unsigned int) signed char
b8698a0f 2724
6715192c
MLI
2725 If we do not cast OP0 first, get_narrower would return
2726 signed_char, which is inconsistent with the case of the
2727 explicit cast. */
2728 op0 = convert (result_type, op0);
2729 op1 = convert (result_type, op1);
2730
828fb3ba
JM
2731 arg0 = c_common_get_narrower (op0, &unsigned0);
2732 arg1 = c_common_get_narrower (op1, &unsigned1);
6715192c
MLI
2733
2734 /* UNS is 1 if the operation to be done is an unsigned one. */
2735 uns = TYPE_UNSIGNED (result_type);
2736
2737 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2738 but it *requires* conversion to FINAL_TYPE. */
b8698a0f 2739
6715192c
MLI
2740 if ((TYPE_PRECISION (TREE_TYPE (op0))
2741 == TYPE_PRECISION (TREE_TYPE (arg0)))
2742 && TREE_TYPE (op0) != result_type)
2743 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2744 if ((TYPE_PRECISION (TREE_TYPE (op1))
2745 == TYPE_PRECISION (TREE_TYPE (arg1)))
2746 && TREE_TYPE (op1) != result_type)
2747 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b8698a0f 2748
6715192c 2749 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
b8698a0f 2750
6715192c
MLI
2751 /* For bitwise operations, signedness of nominal type
2752 does not matter. Consider only how operands were extended. */
2753 if (bitwise)
2754 uns = unsigned0;
b8698a0f 2755
6715192c
MLI
2756 /* Note that in all three cases below we refrain from optimizing
2757 an unsigned operation on sign-extended args.
2758 That would not be valid. */
b8698a0f 2759
6715192c
MLI
2760 /* Both args variable: if both extended in same way
2761 from same width, do it in that width.
2762 Do it unsigned if args were zero-extended. */
2763 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2764 < TYPE_PRECISION (result_type))
2765 && (TYPE_PRECISION (TREE_TYPE (arg1))
2766 == TYPE_PRECISION (TREE_TYPE (arg0)))
2767 && unsigned0 == unsigned1
2768 && (unsigned0 || !uns))
2769 return c_common_signed_or_unsigned_type
2770 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2771
2772 else if (TREE_CODE (arg0) == INTEGER_CST
2773 && (unsigned1 || !uns)
2774 && (TYPE_PRECISION (TREE_TYPE (arg1))
2775 < TYPE_PRECISION (result_type))
2776 && (type
2777 = c_common_signed_or_unsigned_type (unsigned1,
2778 TREE_TYPE (arg1)))
2779 && !POINTER_TYPE_P (type)
2780 && int_fits_type_p (arg0, type))
2781 return type;
2782
2783 else if (TREE_CODE (arg1) == INTEGER_CST
2784 && (unsigned0 || !uns)
2785 && (TYPE_PRECISION (TREE_TYPE (arg0))
2786 < TYPE_PRECISION (result_type))
2787 && (type
2788 = c_common_signed_or_unsigned_type (unsigned0,
2789 TREE_TYPE (arg0)))
2790 && !POINTER_TYPE_P (type)
2791 && int_fits_type_p (arg1, type))
2792 return type;
2793
2794 return result_type;
2795}
2796
7a37fa90
MM
2797/* Returns true iff any integer value of type FROM_TYPE can be represented as
2798 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2799
2800static bool
2801int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2802{
2803 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2804 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2805 REAL_VALUE_TYPE real_low_bound =
2806 real_value_from_int_cst (0, type_low_bound);
2807 REAL_VALUE_TYPE real_high_bound =
2808 real_value_from_int_cst (0, type_high_bound);
2809
2810 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2811 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2812}
2813
2814/* Checks if expression EXPR of complex/real/integer type cannot be converted
2815 to the complex/real/integer type TYPE. Function returns non-zero when:
68fca595
MP
2816 * EXPR is a constant which cannot be exactly converted to TYPE.
2817 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
7a37fa90
MM
2818 for EXPR type and TYPE being both integers or both real, or both
2819 complex.
2820 * EXPR is not a constant of complex type and TYPE is a real or
2821 an integer.
68fca595
MP
2822 * EXPR is not a constant of real type and TYPE is an integer.
2823 * EXPR is not a constant of integer type which cannot be
2824 exactly converted to real type.
7a37fa90 2825
0e3a99ae 2826 Function allows conversions between types of different signedness and
49b0aa18 2827 can return SAFE_CONVERSION (zero) in that case. Function can produce
7a37fa90
MM
2828 signedness warnings if PRODUCE_WARNS is true.
2829
2830 Function allows conversions from complex constants to non-complex types,
2831 provided that imaginary part is zero and real part can be safely converted
2832 to TYPE. */
68fca595 2833
49b0aa18 2834enum conversion_safety
68fca595 2835unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
422c3a54 2836{
49b0aa18 2837 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
374035cb 2838 tree expr_type = TREE_TYPE (expr);
68fca595 2839 loc = expansion_point_location_if_in_system_header (loc);
422c3a54 2840
0e3a99ae 2841 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
0011dedb 2842 {
7a37fa90
MM
2843 /* If type is complex, we are interested in compatibility with
2844 underlying type. */
2845 if (TREE_CODE (type) == COMPLEX_TYPE)
2846 type = TREE_TYPE (type);
2847
422c3a54 2848 /* Warn for real constant that is not an exact integer converted
0e3a99ae 2849 to integer type. */
374035cb 2850 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2851 && TREE_CODE (type) == INTEGER_TYPE)
2852 {
2853 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
49b0aa18 2854 give_warning = UNSAFE_REAL;
0e3a99ae 2855 }
91c41804 2856 /* Warn for an integer constant that does not fit into integer type. */
374035cb 2857 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2858 && TREE_CODE (type) == INTEGER_TYPE
2859 && !int_fits_type_p (expr, type))
2860 {
2861 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
374035cb 2862 && tree_int_cst_sgn (expr) < 0)
0e3a99ae
AS
2863 {
2864 if (produce_warns)
2865 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2866 " implicitly converted to unsigned type");
2867 }
2868 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2869 {
2870 if (produce_warns)
2871 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2872 " constant value to negative integer");
2873 }
7060db96 2874 else
49b0aa18 2875 give_warning = UNSAFE_OTHER;
0e3a99ae 2876 }
422c3a54 2877 else if (TREE_CODE (type) == REAL_TYPE)
0e3a99ae
AS
2878 {
2879 /* Warn for an integer constant that does not fit into real type. */
2880 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2881 {
2882 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2883 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2884 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2885 }
2886 /* Warn for a real constant that does not fit into a smaller
2887 real type. */
2888 else if (TREE_CODE (expr_type) == REAL_TYPE
2889 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2890 {
2891 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2892 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2893 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2894 }
2895 }
2896 }
7a37fa90
MM
2897
2898 else if (TREE_CODE (expr) == COMPLEX_CST)
2899 {
2900 tree imag_part = TREE_IMAGPART (expr);
2901 /* Conversion from complex constant with zero imaginary part,
2902 perform check for conversion of real part. */
2903 if ((TREE_CODE (imag_part) == REAL_CST
2904 && real_zerop (imag_part))
2905 || (TREE_CODE (imag_part) == INTEGER_CST
2906 && integer_zerop (imag_part)))
2907 /* Note: in this branch we use recursive call to unsafe_conversion_p
2908 with different type of EXPR, but it is still safe, because when EXPR
2909 is a constant, it's type is not used in text of generated warnings
2910 (otherwise they could sound misleading). */
2911 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2912 produce_warns);
2913 /* Conversion from complex constant with non-zero imaginary part. */
2914 else
2915 {
2916 /* Conversion to complex type.
2917 Perform checks for both real and imaginary parts. */
2918 if (TREE_CODE (type) == COMPLEX_TYPE)
2919 {
2920 /* Unfortunately, produce_warns must be false in two subsequent
2921 calls of unsafe_conversion_p, because otherwise we could
2922 produce strange "double" warnings, if both real and imaginary
2923 parts have conversion problems related to signedness.
2924
2925 For example:
2926 int32_t _Complex a = 0x80000000 + 0x80000000i;
2927
2928 Possible solution: add a separate function for checking
2929 constants and combine result of two calls appropriately. */
2930 enum conversion_safety re_safety =
2931 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2932 enum conversion_safety im_safety =
2933 unsafe_conversion_p (loc, type, imag_part, false);
2934
2935 /* Merge the results into appropriate single warning. */
2936
2937 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2938 if (re_safety == im_safety)
2939 give_warning = re_safety;
2940 else if (!re_safety && im_safety)
2941 give_warning = im_safety;
2942 else if (re_safety && !im_safety)
2943 give_warning = re_safety;
2944 else
2945 give_warning = UNSAFE_OTHER;
2946 }
2947 /* Warn about conversion from complex to real or integer type. */
2948 else
2949 give_warning = UNSAFE_IMAGINARY;
2950 }
2951 }
2952
2953 /* Checks for remaining case: EXPR is not constant. */
0e3a99ae
AS
2954 else
2955 {
422c3a54 2956 /* Warn for real types converted to integer types. */
6715192c 2957 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae 2958 && TREE_CODE (type) == INTEGER_TYPE)
49b0aa18 2959 give_warning = UNSAFE_REAL;
422c3a54 2960
6715192c 2961 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2962 && TREE_CODE (type) == INTEGER_TYPE)
2963 {
cfdaefec 2964 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
c00e8b06 2965 expr = get_unwidened (expr, 0);
6715192c 2966 expr_type = TREE_TYPE (expr);
cfdaefec 2967
6715192c 2968 /* Don't warn for short y; short x = ((int)y & 0xff); */
b8698a0f 2969 if (TREE_CODE (expr) == BIT_AND_EXPR
0e3a99ae 2970 || TREE_CODE (expr) == BIT_IOR_EXPR
6715192c
MLI
2971 || TREE_CODE (expr) == BIT_XOR_EXPR)
2972 {
374035cb
MLI
2973 /* If both args were extended from a shortest type,
2974 use that type if that is safe. */
b8698a0f
L
2975 expr_type = shorten_binary_op (expr_type,
2976 TREE_OPERAND (expr, 0),
2977 TREE_OPERAND (expr, 1),
6715192c
MLI
2978 /* bitwise */1);
2979
6715192c
MLI
2980 if (TREE_CODE (expr) == BIT_AND_EXPR)
2981 {
2982 tree op0 = TREE_OPERAND (expr, 0);
2983 tree op1 = TREE_OPERAND (expr, 1);
9c591bd0
MLI
2984 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2985 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2986
2987 /* If one of the operands is a non-negative constant
2988 that fits in the target type, then the type of the
2989 other operand does not matter. */
6715192c
MLI
2990 if ((TREE_CODE (op0) == INTEGER_CST
2991 && int_fits_type_p (op0, c_common_signed_type (type))
2992 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2993 || (TREE_CODE (op1) == INTEGER_CST
374035cb 2994 && int_fits_type_p (op1, c_common_signed_type (type))
b8698a0f 2995 && int_fits_type_p (op1,
374035cb 2996 c_common_unsigned_type (type))))
49b0aa18 2997 return SAFE_CONVERSION;
9c591bd0
MLI
2998 /* If constant is unsigned and fits in the target
2999 type, then the result will also fit. */
3000 else if ((TREE_CODE (op0) == INTEGER_CST
b8698a0f 3001 && unsigned0
9c591bd0
MLI
3002 && int_fits_type_p (op0, type))
3003 || (TREE_CODE (op1) == INTEGER_CST
3004 && unsigned1
3005 && int_fits_type_p (op1, type)))
49b0aa18 3006 return SAFE_CONVERSION;
6715192c
MLI
3007 }
3008 }
0e3a99ae 3009 /* Warn for integer types converted to smaller integer types. */
b8698a0f 3010 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 3011 give_warning = UNSAFE_OTHER;
7060db96
MLI
3012
3013 /* When they are the same width but different signedness,
3014 then the value may change. */
0e3a99ae 3015 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
6715192c 3016 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7060db96
MLI
3017 /* Even when converted to a bigger type, if the type is
3018 unsigned but expr is signed, then negative values
3019 will be changed. */
0e3a99ae
AS
3020 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
3021 && produce_warns)
6312e84d
MLI
3022 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
3023 "may change the sign of the result",
3024 type, expr_type);
0e3a99ae 3025 }
422c3a54
MLI
3026
3027 /* Warn for integer types converted to real types if and only if
0e3a99ae
AS
3028 all the range of values of the integer type cannot be
3029 represented by the real type. */
6715192c 3030 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
3031 && TREE_CODE (type) == REAL_TYPE)
3032 {
58076e21
MLI
3033 /* Don't warn about char y = 0xff; float x = (int) y; */
3034 expr = get_unwidened (expr, 0);
3035 expr_type = TREE_TYPE (expr);
3036
7a37fa90 3037 if (!int_safely_convertible_to_real_p (expr_type, type))
49b0aa18 3038 give_warning = UNSAFE_OTHER;
0e3a99ae 3039 }
422c3a54
MLI
3040
3041 /* Warn for real types converted to smaller real types. */
6715192c 3042 else if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
3043 && TREE_CODE (type) == REAL_TYPE
3044 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 3045 give_warning = UNSAFE_REAL;
7a37fa90
MM
3046
3047 /* Check conversion between two complex types. */
3048 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3049 && TREE_CODE (type) == COMPLEX_TYPE)
3050 {
3051 /* Extract underlying types (i.e., type of real and imaginary
3052 parts) of expr_type and type. */
3053 tree from_type = TREE_TYPE (expr_type);
3054 tree to_type = TREE_TYPE (type);
3055
3056 /* Warn for real types converted to integer types. */
3057 if (TREE_CODE (from_type) == REAL_TYPE
3058 && TREE_CODE (to_type) == INTEGER_TYPE)
3059 give_warning = UNSAFE_REAL;
3060
3061 /* Warn for real types converted to smaller real types. */
3062 else if (TREE_CODE (from_type) == REAL_TYPE
3063 && TREE_CODE (to_type) == REAL_TYPE
3064 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
3065 give_warning = UNSAFE_REAL;
3066
3067 /* Check conversion for complex integer types. Here implementation
3068 is simpler than for real-domain integers because it does not
3069 involve sophisticated cases, such as bitmasks, casts, etc. */
3070 else if (TREE_CODE (from_type) == INTEGER_TYPE
3071 && TREE_CODE (to_type) == INTEGER_TYPE)
3072 {
3073 /* Warn for integer types converted to smaller integer types. */
3074 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
3075 give_warning = UNSAFE_OTHER;
3076
3077 /* Check for different signedness, see case for real-domain
3078 integers (above) for a more detailed comment. */
3079 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
3080 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
3081 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
3082 && produce_warns)
3083 warning_at (loc, OPT_Wsign_conversion,
3084 "conversion to %qT from %qT "
3085 "may change the sign of the result",
3086 type, expr_type);
3087 }
3088 else if (TREE_CODE (from_type) == INTEGER_TYPE
3089 && TREE_CODE (to_type) == REAL_TYPE
3090 && !int_safely_convertible_to_real_p (from_type, to_type))
3091 give_warning = UNSAFE_OTHER;
3092 }
3093
3094 /* Warn for complex types converted to real or integer types. */
3095 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3096 && TREE_CODE (type) != COMPLEX_TYPE)
3097 give_warning = UNSAFE_IMAGINARY;
0e3a99ae
AS
3098 }
3099
3100 return give_warning;
3101}
3102
3103/* Warns if the conversion of EXPR to TYPE may alter a value.
3104 This is a helper function for warnings_for_convert_and_check. */
3105
3106static void
68fca595 3107conversion_warning (location_t loc, tree type, tree expr)
0e3a99ae 3108{
0e3a99ae 3109 tree expr_type = TREE_TYPE (expr);
49b0aa18 3110 enum conversion_safety conversion_kind;
422c3a54 3111
49b0aa18 3112 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
0e3a99ae 3113 return;
422c3a54 3114
66f20604
MP
3115 /* This may happen, because for LHS op= RHS we preevaluate
3116 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3117 means we could no longer see the code of the EXPR. */
3118 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3119 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3120 if (TREE_CODE (expr) == SAVE_EXPR)
3121 expr = TREE_OPERAND (expr, 0);
3122
0e3a99ae
AS
3123 switch (TREE_CODE (expr))
3124 {
3125 case EQ_EXPR:
3126 case NE_EXPR:
3127 case LE_EXPR:
3128 case GE_EXPR:
3129 case LT_EXPR:
3130 case GT_EXPR:
3131 case TRUTH_ANDIF_EXPR:
3132 case TRUTH_ORIF_EXPR:
3133 case TRUTH_AND_EXPR:
3134 case TRUTH_OR_EXPR:
3135 case TRUTH_XOR_EXPR:
3136 case TRUTH_NOT_EXPR:
3137 /* Conversion from boolean to a signed:1 bit-field (which only
3138 can hold the values 0 and -1) doesn't lose information - but
3139 it does change the value. */
3140 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3141 warning_at (loc, OPT_Wconversion,
3142 "conversion to %qT from boolean expression", type);
3143 return;
3144
3145 case REAL_CST:
3146 case INTEGER_CST:
7a37fa90 3147 case COMPLEX_CST:
68fca595 3148 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
3149 if (conversion_kind == UNSAFE_REAL)
3150 warning_at (loc, OPT_Wfloat_conversion,
3151 "conversion to %qT alters %qT constant value",
3152 type, expr_type);
3153 else if (conversion_kind)
0e3a99ae
AS
3154 warning_at (loc, OPT_Wconversion,
3155 "conversion to %qT alters %qT constant value",
3156 type, expr_type);
3157 return;
3158
3159 case COND_EXPR:
3160 {
3f46d6a5
MLI
3161 /* In case of COND_EXPR, we do not care about the type of
3162 COND_EXPR, only about the conversion of each operand. */
3163 tree op1 = TREE_OPERAND (expr, 1);
3164 tree op2 = TREE_OPERAND (expr, 2);
3165
68fca595
MP
3166 conversion_warning (loc, type, op1);
3167 conversion_warning (loc, type, op2);
3f46d6a5 3168 return;
0e3a99ae
AS
3169 }
3170
3171 default: /* 'expr' is not a constant. */
68fca595 3172 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
3173 if (conversion_kind == UNSAFE_REAL)
3174 warning_at (loc, OPT_Wfloat_conversion,
3175 "conversion to %qT from %qT may alter its value",
3176 type, expr_type);
7a37fa90
MM
3177 else if (conversion_kind == UNSAFE_IMAGINARY)
3178 warning_at (loc, OPT_Wconversion,
3179 "conversion to %qT from %qT discards imaginary component",
3180 type, expr_type);
49b0aa18 3181 else if (conversion_kind)
0e3a99ae 3182 warning_at (loc, OPT_Wconversion,
6312e84d
MLI
3183 "conversion to %qT from %qT may alter its value",
3184 type, expr_type);
422c3a54
MLI
3185 }
3186}
3187
07231d4f
MLI
3188/* Produce warnings after a conversion. RESULT is the result of
3189 converting EXPR to TYPE. This is a helper function for
3190 convert_and_check and cp_convert_and_check. */
d74154d5 3191
07231d4f 3192void
68fca595
MP
3193warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3194 tree result)
d74154d5 3195{
68fca595 3196 loc = expansion_point_location_if_in_system_header (loc);
5a3c9cf2 3197
91c41804
RS
3198 if (TREE_CODE (expr) == INTEGER_CST
3199 && (TREE_CODE (type) == INTEGER_TYPE
3200 || TREE_CODE (type) == ENUMERAL_TYPE)
3201 && !int_fits_type_p (expr, type))
3202 {
422c3a54
MLI
3203 /* Do not diagnose overflow in a constant expression merely
3204 because a conversion overflowed. */
91c41804 3205 if (TREE_OVERFLOW (result))
d95787e6
RS
3206 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3207
91c41804 3208 if (TYPE_UNSIGNED (type))
422c3a54 3209 {
91c41804
RS
3210 /* This detects cases like converting -129 or 256 to
3211 unsigned char. */
3212 if (!int_fits_type_p (expr, c_common_signed_type (type)))
5a3c9cf2
PC
3213 warning_at (loc, OPT_Woverflow,
3214 "large integer implicitly truncated to unsigned type");
7060db96 3215 else
68fca595 3216 conversion_warning (loc, type, expr);
91c41804 3217 }
b8698a0f 3218 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
68fca595 3219 warning_at (loc, OPT_Woverflow,
f73fe417
MLI
3220 "overflow in implicit constant conversion");
3221 /* No warning for converting 0x80000000 to int. */
3222 else if (pedantic
3223 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3224 || TYPE_PRECISION (TREE_TYPE (expr))
3225 != TYPE_PRECISION (type)))
5a3c9cf2
PC
3226 warning_at (loc, OPT_Woverflow,
3227 "overflow in implicit constant conversion");
f73fe417 3228
7060db96 3229 else
68fca595 3230 conversion_warning (loc, type, expr);
d74154d5 3231 }
ab22c1fa
CF
3232 else if ((TREE_CODE (result) == INTEGER_CST
3233 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
5a3c9cf2
PC
3234 warning_at (loc, OPT_Woverflow,
3235 "overflow in implicit constant conversion");
7060db96 3236 else
68fca595 3237 conversion_warning (loc, type, expr);
07231d4f
MLI
3238}
3239
3240
3241/* Convert EXPR to TYPE, warning about conversion problems with constants.
3242 Invoke this function on every expression that is converted implicitly,
3243 i.e. because of language rules and not because of an explicit cast. */
3244
3245tree
68fca595 3246convert_and_check (location_t loc, tree type, tree expr)
07231d4f
MLI
3247{
3248 tree result;
8ce94e44
JM
3249 tree expr_for_warning;
3250
3251 /* Convert from a value with possible excess precision rather than
3252 via the semantic type, but do not warn about values not fitting
3253 exactly in the semantic type. */
3254 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3255 {
3256 tree orig_type = TREE_TYPE (expr);
3257 expr = TREE_OPERAND (expr, 0);
3258 expr_for_warning = convert (orig_type, expr);
3259 if (orig_type == type)
3260 return expr_for_warning;
3261 }
3262 else
3263 expr_for_warning = expr;
07231d4f
MLI
3264
3265 if (TREE_TYPE (expr) == type)
3266 return expr;
b8698a0f 3267
07231d4f
MLI
3268 result = convert (type, expr);
3269
7d882b83
ILT
3270 if (c_inhibit_evaluation_warnings == 0
3271 && !TREE_OVERFLOW_P (expr)
3272 && result != error_mark_node)
68fca595 3273 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
07231d4f 3274
91c41804 3275 return result;
96571883
BK
3276}
3277\f
235cfbc4
BS
3278/* A node in a list that describes references to variables (EXPR), which are
3279 either read accesses if WRITER is zero, or write accesses, in which case
3280 WRITER is the parent of EXPR. */
3281struct tlist
3282{
3283 struct tlist *next;
3284 tree expr, writer;
3285};
3286
3287/* Used to implement a cache the results of a call to verify_tree. We only
3288 use this for SAVE_EXPRs. */
3289struct tlist_cache
3290{
3291 struct tlist_cache *next;
3292 struct tlist *cache_before_sp;
3293 struct tlist *cache_after_sp;
3294 tree expr;
2683ed8d
BS
3295};
3296
235cfbc4
BS
3297/* Obstack to use when allocating tlist structures, and corresponding
3298 firstobj. */
3299static struct obstack tlist_obstack;
3300static char *tlist_firstobj = 0;
3301
3302/* Keep track of the identifiers we've warned about, so we can avoid duplicate
3303 warnings. */
3304static struct tlist *warned_ids;
3305/* SAVE_EXPRs need special treatment. We process them only once and then
3306 cache the results. */
3307static struct tlist_cache *save_expr_cache;
3308
35b1a6fa
AJ
3309static void add_tlist (struct tlist **, struct tlist *, tree, int);
3310static void merge_tlist (struct tlist **, struct tlist *, int);
3311static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3312static int warning_candidate_p (tree);
1e4ae551 3313static bool candidate_equal_p (const_tree, const_tree);
35b1a6fa
AJ
3314static void warn_for_collisions (struct tlist *);
3315static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3316static struct tlist *new_tlist (struct tlist *, tree, tree);
2683ed8d 3317
235cfbc4
BS
3318/* Create a new struct tlist and fill in its fields. */
3319static struct tlist *
35b1a6fa 3320new_tlist (struct tlist *next, tree t, tree writer)
235cfbc4
BS
3321{
3322 struct tlist *l;
5d038c4c 3323 l = XOBNEW (&tlist_obstack, struct tlist);
235cfbc4
BS
3324 l->next = next;
3325 l->expr = t;
3326 l->writer = writer;
3327 return l;
3328}
3329
3330/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3331 is nonnull, we ignore any node we find which has a writer equal to it. */
3332
3333static void
35b1a6fa 3334add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
235cfbc4
BS
3335{
3336 while (add)
3337 {
3338 struct tlist *next = add->next;
3f75a254 3339 if (!copy)
235cfbc4 3340 add->next = *to;
1e4ae551 3341 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
235cfbc4
BS
3342 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3343 add = next;
3344 }
3345}
3346
3347/* Merge the nodes of ADD into TO. This merging process is done so that for
3348 each variable that already exists in TO, no new node is added; however if
3349 there is a write access recorded in ADD, and an occurrence on TO is only
3350 a read access, then the occurrence in TO will be modified to record the
3351 write. */
2683ed8d
BS
3352
3353static void
35b1a6fa 3354merge_tlist (struct tlist **to, struct tlist *add, int copy)
235cfbc4
BS
3355{
3356 struct tlist **end = to;
3357
3358 while (*end)
3359 end = &(*end)->next;
3360
3361 while (add)
3362 {
3363 int found = 0;
3364 struct tlist *tmp2;
3365 struct tlist *next = add->next;
3366
3367 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1e4ae551 3368 if (candidate_equal_p (tmp2->expr, add->expr))
235cfbc4
BS
3369 {
3370 found = 1;
3f75a254 3371 if (!tmp2->writer)
235cfbc4
BS
3372 tmp2->writer = add->writer;
3373 }
3f75a254 3374 if (!found)
235cfbc4 3375 {
c2bf53a1 3376 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
235cfbc4
BS
3377 end = &(*end)->next;
3378 *end = 0;
3379 }
3380 add = next;
3381 }
3382}
3383
3384/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3385 references in list LIST conflict with it, excluding reads if ONLY writers
3386 is nonzero. */
3387
3388static void
35b1a6fa
AJ
3389warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3390 int only_writes)
235cfbc4
BS
3391{
3392 struct tlist *tmp;
3393
3394 /* Avoid duplicate warnings. */
3395 for (tmp = warned_ids; tmp; tmp = tmp->next)
1e4ae551 3396 if (candidate_equal_p (tmp->expr, written))
235cfbc4
BS
3397 return;
3398
3399 while (list)
3400 {
1e4ae551
MLI
3401 if (candidate_equal_p (list->expr, written)
3402 && !candidate_equal_p (list->writer, writer)
3403 && (!only_writes || list->writer))
235cfbc4
BS
3404 {
3405 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
8400e75e 3406 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
ca085fd7
MLI
3407 OPT_Wsequence_point, "operation on %qE may be undefined",
3408 list->expr);
235cfbc4
BS
3409 }
3410 list = list->next;
3411 }
3412}
3413
3414/* Given a list LIST of references to variables, find whether any of these
3415 can cause conflicts due to missing sequence points. */
3416
3417static void
35b1a6fa 3418warn_for_collisions (struct tlist *list)
235cfbc4
BS
3419{
3420 struct tlist *tmp;
35b1a6fa 3421
235cfbc4
BS
3422 for (tmp = list; tmp; tmp = tmp->next)
3423 {
3424 if (tmp->writer)
3425 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3426 }
3427}
3428
684d9f3b 3429/* Return nonzero if X is a tree that can be verified by the sequence point
235cfbc4
BS
3430 warnings. */
3431static int
35b1a6fa 3432warning_candidate_p (tree x)
2683ed8d 3433{
07078664
JJ
3434 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3435 return 0;
3436
92e948a8
NF
3437 if (TREE_CODE (x) == BLOCK)
3438 return 0;
3439
07078664 3440 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1e4ae551 3441 (lvalue_p) crash on TRY/CATCH. */
07078664
JJ
3442 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3443 return 0;
3444
3445 if (!lvalue_p (x))
3446 return 0;
3447
3448 /* No point to track non-const calls, they will never satisfy
3449 operand_equal_p. */
3450 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3451 return 0;
3452
3453 if (TREE_CODE (x) == STRING_CST)
3454 return 0;
3455
3456 return 1;
1e4ae551
MLI
3457}
3458
3459/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3460static bool
3461candidate_equal_p (const_tree x, const_tree y)
3462{
3463 return (x == y) || (x && y && operand_equal_p (x, y, 0));
235cfbc4 3464}
2683ed8d 3465
235cfbc4
BS
3466/* Walk the tree X, and record accesses to variables. If X is written by the
3467 parent tree, WRITER is the parent.
3468 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3469 expression or its only operand forces a sequence point, then everything up
3470 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3471 in PNO_SP.
3472 Once we return, we will have emitted warnings if any subexpression before
3473 such a sequence point could be undefined. On a higher level, however, the
3474 sequence point may not be relevant, and we'll merge the two lists.
3475
3476 Example: (b++, a) + b;
3477 The call that processes the COMPOUND_EXPR will store the increment of B
3478 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3479 processes the PLUS_EXPR will need to merge the two lists so that
3480 eventually, all accesses end up on the same list (and we'll warn about the
3481 unordered subexpressions b++ and b.
3482
3483 A note on merging. If we modify the former example so that our expression
3484 becomes
3485 (b++, b) + a
3486 care must be taken not simply to add all three expressions into the final
3487 PNO_SP list. The function merge_tlist takes care of that by merging the
3488 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3489 way, so that no more than one access to B is recorded. */
2683ed8d 3490
235cfbc4 3491static void
35b1a6fa
AJ
3492verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3493 tree writer)
235cfbc4
BS
3494{
3495 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3496 enum tree_code code;
6615c446 3497 enum tree_code_class cl;
2683ed8d 3498
f9e1917e
JM
3499 /* X may be NULL if it is the operand of an empty statement expression
3500 ({ }). */
3501 if (x == NULL)
3502 return;
3503
235cfbc4
BS
3504 restart:
3505 code = TREE_CODE (x);
e3a64162 3506 cl = TREE_CODE_CLASS (code);
2683ed8d 3507
235cfbc4 3508 if (warning_candidate_p (x))
1e4ae551 3509 *pno_sp = new_tlist (*pno_sp, x, writer);
235cfbc4
BS
3510
3511 switch (code)
3512 {
52a84e42 3513 case CONSTRUCTOR:
f7716d57 3514 case SIZEOF_EXPR:
52a84e42
BS
3515 return;
3516
235cfbc4
BS
3517 case COMPOUND_EXPR:
3518 case TRUTH_ANDIF_EXPR:
3519 case TRUTH_ORIF_EXPR:
3520 tmp_before = tmp_nosp = tmp_list3 = 0;
3521 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3522 warn_for_collisions (tmp_nosp);
3523 merge_tlist (pbefore_sp, tmp_before, 0);
3524 merge_tlist (pbefore_sp, tmp_nosp, 0);
3525 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3526 merge_tlist (pbefore_sp, tmp_list3, 0);
3527 return;
3528
3529 case COND_EXPR:
3530 tmp_before = tmp_list2 = 0;
3531 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3532 warn_for_collisions (tmp_list2);
3533 merge_tlist (pbefore_sp, tmp_before, 0);
c2bf53a1 3534 merge_tlist (pbefore_sp, tmp_list2, 0);
235cfbc4
BS
3535
3536 tmp_list3 = tmp_nosp = 0;
3537 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3538 warn_for_collisions (tmp_nosp);
3539 merge_tlist (pbefore_sp, tmp_list3, 0);
3540
3541 tmp_list3 = tmp_list2 = 0;
3542 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3543 warn_for_collisions (tmp_list2);
3544 merge_tlist (pbefore_sp, tmp_list3, 0);
3545 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3546 two first, to avoid warning for (a ? b++ : b++). */
3547 merge_tlist (&tmp_nosp, tmp_list2, 0);
3548 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3549 return;
3550
2683ed8d
BS
3551 case PREDECREMENT_EXPR:
3552 case PREINCREMENT_EXPR:
3553 case POSTDECREMENT_EXPR:
3554 case POSTINCREMENT_EXPR:
235cfbc4
BS
3555 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3556 return;
3557
3558 case MODIFY_EXPR:
3559 tmp_before = tmp_nosp = tmp_list3 = 0;
3560 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3561 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3562 /* Expressions inside the LHS are not ordered wrt. the sequence points
3563 in the RHS. Example:
3564 *a = (a++, 2)
3565 Despite the fact that the modification of "a" is in the before_sp
3566 list (tmp_before), it conflicts with the use of "a" in the LHS.
3567 We can handle this by adding the contents of tmp_list3
3568 to those of tmp_before, and redoing the collision warnings for that
3569 list. */
3570 add_tlist (&tmp_before, tmp_list3, x, 1);
3571 warn_for_collisions (tmp_before);
3572 /* Exclude the LHS itself here; we first have to merge it into the
3573 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3574 didn't exclude the LHS, we'd get it twice, once as a read and once
3575 as a write. */
3576 add_tlist (pno_sp, tmp_list3, x, 0);
3577 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3578
3579 merge_tlist (pbefore_sp, tmp_before, 0);
3580 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3581 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3582 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3583 return;
2683ed8d
BS
3584
3585 case CALL_EXPR:
235cfbc4
BS
3586 /* We need to warn about conflicts among arguments and conflicts between
3587 args and the function address. Side effects of the function address,
3588 however, are not ordered by the sequence point of the call. */
5039610b
SL
3589 {
3590 call_expr_arg_iterator iter;
3591 tree arg;
b8698a0f 3592 tmp_before = tmp_nosp = 0;
5039610b
SL
3593 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3594 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3595 {
3596 tmp_list2 = tmp_list3 = 0;
3597 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3598 merge_tlist (&tmp_list3, tmp_list2, 0);
3599 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3600 }
3601 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3602 warn_for_collisions (tmp_before);
3603 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3604 return;
3605 }
2683ed8d
BS
3606
3607 case TREE_LIST:
3608 /* Scan all the list, e.g. indices of multi dimensional array. */
3609 while (x)
3610 {
235cfbc4
BS
3611 tmp_before = tmp_nosp = 0;
3612 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3613 merge_tlist (&tmp_nosp, tmp_before, 0);
3614 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2683ed8d
BS
3615 x = TREE_CHAIN (x);
3616 }
235cfbc4 3617 return;
2683ed8d 3618
235cfbc4
BS
3619 case SAVE_EXPR:
3620 {
3621 struct tlist_cache *t;
3622 for (t = save_expr_cache; t; t = t->next)
1e4ae551 3623 if (candidate_equal_p (t->expr, x))
235cfbc4 3624 break;
2683ed8d 3625
3f75a254 3626 if (!t)
2683ed8d 3627 {
5d038c4c 3628 t = XOBNEW (&tlist_obstack, struct tlist_cache);
235cfbc4
BS
3629 t->next = save_expr_cache;
3630 t->expr = x;
3631 save_expr_cache = t;
3632
3633 tmp_before = tmp_nosp = 0;
3634 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3635 warn_for_collisions (tmp_nosp);
3636
3637 tmp_list3 = 0;
c2bf53a1 3638 merge_tlist (&tmp_list3, tmp_nosp, 0);
235cfbc4
BS
3639 t->cache_before_sp = tmp_before;
3640 t->cache_after_sp = tmp_list3;
2683ed8d 3641 }
235cfbc4
BS
3642 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3643 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3644 return;
3645 }
2683ed8d 3646
528c22f4
MLI
3647 case ADDR_EXPR:
3648 x = TREE_OPERAND (x, 0);
3649 if (DECL_P (x))
3650 return;
3651 writer = 0;
3652 goto restart;
3653
6615c446
JO
3654 default:
3655 /* For other expressions, simply recurse on their operands.
c22cacf3 3656 Manual tail recursion for unary expressions.
6615c446
JO
3657 Other non-expressions need not be processed. */
3658 if (cl == tcc_unary)
3659 {
6615c446
JO
3660 x = TREE_OPERAND (x, 0);
3661 writer = 0;
3662 goto restart;
3663 }
3664 else if (IS_EXPR_CODE_CLASS (cl))
3665 {
3666 int lp;
5039610b 3667 int max = TREE_OPERAND_LENGTH (x);
6615c446
JO
3668 for (lp = 0; lp < max; lp++)
3669 {
3670 tmp_before = tmp_nosp = 0;
3671 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3672 merge_tlist (&tmp_nosp, tmp_before, 0);
3673 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3674 }
3675 }
3676 return;
2683ed8d 3677 }
2683ed8d
BS
3678}
3679
8d9afc4e 3680/* Try to warn for undefined behavior in EXPR due to missing sequence
2683ed8d
BS
3681 points. */
3682
24e47c76 3683DEBUG_FUNCTION void
35b1a6fa 3684verify_sequence_points (tree expr)
2683ed8d 3685{
235cfbc4 3686 struct tlist *before_sp = 0, *after_sp = 0;
2683ed8d 3687
235cfbc4
BS
3688 warned_ids = 0;
3689 save_expr_cache = 0;
3690 if (tlist_firstobj == 0)
2683ed8d 3691 {
235cfbc4 3692 gcc_obstack_init (&tlist_obstack);
28dab132 3693 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2683ed8d
BS
3694 }
3695
235cfbc4
BS
3696 verify_tree (expr, &before_sp, &after_sp, 0);
3697 warn_for_collisions (after_sp);
3698 obstack_free (&tlist_obstack, tlist_firstobj);
2683ed8d 3699}
b30f223b
RS
3700\f
3701/* Validate the expression after `case' and apply default promotions. */
3702
a6c0a76c 3703static tree
62e4eb35 3704check_case_value (location_t loc, tree value)
b30f223b
RS
3705{
3706 if (value == NULL_TREE)
3707 return value;
3708
522ddfa2
JM
3709 if (TREE_CODE (value) == INTEGER_CST)
3710 /* Promote char or short to int. */
3711 value = perform_integral_promotions (value);
3712 else if (value != error_mark_node)
b30f223b 3713 {
62e4eb35 3714 error_at (loc, "case label does not reduce to an integer constant");
b30f223b
RS
3715 value = error_mark_node;
3716 }
b30f223b 3717
bc690db1
RS
3718 constant_expression_warning (value);
3719
b30f223b
RS
3720 return value;
3721}
3722\f
a6c0a76c 3723/* See if the case values LOW and HIGH are in the range of the original
89dbed81 3724 type (i.e. before the default conversion to int) of the switch testing
a6c0a76c
SB
3725 expression.
3726 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2a7e31df 3727 the type before promoting it. CASE_LOW_P is a pointer to the lower
a6c0a76c
SB
3728 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3729 if the case is not a case range.
3730 The caller has to make sure that we are not called with NULL for
b155cfd9
MP
3731 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3732 was a case value that doesn't fit into the range of the ORIG_TYPE.
0fa2e4df 3733 Returns true if the case label is in range of ORIG_TYPE (saturated or
a6c0a76c
SB
3734 untouched) or false if the label is out of range. */
3735
3736static bool
9d548dfb 3737check_case_bounds (location_t loc, tree type, tree orig_type,
b155cfd9
MP
3738 tree *case_low_p, tree *case_high_p,
3739 bool *outside_range_p)
a6c0a76c
SB
3740{
3741 tree min_value, max_value;
3742 tree case_low = *case_low_p;
3743 tree case_high = case_high_p ? *case_high_p : case_low;
3744
3745 /* If there was a problem with the original type, do nothing. */
3746 if (orig_type == error_mark_node)
3747 return true;
3748
3749 min_value = TYPE_MIN_VALUE (orig_type);
3750 max_value = TYPE_MAX_VALUE (orig_type);
3751
3752 /* Case label is less than minimum for type. */
3753 if (tree_int_cst_compare (case_low, min_value) < 0
3754 && tree_int_cst_compare (case_high, min_value) < 0)
3755 {
9d548dfb
MP
3756 warning_at (loc, 0, "case label value is less than minimum value "
3757 "for type");
b155cfd9 3758 *outside_range_p = true;
a6c0a76c
SB
3759 return false;
3760 }
9f63daea 3761
a6c0a76c
SB
3762 /* Case value is greater than maximum for type. */
3763 if (tree_int_cst_compare (case_low, max_value) > 0
3764 && tree_int_cst_compare (case_high, max_value) > 0)
3765 {
9d548dfb 3766 warning_at (loc, 0, "case label value exceeds maximum value for type");
b155cfd9 3767 *outside_range_p = true;
a6c0a76c
SB
3768 return false;
3769 }
3770
3771 /* Saturate lower case label value to minimum. */
3772 if (tree_int_cst_compare (case_high, min_value) >= 0
3773 && tree_int_cst_compare (case_low, min_value) < 0)
3774 {
9d548dfb
MP
3775 warning_at (loc, 0, "lower value in case label range"
3776 " less than minimum value for type");
b155cfd9 3777 *outside_range_p = true;
a6c0a76c
SB
3778 case_low = min_value;
3779 }
9f63daea 3780
a6c0a76c
SB
3781 /* Saturate upper case label value to maximum. */
3782 if (tree_int_cst_compare (case_low, max_value) <= 0
3783 && tree_int_cst_compare (case_high, max_value) > 0)
3784 {
9d548dfb
MP
3785 warning_at (loc, 0, "upper value in case label range"
3786 " exceeds maximum value for type");
b155cfd9 3787 *outside_range_p = true;
a6c0a76c
SB
3788 case_high = max_value;
3789 }
3790
3791 if (*case_low_p != case_low)
3792 *case_low_p = convert (type, case_low);
3793 if (case_high_p && *case_high_p != case_high)
3794 *case_high_p = convert (type, case_high);
3795
3796 return true;
3797}
3798\f
b30f223b
RS
3799/* Return an integer type with BITS bits of precision,
3800 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3801
3802tree
35b1a6fa 3803c_common_type_for_size (unsigned int bits, int unsignedp)
b30f223b 3804{
78a7c317
DD
3805 int i;
3806
a311b52c
JM
3807 if (bits == TYPE_PRECISION (integer_type_node))
3808 return unsignedp ? unsigned_type_node : integer_type_node;
3809
3fc7e390 3810 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
3811 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3812
3fc7e390 3813 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
3814 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3815
3fc7e390 3816 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
3817 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3818
3fc7e390 3819 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
3820 return (unsignedp ? long_long_unsigned_type_node
3821 : long_long_integer_type_node);
3822
78a7c317
DD
3823 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3824 if (int_n_enabled_p[i]
3825 && bits == int_n_data[i].bitsize)
3826 return (unsignedp ? int_n_trees[i].unsigned_type
3827 : int_n_trees[i].signed_type);
a6766312 3828
835f9b4d
GRK
3829 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3830 return (unsignedp ? widest_unsigned_literal_type_node
3831 : widest_integer_literal_type_node);
3832
3fc7e390
RS
3833 if (bits <= TYPE_PRECISION (intQI_type_node))
3834 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3835
3836 if (bits <= TYPE_PRECISION (intHI_type_node))
3837 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3838
3839 if (bits <= TYPE_PRECISION (intSI_type_node))
3840 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3841
3842 if (bits <= TYPE_PRECISION (intDI_type_node))
3843 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3844
b30f223b
RS
3845 return 0;
3846}
3847
ab22c1fa
CF
3848/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3849 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3850 and saturating if SATP is nonzero, otherwise not saturating. */
3851
3852tree
3853c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3854 int unsignedp, int satp)
3855{
ef4bddc2 3856 machine_mode mode;
ab22c1fa
CF
3857 if (ibit == 0)
3858 mode = unsignedp ? UQQmode : QQmode;
3859 else
3860 mode = unsignedp ? UHAmode : HAmode;
3861
3862 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3863 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3864 break;
3865
3866 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3867 {
3868 sorry ("GCC cannot support operators with integer types and "
3869 "fixed-point types that have too many integral and "
3870 "fractional bits together");
3871 return 0;
3872 }
3873
3874 return c_common_type_for_mode (mode, satp);
3875}
3876
d1d3865f
ZW
3877/* Used for communication between c_common_type_for_mode and
3878 c_register_builtin_type. */
793c625f 3879tree registered_builtin_types;
d1d3865f 3880
b30f223b
RS
3881/* Return a data type that has machine mode MODE.
3882 If the mode is an integer,
ab22c1fa
CF
3883 then UNSIGNEDP selects between signed and unsigned types.
3884 If the mode is a fixed-point mode,
3885 then UNSIGNEDP selects between saturating and nonsaturating types. */
b30f223b
RS
3886
3887tree
ef4bddc2 3888c_common_type_for_mode (machine_mode mode, int unsignedp)
b30f223b 3889{
d1d3865f 3890 tree t;
78a7c317 3891 int i;
d1d3865f 3892
a311b52c
JM
3893 if (mode == TYPE_MODE (integer_type_node))
3894 return unsignedp ? unsigned_type_node : integer_type_node;
3895
b30f223b
RS
3896 if (mode == TYPE_MODE (signed_char_type_node))
3897 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3898
3899 if (mode == TYPE_MODE (short_integer_type_node))
3900 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3901
b30f223b
RS
3902 if (mode == TYPE_MODE (long_integer_type_node))
3903 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3904
3905 if (mode == TYPE_MODE (long_long_integer_type_node))
3906 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3907
78a7c317
DD
3908 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3909 if (int_n_enabled_p[i]
3910 && mode == int_n_data[i].m)
3911 return (unsignedp ? int_n_trees[i].unsigned_type
3912 : int_n_trees[i].signed_type);
a6766312 3913
835f9b4d 3914 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 3915 return unsignedp ? widest_unsigned_literal_type_node
6de9cd9a 3916 : widest_integer_literal_type_node;
835f9b4d 3917
0afeef64 3918 if (mode == QImode)
3fc7e390
RS
3919 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3920
0afeef64 3921 if (mode == HImode)
3fc7e390
RS
3922 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3923
0afeef64 3924 if (mode == SImode)
3fc7e390
RS
3925 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3926
0afeef64 3927 if (mode == DImode)
3fc7e390
RS
3928 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3929
21a9616b 3930#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
3931 if (mode == TYPE_MODE (intTI_type_node))
3932 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 3933#endif
a6d7e156 3934
b30f223b
RS
3935 if (mode == TYPE_MODE (float_type_node))
3936 return float_type_node;
3937
3938 if (mode == TYPE_MODE (double_type_node))
3939 return double_type_node;
3940
3941 if (mode == TYPE_MODE (long_double_type_node))
3942 return long_double_type_node;
3943
ff42324e
NS
3944 if (mode == TYPE_MODE (void_type_node))
3945 return void_type_node;
9f63daea 3946
b30f223b 3947 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
19b3ffbc
DD
3948 return (unsignedp
3949 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3950 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b
RS
3951
3952 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
19b3ffbc
DD
3953 return (unsignedp
3954 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3955 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b 3956
7e7e470f
RH
3957 if (COMPLEX_MODE_P (mode))
3958 {
ef4bddc2 3959 machine_mode inner_mode;
7e7e470f
RH
3960 tree inner_type;
3961
3962 if (mode == TYPE_MODE (complex_float_type_node))
3963 return complex_float_type_node;
3964 if (mode == TYPE_MODE (complex_double_type_node))
3965 return complex_double_type_node;
3966 if (mode == TYPE_MODE (complex_long_double_type_node))
3967 return complex_long_double_type_node;
3968
3969 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3970 return complex_integer_type_node;
3971
3972 inner_mode = GET_MODE_INNER (mode);
3973 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3974 if (inner_type != NULL_TREE)
3975 return build_complex_type (inner_type);
3976 }
3977 else if (VECTOR_MODE_P (mode))
4a5eab38 3978 {
ef4bddc2 3979 machine_mode inner_mode = GET_MODE_INNER (mode);
4a5eab38
PB
3980 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3981 if (inner_type != NULL_TREE)
3982 return build_vector_type_for_mode (inner_type, mode);
0afeef64 3983 }
4061f623 3984
9a8ce21f
JG
3985 if (mode == TYPE_MODE (dfloat32_type_node))
3986 return dfloat32_type_node;
3987 if (mode == TYPE_MODE (dfloat64_type_node))
3988 return dfloat64_type_node;
3989 if (mode == TYPE_MODE (dfloat128_type_node))
3990 return dfloat128_type_node;
3991
ab22c1fa
CF
3992 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3993 {
3994 if (mode == TYPE_MODE (short_fract_type_node))
3995 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3996 if (mode == TYPE_MODE (fract_type_node))
3997 return unsignedp ? sat_fract_type_node : fract_type_node;
3998 if (mode == TYPE_MODE (long_fract_type_node))
3999 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
4000 if (mode == TYPE_MODE (long_long_fract_type_node))
4001 return unsignedp ? sat_long_long_fract_type_node
4002 : long_long_fract_type_node;
4003
4004 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
4005 return unsignedp ? sat_unsigned_short_fract_type_node
4006 : unsigned_short_fract_type_node;
4007 if (mode == TYPE_MODE (unsigned_fract_type_node))
4008 return unsignedp ? sat_unsigned_fract_type_node
4009 : unsigned_fract_type_node;
4010 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
4011 return unsignedp ? sat_unsigned_long_fract_type_node
4012 : unsigned_long_fract_type_node;
4013 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
4014 return unsignedp ? sat_unsigned_long_long_fract_type_node
4015 : unsigned_long_long_fract_type_node;
4016
4017 if (mode == TYPE_MODE (short_accum_type_node))
4018 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
4019 if (mode == TYPE_MODE (accum_type_node))
4020 return unsignedp ? sat_accum_type_node : accum_type_node;
4021 if (mode == TYPE_MODE (long_accum_type_node))
4022 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
4023 if (mode == TYPE_MODE (long_long_accum_type_node))
4024 return unsignedp ? sat_long_long_accum_type_node
4025 : long_long_accum_type_node;
4026
4027 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
4028 return unsignedp ? sat_unsigned_short_accum_type_node
4029 : unsigned_short_accum_type_node;
4030 if (mode == TYPE_MODE (unsigned_accum_type_node))
4031 return unsignedp ? sat_unsigned_accum_type_node
4032 : unsigned_accum_type_node;
4033 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
4034 return unsignedp ? sat_unsigned_long_accum_type_node
4035 : unsigned_long_accum_type_node;
4036 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
4037 return unsignedp ? sat_unsigned_long_long_accum_type_node
4038 : unsigned_long_long_accum_type_node;
4039
4040 if (mode == QQmode)
4041 return unsignedp ? sat_qq_type_node : qq_type_node;
4042 if (mode == HQmode)
4043 return unsignedp ? sat_hq_type_node : hq_type_node;
4044 if (mode == SQmode)
4045 return unsignedp ? sat_sq_type_node : sq_type_node;
4046 if (mode == DQmode)
4047 return unsignedp ? sat_dq_type_node : dq_type_node;
4048 if (mode == TQmode)
4049 return unsignedp ? sat_tq_type_node : tq_type_node;
4050
4051 if (mode == UQQmode)
4052 return unsignedp ? sat_uqq_type_node : uqq_type_node;
4053 if (mode == UHQmode)
4054 return unsignedp ? sat_uhq_type_node : uhq_type_node;
4055 if (mode == USQmode)
4056 return unsignedp ? sat_usq_type_node : usq_type_node;
4057 if (mode == UDQmode)
4058 return unsignedp ? sat_udq_type_node : udq_type_node;
4059 if (mode == UTQmode)
4060 return unsignedp ? sat_utq_type_node : utq_type_node;
4061
4062 if (mode == HAmode)
4063 return unsignedp ? sat_ha_type_node : ha_type_node;
4064 if (mode == SAmode)
4065 return unsignedp ? sat_sa_type_node : sa_type_node;
4066 if (mode == DAmode)
4067 return unsignedp ? sat_da_type_node : da_type_node;
4068 if (mode == TAmode)
4069 return unsignedp ? sat_ta_type_node : ta_type_node;
4070
4071 if (mode == UHAmode)
4072 return unsignedp ? sat_uha_type_node : uha_type_node;
4073 if (mode == USAmode)
4074 return unsignedp ? sat_usa_type_node : usa_type_node;
4075 if (mode == UDAmode)
4076 return unsignedp ? sat_uda_type_node : uda_type_node;
4077 if (mode == UTAmode)
4078 return unsignedp ? sat_uta_type_node : uta_type_node;
4079 }
4080
d1d3865f 4081 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
7a421706
MS
4082 if (TYPE_MODE (TREE_VALUE (t)) == mode
4083 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
d1d3865f
ZW
4084 return TREE_VALUE (t);
4085
b30f223b
RS
4086 return 0;
4087}
693a6128 4088
12753674
RE
4089tree
4090c_common_unsigned_type (tree type)
4091{
4092 return c_common_signed_or_unsigned_type (1, type);
4093}
4094
693a6128
GRK
4095/* Return a signed type the same as TYPE in other respects. */
4096
4097tree
35b1a6fa 4098c_common_signed_type (tree type)
693a6128 4099{
ceef8ce4 4100 return c_common_signed_or_unsigned_type (0, type);
693a6128
GRK
4101}
4102
4103/* Return a type the same as TYPE except unsigned or
4104 signed according to UNSIGNEDP. */
4105
4106tree
35b1a6fa 4107c_common_signed_or_unsigned_type (int unsignedp, tree type)
693a6128 4108{
c74a03d2 4109 tree type1;
78a7c317 4110 int i;
693a6128 4111
c74a03d2
RAE
4112 /* This block of code emulates the behavior of the old
4113 c_common_unsigned_type. In particular, it returns
4114 long_unsigned_type_node if passed a long, even when a int would
4115 have the same size. This is necessary for warnings to work
4116 correctly in archs where sizeof(int) == sizeof(long) */
4117
4118 type1 = TYPE_MAIN_VARIANT (type);
4119 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4120 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4121 if (type1 == integer_type_node || type1 == unsigned_type_node)
4122 return unsignedp ? unsigned_type_node : integer_type_node;
4123 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4124 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4125 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4126 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4127 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4128 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
78a7c317
DD
4129
4130 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4131 if (int_n_enabled_p[i]
4132 && (type1 == int_n_trees[i].unsigned_type
4133 || type1 == int_n_trees[i].signed_type))
4134 return (unsignedp ? int_n_trees[i].unsigned_type
4135 : int_n_trees[i].signed_type);
4136
c74a03d2
RAE
4137 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4138 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4139#if HOST_BITS_PER_WIDE_INT >= 64
4140 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4141 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4142#endif
4143 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4144 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4145 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4146 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4147 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4148 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4149 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4150 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4151
70d3fcab
AH
4152#define C_COMMON_FIXED_TYPES(NAME) \
4153 if (type1 == short_ ## NAME ## _type_node \
4154 || type1 == unsigned_short_ ## NAME ## _type_node) \
4155 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4156 : short_ ## NAME ## _type_node; \
4157 if (type1 == NAME ## _type_node \
4158 || type1 == unsigned_ ## NAME ## _type_node) \
4159 return unsignedp ? unsigned_ ## NAME ## _type_node \
4160 : NAME ## _type_node; \
4161 if (type1 == long_ ## NAME ## _type_node \
4162 || type1 == unsigned_long_ ## NAME ## _type_node) \
4163 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4164 : long_ ## NAME ## _type_node; \
4165 if (type1 == long_long_ ## NAME ## _type_node \
4166 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4167 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4168 : long_long_ ## NAME ## _type_node;
4169
4170#define C_COMMON_FIXED_MODE_TYPES(NAME) \
4171 if (type1 == NAME ## _type_node \
4172 || type1 == u ## NAME ## _type_node) \
4173 return unsignedp ? u ## NAME ## _type_node \
4174 : NAME ## _type_node;
4175
4176#define C_COMMON_FIXED_TYPES_SAT(NAME) \
4177 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4178 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4179 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4180 : sat_ ## short_ ## NAME ## _type_node; \
4181 if (type1 == sat_ ## NAME ## _type_node \
4182 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4183 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4184 : sat_ ## NAME ## _type_node; \
4185 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4186 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4187 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4188 : sat_ ## long_ ## NAME ## _type_node; \
4189 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4190 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4191 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4192 : sat_ ## long_long_ ## NAME ## _type_node;
4193
4194#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4195 if (type1 == sat_ ## NAME ## _type_node \
4196 || type1 == sat_ ## u ## NAME ## _type_node) \
4197 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4198 : sat_ ## NAME ## _type_node;
4199
4200 C_COMMON_FIXED_TYPES (fract);
4201 C_COMMON_FIXED_TYPES_SAT (fract);
4202 C_COMMON_FIXED_TYPES (accum);
4203 C_COMMON_FIXED_TYPES_SAT (accum);
4204
4205 C_COMMON_FIXED_MODE_TYPES (qq);
4206 C_COMMON_FIXED_MODE_TYPES (hq);
4207 C_COMMON_FIXED_MODE_TYPES (sq);
4208 C_COMMON_FIXED_MODE_TYPES (dq);
4209 C_COMMON_FIXED_MODE_TYPES (tq);
4210 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4211 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4212 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4213 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4214 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4215 C_COMMON_FIXED_MODE_TYPES (ha);
4216 C_COMMON_FIXED_MODE_TYPES (sa);
4217 C_COMMON_FIXED_MODE_TYPES (da);
4218 C_COMMON_FIXED_MODE_TYPES (ta);
4219 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4220 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4221 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4222 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
ab22c1fa 4223
bc15d0ef
JM
4224 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4225 the precision; they have precision set to match their range, but
4226 may use a wider mode to match an ABI. If we change modes, we may
4227 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4228 the precision as well, so as to yield correct results for
4229 bit-field types. C++ does not have these separate bit-field
4230 types, and producing a signed or unsigned variant of an
4231 ENUMERAL_TYPE may cause other problems as well. */
4232
1e204133
RAE
4233 if (!INTEGRAL_TYPE_P (type)
4234 || TYPE_UNSIGNED (type) == unsignedp)
4235 return type;
4236
bc15d0ef
JM
4237#define TYPE_OK(node) \
4238 (TYPE_MODE (type) == TYPE_MODE (node) \
41b81065 4239 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
bc15d0ef 4240 if (TYPE_OK (signed_char_type_node))
693a6128 4241 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
bc15d0ef 4242 if (TYPE_OK (integer_type_node))
693a6128 4243 return unsignedp ? unsigned_type_node : integer_type_node;
bc15d0ef 4244 if (TYPE_OK (short_integer_type_node))
693a6128 4245 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
bc15d0ef 4246 if (TYPE_OK (long_integer_type_node))
693a6128 4247 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
bc15d0ef 4248 if (TYPE_OK (long_long_integer_type_node))
693a6128
GRK
4249 return (unsignedp ? long_long_unsigned_type_node
4250 : long_long_integer_type_node);
78a7c317
DD
4251
4252 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4253 if (int_n_enabled_p[i]
4254 && TYPE_MODE (type) == int_n_data[i].m
4255 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4256 return (unsignedp ? int_n_trees[i].unsigned_type
4257 : int_n_trees[i].signed_type);
4258
bc15d0ef 4259 if (TYPE_OK (widest_integer_literal_type_node))
693a6128
GRK
4260 return (unsignedp ? widest_unsigned_literal_type_node
4261 : widest_integer_literal_type_node);
4a063bec
RH
4262
4263#if HOST_BITS_PER_WIDE_INT >= 64
bc15d0ef 4264 if (TYPE_OK (intTI_type_node))
4a063bec
RH
4265 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4266#endif
bc15d0ef 4267 if (TYPE_OK (intDI_type_node))
4a063bec 4268 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
bc15d0ef 4269 if (TYPE_OK (intSI_type_node))
4a063bec 4270 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
bc15d0ef 4271 if (TYPE_OK (intHI_type_node))
4a063bec 4272 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
bc15d0ef 4273 if (TYPE_OK (intQI_type_node))
4a063bec 4274 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
bc15d0ef 4275#undef TYPE_OK
4a063bec 4276
41b81065 4277 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
693a6128 4278}
9649812a 4279
38a4afee
MM
4280/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4281
4282tree
4283c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4284{
78a7c317
DD
4285 int i;
4286
38a4afee
MM
4287 /* Extended integer types of the same width as a standard type have
4288 lesser rank, so those of the same width as int promote to int or
4289 unsigned int and are valid for printf formats expecting int or
4290 unsigned int. To avoid such special cases, avoid creating
4291 extended integer types for bit-fields if a standard integer type
4292 is available. */
4293 if (width == TYPE_PRECISION (integer_type_node))
4294 return unsignedp ? unsigned_type_node : integer_type_node;
4295 if (width == TYPE_PRECISION (signed_char_type_node))
4296 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4297 if (width == TYPE_PRECISION (short_integer_type_node))
4298 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4299 if (width == TYPE_PRECISION (long_integer_type_node))
4300 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4301 if (width == TYPE_PRECISION (long_long_integer_type_node))
4302 return (unsignedp ? long_long_unsigned_type_node
4303 : long_long_integer_type_node);
78a7c317
DD
4304 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4305 if (int_n_enabled_p[i]
4306 && width == int_n_data[i].bitsize)
4307 return (unsignedp ? int_n_trees[i].unsigned_type
4308 : int_n_trees[i].signed_type);
38a4afee
MM
4309 return build_nonstandard_integer_type (width, unsignedp);
4310}
4311
9649812a
MM
4312/* The C version of the register_builtin_type langhook. */
4313
4314void
4315c_register_builtin_type (tree type, const char* name)
4316{
4317 tree decl;
4318
c2255bc4
AH
4319 decl = build_decl (UNKNOWN_LOCATION,
4320 TYPE_DECL, get_identifier (name), type);
9649812a
MM
4321 DECL_ARTIFICIAL (decl) = 1;
4322 if (!TYPE_NAME (type))
4323 TYPE_NAME (type) = decl;
4324 pushdecl (decl);
d1d3865f
ZW
4325
4326 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
9649812a 4327}
6acfe908 4328\f
78ef5b89 4329/* Print an error message for invalid operands to arith operation
ba47d38d
AH
4330 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4331 LOCATION is the location of the message. */
b30f223b
RS
4332
4333void
ba47d38d
AH
4334binary_op_error (location_t location, enum tree_code code,
4335 tree type0, tree type1)
b30f223b 4336{
b3694847 4337 const char *opname;
89c78d7d 4338
b30f223b
RS
4339 switch (code)
4340 {
b30f223b
RS
4341 case PLUS_EXPR:
4342 opname = "+"; break;
4343 case MINUS_EXPR:
4344 opname = "-"; break;
4345 case MULT_EXPR:
4346 opname = "*"; break;
4347 case MAX_EXPR:
4348 opname = "max"; break;
4349 case MIN_EXPR:
4350 opname = "min"; break;
4351 case EQ_EXPR:
4352 opname = "=="; break;
4353 case NE_EXPR:
4354 opname = "!="; break;
4355 case LE_EXPR:
4356 opname = "<="; break;
4357 case GE_EXPR:
4358 opname = ">="; break;
4359 case LT_EXPR:
4360 opname = "<"; break;
4361 case GT_EXPR:
4362 opname = ">"; break;
4363 case LSHIFT_EXPR:
4364 opname = "<<"; break;
4365 case RSHIFT_EXPR:
4366 opname = ">>"; break;
4367 case TRUNC_MOD_EXPR:
047de90b 4368 case FLOOR_MOD_EXPR:
b30f223b
RS
4369 opname = "%"; break;
4370 case TRUNC_DIV_EXPR:
047de90b 4371 case FLOOR_DIV_EXPR:
b30f223b
RS
4372 opname = "/"; break;
4373 case BIT_AND_EXPR:
4374 opname = "&"; break;
4375 case BIT_IOR_EXPR:
4376 opname = "|"; break;
4377 case TRUTH_ANDIF_EXPR:
4378 opname = "&&"; break;
4379 case TRUTH_ORIF_EXPR:
4380 opname = "||"; break;
4381 case BIT_XOR_EXPR:
4382 opname = "^"; break;
6d819282 4383 default:
37b2f290 4384 gcc_unreachable ();
b30f223b 4385 }
ba47d38d
AH
4386 error_at (location,
4387 "invalid operands to binary %s (have %qT and %qT)", opname,
4388 type0, type1);
b30f223b
RS
4389}
4390\f
50f305ca
MLI
4391/* Given an expression as a tree, return its original type. Do this
4392 by stripping any conversion that preserves the sign and precision. */
4393static tree
4394expr_original_type (tree expr)
4395{
4396 STRIP_SIGN_NOPS (expr);
4397 return TREE_TYPE (expr);
4398}
4399
b30f223b
RS
4400/* Subroutine of build_binary_op, used for comparison operations.
4401 See if the operands have both been converted from subword integer types
4402 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
4403 This function is also responsible for converting the two operands
4404 to the proper common type for comparison.
b30f223b
RS
4405
4406 The arguments of this function are all pointers to local variables
4407 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4408 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4409
393e8e8b
MP
4410 LOC is the location of the comparison.
4411
b30f223b
RS
4412 If this function returns nonzero, it means that the comparison has
4413 a constant value. What this function returns is an expression for
4414 that value. */
4415
4416tree
393e8e8b
MP
4417shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4418 tree *restype_ptr, enum tree_code *rescode_ptr)
b30f223b 4419{
b3694847 4420 tree type;
b30f223b
RS
4421 tree op0 = *op0_ptr;
4422 tree op1 = *op1_ptr;
4423 int unsignedp0, unsignedp1;
4424 int real1, real2;
4425 tree primop0, primop1;
4426 enum tree_code code = *rescode_ptr;
4427
4428 /* Throw away any conversions to wider types
4429 already present in the operands. */
4430
828fb3ba
JM
4431 primop0 = c_common_get_narrower (op0, &unsignedp0);
4432 primop1 = c_common_get_narrower (op1, &unsignedp1);
b30f223b 4433
126e6609
JJ
4434 /* If primopN is first sign-extended from primopN's precision to opN's
4435 precision, then zero-extended from opN's precision to
4436 *restype_ptr precision, shortenings might be invalid. */
4437 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4438 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4439 && !unsignedp0
4440 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4441 primop0 = op0;
4442 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4443 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4444 && !unsignedp1
4445 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4446 primop1 = op1;
4447
b30f223b
RS
4448 /* Handle the case that OP0 does not *contain* a conversion
4449 but it *requires* conversion to FINAL_TYPE. */
4450
4451 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
8df83eae 4452 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b30f223b 4453 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
8df83eae 4454 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b30f223b
RS
4455
4456 /* If one of the operands must be floated, we cannot optimize. */
4457 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4458 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4459
4460 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
4461 so value is preserved), for canonicalization. Don't do this if
4462 the second arg is 0. */
b30f223b 4463
5af6001b 4464 if (TREE_CONSTANT (primop0)
ab22c1fa
CF
4465 && !integer_zerop (primop1) && !real_zerop (primop1)
4466 && !fixed_zerop (primop1))
b30f223b 4467 {
fab27f52
MM
4468 std::swap (primop0, primop1);
4469 std::swap (op0, op1);
b30f223b
RS
4470 *op0_ptr = op0;
4471 *op1_ptr = op1;
fab27f52
MM
4472 std::swap (unsignedp0, unsignedp1);
4473 std::swap (real1, real2);
b30f223b
RS
4474
4475 switch (code)
4476 {
4477 case LT_EXPR:
4478 code = GT_EXPR;
4479 break;
4480 case GT_EXPR:
4481 code = LT_EXPR;
4482 break;
4483 case LE_EXPR:
4484 code = GE_EXPR;
4485 break;
4486 case GE_EXPR:
4487 code = LE_EXPR;
4488 break;
6d819282
MK
4489 default:
4490 break;
b30f223b
RS
4491 }
4492 *rescode_ptr = code;
4493 }
4494
4495 /* If comparing an integer against a constant more bits wide,
4496 maybe we can deduce a value of 1 or 0 independent of the data.
4497 Or else truncate the constant now
4498 rather than extend the variable at run time.
4499
4500 This is only interesting if the constant is the wider arg.
4501 Also, it is not safe if the constant is unsigned and the
4502 variable arg is signed, since in this case the variable
4503 would be sign-extended and then regarded as unsigned.
4504 Our technique fails in this case because the lowest/highest
4505 possible unsigned results don't follow naturally from the
4506 lowest/highest possible values of the variable operand.
4507 For just EQ_EXPR and NE_EXPR there is another technique that
4508 could be used: see if the constant can be faithfully represented
4509 in the other operand's type, by truncating it and reextending it
4510 and see if that preserves the constant's value. */
4511
4512 if (!real1 && !real2
ab22c1fa 4513 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b30f223b
RS
4514 && TREE_CODE (primop1) == INTEGER_CST
4515 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4516 {
4517 int min_gt, max_gt, min_lt, max_lt;
4518 tree maxval, minval;
4519 /* 1 if comparison is nominally unsigned. */
8df83eae 4520 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b30f223b
RS
4521 tree val;
4522
ceef8ce4
NB
4523 type = c_common_signed_or_unsigned_type (unsignedp0,
4524 TREE_TYPE (primop0));
8bbd5685 4525
b30f223b
RS
4526 maxval = TYPE_MAX_VALUE (type);
4527 minval = TYPE_MIN_VALUE (type);
4528
4529 if (unsignedp && !unsignedp0)
ceef8ce4 4530 *restype_ptr = c_common_signed_type (*restype_ptr);
b30f223b
RS
4531
4532 if (TREE_TYPE (primop1) != *restype_ptr)
fae1b38d 4533 {
af9c6659
NS
4534 /* Convert primop1 to target type, but do not introduce
4535 additional overflow. We know primop1 is an int_cst. */
807e902e
KZ
4536 primop1 = force_fit_type (*restype_ptr,
4537 wide_int::from
4538 (primop1,
4539 TYPE_PRECISION (*restype_ptr),
4540 TYPE_SIGN (TREE_TYPE (primop1))),
4541 0, TREE_OVERFLOW (primop1));
fae1b38d 4542 }
b30f223b
RS
4543 if (type != *restype_ptr)
4544 {
4545 minval = convert (*restype_ptr, minval);
4546 maxval = convert (*restype_ptr, maxval);
4547 }
4548
807e902e
KZ
4549 min_gt = tree_int_cst_lt (primop1, minval);
4550 max_gt = tree_int_cst_lt (primop1, maxval);
4551 min_lt = tree_int_cst_lt (minval, primop1);
4552 max_lt = tree_int_cst_lt (maxval, primop1);
b30f223b
RS
4553
4554 val = 0;
4555 /* This used to be a switch, but Genix compiler can't handle that. */
4556 if (code == NE_EXPR)
4557 {
4558 if (max_lt || min_gt)
de7df9eb 4559 val = truthvalue_true_node;
b30f223b
RS
4560 }
4561 else if (code == EQ_EXPR)
4562 {
4563 if (max_lt || min_gt)
de7df9eb 4564 val = truthvalue_false_node;
b30f223b
RS
4565 }
4566 else if (code == LT_EXPR)
4567 {
4568 if (max_lt)
de7df9eb 4569 val = truthvalue_true_node;
b30f223b 4570 if (!min_lt)
de7df9eb 4571 val = truthvalue_false_node;
b30f223b
RS
4572 }
4573 else if (code == GT_EXPR)
4574 {
4575 if (min_gt)
de7df9eb 4576 val = truthvalue_true_node;
b30f223b 4577 if (!max_gt)
de7df9eb 4578 val = truthvalue_false_node;
b30f223b
RS
4579 }
4580 else if (code == LE_EXPR)
4581 {
4582 if (!max_gt)
de7df9eb 4583 val = truthvalue_true_node;
b30f223b 4584 if (min_gt)
de7df9eb 4585 val = truthvalue_false_node;
b30f223b
RS
4586 }
4587 else if (code == GE_EXPR)
4588 {
4589 if (!min_lt)
de7df9eb 4590 val = truthvalue_true_node;
b30f223b 4591 if (max_lt)
de7df9eb 4592 val = truthvalue_false_node;
b30f223b
RS
4593 }
4594
4595 /* If primop0 was sign-extended and unsigned comparison specd,
4596 we did a signed comparison above using the signed type bounds.
4597 But the comparison we output must be unsigned.
4598
4599 Also, for inequalities, VAL is no good; but if the signed
4600 comparison had *any* fixed result, it follows that the
4601 unsigned comparison just tests the sign in reverse
4602 (positive values are LE, negative ones GE).
4603 So we can generate an unsigned comparison
4604 against an extreme value of the signed type. */
4605
4606 if (unsignedp && !unsignedp0)
4607 {
4608 if (val != 0)
4609 switch (code)
4610 {
4611 case LT_EXPR:
4612 case GE_EXPR:
4613 primop1 = TYPE_MIN_VALUE (type);
4614 val = 0;
4615 break;
4616
4617 case LE_EXPR:
4618 case GT_EXPR:
4619 primop1 = TYPE_MAX_VALUE (type);
4620 val = 0;
4621 break;
6d819282
MK
4622
4623 default:
4624 break;
b30f223b 4625 }
12753674 4626 type = c_common_unsigned_type (type);
b30f223b
RS
4627 }
4628
ca7e759d 4629 if (TREE_CODE (primop0) != INTEGER_CST)
b30f223b 4630 {
de7df9eb 4631 if (val == truthvalue_false_node)
50f305ca
MLI
4632 warning_at (loc, OPT_Wtype_limits,
4633 "comparison is always false due to limited range of data type");
de7df9eb 4634 if (val == truthvalue_true_node)
50f305ca
MLI
4635 warning_at (loc, OPT_Wtype_limits,
4636 "comparison is always true due to limited range of data type");
b30f223b
RS
4637 }
4638
4639 if (val != 0)
4640 {
4641 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4642 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3 4643 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b30f223b
RS
4644 return val;
4645 }
4646
4647 /* Value is not predetermined, but do the comparison
4648 in the type of the operand that is not constant.
4649 TYPE is already properly set. */
4650 }
9a8ce21f
JG
4651
4652 /* If either arg is decimal float and the other is float, find the
4653 proper common type to use for comparison. */
6f450181
RB
4654 else if (real1 && real2
4655 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4656 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4657 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4658
4659 /* If either arg is decimal float and the other is float, fail. */
9a8ce21f
JG
4660 else if (real1 && real2
4661 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4662 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
6f450181 4663 return 0;
9a8ce21f 4664
b30f223b 4665 else if (real1 && real2
766f6c30
RS
4666 && (TYPE_PRECISION (TREE_TYPE (primop0))
4667 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
4668 type = TREE_TYPE (primop0);
4669
4670 /* If args' natural types are both narrower than nominal type
4671 and both extend in the same manner, compare them
4672 in the type of the wider arg.
4673 Otherwise must actually extend both to the nominal
4674 common type lest different ways of extending
4675 alter the result.
4676 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4677
4678 else if (unsignedp0 == unsignedp1 && real1 == real2
4679 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4680 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4681 {
4682 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
ceef8ce4 4683 type = c_common_signed_or_unsigned_type (unsignedp0
8df83eae 4684 || TYPE_UNSIGNED (*restype_ptr),
ceef8ce4 4685 type);
b30f223b
RS
4686 /* Make sure shorter operand is extended the right way
4687 to match the longer operand. */
ceef8ce4
NB
4688 primop0
4689 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4690 TREE_TYPE (primop0)),
4691 primop0);
4692 primop1
4693 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4694 TREE_TYPE (primop1)),
4695 primop1);
b30f223b
RS
4696 }
4697 else
4698 {
4699 /* Here we must do the comparison on the nominal type
4700 using the args exactly as we received them. */
4701 type = *restype_ptr;
4702 primop0 = op0;
4703 primop1 = op1;
4704
4705 if (!real1 && !real2 && integer_zerop (primop1)
8df83eae 4706 && TYPE_UNSIGNED (*restype_ptr))
b30f223b
RS
4707 {
4708 tree value = 0;
50f305ca
MLI
4709 /* All unsigned values are >= 0, so we warn. However,
4710 if OP0 is a constant that is >= 0, the signedness of
4711 the comparison isn't an issue, so suppress the
4712 warning. */
4713 bool warn =
8400e75e 4714 warn_type_limits && !in_system_header_at (loc)
50f305ca
MLI
4715 && !(TREE_CODE (primop0) == INTEGER_CST
4716 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4717 primop0)))
4718 /* Do not warn for enumeration types. */
4719 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4720
b30f223b
RS
4721 switch (code)
4722 {
4723 case GE_EXPR:
50f305ca
MLI
4724 if (warn)
4725 warning_at (loc, OPT_Wtype_limits,
4726 "comparison of unsigned expression >= 0 is always true");
de7df9eb 4727 value = truthvalue_true_node;
b30f223b
RS
4728 break;
4729
4730 case LT_EXPR:
50f305ca
MLI
4731 if (warn)
4732 warning_at (loc, OPT_Wtype_limits,
4733 "comparison of unsigned expression < 0 is always false");
de7df9eb 4734 value = truthvalue_false_node;
6d819282
MK
4735 break;
4736
4737 default:
4738 break;
b30f223b
RS
4739 }
4740
4741 if (value != 0)
4742 {
4743 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4744 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3
RS
4745 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4746 primop0, value);
b30f223b
RS
4747 return value;
4748 }
4749 }
4750 }
4751
4752 *op0_ptr = convert (type, primop0);
4753 *op1_ptr = convert (type, primop1);
4754
de7df9eb 4755 *restype_ptr = truthvalue_type_node;
b30f223b
RS
4756
4757 return 0;
4758}
4759\f
7552da58
JJ
4760/* Return a tree for the sum or difference (RESULTCODE says which)
4761 of pointer PTROP and integer INTOP. */
4762
4763tree
db3927fb 4764pointer_int_sum (location_t loc, enum tree_code resultcode,
fd9b0f32 4765 tree ptrop, tree intop, bool complain)
7552da58 4766{
6ac01510 4767 tree size_exp, ret;
7552da58 4768
7552da58 4769 /* The result is a pointer of the same type that is being added. */
7552da58
JJ
4770 tree result_type = TREE_TYPE (ptrop);
4771
4772 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4773 {
fd9b0f32
PC
4774 if (complain && warn_pointer_arith)
4775 pedwarn (loc, OPT_Wpointer_arith,
4776 "pointer of type %<void *%> used in arithmetic");
4777 else if (!complain)
4778 return error_mark_node;
7552da58
JJ
4779 size_exp = integer_one_node;
4780 }
4781 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4782 {
fd9b0f32
PC
4783 if (complain && warn_pointer_arith)
4784 pedwarn (loc, OPT_Wpointer_arith,
4785 "pointer to a function used in arithmetic");
4786 else if (!complain)
4787 return error_mark_node;
7552da58
JJ
4788 size_exp = integer_one_node;
4789 }
7552da58
JJ
4790 else
4791 size_exp = size_in_bytes (TREE_TYPE (result_type));
4792
6ac01510
ILT
4793 /* We are manipulating pointer values, so we don't need to warn
4794 about relying on undefined signed overflow. We disable the
4795 warning here because we use integer types so fold won't know that
4796 they are really pointers. */
4797 fold_defer_overflow_warnings ();
4798
7552da58
JJ
4799 /* If what we are about to multiply by the size of the elements
4800 contains a constant term, apply distributive law
4801 and multiply that constant term separately.
4802 This helps produce common subexpressions. */
7552da58 4803 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3f75a254 4804 && !TREE_CONSTANT (intop)
7552da58
JJ
4805 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4806 && TREE_CONSTANT (size_exp)
4807 /* If the constant comes from pointer subtraction,
4808 skip this optimization--it would cause an error. */
4809 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4810 /* If the constant is unsigned, and smaller than the pointer size,
4811 then we must skip this optimization. This is because it could cause
4812 an overflow error if the constant is negative but INTOP is not. */
3f75a254 4813 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
7552da58
JJ
4814 || (TYPE_PRECISION (TREE_TYPE (intop))
4815 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4816 {
4817 enum tree_code subcode = resultcode;
4818 tree int_type = TREE_TYPE (intop);
4819 if (TREE_CODE (intop) == MINUS_EXPR)
4820 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4821 /* Convert both subexpression types to the type of intop,
4822 because weird cases involving pointer arithmetic
4823 can result in a sum or difference with different type args. */
ba47d38d
AH
4824 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4825 subcode, ptrop,
7552da58
JJ
4826 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4827 intop = convert (int_type, TREE_OPERAND (intop, 0));
4828 }
4829
4830 /* Convert the integer argument to a type the same size as sizetype
4831 so the multiply won't overflow spuriously. */
7552da58 4832 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
8df83eae 4833 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
35b1a6fa 4834 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
8df83eae 4835 TYPE_UNSIGNED (sizetype)), intop);
7552da58
JJ
4836
4837 /* Replace the integer argument with a suitable product by the object size.
9e9ef331 4838 Do this multiplication as signed, then convert to the appropriate type
65de6659 4839 for the pointer operation and disregard an overflow that occurred only
9e9ef331
EB
4840 because of the sign-extension change in the latter conversion. */
4841 {
4842 tree t = build_binary_op (loc,
4843 MULT_EXPR, intop,
4844 convert (TREE_TYPE (intop), size_exp), 1);
4845 intop = convert (sizetype, t);
4846 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
807e902e 4847 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
9e9ef331 4848 }
5be014d5 4849
280f1ffa 4850 /* Create the sum or difference. */
5be014d5 4851 if (resultcode == MINUS_EXPR)
db3927fb 4852 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
7552da58 4853
5d49b6a7 4854 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
6ac01510
ILT
4855
4856 fold_undefer_and_ignore_overflow_warnings ();
4857
4858 return ret;
7552da58
JJ
4859}
4860\f
e5a94231
JM
4861/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4862 and if NON_CONST is known not to be permitted in an evaluated part
4863 of a constant expression. */
4864
4865tree
4866c_wrap_maybe_const (tree expr, bool non_const)
4867{
4868 bool nowarning = TREE_NO_WARNING (expr);
4869 location_t loc = EXPR_LOCATION (expr);
4870
4871 /* This should never be called for C++. */
4872 if (c_dialect_cxx ())
4873 gcc_unreachable ();
4874
4875 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4876 STRIP_TYPE_NOPS (expr);
4877 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4878 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4879 if (nowarning)
4880 TREE_NO_WARNING (expr) = 1;
4881 protected_set_expr_location (expr, loc);
4882
4883 return expr;
4884}
4885
928c19bb
JM
4886/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4887 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4888 around the SAVE_EXPR if needed so that c_fully_fold does not need
4889 to look inside SAVE_EXPRs. */
4890
4891tree
4892c_save_expr (tree expr)
4893{
4894 bool maybe_const = true;
4895 if (c_dialect_cxx ())
4896 return save_expr (expr);
4897 expr = c_fully_fold (expr, false, &maybe_const);
4898 expr = save_expr (expr);
4899 if (!maybe_const)
e5a94231 4900 expr = c_wrap_maybe_const (expr, true);
928c19bb
JM
4901 return expr;
4902}
4903
b3c6d2ea
ILT
4904/* Return whether EXPR is a declaration whose address can never be
4905 NULL. */
4906
4907bool
58f9752a 4908decl_with_nonnull_addr_p (const_tree expr)
b3c6d2ea
ILT
4909{
4910 return (DECL_P (expr)
4911 && (TREE_CODE (expr) == PARM_DECL
4912 || TREE_CODE (expr) == LABEL_DECL
4913 || !DECL_WEAK (expr)));
4914}
4915
b30f223b 4916/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
85498824
JM
4917 or for an `if' or `while' statement or ?..: exp. It should already
4918 have been validated to be of suitable type; otherwise, a bad
4919 diagnostic may result.
b30f223b 4920
ba47d38d
AH
4921 The EXPR is located at LOCATION.
4922
b30f223b
RS
4923 This preparation consists of taking the ordinary
4924 representation of an expression expr and producing a valid tree
4925 boolean expression describing whether expr is nonzero. We could
de7df9eb 4926 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b30f223b
RS
4927 but we optimize comparisons, &&, ||, and !.
4928
de7df9eb 4929 The resulting type should always be `truthvalue_type_node'. */
b30f223b
RS
4930
4931tree
ba47d38d 4932c_common_truthvalue_conversion (location_t location, tree expr)
b30f223b 4933{
b30f223b
RS
4934 switch (TREE_CODE (expr))
4935 {
d1a7edaf 4936 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
6f312d18
ZW
4937 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4938 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4939 case ORDERED_EXPR: case UNORDERED_EXPR:
90ec750d
RS
4940 if (TREE_TYPE (expr) == truthvalue_type_node)
4941 return expr;
c2255bc4 4942 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
90ec750d 4943 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
c2255bc4 4944 goto ret;
90ec750d 4945
b30f223b
RS
4946 case TRUTH_ANDIF_EXPR:
4947 case TRUTH_ORIF_EXPR:
4948 case TRUTH_AND_EXPR:
4949 case TRUTH_OR_EXPR:
9379fac9 4950 case TRUTH_XOR_EXPR:
90ec750d
RS
4951 if (TREE_TYPE (expr) == truthvalue_type_node)
4952 return expr;
c2255bc4 4953 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
b8698a0f 4954 c_common_truthvalue_conversion (location,
c2255bc4
AH
4955 TREE_OPERAND (expr, 0)),
4956 c_common_truthvalue_conversion (location,
4957 TREE_OPERAND (expr, 1)));
4958 goto ret;
18c0f675 4959
18d00205 4960 case TRUTH_NOT_EXPR:
90ec750d
RS
4961 if (TREE_TYPE (expr) == truthvalue_type_node)
4962 return expr;
c2255bc4
AH
4963 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4964 c_common_truthvalue_conversion (location,
4965 TREE_OPERAND (expr, 0)));
4966 goto ret;
18d00205 4967
b30f223b
RS
4968 case ERROR_MARK:
4969 return expr;
4970
4971 case INTEGER_CST:
d95787e6
RS
4972 return integer_zerop (expr) ? truthvalue_false_node
4973 : truthvalue_true_node;
b30f223b
RS
4974
4975 case REAL_CST:
010c4d9c
RS
4976 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4977 ? truthvalue_true_node
4978 : truthvalue_false_node;
b30f223b 4979
ab22c1fa
CF
4980 case FIXED_CST:
4981 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4982 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4983 ? truthvalue_true_node
4984 : truthvalue_false_node;
4985
90ec750d 4986 case FUNCTION_DECL:
c9f9eb5d 4987 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
90ec750d
RS
4988 /* Fall through. */
4989
b30f223b 4990 case ADDR_EXPR:
1998463c 4991 {
f6f08360 4992 tree inner = TREE_OPERAND (expr, 0);
b3c6d2ea 4993 if (decl_with_nonnull_addr_p (inner))
1998463c 4994 {
b3c6d2ea 4995 /* Common Ada/Pascal programmer's mistake. */
ba47d38d
AH
4996 warning_at (location,
4997 OPT_Waddress,
4998 "the address of %qD will always evaluate as %<true%>",
4999 inner);
1998463c
SB
5000 return truthvalue_true_node;
5001 }
33766b66 5002 break;
1998463c 5003 }
b30f223b 5004
766f6c30 5005 case COMPLEX_EXPR:
c2255bc4 5006 expr = build_binary_op (EXPR_LOCATION (expr),
ba47d38d 5007 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 5008 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
ba47d38d
AH
5009 c_common_truthvalue_conversion (location,
5010 TREE_OPERAND (expr, 0)),
5011 c_common_truthvalue_conversion (location,
5012 TREE_OPERAND (expr, 1)),
766f6c30 5013 0);
c2255bc4 5014 goto ret;
766f6c30 5015
b30f223b
RS
5016 case NEGATE_EXPR:
5017 case ABS_EXPR:
5018 case FLOAT_EXPR:
8ce94e44 5019 case EXCESS_PRECISION_EXPR:
da7d8304 5020 /* These don't change whether an object is nonzero or zero. */
ba47d38d 5021 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b30f223b
RS
5022
5023 case LROTATE_EXPR:
5024 case RROTATE_EXPR:
da7d8304 5025 /* These don't change whether an object is zero or nonzero, but
b30f223b
RS
5026 we can't ignore them if their second arg has side-effects. */
5027 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
c2255bc4
AH
5028 {
5029 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
5030 TREE_OPERAND (expr, 1),
b8698a0f 5031 c_common_truthvalue_conversion
c2255bc4
AH
5032 (location, TREE_OPERAND (expr, 0)));
5033 goto ret;
5034 }
b30f223b 5035 else
ba47d38d
AH
5036 return c_common_truthvalue_conversion (location,
5037 TREE_OPERAND (expr, 0));
b57062ca 5038
b30f223b
RS
5039 case COND_EXPR:
5040 /* Distribute the conversion into the arms of a COND_EXPR. */
928c19bb 5041 if (c_dialect_cxx ())
c2255bc4 5042 {
4cc4f2f4
JJ
5043 tree op1 = TREE_OPERAND (expr, 1);
5044 tree op2 = TREE_OPERAND (expr, 2);
5045 /* In C++ one of the arms might have void type if it is throw. */
5046 if (!VOID_TYPE_P (TREE_TYPE (op1)))
5047 op1 = c_common_truthvalue_conversion (location, op1);
5048 if (!VOID_TYPE_P (TREE_TYPE (op2)))
5049 op2 = c_common_truthvalue_conversion (location, op2);
db3927fb 5050 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4cc4f2f4 5051 TREE_OPERAND (expr, 0), op1, op2);
c2255bc4
AH
5052 goto ret;
5053 }
928c19bb 5054 else
c2255bc4
AH
5055 {
5056 /* Folding will happen later for C. */
5057 expr = build3 (COND_EXPR, truthvalue_type_node,
5058 TREE_OPERAND (expr, 0),
5059 c_common_truthvalue_conversion (location,
5060 TREE_OPERAND (expr, 1)),
5061 c_common_truthvalue_conversion (location,
5062 TREE_OPERAND (expr, 2)));
5063 goto ret;
5064 }
b30f223b 5065
1043771b 5066 CASE_CONVERT:
1ee44b26
JM
5067 {
5068 tree totype = TREE_TYPE (expr);
5069 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
5070
076fecad
PP
5071 if (POINTER_TYPE_P (totype)
5072 && TREE_CODE (fromtype) == REFERENCE_TYPE)
5073 {
5074 tree inner = expr;
5075 STRIP_NOPS (inner);
5076
5077 if (DECL_P (inner))
5078 warning_at (location,
5079 OPT_Waddress,
5080 "the compiler can assume that the address of "
5081 "%qD will always evaluate to %<true%>",
5082 inner);
5083 }
5084
1ee44b26
JM
5085 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
5086 since that affects how `default_conversion' will behave. */
5087 if (TREE_CODE (totype) == REFERENCE_TYPE
5088 || TREE_CODE (fromtype) == REFERENCE_TYPE)
5089 break;
5090 /* Don't strip a conversion from C++0x scoped enum, since they
5091 don't implicitly convert to other types. */
5092 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
5093 && ENUM_IS_SCOPED (fromtype))
5094 break;
5095 /* If this isn't narrowing the argument, we can ignore it. */
5096 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
5097 return c_common_truthvalue_conversion (location,
5098 TREE_OPERAND (expr, 0));
5099 }
b30f223b
RS
5100 break;
5101
e2aab13d 5102 case MODIFY_EXPR:
fbc8d2d3
ILT
5103 if (!TREE_NO_WARNING (expr)
5104 && warn_parentheses)
5105 {
5106 warning (OPT_Wparentheses,
5107 "suggest parentheses around assignment used as truth value");
5108 TREE_NO_WARNING (expr) = 1;
5109 }
e2aab13d 5110 break;
b57062ca 5111
6d819282
MK
5112 default:
5113 break;
b30f223b
RS
5114 }
5115
f0b996c5 5116 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa 5117 {
5386338c 5118 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
c2255bc4 5119 expr = (build_binary_op
ba47d38d
AH
5120 (EXPR_LOCATION (expr),
5121 (TREE_SIDE_EFFECTS (expr)
f0b8d9aa 5122 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
c9f9eb5d
AH
5123 c_common_truthvalue_conversion
5124 (location,
5125 build_unary_op (location, REALPART_EXPR, t, 0)),
5126 c_common_truthvalue_conversion
5127 (location,
5128 build_unary_op (location, IMAGPART_EXPR, t, 0)),
f0b8d9aa 5129 0));
c2255bc4 5130 goto ret;
f0b8d9aa 5131 }
f0b996c5 5132
ab22c1fa
CF
5133 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5134 {
5135 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5136 FCONST0 (TYPE_MODE
5137 (TREE_TYPE (expr))));
ca80e52b 5138 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
ab22c1fa 5139 }
c2255bc4
AH
5140 else
5141 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
ab22c1fa 5142
c2255bc4
AH
5143 ret:
5144 protected_set_expr_location (expr, location);
5145 return expr;
b30f223b
RS
5146}
5147\f
9bc15050
RG
5148static void def_builtin_1 (enum built_in_function fncode,
5149 const char *name,
5150 enum built_in_class fnclass,
5151 tree fntype, tree libtype,
5152 bool both_p, bool fallback_p, bool nonansi_p,
5153 tree fnattrs, bool implicit_p);
fc2aaf30 5154
3932261a
MM
5155
5156/* Apply the TYPE_QUALS to the new DECL. */
5157
5158void
35b1a6fa 5159c_apply_type_quals_to_decl (int type_quals, tree decl)
3932261a 5160{
4b011bbf 5161 tree type = TREE_TYPE (decl);
9f63daea 5162
5a6159dd
AP
5163 if (type == error_mark_node)
5164 return;
4b011bbf 5165
329af3c7
JM
5166 if ((type_quals & TYPE_QUAL_CONST)
5167 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5168 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5169 constructor can produce constant init, so rely on cp_finish_decl to
5170 clear TREE_READONLY if the variable has non-constant init. */
3932261a
MM
5171 TREE_READONLY (decl) = 1;
5172 if (type_quals & TYPE_QUAL_VOLATILE)
5173 {
5174 TREE_SIDE_EFFECTS (decl) = 1;
5175 TREE_THIS_VOLATILE (decl) = 1;
5176 }
6946bc60 5177 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 5178 {
4b011bbf
JM
5179 while (type && TREE_CODE (type) == ARRAY_TYPE)
5180 /* Allow 'restrict' on arrays of pointers.
5181 FIXME currently we just ignore it. */
5182 type = TREE_TYPE (type);
5183 if (!type
5184 || !POINTER_TYPE_P (type)
5185 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
bda67431 5186 error ("invalid use of %<restrict%>");
3932261a
MM
5187 }
5188}
5189
ca752f39 5190struct c_type_hasher : ggc_ptr_hash<tree_node>
2a22f99c
TS
5191{
5192 static hashval_t hash (tree);
5193 static bool equal (tree, tree);
5194};
5195
6de9cd9a
DN
5196/* Hash function for the problem of multiple type definitions in
5197 different files. This must hash all types that will compare
5198 equal via comptypes to the same value. In practice it hashes
9cf737f8 5199 on some of the simple stuff and leaves the details to comptypes. */
6de9cd9a 5200
2a22f99c
TS
5201hashval_t
5202c_type_hasher::hash (tree t)
6de9cd9a 5203{
a19e4d44 5204 int n_elements;
6de9cd9a 5205 int shift, size;
6de9cd9a
DN
5206 tree t2;
5207 switch (TREE_CODE (t))
5208 {
8c27b7d4 5209 /* For pointers, hash on pointee type plus some swizzling. */
325c3691 5210 case POINTER_TYPE:
2a22f99c 5211 return hash (TREE_TYPE (t)) ^ 0x3003003;
325c3691
RH
5212 /* Hash on number of elements and total size. */
5213 case ENUMERAL_TYPE:
5214 shift = 3;
5215 t2 = TYPE_VALUES (t);
5216 break;
5217 case RECORD_TYPE:
5218 shift = 0;
5219 t2 = TYPE_FIELDS (t);
5220 break;
5221 case QUAL_UNION_TYPE:
5222 shift = 1;
5223 t2 = TYPE_FIELDS (t);
5224 break;
5225 case UNION_TYPE:
5226 shift = 2;
5227 t2 = TYPE_FIELDS (t);
5228 break;
5229 default:
366de0ce 5230 gcc_unreachable ();
6de9cd9a 5231 }
a19e4d44
NF
5232 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5233 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5234 n_elements = list_length (t2);
6fc3c3c0
TT
5235 /* We might have a VLA here. */
5236 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5237 size = 0;
5238 else
5239 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
a19e4d44 5240 return ((size << 24) | (n_elements << shift));
6de9cd9a
DN
5241}
5242
2a22f99c
TS
5243bool
5244c_type_hasher::equal (tree t1, tree t2)
5245{
5246 return lang_hooks.types_compatible_p (t1, t2);
5247}
5248
5249static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4fe52ce9 5250
41472af8 5251/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 5252 or a type. Return -1 if we don't do anything special. */
41472af8 5253
4862826d 5254alias_set_type
35b1a6fa 5255c_common_get_alias_set (tree t)
41472af8 5256{
08bc2431 5257 tree u;
35b1a6fa 5258
cb9c2485
JM
5259 /* For VLAs, use the alias set of the element type rather than the
5260 default of alias set 0 for types compared structurally. */
5261 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5262 {
5263 if (TREE_CODE (t) == ARRAY_TYPE)
5264 return get_alias_set (TREE_TYPE (t));
5265 return -1;
5266 }
5267
08bc2431
MM
5268 /* Permit type-punning when accessing a union, provided the access
5269 is directly through the union. For example, this code does not
5270 permit taking the address of a union member and then storing
5271 through it. Even the type-punning allowed here is a GCC
5272 extension, albeit a common and useful one; the C standard says
5273 that such accesses have implementation-defined behavior. */
5274 for (u = t;
5275 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5276 u = TREE_OPERAND (u, 0))
5277 if (TREE_CODE (u) == COMPONENT_REF
5278 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5279 return 0;
ece32014 5280
74d86f4f 5281 /* That's all the expressions we handle specially. */
3f75a254 5282 if (!TYPE_P (t))
74d86f4f
RH
5283 return -1;
5284
95bd1dd7 5285 /* The C standard guarantees that any object may be accessed via an
74d86f4f
RH
5286 lvalue that has character type. */
5287 if (t == char_type_node
5288 || t == signed_char_type_node
5289 || t == unsigned_char_type_node)
3bdf5ad1 5290 return 0;
3932261a 5291
f824e5c3
RK
5292 /* The C standard specifically allows aliasing between signed and
5293 unsigned variants of the same type. We treat the signed
5294 variant as canonical. */
8df83eae 5295 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
8f215dce 5296 {
ceef8ce4 5297 tree t1 = c_common_signed_type (t);
f824e5c3 5298
8f215dce
JJ
5299 /* t1 == t can happen for boolean nodes which are always unsigned. */
5300 if (t1 != t)
5301 return get_alias_set (t1);
5302 }
ece32014 5303
6de9cd9a
DN
5304 /* Handle the case of multiple type nodes referring to "the same" type,
5305 which occurs with IMA. These share an alias set. FIXME: Currently only
5306 C90 is handled. (In C99 type compatibility is not transitive, which
5307 complicates things mightily. The alias set splay trees can theoretically
5308 represent this, but insertion is tricky when you consider all the
5309 different orders things might arrive in.) */
5310
5311 if (c_language != clk_c || flag_isoc99)
5312 return -1;
5313
9cf737f8 5314 /* Save time if there's only one input file. */
d974312d 5315 if (num_in_fnames == 1)
6de9cd9a
DN
5316 return -1;
5317
5318 /* Pointers need special handling if they point to any type that
5319 needs special handling (below). */
5320 if (TREE_CODE (t) == POINTER_TYPE)
5321 {
5322 tree t2;
5323 /* Find bottom type under any nested POINTERs. */
9f63daea 5324 for (t2 = TREE_TYPE (t);
762f7d9d
TT
5325 TREE_CODE (t2) == POINTER_TYPE;
5326 t2 = TREE_TYPE (t2))
5327 ;
9f63daea 5328 if (TREE_CODE (t2) != RECORD_TYPE
762f7d9d
TT
5329 && TREE_CODE (t2) != ENUMERAL_TYPE
5330 && TREE_CODE (t2) != QUAL_UNION_TYPE
5331 && TREE_CODE (t2) != UNION_TYPE)
5332 return -1;
6de9cd9a 5333 if (TYPE_SIZE (t2) == 0)
762f7d9d 5334 return -1;
6de9cd9a
DN
5335 }
5336 /* These are the only cases that need special handling. */
9f63daea 5337 if (TREE_CODE (t) != RECORD_TYPE
6de9cd9a
DN
5338 && TREE_CODE (t) != ENUMERAL_TYPE
5339 && TREE_CODE (t) != QUAL_UNION_TYPE
5340 && TREE_CODE (t) != UNION_TYPE
5341 && TREE_CODE (t) != POINTER_TYPE)
5342 return -1;
5343 /* Undefined? */
5344 if (TYPE_SIZE (t) == 0)
5345 return -1;
5346
9f63daea 5347 /* Look up t in hash table. Only one of the compatible types within each
6de9cd9a
DN
5348 alias set is recorded in the table. */
5349 if (!type_hash_table)
2a22f99c
TS
5350 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5351 tree *slot = type_hash_table->find_slot (t, INSERT);
6de9cd9a 5352 if (*slot != NULL)
6a3203c8
AP
5353 {
5354 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5355 return TYPE_ALIAS_SET ((tree)*slot);
5356 }
6de9cd9a
DN
5357 else
5358 /* Our caller will assign and record (in t) a new alias set; all we need
5359 to do is remember t in the hash table. */
5360 *slot = t;
5361
3bdf5ad1 5362 return -1;
41472af8 5363}
0213a355 5364\f
c2255bc4 5365/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
296674db 5366 the IS_SIZEOF parameter indicates which operator is being applied.
c2255bc4
AH
5367 The COMPLAIN flag controls whether we should diagnose possibly
5368 ill-formed constructs or not. LOC is the location of the SIZEOF or
296674db
JM
5369 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5370 a type in any context should be returned, rather than the normal
5371 alignment for that type. */
03a08664 5372
0213a355 5373tree
c2255bc4 5374c_sizeof_or_alignof_type (location_t loc,
296674db
JM
5375 tree type, bool is_sizeof, bool min_alignof,
5376 int complain)
0213a355 5377{
fa72b064
GDR
5378 const char *op_name;
5379 tree value = NULL;
5380 enum tree_code type_code = TREE_CODE (type);
35b1a6fa 5381
03a08664 5382 op_name = is_sizeof ? "sizeof" : "__alignof__";
35b1a6fa 5383
fa72b064 5384 if (type_code == FUNCTION_TYPE)
0213a355 5385 {
03a08664 5386 if (is_sizeof)
fa72b064 5387 {
44d90fe1
PC
5388 if (complain && warn_pointer_arith)
5389 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 5390 "invalid application of %<sizeof%> to a function type");
5ade1ed2
DG
5391 else if (!complain)
5392 return error_mark_node;
fa72b064
GDR
5393 value = size_one_node;
5394 }
5395 else
d19fa6b5
JM
5396 {
5397 if (complain)
5398 {
5399 if (c_dialect_cxx ())
c1771a20 5400 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
d19fa6b5
JM
5401 "%<alignof%> applied to a function type");
5402 else
c1771a20 5403 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
d19fa6b5
JM
5404 "%<_Alignof%> applied to a function type");
5405 }
5406 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5407 }
fa72b064
GDR
5408 }
5409 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5410 {
35b1a6fa 5411 if (type_code == VOID_TYPE
44d90fe1
PC
5412 && complain && warn_pointer_arith)
5413 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 5414 "invalid application of %qs to a void type", op_name);
5ade1ed2
DG
5415 else if (!complain)
5416 return error_mark_node;
fa72b064 5417 value = size_one_node;
0213a355 5418 }
73ac190a
PC
5419 else if (!COMPLETE_TYPE_P (type)
5420 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
0213a355 5421 {
ea793912 5422 if (complain)
73ac190a 5423 error_at (loc, "invalid application of %qs to incomplete type %qT",
c2255bc4 5424 op_name, type);
cb6addf4 5425 return error_mark_node;
0213a355 5426 }
73ac190a
PC
5427 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5428 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5429 {
5430 if (complain)
5431 error_at (loc, "invalid application of %qs to array type %qT of "
5432 "incomplete element type", op_name, type);
5433 return error_mark_node;
5434 }
0213a355 5435 else
fa72b064 5436 {
03a08664 5437 if (is_sizeof)
fa72b064 5438 /* Convert in case a char is more than one unit. */
db3927fb
AH
5439 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5440 size_int (TYPE_PRECISION (char_type_node)
5441 / BITS_PER_UNIT));
296674db 5442 else if (min_alignof)
2793eeab 5443 value = size_int (min_align_of_type (type));
fa72b064 5444 else
a4e9ffe5 5445 value = size_int (TYPE_ALIGN_UNIT (type));
fa72b064 5446 }
0213a355 5447
3ac8781c
RG
5448 /* VALUE will have the middle-end integer type sizetype.
5449 However, we should really return a value of type `size_t',
5450 which is just a typedef for an ordinary integer type. */
db3927fb 5451 value = fold_convert_loc (loc, size_type_node, value);
35b1a6fa 5452
fa72b064 5453 return value;
0213a355
JM
5454}
5455
5456/* Implement the __alignof keyword: Return the minimum required
837edd5f
GK
5457 alignment of EXPR, measured in bytes. For VAR_DECLs,
5458 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
c2255bc4
AH
5459 from an "aligned" __attribute__ specification). LOC is the
5460 location of the ALIGNOF operator. */
7f4edbcb 5461
0213a355 5462tree
c2255bc4 5463c_alignof_expr (location_t loc, tree expr)
0213a355
JM
5464{
5465 tree t;
5466
837edd5f 5467 if (VAR_OR_FUNCTION_DECL_P (expr))
a4e9ffe5 5468 t = size_int (DECL_ALIGN_UNIT (expr));
35b1a6fa 5469
0213a355
JM
5470 else if (TREE_CODE (expr) == COMPONENT_REF
5471 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5472 {
c2255bc4 5473 error_at (loc, "%<__alignof%> applied to a bit-field");
0213a355
JM
5474 t = size_one_node;
5475 }
5476 else if (TREE_CODE (expr) == COMPONENT_REF
173bf5be 5477 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
a4e9ffe5 5478 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
35b1a6fa 5479
22d03525 5480 else if (INDIRECT_REF_P (expr))
0213a355
JM
5481 {
5482 tree t = TREE_OPERAND (expr, 0);
5483 tree best = t;
5484 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
35b1a6fa 5485
1043771b 5486 while (CONVERT_EXPR_P (t)
173bf5be 5487 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
0213a355
JM
5488 {
5489 int thisalign;
5490
5491 t = TREE_OPERAND (t, 0);
5492 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5493 if (thisalign > bestalign)
5494 best = t, bestalign = thisalign;
5495 }
c2255bc4 5496 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
0213a355
JM
5497 }
5498 else
c2255bc4 5499 return c_alignof (loc, TREE_TYPE (expr));
0213a355 5500
db3927fb 5501 return fold_convert_loc (loc, size_type_node, t);
0213a355
JM
5502}
5503\f
df061a43
RS
5504/* Handle C and C++ default attributes. */
5505
5506enum built_in_attribute
5507{
5508#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5509#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
e384e6b5 5510#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
df061a43
RS
5511#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5512#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
df061a43
RS
5513#include "builtin-attrs.def"
5514#undef DEF_ATTR_NULL_TREE
5515#undef DEF_ATTR_INT
e384e6b5 5516#undef DEF_ATTR_STRING
df061a43
RS
5517#undef DEF_ATTR_IDENT
5518#undef DEF_ATTR_TREE_LIST
df061a43
RS
5519 ATTR_LAST
5520};
5521
5522static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5523
35b1a6fa 5524static void c_init_attributes (void);
df061a43 5525
a0274e3e 5526enum c_builtin_type
7f4edbcb 5527{
10841285
MM
5528#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5529#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5530#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5531#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5532#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5533#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
10a0d495 5534#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
f6a7cffc
TS
5535#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5536 ARG6) NAME,
5537#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5538 ARG6, ARG7) NAME,
5539#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5540 ARG6, ARG7, ARG8) NAME,
10841285
MM
5541#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5542#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5543#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
08291658 5544#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
10a0d495 5545#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
f6a7cffc 5546#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
56a9f6bc
TS
5547 NAME,
5548#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5549 ARG6, ARG7) NAME,
5550#define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5551 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
10841285
MM
5552#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5553#include "builtin-types.def"
5554#undef DEF_PRIMITIVE_TYPE
5555#undef DEF_FUNCTION_TYPE_0
5556#undef DEF_FUNCTION_TYPE_1
5557#undef DEF_FUNCTION_TYPE_2
5558#undef DEF_FUNCTION_TYPE_3
5559#undef DEF_FUNCTION_TYPE_4
10a0d495
JJ
5560#undef DEF_FUNCTION_TYPE_5
5561#undef DEF_FUNCTION_TYPE_6
a0274e3e 5562#undef DEF_FUNCTION_TYPE_7
acf0174b 5563#undef DEF_FUNCTION_TYPE_8
10841285
MM
5564#undef DEF_FUNCTION_TYPE_VAR_0
5565#undef DEF_FUNCTION_TYPE_VAR_1
5566#undef DEF_FUNCTION_TYPE_VAR_2
08291658 5567#undef DEF_FUNCTION_TYPE_VAR_3
10a0d495
JJ
5568#undef DEF_FUNCTION_TYPE_VAR_4
5569#undef DEF_FUNCTION_TYPE_VAR_5
56a9f6bc
TS
5570#undef DEF_FUNCTION_TYPE_VAR_7
5571#undef DEF_FUNCTION_TYPE_VAR_11
10841285 5572#undef DEF_POINTER_TYPE
a0274e3e
JJ
5573 BT_LAST
5574};
5575
5576typedef enum c_builtin_type builtin_type;
10841285 5577
a0274e3e
JJ
5578/* A temporary array for c_common_nodes_and_builtins. Used in
5579 communication with def_fn_type. */
5580static tree builtin_types[(int) BT_LAST + 1];
10841285 5581
a0274e3e
JJ
5582/* A helper function for c_common_nodes_and_builtins. Build function type
5583 for DEF with return type RET and N arguments. If VAR is true, then the
5584 function should be variadic after those N arguments.
5585
5586 Takes special care not to ICE if any of the types involved are
5587 error_mark_node, which indicates that said type is not in fact available
5588 (see builtin_type_for_size). In which case the function type as a whole
5589 should be error_mark_node. */
5590
5591static void
5592def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5593{
8242dd04
NF
5594 tree t;
5595 tree *args = XALLOCAVEC (tree, n);
a0274e3e
JJ
5596 va_list list;
5597 int i;
5598
5599 va_start (list, n);
5600 for (i = 0; i < n; ++i)
5601 {
d75d71e0 5602 builtin_type a = (builtin_type) va_arg (list, int);
a0274e3e
JJ
5603 t = builtin_types[a];
5604 if (t == error_mark_node)
5605 goto egress;
8242dd04 5606 args[i] = t;
a0274e3e 5607 }
a0274e3e 5608
a0274e3e
JJ
5609 t = builtin_types[ret];
5610 if (t == error_mark_node)
5611 goto egress;
8242dd04
NF
5612 if (var)
5613 t = build_varargs_function_type_array (t, n, args);
5614 else
5615 t = build_function_type_array (t, n, args);
a0274e3e
JJ
5616
5617 egress:
5618 builtin_types[def] = t;
0edf1bb2 5619 va_end (list);
a0274e3e
JJ
5620}
5621
c6d86fce
ILT
5622/* Build builtin functions common to both C and C++ language
5623 frontends. */
5624
5625static void
5626c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5627{
5628#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5629 builtin_types[ENUM] = VALUE;
5630#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5631 def_fn_type (ENUM, RETURN, 0, 0);
5632#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5633 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5634#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5635 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5636#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5637 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5638#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5639 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5640#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5641 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5642#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5643 ARG6) \
5644 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5645#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5646 ARG6, ARG7) \
5647 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
acf0174b
JJ
5648#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5649 ARG6, ARG7, ARG8) \
5650 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5651 ARG7, ARG8);
c6d86fce
ILT
5652#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5653 def_fn_type (ENUM, RETURN, 1, 0);
5654#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5655 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5656#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5657 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5658#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5659 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5660#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5661 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5662#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5663 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
56a9f6bc
TS
5664#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5665 ARG6, ARG7) \
5666 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5667#define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5668 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5669 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5670 ARG7, ARG8, ARG9, ARG10, ARG11);
c6d86fce
ILT
5671#define DEF_POINTER_TYPE(ENUM, TYPE) \
5672 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5673
5674#include "builtin-types.def"
5675
5676#undef DEF_PRIMITIVE_TYPE
f6a7cffc 5677#undef DEF_FUNCTION_TYPE_0
c6d86fce
ILT
5678#undef DEF_FUNCTION_TYPE_1
5679#undef DEF_FUNCTION_TYPE_2
5680#undef DEF_FUNCTION_TYPE_3
5681#undef DEF_FUNCTION_TYPE_4
5682#undef DEF_FUNCTION_TYPE_5
5683#undef DEF_FUNCTION_TYPE_6
f6a7cffc
TS
5684#undef DEF_FUNCTION_TYPE_7
5685#undef DEF_FUNCTION_TYPE_8
c6d86fce
ILT
5686#undef DEF_FUNCTION_TYPE_VAR_0
5687#undef DEF_FUNCTION_TYPE_VAR_1
5688#undef DEF_FUNCTION_TYPE_VAR_2
5689#undef DEF_FUNCTION_TYPE_VAR_3
5690#undef DEF_FUNCTION_TYPE_VAR_4
5691#undef DEF_FUNCTION_TYPE_VAR_5
56a9f6bc
TS
5692#undef DEF_FUNCTION_TYPE_VAR_7
5693#undef DEF_FUNCTION_TYPE_VAR_11
c6d86fce
ILT
5694#undef DEF_POINTER_TYPE
5695 builtin_types[(int) BT_LAST] = NULL_TREE;
5696
5697 c_init_attributes ();
5698
5699#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5700 NONANSI_P, ATTRS, IMPLICIT, COND) \
5701 if (NAME && COND) \
5702 def_builtin_1 (ENUM, NAME, CLASS, \
5703 builtin_types[(int) TYPE], \
5704 builtin_types[(int) LIBTYPE], \
5705 BOTH_P, FALLBACK_P, NONANSI_P, \
5706 built_in_attributes[(int) ATTRS], IMPLICIT);
5707#include "builtins.def"
5708#undef DEF_BUILTIN
5709
8de7ef2a
UB
5710 targetm.init_builtins ();
5711
384c400a 5712 build_common_builtin_nodes ();
939b37da 5713
b72271b9 5714 if (flag_cilkplus)
939b37da 5715 cilk_init_builtins ();
c6d86fce
ILT
5716}
5717
c1b61fca
JM
5718/* Like get_identifier, but avoid warnings about null arguments when
5719 the argument may be NULL for targets where GCC lacks stdint.h type
5720 information. */
5721
5722static inline tree
5723c_get_ident (const char *id)
5724{
5725 return get_identifier (id);
5726}
5727
a0274e3e
JJ
5728/* Build tree nodes and builtin functions common to both C and C++ language
5729 frontends. */
5730
5731void
5732c_common_nodes_and_builtins (void)
5733{
b6baa67d
KVH
5734 int char16_type_size;
5735 int char32_type_size;
eaa7c03f
JM
5736 int wchar_type_size;
5737 tree array_domain_type;
9f720c3e 5738 tree va_list_ref_type_node;
daf68dd7 5739 tree va_list_arg_type_node;
78a7c317 5740 int i;
d3707adb 5741
1a072294 5742 build_common_tree_nodes (flag_signed_char, flag_short_double);
fce5dddd 5743
eaa7c03f 5744 /* Define `int' and `char' first so that dbx will output them first. */
6496a589 5745 record_builtin_type (RID_INT, NULL, integer_type_node);
eaa7c03f
JM
5746 record_builtin_type (RID_CHAR, "char", char_type_node);
5747
5748 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5749 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5750 but not C. Are the conditionals here needed? */
37fa72e9 5751 if (c_dialect_cxx ())
6496a589 5752 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
eaa7c03f
JM
5753 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5754 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5755 record_builtin_type (RID_MAX, "long unsigned int",
5756 long_unsigned_type_node);
78a7c317
DD
5757
5758 for (i = 0; i < NUM_INT_N_ENTS; i ++)
a6766312 5759 {
78a7c317
DD
5760 char name[25];
5761
5762 sprintf (name, "__int%d", int_n_data[i].bitsize);
17958621 5763 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
78a7c317
DD
5764 int_n_trees[i].signed_type);
5765 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
17958621 5766 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
a6766312 5767 }
78a7c317 5768
37fa72e9 5769 if (c_dialect_cxx ())
eaa7c03f
JM
5770 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5771 record_builtin_type (RID_MAX, "long long int",
5772 long_long_integer_type_node);
5773 record_builtin_type (RID_MAX, "long long unsigned int",
5774 long_long_unsigned_type_node);
37fa72e9 5775 if (c_dialect_cxx ())
eaa7c03f
JM
5776 record_builtin_type (RID_MAX, "long long unsigned",
5777 long_long_unsigned_type_node);
5778 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5779 record_builtin_type (RID_MAX, "short unsigned int",
5780 short_unsigned_type_node);
37fa72e9 5781 if (c_dialect_cxx ())
eaa7c03f
JM
5782 record_builtin_type (RID_MAX, "unsigned short",
5783 short_unsigned_type_node);
5784
5785 /* Define both `signed char' and `unsigned char'. */
5786 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5787 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5788
b0c48229
NB
5789 /* These are types that c_common_type_for_size and
5790 c_common_type_for_mode use. */
c2255bc4
AH
5791 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5792 TYPE_DECL, NULL_TREE,
ae2bcd98 5793 intQI_type_node));
c2255bc4
AH
5794 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5795 TYPE_DECL, NULL_TREE,
ae2bcd98 5796 intHI_type_node));
c2255bc4
AH
5797 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5798 TYPE_DECL, NULL_TREE,
ae2bcd98 5799 intSI_type_node));
c2255bc4
AH
5800 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5801 TYPE_DECL, NULL_TREE,
ae2bcd98 5802 intDI_type_node));
eaa7c03f 5803#if HOST_BITS_PER_WIDE_INT >= 64
78a7c317
DD
5804 /* Note that this is different than the __int128 type that's part of
5805 the generic __intN support. */
1e1b8649 5806 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5807 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5808 TYPE_DECL,
1e1b8649
AP
5809 get_identifier ("__int128_t"),
5810 intTI_type_node));
eaa7c03f 5811#endif
c2255bc4
AH
5812 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5813 TYPE_DECL, NULL_TREE,
ae2bcd98 5814 unsigned_intQI_type_node));
c2255bc4
AH
5815 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5816 TYPE_DECL, NULL_TREE,
ae2bcd98 5817 unsigned_intHI_type_node));
c2255bc4
AH
5818 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5819 TYPE_DECL, NULL_TREE,
ae2bcd98 5820 unsigned_intSI_type_node));
c2255bc4
AH
5821 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5822 TYPE_DECL, NULL_TREE,
ae2bcd98 5823 unsigned_intDI_type_node));
eaa7c03f 5824#if HOST_BITS_PER_WIDE_INT >= 64
1e1b8649 5825 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5826 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5827 TYPE_DECL,
1e1b8649
AP
5828 get_identifier ("__uint128_t"),
5829 unsigned_intTI_type_node));
eaa7c03f
JM
5830#endif
5831
5832 /* Create the widest literal types. */
5833 widest_integer_literal_type_node
5834 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5835 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5836 TYPE_DECL, NULL_TREE,
ae2bcd98 5837 widest_integer_literal_type_node));
eaa7c03f
JM
5838
5839 widest_unsigned_literal_type_node
5840 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5841 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5842 TYPE_DECL, NULL_TREE,
ae2bcd98 5843 widest_unsigned_literal_type_node));
eaa7c03f 5844
c9f8536c 5845 signed_size_type_node = c_common_signed_type (size_type_node);
eaa7c03f 5846
d1c38823
ZD
5847 pid_type_node =
5848 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5849
6496a589
KG
5850 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5851 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
eaa7c03f
JM
5852 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5853
9a8ce21f
JG
5854 /* Only supported decimal floating point extension if the target
5855 actually supports underlying modes. */
b8698a0f 5856 if (targetm.scalar_mode_supported_p (SDmode)
9a8ce21f
JG
5857 && targetm.scalar_mode_supported_p (DDmode)
5858 && targetm.scalar_mode_supported_p (TDmode))
5859 {
5860 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5861 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5862 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5863 }
5864
ab22c1fa
CF
5865 if (targetm.fixed_point_supported_p ())
5866 {
5867 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5868 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5869 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5870 record_builtin_type (RID_MAX, "long long _Fract",
5871 long_long_fract_type_node);
5872 record_builtin_type (RID_MAX, "unsigned short _Fract",
5873 unsigned_short_fract_type_node);
5874 record_builtin_type (RID_MAX, "unsigned _Fract",
5875 unsigned_fract_type_node);
5876 record_builtin_type (RID_MAX, "unsigned long _Fract",
5877 unsigned_long_fract_type_node);
5878 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5879 unsigned_long_long_fract_type_node);
5880 record_builtin_type (RID_MAX, "_Sat short _Fract",
5881 sat_short_fract_type_node);
5882 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5883 record_builtin_type (RID_MAX, "_Sat long _Fract",
5884 sat_long_fract_type_node);
5885 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5886 sat_long_long_fract_type_node);
5887 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5888 sat_unsigned_short_fract_type_node);
5889 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5890 sat_unsigned_fract_type_node);
5891 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5892 sat_unsigned_long_fract_type_node);
5893 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5894 sat_unsigned_long_long_fract_type_node);
5895 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5896 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5897 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5898 record_builtin_type (RID_MAX, "long long _Accum",
5899 long_long_accum_type_node);
5900 record_builtin_type (RID_MAX, "unsigned short _Accum",
5901 unsigned_short_accum_type_node);
5902 record_builtin_type (RID_MAX, "unsigned _Accum",
5903 unsigned_accum_type_node);
5904 record_builtin_type (RID_MAX, "unsigned long _Accum",
5905 unsigned_long_accum_type_node);
5906 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5907 unsigned_long_long_accum_type_node);
5908 record_builtin_type (RID_MAX, "_Sat short _Accum",
5909 sat_short_accum_type_node);
5910 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5911 record_builtin_type (RID_MAX, "_Sat long _Accum",
5912 sat_long_accum_type_node);
5913 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5914 sat_long_long_accum_type_node);
5915 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5916 sat_unsigned_short_accum_type_node);
5917 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5918 sat_unsigned_accum_type_node);
5919 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5920 sat_unsigned_long_accum_type_node);
5921 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5922 sat_unsigned_long_long_accum_type_node);
5923
5924 }
5925
c2255bc4
AH
5926 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5927 TYPE_DECL,
ae2bcd98
RS
5928 get_identifier ("complex int"),
5929 complex_integer_type_node));
c2255bc4
AH
5930 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5931 TYPE_DECL,
ae2bcd98
RS
5932 get_identifier ("complex float"),
5933 complex_float_type_node));
c2255bc4
AH
5934 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5935 TYPE_DECL,
ae2bcd98
RS
5936 get_identifier ("complex double"),
5937 complex_double_type_node));
5938 lang_hooks.decls.pushdecl
c2255bc4
AH
5939 (build_decl (UNKNOWN_LOCATION,
5940 TYPE_DECL, get_identifier ("complex long double"),
43577e6b 5941 complex_long_double_type_node));
eaa7c03f 5942
498c0f27
JJ
5943 if (c_dialect_cxx ())
5944 /* For C++, make fileptr_type_node a distinct void * type until
5945 FILE type is defined. */
8dd16ecc 5946 fileptr_type_node = build_variant_type_copy (ptr_type_node);
498c0f27 5947
6496a589 5948 record_builtin_type (RID_VOID, NULL, void_type_node);
eaa7c03f 5949
06d40de8
DG
5950 /* Set the TYPE_NAME for any variants that were built before
5951 record_builtin_type gave names to the built-in types. */
5952 {
5953 tree void_name = TYPE_NAME (void_type_node);
5954 TYPE_NAME (void_type_node) = NULL_TREE;
5955 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5956 = void_name;
5957 TYPE_NAME (void_type_node) = void_name;
5958 }
5959
eaa7c03f
JM
5960 void_list_node = build_void_list_node ();
5961
5962 /* Make a type to be the domain of a few array types
5963 whose domains don't really matter.
5964 200 is small enough that it always fits in size_t
5965 and large enough that it can hold most function names for the
5966 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5967 array_domain_type = build_index_type (size_int (200));
5968
5969 /* Make a type for arrays of characters.
5970 With luck nothing will ever really depend on the length of this
5971 array type. */
5972 char_array_type_node
5973 = build_array_type (char_type_node, array_domain_type);
5974
10841285
MM
5975 string_type_node = build_pointer_type (char_type_node);
5976 const_string_type_node
5977 = build_pointer_type (build_qualified_type
5978 (char_type_node, TYPE_QUAL_CONST));
5979
eaa7c03f 5980 /* This is special for C++ so functions can be overloaded. */
a11eba95 5981 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
eaa7c03f
JM
5982 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5983 wchar_type_size = TYPE_PRECISION (wchar_type_node);
207bf79d 5984 underlying_wchar_type_node = wchar_type_node;
37fa72e9 5985 if (c_dialect_cxx ())
eaa7c03f 5986 {
8df83eae 5987 if (TYPE_UNSIGNED (wchar_type_node))
eaa7c03f
JM
5988 wchar_type_node = make_unsigned_type (wchar_type_size);
5989 else
5990 wchar_type_node = make_signed_type (wchar_type_size);
5991 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5992 }
eaa7c03f
JM
5993
5994 /* This is for wide string constants. */
5995 wchar_array_type_node
5996 = build_array_type (wchar_type_node, array_domain_type);
5997
b6baa67d
KVH
5998 /* Define 'char16_t'. */
5999 char16_type_node = get_identifier (CHAR16_TYPE);
6000 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
6001 char16_type_size = TYPE_PRECISION (char16_type_node);
6002 if (c_dialect_cxx ())
6003 {
6004 char16_type_node = make_unsigned_type (char16_type_size);
6005
604b2bfc 6006 if (cxx_dialect >= cxx11)
b6baa67d
KVH
6007 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
6008 }
6009
6010 /* This is for UTF-16 string constants. */
6011 char16_array_type_node
6012 = build_array_type (char16_type_node, array_domain_type);
6013
6014 /* Define 'char32_t'. */
6015 char32_type_node = get_identifier (CHAR32_TYPE);
6016 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
6017 char32_type_size = TYPE_PRECISION (char32_type_node);
6018 if (c_dialect_cxx ())
6019 {
6020 char32_type_node = make_unsigned_type (char32_type_size);
6021
604b2bfc 6022 if (cxx_dialect >= cxx11)
b6baa67d
KVH
6023 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
6024 }
6025
6026 /* This is for UTF-32 string constants. */
6027 char32_array_type_node
6028 = build_array_type (char32_type_node, array_domain_type);
6029
5fd8e536
JM
6030 wint_type_node =
6031 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
6032
6033 intmax_type_node =
6034 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
6035 uintmax_type_node =
6036 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
6037
207bf79d
JM
6038 if (SIG_ATOMIC_TYPE)
6039 sig_atomic_type_node =
c1b61fca 6040 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
207bf79d
JM
6041 if (INT8_TYPE)
6042 int8_type_node =
c1b61fca 6043 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
207bf79d
JM
6044 if (INT16_TYPE)
6045 int16_type_node =
c1b61fca 6046 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
207bf79d
JM
6047 if (INT32_TYPE)
6048 int32_type_node =
c1b61fca 6049 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
207bf79d
JM
6050 if (INT64_TYPE)
6051 int64_type_node =
c1b61fca 6052 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
207bf79d
JM
6053 if (UINT8_TYPE)
6054 uint8_type_node =
c1b61fca 6055 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
207bf79d 6056 if (UINT16_TYPE)
cca615af 6057 c_uint16_type_node = uint16_type_node =
c1b61fca 6058 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
207bf79d 6059 if (UINT32_TYPE)
cca615af 6060 c_uint32_type_node = uint32_type_node =
c1b61fca 6061 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
207bf79d 6062 if (UINT64_TYPE)
cca615af 6063 c_uint64_type_node = uint64_type_node =
c1b61fca 6064 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
207bf79d
JM
6065 if (INT_LEAST8_TYPE)
6066 int_least8_type_node =
c1b61fca 6067 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
207bf79d
JM
6068 if (INT_LEAST16_TYPE)
6069 int_least16_type_node =
c1b61fca 6070 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
207bf79d
JM
6071 if (INT_LEAST32_TYPE)
6072 int_least32_type_node =
c1b61fca 6073 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
207bf79d
JM
6074 if (INT_LEAST64_TYPE)
6075 int_least64_type_node =
c1b61fca 6076 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
207bf79d
JM
6077 if (UINT_LEAST8_TYPE)
6078 uint_least8_type_node =
c1b61fca 6079 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
207bf79d
JM
6080 if (UINT_LEAST16_TYPE)
6081 uint_least16_type_node =
c1b61fca 6082 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
207bf79d
JM
6083 if (UINT_LEAST32_TYPE)
6084 uint_least32_type_node =
c1b61fca 6085 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
207bf79d
JM
6086 if (UINT_LEAST64_TYPE)
6087 uint_least64_type_node =
c1b61fca 6088 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
207bf79d
JM
6089 if (INT_FAST8_TYPE)
6090 int_fast8_type_node =
c1b61fca 6091 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
207bf79d
JM
6092 if (INT_FAST16_TYPE)
6093 int_fast16_type_node =
c1b61fca 6094 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
207bf79d
JM
6095 if (INT_FAST32_TYPE)
6096 int_fast32_type_node =
c1b61fca 6097 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
207bf79d
JM
6098 if (INT_FAST64_TYPE)
6099 int_fast64_type_node =
c1b61fca 6100 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
207bf79d
JM
6101 if (UINT_FAST8_TYPE)
6102 uint_fast8_type_node =
c1b61fca 6103 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
207bf79d
JM
6104 if (UINT_FAST16_TYPE)
6105 uint_fast16_type_node =
c1b61fca 6106 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
207bf79d
JM
6107 if (UINT_FAST32_TYPE)
6108 uint_fast32_type_node =
c1b61fca 6109 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
207bf79d
JM
6110 if (UINT_FAST64_TYPE)
6111 uint_fast64_type_node =
c1b61fca 6112 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
207bf79d
JM
6113 if (INTPTR_TYPE)
6114 intptr_type_node =
c1b61fca 6115 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
207bf79d
JM
6116 if (UINTPTR_TYPE)
6117 uintptr_type_node =
c1b61fca 6118 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
207bf79d 6119
8242dd04
NF
6120 default_function_type
6121 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5fd8e536
JM
6122 ptrdiff_type_node
6123 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
12753674 6124 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5fd8e536 6125
ae2bcd98 6126 lang_hooks.decls.pushdecl
c2255bc4
AH
6127 (build_decl (UNKNOWN_LOCATION,
6128 TYPE_DECL, get_identifier ("__builtin_va_list"),
43577e6b 6129 va_list_type_node));
38f8b050 6130 if (targetm.enum_va_list_p)
d4048208
KT
6131 {
6132 int l;
6133 const char *pname;
6134 tree ptype;
35cbb299 6135
38f8b050 6136 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
d4048208
KT
6137 {
6138 lang_hooks.decls.pushdecl
6139 (build_decl (UNKNOWN_LOCATION,
6140 TYPE_DECL, get_identifier (pname),
6141 ptype));
6142
6143 }
6144 }
daf68dd7 6145
daf68dd7 6146 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
6147 {
6148 va_list_arg_type_node = va_list_ref_type_node =
6149 build_pointer_type (TREE_TYPE (va_list_type_node));
6150 }
daf68dd7 6151 else
9f720c3e
GK
6152 {
6153 va_list_arg_type_node = va_list_type_node;
6154 va_list_ref_type_node = build_reference_type (va_list_type_node);
6155 }
35b1a6fa 6156
c6d86fce
ILT
6157 if (!flag_preprocess_only)
6158 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4677862a 6159
5b47282c 6160 main_identifier_node = get_identifier ("main");
b2f97e4a
MM
6161
6162 /* Create the built-in __null node. It is important that this is
6163 not shared. */
807e902e 6164 null_node = make_int_cst (1, 1);
b2f97e4a 6165 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
a0274e3e
JJ
6166
6167 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6168 memset (builtin_types, 0, sizeof (builtin_types));
7f4edbcb 6169}
d3707adb 6170
3b2db49f
MM
6171/* The number of named compound-literals generated thus far. */
6172static GTY(()) int compound_literal_number;
6173
6174/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6175
6176void
6177set_compound_literal_name (tree decl)
6178{
6179 char *name;
6180 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6181 compound_literal_number);
6182 compound_literal_number++;
6183 DECL_NAME (decl) = get_identifier (name);
6184}
6185
2fe1d762
TV
6186/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6187 TYPE and operand OP. */
6188
6189static tree
6190build_va_arg_1 (location_t loc, tree type, tree op)
6191{
6192 tree expr = build1 (VA_ARG_EXPR, type, op);
6193 SET_EXPR_LOCATION (expr, loc);
6194 return expr;
6195}
6196
6197/* Return a VA_ARG_EXPR corresponding to a source-level expression
6198 va_arg (EXPR, TYPE) at source location LOC. */
6199
d3707adb 6200tree
c2255bc4 6201build_va_arg (location_t loc, tree expr, tree type)
d3707adb 6202{
c7b38fd5
TV
6203 tree va_type = TREE_TYPE (expr);
6204 tree canon_va_type = (va_type == error_mark_node
6205 ? NULL_TREE
6206 : targetm.canonical_va_list_type (va_type));
6207
2fe1d762
TV
6208 if (va_type == error_mark_node
6209 || canon_va_type == NULL_TREE)
c7b38fd5 6210 {
2fe1d762
TV
6211 /* Let's handle things neutrallly, if expr:
6212 - has undeclared type, or
6213 - is not an va_list type. */
6214 return build_va_arg_1 (loc, type, expr);
c7b38fd5 6215 }
ecd0e562 6216
2fe1d762
TV
6217 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6218 {
6219 /* Case 1: Not an array type. */
6220
6221 /* Take the address, to get '&ap'. */
6222 mark_addressable (expr);
6223 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6224
6225 /* Verify that &ap is still recognized as having va_list type. */
6226 tree canon_expr_type
6227 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6228 gcc_assert (canon_expr_type != NULL_TREE);
6229
6230 return build_va_arg_1 (loc, type, expr);
6231 }
6232
6233 /* Case 2: Array type.
6234
6235 Background:
6236
6237 For contrast, let's start with the simple case (case 1). If
6238 canon_va_type is not an array type, but say a char *, then when
6239 passing-by-value a va_list, the type of the va_list param decl is
6240 the same as for another va_list decl (all ap's are char *):
6241
6242 f2_1 (char * ap)
6243 D.1815 = VA_ARG (&ap, 0B, 1);
6244 return D.1815;
6245
6246 f2 (int i)
6247 char * ap.0;
6248 char * ap;
6249 __builtin_va_start (&ap, 0);
6250 ap.0 = ap;
6251 res = f2_1 (ap.0);
6252 __builtin_va_end (&ap);
6253 D.1812 = res;
6254 return D.1812;
6255
6256 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6257 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6258 the same as for another va_list decl (case 2a, struct ap[1]).
6259
6260 f2_1 (struct * ap)
6261 D.1844 = VA_ARG (ap, 0B, 0);
6262 return D.1844;
6263
6264 f2 (int i)
6265 struct ap[1];
6266 __builtin_va_start (&ap, 0);
6267 res = f2_1 (&ap);
6268 __builtin_va_end (&ap);
6269 D.1841 = res;
6270 return D.1841;
6271
6272 Case 2b is different because:
6273 - on the callee side, the parm decl has declared type va_list, but
6274 grokdeclarator changes the type of the parm decl to a pointer to the
6275 array elem type.
6276 - on the caller side, the pass-by-value uses &ap.
6277
6278 We unify these two cases (case 2a: va_list is array type,
6279 case 2b: va_list is pointer to array elem type), by adding '&' for the
6280 array type case, such that we have a pointer to array elem in both
6281 cases. */
6282
6283 if (TREE_CODE (va_type) == ARRAY_TYPE)
6284 {
6285 /* Case 2a: va_list is array type. */
6286
6287 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6288 elem type. */
6289 mark_addressable (expr);
6290 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6291 expr);
6292
6293 /* Verify that &ap is still recognized as having va_list type. */
6294 tree canon_expr_type
6295 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6296 gcc_assert (canon_expr_type != NULL_TREE);
6297 }
6298 else
6299 {
6300 /* Case 2b: va_list is pointer to array elem type. */
6301 gcc_assert (POINTER_TYPE_P (va_type));
6302 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6303
6304 /* Don't take the address. We've already got '&ap'. */
6305 ;
6306 }
6307
6308 return build_va_arg_1 (loc, type, expr);
d3707adb 6309}
fc2aaf30
JM
6310
6311
7d14c755
JM
6312/* Linked list of disabled built-in functions. */
6313
a79683d5 6314struct disabled_builtin
7d14c755
JM
6315{
6316 const char *name;
6317 struct disabled_builtin *next;
a79683d5 6318};
7d14c755
JM
6319static disabled_builtin *disabled_builtins = NULL;
6320
35b1a6fa 6321static bool builtin_function_disabled_p (const char *);
7d14c755
JM
6322
6323/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6324 begins with "__builtin_", give an error. */
6325
6326void
35b1a6fa 6327disable_builtin_function (const char *name)
7d14c755
JM
6328{
6329 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
bda67431 6330 error ("cannot disable built-in function %qs", name);
7d14c755
JM
6331 else
6332 {
1ad463f4
BI
6333 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6334 new_disabled_builtin->name = name;
6335 new_disabled_builtin->next = disabled_builtins;
6336 disabled_builtins = new_disabled_builtin;
7d14c755
JM
6337 }
6338}
6339
6340
6341/* Return true if the built-in function NAME has been disabled, false
6342 otherwise. */
6343
6344static bool
35b1a6fa 6345builtin_function_disabled_p (const char *name)
7d14c755
JM
6346{
6347 disabled_builtin *p;
6348 for (p = disabled_builtins; p != NULL; p = p->next)
6349 {
6350 if (strcmp (name, p->name) == 0)
6351 return true;
6352 }
6353 return false;
6354}
6355
6356
9bc15050
RG
6357/* Worker for DEF_BUILTIN.
6358 Possibly define a builtin function with one or two names.
6359 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6360 nonansi_p and flag_no_nonansi_builtin. */
fc2aaf30 6361
9bc15050
RG
6362static void
6363def_builtin_1 (enum built_in_function fncode,
6364 const char *name,
6365 enum built_in_class fnclass,
6366 tree fntype, tree libtype,
6367 bool both_p, bool fallback_p, bool nonansi_p,
6368 tree fnattrs, bool implicit_p)
fc2aaf30 6369{
9bc15050
RG
6370 tree decl;
6371 const char *libname;
6372
a0274e3e
JJ
6373 if (fntype == error_mark_node)
6374 return;
6375
9bc15050
RG
6376 gcc_assert ((!both_p && !fallback_p)
6377 || !strncmp (name, "__builtin_",
6378 strlen ("__builtin_")));
6379
6380 libname = name + strlen ("__builtin_");
c79efc4d
RÁE
6381 decl = add_builtin_function (name, fntype, fncode, fnclass,
6382 (fallback_p ? libname : NULL),
6383 fnattrs);
e79983f4
MM
6384
6385 set_builtin_decl (fncode, decl, implicit_p);
6386
9bc15050
RG
6387 if (both_p
6388 && !flag_no_builtin && !builtin_function_disabled_p (libname)
7d14c755 6389 && !(nonansi_p && flag_no_nonansi_builtin))
c79efc4d
RÁE
6390 add_builtin_function (libname, libtype, fncode, fnclass,
6391 NULL, fnattrs);
fc2aaf30 6392}
c530479e 6393\f
d72040f5
RH
6394/* Nonzero if the type T promotes to int. This is (nearly) the
6395 integral promotions defined in ISO C99 6.3.1.1/2. */
6396
6397bool
58f9752a 6398c_promoting_integer_type_p (const_tree t)
d72040f5
RH
6399{
6400 switch (TREE_CODE (t))
6401 {
6402 case INTEGER_TYPE:
6403 return (TYPE_MAIN_VARIANT (t) == char_type_node
6404 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6405 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6406 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
c6c04fca
RL
6407 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6408 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d72040f5
RH
6409
6410 case ENUMERAL_TYPE:
6411 /* ??? Technically all enumerations not larger than an int
6412 promote to an int. But this is used along code paths
6413 that only want to notice a size change. */
6414 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6415
6416 case BOOLEAN_TYPE:
6417 return 1;
6418
6419 default:
6420 return 0;
6421 }
6422}
6423
c530479e
RH
6424/* Return 1 if PARMS specifies a fixed number of parameters
6425 and none of their types is affected by default promotions. */
6426
6427int
58f9752a 6428self_promoting_args_p (const_tree parms)
c530479e 6429{
58f9752a 6430 const_tree t;
c530479e
RH
6431 for (t = parms; t; t = TREE_CHAIN (t))
6432 {
b3694847 6433 tree type = TREE_VALUE (t);
7e8176d7 6434
694fea20
VR
6435 if (type == error_mark_node)
6436 continue;
6437
c530479e
RH
6438 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6439 return 0;
6440
6441 if (type == 0)
6442 return 0;
6443
6444 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6445 return 0;
6446
d72040f5 6447 if (c_promoting_integer_type_p (type))
c530479e
RH
6448 return 0;
6449 }
6450 return 1;
6451}
5eda3d66 6452
12ea3302
GDR
6453/* Recursively remove any '*' or '&' operator from TYPE. */
6454tree
6455strip_pointer_operator (tree t)
6456{
6457 while (POINTER_TYPE_P (t))
6458 t = TREE_TYPE (t);
6459 return t;
6460}
6461
ba992967
SP
6462/* Recursively remove pointer or array type from TYPE. */
6463tree
6464strip_pointer_or_array_types (tree t)
6465{
6466 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6467 t = TREE_TYPE (t);
6468 return t;
6469}
6470
8f17b5c5
MM
6471/* Used to compare case labels. K1 and K2 are actually tree nodes
6472 representing case labels, or NULL_TREE for a `default' label.
6473 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6474 K2, and 0 if K1 and K2 are equal. */
6475
6476int
35b1a6fa 6477case_compare (splay_tree_key k1, splay_tree_key k2)
8f17b5c5
MM
6478{
6479 /* Consider a NULL key (such as arises with a `default' label) to be
6480 smaller than anything else. */
6481 if (!k1)
6482 return k2 ? -1 : 0;
6483 else if (!k2)
6484 return k1 ? 1 : 0;
6485
6486 return tree_int_cst_compare ((tree) k1, (tree) k2);
6487}
6488
c2255bc4
AH
6489/* Process a case label, located at LOC, for the range LOW_VALUE
6490 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6491 then this case label is actually a `default' label. If only
6492 HIGH_VALUE is NULL_TREE, then case label was declared using the
6493 usual C/C++ syntax, rather than the GNU case range extension.
6494 CASES is a tree containing all the case ranges processed so far;
b155cfd9
MP
6495 COND is the condition for the switch-statement itself.
6496 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6497 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6498 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
8f17b5c5
MM
6499
6500tree
c2255bc4 6501c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
b155cfd9 6502 tree low_value, tree high_value, bool *outside_range_p)
8f17b5c5
MM
6503{
6504 tree type;
6505 tree label;
6506 tree case_label;
6507 splay_tree_node node;
6508
6509 /* Create the LABEL_DECL itself. */
c2255bc4 6510 label = create_artificial_label (loc);
8f17b5c5
MM
6511
6512 /* If there was an error processing the switch condition, bail now
6513 before we get more confused. */
6514 if (!cond || cond == error_mark_node)
6de9cd9a 6515 goto error_out;
8f17b5c5 6516
35b1a6fa
AJ
6517 if ((low_value && TREE_TYPE (low_value)
6518 && POINTER_TYPE_P (TREE_TYPE (low_value)))
8f17b5c5
MM
6519 || (high_value && TREE_TYPE (high_value)
6520 && POINTER_TYPE_P (TREE_TYPE (high_value))))
522ddfa2 6521 {
c2255bc4 6522 error_at (loc, "pointers are not permitted as case values");
522ddfa2
JM
6523 goto error_out;
6524 }
8f17b5c5
MM
6525
6526 /* Case ranges are a GNU extension. */
fcf73884 6527 if (high_value)
c1771a20 6528 pedwarn (loc, OPT_Wpedantic,
fcf73884 6529 "range expressions in switch statements are non-standard");
8f17b5c5
MM
6530
6531 type = TREE_TYPE (cond);
6532 if (low_value)
6533 {
62e4eb35 6534 low_value = check_case_value (loc, low_value);
68fca595 6535 low_value = convert_and_check (loc, type, low_value);
c0e22534
NS
6536 if (low_value == error_mark_node)
6537 goto error_out;
8f17b5c5
MM
6538 }
6539 if (high_value)
6540 {
62e4eb35 6541 high_value = check_case_value (loc, high_value);
68fca595 6542 high_value = convert_and_check (loc, type, high_value);
c0e22534
NS
6543 if (high_value == error_mark_node)
6544 goto error_out;
8f17b5c5
MM
6545 }
6546
c0e22534
NS
6547 if (low_value && high_value)
6548 {
6549 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
c22cacf3
MS
6550 really a case range, even though it was written that way.
6551 Remove the HIGH_VALUE to simplify later processing. */
c0e22534
NS
6552 if (tree_int_cst_equal (low_value, high_value))
6553 high_value = NULL_TREE;
6554 else if (!tree_int_cst_lt (low_value, high_value))
c2255bc4 6555 warning_at (loc, 0, "empty range specified");
c0e22534 6556 }
8f17b5c5 6557
a6c0a76c
SB
6558 /* See if the case is in range of the type of the original testing
6559 expression. If both low_value and high_value are out of range,
6560 don't insert the case label and return NULL_TREE. */
6561 if (low_value
9d548dfb 6562 && !check_case_bounds (loc, type, orig_type,
b155cfd9
MP
6563 &low_value, high_value ? &high_value : NULL,
6564 outside_range_p))
a6c0a76c
SB
6565 return NULL_TREE;
6566
8f17b5c5
MM
6567 /* Look up the LOW_VALUE in the table of case labels we already
6568 have. */
6569 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6570 /* If there was not an exact match, check for overlapping ranges.
6571 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6572 that's a `default' label and the only overlap is an exact match. */
6573 if (!node && (low_value || high_value))
6574 {
6575 splay_tree_node low_bound;
6576 splay_tree_node high_bound;
6577
6578 /* Even though there wasn't an exact match, there might be an
6579 overlap between this case range and another case range.
6580 Since we've (inductively) not allowed any overlapping case
6581 ranges, we simply need to find the greatest low case label
6582 that is smaller that LOW_VALUE, and the smallest low case
6583 label that is greater than LOW_VALUE. If there is an overlap
6584 it will occur in one of these two ranges. */
6585 low_bound = splay_tree_predecessor (cases,
6586 (splay_tree_key) low_value);
6587 high_bound = splay_tree_successor (cases,
6588 (splay_tree_key) low_value);
6589
6590 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6591 the LOW_VALUE, so there is no need to check unless the
6592 LOW_BOUND is in fact itself a case range. */
6593 if (low_bound
6594 && CASE_HIGH ((tree) low_bound->value)
6595 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6596 low_value) >= 0)
6597 node = low_bound;
6598 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6599 range is bigger than the low end of the current range, so we
6600 are only interested if the current range is a real range, and
6601 not an ordinary case label. */
35b1a6fa 6602 else if (high_bound
8f17b5c5
MM
6603 && high_value
6604 && (tree_int_cst_compare ((tree) high_bound->key,
6605 high_value)
6606 <= 0))
6607 node = high_bound;
6608 }
6609 /* If there was an overlap, issue an error. */
6610 if (node)
6611 {
8c161995 6612 tree duplicate = CASE_LABEL ((tree) node->value);
8f17b5c5
MM
6613
6614 if (high_value)
6615 {
c2255bc4
AH
6616 error_at (loc, "duplicate (or overlapping) case value");
6617 error_at (DECL_SOURCE_LOCATION (duplicate),
6618 "this is the first entry overlapping that value");
8f17b5c5
MM
6619 }
6620 else if (low_value)
6621 {
c2255bc4
AH
6622 error_at (loc, "duplicate case value") ;
6623 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
8f17b5c5
MM
6624 }
6625 else
6626 {
c2255bc4
AH
6627 error_at (loc, "multiple default labels in one switch");
6628 error_at (DECL_SOURCE_LOCATION (duplicate),
6629 "this is the first default label");
8f17b5c5 6630 }
6de9cd9a 6631 goto error_out;
8f17b5c5
MM
6632 }
6633
6634 /* Add a CASE_LABEL to the statement-tree. */
3d528853 6635 case_label = add_stmt (build_case_label (low_value, high_value, label));
8f17b5c5 6636 /* Register this case label in the splay tree. */
35b1a6fa 6637 splay_tree_insert (cases,
8f17b5c5
MM
6638 (splay_tree_key) low_value,
6639 (splay_tree_value) case_label);
6640
6641 return case_label;
6de9cd9a
DN
6642
6643 error_out:
9e14e18f 6644 /* Add a label so that the back-end doesn't think that the beginning of
6de9cd9a 6645 the switch is unreachable. Note that we do not add a case label, as
41806d92 6646 that just leads to duplicates and thence to failure later on. */
6de9cd9a
DN
6647 if (!cases->root)
6648 {
c2255bc4
AH
6649 tree t = create_artificial_label (loc);
6650 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6de9cd9a
DN
6651 }
6652 return error_mark_node;
6653}
6654
6655/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6656 Used to verify that case values match up with enumerator values. */
6657
6658static void
6659match_case_to_enum_1 (tree key, tree type, tree label)
6660{
807e902e
KZ
6661 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6662
6663 if (tree_fits_uhwi_p (key))
6664 print_dec (key, buf, UNSIGNED);
6665 else if (tree_fits_shwi_p (key))
6666 print_dec (key, buf, SIGNED);
6de9cd9a 6667 else
807e902e 6668 print_hex (key, buf);
6de9cd9a
DN
6669
6670 if (TYPE_NAME (type) == 0)
c5d75364
MLI
6671 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6672 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6673 "case value %qs not in enumerated type",
6674 buf);
6de9cd9a 6675 else
c5d75364
MLI
6676 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6677 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6678 "case value %qs not in enumerated type %qT",
6679 buf, type);
6de9cd9a
DN
6680}
6681
c782c2fe
RS
6682/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6683 Used to verify that case values match up with enumerator values. */
6684
6de9cd9a
DN
6685static int
6686match_case_to_enum (splay_tree_node node, void *data)
6687{
6688 tree label = (tree) node->value;
28dab132 6689 tree type = (tree) data;
6de9cd9a
DN
6690
6691 /* Skip default case. */
6692 if (!CASE_LOW (label))
6693 return 0;
6694
c782c2fe 6695 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6de9cd9a 6696 when we did our enum->case scan. Reset our scratch bit after. */
c782c2fe 6697 if (!CASE_LOW_SEEN (label))
6de9cd9a
DN
6698 match_case_to_enum_1 (CASE_LOW (label), type, label);
6699 else
c782c2fe 6700 CASE_LOW_SEEN (label) = 0;
6de9cd9a 6701
c782c2fe
RS
6702 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6703 not set, that means that CASE_HIGH did not appear when we did our
6704 enum->case scan. Reset our scratch bit after. */
6de9cd9a
DN
6705 if (CASE_HIGH (label))
6706 {
c782c2fe
RS
6707 if (!CASE_HIGH_SEEN (label))
6708 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6709 else
6710 CASE_HIGH_SEEN (label) = 0;
6de9cd9a
DN
6711 }
6712
6713 return 0;
6714}
6715
fbc315db
ILT
6716/* Handle -Wswitch*. Called from the front end after parsing the
6717 switch construct. */
6718/* ??? Should probably be somewhere generic, since other languages
6719 besides C and C++ would want this. At the moment, however, C/C++
6720 are the only tree-ssa languages that support enumerations at all,
6721 so the point is moot. */
6de9cd9a 6722
fbc315db
ILT
6723void
6724c_do_switch_warnings (splay_tree cases, location_t switch_location,
b155cfd9
MP
6725 tree type, tree cond, bool bool_cond_p,
6726 bool outside_range_p)
6de9cd9a 6727{
9f63daea 6728 splay_tree_node default_node;
c782c2fe
RS
6729 splay_tree_node node;
6730 tree chain;
6de9cd9a 6731
b155cfd9
MP
6732 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6733 && !warn_switch_bool)
6de9cd9a
DN
6734 return;
6735
6de9cd9a 6736 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
44c21c7f 6737 if (!default_node)
fab922b1
MLI
6738 warning_at (switch_location, OPT_Wswitch_default,
6739 "switch missing default case");
6de9cd9a 6740
b155cfd9
MP
6741 /* There are certain cases where -Wswitch-bool warnings aren't
6742 desirable, such as
6743 switch (boolean)
6744 {
6745 case true: ...
6746 case false: ...
6747 }
6748 so be careful here. */
6749 if (warn_switch_bool && bool_cond_p)
6750 {
6751 splay_tree_node min_node;
6752 /* If there's a default node, it's also the value with the minimal
6753 key. So look at the penultimate key (if any). */
6754 if (default_node)
6755 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6756 else
6757 min_node = splay_tree_min (cases);
6758 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6759
6760 splay_tree_node max_node = splay_tree_max (cases);
6761 /* This might be a case range, so look at the value with the
6762 maximal key and then check CASE_HIGH. */
6763 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6764 if (max)
6765 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6766
6767 /* If there's a case value > 1 or < 0, that is outside bool
6768 range, warn. */
6769 if (outside_range_p
6770 || (max && wi::gts_p (max, 1))
6771 || (min && wi::lts_p (min, 0))
6772 /* And handle the
6773 switch (boolean)
6774 {
6775 case true: ...
6776 case false: ...
6777 default: ...
6778 }
6779 case, where we want to warn. */
6780 || (default_node
6781 && max && wi::eq_p (max, 1)
6782 && min && wi::eq_p (min, 0)))
6783 warning_at (switch_location, OPT_Wswitch_bool,
6784 "switch condition has boolean value");
6785 }
6786
026c3cfd 6787 /* From here on, we only care about enumerated types. */
c782c2fe
RS
6788 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6789 return;
6790
cdb88468
ILT
6791 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6792 if (!warn_switch_enum && !warn_switch)
c782c2fe
RS
6793 return;
6794
cdb88468
ILT
6795 /* Check the cases. Warn about case values which are not members of
6796 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6797 there is no default case, check that exactly all enumeration
6798 literals are covered by the cases. */
6799
c782c2fe
RS
6800 /* Clearing COND if it is not an integer constant simplifies
6801 the tests inside the loop below. */
6802 if (TREE_CODE (cond) != INTEGER_CST)
6803 cond = NULL_TREE;
6804
6805 /* The time complexity here is O(N*lg(N)) worst case, but for the
6806 common case of monotonically increasing enumerators, it is
6807 O(N), since the nature of the splay tree will keep the next
6808 element adjacent to the root at all times. */
6de9cd9a 6809
c782c2fe
RS
6810 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6811 {
6812 tree value = TREE_VALUE (chain);
adf2edec
DG
6813 if (TREE_CODE (value) == CONST_DECL)
6814 value = DECL_INITIAL (value);
c782c2fe
RS
6815 node = splay_tree_lookup (cases, (splay_tree_key) value);
6816 if (node)
6de9cd9a 6817 {
c782c2fe
RS
6818 /* Mark the CASE_LOW part of the case entry as seen. */
6819 tree label = (tree) node->value;
6820 CASE_LOW_SEEN (label) = 1;
6821 continue;
6822 }
6823
6824 /* Even though there wasn't an exact match, there might be a
fa10beec 6825 case range which includes the enumerator's value. */
c782c2fe
RS
6826 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6827 if (node && CASE_HIGH ((tree) node->value))
6828 {
6829 tree label = (tree) node->value;
6830 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6831 if (cmp >= 0)
6de9cd9a 6832 {
c782c2fe
RS
6833 /* If we match the upper bound exactly, mark the CASE_HIGH
6834 part of the case entry as seen. */
6835 if (cmp == 0)
6836 CASE_HIGH_SEEN (label) = 1;
6837 continue;
6de9cd9a
DN
6838 }
6839 }
6840
c782c2fe
RS
6841 /* We've now determined that this enumerated literal isn't
6842 handled by the case labels of the switch statement. */
6de9cd9a 6843
c782c2fe
RS
6844 /* If the switch expression is a constant, we only really care
6845 about whether that constant is handled by the switch. */
6846 if (cond && tree_int_cst_compare (cond, value))
6847 continue;
6de9cd9a 6848
683d6ff9 6849 /* If there is a default_node, the only relevant option is
cdb88468 6850 Wswitch-enum. Otherwise, if both are enabled then we prefer
683d6ff9
MLI
6851 to warn using -Wswitch because -Wswitch is enabled by -Wall
6852 while -Wswitch-enum is explicit. */
cdb88468
ILT
6853 warning_at (switch_location,
6854 (default_node || !warn_switch
6855 ? OPT_Wswitch_enum
6856 : OPT_Wswitch),
6857 "enumeration value %qE not handled in switch",
6858 TREE_PURPOSE (chain));
6de9cd9a 6859 }
c782c2fe
RS
6860
6861 /* Warn if there are case expressions that don't correspond to
6862 enumerators. This can occur since C and C++ don't enforce
6863 type-checking of assignments to enumeration variables.
6864
6865 The time complexity here is now always O(N) worst case, since
6866 we should have marked both the lower bound and upper bound of
6867 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6868 above. This scan also resets those fields. */
683d6ff9 6869
c782c2fe 6870 splay_tree_foreach (cases, match_case_to_enum, type);
8f17b5c5
MM
6871}
6872
6b665219 6873/* Finish an expression taking the address of LABEL (an
6a3799eb
AH
6874 IDENTIFIER_NODE). Returns an expression for the address.
6875
6876 LOC is the location for the expression returned. */
15b732b2 6877
35b1a6fa 6878tree
6a3799eb 6879finish_label_address_expr (tree label, location_t loc)
15b732b2
NB
6880{
6881 tree result;
6882
c1771a20 6883 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
15b732b2 6884
6b665219
MM
6885 if (label == error_mark_node)
6886 return error_mark_node;
6887
15b732b2
NB
6888 label = lookup_label (label);
6889 if (label == NULL_TREE)
6890 result = null_pointer_node;
6891 else
6892 {
6893 TREE_USED (label) = 1;
6894 result = build1 (ADDR_EXPR, ptr_type_node, label);
5805e48d 6895 /* The current function is not necessarily uninlinable.
15b732b2
NB
6896 Computed gotos are incompatible with inlining, but the value
6897 here could be used only in a diagnostic, for example. */
6a3799eb 6898 protected_set_expr_location (result, loc);
15b732b2
NB
6899 }
6900
6901 return result;
6902}
19552aa5
JM
6903\f
6904
6905/* Given a boolean expression ARG, return a tree representing an increment
6906 or decrement (as indicated by CODE) of ARG. The front end must check for
6907 invalid cases (e.g., decrement in C++). */
6908tree
35b1a6fa 6909boolean_increment (enum tree_code code, tree arg)
19552aa5
JM
6910{
6911 tree val;
b5119fa1 6912 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
37fa72e9 6913
19552aa5
JM
6914 arg = stabilize_reference (arg);
6915 switch (code)
6916 {
6917 case PREINCREMENT_EXPR:
53fb4de3 6918 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5
JM
6919 break;
6920 case POSTINCREMENT_EXPR:
53fb4de3 6921 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5 6922 arg = save_expr (arg);
53fb4de3
RS
6923 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6924 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6925 break;
6926 case PREDECREMENT_EXPR:
53fb4de3 6927 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6928 invert_truthvalue_loc (input_location, arg));
19552aa5
JM
6929 break;
6930 case POSTDECREMENT_EXPR:
53fb4de3 6931 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6932 invert_truthvalue_loc (input_location, arg));
19552aa5 6933 arg = save_expr (arg);
53fb4de3
RS
6934 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6935 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6936 break;
6937 default:
366de0ce 6938 gcc_unreachable ();
19552aa5
JM
6939 }
6940 TREE_SIDE_EFFECTS (val) = 1;
6941 return val;
6942}
03dc0325 6943\f
207bf79d
JM
6944/* Built-in macros for stddef.h and stdint.h, that require macros
6945 defined in this file. */
460bd0e3 6946void
35b1a6fa 6947c_stddef_cpp_builtins(void)
3df89291 6948{
5279d739
ZW
6949 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6950 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6951 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6952 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
85291069
JM
6953 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6954 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
c466b2cd
KVH
6955 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6956 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
207bf79d
JM
6957 if (SIG_ATOMIC_TYPE)
6958 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6959 if (INT8_TYPE)
6960 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6961 if (INT16_TYPE)
6962 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6963 if (INT32_TYPE)
6964 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6965 if (INT64_TYPE)
6966 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6967 if (UINT8_TYPE)
6968 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6969 if (UINT16_TYPE)
6970 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6971 if (UINT32_TYPE)
6972 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6973 if (UINT64_TYPE)
6974 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6975 if (INT_LEAST8_TYPE)
6976 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6977 if (INT_LEAST16_TYPE)
6978 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6979 if (INT_LEAST32_TYPE)
6980 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6981 if (INT_LEAST64_TYPE)
6982 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6983 if (UINT_LEAST8_TYPE)
6984 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6985 if (UINT_LEAST16_TYPE)
6986 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6987 if (UINT_LEAST32_TYPE)
6988 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6989 if (UINT_LEAST64_TYPE)
6990 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6991 if (INT_FAST8_TYPE)
6992 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6993 if (INT_FAST16_TYPE)
6994 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6995 if (INT_FAST32_TYPE)
6996 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6997 if (INT_FAST64_TYPE)
6998 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6999 if (UINT_FAST8_TYPE)
7000 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
7001 if (UINT_FAST16_TYPE)
7002 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
7003 if (UINT_FAST32_TYPE)
7004 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
7005 if (UINT_FAST64_TYPE)
7006 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
7007 if (INTPTR_TYPE)
7008 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
7009 if (UINTPTR_TYPE)
7010 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
676997cf
RH
7011}
7012
6431177a 7013static void
35b1a6fa 7014c_init_attributes (void)
6431177a
JM
7015{
7016 /* Fill in the built_in_attributes array. */
4a90aeeb 7017#define DEF_ATTR_NULL_TREE(ENUM) \
6431177a 7018 built_in_attributes[(int) ENUM] = NULL_TREE;
4a90aeeb 7019#define DEF_ATTR_INT(ENUM, VALUE) \
c62c040f 7020 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
e384e6b5
BS
7021#define DEF_ATTR_STRING(ENUM, VALUE) \
7022 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6431177a
JM
7023#define DEF_ATTR_IDENT(ENUM, STRING) \
7024 built_in_attributes[(int) ENUM] = get_identifier (STRING);
7025#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
7026 built_in_attributes[(int) ENUM] \
7027 = tree_cons (built_in_attributes[(int) PURPOSE], \
7028 built_in_attributes[(int) VALUE], \
7029 built_in_attributes[(int) CHAIN]);
6431177a
JM
7030#include "builtin-attrs.def"
7031#undef DEF_ATTR_NULL_TREE
7032#undef DEF_ATTR_INT
7033#undef DEF_ATTR_IDENT
7034#undef DEF_ATTR_TREE_LIST
03dc0325 7035}
26f943fd 7036
943f82e7
JM
7037/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
7038 identifier as an argument, so the front end shouldn't look it up. */
7039
7040bool
564a129d 7041attribute_takes_identifier_p (const_tree attr_id)
943f82e7 7042{
f231b5ff 7043 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
1b9b91a6
JM
7044 if (spec == NULL)
7045 /* Unknown attribute that we'll end up ignoring, return true so we
7046 don't complain about an identifier argument. */
7047 return true;
7048 else if (!strcmp ("mode", spec->name)
7049 || !strcmp ("format", spec->name)
7050 || !strcmp ("cleanup", spec->name))
564a129d
JM
7051 return true;
7052 else
7053 return targetm.attribute_takes_identifier_p (attr_id);
943f82e7
JM
7054}
7055
349ae713
NB
7056/* Attribute handlers common to C front ends. */
7057
7058/* Handle a "packed" attribute; arguments as in
7059 struct attribute_spec.handler. */
7060
7061static tree
e18476eb 7062handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
a742c759 7063 int flags, bool *no_add_attrs)
349ae713 7064{
c6e4cc53 7065 if (TYPE_P (*node))
349ae713
NB
7066 {
7067 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 7068 *node = build_variant_type_copy (*node);
c6e4cc53 7069 TYPE_PACKED (*node) = 1;
349ae713
NB
7070 }
7071 else if (TREE_CODE (*node) == FIELD_DECL)
646c0835 7072 {
2cd36c22
AN
7073 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
7074 /* Still pack bitfields. */
7075 && ! DECL_INITIAL (*node))
646c0835
NS
7076 warning (OPT_Wattributes,
7077 "%qE attribute ignored for field of type %qT",
7078 name, TREE_TYPE (*node));
7079 else
7080 DECL_PACKED (*node) = 1;
7081 }
349ae713 7082 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
c6e4cc53
NS
7083 used for DECL_REGISTER. It wouldn't mean anything anyway.
7084 We can't set DECL_PACKED on the type of a TYPE_DECL, because
7085 that changes what the typedef is typing. */
349ae713
NB
7086 else
7087 {
5c498b10 7088 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7089 *no_add_attrs = true;
7090 }
7091
7092 return NULL_TREE;
7093}
7094
7095/* Handle a "nocommon" attribute; arguments as in
7096 struct attribute_spec.handler. */
7097
7098static tree
35b1a6fa 7099handle_nocommon_attribute (tree *node, tree name,
e18476eb
BI
7100 tree ARG_UNUSED (args),
7101 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 7102{
0ae9bd27 7103 if (VAR_P (*node))
349ae713
NB
7104 DECL_COMMON (*node) = 0;
7105 else
7106 {
5c498b10 7107 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7108 *no_add_attrs = true;
7109 }
7110
7111 return NULL_TREE;
7112}
7113
7114/* Handle a "common" attribute; arguments as in
7115 struct attribute_spec.handler. */
7116
7117static tree
e18476eb
BI
7118handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7119 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 7120{
0ae9bd27 7121 if (VAR_P (*node))
349ae713
NB
7122 DECL_COMMON (*node) = 1;
7123 else
7124 {
5c498b10 7125 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7126 *no_add_attrs = true;
7127 }
7128
7129 return NULL_TREE;
7130}
7131
7132/* Handle a "noreturn" attribute; arguments as in
7133 struct attribute_spec.handler. */
7134
7135static tree
e18476eb
BI
7136handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7137 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7138{
7139 tree type = TREE_TYPE (*node);
7140
7141 /* See FIXME comment in c_common_attribute_table. */
2debdb4f
NP
7142 if (TREE_CODE (*node) == FUNCTION_DECL
7143 || objc_method_decl (TREE_CODE (*node)))
349ae713
NB
7144 TREE_THIS_VOLATILE (*node) = 1;
7145 else if (TREE_CODE (type) == POINTER_TYPE
7146 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7147 TREE_TYPE (*node)
6e7ceb17
PC
7148 = (build_qualified_type
7149 (build_pointer_type
7150 (build_type_variant (TREE_TYPE (type),
7151 TYPE_READONLY (TREE_TYPE (type)), 1)),
7152 TYPE_QUALS (type)));
349ae713
NB
7153 else
7154 {
5c498b10 7155 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7156 *no_add_attrs = true;
7157 }
7158
7159 return NULL_TREE;
7160}
7161
52bf96d2
JH
7162/* Handle a "hot" and attribute; arguments as in
7163 struct attribute_spec.handler. */
7164
7165static tree
7166handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
ab442df7 7167 int ARG_UNUSED (flags), bool *no_add_attrs)
52bf96d2 7168{
e45abe1f
RH
7169 if (TREE_CODE (*node) == FUNCTION_DECL
7170 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
7171 {
7172 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7173 {
45484dcf
MP
7174 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7175 "with attribute %qs", name, "cold");
52bf96d2
JH
7176 *no_add_attrs = true;
7177 }
5779e713
MM
7178 /* Most of the rest of the hot processing is done later with
7179 lookup_attribute. */
52bf96d2
JH
7180 }
7181 else
7182 {
7183 warning (OPT_Wattributes, "%qE attribute ignored", name);
7184 *no_add_attrs = true;
7185 }
7186
7187 return NULL_TREE;
7188}
e45abe1f 7189
52bf96d2
JH
7190/* Handle a "cold" and attribute; arguments as in
7191 struct attribute_spec.handler. */
7192
7193static tree
7194handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7195 int ARG_UNUSED (flags), bool *no_add_attrs)
7196{
e45abe1f
RH
7197 if (TREE_CODE (*node) == FUNCTION_DECL
7198 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
7199 {
7200 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7201 {
45484dcf
MP
7202 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7203 "with attribute %qs", name, "hot");
52bf96d2
JH
7204 *no_add_attrs = true;
7205 }
5779e713
MM
7206 /* Most of the rest of the cold processing is done later with
7207 lookup_attribute. */
52bf96d2
JH
7208 }
7209 else
7210 {
7211 warning (OPT_Wattributes, "%qE attribute ignored", name);
7212 *no_add_attrs = true;
7213 }
7214
7215 return NULL_TREE;
7216}
7217
e664c61c 7218/* Handle a "no_sanitize_address" attribute; arguments as in
77bc5132
JJ
7219 struct attribute_spec.handler. */
7220
7221static tree
e664c61c
KS
7222handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7223 bool *no_add_attrs)
77bc5132
JJ
7224{
7225 if (TREE_CODE (*node) != FUNCTION_DECL)
7226 {
7227 warning (OPT_Wattributes, "%qE attribute ignored", name);
7228 *no_add_attrs = true;
7229 }
7230
7231 return NULL_TREE;
7232}
7233
e664c61c
KS
7234/* Handle a "no_address_safety_analysis" attribute; arguments as in
7235 struct attribute_spec.handler. */
7236
7237static tree
7238handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7239 bool *no_add_attrs)
7240{
7241 if (TREE_CODE (*node) != FUNCTION_DECL)
7242 warning (OPT_Wattributes, "%qE attribute ignored", name);
7243 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7244 DECL_ATTRIBUTES (*node)
7245 = tree_cons (get_identifier ("no_sanitize_address"),
7246 NULL_TREE, DECL_ATTRIBUTES (*node));
7247 *no_add_attrs = true;
7248 return NULL_TREE;
7249}
7250
ce6923c5
MP
7251/* Handle a "no_sanitize_undefined" attribute; arguments as in
7252 struct attribute_spec.handler. */
7253
7254static tree
7255handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7256 bool *no_add_attrs)
7257{
7258 if (TREE_CODE (*node) != FUNCTION_DECL)
7259 {
7260 warning (OPT_Wattributes, "%qE attribute ignored", name);
7261 *no_add_attrs = true;
7262 }
7263
7264 return NULL_TREE;
7265}
7266
5434dc07
MD
7267/* Handle a "stack_protect" attribute; arguments as in
7268 struct attribute_spec.handler. */
7269static tree
7270handle_stack_protect_attribute (tree *node, tree name, tree, int,
7271 bool *no_add_attrs)
7272{
7273 if (TREE_CODE (*node) != FUNCTION_DECL)
7274 {
7275 warning (OPT_Wattributes, "%qE attribute ignored", name);
7276 *no_add_attrs = true;
7277 }
7278 else
7279 DECL_ATTRIBUTES (*node)
7280 = tree_cons (get_identifier ("stack_protect"),
7281 NULL_TREE, DECL_ATTRIBUTES (*node));
7282
7283 return NULL_TREE;
7284}
7285
349ae713
NB
7286/* Handle a "noinline" attribute; arguments as in
7287 struct attribute_spec.handler. */
7288
7289static tree
35b1a6fa 7290handle_noinline_attribute (tree *node, tree name,
e18476eb
BI
7291 tree ARG_UNUSED (args),
7292 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7293{
7294 if (TREE_CODE (*node) == FUNCTION_DECL)
45484dcf
MP
7295 {
7296 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7297 {
7298 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7299 "with attribute %qs", name, "always_inline");
7300 *no_add_attrs = true;
7301 }
7302 else
7303 DECL_UNINLINABLE (*node) = 1;
7304 }
349ae713
NB
7305 else
7306 {
5c498b10 7307 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7308 *no_add_attrs = true;
7309 }
7310
7311 return NULL_TREE;
7312}
7313
86631ea3
MJ
7314/* Handle a "noclone" attribute; arguments as in
7315 struct attribute_spec.handler. */
7316
7317static tree
7318handle_noclone_attribute (tree *node, tree name,
7319 tree ARG_UNUSED (args),
7320 int ARG_UNUSED (flags), bool *no_add_attrs)
7321{
7322 if (TREE_CODE (*node) != FUNCTION_DECL)
7323 {
7324 warning (OPT_Wattributes, "%qE attribute ignored", name);
7325 *no_add_attrs = true;
7326 }
7327
7328 return NULL_TREE;
7329}
7330
185c9e56
ML
7331/* Handle a "no_icf" attribute; arguments as in
7332 struct attribute_spec.handler. */
7333
7334static tree
7335handle_noicf_attribute (tree *node, tree name,
7336 tree ARG_UNUSED (args),
7337 int ARG_UNUSED (flags), bool *no_add_attrs)
7338{
7339 if (TREE_CODE (*node) != FUNCTION_DECL)
7340 {
7341 warning (OPT_Wattributes, "%qE attribute ignored", name);
7342 *no_add_attrs = true;
7343 }
7344
7345 return NULL_TREE;
7346}
7347
7348
349ae713
NB
7349/* Handle a "always_inline" attribute; arguments as in
7350 struct attribute_spec.handler. */
7351
7352static tree
35b1a6fa 7353handle_always_inline_attribute (tree *node, tree name,
e18476eb
BI
7354 tree ARG_UNUSED (args),
7355 int ARG_UNUSED (flags),
a742c759 7356 bool *no_add_attrs)
349ae713
NB
7357{
7358 if (TREE_CODE (*node) == FUNCTION_DECL)
7359 {
45484dcf
MP
7360 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7361 {
7362 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7363 "with %qs attribute", name, "noinline");
7364 *no_add_attrs = true;
7365 }
7366 else
7367 /* Set the attribute and mark it for disregarding inline
7368 limits. */
7369 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
349ae713
NB
7370 }
7371 else
7372 {
5c498b10 7373 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713 7374 *no_add_attrs = true;
4eb7fd83
JJ
7375 }
7376
7377 return NULL_TREE;
7378}
7379
7380/* Handle a "gnu_inline" attribute; arguments as in
7381 struct attribute_spec.handler. */
7382
7383static tree
7384handle_gnu_inline_attribute (tree *node, tree name,
7385 tree ARG_UNUSED (args),
7386 int ARG_UNUSED (flags),
7387 bool *no_add_attrs)
7388{
7389 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7390 {
7391 /* Do nothing else, just set the attribute. We'll get at
7392 it later with lookup_attribute. */
7393 }
7394 else
7395 {
7396 warning (OPT_Wattributes, "%qE attribute ignored", name);
7397 *no_add_attrs = true;
46a4da10
JH
7398 }
7399
7400 return NULL_TREE;
7401}
7402
7403/* Handle a "leaf" attribute; arguments as in
7404 struct attribute_spec.handler. */
7405
7406static tree
7407handle_leaf_attribute (tree *node, tree name,
7408 tree ARG_UNUSED (args),
7409 int ARG_UNUSED (flags), bool *no_add_attrs)
7410{
7411 if (TREE_CODE (*node) != FUNCTION_DECL)
7412 {
7413 warning (OPT_Wattributes, "%qE attribute ignored", name);
7414 *no_add_attrs = true;
7415 }
7416 if (!TREE_PUBLIC (*node))
7417 {
7418 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7419 *no_add_attrs = true;
349ae713
NB
7420 }
7421
7422 return NULL_TREE;
7423}
7424
d752cfdb
JJ
7425/* Handle an "artificial" attribute; arguments as in
7426 struct attribute_spec.handler. */
7427
7428static tree
7429handle_artificial_attribute (tree *node, tree name,
7430 tree ARG_UNUSED (args),
7431 int ARG_UNUSED (flags),
7432 bool *no_add_attrs)
7433{
7434 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7435 {
7436 /* Do nothing else, just set the attribute. We'll get at
7437 it later with lookup_attribute. */
7438 }
7439 else
7440 {
7441 warning (OPT_Wattributes, "%qE attribute ignored", name);
7442 *no_add_attrs = true;
7443 }
7444
7445 return NULL_TREE;
7446}
7447
0691d1d4
RG
7448/* Handle a "flatten" attribute; arguments as in
7449 struct attribute_spec.handler. */
7450
7451static tree
7452handle_flatten_attribute (tree *node, tree name,
c22cacf3
MS
7453 tree args ATTRIBUTE_UNUSED,
7454 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0691d1d4
RG
7455{
7456 if (TREE_CODE (*node) == FUNCTION_DECL)
7457 /* Do nothing else, just set the attribute. We'll get at
7458 it later with lookup_attribute. */
7459 ;
7460 else
7461 {
7462 warning (OPT_Wattributes, "%qE attribute ignored", name);
7463 *no_add_attrs = true;
7464 }
7465
7466 return NULL_TREE;
7467}
7468
d2af6a68
JJ
7469/* Handle a "warning" or "error" attribute; arguments as in
7470 struct attribute_spec.handler. */
7471
7472static tree
7473handle_error_attribute (tree *node, tree name, tree args,
7474 int ARG_UNUSED (flags), bool *no_add_attrs)
7475{
7476 if (TREE_CODE (*node) == FUNCTION_DECL
cf35e2b1 7477 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
d2af6a68
JJ
7478 /* Do nothing else, just set the attribute. We'll get at
7479 it later with lookup_attribute. */
7480 ;
7481 else
7482 {
7483 warning (OPT_Wattributes, "%qE attribute ignored", name);
7484 *no_add_attrs = true;
7485 }
7486
7487 return NULL_TREE;
7488}
0691d1d4 7489
349ae713
NB
7490/* Handle a "used" attribute; arguments as in
7491 struct attribute_spec.handler. */
7492
7493static tree
e18476eb
BI
7494handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7495 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 7496{
d7ddbe24
RH
7497 tree node = *pnode;
7498
7499 if (TREE_CODE (node) == FUNCTION_DECL
0ae9bd27 7500 || (VAR_P (node) && TREE_STATIC (node))
3797cb21 7501 || (TREE_CODE (node) == TYPE_DECL))
4d7d0451 7502 {
4d7d0451 7503 TREE_USED (node) = 1;
8e3e233b 7504 DECL_PRESERVE_P (node) = 1;
0ae9bd27 7505 if (VAR_P (node))
ebfbbdc5 7506 DECL_READ_P (node) = 1;
4d7d0451 7507 }
349ae713
NB
7508 else
7509 {
5c498b10 7510 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7511 *no_add_attrs = true;
7512 }
7513
7514 return NULL_TREE;
7515}
7516
7517/* Handle a "unused" attribute; arguments as in
7518 struct attribute_spec.handler. */
7519
7520static tree
e18476eb
BI
7521handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7522 int flags, bool *no_add_attrs)
349ae713
NB
7523{
7524 if (DECL_P (*node))
7525 {
7526 tree decl = *node;
7527
7528 if (TREE_CODE (decl) == PARM_DECL
af05e6e5 7529 || VAR_OR_FUNCTION_DECL_P (decl)
349ae713
NB
7530 || TREE_CODE (decl) == LABEL_DECL
7531 || TREE_CODE (decl) == TYPE_DECL)
ebfbbdc5
JJ
7532 {
7533 TREE_USED (decl) = 1;
0ae9bd27 7534 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
ebfbbdc5
JJ
7535 DECL_READ_P (decl) = 1;
7536 }
349ae713
NB
7537 else
7538 {
5c498b10 7539 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7540 *no_add_attrs = true;
7541 }
7542 }
7543 else
7544 {
7545 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 7546 *node = build_variant_type_copy (*node);
349ae713
NB
7547 TREE_USED (*node) = 1;
7548 }
7549
7550 return NULL_TREE;
7551}
7552
ce91e74c
JH
7553/* Handle a "externally_visible" attribute; arguments as in
7554 struct attribute_spec.handler. */
7555
7556static tree
7557handle_externally_visible_attribute (tree *pnode, tree name,
7558 tree ARG_UNUSED (args),
7559 int ARG_UNUSED (flags),
7560 bool *no_add_attrs)
7561{
7562 tree node = *pnode;
7563
21b634ae 7564 if (VAR_OR_FUNCTION_DECL_P (node))
ce91e74c 7565 {
343d4b27
JJ
7566 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7567 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7568 {
7569 warning (OPT_Wattributes,
7570 "%qE attribute have effect only on public objects", name);
7571 *no_add_attrs = true;
7572 }
ce91e74c 7573 }
ce91e74c
JH
7574 else
7575 {
7576 warning (OPT_Wattributes, "%qE attribute ignored", name);
7577 *no_add_attrs = true;
7578 }
7579
7580 return NULL_TREE;
7581}
7582
7861b648
AK
7583/* Handle the "no_reorder" attribute. Arguments as in
7584 struct attribute_spec.handler. */
7585
7586static tree
7587handle_no_reorder_attribute (tree *pnode,
7588 tree name,
7589 tree,
7590 int,
7591 bool *no_add_attrs)
7592{
7593 tree node = *pnode;
7594
21b634ae 7595 if (!VAR_OR_FUNCTION_DECL_P (node)
7861b648
AK
7596 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7597 {
7598 warning (OPT_Wattributes,
7599 "%qE attribute only affects top level objects",
7600 name);
7601 *no_add_attrs = true;
7602 }
7603
7604 return NULL_TREE;
7605}
7606
349ae713
NB
7607/* Handle a "const" attribute; arguments as in
7608 struct attribute_spec.handler. */
7609
7610static tree
e18476eb
BI
7611handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7612 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7613{
7614 tree type = TREE_TYPE (*node);
7615
7616 /* See FIXME comment on noreturn in c_common_attribute_table. */
7617 if (TREE_CODE (*node) == FUNCTION_DECL)
7618 TREE_READONLY (*node) = 1;
7619 else if (TREE_CODE (type) == POINTER_TYPE
7620 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7621 TREE_TYPE (*node)
6e7ceb17
PC
7622 = (build_qualified_type
7623 (build_pointer_type
7624 (build_type_variant (TREE_TYPE (type), 1,
7625 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7626 TYPE_QUALS (type)));
349ae713
NB
7627 else
7628 {
5c498b10 7629 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7630 *no_add_attrs = true;
7631 }
7632
7633 return NULL_TREE;
7634}
7635
7636/* Handle a "transparent_union" attribute; arguments as in
7637 struct attribute_spec.handler. */
7638
7639static tree
35b1a6fa 7640handle_transparent_union_attribute (tree *node, tree name,
e18476eb 7641 tree ARG_UNUSED (args), int flags,
a742c759 7642 bool *no_add_attrs)
349ae713 7643{
4009f2e7 7644 tree type;
52dd234b
RH
7645
7646 *no_add_attrs = true;
349ae713 7647
e28d52cf
DS
7648
7649 if (TREE_CODE (*node) == TYPE_DECL
7650 && ! (flags & ATTR_FLAG_CXX11))
4009f2e7
JM
7651 node = &TREE_TYPE (*node);
7652 type = *node;
349ae713 7653
52dd234b 7654 if (TREE_CODE (type) == UNION_TYPE)
349ae713 7655 {
d58d6eb5
JM
7656 /* Make sure that the first field will work for a transparent union.
7657 If the type isn't complete yet, leave the check to the code in
7658 finish_struct. */
7659 if (TYPE_SIZE (type))
7660 {
7661 tree first = first_field (type);
7662 if (first == NULL_TREE
7663 || DECL_ARTIFICIAL (first)
7664 || TYPE_MODE (type) != DECL_MODE (first))
7665 goto ignored;
7666 }
7667
349ae713 7668 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
52dd234b 7669 {
d58d6eb5
JM
7670 /* If the type isn't complete yet, setting the flag
7671 on a variant wouldn't ever be checked. */
7672 if (!TYPE_SIZE (type))
7673 goto ignored;
7674
7675 /* build_duplicate_type doesn't work for C++. */
7676 if (c_dialect_cxx ())
52dd234b
RH
7677 goto ignored;
7678
7679 /* A type variant isn't good enough, since we don't a cast
7680 to such a type removed as a no-op. */
7681 *node = type = build_duplicate_type (type);
7682 }
7683
ebf0bf7f 7684 TYPE_TRANSPARENT_AGGR (type) = 1;
52dd234b 7685 return NULL_TREE;
349ae713
NB
7686 }
7687
52dd234b
RH
7688 ignored:
7689 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7690 return NULL_TREE;
7691}
7692
fc8600f9
MM
7693/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7694 get the requested priority for a constructor or destructor,
7695 possibly issuing diagnostics for invalid or reserved
7696 priorities. */
7697
7698static priority_type
7699get_priority (tree args, bool is_destructor)
7700{
7701 HOST_WIDE_INT pri;
b2f4bed8 7702 tree arg;
fc8600f9
MM
7703
7704 if (!args)
7705 return DEFAULT_INIT_PRIORITY;
b8698a0f 7706
f6fc5c86
MM
7707 if (!SUPPORTS_INIT_PRIORITY)
7708 {
7709 if (is_destructor)
7710 error ("destructor priorities are not supported");
7711 else
7712 error ("constructor priorities are not supported");
7713 return DEFAULT_INIT_PRIORITY;
7714 }
7715
b2f4bed8 7716 arg = TREE_VALUE (args);
fad7652e
JJ
7717 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7718 goto invalid;
7719 if (arg == error_mark_node)
7720 return DEFAULT_INIT_PRIORITY;
8d0d1915 7721 arg = default_conversion (arg);
9541ffee 7722 if (!tree_fits_shwi_p (arg)
b2f4bed8 7723 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
fc8600f9
MM
7724 goto invalid;
7725
9439e9a1 7726 pri = tree_to_shwi (arg);
fc8600f9
MM
7727 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7728 goto invalid;
7729
7730 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7731 {
7732 if (is_destructor)
7733 warning (0,
7734 "destructor priorities from 0 to %d are reserved "
b8698a0f 7735 "for the implementation",
fc8600f9
MM
7736 MAX_RESERVED_INIT_PRIORITY);
7737 else
7738 warning (0,
7739 "constructor priorities from 0 to %d are reserved "
b8698a0f 7740 "for the implementation",
fc8600f9
MM
7741 MAX_RESERVED_INIT_PRIORITY);
7742 }
7743 return pri;
7744
7745 invalid:
7746 if (is_destructor)
7747 error ("destructor priorities must be integers from 0 to %d inclusive",
7748 MAX_INIT_PRIORITY);
7749 else
7750 error ("constructor priorities must be integers from 0 to %d inclusive",
7751 MAX_INIT_PRIORITY);
7752 return DEFAULT_INIT_PRIORITY;
7753}
7754
349ae713
NB
7755/* Handle a "constructor" attribute; arguments as in
7756 struct attribute_spec.handler. */
7757
7758static tree
fc8600f9 7759handle_constructor_attribute (tree *node, tree name, tree args,
e18476eb 7760 int ARG_UNUSED (flags),
a742c759 7761 bool *no_add_attrs)
349ae713
NB
7762{
7763 tree decl = *node;
7764 tree type = TREE_TYPE (decl);
7765
7766 if (TREE_CODE (decl) == FUNCTION_DECL
7767 && TREE_CODE (type) == FUNCTION_TYPE
7768 && decl_function_context (decl) == 0)
7769 {
fc8600f9 7770 priority_type priority;
349ae713 7771 DECL_STATIC_CONSTRUCTOR (decl) = 1;
fc8600f9
MM
7772 priority = get_priority (args, /*is_destructor=*/false);
7773 SET_DECL_INIT_PRIORITY (decl, priority);
349ae713
NB
7774 TREE_USED (decl) = 1;
7775 }
7776 else
7777 {
5c498b10 7778 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7779 *no_add_attrs = true;
7780 }
7781
7782 return NULL_TREE;
7783}
7784
7785/* Handle a "destructor" attribute; arguments as in
7786 struct attribute_spec.handler. */
7787
7788static tree
fc8600f9 7789handle_destructor_attribute (tree *node, tree name, tree args,
e18476eb 7790 int ARG_UNUSED (flags),
a742c759 7791 bool *no_add_attrs)
349ae713
NB
7792{
7793 tree decl = *node;
7794 tree type = TREE_TYPE (decl);
7795
7796 if (TREE_CODE (decl) == FUNCTION_DECL
7797 && TREE_CODE (type) == FUNCTION_TYPE
7798 && decl_function_context (decl) == 0)
7799 {
fc8600f9 7800 priority_type priority;
349ae713 7801 DECL_STATIC_DESTRUCTOR (decl) = 1;
fc8600f9
MM
7802 priority = get_priority (args, /*is_destructor=*/true);
7803 SET_DECL_FINI_PRIORITY (decl, priority);
349ae713
NB
7804 TREE_USED (decl) = 1;
7805 }
7806 else
7807 {
5c498b10 7808 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7809 *no_add_attrs = true;
7810 }
7811
7812 return NULL_TREE;
7813}
7814
4d451982
MLI
7815/* Nonzero if the mode is a valid vector mode for this architecture.
7816 This returns nonzero even if there is no hardware support for the
7817 vector mode, but we can emulate with narrower modes. */
7818
7819static int
ef4bddc2 7820vector_mode_valid_p (machine_mode mode)
4d451982
MLI
7821{
7822 enum mode_class mclass = GET_MODE_CLASS (mode);
ef4bddc2 7823 machine_mode innermode;
4d451982
MLI
7824
7825 /* Doh! What's going on? */
7826 if (mclass != MODE_VECTOR_INT
7827 && mclass != MODE_VECTOR_FLOAT
7828 && mclass != MODE_VECTOR_FRACT
7829 && mclass != MODE_VECTOR_UFRACT
7830 && mclass != MODE_VECTOR_ACCUM
7831 && mclass != MODE_VECTOR_UACCUM)
7832 return 0;
7833
7834 /* Hardware support. Woo hoo! */
7835 if (targetm.vector_mode_supported_p (mode))
7836 return 1;
7837
7838 innermode = GET_MODE_INNER (mode);
7839
7840 /* We should probably return 1 if requesting V4DI and we have no DI,
7841 but we have V2DI, but this is probably very unlikely. */
7842
7843 /* If we have support for the inner mode, we can safely emulate it.
7844 We may not have V2DI, but me can emulate with a pair of DIs. */
7845 return targetm.scalar_mode_supported_p (innermode);
7846}
7847
7848
349ae713
NB
7849/* Handle a "mode" attribute; arguments as in
7850 struct attribute_spec.handler. */
7851
7852static tree
e18476eb
BI
7853handle_mode_attribute (tree *node, tree name, tree args,
7854 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7855{
7856 tree type = *node;
88388a52 7857 tree ident = TREE_VALUE (args);
349ae713
NB
7858
7859 *no_add_attrs = true;
7860
88388a52 7861 if (TREE_CODE (ident) != IDENTIFIER_NODE)
5c498b10 7862 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7863 else
7864 {
7865 int j;
88388a52 7866 const char *p = IDENTIFIER_POINTER (ident);
349ae713 7867 int len = strlen (p);
ef4bddc2 7868 machine_mode mode = VOIDmode;
349ae713 7869 tree typefm;
6dd53648 7870 bool valid_mode;
349ae713
NB
7871
7872 if (len > 4 && p[0] == '_' && p[1] == '_'
7873 && p[len - 1] == '_' && p[len - 2] == '_')
7874 {
28dab132 7875 char *newp = (char *) alloca (len - 1);
349ae713
NB
7876
7877 strcpy (newp, &p[2]);
7878 newp[len - 4] = '\0';
7879 p = newp;
7880 }
7881
7882 /* Change this type to have a type with the specified mode.
7883 First check for the special modes. */
3f75a254 7884 if (!strcmp (p, "byte"))
349ae713
NB
7885 mode = byte_mode;
7886 else if (!strcmp (p, "word"))
7887 mode = word_mode;
3f75a254 7888 else if (!strcmp (p, "pointer"))
349ae713 7889 mode = ptr_mode;
c7ff6e7a
AK
7890 else if (!strcmp (p, "libgcc_cmp_return"))
7891 mode = targetm.libgcc_cmp_return_mode ();
7892 else if (!strcmp (p, "libgcc_shift_count"))
7893 mode = targetm.libgcc_shift_count_mode ();
7b0518e3
UW
7894 else if (!strcmp (p, "unwind_word"))
7895 mode = targetm.unwind_word_mode ();
349ae713
NB
7896 else
7897 for (j = 0; j < NUM_MACHINE_MODES; j++)
7898 if (!strcmp (p, GET_MODE_NAME (j)))
61f03aba 7899 {
ef4bddc2 7900 mode = (machine_mode) j;
61f03aba
RH
7901 break;
7902 }
349ae713
NB
7903
7904 if (mode == VOIDmode)
4a5eab38 7905 {
88388a52 7906 error ("unknown machine mode %qE", ident);
4a5eab38
PB
7907 return NULL_TREE;
7908 }
7909
6dd53648
RH
7910 valid_mode = false;
7911 switch (GET_MODE_CLASS (mode))
4a5eab38 7912 {
6dd53648
RH
7913 case MODE_INT:
7914 case MODE_PARTIAL_INT:
7915 case MODE_FLOAT:
9a8ce21f 7916 case MODE_DECIMAL_FLOAT:
ab22c1fa
CF
7917 case MODE_FRACT:
7918 case MODE_UFRACT:
7919 case MODE_ACCUM:
7920 case MODE_UACCUM:
6dd53648
RH
7921 valid_mode = targetm.scalar_mode_supported_p (mode);
7922 break;
7923
7924 case MODE_COMPLEX_INT:
7925 case MODE_COMPLEX_FLOAT:
7926 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7927 break;
7928
7929 case MODE_VECTOR_INT:
7930 case MODE_VECTOR_FLOAT:
ab22c1fa
CF
7931 case MODE_VECTOR_FRACT:
7932 case MODE_VECTOR_UFRACT:
7933 case MODE_VECTOR_ACCUM:
7934 case MODE_VECTOR_UACCUM:
5c498b10
DD
7935 warning (OPT_Wattributes, "specifying vector types with "
7936 "__attribute__ ((mode)) is deprecated");
7937 warning (OPT_Wattributes,
7938 "use __attribute__ ((vector_size)) instead");
6dd53648
RH
7939 valid_mode = vector_mode_valid_p (mode);
7940 break;
4a5eab38 7941
6dd53648
RH
7942 default:
7943 break;
7944 }
7945 if (!valid_mode)
7946 {
9e637a26 7947 error ("unable to emulate %qs", p);
6dd53648
RH
7948 return NULL_TREE;
7949 }
4a5eab38 7950
6dd53648 7951 if (POINTER_TYPE_P (type))
cb2a532e 7952 {
36c5e70a 7953 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
ef4bddc2 7954 tree (*fn)(tree, machine_mode, bool);
6dd53648 7955
36c5e70a 7956 if (!targetm.addr_space.valid_pointer_mode (mode, as))
cb2a532e 7957 {
9e637a26 7958 error ("invalid pointer mode %qs", p);
cb2a532e
AH
7959 return NULL_TREE;
7960 }
7961
c22cacf3 7962 if (TREE_CODE (type) == POINTER_TYPE)
6dd53648 7963 fn = build_pointer_type_for_mode;
4977bab6 7964 else
6dd53648
RH
7965 fn = build_reference_type_for_mode;
7966 typefm = fn (TREE_TYPE (type), mode, false);
cb2a532e 7967 }
6dd53648 7968 else
ab22c1fa
CF
7969 {
7970 /* For fixed-point modes, we need to test if the signness of type
7971 and the machine mode are consistent. */
7972 if (ALL_FIXED_POINT_MODE_P (mode)
7973 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7974 {
d8a07487 7975 error ("signedness of type and machine mode %qs don%'t match", p);
ab22c1fa
CF
7976 return NULL_TREE;
7977 }
7978 /* For fixed-point modes, we need to pass saturating info. */
7979 typefm = lang_hooks.types.type_for_mode (mode,
7980 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7981 : TYPE_UNSIGNED (type));
7982 }
ec8465a5 7983
6dd53648
RH
7984 if (typefm == NULL_TREE)
7985 {
61f03aba 7986 error ("no data type for mode %qs", p);
6dd53648
RH
7987 return NULL_TREE;
7988 }
ec8465a5
RK
7989 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7990 {
7991 /* For enumeral types, copy the precision from the integer
7992 type returned above. If not an INTEGER_TYPE, we can't use
7993 this mode for this type. */
7994 if (TREE_CODE (typefm) != INTEGER_TYPE)
7995 {
61f03aba 7996 error ("cannot use mode %qs for enumeral types", p);
ec8465a5
RK
7997 return NULL_TREE;
7998 }
7999
99db1ef0
RH
8000 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
8001 {
8002 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
8003 typefm = type;
8004 }
8005 else
8006 {
8007 /* We cannot build a type variant, as there's code that assumes
8008 that TYPE_MAIN_VARIANT has the same mode. This includes the
8009 debug generators. Instead, create a subrange type. This
8010 results in all of the enumeral values being emitted only once
8011 in the original, and the subtype gets them by reference. */
8012 if (TYPE_UNSIGNED (type))
8013 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
8014 else
8015 typefm = make_signed_type (TYPE_PRECISION (typefm));
8016 TREE_TYPE (typefm) = type;
8017 }
ec8465a5 8018 }
a2d36602
RH
8019 else if (VECTOR_MODE_P (mode)
8020 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
8021 : TREE_CODE (type) != TREE_CODE (typefm))
61f03aba
RH
8022 {
8023 error ("mode %qs applied to inappropriate type", p);
8024 return NULL_TREE;
8025 }
8026
6dd53648 8027 *node = typefm;
349ae713
NB
8028 }
8029
8030 return NULL_TREE;
8031}
8032
8033/* Handle a "section" attribute; arguments as in
8034 struct attribute_spec.handler. */
8035
8036static tree
e18476eb
BI
8037handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8038 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
8039{
8040 tree decl = *node;
8041
0373796b 8042 if (!targetm_common.have_named_sections)
349ae713 8043 {
0373796b
JT
8044 error_at (DECL_SOURCE_LOCATION (*node),
8045 "section attributes are not supported for this target");
8046 goto fail;
8047 }
9fb32434 8048
0373796b 8049 user_defined_section_attribute = true;
349ae713 8050
21b634ae 8051 if (!VAR_OR_FUNCTION_DECL_P (decl))
0373796b
JT
8052 {
8053 error ("section attribute not allowed for %q+D", *node);
8054 goto fail;
349ae713 8055 }
0373796b
JT
8056
8057 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
349ae713 8058 {
0373796b
JT
8059 error ("section attribute argument not a string constant");
8060 goto fail;
349ae713
NB
8061 }
8062
0ae9bd27 8063 if (VAR_P (decl)
0373796b
JT
8064 && current_function_decl != NULL_TREE
8065 && !TREE_STATIC (decl))
8066 {
8067 error_at (DECL_SOURCE_LOCATION (decl),
8068 "section attribute cannot be specified for local variables");
8069 goto fail;
8070 }
8071
8072 /* The decl may have already been given a section attribute
8073 from a previous declaration. Ensure they match. */
8074 if (DECL_SECTION_NAME (decl) != NULL
8075 && strcmp (DECL_SECTION_NAME (decl),
8076 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
8077 {
8078 error ("section of %q+D conflicts with previous declaration", *node);
8079 goto fail;
8080 }
8081
0ae9bd27 8082 if (VAR_P (decl)
0373796b
JT
8083 && !targetm.have_tls && targetm.emutls.tmpl_section
8084 && DECL_THREAD_LOCAL_P (decl))
8085 {
8086 error ("section of %q+D cannot be overridden", *node);
8087 goto fail;
8088 }
8089
8090 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
8091 return NULL_TREE;
8092
8093fail:
8094 *no_add_attrs = true;
349ae713
NB
8095 return NULL_TREE;
8096}
8097
d19fa6b5
JM
8098/* Check whether ALIGN is a valid user-specified alignment. If so,
8099 return its base-2 log; if not, output an error and return -1. If
8100 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
8101 no error. */
8102int
8103check_user_alignment (const_tree align, bool allow_zero)
8104{
8105 int i;
8106
661a0813
MP
8107 if (error_operand_p (align))
8108 return -1;
a859517f
MP
8109 if (TREE_CODE (align) != INTEGER_CST
8110 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
d19fa6b5
JM
8111 {
8112 error ("requested alignment is not an integer constant");
8113 return -1;
8114 }
8115 else if (allow_zero && integer_zerop (align))
8116 return -1;
3f12f6e9
SKS
8117 else if (tree_int_cst_sgn (align) == -1
8118 || (i = tree_log2 (align)) == -1)
d19fa6b5 8119 {
3f12f6e9 8120 error ("requested alignment is not a positive power of 2");
d19fa6b5
JM
8121 return -1;
8122 }
8123 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
8124 {
8125 error ("requested alignment is too large");
8126 return -1;
8127 }
8128 return i;
8129}
8130
e28d52cf
DS
8131/*
8132 If in c++-11, check if the c++-11 alignment constraint with respect
8133 to fundamental alignment (in [dcl.align]) are satisfied. If not in
8134 c++-11 mode, does nothing.
8135
8136 [dcl.align]2/ says:
8137
8138 [* if the constant expression evaluates to a fundamental alignment,
8139 the alignment requirement of the declared entity shall be the
8140 specified fundamental alignment.
8141
8142 * if the constant expression evaluates to an extended alignment
8143 and the implementation supports that alignment in the context
8144 of the declaration, the alignment of the declared entity shall
8145 be that alignment
8146
8147 * if the constant expression evaluates to an extended alignment
8148 and the implementation does not support that alignment in the
8149 context of the declaration, the program is ill-formed]. */
8150
8151static bool
8152check_cxx_fundamental_alignment_constraints (tree node,
8153 unsigned align_log,
8154 int flags)
8155{
8156 bool alignment_too_large_p = false;
8157 unsigned requested_alignment = 1U << align_log;
8158 unsigned max_align = 0;
8159
8160 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8161 || (node == NULL_TREE || node == error_mark_node))
8162 return true;
8163
8164 if (cxx_fundamental_alignment_p (requested_alignment))
8165 return true;
8166
8167 if (DECL_P (node))
8168 {
8169 if (TREE_STATIC (node))
8170 {
8171 /* For file scope variables and static members, the target
8172 supports alignments that are at most
8173 MAX_OFILE_ALIGNMENT. */
8174 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8175 alignment_too_large_p = true;
8176 }
8177 else
8178 {
8179#ifdef BIGGEST_FIELD_ALIGNMENT
8180#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8181#else
8182#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8183#endif
8184 /* For non-static members, the target supports either
8185 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8186 if it is defined or BIGGEST_ALIGNMENT. */
8187 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8188 if (TREE_CODE (node) == FIELD_DECL
8189 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8190 alignment_too_large_p = true;
8191#undef MAX_TARGET_FIELD_ALIGNMENT
8192 /* For stack variables, the target supports at most
8193 MAX_STACK_ALIGNMENT. */
8194 else if (decl_function_context (node) != NULL
8195 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8196 alignment_too_large_p = true;
8197 }
8198 }
8199 else if (TYPE_P (node))
8200 {
8201 /* Let's be liberal for types. */
8202 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8203 alignment_too_large_p = true;
8204 }
8205
8206 if (alignment_too_large_p)
8207 pedwarn (input_location, OPT_Wattributes,
8208 "requested alignment %d is larger than %d",
8209 requested_alignment, max_align);
8210
8211 return !alignment_too_large_p;
8212}
8213
349ae713
NB
8214/* Handle a "aligned" attribute; arguments as in
8215 struct attribute_spec.handler. */
8216
8217static tree
e18476eb 8218handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
a742c759 8219 int flags, bool *no_add_attrs)
349ae713
NB
8220{
8221 tree decl = NULL_TREE;
8222 tree *type = NULL;
8223 int is_type = 0;
5d77fb19 8224 tree align_expr;
349ae713
NB
8225 int i;
8226
5d77fb19
MG
8227 if (args)
8228 {
8229 align_expr = TREE_VALUE (args);
661a0813
MP
8230 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8231 && TREE_CODE (align_expr) != FUNCTION_DECL)
5d77fb19
MG
8232 align_expr = default_conversion (align_expr);
8233 }
8234 else
8235 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8236
349ae713
NB
8237 if (DECL_P (*node))
8238 {
8239 decl = *node;
8240 type = &TREE_TYPE (decl);
8241 is_type = TREE_CODE (*node) == TYPE_DECL;
8242 }
8243 else if (TYPE_P (*node))
8244 type = node, is_type = 1;
8245
e28d52cf
DS
8246 if ((i = check_user_alignment (align_expr, false)) == -1
8247 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
d19fa6b5 8248 *no_add_attrs = true;
349ae713
NB
8249 else if (is_type)
8250 {
0f559c16
JM
8251 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8252 /* OK, modify the type in place. */;
349ae713
NB
8253 /* If we have a TYPE_DECL, then copy the type, so that we
8254 don't accidentally modify a builtin type. See pushdecl. */
0f559c16
JM
8255 else if (decl && TREE_TYPE (decl) != error_mark_node
8256 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
349ae713
NB
8257 {
8258 tree tt = TREE_TYPE (decl);
8dd16ecc 8259 *type = build_variant_type_copy (*type);
349ae713
NB
8260 DECL_ORIGINAL_TYPE (decl) = tt;
8261 TYPE_NAME (*type) = decl;
8262 TREE_USED (*type) = TREE_USED (decl);
8263 TREE_TYPE (decl) = *type;
8264 }
0f559c16 8265 else
8dd16ecc 8266 *type = build_variant_type_copy (*type);
349ae713 8267
d9223014 8268 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
349ae713
NB
8269 TYPE_USER_ALIGN (*type) = 1;
8270 }
837edd5f 8271 else if (! VAR_OR_FUNCTION_DECL_P (decl)
349ae713
NB
8272 && TREE_CODE (decl) != FIELD_DECL)
8273 {
dee15844 8274 error ("alignment may not be specified for %q+D", decl);
349ae713
NB
8275 *no_add_attrs = true;
8276 }
e28d52cf
DS
8277 else if (DECL_USER_ALIGN (decl)
8278 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8279 /* C++-11 [dcl.align/4]:
8280
8281 When multiple alignment-specifiers are specified for an
8282 entity, the alignment requirement shall be set to the
8283 strictest specified alignment.
8284
8285 This formally comes from the c++11 specification but we are
8286 doing it for the GNU attribute syntax as well. */
8287 *no_add_attrs = true;
837edd5f 8288 else if (TREE_CODE (decl) == FUNCTION_DECL
d9223014 8289 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
837edd5f
GK
8290 {
8291 if (DECL_USER_ALIGN (decl))
8292 error ("alignment for %q+D was previously specified as %d "
8293 "and may not be decreased", decl,
8294 DECL_ALIGN (decl) / BITS_PER_UNIT);
8295 else
8296 error ("alignment for %q+D must be at least %d", decl,
8297 DECL_ALIGN (decl) / BITS_PER_UNIT);
8298 *no_add_attrs = true;
8299 }
349ae713
NB
8300 else
8301 {
d9223014 8302 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
349ae713
NB
8303 DECL_USER_ALIGN (decl) = 1;
8304 }
8305
8306 return NULL_TREE;
8307}
8308
8309/* Handle a "weak" attribute; arguments as in
8310 struct attribute_spec.handler. */
8311
8312static tree
55af93a8 8313handle_weak_attribute (tree *node, tree name,
e18476eb
BI
8314 tree ARG_UNUSED (args),
8315 int ARG_UNUSED (flags),
8316 bool * ARG_UNUSED (no_add_attrs))
349ae713 8317{
55af93a8 8318 if (TREE_CODE (*node) == FUNCTION_DECL
6b4e94bc
RG
8319 && DECL_DECLARED_INLINE_P (*node))
8320 {
2aa9c6ae 8321 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6b4e94bc
RG
8322 *no_add_attrs = true;
8323 }
ba885ec5
NS
8324 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8325 {
8326 error ("indirect function %q+D cannot be declared weak", *node);
8327 *no_add_attrs = true;
8328 return NULL_TREE;
8329 }
21b634ae 8330 else if (VAR_OR_FUNCTION_DECL_P (*node))
c316b5e4 8331 declare_weak (*node);
55af93a8
DS
8332 else
8333 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8334
8335 return NULL_TREE;
8336}
8337
4bb794e2
ST
8338/* Handle a "noplt" attribute; arguments as in
8339 struct attribute_spec.handler. */
8340
8341static tree
8342handle_noplt_attribute (tree *node, tree name,
8343 tree ARG_UNUSED (args),
8344 int ARG_UNUSED (flags),
8345 bool * ARG_UNUSED (no_add_attrs))
8346{
8347 if (TREE_CODE (*node) != FUNCTION_DECL)
8348 {
8349 warning (OPT_Wattributes,
8350 "%qE attribute is only applicable on functions", name);
8351 *no_add_attrs = true;
8352 return NULL_TREE;
8353 }
8354 return NULL_TREE;
8355}
8356
ba885ec5
NS
8357/* Handle an "alias" or "ifunc" attribute; arguments as in
8358 struct attribute_spec.handler, except that IS_ALIAS tells us
8359 whether this is an alias as opposed to ifunc attribute. */
349ae713
NB
8360
8361static tree
ba885ec5
NS
8362handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8363 bool *no_add_attrs)
349ae713
NB
8364{
8365 tree decl = *node;
8366
ba885ec5 8367 if (TREE_CODE (decl) != FUNCTION_DECL
0ae9bd27 8368 && (!is_alias || !VAR_P (decl)))
feab5a67
JM
8369 {
8370 warning (OPT_Wattributes, "%qE attribute ignored", name);
8371 *no_add_attrs = true;
8372 }
8373 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
b8698a0f 8374 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825
GK
8375 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8376 /* A static variable declaration is always a tentative definition,
8377 but the alias is a non-tentative definition which overrides. */
b8698a0f 8378 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825 8379 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
349ae713 8380 {
ba885ec5 8381 error ("%q+D defined both normally and as %qE attribute", decl, name);
349ae713 8382 *no_add_attrs = true;
ba885ec5 8383 return NULL_TREE;
349ae713 8384 }
ba885ec5
NS
8385 else if (!is_alias
8386 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8387 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8388 {
8389 error ("weak %q+D cannot be defined %qE", decl, name);
8390 *no_add_attrs = true;
8391 return NULL_TREE;
8392 }
f6a76b9f
RH
8393
8394 /* Note that the very first time we process a nested declaration,
8395 decl_function_context will not be set. Indeed, *would* never
8396 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8397 we do below. After such frobbery, pushdecl would set the context.
8398 In any case, this is never what we want. */
8399 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
349ae713
NB
8400 {
8401 tree id;
8402
8403 id = TREE_VALUE (args);
8404 if (TREE_CODE (id) != STRING_CST)
8405 {
ba885ec5 8406 error ("attribute %qE argument not a string", name);
349ae713
NB
8407 *no_add_attrs = true;
8408 return NULL_TREE;
8409 }
8410 id = get_identifier (TREE_STRING_POINTER (id));
8411 /* This counts as a use of the object pointed to. */
8412 TREE_USED (id) = 1;
8413
8414 if (TREE_CODE (decl) == FUNCTION_DECL)
8415 DECL_INITIAL (decl) = error_mark_node;
8416 else
08346abd 8417 TREE_STATIC (decl) = 1;
ba885ec5
NS
8418
8419 if (!is_alias)
8420 /* ifuncs are also aliases, so set that attribute too. */
8421 DECL_ATTRIBUTES (decl)
8422 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
349ae713
NB
8423 }
8424 else
8425 {
5c498b10 8426 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8427 *no_add_attrs = true;
8428 }
8429
f7217cde
JH
8430 if (decl_in_symtab_p (*node))
8431 {
8432 struct symtab_node *n = symtab_node::get (decl);
8433 if (n && n->refuse_visibility_changes)
8434 {
8435 if (is_alias)
8436 error ("%+D declared alias after being used", decl);
8437 else
8438 error ("%+D declared ifunc after being used", decl);
8439 }
8440 }
8441
8442
349ae713
NB
8443 return NULL_TREE;
8444}
8445
ba885ec5
NS
8446/* Handle an "alias" or "ifunc" attribute; arguments as in
8447 struct attribute_spec.handler. */
8448
8449static tree
8450handle_ifunc_attribute (tree *node, tree name, tree args,
8451 int ARG_UNUSED (flags), bool *no_add_attrs)
8452{
8453 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8454}
8455
8456/* Handle an "alias" or "ifunc" attribute; arguments as in
8457 struct attribute_spec.handler. */
8458
8459static tree
8460handle_alias_attribute (tree *node, tree name, tree args,
8461 int ARG_UNUSED (flags), bool *no_add_attrs)
8462{
8463 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8464}
8465
a0203ca7
AO
8466/* Handle a "weakref" attribute; arguments as in struct
8467 attribute_spec.handler. */
8468
8469static tree
8470handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8471 int flags, bool *no_add_attrs)
8472{
8473 tree attr = NULL_TREE;
8474
e1cf56b1
AO
8475 /* We must ignore the attribute when it is associated with
8476 local-scoped decls, since attribute alias is ignored and many
8477 such symbols do not even have a DECL_WEAK field. */
e7b012c0
JJ
8478 if (decl_function_context (*node)
8479 || current_function_decl
21b634ae 8480 || !VAR_OR_FUNCTION_DECL_P (*node))
e1cf56b1
AO
8481 {
8482 warning (OPT_Wattributes, "%qE attribute ignored", name);
8483 *no_add_attrs = true;
8484 return NULL_TREE;
8485 }
8486
ba885ec5
NS
8487 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8488 {
8489 error ("indirect function %q+D cannot be declared weakref", *node);
8490 *no_add_attrs = true;
8491 return NULL_TREE;
8492 }
8493
a0203ca7
AO
8494 /* The idea here is that `weakref("name")' mutates into `weakref,
8495 alias("name")', and weakref without arguments, in turn,
8496 implicitly adds weak. */
8497
8498 if (args)
8499 {
8500 attr = tree_cons (get_identifier ("alias"), args, attr);
8501 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8502
8503 *no_add_attrs = true;
a9b0b825
GK
8504
8505 decl_attributes (node, attr, flags);
a0203ca7
AO
8506 }
8507 else
8508 {
8509 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
c5d75364
MLI
8510 error_at (DECL_SOURCE_LOCATION (*node),
8511 "weakref attribute must appear before alias attribute");
a0203ca7 8512
a9b0b825
GK
8513 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8514 and that isn't supported; and because it wants to add it to
8515 the list of weak decls, which isn't helpful. */
8516 DECL_WEAK (*node) = 1;
a0203ca7
AO
8517 }
8518
f7217cde
JH
8519 if (decl_in_symtab_p (*node))
8520 {
8521 struct symtab_node *n = symtab_node::get (*node);
8522 if (n && n->refuse_visibility_changes)
8523 error ("%+D declared weakref after being used", *node);
8524 }
8525
a0203ca7
AO
8526 return NULL_TREE;
8527}
8528
349ae713
NB
8529/* Handle an "visibility" attribute; arguments as in
8530 struct attribute_spec.handler. */
8531
8532static tree
35b1a6fa 8533handle_visibility_attribute (tree *node, tree name, tree args,
e18476eb 8534 int ARG_UNUSED (flags),
b9e75696 8535 bool *ARG_UNUSED (no_add_attrs))
349ae713
NB
8536{
8537 tree decl = *node;
968b41a1 8538 tree id = TREE_VALUE (args);
b9e75696 8539 enum symbol_visibility vis;
349ae713 8540
d7afec4b
ND
8541 if (TYPE_P (*node))
8542 {
b9e75696
JM
8543 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8544 /* OK */;
8545 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8546 {
8547 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8548 name);
8549 return NULL_TREE;
8550 }
8551 else if (TYPE_FIELDS (*node))
8552 {
8553 error ("%qE attribute ignored because %qT is already defined",
8554 name, *node);
8555 return NULL_TREE;
8556 }
d7afec4b 8557 }
3f75a254 8558 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
349ae713 8559 {
5c498b10 8560 warning (OPT_Wattributes, "%qE attribute ignored", name);
968b41a1 8561 return NULL_TREE;
349ae713 8562 }
349ae713 8563
968b41a1
MA
8564 if (TREE_CODE (id) != STRING_CST)
8565 {
40b97a2e 8566 error ("visibility argument not a string");
968b41a1 8567 return NULL_TREE;
349ae713 8568 }
9f63daea 8569
d7afec4b
ND
8570 /* If this is a type, set the visibility on the type decl. */
8571 if (TYPE_P (decl))
8572 {
8573 decl = TYPE_NAME (decl);
3f75a254 8574 if (!decl)
c22cacf3 8575 return NULL_TREE;
e8233ac2
AP
8576 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8577 {
5c498b10 8578 warning (OPT_Wattributes, "%qE attribute ignored on types",
e8233ac2
AP
8579 name);
8580 return NULL_TREE;
8581 }
d7afec4b 8582 }
349ae713 8583
968b41a1 8584 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
b9e75696 8585 vis = VISIBILITY_DEFAULT;
968b41a1 8586 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
b9e75696 8587 vis = VISIBILITY_INTERNAL;
968b41a1 8588 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
b9e75696 8589 vis = VISIBILITY_HIDDEN;
968b41a1 8590 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
b9e75696 8591 vis = VISIBILITY_PROTECTED;
968b41a1 8592 else
b9e75696
JM
8593 {
8594 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8595 vis = VISIBILITY_DEFAULT;
8596 }
8597
8598 if (DECL_VISIBILITY_SPECIFIED (decl)
3a687f8b
MM
8599 && vis != DECL_VISIBILITY (decl))
8600 {
8601 tree attributes = (TYPE_P (*node)
8602 ? TYPE_ATTRIBUTES (*node)
8603 : DECL_ATTRIBUTES (decl));
8604 if (lookup_attribute ("visibility", attributes))
8605 error ("%qD redeclared with different visibility", decl);
8606 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8607 && lookup_attribute ("dllimport", attributes))
8608 error ("%qD was declared %qs which implies default visibility",
8609 decl, "dllimport");
8610 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8611 && lookup_attribute ("dllexport", attributes))
8612 error ("%qD was declared %qs which implies default visibility",
8613 decl, "dllexport");
8614 }
b9e75696
JM
8615
8616 DECL_VISIBILITY (decl) = vis;
d7afec4b
ND
8617 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8618
b9e75696
JM
8619 /* Go ahead and attach the attribute to the node as well. This is needed
8620 so we can determine whether we have VISIBILITY_DEFAULT because the
8621 visibility was not specified, or because it was explicitly overridden
8622 from the containing scope. */
968b41a1 8623
349ae713
NB
8624 return NULL_TREE;
8625}
8626
b2ca3702
MM
8627/* Determine the ELF symbol visibility for DECL, which is either a
8628 variable or a function. It is an error to use this function if a
8629 definition of DECL is not available in this translation unit.
8630 Returns true if the final visibility has been determined by this
8631 function; false if the caller is free to make additional
8632 modifications. */
8633
8634bool
8635c_determine_visibility (tree decl)
8636{
21b634ae 8637 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
b2ca3702
MM
8638
8639 /* If the user explicitly specified the visibility with an
8640 attribute, honor that. DECL_VISIBILITY will have been set during
6d87092d
JM
8641 the processing of the attribute. We check for an explicit
8642 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8643 to distinguish the use of an attribute from the use of a "#pragma
8644 GCC visibility push(...)"; in the latter case we still want other
8645 considerations to be able to overrule the #pragma. */
8646 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8647 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8648 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8649 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
b2ca3702
MM
8650 return true;
8651
b9e75696
JM
8652 /* Set default visibility to whatever the user supplied with
8653 visibility_specified depending on #pragma GCC visibility. */
8654 if (!DECL_VISIBILITY_SPECIFIED (decl))
8655 {
09812622
JJ
8656 if (visibility_options.inpragma
8657 || DECL_VISIBILITY (decl) != default_visibility)
8658 {
8659 DECL_VISIBILITY (decl) = default_visibility;
8660 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8661 /* If visibility changed and DECL already has DECL_RTL, ensure
8662 symbol flags are updated. */
0ae9bd27 8663 if (((VAR_P (decl) && TREE_STATIC (decl))
09812622
JJ
8664 || TREE_CODE (decl) == FUNCTION_DECL)
8665 && DECL_RTL_SET_P (decl))
8666 make_decl_rtl (decl);
8667 }
b9e75696 8668 }
b2ca3702
MM
8669 return false;
8670}
8671
dce81a1a
JJ
8672/* Handle an "tls_model" attribute; arguments as in
8673 struct attribute_spec.handler. */
8674
8675static tree
35b1a6fa 8676handle_tls_model_attribute (tree *node, tree name, tree args,
e18476eb 8677 int ARG_UNUSED (flags), bool *no_add_attrs)
dce81a1a 8678{
c2f7fa15 8679 tree id;
dce81a1a 8680 tree decl = *node;
c2f7fa15 8681 enum tls_model kind;
dce81a1a 8682
c2f7fa15
SB
8683 *no_add_attrs = true;
8684
0ae9bd27 8685 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
dce81a1a 8686 {
5c498b10 8687 warning (OPT_Wattributes, "%qE attribute ignored", name);
c2f7fa15 8688 return NULL_TREE;
dce81a1a 8689 }
dce81a1a 8690
c2f7fa15
SB
8691 kind = DECL_TLS_MODEL (decl);
8692 id = TREE_VALUE (args);
8693 if (TREE_CODE (id) != STRING_CST)
8694 {
8695 error ("tls_model argument not a string");
8696 return NULL_TREE;
dce81a1a
JJ
8697 }
8698
c2f7fa15
SB
8699 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8700 kind = TLS_MODEL_LOCAL_EXEC;
8701 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8702 kind = TLS_MODEL_INITIAL_EXEC;
8703 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8704 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8705 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8706 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8707 else
8708 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8709
56363ffd 8710 set_decl_tls_model (decl, kind);
dce81a1a
JJ
8711 return NULL_TREE;
8712}
8713
349ae713
NB
8714/* Handle a "no_instrument_function" attribute; arguments as in
8715 struct attribute_spec.handler. */
8716
8717static tree
35b1a6fa 8718handle_no_instrument_function_attribute (tree *node, tree name,
e18476eb
BI
8719 tree ARG_UNUSED (args),
8720 int ARG_UNUSED (flags),
a742c759 8721 bool *no_add_attrs)
349ae713
NB
8722{
8723 tree decl = *node;
8724
8725 if (TREE_CODE (decl) != FUNCTION_DECL)
8726 {
c5d75364
MLI
8727 error_at (DECL_SOURCE_LOCATION (decl),
8728 "%qE attribute applies only to functions", name);
349ae713
NB
8729 *no_add_attrs = true;
8730 }
349ae713
NB
8731 else
8732 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8733
8734 return NULL_TREE;
8735}
8736
8737/* Handle a "malloc" attribute; arguments as in
8738 struct attribute_spec.handler. */
8739
8740static tree
e18476eb
BI
8741handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8742 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 8743{
3425638a
JM
8744 if (TREE_CODE (*node) == FUNCTION_DECL
8745 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
349ae713 8746 DECL_IS_MALLOC (*node) = 1;
349ae713
NB
8747 else
8748 {
5c498b10 8749 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8750 *no_add_attrs = true;
8751 }
8752
8753 return NULL_TREE;
8754}
8755
51bc54a6
DM
8756/* Handle a "alloc_size" attribute; arguments as in
8757 struct attribute_spec.handler. */
8758
8759static tree
8760handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8761 int ARG_UNUSED (flags), bool *no_add_attrs)
8762{
f3f75f69 8763 unsigned arg_count = type_num_arguments (*node);
51bc54a6
DM
8764 for (; args; args = TREE_CHAIN (args))
8765 {
8766 tree position = TREE_VALUE (args);
5d77fb19
MG
8767 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8768 && TREE_CODE (position) != FUNCTION_DECL)
8769 position = default_conversion (position);
51bc54a6 8770
8fcbce72
JJ
8771 if (!tree_fits_uhwi_p (position)
8772 || !arg_count
8773 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
51bc54a6 8774 {
b8698a0f 8775 warning (OPT_Wattributes,
51bc54a6
DM
8776 "alloc_size parameter outside range");
8777 *no_add_attrs = true;
8778 return NULL_TREE;
8779 }
8780 }
8781 return NULL_TREE;
8782}
8783
8fcbce72
JJ
8784/* Handle a "alloc_align" attribute; arguments as in
8785 struct attribute_spec.handler. */
8786
8787static tree
8788handle_alloc_align_attribute (tree *node, tree, tree args, int,
8789 bool *no_add_attrs)
8790{
8791 unsigned arg_count = type_num_arguments (*node);
8792 tree position = TREE_VALUE (args);
8793 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8794 position = default_conversion (position);
8795
8796 if (!tree_fits_uhwi_p (position)
8797 || !arg_count
8798 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8799 {
8800 warning (OPT_Wattributes,
8801 "alloc_align parameter outside range");
8802 *no_add_attrs = true;
8803 return NULL_TREE;
8804 }
8805 return NULL_TREE;
8806}
8807
8808/* Handle a "assume_aligned" attribute; arguments as in
8809 struct attribute_spec.handler. */
8810
8811static tree
8812handle_assume_aligned_attribute (tree *, tree, tree args, int,
8813 bool *no_add_attrs)
8814{
8815 for (; args; args = TREE_CHAIN (args))
8816 {
8817 tree position = TREE_VALUE (args);
8818 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8819 && TREE_CODE (position) != FUNCTION_DECL)
8820 position = default_conversion (position);
8821
8822 if (TREE_CODE (position) != INTEGER_CST)
8823 {
8824 warning (OPT_Wattributes,
8825 "assume_aligned parameter not integer constant");
8826 *no_add_attrs = true;
8827 return NULL_TREE;
8828 }
8829 }
8830 return NULL_TREE;
8831}
8832
0b7b376d
RG
8833/* Handle a "fn spec" attribute; arguments as in
8834 struct attribute_spec.handler. */
8835
8836static tree
8837handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8838 tree args, int ARG_UNUSED (flags),
8839 bool *no_add_attrs ATTRIBUTE_UNUSED)
8840{
8841 gcc_assert (args
8842 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8843 && !TREE_CHAIN (args));
8844 return NULL_TREE;
8845}
8846
d5e254e1
IE
8847/* Handle a "bnd_variable_size" attribute; arguments as in
8848 struct attribute_spec.handler. */
8849
8850static tree
8851handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8852 int ARG_UNUSED (flags), bool *no_add_attrs)
8853{
8854 if (TREE_CODE (*node) != FIELD_DECL)
8855 {
8856 warning (OPT_Wattributes, "%qE attribute ignored", name);
8857 *no_add_attrs = true;
8858 }
8859
8860 return NULL_TREE;
8861}
8862
8863/* Handle a "bnd_legacy" attribute; arguments as in
8864 struct attribute_spec.handler. */
8865
8866static tree
8867handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8868 int ARG_UNUSED (flags), bool *no_add_attrs)
8869{
8870 if (TREE_CODE (*node) != FUNCTION_DECL)
8871 {
8872 warning (OPT_Wattributes, "%qE attribute ignored", name);
8873 *no_add_attrs = true;
8874 }
8875
8876 return NULL_TREE;
8877}
8878
8879/* Handle a "bnd_instrument" attribute; arguments as in
8880 struct attribute_spec.handler. */
8881
8882static tree
8883handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8884 int ARG_UNUSED (flags), bool *no_add_attrs)
8885{
8886 if (TREE_CODE (*node) != FUNCTION_DECL)
8887 {
8888 warning (OPT_Wattributes, "%qE attribute ignored", name);
8889 *no_add_attrs = true;
8890 }
8891
8892 return NULL_TREE;
8893}
8894
2a99e5e6
LL
8895/* Handle a "warn_unused" attribute; arguments as in
8896 struct attribute_spec.handler. */
8897
8898static tree
8899handle_warn_unused_attribute (tree *node, tree name,
8900 tree args ATTRIBUTE_UNUSED,
8901 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8902{
8903 if (TYPE_P (*node))
8904 /* Do nothing else, just set the attribute. We'll get at
8905 it later with lookup_attribute. */
8906 ;
8907 else
8908 {
8909 warning (OPT_Wattributes, "%qE attribute ignored", name);
8910 *no_add_attrs = true;
8911 }
8912
8913 return NULL_TREE;
8914}
8915
acf0174b
JJ
8916/* Handle an "omp declare simd" attribute; arguments as in
8917 struct attribute_spec.handler. */
8918
8919static tree
8920handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8921{
8922 return NULL_TREE;
8923}
8924
8925/* Handle an "omp declare target" attribute; arguments as in
8926 struct attribute_spec.handler. */
8927
8928static tree
8929handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8930{
8931 return NULL_TREE;
8932}
8933
6e9a3221
AN
8934/* Handle a "returns_twice" attribute; arguments as in
8935 struct attribute_spec.handler. */
8936
8937static tree
8938handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8939 int ARG_UNUSED (flags), bool *no_add_attrs)
8940{
8941 if (TREE_CODE (*node) == FUNCTION_DECL)
8942 DECL_IS_RETURNS_TWICE (*node) = 1;
8943 else
8944 {
5c498b10 8945 warning (OPT_Wattributes, "%qE attribute ignored", name);
6e9a3221
AN
8946 *no_add_attrs = true;
8947 }
8948
8949 return NULL_TREE;
8950}
8951
349ae713
NB
8952/* Handle a "no_limit_stack" attribute; arguments as in
8953 struct attribute_spec.handler. */
8954
8955static tree
35b1a6fa 8956handle_no_limit_stack_attribute (tree *node, tree name,
e18476eb
BI
8957 tree ARG_UNUSED (args),
8958 int ARG_UNUSED (flags),
a742c759 8959 bool *no_add_attrs)
349ae713
NB
8960{
8961 tree decl = *node;
8962
8963 if (TREE_CODE (decl) != FUNCTION_DECL)
8964 {
c5d75364
MLI
8965 error_at (DECL_SOURCE_LOCATION (decl),
8966 "%qE attribute applies only to functions", name);
349ae713
NB
8967 *no_add_attrs = true;
8968 }
8969 else if (DECL_INITIAL (decl))
8970 {
c5d75364
MLI
8971 error_at (DECL_SOURCE_LOCATION (decl),
8972 "can%'t set %qE attribute after definition", name);
349ae713
NB
8973 *no_add_attrs = true;
8974 }
8975 else
8976 DECL_NO_LIMIT_STACK (decl) = 1;
8977
8978 return NULL_TREE;
8979}
8980
8981/* Handle a "pure" attribute; arguments as in
8982 struct attribute_spec.handler. */
8983
8984static tree
e18476eb
BI
8985handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8986 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
8987{
8988 if (TREE_CODE (*node) == FUNCTION_DECL)
becfd6e5 8989 DECL_PURE_P (*node) = 1;
349ae713
NB
8990 /* ??? TODO: Support types. */
8991 else
8992 {
5c498b10 8993 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8994 *no_add_attrs = true;
8995 }
8996
8997 return NULL_TREE;
8998}
8999
0a35513e
AH
9000/* Digest an attribute list destined for a transactional memory statement.
9001 ALLOWED is the set of attributes that are allowed for this statement;
9002 return the attribute we parsed. Multiple attributes are never allowed. */
9003
9004int
9005parse_tm_stmt_attr (tree attrs, int allowed)
9006{
9007 tree a_seen = NULL;
9008 int m_seen = 0;
9009
9010 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
9011 {
9012 tree a = TREE_PURPOSE (attrs);
9013 int m = 0;
9014
9015 if (is_attribute_p ("outer", a))
9016 m = TM_STMT_ATTR_OUTER;
9017
9018 if ((m & allowed) == 0)
9019 {
9020 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
9021 continue;
9022 }
9023
9024 if (m_seen == 0)
9025 {
9026 a_seen = a;
9027 m_seen = m;
9028 }
9029 else if (m_seen == m)
9030 warning (OPT_Wattributes, "%qE attribute duplicated", a);
9031 else
9032 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
9033 }
9034
9035 return m_seen;
9036}
9037
9038/* Transform a TM attribute name into a maskable integer and back.
9039 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
9040 to how the lack of an attribute is treated. */
9041
9042int
9043tm_attr_to_mask (tree attr)
9044{
9045 if (attr == NULL)
9046 return 0;
9047 if (is_attribute_p ("transaction_safe", attr))
9048 return TM_ATTR_SAFE;
9049 if (is_attribute_p ("transaction_callable", attr))
9050 return TM_ATTR_CALLABLE;
9051 if (is_attribute_p ("transaction_pure", attr))
9052 return TM_ATTR_PURE;
9053 if (is_attribute_p ("transaction_unsafe", attr))
9054 return TM_ATTR_IRREVOCABLE;
9055 if (is_attribute_p ("transaction_may_cancel_outer", attr))
9056 return TM_ATTR_MAY_CANCEL_OUTER;
9057 return 0;
9058}
9059
9060tree
9061tm_mask_to_attr (int mask)
9062{
9063 const char *str;
9064 switch (mask)
9065 {
9066 case TM_ATTR_SAFE:
9067 str = "transaction_safe";
9068 break;
9069 case TM_ATTR_CALLABLE:
9070 str = "transaction_callable";
9071 break;
9072 case TM_ATTR_PURE:
9073 str = "transaction_pure";
9074 break;
9075 case TM_ATTR_IRREVOCABLE:
9076 str = "transaction_unsafe";
9077 break;
9078 case TM_ATTR_MAY_CANCEL_OUTER:
9079 str = "transaction_may_cancel_outer";
9080 break;
9081 default:
9082 gcc_unreachable ();
9083 }
9084 return get_identifier (str);
9085}
9086
9087/* Return the first TM attribute seen in LIST. */
9088
9089tree
9090find_tm_attribute (tree list)
9091{
9092 for (; list ; list = TREE_CHAIN (list))
9093 {
9094 tree name = TREE_PURPOSE (list);
9095 if (tm_attr_to_mask (name) != 0)
9096 return name;
9097 }
9098 return NULL_TREE;
9099}
9100
9101/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
9102 Here we accept only function types, and verify that none of the other
9103 function TM attributes are also applied. */
9104/* ??? We need to accept class types for C++, but not C. This greatly
9105 complicates this function, since we can no longer rely on the extra
9106 processing given by function_type_required. */
9107
9108static tree
9109handle_tm_attribute (tree *node, tree name, tree args,
9110 int flags, bool *no_add_attrs)
9111{
9112 /* Only one path adds the attribute; others don't. */
9113 *no_add_attrs = true;
9114
9115 switch (TREE_CODE (*node))
9116 {
9117 case RECORD_TYPE:
9118 case UNION_TYPE:
9119 /* Only tm_callable and tm_safe apply to classes. */
9120 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
9121 goto ignored;
9122 /* FALLTHRU */
9123
9124 case FUNCTION_TYPE:
9125 case METHOD_TYPE:
9126 {
9127 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
9128 if (old_name == name)
9129 ;
9130 else if (old_name != NULL_TREE)
9131 error ("type was previously declared %qE", old_name);
9132 else
9133 *no_add_attrs = false;
9134 }
9135 break;
9136
9137 case POINTER_TYPE:
9138 {
9139 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
9140 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
9141 {
9142 tree fn_tmp = TREE_TYPE (*node);
9143 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
9144 *node = build_pointer_type (fn_tmp);
9145 break;
9146 }
9147 }
9148 /* FALLTHRU */
9149
9150 default:
9151 /* If a function is next, pass it on to be tried next. */
9152 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
9153 return tree_cons (name, args, NULL);
9154
9155 ignored:
9156 warning (OPT_Wattributes, "%qE attribute ignored", name);
9157 break;
9158 }
9159
9160 return NULL_TREE;
9161}
9162
9163/* Handle the TM_WRAP attribute; arguments as in
9164 struct attribute_spec.handler. */
9165
9166static tree
9167handle_tm_wrap_attribute (tree *node, tree name, tree args,
9168 int ARG_UNUSED (flags), bool *no_add_attrs)
9169{
9170 tree decl = *node;
9171
9172 /* We don't need the attribute even on success, since we
9173 record the entry in an external table. */
9174 *no_add_attrs = true;
9175
9176 if (TREE_CODE (decl) != FUNCTION_DECL)
9177 warning (OPT_Wattributes, "%qE attribute ignored", name);
9178 else
9179 {
9180 tree wrap_decl = TREE_VALUE (args);
661a0813
MP
9181 if (error_operand_p (wrap_decl))
9182 ;
9183 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
21b634ae 9184 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
0a35513e
AH
9185 error ("%qE argument not an identifier", name);
9186 else
9187 {
9188 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9189 wrap_decl = lookup_name (wrap_decl);
9190 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9191 {
9192 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9193 TREE_TYPE (wrap_decl)))
9194 record_tm_replacement (wrap_decl, decl);
9195 else
9196 error ("%qD is not compatible with %qD", wrap_decl, decl);
9197 }
9198 else
c07d7c02 9199 error ("%qE argument is not a function", name);
0a35513e
AH
9200 }
9201 }
9202
9203 return NULL_TREE;
9204}
9205
9206/* Ignore the given attribute. Used when this attribute may be usefully
9207 overridden by the target, but is not used generically. */
9208
9209static tree
9210ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9211 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9212 bool *no_add_attrs)
9213{
9214 *no_add_attrs = true;
9215 return NULL_TREE;
9216}
9217
dcd6de6d
ZD
9218/* Handle a "no vops" attribute; arguments as in
9219 struct attribute_spec.handler. */
9220
9221static tree
9222handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9223 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9224 bool *ARG_UNUSED (no_add_attrs))
9225{
9226 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9227 DECL_IS_NOVOPS (*node) = 1;
9228 return NULL_TREE;
9229}
9230
349ae713
NB
9231/* Handle a "deprecated" attribute; arguments as in
9232 struct attribute_spec.handler. */
35b1a6fa 9233
349ae713 9234static tree
35b1a6fa 9235handle_deprecated_attribute (tree *node, tree name,
9b86d6bb 9236 tree args, int flags,
a742c759 9237 bool *no_add_attrs)
349ae713
NB
9238{
9239 tree type = NULL_TREE;
9240 int warn = 0;
c51a1ba9 9241 tree what = NULL_TREE;
35b1a6fa 9242
9b86d6bb
L
9243 if (!args)
9244 *no_add_attrs = true;
9245 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9246 {
9247 error ("deprecated message is not a string");
9248 *no_add_attrs = true;
9249 }
9250
349ae713
NB
9251 if (DECL_P (*node))
9252 {
9253 tree decl = *node;
9254 type = TREE_TYPE (decl);
35b1a6fa 9255
349ae713
NB
9256 if (TREE_CODE (decl) == TYPE_DECL
9257 || TREE_CODE (decl) == PARM_DECL
21b634ae 9258 || VAR_OR_FUNCTION_DECL_P (decl)
a1178b30 9259 || TREE_CODE (decl) == FIELD_DECL
fd5c817a 9260 || TREE_CODE (decl) == CONST_DECL
a1178b30 9261 || objc_method_decl (TREE_CODE (decl)))
349ae713
NB
9262 TREE_DEPRECATED (decl) = 1;
9263 else
9264 warn = 1;
9265 }
9266 else if (TYPE_P (*node))
9267 {
9268 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 9269 *node = build_variant_type_copy (*node);
349ae713
NB
9270 TREE_DEPRECATED (*node) = 1;
9271 type = *node;
9272 }
9273 else
9274 warn = 1;
35b1a6fa 9275
349ae713
NB
9276 if (warn)
9277 {
9278 *no_add_attrs = true;
9279 if (type && TYPE_NAME (type))
9280 {
9281 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9 9282 what = TYPE_NAME (*node);
349ae713
NB
9283 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9284 && DECL_NAME (TYPE_NAME (type)))
c51a1ba9 9285 what = DECL_NAME (TYPE_NAME (type));
349ae713
NB
9286 }
9287 if (what)
5c498b10 9288 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
349ae713 9289 else
5c498b10 9290 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
9291 }
9292
9293 return NULL_TREE;
9294}
9295
349ae713
NB
9296/* Handle a "vector_size" attribute; arguments as in
9297 struct attribute_spec.handler. */
9298
9299static tree
35b1a6fa 9300handle_vector_size_attribute (tree *node, tree name, tree args,
e18476eb 9301 int ARG_UNUSED (flags),
a742c759 9302 bool *no_add_attrs)
349ae713
NB
9303{
9304 unsigned HOST_WIDE_INT vecsize, nunits;
ef4bddc2 9305 machine_mode orig_mode;
4a5eab38 9306 tree type = *node, new_type, size;
349ae713
NB
9307
9308 *no_add_attrs = true;
9309
4a5eab38 9310 size = TREE_VALUE (args);
661a0813
MP
9311 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9312 && TREE_CODE (size) != FUNCTION_DECL)
5d77fb19 9313 size = default_conversion (size);
4a5eab38 9314
cc269bb6 9315 if (!tree_fits_uhwi_p (size))
349ae713 9316 {
5c498b10 9317 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
9318 return NULL_TREE;
9319 }
9320
9321 /* Get the vector size (in bytes). */
ae7e9ddd 9322 vecsize = tree_to_uhwi (size);
349ae713
NB
9323
9324 /* We need to provide for vector pointers, vector arrays, and
9325 functions returning vectors. For example:
9326
9327 __attribute__((vector_size(16))) short *foo;
9328
9329 In this case, the mode is SI, but the type being modified is
9330 HI, so we need to look further. */
9331
9332 while (POINTER_TYPE_P (type)
9333 || TREE_CODE (type) == FUNCTION_TYPE
43dc123f 9334 || TREE_CODE (type) == METHOD_TYPE
270e749d
JJ
9335 || TREE_CODE (type) == ARRAY_TYPE
9336 || TREE_CODE (type) == OFFSET_TYPE)
349ae713
NB
9337 type = TREE_TYPE (type);
9338
9339 /* Get the mode of the type being modified. */
9340 orig_mode = TYPE_MODE (type);
9341
270e749d
JJ
9342 if ((!INTEGRAL_TYPE_P (type)
9343 && !SCALAR_FLOAT_TYPE_P (type)
9344 && !FIXED_POINT_TYPE_P (type))
3d8bf70f 9345 || (!SCALAR_FLOAT_MODE_P (orig_mode)
ab22c1fa
CF
9346 && GET_MODE_CLASS (orig_mode) != MODE_INT
9347 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
cc269bb6 9348 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
e4e5261f 9349 || TREE_CODE (type) == BOOLEAN_TYPE)
349ae713 9350 {
c51a1ba9 9351 error ("invalid vector type for attribute %qE", name);
349ae713
NB
9352 return NULL_TREE;
9353 }
9354
ae7e9ddd 9355 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
ee8960e5
JJ
9356 {
9357 error ("vector size not an integral multiple of component size");
9358 return NULL;
9359 }
9360
9361 if (vecsize == 0)
9362 {
9363 error ("zero vector size");
9364 return NULL;
9365 }
9366
349ae713 9367 /* Calculate how many units fit in the vector. */
ae7e9ddd 9368 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
26277d41 9369 if (nunits & (nunits - 1))
349ae713 9370 {
26277d41 9371 error ("number of components of the vector not a power of two");
349ae713
NB
9372 return NULL_TREE;
9373 }
9374
26277d41 9375 new_type = build_vector_type (type, nunits);
349ae713
NB
9376
9377 /* Build back pointers if needed. */
5dc11954 9378 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
349ae713
NB
9379
9380 return NULL_TREE;
9381}
9382
b34c7881
JT
9383/* Handle the "nonnull" attribute. */
9384static tree
e18476eb
BI
9385handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9386 tree args, int ARG_UNUSED (flags),
a742c759 9387 bool *no_add_attrs)
b34c7881
JT
9388{
9389 tree type = *node;
9390 unsigned HOST_WIDE_INT attr_arg_num;
9391
9392 /* If no arguments are specified, all pointer arguments should be
95bd1dd7 9393 non-null. Verify a full prototype is given so that the arguments
b34c7881 9394 will have the correct types when we actually check them later. */
3f75a254 9395 if (!args)
b34c7881 9396 {
f4da8dce 9397 if (!prototype_p (type))
b34c7881
JT
9398 {
9399 error ("nonnull attribute without arguments on a non-prototype");
6de9cd9a 9400 *no_add_attrs = true;
b34c7881
JT
9401 }
9402 return NULL_TREE;
9403 }
9404
9405 /* Argument list specified. Verify that each argument number references
9406 a pointer argument. */
5d77fb19 9407 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
b34c7881 9408 {
6de9cd9a 9409 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
b34c7881 9410
5d77fb19
MG
9411 tree arg = TREE_VALUE (args);
9412 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9413 && TREE_CODE (arg) != FUNCTION_DECL)
9414 arg = default_conversion (arg);
9415
9416 if (!get_nonnull_operand (arg, &arg_num))
b34c7881 9417 {
40b97a2e 9418 error ("nonnull argument has invalid operand number (argument %lu)",
b34c7881
JT
9419 (unsigned long) attr_arg_num);
9420 *no_add_attrs = true;
9421 return NULL_TREE;
9422 }
9423
e19a18d4 9424 if (prototype_p (type))
b34c7881 9425 {
e19a18d4
NF
9426 function_args_iterator iter;
9427 tree argument;
9428
9429 function_args_iter_init (&iter, type);
9430 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
b34c7881 9431 {
e19a18d4
NF
9432 argument = function_args_iter_cond (&iter);
9433 if (argument == NULL_TREE || ck_num == arg_num)
b34c7881 9434 break;
b34c7881
JT
9435 }
9436
3f75a254 9437 if (!argument
e19a18d4 9438 || TREE_CODE (argument) == VOID_TYPE)
b34c7881 9439 {
40b97a2e 9440 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
b34c7881
JT
9441 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9442 *no_add_attrs = true;
9443 return NULL_TREE;
9444 }
9445
e19a18d4 9446 if (TREE_CODE (argument) != POINTER_TYPE)
b34c7881 9447 {
40b97a2e 9448 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
b34c7881
JT
9449 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9450 *no_add_attrs = true;
9451 return NULL_TREE;
9452 }
9453 }
9454 }
9455
9456 return NULL_TREE;
9457}
9458
9459/* Check the argument list of a function call for null in argument slots
94a0dd7b
SL
9460 that are marked as requiring a non-null pointer argument. The NARGS
9461 arguments are passed in the array ARGARRAY.
9462*/
b34c7881
JT
9463
9464static void
94a0dd7b 9465check_function_nonnull (tree attrs, int nargs, tree *argarray)
b34c7881 9466{
332f1d24 9467 tree a;
94a0dd7b 9468 int i;
b34c7881 9469
332f1d24
JJ
9470 attrs = lookup_attribute ("nonnull", attrs);
9471 if (attrs == NULL_TREE)
9472 return;
9473
9474 a = attrs;
9475 /* See if any of the nonnull attributes has no arguments. If so,
9476 then every pointer argument is checked (in which case the check
9477 for pointer type is done in check_nonnull_arg). */
9478 if (TREE_VALUE (a) != NULL_TREE)
9479 do
9480 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9481 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9482
9483 if (a != NULL_TREE)
9484 for (i = 0; i < nargs; i++)
9485 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9486 i + 1);
9487 else
b34c7881 9488 {
332f1d24
JJ
9489 /* Walk the argument list. If we encounter an argument number we
9490 should check for non-null, do it. */
9491 for (i = 0; i < nargs; i++)
b34c7881 9492 {
332f1d24 9493 for (a = attrs; ; a = TREE_CHAIN (a))
6de9cd9a 9494 {
332f1d24
JJ
9495 a = lookup_attribute ("nonnull", a);
9496 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9497 break;
6de9cd9a 9498 }
332f1d24
JJ
9499
9500 if (a != NULL_TREE)
9501 check_function_arguments_recurse (check_nonnull_arg, NULL,
9502 argarray[i], i + 1);
b34c7881
JT
9503 }
9504 }
9505}
9506
254986c7 9507/* Check that the Nth argument of a function call (counting backwards
94a0dd7b
SL
9508 from the end) is a (pointer)0. The NARGS arguments are passed in the
9509 array ARGARRAY. */
3d091dac
KG
9510
9511static void
dde05067 9512check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
3d091dac 9513{
dde05067 9514 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
3d091dac
KG
9515
9516 if (attr)
9517 {
94a0dd7b
SL
9518 int len = 0;
9519 int pos = 0;
9520 tree sentinel;
dde05067
NF
9521 function_args_iterator iter;
9522 tree t;
c22cacf3 9523
94a0dd7b 9524 /* Skip over the named arguments. */
dde05067 9525 FOREACH_FUNCTION_ARGS (fntype, t, iter)
c22cacf3 9526 {
dde05067
NF
9527 if (len == nargs)
9528 break;
94a0dd7b
SL
9529 len++;
9530 }
254986c7 9531
94a0dd7b
SL
9532 if (TREE_VALUE (attr))
9533 {
9534 tree p = TREE_VALUE (TREE_VALUE (attr));
9535 pos = TREE_INT_CST_LOW (p);
9536 }
254986c7 9537
94a0dd7b
SL
9538 /* The sentinel must be one of the varargs, i.e.
9539 in position >= the number of fixed arguments. */
9540 if ((nargs - 1 - pos) < len)
9541 {
7332899a 9542 warning (OPT_Wformat_,
94a0dd7b
SL
9543 "not enough variable arguments to fit a sentinel");
9544 return;
3d091dac 9545 }
94a0dd7b
SL
9546
9547 /* Validate the sentinel. */
9548 sentinel = argarray[nargs - 1 - pos];
9549 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9550 || !integer_zerop (sentinel))
9551 /* Although __null (in C++) is only an integer we allow it
9552 nevertheless, as we are guaranteed that it's exactly
9553 as wide as a pointer, and we don't want to force
9554 users to cast the NULL they have written there.
9555 We warn with -Wstrict-null-sentinel, though. */
9556 && (warn_strict_null_sentinel || null_node != sentinel))
7332899a 9557 warning (OPT_Wformat_, "missing sentinel in function call");
3d091dac
KG
9558 }
9559}
9560
b34c7881
JT
9561/* Helper for check_function_nonnull; given a list of operands which
9562 must be non-null in ARGS, determine if operand PARAM_NUM should be
9563 checked. */
9564
9565static bool
35b1a6fa 9566nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
b34c7881 9567{
6de9cd9a 9568 unsigned HOST_WIDE_INT arg_num = 0;
b34c7881
JT
9569
9570 for (; args; args = TREE_CHAIN (args))
9571 {
366de0ce
NS
9572 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9573
9574 gcc_assert (found);
b34c7881
JT
9575
9576 if (arg_num == param_num)
9577 return true;
9578 }
9579 return false;
9580}
9581
9582/* Check that the function argument PARAM (which is operand number
9583 PARAM_NUM) is non-null. This is called by check_function_nonnull
9584 via check_function_arguments_recurse. */
9585
9586static void
e18476eb 9587check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
35b1a6fa 9588 unsigned HOST_WIDE_INT param_num)
b34c7881
JT
9589{
9590 /* Just skip checking the argument if it's not a pointer. This can
9591 happen if the "nonnull" attribute was given without an operand
9592 list (which means to check every pointer argument). */
9593
9594 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9595 return;
9596
9597 if (integer_zerop (param))
30480ec4
DD
9598 warning (OPT_Wnonnull, "null argument where non-null required "
9599 "(argument %lu)", (unsigned long) param_num);
b34c7881
JT
9600}
9601
9602/* Helper for nonnull attribute handling; fetch the operand number
9603 from the attribute argument list. */
9604
9605static bool
35b1a6fa 9606get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
b34c7881 9607{
807e902e
KZ
9608 /* Verify the arg number is a small constant. */
9609 if (tree_fits_uhwi_p (arg_num_expr))
9610 {
9611 *valp = TREE_INT_CST_LOW (arg_num_expr);
9612 return true;
9613 }
9614 else
b34c7881 9615 return false;
b34c7881 9616}
39f2f3c8
RS
9617
9618/* Handle a "nothrow" attribute; arguments as in
9619 struct attribute_spec.handler. */
9620
9621static tree
e18476eb
BI
9622handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9623 int ARG_UNUSED (flags), bool *no_add_attrs)
39f2f3c8
RS
9624{
9625 if (TREE_CODE (*node) == FUNCTION_DECL)
9626 TREE_NOTHROW (*node) = 1;
9627 /* ??? TODO: Support types. */
9628 else
9629 {
5c498b10 9630 warning (OPT_Wattributes, "%qE attribute ignored", name);
39f2f3c8
RS
9631 *no_add_attrs = true;
9632 }
9633
9634 return NULL_TREE;
9635}
0bfa5f65
RH
9636
9637/* Handle a "cleanup" attribute; arguments as in
9638 struct attribute_spec.handler. */
9639
9640static tree
35b1a6fa 9641handle_cleanup_attribute (tree *node, tree name, tree args,
e18476eb 9642 int ARG_UNUSED (flags), bool *no_add_attrs)
0bfa5f65
RH
9643{
9644 tree decl = *node;
9645 tree cleanup_id, cleanup_decl;
9646
9647 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9648 for global destructors in C++. This requires infrastructure that
9649 we don't have generically at the moment. It's also not a feature
9650 we'd be missing too much, since we do have attribute constructor. */
0ae9bd27 9651 if (!VAR_P (decl) || TREE_STATIC (decl))
0bfa5f65 9652 {
5c498b10 9653 warning (OPT_Wattributes, "%qE attribute ignored", name);
0bfa5f65
RH
9654 *no_add_attrs = true;
9655 return NULL_TREE;
9656 }
9657
9658 /* Verify that the argument is a function in scope. */
9659 /* ??? We could support pointers to functions here as well, if
9660 that was considered desirable. */
9661 cleanup_id = TREE_VALUE (args);
9662 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9663 {
40b97a2e 9664 error ("cleanup argument not an identifier");
0bfa5f65
RH
9665 *no_add_attrs = true;
9666 return NULL_TREE;
9667 }
10e6657a 9668 cleanup_decl = lookup_name (cleanup_id);
0bfa5f65
RH
9669 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9670 {
40b97a2e 9671 error ("cleanup argument not a function");
0bfa5f65
RH
9672 *no_add_attrs = true;
9673 return NULL_TREE;
9674 }
9675
35b1a6fa 9676 /* That the function has proper type is checked with the
0bfa5f65
RH
9677 eventual call to build_function_call. */
9678
9679 return NULL_TREE;
9680}
72954a4f
JM
9681
9682/* Handle a "warn_unused_result" attribute. No special handling. */
9683
9684static tree
9685handle_warn_unused_result_attribute (tree *node, tree name,
e18476eb
BI
9686 tree ARG_UNUSED (args),
9687 int ARG_UNUSED (flags), bool *no_add_attrs)
72954a4f
JM
9688{
9689 /* Ignore the attribute for functions not returning any value. */
9690 if (VOID_TYPE_P (TREE_TYPE (*node)))
9691 {
5c498b10 9692 warning (OPT_Wattributes, "%qE attribute ignored", name);
72954a4f
JM
9693 *no_add_attrs = true;
9694 }
9695
9696 return NULL_TREE;
9697}
3d091dac
KG
9698
9699/* Handle a "sentinel" attribute. */
9700
9701static tree
254986c7 9702handle_sentinel_attribute (tree *node, tree name, tree args,
3d091dac
KG
9703 int ARG_UNUSED (flags), bool *no_add_attrs)
9704{
f4da8dce 9705 if (!prototype_p (*node))
3d091dac 9706 {
5c498b10
DD
9707 warning (OPT_Wattributes,
9708 "%qE attribute requires prototypes with named arguments", name);
3d091dac 9709 *no_add_attrs = true;
3d091dac 9710 }
254986c7
KG
9711 else
9712 {
dcf0c47e 9713 if (!stdarg_p (*node))
c22cacf3 9714 {
5c498b10
DD
9715 warning (OPT_Wattributes,
9716 "%qE attribute only applies to variadic functions", name);
254986c7
KG
9717 *no_add_attrs = true;
9718 }
9719 }
c22cacf3 9720
254986c7 9721 if (args)
3d091dac 9722 {
254986c7 9723 tree position = TREE_VALUE (args);
661a0813
MP
9724 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9725 && TREE_CODE (position) != FUNCTION_DECL)
9726 position = default_conversion (position);
254986c7 9727
661a0813
MP
9728 if (TREE_CODE (position) != INTEGER_CST
9729 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
c22cacf3 9730 {
b8698a0f 9731 warning (OPT_Wattributes,
aa86a51b 9732 "requested position is not an integer constant");
254986c7
KG
9733 *no_add_attrs = true;
9734 }
9735 else
c22cacf3 9736 {
254986c7
KG
9737 if (tree_int_cst_lt (position, integer_zero_node))
9738 {
aa86a51b
DM
9739 warning (OPT_Wattributes,
9740 "requested position is less than zero");
254986c7
KG
9741 *no_add_attrs = true;
9742 }
9743 }
3d091dac 9744 }
c22cacf3 9745
3d091dac
KG
9746 return NULL_TREE;
9747}
b5d32c25
KG
9748
9749/* Handle a "type_generic" attribute. */
9750
9751static tree
9752handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9753 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9754 bool * ARG_UNUSED (no_add_attrs))
9755{
3bf5906b
KG
9756 /* Ensure we have a function type. */
9757 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
b8698a0f 9758
3bf5906b 9759 /* Ensure we have a variadic function. */
dcf0c47e 9760 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5d32c25
KG
9761
9762 return NULL_TREE;
9763}
ab442df7 9764
5779e713 9765/* Handle a "target" attribute. */
ab442df7
MM
9766
9767static tree
5779e713 9768handle_target_attribute (tree *node, tree name, tree args, int flags,
ab442df7
MM
9769 bool *no_add_attrs)
9770{
9771 /* Ensure we have a function type. */
9772 if (TREE_CODE (*node) != FUNCTION_DECL)
9773 {
9774 warning (OPT_Wattributes, "%qE attribute ignored", name);
9775 *no_add_attrs = true;
9776 }
ab442df7 9777 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
32887460 9778 flags))
ab442df7
MM
9779 *no_add_attrs = true;
9780
9781 return NULL_TREE;
9782}
9783
9784/* Arguments being collected for optimization. */
9785typedef const char *const_char_p; /* For DEF_VEC_P. */
9771b263 9786static GTY(()) vec<const_char_p, va_gc> *optimize_args;
ab442df7
MM
9787
9788
9789/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9790 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9791 false for #pragma GCC optimize. */
9792
9793bool
9794parse_optimize_options (tree args, bool attr_p)
9795{
9796 bool ret = true;
9797 unsigned opt_argc;
9798 unsigned i;
cbc19f39 9799 int saved_flag_strict_aliasing;
ab442df7 9800 const char **opt_argv;
6e2f1956
JM
9801 struct cl_decoded_option *decoded_options;
9802 unsigned int decoded_options_count;
ab442df7
MM
9803 tree ap;
9804
9805 /* Build up argv vector. Just in case the string is stored away, use garbage
9806 collected strings. */
9771b263
DN
9807 vec_safe_truncate (optimize_args, 0);
9808 vec_safe_push (optimize_args, (const char *) NULL);
ab442df7
MM
9809
9810 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9811 {
9812 tree value = TREE_VALUE (ap);
9813
9814 if (TREE_CODE (value) == INTEGER_CST)
9815 {
9816 char buffer[20];
9817 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9771b263 9818 vec_safe_push (optimize_args, ggc_strdup (buffer));
ab442df7
MM
9819 }
9820
9821 else if (TREE_CODE (value) == STRING_CST)
9822 {
9823 /* Split string into multiple substrings. */
9824 size_t len = TREE_STRING_LENGTH (value);
9825 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9826 char *end = p + len;
9827 char *comma;
9828 char *next_p = p;
9829
9830 while (next_p != NULL)
9831 {
9832 size_t len2;
9833 char *q, *r;
9834
9835 p = next_p;
9836 comma = strchr (p, ',');
9837 if (comma)
9838 {
9839 len2 = comma - p;
9840 *comma = '\0';
9841 next_p = comma+1;
9842 }
9843 else
9844 {
9845 len2 = end - p;
9846 next_p = NULL;
9847 }
9848
a9429e29 9849 r = q = (char *) ggc_alloc_atomic (len2 + 3);
ab442df7
MM
9850
9851 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9852 options. */
9853 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9854 {
9855 ret = false;
9856 if (attr_p)
9857 warning (OPT_Wattributes,
06730c5d 9858 "bad option %s to optimize attribute", p);
ab442df7
MM
9859 else
9860 warning (OPT_Wpragmas,
de621752 9861 "bad option %s to pragma attribute", p);
ab442df7
MM
9862 continue;
9863 }
9864
9865 if (*p != '-')
9866 {
9867 *r++ = '-';
9868
9869 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9870 itself is -Os, and any other switch begins with a -f. */
9871 if ((*p >= '0' && *p <= '9')
9872 || (p[0] == 's' && p[1] == '\0'))
9873 *r++ = 'O';
9874 else if (*p != 'O')
9875 *r++ = 'f';
9876 }
9877
9878 memcpy (r, p, len2);
9879 r[len2] = '\0';
9771b263 9880 vec_safe_push (optimize_args, (const char *) q);
ab442df7
MM
9881 }
9882
9883 }
9884 }
9885
9771b263 9886 opt_argc = optimize_args->length ();
ab442df7
MM
9887 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9888
9889 for (i = 1; i < opt_argc; i++)
9771b263 9890 opt_argv[i] = (*optimize_args)[i];
ab442df7 9891
cbc19f39
JJ
9892 saved_flag_strict_aliasing = flag_strict_aliasing;
9893
ab442df7 9894 /* Now parse the options. */
a75bfaa6
JM
9895 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9896 &decoded_options,
9897 &decoded_options_count);
9898 decode_options (&global_options, &global_options_set,
a4d8c676
JM
9899 decoded_options, decoded_options_count,
9900 input_location, global_dc);
ab442df7 9901
2b7e2984
SE
9902 targetm.override_options_after_change();
9903
cbc19f39
JJ
9904 /* Don't allow changing -fstrict-aliasing. */
9905 flag_strict_aliasing = saved_flag_strict_aliasing;
9906
9771b263 9907 optimize_args->truncate (0);
ab442df7
MM
9908 return ret;
9909}
9910
9911/* For handling "optimize" attribute. arguments as in
9912 struct attribute_spec.handler. */
9913
9914static tree
9915handle_optimize_attribute (tree *node, tree name, tree args,
9916 int ARG_UNUSED (flags), bool *no_add_attrs)
9917{
9918 /* Ensure we have a function type. */
9919 if (TREE_CODE (*node) != FUNCTION_DECL)
9920 {
9921 warning (OPT_Wattributes, "%qE attribute ignored", name);
9922 *no_add_attrs = true;
9923 }
9924 else
9925 {
9926 struct cl_optimization cur_opts;
9927 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9928
9929 /* Save current options. */
46625112 9930 cl_optimization_save (&cur_opts, &global_options);
ab442df7
MM
9931
9932 /* If we previously had some optimization options, use them as the
9933 default. */
9934 if (old_opts)
46625112
JM
9935 cl_optimization_restore (&global_options,
9936 TREE_OPTIMIZATION (old_opts));
ab442df7
MM
9937
9938 /* Parse options, and update the vector. */
9939 parse_optimize_options (args, true);
9940 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
bf7b5747 9941 = build_optimization_node (&global_options);
ab442df7
MM
9942
9943 /* Restore current options. */
46625112 9944 cl_optimization_restore (&global_options, &cur_opts);
ab442df7
MM
9945 }
9946
9947 return NULL_TREE;
9948}
7458026b
ILT
9949
9950/* Handle a "no_split_stack" attribute. */
9951
9952static tree
9953handle_no_split_stack_attribute (tree *node, tree name,
9954 tree ARG_UNUSED (args),
9955 int ARG_UNUSED (flags),
9956 bool *no_add_attrs)
9957{
9958 tree decl = *node;
9959
9960 if (TREE_CODE (decl) != FUNCTION_DECL)
9961 {
9962 error_at (DECL_SOURCE_LOCATION (decl),
9963 "%qE attribute applies only to functions", name);
9964 *no_add_attrs = true;
9965 }
9966 else if (DECL_INITIAL (decl))
9967 {
9968 error_at (DECL_SOURCE_LOCATION (decl),
9969 "can%'t set %qE attribute after definition", name);
9970 *no_add_attrs = true;
9971 }
9972
9973 return NULL_TREE;
9974}
826cacfe
MG
9975
9976/* Handle a "returns_nonnull" attribute; arguments as in
9977 struct attribute_spec.handler. */
9978
9979static tree
9980handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9981 bool *no_add_attrs)
9982{
9983 // Even without a prototype we still have a return type we can check.
9984 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9985 {
9986 error ("returns_nonnull attribute on a function not returning a pointer");
9987 *no_add_attrs = true;
9988 }
9989 return NULL_TREE;
9990}
9991
976d5a22
TT
9992/* Handle a "designated_init" attribute; arguments as in
9993 struct attribute_spec.handler. */
9994
9995static tree
9996handle_designated_init_attribute (tree *node, tree name, tree, int,
9997 bool *no_add_attrs)
9998{
9999 if (TREE_CODE (*node) != RECORD_TYPE)
10000 {
10001 error ("%qE attribute is only valid on %<struct%> type", name);
10002 *no_add_attrs = true;
10003 }
10004 return NULL_TREE;
10005}
10006
b34c7881 10007\f
dde05067
NF
10008/* Check for valid arguments being passed to a function with FNTYPE.
10009 There are NARGS arguments in the array ARGARRAY. */
b34c7881 10010void
dde05067 10011check_function_arguments (const_tree fntype, int nargs, tree *argarray)
b34c7881
JT
10012{
10013 /* Check for null being passed in a pointer argument that must be
10014 non-null. We also need to do this if format checking is enabled. */
10015
10016 if (warn_nonnull)
dde05067 10017 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
b34c7881
JT
10018
10019 /* Check for errors in format strings. */
10020
e6c69da0 10021 if (warn_format || warn_suggest_attribute_format)
dde05067 10022 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
7876a414
KG
10023
10024 if (warn_format)
dde05067 10025 check_function_sentinel (fntype, nargs, argarray);
b34c7881
JT
10026}
10027
10028/* Generic argument checking recursion routine. PARAM is the argument to
10029 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
10030 once the argument is resolved. CTX is context for the callback. */
10031void
35b1a6fa
AJ
10032check_function_arguments_recurse (void (*callback)
10033 (void *, tree, unsigned HOST_WIDE_INT),
10034 void *ctx, tree param,
10035 unsigned HOST_WIDE_INT param_num)
b34c7881 10036{
1043771b 10037 if (CONVERT_EXPR_P (param)
1344f9a3
JM
10038 && (TYPE_PRECISION (TREE_TYPE (param))
10039 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
b34c7881
JT
10040 {
10041 /* Strip coercion. */
10042 check_function_arguments_recurse (callback, ctx,
6de9cd9a 10043 TREE_OPERAND (param, 0), param_num);
b34c7881
JT
10044 return;
10045 }
10046
10047 if (TREE_CODE (param) == CALL_EXPR)
10048 {
5039610b 10049 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
b34c7881
JT
10050 tree attrs;
10051 bool found_format_arg = false;
10052
10053 /* See if this is a call to a known internationalization function
10054 that modifies a format arg. Such a function may have multiple
10055 format_arg attributes (for example, ngettext). */
10056
10057 for (attrs = TYPE_ATTRIBUTES (type);
10058 attrs;
10059 attrs = TREE_CHAIN (attrs))
10060 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
10061 {
5039610b 10062 tree inner_arg;
b34c7881
JT
10063 tree format_num_expr;
10064 int format_num;
10065 int i;
5039610b 10066 call_expr_arg_iterator iter;
b34c7881
JT
10067
10068 /* Extract the argument number, which was previously checked
10069 to be valid. */
10070 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
b34c7881 10071
6b3b8c27 10072 format_num = tree_to_uhwi (format_num_expr);
b34c7881 10073
5039610b
SL
10074 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
10075 inner_arg != 0;
10076 inner_arg = next_call_expr_arg (&iter), i++)
b34c7881
JT
10077 if (i == format_num)
10078 {
10079 check_function_arguments_recurse (callback, ctx,
5039610b 10080 inner_arg, param_num);
b34c7881
JT
10081 found_format_arg = true;
10082 break;
10083 }
10084 }
10085
10086 /* If we found a format_arg attribute and did a recursive check,
10087 we are done with checking this argument. Otherwise, we continue
10088 and this will be considered a non-literal. */
10089 if (found_format_arg)
10090 return;
10091 }
10092
10093 if (TREE_CODE (param) == COND_EXPR)
10094 {
10095 /* Check both halves of the conditional expression. */
10096 check_function_arguments_recurse (callback, ctx,
6de9cd9a 10097 TREE_OPERAND (param, 1), param_num);
b34c7881 10098 check_function_arguments_recurse (callback, ctx,
6de9cd9a 10099 TREE_OPERAND (param, 2), param_num);
b34c7881
JT
10100 return;
10101 }
10102
10103 (*callback) (ctx, param, param_num);
10104}
e2500fed 10105
a98c2819
MLI
10106/* Checks for a builtin function FNDECL that the number of arguments
10107 NARGS against the required number REQUIRED and issues an error if
10108 there is a mismatch. Returns true if the number of arguments is
10109 correct, otherwise false. */
83322951
RG
10110
10111static bool
a98c2819 10112builtin_function_validate_nargs (tree fndecl, int nargs, int required)
83322951
RG
10113{
10114 if (nargs < required)
10115 {
a98c2819
MLI
10116 error_at (input_location,
10117 "not enough arguments to function %qE", fndecl);
83322951
RG
10118 return false;
10119 }
10120 else if (nargs > required)
10121 {
a98c2819
MLI
10122 error_at (input_location,
10123 "too many arguments to function %qE", fndecl);
83322951
RG
10124 return false;
10125 }
10126 return true;
10127}
10128
10129/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
10130 Returns false if there was an error, otherwise true. */
10131
10132bool
10133check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
10134{
10135 if (!DECL_BUILT_IN (fndecl)
10136 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
10137 return true;
10138
10139 switch (DECL_FUNCTION_CODE (fndecl))
10140 {
10141 case BUILT_IN_CONSTANT_P:
a98c2819 10142 return builtin_function_validate_nargs (fndecl, nargs, 1);
83322951
RG
10143
10144 case BUILT_IN_ISFINITE:
10145 case BUILT_IN_ISINF:
05f41289 10146 case BUILT_IN_ISINF_SIGN:
83322951
RG
10147 case BUILT_IN_ISNAN:
10148 case BUILT_IN_ISNORMAL:
61717a45 10149 case BUILT_IN_SIGNBIT:
a98c2819 10150 if (builtin_function_validate_nargs (fndecl, nargs, 1))
83322951
RG
10151 {
10152 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10153 {
10154 error ("non-floating-point argument in call to "
10155 "function %qE", fndecl);
10156 return false;
10157 }
10158 return true;
10159 }
10160 return false;
10161
10162 case BUILT_IN_ISGREATER:
10163 case BUILT_IN_ISGREATEREQUAL:
10164 case BUILT_IN_ISLESS:
10165 case BUILT_IN_ISLESSEQUAL:
10166 case BUILT_IN_ISLESSGREATER:
10167 case BUILT_IN_ISUNORDERED:
a98c2819 10168 if (builtin_function_validate_nargs (fndecl, nargs, 2))
83322951
RG
10169 {
10170 enum tree_code code0, code1;
10171 code0 = TREE_CODE (TREE_TYPE (args[0]));
10172 code1 = TREE_CODE (TREE_TYPE (args[1]));
10173 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10174 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10175 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10176 {
10177 error ("non-floating-point arguments in call to "
10178 "function %qE", fndecl);
10179 return false;
10180 }
10181 return true;
10182 }
10183 return false;
10184
3bf5906b 10185 case BUILT_IN_FPCLASSIFY:
a98c2819 10186 if (builtin_function_validate_nargs (fndecl, nargs, 6))
3bf5906b
KG
10187 {
10188 unsigned i;
b8698a0f 10189
3bf5906b
KG
10190 for (i=0; i<5; i++)
10191 if (TREE_CODE (args[i]) != INTEGER_CST)
10192 {
10193 error ("non-const integer argument %u in call to function %qE",
10194 i+1, fndecl);
10195 return false;
10196 }
10197
10198 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10199 {
10200 error ("non-floating-point argument in call to function %qE",
10201 fndecl);
10202 return false;
10203 }
10204 return true;
10205 }
10206 return false;
10207
45d439ac
JJ
10208 case BUILT_IN_ASSUME_ALIGNED:
10209 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10210 {
10211 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10212 {
10213 error ("non-integer argument 3 in call to function %qE", fndecl);
10214 return false;
10215 }
10216 return true;
10217 }
10218 return false;
10219
1304953e
JJ
10220 case BUILT_IN_ADD_OVERFLOW:
10221 case BUILT_IN_SUB_OVERFLOW:
10222 case BUILT_IN_MUL_OVERFLOW:
10223 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10224 {
10225 unsigned i;
10226 for (i = 0; i < 2; i++)
10227 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10228 {
10229 error ("argument %u in call to function %qE does not have "
10230 "integral type", i + 1, fndecl);
10231 return false;
10232 }
10233 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10234 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10235 {
10236 error ("argument 3 in call to function %qE does not have "
10237 "pointer to integer type", fndecl);
10238 return false;
10239 }
10240 return true;
10241 }
10242 return false;
10243
83322951
RG
10244 default:
10245 return true;
10246 }
10247}
10248
d07605f5
AP
10249/* Function to help qsort sort FIELD_DECLs by name order. */
10250
10251int
10252field_decl_cmp (const void *x_p, const void *y_p)
10253{
28dab132
BI
10254 const tree *const x = (const tree *const) x_p;
10255 const tree *const y = (const tree *const) y_p;
10256
d07605f5
AP
10257 if (DECL_NAME (*x) == DECL_NAME (*y))
10258 /* A nontype is "greater" than a type. */
10259 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10260 if (DECL_NAME (*x) == NULL_TREE)
10261 return -1;
10262 if (DECL_NAME (*y) == NULL_TREE)
10263 return 1;
10264 if (DECL_NAME (*x) < DECL_NAME (*y))
10265 return -1;
10266 return 1;
10267}
10268
10269static struct {
10270 gt_pointer_operator new_value;
10271 void *cookie;
10272} resort_data;
10273
10274/* This routine compares two fields like field_decl_cmp but using the
10275pointer operator in resort_data. */
10276
10277static int
10278resort_field_decl_cmp (const void *x_p, const void *y_p)
10279{
28dab132
BI
10280 const tree *const x = (const tree *const) x_p;
10281 const tree *const y = (const tree *const) y_p;
d07605f5
AP
10282
10283 if (DECL_NAME (*x) == DECL_NAME (*y))
10284 /* A nontype is "greater" than a type. */
10285 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10286 if (DECL_NAME (*x) == NULL_TREE)
10287 return -1;
10288 if (DECL_NAME (*y) == NULL_TREE)
10289 return 1;
10290 {
10291 tree d1 = DECL_NAME (*x);
10292 tree d2 = DECL_NAME (*y);
10293 resort_data.new_value (&d1, resort_data.cookie);
10294 resort_data.new_value (&d2, resort_data.cookie);
10295 if (d1 < d2)
10296 return -1;
10297 }
10298 return 1;
10299}
10300
10301/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10302
10303void
10304resort_sorted_fields (void *obj,
e18476eb 10305 void * ARG_UNUSED (orig_obj),
6de9cd9a
DN
10306 gt_pointer_operator new_value,
10307 void *cookie)
d07605f5 10308{
e18476eb 10309 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
d07605f5
AP
10310 resort_data.new_value = new_value;
10311 resort_data.cookie = cookie;
10312 qsort (&sf->elts[0], sf->len, sizeof (tree),
6de9cd9a 10313 resort_field_decl_cmp);
d07605f5
AP
10314}
10315
0a3ee0fd
GDR
10316/* Subroutine of c_parse_error.
10317 Return the result of concatenating LHS and RHS. RHS is really
10318 a string literal, its first character is indicated by RHS_START and
3292fb42 10319 RHS_SIZE is its length (including the terminating NUL character).
0a3ee0fd
GDR
10320
10321 The caller is responsible for deleting the returned pointer. */
10322
10323static char *
10324catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10325{
10326 const int lhs_size = strlen (lhs);
10327 char *result = XNEWVEC (char, lhs_size + rhs_size);
10328 strncpy (result, lhs, lhs_size);
10329 strncpy (result + lhs_size, rhs_start, rhs_size);
10330 return result;
10331}
10332
4b794eaf 10333/* Issue the error given by GMSGID, indicating that it occurred before
4bb8ca28
MM
10334 TOKEN, which had the associated VALUE. */
10335
10336void
b8698a0f 10337c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
cfc93532 10338 tree value, unsigned char token_flags)
4bb8ca28 10339{
0a3ee0fd
GDR
10340#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10341
10342 char *message = NULL;
4bb8ca28 10343
cfc93532 10344 if (token_type == CPP_EOF)
4b794eaf 10345 message = catenate_messages (gmsgid, " at end of input");
b8698a0f
L
10346 else if (token_type == CPP_CHAR
10347 || token_type == CPP_WCHAR
cfc93532 10348 || token_type == CPP_CHAR16
fe95b036
ESR
10349 || token_type == CPP_CHAR32
10350 || token_type == CPP_UTF8CHAR)
4bb8ca28
MM
10351 {
10352 unsigned int val = TREE_INT_CST_LOW (value);
b6baa67d
KVH
10353 const char *prefix;
10354
cfc93532 10355 switch (token_type)
b6baa67d
KVH
10356 {
10357 default:
10358 prefix = "";
10359 break;
10360 case CPP_WCHAR:
10361 prefix = "L";
10362 break;
10363 case CPP_CHAR16:
10364 prefix = "u";
10365 break;
10366 case CPP_CHAR32:
10367 prefix = "U";
10368 break;
fe95b036
ESR
10369 case CPP_UTF8CHAR:
10370 prefix = "u8";
10371 break;
b6baa67d
KVH
10372 }
10373
4bb8ca28 10374 if (val <= UCHAR_MAX && ISGRAPH (val))
c22cacf3 10375 message = catenate_messages (gmsgid, " before %s'%c'");
4bb8ca28 10376 else
c22cacf3 10377 message = catenate_messages (gmsgid, " before %s'\\x%x'");
0a3ee0fd 10378
b6baa67d 10379 error (message, prefix, val);
0a3ee0fd
GDR
10380 free (message);
10381 message = NULL;
4bb8ca28 10382 }
65e5a578
ESR
10383 else if (token_type == CPP_CHAR_USERDEF
10384 || token_type == CPP_WCHAR_USERDEF
10385 || token_type == CPP_CHAR16_USERDEF
fe95b036
ESR
10386 || token_type == CPP_CHAR32_USERDEF
10387 || token_type == CPP_UTF8CHAR_USERDEF)
65e5a578
ESR
10388 message = catenate_messages (gmsgid,
10389 " before user-defined character literal");
10390 else if (token_type == CPP_STRING_USERDEF
10391 || token_type == CPP_WSTRING_USERDEF
10392 || token_type == CPP_STRING16_USERDEF
10393 || token_type == CPP_STRING32_USERDEF
10394 || token_type == CPP_UTF8STRING_USERDEF)
10395 message = catenate_messages (gmsgid, " before user-defined string literal");
b8698a0f
L
10396 else if (token_type == CPP_STRING
10397 || token_type == CPP_WSTRING
cfc93532 10398 || token_type == CPP_STRING16
2c6e3f55
JJ
10399 || token_type == CPP_STRING32
10400 || token_type == CPP_UTF8STRING)
4b794eaf 10401 message = catenate_messages (gmsgid, " before string constant");
cfc93532 10402 else if (token_type == CPP_NUMBER)
4b794eaf 10403 message = catenate_messages (gmsgid, " before numeric constant");
cfc93532 10404 else if (token_type == CPP_NAME)
0a3ee0fd 10405 {
4b794eaf 10406 message = catenate_messages (gmsgid, " before %qE");
c51a1ba9 10407 error (message, value);
0a3ee0fd
GDR
10408 free (message);
10409 message = NULL;
10410 }
cfc93532 10411 else if (token_type == CPP_PRAGMA)
bc4071dd 10412 message = catenate_messages (gmsgid, " before %<#pragma%>");
cfc93532 10413 else if (token_type == CPP_PRAGMA_EOL)
bc4071dd 10414 message = catenate_messages (gmsgid, " before end of line");
34429675
JM
10415 else if (token_type == CPP_DECLTYPE)
10416 message = catenate_messages (gmsgid, " before %<decltype%>");
cfc93532 10417 else if (token_type < N_TTYPES)
0a3ee0fd 10418 {
4b794eaf 10419 message = catenate_messages (gmsgid, " before %qs token");
cfc93532 10420 error (message, cpp_type2name (token_type, token_flags));
0a3ee0fd
GDR
10421 free (message);
10422 message = NULL;
10423 }
4bb8ca28 10424 else
4b794eaf 10425 error (gmsgid);
0a3ee0fd
GDR
10426
10427 if (message)
10428 {
10429 error (message);
10430 free (message);
10431 }
c22cacf3 10432#undef catenate_messages
4bb8ca28
MM
10433}
10434
87cf0651
SB
10435/* Return the gcc option code associated with the reason for a cpp
10436 message, or 0 if none. */
10437
10438static int
10439c_option_controlling_cpp_error (int reason)
10440{
b559c810 10441 const struct cpp_reason_option_codes_t *entry;
87cf0651 10442
b559c810 10443 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
87cf0651
SB
10444 {
10445 if (entry->reason == reason)
10446 return entry->option_code;
10447 }
10448 return 0;
10449}
10450
148e4216 10451/* Callback from cpp_error for PFILE to print diagnostics from the
87cf0651
SB
10452 preprocessor. The diagnostic is of type LEVEL, with REASON set
10453 to the reason code if LEVEL is represents a warning, at location
148e4216
JM
10454 LOCATION unless this is after lexing and the compiler's location
10455 should be used instead, with column number possibly overridden by
10456 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10457 the arguments. Returns true if a diagnostic was emitted, false
10458 otherwise. */
10459
10460bool
87cf0651 10461c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
148e4216
JM
10462 location_t location, unsigned int column_override,
10463 const char *msg, va_list *ap)
10464{
10465 diagnostic_info diagnostic;
10466 diagnostic_t dlevel;
e3339d0f 10467 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
148e4216
JM
10468 bool ret;
10469
10470 switch (level)
10471 {
10472 case CPP_DL_WARNING_SYSHDR:
10473 if (flag_no_output)
10474 return false;
e3339d0f 10475 global_dc->dc_warn_system_headers = 1;
148e4216
JM
10476 /* Fall through. */
10477 case CPP_DL_WARNING:
10478 if (flag_no_output)
10479 return false;
10480 dlevel = DK_WARNING;
10481 break;
10482 case CPP_DL_PEDWARN:
10483 if (flag_no_output && !flag_pedantic_errors)
10484 return false;
10485 dlevel = DK_PEDWARN;
10486 break;
10487 case CPP_DL_ERROR:
10488 dlevel = DK_ERROR;
10489 break;
10490 case CPP_DL_ICE:
10491 dlevel = DK_ICE;
10492 break;
10493 case CPP_DL_NOTE:
10494 dlevel = DK_NOTE;
10495 break;
47580d22
JM
10496 case CPP_DL_FATAL:
10497 dlevel = DK_FATAL;
10498 break;
148e4216
JM
10499 default:
10500 gcc_unreachable ();
10501 }
10502 if (done_lexing)
10503 location = input_location;
10504 diagnostic_set_info_translated (&diagnostic, msg, ap,
10505 location, dlevel);
10506 if (column_override)
10507 diagnostic_override_column (&diagnostic, column_override);
87cf0651
SB
10508 diagnostic_override_option_index (&diagnostic,
10509 c_option_controlling_cpp_error (reason));
148e4216
JM
10510 ret = report_diagnostic (&diagnostic);
10511 if (level == CPP_DL_WARNING_SYSHDR)
e3339d0f 10512 global_dc->dc_warn_system_headers = save_warn_system_headers;
148e4216
JM
10513 return ret;
10514}
10515
c5ff069d
ZW
10516/* Convert a character from the host to the target execution character
10517 set. cpplib handles this, mostly. */
10518
10519HOST_WIDE_INT
10520c_common_to_target_charset (HOST_WIDE_INT c)
10521{
10522 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10523 zero-extended under -fno-signed-char. cpplib insists that characters
10524 and character constants are always unsigned. Hence we must convert
10525 back and forth. */
10526 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10527
10528 uc = cpp_host_to_exec_charset (parse_in, uc);
10529
10530 if (flag_signed_char)
10531 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10532 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10533 else
10534 return uc;
10535}
10536
cf9e9959
EB
10537/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10538 references with an INDIRECT_REF of a constant at the bottom; much like the
10539 traditional rendering of offsetof as a macro. Return the folded result. */
ee8a6a3e 10540
cf9e9959
EB
10541tree
10542fold_offsetof_1 (tree expr)
ee8a6a3e 10543{
ee8a6a3e
RH
10544 tree base, off, t;
10545
10546 switch (TREE_CODE (expr))
10547 {
10548 case ERROR_MARK:
10549 return expr;
10550
545b7d8c
VR
10551 case VAR_DECL:
10552 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10553 return error_mark_node;
10554
6d4d7b0e 10555 case CALL_EXPR:
8d5f60ac 10556 case TARGET_EXPR:
6d4d7b0e
PB
10557 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10558 return error_mark_node;
10559
6d4d7b0e
PB
10560 case NOP_EXPR:
10561 case INDIRECT_REF:
cf9e9959 10562 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
61c3c490
DS
10563 {
10564 error ("cannot apply %<offsetof%> to a non constant address");
10565 return error_mark_node;
10566 }
cf9e9959 10567 return TREE_OPERAND (expr, 0);
6d4d7b0e 10568
ee8a6a3e 10569 case COMPONENT_REF:
cf9e9959 10570 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
ee8a6a3e
RH
10571 if (base == error_mark_node)
10572 return base;
10573
10574 t = TREE_OPERAND (expr, 1);
10575 if (DECL_C_BIT_FIELD (t))
10576 {
10577 error ("attempt to take address of bit-field structure "
c51a1ba9 10578 "member %qD", t);
ee8a6a3e
RH
10579 return error_mark_node;
10580 }
db3927fb 10581 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
386b1f1f 10582 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
db3927fb 10583 / BITS_PER_UNIT));
ee8a6a3e
RH
10584 break;
10585
10586 case ARRAY_REF:
cf9e9959 10587 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
ee8a6a3e
RH
10588 if (base == error_mark_node)
10589 return base;
10590
10591 t = TREE_OPERAND (expr, 1);
61c3c490
DS
10592
10593 /* Check if the offset goes beyond the upper bound of the array. */
cf9e9959 10594 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
d32599a6
JJ
10595 {
10596 tree upbound = array_ref_up_bound (expr);
10597 if (upbound != NULL_TREE
10598 && TREE_CODE (upbound) == INTEGER_CST
10599 && !tree_int_cst_equal (upbound,
10600 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10601 {
10602 upbound = size_binop (PLUS_EXPR, upbound,
10603 build_int_cst (TREE_TYPE (upbound), 1));
10604 if (tree_int_cst_lt (upbound, t))
10605 {
10606 tree v;
10607
10608 for (v = TREE_OPERAND (expr, 0);
10609 TREE_CODE (v) == COMPONENT_REF;
10610 v = TREE_OPERAND (v, 0))
10611 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10612 == RECORD_TYPE)
10613 {
910ad8de
NF
10614 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10615 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
d32599a6
JJ
10616 if (TREE_CODE (fld_chain) == FIELD_DECL)
10617 break;
10618
10619 if (fld_chain)
10620 break;
10621 }
10622 /* Don't warn if the array might be considered a poor
10623 man's flexible array member with a very permissive
10624 definition thereof. */
10625 if (TREE_CODE (v) == ARRAY_REF
10626 || TREE_CODE (v) == COMPONENT_REF)
10627 warning (OPT_Warray_bounds,
10628 "index %E denotes an offset "
10629 "greater than size of %qT",
10630 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10631 }
10632 }
10633 }
cf9e9959
EB
10634
10635 t = convert (sizetype, t);
10636 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
ee8a6a3e
RH
10637 break;
10638
1916c916
VR
10639 case COMPOUND_EXPR:
10640 /* Handle static members of volatile structs. */
10641 t = TREE_OPERAND (expr, 1);
0ae9bd27 10642 gcc_assert (VAR_P (t));
cf9e9959 10643 return fold_offsetof_1 (t);
1916c916 10644
ee8a6a3e 10645 default:
366de0ce 10646 gcc_unreachable ();
ee8a6a3e
RH
10647 }
10648
cf9e9959 10649 return fold_build_pointer_plus (base, off);
ee8a6a3e
RH
10650}
10651
cf9e9959
EB
10652/* Likewise, but convert it to the return type of offsetof. */
10653
ee8a6a3e 10654tree
cf9e9959 10655fold_offsetof (tree expr)
ee8a6a3e 10656{
cf9e9959 10657 return convert (size_type_node, fold_offsetof_1 (expr));
ee8a6a3e
RH
10658}
10659
d166d4c3
AK
10660/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10661 expression, because B will always be true. */
10662
10663void
10664warn_for_omitted_condop (location_t location, tree cond)
10665{
10666 if (truth_value_p (TREE_CODE (cond)))
10667 warning_at (location, OPT_Wparentheses,
10668 "the omitted middle operand in ?: will always be %<true%>, "
10669 "suggest explicit middle operand");
10670}
10671
4816c593
NF
10672/* Give an error for storing into ARG, which is 'const'. USE indicates
10673 how ARG was being used. */
10674
10675void
c02065fc 10676readonly_error (location_t loc, tree arg, enum lvalue_use use)
4816c593
NF
10677{
10678 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10679 || use == lv_asm);
10680 /* Using this macro rather than (for example) arrays of messages
10681 ensures that all the format strings are checked at compile
10682 time. */
10683#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10684 : (use == lv_increment ? (I) \
10685 : (use == lv_decrement ? (D) : (AS))))
10686 if (TREE_CODE (arg) == COMPONENT_REF)
10687 {
10688 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
c02065fc
AH
10689 error_at (loc, READONLY_MSG (G_("assignment of member "
10690 "%qD in read-only object"),
10691 G_("increment of member "
10692 "%qD in read-only object"),
10693 G_("decrement of member "
10694 "%qD in read-only object"),
10695 G_("member %qD in read-only object "
10696 "used as %<asm%> output")),
10697 TREE_OPERAND (arg, 1));
4816c593 10698 else
c02065fc
AH
10699 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10700 G_("increment of read-only member %qD"),
10701 G_("decrement of read-only member %qD"),
10702 G_("read-only member %qD used as %<asm%> output")),
10703 TREE_OPERAND (arg, 1));
4816c593 10704 }
0ae9bd27 10705 else if (VAR_P (arg))
c02065fc
AH
10706 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10707 G_("increment of read-only variable %qD"),
10708 G_("decrement of read-only variable %qD"),
10709 G_("read-only variable %qD used as %<asm%> output")),
10710 arg);
4816c593 10711 else if (TREE_CODE (arg) == PARM_DECL)
c02065fc
AH
10712 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10713 G_("increment of read-only parameter %qD"),
10714 G_("decrement of read-only parameter %qD"),
10715 G_("read-only parameter %qD use as %<asm%> output")),
10716 arg);
4816c593
NF
10717 else if (TREE_CODE (arg) == RESULT_DECL)
10718 {
10719 gcc_assert (c_dialect_cxx ());
c02065fc
AH
10720 error_at (loc, READONLY_MSG (G_("assignment of "
10721 "read-only named return value %qD"),
10722 G_("increment of "
10723 "read-only named return value %qD"),
10724 G_("decrement of "
10725 "read-only named return value %qD"),
10726 G_("read-only named return value %qD "
10727 "used as %<asm%>output")),
10728 arg);
4816c593
NF
10729 }
10730 else if (TREE_CODE (arg) == FUNCTION_DECL)
c02065fc
AH
10731 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10732 G_("increment of function %qD"),
10733 G_("decrement of function %qD"),
10734 G_("function %qD used as %<asm%> output")),
10735 arg);
4816c593 10736 else
c02065fc
AH
10737 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10738 G_("increment of read-only location %qE"),
10739 G_("decrement of read-only location %qE"),
10740 G_("read-only location %qE used as %<asm%> output")),
10741 arg);
4816c593
NF
10742}
10743
37dc0d8d 10744/* Print an error message for an invalid lvalue. USE says
7bd11157
TT
10745 how the lvalue is being used and so selects the error message. LOC
10746 is the location for the error. */
5ae9ba3e 10747
37dc0d8d 10748void
7bd11157 10749lvalue_error (location_t loc, enum lvalue_use use)
5ae9ba3e 10750{
37dc0d8d 10751 switch (use)
5ae9ba3e 10752 {
37dc0d8d 10753 case lv_assign:
7bd11157 10754 error_at (loc, "lvalue required as left operand of assignment");
37dc0d8d
JM
10755 break;
10756 case lv_increment:
7bd11157 10757 error_at (loc, "lvalue required as increment operand");
37dc0d8d
JM
10758 break;
10759 case lv_decrement:
7bd11157 10760 error_at (loc, "lvalue required as decrement operand");
37dc0d8d
JM
10761 break;
10762 case lv_addressof:
7bd11157 10763 error_at (loc, "lvalue required as unary %<&%> operand");
37dc0d8d
JM
10764 break;
10765 case lv_asm:
7bd11157 10766 error_at (loc, "lvalue required in asm statement");
37dc0d8d
JM
10767 break;
10768 default:
10769 gcc_unreachable ();
5ae9ba3e 10770 }
5ae9ba3e 10771}
7a6daeb0
NF
10772
10773/* Print an error message for an invalid indirection of type TYPE.
10774 ERRSTRING is the name of the operator for the indirection. */
10775
10776void
10777invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10778{
10779 switch (errstring)
10780 {
10781 case RO_NULL:
10782 gcc_assert (c_dialect_cxx ());
10783 error_at (loc, "invalid type argument (have %qT)", type);
10784 break;
10785 case RO_ARRAY_INDEXING:
10786 error_at (loc,
10787 "invalid type argument of array indexing (have %qT)",
10788 type);
10789 break;
10790 case RO_UNARY_STAR:
10791 error_at (loc,
10792 "invalid type argument of unary %<*%> (have %qT)",
10793 type);
10794 break;
10795 case RO_ARROW:
10796 error_at (loc,
10797 "invalid type argument of %<->%> (have %qT)",
10798 type);
10799 break;
55a7f02f
MP
10800 case RO_ARROW_STAR:
10801 error_at (loc,
10802 "invalid type argument of %<->*%> (have %qT)",
10803 type);
10804 break;
7a6daeb0
NF
10805 case RO_IMPLICIT_CONVERSION:
10806 error_at (loc,
10807 "invalid type argument of implicit conversion (have %qT)",
10808 type);
10809 break;
10810 default:
10811 gcc_unreachable ();
10812 }
10813}
aab038d5
RH
10814\f
10815/* *PTYPE is an incomplete array. Complete it with a domain based on
10816 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10817 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10818 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10819
10820int
10821complete_array_type (tree *ptype, tree initial_value, bool do_default)
10822{
10823 tree maxindex, type, main_type, elt, unqual_elt;
10824 int failure = 0, quals;
06d40de8 10825 hashval_t hashcode = 0;
40d3d530 10826 bool overflow_p = false;
aab038d5
RH
10827
10828 maxindex = size_zero_node;
10829 if (initial_value)
10830 {
10831 if (TREE_CODE (initial_value) == STRING_CST)
10832 {
10833 int eltsize
10834 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10835 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10836 }
10837 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10838 {
9771b263 10839 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
aab038d5 10840
9771b263 10841 if (vec_safe_is_empty (v))
aab038d5
RH
10842 {
10843 if (pedantic)
10844 failure = 3;
830c740f 10845 maxindex = ssize_int (-1);
aab038d5
RH
10846 }
10847 else
10848 {
10849 tree curindex;
4038c495
GB
10850 unsigned HOST_WIDE_INT cnt;
10851 constructor_elt *ce;
cff7525f 10852 bool fold_p = false;
aab038d5 10853
9771b263 10854 if ((*v)[0].index)
40d3d530
JR
10855 maxindex = (*v)[0].index, fold_p = true;
10856
aab038d5
RH
10857 curindex = maxindex;
10858
9771b263 10859 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
aab038d5 10860 {
cff7525f 10861 bool curfold_p = false;
4038c495 10862 if (ce->index)
cff7525f 10863 curindex = ce->index, curfold_p = true;
aab038d5 10864 else
cff7525f
JH
10865 {
10866 if (fold_p)
40d3d530
JR
10867 {
10868 /* Since we treat size types now as ordinary
10869 unsigned types, we need an explicit overflow
10870 check. */
10871 tree orig = curindex;
10872 curindex = fold_convert (sizetype, curindex);
10873 overflow_p |= tree_int_cst_lt (curindex, orig);
10874 }
db3927fb
AH
10875 curindex = size_binop (PLUS_EXPR, curindex,
10876 size_one_node);
cff7525f 10877 }
aab038d5 10878 if (tree_int_cst_lt (maxindex, curindex))
cff7525f 10879 maxindex = curindex, fold_p = curfold_p;
aab038d5 10880 }
40d3d530
JR
10881 if (fold_p)
10882 {
10883 tree orig = maxindex;
10884 maxindex = fold_convert (sizetype, maxindex);
10885 overflow_p |= tree_int_cst_lt (maxindex, orig);
10886 }
aab038d5
RH
10887 }
10888 }
10889 else
10890 {
10891 /* Make an error message unless that happened already. */
10892 if (initial_value != error_mark_node)
10893 failure = 1;
10894 }
10895 }
10896 else
10897 {
10898 failure = 2;
10899 if (!do_default)
10900 return failure;
10901 }
10902
10903 type = *ptype;
10904 elt = TREE_TYPE (type);
10905 quals = TYPE_QUALS (strip_array_types (elt));
10906 if (quals == 0)
10907 unqual_elt = elt;
10908 else
36c5e70a 10909 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
aab038d5
RH
10910
10911 /* Using build_distinct_type_copy and modifying things afterward instead
10912 of using build_array_type to create a new type preserves all of the
10913 TYPE_LANG_FLAG_? bits that the front end may have set. */
10914 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10915 TREE_TYPE (main_type) = unqual_elt;
e6313a78
RG
10916 TYPE_DOMAIN (main_type)
10917 = build_range_type (TREE_TYPE (maxindex),
10918 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
aab038d5
RH
10919 layout_type (main_type);
10920
06d40de8
DG
10921 /* Make sure we have the canonical MAIN_TYPE. */
10922 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
b8698a0f 10923 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
06d40de8
DG
10924 hashcode);
10925 main_type = type_hash_canon (hashcode, main_type);
10926
9ae165a0
DG
10927 /* Fix the canonical type. */
10928 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10929 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10930 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10931 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10932 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10933 != TYPE_DOMAIN (main_type)))
b8698a0f 10934 TYPE_CANONICAL (main_type)
9ae165a0
DG
10935 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10936 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10937 else
10938 TYPE_CANONICAL (main_type) = main_type;
10939
aab038d5
RH
10940 if (quals == 0)
10941 type = main_type;
10942 else
10943 type = c_build_qualified_type (main_type, quals);
10944
7bfcb402
JM
10945 if (COMPLETE_TYPE_P (type)
10946 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
40d3d530 10947 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7bfcb402
JM
10948 {
10949 error ("size of array is too large");
10950 /* If we proceed with the array type as it is, we'll eventually
386b1f1f 10951 crash in tree_to_[su]hwi(). */
7bfcb402
JM
10952 type = error_mark_node;
10953 }
10954
aab038d5
RH
10955 *ptype = type;
10956 return failure;
10957}
5ae9ba3e 10958
30cd1c5d
AS
10959/* Like c_mark_addressable but don't check register qualifier. */
10960void
10961c_common_mark_addressable_vec (tree t)
10962{
10963 while (handled_component_p (t))
10964 t = TREE_OPERAND (t, 0);
0ae9bd27 10965 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
30cd1c5d
AS
10966 return;
10967 TREE_ADDRESSABLE (t) = 1;
10968}
10969
10970
48ae6c13
RH
10971\f
10972/* Used to help initialize the builtin-types.def table. When a type of
10973 the correct size doesn't exist, use error_mark_node instead of NULL.
10974 The later results in segfaults even when a decl using the type doesn't
10975 get invoked. */
10976
10977tree
10978builtin_type_for_size (int size, bool unsignedp)
10979{
21fa2faf 10980 tree type = c_common_type_for_size (size, unsignedp);
48ae6c13
RH
10981 return type ? type : error_mark_node;
10982}
10983
10984/* A helper function for resolve_overloaded_builtin in resolving the
10985 overloaded __sync_ builtins. Returns a positive power of 2 if the
10986 first operand of PARAMS is a pointer to a supported data type.
10987 Returns 0 if an error is encountered. */
10988
10989static int
9771b263 10990sync_resolve_size (tree function, vec<tree, va_gc> *params)
48ae6c13
RH
10991{
10992 tree type;
10993 int size;
10994
9771b263 10995 if (!params)
48ae6c13
RH
10996 {
10997 error ("too few arguments to function %qE", function);
10998 return 0;
10999 }
11000
9771b263 11001 type = TREE_TYPE ((*params)[0]);
6415bd5d
JM
11002 if (TREE_CODE (type) == ARRAY_TYPE)
11003 {
11004 /* Force array-to-pointer decay for C++. */
11005 gcc_assert (c_dialect_cxx());
11006 (*params)[0] = default_conversion ((*params)[0]);
11007 type = TREE_TYPE ((*params)[0]);
11008 }
48ae6c13
RH
11009 if (TREE_CODE (type) != POINTER_TYPE)
11010 goto incompatible;
11011
11012 type = TREE_TYPE (type);
11013 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
11014 goto incompatible;
11015
ae7e9ddd 11016 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
a0274e3e 11017 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
48ae6c13
RH
11018 return size;
11019
11020 incompatible:
11021 error ("incompatible type for argument %d of %qE", 1, function);
11022 return 0;
11023}
11024
c22cacf3 11025/* A helper function for resolve_overloaded_builtin. Adds casts to
48ae6c13
RH
11026 PARAMS to make arguments match up with those of FUNCTION. Drops
11027 the variadic arguments at the end. Returns false if some error
11028 was encountered; true on success. */
11029
11030static bool
86951993 11031sync_resolve_params (location_t loc, tree orig_function, tree function,
9771b263 11032 vec<tree, va_gc> *params, bool orig_format)
48ae6c13 11033{
e19a18d4 11034 function_args_iterator iter;
48ae6c13 11035 tree ptype;
bbbbb16a 11036 unsigned int parmnum;
48ae6c13 11037
e19a18d4 11038 function_args_iter_init (&iter, TREE_TYPE (function));
48ae6c13
RH
11039 /* We've declared the implementation functions to use "volatile void *"
11040 as the pointer parameter, so we shouldn't get any complaints from the
11041 call to check_function_arguments what ever type the user used. */
e19a18d4 11042 function_args_iter_next (&iter);
9771b263 11043 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
267bac10 11044 ptype = TYPE_MAIN_VARIANT (ptype);
48ae6c13
RH
11045
11046 /* For the rest of the values, we need to cast these to FTYPE, so that we
11047 don't get warnings for passing pointer types, etc. */
bbbbb16a 11048 parmnum = 0;
e19a18d4 11049 while (1)
48ae6c13 11050 {
e19a18d4
NF
11051 tree val, arg_type;
11052
11053 arg_type = function_args_iter_cond (&iter);
11054 /* XXX void_type_node belies the abstraction. */
11055 if (arg_type == void_type_node)
11056 break;
48ae6c13 11057
bbbbb16a 11058 ++parmnum;
9771b263 11059 if (params->length () <= parmnum)
48ae6c13 11060 {
86951993 11061 error_at (loc, "too few arguments to function %qE", orig_function);
48ae6c13
RH
11062 return false;
11063 }
11064
e3793c6f
JJ
11065 /* Only convert parameters if arg_type is unsigned integer type with
11066 new format sync routines, i.e. don't attempt to convert pointer
11067 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
11068 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
11069 kinds). */
11070 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
86951993
AM
11071 {
11072 /* Ideally for the first conversion we'd use convert_for_assignment
11073 so that we get warnings for anything that doesn't match the pointer
11074 type. This isn't portable across the C and C++ front ends atm. */
9771b263 11075 val = (*params)[parmnum];
86951993
AM
11076 val = convert (ptype, val);
11077 val = convert (arg_type, val);
9771b263 11078 (*params)[parmnum] = val;
86951993 11079 }
48ae6c13 11080
e19a18d4 11081 function_args_iter_next (&iter);
48ae6c13
RH
11082 }
11083
86951993 11084 /* __atomic routines are not variadic. */
9771b263 11085 if (!orig_format && params->length () != parmnum + 1)
86951993
AM
11086 {
11087 error_at (loc, "too many arguments to function %qE", orig_function);
11088 return false;
11089 }
11090
48ae6c13
RH
11091 /* The definition of these primitives is variadic, with the remaining
11092 being "an optional list of variables protected by the memory barrier".
11093 No clue what that's supposed to mean, precisely, but we consider all
11094 call-clobbered variables to be protected so we're safe. */
9771b263 11095 params->truncate (parmnum + 1);
48ae6c13
RH
11096
11097 return true;
11098}
11099
c22cacf3 11100/* A helper function for resolve_overloaded_builtin. Adds a cast to
48ae6c13
RH
11101 RESULT to make it match the type of the first pointer argument in
11102 PARAMS. */
11103
11104static tree
86951993 11105sync_resolve_return (tree first_param, tree result, bool orig_format)
48ae6c13 11106{
bbbbb16a 11107 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
86951993 11108 tree rtype = TREE_TYPE (result);
99db1ef0 11109 ptype = TYPE_MAIN_VARIANT (ptype);
86951993
AM
11110
11111 /* New format doesn't require casting unless the types are the same size. */
11112 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
11113 return convert (ptype, result);
11114 else
11115 return result;
11116}
11117
11118/* This function verifies the PARAMS to generic atomic FUNCTION.
11119 It returns the size if all the parameters are the same size, otherwise
11120 0 is returned if the parameters are invalid. */
11121
11122static int
9771b263
DN
11123get_atomic_generic_size (location_t loc, tree function,
11124 vec<tree, va_gc> *params)
86951993
AM
11125{
11126 unsigned int n_param;
11127 unsigned int n_model;
11128 unsigned int x;
11129 int size_0;
11130 tree type_0;
11131
11132 /* Determine the parameter makeup. */
11133 switch (DECL_FUNCTION_CODE (function))
11134 {
11135 case BUILT_IN_ATOMIC_EXCHANGE:
11136 n_param = 4;
11137 n_model = 1;
11138 break;
11139 case BUILT_IN_ATOMIC_LOAD:
11140 case BUILT_IN_ATOMIC_STORE:
11141 n_param = 3;
11142 n_model = 1;
11143 break;
11144 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11145 n_param = 6;
11146 n_model = 2;
11147 break;
11148 default:
c466c4ff 11149 gcc_unreachable ();
86951993
AM
11150 }
11151
9771b263 11152 if (vec_safe_length (params) != n_param)
86951993
AM
11153 {
11154 error_at (loc, "incorrect number of arguments to function %qE", function);
11155 return 0;
11156 }
11157
11158 /* Get type of first parameter, and determine its size. */
9771b263 11159 type_0 = TREE_TYPE ((*params)[0]);
6415bd5d
JM
11160 if (TREE_CODE (type_0) == ARRAY_TYPE)
11161 {
11162 /* Force array-to-pointer decay for C++. */
11163 gcc_assert (c_dialect_cxx());
11164 (*params)[0] = default_conversion ((*params)[0]);
11165 type_0 = TREE_TYPE ((*params)[0]);
11166 }
c466c4ff
AM
11167 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11168 {
11169 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11170 function);
11171 return 0;
11172 }
11173
11174 /* Types must be compile time constant sizes. */
11175 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
86951993 11176 {
c466c4ff
AM
11177 error_at (loc,
11178 "argument 1 of %qE must be a pointer to a constant size type",
11179 function);
86951993
AM
11180 return 0;
11181 }
c466c4ff 11182
ae7e9ddd 11183 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
86951993 11184
c466c4ff
AM
11185 /* Zero size objects are not allowed. */
11186 if (size_0 == 0)
11187 {
11188 error_at (loc,
11189 "argument 1 of %qE must be a pointer to a nonzero size object",
11190 function);
11191 return 0;
11192 }
11193
86951993
AM
11194 /* Check each other parameter is a pointer and the same size. */
11195 for (x = 0; x < n_param - n_model; x++)
11196 {
11197 int size;
9771b263 11198 tree type = TREE_TYPE ((*params)[x]);
688010ba 11199 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
86951993
AM
11200 if (n_param == 6 && x == 3)
11201 continue;
11202 if (!POINTER_TYPE_P (type))
11203 {
11204 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11205 function);
11206 return 0;
11207 }
7b56b2f8
MP
11208 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11209 size = type_size ? tree_to_uhwi (type_size) : 0;
86951993
AM
11210 if (size != size_0)
11211 {
11212 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11213 function);
11214 return 0;
11215 }
11216 }
11217
11218 /* Check memory model parameters for validity. */
11219 for (x = n_param - n_model ; x < n_param; x++)
11220 {
9771b263 11221 tree p = (*params)[x];
86951993
AM
11222 if (TREE_CODE (p) == INTEGER_CST)
11223 {
ae7e9ddd 11224 int i = tree_to_uhwi (p);
46b35980 11225 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
86951993
AM
11226 {
11227 warning_at (loc, OPT_Winvalid_memory_model,
11228 "invalid memory model argument %d of %qE", x + 1,
11229 function);
86951993
AM
11230 }
11231 }
11232 else
11233 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11234 {
11235 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11236 function);
11237 return 0;
11238 }
11239 }
11240
11241 return size_0;
11242}
11243
11244
11245/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11246 at the beginning of the parameter list PARAMS representing the size of the
11247 objects. This is to match the library ABI requirement. LOC is the location
11248 of the function call.
11249 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11250 returned to allow the external call to be constructed. */
11251
11252static tree
11253add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9771b263 11254 vec<tree, va_gc> *params)
86951993
AM
11255{
11256 tree size_node;
11257
11258 /* Insert a SIZE_T parameter as the first param. If there isn't
11259 enough space, allocate a new vector and recursively re-build with that. */
9771b263 11260 if (!params->space (1))
86951993
AM
11261 {
11262 unsigned int z, len;
9771b263 11263 vec<tree, va_gc> *v;
86951993
AM
11264 tree f;
11265
9771b263
DN
11266 len = params->length ();
11267 vec_alloc (v, len + 1);
8edbfaa6 11268 v->quick_push (build_int_cst (size_type_node, n));
86951993 11269 for (z = 0; z < len; z++)
9771b263 11270 v->quick_push ((*params)[z]);
81e5eca8 11271 f = build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 11272 vec_free (v);
86951993
AM
11273 return f;
11274 }
11275
11276 /* Add the size parameter and leave as a function call for processing. */
11277 size_node = build_int_cst (size_type_node, n);
9771b263 11278 params->quick_insert (0, size_node);
86951993
AM
11279 return NULL_TREE;
11280}
11281
11282
6b28e197
JM
11283/* Return whether atomic operations for naturally aligned N-byte
11284 arguments are supported, whether inline or through libatomic. */
11285static bool
11286atomic_size_supported_p (int n)
11287{
11288 switch (n)
11289 {
11290 case 1:
11291 case 2:
11292 case 4:
11293 case 8:
11294 return true;
11295
11296 case 16:
11297 return targetm.scalar_mode_supported_p (TImode);
11298
11299 default:
11300 return false;
11301 }
11302}
11303
86951993
AM
11304/* This will process an __atomic_exchange function call, determine whether it
11305 needs to be mapped to the _N variation, or turned into a library call.
11306 LOC is the location of the builtin call.
11307 FUNCTION is the DECL that has been invoked;
11308 PARAMS is the argument list for the call. The return value is non-null
11309 TRUE is returned if it is translated into the proper format for a call to the
11310 external library, and NEW_RETURN is set the tree for that function.
11311 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11312 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11313static bool
11314resolve_overloaded_atomic_exchange (location_t loc, tree function,
9771b263 11315 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11316{
11317 tree p0, p1, p2, p3;
11318 tree I_type, I_type_ptr;
11319 int n = get_atomic_generic_size (loc, function, params);
11320
c466c4ff
AM
11321 /* Size of 0 is an error condition. */
11322 if (n == 0)
11323 {
11324 *new_return = error_mark_node;
11325 return true;
11326 }
11327
86951993 11328 /* If not a lock-free size, change to the library generic format. */
6b28e197 11329 if (!atomic_size_supported_p (n))
86951993
AM
11330 {
11331 *new_return = add_atomic_size_parameter (n, loc, function, params);
11332 return true;
11333 }
11334
11335 /* Otherwise there is a lockfree match, transform the call from:
11336 void fn(T* mem, T* desired, T* return, model)
11337 into
11338 *return = (T) (fn (In* mem, (In) *desired, model)) */
11339
9771b263
DN
11340 p0 = (*params)[0];
11341 p1 = (*params)[1];
11342 p2 = (*params)[2];
11343 p3 = (*params)[3];
86951993
AM
11344
11345 /* Create pointer to appropriate size. */
11346 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11347 I_type_ptr = build_pointer_type (I_type);
11348
11349 /* Convert object pointer to required type. */
11350 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11351 (*params)[0] = p0;
86951993
AM
11352 /* Convert new value to required type, and dereference it. */
11353 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11354 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11355 (*params)[1] = p1;
86951993
AM
11356
11357 /* Move memory model to the 3rd position, and end param list. */
9771b263
DN
11358 (*params)[2] = p3;
11359 params->truncate (3);
86951993
AM
11360
11361 /* Convert return pointer and dereference it for later assignment. */
11362 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11363
11364 return false;
48ae6c13
RH
11365}
11366
86951993
AM
11367
11368/* This will process an __atomic_compare_exchange function call, determine
11369 whether it needs to be mapped to the _N variation, or turned into a lib call.
11370 LOC is the location of the builtin call.
11371 FUNCTION is the DECL that has been invoked;
11372 PARAMS is the argument list for the call. The return value is non-null
11373 TRUE is returned if it is translated into the proper format for a call to the
11374 external library, and NEW_RETURN is set the tree for that function.
11375 FALSE is returned if processing for the _N variation is required. */
11376
11377static bool
11378resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9771b263 11379 vec<tree, va_gc> *params,
86951993
AM
11380 tree *new_return)
11381{
11382 tree p0, p1, p2;
11383 tree I_type, I_type_ptr;
11384 int n = get_atomic_generic_size (loc, function, params);
11385
c466c4ff
AM
11386 /* Size of 0 is an error condition. */
11387 if (n == 0)
11388 {
11389 *new_return = error_mark_node;
11390 return true;
11391 }
11392
86951993 11393 /* If not a lock-free size, change to the library generic format. */
6b28e197 11394 if (!atomic_size_supported_p (n))
86951993
AM
11395 {
11396 /* The library generic format does not have the weak parameter, so
11397 remove it from the param list. Since a parameter has been removed,
11398 we can be sure that there is room for the SIZE_T parameter, meaning
11399 there will not be a recursive rebuilding of the parameter list, so
11400 there is no danger this will be done twice. */
11401 if (n > 0)
11402 {
9771b263
DN
11403 (*params)[3] = (*params)[4];
11404 (*params)[4] = (*params)[5];
11405 params->truncate (5);
86951993
AM
11406 }
11407 *new_return = add_atomic_size_parameter (n, loc, function, params);
11408 return true;
11409 }
11410
11411 /* Otherwise, there is a match, so the call needs to be transformed from:
11412 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11413 into
11414 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11415
9771b263
DN
11416 p0 = (*params)[0];
11417 p1 = (*params)[1];
11418 p2 = (*params)[2];
86951993
AM
11419
11420 /* Create pointer to appropriate size. */
11421 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11422 I_type_ptr = build_pointer_type (I_type);
11423
11424 /* Convert object pointer to required type. */
11425 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11426 (*params)[0] = p0;
86951993
AM
11427
11428 /* Convert expected pointer to required type. */
11429 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9771b263 11430 (*params)[1] = p1;
86951993
AM
11431
11432 /* Convert desired value to required type, and dereference it. */
11433 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11434 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9771b263 11435 (*params)[2] = p2;
86951993
AM
11436
11437 /* The rest of the parameters are fine. NULL means no special return value
11438 processing.*/
11439 *new_return = NULL;
11440 return false;
11441}
11442
11443
11444/* This will process an __atomic_load function call, determine whether it
11445 needs to be mapped to the _N variation, or turned into a library call.
11446 LOC is the location of the builtin call.
11447 FUNCTION is the DECL that has been invoked;
11448 PARAMS is the argument list for the call. The return value is non-null
11449 TRUE is returned if it is translated into the proper format for a call to the
11450 external library, and NEW_RETURN is set the tree for that function.
11451 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11452 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11453
11454static bool
11455resolve_overloaded_atomic_load (location_t loc, tree function,
9771b263 11456 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11457{
11458 tree p0, p1, p2;
11459 tree I_type, I_type_ptr;
11460 int n = get_atomic_generic_size (loc, function, params);
11461
c466c4ff
AM
11462 /* Size of 0 is an error condition. */
11463 if (n == 0)
11464 {
11465 *new_return = error_mark_node;
11466 return true;
11467 }
11468
86951993 11469 /* If not a lock-free size, change to the library generic format. */
6b28e197 11470 if (!atomic_size_supported_p (n))
86951993
AM
11471 {
11472 *new_return = add_atomic_size_parameter (n, loc, function, params);
11473 return true;
11474 }
11475
11476 /* Otherwise, there is a match, so the call needs to be transformed from:
11477 void fn(T* mem, T* return, model)
11478 into
11479 *return = (T) (fn ((In *) mem, model)) */
11480
9771b263
DN
11481 p0 = (*params)[0];
11482 p1 = (*params)[1];
11483 p2 = (*params)[2];
86951993
AM
11484
11485 /* Create pointer to appropriate size. */
11486 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11487 I_type_ptr = build_pointer_type (I_type);
11488
11489 /* Convert object pointer to required type. */
11490 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11491 (*params)[0] = p0;
86951993
AM
11492
11493 /* Move memory model to the 2nd position, and end param list. */
9771b263
DN
11494 (*params)[1] = p2;
11495 params->truncate (2);
86951993
AM
11496
11497 /* Convert return pointer and dereference it for later assignment. */
11498 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11499
11500 return false;
11501}
11502
11503
11504/* This will process an __atomic_store function call, determine whether it
11505 needs to be mapped to the _N variation, or turned into a library call.
11506 LOC is the location of the builtin call.
11507 FUNCTION is the DECL that has been invoked;
11508 PARAMS is the argument list for the call. The return value is non-null
11509 TRUE is returned if it is translated into the proper format for a call to the
11510 external library, and NEW_RETURN is set the tree for that function.
11511 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11512 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11513
11514static bool
11515resolve_overloaded_atomic_store (location_t loc, tree function,
9771b263 11516 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11517{
11518 tree p0, p1;
11519 tree I_type, I_type_ptr;
11520 int n = get_atomic_generic_size (loc, function, params);
11521
c466c4ff
AM
11522 /* Size of 0 is an error condition. */
11523 if (n == 0)
11524 {
11525 *new_return = error_mark_node;
11526 return true;
11527 }
11528
86951993 11529 /* If not a lock-free size, change to the library generic format. */
6b28e197 11530 if (!atomic_size_supported_p (n))
86951993
AM
11531 {
11532 *new_return = add_atomic_size_parameter (n, loc, function, params);
11533 return true;
11534 }
11535
11536 /* Otherwise, there is a match, so the call needs to be transformed from:
11537 void fn(T* mem, T* value, model)
11538 into
11539 fn ((In *) mem, (In) *value, model) */
11540
9771b263
DN
11541 p0 = (*params)[0];
11542 p1 = (*params)[1];
86951993
AM
11543
11544 /* Create pointer to appropriate size. */
11545 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11546 I_type_ptr = build_pointer_type (I_type);
11547
11548 /* Convert object pointer to required type. */
11549 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11550 (*params)[0] = p0;
86951993
AM
11551
11552 /* Convert new value to required type, and dereference it. */
11553 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11554 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11555 (*params)[1] = p1;
86951993
AM
11556
11557 /* The memory model is in the right spot already. Return is void. */
11558 *new_return = NULL_TREE;
11559
11560 return false;
11561}
11562
11563
48ae6c13
RH
11564/* Some builtin functions are placeholders for other expressions. This
11565 function should be called immediately after parsing the call expression
11566 before surrounding code has committed to the type of the expression.
11567
c2255bc4
AH
11568 LOC is the location of the builtin call.
11569
48ae6c13
RH
11570 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11571 PARAMS is the argument list for the call. The return value is non-null
11572 when expansion is complete, and null if normal processing should
11573 continue. */
11574
11575tree
9771b263
DN
11576resolve_overloaded_builtin (location_t loc, tree function,
11577 vec<tree, va_gc> *params)
48ae6c13
RH
11578{
11579 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
86951993
AM
11580 bool orig_format = true;
11581 tree new_return = NULL_TREE;
11582
58646b77
PB
11583 switch (DECL_BUILT_IN_CLASS (function))
11584 {
11585 case BUILT_IN_NORMAL:
11586 break;
11587 case BUILT_IN_MD:
11588 if (targetm.resolve_overloaded_builtin)
c2255bc4 11589 return targetm.resolve_overloaded_builtin (loc, function, params);
58646b77 11590 else
c22cacf3 11591 return NULL_TREE;
58646b77
PB
11592 default:
11593 return NULL_TREE;
11594 }
c22cacf3 11595
58646b77 11596 /* Handle BUILT_IN_NORMAL here. */
48ae6c13
RH
11597 switch (orig_code)
11598 {
86951993
AM
11599 case BUILT_IN_ATOMIC_EXCHANGE:
11600 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11601 case BUILT_IN_ATOMIC_LOAD:
11602 case BUILT_IN_ATOMIC_STORE:
11603 {
11604 /* Handle these 4 together so that they can fall through to the next
11605 case if the call is transformed to an _N variant. */
11606 switch (orig_code)
11607 {
11608 case BUILT_IN_ATOMIC_EXCHANGE:
11609 {
11610 if (resolve_overloaded_atomic_exchange (loc, function, params,
11611 &new_return))
11612 return new_return;
11613 /* Change to the _N variant. */
11614 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11615 break;
11616 }
11617
11618 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11619 {
11620 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11621 params,
11622 &new_return))
11623 return new_return;
11624 /* Change to the _N variant. */
11625 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11626 break;
11627 }
11628 case BUILT_IN_ATOMIC_LOAD:
11629 {
11630 if (resolve_overloaded_atomic_load (loc, function, params,
11631 &new_return))
11632 return new_return;
11633 /* Change to the _N variant. */
11634 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11635 break;
11636 }
11637 case BUILT_IN_ATOMIC_STORE:
11638 {
11639 if (resolve_overloaded_atomic_store (loc, function, params,
11640 &new_return))
11641 return new_return;
11642 /* Change to the _N variant. */
11643 orig_code = BUILT_IN_ATOMIC_STORE_N;
11644 break;
11645 }
11646 default:
11647 gcc_unreachable ();
11648 }
11649 /* Fallthrough to the normal processing. */
11650 }
11651 case BUILT_IN_ATOMIC_EXCHANGE_N:
11652 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11653 case BUILT_IN_ATOMIC_LOAD_N:
11654 case BUILT_IN_ATOMIC_STORE_N:
11655 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11656 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11657 case BUILT_IN_ATOMIC_AND_FETCH_N:
11658 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11659 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11660 case BUILT_IN_ATOMIC_OR_FETCH_N:
11661 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11662 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11663 case BUILT_IN_ATOMIC_FETCH_AND_N:
11664 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11665 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11666 case BUILT_IN_ATOMIC_FETCH_OR_N:
11667 {
11668 orig_format = false;
11669 /* Fallthru for parameter processing. */
11670 }
e0a8ecf2
AM
11671 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11672 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11673 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11674 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11675 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11676 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11677 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11678 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11679 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11680 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11681 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11682 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11683 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11684 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11685 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11686 case BUILT_IN_SYNC_LOCK_RELEASE_N:
48ae6c13
RH
11687 {
11688 int n = sync_resolve_size (function, params);
bbbbb16a 11689 tree new_function, first_param, result;
e79983f4 11690 enum built_in_function fncode;
48ae6c13
RH
11691
11692 if (n == 0)
11693 return error_mark_node;
11694
e79983f4
MM
11695 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11696 new_function = builtin_decl_explicit (fncode);
86951993
AM
11697 if (!sync_resolve_params (loc, function, new_function, params,
11698 orig_format))
48ae6c13
RH
11699 return error_mark_node;
11700
9771b263 11701 first_param = (*params)[0];
81e5eca8
MP
11702 result = build_function_call_vec (loc, vNULL, new_function, params,
11703 NULL);
86951993
AM
11704 if (result == error_mark_node)
11705 return result;
e0a8ecf2 11706 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
86951993
AM
11707 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11708 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11709 result = sync_resolve_return (first_param, result, orig_format);
48ae6c13 11710
86951993
AM
11711 /* If new_return is set, assign function to that expr and cast the
11712 result to void since the generic interface returned void. */
11713 if (new_return)
11714 {
11715 /* Cast function result from I{1,2,4,8,16} to the required type. */
11716 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11717 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11718 result);
11719 TREE_SIDE_EFFECTS (result) = 1;
11720 protected_set_expr_location (result, loc);
11721 result = convert (void_type_node, result);
11722 }
48ae6c13
RH
11723 return result;
11724 }
11725
11726 default:
58646b77 11727 return NULL_TREE;
48ae6c13
RH
11728 }
11729}
11730
0af94e6f
JR
11731/* vector_types_compatible_elements_p is used in type checks of vectors
11732 values used as operands of binary operators. Where it returns true, and
11733 the other checks of the caller succeed (being vector types in he first
11734 place, and matching number of elements), we can just treat the types
11735 as essentially the same.
11736 Contrast with vector_targets_convertible_p, which is used for vector
11737 pointer types, and vector_types_convertible_p, which will allow
11738 language-specific matches under the control of flag_lax_vector_conversions,
11739 and might still require a conversion. */
11740/* True if vector types T1 and T2 can be inputs to the same binary
11741 operator without conversion.
11742 We don't check the overall vector size here because some of our callers
11743 want to give different error messages when the vectors are compatible
11744 except for the element count. */
11745
5bed876a 11746bool
0af94e6f 11747vector_types_compatible_elements_p (tree t1, tree t2)
5bed876a 11748{
0af94e6f
JR
11749 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11750 t1 = TREE_TYPE (t1);
11751 t2 = TREE_TYPE (t2);
11752
5bed876a
AH
11753 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11754
ab22c1fa
CF
11755 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11756 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11757 || c2 == FIXED_POINT_TYPE));
5bed876a 11758
0af94e6f
JR
11759 t1 = c_common_signed_type (t1);
11760 t2 = c_common_signed_type (t2);
5bed876a
AH
11761 /* Equality works here because c_common_signed_type uses
11762 TYPE_MAIN_VARIANT. */
0af94e6f
JR
11763 if (t1 == t2)
11764 return true;
11765 if (opaque && c1 == c2
11766 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11767 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11768 return true;
11769 return false;
5bed876a
AH
11770}
11771
104f8784
KG
11772/* Check for missing format attributes on function pointers. LTYPE is
11773 the new type or left-hand side type. RTYPE is the old type or
11774 right-hand side type. Returns TRUE if LTYPE is missing the desired
11775 attribute. */
11776
11777bool
11778check_missing_format_attribute (tree ltype, tree rtype)
11779{
11780 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11781 tree ra;
11782
11783 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11784 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11785 break;
11786 if (ra)
11787 {
11788 tree la;
11789 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11790 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11791 break;
11792 return !la;
11793 }
11794 else
11795 return false;
11796}
11797
ff6b6641
GDR
11798/* Subscripting with type char is likely to lose on a machine where
11799 chars are signed. So warn on any machine, but optionally. Don't
11800 warn for unsigned char since that type is safe. Don't warn for
11801 signed char because anyone who uses that must have done so
11802 deliberately. Furthermore, we reduce the false positive load by
11803 warning only for non-constant value of type char. */
11804
11805void
5bd012f8 11806warn_array_subscript_with_type_char (location_t loc, tree index)
ff6b6641
GDR
11807{
11808 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11809 && TREE_CODE (index) != INTEGER_CST)
5bd012f8
MP
11810 warning_at (loc, OPT_Wchar_subscripts,
11811 "array subscript has type %<char%>");
ff6b6641
GDR
11812}
11813
2a67bec2
ILT
11814/* Implement -Wparentheses for the unexpected C precedence rules, to
11815 cover cases like x + y << z which readers are likely to
11816 misinterpret. We have seen an expression in which CODE is a binary
100d537d
MLI
11817 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11818 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11819 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11820 expression was not formed using a binary or unary operator, or it
11821 was enclosed in parentheses. */
2a67bec2
ILT
11822
11823void
5d9de0d0 11824warn_about_parentheses (location_t loc, enum tree_code code,
fb3e178a 11825 enum tree_code code_left, tree arg_left,
100d537d 11826 enum tree_code code_right, tree arg_right)
2a67bec2
ILT
11827{
11828 if (!warn_parentheses)
11829 return;
11830
100d537d
MLI
11831 /* This macro tests that the expression ARG with original tree code
11832 CODE appears to be a boolean expression. or the result of folding a
11833 boolean expression. */
11834#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11835 (truth_value_p (TREE_CODE (ARG)) \
11836 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11837 /* Folding may create 0 or 1 integers from other expressions. */ \
11838 || ((CODE) != INTEGER_CST \
11839 && (integer_onep (ARG) || integer_zerop (ARG))))
11840
b8698a0f 11841 switch (code)
2a67bec2 11842 {
100d537d 11843 case LSHIFT_EXPR:
5d9de0d0
PC
11844 if (code_left == PLUS_EXPR)
11845 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11846 "suggest parentheses around %<+%> inside %<<<%>");
11847 else if (code_right == PLUS_EXPR)
11848 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11849 "suggest parentheses around %<+%> inside %<<<%>");
11850 else if (code_left == MINUS_EXPR)
11851 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11852 "suggest parentheses around %<-%> inside %<<<%>");
11853 else if (code_right == MINUS_EXPR)
11854 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11855 "suggest parentheses around %<-%> inside %<<<%>");
100d537d 11856 return;
2a67bec2 11857
100d537d 11858 case RSHIFT_EXPR:
5d9de0d0
PC
11859 if (code_left == PLUS_EXPR)
11860 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11861 "suggest parentheses around %<+%> inside %<>>%>");
11862 else if (code_right == PLUS_EXPR)
11863 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11864 "suggest parentheses around %<+%> inside %<>>%>");
11865 else if (code_left == MINUS_EXPR)
11866 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11867 "suggest parentheses around %<-%> inside %<>>%>");
11868 else if (code_right == MINUS_EXPR)
11869 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11870 "suggest parentheses around %<-%> inside %<>>%>");
100d537d 11871 return;
2a67bec2 11872
100d537d 11873 case TRUTH_ORIF_EXPR:
5d9de0d0
PC
11874 if (code_left == TRUTH_ANDIF_EXPR)
11875 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11876 "suggest parentheses around %<&&%> within %<||%>");
11877 else if (code_right == TRUTH_ANDIF_EXPR)
11878 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11879 "suggest parentheses around %<&&%> within %<||%>");
100d537d
MLI
11880 return;
11881
11882 case BIT_IOR_EXPR:
2a67bec2 11883 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
5d9de0d0
PC
11884 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11885 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11886 "suggest parentheses around arithmetic in operand of %<|%>");
11887 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11888 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11889 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11890 "suggest parentheses around arithmetic in operand of %<|%>");
2a67bec2 11891 /* Check cases like x|y==z */
5d9de0d0
PC
11892 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11893 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11894 "suggest parentheses around comparison in operand of %<|%>");
11895 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11896 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11897 "suggest parentheses around comparison in operand of %<|%>");
11898 /* Check cases like !x | y */
11899 else if (code_left == TRUTH_NOT_EXPR
11900 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11901 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11902 "suggest parentheses around operand of "
11903 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
100d537d 11904 return;
2a67bec2 11905
100d537d 11906 case BIT_XOR_EXPR:
2a67bec2 11907 if (code_left == BIT_AND_EXPR
5d9de0d0
PC
11908 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11909 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11910 "suggest parentheses around arithmetic in operand of %<^%>");
11911 else if (code_right == BIT_AND_EXPR
11912 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11913 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11914 "suggest parentheses around arithmetic in operand of %<^%>");
2a67bec2 11915 /* Check cases like x^y==z */
5d9de0d0
PC
11916 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11917 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11918 "suggest parentheses around comparison in operand of %<^%>");
11919 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11920 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11921 "suggest parentheses around comparison in operand of %<^%>");
11922 return;
2a67bec2 11923
100d537d 11924 case BIT_AND_EXPR:
5d9de0d0
PC
11925 if (code_left == PLUS_EXPR)
11926 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11927 "suggest parentheses around %<+%> in operand of %<&%>");
11928 else if (code_right == PLUS_EXPR)
11929 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11930 "suggest parentheses around %<+%> in operand of %<&%>");
5d9de0d0
PC
11931 else if (code_left == MINUS_EXPR)
11932 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11933 "suggest parentheses around %<-%> in operand of %<&%>");
11934 else if (code_right == MINUS_EXPR)
11935 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11936 "suggest parentheses around %<-%> in operand of %<&%>");
2a67bec2 11937 /* Check cases like x&y==z */
5d9de0d0
PC
11938 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11939 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11940 "suggest parentheses around comparison in operand of %<&%>");
11941 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11942 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11943 "suggest parentheses around comparison in operand of %<&%>");
11944 /* Check cases like !x & y */
11945 else if (code_left == TRUTH_NOT_EXPR
11946 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11947 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11948 "suggest parentheses around operand of "
11949 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
100d537d 11950 return;
2a67bec2 11951
100d537d 11952 case EQ_EXPR:
5d9de0d0
PC
11953 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11954 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11955 "suggest parentheses around comparison in operand of %<==%>");
11956 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11957 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11958 "suggest parentheses around comparison in operand of %<==%>");
11959 return;
11960 case NE_EXPR:
5d9de0d0
PC
11961 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11962 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11963 "suggest parentheses around comparison in operand of %<!=%>");
11964 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11965 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11966 "suggest parentheses around comparison in operand of %<!=%>");
11967 return;
11968
11969 default:
5d9de0d0
PC
11970 if (TREE_CODE_CLASS (code) == tcc_comparison)
11971 {
11972 if (TREE_CODE_CLASS (code_left) == tcc_comparison
fb3e178a
JJ
11973 && code_left != NE_EXPR && code_left != EQ_EXPR
11974 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
5d9de0d0
PC
11975 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11976 "comparisons like %<X<=Y<=Z%> do not "
11977 "have their mathematical meaning");
11978 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
fb3e178a 11979 && code_right != NE_EXPR && code_right != EQ_EXPR
5d9de0d0
PC
11980 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11981 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11982 "comparisons like %<X<=Y<=Z%> do not "
11983 "have their mathematical meaning");
11984 }
100d537d 11985 return;
e7917d06 11986 }
100d537d 11987#undef NOT_A_BOOLEAN_EXPR_P
2a67bec2
ILT
11988}
11989
c616e51b
MLI
11990/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11991
11992void
11993warn_for_unused_label (tree label)
11994{
11995 if (!TREE_USED (label))
11996 {
11997 if (DECL_INITIAL (label))
11998 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11999 else
12000 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
12001 }
12002}
ff6b6641 12003
c9f9eb5d
AH
12004/* Warn for division by zero according to the value of DIVISOR. LOC
12005 is the location of the division operator. */
2e9cb75e
MLI
12006
12007void
c9f9eb5d 12008warn_for_div_by_zero (location_t loc, tree divisor)
2e9cb75e 12009{
ab22c1fa
CF
12010 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
12011 about division by zero. Do not issue a warning if DIVISOR has a
2e9cb75e
MLI
12012 floating-point type, since we consider 0.0/0.0 a valid way of
12013 generating a NaN. */
7d882b83 12014 if (c_inhibit_evaluation_warnings == 0
ab22c1fa 12015 && (integer_zerop (divisor) || fixed_zerop (divisor)))
c9f9eb5d 12016 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
2e9cb75e
MLI
12017}
12018
2d12797c
MLI
12019/* Subroutine of build_binary_op. Give warnings for comparisons
12020 between signed and unsigned quantities that may fail. Do the
12021 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
12022 so that casts will be considered, but default promotions won't
ba47d38d
AH
12023 be.
12024
12025 LOCATION is the location of the comparison operator.
2d12797c
MLI
12026
12027 The arguments of this function map directly to local variables
12028 of build_binary_op. */
12029
b8698a0f 12030void
ba47d38d 12031warn_for_sign_compare (location_t location,
b8698a0f
L
12032 tree orig_op0, tree orig_op1,
12033 tree op0, tree op1,
2d12797c
MLI
12034 tree result_type, enum tree_code resultcode)
12035{
12036 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
12037 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
12038 int unsignedp0, unsignedp1;
b8698a0f 12039
2d12797c
MLI
12040 /* In C++, check for comparison of different enum types. */
12041 if (c_dialect_cxx()
12042 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
12043 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
12044 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
ead51d36 12045 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
2d12797c 12046 {
ba47d38d
AH
12047 warning_at (location,
12048 OPT_Wsign_compare, "comparison between types %qT and %qT",
12049 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
2d12797c
MLI
12050 }
12051
12052 /* Do not warn if the comparison is being done in a signed type,
12053 since the signed type will only be chosen if it can represent
12054 all the values of the unsigned type. */
12055 if (!TYPE_UNSIGNED (result_type))
12056 /* OK */;
12057 /* Do not warn if both operands are unsigned. */
12058 else if (op0_signed == op1_signed)
12059 /* OK */;
12060 else
12061 {
ead51d36 12062 tree sop, uop, base_type;
2d12797c 12063 bool ovf;
ead51d36 12064
2d12797c
MLI
12065 if (op0_signed)
12066 sop = orig_op0, uop = orig_op1;
b8698a0f 12067 else
2d12797c
MLI
12068 sop = orig_op1, uop = orig_op0;
12069
b8698a0f 12070 STRIP_TYPE_NOPS (sop);
2d12797c 12071 STRIP_TYPE_NOPS (uop);
ead51d36
JJ
12072 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12073 ? TREE_TYPE (result_type) : result_type);
2d12797c
MLI
12074
12075 /* Do not warn if the signed quantity is an unsuffixed integer
12076 literal (or some static constant expression involving such
12077 literals or a conditional expression involving such literals)
12078 and it is non-negative. */
12079 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12080 /* OK */;
12081 /* Do not warn if the comparison is an equality operation, the
12082 unsigned quantity is an integral constant, and it would fit
12083 in the result if the result were signed. */
12084 else if (TREE_CODE (uop) == INTEGER_CST
12085 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
ead51d36 12086 && int_fits_type_p (uop, c_common_signed_type (base_type)))
2d12797c
MLI
12087 /* OK */;
12088 /* In C, do not warn if the unsigned quantity is an enumeration
12089 constant and its maximum value would fit in the result if the
12090 result were signed. */
12091 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12092 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12093 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
ead51d36 12094 c_common_signed_type (base_type)))
2d12797c 12095 /* OK */;
b8698a0f 12096 else
ba47d38d 12097 warning_at (location,
b8698a0f 12098 OPT_Wsign_compare,
ba47d38d 12099 "comparison between signed and unsigned integer expressions");
2d12797c 12100 }
b8698a0f 12101
2d12797c
MLI
12102 /* Warn if two unsigned values are being compared in a size larger
12103 than their original size, and one (and only one) is the result of
12104 a `~' operator. This comparison will always fail.
b8698a0f 12105
2d12797c
MLI
12106 Also warn if one operand is a constant, and the constant does not
12107 have all bits set that are set in the ~ operand when it is
12108 extended. */
12109
828fb3ba
JM
12110 op0 = c_common_get_narrower (op0, &unsignedp0);
12111 op1 = c_common_get_narrower (op1, &unsignedp1);
b8698a0f 12112
2d12797c
MLI
12113 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12114 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12115 {
12116 if (TREE_CODE (op0) == BIT_NOT_EXPR)
828fb3ba 12117 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2d12797c 12118 if (TREE_CODE (op1) == BIT_NOT_EXPR)
828fb3ba 12119 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2d12797c 12120
9541ffee 12121 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2d12797c
MLI
12122 {
12123 tree primop;
12124 HOST_WIDE_INT constant, mask;
12125 int unsignedp;
12126 unsigned int bits;
b8698a0f 12127
9541ffee 12128 if (tree_fits_shwi_p (op0))
2d12797c
MLI
12129 {
12130 primop = op1;
12131 unsignedp = unsignedp1;
9439e9a1 12132 constant = tree_to_shwi (op0);
2d12797c
MLI
12133 }
12134 else
12135 {
12136 primop = op0;
12137 unsignedp = unsignedp0;
9439e9a1 12138 constant = tree_to_shwi (op1);
2d12797c 12139 }
b8698a0f 12140
2d12797c
MLI
12141 bits = TYPE_PRECISION (TREE_TYPE (primop));
12142 if (bits < TYPE_PRECISION (result_type)
12143 && bits < HOST_BITS_PER_LONG && unsignedp)
12144 {
aa256c4a 12145 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
2d12797c
MLI
12146 if ((mask & constant) != mask)
12147 {
12148 if (constant == 0)
0a756a3f
MP
12149 warning_at (location, OPT_Wsign_compare,
12150 "promoted ~unsigned is always non-zero");
2d12797c 12151 else
b8698a0f 12152 warning_at (location, OPT_Wsign_compare,
ba47d38d 12153 "comparison of promoted ~unsigned with constant");
2d12797c
MLI
12154 }
12155 }
12156 }
12157 else if (unsignedp0 && unsignedp1
12158 && (TYPE_PRECISION (TREE_TYPE (op0))
12159 < TYPE_PRECISION (result_type))
12160 && (TYPE_PRECISION (TREE_TYPE (op1))
12161 < TYPE_PRECISION (result_type)))
ba47d38d 12162 warning_at (location, OPT_Wsign_compare,
2d12797c
MLI
12163 "comparison of promoted ~unsigned with unsigned");
12164 }
12165}
12166
c5ee1358
MM
12167/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12168 type via c_common_type. If -Wdouble-promotion is in use, and the
12169 conditions for warning have been met, issue a warning. GMSGID is
12170 the warning message. It must have two %T specifiers for the type
12171 that was converted (generally "float") and the type to which it was
12172 converted (generally "double), respectively. LOC is the location
12173 to which the awrning should refer. */
12174
12175void
12176do_warn_double_promotion (tree result_type, tree type1, tree type2,
12177 const char *gmsgid, location_t loc)
12178{
12179 tree source_type;
12180
12181 if (!warn_double_promotion)
12182 return;
12183 /* If the conversion will not occur at run-time, there is no need to
12184 warn about it. */
12185 if (c_inhibit_evaluation_warnings)
12186 return;
12187 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12188 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12189 return;
12190 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12191 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12192 source_type = type1;
12193 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12194 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12195 source_type = type2;
12196 else
12197 return;
12198 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12199}
12200
da2e71c9
MLI
12201/* Possibly warn about unused parameters. */
12202
12203void
12204do_warn_unused_parameter (tree fn)
12205{
12206 tree decl;
12207
12208 for (decl = DECL_ARGUMENTS (fn);
12209 decl; decl = DECL_CHAIN (decl))
12210 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12211 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12212 && !TREE_NO_WARNING (decl))
12213 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12214 "unused parameter %qD", decl);
12215}
12216
12217
d0940d56
DS
12218/* Setup a TYPE_DECL node as a typedef representation.
12219
12220 X is a TYPE_DECL for a typedef statement. Create a brand new
12221 ..._TYPE node (which will be just a variant of the existing
12222 ..._TYPE node with identical properties) and then install X
12223 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12224
12225 The whole point here is to end up with a situation where each
12226 and every ..._TYPE node the compiler creates will be uniquely
12227 associated with AT MOST one node representing a typedef name.
12228 This way, even though the compiler substitutes corresponding
12229 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12230 early on, later parts of the compiler can always do the reverse
12231 translation and get back the corresponding typedef name. For
12232 example, given:
12233
12234 typedef struct S MY_TYPE;
12235 MY_TYPE object;
12236
12237 Later parts of the compiler might only know that `object' was of
12238 type `struct S' if it were not for code just below. With this
12239 code however, later parts of the compiler see something like:
12240
12241 struct S' == struct S
12242 typedef struct S' MY_TYPE;
12243 struct S' object;
12244
12245 And they can then deduce (from the node for type struct S') that
12246 the original object declaration was:
12247
12248 MY_TYPE object;
12249
12250 Being able to do this is important for proper support of protoize,
12251 and also for generating precise symbolic debugging information
12252 which takes full account of the programmer's (typedef) vocabulary.
12253
12254 Obviously, we don't want to generate a duplicate ..._TYPE node if
12255 the TYPE_DECL node that we are now processing really represents a
12256 standard built-in type. */
12257
12258void
12259set_underlying_type (tree x)
12260{
12261 if (x == error_mark_node)
12262 return;
12263 if (DECL_IS_BUILTIN (x))
12264 {
12265 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12266 TYPE_NAME (TREE_TYPE (x)) = x;
12267 }
12268 else if (TREE_TYPE (x) != error_mark_node
12269 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12270 {
12271 tree tt = TREE_TYPE (x);
12272 DECL_ORIGINAL_TYPE (x) = tt;
12273 tt = build_variant_type_copy (tt);
12274 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12275 TYPE_NAME (tt) = x;
12276 TREE_USED (tt) = TREE_USED (x);
12277 TREE_TYPE (x) = tt;
12278 }
12279}
12280
b646ba3f
DS
12281/* Record the types used by the current global variable declaration
12282 being parsed, so that we can decide later to emit their debug info.
12283 Those types are in types_used_by_cur_var_decl, and we are going to
12284 store them in the types_used_by_vars_hash hash table.
12285 DECL is the declaration of the global variable that has been parsed. */
12286
12287void
12288record_types_used_by_current_var_decl (tree decl)
12289{
12290 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12291
9771b263 12292 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
b646ba3f 12293 {
9771b263 12294 tree type = types_used_by_cur_var_decl->pop ();
bc87224e 12295 types_used_by_var_decl_insert (type, decl);
b646ba3f
DS
12296 }
12297}
12298
3797cb21
DS
12299/* If DECL is a typedef that is declared in the current function,
12300 record it for the purpose of -Wunused-local-typedefs. */
12301
12302void
12303record_locally_defined_typedef (tree decl)
12304{
12305 struct c_language_function *l;
12306
12307 if (!warn_unused_local_typedefs
12308 || cfun == NULL
12309 /* if this is not a locally defined typedef then we are not
12310 interested. */
12311 || !is_typedef_decl (decl)
12312 || !decl_function_context (decl))
12313 return;
12314
12315 l = (struct c_language_function *) cfun->language;
9771b263 12316 vec_safe_push (l->local_typedefs, decl);
3797cb21
DS
12317}
12318
12319/* If T is a TYPE_DECL declared locally, mark it as used. */
12320
12321void
12322maybe_record_typedef_use (tree t)
12323{
12324 if (!is_typedef_decl (t))
12325 return;
12326
12327 TREE_USED (t) = true;
12328}
12329
12330/* Warn if there are some unused locally defined typedefs in the
12331 current function. */
12332
12333void
12334maybe_warn_unused_local_typedefs (void)
12335{
12336 int i;
12337 tree decl;
12338 /* The number of times we have emitted -Wunused-local-typedefs
12339 warnings. If this is different from errorcount, that means some
12340 unrelated errors have been issued. In which case, we'll avoid
12341 emitting "unused-local-typedefs" warnings. */
12342 static int unused_local_typedefs_warn_count;
12343 struct c_language_function *l;
12344
12345 if (cfun == NULL)
12346 return;
12347
12348 if ((l = (struct c_language_function *) cfun->language) == NULL)
12349 return;
12350
12351 if (warn_unused_local_typedefs
12352 && errorcount == unused_local_typedefs_warn_count)
12353 {
9771b263 12354 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
3797cb21
DS
12355 if (!TREE_USED (decl))
12356 warning_at (DECL_SOURCE_LOCATION (decl),
12357 OPT_Wunused_local_typedefs,
12358 "typedef %qD locally defined but not used", decl);
12359 unused_local_typedefs_warn_count = errorcount;
12360 }
12361
9771b263 12362 vec_free (l->local_typedefs);
3797cb21
DS
12363}
12364
04159acf
MP
12365/* Warn about boolean expression compared with an integer value different
12366 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12367 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12368 are the operands of the comparison. The caller must ensure that
12369 either operand is a boolean expression. */
12370
12371void
12372maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12373 tree op1)
12374{
12375 if (TREE_CODE_CLASS (code) != tcc_comparison)
12376 return;
12377
12378 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12379 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12380 if (!cst)
12381 return;
12382
12383 if (!integer_zerop (cst) && !integer_onep (cst))
12384 {
716c0ba6
MP
12385 int sign = (TREE_CODE (op0) == INTEGER_CST
12386 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
04159acf
MP
12387 if (code == EQ_EXPR
12388 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12389 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12390 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12391 "with boolean expression is always false", cst);
12392 else
12393 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12394 "with boolean expression is always true", cst);
12395 }
716c0ba6
MP
12396 else if (integer_zerop (cst) || integer_onep (cst))
12397 {
577cd070
MP
12398 /* If the non-constant operand isn't of a boolean type, we
12399 don't want to warn here. */
12400 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12401 /* Handle booleans promoted to integers. */
12402 if (CONVERT_EXPR_P (noncst)
12403 && TREE_TYPE (noncst) == integer_type_node
12404 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12405 /* Warn. */;
12406 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12407 && !truth_value_p (TREE_CODE (noncst)))
12408 return;
716c0ba6
MP
12409 /* Do some magic to get the right diagnostics. */
12410 bool flag = TREE_CODE (op0) == INTEGER_CST;
12411 flag = integer_zerop (cst) ? flag : !flag;
12412 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12413 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12414 "with boolean expression is always true", cst);
12415 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12416 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12417 "with boolean expression is always false", cst);
12418 }
04159acf
MP
12419}
12420
451b5e48
MP
12421/* Warn if signed left shift overflows. We don't warn
12422 about left-shifting 1 into the sign bit in C++14; cf.
12423 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12424 LOC is a location of the shift; OP0 and OP1 are the operands.
12425 Return true if an overflow is detected, false otherwise. */
12426
12427bool
12428maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12429{
12430 if (TREE_CODE (op0) != INTEGER_CST
12431 || TREE_CODE (op1) != INTEGER_CST)
12432 return false;
12433
12434 tree type0 = TREE_TYPE (op0);
12435 unsigned int prec0 = TYPE_PRECISION (type0);
12436
12437 /* Left-hand operand must be signed. */
12438 if (TYPE_UNSIGNED (type0))
12439 return false;
12440
b893e375
MP
12441 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12442 + TREE_INT_CST_LOW (op1));
451b5e48 12443 /* Handle the left-shifting 1 into the sign bit case. */
b893e375 12444 if (min_prec == prec0 + 1)
451b5e48
MP
12445 {
12446 /* Never warn for C++14 onwards. */
12447 if (cxx_dialect >= cxx14)
12448 return false;
12449 /* Otherwise only if -Wshift-overflow=2. But return
12450 true to signal an overflow for the sake of integer
12451 constant expressions. */
12452 if (warn_shift_overflow < 2)
12453 return true;
12454 }
12455
451b5e48
MP
12456 bool overflowed = min_prec > prec0;
12457 if (overflowed && c_inhibit_evaluation_warnings == 0)
12458 warning_at (loc, OPT_Wshift_overflow_,
12459 "result of %qE requires %u bits to represent, "
12460 "but %qT only has %u bits",
12461 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12462 min_prec, type0, prec0);
12463
12464 return overflowed;
12465}
12466
c166b898
ILT
12467/* The C and C++ parsers both use vectors to hold function arguments.
12468 For efficiency, we keep a cache of unused vectors. This is the
12469 cache. */
12470
9771b263
DN
12471typedef vec<tree, va_gc> *tree_gc_vec;
12472static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
c166b898
ILT
12473
12474/* Return a new vector from the cache. If the cache is empty,
12475 allocate a new vector. These vectors are GC'ed, so it is OK if the
12476 pointer is not released.. */
12477
9771b263 12478vec<tree, va_gc> *
c166b898
ILT
12479make_tree_vector (void)
12480{
9771b263
DN
12481 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12482 return tree_vector_cache->pop ();
c166b898
ILT
12483 else
12484 {
9771b263 12485 /* Passing 0 to vec::alloc returns NULL, and our callers require
c166b898
ILT
12486 that we always return a non-NULL value. The vector code uses
12487 4 when growing a NULL vector, so we do too. */
9771b263
DN
12488 vec<tree, va_gc> *v;
12489 vec_alloc (v, 4);
12490 return v;
c166b898
ILT
12491 }
12492}
12493
12494/* Release a vector of trees back to the cache. */
12495
12496void
9771b263 12497release_tree_vector (vec<tree, va_gc> *vec)
c166b898
ILT
12498{
12499 if (vec != NULL)
12500 {
9771b263
DN
12501 vec->truncate (0);
12502 vec_safe_push (tree_vector_cache, vec);
c166b898
ILT
12503 }
12504}
12505
12506/* Get a new tree vector holding a single tree. */
12507
9771b263 12508vec<tree, va_gc> *
c166b898
ILT
12509make_tree_vector_single (tree t)
12510{
9771b263
DN
12511 vec<tree, va_gc> *ret = make_tree_vector ();
12512 ret->quick_push (t);
c166b898
ILT
12513 return ret;
12514}
12515
c12ff9d8
JM
12516/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12517
9771b263 12518vec<tree, va_gc> *
c12ff9d8
JM
12519make_tree_vector_from_list (tree list)
12520{
9771b263 12521 vec<tree, va_gc> *ret = make_tree_vector ();
c12ff9d8 12522 for (; list; list = TREE_CHAIN (list))
9771b263 12523 vec_safe_push (ret, TREE_VALUE (list));
c12ff9d8
JM
12524 return ret;
12525}
12526
c166b898
ILT
12527/* Get a new tree vector which is a copy of an existing one. */
12528
9771b263
DN
12529vec<tree, va_gc> *
12530make_tree_vector_copy (const vec<tree, va_gc> *orig)
c166b898 12531{
9771b263 12532 vec<tree, va_gc> *ret;
c166b898
ILT
12533 unsigned int ix;
12534 tree t;
12535
12536 ret = make_tree_vector ();
9771b263
DN
12537 vec_safe_reserve (ret, vec_safe_length (orig));
12538 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12539 ret->quick_push (t);
c166b898
ILT
12540 return ret;
12541}
12542
a9aa2c3a
NF
12543/* Return true if KEYWORD starts a type specifier. */
12544
12545bool
12546keyword_begins_type_specifier (enum rid keyword)
12547{
12548 switch (keyword)
12549 {
38b7bc7f 12550 case RID_AUTO_TYPE:
a9aa2c3a
NF
12551 case RID_INT:
12552 case RID_CHAR:
12553 case RID_FLOAT:
12554 case RID_DOUBLE:
12555 case RID_VOID:
a9aa2c3a
NF
12556 case RID_UNSIGNED:
12557 case RID_LONG:
12558 case RID_SHORT:
12559 case RID_SIGNED:
12560 case RID_DFLOAT32:
12561 case RID_DFLOAT64:
12562 case RID_DFLOAT128:
12563 case RID_FRACT:
12564 case RID_ACCUM:
12565 case RID_BOOL:
12566 case RID_WCHAR:
12567 case RID_CHAR16:
12568 case RID_CHAR32:
12569 case RID_SAT:
12570 case RID_COMPLEX:
12571 case RID_TYPEOF:
12572 case RID_STRUCT:
12573 case RID_CLASS:
12574 case RID_UNION:
12575 case RID_ENUM:
12576 return true;
12577 default:
78a7c317
DD
12578 if (keyword >= RID_FIRST_INT_N
12579 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12580 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12581 return true;
a9aa2c3a
NF
12582 return false;
12583 }
12584}
12585
12586/* Return true if KEYWORD names a type qualifier. */
12587
12588bool
12589keyword_is_type_qualifier (enum rid keyword)
12590{
12591 switch (keyword)
12592 {
12593 case RID_CONST:
12594 case RID_VOLATILE:
12595 case RID_RESTRICT:
267bac10 12596 case RID_ATOMIC:
a9aa2c3a
NF
12597 return true;
12598 default:
12599 return false;
12600 }
12601}
12602
12603/* Return true if KEYWORD names a storage class specifier.
12604
12605 RID_TYPEDEF is not included in this list despite `typedef' being
12606 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12607 such for syntactic convenience only. */
12608
12609bool
12610keyword_is_storage_class_specifier (enum rid keyword)
12611{
12612 switch (keyword)
12613 {
12614 case RID_STATIC:
12615 case RID_EXTERN:
12616 case RID_REGISTER:
12617 case RID_AUTO:
12618 case RID_MUTABLE:
12619 case RID_THREAD:
12620 return true;
12621 default:
12622 return false;
12623 }
12624}
12625
ba9e6dd5
NF
12626/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12627
12628static bool
12629keyword_is_function_specifier (enum rid keyword)
12630{
12631 switch (keyword)
12632 {
12633 case RID_INLINE:
bbceee64 12634 case RID_NORETURN:
ba9e6dd5
NF
12635 case RID_VIRTUAL:
12636 case RID_EXPLICIT:
12637 return true;
12638 default:
12639 return false;
12640 }
12641}
12642
12643/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12644 declaration-specifier (C99 6.7). */
12645
12646bool
12647keyword_is_decl_specifier (enum rid keyword)
12648{
12649 if (keyword_is_storage_class_specifier (keyword)
12650 || keyword_is_type_qualifier (keyword)
12651 || keyword_is_function_specifier (keyword))
12652 return true;
12653
12654 switch (keyword)
12655 {
12656 case RID_TYPEDEF:
12657 case RID_FRIEND:
12658 case RID_CONSTEXPR:
12659 return true;
12660 default:
12661 return false;
12662 }
12663}
12664
81f653d6
NF
12665/* Initialize language-specific-bits of tree_contains_struct. */
12666
12667void
12668c_common_init_ts (void)
12669{
12670 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12671 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
36536d79 12672 MARK_TS_TYPED (ARRAY_NOTATION_REF);
81f653d6
NF
12673}
12674
3ce4f9e4
ESR
12675/* Build a user-defined numeric literal out of an integer constant type VALUE
12676 with identifier SUFFIX. */
12677
12678tree
2d7aa578
ESR
12679build_userdef_literal (tree suffix_id, tree value,
12680 enum overflow_type overflow, tree num_string)
3ce4f9e4
ESR
12681{
12682 tree literal = make_node (USERDEF_LITERAL);
12683 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12684 USERDEF_LITERAL_VALUE (literal) = value;
2d7aa578 12685 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
3ce4f9e4
ESR
12686 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12687 return literal;
12688}
12689
7edaa4d2 12690/* For vector[index], convert the vector to a
aa7da51a
JJ
12691 pointer of the underlying type. Return true if the resulting
12692 ARRAY_REF should not be an lvalue. */
12693
12694bool
7edaa4d2 12695convert_vector_to_pointer_for_subscript (location_t loc,
aa7da51a 12696 tree *vecp, tree index)
7edaa4d2 12697{
aa7da51a 12698 bool ret = false;
31521951 12699 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7edaa4d2
MG
12700 {
12701 tree type = TREE_TYPE (*vecp);
12702 tree type1;
12703
aa7da51a 12704 ret = !lvalue_p (*vecp);
7edaa4d2 12705 if (TREE_CODE (index) == INTEGER_CST)
cc269bb6 12706 if (!tree_fits_uhwi_p (index)
7d362f6c 12707 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7edaa4d2
MG
12708 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12709
aa7da51a
JJ
12710 if (ret)
12711 {
b731b390 12712 tree tmp = create_tmp_var_raw (type);
aa7da51a
JJ
12713 DECL_SOURCE_LOCATION (tmp) = loc;
12714 *vecp = c_save_expr (*vecp);
12715 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12716 {
12717 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12718 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12719 *vecp
12720 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12721 *vecp, NULL_TREE, NULL_TREE),
12722 non_const);
12723 }
12724 else
12725 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12726 NULL_TREE, NULL_TREE);
12727 SET_EXPR_LOCATION (*vecp, loc);
12728 c_common_mark_addressable_vec (tmp);
12729 }
12730 else
12731 c_common_mark_addressable_vec (*vecp);
7edaa4d2 12732 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7edaa4d2 12733 type1 = build_pointer_type (TREE_TYPE (*vecp));
b0f1bf36
RB
12734 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12735 if (!ref_all
12736 && !DECL_P (*vecp))
12737 {
12738 /* If the original vector isn't declared may_alias and it
12739 isn't a bare vector look if the subscripting would
12740 alias the vector we subscript, and if not, force ref-all. */
12741 alias_set_type vecset = get_alias_set (*vecp);
12742 alias_set_type sset = get_alias_set (type);
12743 if (!alias_sets_must_conflict_p (sset, vecset)
12744 && !alias_set_subset_of (sset, vecset))
12745 ref_all = true;
12746 }
12747 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7edaa4d2
MG
12748 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12749 *vecp = convert (type, *vecp);
12750 }
aa7da51a 12751 return ret;
7edaa4d2
MG
12752}
12753
a212e43f
MG
12754/* Determine which of the operands, if any, is a scalar that needs to be
12755 converted to a vector, for the range of operations. */
12756enum stv_conv
12757scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12758 bool complain)
12759{
12760 tree type0 = TREE_TYPE (op0);
12761 tree type1 = TREE_TYPE (op1);
12762 bool integer_only_op = false;
12763 enum stv_conv ret = stv_firstarg;
12764
31521951 12765 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
a212e43f
MG
12766 switch (code)
12767 {
12768 /* Most GENERIC binary expressions require homogeneous arguments.
12769 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12770 argument that is a vector and a second one that is a scalar, so
12771 we never return stv_secondarg for them. */
12772 case RSHIFT_EXPR:
12773 case LSHIFT_EXPR:
12774 if (TREE_CODE (type0) == INTEGER_TYPE
12775 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12776 {
68fca595 12777 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12778 {
12779 if (complain)
12780 error_at (loc, "conversion of scalar %qT to vector %qT "
12781 "involves truncation", type0, type1);
12782 return stv_error;
12783 }
12784 else
12785 return stv_firstarg;
12786 }
12787 break;
12788
12789 case BIT_IOR_EXPR:
12790 case BIT_XOR_EXPR:
12791 case BIT_AND_EXPR:
12792 integer_only_op = true;
12793 /* ... fall through ... */
12794
93100c6b
MG
12795 case VEC_COND_EXPR:
12796
a212e43f
MG
12797 case PLUS_EXPR:
12798 case MINUS_EXPR:
12799 case MULT_EXPR:
12800 case TRUNC_DIV_EXPR:
12801 case CEIL_DIV_EXPR:
12802 case FLOOR_DIV_EXPR:
12803 case ROUND_DIV_EXPR:
12804 case EXACT_DIV_EXPR:
12805 case TRUNC_MOD_EXPR:
12806 case FLOOR_MOD_EXPR:
12807 case RDIV_EXPR:
12808 case EQ_EXPR:
12809 case NE_EXPR:
12810 case LE_EXPR:
12811 case GE_EXPR:
12812 case LT_EXPR:
12813 case GT_EXPR:
12814 /* What about UNLT_EXPR? */
31521951 12815 if (VECTOR_TYPE_P (type0))
a212e43f 12816 {
a212e43f 12817 ret = stv_secondarg;
6b4db501
MM
12818 std::swap (type0, type1);
12819 std::swap (op0, op1);
a212e43f
MG
12820 }
12821
12822 if (TREE_CODE (type0) == INTEGER_TYPE
12823 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12824 {
68fca595 12825 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12826 {
12827 if (complain)
12828 error_at (loc, "conversion of scalar %qT to vector %qT "
12829 "involves truncation", type0, type1);
12830 return stv_error;
12831 }
12832 return ret;
12833 }
12834 else if (!integer_only_op
12835 /* Allow integer --> real conversion if safe. */
12836 && (TREE_CODE (type0) == REAL_TYPE
12837 || TREE_CODE (type0) == INTEGER_TYPE)
12838 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12839 {
68fca595 12840 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12841 {
12842 if (complain)
12843 error_at (loc, "conversion of scalar %qT to vector %qT "
12844 "involves truncation", type0, type1);
12845 return stv_error;
12846 }
12847 return ret;
12848 }
12849 default:
12850 break;
12851 }
12852
12853 return stv_nothing;
12854}
12855
e28d52cf
DS
12856/* Return true iff ALIGN is an integral constant that is a fundamental
12857 alignment, as defined by [basic.align] in the c++-11
12858 specifications.
12859
12860 That is:
12861
12862 [A fundamental alignment is represented by an alignment less than or
12863 equal to the greatest alignment supported by the implementation
12864 in all contexts, which is equal to
12865 alignof(max_align_t)]. */
12866
12867bool
12868cxx_fundamental_alignment_p (unsigned align)
12869{
12870 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12871 TYPE_ALIGN (long_double_type_node)));
12872}
12873
f04dda30
MP
12874/* Return true if T is a pointer to a zero-sized aggregate. */
12875
12876bool
12877pointer_to_zero_sized_aggr_p (tree t)
12878{
12879 if (!POINTER_TYPE_P (t))
12880 return false;
12881 t = TREE_TYPE (t);
12882 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12883}
12884
1807ffc1
MS
12885/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12886 with no library fallback or for an ADDR_EXPR whose operand is such type
12887 issues an error pointing to the location LOC.
12888 Returns true when the expression has been diagnosed and false
12889 otherwise. */
12890bool
12891reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12892{
12893 if (TREE_CODE (expr) == ADDR_EXPR)
12894 expr = TREE_OPERAND (expr, 0);
12895
12896 if (TREE_TYPE (expr)
12897 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12898 && DECL_P (expr)
12899 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12900 false positives for user-declared built-ins such as abs or
12901 strlen, and for C++ operators new and delete.
12902 The c_decl_implicit() test avoids false positives for implicitly
12903 declared built-ins with library fallbacks (such as abs). */
12904 && DECL_BUILT_IN (expr)
12905 && DECL_IS_BUILTIN (expr)
12906 && !c_decl_implicit (expr)
12907 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12908 {
12909 if (loc == UNKNOWN_LOCATION)
12910 loc = EXPR_LOC_OR_LOC (expr, input_location);
12911
12912 /* Reject arguments that are built-in functions with
12913 no library fallback. */
12914 error_at (loc, "built-in function %qE must be directly called", expr);
12915
12916 return true;
12917 }
12918
12919 return false;
12920}
12921
39dabefd 12922#include "gt-c-family-c-common.h"