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