]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
* config/nvptx/nvptx.c (nvptx_option_override): Don't kill debug level.
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
818ab71a 2 Copyright (C) 1992-2016 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"
2adfab87
AM
25#include "target.h"
26#include "function.h"
2adfab87 27#include "tree.h"
b559c810 28#include "c-common.h"
2adfab87
AM
29#include "gimple-expr.h"
30#include "tm_p.h"
31#include "stringpool.h"
32#include "cgraph.h"
33#include "diagnostic.h"
d9b2742a 34#include "intl.h"
d8a2d370
DN
35#include "stor-layout.h"
36#include "calls.h"
d8a2d370
DN
37#include "attribs.h"
38#include "varasm.h"
39#include "trans-mem.h"
61d3ce20 40#include "c-objc.h"
677f3fa8 41#include "common/common-target.h"
7afff7cf 42#include "langhooks.h"
d57a4b98 43#include "tree-inline.h"
5f1989e6 44#include "toplev.h"
6de9cd9a 45#include "tree-iterator.h"
d974312d 46#include "opts.h"
45b0be94 47#include "gimplify.h"
cb60f38d 48
81a75f0f 49cpp_reader *parse_in; /* Declared in c-pragma.h. */
c8724862 50
8fba1830
BRF
51/* Mode used to build pointers (VOIDmode means ptr_mode). */
52
53machine_mode c_default_pointer_mode = VOIDmode;
54
7f4edbcb 55/* The following symbols are subsumed in the c_global_trees array, and
d125d268 56 listed here individually for documentation purposes.
7f4edbcb
BS
57
58 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
59
60 tree short_integer_type_node;
61 tree long_integer_type_node;
62 tree long_long_integer_type_node;
63
64 tree short_unsigned_type_node;
65 tree long_unsigned_type_node;
66 tree long_long_unsigned_type_node;
67
de7df9eb
JM
68 tree truthvalue_type_node;
69 tree truthvalue_false_node;
70 tree truthvalue_true_node;
7f4edbcb
BS
71
72 tree ptrdiff_type_node;
73
74 tree unsigned_char_type_node;
75 tree signed_char_type_node;
76 tree wchar_type_node;
7f4edbcb 77
b6baa67d
KVH
78 tree char16_type_node;
79 tree char32_type_node;
80
7f4edbcb
BS
81 tree float_type_node;
82 tree double_type_node;
83 tree long_double_type_node;
84
85 tree complex_integer_type_node;
86 tree complex_float_type_node;
87 tree complex_double_type_node;
88 tree complex_long_double_type_node;
89
9a8ce21f
JG
90 tree dfloat32_type_node;
91 tree dfloat64_type_node;
92 tree_dfloat128_type_node;
93
7f4edbcb
BS
94 tree intQI_type_node;
95 tree intHI_type_node;
96 tree intSI_type_node;
97 tree intDI_type_node;
98 tree intTI_type_node;
99
100 tree unsigned_intQI_type_node;
101 tree unsigned_intHI_type_node;
102 tree unsigned_intSI_type_node;
103 tree unsigned_intDI_type_node;
104 tree unsigned_intTI_type_node;
105
106 tree widest_integer_literal_type_node;
107 tree widest_unsigned_literal_type_node;
108
109 Nodes for types `void *' and `const void *'.
110
111 tree ptr_type_node, const_ptr_type_node;
112
113 Nodes for types `char *' and `const char *'.
114
115 tree string_type_node, const_string_type_node;
116
117 Type `char[SOMENUMBER]'.
118 Used when an array of char is needed and the size is irrelevant.
119
120 tree char_array_type_node;
121
7f4edbcb
BS
122 Type `wchar_t[SOMENUMBER]' or something like it.
123 Used when a wide string literal is created.
124
125 tree wchar_array_type_node;
126
b6baa67d
KVH
127 Type `char16_t[SOMENUMBER]' or something like it.
128 Used when a UTF-16 string literal is created.
129
130 tree char16_array_type_node;
131
132 Type `char32_t[SOMENUMBER]' or something like it.
133 Used when a UTF-32 string literal is created.
134
135 tree char32_array_type_node;
136
7f4edbcb
BS
137 Type `int ()' -- used for implicit declaration of functions.
138
139 tree default_function_type;
140
7f4edbcb
BS
141 A VOID_TYPE node, packaged in a TREE_LIST.
142
143 tree void_list_node;
144
684d9f3b 145 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
0ba8a114
NS
146 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
147 VAR_DECLS, but C++ does.)
63ad61ed 148
0ba8a114 149 tree function_name_decl_node;
684d9f3b 150 tree pretty_function_name_decl_node;
0ba8a114
NS
151 tree c99_function_name_decl_node;
152
153 Stack of nested function name VAR_DECLs.
35b1a6fa 154
0ba8a114 155 tree saved_function_name_decls;
63ad61ed 156
7f4edbcb
BS
157*/
158
159tree c_global_trees[CTI_MAX];
17211ab5 160\f
4078b403
NB
161/* Switches common to the C front ends. */
162
63973df3
NB
163/* Nonzero means don't output line number information. */
164
165char flag_no_line_commands;
166
167/* Nonzero causes -E output not to be done, but directives such as
168 #define that have side effects are still obeyed. */
169
170char flag_no_output;
171
172/* Nonzero means dump macros in some fashion. */
173
174char flag_dump_macros;
175
176/* Nonzero means pass #include lines through to the output. */
177
178char flag_dump_includes;
179
c0d578e6
GK
180/* Nonzero means process PCH files while preprocessing. */
181
182bool flag_pch_preprocess;
183
17211ab5
GK
184/* The file name to which we should write a precompiled header, or
185 NULL if no header will be written in this compile. */
186
187const char *pch_file;
188
3df89291
NB
189/* Nonzero if an ISO standard was selected. It rejects macros in the
190 user's namespace. */
191int flag_iso;
192
4078b403
NB
193/* C/ObjC language option variables. */
194
195
4078b403
NB
196/* Nonzero means allow type mismatches in conditional expressions;
197 just make their values `void'. */
198
199int flag_cond_mismatch;
200
201/* Nonzero means enable C89 Amendment 1 features. */
202
203int flag_isoc94;
204
48b0b196 205/* Nonzero means use the ISO C99 (or C11) dialect of C. */
4078b403
NB
206
207int flag_isoc99;
208
48b0b196 209/* Nonzero means use the ISO C11 dialect of C. */
2778d766 210
48b0b196 211int flag_isoc11;
2778d766 212
6614fd40 213/* Nonzero means that we have builtin functions, and main is an int. */
4078b403
NB
214
215int flag_hosted = 1;
216
4078b403
NB
217
218/* ObjC language option variables. */
219
220
4078b403
NB
221/* Tells the compiler that this is a special run. Do not perform any
222 compiling, instead we are to test some platform dependent features
223 and output a C header file with appropriate definitions. */
224
225int print_struct_values;
226
fa10beec 227/* Tells the compiler what is the constant string class for ObjC. */
4078b403
NB
228
229const char *constant_string_class_name;
230
4078b403
NB
231
232/* C++ language option variables. */
233
a75f1574
JM
234/* The reference version of the ABI for -Wabi. */
235
236int warn_abi_version = -1;
4078b403 237
4078b403
NB
238/* Nonzero means generate separate instantiation control files and
239 juggle them at link time. */
240
241int flag_use_repository;
242
129211bc 243/* The C++ dialect being used. Default set in c_common_post_options. */
966541e3 244
129211bc 245enum cxx_dialect cxx_dialect = cxx_unset;
966541e3 246
4afe7ad7 247/* Maximum template instantiation depth. This limit exists to limit the
3ff60975 248 time it takes to notice excessively recursive template instantiations.
4078b403 249
3ff60975
JM
250 The default is lower than the 1024 recommended by the C++0x standard
251 because G++ runs out of stack before 1024 with highly recursive template
252 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
253
254int max_tinst_depth = 900;
4078b403 255
f09f1de5
MM
256/* The elements of `ridpointers' are identifier nodes for the reserved
257 type names and storage classes. It is indexed by a RID_... value. */
258tree *ridpointers;
259
c2255bc4 260tree (*make_fname_decl) (location_t, tree, int);
2ce07e2d 261
7d882b83
ILT
262/* Nonzero means don't warn about problems that occur when the code is
263 executed. */
264int c_inhibit_evaluation_warnings;
e78a3b42 265
5386338c
JM
266/* Whether we are building a boolean conversion inside
267 convert_for_assignment, or some other late binary operation. If
268 build_binary_op is called for C (from code shared by C and C++) in
269 this case, then the operands have already been folded and the
270 result will not be folded again, so C_MAYBE_CONST_EXPR should not
271 be generated. */
272bool in_late_binary_op;
273
148e4216
JM
274/* Whether lexing has been completed, so subsequent preprocessor
275 errors should use the compiler's input_location. */
276bool done_lexing = false;
277
ec5c56db 278/* Information about how a function name is generated. */
0ba8a114
NS
279struct fname_var_t
280{
8b60264b
KG
281 tree *const decl; /* pointer to the VAR_DECL. */
282 const unsigned rid; /* RID number for the identifier. */
283 const int pretty; /* How pretty is it? */
0ba8a114
NS
284};
285
ec5c56db 286/* The three ways of getting then name of the current function. */
0ba8a114
NS
287
288const struct fname_var_t fname_vars[] =
289{
ec5c56db 290 /* C99 compliant __func__, must be first. */
0ba8a114 291 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
ec5c56db 292 /* GCC __FUNCTION__ compliant. */
0ba8a114 293 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
ec5c56db 294 /* GCC __PRETTY_FUNCTION__ compliant. */
0ba8a114
NS
295 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
296 {NULL, 0, 0},
297};
298
c98cd5bf
JM
299/* Global visibility options. */
300struct visibility_flags visibility_options;
301
62e4eb35 302static tree check_case_value (location_t, tree);
b155cfd9
MP
303static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
304 bool *);
4724b3de 305
35b1a6fa
AJ
306static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
307static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
308static tree handle_common_attribute (tree *, tree, tree, int, bool *);
309static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52bf96d2
JH
310static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
311static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
e664c61c
KS
312static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
313 int, bool *);
77bc5132
JJ
314static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
315 int, bool *);
ce6923c5
MP
316static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
317 bool *);
5434dc07 318static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 319static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
86631ea3 320static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
185c9e56 321static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
46a4da10 322static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
323static tree handle_always_inline_attribute (tree *, tree, tree, int,
324 bool *);
d752cfdb
JJ
325static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
326static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0691d1d4 327static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
d2af6a68 328static tree handle_error_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
329static tree handle_used_attribute (tree *, tree, tree, int, bool *);
330static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
ce91e74c
JH
331static tree handle_externally_visible_attribute (tree *, tree, tree, int,
332 bool *);
7861b648
AK
333static tree handle_no_reorder_attribute (tree *, tree, tree, int,
334 bool *);
35b1a6fa
AJ
335static tree handle_const_attribute (tree *, tree, tree, int, bool *);
336static tree handle_transparent_union_attribute (tree *, tree, tree,
337 int, bool *);
ee45a32d
EB
338static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
339 int, bool *);
35b1a6fa
AJ
340static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
341static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
342static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
343static tree handle_section_attribute (tree *, tree, tree, int, bool *);
344static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
345static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4bb794e2 346static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
ba885ec5
NS
347static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
348static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 349static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
a0203ca7 350static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
35b1a6fa
AJ
351static tree handle_visibility_attribute (tree *, tree, tree, int,
352 bool *);
353static tree handle_tls_model_attribute (tree *, tree, tree, int,
354 bool *);
355static tree handle_no_instrument_function_attribute (tree *, tree,
356 tree, int, bool *);
357static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
6e9a3221 358static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
359static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
360 bool *);
361static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
0a35513e
AH
362static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
363static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
dcd6de6d 364static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
365static tree handle_deprecated_attribute (tree *, tree, tree, int,
366 bool *);
367static tree handle_vector_size_attribute (tree *, tree, tree, int,
368 bool *);
369static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
370static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
371static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
72954a4f
JM
372static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
373 bool *);
3d091dac 374static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5d32c25 375static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
51bc54a6 376static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
8fcbce72
JJ
377static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
378static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
5779e713 379static tree handle_target_attribute (tree *, tree, tree, int, bool *);
3b1661a9 380static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
ab442df7 381static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
0a35513e 382static tree ignore_attribute (tree *, tree, tree, int, bool *);
7458026b 383static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
0b7b376d 384static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
2a99e5e6 385static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
826cacfe 386static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
387static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
388 bool *);
fff77217 389static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
390static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
391 bool *);
976d5a22 392static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
d5e254e1
IE
393static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
394static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
395static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
35b1a6fa 396
35b1a6fa
AJ
397static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
398static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
399static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
d07605f5 400static int resort_field_decl_cmp (const void *, const void *);
b34c7881 401
eea1139b
ILT
402/* Reserved words. The third field is a mask: keywords are disabled
403 if they match the mask.
404
405 Masks for languages:
406 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
407 C --std=c99: D_CXXONLY | D_OBJC
408 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
409 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
410 C++ --std=c0x: D_CONLY | D_OBJC
411 ObjC++ is like C++ except that D_OBJC is not set
412
413 If -fno-asm is used, D_ASM is added to the mask. If
414 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
415 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
1973201f 416 In C with -Wc++-compat, we warn if D_CXXWARN is set.
eea1139b 417
1973201f
NP
418 Note the complication of the D_CXX_OBJC keywords. These are
419 reserved words such as 'class'. In C++, 'class' is a reserved
420 word. In Objective-C++ it is too. In Objective-C, it is a
421 reserved word too, but only if it follows an '@' sign.
422*/
eea1139b
ILT
423const struct c_common_resword c_common_reswords[] =
424{
d19fa6b5
JM
425 { "_Alignas", RID_ALIGNAS, D_CONLY },
426 { "_Alignof", RID_ALIGNOF, D_CONLY },
267bac10 427 { "_Atomic", RID_ATOMIC, D_CONLY },
eea1139b
ILT
428 { "_Bool", RID_BOOL, D_CONLY },
429 { "_Complex", RID_COMPLEX, 0 },
939b37da
BI
430 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
431 { "_Cilk_sync", RID_CILK_SYNC, 0 },
9a771876 432 { "_Cilk_for", RID_CILK_FOR, 0 },
fa5da7de 433 { "_Imaginary", RID_IMAGINARY, D_CONLY },
eea1139b
ILT
434 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
435 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
436 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
437 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
438 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
439 { "_Sat", RID_SAT, D_CONLY | D_EXT },
32912286 440 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
bbceee64 441 { "_Noreturn", RID_NORETURN, D_CONLY },
433cc7b0 442 { "_Generic", RID_GENERIC, D_CONLY },
582d9b50 443 { "_Thread_local", RID_THREAD, D_CONLY },
eea1139b
ILT
444 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
445 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
446 { "__alignof", RID_ALIGNOF, 0 },
447 { "__alignof__", RID_ALIGNOF, 0 },
448 { "__asm", RID_ASM, 0 },
449 { "__asm__", RID_ASM, 0 },
450 { "__attribute", RID_ATTRIBUTE, 0 },
451 { "__attribute__", RID_ATTRIBUTE, 0 },
38b7bc7f 452 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
4daba884 453 { "__bases", RID_BASES, D_CXXONLY },
74893f25
RH
454 { "__builtin_call_with_static_chain",
455 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
eea1139b 456 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
d4a83c10 457 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
9e1a8dd1 458 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
eea1139b
ILT
459 { "__builtin_offsetof", RID_OFFSETOF, 0 },
460 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
461 { "__builtin_va_arg", RID_VA_ARG, 0 },
462 { "__complex", RID_COMPLEX, 0 },
463 { "__complex__", RID_COMPLEX, 0 },
464 { "__const", RID_CONST, 0 },
465 { "__const__", RID_CONST, 0 },
466 { "__decltype", RID_DECLTYPE, D_CXXONLY },
4daba884 467 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
eea1139b
ILT
468 { "__extension__", RID_EXTENSION, 0 },
469 { "__func__", RID_C99_FUNCTION_NAME, 0 },
470 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
471 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
472 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
473 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
474 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
475 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
476 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
477 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
3c0d13bf
PC
478 { "__imag", RID_IMAGPART, 0 },
479 { "__imag__", RID_IMAGPART, 0 },
480 { "__inline", RID_INLINE, 0 },
481 { "__inline__", RID_INLINE, 0 },
eea1139b
ILT
482 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
483 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
484 { "__is_class", RID_IS_CLASS, D_CXXONLY },
eea1139b
ILT
485 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
486 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
b3908fcc 487 { "__is_final", RID_IS_FINAL, D_CXXONLY },
3c0d13bf 488 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
eea1139b
ILT
489 { "__is_pod", RID_IS_POD, D_CXXONLY },
490 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
971e17ff 491 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c32097d8
JM
492 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
493 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
dd5d5481
JM
494 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
495 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
b752325e 496 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
eea1139b 497 { "__is_union", RID_IS_UNION, D_CXXONLY },
eea1139b
ILT
498 { "__label__", RID_LABEL, 0 },
499 { "__null", RID_NULL, 0 },
500 { "__real", RID_REALPART, 0 },
501 { "__real__", RID_REALPART, 0 },
502 { "__restrict", RID_RESTRICT, 0 },
503 { "__restrict__", RID_RESTRICT, 0 },
504 { "__signed", RID_SIGNED, 0 },
505 { "__signed__", RID_SIGNED, 0 },
506 { "__thread", RID_THREAD, 0 },
0a35513e
AH
507 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
508 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
509 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
eea1139b
ILT
510 { "__typeof", RID_TYPEOF, 0 },
511 { "__typeof__", RID_TYPEOF, 0 },
3c0d13bf 512 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
eea1139b
ILT
513 { "__volatile", RID_VOLATILE, 0 },
514 { "__volatile__", RID_VOLATILE, 0 },
36a85135
JM
515 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
516 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
517 { "asm", RID_ASM, D_ASM },
518 { "auto", RID_AUTO, 0 },
03c3034e 519 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
520 { "break", RID_BREAK, 0 },
521 { "case", RID_CASE, 0 },
2696a995 522 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
eea1139b 523 { "char", RID_CHAR, 0 },
36a85135
JM
524 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
525 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995 526 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
eea1139b 527 { "const", RID_CONST, 0 },
36a85135 528 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
529 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
530 { "continue", RID_CONTINUE, 0 },
36a85135 531 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b 532 { "default", RID_DEFAULT, 0 },
2696a995 533 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
534 { "do", RID_DO, 0 },
535 { "double", RID_DOUBLE, 0 },
536 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
537 { "else", RID_ELSE, 0 },
538 { "enum", RID_ENUM, 0 },
2696a995
KG
539 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
540 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
eea1139b 541 { "extern", RID_EXTERN, 0 },
2696a995 542 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
543 { "float", RID_FLOAT, 0 },
544 { "for", RID_FOR, 0 },
2696a995 545 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
546 { "goto", RID_GOTO, 0 },
547 { "if", RID_IF, 0 },
548 { "inline", RID_INLINE, D_EXT89 },
549 { "int", RID_INT, 0 },
550 { "long", RID_LONG, 0 },
551 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
2696a995
KG
552 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
553 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
36a85135
JM
554 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
555 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995
KG
556 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
557 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
558 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
559 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
eea1139b
ILT
560 { "register", RID_REGISTER, 0 },
561 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
562 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
563 { "return", RID_RETURN, 0 },
564 { "short", RID_SHORT, 0 },
565 { "signed", RID_SIGNED, 0 },
566 { "sizeof", RID_SIZEOF, 0 },
567 { "static", RID_STATIC, 0 },
36a85135 568 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
569 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
570 { "struct", RID_STRUCT, 0 },
571 { "switch", RID_SWITCH, 0 },
2696a995
KG
572 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
573 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
36a85135 574 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995
KG
575 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
576 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
577 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
eea1139b 578 { "typedef", RID_TYPEDEF, 0 },
2696a995
KG
579 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
580 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
581 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
582 { "union", RID_UNION, 0 },
583 { "unsigned", RID_UNSIGNED, 0 },
2696a995
KG
584 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
585 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
586 { "void", RID_VOID, 0 },
587 { "volatile", RID_VOLATILE, 0 },
588 { "wchar_t", RID_WCHAR, D_CXXONLY },
589 { "while", RID_WHILE, 0 },
971e17ff 590
b8fd7909
JM
591 /* C++ transactional memory. */
592 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
593 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
594 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
595 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
596
971e17ff
AS
597 /* Concepts-related keywords */
598 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
599 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
600
eea1139b
ILT
601 /* These Objective-C keywords are recognized only immediately after
602 an '@'. */
603 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
604 { "defs", RID_AT_DEFS, D_OBJC },
605 { "encode", RID_AT_ENCODE, D_OBJC },
606 { "end", RID_AT_END, D_OBJC },
607 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
608 { "interface", RID_AT_INTERFACE, D_OBJC },
609 { "protocol", RID_AT_PROTOCOL, D_OBJC },
610 { "selector", RID_AT_SELECTOR, D_OBJC },
611 { "finally", RID_AT_FINALLY, D_OBJC },
92902b1b
IS
612 { "optional", RID_AT_OPTIONAL, D_OBJC },
613 { "required", RID_AT_REQUIRED, D_OBJC },
668ea4b1 614 { "property", RID_AT_PROPERTY, D_OBJC },
c37d8c30 615 { "package", RID_AT_PACKAGE, D_OBJC },
da57d1b9
NP
616 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
617 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
eea1139b
ILT
618 /* These are recognized only in protocol-qualifier context
619 (see above) */
620 { "bycopy", RID_BYCOPY, D_OBJC },
621 { "byref", RID_BYREF, D_OBJC },
622 { "in", RID_IN, D_OBJC },
623 { "inout", RID_INOUT, D_OBJC },
624 { "oneway", RID_ONEWAY, D_OBJC },
625 { "out", RID_OUT, D_OBJC },
668ea4b1 626 /* These are recognized inside a property attribute list */
200290f2
NP
627 { "assign", RID_ASSIGN, D_OBJC },
628 { "copy", RID_COPY, D_OBJC },
668ea4b1 629 { "getter", RID_GETTER, D_OBJC },
200290f2
NP
630 { "nonatomic", RID_NONATOMIC, D_OBJC },
631 { "readonly", RID_READONLY, D_OBJC },
632 { "readwrite", RID_READWRITE, D_OBJC },
633 { "retain", RID_RETAIN, D_OBJC },
668ea4b1 634 { "setter", RID_SETTER, D_OBJC },
eea1139b
ILT
635};
636
637const unsigned int num_c_common_reswords =
638 sizeof c_common_reswords / sizeof (struct c_common_resword);
639
d5e254e1
IE
640/* Table of machine-independent attributes common to all C-like languages.
641
642 All attributes referencing arguments should be additionally processed
643 in chkp_copy_function_type_adding_bounds for correct instrumentation
644 by Pointer Bounds Checker.
645 Current list of processed common attributes: nonnull. */
349ae713
NB
646const struct attribute_spec c_common_attribute_table[] =
647{
62d784f7
KT
648 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
649 affects_type_identity } */
349ae713 650 { "packed", 0, 0, false, false, false,
62d784f7 651 handle_packed_attribute , false},
349ae713 652 { "nocommon", 0, 0, true, false, false,
62d784f7 653 handle_nocommon_attribute, false},
349ae713 654 { "common", 0, 0, true, false, false,
62d784f7 655 handle_common_attribute, false },
349ae713
NB
656 /* FIXME: logically, noreturn attributes should be listed as
657 "false, true, true" and apply to function types. But implementing this
658 would require all the places in the compiler that use TREE_THIS_VOLATILE
659 on a decl to identify non-returning functions to be located and fixed
660 to check the function type instead. */
661 { "noreturn", 0, 0, true, false, false,
62d784f7 662 handle_noreturn_attribute, false },
349ae713 663 { "volatile", 0, 0, true, false, false,
62d784f7 664 handle_noreturn_attribute, false },
5434dc07
MD
665 { "stack_protect", 0, 0, true, false, false,
666 handle_stack_protect_attribute, false },
349ae713 667 { "noinline", 0, 0, true, false, false,
62d784f7 668 handle_noinline_attribute, false },
86631ea3 669 { "noclone", 0, 0, true, false, false,
62d784f7 670 handle_noclone_attribute, false },
185c9e56
ML
671 { "no_icf", 0, 0, true, false, false,
672 handle_noicf_attribute, false },
46a4da10 673 { "leaf", 0, 0, true, false, false,
62d784f7 674 handle_leaf_attribute, false },
349ae713 675 { "always_inline", 0, 0, true, false, false,
62d784f7 676 handle_always_inline_attribute, false },
4eb7fd83 677 { "gnu_inline", 0, 0, true, false, false,
62d784f7 678 handle_gnu_inline_attribute, false },
d752cfdb 679 { "artificial", 0, 0, true, false, false,
62d784f7 680 handle_artificial_attribute, false },
0691d1d4 681 { "flatten", 0, 0, true, false, false,
62d784f7 682 handle_flatten_attribute, false },
349ae713 683 { "used", 0, 0, true, false, false,
62d784f7 684 handle_used_attribute, false },
349ae713 685 { "unused", 0, 0, false, false, false,
62d784f7 686 handle_unused_attribute, false },
ce91e74c 687 { "externally_visible", 0, 0, true, false, false,
62d784f7 688 handle_externally_visible_attribute, false },
7861b648
AK
689 { "no_reorder", 0, 0, true, false, false,
690 handle_no_reorder_attribute, false },
349ae713
NB
691 /* The same comments as for noreturn attributes apply to const ones. */
692 { "const", 0, 0, true, false, false,
62d784f7 693 handle_const_attribute, false },
ee45a32d
EB
694 { "scalar_storage_order", 1, 1, false, false, false,
695 handle_scalar_storage_order_attribute, false },
349ae713 696 { "transparent_union", 0, 0, false, false, false,
62d784f7 697 handle_transparent_union_attribute, false },
fc8600f9 698 { "constructor", 0, 1, true, false, false,
62d784f7 699 handle_constructor_attribute, false },
fc8600f9 700 { "destructor", 0, 1, true, false, false,
62d784f7 701 handle_destructor_attribute, false },
349ae713 702 { "mode", 1, 1, false, true, false,
62d784f7 703 handle_mode_attribute, false },
349ae713 704 { "section", 1, 1, true, false, false,
62d784f7 705 handle_section_attribute, false },
349ae713 706 { "aligned", 0, 1, false, false, false,
62d784f7 707 handle_aligned_attribute, false },
349ae713 708 { "weak", 0, 0, true, false, false,
62d784f7 709 handle_weak_attribute, false },
4bb794e2
ST
710 { "noplt", 0, 0, true, false, false,
711 handle_noplt_attribute, false },
ba885ec5 712 { "ifunc", 1, 1, true, false, false,
62d784f7 713 handle_ifunc_attribute, false },
349ae713 714 { "alias", 1, 1, true, false, false,
62d784f7 715 handle_alias_attribute, false },
a0203ca7 716 { "weakref", 0, 1, true, false, false,
62d784f7 717 handle_weakref_attribute, false },
349ae713 718 { "no_instrument_function", 0, 0, true, false, false,
62d784f7
KT
719 handle_no_instrument_function_attribute,
720 false },
349ae713 721 { "malloc", 0, 0, true, false, false,
62d784f7 722 handle_malloc_attribute, false },
6e9a3221 723 { "returns_twice", 0, 0, true, false, false,
62d784f7 724 handle_returns_twice_attribute, false },
349ae713 725 { "no_stack_limit", 0, 0, true, false, false,
62d784f7 726 handle_no_limit_stack_attribute, false },
349ae713 727 { "pure", 0, 0, true, false, false,
62d784f7 728 handle_pure_attribute, false },
0a35513e
AH
729 { "transaction_callable", 0, 0, false, true, false,
730 handle_tm_attribute, false },
731 { "transaction_unsafe", 0, 0, false, true, false,
b8fd7909 732 handle_tm_attribute, true },
0a35513e 733 { "transaction_safe", 0, 0, false, true, false,
b8fd7909
JM
734 handle_tm_attribute, true },
735 { "transaction_safe_dynamic", 0, 0, true, false, false,
0a35513e
AH
736 handle_tm_attribute, false },
737 { "transaction_may_cancel_outer", 0, 0, false, true, false,
738 handle_tm_attribute, false },
739 /* ??? These two attributes didn't make the transition from the
740 Intel language document to the multi-vendor language document. */
741 { "transaction_pure", 0, 0, false, true, false,
742 handle_tm_attribute, false },
743 { "transaction_wrap", 1, 1, true, false, false,
744 handle_tm_wrap_attribute, false },
dcd6de6d
ZD
745 /* For internal use (marking of builtins) only. The name contains space
746 to prevent its usage in source code. */
747 { "no vops", 0, 0, true, false, false,
62d784f7 748 handle_novops_attribute, false },
9b86d6bb 749 { "deprecated", 0, 1, false, false, false,
62d784f7 750 handle_deprecated_attribute, false },
349ae713 751 { "vector_size", 1, 1, false, true, false,
62d784f7 752 handle_vector_size_attribute, false },
d7afec4b 753 { "visibility", 1, 1, false, false, false,
62d784f7 754 handle_visibility_attribute, false },
dce81a1a 755 { "tls_model", 1, 1, true, false, false,
62d784f7 756 handle_tls_model_attribute, false },
b34c7881 757 { "nonnull", 0, -1, false, true, true,
62d784f7 758 handle_nonnull_attribute, false },
39f2f3c8 759 { "nothrow", 0, 0, true, false, false,
62d784f7
KT
760 handle_nothrow_attribute, false },
761 { "may_alias", 0, 0, false, true, false, NULL, false },
0bfa5f65 762 { "cleanup", 1, 1, true, false, false,
62d784f7 763 handle_cleanup_attribute, false },
72954a4f 764 { "warn_unused_result", 0, 0, false, true, true,
62d784f7 765 handle_warn_unused_result_attribute, false },
254986c7 766 { "sentinel", 0, 1, false, true, true,
62d784f7 767 handle_sentinel_attribute, false },
b5d32c25
KG
768 /* For internal use (marking of builtins) only. The name contains space
769 to prevent its usage in source code. */
770 { "type generic", 0, 0, false, true, true,
62d784f7 771 handle_type_generic_attribute, false },
51bc54a6 772 { "alloc_size", 1, 2, false, true, true,
62d784f7 773 handle_alloc_size_attribute, false },
52bf96d2 774 { "cold", 0, 0, true, false, false,
62d784f7 775 handle_cold_attribute, false },
52bf96d2 776 { "hot", 0, 0, true, false, false,
62d784f7 777 handle_hot_attribute, false },
77bc5132
JJ
778 { "no_address_safety_analysis",
779 0, 0, true, false, false,
780 handle_no_address_safety_analysis_attribute,
781 false },
e664c61c
KS
782 { "no_sanitize_address", 0, 0, true, false, false,
783 handle_no_sanitize_address_attribute,
784 false },
de35aa66
MS
785 { "no_sanitize_thread", 0, 0, true, false, false,
786 handle_no_sanitize_address_attribute,
787 false },
ce6923c5
MP
788 { "no_sanitize_undefined", 0, 0, true, false, false,
789 handle_no_sanitize_undefined_attribute,
790 false },
d2af6a68 791 { "warning", 1, 1, true, false, false,
62d784f7 792 handle_error_attribute, false },
d2af6a68 793 { "error", 1, 1, true, false, false,
62d784f7 794 handle_error_attribute, false },
5779e713 795 { "target", 1, -1, true, false, false,
62d784f7 796 handle_target_attribute, false },
3b1661a9
ES
797 { "target_clones", 1, -1, true, false, false,
798 handle_target_clones_attribute, false },
ab442df7 799 { "optimize", 1, -1, true, false, false,
62d784f7 800 handle_optimize_attribute, false },
0a35513e
AH
801 /* For internal use only. The leading '*' both prevents its usage in
802 source code and signals that it may be overridden by machine tables. */
803 { "*tm regparm", 0, 0, false, true, true,
804 ignore_attribute, false },
7458026b 805 { "no_split_stack", 0, 0, true, false, false,
62d784f7 806 handle_no_split_stack_attribute, false },
0b7b376d
RG
807 /* For internal use (marking of builtins and runtime functions) only.
808 The name contains space to prevent its usage in source code. */
809 { "fn spec", 1, 1, false, true, true,
62d784f7 810 handle_fnspec_attribute, false },
2a99e5e6
LL
811 { "warn_unused", 0, 0, false, false, false,
812 handle_warn_unused_attribute, false },
826cacfe
MG
813 { "returns_nonnull", 0, 0, false, true, true,
814 handle_returns_nonnull_attribute, false },
acf0174b
JJ
815 { "omp declare simd", 0, -1, true, false, false,
816 handle_omp_declare_simd_attribute, false },
41958c28
BI
817 { "cilk simd function", 0, -1, true, false, false,
818 handle_omp_declare_simd_attribute, false },
e7b69085 819 { "simd", 0, 1, true, false, false,
fff77217 820 handle_simd_attribute, false },
acf0174b
JJ
821 { "omp declare target", 0, 0, true, false, false,
822 handle_omp_declare_target_attribute, false },
4a38b02b
IV
823 { "omp declare target link", 0, 0, true, false, false,
824 handle_omp_declare_target_attribute, false },
8fcbce72
JJ
825 { "alloc_align", 1, 1, false, true, true,
826 handle_alloc_align_attribute, false },
827 { "assume_aligned", 1, 2, false, true, true,
828 handle_assume_aligned_attribute, false },
976d5a22
TT
829 { "designated_init", 0, 0, false, true, false,
830 handle_designated_init_attribute, false },
d5e254e1
IE
831 { "bnd_variable_size", 0, 0, true, false, false,
832 handle_bnd_variable_size_attribute, false },
833 { "bnd_legacy", 0, 0, true, false, false,
834 handle_bnd_legacy, false },
835 { "bnd_instrument", 0, 0, true, false, false,
836 handle_bnd_instrument, false },
62d784f7 837 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
838};
839
840/* Give the specifications for the format attributes, used by C and all
d5e254e1 841 descendants.
349ae713 842
d5e254e1
IE
843 All attributes referencing arguments should be additionally processed
844 in chkp_copy_function_type_adding_bounds for correct instrumentation
845 by Pointer Bounds Checker.
846 Current list of processed format attributes: format, format_arg. */
349ae713
NB
847const struct attribute_spec c_common_format_attribute_table[] =
848{
62d784f7
KT
849 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
850 affects_type_identity } */
349ae713 851 { "format", 3, 3, false, true, true,
62d784f7 852 handle_format_attribute, false },
349ae713 853 { "format_arg", 1, 1, false, true, true,
62d784f7
KT
854 handle_format_arg_attribute, false },
855 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
856};
857
36c5e70a 858/* Return identifier for address space AS. */
3ef0694c 859
36c5e70a
BE
860const char *
861c_addr_space_name (addr_space_t as)
862{
3ef0694c
UW
863 int rid = RID_FIRST_ADDR_SPACE + as;
864 gcc_assert (ridpointers [rid]);
865 return IDENTIFIER_POINTER (ridpointers [rid]);
36c5e70a
BE
866}
867
ec5c56db 868/* Push current bindings for the function name VAR_DECLS. */
7da551a2
RS
869
870void
35b1a6fa 871start_fname_decls (void)
7da551a2 872{
0ba8a114
NS
873 unsigned ix;
874 tree saved = NULL_TREE;
35b1a6fa 875
0ba8a114
NS
876 for (ix = 0; fname_vars[ix].decl; ix++)
877 {
878 tree decl = *fname_vars[ix].decl;
7da551a2 879
0ba8a114
NS
880 if (decl)
881 {
c62c040f
RG
882 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
883 saved);
0ba8a114
NS
884 *fname_vars[ix].decl = NULL_TREE;
885 }
886 }
887 if (saved || saved_function_name_decls)
888 /* Normally they'll have been NULL, so only push if we've got a
889 stack, or they are non-NULL. */
890 saved_function_name_decls = tree_cons (saved, NULL_TREE,
891 saved_function_name_decls);
892}
893
325c3691
RH
894/* Finish up the current bindings, adding them into the current function's
895 statement tree. This must be done _before_ finish_stmt_tree is called.
896 If there is no current function, we must be at file scope and no statements
897 are involved. Pop the previous bindings. */
0ba8a114
NS
898
899void
35b1a6fa 900finish_fname_decls (void)
0ba8a114
NS
901{
902 unsigned ix;
325c3691 903 tree stmts = NULL_TREE;
0ba8a114
NS
904 tree stack = saved_function_name_decls;
905
906 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
325c3691 907 append_to_statement_list (TREE_VALUE (stack), &stmts);
35b1a6fa 908
325c3691 909 if (stmts)
0ba8a114 910 {
325c3691 911 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
bfaba7a9 912
325c3691
RH
913 if (TREE_CODE (*bodyp) == BIND_EXPR)
914 bodyp = &BIND_EXPR_BODY (*bodyp);
6cce57b0 915
86ad3aa9 916 append_to_statement_list_force (*bodyp, &stmts);
325c3691 917 *bodyp = stmts;
0ba8a114 918 }
35b1a6fa 919
0ba8a114
NS
920 for (ix = 0; fname_vars[ix].decl; ix++)
921 *fname_vars[ix].decl = NULL_TREE;
35b1a6fa 922
0ba8a114 923 if (stack)
7da551a2 924 {
ec5c56db 925 /* We had saved values, restore them. */
0ba8a114
NS
926 tree saved;
927
928 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
929 {
930 tree decl = TREE_PURPOSE (saved);
931 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
35b1a6fa 932
0ba8a114
NS
933 *fname_vars[ix].decl = decl;
934 }
935 stack = TREE_CHAIN (stack);
7da551a2 936 }
0ba8a114
NS
937 saved_function_name_decls = stack;
938}
939
6cce57b0 940/* Return the text name of the current function, suitably prettified
0d0bc036 941 by PRETTY_P. Return string must be freed by caller. */
0ba8a114
NS
942
943const char *
35b1a6fa 944fname_as_string (int pretty_p)
0ba8a114 945{
47ab33b2 946 const char *name = "top level";
0d0bc036 947 char *namep;
46c2514e
TT
948 int vrb = 2, len;
949 cpp_string cstr = { 0, 0 }, strname;
47ab33b2 950
3f75a254 951 if (!pretty_p)
47ab33b2
MA
952 {
953 name = "";
954 vrb = 0;
955 }
956
957 if (current_function_decl)
ae2bcd98 958 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
47ab33b2 959
46c2514e 960 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
0d0bc036 961
46c2514e
TT
962 namep = XNEWVEC (char, len);
963 snprintf (namep, len, "\"%s\"", name);
964 strname.text = (unsigned char *) namep;
965 strname.len = len - 1;
0d0bc036 966
b6baa67d 967 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
46c2514e
TT
968 {
969 XDELETEVEC (namep);
970 return (const char *) cstr.text;
0d0bc036 971 }
0d0bc036
AH
972
973 return namep;
0ba8a114
NS
974}
975
0ba8a114
NS
976/* Return the VAR_DECL for a const char array naming the current
977 function. If the VAR_DECL has not yet been created, create it
978 now. RID indicates how it should be formatted and IDENTIFIER_NODE
979 ID is its name (unfortunately C and C++ hold the RID values of
980 keywords in different places, so we can't derive RID from ID in
3ba09659
AH
981 this language independent code. LOC is the location of the
982 function. */
0ba8a114
NS
983
984tree
3ba09659 985fname_decl (location_t loc, unsigned int rid, tree id)
0ba8a114
NS
986{
987 unsigned ix;
988 tree decl = NULL_TREE;
989
990 for (ix = 0; fname_vars[ix].decl; ix++)
991 if (fname_vars[ix].rid == rid)
992 break;
993
994 decl = *fname_vars[ix].decl;
995 if (!decl)
7da551a2 996 {
8d3e27d1
DJ
997 /* If a tree is built here, it would normally have the lineno of
998 the current statement. Later this tree will be moved to the
999 beginning of the function and this line number will be wrong.
1000 To avoid this problem set the lineno to 0 here; that prevents
4b7e68e7 1001 it from appearing in the RTL. */
325c3691 1002 tree stmts;
3c20847b 1003 location_t saved_location = input_location;
3c20847b 1004 input_location = UNKNOWN_LOCATION;
35b1a6fa 1005
325c3691 1006 stmts = push_stmt_list ();
c2255bc4 1007 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
325c3691
RH
1008 stmts = pop_stmt_list (stmts);
1009 if (!IS_EMPTY_STMT (stmts))
1010 saved_function_name_decls
1011 = tree_cons (decl, stmts, saved_function_name_decls);
0ba8a114 1012 *fname_vars[ix].decl = decl;
3c20847b 1013 input_location = saved_location;
7da551a2 1014 }
0ba8a114 1015 if (!ix && !current_function_decl)
3ba09659 1016 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
6cce57b0 1017
0ba8a114 1018 return decl;
7da551a2
RS
1019}
1020
b84a3874 1021/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b30f223b
RS
1022
1023tree
35b1a6fa 1024fix_string_type (tree value)
b30f223b 1025{
b84a3874
RH
1026 int length = TREE_STRING_LENGTH (value);
1027 int nchars;
c162c75e
MA
1028 tree e_type, i_type, a_type;
1029
b57062ca 1030 /* Compute the number of elements, for the array type. */
b6baa67d
KVH
1031 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1032 {
1033 nchars = length;
1034 e_type = char_type_node;
1035 }
1036 else if (TREE_TYPE (value) == char16_array_type_node)
1037 {
1038 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1039 e_type = char16_type_node;
1040 }
1041 else if (TREE_TYPE (value) == char32_array_type_node)
1042 {
1043 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1044 e_type = char32_type_node;
1045 }
1046 else
1047 {
1048 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1049 e_type = wchar_type_node;
1050 }
b30f223b 1051
89a42ac8
ZW
1052 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1053 limit in C++98 Annex B is very large (65536) and is not normative,
1054 so we do not diagnose it (warn_overlength_strings is forced off
1055 in c_common_post_options). */
1056 if (warn_overlength_strings)
1057 {
1058 const int nchars_max = flag_isoc99 ? 4095 : 509;
1059 const int relevant_std = flag_isoc99 ? 99 : 90;
1060 if (nchars - 1 > nchars_max)
1061 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1062 separate the %d from the 'C'. 'ISO' should not be
1063 translated, but it may be moved after 'C%d' in languages
1064 where modifiers follow nouns. */
509c9d60 1065 pedwarn (input_location, OPT_Woverlength_strings,
fcf73884 1066 "string length %qd is greater than the length %qd "
89a42ac8
ZW
1067 "ISO C%d compilers are required to support",
1068 nchars - 1, nchars_max, relevant_std);
1069 }
1326a48b 1070
cfb10bd3
GDR
1071 /* Create the array type for the string constant. The ISO C++
1072 standard says that a string literal has type `const char[N]' or
1073 `const wchar_t[N]'. We use the same logic when invoked as a C
1074 front-end with -Wwrite-strings.
1075 ??? We should change the type of an expression depending on the
1076 state of a warning flag. We should just be warning -- see how
1077 this is handled in the C++ front-end for the deprecated implicit
1078 conversion from string literals to `char*' or `wchar_t*'.
c162c75e
MA
1079
1080 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1081 array type being the unqualified version of that type.
1082 Therefore, if we are constructing an array of const char, we must
1083 construct the matching unqualified array type first. The C front
1084 end does not require this, but it does no harm, so we do it
1085 unconditionally. */
c62c040f 1086 i_type = build_index_type (size_int (nchars - 1));
c162c75e 1087 a_type = build_array_type (e_type, i_type);
cfb10bd3 1088 if (c_dialect_cxx() || warn_write_strings)
46df2823 1089 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
d9cf7c82 1090
c162c75e 1091 TREE_TYPE (value) = a_type;
ccd4c832 1092 TREE_CONSTANT (value) = 1;
3521b33c 1093 TREE_READONLY (value) = 1;
b30f223b
RS
1094 TREE_STATIC (value) = 1;
1095 return value;
1096}
1097\f
f479b43d
JM
1098/* Fold X for consideration by one of the warning functions when checking
1099 whether an expression has a constant value. */
1100
1101static tree
1102fold_for_warn (tree x)
1103{
1104 if (c_dialect_cxx ())
1105 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1106 else
1107 /* The C front-end has already folded X appropriately. */
1108 return x;
1109}
1110
d74154d5
RS
1111/* Print a warning if a constant expression had overflow in folding.
1112 Invoke this function on every expression that the language
1113 requires to be a constant expression.
1114 Note the ANSI C standard says it is erroneous for a
1115 constant expression to overflow. */
96571883
BK
1116
1117void
35b1a6fa 1118constant_expression_warning (tree value)
393eda6a 1119{
b8698a0f 1120 if (warn_overflow && pedantic
393eda6a
MLI
1121 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1122 || TREE_CODE (value) == FIXED_CST
1123 || TREE_CODE (value) == VECTOR_CST
1124 || TREE_CODE (value) == COMPLEX_CST)
1125 && TREE_OVERFLOW (value))
509c9d60 1126 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
393eda6a
MLI
1127}
1128
1129/* The same as above but print an unconditional error. */
1130void
1131constant_expression_error (tree value)
96571883 1132{
c05f751c 1133 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
ab22c1fa 1134 || TREE_CODE (value) == FIXED_CST
69ef87e2 1135 || TREE_CODE (value) == VECTOR_CST
c05f751c 1136 || TREE_CODE (value) == COMPLEX_CST)
393eda6a
MLI
1137 && TREE_OVERFLOW (value))
1138 error ("overflow in constant expression");
d74154d5
RS
1139}
1140
59c0753d
MLI
1141/* Print a warning if an expression had overflow in folding and its
1142 operands hadn't.
1143
d74154d5
RS
1144 Invoke this function on every expression that
1145 (1) appears in the source code, and
59c0753d 1146 (2) is a constant expression that overflowed, and
d74154d5 1147 (3) is not already checked by convert_and_check;
59c0753d
MLI
1148 however, do not invoke this function on operands of explicit casts
1149 or when the expression is the result of an operator and any operand
1150 already overflowed. */
d74154d5
RS
1151
1152void
c2255bc4 1153overflow_warning (location_t loc, tree value)
d74154d5 1154{
7d882b83
ILT
1155 if (c_inhibit_evaluation_warnings != 0)
1156 return;
59c0753d
MLI
1157
1158 switch (TREE_CODE (value))
69ef87e2 1159 {
59c0753d 1160 case INTEGER_CST:
c2255bc4 1161 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
59c0753d 1162 break;
b8698a0f 1163
59c0753d 1164 case REAL_CST:
c2255bc4
AH
1165 warning_at (loc, OPT_Woverflow,
1166 "floating point overflow in expression");
59c0753d 1167 break;
b8698a0f 1168
ab22c1fa 1169 case FIXED_CST:
c2255bc4 1170 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
ab22c1fa
CF
1171 break;
1172
59c0753d 1173 case VECTOR_CST:
c2255bc4 1174 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
59c0753d 1175 break;
b8698a0f 1176
59c0753d
MLI
1177 case COMPLEX_CST:
1178 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
c2255bc4
AH
1179 warning_at (loc, OPT_Woverflow,
1180 "complex integer overflow in expression");
59c0753d 1181 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
c2255bc4
AH
1182 warning_at (loc, OPT_Woverflow,
1183 "complex floating point overflow in expression");
59c0753d
MLI
1184 break;
1185
1186 default:
1187 break;
69ef87e2 1188 }
d74154d5
RS
1189}
1190
ca409efd
MLI
1191/* Warn about uses of logical || / && operator in a context where it
1192 is likely that the bitwise equivalent was intended by the
1193 programmer. We have seen an expression in which CODE is a binary
a243fb4a
MLI
1194 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1195 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
63a08740 1196void
a243fb4a 1197warn_logical_operator (location_t location, enum tree_code code, tree type,
b8698a0f 1198 enum tree_code code_left, tree op_left,
ca409efd 1199 enum tree_code ARG_UNUSED (code_right), tree op_right)
63a08740 1200{
a243fb4a
MLI
1201 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1202 int in0_p, in1_p, in_p;
1203 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1204 bool strict_overflow_p = false;
1205
ca409efd
MLI
1206 if (code != TRUTH_ANDIF_EXPR
1207 && code != TRUTH_AND_EXPR
1208 && code != TRUTH_ORIF_EXPR
1209 && code != TRUTH_OR_EXPR)
1210 return;
1211
b8787813
MP
1212 /* We don't want to warn if either operand comes from a macro
1213 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1214 see PR61534. */
1215 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1216 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1217 return;
1218
ca409efd
MLI
1219 /* Warn if &&/|| are being used in a context where it is
1220 likely that the bitwise equivalent was intended by the
1221 programmer. That is, an expression such as op && MASK
1222 where op should not be any boolean expression, nor a
1223 constant, and mask seems to be a non-boolean integer constant. */
f479b43d
JM
1224 if (TREE_CODE (op_right) == CONST_DECL)
1225 /* An enumerator counts as a constant. */
1226 op_right = DECL_INITIAL (op_right);
ca409efd
MLI
1227 if (!truth_value_p (code_left)
1228 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1229 && !CONSTANT_CLASS_P (op_left)
1230 && !TREE_NO_WARNING (op_left)
1231 && TREE_CODE (op_right) == INTEGER_CST
1232 && !integer_zerop (op_right)
1233 && !integer_onep (op_right))
63a08740 1234 {
a243fb4a 1235 if (or_op)
ca409efd
MLI
1236 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1237 " applied to non-boolean constant");
1238 else
1239 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1240 " applied to non-boolean constant");
1241 TREE_NO_WARNING (op_left) = true;
a243fb4a
MLI
1242 return;
1243 }
1244
1245 /* We do not warn for constants because they are typical of macro
1246 expansions that test for features. */
f479b43d
JM
1247 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1248 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
a243fb4a
MLI
1249 return;
1250
1251 /* This warning only makes sense with logical operands. */
1252 if (!(truth_value_p (TREE_CODE (op_left))
1253 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1254 || !(truth_value_p (TREE_CODE (op_right))
1255 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1256 return;
1257
3c9aabbd
MG
1258 /* The range computations only work with scalars. */
1259 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1260 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1261 return;
a243fb4a 1262
f2c4a785
MLI
1263 /* We first test whether either side separately is trivially true
1264 (with OR) or trivially false (with AND). If so, do not warn.
1265 This is a common idiom for testing ranges of data types in
1266 portable code. */
1267 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1268 if (!lhs)
1269 return;
1270 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
a243fb4a
MLI
1271 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1272
f2c4a785
MLI
1273 /* If this is an OR operation, invert both sides; now, the result
1274 should be always false to get a warning. */
1275 if (or_op)
1276 in0_p = !in0_p;
1277
1278 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1e537948 1279 if (tem && integer_zerop (tem))
f2c4a785
MLI
1280 return;
1281
1282 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1283 if (!rhs)
1284 return;
1285 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
a243fb4a 1286 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
b8698a0f 1287
f2c4a785
MLI
1288 /* If this is an OR operation, invert both sides; now, the result
1289 should be always false to get a warning. */
a243fb4a 1290 if (or_op)
f2c4a785 1291 in1_p = !in1_p;
b8698a0f 1292
f2c4a785 1293 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1e537948 1294 if (tem && integer_zerop (tem))
f2c4a785
MLI
1295 return;
1296
1297 /* If both expressions have the same operand, if we can merge the
8c2b7f79 1298 ranges, ... */
f2c4a785 1299 if (operand_equal_p (lhs, rhs, 0)
a243fb4a 1300 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
8c2b7f79 1301 in1_p, low1, high1))
a243fb4a 1302 {
8c2b7f79
MP
1303 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1304 /* ... and if the range test is always false, then warn. */
1305 if (tem && integer_zerop (tem))
1306 {
1307 if (or_op)
1308 warning_at (location, OPT_Wlogical_op,
1309 "logical %<or%> of collectively exhaustive tests is "
1310 "always true");
1311 else
1312 warning_at (location, OPT_Wlogical_op,
1313 "logical %<and%> of mutually exclusive tests is "
1314 "always false");
1315 }
1316 /* Or warn if the operands have exactly the same range, e.g.
1317 A > 0 && A > 0. */
a68ae2e1
MP
1318 else if (tree_int_cst_equal (low0, low1)
1319 && tree_int_cst_equal (high0, high1))
8c2b7f79
MP
1320 {
1321 if (or_op)
1322 warning_at (location, OPT_Wlogical_op,
1323 "logical %<or%> of equal expressions");
1324 else
1325 warning_at (location, OPT_Wlogical_op,
1326 "logical %<and%> of equal expressions");
1327 }
63a08740
DM
1328 }
1329}
1330
05b28fd6
MP
1331/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1332 with constant indices. */
1333
1334static tree
1335find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1336{
1337 tree expr = *expr_p;
1338
1339 if ((TREE_CODE (expr) == ARRAY_REF
1340 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1341 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1342 {
1343 *(bool *) data = true;
1344 *walk_subtrees = 0;
1345 }
1346
1347 return NULL_TREE;
1348}
1349
1350/* Warn if a self-comparison always evaluates to true or false. LOC
1351 is the location of the comparison with code CODE, LHS and RHS are
1352 operands of the comparison. */
1353
1354void
1355warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1356{
1357 if (TREE_CODE_CLASS (code) != tcc_comparison)
1358 return;
1359
f2afe6dd
MP
1360 /* Don't warn for various macro expansions. */
1361 if (from_macro_expansion_at (loc)
1362 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1363 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1364 return;
1365
05b28fd6
MP
1366 /* We do not warn for constants because they are typical of macro
1367 expansions that test for features, sizeof, and similar. */
f479b43d
JM
1368 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1369 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
05b28fd6
MP
1370 return;
1371
1372 /* Don't warn for e.g.
1373 HOST_WIDE_INT n;
1374 ...
1375 if (n == (long) n) ...
1376 */
1377 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1378 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1379 return;
1380
173864e8
MP
1381 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1382 It could be a NaN, and NaN never compares equal to anything, even
1383 itself. */
1384 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1385 return;
1386
05b28fd6
MP
1387 if (operand_equal_p (lhs, rhs, 0))
1388 {
1389 /* Don't warn about array references with constant indices;
1390 these are likely to come from a macro. */
1391 bool found = false;
1392 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1393 &found);
1394 if (found)
1395 return;
1396 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1397 || code == GE_EXPR || code == UNLE_EXPR
1398 || code == UNGE_EXPR || code == UNEQ_EXPR);
1399 if (always_true)
1400 warning_at (loc, OPT_Wtautological_compare,
1401 "self-comparison always evaluates to true");
1402 else
1403 warning_at (loc, OPT_Wtautological_compare,
1404 "self-comparison always evaluates to false");
1405 }
1406}
1407
742938c9
MP
1408/* Warn about logical not used on the left hand side operand of a comparison.
1409 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
59ea0364 1410 Do not warn if RHS is of a boolean type. */
742938c9
MP
1411
1412void
1413warn_logical_not_parentheses (location_t location, enum tree_code code,
59ea0364 1414 tree rhs)
742938c9 1415{
59ea0364
MP
1416 if (TREE_CODE_CLASS (code) != tcc_comparison
1417 || TREE_TYPE (rhs) == NULL_TREE
1418 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
742938c9
MP
1419 return;
1420
7ccb1a11
JJ
1421 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1422 !(x == 0) or !(y != 0). */
1423 if ((code == EQ_EXPR || code == NE_EXPR)
1424 && integer_zerop (rhs))
1425 return;
1426
742938c9
MP
1427 warning_at (location, OPT_Wlogical_not_parentheses,
1428 "logical not is only applied to the left hand side of "
1429 "comparison");
1430}
63a08740 1431
fd4116f4
MLI
1432/* Warn if EXP contains any computations whose results are not used.
1433 Return true if a warning is printed; false otherwise. LOCUS is the
1434 (potential) location of the expression. */
1435
1436bool
1437warn_if_unused_value (const_tree exp, location_t locus)
1438{
1439 restart:
1440 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1441 return false;
1442
1443 /* Don't warn about void constructs. This includes casting to void,
1444 void function calls, and statement expressions with a final cast
1445 to void. */
1446 if (VOID_TYPE_P (TREE_TYPE (exp)))
1447 return false;
1448
1449 if (EXPR_HAS_LOCATION (exp))
1450 locus = EXPR_LOCATION (exp);
1451
1452 switch (TREE_CODE (exp))
1453 {
1454 case PREINCREMENT_EXPR:
1455 case POSTINCREMENT_EXPR:
1456 case PREDECREMENT_EXPR:
1457 case POSTDECREMENT_EXPR:
1458 case MODIFY_EXPR:
1459 case INIT_EXPR:
1460 case TARGET_EXPR:
1461 case CALL_EXPR:
1462 case TRY_CATCH_EXPR:
1463 case WITH_CLEANUP_EXPR:
1464 case EXIT_EXPR:
1465 case VA_ARG_EXPR:
1466 return false;
1467
1468 case BIND_EXPR:
1469 /* For a binding, warn if no side effect within it. */
1470 exp = BIND_EXPR_BODY (exp);
1471 goto restart;
1472
1473 case SAVE_EXPR:
1474 case NON_LVALUE_EXPR:
007a787d 1475 case NOP_EXPR:
fd4116f4
MLI
1476 exp = TREE_OPERAND (exp, 0);
1477 goto restart;
1478
1479 case TRUTH_ORIF_EXPR:
1480 case TRUTH_ANDIF_EXPR:
1481 /* In && or ||, warn if 2nd operand has no side effect. */
1482 exp = TREE_OPERAND (exp, 1);
1483 goto restart;
1484
1485 case COMPOUND_EXPR:
1486 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1487 return true;
1488 /* Let people do `(foo (), 0)' without a warning. */
1489 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1490 return false;
1491 exp = TREE_OPERAND (exp, 1);
1492 goto restart;
1493
1494 case COND_EXPR:
1495 /* If this is an expression with side effects, don't warn; this
1496 case commonly appears in macro expansions. */
1497 if (TREE_SIDE_EFFECTS (exp))
1498 return false;
1499 goto warn;
1500
1501 case INDIRECT_REF:
1502 /* Don't warn about automatic dereferencing of references, since
1503 the user cannot control it. */
1504 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1505 {
1506 exp = TREE_OPERAND (exp, 0);
1507 goto restart;
1508 }
1509 /* Fall through. */
1510
1511 default:
1512 /* Referencing a volatile value is a side effect, so don't warn. */
1513 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1514 && TREE_THIS_VOLATILE (exp))
1515 return false;
1516
1517 /* If this is an expression which has no operands, there is no value
1518 to be unused. There are no such language-independent codes,
1519 but front ends may define such. */
1520 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1521 return false;
1522
1523 warn:
1524 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1525 }
1526}
1527
1528
de9c56a4
RG
1529/* Print a warning about casts that might indicate violation
1530 of strict aliasing rules if -Wstrict-aliasing is used and
3f0a2a47
DM
1531 strict aliasing mode is in effect. OTYPE is the original
1532 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
de9c56a4 1533
79bedddc 1534bool
3f0a2a47 1535strict_aliasing_warning (tree otype, tree type, tree expr)
de9c56a4 1536{
255d3827
RG
1537 /* Strip pointer conversion chains and get to the correct original type. */
1538 STRIP_NOPS (expr);
1539 otype = TREE_TYPE (expr);
1540
ac7ee6ad
RG
1541 if (!(flag_strict_aliasing
1542 && POINTER_TYPE_P (type)
1543 && POINTER_TYPE_P (otype)
1544 && !VOID_TYPE_P (TREE_TYPE (type)))
1545 /* If the type we are casting to is a ref-all pointer
1546 dereferencing it is always valid. */
1547 || TYPE_REF_CAN_ALIAS_ALL (type))
79bedddc
SR
1548 return false;
1549
1550 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
de9c56a4 1551 && (DECL_P (TREE_OPERAND (expr, 0))
79bedddc 1552 || handled_component_p (TREE_OPERAND (expr, 0))))
de9c56a4
RG
1553 {
1554 /* Casting the address of an object to non void pointer. Warn
1555 if the cast breaks type based aliasing. */
79bedddc
SR
1556 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1557 {
1558 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1559 "might break strict-aliasing rules");
1560 return true;
1561 }
de9c56a4
RG
1562 else
1563 {
b8698a0f 1564 /* warn_strict_aliasing >= 3. This includes the default (3).
79bedddc 1565 Only warn if the cast is dereferenced immediately. */
4862826d 1566 alias_set_type set1 =
79bedddc 1567 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
4862826d 1568 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
de9c56a4 1569
4653cae5 1570 if (set1 != set2 && set2 != 0
e9a35493 1571 && (set1 == 0 || !alias_set_subset_of (set2, set1)))
79bedddc
SR
1572 {
1573 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1574 "pointer will break strict-aliasing rules");
1575 return true;
1576 }
1577 else if (warn_strict_aliasing == 2
836f7794 1578 && !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1579 {
1580 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1581 "pointer might break strict-aliasing rules");
1582 return true;
1583 }
de9c56a4
RG
1584 }
1585 }
79bedddc
SR
1586 else
1587 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1588 {
1589 /* At this level, warn for any conversions, even if an address is
1590 not taken in the same statement. This will likely produce many
1591 false positives, but could be useful to pinpoint problems that
1592 are not revealed at higher levels. */
4862826d
ILT
1593 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1594 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1595 if (!COMPLETE_TYPE_P (type)
836f7794 1596 || !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1597 {
1598 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1599 "pointer might break strict-aliasing rules");
1600 return true;
1601 }
1602 }
1603
1604 return false;
de9c56a4
RG
1605}
1606
1a4049e7
JJ
1607/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1608 sizeof as last operand of certain builtins. */
1609
1610void
3a785c97 1611sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
9771b263 1612 vec<tree, va_gc> *params, tree *sizeof_arg,
1a4049e7
JJ
1613 bool (*comp_types) (tree, tree))
1614{
1615 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
3a785c97
JJ
1616 bool strop = false, cmp = false;
1617 unsigned int idx = ~0;
1618 location_t loc;
1a4049e7
JJ
1619
1620 if (TREE_CODE (callee) != FUNCTION_DECL
1621 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
9771b263 1622 || vec_safe_length (params) <= 1)
1a4049e7
JJ
1623 return;
1624
1a4049e7
JJ
1625 switch (DECL_FUNCTION_CODE (callee))
1626 {
1627 case BUILT_IN_STRNCMP:
1628 case BUILT_IN_STRNCASECMP:
3a785c97
JJ
1629 cmp = true;
1630 /* FALLTHRU */
1a4049e7 1631 case BUILT_IN_STRNCPY:
3a785c97 1632 case BUILT_IN_STRNCPY_CHK:
1a4049e7 1633 case BUILT_IN_STRNCAT:
3a785c97
JJ
1634 case BUILT_IN_STRNCAT_CHK:
1635 case BUILT_IN_STPNCPY:
1636 case BUILT_IN_STPNCPY_CHK:
1a4049e7
JJ
1637 strop = true;
1638 /* FALLTHRU */
1639 case BUILT_IN_MEMCPY:
3a785c97 1640 case BUILT_IN_MEMCPY_CHK:
1a4049e7 1641 case BUILT_IN_MEMMOVE:
3a785c97 1642 case BUILT_IN_MEMMOVE_CHK:
9771b263 1643 if (params->length () < 3)
3a785c97 1644 return;
9771b263
DN
1645 src = (*params)[1];
1646 dest = (*params)[0];
3a785c97
JJ
1647 idx = 2;
1648 break;
1649 case BUILT_IN_BCOPY:
9771b263 1650 if (params->length () < 3)
3a785c97 1651 return;
9771b263
DN
1652 src = (*params)[0];
1653 dest = (*params)[1];
3a785c97
JJ
1654 idx = 2;
1655 break;
1a4049e7 1656 case BUILT_IN_MEMCMP:
3a785c97 1657 case BUILT_IN_BCMP:
9771b263 1658 if (params->length () < 3)
1a4049e7 1659 return;
9771b263
DN
1660 src = (*params)[1];
1661 dest = (*params)[0];
3a785c97
JJ
1662 idx = 2;
1663 cmp = true;
1a4049e7
JJ
1664 break;
1665 case BUILT_IN_MEMSET:
3a785c97 1666 case BUILT_IN_MEMSET_CHK:
9771b263 1667 if (params->length () < 3)
1a4049e7 1668 return;
9771b263 1669 dest = (*params)[0];
3a785c97
JJ
1670 idx = 2;
1671 break;
1672 case BUILT_IN_BZERO:
9771b263 1673 dest = (*params)[0];
3a785c97 1674 idx = 1;
1a4049e7
JJ
1675 break;
1676 case BUILT_IN_STRNDUP:
9771b263 1677 src = (*params)[0];
1a4049e7 1678 strop = true;
3a785c97
JJ
1679 idx = 1;
1680 break;
1681 case BUILT_IN_MEMCHR:
9771b263 1682 if (params->length () < 3)
3a785c97 1683 return;
9771b263 1684 src = (*params)[0];
3a785c97
JJ
1685 idx = 2;
1686 break;
1687 case BUILT_IN_SNPRINTF:
1688 case BUILT_IN_SNPRINTF_CHK:
1689 case BUILT_IN_VSNPRINTF:
1690 case BUILT_IN_VSNPRINTF_CHK:
9771b263 1691 dest = (*params)[0];
3a785c97
JJ
1692 idx = 1;
1693 strop = true;
1a4049e7
JJ
1694 break;
1695 default:
1696 break;
1697 }
1698
3a785c97
JJ
1699 if (idx >= 3)
1700 return;
1701
1702 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1703 return;
1704
1705 type = TYPE_P (sizeof_arg[idx])
1706 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1707 if (!POINTER_TYPE_P (type))
1708 return;
1709
1a4049e7
JJ
1710 if (dest
1711 && (tem = tree_strip_nop_conversions (dest))
1712 && POINTER_TYPE_P (TREE_TYPE (tem))
1713 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1714 return;
1715
1716 if (src
1717 && (tem = tree_strip_nop_conversions (src))
1718 && POINTER_TYPE_P (TREE_TYPE (tem))
1719 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1720 return;
1721
3a785c97
JJ
1722 loc = sizeof_arg_loc[idx];
1723
1724 if (dest && !cmp)
1a4049e7 1725 {
3a785c97
JJ
1726 if (!TYPE_P (sizeof_arg[idx])
1727 && operand_equal_p (dest, sizeof_arg[idx], 0)
1a4049e7
JJ
1728 && comp_types (TREE_TYPE (dest), type))
1729 {
3a785c97 1730 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
1731 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1732 "argument to %<sizeof%> in %qD call is the same "
1733 "expression as the destination; did you mean to "
1734 "remove the addressof?", callee);
1735 else if ((TYPE_PRECISION (TREE_TYPE (type))
1736 == TYPE_PRECISION (char_type_node))
1737 || strop)
1738 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1739 "argument to %<sizeof%> in %qD call is the same "
1740 "expression as the destination; did you mean to "
1741 "provide an explicit length?", callee);
1742 else
1743 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1744 "argument to %<sizeof%> in %qD call is the same "
1745 "expression as the destination; did you mean to "
1746 "dereference it?", callee);
1747 return;
1748 }
1749
1750 if (POINTER_TYPE_P (TREE_TYPE (dest))
1751 && !strop
1752 && comp_types (TREE_TYPE (dest), type)
1753 && !VOID_TYPE_P (TREE_TYPE (type)))
1754 {
1755 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1756 "argument to %<sizeof%> in %qD call is the same "
1757 "pointer type %qT as the destination; expected %qT "
1758 "or an explicit length", callee, TREE_TYPE (dest),
1759 TREE_TYPE (TREE_TYPE (dest)));
1760 return;
1761 }
1762 }
1763
3a785c97 1764 if (src && !cmp)
1a4049e7 1765 {
3a785c97
JJ
1766 if (!TYPE_P (sizeof_arg[idx])
1767 && operand_equal_p (src, sizeof_arg[idx], 0)
1a4049e7
JJ
1768 && comp_types (TREE_TYPE (src), type))
1769 {
3a785c97 1770 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
1771 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1772 "argument to %<sizeof%> in %qD call is the same "
1773 "expression as the source; did you mean to "
1774 "remove the addressof?", callee);
1775 else if ((TYPE_PRECISION (TREE_TYPE (type))
1776 == TYPE_PRECISION (char_type_node))
1777 || strop)
1778 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1779 "argument to %<sizeof%> in %qD call is the same "
1780 "expression as the source; did you mean to "
1781 "provide an explicit length?", callee);
1782 else
1783 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1784 "argument to %<sizeof%> in %qD call is the same "
1785 "expression as the source; did you mean to "
1786 "dereference it?", callee);
1787 return;
1788 }
1789
1790 if (POINTER_TYPE_P (TREE_TYPE (src))
1791 && !strop
1792 && comp_types (TREE_TYPE (src), type)
1793 && !VOID_TYPE_P (TREE_TYPE (type)))
1794 {
1795 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1796 "argument to %<sizeof%> in %qD call is the same "
1797 "pointer type %qT as the source; expected %qT "
1798 "or an explicit length", callee, TREE_TYPE (src),
1799 TREE_TYPE (TREE_TYPE (src)));
1800 return;
1801 }
1802 }
3a785c97
JJ
1803
1804 if (dest)
1805 {
1806 if (!TYPE_P (sizeof_arg[idx])
1807 && operand_equal_p (dest, sizeof_arg[idx], 0)
1808 && comp_types (TREE_TYPE (dest), type))
1809 {
1810 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1811 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1812 "argument to %<sizeof%> in %qD call is the same "
1813 "expression as the first source; did you mean to "
1814 "remove the addressof?", callee);
1815 else if ((TYPE_PRECISION (TREE_TYPE (type))
1816 == TYPE_PRECISION (char_type_node))
1817 || strop)
1818 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1819 "argument to %<sizeof%> in %qD call is the same "
1820 "expression as the first source; did you mean to "
1821 "provide an explicit length?", callee);
1822 else
1823 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1824 "argument to %<sizeof%> in %qD call is the same "
1825 "expression as the first source; did you mean to "
1826 "dereference it?", callee);
1827 return;
1828 }
1829
1830 if (POINTER_TYPE_P (TREE_TYPE (dest))
1831 && !strop
1832 && comp_types (TREE_TYPE (dest), type)
1833 && !VOID_TYPE_P (TREE_TYPE (type)))
1834 {
1835 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1836 "argument to %<sizeof%> in %qD call is the same "
1837 "pointer type %qT as the first source; expected %qT "
1838 "or an explicit length", callee, TREE_TYPE (dest),
1839 TREE_TYPE (TREE_TYPE (dest)));
1840 return;
1841 }
1842 }
1843
1844 if (src)
1845 {
1846 if (!TYPE_P (sizeof_arg[idx])
1847 && operand_equal_p (src, sizeof_arg[idx], 0)
1848 && comp_types (TREE_TYPE (src), type))
1849 {
1850 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1851 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1852 "argument to %<sizeof%> in %qD call is the same "
1853 "expression as the second source; did you mean to "
1854 "remove the addressof?", callee);
1855 else if ((TYPE_PRECISION (TREE_TYPE (type))
1856 == TYPE_PRECISION (char_type_node))
1857 || strop)
1858 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1859 "argument to %<sizeof%> in %qD call is the same "
1860 "expression as the second source; did you mean to "
1861 "provide an explicit length?", callee);
1862 else
1863 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1864 "argument to %<sizeof%> in %qD call is the same "
1865 "expression as the second source; did you mean to "
1866 "dereference it?", callee);
1867 return;
1868 }
1869
1870 if (POINTER_TYPE_P (TREE_TYPE (src))
1871 && !strop
1872 && comp_types (TREE_TYPE (src), type)
1873 && !VOID_TYPE_P (TREE_TYPE (type)))
1874 {
1875 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1876 "argument to %<sizeof%> in %qD call is the same "
1877 "pointer type %qT as the second source; expected %qT "
1878 "or an explicit length", callee, TREE_TYPE (src),
1879 TREE_TYPE (TREE_TYPE (src)));
1880 return;
1881 }
1882 }
1883
1a4049e7
JJ
1884}
1885
a1e45ff0
DM
1886/* Warn for unlikely, improbable, or stupid DECL declarations
1887 of `main'. */
1888
1889void
1890check_main_parameter_types (tree decl)
1891{
e19a18d4
NF
1892 function_args_iterator iter;
1893 tree type;
a1e45ff0
DM
1894 int argct = 0;
1895
e19a18d4
NF
1896 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1897 {
1898 /* XXX void_type_node belies the abstraction. */
1899 if (type == void_type_node || type == error_mark_node )
1900 break;
1901
f827930a
MP
1902 tree t = type;
1903 if (TYPE_ATOMIC (t))
1904 pedwarn (input_location, OPT_Wmain,
1905 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1906 type, decl);
1907 while (POINTER_TYPE_P (t))
1908 {
1909 t = TREE_TYPE (t);
1910 if (TYPE_ATOMIC (t))
1911 pedwarn (input_location, OPT_Wmain,
1912 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1913 type, decl);
1914 }
1915
e19a18d4
NF
1916 ++argct;
1917 switch (argct)
1918 {
1919 case 1:
1920 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1921 pedwarn (input_location, OPT_Wmain,
1922 "first argument of %q+D should be %<int%>", decl);
1923 break;
1924
1925 case 2:
1926 if (TREE_CODE (type) != POINTER_TYPE
1927 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1928 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1929 != char_type_node))
1930 pedwarn (input_location, OPT_Wmain,
1931 "second argument of %q+D should be %<char **%>", decl);
1932 break;
1933
1934 case 3:
1935 if (TREE_CODE (type) != POINTER_TYPE
1936 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1937 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1938 != char_type_node))
1939 pedwarn (input_location, OPT_Wmain,
1940 "third argument of %q+D should probably be "
1941 "%<char **%>", decl);
1942 break;
1943 }
1944 }
a1e45ff0
DM
1945
1946 /* It is intentional that this message does not mention the third
1947 argument because it's only mentioned in an appendix of the
1948 standard. */
1949 if (argct > 0 && (argct < 2 || argct > 3))
e19a18d4
NF
1950 pedwarn (input_location, OPT_Wmain,
1951 "%q+D takes only zero or two arguments", decl);
38e514c0
MP
1952
1953 if (stdarg_p (TREE_TYPE (decl)))
1954 pedwarn (input_location, OPT_Wmain,
1955 "%q+D declared as variadic function", decl);
a1e45ff0
DM
1956}
1957
0af94e6f
JR
1958/* vector_targets_convertible_p is used for vector pointer types. The
1959 callers perform various checks that the qualifiers are satisfactory,
1960 while OTOH vector_targets_convertible_p ignores the number of elements
1961 in the vectors. That's fine with vector pointers as we can consider,
1962 say, a vector of 8 elements as two consecutive vectors of 4 elements,
1963 and that does not require and conversion of the pointer values.
1964 In contrast, vector_types_convertible_p and
1965 vector_types_compatible_elements_p are used for vector value types. */
f83c7f63
DJ
1966/* True if pointers to distinct types T1 and T2 can be converted to
1967 each other without an explicit cast. Only returns true for opaque
1968 vector types. */
1969bool
1970vector_targets_convertible_p (const_tree t1, const_tree t2)
1971{
31521951 1972 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
b6fc2cdb 1973 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
f83c7f63
DJ
1974 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1975 return true;
1976
1977 return false;
1978}
1979
0af94e6f
JR
1980/* vector_types_convertible_p is used for vector value types.
1981 It could in principle call vector_targets_convertible_p as a subroutine,
1982 but then the check for vector type would be duplicated with its callers,
1983 and also the purpose of vector_targets_convertible_p would become
1984 muddled.
1985 Where vector_types_convertible_p returns true, a conversion might still be
1986 needed to make the types match.
1987 In contrast, vector_targets_convertible_p is used for vector pointer
1988 values, and vector_types_compatible_elements_p is used specifically
1989 in the context for binary operators, as a check if use is possible without
1990 conversion. */
00c8e9f6
MS
1991/* True if vector types T1 and T2 can be converted to each other
1992 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1993 can only be converted with -flax-vector-conversions yet that is not
1994 in effect, emit a note telling the user about that option if such
1995 a note has not previously been emitted. */
1996bool
58f9752a 1997vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
cc27e657 1998{
00c8e9f6 1999 static bool emitted_lax_note = false;
14e765da
JM
2000 bool convertible_lax;
2001
b6fc2cdb 2002 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
14e765da
JM
2003 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2004 return true;
2005
2006 convertible_lax =
2007 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2008 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
a5e0cd1d 2009 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
14e765da
JM
2010 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2011 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
00c8e9f6
MS
2012
2013 if (!convertible_lax || flag_lax_vector_conversions)
2014 return convertible_lax;
2015
2016 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
cf7bc668 2017 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
00c8e9f6
MS
2018 return true;
2019
2020 if (emit_lax_note && !emitted_lax_note)
2021 {
2022 emitted_lax_note = true;
1f5b3869 2023 inform (input_location, "use -flax-vector-conversions to permit "
00c8e9f6
MS
2024 "conversions between vectors with differing "
2025 "element types or numbers of subparts");
2026 }
2027
2028 return false;
cc27e657
PB
2029}
2030
9e1a8dd1
RR
2031/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2032 and have vector types, V0 has the same type as V1, and the number of
2033 elements of V0, V1, MASK is the same.
2034
2035 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2036 called with two arguments. In this case implementation passes the
2037 first argument twice in order to share the same tree code. This fact
2038 could enable the mask-values being twice the vector length. This is
2039 an implementation accident and this semantics is not guaranteed to
2040 the user. */
2041tree
bedc293e
MG
2042c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2043 bool complain)
9e1a8dd1
RR
2044{
2045 tree ret;
2046 bool wrap = true;
2047 bool maybe_const = false;
2048 bool two_arguments = false;
2049
2050 if (v1 == NULL_TREE)
2051 {
2052 two_arguments = true;
2053 v1 = v0;
2054 }
2055
2056 if (v0 == error_mark_node || v1 == error_mark_node
2057 || mask == error_mark_node)
2058 return error_mark_node;
2059
31521951 2060 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
9e1a8dd1 2061 {
bedc293e
MG
2062 if (complain)
2063 error_at (loc, "__builtin_shuffle last argument must "
2064 "be an integer vector");
9e1a8dd1
RR
2065 return error_mark_node;
2066 }
2067
31521951
MP
2068 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2069 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
9e1a8dd1 2070 {
bedc293e
MG
2071 if (complain)
2072 error_at (loc, "__builtin_shuffle arguments must be vectors");
9e1a8dd1
RR
2073 return error_mark_node;
2074 }
2075
2076 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2077 {
bedc293e
MG
2078 if (complain)
2079 error_at (loc, "__builtin_shuffle argument vectors must be of "
2080 "the same type");
9e1a8dd1
RR
2081 return error_mark_node;
2082 }
2083
2084 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2085 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2086 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2087 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2088 {
bedc293e
MG
2089 if (complain)
2090 error_at (loc, "__builtin_shuffle number of elements of the "
2091 "argument vector(s) and the mask vector should "
2092 "be the same");
9e1a8dd1
RR
2093 return error_mark_node;
2094 }
2095
2096 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2097 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2098 {
bedc293e
MG
2099 if (complain)
2100 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2101 "must have the same size as inner type of the mask");
9e1a8dd1
RR
2102 return error_mark_node;
2103 }
2104
2105 if (!c_dialect_cxx ())
2106 {
2107 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2108 v0 = c_fully_fold (v0, false, &maybe_const);
2109 wrap &= maybe_const;
2110
2111 if (two_arguments)
2112 v1 = v0 = save_expr (v0);
2113 else
2114 {
2115 v1 = c_fully_fold (v1, false, &maybe_const);
2116 wrap &= maybe_const;
2117 }
2118
2119 mask = c_fully_fold (mask, false, &maybe_const);
2120 wrap &= maybe_const;
2121 }
bedc293e
MG
2122 else if (two_arguments)
2123 v1 = v0 = save_expr (v0);
9e1a8dd1
RR
2124
2125 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2126
2127 if (!c_dialect_cxx () && !wrap)
2128 ret = c_wrap_maybe_const (ret, true);
2129
2130 return ret;
2131}
2132
828fb3ba
JM
2133/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2134 to integral type. */
2135
2136static tree
2137c_common_get_narrower (tree op, int *unsignedp_ptr)
2138{
2139 op = get_narrower (op, unsignedp_ptr);
2140
2141 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2142 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2143 {
2144 /* C++0x scoped enumerations don't implicitly convert to integral
2145 type; if we stripped an explicit conversion to a larger type we
2146 need to replace it so common_type will still work. */
21fa2faf
RG
2147 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2148 TYPE_UNSIGNED (TREE_TYPE (op)));
828fb3ba
JM
2149 op = fold_convert (type, op);
2150 }
2151 return op;
2152}
2153
6715192c
MLI
2154/* This is a helper function of build_binary_op.
2155
2156 For certain operations if both args were extended from the same
2157 smaller type, do the arithmetic in that type and then extend.
2158
2159 BITWISE indicates a bitwise operation.
2160 For them, this optimization is safe only if
2161 both args are zero-extended or both are sign-extended.
2162 Otherwise, we might change the result.
2163 Eg, (short)-1 | (unsigned short)-1 is (int)-1
b8698a0f 2164 but calculated in (unsigned short) it would be (unsigned short)-1.
6715192c 2165*/
828fb3ba
JM
2166tree
2167shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
6715192c
MLI
2168{
2169 int unsigned0, unsigned1;
2170 tree arg0, arg1;
2171 int uns;
2172 tree type;
2173
2174 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2175 excessive narrowing when we call get_narrower below. For
2176 example, suppose that OP0 is of unsigned int extended
2177 from signed char and that RESULT_TYPE is long long int.
2178 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2179 like
b8698a0f 2180
6715192c
MLI
2181 (long long int) (unsigned int) signed_char
2182
2183 which get_narrower would narrow down to
b8698a0f 2184
6715192c 2185 (unsigned int) signed char
b8698a0f 2186
6715192c
MLI
2187 If we do not cast OP0 first, get_narrower would return
2188 signed_char, which is inconsistent with the case of the
2189 explicit cast. */
2190 op0 = convert (result_type, op0);
2191 op1 = convert (result_type, op1);
2192
828fb3ba
JM
2193 arg0 = c_common_get_narrower (op0, &unsigned0);
2194 arg1 = c_common_get_narrower (op1, &unsigned1);
6715192c
MLI
2195
2196 /* UNS is 1 if the operation to be done is an unsigned one. */
2197 uns = TYPE_UNSIGNED (result_type);
2198
2199 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2200 but it *requires* conversion to FINAL_TYPE. */
b8698a0f 2201
6715192c
MLI
2202 if ((TYPE_PRECISION (TREE_TYPE (op0))
2203 == TYPE_PRECISION (TREE_TYPE (arg0)))
2204 && TREE_TYPE (op0) != result_type)
2205 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2206 if ((TYPE_PRECISION (TREE_TYPE (op1))
2207 == TYPE_PRECISION (TREE_TYPE (arg1)))
2208 && TREE_TYPE (op1) != result_type)
2209 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b8698a0f 2210
6715192c 2211 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
b8698a0f 2212
6715192c
MLI
2213 /* For bitwise operations, signedness of nominal type
2214 does not matter. Consider only how operands were extended. */
2215 if (bitwise)
2216 uns = unsigned0;
b8698a0f 2217
6715192c
MLI
2218 /* Note that in all three cases below we refrain from optimizing
2219 an unsigned operation on sign-extended args.
2220 That would not be valid. */
b8698a0f 2221
6715192c
MLI
2222 /* Both args variable: if both extended in same way
2223 from same width, do it in that width.
2224 Do it unsigned if args were zero-extended. */
2225 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2226 < TYPE_PRECISION (result_type))
2227 && (TYPE_PRECISION (TREE_TYPE (arg1))
2228 == TYPE_PRECISION (TREE_TYPE (arg0)))
2229 && unsigned0 == unsigned1
2230 && (unsigned0 || !uns))
2231 return c_common_signed_or_unsigned_type
2232 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2233
2234 else if (TREE_CODE (arg0) == INTEGER_CST
2235 && (unsigned1 || !uns)
2236 && (TYPE_PRECISION (TREE_TYPE (arg1))
2237 < TYPE_PRECISION (result_type))
2238 && (type
2239 = c_common_signed_or_unsigned_type (unsigned1,
2240 TREE_TYPE (arg1)))
2241 && !POINTER_TYPE_P (type)
2242 && int_fits_type_p (arg0, type))
2243 return type;
2244
2245 else if (TREE_CODE (arg1) == INTEGER_CST
2246 && (unsigned0 || !uns)
2247 && (TYPE_PRECISION (TREE_TYPE (arg0))
2248 < TYPE_PRECISION (result_type))
2249 && (type
2250 = c_common_signed_or_unsigned_type (unsigned0,
2251 TREE_TYPE (arg0)))
2252 && !POINTER_TYPE_P (type)
2253 && int_fits_type_p (arg1, type))
2254 return type;
2255
2256 return result_type;
2257}
2258
7a37fa90
MM
2259/* Returns true iff any integer value of type FROM_TYPE can be represented as
2260 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2261
2262static bool
2263int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2264{
2265 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2266 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2267 REAL_VALUE_TYPE real_low_bound =
2268 real_value_from_int_cst (0, type_low_bound);
2269 REAL_VALUE_TYPE real_high_bound =
2270 real_value_from_int_cst (0, type_high_bound);
2271
2272 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2273 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2274}
2275
2276/* Checks if expression EXPR of complex/real/integer type cannot be converted
2277 to the complex/real/integer type TYPE. Function returns non-zero when:
68fca595
MP
2278 * EXPR is a constant which cannot be exactly converted to TYPE.
2279 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
7a37fa90
MM
2280 for EXPR type and TYPE being both integers or both real, or both
2281 complex.
2282 * EXPR is not a constant of complex type and TYPE is a real or
2283 an integer.
68fca595
MP
2284 * EXPR is not a constant of real type and TYPE is an integer.
2285 * EXPR is not a constant of integer type which cannot be
2286 exactly converted to real type.
7a37fa90 2287
0e3a99ae 2288 Function allows conversions between types of different signedness and
49b0aa18 2289 can return SAFE_CONVERSION (zero) in that case. Function can produce
7a37fa90
MM
2290 signedness warnings if PRODUCE_WARNS is true.
2291
2292 Function allows conversions from complex constants to non-complex types,
2293 provided that imaginary part is zero and real part can be safely converted
2294 to TYPE. */
68fca595 2295
49b0aa18 2296enum conversion_safety
68fca595 2297unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
422c3a54 2298{
49b0aa18 2299 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
374035cb 2300 tree expr_type = TREE_TYPE (expr);
68fca595 2301 loc = expansion_point_location_if_in_system_header (loc);
422c3a54 2302
0e3a99ae 2303 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
0011dedb 2304 {
7a37fa90
MM
2305 /* If type is complex, we are interested in compatibility with
2306 underlying type. */
2307 if (TREE_CODE (type) == COMPLEX_TYPE)
2308 type = TREE_TYPE (type);
2309
422c3a54 2310 /* Warn for real constant that is not an exact integer converted
0e3a99ae 2311 to integer type. */
374035cb 2312 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2313 && TREE_CODE (type) == INTEGER_TYPE)
2314 {
2315 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
49b0aa18 2316 give_warning = UNSAFE_REAL;
0e3a99ae 2317 }
91c41804 2318 /* Warn for an integer constant that does not fit into integer type. */
374035cb 2319 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2320 && TREE_CODE (type) == INTEGER_TYPE
2321 && !int_fits_type_p (expr, type))
2322 {
2323 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
374035cb 2324 && tree_int_cst_sgn (expr) < 0)
0e3a99ae
AS
2325 {
2326 if (produce_warns)
2327 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2328 " implicitly converted to unsigned type");
2329 }
2330 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2331 {
2332 if (produce_warns)
2333 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2334 " constant value to negative integer");
2335 }
7060db96 2336 else
49b0aa18 2337 give_warning = UNSAFE_OTHER;
0e3a99ae 2338 }
422c3a54 2339 else if (TREE_CODE (type) == REAL_TYPE)
0e3a99ae
AS
2340 {
2341 /* Warn for an integer constant that does not fit into real type. */
2342 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2343 {
2344 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2345 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2346 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2347 }
2348 /* Warn for a real constant that does not fit into a smaller
2349 real type. */
2350 else if (TREE_CODE (expr_type) == REAL_TYPE
2351 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2352 {
2353 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2354 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2355 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2356 }
2357 }
2358 }
7a37fa90
MM
2359
2360 else if (TREE_CODE (expr) == COMPLEX_CST)
2361 {
2362 tree imag_part = TREE_IMAGPART (expr);
2363 /* Conversion from complex constant with zero imaginary part,
2364 perform check for conversion of real part. */
2365 if ((TREE_CODE (imag_part) == REAL_CST
2366 && real_zerop (imag_part))
2367 || (TREE_CODE (imag_part) == INTEGER_CST
2368 && integer_zerop (imag_part)))
2369 /* Note: in this branch we use recursive call to unsafe_conversion_p
2370 with different type of EXPR, but it is still safe, because when EXPR
2371 is a constant, it's type is not used in text of generated warnings
2372 (otherwise they could sound misleading). */
2373 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2374 produce_warns);
2375 /* Conversion from complex constant with non-zero imaginary part. */
2376 else
2377 {
2378 /* Conversion to complex type.
2379 Perform checks for both real and imaginary parts. */
2380 if (TREE_CODE (type) == COMPLEX_TYPE)
2381 {
2382 /* Unfortunately, produce_warns must be false in two subsequent
2383 calls of unsafe_conversion_p, because otherwise we could
2384 produce strange "double" warnings, if both real and imaginary
2385 parts have conversion problems related to signedness.
2386
2387 For example:
2388 int32_t _Complex a = 0x80000000 + 0x80000000i;
2389
2390 Possible solution: add a separate function for checking
2391 constants and combine result of two calls appropriately. */
2392 enum conversion_safety re_safety =
2393 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2394 enum conversion_safety im_safety =
2395 unsafe_conversion_p (loc, type, imag_part, false);
2396
2397 /* Merge the results into appropriate single warning. */
2398
2399 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2400 if (re_safety == im_safety)
2401 give_warning = re_safety;
2402 else if (!re_safety && im_safety)
2403 give_warning = im_safety;
2404 else if (re_safety && !im_safety)
2405 give_warning = re_safety;
2406 else
2407 give_warning = UNSAFE_OTHER;
2408 }
2409 /* Warn about conversion from complex to real or integer type. */
2410 else
2411 give_warning = UNSAFE_IMAGINARY;
2412 }
2413 }
2414
2415 /* Checks for remaining case: EXPR is not constant. */
0e3a99ae
AS
2416 else
2417 {
422c3a54 2418 /* Warn for real types converted to integer types. */
6715192c 2419 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae 2420 && TREE_CODE (type) == INTEGER_TYPE)
49b0aa18 2421 give_warning = UNSAFE_REAL;
422c3a54 2422
6715192c 2423 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2424 && TREE_CODE (type) == INTEGER_TYPE)
2425 {
cfdaefec 2426 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
c00e8b06 2427 expr = get_unwidened (expr, 0);
6715192c 2428 expr_type = TREE_TYPE (expr);
cfdaefec 2429
6715192c 2430 /* Don't warn for short y; short x = ((int)y & 0xff); */
b8698a0f 2431 if (TREE_CODE (expr) == BIT_AND_EXPR
0e3a99ae 2432 || TREE_CODE (expr) == BIT_IOR_EXPR
6715192c
MLI
2433 || TREE_CODE (expr) == BIT_XOR_EXPR)
2434 {
374035cb
MLI
2435 /* If both args were extended from a shortest type,
2436 use that type if that is safe. */
b8698a0f
L
2437 expr_type = shorten_binary_op (expr_type,
2438 TREE_OPERAND (expr, 0),
2439 TREE_OPERAND (expr, 1),
6715192c
MLI
2440 /* bitwise */1);
2441
6715192c
MLI
2442 if (TREE_CODE (expr) == BIT_AND_EXPR)
2443 {
2444 tree op0 = TREE_OPERAND (expr, 0);
2445 tree op1 = TREE_OPERAND (expr, 1);
9c591bd0
MLI
2446 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2447 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2448
2449 /* If one of the operands is a non-negative constant
2450 that fits in the target type, then the type of the
2451 other operand does not matter. */
6715192c
MLI
2452 if ((TREE_CODE (op0) == INTEGER_CST
2453 && int_fits_type_p (op0, c_common_signed_type (type))
2454 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2455 || (TREE_CODE (op1) == INTEGER_CST
374035cb 2456 && int_fits_type_p (op1, c_common_signed_type (type))
b8698a0f 2457 && int_fits_type_p (op1,
374035cb 2458 c_common_unsigned_type (type))))
49b0aa18 2459 return SAFE_CONVERSION;
9c591bd0
MLI
2460 /* If constant is unsigned and fits in the target
2461 type, then the result will also fit. */
2462 else if ((TREE_CODE (op0) == INTEGER_CST
b8698a0f 2463 && unsigned0
9c591bd0
MLI
2464 && int_fits_type_p (op0, type))
2465 || (TREE_CODE (op1) == INTEGER_CST
2466 && unsigned1
2467 && int_fits_type_p (op1, type)))
49b0aa18 2468 return SAFE_CONVERSION;
6715192c
MLI
2469 }
2470 }
0e3a99ae 2471 /* Warn for integer types converted to smaller integer types. */
b8698a0f 2472 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2473 give_warning = UNSAFE_OTHER;
7060db96
MLI
2474
2475 /* When they are the same width but different signedness,
2476 then the value may change. */
0e3a99ae 2477 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
6715192c 2478 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7060db96
MLI
2479 /* Even when converted to a bigger type, if the type is
2480 unsigned but expr is signed, then negative values
2481 will be changed. */
0e3a99ae
AS
2482 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2483 && produce_warns)
6312e84d
MLI
2484 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2485 "may change the sign of the result",
2486 type, expr_type);
0e3a99ae 2487 }
422c3a54
MLI
2488
2489 /* Warn for integer types converted to real types if and only if
0e3a99ae
AS
2490 all the range of values of the integer type cannot be
2491 represented by the real type. */
6715192c 2492 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2493 && TREE_CODE (type) == REAL_TYPE)
2494 {
58076e21
MLI
2495 /* Don't warn about char y = 0xff; float x = (int) y; */
2496 expr = get_unwidened (expr, 0);
2497 expr_type = TREE_TYPE (expr);
2498
7a37fa90 2499 if (!int_safely_convertible_to_real_p (expr_type, type))
49b0aa18 2500 give_warning = UNSAFE_OTHER;
0e3a99ae 2501 }
422c3a54
MLI
2502
2503 /* Warn for real types converted to smaller real types. */
6715192c 2504 else if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2505 && TREE_CODE (type) == REAL_TYPE
2506 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2507 give_warning = UNSAFE_REAL;
7a37fa90
MM
2508
2509 /* Check conversion between two complex types. */
2510 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2511 && TREE_CODE (type) == COMPLEX_TYPE)
2512 {
2513 /* Extract underlying types (i.e., type of real and imaginary
2514 parts) of expr_type and type. */
2515 tree from_type = TREE_TYPE (expr_type);
2516 tree to_type = TREE_TYPE (type);
2517
2518 /* Warn for real types converted to integer types. */
2519 if (TREE_CODE (from_type) == REAL_TYPE
2520 && TREE_CODE (to_type) == INTEGER_TYPE)
2521 give_warning = UNSAFE_REAL;
2522
2523 /* Warn for real types converted to smaller real types. */
2524 else if (TREE_CODE (from_type) == REAL_TYPE
2525 && TREE_CODE (to_type) == REAL_TYPE
2526 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2527 give_warning = UNSAFE_REAL;
2528
2529 /* Check conversion for complex integer types. Here implementation
2530 is simpler than for real-domain integers because it does not
2531 involve sophisticated cases, such as bitmasks, casts, etc. */
2532 else if (TREE_CODE (from_type) == INTEGER_TYPE
2533 && TREE_CODE (to_type) == INTEGER_TYPE)
2534 {
2535 /* Warn for integer types converted to smaller integer types. */
2536 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2537 give_warning = UNSAFE_OTHER;
2538
2539 /* Check for different signedness, see case for real-domain
2540 integers (above) for a more detailed comment. */
2541 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2542 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2543 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2544 && produce_warns)
2545 warning_at (loc, OPT_Wsign_conversion,
2546 "conversion to %qT from %qT "
2547 "may change the sign of the result",
2548 type, expr_type);
2549 }
2550 else if (TREE_CODE (from_type) == INTEGER_TYPE
2551 && TREE_CODE (to_type) == REAL_TYPE
2552 && !int_safely_convertible_to_real_p (from_type, to_type))
2553 give_warning = UNSAFE_OTHER;
2554 }
2555
2556 /* Warn for complex types converted to real or integer types. */
2557 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2558 && TREE_CODE (type) != COMPLEX_TYPE)
2559 give_warning = UNSAFE_IMAGINARY;
0e3a99ae
AS
2560 }
2561
2562 return give_warning;
2563}
2564
2565/* Warns if the conversion of EXPR to TYPE may alter a value.
2566 This is a helper function for warnings_for_convert_and_check. */
2567
2568static void
68fca595 2569conversion_warning (location_t loc, tree type, tree expr)
0e3a99ae 2570{
0e3a99ae 2571 tree expr_type = TREE_TYPE (expr);
49b0aa18 2572 enum conversion_safety conversion_kind;
422c3a54 2573
49b0aa18 2574 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
0e3a99ae 2575 return;
422c3a54 2576
66f20604
MP
2577 /* This may happen, because for LHS op= RHS we preevaluate
2578 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2579 means we could no longer see the code of the EXPR. */
2580 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2581 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2582 if (TREE_CODE (expr) == SAVE_EXPR)
2583 expr = TREE_OPERAND (expr, 0);
2584
0e3a99ae
AS
2585 switch (TREE_CODE (expr))
2586 {
2587 case EQ_EXPR:
2588 case NE_EXPR:
2589 case LE_EXPR:
2590 case GE_EXPR:
2591 case LT_EXPR:
2592 case GT_EXPR:
2593 case TRUTH_ANDIF_EXPR:
2594 case TRUTH_ORIF_EXPR:
2595 case TRUTH_AND_EXPR:
2596 case TRUTH_OR_EXPR:
2597 case TRUTH_XOR_EXPR:
2598 case TRUTH_NOT_EXPR:
2599 /* Conversion from boolean to a signed:1 bit-field (which only
2600 can hold the values 0 and -1) doesn't lose information - but
2601 it does change the value. */
2602 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2603 warning_at (loc, OPT_Wconversion,
2604 "conversion to %qT from boolean expression", type);
2605 return;
2606
2607 case REAL_CST:
2608 case INTEGER_CST:
7a37fa90 2609 case COMPLEX_CST:
68fca595 2610 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
2611 if (conversion_kind == UNSAFE_REAL)
2612 warning_at (loc, OPT_Wfloat_conversion,
2613 "conversion to %qT alters %qT constant value",
2614 type, expr_type);
2615 else if (conversion_kind)
0e3a99ae
AS
2616 warning_at (loc, OPT_Wconversion,
2617 "conversion to %qT alters %qT constant value",
2618 type, expr_type);
2619 return;
2620
2621 case COND_EXPR:
2622 {
3f46d6a5
MLI
2623 /* In case of COND_EXPR, we do not care about the type of
2624 COND_EXPR, only about the conversion of each operand. */
2625 tree op1 = TREE_OPERAND (expr, 1);
2626 tree op2 = TREE_OPERAND (expr, 2);
2627
68fca595
MP
2628 conversion_warning (loc, type, op1);
2629 conversion_warning (loc, type, op2);
3f46d6a5 2630 return;
0e3a99ae
AS
2631 }
2632
2633 default: /* 'expr' is not a constant. */
68fca595 2634 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
2635 if (conversion_kind == UNSAFE_REAL)
2636 warning_at (loc, OPT_Wfloat_conversion,
2637 "conversion to %qT from %qT may alter its value",
2638 type, expr_type);
7a37fa90
MM
2639 else if (conversion_kind == UNSAFE_IMAGINARY)
2640 warning_at (loc, OPT_Wconversion,
2641 "conversion to %qT from %qT discards imaginary component",
2642 type, expr_type);
49b0aa18 2643 else if (conversion_kind)
0e3a99ae 2644 warning_at (loc, OPT_Wconversion,
6312e84d
MLI
2645 "conversion to %qT from %qT may alter its value",
2646 type, expr_type);
422c3a54
MLI
2647 }
2648}
2649
07231d4f
MLI
2650/* Produce warnings after a conversion. RESULT is the result of
2651 converting EXPR to TYPE. This is a helper function for
2652 convert_and_check and cp_convert_and_check. */
d74154d5 2653
07231d4f 2654void
68fca595
MP
2655warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2656 tree result)
d74154d5 2657{
68fca595 2658 loc = expansion_point_location_if_in_system_header (loc);
5a3c9cf2 2659
91c41804
RS
2660 if (TREE_CODE (expr) == INTEGER_CST
2661 && (TREE_CODE (type) == INTEGER_TYPE
2662 || TREE_CODE (type) == ENUMERAL_TYPE)
2663 && !int_fits_type_p (expr, type))
2664 {
422c3a54
MLI
2665 /* Do not diagnose overflow in a constant expression merely
2666 because a conversion overflowed. */
91c41804 2667 if (TREE_OVERFLOW (result))
d95787e6
RS
2668 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2669
91c41804 2670 if (TYPE_UNSIGNED (type))
422c3a54 2671 {
91c41804
RS
2672 /* This detects cases like converting -129 or 256 to
2673 unsigned char. */
2674 if (!int_fits_type_p (expr, c_common_signed_type (type)))
5a3c9cf2
PC
2675 warning_at (loc, OPT_Woverflow,
2676 "large integer implicitly truncated to unsigned type");
7060db96 2677 else
68fca595 2678 conversion_warning (loc, type, expr);
91c41804 2679 }
b8698a0f 2680 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
68fca595 2681 warning_at (loc, OPT_Woverflow,
f73fe417
MLI
2682 "overflow in implicit constant conversion");
2683 /* No warning for converting 0x80000000 to int. */
2684 else if (pedantic
2685 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2686 || TYPE_PRECISION (TREE_TYPE (expr))
2687 != TYPE_PRECISION (type)))
5a3c9cf2
PC
2688 warning_at (loc, OPT_Woverflow,
2689 "overflow in implicit constant conversion");
f73fe417 2690
7060db96 2691 else
68fca595 2692 conversion_warning (loc, type, expr);
d74154d5 2693 }
ab22c1fa
CF
2694 else if ((TREE_CODE (result) == INTEGER_CST
2695 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
5a3c9cf2
PC
2696 warning_at (loc, OPT_Woverflow,
2697 "overflow in implicit constant conversion");
7060db96 2698 else
68fca595 2699 conversion_warning (loc, type, expr);
07231d4f
MLI
2700}
2701
2702
2703/* Convert EXPR to TYPE, warning about conversion problems with constants.
2704 Invoke this function on every expression that is converted implicitly,
2705 i.e. because of language rules and not because of an explicit cast. */
2706
2707tree
68fca595 2708convert_and_check (location_t loc, tree type, tree expr)
07231d4f
MLI
2709{
2710 tree result;
8ce94e44
JM
2711 tree expr_for_warning;
2712
2713 /* Convert from a value with possible excess precision rather than
2714 via the semantic type, but do not warn about values not fitting
2715 exactly in the semantic type. */
2716 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2717 {
2718 tree orig_type = TREE_TYPE (expr);
2719 expr = TREE_OPERAND (expr, 0);
2720 expr_for_warning = convert (orig_type, expr);
2721 if (orig_type == type)
2722 return expr_for_warning;
2723 }
2724 else
2725 expr_for_warning = expr;
07231d4f
MLI
2726
2727 if (TREE_TYPE (expr) == type)
2728 return expr;
b8698a0f 2729
07231d4f
MLI
2730 result = convert (type, expr);
2731
7d882b83
ILT
2732 if (c_inhibit_evaluation_warnings == 0
2733 && !TREE_OVERFLOW_P (expr)
2734 && result != error_mark_node)
68fca595 2735 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
07231d4f 2736
91c41804 2737 return result;
96571883
BK
2738}
2739\f
235cfbc4
BS
2740/* A node in a list that describes references to variables (EXPR), which are
2741 either read accesses if WRITER is zero, or write accesses, in which case
2742 WRITER is the parent of EXPR. */
2743struct tlist
2744{
2745 struct tlist *next;
2746 tree expr, writer;
2747};
2748
2749/* Used to implement a cache the results of a call to verify_tree. We only
2750 use this for SAVE_EXPRs. */
2751struct tlist_cache
2752{
2753 struct tlist_cache *next;
2754 struct tlist *cache_before_sp;
2755 struct tlist *cache_after_sp;
2756 tree expr;
2683ed8d
BS
2757};
2758
235cfbc4
BS
2759/* Obstack to use when allocating tlist structures, and corresponding
2760 firstobj. */
2761static struct obstack tlist_obstack;
2762static char *tlist_firstobj = 0;
2763
2764/* Keep track of the identifiers we've warned about, so we can avoid duplicate
2765 warnings. */
2766static struct tlist *warned_ids;
2767/* SAVE_EXPRs need special treatment. We process them only once and then
2768 cache the results. */
2769static struct tlist_cache *save_expr_cache;
2770
35b1a6fa
AJ
2771static void add_tlist (struct tlist **, struct tlist *, tree, int);
2772static void merge_tlist (struct tlist **, struct tlist *, int);
2773static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2774static int warning_candidate_p (tree);
1e4ae551 2775static bool candidate_equal_p (const_tree, const_tree);
35b1a6fa
AJ
2776static void warn_for_collisions (struct tlist *);
2777static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2778static struct tlist *new_tlist (struct tlist *, tree, tree);
2683ed8d 2779
235cfbc4
BS
2780/* Create a new struct tlist and fill in its fields. */
2781static struct tlist *
35b1a6fa 2782new_tlist (struct tlist *next, tree t, tree writer)
235cfbc4
BS
2783{
2784 struct tlist *l;
5d038c4c 2785 l = XOBNEW (&tlist_obstack, struct tlist);
235cfbc4
BS
2786 l->next = next;
2787 l->expr = t;
2788 l->writer = writer;
2789 return l;
2790}
2791
2792/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2793 is nonnull, we ignore any node we find which has a writer equal to it. */
2794
2795static void
35b1a6fa 2796add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
235cfbc4
BS
2797{
2798 while (add)
2799 {
2800 struct tlist *next = add->next;
3f75a254 2801 if (!copy)
235cfbc4 2802 add->next = *to;
1e4ae551 2803 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
235cfbc4
BS
2804 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2805 add = next;
2806 }
2807}
2808
2809/* Merge the nodes of ADD into TO. This merging process is done so that for
2810 each variable that already exists in TO, no new node is added; however if
2811 there is a write access recorded in ADD, and an occurrence on TO is only
2812 a read access, then the occurrence in TO will be modified to record the
2813 write. */
2683ed8d
BS
2814
2815static void
35b1a6fa 2816merge_tlist (struct tlist **to, struct tlist *add, int copy)
235cfbc4
BS
2817{
2818 struct tlist **end = to;
2819
2820 while (*end)
2821 end = &(*end)->next;
2822
2823 while (add)
2824 {
2825 int found = 0;
2826 struct tlist *tmp2;
2827 struct tlist *next = add->next;
2828
2829 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1e4ae551 2830 if (candidate_equal_p (tmp2->expr, add->expr))
235cfbc4
BS
2831 {
2832 found = 1;
3f75a254 2833 if (!tmp2->writer)
235cfbc4
BS
2834 tmp2->writer = add->writer;
2835 }
3f75a254 2836 if (!found)
235cfbc4 2837 {
c2bf53a1 2838 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
235cfbc4
BS
2839 end = &(*end)->next;
2840 *end = 0;
2841 }
2842 add = next;
2843 }
2844}
2845
2846/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2847 references in list LIST conflict with it, excluding reads if ONLY writers
2848 is nonzero. */
2849
2850static void
35b1a6fa
AJ
2851warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2852 int only_writes)
235cfbc4
BS
2853{
2854 struct tlist *tmp;
2855
2856 /* Avoid duplicate warnings. */
2857 for (tmp = warned_ids; tmp; tmp = tmp->next)
1e4ae551 2858 if (candidate_equal_p (tmp->expr, written))
235cfbc4
BS
2859 return;
2860
2861 while (list)
2862 {
1e4ae551
MLI
2863 if (candidate_equal_p (list->expr, written)
2864 && !candidate_equal_p (list->writer, writer)
2865 && (!only_writes || list->writer))
235cfbc4
BS
2866 {
2867 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
8400e75e 2868 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
ca085fd7
MLI
2869 OPT_Wsequence_point, "operation on %qE may be undefined",
2870 list->expr);
235cfbc4
BS
2871 }
2872 list = list->next;
2873 }
2874}
2875
2876/* Given a list LIST of references to variables, find whether any of these
2877 can cause conflicts due to missing sequence points. */
2878
2879static void
35b1a6fa 2880warn_for_collisions (struct tlist *list)
235cfbc4
BS
2881{
2882 struct tlist *tmp;
35b1a6fa 2883
235cfbc4
BS
2884 for (tmp = list; tmp; tmp = tmp->next)
2885 {
2886 if (tmp->writer)
2887 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2888 }
2889}
2890
684d9f3b 2891/* Return nonzero if X is a tree that can be verified by the sequence point
235cfbc4
BS
2892 warnings. */
2893static int
35b1a6fa 2894warning_candidate_p (tree x)
2683ed8d 2895{
07078664
JJ
2896 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2897 return 0;
2898
92e948a8
NF
2899 if (TREE_CODE (x) == BLOCK)
2900 return 0;
2901
07078664 2902 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1e4ae551 2903 (lvalue_p) crash on TRY/CATCH. */
07078664
JJ
2904 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2905 return 0;
2906
2907 if (!lvalue_p (x))
2908 return 0;
2909
2910 /* No point to track non-const calls, they will never satisfy
2911 operand_equal_p. */
2912 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2913 return 0;
2914
2915 if (TREE_CODE (x) == STRING_CST)
2916 return 0;
2917
2918 return 1;
1e4ae551
MLI
2919}
2920
2921/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2922static bool
2923candidate_equal_p (const_tree x, const_tree y)
2924{
2925 return (x == y) || (x && y && operand_equal_p (x, y, 0));
235cfbc4 2926}
2683ed8d 2927
235cfbc4
BS
2928/* Walk the tree X, and record accesses to variables. If X is written by the
2929 parent tree, WRITER is the parent.
2930 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2931 expression or its only operand forces a sequence point, then everything up
2932 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2933 in PNO_SP.
2934 Once we return, we will have emitted warnings if any subexpression before
2935 such a sequence point could be undefined. On a higher level, however, the
2936 sequence point may not be relevant, and we'll merge the two lists.
2937
2938 Example: (b++, a) + b;
2939 The call that processes the COMPOUND_EXPR will store the increment of B
2940 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2941 processes the PLUS_EXPR will need to merge the two lists so that
2942 eventually, all accesses end up on the same list (and we'll warn about the
2943 unordered subexpressions b++ and b.
2944
2945 A note on merging. If we modify the former example so that our expression
2946 becomes
2947 (b++, b) + a
2948 care must be taken not simply to add all three expressions into the final
2949 PNO_SP list. The function merge_tlist takes care of that by merging the
2950 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2951 way, so that no more than one access to B is recorded. */
2683ed8d 2952
235cfbc4 2953static void
35b1a6fa
AJ
2954verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2955 tree writer)
235cfbc4
BS
2956{
2957 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2958 enum tree_code code;
6615c446 2959 enum tree_code_class cl;
2683ed8d 2960
f9e1917e
JM
2961 /* X may be NULL if it is the operand of an empty statement expression
2962 ({ }). */
2963 if (x == NULL)
2964 return;
2965
235cfbc4
BS
2966 restart:
2967 code = TREE_CODE (x);
e3a64162 2968 cl = TREE_CODE_CLASS (code);
2683ed8d 2969
235cfbc4 2970 if (warning_candidate_p (x))
1e4ae551 2971 *pno_sp = new_tlist (*pno_sp, x, writer);
235cfbc4
BS
2972
2973 switch (code)
2974 {
52a84e42 2975 case CONSTRUCTOR:
f7716d57 2976 case SIZEOF_EXPR:
52a84e42
BS
2977 return;
2978
235cfbc4
BS
2979 case COMPOUND_EXPR:
2980 case TRUTH_ANDIF_EXPR:
2981 case TRUTH_ORIF_EXPR:
2982 tmp_before = tmp_nosp = tmp_list3 = 0;
2983 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2984 warn_for_collisions (tmp_nosp);
2985 merge_tlist (pbefore_sp, tmp_before, 0);
2986 merge_tlist (pbefore_sp, tmp_nosp, 0);
2987 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2988 merge_tlist (pbefore_sp, tmp_list3, 0);
2989 return;
2990
2991 case COND_EXPR:
2992 tmp_before = tmp_list2 = 0;
2993 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2994 warn_for_collisions (tmp_list2);
2995 merge_tlist (pbefore_sp, tmp_before, 0);
c2bf53a1 2996 merge_tlist (pbefore_sp, tmp_list2, 0);
235cfbc4
BS
2997
2998 tmp_list3 = tmp_nosp = 0;
2999 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3000 warn_for_collisions (tmp_nosp);
3001 merge_tlist (pbefore_sp, tmp_list3, 0);
3002
3003 tmp_list3 = tmp_list2 = 0;
3004 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3005 warn_for_collisions (tmp_list2);
3006 merge_tlist (pbefore_sp, tmp_list3, 0);
3007 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3008 two first, to avoid warning for (a ? b++ : b++). */
3009 merge_tlist (&tmp_nosp, tmp_list2, 0);
3010 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3011 return;
3012
2683ed8d
BS
3013 case PREDECREMENT_EXPR:
3014 case PREINCREMENT_EXPR:
3015 case POSTDECREMENT_EXPR:
3016 case POSTINCREMENT_EXPR:
235cfbc4
BS
3017 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3018 return;
3019
3020 case MODIFY_EXPR:
3021 tmp_before = tmp_nosp = tmp_list3 = 0;
3022 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3023 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3024 /* Expressions inside the LHS are not ordered wrt. the sequence points
3025 in the RHS. Example:
3026 *a = (a++, 2)
3027 Despite the fact that the modification of "a" is in the before_sp
3028 list (tmp_before), it conflicts with the use of "a" in the LHS.
3029 We can handle this by adding the contents of tmp_list3
3030 to those of tmp_before, and redoing the collision warnings for that
3031 list. */
3032 add_tlist (&tmp_before, tmp_list3, x, 1);
3033 warn_for_collisions (tmp_before);
3034 /* Exclude the LHS itself here; we first have to merge it into the
3035 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3036 didn't exclude the LHS, we'd get it twice, once as a read and once
3037 as a write. */
3038 add_tlist (pno_sp, tmp_list3, x, 0);
3039 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3040
3041 merge_tlist (pbefore_sp, tmp_before, 0);
3042 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3043 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3044 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3045 return;
2683ed8d
BS
3046
3047 case CALL_EXPR:
235cfbc4
BS
3048 /* We need to warn about conflicts among arguments and conflicts between
3049 args and the function address. Side effects of the function address,
3050 however, are not ordered by the sequence point of the call. */
5039610b
SL
3051 {
3052 call_expr_arg_iterator iter;
3053 tree arg;
b8698a0f 3054 tmp_before = tmp_nosp = 0;
5039610b
SL
3055 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3056 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3057 {
3058 tmp_list2 = tmp_list3 = 0;
3059 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3060 merge_tlist (&tmp_list3, tmp_list2, 0);
3061 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3062 }
3063 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3064 warn_for_collisions (tmp_before);
3065 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3066 return;
3067 }
2683ed8d
BS
3068
3069 case TREE_LIST:
3070 /* Scan all the list, e.g. indices of multi dimensional array. */
3071 while (x)
3072 {
235cfbc4
BS
3073 tmp_before = tmp_nosp = 0;
3074 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3075 merge_tlist (&tmp_nosp, tmp_before, 0);
3076 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2683ed8d
BS
3077 x = TREE_CHAIN (x);
3078 }
235cfbc4 3079 return;
2683ed8d 3080
235cfbc4
BS
3081 case SAVE_EXPR:
3082 {
3083 struct tlist_cache *t;
3084 for (t = save_expr_cache; t; t = t->next)
1e4ae551 3085 if (candidate_equal_p (t->expr, x))
235cfbc4 3086 break;
2683ed8d 3087
3f75a254 3088 if (!t)
2683ed8d 3089 {
5d038c4c 3090 t = XOBNEW (&tlist_obstack, struct tlist_cache);
235cfbc4
BS
3091 t->next = save_expr_cache;
3092 t->expr = x;
3093 save_expr_cache = t;
3094
3095 tmp_before = tmp_nosp = 0;
3096 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3097 warn_for_collisions (tmp_nosp);
3098
3099 tmp_list3 = 0;
c2bf53a1 3100 merge_tlist (&tmp_list3, tmp_nosp, 0);
235cfbc4
BS
3101 t->cache_before_sp = tmp_before;
3102 t->cache_after_sp = tmp_list3;
2683ed8d 3103 }
235cfbc4
BS
3104 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3105 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3106 return;
3107 }
2683ed8d 3108
528c22f4
MLI
3109 case ADDR_EXPR:
3110 x = TREE_OPERAND (x, 0);
3111 if (DECL_P (x))
3112 return;
3113 writer = 0;
3114 goto restart;
3115
6615c446
JO
3116 default:
3117 /* For other expressions, simply recurse on their operands.
c22cacf3 3118 Manual tail recursion for unary expressions.
6615c446
JO
3119 Other non-expressions need not be processed. */
3120 if (cl == tcc_unary)
3121 {
6615c446
JO
3122 x = TREE_OPERAND (x, 0);
3123 writer = 0;
3124 goto restart;
3125 }
3126 else if (IS_EXPR_CODE_CLASS (cl))
3127 {
3128 int lp;
5039610b 3129 int max = TREE_OPERAND_LENGTH (x);
6615c446
JO
3130 for (lp = 0; lp < max; lp++)
3131 {
3132 tmp_before = tmp_nosp = 0;
3133 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3134 merge_tlist (&tmp_nosp, tmp_before, 0);
3135 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3136 }
3137 }
3138 return;
2683ed8d 3139 }
2683ed8d
BS
3140}
3141
8d9afc4e 3142/* Try to warn for undefined behavior in EXPR due to missing sequence
2683ed8d
BS
3143 points. */
3144
24e47c76 3145DEBUG_FUNCTION void
35b1a6fa 3146verify_sequence_points (tree expr)
2683ed8d 3147{
235cfbc4 3148 struct tlist *before_sp = 0, *after_sp = 0;
2683ed8d 3149
235cfbc4
BS
3150 warned_ids = 0;
3151 save_expr_cache = 0;
3152 if (tlist_firstobj == 0)
2683ed8d 3153 {
235cfbc4 3154 gcc_obstack_init (&tlist_obstack);
28dab132 3155 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2683ed8d
BS
3156 }
3157
235cfbc4
BS
3158 verify_tree (expr, &before_sp, &after_sp, 0);
3159 warn_for_collisions (after_sp);
3160 obstack_free (&tlist_obstack, tlist_firstobj);
2683ed8d 3161}
b30f223b
RS
3162\f
3163/* Validate the expression after `case' and apply default promotions. */
3164
a6c0a76c 3165static tree
62e4eb35 3166check_case_value (location_t loc, tree value)
b30f223b
RS
3167{
3168 if (value == NULL_TREE)
3169 return value;
3170
522ddfa2
JM
3171 if (TREE_CODE (value) == INTEGER_CST)
3172 /* Promote char or short to int. */
3173 value = perform_integral_promotions (value);
3174 else if (value != error_mark_node)
b30f223b 3175 {
62e4eb35 3176 error_at (loc, "case label does not reduce to an integer constant");
b30f223b
RS
3177 value = error_mark_node;
3178 }
b30f223b 3179
bc690db1
RS
3180 constant_expression_warning (value);
3181
b30f223b
RS
3182 return value;
3183}
3184\f
a6c0a76c 3185/* See if the case values LOW and HIGH are in the range of the original
89dbed81 3186 type (i.e. before the default conversion to int) of the switch testing
a6c0a76c
SB
3187 expression.
3188 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2a7e31df 3189 the type before promoting it. CASE_LOW_P is a pointer to the lower
a6c0a76c
SB
3190 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3191 if the case is not a case range.
3192 The caller has to make sure that we are not called with NULL for
b155cfd9
MP
3193 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3194 was a case value that doesn't fit into the range of the ORIG_TYPE.
0fa2e4df 3195 Returns true if the case label is in range of ORIG_TYPE (saturated or
a6c0a76c
SB
3196 untouched) or false if the label is out of range. */
3197
3198static bool
9d548dfb 3199check_case_bounds (location_t loc, tree type, tree orig_type,
b155cfd9
MP
3200 tree *case_low_p, tree *case_high_p,
3201 bool *outside_range_p)
a6c0a76c
SB
3202{
3203 tree min_value, max_value;
3204 tree case_low = *case_low_p;
3205 tree case_high = case_high_p ? *case_high_p : case_low;
3206
3207 /* If there was a problem with the original type, do nothing. */
3208 if (orig_type == error_mark_node)
3209 return true;
3210
3211 min_value = TYPE_MIN_VALUE (orig_type);
3212 max_value = TYPE_MAX_VALUE (orig_type);
3213
0f62c7a0
MP
3214 /* We'll really need integer constants here. */
3215 case_low = fold (case_low);
3216 case_high = fold (case_high);
3217
a6c0a76c
SB
3218 /* Case label is less than minimum for type. */
3219 if (tree_int_cst_compare (case_low, min_value) < 0
3220 && tree_int_cst_compare (case_high, min_value) < 0)
3221 {
9d548dfb
MP
3222 warning_at (loc, 0, "case label value is less than minimum value "
3223 "for type");
b155cfd9 3224 *outside_range_p = true;
a6c0a76c
SB
3225 return false;
3226 }
9f63daea 3227
a6c0a76c
SB
3228 /* Case value is greater than maximum for type. */
3229 if (tree_int_cst_compare (case_low, max_value) > 0
3230 && tree_int_cst_compare (case_high, max_value) > 0)
3231 {
9d548dfb 3232 warning_at (loc, 0, "case label value exceeds maximum value for type");
b155cfd9 3233 *outside_range_p = true;
a6c0a76c
SB
3234 return false;
3235 }
3236
3237 /* Saturate lower case label value to minimum. */
3238 if (tree_int_cst_compare (case_high, min_value) >= 0
3239 && tree_int_cst_compare (case_low, min_value) < 0)
3240 {
9d548dfb
MP
3241 warning_at (loc, 0, "lower value in case label range"
3242 " less than minimum value for type");
b155cfd9 3243 *outside_range_p = true;
a6c0a76c
SB
3244 case_low = min_value;
3245 }
9f63daea 3246
a6c0a76c
SB
3247 /* Saturate upper case label value to maximum. */
3248 if (tree_int_cst_compare (case_low, max_value) <= 0
3249 && tree_int_cst_compare (case_high, max_value) > 0)
3250 {
9d548dfb
MP
3251 warning_at (loc, 0, "upper value in case label range"
3252 " exceeds maximum value for type");
b155cfd9 3253 *outside_range_p = true;
a6c0a76c
SB
3254 case_high = max_value;
3255 }
3256
3257 if (*case_low_p != case_low)
3258 *case_low_p = convert (type, case_low);
3259 if (case_high_p && *case_high_p != case_high)
3260 *case_high_p = convert (type, case_high);
3261
3262 return true;
3263}
3264\f
b30f223b
RS
3265/* Return an integer type with BITS bits of precision,
3266 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3267
3268tree
35b1a6fa 3269c_common_type_for_size (unsigned int bits, int unsignedp)
b30f223b 3270{
78a7c317
DD
3271 int i;
3272
a311b52c
JM
3273 if (bits == TYPE_PRECISION (integer_type_node))
3274 return unsignedp ? unsigned_type_node : integer_type_node;
3275
3fc7e390 3276 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
3277 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3278
3fc7e390 3279 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
3280 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3281
3fc7e390 3282 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
3283 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3284
3fc7e390 3285 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
3286 return (unsignedp ? long_long_unsigned_type_node
3287 : long_long_integer_type_node);
3288
78a7c317
DD
3289 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3290 if (int_n_enabled_p[i]
3291 && bits == int_n_data[i].bitsize)
3292 return (unsignedp ? int_n_trees[i].unsigned_type
3293 : int_n_trees[i].signed_type);
a6766312 3294
835f9b4d
GRK
3295 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3296 return (unsignedp ? widest_unsigned_literal_type_node
3297 : widest_integer_literal_type_node);
3298
3fc7e390
RS
3299 if (bits <= TYPE_PRECISION (intQI_type_node))
3300 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3301
3302 if (bits <= TYPE_PRECISION (intHI_type_node))
3303 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3304
3305 if (bits <= TYPE_PRECISION (intSI_type_node))
3306 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3307
3308 if (bits <= TYPE_PRECISION (intDI_type_node))
3309 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3310
b30f223b
RS
3311 return 0;
3312}
3313
ab22c1fa
CF
3314/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3315 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3316 and saturating if SATP is nonzero, otherwise not saturating. */
3317
3318tree
3319c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3320 int unsignedp, int satp)
3321{
ef4bddc2 3322 machine_mode mode;
ab22c1fa
CF
3323 if (ibit == 0)
3324 mode = unsignedp ? UQQmode : QQmode;
3325 else
3326 mode = unsignedp ? UHAmode : HAmode;
3327
3328 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3329 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3330 break;
3331
3332 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3333 {
3334 sorry ("GCC cannot support operators with integer types and "
3335 "fixed-point types that have too many integral and "
3336 "fractional bits together");
3337 return 0;
3338 }
3339
3340 return c_common_type_for_mode (mode, satp);
3341}
3342
d1d3865f
ZW
3343/* Used for communication between c_common_type_for_mode and
3344 c_register_builtin_type. */
793c625f 3345tree registered_builtin_types;
d1d3865f 3346
b30f223b
RS
3347/* Return a data type that has machine mode MODE.
3348 If the mode is an integer,
ab22c1fa
CF
3349 then UNSIGNEDP selects between signed and unsigned types.
3350 If the mode is a fixed-point mode,
3351 then UNSIGNEDP selects between saturating and nonsaturating types. */
b30f223b
RS
3352
3353tree
ef4bddc2 3354c_common_type_for_mode (machine_mode mode, int unsignedp)
b30f223b 3355{
d1d3865f 3356 tree t;
78a7c317 3357 int i;
d1d3865f 3358
a311b52c
JM
3359 if (mode == TYPE_MODE (integer_type_node))
3360 return unsignedp ? unsigned_type_node : integer_type_node;
3361
b30f223b
RS
3362 if (mode == TYPE_MODE (signed_char_type_node))
3363 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3364
3365 if (mode == TYPE_MODE (short_integer_type_node))
3366 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3367
b30f223b
RS
3368 if (mode == TYPE_MODE (long_integer_type_node))
3369 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3370
3371 if (mode == TYPE_MODE (long_long_integer_type_node))
3372 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3373
78a7c317
DD
3374 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3375 if (int_n_enabled_p[i]
3376 && mode == int_n_data[i].m)
3377 return (unsignedp ? int_n_trees[i].unsigned_type
3378 : int_n_trees[i].signed_type);
a6766312 3379
835f9b4d 3380 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 3381 return unsignedp ? widest_unsigned_literal_type_node
6de9cd9a 3382 : widest_integer_literal_type_node;
835f9b4d 3383
0afeef64 3384 if (mode == QImode)
3fc7e390
RS
3385 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3386
0afeef64 3387 if (mode == HImode)
3fc7e390
RS
3388 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3389
0afeef64 3390 if (mode == SImode)
3fc7e390
RS
3391 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3392
0afeef64 3393 if (mode == DImode)
3fc7e390
RS
3394 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3395
21a9616b 3396#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
3397 if (mode == TYPE_MODE (intTI_type_node))
3398 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 3399#endif
a6d7e156 3400
b30f223b
RS
3401 if (mode == TYPE_MODE (float_type_node))
3402 return float_type_node;
3403
3404 if (mode == TYPE_MODE (double_type_node))
3405 return double_type_node;
3406
3407 if (mode == TYPE_MODE (long_double_type_node))
3408 return long_double_type_node;
3409
ff42324e
NS
3410 if (mode == TYPE_MODE (void_type_node))
3411 return void_type_node;
9f63daea 3412
b30f223b 3413 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
19b3ffbc
DD
3414 return (unsignedp
3415 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3416 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b
RS
3417
3418 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
19b3ffbc
DD
3419 return (unsignedp
3420 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3421 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b 3422
7e7e470f
RH
3423 if (COMPLEX_MODE_P (mode))
3424 {
ef4bddc2 3425 machine_mode inner_mode;
7e7e470f
RH
3426 tree inner_type;
3427
3428 if (mode == TYPE_MODE (complex_float_type_node))
3429 return complex_float_type_node;
3430 if (mode == TYPE_MODE (complex_double_type_node))
3431 return complex_double_type_node;
3432 if (mode == TYPE_MODE (complex_long_double_type_node))
3433 return complex_long_double_type_node;
3434
3435 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3436 return complex_integer_type_node;
3437
3438 inner_mode = GET_MODE_INNER (mode);
3439 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3440 if (inner_type != NULL_TREE)
3441 return build_complex_type (inner_type);
3442 }
3443 else if (VECTOR_MODE_P (mode))
4a5eab38 3444 {
ef4bddc2 3445 machine_mode inner_mode = GET_MODE_INNER (mode);
4a5eab38
PB
3446 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3447 if (inner_type != NULL_TREE)
3448 return build_vector_type_for_mode (inner_type, mode);
0afeef64 3449 }
4061f623 3450
9a8ce21f
JG
3451 if (mode == TYPE_MODE (dfloat32_type_node))
3452 return dfloat32_type_node;
3453 if (mode == TYPE_MODE (dfloat64_type_node))
3454 return dfloat64_type_node;
3455 if (mode == TYPE_MODE (dfloat128_type_node))
3456 return dfloat128_type_node;
3457
ab22c1fa
CF
3458 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3459 {
3460 if (mode == TYPE_MODE (short_fract_type_node))
3461 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3462 if (mode == TYPE_MODE (fract_type_node))
3463 return unsignedp ? sat_fract_type_node : fract_type_node;
3464 if (mode == TYPE_MODE (long_fract_type_node))
3465 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3466 if (mode == TYPE_MODE (long_long_fract_type_node))
3467 return unsignedp ? sat_long_long_fract_type_node
3468 : long_long_fract_type_node;
3469
3470 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3471 return unsignedp ? sat_unsigned_short_fract_type_node
3472 : unsigned_short_fract_type_node;
3473 if (mode == TYPE_MODE (unsigned_fract_type_node))
3474 return unsignedp ? sat_unsigned_fract_type_node
3475 : unsigned_fract_type_node;
3476 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3477 return unsignedp ? sat_unsigned_long_fract_type_node
3478 : unsigned_long_fract_type_node;
3479 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3480 return unsignedp ? sat_unsigned_long_long_fract_type_node
3481 : unsigned_long_long_fract_type_node;
3482
3483 if (mode == TYPE_MODE (short_accum_type_node))
3484 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3485 if (mode == TYPE_MODE (accum_type_node))
3486 return unsignedp ? sat_accum_type_node : accum_type_node;
3487 if (mode == TYPE_MODE (long_accum_type_node))
3488 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3489 if (mode == TYPE_MODE (long_long_accum_type_node))
3490 return unsignedp ? sat_long_long_accum_type_node
3491 : long_long_accum_type_node;
3492
3493 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3494 return unsignedp ? sat_unsigned_short_accum_type_node
3495 : unsigned_short_accum_type_node;
3496 if (mode == TYPE_MODE (unsigned_accum_type_node))
3497 return unsignedp ? sat_unsigned_accum_type_node
3498 : unsigned_accum_type_node;
3499 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3500 return unsignedp ? sat_unsigned_long_accum_type_node
3501 : unsigned_long_accum_type_node;
3502 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3503 return unsignedp ? sat_unsigned_long_long_accum_type_node
3504 : unsigned_long_long_accum_type_node;
3505
3506 if (mode == QQmode)
3507 return unsignedp ? sat_qq_type_node : qq_type_node;
3508 if (mode == HQmode)
3509 return unsignedp ? sat_hq_type_node : hq_type_node;
3510 if (mode == SQmode)
3511 return unsignedp ? sat_sq_type_node : sq_type_node;
3512 if (mode == DQmode)
3513 return unsignedp ? sat_dq_type_node : dq_type_node;
3514 if (mode == TQmode)
3515 return unsignedp ? sat_tq_type_node : tq_type_node;
3516
3517 if (mode == UQQmode)
3518 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3519 if (mode == UHQmode)
3520 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3521 if (mode == USQmode)
3522 return unsignedp ? sat_usq_type_node : usq_type_node;
3523 if (mode == UDQmode)
3524 return unsignedp ? sat_udq_type_node : udq_type_node;
3525 if (mode == UTQmode)
3526 return unsignedp ? sat_utq_type_node : utq_type_node;
3527
3528 if (mode == HAmode)
3529 return unsignedp ? sat_ha_type_node : ha_type_node;
3530 if (mode == SAmode)
3531 return unsignedp ? sat_sa_type_node : sa_type_node;
3532 if (mode == DAmode)
3533 return unsignedp ? sat_da_type_node : da_type_node;
3534 if (mode == TAmode)
3535 return unsignedp ? sat_ta_type_node : ta_type_node;
3536
3537 if (mode == UHAmode)
3538 return unsignedp ? sat_uha_type_node : uha_type_node;
3539 if (mode == USAmode)
3540 return unsignedp ? sat_usa_type_node : usa_type_node;
3541 if (mode == UDAmode)
3542 return unsignedp ? sat_uda_type_node : uda_type_node;
3543 if (mode == UTAmode)
3544 return unsignedp ? sat_uta_type_node : uta_type_node;
3545 }
3546
d1d3865f 3547 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
7a421706
MS
3548 if (TYPE_MODE (TREE_VALUE (t)) == mode
3549 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
d1d3865f
ZW
3550 return TREE_VALUE (t);
3551
b30f223b
RS
3552 return 0;
3553}
693a6128 3554
12753674
RE
3555tree
3556c_common_unsigned_type (tree type)
3557{
3558 return c_common_signed_or_unsigned_type (1, type);
3559}
3560
693a6128
GRK
3561/* Return a signed type the same as TYPE in other respects. */
3562
3563tree
35b1a6fa 3564c_common_signed_type (tree type)
693a6128 3565{
ceef8ce4 3566 return c_common_signed_or_unsigned_type (0, type);
693a6128
GRK
3567}
3568
3569/* Return a type the same as TYPE except unsigned or
3570 signed according to UNSIGNEDP. */
3571
3572tree
35b1a6fa 3573c_common_signed_or_unsigned_type (int unsignedp, tree type)
693a6128 3574{
c74a03d2 3575 tree type1;
78a7c317 3576 int i;
693a6128 3577
c74a03d2
RAE
3578 /* This block of code emulates the behavior of the old
3579 c_common_unsigned_type. In particular, it returns
3580 long_unsigned_type_node if passed a long, even when a int would
3581 have the same size. This is necessary for warnings to work
3582 correctly in archs where sizeof(int) == sizeof(long) */
3583
3584 type1 = TYPE_MAIN_VARIANT (type);
3585 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3586 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3587 if (type1 == integer_type_node || type1 == unsigned_type_node)
3588 return unsignedp ? unsigned_type_node : integer_type_node;
3589 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3590 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3591 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3592 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3593 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3594 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
78a7c317
DD
3595
3596 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3597 if (int_n_enabled_p[i]
3598 && (type1 == int_n_trees[i].unsigned_type
3599 || type1 == int_n_trees[i].signed_type))
3600 return (unsignedp ? int_n_trees[i].unsigned_type
3601 : int_n_trees[i].signed_type);
3602
c74a03d2
RAE
3603 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3604 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3605#if HOST_BITS_PER_WIDE_INT >= 64
3606 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3607 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3608#endif
3609 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3610 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3611 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3612 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3613 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3614 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3615 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3616 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3617
70d3fcab
AH
3618#define C_COMMON_FIXED_TYPES(NAME) \
3619 if (type1 == short_ ## NAME ## _type_node \
3620 || type1 == unsigned_short_ ## NAME ## _type_node) \
3621 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3622 : short_ ## NAME ## _type_node; \
3623 if (type1 == NAME ## _type_node \
3624 || type1 == unsigned_ ## NAME ## _type_node) \
3625 return unsignedp ? unsigned_ ## NAME ## _type_node \
3626 : NAME ## _type_node; \
3627 if (type1 == long_ ## NAME ## _type_node \
3628 || type1 == unsigned_long_ ## NAME ## _type_node) \
3629 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3630 : long_ ## NAME ## _type_node; \
3631 if (type1 == long_long_ ## NAME ## _type_node \
3632 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3633 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3634 : long_long_ ## NAME ## _type_node;
3635
3636#define C_COMMON_FIXED_MODE_TYPES(NAME) \
3637 if (type1 == NAME ## _type_node \
3638 || type1 == u ## NAME ## _type_node) \
3639 return unsignedp ? u ## NAME ## _type_node \
3640 : NAME ## _type_node;
3641
3642#define C_COMMON_FIXED_TYPES_SAT(NAME) \
3643 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3644 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3645 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3646 : sat_ ## short_ ## NAME ## _type_node; \
3647 if (type1 == sat_ ## NAME ## _type_node \
3648 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3649 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3650 : sat_ ## NAME ## _type_node; \
3651 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3652 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3653 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3654 : sat_ ## long_ ## NAME ## _type_node; \
3655 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3656 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3657 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3658 : sat_ ## long_long_ ## NAME ## _type_node;
3659
3660#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3661 if (type1 == sat_ ## NAME ## _type_node \
3662 || type1 == sat_ ## u ## NAME ## _type_node) \
3663 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3664 : sat_ ## NAME ## _type_node;
3665
3666 C_COMMON_FIXED_TYPES (fract);
3667 C_COMMON_FIXED_TYPES_SAT (fract);
3668 C_COMMON_FIXED_TYPES (accum);
3669 C_COMMON_FIXED_TYPES_SAT (accum);
3670
3671 C_COMMON_FIXED_MODE_TYPES (qq);
3672 C_COMMON_FIXED_MODE_TYPES (hq);
3673 C_COMMON_FIXED_MODE_TYPES (sq);
3674 C_COMMON_FIXED_MODE_TYPES (dq);
3675 C_COMMON_FIXED_MODE_TYPES (tq);
3676 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3677 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3678 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3679 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3680 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3681 C_COMMON_FIXED_MODE_TYPES (ha);
3682 C_COMMON_FIXED_MODE_TYPES (sa);
3683 C_COMMON_FIXED_MODE_TYPES (da);
3684 C_COMMON_FIXED_MODE_TYPES (ta);
3685 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3686 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3687 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3688 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
ab22c1fa 3689
bc15d0ef
JM
3690 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3691 the precision; they have precision set to match their range, but
3692 may use a wider mode to match an ABI. If we change modes, we may
3693 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3694 the precision as well, so as to yield correct results for
3695 bit-field types. C++ does not have these separate bit-field
3696 types, and producing a signed or unsigned variant of an
3697 ENUMERAL_TYPE may cause other problems as well. */
3698
1e204133
RAE
3699 if (!INTEGRAL_TYPE_P (type)
3700 || TYPE_UNSIGNED (type) == unsignedp)
3701 return type;
3702
bc15d0ef
JM
3703#define TYPE_OK(node) \
3704 (TYPE_MODE (type) == TYPE_MODE (node) \
41b81065 3705 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
bc15d0ef 3706 if (TYPE_OK (signed_char_type_node))
693a6128 3707 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
bc15d0ef 3708 if (TYPE_OK (integer_type_node))
693a6128 3709 return unsignedp ? unsigned_type_node : integer_type_node;
bc15d0ef 3710 if (TYPE_OK (short_integer_type_node))
693a6128 3711 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
bc15d0ef 3712 if (TYPE_OK (long_integer_type_node))
693a6128 3713 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
bc15d0ef 3714 if (TYPE_OK (long_long_integer_type_node))
693a6128
GRK
3715 return (unsignedp ? long_long_unsigned_type_node
3716 : long_long_integer_type_node);
78a7c317
DD
3717
3718 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3719 if (int_n_enabled_p[i]
3720 && TYPE_MODE (type) == int_n_data[i].m
3721 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3722 return (unsignedp ? int_n_trees[i].unsigned_type
3723 : int_n_trees[i].signed_type);
3724
bc15d0ef 3725 if (TYPE_OK (widest_integer_literal_type_node))
693a6128
GRK
3726 return (unsignedp ? widest_unsigned_literal_type_node
3727 : widest_integer_literal_type_node);
4a063bec
RH
3728
3729#if HOST_BITS_PER_WIDE_INT >= 64
bc15d0ef 3730 if (TYPE_OK (intTI_type_node))
4a063bec
RH
3731 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3732#endif
bc15d0ef 3733 if (TYPE_OK (intDI_type_node))
4a063bec 3734 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
bc15d0ef 3735 if (TYPE_OK (intSI_type_node))
4a063bec 3736 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
bc15d0ef 3737 if (TYPE_OK (intHI_type_node))
4a063bec 3738 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
bc15d0ef 3739 if (TYPE_OK (intQI_type_node))
4a063bec 3740 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
bc15d0ef 3741#undef TYPE_OK
4a063bec 3742
41b81065 3743 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
693a6128 3744}
9649812a 3745
38a4afee
MM
3746/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3747
3748tree
3749c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3750{
78a7c317
DD
3751 int i;
3752
38a4afee
MM
3753 /* Extended integer types of the same width as a standard type have
3754 lesser rank, so those of the same width as int promote to int or
3755 unsigned int and are valid for printf formats expecting int or
3756 unsigned int. To avoid such special cases, avoid creating
3757 extended integer types for bit-fields if a standard integer type
3758 is available. */
3759 if (width == TYPE_PRECISION (integer_type_node))
3760 return unsignedp ? unsigned_type_node : integer_type_node;
3761 if (width == TYPE_PRECISION (signed_char_type_node))
3762 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3763 if (width == TYPE_PRECISION (short_integer_type_node))
3764 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3765 if (width == TYPE_PRECISION (long_integer_type_node))
3766 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3767 if (width == TYPE_PRECISION (long_long_integer_type_node))
3768 return (unsignedp ? long_long_unsigned_type_node
3769 : long_long_integer_type_node);
78a7c317
DD
3770 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3771 if (int_n_enabled_p[i]
3772 && width == int_n_data[i].bitsize)
3773 return (unsignedp ? int_n_trees[i].unsigned_type
3774 : int_n_trees[i].signed_type);
38a4afee
MM
3775 return build_nonstandard_integer_type (width, unsignedp);
3776}
3777
9649812a
MM
3778/* The C version of the register_builtin_type langhook. */
3779
3780void
3781c_register_builtin_type (tree type, const char* name)
3782{
3783 tree decl;
3784
c2255bc4
AH
3785 decl = build_decl (UNKNOWN_LOCATION,
3786 TYPE_DECL, get_identifier (name), type);
9649812a
MM
3787 DECL_ARTIFICIAL (decl) = 1;
3788 if (!TYPE_NAME (type))
3789 TYPE_NAME (type) = decl;
3790 pushdecl (decl);
d1d3865f
ZW
3791
3792 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
9649812a 3793}
6acfe908 3794\f
78ef5b89 3795/* Print an error message for invalid operands to arith operation
ba47d38d 3796 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
745e411d
DM
3797 RICHLOC is a rich location for the message, containing either
3798 three separate locations for each of the operator and operands
3799
3800 lhs op rhs
3801 ~~~ ^~ ~~~
3802
3803 (C FE), or one location ranging over all over them
3804
3805 lhs op rhs
3806 ~~~~^~~~~~
3807
3808 (C++ FE). */
b30f223b
RS
3809
3810void
745e411d 3811binary_op_error (rich_location *richloc, enum tree_code code,
ba47d38d 3812 tree type0, tree type1)
b30f223b 3813{
b3694847 3814 const char *opname;
89c78d7d 3815
b30f223b
RS
3816 switch (code)
3817 {
b30f223b
RS
3818 case PLUS_EXPR:
3819 opname = "+"; break;
3820 case MINUS_EXPR:
3821 opname = "-"; break;
3822 case MULT_EXPR:
3823 opname = "*"; break;
3824 case MAX_EXPR:
3825 opname = "max"; break;
3826 case MIN_EXPR:
3827 opname = "min"; break;
3828 case EQ_EXPR:
3829 opname = "=="; break;
3830 case NE_EXPR:
3831 opname = "!="; break;
3832 case LE_EXPR:
3833 opname = "<="; break;
3834 case GE_EXPR:
3835 opname = ">="; break;
3836 case LT_EXPR:
3837 opname = "<"; break;
3838 case GT_EXPR:
3839 opname = ">"; break;
3840 case LSHIFT_EXPR:
3841 opname = "<<"; break;
3842 case RSHIFT_EXPR:
3843 opname = ">>"; break;
3844 case TRUNC_MOD_EXPR:
047de90b 3845 case FLOOR_MOD_EXPR:
b30f223b
RS
3846 opname = "%"; break;
3847 case TRUNC_DIV_EXPR:
047de90b 3848 case FLOOR_DIV_EXPR:
b30f223b
RS
3849 opname = "/"; break;
3850 case BIT_AND_EXPR:
3851 opname = "&"; break;
3852 case BIT_IOR_EXPR:
3853 opname = "|"; break;
3854 case TRUTH_ANDIF_EXPR:
3855 opname = "&&"; break;
3856 case TRUTH_ORIF_EXPR:
3857 opname = "||"; break;
3858 case BIT_XOR_EXPR:
3859 opname = "^"; break;
6d819282 3860 default:
37b2f290 3861 gcc_unreachable ();
b30f223b 3862 }
745e411d
DM
3863 error_at_rich_loc (richloc,
3864 "invalid operands to binary %s (have %qT and %qT)",
3865 opname, type0, type1);
b30f223b
RS
3866}
3867\f
50f305ca
MLI
3868/* Given an expression as a tree, return its original type. Do this
3869 by stripping any conversion that preserves the sign and precision. */
3870static tree
3871expr_original_type (tree expr)
3872{
3873 STRIP_SIGN_NOPS (expr);
3874 return TREE_TYPE (expr);
3875}
3876
b30f223b
RS
3877/* Subroutine of build_binary_op, used for comparison operations.
3878 See if the operands have both been converted from subword integer types
3879 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
3880 This function is also responsible for converting the two operands
3881 to the proper common type for comparison.
b30f223b
RS
3882
3883 The arguments of this function are all pointers to local variables
3884 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3885 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3886
393e8e8b
MP
3887 LOC is the location of the comparison.
3888
b30f223b
RS
3889 If this function returns nonzero, it means that the comparison has
3890 a constant value. What this function returns is an expression for
3891 that value. */
3892
3893tree
393e8e8b
MP
3894shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3895 tree *restype_ptr, enum tree_code *rescode_ptr)
b30f223b 3896{
b3694847 3897 tree type;
b30f223b
RS
3898 tree op0 = *op0_ptr;
3899 tree op1 = *op1_ptr;
3900 int unsignedp0, unsignedp1;
3901 int real1, real2;
3902 tree primop0, primop1;
3903 enum tree_code code = *rescode_ptr;
3904
3905 /* Throw away any conversions to wider types
3906 already present in the operands. */
3907
828fb3ba
JM
3908 primop0 = c_common_get_narrower (op0, &unsignedp0);
3909 primop1 = c_common_get_narrower (op1, &unsignedp1);
b30f223b 3910
126e6609
JJ
3911 /* If primopN is first sign-extended from primopN's precision to opN's
3912 precision, then zero-extended from opN's precision to
3913 *restype_ptr precision, shortenings might be invalid. */
3914 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3915 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3916 && !unsignedp0
3917 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3918 primop0 = op0;
3919 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3920 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3921 && !unsignedp1
3922 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3923 primop1 = op1;
3924
b30f223b
RS
3925 /* Handle the case that OP0 does not *contain* a conversion
3926 but it *requires* conversion to FINAL_TYPE. */
3927
3928 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
8df83eae 3929 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b30f223b 3930 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
8df83eae 3931 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b30f223b
RS
3932
3933 /* If one of the operands must be floated, we cannot optimize. */
3934 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3935 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3936
3937 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
3938 so value is preserved), for canonicalization. Don't do this if
3939 the second arg is 0. */
b30f223b 3940
5af6001b 3941 if (TREE_CONSTANT (primop0)
ab22c1fa
CF
3942 && !integer_zerop (primop1) && !real_zerop (primop1)
3943 && !fixed_zerop (primop1))
b30f223b 3944 {
fab27f52
MM
3945 std::swap (primop0, primop1);
3946 std::swap (op0, op1);
b30f223b
RS
3947 *op0_ptr = op0;
3948 *op1_ptr = op1;
fab27f52
MM
3949 std::swap (unsignedp0, unsignedp1);
3950 std::swap (real1, real2);
b30f223b
RS
3951
3952 switch (code)
3953 {
3954 case LT_EXPR:
3955 code = GT_EXPR;
3956 break;
3957 case GT_EXPR:
3958 code = LT_EXPR;
3959 break;
3960 case LE_EXPR:
3961 code = GE_EXPR;
3962 break;
3963 case GE_EXPR:
3964 code = LE_EXPR;
3965 break;
6d819282
MK
3966 default:
3967 break;
b30f223b
RS
3968 }
3969 *rescode_ptr = code;
3970 }
3971
3972 /* If comparing an integer against a constant more bits wide,
3973 maybe we can deduce a value of 1 or 0 independent of the data.
3974 Or else truncate the constant now
3975 rather than extend the variable at run time.
3976
3977 This is only interesting if the constant is the wider arg.
3978 Also, it is not safe if the constant is unsigned and the
3979 variable arg is signed, since in this case the variable
3980 would be sign-extended and then regarded as unsigned.
3981 Our technique fails in this case because the lowest/highest
3982 possible unsigned results don't follow naturally from the
3983 lowest/highest possible values of the variable operand.
3984 For just EQ_EXPR and NE_EXPR there is another technique that
3985 could be used: see if the constant can be faithfully represented
3986 in the other operand's type, by truncating it and reextending it
3987 and see if that preserves the constant's value. */
3988
3989 if (!real1 && !real2
ab22c1fa 3990 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b30f223b
RS
3991 && TREE_CODE (primop1) == INTEGER_CST
3992 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3993 {
3994 int min_gt, max_gt, min_lt, max_lt;
3995 tree maxval, minval;
3996 /* 1 if comparison is nominally unsigned. */
8df83eae 3997 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b30f223b
RS
3998 tree val;
3999
ceef8ce4
NB
4000 type = c_common_signed_or_unsigned_type (unsignedp0,
4001 TREE_TYPE (primop0));
8bbd5685 4002
b30f223b
RS
4003 maxval = TYPE_MAX_VALUE (type);
4004 minval = TYPE_MIN_VALUE (type);
4005
4006 if (unsignedp && !unsignedp0)
ceef8ce4 4007 *restype_ptr = c_common_signed_type (*restype_ptr);
b30f223b
RS
4008
4009 if (TREE_TYPE (primop1) != *restype_ptr)
fae1b38d 4010 {
af9c6659
NS
4011 /* Convert primop1 to target type, but do not introduce
4012 additional overflow. We know primop1 is an int_cst. */
807e902e
KZ
4013 primop1 = force_fit_type (*restype_ptr,
4014 wide_int::from
4015 (primop1,
4016 TYPE_PRECISION (*restype_ptr),
4017 TYPE_SIGN (TREE_TYPE (primop1))),
4018 0, TREE_OVERFLOW (primop1));
fae1b38d 4019 }
b30f223b
RS
4020 if (type != *restype_ptr)
4021 {
4022 minval = convert (*restype_ptr, minval);
4023 maxval = convert (*restype_ptr, maxval);
4024 }
4025
807e902e
KZ
4026 min_gt = tree_int_cst_lt (primop1, minval);
4027 max_gt = tree_int_cst_lt (primop1, maxval);
4028 min_lt = tree_int_cst_lt (minval, primop1);
4029 max_lt = tree_int_cst_lt (maxval, primop1);
b30f223b
RS
4030
4031 val = 0;
4032 /* This used to be a switch, but Genix compiler can't handle that. */
4033 if (code == NE_EXPR)
4034 {
4035 if (max_lt || min_gt)
de7df9eb 4036 val = truthvalue_true_node;
b30f223b
RS
4037 }
4038 else if (code == EQ_EXPR)
4039 {
4040 if (max_lt || min_gt)
de7df9eb 4041 val = truthvalue_false_node;
b30f223b
RS
4042 }
4043 else if (code == LT_EXPR)
4044 {
4045 if (max_lt)
de7df9eb 4046 val = truthvalue_true_node;
b30f223b 4047 if (!min_lt)
de7df9eb 4048 val = truthvalue_false_node;
b30f223b
RS
4049 }
4050 else if (code == GT_EXPR)
4051 {
4052 if (min_gt)
de7df9eb 4053 val = truthvalue_true_node;
b30f223b 4054 if (!max_gt)
de7df9eb 4055 val = truthvalue_false_node;
b30f223b
RS
4056 }
4057 else if (code == LE_EXPR)
4058 {
4059 if (!max_gt)
de7df9eb 4060 val = truthvalue_true_node;
b30f223b 4061 if (min_gt)
de7df9eb 4062 val = truthvalue_false_node;
b30f223b
RS
4063 }
4064 else if (code == GE_EXPR)
4065 {
4066 if (!min_lt)
de7df9eb 4067 val = truthvalue_true_node;
b30f223b 4068 if (max_lt)
de7df9eb 4069 val = truthvalue_false_node;
b30f223b
RS
4070 }
4071
4072 /* If primop0 was sign-extended and unsigned comparison specd,
4073 we did a signed comparison above using the signed type bounds.
4074 But the comparison we output must be unsigned.
4075
4076 Also, for inequalities, VAL is no good; but if the signed
4077 comparison had *any* fixed result, it follows that the
4078 unsigned comparison just tests the sign in reverse
4079 (positive values are LE, negative ones GE).
4080 So we can generate an unsigned comparison
4081 against an extreme value of the signed type. */
4082
4083 if (unsignedp && !unsignedp0)
4084 {
4085 if (val != 0)
4086 switch (code)
4087 {
4088 case LT_EXPR:
4089 case GE_EXPR:
4090 primop1 = TYPE_MIN_VALUE (type);
4091 val = 0;
4092 break;
4093
4094 case LE_EXPR:
4095 case GT_EXPR:
4096 primop1 = TYPE_MAX_VALUE (type);
4097 val = 0;
4098 break;
6d819282
MK
4099
4100 default:
4101 break;
b30f223b 4102 }
12753674 4103 type = c_common_unsigned_type (type);
b30f223b
RS
4104 }
4105
d0eccfcd 4106 if (TREE_CODE (primop0) != INTEGER_CST
269adb9d
JM
4107 /* Don't warn if it's from a (non-system) macro. */
4108 && !(from_macro_expansion_at
4109 (expansion_point_location_if_in_system_header
4110 (EXPR_LOCATION (primop0)))))
b30f223b 4111 {
de7df9eb 4112 if (val == truthvalue_false_node)
50f305ca
MLI
4113 warning_at (loc, OPT_Wtype_limits,
4114 "comparison is always false due to limited range of data type");
de7df9eb 4115 if (val == truthvalue_true_node)
50f305ca
MLI
4116 warning_at (loc, OPT_Wtype_limits,
4117 "comparison is always true due to limited range of data type");
b30f223b
RS
4118 }
4119
4120 if (val != 0)
4121 {
4122 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4123 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3 4124 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b30f223b
RS
4125 return val;
4126 }
4127
4128 /* Value is not predetermined, but do the comparison
4129 in the type of the operand that is not constant.
4130 TYPE is already properly set. */
4131 }
9a8ce21f
JG
4132
4133 /* If either arg is decimal float and the other is float, find the
4134 proper common type to use for comparison. */
6f450181
RB
4135 else if (real1 && real2
4136 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4137 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4138 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4139
4140 /* If either arg is decimal float and the other is float, fail. */
9a8ce21f
JG
4141 else if (real1 && real2
4142 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4143 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
6f450181 4144 return 0;
9a8ce21f 4145
b30f223b 4146 else if (real1 && real2
766f6c30
RS
4147 && (TYPE_PRECISION (TREE_TYPE (primop0))
4148 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
4149 type = TREE_TYPE (primop0);
4150
4151 /* If args' natural types are both narrower than nominal type
4152 and both extend in the same manner, compare them
4153 in the type of the wider arg.
4154 Otherwise must actually extend both to the nominal
4155 common type lest different ways of extending
4156 alter the result.
4157 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4158
4159 else if (unsignedp0 == unsignedp1 && real1 == real2
4160 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4161 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4162 {
4163 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
ceef8ce4 4164 type = c_common_signed_or_unsigned_type (unsignedp0
8df83eae 4165 || TYPE_UNSIGNED (*restype_ptr),
ceef8ce4 4166 type);
b30f223b
RS
4167 /* Make sure shorter operand is extended the right way
4168 to match the longer operand. */
ceef8ce4
NB
4169 primop0
4170 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4171 TREE_TYPE (primop0)),
4172 primop0);
4173 primop1
4174 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4175 TREE_TYPE (primop1)),
4176 primop1);
b30f223b
RS
4177 }
4178 else
4179 {
4180 /* Here we must do the comparison on the nominal type
4181 using the args exactly as we received them. */
4182 type = *restype_ptr;
4183 primop0 = op0;
4184 primop1 = op1;
4185
4186 if (!real1 && !real2 && integer_zerop (primop1)
8df83eae 4187 && TYPE_UNSIGNED (*restype_ptr))
b30f223b
RS
4188 {
4189 tree value = 0;
50f305ca
MLI
4190 /* All unsigned values are >= 0, so we warn. However,
4191 if OP0 is a constant that is >= 0, the signedness of
4192 the comparison isn't an issue, so suppress the
4193 warning. */
4194 bool warn =
8400e75e 4195 warn_type_limits && !in_system_header_at (loc)
50f305ca
MLI
4196 && !(TREE_CODE (primop0) == INTEGER_CST
4197 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4198 primop0)))
4199 /* Do not warn for enumeration types. */
4200 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4201
b30f223b
RS
4202 switch (code)
4203 {
4204 case GE_EXPR:
50f305ca
MLI
4205 if (warn)
4206 warning_at (loc, OPT_Wtype_limits,
4207 "comparison of unsigned expression >= 0 is always true");
de7df9eb 4208 value = truthvalue_true_node;
b30f223b
RS
4209 break;
4210
4211 case LT_EXPR:
50f305ca
MLI
4212 if (warn)
4213 warning_at (loc, OPT_Wtype_limits,
4214 "comparison of unsigned expression < 0 is always false");
de7df9eb 4215 value = truthvalue_false_node;
6d819282
MK
4216 break;
4217
4218 default:
4219 break;
b30f223b
RS
4220 }
4221
4222 if (value != 0)
4223 {
4224 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4225 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3
RS
4226 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4227 primop0, value);
b30f223b
RS
4228 return value;
4229 }
4230 }
4231 }
4232
4233 *op0_ptr = convert (type, primop0);
4234 *op1_ptr = convert (type, primop1);
4235
de7df9eb 4236 *restype_ptr = truthvalue_type_node;
b30f223b
RS
4237
4238 return 0;
4239}
4240\f
7552da58
JJ
4241/* Return a tree for the sum or difference (RESULTCODE says which)
4242 of pointer PTROP and integer INTOP. */
4243
4244tree
db3927fb 4245pointer_int_sum (location_t loc, enum tree_code resultcode,
fd9b0f32 4246 tree ptrop, tree intop, bool complain)
7552da58 4247{
6ac01510 4248 tree size_exp, ret;
7552da58 4249
7552da58 4250 /* The result is a pointer of the same type that is being added. */
7552da58
JJ
4251 tree result_type = TREE_TYPE (ptrop);
4252
4253 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4254 {
fd9b0f32
PC
4255 if (complain && warn_pointer_arith)
4256 pedwarn (loc, OPT_Wpointer_arith,
4257 "pointer of type %<void *%> used in arithmetic");
4258 else if (!complain)
4259 return error_mark_node;
7552da58
JJ
4260 size_exp = integer_one_node;
4261 }
4262 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4263 {
fd9b0f32
PC
4264 if (complain && warn_pointer_arith)
4265 pedwarn (loc, OPT_Wpointer_arith,
4266 "pointer to a function used in arithmetic");
4267 else if (!complain)
4268 return error_mark_node;
7552da58
JJ
4269 size_exp = integer_one_node;
4270 }
7552da58
JJ
4271 else
4272 size_exp = size_in_bytes (TREE_TYPE (result_type));
4273
6ac01510
ILT
4274 /* We are manipulating pointer values, so we don't need to warn
4275 about relying on undefined signed overflow. We disable the
4276 warning here because we use integer types so fold won't know that
4277 they are really pointers. */
4278 fold_defer_overflow_warnings ();
4279
7552da58
JJ
4280 /* If what we are about to multiply by the size of the elements
4281 contains a constant term, apply distributive law
4282 and multiply that constant term separately.
4283 This helps produce common subexpressions. */
7552da58 4284 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3f75a254 4285 && !TREE_CONSTANT (intop)
7552da58
JJ
4286 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4287 && TREE_CONSTANT (size_exp)
4288 /* If the constant comes from pointer subtraction,
4289 skip this optimization--it would cause an error. */
4290 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4291 /* If the constant is unsigned, and smaller than the pointer size,
4292 then we must skip this optimization. This is because it could cause
4293 an overflow error if the constant is negative but INTOP is not. */
3f75a254 4294 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
7552da58
JJ
4295 || (TYPE_PRECISION (TREE_TYPE (intop))
4296 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4297 {
4298 enum tree_code subcode = resultcode;
4299 tree int_type = TREE_TYPE (intop);
4300 if (TREE_CODE (intop) == MINUS_EXPR)
4301 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4302 /* Convert both subexpression types to the type of intop,
4303 because weird cases involving pointer arithmetic
4304 can result in a sum or difference with different type args. */
ba47d38d
AH
4305 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4306 subcode, ptrop,
7552da58
JJ
4307 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4308 intop = convert (int_type, TREE_OPERAND (intop, 0));
4309 }
4310
4311 /* Convert the integer argument to a type the same size as sizetype
4312 so the multiply won't overflow spuriously. */
7552da58 4313 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
8df83eae 4314 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
35b1a6fa 4315 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
8df83eae 4316 TYPE_UNSIGNED (sizetype)), intop);
7552da58
JJ
4317
4318 /* Replace the integer argument with a suitable product by the object size.
9e9ef331 4319 Do this multiplication as signed, then convert to the appropriate type
65de6659 4320 for the pointer operation and disregard an overflow that occurred only
9e9ef331
EB
4321 because of the sign-extension change in the latter conversion. */
4322 {
d90ec4f2
JM
4323 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4324 convert (TREE_TYPE (intop), size_exp));
9e9ef331
EB
4325 intop = convert (sizetype, t);
4326 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
807e902e 4327 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
9e9ef331 4328 }
5be014d5 4329
280f1ffa 4330 /* Create the sum or difference. */
5be014d5 4331 if (resultcode == MINUS_EXPR)
db3927fb 4332 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
7552da58 4333
5d49b6a7 4334 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
6ac01510
ILT
4335
4336 fold_undefer_and_ignore_overflow_warnings ();
4337
4338 return ret;
7552da58
JJ
4339}
4340\f
e5a94231
JM
4341/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4342 and if NON_CONST is known not to be permitted in an evaluated part
4343 of a constant expression. */
4344
4345tree
4346c_wrap_maybe_const (tree expr, bool non_const)
4347{
4348 bool nowarning = TREE_NO_WARNING (expr);
4349 location_t loc = EXPR_LOCATION (expr);
4350
4351 /* This should never be called for C++. */
4352 if (c_dialect_cxx ())
4353 gcc_unreachable ();
4354
4355 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4356 STRIP_TYPE_NOPS (expr);
4357 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4358 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4359 if (nowarning)
4360 TREE_NO_WARNING (expr) = 1;
4361 protected_set_expr_location (expr, loc);
4362
4363 return expr;
4364}
4365
928c19bb
JM
4366/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4367 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4368 around the SAVE_EXPR if needed so that c_fully_fold does not need
4369 to look inside SAVE_EXPRs. */
4370
4371tree
4372c_save_expr (tree expr)
4373{
4374 bool maybe_const = true;
4375 if (c_dialect_cxx ())
4376 return save_expr (expr);
4377 expr = c_fully_fold (expr, false, &maybe_const);
4378 expr = save_expr (expr);
4379 if (!maybe_const)
e5a94231 4380 expr = c_wrap_maybe_const (expr, true);
928c19bb
JM
4381 return expr;
4382}
4383
b3c6d2ea
ILT
4384/* Return whether EXPR is a declaration whose address can never be
4385 NULL. */
4386
4387bool
58f9752a 4388decl_with_nonnull_addr_p (const_tree expr)
b3c6d2ea
ILT
4389{
4390 return (DECL_P (expr)
4391 && (TREE_CODE (expr) == PARM_DECL
4392 || TREE_CODE (expr) == LABEL_DECL
4393 || !DECL_WEAK (expr)));
4394}
4395
b30f223b 4396/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
85498824
JM
4397 or for an `if' or `while' statement or ?..: exp. It should already
4398 have been validated to be of suitable type; otherwise, a bad
4399 diagnostic may result.
b30f223b 4400
ba47d38d
AH
4401 The EXPR is located at LOCATION.
4402
b30f223b
RS
4403 This preparation consists of taking the ordinary
4404 representation of an expression expr and producing a valid tree
4405 boolean expression describing whether expr is nonzero. We could
de7df9eb 4406 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b30f223b
RS
4407 but we optimize comparisons, &&, ||, and !.
4408
de7df9eb 4409 The resulting type should always be `truthvalue_type_node'. */
b30f223b
RS
4410
4411tree
ba47d38d 4412c_common_truthvalue_conversion (location_t location, tree expr)
b30f223b 4413{
b30f223b
RS
4414 switch (TREE_CODE (expr))
4415 {
d1a7edaf 4416 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
6f312d18
ZW
4417 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4418 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4419 case ORDERED_EXPR: case UNORDERED_EXPR:
90ec750d
RS
4420 if (TREE_TYPE (expr) == truthvalue_type_node)
4421 return expr;
c2255bc4 4422 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
90ec750d 4423 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
c2255bc4 4424 goto ret;
90ec750d 4425
b30f223b
RS
4426 case TRUTH_ANDIF_EXPR:
4427 case TRUTH_ORIF_EXPR:
4428 case TRUTH_AND_EXPR:
4429 case TRUTH_OR_EXPR:
9379fac9 4430 case TRUTH_XOR_EXPR:
90ec750d
RS
4431 if (TREE_TYPE (expr) == truthvalue_type_node)
4432 return expr;
c2255bc4 4433 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
b8698a0f 4434 c_common_truthvalue_conversion (location,
c2255bc4
AH
4435 TREE_OPERAND (expr, 0)),
4436 c_common_truthvalue_conversion (location,
4437 TREE_OPERAND (expr, 1)));
4438 goto ret;
18c0f675 4439
18d00205 4440 case TRUTH_NOT_EXPR:
90ec750d
RS
4441 if (TREE_TYPE (expr) == truthvalue_type_node)
4442 return expr;
c2255bc4
AH
4443 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4444 c_common_truthvalue_conversion (location,
4445 TREE_OPERAND (expr, 0)));
4446 goto ret;
18d00205 4447
b30f223b
RS
4448 case ERROR_MARK:
4449 return expr;
4450
4451 case INTEGER_CST:
d95787e6
RS
4452 return integer_zerop (expr) ? truthvalue_false_node
4453 : truthvalue_true_node;
b30f223b
RS
4454
4455 case REAL_CST:
010c4d9c
RS
4456 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4457 ? truthvalue_true_node
4458 : truthvalue_false_node;
b30f223b 4459
ab22c1fa
CF
4460 case FIXED_CST:
4461 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4462 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4463 ? truthvalue_true_node
4464 : truthvalue_false_node;
4465
90ec750d 4466 case FUNCTION_DECL:
c9f9eb5d 4467 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
90ec750d
RS
4468 /* Fall through. */
4469
b30f223b 4470 case ADDR_EXPR:
1998463c 4471 {
f6f08360 4472 tree inner = TREE_OPERAND (expr, 0);
b3c6d2ea 4473 if (decl_with_nonnull_addr_p (inner))
1998463c 4474 {
b3c6d2ea 4475 /* Common Ada/Pascal programmer's mistake. */
ba47d38d
AH
4476 warning_at (location,
4477 OPT_Waddress,
4478 "the address of %qD will always evaluate as %<true%>",
4479 inner);
1998463c
SB
4480 return truthvalue_true_node;
4481 }
33766b66 4482 break;
1998463c 4483 }
b30f223b 4484
766f6c30 4485 case COMPLEX_EXPR:
c2255bc4 4486 expr = build_binary_op (EXPR_LOCATION (expr),
ba47d38d 4487 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 4488 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
ba47d38d
AH
4489 c_common_truthvalue_conversion (location,
4490 TREE_OPERAND (expr, 0)),
4491 c_common_truthvalue_conversion (location,
4492 TREE_OPERAND (expr, 1)),
766f6c30 4493 0);
c2255bc4 4494 goto ret;
766f6c30 4495
b30f223b
RS
4496 case NEGATE_EXPR:
4497 case ABS_EXPR:
4498 case FLOAT_EXPR:
8ce94e44 4499 case EXCESS_PRECISION_EXPR:
da7d8304 4500 /* These don't change whether an object is nonzero or zero. */
ba47d38d 4501 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b30f223b
RS
4502
4503 case LROTATE_EXPR:
4504 case RROTATE_EXPR:
da7d8304 4505 /* These don't change whether an object is zero or nonzero, but
b30f223b
RS
4506 we can't ignore them if their second arg has side-effects. */
4507 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
c2255bc4
AH
4508 {
4509 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4510 TREE_OPERAND (expr, 1),
b8698a0f 4511 c_common_truthvalue_conversion
c2255bc4
AH
4512 (location, TREE_OPERAND (expr, 0)));
4513 goto ret;
4514 }
b30f223b 4515 else
ba47d38d
AH
4516 return c_common_truthvalue_conversion (location,
4517 TREE_OPERAND (expr, 0));
b57062ca 4518
b30f223b
RS
4519 case COND_EXPR:
4520 /* Distribute the conversion into the arms of a COND_EXPR. */
928c19bb 4521 if (c_dialect_cxx ())
c2255bc4 4522 {
4cc4f2f4
JJ
4523 tree op1 = TREE_OPERAND (expr, 1);
4524 tree op2 = TREE_OPERAND (expr, 2);
4525 /* In C++ one of the arms might have void type if it is throw. */
4526 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4527 op1 = c_common_truthvalue_conversion (location, op1);
4528 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4529 op2 = c_common_truthvalue_conversion (location, op2);
db3927fb 4530 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4cc4f2f4 4531 TREE_OPERAND (expr, 0), op1, op2);
c2255bc4
AH
4532 goto ret;
4533 }
928c19bb 4534 else
c2255bc4
AH
4535 {
4536 /* Folding will happen later for C. */
4537 expr = build3 (COND_EXPR, truthvalue_type_node,
4538 TREE_OPERAND (expr, 0),
4539 c_common_truthvalue_conversion (location,
4540 TREE_OPERAND (expr, 1)),
4541 c_common_truthvalue_conversion (location,
4542 TREE_OPERAND (expr, 2)));
4543 goto ret;
4544 }
b30f223b 4545
1043771b 4546 CASE_CONVERT:
1ee44b26
JM
4547 {
4548 tree totype = TREE_TYPE (expr);
4549 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4550
076fecad
PP
4551 if (POINTER_TYPE_P (totype)
4552 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4553 {
4554 tree inner = expr;
4555 STRIP_NOPS (inner);
4556
4557 if (DECL_P (inner))
4558 warning_at (location,
4559 OPT_Waddress,
4560 "the compiler can assume that the address of "
4561 "%qD will always evaluate to %<true%>",
4562 inner);
4563 }
4564
1ee44b26
JM
4565 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4566 since that affects how `default_conversion' will behave. */
4567 if (TREE_CODE (totype) == REFERENCE_TYPE
4568 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4569 break;
4570 /* Don't strip a conversion from C++0x scoped enum, since they
4571 don't implicitly convert to other types. */
4572 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4573 && ENUM_IS_SCOPED (fromtype))
4574 break;
4575 /* If this isn't narrowing the argument, we can ignore it. */
4576 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4577 return c_common_truthvalue_conversion (location,
4578 TREE_OPERAND (expr, 0));
4579 }
b30f223b
RS
4580 break;
4581
e2aab13d 4582 case MODIFY_EXPR:
fbc8d2d3
ILT
4583 if (!TREE_NO_WARNING (expr)
4584 && warn_parentheses)
4585 {
4586 warning (OPT_Wparentheses,
4587 "suggest parentheses around assignment used as truth value");
4588 TREE_NO_WARNING (expr) = 1;
4589 }
e2aab13d 4590 break;
b57062ca 4591
6d819282
MK
4592 default:
4593 break;
b30f223b
RS
4594 }
4595
f0b996c5 4596 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa 4597 {
5386338c 4598 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
c2255bc4 4599 expr = (build_binary_op
ba47d38d
AH
4600 (EXPR_LOCATION (expr),
4601 (TREE_SIDE_EFFECTS (expr)
f0b8d9aa 4602 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
c9f9eb5d
AH
4603 c_common_truthvalue_conversion
4604 (location,
4605 build_unary_op (location, REALPART_EXPR, t, 0)),
4606 c_common_truthvalue_conversion
4607 (location,
4608 build_unary_op (location, IMAGPART_EXPR, t, 0)),
f0b8d9aa 4609 0));
c2255bc4 4610 goto ret;
f0b8d9aa 4611 }
f0b996c5 4612
ab22c1fa
CF
4613 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4614 {
4615 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4616 FCONST0 (TYPE_MODE
4617 (TREE_TYPE (expr))));
ca80e52b 4618 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
ab22c1fa 4619 }
c2255bc4
AH
4620 else
4621 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
ab22c1fa 4622
c2255bc4
AH
4623 ret:
4624 protected_set_expr_location (expr, location);
4625 return expr;
b30f223b
RS
4626}
4627\f
9bc15050
RG
4628static void def_builtin_1 (enum built_in_function fncode,
4629 const char *name,
4630 enum built_in_class fnclass,
4631 tree fntype, tree libtype,
4632 bool both_p, bool fallback_p, bool nonansi_p,
4633 tree fnattrs, bool implicit_p);
fc2aaf30 4634
3932261a
MM
4635
4636/* Apply the TYPE_QUALS to the new DECL. */
4637
4638void
35b1a6fa 4639c_apply_type_quals_to_decl (int type_quals, tree decl)
3932261a 4640{
4b011bbf 4641 tree type = TREE_TYPE (decl);
9f63daea 4642
5a6159dd
AP
4643 if (type == error_mark_node)
4644 return;
4b011bbf 4645
329af3c7
JM
4646 if ((type_quals & TYPE_QUAL_CONST)
4647 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4648 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4649 constructor can produce constant init, so rely on cp_finish_decl to
4650 clear TREE_READONLY if the variable has non-constant init. */
3932261a
MM
4651 TREE_READONLY (decl) = 1;
4652 if (type_quals & TYPE_QUAL_VOLATILE)
4653 {
4654 TREE_SIDE_EFFECTS (decl) = 1;
4655 TREE_THIS_VOLATILE (decl) = 1;
4656 }
6946bc60 4657 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 4658 {
4b011bbf
JM
4659 while (type && TREE_CODE (type) == ARRAY_TYPE)
4660 /* Allow 'restrict' on arrays of pointers.
4661 FIXME currently we just ignore it. */
4662 type = TREE_TYPE (type);
4663 if (!type
4664 || !POINTER_TYPE_P (type)
4665 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
bda67431 4666 error ("invalid use of %<restrict%>");
3932261a
MM
4667 }
4668}
4669
ca752f39 4670struct c_type_hasher : ggc_ptr_hash<tree_node>
2a22f99c
TS
4671{
4672 static hashval_t hash (tree);
4673 static bool equal (tree, tree);
4674};
4675
6de9cd9a
DN
4676/* Hash function for the problem of multiple type definitions in
4677 different files. This must hash all types that will compare
4678 equal via comptypes to the same value. In practice it hashes
9cf737f8 4679 on some of the simple stuff and leaves the details to comptypes. */
6de9cd9a 4680
2a22f99c
TS
4681hashval_t
4682c_type_hasher::hash (tree t)
6de9cd9a 4683{
a19e4d44 4684 int n_elements;
6de9cd9a 4685 int shift, size;
6de9cd9a
DN
4686 tree t2;
4687 switch (TREE_CODE (t))
4688 {
8c27b7d4 4689 /* For pointers, hash on pointee type plus some swizzling. */
325c3691 4690 case POINTER_TYPE:
2a22f99c 4691 return hash (TREE_TYPE (t)) ^ 0x3003003;
325c3691
RH
4692 /* Hash on number of elements and total size. */
4693 case ENUMERAL_TYPE:
4694 shift = 3;
4695 t2 = TYPE_VALUES (t);
4696 break;
4697 case RECORD_TYPE:
4698 shift = 0;
4699 t2 = TYPE_FIELDS (t);
4700 break;
4701 case QUAL_UNION_TYPE:
4702 shift = 1;
4703 t2 = TYPE_FIELDS (t);
4704 break;
4705 case UNION_TYPE:
4706 shift = 2;
4707 t2 = TYPE_FIELDS (t);
4708 break;
4709 default:
366de0ce 4710 gcc_unreachable ();
6de9cd9a 4711 }
a19e4d44
NF
4712 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4713 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4714 n_elements = list_length (t2);
6fc3c3c0
TT
4715 /* We might have a VLA here. */
4716 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4717 size = 0;
4718 else
4719 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
a19e4d44 4720 return ((size << 24) | (n_elements << shift));
6de9cd9a
DN
4721}
4722
2a22f99c
TS
4723bool
4724c_type_hasher::equal (tree t1, tree t2)
4725{
4726 return lang_hooks.types_compatible_p (t1, t2);
4727}
4728
4729static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4fe52ce9 4730
41472af8 4731/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 4732 or a type. Return -1 if we don't do anything special. */
41472af8 4733
4862826d 4734alias_set_type
35b1a6fa 4735c_common_get_alias_set (tree t)
41472af8 4736{
08bc2431 4737 tree u;
35b1a6fa 4738
cb9c2485
JM
4739 /* For VLAs, use the alias set of the element type rather than the
4740 default of alias set 0 for types compared structurally. */
4741 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4742 {
4743 if (TREE_CODE (t) == ARRAY_TYPE)
4744 return get_alias_set (TREE_TYPE (t));
4745 return -1;
4746 }
4747
08bc2431
MM
4748 /* Permit type-punning when accessing a union, provided the access
4749 is directly through the union. For example, this code does not
4750 permit taking the address of a union member and then storing
4751 through it. Even the type-punning allowed here is a GCC
4752 extension, albeit a common and useful one; the C standard says
4753 that such accesses have implementation-defined behavior. */
4754 for (u = t;
4755 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4756 u = TREE_OPERAND (u, 0))
4757 if (TREE_CODE (u) == COMPONENT_REF
4758 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4759 return 0;
ece32014 4760
74d86f4f 4761 /* That's all the expressions we handle specially. */
3f75a254 4762 if (!TYPE_P (t))
74d86f4f
RH
4763 return -1;
4764
95bd1dd7 4765 /* The C standard guarantees that any object may be accessed via an
74d86f4f
RH
4766 lvalue that has character type. */
4767 if (t == char_type_node
4768 || t == signed_char_type_node
4769 || t == unsigned_char_type_node)
3bdf5ad1 4770 return 0;
3932261a 4771
f824e5c3
RK
4772 /* The C standard specifically allows aliasing between signed and
4773 unsigned variants of the same type. We treat the signed
4774 variant as canonical. */
8df83eae 4775 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
8f215dce 4776 {
ceef8ce4 4777 tree t1 = c_common_signed_type (t);
f824e5c3 4778
8f215dce
JJ
4779 /* t1 == t can happen for boolean nodes which are always unsigned. */
4780 if (t1 != t)
4781 return get_alias_set (t1);
4782 }
ece32014 4783
6de9cd9a
DN
4784 /* Handle the case of multiple type nodes referring to "the same" type,
4785 which occurs with IMA. These share an alias set. FIXME: Currently only
4786 C90 is handled. (In C99 type compatibility is not transitive, which
4787 complicates things mightily. The alias set splay trees can theoretically
4788 represent this, but insertion is tricky when you consider all the
4789 different orders things might arrive in.) */
4790
4791 if (c_language != clk_c || flag_isoc99)
4792 return -1;
4793
9cf737f8 4794 /* Save time if there's only one input file. */
d974312d 4795 if (num_in_fnames == 1)
6de9cd9a
DN
4796 return -1;
4797
4798 /* Pointers need special handling if they point to any type that
4799 needs special handling (below). */
4800 if (TREE_CODE (t) == POINTER_TYPE)
4801 {
4802 tree t2;
4803 /* Find bottom type under any nested POINTERs. */
9f63daea 4804 for (t2 = TREE_TYPE (t);
762f7d9d
TT
4805 TREE_CODE (t2) == POINTER_TYPE;
4806 t2 = TREE_TYPE (t2))
4807 ;
a868811e
MP
4808 if (!RECORD_OR_UNION_TYPE_P (t2)
4809 && TREE_CODE (t2) != ENUMERAL_TYPE)
762f7d9d 4810 return -1;
6de9cd9a 4811 if (TYPE_SIZE (t2) == 0)
762f7d9d 4812 return -1;
6de9cd9a
DN
4813 }
4814 /* These are the only cases that need special handling. */
a868811e 4815 if (!RECORD_OR_UNION_TYPE_P (t)
6de9cd9a 4816 && TREE_CODE (t) != ENUMERAL_TYPE
6de9cd9a
DN
4817 && TREE_CODE (t) != POINTER_TYPE)
4818 return -1;
4819 /* Undefined? */
4820 if (TYPE_SIZE (t) == 0)
4821 return -1;
4822
9f63daea 4823 /* Look up t in hash table. Only one of the compatible types within each
6de9cd9a
DN
4824 alias set is recorded in the table. */
4825 if (!type_hash_table)
2a22f99c
TS
4826 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4827 tree *slot = type_hash_table->find_slot (t, INSERT);
6de9cd9a 4828 if (*slot != NULL)
6a3203c8
AP
4829 {
4830 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4831 return TYPE_ALIAS_SET ((tree)*slot);
4832 }
6de9cd9a
DN
4833 else
4834 /* Our caller will assign and record (in t) a new alias set; all we need
4835 to do is remember t in the hash table. */
4836 *slot = t;
4837
3bdf5ad1 4838 return -1;
41472af8 4839}
0213a355 4840\f
c2255bc4 4841/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
296674db 4842 the IS_SIZEOF parameter indicates which operator is being applied.
c2255bc4
AH
4843 The COMPLAIN flag controls whether we should diagnose possibly
4844 ill-formed constructs or not. LOC is the location of the SIZEOF or
296674db
JM
4845 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4846 a type in any context should be returned, rather than the normal
4847 alignment for that type. */
03a08664 4848
0213a355 4849tree
c2255bc4 4850c_sizeof_or_alignof_type (location_t loc,
296674db
JM
4851 tree type, bool is_sizeof, bool min_alignof,
4852 int complain)
0213a355 4853{
fa72b064
GDR
4854 const char *op_name;
4855 tree value = NULL;
4856 enum tree_code type_code = TREE_CODE (type);
35b1a6fa 4857
03a08664 4858 op_name = is_sizeof ? "sizeof" : "__alignof__";
35b1a6fa 4859
fa72b064 4860 if (type_code == FUNCTION_TYPE)
0213a355 4861 {
03a08664 4862 if (is_sizeof)
fa72b064 4863 {
44d90fe1
PC
4864 if (complain && warn_pointer_arith)
4865 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 4866 "invalid application of %<sizeof%> to a function type");
5ade1ed2
DG
4867 else if (!complain)
4868 return error_mark_node;
fa72b064
GDR
4869 value = size_one_node;
4870 }
4871 else
d19fa6b5
JM
4872 {
4873 if (complain)
4874 {
4875 if (c_dialect_cxx ())
c1771a20 4876 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
d19fa6b5
JM
4877 "%<alignof%> applied to a function type");
4878 else
c1771a20 4879 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
d19fa6b5
JM
4880 "%<_Alignof%> applied to a function type");
4881 }
4882 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4883 }
fa72b064
GDR
4884 }
4885 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4886 {
35b1a6fa 4887 if (type_code == VOID_TYPE
44d90fe1
PC
4888 && complain && warn_pointer_arith)
4889 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 4890 "invalid application of %qs to a void type", op_name);
5ade1ed2
DG
4891 else if (!complain)
4892 return error_mark_node;
fa72b064 4893 value = size_one_node;
0213a355 4894 }
73ac190a
PC
4895 else if (!COMPLETE_TYPE_P (type)
4896 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
0213a355 4897 {
ea793912 4898 if (complain)
73ac190a 4899 error_at (loc, "invalid application of %qs to incomplete type %qT",
c2255bc4 4900 op_name, type);
cb6addf4 4901 return error_mark_node;
0213a355 4902 }
73ac190a
PC
4903 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4904 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4905 {
4906 if (complain)
4907 error_at (loc, "invalid application of %qs to array type %qT of "
4908 "incomplete element type", op_name, type);
4909 return error_mark_node;
4910 }
0213a355 4911 else
fa72b064 4912 {
03a08664 4913 if (is_sizeof)
fa72b064 4914 /* Convert in case a char is more than one unit. */
db3927fb
AH
4915 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4916 size_int (TYPE_PRECISION (char_type_node)
4917 / BITS_PER_UNIT));
296674db 4918 else if (min_alignof)
2793eeab 4919 value = size_int (min_align_of_type (type));
fa72b064 4920 else
a4e9ffe5 4921 value = size_int (TYPE_ALIGN_UNIT (type));
fa72b064 4922 }
0213a355 4923
3ac8781c
RG
4924 /* VALUE will have the middle-end integer type sizetype.
4925 However, we should really return a value of type `size_t',
4926 which is just a typedef for an ordinary integer type. */
db3927fb 4927 value = fold_convert_loc (loc, size_type_node, value);
35b1a6fa 4928
fa72b064 4929 return value;
0213a355
JM
4930}
4931
4932/* Implement the __alignof keyword: Return the minimum required
837edd5f
GK
4933 alignment of EXPR, measured in bytes. For VAR_DECLs,
4934 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
c2255bc4
AH
4935 from an "aligned" __attribute__ specification). LOC is the
4936 location of the ALIGNOF operator. */
7f4edbcb 4937
0213a355 4938tree
c2255bc4 4939c_alignof_expr (location_t loc, tree expr)
0213a355
JM
4940{
4941 tree t;
4942
837edd5f 4943 if (VAR_OR_FUNCTION_DECL_P (expr))
a4e9ffe5 4944 t = size_int (DECL_ALIGN_UNIT (expr));
35b1a6fa 4945
0213a355
JM
4946 else if (TREE_CODE (expr) == COMPONENT_REF
4947 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4948 {
c2255bc4 4949 error_at (loc, "%<__alignof%> applied to a bit-field");
0213a355
JM
4950 t = size_one_node;
4951 }
4952 else if (TREE_CODE (expr) == COMPONENT_REF
173bf5be 4953 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
a4e9ffe5 4954 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
35b1a6fa 4955
22d03525 4956 else if (INDIRECT_REF_P (expr))
0213a355
JM
4957 {
4958 tree t = TREE_OPERAND (expr, 0);
4959 tree best = t;
4960 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
35b1a6fa 4961
1043771b 4962 while (CONVERT_EXPR_P (t)
173bf5be 4963 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
0213a355
JM
4964 {
4965 int thisalign;
4966
4967 t = TREE_OPERAND (t, 0);
4968 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4969 if (thisalign > bestalign)
4970 best = t, bestalign = thisalign;
4971 }
c2255bc4 4972 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
0213a355
JM
4973 }
4974 else
c2255bc4 4975 return c_alignof (loc, TREE_TYPE (expr));
0213a355 4976
db3927fb 4977 return fold_convert_loc (loc, size_type_node, t);
0213a355
JM
4978}
4979\f
df061a43
RS
4980/* Handle C and C++ default attributes. */
4981
4982enum built_in_attribute
4983{
4984#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4985#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
e384e6b5 4986#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
df061a43
RS
4987#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4988#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
df061a43
RS
4989#include "builtin-attrs.def"
4990#undef DEF_ATTR_NULL_TREE
4991#undef DEF_ATTR_INT
e384e6b5 4992#undef DEF_ATTR_STRING
df061a43
RS
4993#undef DEF_ATTR_IDENT
4994#undef DEF_ATTR_TREE_LIST
df061a43
RS
4995 ATTR_LAST
4996};
4997
4998static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4999
35b1a6fa 5000static void c_init_attributes (void);
df061a43 5001
a0274e3e 5002enum c_builtin_type
7f4edbcb 5003{
10841285
MM
5004#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5005#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5006#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5007#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5008#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5009#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
10a0d495 5010#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
f6a7cffc
TS
5011#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5012 ARG6) NAME,
5013#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5014 ARG6, ARG7) NAME,
5015#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5016 ARG6, ARG7, ARG8) NAME,
d9a6bd32
JJ
5017#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5018 ARG6, ARG7, ARG8, ARG9) NAME,
5019#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5020 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5021#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5022 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
10841285
MM
5023#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5024#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5025#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
08291658 5026#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
10a0d495 5027#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
f6a7cffc 5028#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
56a9f6bc 5029 NAME,
3e32ee19
NS
5030#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5031 ARG6) NAME,
56a9f6bc
TS
5032#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5033 ARG6, ARG7) NAME,
10841285
MM
5034#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5035#include "builtin-types.def"
5036#undef DEF_PRIMITIVE_TYPE
5037#undef DEF_FUNCTION_TYPE_0
5038#undef DEF_FUNCTION_TYPE_1
5039#undef DEF_FUNCTION_TYPE_2
5040#undef DEF_FUNCTION_TYPE_3
5041#undef DEF_FUNCTION_TYPE_4
10a0d495
JJ
5042#undef DEF_FUNCTION_TYPE_5
5043#undef DEF_FUNCTION_TYPE_6
a0274e3e 5044#undef DEF_FUNCTION_TYPE_7
acf0174b 5045#undef DEF_FUNCTION_TYPE_8
d9a6bd32
JJ
5046#undef DEF_FUNCTION_TYPE_9
5047#undef DEF_FUNCTION_TYPE_10
5048#undef DEF_FUNCTION_TYPE_11
10841285
MM
5049#undef DEF_FUNCTION_TYPE_VAR_0
5050#undef DEF_FUNCTION_TYPE_VAR_1
5051#undef DEF_FUNCTION_TYPE_VAR_2
08291658 5052#undef DEF_FUNCTION_TYPE_VAR_3
10a0d495
JJ
5053#undef DEF_FUNCTION_TYPE_VAR_4
5054#undef DEF_FUNCTION_TYPE_VAR_5
3e32ee19 5055#undef DEF_FUNCTION_TYPE_VAR_6
56a9f6bc 5056#undef DEF_FUNCTION_TYPE_VAR_7
10841285 5057#undef DEF_POINTER_TYPE
a0274e3e
JJ
5058 BT_LAST
5059};
5060
5061typedef enum c_builtin_type builtin_type;
10841285 5062
a0274e3e
JJ
5063/* A temporary array for c_common_nodes_and_builtins. Used in
5064 communication with def_fn_type. */
5065static tree builtin_types[(int) BT_LAST + 1];
10841285 5066
a0274e3e
JJ
5067/* A helper function for c_common_nodes_and_builtins. Build function type
5068 for DEF with return type RET and N arguments. If VAR is true, then the
5069 function should be variadic after those N arguments.
5070
5071 Takes special care not to ICE if any of the types involved are
5072 error_mark_node, which indicates that said type is not in fact available
5073 (see builtin_type_for_size). In which case the function type as a whole
5074 should be error_mark_node. */
5075
5076static void
5077def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5078{
8242dd04
NF
5079 tree t;
5080 tree *args = XALLOCAVEC (tree, n);
a0274e3e
JJ
5081 va_list list;
5082 int i;
5083
5084 va_start (list, n);
5085 for (i = 0; i < n; ++i)
5086 {
d75d71e0 5087 builtin_type a = (builtin_type) va_arg (list, int);
a0274e3e
JJ
5088 t = builtin_types[a];
5089 if (t == error_mark_node)
5090 goto egress;
8242dd04 5091 args[i] = t;
a0274e3e 5092 }
a0274e3e 5093
a0274e3e
JJ
5094 t = builtin_types[ret];
5095 if (t == error_mark_node)
5096 goto egress;
8242dd04
NF
5097 if (var)
5098 t = build_varargs_function_type_array (t, n, args);
5099 else
5100 t = build_function_type_array (t, n, args);
a0274e3e
JJ
5101
5102 egress:
5103 builtin_types[def] = t;
0edf1bb2 5104 va_end (list);
a0274e3e
JJ
5105}
5106
c6d86fce
ILT
5107/* Build builtin functions common to both C and C++ language
5108 frontends. */
5109
5110static void
5111c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5112{
5113#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5114 builtin_types[ENUM] = VALUE;
5115#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5116 def_fn_type (ENUM, RETURN, 0, 0);
5117#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5118 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5119#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5120 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5121#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5122 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5123#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5124 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5125#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5126 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5127#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5128 ARG6) \
5129 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5130#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5131 ARG6, ARG7) \
5132 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
acf0174b
JJ
5133#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5134 ARG6, ARG7, ARG8) \
5135 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5136 ARG7, ARG8);
d9a6bd32
JJ
5137#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5138 ARG6, ARG7, ARG8, ARG9) \
5139 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5140 ARG7, ARG8, ARG9);
5141#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5142 ARG6, ARG7, ARG8, ARG9, ARG10) \
5143 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5144 ARG7, ARG8, ARG9, ARG10);
5145#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5146 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5147 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5148 ARG7, ARG8, ARG9, ARG10, ARG11);
c6d86fce
ILT
5149#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5150 def_fn_type (ENUM, RETURN, 1, 0);
5151#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5152 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5153#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5154 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5155#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5156 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5157#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5158 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5159#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5160 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3e32ee19
NS
5161#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5162 ARG6) \
5163 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
56a9f6bc
TS
5164#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5165 ARG6, ARG7) \
5166 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
c6d86fce
ILT
5167#define DEF_POINTER_TYPE(ENUM, TYPE) \
5168 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5169
5170#include "builtin-types.def"
5171
5172#undef DEF_PRIMITIVE_TYPE
f6a7cffc 5173#undef DEF_FUNCTION_TYPE_0
c6d86fce
ILT
5174#undef DEF_FUNCTION_TYPE_1
5175#undef DEF_FUNCTION_TYPE_2
5176#undef DEF_FUNCTION_TYPE_3
5177#undef DEF_FUNCTION_TYPE_4
5178#undef DEF_FUNCTION_TYPE_5
5179#undef DEF_FUNCTION_TYPE_6
f6a7cffc
TS
5180#undef DEF_FUNCTION_TYPE_7
5181#undef DEF_FUNCTION_TYPE_8
d9a6bd32
JJ
5182#undef DEF_FUNCTION_TYPE_9
5183#undef DEF_FUNCTION_TYPE_10
5184#undef DEF_FUNCTION_TYPE_11
c6d86fce
ILT
5185#undef DEF_FUNCTION_TYPE_VAR_0
5186#undef DEF_FUNCTION_TYPE_VAR_1
5187#undef DEF_FUNCTION_TYPE_VAR_2
5188#undef DEF_FUNCTION_TYPE_VAR_3
5189#undef DEF_FUNCTION_TYPE_VAR_4
5190#undef DEF_FUNCTION_TYPE_VAR_5
3e32ee19 5191#undef DEF_FUNCTION_TYPE_VAR_6
56a9f6bc 5192#undef DEF_FUNCTION_TYPE_VAR_7
c6d86fce
ILT
5193#undef DEF_POINTER_TYPE
5194 builtin_types[(int) BT_LAST] = NULL_TREE;
5195
5196 c_init_attributes ();
5197
5198#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5199 NONANSI_P, ATTRS, IMPLICIT, COND) \
5200 if (NAME && COND) \
5201 def_builtin_1 (ENUM, NAME, CLASS, \
5202 builtin_types[(int) TYPE], \
5203 builtin_types[(int) LIBTYPE], \
5204 BOTH_P, FALLBACK_P, NONANSI_P, \
5205 built_in_attributes[(int) ATTRS], IMPLICIT);
5206#include "builtins.def"
c6d86fce 5207
8de7ef2a
UB
5208 targetm.init_builtins ();
5209
384c400a 5210 build_common_builtin_nodes ();
939b37da 5211
b72271b9 5212 if (flag_cilkplus)
939b37da 5213 cilk_init_builtins ();
c6d86fce
ILT
5214}
5215
c1b61fca
JM
5216/* Like get_identifier, but avoid warnings about null arguments when
5217 the argument may be NULL for targets where GCC lacks stdint.h type
5218 information. */
5219
5220static inline tree
5221c_get_ident (const char *id)
5222{
5223 return get_identifier (id);
5224}
5225
a0274e3e
JJ
5226/* Build tree nodes and builtin functions common to both C and C++ language
5227 frontends. */
5228
5229void
5230c_common_nodes_and_builtins (void)
5231{
b6baa67d
KVH
5232 int char16_type_size;
5233 int char32_type_size;
eaa7c03f
JM
5234 int wchar_type_size;
5235 tree array_domain_type;
9f720c3e 5236 tree va_list_ref_type_node;
daf68dd7 5237 tree va_list_arg_type_node;
78a7c317 5238 int i;
d3707adb 5239
a011cd92 5240 build_common_tree_nodes (flag_signed_char);
fce5dddd 5241
eaa7c03f 5242 /* Define `int' and `char' first so that dbx will output them first. */
6496a589 5243 record_builtin_type (RID_INT, NULL, integer_type_node);
eaa7c03f
JM
5244 record_builtin_type (RID_CHAR, "char", char_type_node);
5245
5246 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5247 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5248 but not C. Are the conditionals here needed? */
37fa72e9 5249 if (c_dialect_cxx ())
6496a589 5250 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
eaa7c03f
JM
5251 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5252 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5253 record_builtin_type (RID_MAX, "long unsigned int",
5254 long_unsigned_type_node);
78a7c317
DD
5255
5256 for (i = 0; i < NUM_INT_N_ENTS; i ++)
a6766312 5257 {
78a7c317
DD
5258 char name[25];
5259
5260 sprintf (name, "__int%d", int_n_data[i].bitsize);
17958621 5261 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
78a7c317
DD
5262 int_n_trees[i].signed_type);
5263 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
17958621 5264 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
a6766312 5265 }
78a7c317 5266
37fa72e9 5267 if (c_dialect_cxx ())
eaa7c03f
JM
5268 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5269 record_builtin_type (RID_MAX, "long long int",
5270 long_long_integer_type_node);
5271 record_builtin_type (RID_MAX, "long long unsigned int",
5272 long_long_unsigned_type_node);
37fa72e9 5273 if (c_dialect_cxx ())
eaa7c03f
JM
5274 record_builtin_type (RID_MAX, "long long unsigned",
5275 long_long_unsigned_type_node);
5276 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5277 record_builtin_type (RID_MAX, "short unsigned int",
5278 short_unsigned_type_node);
37fa72e9 5279 if (c_dialect_cxx ())
eaa7c03f
JM
5280 record_builtin_type (RID_MAX, "unsigned short",
5281 short_unsigned_type_node);
5282
5283 /* Define both `signed char' and `unsigned char'. */
5284 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5285 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5286
b0c48229
NB
5287 /* These are types that c_common_type_for_size and
5288 c_common_type_for_mode use. */
c2255bc4
AH
5289 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5290 TYPE_DECL, NULL_TREE,
ae2bcd98 5291 intQI_type_node));
c2255bc4
AH
5292 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5293 TYPE_DECL, NULL_TREE,
ae2bcd98 5294 intHI_type_node));
c2255bc4
AH
5295 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5296 TYPE_DECL, NULL_TREE,
ae2bcd98 5297 intSI_type_node));
c2255bc4
AH
5298 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5299 TYPE_DECL, NULL_TREE,
ae2bcd98 5300 intDI_type_node));
eaa7c03f 5301#if HOST_BITS_PER_WIDE_INT >= 64
78a7c317
DD
5302 /* Note that this is different than the __int128 type that's part of
5303 the generic __intN support. */
1e1b8649 5304 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5305 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5306 TYPE_DECL,
1e1b8649
AP
5307 get_identifier ("__int128_t"),
5308 intTI_type_node));
eaa7c03f 5309#endif
c2255bc4
AH
5310 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5311 TYPE_DECL, NULL_TREE,
ae2bcd98 5312 unsigned_intQI_type_node));
c2255bc4
AH
5313 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5314 TYPE_DECL, NULL_TREE,
ae2bcd98 5315 unsigned_intHI_type_node));
c2255bc4
AH
5316 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5317 TYPE_DECL, NULL_TREE,
ae2bcd98 5318 unsigned_intSI_type_node));
c2255bc4
AH
5319 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5320 TYPE_DECL, NULL_TREE,
ae2bcd98 5321 unsigned_intDI_type_node));
eaa7c03f 5322#if HOST_BITS_PER_WIDE_INT >= 64
1e1b8649 5323 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5324 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5325 TYPE_DECL,
1e1b8649
AP
5326 get_identifier ("__uint128_t"),
5327 unsigned_intTI_type_node));
eaa7c03f
JM
5328#endif
5329
5330 /* Create the widest literal types. */
5331 widest_integer_literal_type_node
5332 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5333 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5334 TYPE_DECL, NULL_TREE,
ae2bcd98 5335 widest_integer_literal_type_node));
eaa7c03f
JM
5336
5337 widest_unsigned_literal_type_node
5338 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5339 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5340 TYPE_DECL, NULL_TREE,
ae2bcd98 5341 widest_unsigned_literal_type_node));
eaa7c03f 5342
c9f8536c 5343 signed_size_type_node = c_common_signed_type (size_type_node);
eaa7c03f 5344
d1c38823
ZD
5345 pid_type_node =
5346 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5347
6496a589
KG
5348 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5349 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
eaa7c03f
JM
5350 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5351
9a8ce21f
JG
5352 /* Only supported decimal floating point extension if the target
5353 actually supports underlying modes. */
b8698a0f 5354 if (targetm.scalar_mode_supported_p (SDmode)
9a8ce21f
JG
5355 && targetm.scalar_mode_supported_p (DDmode)
5356 && targetm.scalar_mode_supported_p (TDmode))
5357 {
5358 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5359 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5360 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5361 }
5362
ab22c1fa
CF
5363 if (targetm.fixed_point_supported_p ())
5364 {
5365 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5366 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5367 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5368 record_builtin_type (RID_MAX, "long long _Fract",
5369 long_long_fract_type_node);
5370 record_builtin_type (RID_MAX, "unsigned short _Fract",
5371 unsigned_short_fract_type_node);
5372 record_builtin_type (RID_MAX, "unsigned _Fract",
5373 unsigned_fract_type_node);
5374 record_builtin_type (RID_MAX, "unsigned long _Fract",
5375 unsigned_long_fract_type_node);
5376 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5377 unsigned_long_long_fract_type_node);
5378 record_builtin_type (RID_MAX, "_Sat short _Fract",
5379 sat_short_fract_type_node);
5380 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5381 record_builtin_type (RID_MAX, "_Sat long _Fract",
5382 sat_long_fract_type_node);
5383 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5384 sat_long_long_fract_type_node);
5385 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5386 sat_unsigned_short_fract_type_node);
5387 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5388 sat_unsigned_fract_type_node);
5389 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5390 sat_unsigned_long_fract_type_node);
5391 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5392 sat_unsigned_long_long_fract_type_node);
5393 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5394 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5395 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5396 record_builtin_type (RID_MAX, "long long _Accum",
5397 long_long_accum_type_node);
5398 record_builtin_type (RID_MAX, "unsigned short _Accum",
5399 unsigned_short_accum_type_node);
5400 record_builtin_type (RID_MAX, "unsigned _Accum",
5401 unsigned_accum_type_node);
5402 record_builtin_type (RID_MAX, "unsigned long _Accum",
5403 unsigned_long_accum_type_node);
5404 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5405 unsigned_long_long_accum_type_node);
5406 record_builtin_type (RID_MAX, "_Sat short _Accum",
5407 sat_short_accum_type_node);
5408 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5409 record_builtin_type (RID_MAX, "_Sat long _Accum",
5410 sat_long_accum_type_node);
5411 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5412 sat_long_long_accum_type_node);
5413 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5414 sat_unsigned_short_accum_type_node);
5415 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5416 sat_unsigned_accum_type_node);
5417 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5418 sat_unsigned_long_accum_type_node);
5419 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5420 sat_unsigned_long_long_accum_type_node);
5421
5422 }
5423
c2255bc4
AH
5424 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5425 TYPE_DECL,
ae2bcd98
RS
5426 get_identifier ("complex int"),
5427 complex_integer_type_node));
c2255bc4
AH
5428 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5429 TYPE_DECL,
ae2bcd98
RS
5430 get_identifier ("complex float"),
5431 complex_float_type_node));
c2255bc4
AH
5432 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5433 TYPE_DECL,
ae2bcd98
RS
5434 get_identifier ("complex double"),
5435 complex_double_type_node));
5436 lang_hooks.decls.pushdecl
c2255bc4
AH
5437 (build_decl (UNKNOWN_LOCATION,
5438 TYPE_DECL, get_identifier ("complex long double"),
43577e6b 5439 complex_long_double_type_node));
eaa7c03f 5440
498c0f27
JJ
5441 if (c_dialect_cxx ())
5442 /* For C++, make fileptr_type_node a distinct void * type until
5443 FILE type is defined. */
8dd16ecc 5444 fileptr_type_node = build_variant_type_copy (ptr_type_node);
498c0f27 5445
6496a589 5446 record_builtin_type (RID_VOID, NULL, void_type_node);
eaa7c03f 5447
06d40de8
DG
5448 /* Set the TYPE_NAME for any variants that were built before
5449 record_builtin_type gave names to the built-in types. */
5450 {
5451 tree void_name = TYPE_NAME (void_type_node);
5452 TYPE_NAME (void_type_node) = NULL_TREE;
5453 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5454 = void_name;
5455 TYPE_NAME (void_type_node) = void_name;
5456 }
5457
eaa7c03f
JM
5458 void_list_node = build_void_list_node ();
5459
5460 /* Make a type to be the domain of a few array types
5461 whose domains don't really matter.
5462 200 is small enough that it always fits in size_t
5463 and large enough that it can hold most function names for the
5464 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5465 array_domain_type = build_index_type (size_int (200));
5466
5467 /* Make a type for arrays of characters.
5468 With luck nothing will ever really depend on the length of this
5469 array type. */
5470 char_array_type_node
5471 = build_array_type (char_type_node, array_domain_type);
5472
10841285
MM
5473 string_type_node = build_pointer_type (char_type_node);
5474 const_string_type_node
5475 = build_pointer_type (build_qualified_type
5476 (char_type_node, TYPE_QUAL_CONST));
5477
eaa7c03f 5478 /* This is special for C++ so functions can be overloaded. */
a11eba95 5479 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
eaa7c03f
JM
5480 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5481 wchar_type_size = TYPE_PRECISION (wchar_type_node);
207bf79d 5482 underlying_wchar_type_node = wchar_type_node;
37fa72e9 5483 if (c_dialect_cxx ())
eaa7c03f 5484 {
8df83eae 5485 if (TYPE_UNSIGNED (wchar_type_node))
eaa7c03f
JM
5486 wchar_type_node = make_unsigned_type (wchar_type_size);
5487 else
5488 wchar_type_node = make_signed_type (wchar_type_size);
5489 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5490 }
eaa7c03f
JM
5491
5492 /* This is for wide string constants. */
5493 wchar_array_type_node
5494 = build_array_type (wchar_type_node, array_domain_type);
5495
b6baa67d
KVH
5496 /* Define 'char16_t'. */
5497 char16_type_node = get_identifier (CHAR16_TYPE);
5498 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5499 char16_type_size = TYPE_PRECISION (char16_type_node);
5500 if (c_dialect_cxx ())
5501 {
5502 char16_type_node = make_unsigned_type (char16_type_size);
5503
604b2bfc 5504 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5505 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5506 }
5507
5508 /* This is for UTF-16 string constants. */
5509 char16_array_type_node
5510 = build_array_type (char16_type_node, array_domain_type);
5511
5512 /* Define 'char32_t'. */
5513 char32_type_node = get_identifier (CHAR32_TYPE);
5514 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5515 char32_type_size = TYPE_PRECISION (char32_type_node);
5516 if (c_dialect_cxx ())
5517 {
5518 char32_type_node = make_unsigned_type (char32_type_size);
5519
604b2bfc 5520 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5521 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5522 }
5523
5524 /* This is for UTF-32 string constants. */
5525 char32_array_type_node
5526 = build_array_type (char32_type_node, array_domain_type);
5527
5fd8e536
JM
5528 wint_type_node =
5529 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5530
5531 intmax_type_node =
5532 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5533 uintmax_type_node =
5534 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5535
207bf79d
JM
5536 if (SIG_ATOMIC_TYPE)
5537 sig_atomic_type_node =
c1b61fca 5538 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
207bf79d
JM
5539 if (INT8_TYPE)
5540 int8_type_node =
c1b61fca 5541 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
207bf79d
JM
5542 if (INT16_TYPE)
5543 int16_type_node =
c1b61fca 5544 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
207bf79d
JM
5545 if (INT32_TYPE)
5546 int32_type_node =
c1b61fca 5547 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
207bf79d
JM
5548 if (INT64_TYPE)
5549 int64_type_node =
c1b61fca 5550 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
207bf79d
JM
5551 if (UINT8_TYPE)
5552 uint8_type_node =
c1b61fca 5553 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
207bf79d 5554 if (UINT16_TYPE)
cca615af 5555 c_uint16_type_node = uint16_type_node =
c1b61fca 5556 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
207bf79d 5557 if (UINT32_TYPE)
cca615af 5558 c_uint32_type_node = uint32_type_node =
c1b61fca 5559 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
207bf79d 5560 if (UINT64_TYPE)
cca615af 5561 c_uint64_type_node = uint64_type_node =
c1b61fca 5562 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
207bf79d
JM
5563 if (INT_LEAST8_TYPE)
5564 int_least8_type_node =
c1b61fca 5565 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
207bf79d
JM
5566 if (INT_LEAST16_TYPE)
5567 int_least16_type_node =
c1b61fca 5568 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
207bf79d
JM
5569 if (INT_LEAST32_TYPE)
5570 int_least32_type_node =
c1b61fca 5571 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
207bf79d
JM
5572 if (INT_LEAST64_TYPE)
5573 int_least64_type_node =
c1b61fca 5574 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
207bf79d
JM
5575 if (UINT_LEAST8_TYPE)
5576 uint_least8_type_node =
c1b61fca 5577 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
207bf79d
JM
5578 if (UINT_LEAST16_TYPE)
5579 uint_least16_type_node =
c1b61fca 5580 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
207bf79d
JM
5581 if (UINT_LEAST32_TYPE)
5582 uint_least32_type_node =
c1b61fca 5583 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
207bf79d
JM
5584 if (UINT_LEAST64_TYPE)
5585 uint_least64_type_node =
c1b61fca 5586 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
207bf79d
JM
5587 if (INT_FAST8_TYPE)
5588 int_fast8_type_node =
c1b61fca 5589 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
207bf79d
JM
5590 if (INT_FAST16_TYPE)
5591 int_fast16_type_node =
c1b61fca 5592 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
207bf79d
JM
5593 if (INT_FAST32_TYPE)
5594 int_fast32_type_node =
c1b61fca 5595 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
207bf79d
JM
5596 if (INT_FAST64_TYPE)
5597 int_fast64_type_node =
c1b61fca 5598 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
207bf79d
JM
5599 if (UINT_FAST8_TYPE)
5600 uint_fast8_type_node =
c1b61fca 5601 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
207bf79d
JM
5602 if (UINT_FAST16_TYPE)
5603 uint_fast16_type_node =
c1b61fca 5604 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
207bf79d
JM
5605 if (UINT_FAST32_TYPE)
5606 uint_fast32_type_node =
c1b61fca 5607 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
207bf79d
JM
5608 if (UINT_FAST64_TYPE)
5609 uint_fast64_type_node =
c1b61fca 5610 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
207bf79d
JM
5611 if (INTPTR_TYPE)
5612 intptr_type_node =
c1b61fca 5613 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
207bf79d
JM
5614 if (UINTPTR_TYPE)
5615 uintptr_type_node =
c1b61fca 5616 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
207bf79d 5617
8242dd04
NF
5618 default_function_type
5619 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5fd8e536
JM
5620 ptrdiff_type_node
5621 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
12753674 5622 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5fd8e536 5623
ae2bcd98 5624 lang_hooks.decls.pushdecl
c2255bc4
AH
5625 (build_decl (UNKNOWN_LOCATION,
5626 TYPE_DECL, get_identifier ("__builtin_va_list"),
43577e6b 5627 va_list_type_node));
38f8b050 5628 if (targetm.enum_va_list_p)
d4048208
KT
5629 {
5630 int l;
5631 const char *pname;
5632 tree ptype;
35cbb299 5633
38f8b050 5634 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
d4048208
KT
5635 {
5636 lang_hooks.decls.pushdecl
5637 (build_decl (UNKNOWN_LOCATION,
5638 TYPE_DECL, get_identifier (pname),
5639 ptype));
5640
5641 }
5642 }
daf68dd7 5643
daf68dd7 5644 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
5645 {
5646 va_list_arg_type_node = va_list_ref_type_node =
5647 build_pointer_type (TREE_TYPE (va_list_type_node));
5648 }
daf68dd7 5649 else
9f720c3e
GK
5650 {
5651 va_list_arg_type_node = va_list_type_node;
5652 va_list_ref_type_node = build_reference_type (va_list_type_node);
5653 }
35b1a6fa 5654
c6d86fce
ILT
5655 if (!flag_preprocess_only)
5656 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4677862a 5657
5b47282c 5658 main_identifier_node = get_identifier ("main");
b2f97e4a
MM
5659
5660 /* Create the built-in __null node. It is important that this is
5661 not shared. */
807e902e 5662 null_node = make_int_cst (1, 1);
b2f97e4a 5663 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
a0274e3e
JJ
5664
5665 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5666 memset (builtin_types, 0, sizeof (builtin_types));
7f4edbcb 5667}
d3707adb 5668
3b2db49f
MM
5669/* The number of named compound-literals generated thus far. */
5670static GTY(()) int compound_literal_number;
5671
5672/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5673
5674void
5675set_compound_literal_name (tree decl)
5676{
5677 char *name;
5678 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5679 compound_literal_number);
5680 compound_literal_number++;
5681 DECL_NAME (decl) = get_identifier (name);
5682}
5683
2fe1d762
TV
5684/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5685 TYPE and operand OP. */
5686
5687static tree
5688build_va_arg_1 (location_t loc, tree type, tree op)
5689{
5690 tree expr = build1 (VA_ARG_EXPR, type, op);
5691 SET_EXPR_LOCATION (expr, loc);
5692 return expr;
5693}
5694
5695/* Return a VA_ARG_EXPR corresponding to a source-level expression
5696 va_arg (EXPR, TYPE) at source location LOC. */
5697
d3707adb 5698tree
c2255bc4 5699build_va_arg (location_t loc, tree expr, tree type)
d3707adb 5700{
c7b38fd5
TV
5701 tree va_type = TREE_TYPE (expr);
5702 tree canon_va_type = (va_type == error_mark_node
5703 ? NULL_TREE
5704 : targetm.canonical_va_list_type (va_type));
5705
2fe1d762
TV
5706 if (va_type == error_mark_node
5707 || canon_va_type == NULL_TREE)
c7b38fd5 5708 {
2fe1d762
TV
5709 /* Let's handle things neutrallly, if expr:
5710 - has undeclared type, or
5711 - is not an va_list type. */
5712 return build_va_arg_1 (loc, type, expr);
c7b38fd5 5713 }
ecd0e562 5714
2fe1d762
TV
5715 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5716 {
5717 /* Case 1: Not an array type. */
5718
5719 /* Take the address, to get '&ap'. */
5720 mark_addressable (expr);
5721 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5722
5723 /* Verify that &ap is still recognized as having va_list type. */
5724 tree canon_expr_type
5725 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5726 gcc_assert (canon_expr_type != NULL_TREE);
5727
5728 return build_va_arg_1 (loc, type, expr);
5729 }
5730
5731 /* Case 2: Array type.
5732
5733 Background:
5734
5735 For contrast, let's start with the simple case (case 1). If
5736 canon_va_type is not an array type, but say a char *, then when
5737 passing-by-value a va_list, the type of the va_list param decl is
5738 the same as for another va_list decl (all ap's are char *):
5739
5740 f2_1 (char * ap)
5741 D.1815 = VA_ARG (&ap, 0B, 1);
5742 return D.1815;
5743
5744 f2 (int i)
5745 char * ap.0;
5746 char * ap;
5747 __builtin_va_start (&ap, 0);
5748 ap.0 = ap;
5749 res = f2_1 (ap.0);
5750 __builtin_va_end (&ap);
5751 D.1812 = res;
5752 return D.1812;
5753
5754 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5755 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5756 the same as for another va_list decl (case 2a, struct ap[1]).
5757
5758 f2_1 (struct * ap)
5759 D.1844 = VA_ARG (ap, 0B, 0);
5760 return D.1844;
5761
5762 f2 (int i)
5763 struct ap[1];
5764 __builtin_va_start (&ap, 0);
5765 res = f2_1 (&ap);
5766 __builtin_va_end (&ap);
5767 D.1841 = res;
5768 return D.1841;
5769
5770 Case 2b is different because:
5771 - on the callee side, the parm decl has declared type va_list, but
5772 grokdeclarator changes the type of the parm decl to a pointer to the
5773 array elem type.
5774 - on the caller side, the pass-by-value uses &ap.
5775
5776 We unify these two cases (case 2a: va_list is array type,
5777 case 2b: va_list is pointer to array elem type), by adding '&' for the
5778 array type case, such that we have a pointer to array elem in both
5779 cases. */
5780
5781 if (TREE_CODE (va_type) == ARRAY_TYPE)
5782 {
5783 /* Case 2a: va_list is array type. */
5784
5785 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5786 elem type. */
5787 mark_addressable (expr);
5788 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5789 expr);
5790
5791 /* Verify that &ap is still recognized as having va_list type. */
5792 tree canon_expr_type
5793 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5794 gcc_assert (canon_expr_type != NULL_TREE);
5795 }
5796 else
5797 {
5798 /* Case 2b: va_list is pointer to array elem type. */
5799 gcc_assert (POINTER_TYPE_P (va_type));
5800 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
5801
5802 /* Don't take the address. We've already got '&ap'. */
5803 ;
5804 }
5805
5806 return build_va_arg_1 (loc, type, expr);
d3707adb 5807}
fc2aaf30
JM
5808
5809
7d14c755
JM
5810/* Linked list of disabled built-in functions. */
5811
a79683d5 5812struct disabled_builtin
7d14c755
JM
5813{
5814 const char *name;
5815 struct disabled_builtin *next;
a79683d5 5816};
7d14c755
JM
5817static disabled_builtin *disabled_builtins = NULL;
5818
35b1a6fa 5819static bool builtin_function_disabled_p (const char *);
7d14c755
JM
5820
5821/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5822 begins with "__builtin_", give an error. */
5823
5824void
35b1a6fa 5825disable_builtin_function (const char *name)
7d14c755
JM
5826{
5827 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
bda67431 5828 error ("cannot disable built-in function %qs", name);
7d14c755
JM
5829 else
5830 {
1ad463f4
BI
5831 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5832 new_disabled_builtin->name = name;
5833 new_disabled_builtin->next = disabled_builtins;
5834 disabled_builtins = new_disabled_builtin;
7d14c755
JM
5835 }
5836}
5837
5838
5839/* Return true if the built-in function NAME has been disabled, false
5840 otherwise. */
5841
5842static bool
35b1a6fa 5843builtin_function_disabled_p (const char *name)
7d14c755
JM
5844{
5845 disabled_builtin *p;
5846 for (p = disabled_builtins; p != NULL; p = p->next)
5847 {
5848 if (strcmp (name, p->name) == 0)
5849 return true;
5850 }
5851 return false;
5852}
5853
5854
9bc15050
RG
5855/* Worker for DEF_BUILTIN.
5856 Possibly define a builtin function with one or two names.
5857 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5858 nonansi_p and flag_no_nonansi_builtin. */
fc2aaf30 5859
9bc15050
RG
5860static void
5861def_builtin_1 (enum built_in_function fncode,
5862 const char *name,
5863 enum built_in_class fnclass,
5864 tree fntype, tree libtype,
5865 bool both_p, bool fallback_p, bool nonansi_p,
5866 tree fnattrs, bool implicit_p)
fc2aaf30 5867{
9bc15050
RG
5868 tree decl;
5869 const char *libname;
5870
a0274e3e
JJ
5871 if (fntype == error_mark_node)
5872 return;
5873
9bc15050
RG
5874 gcc_assert ((!both_p && !fallback_p)
5875 || !strncmp (name, "__builtin_",
5876 strlen ("__builtin_")));
5877
5878 libname = name + strlen ("__builtin_");
c79efc4d
RÁE
5879 decl = add_builtin_function (name, fntype, fncode, fnclass,
5880 (fallback_p ? libname : NULL),
5881 fnattrs);
e79983f4
MM
5882
5883 set_builtin_decl (fncode, decl, implicit_p);
5884
9bc15050
RG
5885 if (both_p
5886 && !flag_no_builtin && !builtin_function_disabled_p (libname)
7d14c755 5887 && !(nonansi_p && flag_no_nonansi_builtin))
c79efc4d
RÁE
5888 add_builtin_function (libname, libtype, fncode, fnclass,
5889 NULL, fnattrs);
fc2aaf30 5890}
c530479e 5891\f
d72040f5
RH
5892/* Nonzero if the type T promotes to int. This is (nearly) the
5893 integral promotions defined in ISO C99 6.3.1.1/2. */
5894
5895bool
58f9752a 5896c_promoting_integer_type_p (const_tree t)
d72040f5
RH
5897{
5898 switch (TREE_CODE (t))
5899 {
5900 case INTEGER_TYPE:
5901 return (TYPE_MAIN_VARIANT (t) == char_type_node
5902 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5903 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5904 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
c6c04fca
RL
5905 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5906 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d72040f5
RH
5907
5908 case ENUMERAL_TYPE:
5909 /* ??? Technically all enumerations not larger than an int
5910 promote to an int. But this is used along code paths
5911 that only want to notice a size change. */
5912 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5913
5914 case BOOLEAN_TYPE:
5915 return 1;
5916
5917 default:
5918 return 0;
5919 }
5920}
5921
c530479e
RH
5922/* Return 1 if PARMS specifies a fixed number of parameters
5923 and none of their types is affected by default promotions. */
5924
5925int
58f9752a 5926self_promoting_args_p (const_tree parms)
c530479e 5927{
58f9752a 5928 const_tree t;
c530479e
RH
5929 for (t = parms; t; t = TREE_CHAIN (t))
5930 {
b3694847 5931 tree type = TREE_VALUE (t);
7e8176d7 5932
694fea20
VR
5933 if (type == error_mark_node)
5934 continue;
5935
c530479e
RH
5936 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5937 return 0;
5938
5939 if (type == 0)
5940 return 0;
5941
5942 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5943 return 0;
5944
d72040f5 5945 if (c_promoting_integer_type_p (type))
c530479e
RH
5946 return 0;
5947 }
5948 return 1;
5949}
5eda3d66 5950
12ea3302
GDR
5951/* Recursively remove any '*' or '&' operator from TYPE. */
5952tree
5953strip_pointer_operator (tree t)
5954{
5955 while (POINTER_TYPE_P (t))
5956 t = TREE_TYPE (t);
5957 return t;
5958}
5959
ba992967
SP
5960/* Recursively remove pointer or array type from TYPE. */
5961tree
5962strip_pointer_or_array_types (tree t)
5963{
5964 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5965 t = TREE_TYPE (t);
5966 return t;
5967}
5968
8f17b5c5
MM
5969/* Used to compare case labels. K1 and K2 are actually tree nodes
5970 representing case labels, or NULL_TREE for a `default' label.
5971 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5972 K2, and 0 if K1 and K2 are equal. */
5973
5974int
35b1a6fa 5975case_compare (splay_tree_key k1, splay_tree_key k2)
8f17b5c5
MM
5976{
5977 /* Consider a NULL key (such as arises with a `default' label) to be
5978 smaller than anything else. */
5979 if (!k1)
5980 return k2 ? -1 : 0;
5981 else if (!k2)
5982 return k1 ? 1 : 0;
5983
5984 return tree_int_cst_compare ((tree) k1, (tree) k2);
5985}
5986
c2255bc4
AH
5987/* Process a case label, located at LOC, for the range LOW_VALUE
5988 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5989 then this case label is actually a `default' label. If only
5990 HIGH_VALUE is NULL_TREE, then case label was declared using the
5991 usual C/C++ syntax, rather than the GNU case range extension.
5992 CASES is a tree containing all the case ranges processed so far;
b155cfd9
MP
5993 COND is the condition for the switch-statement itself.
5994 OUTSIDE_RANGE_P says whether there was a case value that doesn't
5995 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
5996 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
8f17b5c5
MM
5997
5998tree
c2255bc4 5999c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
b155cfd9 6000 tree low_value, tree high_value, bool *outside_range_p)
8f17b5c5
MM
6001{
6002 tree type;
6003 tree label;
6004 tree case_label;
6005 splay_tree_node node;
6006
6007 /* Create the LABEL_DECL itself. */
c2255bc4 6008 label = create_artificial_label (loc);
8f17b5c5
MM
6009
6010 /* If there was an error processing the switch condition, bail now
6011 before we get more confused. */
6012 if (!cond || cond == error_mark_node)
6de9cd9a 6013 goto error_out;
8f17b5c5 6014
35b1a6fa
AJ
6015 if ((low_value && TREE_TYPE (low_value)
6016 && POINTER_TYPE_P (TREE_TYPE (low_value)))
8f17b5c5
MM
6017 || (high_value && TREE_TYPE (high_value)
6018 && POINTER_TYPE_P (TREE_TYPE (high_value))))
522ddfa2 6019 {
c2255bc4 6020 error_at (loc, "pointers are not permitted as case values");
522ddfa2
JM
6021 goto error_out;
6022 }
8f17b5c5
MM
6023
6024 /* Case ranges are a GNU extension. */
fcf73884 6025 if (high_value)
c1771a20 6026 pedwarn (loc, OPT_Wpedantic,
fcf73884 6027 "range expressions in switch statements are non-standard");
8f17b5c5
MM
6028
6029 type = TREE_TYPE (cond);
6030 if (low_value)
6031 {
62e4eb35 6032 low_value = check_case_value (loc, low_value);
68fca595 6033 low_value = convert_and_check (loc, type, low_value);
c0e22534
NS
6034 if (low_value == error_mark_node)
6035 goto error_out;
8f17b5c5
MM
6036 }
6037 if (high_value)
6038 {
62e4eb35 6039 high_value = check_case_value (loc, high_value);
68fca595 6040 high_value = convert_and_check (loc, type, high_value);
c0e22534
NS
6041 if (high_value == error_mark_node)
6042 goto error_out;
8f17b5c5
MM
6043 }
6044
c0e22534
NS
6045 if (low_value && high_value)
6046 {
6047 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
c22cacf3
MS
6048 really a case range, even though it was written that way.
6049 Remove the HIGH_VALUE to simplify later processing. */
c0e22534
NS
6050 if (tree_int_cst_equal (low_value, high_value))
6051 high_value = NULL_TREE;
6052 else if (!tree_int_cst_lt (low_value, high_value))
c2255bc4 6053 warning_at (loc, 0, "empty range specified");
c0e22534 6054 }
8f17b5c5 6055
a6c0a76c
SB
6056 /* See if the case is in range of the type of the original testing
6057 expression. If both low_value and high_value are out of range,
6058 don't insert the case label and return NULL_TREE. */
6059 if (low_value
9d548dfb 6060 && !check_case_bounds (loc, type, orig_type,
b155cfd9
MP
6061 &low_value, high_value ? &high_value : NULL,
6062 outside_range_p))
a6c0a76c
SB
6063 return NULL_TREE;
6064
8f17b5c5
MM
6065 /* Look up the LOW_VALUE in the table of case labels we already
6066 have. */
6067 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6068 /* If there was not an exact match, check for overlapping ranges.
6069 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6070 that's a `default' label and the only overlap is an exact match. */
6071 if (!node && (low_value || high_value))
6072 {
6073 splay_tree_node low_bound;
6074 splay_tree_node high_bound;
6075
6076 /* Even though there wasn't an exact match, there might be an
6077 overlap between this case range and another case range.
6078 Since we've (inductively) not allowed any overlapping case
6079 ranges, we simply need to find the greatest low case label
6080 that is smaller that LOW_VALUE, and the smallest low case
6081 label that is greater than LOW_VALUE. If there is an overlap
6082 it will occur in one of these two ranges. */
6083 low_bound = splay_tree_predecessor (cases,
6084 (splay_tree_key) low_value);
6085 high_bound = splay_tree_successor (cases,
6086 (splay_tree_key) low_value);
6087
6088 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6089 the LOW_VALUE, so there is no need to check unless the
6090 LOW_BOUND is in fact itself a case range. */
6091 if (low_bound
6092 && CASE_HIGH ((tree) low_bound->value)
6093 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6094 low_value) >= 0)
6095 node = low_bound;
6096 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6097 range is bigger than the low end of the current range, so we
6098 are only interested if the current range is a real range, and
6099 not an ordinary case label. */
35b1a6fa 6100 else if (high_bound
8f17b5c5
MM
6101 && high_value
6102 && (tree_int_cst_compare ((tree) high_bound->key,
6103 high_value)
6104 <= 0))
6105 node = high_bound;
6106 }
6107 /* If there was an overlap, issue an error. */
6108 if (node)
6109 {
8c161995 6110 tree duplicate = CASE_LABEL ((tree) node->value);
8f17b5c5
MM
6111
6112 if (high_value)
6113 {
c2255bc4
AH
6114 error_at (loc, "duplicate (or overlapping) case value");
6115 error_at (DECL_SOURCE_LOCATION (duplicate),
6116 "this is the first entry overlapping that value");
8f17b5c5
MM
6117 }
6118 else if (low_value)
6119 {
c2255bc4
AH
6120 error_at (loc, "duplicate case value") ;
6121 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
8f17b5c5
MM
6122 }
6123 else
6124 {
c2255bc4
AH
6125 error_at (loc, "multiple default labels in one switch");
6126 error_at (DECL_SOURCE_LOCATION (duplicate),
6127 "this is the first default label");
8f17b5c5 6128 }
6de9cd9a 6129 goto error_out;
8f17b5c5
MM
6130 }
6131
6132 /* Add a CASE_LABEL to the statement-tree. */
3d528853 6133 case_label = add_stmt (build_case_label (low_value, high_value, label));
8f17b5c5 6134 /* Register this case label in the splay tree. */
35b1a6fa 6135 splay_tree_insert (cases,
8f17b5c5
MM
6136 (splay_tree_key) low_value,
6137 (splay_tree_value) case_label);
6138
6139 return case_label;
6de9cd9a
DN
6140
6141 error_out:
9e14e18f 6142 /* Add a label so that the back-end doesn't think that the beginning of
6de9cd9a 6143 the switch is unreachable. Note that we do not add a case label, as
41806d92 6144 that just leads to duplicates and thence to failure later on. */
6de9cd9a
DN
6145 if (!cases->root)
6146 {
c2255bc4
AH
6147 tree t = create_artificial_label (loc);
6148 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6de9cd9a
DN
6149 }
6150 return error_mark_node;
6151}
6152
6153/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6154 Used to verify that case values match up with enumerator values. */
6155
6156static void
6157match_case_to_enum_1 (tree key, tree type, tree label)
6158{
807e902e
KZ
6159 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6160
6161 if (tree_fits_uhwi_p (key))
6162 print_dec (key, buf, UNSIGNED);
6163 else if (tree_fits_shwi_p (key))
6164 print_dec (key, buf, SIGNED);
6de9cd9a 6165 else
807e902e 6166 print_hex (key, buf);
6de9cd9a
DN
6167
6168 if (TYPE_NAME (type) == 0)
c5d75364
MLI
6169 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6170 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6171 "case value %qs not in enumerated type",
6172 buf);
6de9cd9a 6173 else
c5d75364
MLI
6174 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6175 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6176 "case value %qs not in enumerated type %qT",
6177 buf, type);
6de9cd9a
DN
6178}
6179
c782c2fe
RS
6180/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6181 Used to verify that case values match up with enumerator values. */
6182
6de9cd9a
DN
6183static int
6184match_case_to_enum (splay_tree_node node, void *data)
6185{
6186 tree label = (tree) node->value;
28dab132 6187 tree type = (tree) data;
6de9cd9a
DN
6188
6189 /* Skip default case. */
6190 if (!CASE_LOW (label))
6191 return 0;
6192
c782c2fe 6193 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6de9cd9a 6194 when we did our enum->case scan. Reset our scratch bit after. */
c782c2fe 6195 if (!CASE_LOW_SEEN (label))
6de9cd9a
DN
6196 match_case_to_enum_1 (CASE_LOW (label), type, label);
6197 else
c782c2fe 6198 CASE_LOW_SEEN (label) = 0;
6de9cd9a 6199
c782c2fe
RS
6200 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6201 not set, that means that CASE_HIGH did not appear when we did our
6202 enum->case scan. Reset our scratch bit after. */
6de9cd9a
DN
6203 if (CASE_HIGH (label))
6204 {
c782c2fe
RS
6205 if (!CASE_HIGH_SEEN (label))
6206 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6207 else
6208 CASE_HIGH_SEEN (label) = 0;
6de9cd9a
DN
6209 }
6210
6211 return 0;
6212}
6213
fbc315db
ILT
6214/* Handle -Wswitch*. Called from the front end after parsing the
6215 switch construct. */
6216/* ??? Should probably be somewhere generic, since other languages
6217 besides C and C++ would want this. At the moment, however, C/C++
6218 are the only tree-ssa languages that support enumerations at all,
6219 so the point is moot. */
6de9cd9a 6220
fbc315db
ILT
6221void
6222c_do_switch_warnings (splay_tree cases, location_t switch_location,
b155cfd9
MP
6223 tree type, tree cond, bool bool_cond_p,
6224 bool outside_range_p)
6de9cd9a 6225{
9f63daea 6226 splay_tree_node default_node;
c782c2fe
RS
6227 splay_tree_node node;
6228 tree chain;
6de9cd9a 6229
b155cfd9
MP
6230 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6231 && !warn_switch_bool)
6de9cd9a
DN
6232 return;
6233
6de9cd9a 6234 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
44c21c7f 6235 if (!default_node)
fab922b1
MLI
6236 warning_at (switch_location, OPT_Wswitch_default,
6237 "switch missing default case");
6de9cd9a 6238
b155cfd9
MP
6239 /* There are certain cases where -Wswitch-bool warnings aren't
6240 desirable, such as
6241 switch (boolean)
6242 {
6243 case true: ...
6244 case false: ...
6245 }
6246 so be careful here. */
6247 if (warn_switch_bool && bool_cond_p)
6248 {
6249 splay_tree_node min_node;
6250 /* If there's a default node, it's also the value with the minimal
6251 key. So look at the penultimate key (if any). */
6252 if (default_node)
6253 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6254 else
6255 min_node = splay_tree_min (cases);
6256 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6257
6258 splay_tree_node max_node = splay_tree_max (cases);
6259 /* This might be a case range, so look at the value with the
6260 maximal key and then check CASE_HIGH. */
6261 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6262 if (max)
6263 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6264
6265 /* If there's a case value > 1 or < 0, that is outside bool
6266 range, warn. */
6267 if (outside_range_p
6268 || (max && wi::gts_p (max, 1))
6269 || (min && wi::lts_p (min, 0))
6270 /* And handle the
6271 switch (boolean)
6272 {
6273 case true: ...
6274 case false: ...
6275 default: ...
6276 }
6277 case, where we want to warn. */
6278 || (default_node
6279 && max && wi::eq_p (max, 1)
6280 && min && wi::eq_p (min, 0)))
6281 warning_at (switch_location, OPT_Wswitch_bool,
6282 "switch condition has boolean value");
6283 }
6284
026c3cfd 6285 /* From here on, we only care about enumerated types. */
c782c2fe
RS
6286 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6287 return;
6288
cdb88468
ILT
6289 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6290 if (!warn_switch_enum && !warn_switch)
c782c2fe
RS
6291 return;
6292
cdb88468
ILT
6293 /* Check the cases. Warn about case values which are not members of
6294 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6295 there is no default case, check that exactly all enumeration
6296 literals are covered by the cases. */
6297
c782c2fe
RS
6298 /* Clearing COND if it is not an integer constant simplifies
6299 the tests inside the loop below. */
6300 if (TREE_CODE (cond) != INTEGER_CST)
6301 cond = NULL_TREE;
6302
6303 /* The time complexity here is O(N*lg(N)) worst case, but for the
6304 common case of monotonically increasing enumerators, it is
6305 O(N), since the nature of the splay tree will keep the next
6306 element adjacent to the root at all times. */
6de9cd9a 6307
c782c2fe
RS
6308 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6309 {
6310 tree value = TREE_VALUE (chain);
adf2edec
DG
6311 if (TREE_CODE (value) == CONST_DECL)
6312 value = DECL_INITIAL (value);
c782c2fe
RS
6313 node = splay_tree_lookup (cases, (splay_tree_key) value);
6314 if (node)
6de9cd9a 6315 {
c782c2fe
RS
6316 /* Mark the CASE_LOW part of the case entry as seen. */
6317 tree label = (tree) node->value;
6318 CASE_LOW_SEEN (label) = 1;
6319 continue;
6320 }
6321
6322 /* Even though there wasn't an exact match, there might be a
fa10beec 6323 case range which includes the enumerator's value. */
c782c2fe
RS
6324 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6325 if (node && CASE_HIGH ((tree) node->value))
6326 {
6327 tree label = (tree) node->value;
6328 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6329 if (cmp >= 0)
6de9cd9a 6330 {
c782c2fe
RS
6331 /* If we match the upper bound exactly, mark the CASE_HIGH
6332 part of the case entry as seen. */
6333 if (cmp == 0)
6334 CASE_HIGH_SEEN (label) = 1;
6335 continue;
6de9cd9a
DN
6336 }
6337 }
6338
c782c2fe
RS
6339 /* We've now determined that this enumerated literal isn't
6340 handled by the case labels of the switch statement. */
6de9cd9a 6341
c782c2fe
RS
6342 /* If the switch expression is a constant, we only really care
6343 about whether that constant is handled by the switch. */
6344 if (cond && tree_int_cst_compare (cond, value))
6345 continue;
6de9cd9a 6346
683d6ff9 6347 /* If there is a default_node, the only relevant option is
cdb88468 6348 Wswitch-enum. Otherwise, if both are enabled then we prefer
683d6ff9
MLI
6349 to warn using -Wswitch because -Wswitch is enabled by -Wall
6350 while -Wswitch-enum is explicit. */
cdb88468
ILT
6351 warning_at (switch_location,
6352 (default_node || !warn_switch
6353 ? OPT_Wswitch_enum
6354 : OPT_Wswitch),
6355 "enumeration value %qE not handled in switch",
6356 TREE_PURPOSE (chain));
6de9cd9a 6357 }
c782c2fe
RS
6358
6359 /* Warn if there are case expressions that don't correspond to
6360 enumerators. This can occur since C and C++ don't enforce
6361 type-checking of assignments to enumeration variables.
6362
6363 The time complexity here is now always O(N) worst case, since
6364 we should have marked both the lower bound and upper bound of
6365 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6366 above. This scan also resets those fields. */
683d6ff9 6367
c782c2fe 6368 splay_tree_foreach (cases, match_case_to_enum, type);
8f17b5c5
MM
6369}
6370
6b665219 6371/* Finish an expression taking the address of LABEL (an
6a3799eb
AH
6372 IDENTIFIER_NODE). Returns an expression for the address.
6373
6374 LOC is the location for the expression returned. */
15b732b2 6375
35b1a6fa 6376tree
6a3799eb 6377finish_label_address_expr (tree label, location_t loc)
15b732b2
NB
6378{
6379 tree result;
6380
c1771a20 6381 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
15b732b2 6382
6b665219
MM
6383 if (label == error_mark_node)
6384 return error_mark_node;
6385
15b732b2
NB
6386 label = lookup_label (label);
6387 if (label == NULL_TREE)
6388 result = null_pointer_node;
6389 else
6390 {
6391 TREE_USED (label) = 1;
6392 result = build1 (ADDR_EXPR, ptr_type_node, label);
5805e48d 6393 /* The current function is not necessarily uninlinable.
15b732b2
NB
6394 Computed gotos are incompatible with inlining, but the value
6395 here could be used only in a diagnostic, for example. */
6a3799eb 6396 protected_set_expr_location (result, loc);
15b732b2
NB
6397 }
6398
6399 return result;
6400}
19552aa5
JM
6401\f
6402
6403/* Given a boolean expression ARG, return a tree representing an increment
6404 or decrement (as indicated by CODE) of ARG. The front end must check for
6405 invalid cases (e.g., decrement in C++). */
6406tree
35b1a6fa 6407boolean_increment (enum tree_code code, tree arg)
19552aa5
JM
6408{
6409 tree val;
b5119fa1 6410 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
37fa72e9 6411
19552aa5
JM
6412 arg = stabilize_reference (arg);
6413 switch (code)
6414 {
6415 case PREINCREMENT_EXPR:
53fb4de3 6416 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5
JM
6417 break;
6418 case POSTINCREMENT_EXPR:
53fb4de3 6419 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5 6420 arg = save_expr (arg);
53fb4de3
RS
6421 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6422 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6423 break;
6424 case PREDECREMENT_EXPR:
53fb4de3 6425 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6426 invert_truthvalue_loc (input_location, arg));
19552aa5
JM
6427 break;
6428 case POSTDECREMENT_EXPR:
53fb4de3 6429 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6430 invert_truthvalue_loc (input_location, arg));
19552aa5 6431 arg = save_expr (arg);
53fb4de3
RS
6432 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6433 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6434 break;
6435 default:
366de0ce 6436 gcc_unreachable ();
19552aa5
JM
6437 }
6438 TREE_SIDE_EFFECTS (val) = 1;
6439 return val;
6440}
03dc0325 6441\f
207bf79d
JM
6442/* Built-in macros for stddef.h and stdint.h, that require macros
6443 defined in this file. */
460bd0e3 6444void
35b1a6fa 6445c_stddef_cpp_builtins(void)
3df89291 6446{
5279d739
ZW
6447 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6448 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6449 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6450 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
85291069
JM
6451 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6452 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
c466b2cd
KVH
6453 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6454 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
207bf79d
JM
6455 if (SIG_ATOMIC_TYPE)
6456 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6457 if (INT8_TYPE)
6458 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6459 if (INT16_TYPE)
6460 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6461 if (INT32_TYPE)
6462 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6463 if (INT64_TYPE)
6464 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6465 if (UINT8_TYPE)
6466 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6467 if (UINT16_TYPE)
6468 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6469 if (UINT32_TYPE)
6470 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6471 if (UINT64_TYPE)
6472 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6473 if (INT_LEAST8_TYPE)
6474 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6475 if (INT_LEAST16_TYPE)
6476 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6477 if (INT_LEAST32_TYPE)
6478 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6479 if (INT_LEAST64_TYPE)
6480 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6481 if (UINT_LEAST8_TYPE)
6482 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6483 if (UINT_LEAST16_TYPE)
6484 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6485 if (UINT_LEAST32_TYPE)
6486 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6487 if (UINT_LEAST64_TYPE)
6488 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6489 if (INT_FAST8_TYPE)
6490 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6491 if (INT_FAST16_TYPE)
6492 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6493 if (INT_FAST32_TYPE)
6494 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6495 if (INT_FAST64_TYPE)
6496 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6497 if (UINT_FAST8_TYPE)
6498 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6499 if (UINT_FAST16_TYPE)
6500 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6501 if (UINT_FAST32_TYPE)
6502 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6503 if (UINT_FAST64_TYPE)
6504 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6505 if (INTPTR_TYPE)
6506 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6507 if (UINTPTR_TYPE)
6508 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
676997cf
RH
6509}
6510
6431177a 6511static void
35b1a6fa 6512c_init_attributes (void)
6431177a
JM
6513{
6514 /* Fill in the built_in_attributes array. */
4a90aeeb 6515#define DEF_ATTR_NULL_TREE(ENUM) \
6431177a 6516 built_in_attributes[(int) ENUM] = NULL_TREE;
4a90aeeb 6517#define DEF_ATTR_INT(ENUM, VALUE) \
c62c040f 6518 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
e384e6b5
BS
6519#define DEF_ATTR_STRING(ENUM, VALUE) \
6520 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6431177a
JM
6521#define DEF_ATTR_IDENT(ENUM, STRING) \
6522 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6523#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6524 built_in_attributes[(int) ENUM] \
6525 = tree_cons (built_in_attributes[(int) PURPOSE], \
6526 built_in_attributes[(int) VALUE], \
6527 built_in_attributes[(int) CHAIN]);
6431177a
JM
6528#include "builtin-attrs.def"
6529#undef DEF_ATTR_NULL_TREE
6530#undef DEF_ATTR_INT
6531#undef DEF_ATTR_IDENT
6532#undef DEF_ATTR_TREE_LIST
03dc0325 6533}
26f943fd 6534
943f82e7
JM
6535/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6536 identifier as an argument, so the front end shouldn't look it up. */
6537
6538bool
564a129d 6539attribute_takes_identifier_p (const_tree attr_id)
943f82e7 6540{
f231b5ff 6541 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
1b9b91a6
JM
6542 if (spec == NULL)
6543 /* Unknown attribute that we'll end up ignoring, return true so we
6544 don't complain about an identifier argument. */
6545 return true;
6546 else if (!strcmp ("mode", spec->name)
6547 || !strcmp ("format", spec->name)
6548 || !strcmp ("cleanup", spec->name))
564a129d
JM
6549 return true;
6550 else
6551 return targetm.attribute_takes_identifier_p (attr_id);
943f82e7
JM
6552}
6553
349ae713
NB
6554/* Attribute handlers common to C front ends. */
6555
6556/* Handle a "packed" attribute; arguments as in
6557 struct attribute_spec.handler. */
6558
6559static tree
e18476eb 6560handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
a742c759 6561 int flags, bool *no_add_attrs)
349ae713 6562{
c6e4cc53 6563 if (TYPE_P (*node))
349ae713
NB
6564 {
6565 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 6566 *node = build_variant_type_copy (*node);
c6e4cc53 6567 TYPE_PACKED (*node) = 1;
349ae713
NB
6568 }
6569 else if (TREE_CODE (*node) == FIELD_DECL)
646c0835 6570 {
2cd36c22
AN
6571 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6572 /* Still pack bitfields. */
6573 && ! DECL_INITIAL (*node))
646c0835
NS
6574 warning (OPT_Wattributes,
6575 "%qE attribute ignored for field of type %qT",
6576 name, TREE_TYPE (*node));
6577 else
6578 DECL_PACKED (*node) = 1;
6579 }
349ae713 6580 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
c6e4cc53
NS
6581 used for DECL_REGISTER. It wouldn't mean anything anyway.
6582 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6583 that changes what the typedef is typing. */
349ae713
NB
6584 else
6585 {
5c498b10 6586 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6587 *no_add_attrs = true;
6588 }
6589
6590 return NULL_TREE;
6591}
6592
6593/* Handle a "nocommon" attribute; arguments as in
6594 struct attribute_spec.handler. */
6595
6596static tree
35b1a6fa 6597handle_nocommon_attribute (tree *node, tree name,
e18476eb
BI
6598 tree ARG_UNUSED (args),
6599 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 6600{
0ae9bd27 6601 if (VAR_P (*node))
349ae713
NB
6602 DECL_COMMON (*node) = 0;
6603 else
6604 {
5c498b10 6605 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6606 *no_add_attrs = true;
6607 }
6608
6609 return NULL_TREE;
6610}
6611
6612/* Handle a "common" attribute; arguments as in
6613 struct attribute_spec.handler. */
6614
6615static tree
e18476eb
BI
6616handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6617 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 6618{
0ae9bd27 6619 if (VAR_P (*node))
349ae713
NB
6620 DECL_COMMON (*node) = 1;
6621 else
6622 {
5c498b10 6623 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6624 *no_add_attrs = true;
6625 }
6626
6627 return NULL_TREE;
6628}
6629
6630/* Handle a "noreturn" attribute; arguments as in
6631 struct attribute_spec.handler. */
6632
6633static tree
e18476eb
BI
6634handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6635 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6636{
6637 tree type = TREE_TYPE (*node);
6638
6639 /* See FIXME comment in c_common_attribute_table. */
2debdb4f
NP
6640 if (TREE_CODE (*node) == FUNCTION_DECL
6641 || objc_method_decl (TREE_CODE (*node)))
349ae713
NB
6642 TREE_THIS_VOLATILE (*node) = 1;
6643 else if (TREE_CODE (type) == POINTER_TYPE
6644 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6645 TREE_TYPE (*node)
6e7ceb17
PC
6646 = (build_qualified_type
6647 (build_pointer_type
6648 (build_type_variant (TREE_TYPE (type),
6649 TYPE_READONLY (TREE_TYPE (type)), 1)),
6650 TYPE_QUALS (type)));
349ae713
NB
6651 else
6652 {
5c498b10 6653 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6654 *no_add_attrs = true;
6655 }
6656
6657 return NULL_TREE;
6658}
6659
52bf96d2
JH
6660/* Handle a "hot" and attribute; arguments as in
6661 struct attribute_spec.handler. */
6662
6663static tree
6664handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
ab442df7 6665 int ARG_UNUSED (flags), bool *no_add_attrs)
52bf96d2 6666{
e45abe1f
RH
6667 if (TREE_CODE (*node) == FUNCTION_DECL
6668 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
6669 {
6670 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6671 {
45484dcf
MP
6672 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6673 "with attribute %qs", name, "cold");
52bf96d2
JH
6674 *no_add_attrs = true;
6675 }
5779e713
MM
6676 /* Most of the rest of the hot processing is done later with
6677 lookup_attribute. */
52bf96d2
JH
6678 }
6679 else
6680 {
6681 warning (OPT_Wattributes, "%qE attribute ignored", name);
6682 *no_add_attrs = true;
6683 }
6684
6685 return NULL_TREE;
6686}
e45abe1f 6687
52bf96d2
JH
6688/* Handle a "cold" and attribute; arguments as in
6689 struct attribute_spec.handler. */
6690
6691static tree
6692handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6693 int ARG_UNUSED (flags), bool *no_add_attrs)
6694{
e45abe1f
RH
6695 if (TREE_CODE (*node) == FUNCTION_DECL
6696 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
6697 {
6698 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6699 {
45484dcf
MP
6700 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6701 "with attribute %qs", name, "hot");
52bf96d2
JH
6702 *no_add_attrs = true;
6703 }
5779e713
MM
6704 /* Most of the rest of the cold processing is done later with
6705 lookup_attribute. */
52bf96d2
JH
6706 }
6707 else
6708 {
6709 warning (OPT_Wattributes, "%qE attribute ignored", name);
6710 *no_add_attrs = true;
6711 }
6712
6713 return NULL_TREE;
6714}
6715
e664c61c 6716/* Handle a "no_sanitize_address" attribute; arguments as in
77bc5132
JJ
6717 struct attribute_spec.handler. */
6718
6719static tree
e664c61c
KS
6720handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6721 bool *no_add_attrs)
77bc5132
JJ
6722{
6723 if (TREE_CODE (*node) != FUNCTION_DECL)
6724 {
6725 warning (OPT_Wattributes, "%qE attribute ignored", name);
6726 *no_add_attrs = true;
6727 }
6728
6729 return NULL_TREE;
6730}
6731
e664c61c
KS
6732/* Handle a "no_address_safety_analysis" attribute; arguments as in
6733 struct attribute_spec.handler. */
6734
6735static tree
6736handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6737 bool *no_add_attrs)
6738{
6739 if (TREE_CODE (*node) != FUNCTION_DECL)
6740 warning (OPT_Wattributes, "%qE attribute ignored", name);
6741 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6742 DECL_ATTRIBUTES (*node)
6743 = tree_cons (get_identifier ("no_sanitize_address"),
6744 NULL_TREE, DECL_ATTRIBUTES (*node));
6745 *no_add_attrs = true;
6746 return NULL_TREE;
6747}
6748
ce6923c5
MP
6749/* Handle a "no_sanitize_undefined" attribute; arguments as in
6750 struct attribute_spec.handler. */
6751
6752static tree
6753handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6754 bool *no_add_attrs)
6755{
6756 if (TREE_CODE (*node) != FUNCTION_DECL)
6757 {
6758 warning (OPT_Wattributes, "%qE attribute ignored", name);
6759 *no_add_attrs = true;
6760 }
6761
6762 return NULL_TREE;
6763}
6764
5434dc07
MD
6765/* Handle a "stack_protect" attribute; arguments as in
6766 struct attribute_spec.handler. */
6767static tree
6768handle_stack_protect_attribute (tree *node, tree name, tree, int,
6769 bool *no_add_attrs)
6770{
6771 if (TREE_CODE (*node) != FUNCTION_DECL)
6772 {
6773 warning (OPT_Wattributes, "%qE attribute ignored", name);
6774 *no_add_attrs = true;
6775 }
6776 else
6777 DECL_ATTRIBUTES (*node)
6778 = tree_cons (get_identifier ("stack_protect"),
6779 NULL_TREE, DECL_ATTRIBUTES (*node));
6780
6781 return NULL_TREE;
6782}
6783
349ae713
NB
6784/* Handle a "noinline" attribute; arguments as in
6785 struct attribute_spec.handler. */
6786
6787static tree
35b1a6fa 6788handle_noinline_attribute (tree *node, tree name,
e18476eb
BI
6789 tree ARG_UNUSED (args),
6790 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6791{
6792 if (TREE_CODE (*node) == FUNCTION_DECL)
45484dcf
MP
6793 {
6794 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6795 {
6796 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6797 "with attribute %qs", name, "always_inline");
6798 *no_add_attrs = true;
6799 }
6800 else
6801 DECL_UNINLINABLE (*node) = 1;
6802 }
349ae713
NB
6803 else
6804 {
5c498b10 6805 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6806 *no_add_attrs = true;
6807 }
6808
6809 return NULL_TREE;
6810}
6811
86631ea3
MJ
6812/* Handle a "noclone" attribute; arguments as in
6813 struct attribute_spec.handler. */
6814
6815static tree
6816handle_noclone_attribute (tree *node, tree name,
6817 tree ARG_UNUSED (args),
6818 int ARG_UNUSED (flags), bool *no_add_attrs)
6819{
6820 if (TREE_CODE (*node) != FUNCTION_DECL)
6821 {
6822 warning (OPT_Wattributes, "%qE attribute ignored", name);
6823 *no_add_attrs = true;
6824 }
6825
6826 return NULL_TREE;
6827}
6828
185c9e56
ML
6829/* Handle a "no_icf" attribute; arguments as in
6830 struct attribute_spec.handler. */
6831
6832static tree
6833handle_noicf_attribute (tree *node, tree name,
6834 tree ARG_UNUSED (args),
6835 int ARG_UNUSED (flags), bool *no_add_attrs)
6836{
6837 if (TREE_CODE (*node) != FUNCTION_DECL)
6838 {
6839 warning (OPT_Wattributes, "%qE attribute ignored", name);
6840 *no_add_attrs = true;
6841 }
6842
6843 return NULL_TREE;
6844}
6845
6846
349ae713
NB
6847/* Handle a "always_inline" attribute; arguments as in
6848 struct attribute_spec.handler. */
6849
6850static tree
35b1a6fa 6851handle_always_inline_attribute (tree *node, tree name,
e18476eb
BI
6852 tree ARG_UNUSED (args),
6853 int ARG_UNUSED (flags),
a742c759 6854 bool *no_add_attrs)
349ae713
NB
6855{
6856 if (TREE_CODE (*node) == FUNCTION_DECL)
6857 {
45484dcf
MP
6858 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6859 {
6860 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6861 "with %qs attribute", name, "noinline");
6862 *no_add_attrs = true;
6863 }
3b1661a9
ES
6864 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
6865 {
6866 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6867 "with %qs attribute", name, "target_clones");
6868 *no_add_attrs = true;
6869 }
45484dcf
MP
6870 else
6871 /* Set the attribute and mark it for disregarding inline
6872 limits. */
6873 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
349ae713
NB
6874 }
6875 else
6876 {
5c498b10 6877 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713 6878 *no_add_attrs = true;
4eb7fd83
JJ
6879 }
6880
6881 return NULL_TREE;
6882}
6883
6884/* Handle a "gnu_inline" attribute; arguments as in
6885 struct attribute_spec.handler. */
6886
6887static tree
6888handle_gnu_inline_attribute (tree *node, tree name,
6889 tree ARG_UNUSED (args),
6890 int ARG_UNUSED (flags),
6891 bool *no_add_attrs)
6892{
6893 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6894 {
6895 /* Do nothing else, just set the attribute. We'll get at
6896 it later with lookup_attribute. */
6897 }
6898 else
6899 {
6900 warning (OPT_Wattributes, "%qE attribute ignored", name);
6901 *no_add_attrs = true;
46a4da10
JH
6902 }
6903
6904 return NULL_TREE;
6905}
6906
6907/* Handle a "leaf" attribute; arguments as in
6908 struct attribute_spec.handler. */
6909
6910static tree
6911handle_leaf_attribute (tree *node, tree name,
6912 tree ARG_UNUSED (args),
6913 int ARG_UNUSED (flags), bool *no_add_attrs)
6914{
6915 if (TREE_CODE (*node) != FUNCTION_DECL)
6916 {
6917 warning (OPT_Wattributes, "%qE attribute ignored", name);
6918 *no_add_attrs = true;
6919 }
6920 if (!TREE_PUBLIC (*node))
6921 {
6922 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6923 *no_add_attrs = true;
349ae713
NB
6924 }
6925
6926 return NULL_TREE;
6927}
6928
d752cfdb
JJ
6929/* Handle an "artificial" attribute; arguments as in
6930 struct attribute_spec.handler. */
6931
6932static tree
6933handle_artificial_attribute (tree *node, tree name,
6934 tree ARG_UNUSED (args),
6935 int ARG_UNUSED (flags),
6936 bool *no_add_attrs)
6937{
6938 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6939 {
6940 /* Do nothing else, just set the attribute. We'll get at
6941 it later with lookup_attribute. */
6942 }
6943 else
6944 {
6945 warning (OPT_Wattributes, "%qE attribute ignored", name);
6946 *no_add_attrs = true;
6947 }
6948
6949 return NULL_TREE;
6950}
6951
0691d1d4
RG
6952/* Handle a "flatten" attribute; arguments as in
6953 struct attribute_spec.handler. */
6954
6955static tree
6956handle_flatten_attribute (tree *node, tree name,
c22cacf3
MS
6957 tree args ATTRIBUTE_UNUSED,
6958 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0691d1d4
RG
6959{
6960 if (TREE_CODE (*node) == FUNCTION_DECL)
6961 /* Do nothing else, just set the attribute. We'll get at
6962 it later with lookup_attribute. */
6963 ;
6964 else
6965 {
6966 warning (OPT_Wattributes, "%qE attribute ignored", name);
6967 *no_add_attrs = true;
6968 }
6969
6970 return NULL_TREE;
6971}
6972
d2af6a68
JJ
6973/* Handle a "warning" or "error" attribute; arguments as in
6974 struct attribute_spec.handler. */
6975
6976static tree
6977handle_error_attribute (tree *node, tree name, tree args,
6978 int ARG_UNUSED (flags), bool *no_add_attrs)
6979{
6980 if (TREE_CODE (*node) == FUNCTION_DECL
cf35e2b1 6981 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
d2af6a68
JJ
6982 /* Do nothing else, just set the attribute. We'll get at
6983 it later with lookup_attribute. */
6984 ;
6985 else
6986 {
6987 warning (OPT_Wattributes, "%qE attribute ignored", name);
6988 *no_add_attrs = true;
6989 }
6990
6991 return NULL_TREE;
6992}
0691d1d4 6993
349ae713
NB
6994/* Handle a "used" attribute; arguments as in
6995 struct attribute_spec.handler. */
6996
6997static tree
e18476eb
BI
6998handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6999 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 7000{
d7ddbe24
RH
7001 tree node = *pnode;
7002
7003 if (TREE_CODE (node) == FUNCTION_DECL
0ae9bd27 7004 || (VAR_P (node) && TREE_STATIC (node))
3797cb21 7005 || (TREE_CODE (node) == TYPE_DECL))
4d7d0451 7006 {
4d7d0451 7007 TREE_USED (node) = 1;
8e3e233b 7008 DECL_PRESERVE_P (node) = 1;
0ae9bd27 7009 if (VAR_P (node))
ebfbbdc5 7010 DECL_READ_P (node) = 1;
4d7d0451 7011 }
349ae713
NB
7012 else
7013 {
5c498b10 7014 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7015 *no_add_attrs = true;
7016 }
7017
7018 return NULL_TREE;
7019}
7020
7021/* Handle a "unused" attribute; arguments as in
7022 struct attribute_spec.handler. */
7023
7024static tree
e18476eb
BI
7025handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7026 int flags, bool *no_add_attrs)
349ae713
NB
7027{
7028 if (DECL_P (*node))
7029 {
7030 tree decl = *node;
7031
7032 if (TREE_CODE (decl) == PARM_DECL
af05e6e5 7033 || VAR_OR_FUNCTION_DECL_P (decl)
349ae713
NB
7034 || TREE_CODE (decl) == LABEL_DECL
7035 || TREE_CODE (decl) == TYPE_DECL)
ebfbbdc5
JJ
7036 {
7037 TREE_USED (decl) = 1;
0ae9bd27 7038 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
ebfbbdc5
JJ
7039 DECL_READ_P (decl) = 1;
7040 }
349ae713
NB
7041 else
7042 {
5c498b10 7043 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7044 *no_add_attrs = true;
7045 }
7046 }
7047 else
7048 {
7049 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 7050 *node = build_variant_type_copy (*node);
349ae713
NB
7051 TREE_USED (*node) = 1;
7052 }
7053
7054 return NULL_TREE;
7055}
7056
ce91e74c
JH
7057/* Handle a "externally_visible" attribute; arguments as in
7058 struct attribute_spec.handler. */
7059
7060static tree
7061handle_externally_visible_attribute (tree *pnode, tree name,
7062 tree ARG_UNUSED (args),
7063 int ARG_UNUSED (flags),
7064 bool *no_add_attrs)
7065{
7066 tree node = *pnode;
7067
21b634ae 7068 if (VAR_OR_FUNCTION_DECL_P (node))
ce91e74c 7069 {
343d4b27
JJ
7070 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7071 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7072 {
7073 warning (OPT_Wattributes,
7074 "%qE attribute have effect only on public objects", name);
7075 *no_add_attrs = true;
7076 }
ce91e74c 7077 }
ce91e74c
JH
7078 else
7079 {
7080 warning (OPT_Wattributes, "%qE attribute ignored", name);
7081 *no_add_attrs = true;
7082 }
7083
7084 return NULL_TREE;
7085}
7086
7861b648
AK
7087/* Handle the "no_reorder" attribute. Arguments as in
7088 struct attribute_spec.handler. */
7089
7090static tree
7091handle_no_reorder_attribute (tree *pnode,
7092 tree name,
7093 tree,
7094 int,
7095 bool *no_add_attrs)
7096{
7097 tree node = *pnode;
7098
21b634ae 7099 if (!VAR_OR_FUNCTION_DECL_P (node)
7861b648
AK
7100 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7101 {
7102 warning (OPT_Wattributes,
7103 "%qE attribute only affects top level objects",
7104 name);
7105 *no_add_attrs = true;
7106 }
7107
7108 return NULL_TREE;
7109}
7110
349ae713
NB
7111/* Handle a "const" attribute; arguments as in
7112 struct attribute_spec.handler. */
7113
7114static tree
e18476eb
BI
7115handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7116 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7117{
7118 tree type = TREE_TYPE (*node);
7119
7120 /* See FIXME comment on noreturn in c_common_attribute_table. */
7121 if (TREE_CODE (*node) == FUNCTION_DECL)
7122 TREE_READONLY (*node) = 1;
7123 else if (TREE_CODE (type) == POINTER_TYPE
7124 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7125 TREE_TYPE (*node)
6e7ceb17
PC
7126 = (build_qualified_type
7127 (build_pointer_type
7128 (build_type_variant (TREE_TYPE (type), 1,
7129 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7130 TYPE_QUALS (type)));
349ae713
NB
7131 else
7132 {
5c498b10 7133 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7134 *no_add_attrs = true;
7135 }
7136
7137 return NULL_TREE;
7138}
7139
ee45a32d
EB
7140/* Handle a "scalar_storage_order" attribute; arguments as in
7141 struct attribute_spec.handler. */
7142
7143static tree
7144handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7145 int flags, bool *no_add_attrs)
7146{
7147 tree id = TREE_VALUE (args);
7148 tree type;
7149
7150 if (TREE_CODE (*node) == TYPE_DECL
7151 && ! (flags & ATTR_FLAG_CXX11))
7152 node = &TREE_TYPE (*node);
7153 type = *node;
7154
7155 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7156 {
7157 error ("scalar_storage_order is not supported because endianness "
7158 "is not uniform");
7159 return NULL_TREE;
7160 }
7161
7162 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7163 {
7164 bool reverse = false;
7165
7166 if (TREE_CODE (id) == STRING_CST
7167 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7168 reverse = !BYTES_BIG_ENDIAN;
7169 else if (TREE_CODE (id) == STRING_CST
7170 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7171 reverse = BYTES_BIG_ENDIAN;
7172 else
7173 {
7174 error ("scalar_storage_order argument must be one of \"big-endian\""
7175 " or \"little-endian\"");
7176 return NULL_TREE;
7177 }
7178
7179 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7180 {
7181 if (reverse)
7182 /* A type variant isn't good enough, since we don't want a cast
7183 to such a type to be removed as a no-op. */
7184 *node = type = build_duplicate_type (type);
7185 }
7186
7187 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7188 return NULL_TREE;
7189 }
7190
7191 warning (OPT_Wattributes, "%qE attribute ignored", name);
7192 *no_add_attrs = true;
7193 return NULL_TREE;
7194}
7195
349ae713
NB
7196/* Handle a "transparent_union" attribute; arguments as in
7197 struct attribute_spec.handler. */
7198
7199static tree
35b1a6fa 7200handle_transparent_union_attribute (tree *node, tree name,
e18476eb 7201 tree ARG_UNUSED (args), int flags,
a742c759 7202 bool *no_add_attrs)
349ae713 7203{
4009f2e7 7204 tree type;
52dd234b
RH
7205
7206 *no_add_attrs = true;
349ae713 7207
e28d52cf
DS
7208 if (TREE_CODE (*node) == TYPE_DECL
7209 && ! (flags & ATTR_FLAG_CXX11))
4009f2e7
JM
7210 node = &TREE_TYPE (*node);
7211 type = *node;
349ae713 7212
52dd234b 7213 if (TREE_CODE (type) == UNION_TYPE)
349ae713 7214 {
d58d6eb5
JM
7215 /* Make sure that the first field will work for a transparent union.
7216 If the type isn't complete yet, leave the check to the code in
7217 finish_struct. */
7218 if (TYPE_SIZE (type))
7219 {
7220 tree first = first_field (type);
7221 if (first == NULL_TREE
7222 || DECL_ARTIFICIAL (first)
7223 || TYPE_MODE (type) != DECL_MODE (first))
7224 goto ignored;
7225 }
7226
349ae713 7227 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
52dd234b 7228 {
d58d6eb5
JM
7229 /* If the type isn't complete yet, setting the flag
7230 on a variant wouldn't ever be checked. */
7231 if (!TYPE_SIZE (type))
7232 goto ignored;
7233
7234 /* build_duplicate_type doesn't work for C++. */
7235 if (c_dialect_cxx ())
52dd234b
RH
7236 goto ignored;
7237
ee45a32d
EB
7238 /* A type variant isn't good enough, since we don't want a cast
7239 to such a type to be removed as a no-op. */
52dd234b
RH
7240 *node = type = build_duplicate_type (type);
7241 }
7242
75c8aac3
JH
7243 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7244 TYPE_TRANSPARENT_AGGR (t) = 1;
52dd234b 7245 return NULL_TREE;
349ae713
NB
7246 }
7247
52dd234b
RH
7248 ignored:
7249 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7250 return NULL_TREE;
7251}
7252
fc8600f9
MM
7253/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7254 get the requested priority for a constructor or destructor,
7255 possibly issuing diagnostics for invalid or reserved
7256 priorities. */
7257
7258static priority_type
7259get_priority (tree args, bool is_destructor)
7260{
7261 HOST_WIDE_INT pri;
b2f4bed8 7262 tree arg;
fc8600f9
MM
7263
7264 if (!args)
7265 return DEFAULT_INIT_PRIORITY;
b8698a0f 7266
f6fc5c86
MM
7267 if (!SUPPORTS_INIT_PRIORITY)
7268 {
7269 if (is_destructor)
7270 error ("destructor priorities are not supported");
7271 else
7272 error ("constructor priorities are not supported");
7273 return DEFAULT_INIT_PRIORITY;
7274 }
7275
b2f4bed8 7276 arg = TREE_VALUE (args);
fad7652e
JJ
7277 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7278 goto invalid;
7279 if (arg == error_mark_node)
7280 return DEFAULT_INIT_PRIORITY;
8d0d1915 7281 arg = default_conversion (arg);
9541ffee 7282 if (!tree_fits_shwi_p (arg)
b2f4bed8 7283 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
fc8600f9
MM
7284 goto invalid;
7285
9439e9a1 7286 pri = tree_to_shwi (arg);
fc8600f9
MM
7287 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7288 goto invalid;
7289
7290 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7291 {
7292 if (is_destructor)
7293 warning (0,
7294 "destructor priorities from 0 to %d are reserved "
b8698a0f 7295 "for the implementation",
fc8600f9
MM
7296 MAX_RESERVED_INIT_PRIORITY);
7297 else
7298 warning (0,
7299 "constructor priorities from 0 to %d are reserved "
b8698a0f 7300 "for the implementation",
fc8600f9
MM
7301 MAX_RESERVED_INIT_PRIORITY);
7302 }
7303 return pri;
7304
7305 invalid:
7306 if (is_destructor)
7307 error ("destructor priorities must be integers from 0 to %d inclusive",
7308 MAX_INIT_PRIORITY);
7309 else
7310 error ("constructor priorities must be integers from 0 to %d inclusive",
7311 MAX_INIT_PRIORITY);
7312 return DEFAULT_INIT_PRIORITY;
7313}
7314
349ae713
NB
7315/* Handle a "constructor" attribute; arguments as in
7316 struct attribute_spec.handler. */
7317
7318static tree
fc8600f9 7319handle_constructor_attribute (tree *node, tree name, tree args,
e18476eb 7320 int ARG_UNUSED (flags),
a742c759 7321 bool *no_add_attrs)
349ae713
NB
7322{
7323 tree decl = *node;
7324 tree type = TREE_TYPE (decl);
7325
7326 if (TREE_CODE (decl) == FUNCTION_DECL
7327 && TREE_CODE (type) == FUNCTION_TYPE
7328 && decl_function_context (decl) == 0)
7329 {
fc8600f9 7330 priority_type priority;
349ae713 7331 DECL_STATIC_CONSTRUCTOR (decl) = 1;
fc8600f9
MM
7332 priority = get_priority (args, /*is_destructor=*/false);
7333 SET_DECL_INIT_PRIORITY (decl, priority);
349ae713
NB
7334 TREE_USED (decl) = 1;
7335 }
7336 else
7337 {
5c498b10 7338 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7339 *no_add_attrs = true;
7340 }
7341
7342 return NULL_TREE;
7343}
7344
7345/* Handle a "destructor" attribute; arguments as in
7346 struct attribute_spec.handler. */
7347
7348static tree
fc8600f9 7349handle_destructor_attribute (tree *node, tree name, tree args,
e18476eb 7350 int ARG_UNUSED (flags),
a742c759 7351 bool *no_add_attrs)
349ae713
NB
7352{
7353 tree decl = *node;
7354 tree type = TREE_TYPE (decl);
7355
7356 if (TREE_CODE (decl) == FUNCTION_DECL
7357 && TREE_CODE (type) == FUNCTION_TYPE
7358 && decl_function_context (decl) == 0)
7359 {
fc8600f9 7360 priority_type priority;
349ae713 7361 DECL_STATIC_DESTRUCTOR (decl) = 1;
fc8600f9
MM
7362 priority = get_priority (args, /*is_destructor=*/true);
7363 SET_DECL_FINI_PRIORITY (decl, priority);
349ae713
NB
7364 TREE_USED (decl) = 1;
7365 }
7366 else
7367 {
5c498b10 7368 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7369 *no_add_attrs = true;
7370 }
7371
7372 return NULL_TREE;
7373}
7374
4d451982
MLI
7375/* Nonzero if the mode is a valid vector mode for this architecture.
7376 This returns nonzero even if there is no hardware support for the
7377 vector mode, but we can emulate with narrower modes. */
7378
7379static int
ef4bddc2 7380vector_mode_valid_p (machine_mode mode)
4d451982
MLI
7381{
7382 enum mode_class mclass = GET_MODE_CLASS (mode);
ef4bddc2 7383 machine_mode innermode;
4d451982
MLI
7384
7385 /* Doh! What's going on? */
7386 if (mclass != MODE_VECTOR_INT
7387 && mclass != MODE_VECTOR_FLOAT
7388 && mclass != MODE_VECTOR_FRACT
7389 && mclass != MODE_VECTOR_UFRACT
7390 && mclass != MODE_VECTOR_ACCUM
7391 && mclass != MODE_VECTOR_UACCUM)
7392 return 0;
7393
7394 /* Hardware support. Woo hoo! */
7395 if (targetm.vector_mode_supported_p (mode))
7396 return 1;
7397
7398 innermode = GET_MODE_INNER (mode);
7399
7400 /* We should probably return 1 if requesting V4DI and we have no DI,
7401 but we have V2DI, but this is probably very unlikely. */
7402
7403 /* If we have support for the inner mode, we can safely emulate it.
7404 We may not have V2DI, but me can emulate with a pair of DIs. */
7405 return targetm.scalar_mode_supported_p (innermode);
7406}
7407
7408
349ae713
NB
7409/* Handle a "mode" attribute; arguments as in
7410 struct attribute_spec.handler. */
7411
7412static tree
e18476eb
BI
7413handle_mode_attribute (tree *node, tree name, tree args,
7414 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7415{
7416 tree type = *node;
88388a52 7417 tree ident = TREE_VALUE (args);
349ae713
NB
7418
7419 *no_add_attrs = true;
7420
88388a52 7421 if (TREE_CODE (ident) != IDENTIFIER_NODE)
5c498b10 7422 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7423 else
7424 {
7425 int j;
88388a52 7426 const char *p = IDENTIFIER_POINTER (ident);
349ae713 7427 int len = strlen (p);
ef4bddc2 7428 machine_mode mode = VOIDmode;
349ae713 7429 tree typefm;
6dd53648 7430 bool valid_mode;
349ae713
NB
7431
7432 if (len > 4 && p[0] == '_' && p[1] == '_'
7433 && p[len - 1] == '_' && p[len - 2] == '_')
7434 {
28dab132 7435 char *newp = (char *) alloca (len - 1);
349ae713
NB
7436
7437 strcpy (newp, &p[2]);
7438 newp[len - 4] = '\0';
7439 p = newp;
7440 }
7441
7442 /* Change this type to have a type with the specified mode.
7443 First check for the special modes. */
3f75a254 7444 if (!strcmp (p, "byte"))
349ae713
NB
7445 mode = byte_mode;
7446 else if (!strcmp (p, "word"))
7447 mode = word_mode;
3f75a254 7448 else if (!strcmp (p, "pointer"))
349ae713 7449 mode = ptr_mode;
c7ff6e7a
AK
7450 else if (!strcmp (p, "libgcc_cmp_return"))
7451 mode = targetm.libgcc_cmp_return_mode ();
7452 else if (!strcmp (p, "libgcc_shift_count"))
7453 mode = targetm.libgcc_shift_count_mode ();
7b0518e3
UW
7454 else if (!strcmp (p, "unwind_word"))
7455 mode = targetm.unwind_word_mode ();
349ae713
NB
7456 else
7457 for (j = 0; j < NUM_MACHINE_MODES; j++)
7458 if (!strcmp (p, GET_MODE_NAME (j)))
61f03aba 7459 {
ef4bddc2 7460 mode = (machine_mode) j;
61f03aba
RH
7461 break;
7462 }
349ae713
NB
7463
7464 if (mode == VOIDmode)
4a5eab38 7465 {
88388a52 7466 error ("unknown machine mode %qE", ident);
4a5eab38
PB
7467 return NULL_TREE;
7468 }
7469
6dd53648
RH
7470 valid_mode = false;
7471 switch (GET_MODE_CLASS (mode))
4a5eab38 7472 {
6dd53648
RH
7473 case MODE_INT:
7474 case MODE_PARTIAL_INT:
7475 case MODE_FLOAT:
9a8ce21f 7476 case MODE_DECIMAL_FLOAT:
ab22c1fa
CF
7477 case MODE_FRACT:
7478 case MODE_UFRACT:
7479 case MODE_ACCUM:
7480 case MODE_UACCUM:
6dd53648
RH
7481 valid_mode = targetm.scalar_mode_supported_p (mode);
7482 break;
7483
7484 case MODE_COMPLEX_INT:
7485 case MODE_COMPLEX_FLOAT:
7486 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7487 break;
7488
7489 case MODE_VECTOR_INT:
7490 case MODE_VECTOR_FLOAT:
ab22c1fa
CF
7491 case MODE_VECTOR_FRACT:
7492 case MODE_VECTOR_UFRACT:
7493 case MODE_VECTOR_ACCUM:
7494 case MODE_VECTOR_UACCUM:
5c498b10
DD
7495 warning (OPT_Wattributes, "specifying vector types with "
7496 "__attribute__ ((mode)) is deprecated");
7497 warning (OPT_Wattributes,
7498 "use __attribute__ ((vector_size)) instead");
6dd53648
RH
7499 valid_mode = vector_mode_valid_p (mode);
7500 break;
4a5eab38 7501
6dd53648
RH
7502 default:
7503 break;
7504 }
7505 if (!valid_mode)
7506 {
9e637a26 7507 error ("unable to emulate %qs", p);
6dd53648
RH
7508 return NULL_TREE;
7509 }
4a5eab38 7510
6dd53648 7511 if (POINTER_TYPE_P (type))
cb2a532e 7512 {
36c5e70a 7513 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
ef4bddc2 7514 tree (*fn)(tree, machine_mode, bool);
6dd53648 7515
36c5e70a 7516 if (!targetm.addr_space.valid_pointer_mode (mode, as))
cb2a532e 7517 {
9e637a26 7518 error ("invalid pointer mode %qs", p);
cb2a532e
AH
7519 return NULL_TREE;
7520 }
7521
c22cacf3 7522 if (TREE_CODE (type) == POINTER_TYPE)
6dd53648 7523 fn = build_pointer_type_for_mode;
4977bab6 7524 else
6dd53648
RH
7525 fn = build_reference_type_for_mode;
7526 typefm = fn (TREE_TYPE (type), mode, false);
cb2a532e 7527 }
6dd53648 7528 else
ab22c1fa
CF
7529 {
7530 /* For fixed-point modes, we need to test if the signness of type
7531 and the machine mode are consistent. */
7532 if (ALL_FIXED_POINT_MODE_P (mode)
7533 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7534 {
d8a07487 7535 error ("signedness of type and machine mode %qs don%'t match", p);
ab22c1fa
CF
7536 return NULL_TREE;
7537 }
7538 /* For fixed-point modes, we need to pass saturating info. */
7539 typefm = lang_hooks.types.type_for_mode (mode,
7540 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7541 : TYPE_UNSIGNED (type));
7542 }
ec8465a5 7543
6dd53648
RH
7544 if (typefm == NULL_TREE)
7545 {
61f03aba 7546 error ("no data type for mode %qs", p);
6dd53648
RH
7547 return NULL_TREE;
7548 }
ec8465a5
RK
7549 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7550 {
7551 /* For enumeral types, copy the precision from the integer
7552 type returned above. If not an INTEGER_TYPE, we can't use
7553 this mode for this type. */
7554 if (TREE_CODE (typefm) != INTEGER_TYPE)
7555 {
61f03aba 7556 error ("cannot use mode %qs for enumeral types", p);
ec8465a5
RK
7557 return NULL_TREE;
7558 }
7559
99db1ef0
RH
7560 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7561 {
7562 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7563 typefm = type;
7564 }
7565 else
7566 {
7567 /* We cannot build a type variant, as there's code that assumes
7568 that TYPE_MAIN_VARIANT has the same mode. This includes the
7569 debug generators. Instead, create a subrange type. This
7570 results in all of the enumeral values being emitted only once
7571 in the original, and the subtype gets them by reference. */
7572 if (TYPE_UNSIGNED (type))
7573 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7574 else
7575 typefm = make_signed_type (TYPE_PRECISION (typefm));
7576 TREE_TYPE (typefm) = type;
7577 }
ec8465a5 7578 }
a2d36602
RH
7579 else if (VECTOR_MODE_P (mode)
7580 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7581 : TREE_CODE (type) != TREE_CODE (typefm))
61f03aba
RH
7582 {
7583 error ("mode %qs applied to inappropriate type", p);
7584 return NULL_TREE;
7585 }
7586
6dd53648 7587 *node = typefm;
349ae713
NB
7588 }
7589
7590 return NULL_TREE;
7591}
7592
7593/* Handle a "section" attribute; arguments as in
7594 struct attribute_spec.handler. */
7595
7596static tree
e18476eb
BI
7597handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7598 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7599{
7600 tree decl = *node;
7601
0373796b 7602 if (!targetm_common.have_named_sections)
349ae713 7603 {
0373796b
JT
7604 error_at (DECL_SOURCE_LOCATION (*node),
7605 "section attributes are not supported for this target");
7606 goto fail;
7607 }
9fb32434 7608
0373796b 7609 user_defined_section_attribute = true;
349ae713 7610
21b634ae 7611 if (!VAR_OR_FUNCTION_DECL_P (decl))
0373796b
JT
7612 {
7613 error ("section attribute not allowed for %q+D", *node);
7614 goto fail;
349ae713 7615 }
0373796b
JT
7616
7617 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
349ae713 7618 {
0373796b
JT
7619 error ("section attribute argument not a string constant");
7620 goto fail;
349ae713
NB
7621 }
7622
0ae9bd27 7623 if (VAR_P (decl)
0373796b
JT
7624 && current_function_decl != NULL_TREE
7625 && !TREE_STATIC (decl))
7626 {
7627 error_at (DECL_SOURCE_LOCATION (decl),
7628 "section attribute cannot be specified for local variables");
7629 goto fail;
7630 }
7631
7632 /* The decl may have already been given a section attribute
7633 from a previous declaration. Ensure they match. */
7634 if (DECL_SECTION_NAME (decl) != NULL
7635 && strcmp (DECL_SECTION_NAME (decl),
7636 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7637 {
7638 error ("section of %q+D conflicts with previous declaration", *node);
7639 goto fail;
7640 }
7641
0ae9bd27 7642 if (VAR_P (decl)
0373796b
JT
7643 && !targetm.have_tls && targetm.emutls.tmpl_section
7644 && DECL_THREAD_LOCAL_P (decl))
7645 {
7646 error ("section of %q+D cannot be overridden", *node);
7647 goto fail;
7648 }
7649
7650 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7651 return NULL_TREE;
7652
7653fail:
7654 *no_add_attrs = true;
349ae713
NB
7655 return NULL_TREE;
7656}
7657
d19fa6b5
JM
7658/* Check whether ALIGN is a valid user-specified alignment. If so,
7659 return its base-2 log; if not, output an error and return -1. If
7660 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7661 no error. */
7662int
7663check_user_alignment (const_tree align, bool allow_zero)
7664{
7665 int i;
7666
661a0813
MP
7667 if (error_operand_p (align))
7668 return -1;
a859517f
MP
7669 if (TREE_CODE (align) != INTEGER_CST
7670 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
d19fa6b5
JM
7671 {
7672 error ("requested alignment is not an integer constant");
7673 return -1;
7674 }
7675 else if (allow_zero && integer_zerop (align))
7676 return -1;
3f12f6e9
SKS
7677 else if (tree_int_cst_sgn (align) == -1
7678 || (i = tree_log2 (align)) == -1)
d19fa6b5 7679 {
3f12f6e9 7680 error ("requested alignment is not a positive power of 2");
d19fa6b5
JM
7681 return -1;
7682 }
7683 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7684 {
7685 error ("requested alignment is too large");
7686 return -1;
7687 }
7688 return i;
7689}
7690
e28d52cf
DS
7691/*
7692 If in c++-11, check if the c++-11 alignment constraint with respect
7693 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7694 c++-11 mode, does nothing.
7695
7696 [dcl.align]2/ says:
7697
7698 [* if the constant expression evaluates to a fundamental alignment,
7699 the alignment requirement of the declared entity shall be the
7700 specified fundamental alignment.
7701
7702 * if the constant expression evaluates to an extended alignment
7703 and the implementation supports that alignment in the context
7704 of the declaration, the alignment of the declared entity shall
7705 be that alignment
7706
7707 * if the constant expression evaluates to an extended alignment
7708 and the implementation does not support that alignment in the
7709 context of the declaration, the program is ill-formed]. */
7710
7711static bool
7712check_cxx_fundamental_alignment_constraints (tree node,
7713 unsigned align_log,
7714 int flags)
7715{
7716 bool alignment_too_large_p = false;
7717 unsigned requested_alignment = 1U << align_log;
7718 unsigned max_align = 0;
7719
7720 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7721 || (node == NULL_TREE || node == error_mark_node))
7722 return true;
7723
7724 if (cxx_fundamental_alignment_p (requested_alignment))
7725 return true;
7726
7727 if (DECL_P (node))
7728 {
7729 if (TREE_STATIC (node))
7730 {
7731 /* For file scope variables and static members, the target
7732 supports alignments that are at most
7733 MAX_OFILE_ALIGNMENT. */
7734 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7735 alignment_too_large_p = true;
7736 }
7737 else
7738 {
7739#ifdef BIGGEST_FIELD_ALIGNMENT
7740#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7741#else
7742#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7743#endif
7744 /* For non-static members, the target supports either
7745 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7746 if it is defined or BIGGEST_ALIGNMENT. */
7747 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7748 if (TREE_CODE (node) == FIELD_DECL
7749 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7750 alignment_too_large_p = true;
7751#undef MAX_TARGET_FIELD_ALIGNMENT
7752 /* For stack variables, the target supports at most
7753 MAX_STACK_ALIGNMENT. */
7754 else if (decl_function_context (node) != NULL
7755 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7756 alignment_too_large_p = true;
7757 }
7758 }
7759 else if (TYPE_P (node))
7760 {
7761 /* Let's be liberal for types. */
7762 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7763 alignment_too_large_p = true;
7764 }
7765
7766 if (alignment_too_large_p)
7767 pedwarn (input_location, OPT_Wattributes,
7768 "requested alignment %d is larger than %d",
7769 requested_alignment, max_align);
7770
7771 return !alignment_too_large_p;
7772}
7773
349ae713
NB
7774/* Handle a "aligned" attribute; arguments as in
7775 struct attribute_spec.handler. */
7776
7777static tree
e18476eb 7778handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
a742c759 7779 int flags, bool *no_add_attrs)
349ae713
NB
7780{
7781 tree decl = NULL_TREE;
7782 tree *type = NULL;
7783 int is_type = 0;
5d77fb19 7784 tree align_expr;
349ae713
NB
7785 int i;
7786
5d77fb19
MG
7787 if (args)
7788 {
7789 align_expr = TREE_VALUE (args);
661a0813
MP
7790 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7791 && TREE_CODE (align_expr) != FUNCTION_DECL)
5d77fb19
MG
7792 align_expr = default_conversion (align_expr);
7793 }
7794 else
7795 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7796
349ae713
NB
7797 if (DECL_P (*node))
7798 {
7799 decl = *node;
7800 type = &TREE_TYPE (decl);
7801 is_type = TREE_CODE (*node) == TYPE_DECL;
7802 }
7803 else if (TYPE_P (*node))
7804 type = node, is_type = 1;
7805
e28d52cf
DS
7806 if ((i = check_user_alignment (align_expr, false)) == -1
7807 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
d19fa6b5 7808 *no_add_attrs = true;
349ae713
NB
7809 else if (is_type)
7810 {
0f559c16
JM
7811 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7812 /* OK, modify the type in place. */;
349ae713
NB
7813 /* If we have a TYPE_DECL, then copy the type, so that we
7814 don't accidentally modify a builtin type. See pushdecl. */
0f559c16
JM
7815 else if (decl && TREE_TYPE (decl) != error_mark_node
7816 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
349ae713
NB
7817 {
7818 tree tt = TREE_TYPE (decl);
8dd16ecc 7819 *type = build_variant_type_copy (*type);
349ae713
NB
7820 DECL_ORIGINAL_TYPE (decl) = tt;
7821 TYPE_NAME (*type) = decl;
7822 TREE_USED (*type) = TREE_USED (decl);
7823 TREE_TYPE (decl) = *type;
7824 }
0f559c16 7825 else
8dd16ecc 7826 *type = build_variant_type_copy (*type);
349ae713 7827
d9223014 7828 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
349ae713
NB
7829 TYPE_USER_ALIGN (*type) = 1;
7830 }
837edd5f 7831 else if (! VAR_OR_FUNCTION_DECL_P (decl)
349ae713
NB
7832 && TREE_CODE (decl) != FIELD_DECL)
7833 {
dee15844 7834 error ("alignment may not be specified for %q+D", decl);
349ae713
NB
7835 *no_add_attrs = true;
7836 }
e28d52cf
DS
7837 else if (DECL_USER_ALIGN (decl)
7838 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7839 /* C++-11 [dcl.align/4]:
7840
7841 When multiple alignment-specifiers are specified for an
7842 entity, the alignment requirement shall be set to the
7843 strictest specified alignment.
7844
7845 This formally comes from the c++11 specification but we are
7846 doing it for the GNU attribute syntax as well. */
7847 *no_add_attrs = true;
837edd5f 7848 else if (TREE_CODE (decl) == FUNCTION_DECL
d9223014 7849 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
837edd5f
GK
7850 {
7851 if (DECL_USER_ALIGN (decl))
7852 error ("alignment for %q+D was previously specified as %d "
7853 "and may not be decreased", decl,
7854 DECL_ALIGN (decl) / BITS_PER_UNIT);
7855 else
7856 error ("alignment for %q+D must be at least %d", decl,
7857 DECL_ALIGN (decl) / BITS_PER_UNIT);
7858 *no_add_attrs = true;
7859 }
349ae713
NB
7860 else
7861 {
d9223014 7862 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
349ae713
NB
7863 DECL_USER_ALIGN (decl) = 1;
7864 }
7865
7866 return NULL_TREE;
7867}
7868
7869/* Handle a "weak" attribute; arguments as in
7870 struct attribute_spec.handler. */
7871
7872static tree
55af93a8 7873handle_weak_attribute (tree *node, tree name,
e18476eb
BI
7874 tree ARG_UNUSED (args),
7875 int ARG_UNUSED (flags),
7876 bool * ARG_UNUSED (no_add_attrs))
349ae713 7877{
55af93a8 7878 if (TREE_CODE (*node) == FUNCTION_DECL
6b4e94bc
RG
7879 && DECL_DECLARED_INLINE_P (*node))
7880 {
2aa9c6ae 7881 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6b4e94bc
RG
7882 *no_add_attrs = true;
7883 }
ba885ec5
NS
7884 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7885 {
7886 error ("indirect function %q+D cannot be declared weak", *node);
7887 *no_add_attrs = true;
7888 return NULL_TREE;
7889 }
21b634ae 7890 else if (VAR_OR_FUNCTION_DECL_P (*node))
c316b5e4 7891 declare_weak (*node);
55af93a8
DS
7892 else
7893 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7894
7895 return NULL_TREE;
7896}
7897
4bb794e2
ST
7898/* Handle a "noplt" attribute; arguments as in
7899 struct attribute_spec.handler. */
7900
7901static tree
7902handle_noplt_attribute (tree *node, tree name,
7903 tree ARG_UNUSED (args),
7904 int ARG_UNUSED (flags),
7905 bool * ARG_UNUSED (no_add_attrs))
7906{
7907 if (TREE_CODE (*node) != FUNCTION_DECL)
7908 {
7909 warning (OPT_Wattributes,
7910 "%qE attribute is only applicable on functions", name);
7911 *no_add_attrs = true;
7912 return NULL_TREE;
7913 }
7914 return NULL_TREE;
7915}
7916
ba885ec5
NS
7917/* Handle an "alias" or "ifunc" attribute; arguments as in
7918 struct attribute_spec.handler, except that IS_ALIAS tells us
7919 whether this is an alias as opposed to ifunc attribute. */
349ae713
NB
7920
7921static tree
ba885ec5
NS
7922handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7923 bool *no_add_attrs)
349ae713
NB
7924{
7925 tree decl = *node;
7926
ba885ec5 7927 if (TREE_CODE (decl) != FUNCTION_DECL
0ae9bd27 7928 && (!is_alias || !VAR_P (decl)))
feab5a67
JM
7929 {
7930 warning (OPT_Wattributes, "%qE attribute ignored", name);
7931 *no_add_attrs = true;
7932 }
7933 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
b8698a0f 7934 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825
GK
7935 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7936 /* A static variable declaration is always a tentative definition,
7937 but the alias is a non-tentative definition which overrides. */
b8698a0f 7938 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825 7939 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
349ae713 7940 {
ba885ec5 7941 error ("%q+D defined both normally and as %qE attribute", decl, name);
349ae713 7942 *no_add_attrs = true;
ba885ec5 7943 return NULL_TREE;
349ae713 7944 }
ba885ec5
NS
7945 else if (!is_alias
7946 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7947 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7948 {
7949 error ("weak %q+D cannot be defined %qE", decl, name);
7950 *no_add_attrs = true;
7951 return NULL_TREE;
7952 }
f6a76b9f
RH
7953
7954 /* Note that the very first time we process a nested declaration,
7955 decl_function_context will not be set. Indeed, *would* never
7956 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7957 we do below. After such frobbery, pushdecl would set the context.
7958 In any case, this is never what we want. */
7959 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
349ae713
NB
7960 {
7961 tree id;
7962
7963 id = TREE_VALUE (args);
7964 if (TREE_CODE (id) != STRING_CST)
7965 {
ba885ec5 7966 error ("attribute %qE argument not a string", name);
349ae713
NB
7967 *no_add_attrs = true;
7968 return NULL_TREE;
7969 }
7970 id = get_identifier (TREE_STRING_POINTER (id));
7971 /* This counts as a use of the object pointed to. */
7972 TREE_USED (id) = 1;
7973
7974 if (TREE_CODE (decl) == FUNCTION_DECL)
7975 DECL_INITIAL (decl) = error_mark_node;
7976 else
08346abd 7977 TREE_STATIC (decl) = 1;
ba885ec5
NS
7978
7979 if (!is_alias)
7980 /* ifuncs are also aliases, so set that attribute too. */
7981 DECL_ATTRIBUTES (decl)
7982 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
349ae713
NB
7983 }
7984 else
7985 {
5c498b10 7986 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7987 *no_add_attrs = true;
7988 }
7989
f7217cde
JH
7990 if (decl_in_symtab_p (*node))
7991 {
7992 struct symtab_node *n = symtab_node::get (decl);
7993 if (n && n->refuse_visibility_changes)
7994 {
7995 if (is_alias)
7996 error ("%+D declared alias after being used", decl);
7997 else
7998 error ("%+D declared ifunc after being used", decl);
7999 }
8000 }
8001
8002
349ae713
NB
8003 return NULL_TREE;
8004}
8005
ba885ec5
NS
8006/* Handle an "alias" or "ifunc" attribute; arguments as in
8007 struct attribute_spec.handler. */
8008
8009static tree
8010handle_ifunc_attribute (tree *node, tree name, tree args,
8011 int ARG_UNUSED (flags), bool *no_add_attrs)
8012{
8013 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8014}
8015
8016/* Handle an "alias" or "ifunc" attribute; arguments as in
8017 struct attribute_spec.handler. */
8018
8019static tree
8020handle_alias_attribute (tree *node, tree name, tree args,
8021 int ARG_UNUSED (flags), bool *no_add_attrs)
8022{
8023 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8024}
8025
a0203ca7
AO
8026/* Handle a "weakref" attribute; arguments as in struct
8027 attribute_spec.handler. */
8028
8029static tree
8030handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8031 int flags, bool *no_add_attrs)
8032{
8033 tree attr = NULL_TREE;
8034
e1cf56b1
AO
8035 /* We must ignore the attribute when it is associated with
8036 local-scoped decls, since attribute alias is ignored and many
8037 such symbols do not even have a DECL_WEAK field. */
e7b012c0
JJ
8038 if (decl_function_context (*node)
8039 || current_function_decl
21b634ae 8040 || !VAR_OR_FUNCTION_DECL_P (*node))
e1cf56b1
AO
8041 {
8042 warning (OPT_Wattributes, "%qE attribute ignored", name);
8043 *no_add_attrs = true;
8044 return NULL_TREE;
8045 }
8046
ba885ec5
NS
8047 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8048 {
8049 error ("indirect function %q+D cannot be declared weakref", *node);
8050 *no_add_attrs = true;
8051 return NULL_TREE;
8052 }
8053
a0203ca7
AO
8054 /* The idea here is that `weakref("name")' mutates into `weakref,
8055 alias("name")', and weakref without arguments, in turn,
8056 implicitly adds weak. */
8057
8058 if (args)
8059 {
8060 attr = tree_cons (get_identifier ("alias"), args, attr);
8061 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8062
8063 *no_add_attrs = true;
a9b0b825
GK
8064
8065 decl_attributes (node, attr, flags);
a0203ca7
AO
8066 }
8067 else
8068 {
8069 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
c5d75364
MLI
8070 error_at (DECL_SOURCE_LOCATION (*node),
8071 "weakref attribute must appear before alias attribute");
a0203ca7 8072
a9b0b825
GK
8073 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8074 and that isn't supported; and because it wants to add it to
8075 the list of weak decls, which isn't helpful. */
8076 DECL_WEAK (*node) = 1;
a0203ca7
AO
8077 }
8078
f7217cde
JH
8079 if (decl_in_symtab_p (*node))
8080 {
8081 struct symtab_node *n = symtab_node::get (*node);
8082 if (n && n->refuse_visibility_changes)
8083 error ("%+D declared weakref after being used", *node);
8084 }
8085
a0203ca7
AO
8086 return NULL_TREE;
8087}
8088
349ae713
NB
8089/* Handle an "visibility" attribute; arguments as in
8090 struct attribute_spec.handler. */
8091
8092static tree
35b1a6fa 8093handle_visibility_attribute (tree *node, tree name, tree args,
e18476eb 8094 int ARG_UNUSED (flags),
b9e75696 8095 bool *ARG_UNUSED (no_add_attrs))
349ae713
NB
8096{
8097 tree decl = *node;
968b41a1 8098 tree id = TREE_VALUE (args);
b9e75696 8099 enum symbol_visibility vis;
349ae713 8100
d7afec4b
ND
8101 if (TYPE_P (*node))
8102 {
b9e75696
JM
8103 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8104 /* OK */;
a868811e 8105 else if (!RECORD_OR_UNION_TYPE_P (*node))
b9e75696
JM
8106 {
8107 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8108 name);
8109 return NULL_TREE;
8110 }
8111 else if (TYPE_FIELDS (*node))
8112 {
8113 error ("%qE attribute ignored because %qT is already defined",
8114 name, *node);
8115 return NULL_TREE;
8116 }
d7afec4b 8117 }
3f75a254 8118 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
349ae713 8119 {
5c498b10 8120 warning (OPT_Wattributes, "%qE attribute ignored", name);
968b41a1 8121 return NULL_TREE;
349ae713 8122 }
349ae713 8123
968b41a1
MA
8124 if (TREE_CODE (id) != STRING_CST)
8125 {
40b97a2e 8126 error ("visibility argument not a string");
968b41a1 8127 return NULL_TREE;
349ae713 8128 }
9f63daea 8129
d7afec4b
ND
8130 /* If this is a type, set the visibility on the type decl. */
8131 if (TYPE_P (decl))
8132 {
8133 decl = TYPE_NAME (decl);
3f75a254 8134 if (!decl)
c22cacf3 8135 return NULL_TREE;
e8233ac2
AP
8136 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8137 {
5c498b10 8138 warning (OPT_Wattributes, "%qE attribute ignored on types",
e8233ac2
AP
8139 name);
8140 return NULL_TREE;
8141 }
d7afec4b 8142 }
349ae713 8143
968b41a1 8144 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
b9e75696 8145 vis = VISIBILITY_DEFAULT;
968b41a1 8146 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
b9e75696 8147 vis = VISIBILITY_INTERNAL;
968b41a1 8148 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
b9e75696 8149 vis = VISIBILITY_HIDDEN;
968b41a1 8150 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
b9e75696 8151 vis = VISIBILITY_PROTECTED;
968b41a1 8152 else
b9e75696
JM
8153 {
8154 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8155 vis = VISIBILITY_DEFAULT;
8156 }
8157
8158 if (DECL_VISIBILITY_SPECIFIED (decl)
3a687f8b
MM
8159 && vis != DECL_VISIBILITY (decl))
8160 {
8161 tree attributes = (TYPE_P (*node)
8162 ? TYPE_ATTRIBUTES (*node)
8163 : DECL_ATTRIBUTES (decl));
8164 if (lookup_attribute ("visibility", attributes))
8165 error ("%qD redeclared with different visibility", decl);
8166 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8167 && lookup_attribute ("dllimport", attributes))
8168 error ("%qD was declared %qs which implies default visibility",
8169 decl, "dllimport");
8170 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8171 && lookup_attribute ("dllexport", attributes))
8172 error ("%qD was declared %qs which implies default visibility",
8173 decl, "dllexport");
8174 }
b9e75696
JM
8175
8176 DECL_VISIBILITY (decl) = vis;
d7afec4b
ND
8177 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8178
b9e75696
JM
8179 /* Go ahead and attach the attribute to the node as well. This is needed
8180 so we can determine whether we have VISIBILITY_DEFAULT because the
8181 visibility was not specified, or because it was explicitly overridden
8182 from the containing scope. */
968b41a1 8183
349ae713
NB
8184 return NULL_TREE;
8185}
8186
b2ca3702
MM
8187/* Determine the ELF symbol visibility for DECL, which is either a
8188 variable or a function. It is an error to use this function if a
8189 definition of DECL is not available in this translation unit.
8190 Returns true if the final visibility has been determined by this
8191 function; false if the caller is free to make additional
8192 modifications. */
8193
8194bool
8195c_determine_visibility (tree decl)
8196{
21b634ae 8197 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
b2ca3702
MM
8198
8199 /* If the user explicitly specified the visibility with an
8200 attribute, honor that. DECL_VISIBILITY will have been set during
6d87092d
JM
8201 the processing of the attribute. We check for an explicit
8202 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8203 to distinguish the use of an attribute from the use of a "#pragma
8204 GCC visibility push(...)"; in the latter case we still want other
8205 considerations to be able to overrule the #pragma. */
8206 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8207 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8208 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8209 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
b2ca3702
MM
8210 return true;
8211
b9e75696
JM
8212 /* Set default visibility to whatever the user supplied with
8213 visibility_specified depending on #pragma GCC visibility. */
8214 if (!DECL_VISIBILITY_SPECIFIED (decl))
8215 {
09812622
JJ
8216 if (visibility_options.inpragma
8217 || DECL_VISIBILITY (decl) != default_visibility)
8218 {
8219 DECL_VISIBILITY (decl) = default_visibility;
8220 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8221 /* If visibility changed and DECL already has DECL_RTL, ensure
8222 symbol flags are updated. */
0ae9bd27 8223 if (((VAR_P (decl) && TREE_STATIC (decl))
09812622
JJ
8224 || TREE_CODE (decl) == FUNCTION_DECL)
8225 && DECL_RTL_SET_P (decl))
8226 make_decl_rtl (decl);
8227 }
b9e75696 8228 }
b2ca3702
MM
8229 return false;
8230}
8231
dce81a1a
JJ
8232/* Handle an "tls_model" attribute; arguments as in
8233 struct attribute_spec.handler. */
8234
8235static tree
35b1a6fa 8236handle_tls_model_attribute (tree *node, tree name, tree args,
e18476eb 8237 int ARG_UNUSED (flags), bool *no_add_attrs)
dce81a1a 8238{
c2f7fa15 8239 tree id;
dce81a1a 8240 tree decl = *node;
c2f7fa15 8241 enum tls_model kind;
dce81a1a 8242
c2f7fa15
SB
8243 *no_add_attrs = true;
8244
0ae9bd27 8245 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
dce81a1a 8246 {
5c498b10 8247 warning (OPT_Wattributes, "%qE attribute ignored", name);
c2f7fa15 8248 return NULL_TREE;
dce81a1a 8249 }
dce81a1a 8250
c2f7fa15
SB
8251 kind = DECL_TLS_MODEL (decl);
8252 id = TREE_VALUE (args);
8253 if (TREE_CODE (id) != STRING_CST)
8254 {
8255 error ("tls_model argument not a string");
8256 return NULL_TREE;
dce81a1a
JJ
8257 }
8258
c2f7fa15
SB
8259 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8260 kind = TLS_MODEL_LOCAL_EXEC;
8261 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8262 kind = TLS_MODEL_INITIAL_EXEC;
8263 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8264 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8265 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8266 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8267 else
8268 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8269
56363ffd 8270 set_decl_tls_model (decl, kind);
dce81a1a
JJ
8271 return NULL_TREE;
8272}
8273
349ae713
NB
8274/* Handle a "no_instrument_function" attribute; arguments as in
8275 struct attribute_spec.handler. */
8276
8277static tree
35b1a6fa 8278handle_no_instrument_function_attribute (tree *node, tree name,
e18476eb
BI
8279 tree ARG_UNUSED (args),
8280 int ARG_UNUSED (flags),
a742c759 8281 bool *no_add_attrs)
349ae713
NB
8282{
8283 tree decl = *node;
8284
8285 if (TREE_CODE (decl) != FUNCTION_DECL)
8286 {
c5d75364
MLI
8287 error_at (DECL_SOURCE_LOCATION (decl),
8288 "%qE attribute applies only to functions", name);
349ae713
NB
8289 *no_add_attrs = true;
8290 }
349ae713
NB
8291 else
8292 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8293
8294 return NULL_TREE;
8295}
8296
8297/* Handle a "malloc" attribute; arguments as in
8298 struct attribute_spec.handler. */
8299
8300static tree
e18476eb
BI
8301handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8302 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 8303{
3425638a
JM
8304 if (TREE_CODE (*node) == FUNCTION_DECL
8305 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
349ae713 8306 DECL_IS_MALLOC (*node) = 1;
349ae713
NB
8307 else
8308 {
5c498b10 8309 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8310 *no_add_attrs = true;
8311 }
8312
8313 return NULL_TREE;
8314}
8315
51bc54a6
DM
8316/* Handle a "alloc_size" attribute; arguments as in
8317 struct attribute_spec.handler. */
8318
8319static tree
8320handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8321 int ARG_UNUSED (flags), bool *no_add_attrs)
8322{
f3f75f69 8323 unsigned arg_count = type_num_arguments (*node);
51bc54a6
DM
8324 for (; args; args = TREE_CHAIN (args))
8325 {
8326 tree position = TREE_VALUE (args);
5d77fb19
MG
8327 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8328 && TREE_CODE (position) != FUNCTION_DECL)
8329 position = default_conversion (position);
51bc54a6 8330
8fcbce72
JJ
8331 if (!tree_fits_uhwi_p (position)
8332 || !arg_count
8333 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
51bc54a6 8334 {
b8698a0f 8335 warning (OPT_Wattributes,
51bc54a6
DM
8336 "alloc_size parameter outside range");
8337 *no_add_attrs = true;
8338 return NULL_TREE;
8339 }
8340 }
8341 return NULL_TREE;
8342}
8343
8fcbce72
JJ
8344/* Handle a "alloc_align" attribute; arguments as in
8345 struct attribute_spec.handler. */
8346
8347static tree
8348handle_alloc_align_attribute (tree *node, tree, tree args, int,
8349 bool *no_add_attrs)
8350{
8351 unsigned arg_count = type_num_arguments (*node);
8352 tree position = TREE_VALUE (args);
8353 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8354 position = default_conversion (position);
8355
8356 if (!tree_fits_uhwi_p (position)
8357 || !arg_count
8358 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8359 {
8360 warning (OPT_Wattributes,
8361 "alloc_align parameter outside range");
8362 *no_add_attrs = true;
8363 return NULL_TREE;
8364 }
8365 return NULL_TREE;
8366}
8367
8368/* Handle a "assume_aligned" attribute; arguments as in
8369 struct attribute_spec.handler. */
8370
8371static tree
8372handle_assume_aligned_attribute (tree *, tree, tree args, int,
8373 bool *no_add_attrs)
8374{
8375 for (; args; args = TREE_CHAIN (args))
8376 {
8377 tree position = TREE_VALUE (args);
8378 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8379 && TREE_CODE (position) != FUNCTION_DECL)
8380 position = default_conversion (position);
8381
8382 if (TREE_CODE (position) != INTEGER_CST)
8383 {
8384 warning (OPT_Wattributes,
8385 "assume_aligned parameter not integer constant");
8386 *no_add_attrs = true;
8387 return NULL_TREE;
8388 }
8389 }
8390 return NULL_TREE;
8391}
8392
0b7b376d
RG
8393/* Handle a "fn spec" attribute; arguments as in
8394 struct attribute_spec.handler. */
8395
8396static tree
8397handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8398 tree args, int ARG_UNUSED (flags),
8399 bool *no_add_attrs ATTRIBUTE_UNUSED)
8400{
8401 gcc_assert (args
8402 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8403 && !TREE_CHAIN (args));
8404 return NULL_TREE;
8405}
8406
d5e254e1
IE
8407/* Handle a "bnd_variable_size" attribute; arguments as in
8408 struct attribute_spec.handler. */
8409
8410static tree
8411handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8412 int ARG_UNUSED (flags), bool *no_add_attrs)
8413{
8414 if (TREE_CODE (*node) != FIELD_DECL)
8415 {
8416 warning (OPT_Wattributes, "%qE attribute ignored", name);
8417 *no_add_attrs = true;
8418 }
8419
8420 return NULL_TREE;
8421}
8422
8423/* Handle a "bnd_legacy" attribute; arguments as in
8424 struct attribute_spec.handler. */
8425
8426static tree
8427handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8428 int ARG_UNUSED (flags), bool *no_add_attrs)
8429{
8430 if (TREE_CODE (*node) != FUNCTION_DECL)
8431 {
8432 warning (OPT_Wattributes, "%qE attribute ignored", name);
8433 *no_add_attrs = true;
8434 }
8435
8436 return NULL_TREE;
8437}
8438
8439/* Handle a "bnd_instrument" attribute; arguments as in
8440 struct attribute_spec.handler. */
8441
8442static tree
8443handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8444 int ARG_UNUSED (flags), bool *no_add_attrs)
8445{
8446 if (TREE_CODE (*node) != FUNCTION_DECL)
8447 {
8448 warning (OPT_Wattributes, "%qE attribute ignored", name);
8449 *no_add_attrs = true;
8450 }
8451
8452 return NULL_TREE;
8453}
8454
2a99e5e6
LL
8455/* Handle a "warn_unused" attribute; arguments as in
8456 struct attribute_spec.handler. */
8457
8458static tree
8459handle_warn_unused_attribute (tree *node, tree name,
8460 tree args ATTRIBUTE_UNUSED,
8461 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8462{
8463 if (TYPE_P (*node))
8464 /* Do nothing else, just set the attribute. We'll get at
8465 it later with lookup_attribute. */
8466 ;
8467 else
8468 {
8469 warning (OPT_Wattributes, "%qE attribute ignored", name);
8470 *no_add_attrs = true;
8471 }
8472
8473 return NULL_TREE;
8474}
8475
acf0174b
JJ
8476/* Handle an "omp declare simd" attribute; arguments as in
8477 struct attribute_spec.handler. */
8478
8479static tree
8480handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8481{
8482 return NULL_TREE;
8483}
8484
fff77217
KY
8485/* Handle a "simd" attribute. */
8486
8487static tree
e7b69085 8488handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
fff77217
KY
8489{
8490 if (TREE_CODE (*node) == FUNCTION_DECL)
8491 {
8492 if (lookup_attribute ("cilk simd function",
8493 DECL_ATTRIBUTES (*node)) != NULL)
8494 {
8495 error_at (DECL_SOURCE_LOCATION (*node),
8496 "%<__simd__%> attribute cannot be used in the same "
8497 "function marked as a Cilk Plus SIMD-enabled function");
8498 *no_add_attrs = true;
8499 }
8500 else
e7b69085
KY
8501 {
8502 tree t = get_identifier ("omp declare simd");
8503 tree attr = NULL_TREE;
8504 if (args)
8505 {
8506 tree id = TREE_VALUE (args);
8507
8508 if (TREE_CODE (id) != STRING_CST)
8509 {
8510 error ("attribute %qE argument not a string", name);
8511 *no_add_attrs = true;
8512 return NULL_TREE;
8513 }
8514
8515 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8516 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8517 OMP_CLAUSE_NOTINBRANCH);
8518 else
8519 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8520 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8521 OMP_CLAUSE_INBRANCH);
8522 else
8523 {
8524 error ("only %<inbranch%> and %<notinbranch%> flags are "
8525 "allowed for %<__simd__%> attribute");
8526 *no_add_attrs = true;
8527 return NULL_TREE;
8528 }
8529 }
8530
8531 DECL_ATTRIBUTES (*node) = tree_cons (t,
8532 build_tree_list (NULL_TREE,
8533 attr),
8534 DECL_ATTRIBUTES (*node));
8535 }
fff77217
KY
8536 }
8537 else
8538 {
8539 warning (OPT_Wattributes, "%qE attribute ignored", name);
8540 *no_add_attrs = true;
8541 }
8542
8543 return NULL_TREE;
8544}
8545
acf0174b
JJ
8546/* Handle an "omp declare target" attribute; arguments as in
8547 struct attribute_spec.handler. */
8548
8549static tree
8550handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8551{
8552 return NULL_TREE;
8553}
8554
6e9a3221
AN
8555/* Handle a "returns_twice" attribute; arguments as in
8556 struct attribute_spec.handler. */
8557
8558static tree
8559handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8560 int ARG_UNUSED (flags), bool *no_add_attrs)
8561{
8562 if (TREE_CODE (*node) == FUNCTION_DECL)
8563 DECL_IS_RETURNS_TWICE (*node) = 1;
8564 else
8565 {
5c498b10 8566 warning (OPT_Wattributes, "%qE attribute ignored", name);
6e9a3221
AN
8567 *no_add_attrs = true;
8568 }
8569
8570 return NULL_TREE;
8571}
8572
349ae713
NB
8573/* Handle a "no_limit_stack" attribute; arguments as in
8574 struct attribute_spec.handler. */
8575
8576static tree
35b1a6fa 8577handle_no_limit_stack_attribute (tree *node, tree name,
e18476eb
BI
8578 tree ARG_UNUSED (args),
8579 int ARG_UNUSED (flags),
a742c759 8580 bool *no_add_attrs)
349ae713
NB
8581{
8582 tree decl = *node;
8583
8584 if (TREE_CODE (decl) != FUNCTION_DECL)
8585 {
c5d75364
MLI
8586 error_at (DECL_SOURCE_LOCATION (decl),
8587 "%qE attribute applies only to functions", name);
349ae713
NB
8588 *no_add_attrs = true;
8589 }
8590 else if (DECL_INITIAL (decl))
8591 {
c5d75364
MLI
8592 error_at (DECL_SOURCE_LOCATION (decl),
8593 "can%'t set %qE attribute after definition", name);
349ae713
NB
8594 *no_add_attrs = true;
8595 }
8596 else
8597 DECL_NO_LIMIT_STACK (decl) = 1;
8598
8599 return NULL_TREE;
8600}
8601
8602/* Handle a "pure" attribute; arguments as in
8603 struct attribute_spec.handler. */
8604
8605static tree
e18476eb
BI
8606handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8607 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
8608{
8609 if (TREE_CODE (*node) == FUNCTION_DECL)
becfd6e5 8610 DECL_PURE_P (*node) = 1;
349ae713
NB
8611 /* ??? TODO: Support types. */
8612 else
8613 {
5c498b10 8614 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8615 *no_add_attrs = true;
8616 }
8617
8618 return NULL_TREE;
8619}
8620
0a35513e
AH
8621/* Digest an attribute list destined for a transactional memory statement.
8622 ALLOWED is the set of attributes that are allowed for this statement;
8623 return the attribute we parsed. Multiple attributes are never allowed. */
8624
8625int
8626parse_tm_stmt_attr (tree attrs, int allowed)
8627{
8628 tree a_seen = NULL;
8629 int m_seen = 0;
8630
8631 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8632 {
8633 tree a = TREE_PURPOSE (attrs);
8634 int m = 0;
8635
8636 if (is_attribute_p ("outer", a))
8637 m = TM_STMT_ATTR_OUTER;
8638
8639 if ((m & allowed) == 0)
8640 {
8641 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8642 continue;
8643 }
8644
8645 if (m_seen == 0)
8646 {
8647 a_seen = a;
8648 m_seen = m;
8649 }
8650 else if (m_seen == m)
8651 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8652 else
8653 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8654 }
8655
8656 return m_seen;
8657}
8658
8659/* Transform a TM attribute name into a maskable integer and back.
8660 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8661 to how the lack of an attribute is treated. */
8662
8663int
8664tm_attr_to_mask (tree attr)
8665{
8666 if (attr == NULL)
8667 return 0;
8668 if (is_attribute_p ("transaction_safe", attr))
8669 return TM_ATTR_SAFE;
8670 if (is_attribute_p ("transaction_callable", attr))
8671 return TM_ATTR_CALLABLE;
8672 if (is_attribute_p ("transaction_pure", attr))
8673 return TM_ATTR_PURE;
8674 if (is_attribute_p ("transaction_unsafe", attr))
8675 return TM_ATTR_IRREVOCABLE;
8676 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8677 return TM_ATTR_MAY_CANCEL_OUTER;
8678 return 0;
8679}
8680
8681tree
8682tm_mask_to_attr (int mask)
8683{
8684 const char *str;
8685 switch (mask)
8686 {
8687 case TM_ATTR_SAFE:
8688 str = "transaction_safe";
8689 break;
8690 case TM_ATTR_CALLABLE:
8691 str = "transaction_callable";
8692 break;
8693 case TM_ATTR_PURE:
8694 str = "transaction_pure";
8695 break;
8696 case TM_ATTR_IRREVOCABLE:
8697 str = "transaction_unsafe";
8698 break;
8699 case TM_ATTR_MAY_CANCEL_OUTER:
8700 str = "transaction_may_cancel_outer";
8701 break;
8702 default:
8703 gcc_unreachable ();
8704 }
8705 return get_identifier (str);
8706}
8707
8708/* Return the first TM attribute seen in LIST. */
8709
8710tree
8711find_tm_attribute (tree list)
8712{
8713 for (; list ; list = TREE_CHAIN (list))
8714 {
8715 tree name = TREE_PURPOSE (list);
8716 if (tm_attr_to_mask (name) != 0)
8717 return name;
8718 }
8719 return NULL_TREE;
8720}
8721
8722/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8723 Here we accept only function types, and verify that none of the other
8724 function TM attributes are also applied. */
8725/* ??? We need to accept class types for C++, but not C. This greatly
8726 complicates this function, since we can no longer rely on the extra
8727 processing given by function_type_required. */
8728
8729static tree
8730handle_tm_attribute (tree *node, tree name, tree args,
8731 int flags, bool *no_add_attrs)
8732{
8733 /* Only one path adds the attribute; others don't. */
8734 *no_add_attrs = true;
8735
8736 switch (TREE_CODE (*node))
8737 {
8738 case RECORD_TYPE:
8739 case UNION_TYPE:
8740 /* Only tm_callable and tm_safe apply to classes. */
8741 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8742 goto ignored;
8743 /* FALLTHRU */
8744
8745 case FUNCTION_TYPE:
8746 case METHOD_TYPE:
8747 {
8748 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8749 if (old_name == name)
8750 ;
8751 else if (old_name != NULL_TREE)
8752 error ("type was previously declared %qE", old_name);
8753 else
8754 *no_add_attrs = false;
8755 }
8756 break;
8757
b8fd7909
JM
8758 case FUNCTION_DECL:
8759 {
8760 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8761 want to set transaction_safe on the type. */
8762 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8763 if (!TYPE_P (DECL_CONTEXT (*node)))
8764 error_at (DECL_SOURCE_LOCATION (*node),
8765 "%<transaction_safe_dynamic%> may only be specified for "
8766 "a virtual function");
8767 *no_add_attrs = false;
8768 decl_attributes (&TREE_TYPE (*node),
8769 build_tree_list (get_identifier ("transaction_safe"),
8770 NULL_TREE),
8771 0);
8772 break;
8773 }
8774
0a35513e
AH
8775 case POINTER_TYPE:
8776 {
8777 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8778 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8779 {
8780 tree fn_tmp = TREE_TYPE (*node);
8781 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8782 *node = build_pointer_type (fn_tmp);
8783 break;
8784 }
8785 }
8786 /* FALLTHRU */
8787
8788 default:
8789 /* If a function is next, pass it on to be tried next. */
8790 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8791 return tree_cons (name, args, NULL);
8792
8793 ignored:
8794 warning (OPT_Wattributes, "%qE attribute ignored", name);
8795 break;
8796 }
8797
8798 return NULL_TREE;
8799}
8800
8801/* Handle the TM_WRAP attribute; arguments as in
8802 struct attribute_spec.handler. */
8803
8804static tree
8805handle_tm_wrap_attribute (tree *node, tree name, tree args,
8806 int ARG_UNUSED (flags), bool *no_add_attrs)
8807{
8808 tree decl = *node;
8809
8810 /* We don't need the attribute even on success, since we
8811 record the entry in an external table. */
8812 *no_add_attrs = true;
8813
8814 if (TREE_CODE (decl) != FUNCTION_DECL)
8815 warning (OPT_Wattributes, "%qE attribute ignored", name);
8816 else
8817 {
8818 tree wrap_decl = TREE_VALUE (args);
661a0813
MP
8819 if (error_operand_p (wrap_decl))
8820 ;
8821 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
21b634ae 8822 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
0a35513e
AH
8823 error ("%qE argument not an identifier", name);
8824 else
8825 {
8826 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8827 wrap_decl = lookup_name (wrap_decl);
8828 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8829 {
8830 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8831 TREE_TYPE (wrap_decl)))
8832 record_tm_replacement (wrap_decl, decl);
8833 else
8834 error ("%qD is not compatible with %qD", wrap_decl, decl);
8835 }
8836 else
c07d7c02 8837 error ("%qE argument is not a function", name);
0a35513e
AH
8838 }
8839 }
8840
8841 return NULL_TREE;
8842}
8843
8844/* Ignore the given attribute. Used when this attribute may be usefully
8845 overridden by the target, but is not used generically. */
8846
8847static tree
8848ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8849 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8850 bool *no_add_attrs)
8851{
8852 *no_add_attrs = true;
8853 return NULL_TREE;
8854}
8855
dcd6de6d
ZD
8856/* Handle a "no vops" attribute; arguments as in
8857 struct attribute_spec.handler. */
8858
8859static tree
8860handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8861 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8862 bool *ARG_UNUSED (no_add_attrs))
8863{
8864 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8865 DECL_IS_NOVOPS (*node) = 1;
8866 return NULL_TREE;
8867}
8868
349ae713
NB
8869/* Handle a "deprecated" attribute; arguments as in
8870 struct attribute_spec.handler. */
35b1a6fa 8871
349ae713 8872static tree
35b1a6fa 8873handle_deprecated_attribute (tree *node, tree name,
9b86d6bb 8874 tree args, int flags,
a742c759 8875 bool *no_add_attrs)
349ae713
NB
8876{
8877 tree type = NULL_TREE;
8878 int warn = 0;
c51a1ba9 8879 tree what = NULL_TREE;
35b1a6fa 8880
9b86d6bb
L
8881 if (!args)
8882 *no_add_attrs = true;
8883 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8884 {
8885 error ("deprecated message is not a string");
8886 *no_add_attrs = true;
8887 }
8888
349ae713
NB
8889 if (DECL_P (*node))
8890 {
8891 tree decl = *node;
8892 type = TREE_TYPE (decl);
35b1a6fa 8893
349ae713
NB
8894 if (TREE_CODE (decl) == TYPE_DECL
8895 || TREE_CODE (decl) == PARM_DECL
21b634ae 8896 || VAR_OR_FUNCTION_DECL_P (decl)
a1178b30 8897 || TREE_CODE (decl) == FIELD_DECL
fd5c817a 8898 || TREE_CODE (decl) == CONST_DECL
a1178b30 8899 || objc_method_decl (TREE_CODE (decl)))
349ae713
NB
8900 TREE_DEPRECATED (decl) = 1;
8901 else
8902 warn = 1;
8903 }
8904 else if (TYPE_P (*node))
8905 {
8906 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 8907 *node = build_variant_type_copy (*node);
349ae713
NB
8908 TREE_DEPRECATED (*node) = 1;
8909 type = *node;
8910 }
8911 else
8912 warn = 1;
35b1a6fa 8913
349ae713
NB
8914 if (warn)
8915 {
8916 *no_add_attrs = true;
8917 if (type && TYPE_NAME (type))
8918 {
8919 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9 8920 what = TYPE_NAME (*node);
349ae713
NB
8921 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8922 && DECL_NAME (TYPE_NAME (type)))
c51a1ba9 8923 what = DECL_NAME (TYPE_NAME (type));
349ae713
NB
8924 }
8925 if (what)
5c498b10 8926 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
349ae713 8927 else
5c498b10 8928 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8929 }
8930
8931 return NULL_TREE;
8932}
8933
349ae713
NB
8934/* Handle a "vector_size" attribute; arguments as in
8935 struct attribute_spec.handler. */
8936
8937static tree
35b1a6fa 8938handle_vector_size_attribute (tree *node, tree name, tree args,
e18476eb 8939 int ARG_UNUSED (flags),
a742c759 8940 bool *no_add_attrs)
349ae713
NB
8941{
8942 unsigned HOST_WIDE_INT vecsize, nunits;
ef4bddc2 8943 machine_mode orig_mode;
4a5eab38 8944 tree type = *node, new_type, size;
349ae713
NB
8945
8946 *no_add_attrs = true;
8947
4a5eab38 8948 size = TREE_VALUE (args);
661a0813
MP
8949 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8950 && TREE_CODE (size) != FUNCTION_DECL)
5d77fb19 8951 size = default_conversion (size);
4a5eab38 8952
cc269bb6 8953 if (!tree_fits_uhwi_p (size))
349ae713 8954 {
5c498b10 8955 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8956 return NULL_TREE;
8957 }
8958
8959 /* Get the vector size (in bytes). */
ae7e9ddd 8960 vecsize = tree_to_uhwi (size);
349ae713
NB
8961
8962 /* We need to provide for vector pointers, vector arrays, and
8963 functions returning vectors. For example:
8964
8965 __attribute__((vector_size(16))) short *foo;
8966
8967 In this case, the mode is SI, but the type being modified is
8968 HI, so we need to look further. */
8969
8970 while (POINTER_TYPE_P (type)
8971 || TREE_CODE (type) == FUNCTION_TYPE
43dc123f 8972 || TREE_CODE (type) == METHOD_TYPE
270e749d
JJ
8973 || TREE_CODE (type) == ARRAY_TYPE
8974 || TREE_CODE (type) == OFFSET_TYPE)
349ae713
NB
8975 type = TREE_TYPE (type);
8976
8977 /* Get the mode of the type being modified. */
8978 orig_mode = TYPE_MODE (type);
8979
270e749d
JJ
8980 if ((!INTEGRAL_TYPE_P (type)
8981 && !SCALAR_FLOAT_TYPE_P (type)
8982 && !FIXED_POINT_TYPE_P (type))
3d8bf70f 8983 || (!SCALAR_FLOAT_MODE_P (orig_mode)
ab22c1fa
CF
8984 && GET_MODE_CLASS (orig_mode) != MODE_INT
8985 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
cc269bb6 8986 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
e4e5261f 8987 || TREE_CODE (type) == BOOLEAN_TYPE)
349ae713 8988 {
c51a1ba9 8989 error ("invalid vector type for attribute %qE", name);
349ae713
NB
8990 return NULL_TREE;
8991 }
8992
ae7e9ddd 8993 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
ee8960e5
JJ
8994 {
8995 error ("vector size not an integral multiple of component size");
8996 return NULL;
8997 }
8998
8999 if (vecsize == 0)
9000 {
9001 error ("zero vector size");
9002 return NULL;
9003 }
9004
349ae713 9005 /* Calculate how many units fit in the vector. */
ae7e9ddd 9006 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
26277d41 9007 if (nunits & (nunits - 1))
349ae713 9008 {
26277d41 9009 error ("number of components of the vector not a power of two");
349ae713
NB
9010 return NULL_TREE;
9011 }
9012
26277d41 9013 new_type = build_vector_type (type, nunits);
349ae713
NB
9014
9015 /* Build back pointers if needed. */
5dc11954 9016 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
349ae713
NB
9017
9018 return NULL_TREE;
9019}
9020
b34c7881
JT
9021/* Handle the "nonnull" attribute. */
9022static tree
e18476eb
BI
9023handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9024 tree args, int ARG_UNUSED (flags),
a742c759 9025 bool *no_add_attrs)
b34c7881
JT
9026{
9027 tree type = *node;
9028 unsigned HOST_WIDE_INT attr_arg_num;
9029
9030 /* If no arguments are specified, all pointer arguments should be
95bd1dd7 9031 non-null. Verify a full prototype is given so that the arguments
b34c7881 9032 will have the correct types when we actually check them later. */
3f75a254 9033 if (!args)
b34c7881 9034 {
f4da8dce 9035 if (!prototype_p (type))
b34c7881
JT
9036 {
9037 error ("nonnull attribute without arguments on a non-prototype");
6de9cd9a 9038 *no_add_attrs = true;
b34c7881
JT
9039 }
9040 return NULL_TREE;
9041 }
9042
9043 /* Argument list specified. Verify that each argument number references
9044 a pointer argument. */
5d77fb19 9045 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
b34c7881 9046 {
6de9cd9a 9047 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
b34c7881 9048
5d77fb19
MG
9049 tree arg = TREE_VALUE (args);
9050 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9051 && TREE_CODE (arg) != FUNCTION_DECL)
9052 arg = default_conversion (arg);
9053
9054 if (!get_nonnull_operand (arg, &arg_num))
b34c7881 9055 {
40b97a2e 9056 error ("nonnull argument has invalid operand number (argument %lu)",
b34c7881
JT
9057 (unsigned long) attr_arg_num);
9058 *no_add_attrs = true;
9059 return NULL_TREE;
9060 }
9061
e19a18d4 9062 if (prototype_p (type))
b34c7881 9063 {
e19a18d4
NF
9064 function_args_iterator iter;
9065 tree argument;
9066
9067 function_args_iter_init (&iter, type);
9068 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
b34c7881 9069 {
e19a18d4
NF
9070 argument = function_args_iter_cond (&iter);
9071 if (argument == NULL_TREE || ck_num == arg_num)
b34c7881 9072 break;
b34c7881
JT
9073 }
9074
3f75a254 9075 if (!argument
e19a18d4 9076 || TREE_CODE (argument) == VOID_TYPE)
b34c7881 9077 {
40b97a2e 9078 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
b34c7881
JT
9079 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9080 *no_add_attrs = true;
9081 return NULL_TREE;
9082 }
9083
e19a18d4 9084 if (TREE_CODE (argument) != POINTER_TYPE)
b34c7881 9085 {
40b97a2e 9086 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
b34c7881
JT
9087 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9088 *no_add_attrs = true;
9089 return NULL_TREE;
9090 }
9091 }
9092 }
9093
9094 return NULL_TREE;
9095}
9096
9097/* Check the argument list of a function call for null in argument slots
94a0dd7b 9098 that are marked as requiring a non-null pointer argument. The NARGS
3342fd71 9099 arguments are passed in the array ARGARRAY. */
b34c7881
JT
9100
9101static void
3342fd71 9102check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
b34c7881 9103{
332f1d24 9104 tree a;
94a0dd7b 9105 int i;
b34c7881 9106
332f1d24
JJ
9107 attrs = lookup_attribute ("nonnull", attrs);
9108 if (attrs == NULL_TREE)
9109 return;
9110
9111 a = attrs;
9112 /* See if any of the nonnull attributes has no arguments. If so,
9113 then every pointer argument is checked (in which case the check
9114 for pointer type is done in check_nonnull_arg). */
9115 if (TREE_VALUE (a) != NULL_TREE)
9116 do
9117 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9118 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9119
9120 if (a != NULL_TREE)
9121 for (i = 0; i < nargs; i++)
3342fd71 9122 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
332f1d24
JJ
9123 i + 1);
9124 else
b34c7881 9125 {
332f1d24
JJ
9126 /* Walk the argument list. If we encounter an argument number we
9127 should check for non-null, do it. */
9128 for (i = 0; i < nargs; i++)
b34c7881 9129 {
332f1d24 9130 for (a = attrs; ; a = TREE_CHAIN (a))
6de9cd9a 9131 {
332f1d24
JJ
9132 a = lookup_attribute ("nonnull", a);
9133 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9134 break;
6de9cd9a 9135 }
332f1d24
JJ
9136
9137 if (a != NULL_TREE)
3342fd71 9138 check_function_arguments_recurse (check_nonnull_arg, &loc,
332f1d24 9139 argarray[i], i + 1);
b34c7881
JT
9140 }
9141 }
9142}
9143
254986c7 9144/* Check that the Nth argument of a function call (counting backwards
94a0dd7b
SL
9145 from the end) is a (pointer)0. The NARGS arguments are passed in the
9146 array ARGARRAY. */
3d091dac
KG
9147
9148static void
dde05067 9149check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
3d091dac 9150{
dde05067 9151 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
3d091dac
KG
9152
9153 if (attr)
9154 {
94a0dd7b
SL
9155 int len = 0;
9156 int pos = 0;
9157 tree sentinel;
dde05067
NF
9158 function_args_iterator iter;
9159 tree t;
c22cacf3 9160
94a0dd7b 9161 /* Skip over the named arguments. */
dde05067 9162 FOREACH_FUNCTION_ARGS (fntype, t, iter)
c22cacf3 9163 {
dde05067
NF
9164 if (len == nargs)
9165 break;
94a0dd7b
SL
9166 len++;
9167 }
254986c7 9168
94a0dd7b
SL
9169 if (TREE_VALUE (attr))
9170 {
9171 tree p = TREE_VALUE (TREE_VALUE (attr));
9172 pos = TREE_INT_CST_LOW (p);
9173 }
254986c7 9174
94a0dd7b
SL
9175 /* The sentinel must be one of the varargs, i.e.
9176 in position >= the number of fixed arguments. */
9177 if ((nargs - 1 - pos) < len)
9178 {
7332899a 9179 warning (OPT_Wformat_,
94a0dd7b
SL
9180 "not enough variable arguments to fit a sentinel");
9181 return;
3d091dac 9182 }
94a0dd7b
SL
9183
9184 /* Validate the sentinel. */
9185 sentinel = argarray[nargs - 1 - pos];
9186 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9187 || !integer_zerop (sentinel))
9188 /* Although __null (in C++) is only an integer we allow it
9189 nevertheless, as we are guaranteed that it's exactly
9190 as wide as a pointer, and we don't want to force
9191 users to cast the NULL they have written there.
9192 We warn with -Wstrict-null-sentinel, though. */
9193 && (warn_strict_null_sentinel || null_node != sentinel))
7332899a 9194 warning (OPT_Wformat_, "missing sentinel in function call");
3d091dac
KG
9195 }
9196}
9197
b34c7881
JT
9198/* Helper for check_function_nonnull; given a list of operands which
9199 must be non-null in ARGS, determine if operand PARAM_NUM should be
9200 checked. */
9201
9202static bool
35b1a6fa 9203nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
b34c7881 9204{
6de9cd9a 9205 unsigned HOST_WIDE_INT arg_num = 0;
b34c7881
JT
9206
9207 for (; args; args = TREE_CHAIN (args))
9208 {
366de0ce
NS
9209 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9210
9211 gcc_assert (found);
b34c7881
JT
9212
9213 if (arg_num == param_num)
9214 return true;
9215 }
9216 return false;
9217}
9218
9219/* Check that the function argument PARAM (which is operand number
9220 PARAM_NUM) is non-null. This is called by check_function_nonnull
9221 via check_function_arguments_recurse. */
9222
9223static void
3342fd71 9224check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
b34c7881 9225{
3342fd71
BS
9226 location_t *ploc = (location_t *) ctx;
9227
b34c7881
JT
9228 /* Just skip checking the argument if it's not a pointer. This can
9229 happen if the "nonnull" attribute was given without an operand
9230 list (which means to check every pointer argument). */
9231
9232 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9233 return;
9234
9235 if (integer_zerop (param))
3342fd71
BS
9236 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9237 "(argument %lu)", (unsigned long) param_num);
b34c7881
JT
9238}
9239
9240/* Helper for nonnull attribute handling; fetch the operand number
9241 from the attribute argument list. */
9242
9243static bool
35b1a6fa 9244get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
b34c7881 9245{
807e902e
KZ
9246 /* Verify the arg number is a small constant. */
9247 if (tree_fits_uhwi_p (arg_num_expr))
9248 {
9249 *valp = TREE_INT_CST_LOW (arg_num_expr);
9250 return true;
9251 }
9252 else
b34c7881 9253 return false;
b34c7881 9254}
39f2f3c8
RS
9255
9256/* Handle a "nothrow" attribute; arguments as in
9257 struct attribute_spec.handler. */
9258
9259static tree
e18476eb
BI
9260handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9261 int ARG_UNUSED (flags), bool *no_add_attrs)
39f2f3c8
RS
9262{
9263 if (TREE_CODE (*node) == FUNCTION_DECL)
9264 TREE_NOTHROW (*node) = 1;
9265 /* ??? TODO: Support types. */
9266 else
9267 {
5c498b10 9268 warning (OPT_Wattributes, "%qE attribute ignored", name);
39f2f3c8
RS
9269 *no_add_attrs = true;
9270 }
9271
9272 return NULL_TREE;
9273}
0bfa5f65
RH
9274
9275/* Handle a "cleanup" attribute; arguments as in
9276 struct attribute_spec.handler. */
9277
9278static tree
35b1a6fa 9279handle_cleanup_attribute (tree *node, tree name, tree args,
e18476eb 9280 int ARG_UNUSED (flags), bool *no_add_attrs)
0bfa5f65
RH
9281{
9282 tree decl = *node;
9283 tree cleanup_id, cleanup_decl;
9284
9285 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9286 for global destructors in C++. This requires infrastructure that
9287 we don't have generically at the moment. It's also not a feature
9288 we'd be missing too much, since we do have attribute constructor. */
0ae9bd27 9289 if (!VAR_P (decl) || TREE_STATIC (decl))
0bfa5f65 9290 {
5c498b10 9291 warning (OPT_Wattributes, "%qE attribute ignored", name);
0bfa5f65
RH
9292 *no_add_attrs = true;
9293 return NULL_TREE;
9294 }
9295
9296 /* Verify that the argument is a function in scope. */
9297 /* ??? We could support pointers to functions here as well, if
9298 that was considered desirable. */
9299 cleanup_id = TREE_VALUE (args);
9300 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9301 {
40b97a2e 9302 error ("cleanup argument not an identifier");
0bfa5f65
RH
9303 *no_add_attrs = true;
9304 return NULL_TREE;
9305 }
10e6657a 9306 cleanup_decl = lookup_name (cleanup_id);
0bfa5f65
RH
9307 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9308 {
40b97a2e 9309 error ("cleanup argument not a function");
0bfa5f65
RH
9310 *no_add_attrs = true;
9311 return NULL_TREE;
9312 }
9313
35b1a6fa 9314 /* That the function has proper type is checked with the
0bfa5f65
RH
9315 eventual call to build_function_call. */
9316
9317 return NULL_TREE;
9318}
72954a4f
JM
9319
9320/* Handle a "warn_unused_result" attribute. No special handling. */
9321
9322static tree
9323handle_warn_unused_result_attribute (tree *node, tree name,
e18476eb
BI
9324 tree ARG_UNUSED (args),
9325 int ARG_UNUSED (flags), bool *no_add_attrs)
72954a4f
JM
9326{
9327 /* Ignore the attribute for functions not returning any value. */
9328 if (VOID_TYPE_P (TREE_TYPE (*node)))
9329 {
5c498b10 9330 warning (OPT_Wattributes, "%qE attribute ignored", name);
72954a4f
JM
9331 *no_add_attrs = true;
9332 }
9333
9334 return NULL_TREE;
9335}
3d091dac
KG
9336
9337/* Handle a "sentinel" attribute. */
9338
9339static tree
254986c7 9340handle_sentinel_attribute (tree *node, tree name, tree args,
3d091dac
KG
9341 int ARG_UNUSED (flags), bool *no_add_attrs)
9342{
f4da8dce 9343 if (!prototype_p (*node))
3d091dac 9344 {
5c498b10
DD
9345 warning (OPT_Wattributes,
9346 "%qE attribute requires prototypes with named arguments", name);
3d091dac 9347 *no_add_attrs = true;
3d091dac 9348 }
254986c7
KG
9349 else
9350 {
dcf0c47e 9351 if (!stdarg_p (*node))
c22cacf3 9352 {
5c498b10
DD
9353 warning (OPT_Wattributes,
9354 "%qE attribute only applies to variadic functions", name);
254986c7
KG
9355 *no_add_attrs = true;
9356 }
9357 }
c22cacf3 9358
254986c7 9359 if (args)
3d091dac 9360 {
254986c7 9361 tree position = TREE_VALUE (args);
661a0813
MP
9362 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9363 && TREE_CODE (position) != FUNCTION_DECL)
9364 position = default_conversion (position);
254986c7 9365
661a0813
MP
9366 if (TREE_CODE (position) != INTEGER_CST
9367 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
c22cacf3 9368 {
b8698a0f 9369 warning (OPT_Wattributes,
aa86a51b 9370 "requested position is not an integer constant");
254986c7
KG
9371 *no_add_attrs = true;
9372 }
9373 else
c22cacf3 9374 {
254986c7
KG
9375 if (tree_int_cst_lt (position, integer_zero_node))
9376 {
aa86a51b
DM
9377 warning (OPT_Wattributes,
9378 "requested position is less than zero");
254986c7
KG
9379 *no_add_attrs = true;
9380 }
9381 }
3d091dac 9382 }
c22cacf3 9383
3d091dac
KG
9384 return NULL_TREE;
9385}
b5d32c25
KG
9386
9387/* Handle a "type_generic" attribute. */
9388
9389static tree
9390handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9391 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9392 bool * ARG_UNUSED (no_add_attrs))
9393{
3bf5906b
KG
9394 /* Ensure we have a function type. */
9395 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
b8698a0f 9396
3bf5906b 9397 /* Ensure we have a variadic function. */
dcf0c47e 9398 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5d32c25
KG
9399
9400 return NULL_TREE;
9401}
ab442df7 9402
5779e713 9403/* Handle a "target" attribute. */
ab442df7
MM
9404
9405static tree
5779e713 9406handle_target_attribute (tree *node, tree name, tree args, int flags,
ab442df7
MM
9407 bool *no_add_attrs)
9408{
9409 /* Ensure we have a function type. */
9410 if (TREE_CODE (*node) != FUNCTION_DECL)
9411 {
9412 warning (OPT_Wattributes, "%qE attribute ignored", name);
9413 *no_add_attrs = true;
9414 }
3b1661a9
ES
9415 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9416 {
9417 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9418 "with %qs attribute", name, "target_clones");
9419 *no_add_attrs = true;
9420 }
ab442df7 9421 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
32887460 9422 flags))
ab442df7
MM
9423 *no_add_attrs = true;
9424
9425 return NULL_TREE;
9426}
9427
3b1661a9
ES
9428/* Handle a "target_clones" attribute. */
9429
9430static tree
9431handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9432 int ARG_UNUSED (flags), bool *no_add_attrs)
9433{
9434 /* Ensure we have a function type. */
9435 if (TREE_CODE (*node) == FUNCTION_DECL)
9436 {
9437 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9438 {
9439 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9440 "with %qs attribute", name, "always_inline");
9441 *no_add_attrs = true;
9442 }
9443 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9444 {
9445 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9446 "with %qs attribute", name, "target");
9447 *no_add_attrs = true;
9448 }
9449 else
9450 /* Do not inline functions with multiple clone targets. */
9451 DECL_UNINLINABLE (*node) = 1;
9452 }
9453 else
9454 {
9455 warning (OPT_Wattributes, "%qE attribute ignored", name);
9456 *no_add_attrs = true;
9457 }
9458 return NULL_TREE;
9459}
9460
ab442df7
MM
9461/* Arguments being collected for optimization. */
9462typedef const char *const_char_p; /* For DEF_VEC_P. */
9771b263 9463static GTY(()) vec<const_char_p, va_gc> *optimize_args;
ab442df7
MM
9464
9465
9466/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9467 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9468 false for #pragma GCC optimize. */
9469
9470bool
9471parse_optimize_options (tree args, bool attr_p)
9472{
9473 bool ret = true;
9474 unsigned opt_argc;
9475 unsigned i;
9476 const char **opt_argv;
6e2f1956
JM
9477 struct cl_decoded_option *decoded_options;
9478 unsigned int decoded_options_count;
ab442df7
MM
9479 tree ap;
9480
9481 /* Build up argv vector. Just in case the string is stored away, use garbage
9482 collected strings. */
9771b263
DN
9483 vec_safe_truncate (optimize_args, 0);
9484 vec_safe_push (optimize_args, (const char *) NULL);
ab442df7
MM
9485
9486 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9487 {
9488 tree value = TREE_VALUE (ap);
9489
9490 if (TREE_CODE (value) == INTEGER_CST)
9491 {
9492 char buffer[20];
9493 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9771b263 9494 vec_safe_push (optimize_args, ggc_strdup (buffer));
ab442df7
MM
9495 }
9496
9497 else if (TREE_CODE (value) == STRING_CST)
9498 {
9499 /* Split string into multiple substrings. */
9500 size_t len = TREE_STRING_LENGTH (value);
9501 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9502 char *end = p + len;
9503 char *comma;
9504 char *next_p = p;
9505
9506 while (next_p != NULL)
9507 {
9508 size_t len2;
9509 char *q, *r;
9510
9511 p = next_p;
9512 comma = strchr (p, ',');
9513 if (comma)
9514 {
9515 len2 = comma - p;
9516 *comma = '\0';
9517 next_p = comma+1;
9518 }
9519 else
9520 {
9521 len2 = end - p;
9522 next_p = NULL;
9523 }
9524
a9429e29 9525 r = q = (char *) ggc_alloc_atomic (len2 + 3);
ab442df7
MM
9526
9527 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9528 options. */
9529 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9530 {
9531 ret = false;
9532 if (attr_p)
9533 warning (OPT_Wattributes,
06730c5d 9534 "bad option %s to optimize attribute", p);
ab442df7
MM
9535 else
9536 warning (OPT_Wpragmas,
de621752 9537 "bad option %s to pragma attribute", p);
ab442df7
MM
9538 continue;
9539 }
9540
9541 if (*p != '-')
9542 {
9543 *r++ = '-';
9544
9545 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9546 itself is -Os, and any other switch begins with a -f. */
9547 if ((*p >= '0' && *p <= '9')
9548 || (p[0] == 's' && p[1] == '\0'))
9549 *r++ = 'O';
9550 else if (*p != 'O')
9551 *r++ = 'f';
9552 }
9553
9554 memcpy (r, p, len2);
9555 r[len2] = '\0';
9771b263 9556 vec_safe_push (optimize_args, (const char *) q);
ab442df7
MM
9557 }
9558
9559 }
9560 }
9561
9771b263 9562 opt_argc = optimize_args->length ();
ab442df7
MM
9563 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9564
9565 for (i = 1; i < opt_argc; i++)
9771b263 9566 opt_argv[i] = (*optimize_args)[i];
ab442df7
MM
9567
9568 /* Now parse the options. */
a75bfaa6
JM
9569 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9570 &decoded_options,
9571 &decoded_options_count);
9572 decode_options (&global_options, &global_options_set,
a4d8c676
JM
9573 decoded_options, decoded_options_count,
9574 input_location, global_dc);
ab442df7 9575
2b7e2984
SE
9576 targetm.override_options_after_change();
9577
9771b263 9578 optimize_args->truncate (0);
ab442df7
MM
9579 return ret;
9580}
9581
9582/* For handling "optimize" attribute. arguments as in
9583 struct attribute_spec.handler. */
9584
9585static tree
9586handle_optimize_attribute (tree *node, tree name, tree args,
9587 int ARG_UNUSED (flags), bool *no_add_attrs)
9588{
9589 /* Ensure we have a function type. */
9590 if (TREE_CODE (*node) != FUNCTION_DECL)
9591 {
9592 warning (OPT_Wattributes, "%qE attribute ignored", name);
9593 *no_add_attrs = true;
9594 }
9595 else
9596 {
9597 struct cl_optimization cur_opts;
9598 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9599
9600 /* Save current options. */
46625112 9601 cl_optimization_save (&cur_opts, &global_options);
ab442df7
MM
9602
9603 /* If we previously had some optimization options, use them as the
9604 default. */
9605 if (old_opts)
46625112
JM
9606 cl_optimization_restore (&global_options,
9607 TREE_OPTIMIZATION (old_opts));
ab442df7
MM
9608
9609 /* Parse options, and update the vector. */
9610 parse_optimize_options (args, true);
9611 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
bf7b5747 9612 = build_optimization_node (&global_options);
ab442df7
MM
9613
9614 /* Restore current options. */
46625112 9615 cl_optimization_restore (&global_options, &cur_opts);
ab442df7
MM
9616 }
9617
9618 return NULL_TREE;
9619}
7458026b
ILT
9620
9621/* Handle a "no_split_stack" attribute. */
9622
9623static tree
9624handle_no_split_stack_attribute (tree *node, tree name,
9625 tree ARG_UNUSED (args),
9626 int ARG_UNUSED (flags),
9627 bool *no_add_attrs)
9628{
9629 tree decl = *node;
9630
9631 if (TREE_CODE (decl) != FUNCTION_DECL)
9632 {
9633 error_at (DECL_SOURCE_LOCATION (decl),
9634 "%qE attribute applies only to functions", name);
9635 *no_add_attrs = true;
9636 }
9637 else if (DECL_INITIAL (decl))
9638 {
9639 error_at (DECL_SOURCE_LOCATION (decl),
9640 "can%'t set %qE attribute after definition", name);
9641 *no_add_attrs = true;
9642 }
9643
9644 return NULL_TREE;
9645}
826cacfe
MG
9646
9647/* Handle a "returns_nonnull" attribute; arguments as in
9648 struct attribute_spec.handler. */
9649
9650static tree
9651handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9652 bool *no_add_attrs)
9653{
9654 // Even without a prototype we still have a return type we can check.
9655 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9656 {
9657 error ("returns_nonnull attribute on a function not returning a pointer");
9658 *no_add_attrs = true;
9659 }
9660 return NULL_TREE;
9661}
9662
976d5a22
TT
9663/* Handle a "designated_init" attribute; arguments as in
9664 struct attribute_spec.handler. */
9665
9666static tree
9667handle_designated_init_attribute (tree *node, tree name, tree, int,
9668 bool *no_add_attrs)
9669{
9670 if (TREE_CODE (*node) != RECORD_TYPE)
9671 {
9672 error ("%qE attribute is only valid on %<struct%> type", name);
9673 *no_add_attrs = true;
9674 }
9675 return NULL_TREE;
9676}
9677
b34c7881 9678\f
dde05067 9679/* Check for valid arguments being passed to a function with FNTYPE.
3342fd71
BS
9680 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9681 diagnostics. */
b34c7881 9682void
3342fd71
BS
9683check_function_arguments (location_t loc, const_tree fntype, int nargs,
9684 tree *argarray)
b34c7881
JT
9685{
9686 /* Check for null being passed in a pointer argument that must be
9687 non-null. We also need to do this if format checking is enabled. */
9688
9689 if (warn_nonnull)
3342fd71 9690 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
b34c7881
JT
9691
9692 /* Check for errors in format strings. */
9693
e6c69da0 9694 if (warn_format || warn_suggest_attribute_format)
dde05067 9695 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
7876a414
KG
9696
9697 if (warn_format)
dde05067 9698 check_function_sentinel (fntype, nargs, argarray);
b34c7881
JT
9699}
9700
9701/* Generic argument checking recursion routine. PARAM is the argument to
9702 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9703 once the argument is resolved. CTX is context for the callback. */
9704void
35b1a6fa
AJ
9705check_function_arguments_recurse (void (*callback)
9706 (void *, tree, unsigned HOST_WIDE_INT),
9707 void *ctx, tree param,
9708 unsigned HOST_WIDE_INT param_num)
b34c7881 9709{
1043771b 9710 if (CONVERT_EXPR_P (param)
1344f9a3
JM
9711 && (TYPE_PRECISION (TREE_TYPE (param))
9712 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
b34c7881
JT
9713 {
9714 /* Strip coercion. */
9715 check_function_arguments_recurse (callback, ctx,
6de9cd9a 9716 TREE_OPERAND (param, 0), param_num);
b34c7881
JT
9717 return;
9718 }
9719
9720 if (TREE_CODE (param) == CALL_EXPR)
9721 {
5039610b 9722 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
b34c7881
JT
9723 tree attrs;
9724 bool found_format_arg = false;
9725
9726 /* See if this is a call to a known internationalization function
9727 that modifies a format arg. Such a function may have multiple
9728 format_arg attributes (for example, ngettext). */
9729
9730 for (attrs = TYPE_ATTRIBUTES (type);
9731 attrs;
9732 attrs = TREE_CHAIN (attrs))
9733 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9734 {
5039610b 9735 tree inner_arg;
b34c7881
JT
9736 tree format_num_expr;
9737 int format_num;
9738 int i;
5039610b 9739 call_expr_arg_iterator iter;
b34c7881
JT
9740
9741 /* Extract the argument number, which was previously checked
9742 to be valid. */
9743 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
b34c7881 9744
6b3b8c27 9745 format_num = tree_to_uhwi (format_num_expr);
b34c7881 9746
5039610b
SL
9747 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9748 inner_arg != 0;
9749 inner_arg = next_call_expr_arg (&iter), i++)
b34c7881
JT
9750 if (i == format_num)
9751 {
9752 check_function_arguments_recurse (callback, ctx,
5039610b 9753 inner_arg, param_num);
b34c7881
JT
9754 found_format_arg = true;
9755 break;
9756 }
9757 }
9758
9759 /* If we found a format_arg attribute and did a recursive check,
9760 we are done with checking this argument. Otherwise, we continue
9761 and this will be considered a non-literal. */
9762 if (found_format_arg)
9763 return;
9764 }
9765
9766 if (TREE_CODE (param) == COND_EXPR)
9767 {
7f26f7df
JM
9768 /* Simplify to avoid warning for an impossible case. */
9769 param = fold_for_warn (param);
9770 if (TREE_CODE (param) == COND_EXPR)
9771 {
9772 /* Check both halves of the conditional expression. */
9773 check_function_arguments_recurse (callback, ctx,
9774 TREE_OPERAND (param, 1),
9775 param_num);
9776 check_function_arguments_recurse (callback, ctx,
9777 TREE_OPERAND (param, 2),
9778 param_num);
9779 return;
9780 }
b34c7881
JT
9781 }
9782
9783 (*callback) (ctx, param, param_num);
9784}
e2500fed 9785
a98c2819
MLI
9786/* Checks for a builtin function FNDECL that the number of arguments
9787 NARGS against the required number REQUIRED and issues an error if
9788 there is a mismatch. Returns true if the number of arguments is
9789 correct, otherwise false. */
83322951
RG
9790
9791static bool
a98c2819 9792builtin_function_validate_nargs (tree fndecl, int nargs, int required)
83322951
RG
9793{
9794 if (nargs < required)
9795 {
a98c2819
MLI
9796 error_at (input_location,
9797 "not enough arguments to function %qE", fndecl);
83322951
RG
9798 return false;
9799 }
9800 else if (nargs > required)
9801 {
a98c2819
MLI
9802 error_at (input_location,
9803 "too many arguments to function %qE", fndecl);
83322951
RG
9804 return false;
9805 }
9806 return true;
9807}
9808
9809/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9810 Returns false if there was an error, otherwise true. */
9811
9812bool
9813check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9814{
9815 if (!DECL_BUILT_IN (fndecl)
9816 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9817 return true;
9818
9819 switch (DECL_FUNCTION_CODE (fndecl))
9820 {
35886f0b
MS
9821 case BUILT_IN_ALLOCA_WITH_ALIGN:
9822 {
9823 /* Get the requested alignment (in bits) if it's a constant
9824 integer expression. */
9825 unsigned HOST_WIDE_INT align
9826 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
9827
9828 /* Determine if the requested alignment is a power of 2. */
9829 if ((align & (align - 1)))
9830 align = 0;
9831
9832 /* The maximum alignment in bits corresponding to the same
9833 maximum in bytes enforced in check_user_alignment(). */
9834 unsigned maxalign = (UINT_MAX >> 1) + 1;
9835
9836 /* Reject invalid alignments. */
9837 if (align < BITS_PER_UNIT || maxalign < align)
9838 {
9839 error_at (EXPR_LOC_OR_LOC (args[1], input_location),
9840 "second argument to function %qE must be a constant "
9841 "integer power of 2 between %qi and %qu bits",
9842 fndecl, BITS_PER_UNIT, maxalign);
9843 return false;
9844 }
9845 return true;
9846 }
9847
83322951 9848 case BUILT_IN_CONSTANT_P:
a98c2819 9849 return builtin_function_validate_nargs (fndecl, nargs, 1);
83322951
RG
9850
9851 case BUILT_IN_ISFINITE:
9852 case BUILT_IN_ISINF:
05f41289 9853 case BUILT_IN_ISINF_SIGN:
83322951
RG
9854 case BUILT_IN_ISNAN:
9855 case BUILT_IN_ISNORMAL:
61717a45 9856 case BUILT_IN_SIGNBIT:
a98c2819 9857 if (builtin_function_validate_nargs (fndecl, nargs, 1))
83322951
RG
9858 {
9859 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9860 {
9861 error ("non-floating-point argument in call to "
9862 "function %qE", fndecl);
9863 return false;
9864 }
9865 return true;
9866 }
9867 return false;
9868
9869 case BUILT_IN_ISGREATER:
9870 case BUILT_IN_ISGREATEREQUAL:
9871 case BUILT_IN_ISLESS:
9872 case BUILT_IN_ISLESSEQUAL:
9873 case BUILT_IN_ISLESSGREATER:
9874 case BUILT_IN_ISUNORDERED:
a98c2819 9875 if (builtin_function_validate_nargs (fndecl, nargs, 2))
83322951
RG
9876 {
9877 enum tree_code code0, code1;
9878 code0 = TREE_CODE (TREE_TYPE (args[0]));
9879 code1 = TREE_CODE (TREE_TYPE (args[1]));
9880 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9881 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9882 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9883 {
9884 error ("non-floating-point arguments in call to "
9885 "function %qE", fndecl);
9886 return false;
9887 }
9888 return true;
9889 }
9890 return false;
9891
3bf5906b 9892 case BUILT_IN_FPCLASSIFY:
a98c2819 9893 if (builtin_function_validate_nargs (fndecl, nargs, 6))
3bf5906b
KG
9894 {
9895 unsigned i;
b8698a0f 9896
3bf5906b
KG
9897 for (i=0; i<5; i++)
9898 if (TREE_CODE (args[i]) != INTEGER_CST)
9899 {
9900 error ("non-const integer argument %u in call to function %qE",
9901 i+1, fndecl);
9902 return false;
9903 }
9904
9905 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9906 {
9907 error ("non-floating-point argument in call to function %qE",
9908 fndecl);
9909 return false;
9910 }
9911 return true;
9912 }
9913 return false;
9914
45d439ac
JJ
9915 case BUILT_IN_ASSUME_ALIGNED:
9916 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9917 {
9918 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9919 {
9920 error ("non-integer argument 3 in call to function %qE", fndecl);
9921 return false;
9922 }
9923 return true;
9924 }
9925 return false;
9926
1304953e
JJ
9927 case BUILT_IN_ADD_OVERFLOW:
9928 case BUILT_IN_SUB_OVERFLOW:
9929 case BUILT_IN_MUL_OVERFLOW:
9930 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9931 {
9932 unsigned i;
9933 for (i = 0; i < 2; i++)
9934 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9935 {
9936 error ("argument %u in call to function %qE does not have "
9937 "integral type", i + 1, fndecl);
9938 return false;
9939 }
9940 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9941 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9942 {
9943 error ("argument 3 in call to function %qE does not have "
9944 "pointer to integer type", fndecl);
9945 return false;
9946 }
9947 return true;
9948 }
9949 return false;
9950
83322951
RG
9951 default:
9952 return true;
9953 }
9954}
9955
d07605f5
AP
9956/* Function to help qsort sort FIELD_DECLs by name order. */
9957
9958int
9959field_decl_cmp (const void *x_p, const void *y_p)
9960{
28dab132
BI
9961 const tree *const x = (const tree *const) x_p;
9962 const tree *const y = (const tree *const) y_p;
9963
d07605f5
AP
9964 if (DECL_NAME (*x) == DECL_NAME (*y))
9965 /* A nontype is "greater" than a type. */
9966 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9967 if (DECL_NAME (*x) == NULL_TREE)
9968 return -1;
9969 if (DECL_NAME (*y) == NULL_TREE)
9970 return 1;
9971 if (DECL_NAME (*x) < DECL_NAME (*y))
9972 return -1;
9973 return 1;
9974}
9975
9976static struct {
9977 gt_pointer_operator new_value;
9978 void *cookie;
9979} resort_data;
9980
9981/* This routine compares two fields like field_decl_cmp but using the
9982pointer operator in resort_data. */
9983
9984static int
9985resort_field_decl_cmp (const void *x_p, const void *y_p)
9986{
28dab132
BI
9987 const tree *const x = (const tree *const) x_p;
9988 const tree *const y = (const tree *const) y_p;
d07605f5
AP
9989
9990 if (DECL_NAME (*x) == DECL_NAME (*y))
9991 /* A nontype is "greater" than a type. */
9992 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9993 if (DECL_NAME (*x) == NULL_TREE)
9994 return -1;
9995 if (DECL_NAME (*y) == NULL_TREE)
9996 return 1;
9997 {
9998 tree d1 = DECL_NAME (*x);
9999 tree d2 = DECL_NAME (*y);
10000 resort_data.new_value (&d1, resort_data.cookie);
10001 resort_data.new_value (&d2, resort_data.cookie);
10002 if (d1 < d2)
10003 return -1;
10004 }
10005 return 1;
10006}
10007
10008/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10009
10010void
10011resort_sorted_fields (void *obj,
e18476eb 10012 void * ARG_UNUSED (orig_obj),
6de9cd9a
DN
10013 gt_pointer_operator new_value,
10014 void *cookie)
d07605f5 10015{
e18476eb 10016 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
d07605f5
AP
10017 resort_data.new_value = new_value;
10018 resort_data.cookie = cookie;
10019 qsort (&sf->elts[0], sf->len, sizeof (tree),
6de9cd9a 10020 resort_field_decl_cmp);
d07605f5
AP
10021}
10022
0a3ee0fd
GDR
10023/* Subroutine of c_parse_error.
10024 Return the result of concatenating LHS and RHS. RHS is really
10025 a string literal, its first character is indicated by RHS_START and
3292fb42 10026 RHS_SIZE is its length (including the terminating NUL character).
0a3ee0fd
GDR
10027
10028 The caller is responsible for deleting the returned pointer. */
10029
10030static char *
10031catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10032{
10033 const int lhs_size = strlen (lhs);
10034 char *result = XNEWVEC (char, lhs_size + rhs_size);
10035 strncpy (result, lhs, lhs_size);
10036 strncpy (result + lhs_size, rhs_start, rhs_size);
10037 return result;
10038}
10039
4b794eaf 10040/* Issue the error given by GMSGID, indicating that it occurred before
4bb8ca28
MM
10041 TOKEN, which had the associated VALUE. */
10042
10043void
b8698a0f 10044c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
cfc93532 10045 tree value, unsigned char token_flags)
4bb8ca28 10046{
0a3ee0fd
GDR
10047#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10048
10049 char *message = NULL;
4bb8ca28 10050
cfc93532 10051 if (token_type == CPP_EOF)
4b794eaf 10052 message = catenate_messages (gmsgid, " at end of input");
b8698a0f
L
10053 else if (token_type == CPP_CHAR
10054 || token_type == CPP_WCHAR
cfc93532 10055 || token_type == CPP_CHAR16
fe95b036
ESR
10056 || token_type == CPP_CHAR32
10057 || token_type == CPP_UTF8CHAR)
4bb8ca28
MM
10058 {
10059 unsigned int val = TREE_INT_CST_LOW (value);
b6baa67d
KVH
10060 const char *prefix;
10061
cfc93532 10062 switch (token_type)
b6baa67d
KVH
10063 {
10064 default:
10065 prefix = "";
10066 break;
10067 case CPP_WCHAR:
10068 prefix = "L";
10069 break;
10070 case CPP_CHAR16:
10071 prefix = "u";
10072 break;
10073 case CPP_CHAR32:
10074 prefix = "U";
10075 break;
fe95b036
ESR
10076 case CPP_UTF8CHAR:
10077 prefix = "u8";
10078 break;
b6baa67d
KVH
10079 }
10080
4bb8ca28 10081 if (val <= UCHAR_MAX && ISGRAPH (val))
c22cacf3 10082 message = catenate_messages (gmsgid, " before %s'%c'");
4bb8ca28 10083 else
c22cacf3 10084 message = catenate_messages (gmsgid, " before %s'\\x%x'");
0a3ee0fd 10085
b6baa67d 10086 error (message, prefix, val);
0a3ee0fd
GDR
10087 free (message);
10088 message = NULL;
4bb8ca28 10089 }
65e5a578
ESR
10090 else if (token_type == CPP_CHAR_USERDEF
10091 || token_type == CPP_WCHAR_USERDEF
10092 || token_type == CPP_CHAR16_USERDEF
fe95b036
ESR
10093 || token_type == CPP_CHAR32_USERDEF
10094 || token_type == CPP_UTF8CHAR_USERDEF)
65e5a578
ESR
10095 message = catenate_messages (gmsgid,
10096 " before user-defined character literal");
10097 else if (token_type == CPP_STRING_USERDEF
10098 || token_type == CPP_WSTRING_USERDEF
10099 || token_type == CPP_STRING16_USERDEF
10100 || token_type == CPP_STRING32_USERDEF
10101 || token_type == CPP_UTF8STRING_USERDEF)
10102 message = catenate_messages (gmsgid, " before user-defined string literal");
b8698a0f
L
10103 else if (token_type == CPP_STRING
10104 || token_type == CPP_WSTRING
cfc93532 10105 || token_type == CPP_STRING16
2c6e3f55
JJ
10106 || token_type == CPP_STRING32
10107 || token_type == CPP_UTF8STRING)
4b794eaf 10108 message = catenate_messages (gmsgid, " before string constant");
cfc93532 10109 else if (token_type == CPP_NUMBER)
4b794eaf 10110 message = catenate_messages (gmsgid, " before numeric constant");
cfc93532 10111 else if (token_type == CPP_NAME)
0a3ee0fd 10112 {
4b794eaf 10113 message = catenate_messages (gmsgid, " before %qE");
c51a1ba9 10114 error (message, value);
0a3ee0fd
GDR
10115 free (message);
10116 message = NULL;
10117 }
cfc93532 10118 else if (token_type == CPP_PRAGMA)
bc4071dd 10119 message = catenate_messages (gmsgid, " before %<#pragma%>");
cfc93532 10120 else if (token_type == CPP_PRAGMA_EOL)
bc4071dd 10121 message = catenate_messages (gmsgid, " before end of line");
34429675
JM
10122 else if (token_type == CPP_DECLTYPE)
10123 message = catenate_messages (gmsgid, " before %<decltype%>");
cfc93532 10124 else if (token_type < N_TTYPES)
0a3ee0fd 10125 {
4b794eaf 10126 message = catenate_messages (gmsgid, " before %qs token");
cfc93532 10127 error (message, cpp_type2name (token_type, token_flags));
0a3ee0fd
GDR
10128 free (message);
10129 message = NULL;
10130 }
4bb8ca28 10131 else
4b794eaf 10132 error (gmsgid);
0a3ee0fd
GDR
10133
10134 if (message)
10135 {
10136 error (message);
10137 free (message);
10138 }
c22cacf3 10139#undef catenate_messages
4bb8ca28
MM
10140}
10141
87cf0651
SB
10142/* Return the gcc option code associated with the reason for a cpp
10143 message, or 0 if none. */
10144
10145static int
10146c_option_controlling_cpp_error (int reason)
10147{
b559c810 10148 const struct cpp_reason_option_codes_t *entry;
87cf0651 10149
b559c810 10150 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
87cf0651
SB
10151 {
10152 if (entry->reason == reason)
10153 return entry->option_code;
10154 }
10155 return 0;
10156}
10157
148e4216 10158/* Callback from cpp_error for PFILE to print diagnostics from the
87cf0651
SB
10159 preprocessor. The diagnostic is of type LEVEL, with REASON set
10160 to the reason code if LEVEL is represents a warning, at location
8a645150
DM
10161 RICHLOC unless this is after lexing and the compiler's location
10162 should be used instead; MSG is the translated message and AP
148e4216
JM
10163 the arguments. Returns true if a diagnostic was emitted, false
10164 otherwise. */
10165
10166bool
87cf0651 10167c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
8a645150 10168 rich_location *richloc,
148e4216
JM
10169 const char *msg, va_list *ap)
10170{
10171 diagnostic_info diagnostic;
10172 diagnostic_t dlevel;
e3339d0f 10173 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
148e4216
JM
10174 bool ret;
10175
10176 switch (level)
10177 {
10178 case CPP_DL_WARNING_SYSHDR:
10179 if (flag_no_output)
10180 return false;
e3339d0f 10181 global_dc->dc_warn_system_headers = 1;
148e4216
JM
10182 /* Fall through. */
10183 case CPP_DL_WARNING:
10184 if (flag_no_output)
10185 return false;
10186 dlevel = DK_WARNING;
10187 break;
10188 case CPP_DL_PEDWARN:
10189 if (flag_no_output && !flag_pedantic_errors)
10190 return false;
10191 dlevel = DK_PEDWARN;
10192 break;
10193 case CPP_DL_ERROR:
10194 dlevel = DK_ERROR;
10195 break;
10196 case CPP_DL_ICE:
10197 dlevel = DK_ICE;
10198 break;
10199 case CPP_DL_NOTE:
10200 dlevel = DK_NOTE;
10201 break;
47580d22
JM
10202 case CPP_DL_FATAL:
10203 dlevel = DK_FATAL;
10204 break;
148e4216
JM
10205 default:
10206 gcc_unreachable ();
10207 }
10208 if (done_lexing)
f79520bb 10209 richloc->set_range (line_table, 0, input_location, true);
148e4216 10210 diagnostic_set_info_translated (&diagnostic, msg, ap,
8a645150 10211 richloc, dlevel);
87cf0651
SB
10212 diagnostic_override_option_index (&diagnostic,
10213 c_option_controlling_cpp_error (reason));
148e4216
JM
10214 ret = report_diagnostic (&diagnostic);
10215 if (level == CPP_DL_WARNING_SYSHDR)
e3339d0f 10216 global_dc->dc_warn_system_headers = save_warn_system_headers;
148e4216
JM
10217 return ret;
10218}
10219
c5ff069d
ZW
10220/* Convert a character from the host to the target execution character
10221 set. cpplib handles this, mostly. */
10222
10223HOST_WIDE_INT
10224c_common_to_target_charset (HOST_WIDE_INT c)
10225{
10226 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10227 zero-extended under -fno-signed-char. cpplib insists that characters
10228 and character constants are always unsigned. Hence we must convert
10229 back and forth. */
10230 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10231
10232 uc = cpp_host_to_exec_charset (parse_in, uc);
10233
10234 if (flag_signed_char)
10235 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10236 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10237 else
10238 return uc;
10239}
10240
cf9e9959
EB
10241/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10242 references with an INDIRECT_REF of a constant at the bottom; much like the
10243 traditional rendering of offsetof as a macro. Return the folded result. */
ee8a6a3e 10244
cf9e9959 10245tree
c85158de 10246fold_offsetof_1 (tree expr, enum tree_code ctx)
ee8a6a3e 10247{
ee8a6a3e 10248 tree base, off, t;
c85158de
MS
10249 tree_code code = TREE_CODE (expr);
10250 switch (code)
ee8a6a3e
RH
10251 {
10252 case ERROR_MARK:
10253 return expr;
10254
545b7d8c
VR
10255 case VAR_DECL:
10256 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10257 return error_mark_node;
10258
6d4d7b0e 10259 case CALL_EXPR:
8d5f60ac 10260 case TARGET_EXPR:
6d4d7b0e
PB
10261 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10262 return error_mark_node;
10263
6d4d7b0e
PB
10264 case NOP_EXPR:
10265 case INDIRECT_REF:
cf9e9959 10266 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
61c3c490
DS
10267 {
10268 error ("cannot apply %<offsetof%> to a non constant address");
10269 return error_mark_node;
10270 }
cf9e9959 10271 return TREE_OPERAND (expr, 0);
6d4d7b0e 10272
ee8a6a3e 10273 case COMPONENT_REF:
c85158de 10274 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
ee8a6a3e
RH
10275 if (base == error_mark_node)
10276 return base;
10277
10278 t = TREE_OPERAND (expr, 1);
10279 if (DECL_C_BIT_FIELD (t))
10280 {
10281 error ("attempt to take address of bit-field structure "
c51a1ba9 10282 "member %qD", t);
ee8a6a3e
RH
10283 return error_mark_node;
10284 }
db3927fb 10285 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
386b1f1f 10286 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
db3927fb 10287 / BITS_PER_UNIT));
ee8a6a3e
RH
10288 break;
10289
10290 case ARRAY_REF:
c85158de 10291 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
ee8a6a3e
RH
10292 if (base == error_mark_node)
10293 return base;
10294
10295 t = TREE_OPERAND (expr, 1);
61c3c490
DS
10296
10297 /* Check if the offset goes beyond the upper bound of the array. */
cf9e9959 10298 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
d32599a6
JJ
10299 {
10300 tree upbound = array_ref_up_bound (expr);
10301 if (upbound != NULL_TREE
10302 && TREE_CODE (upbound) == INTEGER_CST
10303 && !tree_int_cst_equal (upbound,
10304 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10305 {
c85158de
MS
10306 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10307 upbound = size_binop (PLUS_EXPR, upbound,
10308 build_int_cst (TREE_TYPE (upbound), 1));
d32599a6
JJ
10309 if (tree_int_cst_lt (upbound, t))
10310 {
10311 tree v;
10312
10313 for (v = TREE_OPERAND (expr, 0);
10314 TREE_CODE (v) == COMPONENT_REF;
10315 v = TREE_OPERAND (v, 0))
10316 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10317 == RECORD_TYPE)
10318 {
910ad8de
NF
10319 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10320 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
d32599a6
JJ
10321 if (TREE_CODE (fld_chain) == FIELD_DECL)
10322 break;
10323
10324 if (fld_chain)
10325 break;
10326 }
10327 /* Don't warn if the array might be considered a poor
10328 man's flexible array member with a very permissive
10329 definition thereof. */
10330 if (TREE_CODE (v) == ARRAY_REF
10331 || TREE_CODE (v) == COMPONENT_REF)
10332 warning (OPT_Warray_bounds,
10333 "index %E denotes an offset "
10334 "greater than size of %qT",
10335 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10336 }
10337 }
10338 }
cf9e9959
EB
10339
10340 t = convert (sizetype, t);
10341 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
ee8a6a3e
RH
10342 break;
10343
1916c916
VR
10344 case COMPOUND_EXPR:
10345 /* Handle static members of volatile structs. */
10346 t = TREE_OPERAND (expr, 1);
0ae9bd27 10347 gcc_assert (VAR_P (t));
cf9e9959 10348 return fold_offsetof_1 (t);
1916c916 10349
ee8a6a3e 10350 default:
366de0ce 10351 gcc_unreachable ();
ee8a6a3e
RH
10352 }
10353
cf9e9959 10354 return fold_build_pointer_plus (base, off);
ee8a6a3e
RH
10355}
10356
cf9e9959
EB
10357/* Likewise, but convert it to the return type of offsetof. */
10358
ee8a6a3e 10359tree
cf9e9959 10360fold_offsetof (tree expr)
ee8a6a3e 10361{
cf9e9959 10362 return convert (size_type_node, fold_offsetof_1 (expr));
ee8a6a3e
RH
10363}
10364
d166d4c3
AK
10365/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10366 expression, because B will always be true. */
10367
10368void
10369warn_for_omitted_condop (location_t location, tree cond)
10370{
10371 if (truth_value_p (TREE_CODE (cond)))
10372 warning_at (location, OPT_Wparentheses,
10373 "the omitted middle operand in ?: will always be %<true%>, "
10374 "suggest explicit middle operand");
10375}
10376
4816c593
NF
10377/* Give an error for storing into ARG, which is 'const'. USE indicates
10378 how ARG was being used. */
10379
10380void
c02065fc 10381readonly_error (location_t loc, tree arg, enum lvalue_use use)
4816c593
NF
10382{
10383 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10384 || use == lv_asm);
10385 /* Using this macro rather than (for example) arrays of messages
10386 ensures that all the format strings are checked at compile
10387 time. */
10388#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10389 : (use == lv_increment ? (I) \
10390 : (use == lv_decrement ? (D) : (AS))))
10391 if (TREE_CODE (arg) == COMPONENT_REF)
10392 {
10393 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
c02065fc
AH
10394 error_at (loc, READONLY_MSG (G_("assignment of member "
10395 "%qD in read-only object"),
10396 G_("increment of member "
10397 "%qD in read-only object"),
10398 G_("decrement of member "
10399 "%qD in read-only object"),
10400 G_("member %qD in read-only object "
10401 "used as %<asm%> output")),
10402 TREE_OPERAND (arg, 1));
4816c593 10403 else
c02065fc
AH
10404 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10405 G_("increment of read-only member %qD"),
10406 G_("decrement of read-only member %qD"),
10407 G_("read-only member %qD used as %<asm%> output")),
10408 TREE_OPERAND (arg, 1));
4816c593 10409 }
0ae9bd27 10410 else if (VAR_P (arg))
c02065fc
AH
10411 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10412 G_("increment of read-only variable %qD"),
10413 G_("decrement of read-only variable %qD"),
10414 G_("read-only variable %qD used as %<asm%> output")),
10415 arg);
4816c593 10416 else if (TREE_CODE (arg) == PARM_DECL)
c02065fc
AH
10417 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10418 G_("increment of read-only parameter %qD"),
10419 G_("decrement of read-only parameter %qD"),
10420 G_("read-only parameter %qD use as %<asm%> output")),
10421 arg);
4816c593
NF
10422 else if (TREE_CODE (arg) == RESULT_DECL)
10423 {
10424 gcc_assert (c_dialect_cxx ());
c02065fc
AH
10425 error_at (loc, READONLY_MSG (G_("assignment of "
10426 "read-only named return value %qD"),
10427 G_("increment of "
10428 "read-only named return value %qD"),
10429 G_("decrement of "
10430 "read-only named return value %qD"),
10431 G_("read-only named return value %qD "
10432 "used as %<asm%>output")),
10433 arg);
4816c593
NF
10434 }
10435 else if (TREE_CODE (arg) == FUNCTION_DECL)
c02065fc
AH
10436 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10437 G_("increment of function %qD"),
10438 G_("decrement of function %qD"),
10439 G_("function %qD used as %<asm%> output")),
10440 arg);
4816c593 10441 else
c02065fc
AH
10442 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10443 G_("increment of read-only location %qE"),
10444 G_("decrement of read-only location %qE"),
10445 G_("read-only location %qE used as %<asm%> output")),
10446 arg);
4816c593
NF
10447}
10448
37dc0d8d 10449/* Print an error message for an invalid lvalue. USE says
7bd11157
TT
10450 how the lvalue is being used and so selects the error message. LOC
10451 is the location for the error. */
5ae9ba3e 10452
37dc0d8d 10453void
7bd11157 10454lvalue_error (location_t loc, enum lvalue_use use)
5ae9ba3e 10455{
37dc0d8d 10456 switch (use)
5ae9ba3e 10457 {
37dc0d8d 10458 case lv_assign:
7bd11157 10459 error_at (loc, "lvalue required as left operand of assignment");
37dc0d8d
JM
10460 break;
10461 case lv_increment:
7bd11157 10462 error_at (loc, "lvalue required as increment operand");
37dc0d8d
JM
10463 break;
10464 case lv_decrement:
7bd11157 10465 error_at (loc, "lvalue required as decrement operand");
37dc0d8d
JM
10466 break;
10467 case lv_addressof:
7bd11157 10468 error_at (loc, "lvalue required as unary %<&%> operand");
37dc0d8d
JM
10469 break;
10470 case lv_asm:
7bd11157 10471 error_at (loc, "lvalue required in asm statement");
37dc0d8d
JM
10472 break;
10473 default:
10474 gcc_unreachable ();
5ae9ba3e 10475 }
5ae9ba3e 10476}
7a6daeb0
NF
10477
10478/* Print an error message for an invalid indirection of type TYPE.
10479 ERRSTRING is the name of the operator for the indirection. */
10480
10481void
10482invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10483{
10484 switch (errstring)
10485 {
10486 case RO_NULL:
10487 gcc_assert (c_dialect_cxx ());
10488 error_at (loc, "invalid type argument (have %qT)", type);
10489 break;
10490 case RO_ARRAY_INDEXING:
10491 error_at (loc,
10492 "invalid type argument of array indexing (have %qT)",
10493 type);
10494 break;
10495 case RO_UNARY_STAR:
10496 error_at (loc,
10497 "invalid type argument of unary %<*%> (have %qT)",
10498 type);
10499 break;
10500 case RO_ARROW:
10501 error_at (loc,
10502 "invalid type argument of %<->%> (have %qT)",
10503 type);
10504 break;
55a7f02f
MP
10505 case RO_ARROW_STAR:
10506 error_at (loc,
10507 "invalid type argument of %<->*%> (have %qT)",
10508 type);
10509 break;
7a6daeb0
NF
10510 case RO_IMPLICIT_CONVERSION:
10511 error_at (loc,
10512 "invalid type argument of implicit conversion (have %qT)",
10513 type);
10514 break;
10515 default:
10516 gcc_unreachable ();
10517 }
10518}
aab038d5
RH
10519\f
10520/* *PTYPE is an incomplete array. Complete it with a domain based on
10521 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10522 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10523 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10524
10525int
10526complete_array_type (tree *ptype, tree initial_value, bool do_default)
10527{
10528 tree maxindex, type, main_type, elt, unqual_elt;
10529 int failure = 0, quals;
06d40de8 10530 hashval_t hashcode = 0;
40d3d530 10531 bool overflow_p = false;
aab038d5
RH
10532
10533 maxindex = size_zero_node;
10534 if (initial_value)
10535 {
10536 if (TREE_CODE (initial_value) == STRING_CST)
10537 {
10538 int eltsize
10539 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10540 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10541 }
10542 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10543 {
9771b263 10544 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
aab038d5 10545
9771b263 10546 if (vec_safe_is_empty (v))
aab038d5
RH
10547 {
10548 if (pedantic)
10549 failure = 3;
830c740f 10550 maxindex = ssize_int (-1);
aab038d5
RH
10551 }
10552 else
10553 {
10554 tree curindex;
4038c495
GB
10555 unsigned HOST_WIDE_INT cnt;
10556 constructor_elt *ce;
cff7525f 10557 bool fold_p = false;
aab038d5 10558
9771b263 10559 if ((*v)[0].index)
40d3d530
JR
10560 maxindex = (*v)[0].index, fold_p = true;
10561
aab038d5
RH
10562 curindex = maxindex;
10563
9771b263 10564 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
aab038d5 10565 {
cff7525f 10566 bool curfold_p = false;
4038c495 10567 if (ce->index)
cff7525f 10568 curindex = ce->index, curfold_p = true;
aab038d5 10569 else
cff7525f
JH
10570 {
10571 if (fold_p)
40d3d530
JR
10572 {
10573 /* Since we treat size types now as ordinary
10574 unsigned types, we need an explicit overflow
10575 check. */
10576 tree orig = curindex;
10577 curindex = fold_convert (sizetype, curindex);
10578 overflow_p |= tree_int_cst_lt (curindex, orig);
10579 }
db3927fb
AH
10580 curindex = size_binop (PLUS_EXPR, curindex,
10581 size_one_node);
cff7525f 10582 }
aab038d5 10583 if (tree_int_cst_lt (maxindex, curindex))
cff7525f 10584 maxindex = curindex, fold_p = curfold_p;
aab038d5 10585 }
40d3d530
JR
10586 if (fold_p)
10587 {
10588 tree orig = maxindex;
10589 maxindex = fold_convert (sizetype, maxindex);
10590 overflow_p |= tree_int_cst_lt (maxindex, orig);
10591 }
aab038d5
RH
10592 }
10593 }
10594 else
10595 {
10596 /* Make an error message unless that happened already. */
10597 if (initial_value != error_mark_node)
10598 failure = 1;
10599 }
10600 }
10601 else
10602 {
10603 failure = 2;
10604 if (!do_default)
10605 return failure;
10606 }
10607
10608 type = *ptype;
10609 elt = TREE_TYPE (type);
10610 quals = TYPE_QUALS (strip_array_types (elt));
10611 if (quals == 0)
10612 unqual_elt = elt;
10613 else
36c5e70a 10614 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
aab038d5
RH
10615
10616 /* Using build_distinct_type_copy and modifying things afterward instead
10617 of using build_array_type to create a new type preserves all of the
10618 TYPE_LANG_FLAG_? bits that the front end may have set. */
10619 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10620 TREE_TYPE (main_type) = unqual_elt;
e6313a78
RG
10621 TYPE_DOMAIN (main_type)
10622 = build_range_type (TREE_TYPE (maxindex),
10623 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
aab038d5
RH
10624 layout_type (main_type);
10625
06d40de8
DG
10626 /* Make sure we have the canonical MAIN_TYPE. */
10627 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
b8698a0f 10628 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
06d40de8
DG
10629 hashcode);
10630 main_type = type_hash_canon (hashcode, main_type);
10631
9ae165a0
DG
10632 /* Fix the canonical type. */
10633 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10634 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10635 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10636 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10637 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10638 != TYPE_DOMAIN (main_type)))
b8698a0f 10639 TYPE_CANONICAL (main_type)
9ae165a0
DG
10640 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10641 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10642 else
10643 TYPE_CANONICAL (main_type) = main_type;
10644
aab038d5
RH
10645 if (quals == 0)
10646 type = main_type;
10647 else
10648 type = c_build_qualified_type (main_type, quals);
10649
7bfcb402
JM
10650 if (COMPLETE_TYPE_P (type)
10651 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
40d3d530 10652 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7bfcb402
JM
10653 {
10654 error ("size of array is too large");
10655 /* If we proceed with the array type as it is, we'll eventually
386b1f1f 10656 crash in tree_to_[su]hwi(). */
7bfcb402
JM
10657 type = error_mark_node;
10658 }
10659
aab038d5
RH
10660 *ptype = type;
10661 return failure;
10662}
5ae9ba3e 10663
30cd1c5d
AS
10664/* Like c_mark_addressable but don't check register qualifier. */
10665void
10666c_common_mark_addressable_vec (tree t)
10667{
10668 while (handled_component_p (t))
10669 t = TREE_OPERAND (t, 0);
0ae9bd27 10670 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
30cd1c5d
AS
10671 return;
10672 TREE_ADDRESSABLE (t) = 1;
10673}
10674
10675
48ae6c13
RH
10676\f
10677/* Used to help initialize the builtin-types.def table. When a type of
10678 the correct size doesn't exist, use error_mark_node instead of NULL.
10679 The later results in segfaults even when a decl using the type doesn't
10680 get invoked. */
10681
10682tree
10683builtin_type_for_size (int size, bool unsignedp)
10684{
21fa2faf 10685 tree type = c_common_type_for_size (size, unsignedp);
48ae6c13
RH
10686 return type ? type : error_mark_node;
10687}
10688
10689/* A helper function for resolve_overloaded_builtin in resolving the
10690 overloaded __sync_ builtins. Returns a positive power of 2 if the
10691 first operand of PARAMS is a pointer to a supported data type.
7a127fa7
MS
10692 Returns 0 if an error is encountered.
10693 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10694 built-ins. */
48ae6c13
RH
10695
10696static int
7a127fa7 10697sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
48ae6c13 10698{
7a127fa7
MS
10699 /* Type of the argument. */
10700 tree argtype;
10701 /* Type the argument points to. */
48ae6c13
RH
10702 tree type;
10703 int size;
10704
ba6b3795 10705 if (vec_safe_is_empty (params))
48ae6c13
RH
10706 {
10707 error ("too few arguments to function %qE", function);
10708 return 0;
10709 }
10710
7a127fa7 10711 argtype = type = TREE_TYPE ((*params)[0]);
6415bd5d
JM
10712 if (TREE_CODE (type) == ARRAY_TYPE)
10713 {
10714 /* Force array-to-pointer decay for C++. */
10715 gcc_assert (c_dialect_cxx());
10716 (*params)[0] = default_conversion ((*params)[0]);
10717 type = TREE_TYPE ((*params)[0]);
10718 }
48ae6c13
RH
10719 if (TREE_CODE (type) != POINTER_TYPE)
10720 goto incompatible;
10721
10722 type = TREE_TYPE (type);
10723 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10724 goto incompatible;
10725
7a127fa7
MS
10726 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
10727 goto incompatible;
10728
ae7e9ddd 10729 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
a0274e3e 10730 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
48ae6c13
RH
10731 return size;
10732
10733 incompatible:
9f04a53e
MS
10734 /* Issue the diagnostic only if the argument is valid, otherwise
10735 it would be redundant at best and could be misleading. */
10736 if (argtype != error_mark_node)
10737 error ("operand type %qT is incompatible with argument %d of %qE",
10738 argtype, 1, function);
48ae6c13
RH
10739 return 0;
10740}
10741
c22cacf3 10742/* A helper function for resolve_overloaded_builtin. Adds casts to
48ae6c13
RH
10743 PARAMS to make arguments match up with those of FUNCTION. Drops
10744 the variadic arguments at the end. Returns false if some error
10745 was encountered; true on success. */
10746
10747static bool
86951993 10748sync_resolve_params (location_t loc, tree orig_function, tree function,
9771b263 10749 vec<tree, va_gc> *params, bool orig_format)
48ae6c13 10750{
e19a18d4 10751 function_args_iterator iter;
48ae6c13 10752 tree ptype;
bbbbb16a 10753 unsigned int parmnum;
48ae6c13 10754
e19a18d4 10755 function_args_iter_init (&iter, TREE_TYPE (function));
48ae6c13
RH
10756 /* We've declared the implementation functions to use "volatile void *"
10757 as the pointer parameter, so we shouldn't get any complaints from the
10758 call to check_function_arguments what ever type the user used. */
e19a18d4 10759 function_args_iter_next (&iter);
9771b263 10760 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
267bac10 10761 ptype = TYPE_MAIN_VARIANT (ptype);
48ae6c13
RH
10762
10763 /* For the rest of the values, we need to cast these to FTYPE, so that we
10764 don't get warnings for passing pointer types, etc. */
bbbbb16a 10765 parmnum = 0;
e19a18d4 10766 while (1)
48ae6c13 10767 {
e19a18d4
NF
10768 tree val, arg_type;
10769
10770 arg_type = function_args_iter_cond (&iter);
10771 /* XXX void_type_node belies the abstraction. */
10772 if (arg_type == void_type_node)
10773 break;
48ae6c13 10774
bbbbb16a 10775 ++parmnum;
9771b263 10776 if (params->length () <= parmnum)
48ae6c13 10777 {
86951993 10778 error_at (loc, "too few arguments to function %qE", orig_function);
48ae6c13
RH
10779 return false;
10780 }
10781
e3793c6f
JJ
10782 /* Only convert parameters if arg_type is unsigned integer type with
10783 new format sync routines, i.e. don't attempt to convert pointer
10784 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10785 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10786 kinds). */
10787 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
86951993
AM
10788 {
10789 /* Ideally for the first conversion we'd use convert_for_assignment
10790 so that we get warnings for anything that doesn't match the pointer
10791 type. This isn't portable across the C and C++ front ends atm. */
9771b263 10792 val = (*params)[parmnum];
86951993
AM
10793 val = convert (ptype, val);
10794 val = convert (arg_type, val);
9771b263 10795 (*params)[parmnum] = val;
86951993 10796 }
48ae6c13 10797
e19a18d4 10798 function_args_iter_next (&iter);
48ae6c13
RH
10799 }
10800
86951993 10801 /* __atomic routines are not variadic. */
9771b263 10802 if (!orig_format && params->length () != parmnum + 1)
86951993
AM
10803 {
10804 error_at (loc, "too many arguments to function %qE", orig_function);
10805 return false;
10806 }
10807
48ae6c13
RH
10808 /* The definition of these primitives is variadic, with the remaining
10809 being "an optional list of variables protected by the memory barrier".
10810 No clue what that's supposed to mean, precisely, but we consider all
10811 call-clobbered variables to be protected so we're safe. */
9771b263 10812 params->truncate (parmnum + 1);
48ae6c13
RH
10813
10814 return true;
10815}
10816
c22cacf3 10817/* A helper function for resolve_overloaded_builtin. Adds a cast to
48ae6c13
RH
10818 RESULT to make it match the type of the first pointer argument in
10819 PARAMS. */
10820
10821static tree
86951993 10822sync_resolve_return (tree first_param, tree result, bool orig_format)
48ae6c13 10823{
bbbbb16a 10824 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
86951993 10825 tree rtype = TREE_TYPE (result);
99db1ef0 10826 ptype = TYPE_MAIN_VARIANT (ptype);
86951993
AM
10827
10828 /* New format doesn't require casting unless the types are the same size. */
10829 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10830 return convert (ptype, result);
10831 else
10832 return result;
10833}
10834
10835/* This function verifies the PARAMS to generic atomic FUNCTION.
10836 It returns the size if all the parameters are the same size, otherwise
10837 0 is returned if the parameters are invalid. */
10838
10839static int
9771b263
DN
10840get_atomic_generic_size (location_t loc, tree function,
10841 vec<tree, va_gc> *params)
86951993
AM
10842{
10843 unsigned int n_param;
10844 unsigned int n_model;
10845 unsigned int x;
10846 int size_0;
10847 tree type_0;
10848
10849 /* Determine the parameter makeup. */
10850 switch (DECL_FUNCTION_CODE (function))
10851 {
10852 case BUILT_IN_ATOMIC_EXCHANGE:
10853 n_param = 4;
10854 n_model = 1;
10855 break;
10856 case BUILT_IN_ATOMIC_LOAD:
10857 case BUILT_IN_ATOMIC_STORE:
10858 n_param = 3;
10859 n_model = 1;
10860 break;
10861 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10862 n_param = 6;
10863 n_model = 2;
10864 break;
10865 default:
c466c4ff 10866 gcc_unreachable ();
86951993
AM
10867 }
10868
9771b263 10869 if (vec_safe_length (params) != n_param)
86951993
AM
10870 {
10871 error_at (loc, "incorrect number of arguments to function %qE", function);
10872 return 0;
10873 }
10874
10875 /* Get type of first parameter, and determine its size. */
9771b263 10876 type_0 = TREE_TYPE ((*params)[0]);
6415bd5d
JM
10877 if (TREE_CODE (type_0) == ARRAY_TYPE)
10878 {
10879 /* Force array-to-pointer decay for C++. */
10880 gcc_assert (c_dialect_cxx());
10881 (*params)[0] = default_conversion ((*params)[0]);
10882 type_0 = TREE_TYPE ((*params)[0]);
10883 }
c466c4ff
AM
10884 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10885 {
10886 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10887 function);
10888 return 0;
10889 }
10890
10891 /* Types must be compile time constant sizes. */
10892 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
86951993 10893 {
c466c4ff
AM
10894 error_at (loc,
10895 "argument 1 of %qE must be a pointer to a constant size type",
10896 function);
86951993
AM
10897 return 0;
10898 }
c466c4ff 10899
ae7e9ddd 10900 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
86951993 10901
c466c4ff
AM
10902 /* Zero size objects are not allowed. */
10903 if (size_0 == 0)
10904 {
10905 error_at (loc,
10906 "argument 1 of %qE must be a pointer to a nonzero size object",
10907 function);
10908 return 0;
10909 }
10910
86951993
AM
10911 /* Check each other parameter is a pointer and the same size. */
10912 for (x = 0; x < n_param - n_model; x++)
10913 {
10914 int size;
9771b263 10915 tree type = TREE_TYPE ((*params)[x]);
688010ba 10916 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
86951993
AM
10917 if (n_param == 6 && x == 3)
10918 continue;
10919 if (!POINTER_TYPE_P (type))
10920 {
10921 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10922 function);
10923 return 0;
10924 }
7b56b2f8
MP
10925 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10926 size = type_size ? tree_to_uhwi (type_size) : 0;
86951993
AM
10927 if (size != size_0)
10928 {
10929 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10930 function);
10931 return 0;
10932 }
10933 }
10934
10935 /* Check memory model parameters for validity. */
10936 for (x = n_param - n_model ; x < n_param; x++)
10937 {
9771b263 10938 tree p = (*params)[x];
86951993
AM
10939 if (TREE_CODE (p) == INTEGER_CST)
10940 {
ae7e9ddd 10941 int i = tree_to_uhwi (p);
46b35980 10942 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
86951993
AM
10943 {
10944 warning_at (loc, OPT_Winvalid_memory_model,
10945 "invalid memory model argument %d of %qE", x + 1,
10946 function);
86951993
AM
10947 }
10948 }
10949 else
10950 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10951 {
10952 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10953 function);
10954 return 0;
10955 }
10956 }
10957
10958 return size_0;
10959}
10960
10961
10962/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10963 at the beginning of the parameter list PARAMS representing the size of the
10964 objects. This is to match the library ABI requirement. LOC is the location
10965 of the function call.
10966 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10967 returned to allow the external call to be constructed. */
10968
10969static tree
10970add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9771b263 10971 vec<tree, va_gc> *params)
86951993
AM
10972{
10973 tree size_node;
10974
10975 /* Insert a SIZE_T parameter as the first param. If there isn't
10976 enough space, allocate a new vector and recursively re-build with that. */
9771b263 10977 if (!params->space (1))
86951993
AM
10978 {
10979 unsigned int z, len;
9771b263 10980 vec<tree, va_gc> *v;
86951993
AM
10981 tree f;
10982
9771b263
DN
10983 len = params->length ();
10984 vec_alloc (v, len + 1);
8edbfaa6 10985 v->quick_push (build_int_cst (size_type_node, n));
86951993 10986 for (z = 0; z < len; z++)
9771b263 10987 v->quick_push ((*params)[z]);
81e5eca8 10988 f = build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 10989 vec_free (v);
86951993
AM
10990 return f;
10991 }
10992
10993 /* Add the size parameter and leave as a function call for processing. */
10994 size_node = build_int_cst (size_type_node, n);
9771b263 10995 params->quick_insert (0, size_node);
86951993
AM
10996 return NULL_TREE;
10997}
10998
10999
6b28e197
JM
11000/* Return whether atomic operations for naturally aligned N-byte
11001 arguments are supported, whether inline or through libatomic. */
11002static bool
11003atomic_size_supported_p (int n)
11004{
11005 switch (n)
11006 {
11007 case 1:
11008 case 2:
11009 case 4:
11010 case 8:
11011 return true;
11012
11013 case 16:
11014 return targetm.scalar_mode_supported_p (TImode);
11015
11016 default:
11017 return false;
11018 }
11019}
11020
86951993
AM
11021/* This will process an __atomic_exchange function call, determine whether it
11022 needs to be mapped to the _N variation, or turned into a library call.
11023 LOC is the location of the builtin call.
11024 FUNCTION is the DECL that has been invoked;
11025 PARAMS is the argument list for the call. The return value is non-null
11026 TRUE is returned if it is translated into the proper format for a call to the
11027 external library, and NEW_RETURN is set the tree for that function.
11028 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11029 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11030static bool
11031resolve_overloaded_atomic_exchange (location_t loc, tree function,
9771b263 11032 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11033{
11034 tree p0, p1, p2, p3;
11035 tree I_type, I_type_ptr;
11036 int n = get_atomic_generic_size (loc, function, params);
11037
c466c4ff
AM
11038 /* Size of 0 is an error condition. */
11039 if (n == 0)
11040 {
11041 *new_return = error_mark_node;
11042 return true;
11043 }
11044
86951993 11045 /* If not a lock-free size, change to the library generic format. */
6b28e197 11046 if (!atomic_size_supported_p (n))
86951993
AM
11047 {
11048 *new_return = add_atomic_size_parameter (n, loc, function, params);
11049 return true;
11050 }
11051
11052 /* Otherwise there is a lockfree match, transform the call from:
11053 void fn(T* mem, T* desired, T* return, model)
11054 into
11055 *return = (T) (fn (In* mem, (In) *desired, model)) */
11056
9771b263
DN
11057 p0 = (*params)[0];
11058 p1 = (*params)[1];
11059 p2 = (*params)[2];
11060 p3 = (*params)[3];
86951993
AM
11061
11062 /* Create pointer to appropriate size. */
11063 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11064 I_type_ptr = build_pointer_type (I_type);
11065
11066 /* Convert object pointer to required type. */
11067 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11068 (*params)[0] = p0;
86951993
AM
11069 /* Convert new value to required type, and dereference it. */
11070 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11071 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11072 (*params)[1] = p1;
86951993
AM
11073
11074 /* Move memory model to the 3rd position, and end param list. */
9771b263
DN
11075 (*params)[2] = p3;
11076 params->truncate (3);
86951993
AM
11077
11078 /* Convert return pointer and dereference it for later assignment. */
11079 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11080
11081 return false;
48ae6c13
RH
11082}
11083
86951993
AM
11084
11085/* This will process an __atomic_compare_exchange function call, determine
11086 whether it needs to be mapped to the _N variation, or turned into a lib call.
11087 LOC is the location of the builtin call.
11088 FUNCTION is the DECL that has been invoked;
11089 PARAMS is the argument list for the call. The return value is non-null
11090 TRUE is returned if it is translated into the proper format for a call to the
11091 external library, and NEW_RETURN is set the tree for that function.
11092 FALSE is returned if processing for the _N variation is required. */
11093
11094static bool
11095resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9771b263 11096 vec<tree, va_gc> *params,
86951993
AM
11097 tree *new_return)
11098{
11099 tree p0, p1, p2;
11100 tree I_type, I_type_ptr;
11101 int n = get_atomic_generic_size (loc, function, params);
11102
c466c4ff
AM
11103 /* Size of 0 is an error condition. */
11104 if (n == 0)
11105 {
11106 *new_return = error_mark_node;
11107 return true;
11108 }
11109
86951993 11110 /* If not a lock-free size, change to the library generic format. */
6b28e197 11111 if (!atomic_size_supported_p (n))
86951993
AM
11112 {
11113 /* The library generic format does not have the weak parameter, so
11114 remove it from the param list. Since a parameter has been removed,
11115 we can be sure that there is room for the SIZE_T parameter, meaning
11116 there will not be a recursive rebuilding of the parameter list, so
11117 there is no danger this will be done twice. */
11118 if (n > 0)
11119 {
9771b263
DN
11120 (*params)[3] = (*params)[4];
11121 (*params)[4] = (*params)[5];
11122 params->truncate (5);
86951993
AM
11123 }
11124 *new_return = add_atomic_size_parameter (n, loc, function, params);
11125 return true;
11126 }
11127
11128 /* Otherwise, there is a match, so the call needs to be transformed from:
11129 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11130 into
11131 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11132
9771b263
DN
11133 p0 = (*params)[0];
11134 p1 = (*params)[1];
11135 p2 = (*params)[2];
86951993
AM
11136
11137 /* Create pointer to appropriate size. */
11138 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11139 I_type_ptr = build_pointer_type (I_type);
11140
11141 /* Convert object pointer to required type. */
11142 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11143 (*params)[0] = p0;
86951993
AM
11144
11145 /* Convert expected pointer to required type. */
11146 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9771b263 11147 (*params)[1] = p1;
86951993
AM
11148
11149 /* Convert desired value to required type, and dereference it. */
11150 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11151 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9771b263 11152 (*params)[2] = p2;
86951993
AM
11153
11154 /* The rest of the parameters are fine. NULL means no special return value
11155 processing.*/
11156 *new_return = NULL;
11157 return false;
11158}
11159
11160
11161/* This will process an __atomic_load function call, determine whether it
11162 needs to be mapped to the _N variation, or turned into a library call.
11163 LOC is the location of the builtin call.
11164 FUNCTION is the DECL that has been invoked;
11165 PARAMS is the argument list for the call. The return value is non-null
11166 TRUE is returned if it is translated into the proper format for a call to the
11167 external library, and NEW_RETURN is set the tree for that function.
11168 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11169 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11170
11171static bool
11172resolve_overloaded_atomic_load (location_t loc, tree function,
9771b263 11173 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11174{
11175 tree p0, p1, p2;
11176 tree I_type, I_type_ptr;
11177 int n = get_atomic_generic_size (loc, function, params);
11178
c466c4ff
AM
11179 /* Size of 0 is an error condition. */
11180 if (n == 0)
11181 {
11182 *new_return = error_mark_node;
11183 return true;
11184 }
11185
86951993 11186 /* If not a lock-free size, change to the library generic format. */
6b28e197 11187 if (!atomic_size_supported_p (n))
86951993
AM
11188 {
11189 *new_return = add_atomic_size_parameter (n, loc, function, params);
11190 return true;
11191 }
11192
11193 /* Otherwise, there is a match, so the call needs to be transformed from:
11194 void fn(T* mem, T* return, model)
11195 into
11196 *return = (T) (fn ((In *) mem, model)) */
11197
9771b263
DN
11198 p0 = (*params)[0];
11199 p1 = (*params)[1];
11200 p2 = (*params)[2];
86951993
AM
11201
11202 /* Create pointer to appropriate size. */
11203 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11204 I_type_ptr = build_pointer_type (I_type);
11205
11206 /* Convert object pointer to required type. */
11207 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11208 (*params)[0] = p0;
86951993
AM
11209
11210 /* Move memory model to the 2nd position, and end param list. */
9771b263
DN
11211 (*params)[1] = p2;
11212 params->truncate (2);
86951993
AM
11213
11214 /* Convert return pointer and dereference it for later assignment. */
11215 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11216
11217 return false;
11218}
11219
11220
11221/* This will process an __atomic_store function call, determine whether it
11222 needs to be mapped to the _N variation, or turned into a library call.
11223 LOC is the location of the builtin call.
11224 FUNCTION is the DECL that has been invoked;
11225 PARAMS is the argument list for the call. The return value is non-null
11226 TRUE is returned if it is translated into the proper format for a call to the
11227 external library, and NEW_RETURN is set the tree for that function.
11228 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11229 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11230
11231static bool
11232resolve_overloaded_atomic_store (location_t loc, tree function,
9771b263 11233 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11234{
11235 tree p0, p1;
11236 tree I_type, I_type_ptr;
11237 int n = get_atomic_generic_size (loc, function, params);
11238
c466c4ff
AM
11239 /* Size of 0 is an error condition. */
11240 if (n == 0)
11241 {
11242 *new_return = error_mark_node;
11243 return true;
11244 }
11245
86951993 11246 /* If not a lock-free size, change to the library generic format. */
6b28e197 11247 if (!atomic_size_supported_p (n))
86951993
AM
11248 {
11249 *new_return = add_atomic_size_parameter (n, loc, function, params);
11250 return true;
11251 }
11252
11253 /* Otherwise, there is a match, so the call needs to be transformed from:
11254 void fn(T* mem, T* value, model)
11255 into
11256 fn ((In *) mem, (In) *value, model) */
11257
9771b263
DN
11258 p0 = (*params)[0];
11259 p1 = (*params)[1];
86951993
AM
11260
11261 /* Create pointer to appropriate size. */
11262 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11263 I_type_ptr = build_pointer_type (I_type);
11264
11265 /* Convert object pointer to required type. */
11266 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11267 (*params)[0] = p0;
86951993
AM
11268
11269 /* Convert new value to required type, and dereference it. */
11270 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11271 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11272 (*params)[1] = p1;
86951993
AM
11273
11274 /* The memory model is in the right spot already. Return is void. */
11275 *new_return = NULL_TREE;
11276
11277 return false;
11278}
11279
11280
48ae6c13
RH
11281/* Some builtin functions are placeholders for other expressions. This
11282 function should be called immediately after parsing the call expression
11283 before surrounding code has committed to the type of the expression.
11284
c2255bc4
AH
11285 LOC is the location of the builtin call.
11286
48ae6c13
RH
11287 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11288 PARAMS is the argument list for the call. The return value is non-null
11289 when expansion is complete, and null if normal processing should
11290 continue. */
11291
11292tree
9771b263
DN
11293resolve_overloaded_builtin (location_t loc, tree function,
11294 vec<tree, va_gc> *params)
48ae6c13
RH
11295{
11296 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7a127fa7
MS
11297
11298 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11299 Those are not valid to call with a pointer to _Bool (or C++ bool)
11300 and so must be rejected. */
11301 bool fetch_op = true;
86951993
AM
11302 bool orig_format = true;
11303 tree new_return = NULL_TREE;
11304
58646b77
PB
11305 switch (DECL_BUILT_IN_CLASS (function))
11306 {
11307 case BUILT_IN_NORMAL:
11308 break;
11309 case BUILT_IN_MD:
11310 if (targetm.resolve_overloaded_builtin)
c2255bc4 11311 return targetm.resolve_overloaded_builtin (loc, function, params);
58646b77 11312 else
c22cacf3 11313 return NULL_TREE;
58646b77
PB
11314 default:
11315 return NULL_TREE;
11316 }
c22cacf3 11317
58646b77 11318 /* Handle BUILT_IN_NORMAL here. */
48ae6c13
RH
11319 switch (orig_code)
11320 {
86951993
AM
11321 case BUILT_IN_ATOMIC_EXCHANGE:
11322 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11323 case BUILT_IN_ATOMIC_LOAD:
11324 case BUILT_IN_ATOMIC_STORE:
11325 {
11326 /* Handle these 4 together so that they can fall through to the next
11327 case if the call is transformed to an _N variant. */
11328 switch (orig_code)
11329 {
11330 case BUILT_IN_ATOMIC_EXCHANGE:
11331 {
11332 if (resolve_overloaded_atomic_exchange (loc, function, params,
11333 &new_return))
11334 return new_return;
11335 /* Change to the _N variant. */
11336 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11337 break;
11338 }
11339
11340 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11341 {
11342 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11343 params,
11344 &new_return))
11345 return new_return;
11346 /* Change to the _N variant. */
11347 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11348 break;
11349 }
11350 case BUILT_IN_ATOMIC_LOAD:
11351 {
11352 if (resolve_overloaded_atomic_load (loc, function, params,
11353 &new_return))
11354 return new_return;
11355 /* Change to the _N variant. */
11356 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11357 break;
11358 }
11359 case BUILT_IN_ATOMIC_STORE:
11360 {
11361 if (resolve_overloaded_atomic_store (loc, function, params,
11362 &new_return))
11363 return new_return;
11364 /* Change to the _N variant. */
11365 orig_code = BUILT_IN_ATOMIC_STORE_N;
11366 break;
11367 }
11368 default:
11369 gcc_unreachable ();
11370 }
11371 /* Fallthrough to the normal processing. */
11372 }
11373 case BUILT_IN_ATOMIC_EXCHANGE_N:
11374 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11375 case BUILT_IN_ATOMIC_LOAD_N:
11376 case BUILT_IN_ATOMIC_STORE_N:
7a127fa7
MS
11377 {
11378 fetch_op = false;
11379 /* Fallthrough to further processing. */
11380 }
86951993
AM
11381 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11382 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11383 case BUILT_IN_ATOMIC_AND_FETCH_N:
11384 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11385 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11386 case BUILT_IN_ATOMIC_OR_FETCH_N:
11387 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11388 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11389 case BUILT_IN_ATOMIC_FETCH_AND_N:
11390 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11391 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11392 case BUILT_IN_ATOMIC_FETCH_OR_N:
11393 {
11394 orig_format = false;
11395 /* Fallthru for parameter processing. */
11396 }
e0a8ecf2
AM
11397 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11398 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11399 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11400 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11401 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11402 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11403 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11404 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11405 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11406 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11407 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11408 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11409 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11410 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11411 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11412 case BUILT_IN_SYNC_LOCK_RELEASE_N:
48ae6c13 11413 {
7a127fa7
MS
11414 /* The following are not _FETCH_OPs and must be accepted with
11415 pointers to _Bool (or C++ bool). */
11416 if (fetch_op)
11417 fetch_op =
11418 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11419 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11420 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11421 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11422
11423 int n = sync_resolve_size (function, params, fetch_op);
bbbbb16a 11424 tree new_function, first_param, result;
e79983f4 11425 enum built_in_function fncode;
48ae6c13
RH
11426
11427 if (n == 0)
11428 return error_mark_node;
11429
e79983f4
MM
11430 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11431 new_function = builtin_decl_explicit (fncode);
86951993
AM
11432 if (!sync_resolve_params (loc, function, new_function, params,
11433 orig_format))
48ae6c13
RH
11434 return error_mark_node;
11435
9771b263 11436 first_param = (*params)[0];
81e5eca8
MP
11437 result = build_function_call_vec (loc, vNULL, new_function, params,
11438 NULL);
86951993
AM
11439 if (result == error_mark_node)
11440 return result;
e0a8ecf2 11441 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
86951993
AM
11442 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11443 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11444 result = sync_resolve_return (first_param, result, orig_format);
48ae6c13 11445
86951993
AM
11446 /* If new_return is set, assign function to that expr and cast the
11447 result to void since the generic interface returned void. */
11448 if (new_return)
11449 {
11450 /* Cast function result from I{1,2,4,8,16} to the required type. */
11451 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11452 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11453 result);
11454 TREE_SIDE_EFFECTS (result) = 1;
11455 protected_set_expr_location (result, loc);
11456 result = convert (void_type_node, result);
11457 }
48ae6c13
RH
11458 return result;
11459 }
11460
11461 default:
58646b77 11462 return NULL_TREE;
48ae6c13
RH
11463 }
11464}
11465
0af94e6f
JR
11466/* vector_types_compatible_elements_p is used in type checks of vectors
11467 values used as operands of binary operators. Where it returns true, and
11468 the other checks of the caller succeed (being vector types in he first
11469 place, and matching number of elements), we can just treat the types
11470 as essentially the same.
11471 Contrast with vector_targets_convertible_p, which is used for vector
11472 pointer types, and vector_types_convertible_p, which will allow
11473 language-specific matches under the control of flag_lax_vector_conversions,
11474 and might still require a conversion. */
11475/* True if vector types T1 and T2 can be inputs to the same binary
11476 operator without conversion.
11477 We don't check the overall vector size here because some of our callers
11478 want to give different error messages when the vectors are compatible
11479 except for the element count. */
11480
5bed876a 11481bool
0af94e6f 11482vector_types_compatible_elements_p (tree t1, tree t2)
5bed876a 11483{
0af94e6f
JR
11484 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11485 t1 = TREE_TYPE (t1);
11486 t2 = TREE_TYPE (t2);
11487
5bed876a
AH
11488 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11489
ab22c1fa
CF
11490 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11491 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11492 || c2 == FIXED_POINT_TYPE));
5bed876a 11493
0af94e6f
JR
11494 t1 = c_common_signed_type (t1);
11495 t2 = c_common_signed_type (t2);
5bed876a
AH
11496 /* Equality works here because c_common_signed_type uses
11497 TYPE_MAIN_VARIANT. */
0af94e6f
JR
11498 if (t1 == t2)
11499 return true;
11500 if (opaque && c1 == c2
11501 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11502 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11503 return true;
11504 return false;
5bed876a
AH
11505}
11506
104f8784
KG
11507/* Check for missing format attributes on function pointers. LTYPE is
11508 the new type or left-hand side type. RTYPE is the old type or
11509 right-hand side type. Returns TRUE if LTYPE is missing the desired
11510 attribute. */
11511
11512bool
11513check_missing_format_attribute (tree ltype, tree rtype)
11514{
11515 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11516 tree ra;
11517
11518 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11519 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11520 break;
11521 if (ra)
11522 {
11523 tree la;
11524 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11525 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11526 break;
11527 return !la;
11528 }
11529 else
11530 return false;
11531}
11532
ff6b6641
GDR
11533/* Subscripting with type char is likely to lose on a machine where
11534 chars are signed. So warn on any machine, but optionally. Don't
11535 warn for unsigned char since that type is safe. Don't warn for
11536 signed char because anyone who uses that must have done so
11537 deliberately. Furthermore, we reduce the false positive load by
11538 warning only for non-constant value of type char. */
11539
11540void
5bd012f8 11541warn_array_subscript_with_type_char (location_t loc, tree index)
ff6b6641
GDR
11542{
11543 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11544 && TREE_CODE (index) != INTEGER_CST)
5bd012f8
MP
11545 warning_at (loc, OPT_Wchar_subscripts,
11546 "array subscript has type %<char%>");
ff6b6641
GDR
11547}
11548
2a67bec2
ILT
11549/* Implement -Wparentheses for the unexpected C precedence rules, to
11550 cover cases like x + y << z which readers are likely to
11551 misinterpret. We have seen an expression in which CODE is a binary
100d537d
MLI
11552 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11553 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11554 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11555 expression was not formed using a binary or unary operator, or it
11556 was enclosed in parentheses. */
2a67bec2
ILT
11557
11558void
5d9de0d0 11559warn_about_parentheses (location_t loc, enum tree_code code,
fb3e178a 11560 enum tree_code code_left, tree arg_left,
100d537d 11561 enum tree_code code_right, tree arg_right)
2a67bec2
ILT
11562{
11563 if (!warn_parentheses)
11564 return;
11565
100d537d
MLI
11566 /* This macro tests that the expression ARG with original tree code
11567 CODE appears to be a boolean expression. or the result of folding a
11568 boolean expression. */
11569#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11570 (truth_value_p (TREE_CODE (ARG)) \
11571 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11572 /* Folding may create 0 or 1 integers from other expressions. */ \
11573 || ((CODE) != INTEGER_CST \
11574 && (integer_onep (ARG) || integer_zerop (ARG))))
11575
b8698a0f 11576 switch (code)
2a67bec2 11577 {
100d537d 11578 case LSHIFT_EXPR:
5d9de0d0
PC
11579 if (code_left == PLUS_EXPR)
11580 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11581 "suggest parentheses around %<+%> inside %<<<%>");
11582 else if (code_right == PLUS_EXPR)
11583 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11584 "suggest parentheses around %<+%> inside %<<<%>");
11585 else if (code_left == MINUS_EXPR)
11586 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11587 "suggest parentheses around %<-%> inside %<<<%>");
11588 else if (code_right == MINUS_EXPR)
11589 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11590 "suggest parentheses around %<-%> inside %<<<%>");
100d537d 11591 return;
2a67bec2 11592
100d537d 11593 case RSHIFT_EXPR:
5d9de0d0
PC
11594 if (code_left == PLUS_EXPR)
11595 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11596 "suggest parentheses around %<+%> inside %<>>%>");
11597 else if (code_right == PLUS_EXPR)
11598 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11599 "suggest parentheses around %<+%> inside %<>>%>");
11600 else if (code_left == MINUS_EXPR)
11601 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11602 "suggest parentheses around %<-%> inside %<>>%>");
11603 else if (code_right == MINUS_EXPR)
11604 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11605 "suggest parentheses around %<-%> inside %<>>%>");
100d537d 11606 return;
2a67bec2 11607
100d537d 11608 case TRUTH_ORIF_EXPR:
5d9de0d0
PC
11609 if (code_left == TRUTH_ANDIF_EXPR)
11610 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11611 "suggest parentheses around %<&&%> within %<||%>");
11612 else if (code_right == TRUTH_ANDIF_EXPR)
11613 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11614 "suggest parentheses around %<&&%> within %<||%>");
100d537d
MLI
11615 return;
11616
11617 case BIT_IOR_EXPR:
2a67bec2 11618 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
5d9de0d0
PC
11619 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11620 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11621 "suggest parentheses around arithmetic in operand of %<|%>");
11622 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11623 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11624 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11625 "suggest parentheses around arithmetic in operand of %<|%>");
2a67bec2 11626 /* Check cases like x|y==z */
5d9de0d0
PC
11627 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11628 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11629 "suggest parentheses around comparison in operand of %<|%>");
11630 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11631 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11632 "suggest parentheses around comparison in operand of %<|%>");
11633 /* Check cases like !x | y */
11634 else if (code_left == TRUTH_NOT_EXPR
11635 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11636 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11637 "suggest parentheses around operand of "
11638 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
100d537d 11639 return;
2a67bec2 11640
100d537d 11641 case BIT_XOR_EXPR:
2a67bec2 11642 if (code_left == BIT_AND_EXPR
5d9de0d0
PC
11643 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11644 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11645 "suggest parentheses around arithmetic in operand of %<^%>");
11646 else if (code_right == BIT_AND_EXPR
11647 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11648 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11649 "suggest parentheses around arithmetic in operand of %<^%>");
2a67bec2 11650 /* Check cases like x^y==z */
5d9de0d0
PC
11651 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11652 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11653 "suggest parentheses around comparison in operand of %<^%>");
11654 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11655 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11656 "suggest parentheses around comparison in operand of %<^%>");
11657 return;
2a67bec2 11658
100d537d 11659 case BIT_AND_EXPR:
5d9de0d0
PC
11660 if (code_left == PLUS_EXPR)
11661 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11662 "suggest parentheses around %<+%> in operand of %<&%>");
11663 else if (code_right == PLUS_EXPR)
11664 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11665 "suggest parentheses around %<+%> in operand of %<&%>");
5d9de0d0
PC
11666 else if (code_left == MINUS_EXPR)
11667 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11668 "suggest parentheses around %<-%> in operand of %<&%>");
11669 else if (code_right == MINUS_EXPR)
11670 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11671 "suggest parentheses around %<-%> in operand of %<&%>");
2a67bec2 11672 /* Check cases like x&y==z */
5d9de0d0
PC
11673 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11674 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11675 "suggest parentheses around comparison in operand of %<&%>");
11676 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11677 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11678 "suggest parentheses around comparison in operand of %<&%>");
11679 /* Check cases like !x & y */
11680 else if (code_left == TRUTH_NOT_EXPR
11681 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11682 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11683 "suggest parentheses around operand of "
11684 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
100d537d 11685 return;
2a67bec2 11686
100d537d 11687 case EQ_EXPR:
5d9de0d0
PC
11688 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11689 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11690 "suggest parentheses around comparison in operand of %<==%>");
11691 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11692 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11693 "suggest parentheses around comparison in operand of %<==%>");
11694 return;
11695 case NE_EXPR:
5d9de0d0
PC
11696 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11697 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11698 "suggest parentheses around comparison in operand of %<!=%>");
11699 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11700 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11701 "suggest parentheses around comparison in operand of %<!=%>");
11702 return;
11703
11704 default:
5d9de0d0
PC
11705 if (TREE_CODE_CLASS (code) == tcc_comparison)
11706 {
11707 if (TREE_CODE_CLASS (code_left) == tcc_comparison
fb3e178a
JJ
11708 && code_left != NE_EXPR && code_left != EQ_EXPR
11709 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
5d9de0d0
PC
11710 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11711 "comparisons like %<X<=Y<=Z%> do not "
11712 "have their mathematical meaning");
11713 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
fb3e178a 11714 && code_right != NE_EXPR && code_right != EQ_EXPR
5d9de0d0
PC
11715 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11716 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11717 "comparisons like %<X<=Y<=Z%> do not "
11718 "have their mathematical meaning");
11719 }
100d537d 11720 return;
e7917d06 11721 }
100d537d 11722#undef NOT_A_BOOLEAN_EXPR_P
2a67bec2
ILT
11723}
11724
c616e51b
MLI
11725/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11726
11727void
11728warn_for_unused_label (tree label)
11729{
11730 if (!TREE_USED (label))
11731 {
11732 if (DECL_INITIAL (label))
11733 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11734 else
11735 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11736 }
11737}
ff6b6641 11738
c9f9eb5d
AH
11739/* Warn for division by zero according to the value of DIVISOR. LOC
11740 is the location of the division operator. */
2e9cb75e
MLI
11741
11742void
c9f9eb5d 11743warn_for_div_by_zero (location_t loc, tree divisor)
2e9cb75e 11744{
ab22c1fa
CF
11745 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11746 about division by zero. Do not issue a warning if DIVISOR has a
2e9cb75e
MLI
11747 floating-point type, since we consider 0.0/0.0 a valid way of
11748 generating a NaN. */
7d882b83 11749 if (c_inhibit_evaluation_warnings == 0
ab22c1fa 11750 && (integer_zerop (divisor) || fixed_zerop (divisor)))
c9f9eb5d 11751 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
2e9cb75e
MLI
11752}
11753
2d12797c
MLI
11754/* Subroutine of build_binary_op. Give warnings for comparisons
11755 between signed and unsigned quantities that may fail. Do the
11756 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11757 so that casts will be considered, but default promotions won't
ba47d38d
AH
11758 be.
11759
11760 LOCATION is the location of the comparison operator.
2d12797c
MLI
11761
11762 The arguments of this function map directly to local variables
11763 of build_binary_op. */
11764
b8698a0f 11765void
ba47d38d 11766warn_for_sign_compare (location_t location,
b8698a0f
L
11767 tree orig_op0, tree orig_op1,
11768 tree op0, tree op1,
2d12797c
MLI
11769 tree result_type, enum tree_code resultcode)
11770{
11771 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11772 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11773 int unsignedp0, unsignedp1;
b8698a0f 11774
2d12797c
MLI
11775 /* In C++, check for comparison of different enum types. */
11776 if (c_dialect_cxx()
11777 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11778 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11779 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
ead51d36 11780 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
2d12797c 11781 {
ba47d38d
AH
11782 warning_at (location,
11783 OPT_Wsign_compare, "comparison between types %qT and %qT",
11784 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
2d12797c
MLI
11785 }
11786
11787 /* Do not warn if the comparison is being done in a signed type,
11788 since the signed type will only be chosen if it can represent
11789 all the values of the unsigned type. */
11790 if (!TYPE_UNSIGNED (result_type))
11791 /* OK */;
11792 /* Do not warn if both operands are unsigned. */
11793 else if (op0_signed == op1_signed)
11794 /* OK */;
11795 else
11796 {
ead51d36 11797 tree sop, uop, base_type;
2d12797c 11798 bool ovf;
ead51d36 11799
2d12797c
MLI
11800 if (op0_signed)
11801 sop = orig_op0, uop = orig_op1;
b8698a0f 11802 else
2d12797c
MLI
11803 sop = orig_op1, uop = orig_op0;
11804
b8698a0f 11805 STRIP_TYPE_NOPS (sop);
2d12797c 11806 STRIP_TYPE_NOPS (uop);
ead51d36
JJ
11807 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11808 ? TREE_TYPE (result_type) : result_type);
2d12797c
MLI
11809
11810 /* Do not warn if the signed quantity is an unsuffixed integer
11811 literal (or some static constant expression involving such
11812 literals or a conditional expression involving such literals)
11813 and it is non-negative. */
11814 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11815 /* OK */;
11816 /* Do not warn if the comparison is an equality operation, the
11817 unsigned quantity is an integral constant, and it would fit
11818 in the result if the result were signed. */
11819 else if (TREE_CODE (uop) == INTEGER_CST
11820 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
ead51d36 11821 && int_fits_type_p (uop, c_common_signed_type (base_type)))
2d12797c
MLI
11822 /* OK */;
11823 /* In C, do not warn if the unsigned quantity is an enumeration
11824 constant and its maximum value would fit in the result if the
11825 result were signed. */
11826 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11827 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11828 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
ead51d36 11829 c_common_signed_type (base_type)))
2d12797c 11830 /* OK */;
b8698a0f 11831 else
ba47d38d 11832 warning_at (location,
b8698a0f 11833 OPT_Wsign_compare,
ba47d38d 11834 "comparison between signed and unsigned integer expressions");
2d12797c 11835 }
b8698a0f 11836
2d12797c
MLI
11837 /* Warn if two unsigned values are being compared in a size larger
11838 than their original size, and one (and only one) is the result of
11839 a `~' operator. This comparison will always fail.
b8698a0f 11840
2d12797c
MLI
11841 Also warn if one operand is a constant, and the constant does not
11842 have all bits set that are set in the ~ operand when it is
11843 extended. */
11844
828fb3ba
JM
11845 op0 = c_common_get_narrower (op0, &unsignedp0);
11846 op1 = c_common_get_narrower (op1, &unsignedp1);
b8698a0f 11847
2d12797c
MLI
11848 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11849 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11850 {
11851 if (TREE_CODE (op0) == BIT_NOT_EXPR)
828fb3ba 11852 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2d12797c 11853 if (TREE_CODE (op1) == BIT_NOT_EXPR)
828fb3ba 11854 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2d12797c 11855
9541ffee 11856 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2d12797c
MLI
11857 {
11858 tree primop;
11859 HOST_WIDE_INT constant, mask;
11860 int unsignedp;
11861 unsigned int bits;
b8698a0f 11862
9541ffee 11863 if (tree_fits_shwi_p (op0))
2d12797c
MLI
11864 {
11865 primop = op1;
11866 unsignedp = unsignedp1;
9439e9a1 11867 constant = tree_to_shwi (op0);
2d12797c
MLI
11868 }
11869 else
11870 {
11871 primop = op0;
11872 unsignedp = unsignedp0;
9439e9a1 11873 constant = tree_to_shwi (op1);
2d12797c 11874 }
b8698a0f 11875
2d12797c
MLI
11876 bits = TYPE_PRECISION (TREE_TYPE (primop));
11877 if (bits < TYPE_PRECISION (result_type)
11878 && bits < HOST_BITS_PER_LONG && unsignedp)
11879 {
aa256c4a 11880 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
2d12797c
MLI
11881 if ((mask & constant) != mask)
11882 {
11883 if (constant == 0)
0a756a3f
MP
11884 warning_at (location, OPT_Wsign_compare,
11885 "promoted ~unsigned is always non-zero");
2d12797c 11886 else
b8698a0f 11887 warning_at (location, OPT_Wsign_compare,
ba47d38d 11888 "comparison of promoted ~unsigned with constant");
2d12797c
MLI
11889 }
11890 }
11891 }
11892 else if (unsignedp0 && unsignedp1
11893 && (TYPE_PRECISION (TREE_TYPE (op0))
11894 < TYPE_PRECISION (result_type))
11895 && (TYPE_PRECISION (TREE_TYPE (op1))
11896 < TYPE_PRECISION (result_type)))
ba47d38d 11897 warning_at (location, OPT_Wsign_compare,
2d12797c
MLI
11898 "comparison of promoted ~unsigned with unsigned");
11899 }
11900}
11901
c5ee1358
MM
11902/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11903 type via c_common_type. If -Wdouble-promotion is in use, and the
11904 conditions for warning have been met, issue a warning. GMSGID is
11905 the warning message. It must have two %T specifiers for the type
11906 that was converted (generally "float") and the type to which it was
11907 converted (generally "double), respectively. LOC is the location
11908 to which the awrning should refer. */
11909
11910void
11911do_warn_double_promotion (tree result_type, tree type1, tree type2,
11912 const char *gmsgid, location_t loc)
11913{
11914 tree source_type;
11915
11916 if (!warn_double_promotion)
11917 return;
11918 /* If the conversion will not occur at run-time, there is no need to
11919 warn about it. */
11920 if (c_inhibit_evaluation_warnings)
11921 return;
11922 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11923 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11924 return;
11925 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11926 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11927 source_type = type1;
11928 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11929 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11930 source_type = type2;
11931 else
11932 return;
11933 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11934}
11935
da2e71c9
MLI
11936/* Possibly warn about unused parameters. */
11937
11938void
11939do_warn_unused_parameter (tree fn)
11940{
11941 tree decl;
11942
11943 for (decl = DECL_ARGUMENTS (fn);
11944 decl; decl = DECL_CHAIN (decl))
11945 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
11946 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
11947 && !TREE_NO_WARNING (decl))
11948 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
11949 "unused parameter %qD", decl);
11950}
11951
11952
d0940d56
DS
11953/* Setup a TYPE_DECL node as a typedef representation.
11954
11955 X is a TYPE_DECL for a typedef statement. Create a brand new
11956 ..._TYPE node (which will be just a variant of the existing
11957 ..._TYPE node with identical properties) and then install X
11958 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11959
11960 The whole point here is to end up with a situation where each
11961 and every ..._TYPE node the compiler creates will be uniquely
11962 associated with AT MOST one node representing a typedef name.
11963 This way, even though the compiler substitutes corresponding
11964 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11965 early on, later parts of the compiler can always do the reverse
11966 translation and get back the corresponding typedef name. For
11967 example, given:
11968
11969 typedef struct S MY_TYPE;
11970 MY_TYPE object;
11971
11972 Later parts of the compiler might only know that `object' was of
11973 type `struct S' if it were not for code just below. With this
11974 code however, later parts of the compiler see something like:
11975
11976 struct S' == struct S
11977 typedef struct S' MY_TYPE;
11978 struct S' object;
11979
11980 And they can then deduce (from the node for type struct S') that
11981 the original object declaration was:
11982
11983 MY_TYPE object;
11984
11985 Being able to do this is important for proper support of protoize,
11986 and also for generating precise symbolic debugging information
11987 which takes full account of the programmer's (typedef) vocabulary.
11988
11989 Obviously, we don't want to generate a duplicate ..._TYPE node if
11990 the TYPE_DECL node that we are now processing really represents a
11991 standard built-in type. */
11992
11993void
11994set_underlying_type (tree x)
11995{
11996 if (x == error_mark_node)
11997 return;
11998 if (DECL_IS_BUILTIN (x))
11999 {
12000 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12001 TYPE_NAME (TREE_TYPE (x)) = x;
12002 }
12003 else if (TREE_TYPE (x) != error_mark_node
12004 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12005 {
12006 tree tt = TREE_TYPE (x);
12007 DECL_ORIGINAL_TYPE (x) = tt;
12008 tt = build_variant_type_copy (tt);
12009 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12010 TYPE_NAME (tt) = x;
12011 TREE_USED (tt) = TREE_USED (x);
12012 TREE_TYPE (x) = tt;
12013 }
12014}
12015
b646ba3f
DS
12016/* Record the types used by the current global variable declaration
12017 being parsed, so that we can decide later to emit their debug info.
12018 Those types are in types_used_by_cur_var_decl, and we are going to
12019 store them in the types_used_by_vars_hash hash table.
12020 DECL is the declaration of the global variable that has been parsed. */
12021
12022void
12023record_types_used_by_current_var_decl (tree decl)
12024{
12025 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12026
9771b263 12027 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
b646ba3f 12028 {
9771b263 12029 tree type = types_used_by_cur_var_decl->pop ();
bc87224e 12030 types_used_by_var_decl_insert (type, decl);
b646ba3f
DS
12031 }
12032}
12033
3797cb21
DS
12034/* If DECL is a typedef that is declared in the current function,
12035 record it for the purpose of -Wunused-local-typedefs. */
12036
12037void
12038record_locally_defined_typedef (tree decl)
12039{
12040 struct c_language_function *l;
12041
12042 if (!warn_unused_local_typedefs
12043 || cfun == NULL
12044 /* if this is not a locally defined typedef then we are not
12045 interested. */
12046 || !is_typedef_decl (decl)
12047 || !decl_function_context (decl))
12048 return;
12049
12050 l = (struct c_language_function *) cfun->language;
9771b263 12051 vec_safe_push (l->local_typedefs, decl);
3797cb21
DS
12052}
12053
12054/* If T is a TYPE_DECL declared locally, mark it as used. */
12055
12056void
12057maybe_record_typedef_use (tree t)
12058{
12059 if (!is_typedef_decl (t))
12060 return;
12061
12062 TREE_USED (t) = true;
12063}
12064
12065/* Warn if there are some unused locally defined typedefs in the
12066 current function. */
12067
12068void
12069maybe_warn_unused_local_typedefs (void)
12070{
12071 int i;
12072 tree decl;
12073 /* The number of times we have emitted -Wunused-local-typedefs
12074 warnings. If this is different from errorcount, that means some
12075 unrelated errors have been issued. In which case, we'll avoid
12076 emitting "unused-local-typedefs" warnings. */
12077 static int unused_local_typedefs_warn_count;
12078 struct c_language_function *l;
12079
12080 if (cfun == NULL)
12081 return;
12082
12083 if ((l = (struct c_language_function *) cfun->language) == NULL)
12084 return;
12085
12086 if (warn_unused_local_typedefs
12087 && errorcount == unused_local_typedefs_warn_count)
12088 {
9771b263 12089 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
3797cb21
DS
12090 if (!TREE_USED (decl))
12091 warning_at (DECL_SOURCE_LOCATION (decl),
12092 OPT_Wunused_local_typedefs,
12093 "typedef %qD locally defined but not used", decl);
12094 unused_local_typedefs_warn_count = errorcount;
12095 }
12096
9771b263 12097 vec_free (l->local_typedefs);
3797cb21
DS
12098}
12099
04159acf
MP
12100/* Warn about boolean expression compared with an integer value different
12101 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12102 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12103 are the operands of the comparison. The caller must ensure that
12104 either operand is a boolean expression. */
12105
12106void
12107maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12108 tree op1)
12109{
12110 if (TREE_CODE_CLASS (code) != tcc_comparison)
12111 return;
12112
f479b43d
JM
12113 tree f, cst;
12114 if (f = fold_for_warn (op0),
12115 TREE_CODE (f) == INTEGER_CST)
12116 cst = op0 = f;
12117 else if (f = fold_for_warn (op1),
12118 TREE_CODE (f) == INTEGER_CST)
12119 cst = op1 = f;
12120 else
04159acf
MP
12121 return;
12122
12123 if (!integer_zerop (cst) && !integer_onep (cst))
12124 {
716c0ba6
MP
12125 int sign = (TREE_CODE (op0) == INTEGER_CST
12126 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
04159acf
MP
12127 if (code == EQ_EXPR
12128 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12129 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12130 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12131 "with boolean expression is always false", cst);
12132 else
12133 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12134 "with boolean expression is always true", cst);
12135 }
716c0ba6
MP
12136 else if (integer_zerop (cst) || integer_onep (cst))
12137 {
577cd070
MP
12138 /* If the non-constant operand isn't of a boolean type, we
12139 don't want to warn here. */
12140 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12141 /* Handle booleans promoted to integers. */
12142 if (CONVERT_EXPR_P (noncst)
12143 && TREE_TYPE (noncst) == integer_type_node
12144 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12145 /* Warn. */;
12146 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12147 && !truth_value_p (TREE_CODE (noncst)))
12148 return;
716c0ba6
MP
12149 /* Do some magic to get the right diagnostics. */
12150 bool flag = TREE_CODE (op0) == INTEGER_CST;
12151 flag = integer_zerop (cst) ? flag : !flag;
12152 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12153 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12154 "with boolean expression is always true", cst);
12155 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12156 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12157 "with boolean expression is always false", cst);
12158 }
04159acf
MP
12159}
12160
451b5e48
MP
12161/* Warn if signed left shift overflows. We don't warn
12162 about left-shifting 1 into the sign bit in C++14; cf.
12163 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12164 LOC is a location of the shift; OP0 and OP1 are the operands.
12165 Return true if an overflow is detected, false otherwise. */
12166
12167bool
12168maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12169{
12170 if (TREE_CODE (op0) != INTEGER_CST
12171 || TREE_CODE (op1) != INTEGER_CST)
12172 return false;
12173
12174 tree type0 = TREE_TYPE (op0);
12175 unsigned int prec0 = TYPE_PRECISION (type0);
12176
12177 /* Left-hand operand must be signed. */
12178 if (TYPE_UNSIGNED (type0))
12179 return false;
12180
b893e375
MP
12181 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12182 + TREE_INT_CST_LOW (op1));
c5404f1c
PB
12183 /* Handle the case of left-shifting 1 into the sign bit.
12184 * However, shifting 1 _out_ of the sign bit, as in
12185 * INT_MIN << 1, is considered an overflow.
12186 */
12187 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
451b5e48
MP
12188 {
12189 /* Never warn for C++14 onwards. */
12190 if (cxx_dialect >= cxx14)
12191 return false;
12192 /* Otherwise only if -Wshift-overflow=2. But return
12193 true to signal an overflow for the sake of integer
12194 constant expressions. */
12195 if (warn_shift_overflow < 2)
12196 return true;
12197 }
12198
451b5e48
MP
12199 bool overflowed = min_prec > prec0;
12200 if (overflowed && c_inhibit_evaluation_warnings == 0)
12201 warning_at (loc, OPT_Wshift_overflow_,
12202 "result of %qE requires %u bits to represent, "
12203 "but %qT only has %u bits",
12204 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12205 min_prec, type0, prec0);
12206
12207 return overflowed;
12208}
12209
c166b898
ILT
12210/* The C and C++ parsers both use vectors to hold function arguments.
12211 For efficiency, we keep a cache of unused vectors. This is the
12212 cache. */
12213
9771b263
DN
12214typedef vec<tree, va_gc> *tree_gc_vec;
12215static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
c166b898
ILT
12216
12217/* Return a new vector from the cache. If the cache is empty,
12218 allocate a new vector. These vectors are GC'ed, so it is OK if the
12219 pointer is not released.. */
12220
9771b263 12221vec<tree, va_gc> *
c166b898
ILT
12222make_tree_vector (void)
12223{
9771b263
DN
12224 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12225 return tree_vector_cache->pop ();
c166b898
ILT
12226 else
12227 {
9771b263 12228 /* Passing 0 to vec::alloc returns NULL, and our callers require
c166b898
ILT
12229 that we always return a non-NULL value. The vector code uses
12230 4 when growing a NULL vector, so we do too. */
9771b263
DN
12231 vec<tree, va_gc> *v;
12232 vec_alloc (v, 4);
12233 return v;
c166b898
ILT
12234 }
12235}
12236
12237/* Release a vector of trees back to the cache. */
12238
12239void
9771b263 12240release_tree_vector (vec<tree, va_gc> *vec)
c166b898
ILT
12241{
12242 if (vec != NULL)
12243 {
9771b263
DN
12244 vec->truncate (0);
12245 vec_safe_push (tree_vector_cache, vec);
c166b898
ILT
12246 }
12247}
12248
12249/* Get a new tree vector holding a single tree. */
12250
9771b263 12251vec<tree, va_gc> *
c166b898
ILT
12252make_tree_vector_single (tree t)
12253{
9771b263
DN
12254 vec<tree, va_gc> *ret = make_tree_vector ();
12255 ret->quick_push (t);
c166b898
ILT
12256 return ret;
12257}
12258
c12ff9d8
JM
12259/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12260
9771b263 12261vec<tree, va_gc> *
c12ff9d8
JM
12262make_tree_vector_from_list (tree list)
12263{
9771b263 12264 vec<tree, va_gc> *ret = make_tree_vector ();
c12ff9d8 12265 for (; list; list = TREE_CHAIN (list))
9771b263 12266 vec_safe_push (ret, TREE_VALUE (list));
c12ff9d8
JM
12267 return ret;
12268}
12269
c166b898
ILT
12270/* Get a new tree vector which is a copy of an existing one. */
12271
9771b263
DN
12272vec<tree, va_gc> *
12273make_tree_vector_copy (const vec<tree, va_gc> *orig)
c166b898 12274{
9771b263 12275 vec<tree, va_gc> *ret;
c166b898
ILT
12276 unsigned int ix;
12277 tree t;
12278
12279 ret = make_tree_vector ();
9771b263
DN
12280 vec_safe_reserve (ret, vec_safe_length (orig));
12281 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12282 ret->quick_push (t);
c166b898
ILT
12283 return ret;
12284}
12285
a9aa2c3a
NF
12286/* Return true if KEYWORD starts a type specifier. */
12287
12288bool
12289keyword_begins_type_specifier (enum rid keyword)
12290{
12291 switch (keyword)
12292 {
38b7bc7f 12293 case RID_AUTO_TYPE:
a9aa2c3a
NF
12294 case RID_INT:
12295 case RID_CHAR:
12296 case RID_FLOAT:
12297 case RID_DOUBLE:
12298 case RID_VOID:
a9aa2c3a
NF
12299 case RID_UNSIGNED:
12300 case RID_LONG:
12301 case RID_SHORT:
12302 case RID_SIGNED:
12303 case RID_DFLOAT32:
12304 case RID_DFLOAT64:
12305 case RID_DFLOAT128:
12306 case RID_FRACT:
12307 case RID_ACCUM:
12308 case RID_BOOL:
12309 case RID_WCHAR:
12310 case RID_CHAR16:
12311 case RID_CHAR32:
12312 case RID_SAT:
12313 case RID_COMPLEX:
12314 case RID_TYPEOF:
12315 case RID_STRUCT:
12316 case RID_CLASS:
12317 case RID_UNION:
12318 case RID_ENUM:
12319 return true;
12320 default:
78a7c317
DD
12321 if (keyword >= RID_FIRST_INT_N
12322 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12323 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12324 return true;
a9aa2c3a
NF
12325 return false;
12326 }
12327}
12328
12329/* Return true if KEYWORD names a type qualifier. */
12330
12331bool
12332keyword_is_type_qualifier (enum rid keyword)
12333{
12334 switch (keyword)
12335 {
12336 case RID_CONST:
12337 case RID_VOLATILE:
12338 case RID_RESTRICT:
267bac10 12339 case RID_ATOMIC:
a9aa2c3a
NF
12340 return true;
12341 default:
12342 return false;
12343 }
12344}
12345
12346/* Return true if KEYWORD names a storage class specifier.
12347
12348 RID_TYPEDEF is not included in this list despite `typedef' being
12349 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12350 such for syntactic convenience only. */
12351
12352bool
12353keyword_is_storage_class_specifier (enum rid keyword)
12354{
12355 switch (keyword)
12356 {
12357 case RID_STATIC:
12358 case RID_EXTERN:
12359 case RID_REGISTER:
12360 case RID_AUTO:
12361 case RID_MUTABLE:
12362 case RID_THREAD:
12363 return true;
12364 default:
12365 return false;
12366 }
12367}
12368
ba9e6dd5
NF
12369/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12370
12371static bool
12372keyword_is_function_specifier (enum rid keyword)
12373{
12374 switch (keyword)
12375 {
12376 case RID_INLINE:
bbceee64 12377 case RID_NORETURN:
ba9e6dd5
NF
12378 case RID_VIRTUAL:
12379 case RID_EXPLICIT:
12380 return true;
12381 default:
12382 return false;
12383 }
12384}
12385
12386/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12387 declaration-specifier (C99 6.7). */
12388
12389bool
12390keyword_is_decl_specifier (enum rid keyword)
12391{
12392 if (keyword_is_storage_class_specifier (keyword)
12393 || keyword_is_type_qualifier (keyword)
12394 || keyword_is_function_specifier (keyword))
12395 return true;
12396
12397 switch (keyword)
12398 {
12399 case RID_TYPEDEF:
12400 case RID_FRIEND:
12401 case RID_CONSTEXPR:
12402 return true;
12403 default:
12404 return false;
12405 }
12406}
12407
81f653d6
NF
12408/* Initialize language-specific-bits of tree_contains_struct. */
12409
12410void
12411c_common_init_ts (void)
12412{
12413 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12414 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
36536d79 12415 MARK_TS_TYPED (ARRAY_NOTATION_REF);
81f653d6
NF
12416}
12417
3ce4f9e4
ESR
12418/* Build a user-defined numeric literal out of an integer constant type VALUE
12419 with identifier SUFFIX. */
12420
12421tree
2d7aa578
ESR
12422build_userdef_literal (tree suffix_id, tree value,
12423 enum overflow_type overflow, tree num_string)
3ce4f9e4
ESR
12424{
12425 tree literal = make_node (USERDEF_LITERAL);
12426 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12427 USERDEF_LITERAL_VALUE (literal) = value;
2d7aa578 12428 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
3ce4f9e4
ESR
12429 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12430 return literal;
12431}
12432
7edaa4d2 12433/* For vector[index], convert the vector to a
aa7da51a
JJ
12434 pointer of the underlying type. Return true if the resulting
12435 ARRAY_REF should not be an lvalue. */
12436
12437bool
7edaa4d2 12438convert_vector_to_pointer_for_subscript (location_t loc,
aa7da51a 12439 tree *vecp, tree index)
7edaa4d2 12440{
aa7da51a 12441 bool ret = false;
31521951 12442 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7edaa4d2
MG
12443 {
12444 tree type = TREE_TYPE (*vecp);
12445 tree type1;
12446
aa7da51a 12447 ret = !lvalue_p (*vecp);
7edaa4d2 12448 if (TREE_CODE (index) == INTEGER_CST)
cc269bb6 12449 if (!tree_fits_uhwi_p (index)
7d362f6c 12450 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7edaa4d2
MG
12451 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12452
aa7da51a
JJ
12453 if (ret)
12454 {
b731b390 12455 tree tmp = create_tmp_var_raw (type);
aa7da51a
JJ
12456 DECL_SOURCE_LOCATION (tmp) = loc;
12457 *vecp = c_save_expr (*vecp);
12458 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12459 {
12460 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12461 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12462 *vecp
12463 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12464 *vecp, NULL_TREE, NULL_TREE),
12465 non_const);
12466 }
12467 else
12468 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12469 NULL_TREE, NULL_TREE);
12470 SET_EXPR_LOCATION (*vecp, loc);
12471 c_common_mark_addressable_vec (tmp);
12472 }
12473 else
12474 c_common_mark_addressable_vec (*vecp);
7edaa4d2 12475 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7edaa4d2 12476 type1 = build_pointer_type (TREE_TYPE (*vecp));
b0f1bf36
RB
12477 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12478 if (!ref_all
12479 && !DECL_P (*vecp))
12480 {
12481 /* If the original vector isn't declared may_alias and it
12482 isn't a bare vector look if the subscripting would
12483 alias the vector we subscript, and if not, force ref-all. */
12484 alias_set_type vecset = get_alias_set (*vecp);
12485 alias_set_type sset = get_alias_set (type);
12486 if (!alias_sets_must_conflict_p (sset, vecset)
12487 && !alias_set_subset_of (sset, vecset))
12488 ref_all = true;
12489 }
12490 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7edaa4d2
MG
12491 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12492 *vecp = convert (type, *vecp);
12493 }
aa7da51a 12494 return ret;
7edaa4d2
MG
12495}
12496
a212e43f
MG
12497/* Determine which of the operands, if any, is a scalar that needs to be
12498 converted to a vector, for the range of operations. */
12499enum stv_conv
12500scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12501 bool complain)
12502{
12503 tree type0 = TREE_TYPE (op0);
12504 tree type1 = TREE_TYPE (op1);
12505 bool integer_only_op = false;
12506 enum stv_conv ret = stv_firstarg;
12507
31521951 12508 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
a212e43f
MG
12509 switch (code)
12510 {
12511 /* Most GENERIC binary expressions require homogeneous arguments.
12512 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12513 argument that is a vector and a second one that is a scalar, so
12514 we never return stv_secondarg for them. */
12515 case RSHIFT_EXPR:
12516 case LSHIFT_EXPR:
12517 if (TREE_CODE (type0) == INTEGER_TYPE
12518 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12519 {
68fca595 12520 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12521 {
12522 if (complain)
12523 error_at (loc, "conversion of scalar %qT to vector %qT "
12524 "involves truncation", type0, type1);
12525 return stv_error;
12526 }
12527 else
12528 return stv_firstarg;
12529 }
12530 break;
12531
12532 case BIT_IOR_EXPR:
12533 case BIT_XOR_EXPR:
12534 case BIT_AND_EXPR:
12535 integer_only_op = true;
12536 /* ... fall through ... */
12537
93100c6b
MG
12538 case VEC_COND_EXPR:
12539
a212e43f
MG
12540 case PLUS_EXPR:
12541 case MINUS_EXPR:
12542 case MULT_EXPR:
12543 case TRUNC_DIV_EXPR:
12544 case CEIL_DIV_EXPR:
12545 case FLOOR_DIV_EXPR:
12546 case ROUND_DIV_EXPR:
12547 case EXACT_DIV_EXPR:
12548 case TRUNC_MOD_EXPR:
12549 case FLOOR_MOD_EXPR:
12550 case RDIV_EXPR:
12551 case EQ_EXPR:
12552 case NE_EXPR:
12553 case LE_EXPR:
12554 case GE_EXPR:
12555 case LT_EXPR:
12556 case GT_EXPR:
12557 /* What about UNLT_EXPR? */
31521951 12558 if (VECTOR_TYPE_P (type0))
a212e43f 12559 {
a212e43f 12560 ret = stv_secondarg;
6b4db501
MM
12561 std::swap (type0, type1);
12562 std::swap (op0, op1);
a212e43f
MG
12563 }
12564
12565 if (TREE_CODE (type0) == INTEGER_TYPE
12566 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12567 {
68fca595 12568 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12569 {
12570 if (complain)
12571 error_at (loc, "conversion of scalar %qT to vector %qT "
12572 "involves truncation", type0, type1);
12573 return stv_error;
12574 }
12575 return ret;
12576 }
12577 else if (!integer_only_op
12578 /* Allow integer --> real conversion if safe. */
12579 && (TREE_CODE (type0) == REAL_TYPE
12580 || TREE_CODE (type0) == INTEGER_TYPE)
12581 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12582 {
68fca595 12583 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12584 {
12585 if (complain)
12586 error_at (loc, "conversion of scalar %qT to vector %qT "
12587 "involves truncation", type0, type1);
12588 return stv_error;
12589 }
12590 return ret;
12591 }
12592 default:
12593 break;
12594 }
12595
12596 return stv_nothing;
12597}
12598
e28d52cf
DS
12599/* Return true iff ALIGN is an integral constant that is a fundamental
12600 alignment, as defined by [basic.align] in the c++-11
12601 specifications.
12602
12603 That is:
12604
12605 [A fundamental alignment is represented by an alignment less than or
12606 equal to the greatest alignment supported by the implementation
12607 in all contexts, which is equal to
12608 alignof(max_align_t)]. */
12609
12610bool
12611cxx_fundamental_alignment_p (unsigned align)
12612{
12613 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12614 TYPE_ALIGN (long_double_type_node)));
12615}
12616
f04dda30
MP
12617/* Return true if T is a pointer to a zero-sized aggregate. */
12618
12619bool
12620pointer_to_zero_sized_aggr_p (tree t)
12621{
12622 if (!POINTER_TYPE_P (t))
12623 return false;
12624 t = TREE_TYPE (t);
12625 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12626}
12627
1807ffc1
MS
12628/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12629 with no library fallback or for an ADDR_EXPR whose operand is such type
12630 issues an error pointing to the location LOC.
12631 Returns true when the expression has been diagnosed and false
12632 otherwise. */
12633bool
12634reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12635{
12636 if (TREE_CODE (expr) == ADDR_EXPR)
12637 expr = TREE_OPERAND (expr, 0);
12638
12639 if (TREE_TYPE (expr)
12640 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
1be56bc5 12641 && TREE_CODE (expr) == FUNCTION_DECL
1807ffc1
MS
12642 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12643 false positives for user-declared built-ins such as abs or
12644 strlen, and for C++ operators new and delete.
12645 The c_decl_implicit() test avoids false positives for implicitly
12646 declared built-ins with library fallbacks (such as abs). */
12647 && DECL_BUILT_IN (expr)
12648 && DECL_IS_BUILTIN (expr)
12649 && !c_decl_implicit (expr)
12650 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12651 {
12652 if (loc == UNKNOWN_LOCATION)
12653 loc = EXPR_LOC_OR_LOC (expr, input_location);
12654
12655 /* Reject arguments that are built-in functions with
12656 no library fallback. */
12657 error_at (loc, "built-in function %qE must be directly called", expr);
12658
12659 return true;
12660 }
12661
12662 return false;
12663}
12664
3e3b8d63
MP
12665/* If we're creating an if-else-if condition chain, first see if we
12666 already have this COND in the CHAIN. If so, warn and don't add COND
12667 into the vector, otherwise add the COND there. LOC is the location
12668 of COND. */
12669
12670void
12671warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12672{
12673 /* No chain has been created yet. Do nothing. */
12674 if (*chain == NULL)
12675 return;
12676
12677 if (TREE_SIDE_EFFECTS (cond))
12678 {
12679 /* Uh-oh! This condition has a side-effect, thus invalidates
12680 the whole chain. */
12681 delete *chain;
12682 *chain = NULL;
12683 return;
12684 }
12685
12686 unsigned int ix;
12687 tree t;
12688 bool found = false;
12689 FOR_EACH_VEC_ELT (**chain, ix, t)
12690 if (operand_equal_p (cond, t, 0))
12691 {
12692 if (warning_at (loc, OPT_Wduplicated_cond,
12693 "duplicated %<if%> condition"))
12694 inform (EXPR_LOCATION (t), "previously used here");
12695 found = true;
12696 break;
12697 }
12698
12699 if (!found
12700 && !CONSTANT_CLASS_P (cond)
12701 /* Don't infinitely grow the chain. */
12702 && (*chain)->length () < 512)
12703 (*chain)->safe_push (cond);
12704}
12705
e78bede6
MP
12706/* Check if array size calculations overflow or if the array covers more
12707 than half of the address space. Return true if the size of the array
12708 is valid, false otherwise. TYPE is the type of the array and NAME is
12709 the name of the array, or NULL_TREE for unnamed arrays. */
12710
12711bool
12712valid_array_size_p (location_t loc, tree type, tree name)
12713{
12714 if (type != error_mark_node
12715 && COMPLETE_TYPE_P (type)
12716 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12717 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
12718 {
12719 if (name)
12720 error_at (loc, "size of array %qE is too large", name);
12721 else
12722 error_at (loc, "size of unnamed array is too large");
12723 return false;
12724 }
12725 return true;
12726}
12727
39dabefd 12728#include "gt-c-family-c-common.h"