]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
c-common.c (get_source_date_epoch): New function...
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
818ab71a 2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
b30f223b 3
1322177d 4This file is part of GCC.
b30f223b 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
b30f223b 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
b30f223b
RS
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
b30f223b 19
40e23961
MC
20#define GCC_C_COMMON_C
21
b30f223b 22#include "config.h"
670ee920 23#include "system.h"
4977bab6 24#include "coretypes.h"
2adfab87
AM
25#include "target.h"
26#include "function.h"
2adfab87 27#include "tree.h"
b559c810 28#include "c-common.h"
2adfab87
AM
29#include "gimple-expr.h"
30#include "tm_p.h"
31#include "stringpool.h"
32#include "cgraph.h"
33#include "diagnostic.h"
d9b2742a 34#include "intl.h"
d8a2d370
DN
35#include "stor-layout.h"
36#include "calls.h"
d8a2d370
DN
37#include "attribs.h"
38#include "varasm.h"
39#include "trans-mem.h"
61d3ce20 40#include "c-objc.h"
677f3fa8 41#include "common/common-target.h"
7afff7cf 42#include "langhooks.h"
d57a4b98 43#include "tree-inline.h"
5f1989e6 44#include "toplev.h"
6de9cd9a 45#include "tree-iterator.h"
d974312d 46#include "opts.h"
45b0be94 47#include "gimplify.h"
cb60f38d 48
81a75f0f 49cpp_reader *parse_in; /* Declared in c-pragma.h. */
c8724862 50
8fba1830
BRF
51/* Mode used to build pointers (VOIDmode means ptr_mode). */
52
53machine_mode c_default_pointer_mode = VOIDmode;
54
7f4edbcb 55/* The following symbols are subsumed in the c_global_trees array, and
d125d268 56 listed here individually for documentation purposes.
7f4edbcb
BS
57
58 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
59
60 tree short_integer_type_node;
61 tree long_integer_type_node;
62 tree long_long_integer_type_node;
63
64 tree short_unsigned_type_node;
65 tree long_unsigned_type_node;
66 tree long_long_unsigned_type_node;
67
de7df9eb
JM
68 tree truthvalue_type_node;
69 tree truthvalue_false_node;
70 tree truthvalue_true_node;
7f4edbcb
BS
71
72 tree ptrdiff_type_node;
73
74 tree unsigned_char_type_node;
75 tree signed_char_type_node;
76 tree wchar_type_node;
7f4edbcb 77
b6baa67d
KVH
78 tree char16_type_node;
79 tree char32_type_node;
80
7f4edbcb
BS
81 tree float_type_node;
82 tree double_type_node;
83 tree long_double_type_node;
84
85 tree complex_integer_type_node;
86 tree complex_float_type_node;
87 tree complex_double_type_node;
88 tree complex_long_double_type_node;
89
9a8ce21f
JG
90 tree dfloat32_type_node;
91 tree dfloat64_type_node;
92 tree_dfloat128_type_node;
93
7f4edbcb
BS
94 tree intQI_type_node;
95 tree intHI_type_node;
96 tree intSI_type_node;
97 tree intDI_type_node;
98 tree intTI_type_node;
99
100 tree unsigned_intQI_type_node;
101 tree unsigned_intHI_type_node;
102 tree unsigned_intSI_type_node;
103 tree unsigned_intDI_type_node;
104 tree unsigned_intTI_type_node;
105
106 tree widest_integer_literal_type_node;
107 tree widest_unsigned_literal_type_node;
108
109 Nodes for types `void *' and `const void *'.
110
111 tree ptr_type_node, const_ptr_type_node;
112
113 Nodes for types `char *' and `const char *'.
114
115 tree string_type_node, const_string_type_node;
116
117 Type `char[SOMENUMBER]'.
118 Used when an array of char is needed and the size is irrelevant.
119
120 tree char_array_type_node;
121
7f4edbcb
BS
122 Type `wchar_t[SOMENUMBER]' or something like it.
123 Used when a wide string literal is created.
124
125 tree wchar_array_type_node;
126
b6baa67d
KVH
127 Type `char16_t[SOMENUMBER]' or something like it.
128 Used when a UTF-16 string literal is created.
129
130 tree char16_array_type_node;
131
132 Type `char32_t[SOMENUMBER]' or something like it.
133 Used when a UTF-32 string literal is created.
134
135 tree char32_array_type_node;
136
7f4edbcb
BS
137 Type `int ()' -- used for implicit declaration of functions.
138
139 tree default_function_type;
140
7f4edbcb
BS
141 A VOID_TYPE node, packaged in a TREE_LIST.
142
143 tree void_list_node;
144
684d9f3b 145 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
0ba8a114
NS
146 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
147 VAR_DECLS, but C++ does.)
63ad61ed 148
0ba8a114 149 tree function_name_decl_node;
684d9f3b 150 tree pretty_function_name_decl_node;
0ba8a114
NS
151 tree c99_function_name_decl_node;
152
153 Stack of nested function name VAR_DECLs.
35b1a6fa 154
0ba8a114 155 tree saved_function_name_decls;
63ad61ed 156
7f4edbcb
BS
157*/
158
159tree c_global_trees[CTI_MAX];
17211ab5 160\f
4078b403
NB
161/* Switches common to the C front ends. */
162
63973df3
NB
163/* Nonzero means don't output line number information. */
164
165char flag_no_line_commands;
166
167/* Nonzero causes -E output not to be done, but directives such as
168 #define that have side effects are still obeyed. */
169
170char flag_no_output;
171
172/* Nonzero means dump macros in some fashion. */
173
174char flag_dump_macros;
175
176/* Nonzero means pass #include lines through to the output. */
177
178char flag_dump_includes;
179
c0d578e6
GK
180/* Nonzero means process PCH files while preprocessing. */
181
182bool flag_pch_preprocess;
183
17211ab5
GK
184/* The file name to which we should write a precompiled header, or
185 NULL if no header will be written in this compile. */
186
187const char *pch_file;
188
3df89291
NB
189/* Nonzero if an ISO standard was selected. It rejects macros in the
190 user's namespace. */
191int flag_iso;
192
4078b403
NB
193/* C/ObjC language option variables. */
194
195
4078b403
NB
196/* Nonzero means allow type mismatches in conditional expressions;
197 just make their values `void'. */
198
199int flag_cond_mismatch;
200
201/* Nonzero means enable C89 Amendment 1 features. */
202
203int flag_isoc94;
204
48b0b196 205/* Nonzero means use the ISO C99 (or C11) dialect of C. */
4078b403
NB
206
207int flag_isoc99;
208
48b0b196 209/* Nonzero means use the ISO C11 dialect of C. */
2778d766 210
48b0b196 211int flag_isoc11;
2778d766 212
6614fd40 213/* Nonzero means that we have builtin functions, and main is an int. */
4078b403
NB
214
215int flag_hosted = 1;
216
4078b403
NB
217
218/* ObjC language option variables. */
219
220
4078b403
NB
221/* Tells the compiler that this is a special run. Do not perform any
222 compiling, instead we are to test some platform dependent features
223 and output a C header file with appropriate definitions. */
224
225int print_struct_values;
226
fa10beec 227/* Tells the compiler what is the constant string class for ObjC. */
4078b403
NB
228
229const char *constant_string_class_name;
230
4078b403
NB
231
232/* C++ language option variables. */
233
a75f1574
JM
234/* The reference version of the ABI for -Wabi. */
235
236int warn_abi_version = -1;
4078b403 237
4078b403
NB
238/* Nonzero means generate separate instantiation control files and
239 juggle them at link time. */
240
241int flag_use_repository;
242
129211bc 243/* The C++ dialect being used. Default set in c_common_post_options. */
966541e3 244
129211bc 245enum cxx_dialect cxx_dialect = cxx_unset;
966541e3 246
4afe7ad7 247/* Maximum template instantiation depth. This limit exists to limit the
3ff60975 248 time it takes to notice excessively recursive template instantiations.
4078b403 249
3ff60975
JM
250 The default is lower than the 1024 recommended by the C++0x standard
251 because G++ runs out of stack before 1024 with highly recursive template
252 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
253
254int max_tinst_depth = 900;
4078b403 255
f09f1de5
MM
256/* The elements of `ridpointers' are identifier nodes for the reserved
257 type names and storage classes. It is indexed by a RID_... value. */
258tree *ridpointers;
259
c2255bc4 260tree (*make_fname_decl) (location_t, tree, int);
2ce07e2d 261
7d882b83
ILT
262/* Nonzero means don't warn about problems that occur when the code is
263 executed. */
264int c_inhibit_evaluation_warnings;
e78a3b42 265
5386338c
JM
266/* Whether we are building a boolean conversion inside
267 convert_for_assignment, or some other late binary operation. If
268 build_binary_op is called for C (from code shared by C and C++) in
269 this case, then the operands have already been folded and the
270 result will not be folded again, so C_MAYBE_CONST_EXPR should not
271 be generated. */
272bool in_late_binary_op;
273
148e4216
JM
274/* Whether lexing has been completed, so subsequent preprocessor
275 errors should use the compiler's input_location. */
276bool done_lexing = false;
277
ec5c56db 278/* Information about how a function name is generated. */
0ba8a114
NS
279struct fname_var_t
280{
8b60264b
KG
281 tree *const decl; /* pointer to the VAR_DECL. */
282 const unsigned rid; /* RID number for the identifier. */
283 const int pretty; /* How pretty is it? */
0ba8a114
NS
284};
285
ec5c56db 286/* The three ways of getting then name of the current function. */
0ba8a114
NS
287
288const struct fname_var_t fname_vars[] =
289{
ec5c56db 290 /* C99 compliant __func__, must be first. */
0ba8a114 291 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
ec5c56db 292 /* GCC __FUNCTION__ compliant. */
0ba8a114 293 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
ec5c56db 294 /* GCC __PRETTY_FUNCTION__ compliant. */
0ba8a114
NS
295 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
296 {NULL, 0, 0},
297};
298
c98cd5bf
JM
299/* Global visibility options. */
300struct visibility_flags visibility_options;
301
62e4eb35 302static tree check_case_value (location_t, tree);
b155cfd9
MP
303static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
304 bool *);
4724b3de 305
35b1a6fa
AJ
306static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
307static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
308static tree handle_common_attribute (tree *, tree, tree, int, bool *);
309static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52bf96d2
JH
310static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
311static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
e664c61c
KS
312static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
313 int, bool *);
77bc5132
JJ
314static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
315 int, bool *);
ce6923c5
MP
316static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
317 bool *);
5434dc07 318static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 319static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
86631ea3 320static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
185c9e56 321static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
46a4da10 322static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
323static tree handle_always_inline_attribute (tree *, tree, tree, int,
324 bool *);
d752cfdb
JJ
325static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
326static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0691d1d4 327static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
d2af6a68 328static tree handle_error_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 329static tree handle_used_attribute (tree *, tree, tree, int, bool *);
ce91e74c
JH
330static tree handle_externally_visible_attribute (tree *, tree, tree, int,
331 bool *);
7861b648
AK
332static tree handle_no_reorder_attribute (tree *, tree, tree, int,
333 bool *);
35b1a6fa
AJ
334static tree handle_const_attribute (tree *, tree, tree, int, bool *);
335static tree handle_transparent_union_attribute (tree *, tree, tree,
336 int, bool *);
ee45a32d
EB
337static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
338 int, bool *);
35b1a6fa
AJ
339static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
340static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
341static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
342static tree handle_section_attribute (tree *, tree, tree, int, bool *);
343static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
344static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4bb794e2 345static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
ba885ec5
NS
346static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
347static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 348static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
a0203ca7 349static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
35b1a6fa
AJ
350static tree handle_visibility_attribute (tree *, tree, tree, int,
351 bool *);
352static tree handle_tls_model_attribute (tree *, tree, tree, int,
353 bool *);
354static tree handle_no_instrument_function_attribute (tree *, tree,
355 tree, int, bool *);
356static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
6e9a3221 357static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
358static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
359 bool *);
360static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
0a35513e
AH
361static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
362static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
dcd6de6d 363static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
364static tree handle_deprecated_attribute (tree *, tree, tree, int,
365 bool *);
366static tree handle_vector_size_attribute (tree *, tree, tree, int,
367 bool *);
368static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
369static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
370static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
72954a4f
JM
371static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
372 bool *);
3d091dac 373static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5d32c25 374static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
51bc54a6 375static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
8fcbce72
JJ
376static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
377static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
5779e713 378static tree handle_target_attribute (tree *, tree, tree, int, bool *);
3b1661a9 379static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
ab442df7 380static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
0a35513e 381static tree ignore_attribute (tree *, tree, tree, int, bool *);
7458026b 382static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
0b7b376d 383static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
2a99e5e6 384static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
826cacfe 385static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
386static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
387 bool *);
fff77217 388static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
389static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
390 bool *);
976d5a22 391static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
d5e254e1
IE
392static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
393static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
394static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
35b1a6fa 395
35b1a6fa
AJ
396static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
397static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
398static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
d07605f5 399static int resort_field_decl_cmp (const void *, const void *);
b34c7881 400
eea1139b
ILT
401/* Reserved words. The third field is a mask: keywords are disabled
402 if they match the mask.
403
404 Masks for languages:
405 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
406 C --std=c99: D_CXXONLY | D_OBJC
407 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
408 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
409 C++ --std=c0x: D_CONLY | D_OBJC
410 ObjC++ is like C++ except that D_OBJC is not set
411
412 If -fno-asm is used, D_ASM is added to the mask. If
413 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
414 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
1973201f 415 In C with -Wc++-compat, we warn if D_CXXWARN is set.
eea1139b 416
1973201f
NP
417 Note the complication of the D_CXX_OBJC keywords. These are
418 reserved words such as 'class'. In C++, 'class' is a reserved
419 word. In Objective-C++ it is too. In Objective-C, it is a
420 reserved word too, but only if it follows an '@' sign.
421*/
eea1139b
ILT
422const struct c_common_resword c_common_reswords[] =
423{
d19fa6b5
JM
424 { "_Alignas", RID_ALIGNAS, D_CONLY },
425 { "_Alignof", RID_ALIGNOF, D_CONLY },
267bac10 426 { "_Atomic", RID_ATOMIC, D_CONLY },
eea1139b
ILT
427 { "_Bool", RID_BOOL, D_CONLY },
428 { "_Complex", RID_COMPLEX, 0 },
939b37da
BI
429 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
430 { "_Cilk_sync", RID_CILK_SYNC, 0 },
9a771876 431 { "_Cilk_for", RID_CILK_FOR, 0 },
fa5da7de 432 { "_Imaginary", RID_IMAGINARY, D_CONLY },
eea1139b
ILT
433 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
434 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
435 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
436 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
437 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
438 { "_Sat", RID_SAT, D_CONLY | D_EXT },
32912286 439 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
bbceee64 440 { "_Noreturn", RID_NORETURN, D_CONLY },
433cc7b0 441 { "_Generic", RID_GENERIC, D_CONLY },
582d9b50 442 { "_Thread_local", RID_THREAD, D_CONLY },
eea1139b
ILT
443 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
444 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
445 { "__alignof", RID_ALIGNOF, 0 },
446 { "__alignof__", RID_ALIGNOF, 0 },
447 { "__asm", RID_ASM, 0 },
448 { "__asm__", RID_ASM, 0 },
449 { "__attribute", RID_ATTRIBUTE, 0 },
450 { "__attribute__", RID_ATTRIBUTE, 0 },
38b7bc7f 451 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
4daba884 452 { "__bases", RID_BASES, D_CXXONLY },
74893f25
RH
453 { "__builtin_call_with_static_chain",
454 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
eea1139b 455 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
d4a83c10 456 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
9e1a8dd1 457 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
eea1139b
ILT
458 { "__builtin_offsetof", RID_OFFSETOF, 0 },
459 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
460 { "__builtin_va_arg", RID_VA_ARG, 0 },
461 { "__complex", RID_COMPLEX, 0 },
462 { "__complex__", RID_COMPLEX, 0 },
463 { "__const", RID_CONST, 0 },
464 { "__const__", RID_CONST, 0 },
465 { "__decltype", RID_DECLTYPE, D_CXXONLY },
4daba884 466 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
eea1139b
ILT
467 { "__extension__", RID_EXTENSION, 0 },
468 { "__func__", RID_C99_FUNCTION_NAME, 0 },
469 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
470 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
471 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
472 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
473 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
474 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
475 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
476 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
3c0d13bf
PC
477 { "__imag", RID_IMAGPART, 0 },
478 { "__imag__", RID_IMAGPART, 0 },
479 { "__inline", RID_INLINE, 0 },
480 { "__inline__", RID_INLINE, 0 },
eea1139b
ILT
481 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
482 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
483 { "__is_class", RID_IS_CLASS, D_CXXONLY },
eea1139b
ILT
484 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
485 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
b3908fcc 486 { "__is_final", RID_IS_FINAL, D_CXXONLY },
3c0d13bf 487 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
eea1139b
ILT
488 { "__is_pod", RID_IS_POD, D_CXXONLY },
489 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
971e17ff 490 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c32097d8
JM
491 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
492 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
dd5d5481
JM
493 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
494 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
b752325e 495 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
eea1139b 496 { "__is_union", RID_IS_UNION, D_CXXONLY },
eea1139b
ILT
497 { "__label__", RID_LABEL, 0 },
498 { "__null", RID_NULL, 0 },
499 { "__real", RID_REALPART, 0 },
500 { "__real__", RID_REALPART, 0 },
501 { "__restrict", RID_RESTRICT, 0 },
502 { "__restrict__", RID_RESTRICT, 0 },
503 { "__signed", RID_SIGNED, 0 },
504 { "__signed__", RID_SIGNED, 0 },
505 { "__thread", RID_THREAD, 0 },
0a35513e
AH
506 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
507 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
508 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
eea1139b
ILT
509 { "__typeof", RID_TYPEOF, 0 },
510 { "__typeof__", RID_TYPEOF, 0 },
3c0d13bf 511 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
eea1139b
ILT
512 { "__volatile", RID_VOLATILE, 0 },
513 { "__volatile__", RID_VOLATILE, 0 },
36a85135
JM
514 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
515 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
516 { "asm", RID_ASM, D_ASM },
517 { "auto", RID_AUTO, 0 },
03c3034e 518 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
519 { "break", RID_BREAK, 0 },
520 { "case", RID_CASE, 0 },
2696a995 521 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
eea1139b 522 { "char", RID_CHAR, 0 },
36a85135
JM
523 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
524 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995 525 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
eea1139b 526 { "const", RID_CONST, 0 },
36a85135 527 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
528 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
529 { "continue", RID_CONTINUE, 0 },
36a85135 530 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b 531 { "default", RID_DEFAULT, 0 },
2696a995 532 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
533 { "do", RID_DO, 0 },
534 { "double", RID_DOUBLE, 0 },
535 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
536 { "else", RID_ELSE, 0 },
537 { "enum", RID_ENUM, 0 },
2696a995
KG
538 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
539 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
eea1139b 540 { "extern", RID_EXTERN, 0 },
2696a995 541 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
542 { "float", RID_FLOAT, 0 },
543 { "for", RID_FOR, 0 },
2696a995 544 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
545 { "goto", RID_GOTO, 0 },
546 { "if", RID_IF, 0 },
547 { "inline", RID_INLINE, D_EXT89 },
548 { "int", RID_INT, 0 },
549 { "long", RID_LONG, 0 },
550 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
2696a995
KG
551 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
552 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
36a85135
JM
553 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
554 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995
KG
555 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
556 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
557 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
558 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
eea1139b
ILT
559 { "register", RID_REGISTER, 0 },
560 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
561 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
562 { "return", RID_RETURN, 0 },
563 { "short", RID_SHORT, 0 },
564 { "signed", RID_SIGNED, 0 },
565 { "sizeof", RID_SIZEOF, 0 },
566 { "static", RID_STATIC, 0 },
36a85135 567 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
568 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
569 { "struct", RID_STRUCT, 0 },
570 { "switch", RID_SWITCH, 0 },
2696a995
KG
571 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
572 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
36a85135 573 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995
KG
574 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
575 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
576 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
eea1139b 577 { "typedef", RID_TYPEDEF, 0 },
2696a995
KG
578 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
579 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
580 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
581 { "union", RID_UNION, 0 },
582 { "unsigned", RID_UNSIGNED, 0 },
2696a995
KG
583 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
584 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
585 { "void", RID_VOID, 0 },
586 { "volatile", RID_VOLATILE, 0 },
587 { "wchar_t", RID_WCHAR, D_CXXONLY },
588 { "while", RID_WHILE, 0 },
971e17ff 589
b8fd7909
JM
590 /* C++ transactional memory. */
591 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
592 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
593 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
594 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
595
971e17ff
AS
596 /* Concepts-related keywords */
597 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
598 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
599
eea1139b
ILT
600 /* These Objective-C keywords are recognized only immediately after
601 an '@'. */
602 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
603 { "defs", RID_AT_DEFS, D_OBJC },
604 { "encode", RID_AT_ENCODE, D_OBJC },
605 { "end", RID_AT_END, D_OBJC },
606 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
607 { "interface", RID_AT_INTERFACE, D_OBJC },
608 { "protocol", RID_AT_PROTOCOL, D_OBJC },
609 { "selector", RID_AT_SELECTOR, D_OBJC },
610 { "finally", RID_AT_FINALLY, D_OBJC },
92902b1b
IS
611 { "optional", RID_AT_OPTIONAL, D_OBJC },
612 { "required", RID_AT_REQUIRED, D_OBJC },
668ea4b1 613 { "property", RID_AT_PROPERTY, D_OBJC },
c37d8c30 614 { "package", RID_AT_PACKAGE, D_OBJC },
da57d1b9
NP
615 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
616 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
eea1139b
ILT
617 /* These are recognized only in protocol-qualifier context
618 (see above) */
619 { "bycopy", RID_BYCOPY, D_OBJC },
620 { "byref", RID_BYREF, D_OBJC },
621 { "in", RID_IN, D_OBJC },
622 { "inout", RID_INOUT, D_OBJC },
623 { "oneway", RID_ONEWAY, D_OBJC },
624 { "out", RID_OUT, D_OBJC },
668ea4b1 625 /* These are recognized inside a property attribute list */
200290f2
NP
626 { "assign", RID_ASSIGN, D_OBJC },
627 { "copy", RID_COPY, D_OBJC },
668ea4b1 628 { "getter", RID_GETTER, D_OBJC },
200290f2
NP
629 { "nonatomic", RID_NONATOMIC, D_OBJC },
630 { "readonly", RID_READONLY, D_OBJC },
631 { "readwrite", RID_READWRITE, D_OBJC },
632 { "retain", RID_RETAIN, D_OBJC },
668ea4b1 633 { "setter", RID_SETTER, D_OBJC },
eea1139b
ILT
634};
635
636const unsigned int num_c_common_reswords =
637 sizeof c_common_reswords / sizeof (struct c_common_resword);
638
d5e254e1
IE
639/* Table of machine-independent attributes common to all C-like languages.
640
641 All attributes referencing arguments should be additionally processed
642 in chkp_copy_function_type_adding_bounds for correct instrumentation
643 by Pointer Bounds Checker.
644 Current list of processed common attributes: nonnull. */
349ae713
NB
645const struct attribute_spec c_common_attribute_table[] =
646{
62d784f7
KT
647 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
648 affects_type_identity } */
349ae713 649 { "packed", 0, 0, false, false, false,
62d784f7 650 handle_packed_attribute , false},
349ae713 651 { "nocommon", 0, 0, true, false, false,
62d784f7 652 handle_nocommon_attribute, false},
349ae713 653 { "common", 0, 0, true, false, false,
62d784f7 654 handle_common_attribute, false },
349ae713
NB
655 /* FIXME: logically, noreturn attributes should be listed as
656 "false, true, true" and apply to function types. But implementing this
657 would require all the places in the compiler that use TREE_THIS_VOLATILE
658 on a decl to identify non-returning functions to be located and fixed
659 to check the function type instead. */
660 { "noreturn", 0, 0, true, false, false,
62d784f7 661 handle_noreturn_attribute, false },
349ae713 662 { "volatile", 0, 0, true, false, false,
62d784f7 663 handle_noreturn_attribute, false },
5434dc07
MD
664 { "stack_protect", 0, 0, true, false, false,
665 handle_stack_protect_attribute, false },
349ae713 666 { "noinline", 0, 0, true, false, false,
62d784f7 667 handle_noinline_attribute, false },
86631ea3 668 { "noclone", 0, 0, true, false, false,
62d784f7 669 handle_noclone_attribute, false },
185c9e56
ML
670 { "no_icf", 0, 0, true, false, false,
671 handle_noicf_attribute, false },
46a4da10 672 { "leaf", 0, 0, true, false, false,
62d784f7 673 handle_leaf_attribute, false },
349ae713 674 { "always_inline", 0, 0, true, false, false,
62d784f7 675 handle_always_inline_attribute, false },
4eb7fd83 676 { "gnu_inline", 0, 0, true, false, false,
62d784f7 677 handle_gnu_inline_attribute, false },
d752cfdb 678 { "artificial", 0, 0, true, false, false,
62d784f7 679 handle_artificial_attribute, false },
0691d1d4 680 { "flatten", 0, 0, true, false, false,
62d784f7 681 handle_flatten_attribute, false },
349ae713 682 { "used", 0, 0, true, false, false,
62d784f7 683 handle_used_attribute, false },
349ae713 684 { "unused", 0, 0, false, false, false,
62d784f7 685 handle_unused_attribute, false },
ce91e74c 686 { "externally_visible", 0, 0, true, false, false,
62d784f7 687 handle_externally_visible_attribute, false },
7861b648
AK
688 { "no_reorder", 0, 0, true, false, false,
689 handle_no_reorder_attribute, false },
349ae713
NB
690 /* The same comments as for noreturn attributes apply to const ones. */
691 { "const", 0, 0, true, false, false,
62d784f7 692 handle_const_attribute, false },
ee45a32d
EB
693 { "scalar_storage_order", 1, 1, false, false, false,
694 handle_scalar_storage_order_attribute, false },
349ae713 695 { "transparent_union", 0, 0, false, false, false,
62d784f7 696 handle_transparent_union_attribute, false },
fc8600f9 697 { "constructor", 0, 1, true, false, false,
62d784f7 698 handle_constructor_attribute, false },
fc8600f9 699 { "destructor", 0, 1, true, false, false,
62d784f7 700 handle_destructor_attribute, false },
349ae713 701 { "mode", 1, 1, false, true, false,
62d784f7 702 handle_mode_attribute, false },
349ae713 703 { "section", 1, 1, true, false, false,
62d784f7 704 handle_section_attribute, false },
349ae713 705 { "aligned", 0, 1, false, false, false,
62d784f7 706 handle_aligned_attribute, false },
349ae713 707 { "weak", 0, 0, true, false, false,
62d784f7 708 handle_weak_attribute, false },
4bb794e2
ST
709 { "noplt", 0, 0, true, false, false,
710 handle_noplt_attribute, false },
ba885ec5 711 { "ifunc", 1, 1, true, false, false,
62d784f7 712 handle_ifunc_attribute, false },
349ae713 713 { "alias", 1, 1, true, false, false,
62d784f7 714 handle_alias_attribute, false },
a0203ca7 715 { "weakref", 0, 1, true, false, false,
62d784f7 716 handle_weakref_attribute, false },
349ae713 717 { "no_instrument_function", 0, 0, true, false, false,
62d784f7
KT
718 handle_no_instrument_function_attribute,
719 false },
349ae713 720 { "malloc", 0, 0, true, false, false,
62d784f7 721 handle_malloc_attribute, false },
6e9a3221 722 { "returns_twice", 0, 0, true, false, false,
62d784f7 723 handle_returns_twice_attribute, false },
349ae713 724 { "no_stack_limit", 0, 0, true, false, false,
62d784f7 725 handle_no_limit_stack_attribute, false },
349ae713 726 { "pure", 0, 0, true, false, false,
62d784f7 727 handle_pure_attribute, false },
0a35513e
AH
728 { "transaction_callable", 0, 0, false, true, false,
729 handle_tm_attribute, false },
730 { "transaction_unsafe", 0, 0, false, true, false,
b8fd7909 731 handle_tm_attribute, true },
0a35513e 732 { "transaction_safe", 0, 0, false, true, false,
b8fd7909
JM
733 handle_tm_attribute, true },
734 { "transaction_safe_dynamic", 0, 0, true, false, false,
0a35513e
AH
735 handle_tm_attribute, false },
736 { "transaction_may_cancel_outer", 0, 0, false, true, false,
737 handle_tm_attribute, false },
738 /* ??? These two attributes didn't make the transition from the
739 Intel language document to the multi-vendor language document. */
740 { "transaction_pure", 0, 0, false, true, false,
741 handle_tm_attribute, false },
742 { "transaction_wrap", 1, 1, true, false, false,
743 handle_tm_wrap_attribute, false },
dcd6de6d
ZD
744 /* For internal use (marking of builtins) only. The name contains space
745 to prevent its usage in source code. */
746 { "no vops", 0, 0, true, false, false,
62d784f7 747 handle_novops_attribute, false },
9b86d6bb 748 { "deprecated", 0, 1, false, false, false,
62d784f7 749 handle_deprecated_attribute, false },
349ae713 750 { "vector_size", 1, 1, false, true, false,
62d784f7 751 handle_vector_size_attribute, false },
d7afec4b 752 { "visibility", 1, 1, false, false, false,
62d784f7 753 handle_visibility_attribute, false },
dce81a1a 754 { "tls_model", 1, 1, true, false, false,
62d784f7 755 handle_tls_model_attribute, false },
b34c7881 756 { "nonnull", 0, -1, false, true, true,
62d784f7 757 handle_nonnull_attribute, false },
39f2f3c8 758 { "nothrow", 0, 0, true, false, false,
62d784f7
KT
759 handle_nothrow_attribute, false },
760 { "may_alias", 0, 0, false, true, false, NULL, false },
0bfa5f65 761 { "cleanup", 1, 1, true, false, false,
62d784f7 762 handle_cleanup_attribute, false },
72954a4f 763 { "warn_unused_result", 0, 0, false, true, true,
62d784f7 764 handle_warn_unused_result_attribute, false },
254986c7 765 { "sentinel", 0, 1, false, true, true,
62d784f7 766 handle_sentinel_attribute, false },
b5d32c25
KG
767 /* For internal use (marking of builtins) only. The name contains space
768 to prevent its usage in source code. */
769 { "type generic", 0, 0, false, true, true,
62d784f7 770 handle_type_generic_attribute, false },
51bc54a6 771 { "alloc_size", 1, 2, false, true, true,
62d784f7 772 handle_alloc_size_attribute, false },
52bf96d2 773 { "cold", 0, 0, true, false, false,
62d784f7 774 handle_cold_attribute, false },
52bf96d2 775 { "hot", 0, 0, true, false, false,
62d784f7 776 handle_hot_attribute, false },
77bc5132
JJ
777 { "no_address_safety_analysis",
778 0, 0, true, false, false,
779 handle_no_address_safety_analysis_attribute,
780 false },
e664c61c
KS
781 { "no_sanitize_address", 0, 0, true, false, false,
782 handle_no_sanitize_address_attribute,
783 false },
de35aa66
MS
784 { "no_sanitize_thread", 0, 0, true, false, false,
785 handle_no_sanitize_address_attribute,
786 false },
ce6923c5
MP
787 { "no_sanitize_undefined", 0, 0, true, false, false,
788 handle_no_sanitize_undefined_attribute,
789 false },
d2af6a68 790 { "warning", 1, 1, true, false, false,
62d784f7 791 handle_error_attribute, false },
d2af6a68 792 { "error", 1, 1, true, false, false,
62d784f7 793 handle_error_attribute, false },
5779e713 794 { "target", 1, -1, true, false, false,
62d784f7 795 handle_target_attribute, false },
3b1661a9
ES
796 { "target_clones", 1, -1, true, false, false,
797 handle_target_clones_attribute, false },
ab442df7 798 { "optimize", 1, -1, true, false, false,
62d784f7 799 handle_optimize_attribute, false },
0a35513e
AH
800 /* For internal use only. The leading '*' both prevents its usage in
801 source code and signals that it may be overridden by machine tables. */
802 { "*tm regparm", 0, 0, false, true, true,
803 ignore_attribute, false },
7458026b 804 { "no_split_stack", 0, 0, true, false, false,
62d784f7 805 handle_no_split_stack_attribute, false },
0b7b376d
RG
806 /* For internal use (marking of builtins and runtime functions) only.
807 The name contains space to prevent its usage in source code. */
808 { "fn spec", 1, 1, false, true, true,
62d784f7 809 handle_fnspec_attribute, false },
2a99e5e6
LL
810 { "warn_unused", 0, 0, false, false, false,
811 handle_warn_unused_attribute, false },
826cacfe
MG
812 { "returns_nonnull", 0, 0, false, true, true,
813 handle_returns_nonnull_attribute, false },
acf0174b
JJ
814 { "omp declare simd", 0, -1, true, false, false,
815 handle_omp_declare_simd_attribute, false },
41958c28
BI
816 { "cilk simd function", 0, -1, true, false, false,
817 handle_omp_declare_simd_attribute, false },
e7b69085 818 { "simd", 0, 1, true, false, false,
fff77217 819 handle_simd_attribute, false },
acf0174b
JJ
820 { "omp declare target", 0, 0, true, false, false,
821 handle_omp_declare_target_attribute, false },
4a38b02b
IV
822 { "omp declare target link", 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
f479b43d
JM
1097/* Fold X for consideration by one of the warning functions when checking
1098 whether an expression has a constant value. */
1099
1100static tree
1101fold_for_warn (tree x)
1102{
1103 if (c_dialect_cxx ())
1104 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1105 else
1106 /* The C front-end has already folded X appropriately. */
1107 return x;
1108}
1109
d74154d5
RS
1110/* Print a warning if a constant expression had overflow in folding.
1111 Invoke this function on every expression that the language
1112 requires to be a constant expression.
1113 Note the ANSI C standard says it is erroneous for a
1114 constant expression to overflow. */
96571883
BK
1115
1116void
35b1a6fa 1117constant_expression_warning (tree value)
393eda6a 1118{
b8698a0f 1119 if (warn_overflow && pedantic
393eda6a
MLI
1120 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1121 || TREE_CODE (value) == FIXED_CST
1122 || TREE_CODE (value) == VECTOR_CST
1123 || TREE_CODE (value) == COMPLEX_CST)
1124 && TREE_OVERFLOW (value))
509c9d60 1125 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
393eda6a
MLI
1126}
1127
1128/* The same as above but print an unconditional error. */
1129void
1130constant_expression_error (tree value)
96571883 1131{
c05f751c 1132 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
ab22c1fa 1133 || TREE_CODE (value) == FIXED_CST
69ef87e2 1134 || TREE_CODE (value) == VECTOR_CST
c05f751c 1135 || TREE_CODE (value) == COMPLEX_CST)
393eda6a
MLI
1136 && TREE_OVERFLOW (value))
1137 error ("overflow in constant expression");
d74154d5
RS
1138}
1139
59c0753d
MLI
1140/* Print a warning if an expression had overflow in folding and its
1141 operands hadn't.
1142
d74154d5
RS
1143 Invoke this function on every expression that
1144 (1) appears in the source code, and
59c0753d 1145 (2) is a constant expression that overflowed, and
d74154d5 1146 (3) is not already checked by convert_and_check;
59c0753d
MLI
1147 however, do not invoke this function on operands of explicit casts
1148 or when the expression is the result of an operator and any operand
1149 already overflowed. */
d74154d5
RS
1150
1151void
c2255bc4 1152overflow_warning (location_t loc, tree value)
d74154d5 1153{
7d882b83
ILT
1154 if (c_inhibit_evaluation_warnings != 0)
1155 return;
59c0753d
MLI
1156
1157 switch (TREE_CODE (value))
69ef87e2 1158 {
59c0753d 1159 case INTEGER_CST:
c2255bc4 1160 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
59c0753d 1161 break;
b8698a0f 1162
59c0753d 1163 case REAL_CST:
c2255bc4
AH
1164 warning_at (loc, OPT_Woverflow,
1165 "floating point overflow in expression");
59c0753d 1166 break;
b8698a0f 1167
ab22c1fa 1168 case FIXED_CST:
c2255bc4 1169 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
ab22c1fa
CF
1170 break;
1171
59c0753d 1172 case VECTOR_CST:
c2255bc4 1173 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
59c0753d 1174 break;
b8698a0f 1175
59c0753d
MLI
1176 case COMPLEX_CST:
1177 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
c2255bc4
AH
1178 warning_at (loc, OPT_Woverflow,
1179 "complex integer overflow in expression");
59c0753d 1180 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
c2255bc4
AH
1181 warning_at (loc, OPT_Woverflow,
1182 "complex floating point overflow in expression");
59c0753d
MLI
1183 break;
1184
1185 default:
1186 break;
69ef87e2 1187 }
d74154d5
RS
1188}
1189
ca409efd
MLI
1190/* Warn about uses of logical || / && operator in a context where it
1191 is likely that the bitwise equivalent was intended by the
1192 programmer. We have seen an expression in which CODE is a binary
a243fb4a
MLI
1193 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1194 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
63a08740 1195void
a243fb4a 1196warn_logical_operator (location_t location, enum tree_code code, tree type,
b8698a0f 1197 enum tree_code code_left, tree op_left,
ca409efd 1198 enum tree_code ARG_UNUSED (code_right), tree op_right)
63a08740 1199{
a243fb4a
MLI
1200 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1201 int in0_p, in1_p, in_p;
1202 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1203 bool strict_overflow_p = false;
1204
ca409efd
MLI
1205 if (code != TRUTH_ANDIF_EXPR
1206 && code != TRUTH_AND_EXPR
1207 && code != TRUTH_ORIF_EXPR
1208 && code != TRUTH_OR_EXPR)
1209 return;
1210
b8787813
MP
1211 /* We don't want to warn if either operand comes from a macro
1212 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1213 see PR61534. */
1214 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1215 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1216 return;
1217
ca409efd
MLI
1218 /* Warn if &&/|| are being used in a context where it is
1219 likely that the bitwise equivalent was intended by the
1220 programmer. That is, an expression such as op && MASK
1221 where op should not be any boolean expression, nor a
1222 constant, and mask seems to be a non-boolean integer constant. */
f479b43d
JM
1223 if (TREE_CODE (op_right) == CONST_DECL)
1224 /* An enumerator counts as a constant. */
1225 op_right = DECL_INITIAL (op_right);
ca409efd
MLI
1226 if (!truth_value_p (code_left)
1227 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1228 && !CONSTANT_CLASS_P (op_left)
1229 && !TREE_NO_WARNING (op_left)
1230 && TREE_CODE (op_right) == INTEGER_CST
1231 && !integer_zerop (op_right)
1232 && !integer_onep (op_right))
63a08740 1233 {
a243fb4a 1234 if (or_op)
ca409efd
MLI
1235 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1236 " applied to non-boolean constant");
1237 else
1238 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1239 " applied to non-boolean constant");
1240 TREE_NO_WARNING (op_left) = true;
a243fb4a
MLI
1241 return;
1242 }
1243
1244 /* We do not warn for constants because they are typical of macro
1245 expansions that test for features. */
f479b43d
JM
1246 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1247 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
a243fb4a
MLI
1248 return;
1249
1250 /* This warning only makes sense with logical operands. */
1251 if (!(truth_value_p (TREE_CODE (op_left))
1252 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1253 || !(truth_value_p (TREE_CODE (op_right))
1254 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1255 return;
1256
3c9aabbd
MG
1257 /* The range computations only work with scalars. */
1258 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1259 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1260 return;
a243fb4a 1261
f2c4a785
MLI
1262 /* We first test whether either side separately is trivially true
1263 (with OR) or trivially false (with AND). If so, do not warn.
1264 This is a common idiom for testing ranges of data types in
1265 portable code. */
1266 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1267 if (!lhs)
1268 return;
1269 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
a243fb4a
MLI
1270 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1271
f2c4a785
MLI
1272 /* If this is an OR operation, invert both sides; now, the result
1273 should be always false to get a warning. */
1274 if (or_op)
1275 in0_p = !in0_p;
1276
1277 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1e537948 1278 if (tem && integer_zerop (tem))
f2c4a785
MLI
1279 return;
1280
1281 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1282 if (!rhs)
1283 return;
1284 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
a243fb4a 1285 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
b8698a0f 1286
f2c4a785
MLI
1287 /* If this is an OR operation, invert both sides; now, the result
1288 should be always false to get a warning. */
a243fb4a 1289 if (or_op)
f2c4a785 1290 in1_p = !in1_p;
b8698a0f 1291
f2c4a785 1292 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1e537948 1293 if (tem && integer_zerop (tem))
f2c4a785
MLI
1294 return;
1295
1296 /* If both expressions have the same operand, if we can merge the
8c2b7f79 1297 ranges, ... */
f2c4a785 1298 if (operand_equal_p (lhs, rhs, 0)
a243fb4a 1299 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
8c2b7f79 1300 in1_p, low1, high1))
a243fb4a 1301 {
8c2b7f79
MP
1302 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1303 /* ... and if the range test is always false, then warn. */
1304 if (tem && integer_zerop (tem))
1305 {
1306 if (or_op)
1307 warning_at (location, OPT_Wlogical_op,
1308 "logical %<or%> of collectively exhaustive tests is "
1309 "always true");
1310 else
1311 warning_at (location, OPT_Wlogical_op,
1312 "logical %<and%> of mutually exclusive tests is "
1313 "always false");
1314 }
1315 /* Or warn if the operands have exactly the same range, e.g.
1316 A > 0 && A > 0. */
a68ae2e1
MP
1317 else if (tree_int_cst_equal (low0, low1)
1318 && tree_int_cst_equal (high0, high1))
8c2b7f79
MP
1319 {
1320 if (or_op)
1321 warning_at (location, OPT_Wlogical_op,
1322 "logical %<or%> of equal expressions");
1323 else
1324 warning_at (location, OPT_Wlogical_op,
1325 "logical %<and%> of equal expressions");
1326 }
63a08740
DM
1327 }
1328}
1329
05b28fd6
MP
1330/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1331 with constant indices. */
1332
1333static tree
1334find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1335{
1336 tree expr = *expr_p;
1337
1338 if ((TREE_CODE (expr) == ARRAY_REF
1339 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1340 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1341 {
1342 *(bool *) data = true;
1343 *walk_subtrees = 0;
1344 }
1345
1346 return NULL_TREE;
1347}
1348
1349/* Warn if a self-comparison always evaluates to true or false. LOC
1350 is the location of the comparison with code CODE, LHS and RHS are
1351 operands of the comparison. */
1352
1353void
1354warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1355{
1356 if (TREE_CODE_CLASS (code) != tcc_comparison)
1357 return;
1358
f2afe6dd
MP
1359 /* Don't warn for various macro expansions. */
1360 if (from_macro_expansion_at (loc)
1361 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1362 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1363 return;
1364
05b28fd6
MP
1365 /* We do not warn for constants because they are typical of macro
1366 expansions that test for features, sizeof, and similar. */
f479b43d
JM
1367 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1368 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
05b28fd6
MP
1369 return;
1370
1371 /* Don't warn for e.g.
1372 HOST_WIDE_INT n;
1373 ...
1374 if (n == (long) n) ...
1375 */
1376 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1377 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1378 return;
1379
173864e8
MP
1380 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1381 It could be a NaN, and NaN never compares equal to anything, even
1382 itself. */
1383 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1384 return;
1385
05b28fd6
MP
1386 if (operand_equal_p (lhs, rhs, 0))
1387 {
1388 /* Don't warn about array references with constant indices;
1389 these are likely to come from a macro. */
1390 bool found = false;
1391 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1392 &found);
1393 if (found)
1394 return;
1395 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1396 || code == GE_EXPR || code == UNLE_EXPR
1397 || code == UNGE_EXPR || code == UNEQ_EXPR);
1398 if (always_true)
1399 warning_at (loc, OPT_Wtautological_compare,
1400 "self-comparison always evaluates to true");
1401 else
1402 warning_at (loc, OPT_Wtautological_compare,
1403 "self-comparison always evaluates to false");
1404 }
1405}
1406
742938c9
MP
1407/* Warn about logical not used on the left hand side operand of a comparison.
1408 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
59ea0364 1409 Do not warn if RHS is of a boolean type. */
742938c9
MP
1410
1411void
1412warn_logical_not_parentheses (location_t location, enum tree_code code,
59ea0364 1413 tree rhs)
742938c9 1414{
59ea0364
MP
1415 if (TREE_CODE_CLASS (code) != tcc_comparison
1416 || TREE_TYPE (rhs) == NULL_TREE
1417 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
742938c9
MP
1418 return;
1419
7ccb1a11
JJ
1420 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1421 !(x == 0) or !(y != 0). */
1422 if ((code == EQ_EXPR || code == NE_EXPR)
1423 && integer_zerop (rhs))
1424 return;
1425
742938c9
MP
1426 warning_at (location, OPT_Wlogical_not_parentheses,
1427 "logical not is only applied to the left hand side of "
1428 "comparison");
1429}
63a08740 1430
fd4116f4
MLI
1431/* Warn if EXP contains any computations whose results are not used.
1432 Return true if a warning is printed; false otherwise. LOCUS is the
1433 (potential) location of the expression. */
1434
1435bool
1436warn_if_unused_value (const_tree exp, location_t locus)
1437{
1438 restart:
1439 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1440 return false;
1441
1442 /* Don't warn about void constructs. This includes casting to void,
1443 void function calls, and statement expressions with a final cast
1444 to void. */
1445 if (VOID_TYPE_P (TREE_TYPE (exp)))
1446 return false;
1447
1448 if (EXPR_HAS_LOCATION (exp))
1449 locus = EXPR_LOCATION (exp);
1450
1451 switch (TREE_CODE (exp))
1452 {
1453 case PREINCREMENT_EXPR:
1454 case POSTINCREMENT_EXPR:
1455 case PREDECREMENT_EXPR:
1456 case POSTDECREMENT_EXPR:
1457 case MODIFY_EXPR:
1458 case INIT_EXPR:
1459 case TARGET_EXPR:
1460 case CALL_EXPR:
1461 case TRY_CATCH_EXPR:
1462 case WITH_CLEANUP_EXPR:
1463 case EXIT_EXPR:
1464 case VA_ARG_EXPR:
1465 return false;
1466
1467 case BIND_EXPR:
1468 /* For a binding, warn if no side effect within it. */
1469 exp = BIND_EXPR_BODY (exp);
1470 goto restart;
1471
1472 case SAVE_EXPR:
1473 case NON_LVALUE_EXPR:
007a787d 1474 case NOP_EXPR:
fd4116f4
MLI
1475 exp = TREE_OPERAND (exp, 0);
1476 goto restart;
1477
1478 case TRUTH_ORIF_EXPR:
1479 case TRUTH_ANDIF_EXPR:
1480 /* In && or ||, warn if 2nd operand has no side effect. */
1481 exp = TREE_OPERAND (exp, 1);
1482 goto restart;
1483
1484 case COMPOUND_EXPR:
1485 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1486 return true;
1487 /* Let people do `(foo (), 0)' without a warning. */
1488 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1489 return false;
1490 exp = TREE_OPERAND (exp, 1);
1491 goto restart;
1492
1493 case COND_EXPR:
1494 /* If this is an expression with side effects, don't warn; this
1495 case commonly appears in macro expansions. */
1496 if (TREE_SIDE_EFFECTS (exp))
1497 return false;
1498 goto warn;
1499
1500 case INDIRECT_REF:
1501 /* Don't warn about automatic dereferencing of references, since
1502 the user cannot control it. */
1503 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1504 {
1505 exp = TREE_OPERAND (exp, 0);
1506 goto restart;
1507 }
1508 /* Fall through. */
1509
1510 default:
1511 /* Referencing a volatile value is a side effect, so don't warn. */
1512 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1513 && TREE_THIS_VOLATILE (exp))
1514 return false;
1515
1516 /* If this is an expression which has no operands, there is no value
1517 to be unused. There are no such language-independent codes,
1518 but front ends may define such. */
1519 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1520 return false;
1521
1522 warn:
1523 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1524 }
1525}
1526
1527
de9c56a4
RG
1528/* Print a warning about casts that might indicate violation
1529 of strict aliasing rules if -Wstrict-aliasing is used and
3f0a2a47
DM
1530 strict aliasing mode is in effect. OTYPE is the original
1531 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
de9c56a4 1532
79bedddc 1533bool
3f0a2a47 1534strict_aliasing_warning (tree otype, tree type, tree expr)
de9c56a4 1535{
255d3827
RG
1536 /* Strip pointer conversion chains and get to the correct original type. */
1537 STRIP_NOPS (expr);
1538 otype = TREE_TYPE (expr);
1539
ac7ee6ad
RG
1540 if (!(flag_strict_aliasing
1541 && POINTER_TYPE_P (type)
1542 && POINTER_TYPE_P (otype)
1543 && !VOID_TYPE_P (TREE_TYPE (type)))
1544 /* If the type we are casting to is a ref-all pointer
1545 dereferencing it is always valid. */
1546 || TYPE_REF_CAN_ALIAS_ALL (type))
79bedddc
SR
1547 return false;
1548
1549 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
de9c56a4 1550 && (DECL_P (TREE_OPERAND (expr, 0))
79bedddc 1551 || handled_component_p (TREE_OPERAND (expr, 0))))
de9c56a4
RG
1552 {
1553 /* Casting the address of an object to non void pointer. Warn
1554 if the cast breaks type based aliasing. */
79bedddc
SR
1555 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1556 {
1557 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1558 "might break strict-aliasing rules");
1559 return true;
1560 }
de9c56a4
RG
1561 else
1562 {
b8698a0f 1563 /* warn_strict_aliasing >= 3. This includes the default (3).
79bedddc 1564 Only warn if the cast is dereferenced immediately. */
4862826d 1565 alias_set_type set1 =
79bedddc 1566 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
4862826d 1567 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
de9c56a4 1568
4653cae5 1569 if (set1 != set2 && set2 != 0
c06d25bb
RB
1570 && (set1 == 0
1571 || (!alias_set_subset_of (set2, set1)
1572 && !alias_sets_conflict_p (set1, set2))))
79bedddc
SR
1573 {
1574 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1575 "pointer will break strict-aliasing rules");
1576 return true;
1577 }
1578 else if (warn_strict_aliasing == 2
836f7794 1579 && !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1580 {
1581 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1582 "pointer might break strict-aliasing rules");
1583 return true;
1584 }
de9c56a4
RG
1585 }
1586 }
79bedddc
SR
1587 else
1588 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1589 {
1590 /* At this level, warn for any conversions, even if an address is
1591 not taken in the same statement. This will likely produce many
1592 false positives, but could be useful to pinpoint problems that
1593 are not revealed at higher levels. */
4862826d
ILT
1594 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1595 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1596 if (!COMPLETE_TYPE_P (type)
836f7794 1597 || !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1598 {
1599 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1600 "pointer might break strict-aliasing rules");
1601 return true;
1602 }
1603 }
1604
1605 return false;
de9c56a4
RG
1606}
1607
1a4049e7
JJ
1608/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1609 sizeof as last operand of certain builtins. */
1610
1611void
3a785c97 1612sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
9771b263 1613 vec<tree, va_gc> *params, tree *sizeof_arg,
1a4049e7
JJ
1614 bool (*comp_types) (tree, tree))
1615{
1616 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
3a785c97
JJ
1617 bool strop = false, cmp = false;
1618 unsigned int idx = ~0;
1619 location_t loc;
1a4049e7
JJ
1620
1621 if (TREE_CODE (callee) != FUNCTION_DECL
1622 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
9771b263 1623 || vec_safe_length (params) <= 1)
1a4049e7
JJ
1624 return;
1625
1a4049e7
JJ
1626 switch (DECL_FUNCTION_CODE (callee))
1627 {
1628 case BUILT_IN_STRNCMP:
1629 case BUILT_IN_STRNCASECMP:
3a785c97
JJ
1630 cmp = true;
1631 /* FALLTHRU */
1a4049e7 1632 case BUILT_IN_STRNCPY:
3a785c97 1633 case BUILT_IN_STRNCPY_CHK:
1a4049e7 1634 case BUILT_IN_STRNCAT:
3a785c97
JJ
1635 case BUILT_IN_STRNCAT_CHK:
1636 case BUILT_IN_STPNCPY:
1637 case BUILT_IN_STPNCPY_CHK:
1a4049e7
JJ
1638 strop = true;
1639 /* FALLTHRU */
1640 case BUILT_IN_MEMCPY:
3a785c97 1641 case BUILT_IN_MEMCPY_CHK:
1a4049e7 1642 case BUILT_IN_MEMMOVE:
3a785c97 1643 case BUILT_IN_MEMMOVE_CHK:
9771b263 1644 if (params->length () < 3)
3a785c97 1645 return;
9771b263
DN
1646 src = (*params)[1];
1647 dest = (*params)[0];
3a785c97
JJ
1648 idx = 2;
1649 break;
1650 case BUILT_IN_BCOPY:
9771b263 1651 if (params->length () < 3)
3a785c97 1652 return;
9771b263
DN
1653 src = (*params)[0];
1654 dest = (*params)[1];
3a785c97
JJ
1655 idx = 2;
1656 break;
1a4049e7 1657 case BUILT_IN_MEMCMP:
3a785c97 1658 case BUILT_IN_BCMP:
9771b263 1659 if (params->length () < 3)
1a4049e7 1660 return;
9771b263
DN
1661 src = (*params)[1];
1662 dest = (*params)[0];
3a785c97
JJ
1663 idx = 2;
1664 cmp = true;
1a4049e7
JJ
1665 break;
1666 case BUILT_IN_MEMSET:
3a785c97 1667 case BUILT_IN_MEMSET_CHK:
9771b263 1668 if (params->length () < 3)
1a4049e7 1669 return;
9771b263 1670 dest = (*params)[0];
3a785c97
JJ
1671 idx = 2;
1672 break;
1673 case BUILT_IN_BZERO:
9771b263 1674 dest = (*params)[0];
3a785c97 1675 idx = 1;
1a4049e7
JJ
1676 break;
1677 case BUILT_IN_STRNDUP:
9771b263 1678 src = (*params)[0];
1a4049e7 1679 strop = true;
3a785c97
JJ
1680 idx = 1;
1681 break;
1682 case BUILT_IN_MEMCHR:
9771b263 1683 if (params->length () < 3)
3a785c97 1684 return;
9771b263 1685 src = (*params)[0];
3a785c97
JJ
1686 idx = 2;
1687 break;
1688 case BUILT_IN_SNPRINTF:
1689 case BUILT_IN_SNPRINTF_CHK:
1690 case BUILT_IN_VSNPRINTF:
1691 case BUILT_IN_VSNPRINTF_CHK:
9771b263 1692 dest = (*params)[0];
3a785c97
JJ
1693 idx = 1;
1694 strop = true;
1a4049e7
JJ
1695 break;
1696 default:
1697 break;
1698 }
1699
3a785c97
JJ
1700 if (idx >= 3)
1701 return;
1702
1703 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1704 return;
1705
1706 type = TYPE_P (sizeof_arg[idx])
1707 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1708 if (!POINTER_TYPE_P (type))
1709 return;
1710
1a4049e7
JJ
1711 if (dest
1712 && (tem = tree_strip_nop_conversions (dest))
1713 && POINTER_TYPE_P (TREE_TYPE (tem))
1714 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1715 return;
1716
1717 if (src
1718 && (tem = tree_strip_nop_conversions (src))
1719 && POINTER_TYPE_P (TREE_TYPE (tem))
1720 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1721 return;
1722
3a785c97
JJ
1723 loc = sizeof_arg_loc[idx];
1724
1725 if (dest && !cmp)
1a4049e7 1726 {
3a785c97
JJ
1727 if (!TYPE_P (sizeof_arg[idx])
1728 && operand_equal_p (dest, sizeof_arg[idx], 0)
1a4049e7
JJ
1729 && comp_types (TREE_TYPE (dest), type))
1730 {
3a785c97 1731 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
1732 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1733 "argument to %<sizeof%> in %qD call is the same "
1734 "expression as the destination; did you mean to "
1735 "remove the addressof?", callee);
1736 else if ((TYPE_PRECISION (TREE_TYPE (type))
1737 == TYPE_PRECISION (char_type_node))
1738 || strop)
1739 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1740 "argument to %<sizeof%> in %qD call is the same "
1741 "expression as the destination; did you mean to "
1742 "provide an explicit length?", callee);
1743 else
1744 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1745 "argument to %<sizeof%> in %qD call is the same "
1746 "expression as the destination; did you mean to "
1747 "dereference it?", callee);
1748 return;
1749 }
1750
1751 if (POINTER_TYPE_P (TREE_TYPE (dest))
1752 && !strop
1753 && comp_types (TREE_TYPE (dest), type)
1754 && !VOID_TYPE_P (TREE_TYPE (type)))
1755 {
1756 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1757 "argument to %<sizeof%> in %qD call is the same "
1758 "pointer type %qT as the destination; expected %qT "
1759 "or an explicit length", callee, TREE_TYPE (dest),
1760 TREE_TYPE (TREE_TYPE (dest)));
1761 return;
1762 }
1763 }
1764
3a785c97 1765 if (src && !cmp)
1a4049e7 1766 {
3a785c97
JJ
1767 if (!TYPE_P (sizeof_arg[idx])
1768 && operand_equal_p (src, sizeof_arg[idx], 0)
1a4049e7
JJ
1769 && comp_types (TREE_TYPE (src), type))
1770 {
3a785c97 1771 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
1772 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1773 "argument to %<sizeof%> in %qD call is the same "
1774 "expression as the source; did you mean to "
1775 "remove the addressof?", callee);
1776 else if ((TYPE_PRECISION (TREE_TYPE (type))
1777 == TYPE_PRECISION (char_type_node))
1778 || strop)
1779 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1780 "argument to %<sizeof%> in %qD call is the same "
1781 "expression as the source; did you mean to "
1782 "provide an explicit length?", callee);
1783 else
1784 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1785 "argument to %<sizeof%> in %qD call is the same "
1786 "expression as the source; did you mean to "
1787 "dereference it?", callee);
1788 return;
1789 }
1790
1791 if (POINTER_TYPE_P (TREE_TYPE (src))
1792 && !strop
1793 && comp_types (TREE_TYPE (src), type)
1794 && !VOID_TYPE_P (TREE_TYPE (type)))
1795 {
1796 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1797 "argument to %<sizeof%> in %qD call is the same "
1798 "pointer type %qT as the source; expected %qT "
1799 "or an explicit length", callee, TREE_TYPE (src),
1800 TREE_TYPE (TREE_TYPE (src)));
1801 return;
1802 }
1803 }
3a785c97
JJ
1804
1805 if (dest)
1806 {
1807 if (!TYPE_P (sizeof_arg[idx])
1808 && operand_equal_p (dest, sizeof_arg[idx], 0)
1809 && comp_types (TREE_TYPE (dest), type))
1810 {
1811 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1812 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1813 "argument to %<sizeof%> in %qD call is the same "
1814 "expression as the first source; did you mean to "
1815 "remove the addressof?", callee);
1816 else if ((TYPE_PRECISION (TREE_TYPE (type))
1817 == TYPE_PRECISION (char_type_node))
1818 || strop)
1819 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1820 "argument to %<sizeof%> in %qD call is the same "
1821 "expression as the first source; did you mean to "
1822 "provide an explicit length?", callee);
1823 else
1824 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1825 "argument to %<sizeof%> in %qD call is the same "
1826 "expression as the first source; did you mean to "
1827 "dereference it?", callee);
1828 return;
1829 }
1830
1831 if (POINTER_TYPE_P (TREE_TYPE (dest))
1832 && !strop
1833 && comp_types (TREE_TYPE (dest), type)
1834 && !VOID_TYPE_P (TREE_TYPE (type)))
1835 {
1836 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1837 "argument to %<sizeof%> in %qD call is the same "
1838 "pointer type %qT as the first source; expected %qT "
1839 "or an explicit length", callee, TREE_TYPE (dest),
1840 TREE_TYPE (TREE_TYPE (dest)));
1841 return;
1842 }
1843 }
1844
1845 if (src)
1846 {
1847 if (!TYPE_P (sizeof_arg[idx])
1848 && operand_equal_p (src, sizeof_arg[idx], 0)
1849 && comp_types (TREE_TYPE (src), type))
1850 {
1851 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1852 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1853 "argument to %<sizeof%> in %qD call is the same "
1854 "expression as the second source; did you mean to "
1855 "remove the addressof?", callee);
1856 else if ((TYPE_PRECISION (TREE_TYPE (type))
1857 == TYPE_PRECISION (char_type_node))
1858 || strop)
1859 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1860 "argument to %<sizeof%> in %qD call is the same "
1861 "expression as the second source; did you mean to "
1862 "provide an explicit length?", callee);
1863 else
1864 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1865 "argument to %<sizeof%> in %qD call is the same "
1866 "expression as the second source; did you mean to "
1867 "dereference it?", callee);
1868 return;
1869 }
1870
1871 if (POINTER_TYPE_P (TREE_TYPE (src))
1872 && !strop
1873 && comp_types (TREE_TYPE (src), type)
1874 && !VOID_TYPE_P (TREE_TYPE (type)))
1875 {
1876 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1877 "argument to %<sizeof%> in %qD call is the same "
1878 "pointer type %qT as the second source; expected %qT "
1879 "or an explicit length", callee, TREE_TYPE (src),
1880 TREE_TYPE (TREE_TYPE (src)));
1881 return;
1882 }
1883 }
1884
1a4049e7
JJ
1885}
1886
a1e45ff0
DM
1887/* Warn for unlikely, improbable, or stupid DECL declarations
1888 of `main'. */
1889
1890void
1891check_main_parameter_types (tree decl)
1892{
e19a18d4
NF
1893 function_args_iterator iter;
1894 tree type;
a1e45ff0
DM
1895 int argct = 0;
1896
e19a18d4
NF
1897 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1898 {
1899 /* XXX void_type_node belies the abstraction. */
1900 if (type == void_type_node || type == error_mark_node )
1901 break;
1902
f827930a
MP
1903 tree t = type;
1904 if (TYPE_ATOMIC (t))
1905 pedwarn (input_location, OPT_Wmain,
1906 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1907 type, decl);
1908 while (POINTER_TYPE_P (t))
1909 {
1910 t = TREE_TYPE (t);
1911 if (TYPE_ATOMIC (t))
1912 pedwarn (input_location, OPT_Wmain,
1913 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1914 type, decl);
1915 }
1916
e19a18d4
NF
1917 ++argct;
1918 switch (argct)
1919 {
1920 case 1:
1921 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1922 pedwarn (input_location, OPT_Wmain,
1923 "first argument of %q+D should be %<int%>", decl);
1924 break;
1925
1926 case 2:
1927 if (TREE_CODE (type) != POINTER_TYPE
1928 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1929 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1930 != char_type_node))
1931 pedwarn (input_location, OPT_Wmain,
1932 "second argument of %q+D should be %<char **%>", decl);
1933 break;
1934
1935 case 3:
1936 if (TREE_CODE (type) != POINTER_TYPE
1937 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1938 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1939 != char_type_node))
1940 pedwarn (input_location, OPT_Wmain,
1941 "third argument of %q+D should probably be "
1942 "%<char **%>", decl);
1943 break;
1944 }
1945 }
a1e45ff0
DM
1946
1947 /* It is intentional that this message does not mention the third
1948 argument because it's only mentioned in an appendix of the
1949 standard. */
1950 if (argct > 0 && (argct < 2 || argct > 3))
e19a18d4
NF
1951 pedwarn (input_location, OPT_Wmain,
1952 "%q+D takes only zero or two arguments", decl);
38e514c0
MP
1953
1954 if (stdarg_p (TREE_TYPE (decl)))
1955 pedwarn (input_location, OPT_Wmain,
1956 "%q+D declared as variadic function", decl);
a1e45ff0
DM
1957}
1958
0af94e6f
JR
1959/* vector_targets_convertible_p is used for vector pointer types. The
1960 callers perform various checks that the qualifiers are satisfactory,
1961 while OTOH vector_targets_convertible_p ignores the number of elements
1962 in the vectors. That's fine with vector pointers as we can consider,
1963 say, a vector of 8 elements as two consecutive vectors of 4 elements,
1964 and that does not require and conversion of the pointer values.
1965 In contrast, vector_types_convertible_p and
1966 vector_types_compatible_elements_p are used for vector value types. */
f83c7f63
DJ
1967/* True if pointers to distinct types T1 and T2 can be converted to
1968 each other without an explicit cast. Only returns true for opaque
1969 vector types. */
1970bool
1971vector_targets_convertible_p (const_tree t1, const_tree t2)
1972{
31521951 1973 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
b6fc2cdb 1974 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
f83c7f63
DJ
1975 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1976 return true;
1977
1978 return false;
1979}
1980
0af94e6f
JR
1981/* vector_types_convertible_p is used for vector value types.
1982 It could in principle call vector_targets_convertible_p as a subroutine,
1983 but then the check for vector type would be duplicated with its callers,
1984 and also the purpose of vector_targets_convertible_p would become
1985 muddled.
1986 Where vector_types_convertible_p returns true, a conversion might still be
1987 needed to make the types match.
1988 In contrast, vector_targets_convertible_p is used for vector pointer
1989 values, and vector_types_compatible_elements_p is used specifically
1990 in the context for binary operators, as a check if use is possible without
1991 conversion. */
00c8e9f6
MS
1992/* True if vector types T1 and T2 can be converted to each other
1993 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1994 can only be converted with -flax-vector-conversions yet that is not
1995 in effect, emit a note telling the user about that option if such
1996 a note has not previously been emitted. */
1997bool
58f9752a 1998vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
cc27e657 1999{
00c8e9f6 2000 static bool emitted_lax_note = false;
14e765da
JM
2001 bool convertible_lax;
2002
b6fc2cdb 2003 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
14e765da
JM
2004 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2005 return true;
2006
2007 convertible_lax =
2008 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2009 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
a5e0cd1d 2010 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
14e765da
JM
2011 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2012 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
00c8e9f6
MS
2013
2014 if (!convertible_lax || flag_lax_vector_conversions)
2015 return convertible_lax;
2016
2017 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
cf7bc668 2018 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
00c8e9f6
MS
2019 return true;
2020
2021 if (emit_lax_note && !emitted_lax_note)
2022 {
2023 emitted_lax_note = true;
1f5b3869 2024 inform (input_location, "use -flax-vector-conversions to permit "
00c8e9f6
MS
2025 "conversions between vectors with differing "
2026 "element types or numbers of subparts");
2027 }
2028
2029 return false;
cc27e657
PB
2030}
2031
9e1a8dd1
RR
2032/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2033 and have vector types, V0 has the same type as V1, and the number of
2034 elements of V0, V1, MASK is the same.
2035
2036 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2037 called with two arguments. In this case implementation passes the
2038 first argument twice in order to share the same tree code. This fact
2039 could enable the mask-values being twice the vector length. This is
2040 an implementation accident and this semantics is not guaranteed to
2041 the user. */
2042tree
bedc293e
MG
2043c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2044 bool complain)
9e1a8dd1
RR
2045{
2046 tree ret;
2047 bool wrap = true;
2048 bool maybe_const = false;
2049 bool two_arguments = false;
2050
2051 if (v1 == NULL_TREE)
2052 {
2053 two_arguments = true;
2054 v1 = v0;
2055 }
2056
2057 if (v0 == error_mark_node || v1 == error_mark_node
2058 || mask == error_mark_node)
2059 return error_mark_node;
2060
31521951 2061 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
9e1a8dd1 2062 {
bedc293e
MG
2063 if (complain)
2064 error_at (loc, "__builtin_shuffle last argument must "
2065 "be an integer vector");
9e1a8dd1
RR
2066 return error_mark_node;
2067 }
2068
31521951
MP
2069 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2070 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
9e1a8dd1 2071 {
bedc293e
MG
2072 if (complain)
2073 error_at (loc, "__builtin_shuffle arguments must be vectors");
9e1a8dd1
RR
2074 return error_mark_node;
2075 }
2076
2077 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2078 {
bedc293e
MG
2079 if (complain)
2080 error_at (loc, "__builtin_shuffle argument vectors must be of "
2081 "the same type");
9e1a8dd1
RR
2082 return error_mark_node;
2083 }
2084
2085 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2086 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2087 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2088 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2089 {
bedc293e
MG
2090 if (complain)
2091 error_at (loc, "__builtin_shuffle number of elements of the "
2092 "argument vector(s) and the mask vector should "
2093 "be the same");
9e1a8dd1
RR
2094 return error_mark_node;
2095 }
2096
2097 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2098 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2099 {
bedc293e
MG
2100 if (complain)
2101 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2102 "must have the same size as inner type of the mask");
9e1a8dd1
RR
2103 return error_mark_node;
2104 }
2105
2106 if (!c_dialect_cxx ())
2107 {
2108 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2109 v0 = c_fully_fold (v0, false, &maybe_const);
2110 wrap &= maybe_const;
2111
2112 if (two_arguments)
2113 v1 = v0 = save_expr (v0);
2114 else
2115 {
2116 v1 = c_fully_fold (v1, false, &maybe_const);
2117 wrap &= maybe_const;
2118 }
2119
2120 mask = c_fully_fold (mask, false, &maybe_const);
2121 wrap &= maybe_const;
2122 }
bedc293e
MG
2123 else if (two_arguments)
2124 v1 = v0 = save_expr (v0);
9e1a8dd1
RR
2125
2126 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2127
2128 if (!c_dialect_cxx () && !wrap)
2129 ret = c_wrap_maybe_const (ret, true);
2130
2131 return ret;
2132}
2133
828fb3ba
JM
2134/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2135 to integral type. */
2136
2137static tree
2138c_common_get_narrower (tree op, int *unsignedp_ptr)
2139{
2140 op = get_narrower (op, unsignedp_ptr);
2141
2142 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2143 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2144 {
2145 /* C++0x scoped enumerations don't implicitly convert to integral
2146 type; if we stripped an explicit conversion to a larger type we
2147 need to replace it so common_type will still work. */
21fa2faf
RG
2148 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2149 TYPE_UNSIGNED (TREE_TYPE (op)));
828fb3ba
JM
2150 op = fold_convert (type, op);
2151 }
2152 return op;
2153}
2154
6715192c
MLI
2155/* This is a helper function of build_binary_op.
2156
2157 For certain operations if both args were extended from the same
2158 smaller type, do the arithmetic in that type and then extend.
2159
2160 BITWISE indicates a bitwise operation.
2161 For them, this optimization is safe only if
2162 both args are zero-extended or both are sign-extended.
2163 Otherwise, we might change the result.
2164 Eg, (short)-1 | (unsigned short)-1 is (int)-1
b8698a0f 2165 but calculated in (unsigned short) it would be (unsigned short)-1.
6715192c 2166*/
828fb3ba
JM
2167tree
2168shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
6715192c
MLI
2169{
2170 int unsigned0, unsigned1;
2171 tree arg0, arg1;
2172 int uns;
2173 tree type;
2174
2175 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2176 excessive narrowing when we call get_narrower below. For
2177 example, suppose that OP0 is of unsigned int extended
2178 from signed char and that RESULT_TYPE is long long int.
2179 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2180 like
b8698a0f 2181
6715192c
MLI
2182 (long long int) (unsigned int) signed_char
2183
2184 which get_narrower would narrow down to
b8698a0f 2185
6715192c 2186 (unsigned int) signed char
b8698a0f 2187
6715192c
MLI
2188 If we do not cast OP0 first, get_narrower would return
2189 signed_char, which is inconsistent with the case of the
2190 explicit cast. */
2191 op0 = convert (result_type, op0);
2192 op1 = convert (result_type, op1);
2193
828fb3ba
JM
2194 arg0 = c_common_get_narrower (op0, &unsigned0);
2195 arg1 = c_common_get_narrower (op1, &unsigned1);
6715192c
MLI
2196
2197 /* UNS is 1 if the operation to be done is an unsigned one. */
2198 uns = TYPE_UNSIGNED (result_type);
2199
2200 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2201 but it *requires* conversion to FINAL_TYPE. */
b8698a0f 2202
6715192c
MLI
2203 if ((TYPE_PRECISION (TREE_TYPE (op0))
2204 == TYPE_PRECISION (TREE_TYPE (arg0)))
2205 && TREE_TYPE (op0) != result_type)
2206 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2207 if ((TYPE_PRECISION (TREE_TYPE (op1))
2208 == TYPE_PRECISION (TREE_TYPE (arg1)))
2209 && TREE_TYPE (op1) != result_type)
2210 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b8698a0f 2211
6715192c 2212 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
b8698a0f 2213
6715192c
MLI
2214 /* For bitwise operations, signedness of nominal type
2215 does not matter. Consider only how operands were extended. */
2216 if (bitwise)
2217 uns = unsigned0;
b8698a0f 2218
6715192c
MLI
2219 /* Note that in all three cases below we refrain from optimizing
2220 an unsigned operation on sign-extended args.
2221 That would not be valid. */
b8698a0f 2222
6715192c
MLI
2223 /* Both args variable: if both extended in same way
2224 from same width, do it in that width.
2225 Do it unsigned if args were zero-extended. */
2226 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2227 < TYPE_PRECISION (result_type))
2228 && (TYPE_PRECISION (TREE_TYPE (arg1))
2229 == TYPE_PRECISION (TREE_TYPE (arg0)))
2230 && unsigned0 == unsigned1
2231 && (unsigned0 || !uns))
2232 return c_common_signed_or_unsigned_type
2233 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2234
2235 else if (TREE_CODE (arg0) == INTEGER_CST
2236 && (unsigned1 || !uns)
2237 && (TYPE_PRECISION (TREE_TYPE (arg1))
2238 < TYPE_PRECISION (result_type))
2239 && (type
2240 = c_common_signed_or_unsigned_type (unsigned1,
2241 TREE_TYPE (arg1)))
2242 && !POINTER_TYPE_P (type)
2243 && int_fits_type_p (arg0, type))
2244 return type;
2245
2246 else if (TREE_CODE (arg1) == INTEGER_CST
2247 && (unsigned0 || !uns)
2248 && (TYPE_PRECISION (TREE_TYPE (arg0))
2249 < TYPE_PRECISION (result_type))
2250 && (type
2251 = c_common_signed_or_unsigned_type (unsigned0,
2252 TREE_TYPE (arg0)))
2253 && !POINTER_TYPE_P (type)
2254 && int_fits_type_p (arg1, type))
2255 return type;
2256
2257 return result_type;
2258}
2259
7a37fa90
MM
2260/* Returns true iff any integer value of type FROM_TYPE can be represented as
2261 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2262
2263static bool
2264int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2265{
2266 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2267 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2268 REAL_VALUE_TYPE real_low_bound =
2269 real_value_from_int_cst (0, type_low_bound);
2270 REAL_VALUE_TYPE real_high_bound =
2271 real_value_from_int_cst (0, type_high_bound);
2272
2273 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2274 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2275}
2276
2277/* Checks if expression EXPR of complex/real/integer type cannot be converted
2278 to the complex/real/integer type TYPE. Function returns non-zero when:
68fca595
MP
2279 * EXPR is a constant which cannot be exactly converted to TYPE.
2280 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
7a37fa90
MM
2281 for EXPR type and TYPE being both integers or both real, or both
2282 complex.
2283 * EXPR is not a constant of complex type and TYPE is a real or
2284 an integer.
68fca595
MP
2285 * EXPR is not a constant of real type and TYPE is an integer.
2286 * EXPR is not a constant of integer type which cannot be
2287 exactly converted to real type.
7a37fa90 2288
0e3a99ae 2289 Function allows conversions between types of different signedness and
49b0aa18 2290 can return SAFE_CONVERSION (zero) in that case. Function can produce
7a37fa90
MM
2291 signedness warnings if PRODUCE_WARNS is true.
2292
2293 Function allows conversions from complex constants to non-complex types,
2294 provided that imaginary part is zero and real part can be safely converted
2295 to TYPE. */
68fca595 2296
49b0aa18 2297enum conversion_safety
68fca595 2298unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
422c3a54 2299{
49b0aa18 2300 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
374035cb 2301 tree expr_type = TREE_TYPE (expr);
68fca595 2302 loc = expansion_point_location_if_in_system_header (loc);
422c3a54 2303
0e3a99ae 2304 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
0011dedb 2305 {
7a37fa90
MM
2306 /* If type is complex, we are interested in compatibility with
2307 underlying type. */
2308 if (TREE_CODE (type) == COMPLEX_TYPE)
2309 type = TREE_TYPE (type);
2310
422c3a54 2311 /* Warn for real constant that is not an exact integer converted
0e3a99ae 2312 to integer type. */
374035cb 2313 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2314 && TREE_CODE (type) == INTEGER_TYPE)
2315 {
2316 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
49b0aa18 2317 give_warning = UNSAFE_REAL;
0e3a99ae 2318 }
91c41804 2319 /* Warn for an integer constant that does not fit into integer type. */
374035cb 2320 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2321 && TREE_CODE (type) == INTEGER_TYPE
2322 && !int_fits_type_p (expr, type))
2323 {
2324 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
374035cb 2325 && tree_int_cst_sgn (expr) < 0)
0e3a99ae
AS
2326 {
2327 if (produce_warns)
2328 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2329 " implicitly converted to unsigned type");
2330 }
2331 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2332 {
2333 if (produce_warns)
2334 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2335 " constant value to negative integer");
2336 }
7060db96 2337 else
49b0aa18 2338 give_warning = UNSAFE_OTHER;
0e3a99ae 2339 }
422c3a54 2340 else if (TREE_CODE (type) == REAL_TYPE)
0e3a99ae
AS
2341 {
2342 /* Warn for an integer constant that does not fit into real type. */
2343 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2344 {
2345 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2346 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2347 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2348 }
2349 /* Warn for a real constant that does not fit into a smaller
2350 real type. */
2351 else if (TREE_CODE (expr_type) == REAL_TYPE
2352 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2353 {
2354 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2355 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2356 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2357 }
2358 }
2359 }
7a37fa90
MM
2360
2361 else if (TREE_CODE (expr) == COMPLEX_CST)
2362 {
2363 tree imag_part = TREE_IMAGPART (expr);
2364 /* Conversion from complex constant with zero imaginary part,
2365 perform check for conversion of real part. */
2366 if ((TREE_CODE (imag_part) == REAL_CST
2367 && real_zerop (imag_part))
2368 || (TREE_CODE (imag_part) == INTEGER_CST
2369 && integer_zerop (imag_part)))
2370 /* Note: in this branch we use recursive call to unsafe_conversion_p
2371 with different type of EXPR, but it is still safe, because when EXPR
2372 is a constant, it's type is not used in text of generated warnings
2373 (otherwise they could sound misleading). */
2374 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2375 produce_warns);
2376 /* Conversion from complex constant with non-zero imaginary part. */
2377 else
2378 {
2379 /* Conversion to complex type.
2380 Perform checks for both real and imaginary parts. */
2381 if (TREE_CODE (type) == COMPLEX_TYPE)
2382 {
2383 /* Unfortunately, produce_warns must be false in two subsequent
2384 calls of unsafe_conversion_p, because otherwise we could
2385 produce strange "double" warnings, if both real and imaginary
2386 parts have conversion problems related to signedness.
2387
2388 For example:
2389 int32_t _Complex a = 0x80000000 + 0x80000000i;
2390
2391 Possible solution: add a separate function for checking
2392 constants and combine result of two calls appropriately. */
2393 enum conversion_safety re_safety =
2394 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2395 enum conversion_safety im_safety =
2396 unsafe_conversion_p (loc, type, imag_part, false);
2397
2398 /* Merge the results into appropriate single warning. */
2399
2400 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2401 if (re_safety == im_safety)
2402 give_warning = re_safety;
2403 else if (!re_safety && im_safety)
2404 give_warning = im_safety;
2405 else if (re_safety && !im_safety)
2406 give_warning = re_safety;
2407 else
2408 give_warning = UNSAFE_OTHER;
2409 }
2410 /* Warn about conversion from complex to real or integer type. */
2411 else
2412 give_warning = UNSAFE_IMAGINARY;
2413 }
2414 }
2415
2416 /* Checks for remaining case: EXPR is not constant. */
0e3a99ae
AS
2417 else
2418 {
422c3a54 2419 /* Warn for real types converted to integer types. */
6715192c 2420 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae 2421 && TREE_CODE (type) == INTEGER_TYPE)
49b0aa18 2422 give_warning = UNSAFE_REAL;
422c3a54 2423
6715192c 2424 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2425 && TREE_CODE (type) == INTEGER_TYPE)
2426 {
cfdaefec 2427 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
c00e8b06 2428 expr = get_unwidened (expr, 0);
6715192c 2429 expr_type = TREE_TYPE (expr);
cfdaefec 2430
6715192c 2431 /* Don't warn for short y; short x = ((int)y & 0xff); */
b8698a0f 2432 if (TREE_CODE (expr) == BIT_AND_EXPR
0e3a99ae 2433 || TREE_CODE (expr) == BIT_IOR_EXPR
6715192c
MLI
2434 || TREE_CODE (expr) == BIT_XOR_EXPR)
2435 {
374035cb
MLI
2436 /* If both args were extended from a shortest type,
2437 use that type if that is safe. */
b8698a0f
L
2438 expr_type = shorten_binary_op (expr_type,
2439 TREE_OPERAND (expr, 0),
2440 TREE_OPERAND (expr, 1),
6715192c
MLI
2441 /* bitwise */1);
2442
6715192c
MLI
2443 if (TREE_CODE (expr) == BIT_AND_EXPR)
2444 {
2445 tree op0 = TREE_OPERAND (expr, 0);
2446 tree op1 = TREE_OPERAND (expr, 1);
9c591bd0
MLI
2447 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2448 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2449
2450 /* If one of the operands is a non-negative constant
2451 that fits in the target type, then the type of the
2452 other operand does not matter. */
6715192c
MLI
2453 if ((TREE_CODE (op0) == INTEGER_CST
2454 && int_fits_type_p (op0, c_common_signed_type (type))
2455 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2456 || (TREE_CODE (op1) == INTEGER_CST
374035cb 2457 && int_fits_type_p (op1, c_common_signed_type (type))
b8698a0f 2458 && int_fits_type_p (op1,
374035cb 2459 c_common_unsigned_type (type))))
49b0aa18 2460 return SAFE_CONVERSION;
9c591bd0
MLI
2461 /* If constant is unsigned and fits in the target
2462 type, then the result will also fit. */
2463 else if ((TREE_CODE (op0) == INTEGER_CST
b8698a0f 2464 && unsigned0
9c591bd0
MLI
2465 && int_fits_type_p (op0, type))
2466 || (TREE_CODE (op1) == INTEGER_CST
2467 && unsigned1
2468 && int_fits_type_p (op1, type)))
49b0aa18 2469 return SAFE_CONVERSION;
6715192c
MLI
2470 }
2471 }
0e3a99ae 2472 /* Warn for integer types converted to smaller integer types. */
b8698a0f 2473 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2474 give_warning = UNSAFE_OTHER;
7060db96
MLI
2475
2476 /* When they are the same width but different signedness,
2477 then the value may change. */
0e3a99ae 2478 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
6715192c 2479 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7060db96
MLI
2480 /* Even when converted to a bigger type, if the type is
2481 unsigned but expr is signed, then negative values
2482 will be changed. */
0e3a99ae
AS
2483 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2484 && produce_warns)
6312e84d
MLI
2485 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2486 "may change the sign of the result",
2487 type, expr_type);
0e3a99ae 2488 }
422c3a54
MLI
2489
2490 /* Warn for integer types converted to real types if and only if
0e3a99ae
AS
2491 all the range of values of the integer type cannot be
2492 represented by the real type. */
6715192c 2493 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2494 && TREE_CODE (type) == REAL_TYPE)
2495 {
58076e21
MLI
2496 /* Don't warn about char y = 0xff; float x = (int) y; */
2497 expr = get_unwidened (expr, 0);
2498 expr_type = TREE_TYPE (expr);
2499
7a37fa90 2500 if (!int_safely_convertible_to_real_p (expr_type, type))
49b0aa18 2501 give_warning = UNSAFE_OTHER;
0e3a99ae 2502 }
422c3a54
MLI
2503
2504 /* Warn for real types converted to smaller real types. */
6715192c 2505 else if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2506 && TREE_CODE (type) == REAL_TYPE
2507 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2508 give_warning = UNSAFE_REAL;
7a37fa90
MM
2509
2510 /* Check conversion between two complex types. */
2511 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2512 && TREE_CODE (type) == COMPLEX_TYPE)
2513 {
2514 /* Extract underlying types (i.e., type of real and imaginary
2515 parts) of expr_type and type. */
2516 tree from_type = TREE_TYPE (expr_type);
2517 tree to_type = TREE_TYPE (type);
2518
2519 /* Warn for real types converted to integer types. */
2520 if (TREE_CODE (from_type) == REAL_TYPE
2521 && TREE_CODE (to_type) == INTEGER_TYPE)
2522 give_warning = UNSAFE_REAL;
2523
2524 /* Warn for real types converted to smaller real types. */
2525 else if (TREE_CODE (from_type) == REAL_TYPE
2526 && TREE_CODE (to_type) == REAL_TYPE
2527 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2528 give_warning = UNSAFE_REAL;
2529
2530 /* Check conversion for complex integer types. Here implementation
2531 is simpler than for real-domain integers because it does not
2532 involve sophisticated cases, such as bitmasks, casts, etc. */
2533 else if (TREE_CODE (from_type) == INTEGER_TYPE
2534 && TREE_CODE (to_type) == INTEGER_TYPE)
2535 {
2536 /* Warn for integer types converted to smaller integer types. */
2537 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2538 give_warning = UNSAFE_OTHER;
2539
2540 /* Check for different signedness, see case for real-domain
2541 integers (above) for a more detailed comment. */
2542 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2543 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2544 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2545 && produce_warns)
2546 warning_at (loc, OPT_Wsign_conversion,
2547 "conversion to %qT from %qT "
2548 "may change the sign of the result",
2549 type, expr_type);
2550 }
2551 else if (TREE_CODE (from_type) == INTEGER_TYPE
2552 && TREE_CODE (to_type) == REAL_TYPE
2553 && !int_safely_convertible_to_real_p (from_type, to_type))
2554 give_warning = UNSAFE_OTHER;
2555 }
2556
2557 /* Warn for complex types converted to real or integer types. */
2558 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2559 && TREE_CODE (type) != COMPLEX_TYPE)
2560 give_warning = UNSAFE_IMAGINARY;
0e3a99ae
AS
2561 }
2562
2563 return give_warning;
2564}
2565
2566/* Warns if the conversion of EXPR to TYPE may alter a value.
2567 This is a helper function for warnings_for_convert_and_check. */
2568
2569static void
68fca595 2570conversion_warning (location_t loc, tree type, tree expr)
0e3a99ae 2571{
0e3a99ae 2572 tree expr_type = TREE_TYPE (expr);
49b0aa18 2573 enum conversion_safety conversion_kind;
422c3a54 2574
49b0aa18 2575 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
0e3a99ae 2576 return;
422c3a54 2577
66f20604
MP
2578 /* This may happen, because for LHS op= RHS we preevaluate
2579 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2580 means we could no longer see the code of the EXPR. */
2581 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2582 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2583 if (TREE_CODE (expr) == SAVE_EXPR)
2584 expr = TREE_OPERAND (expr, 0);
2585
0e3a99ae
AS
2586 switch (TREE_CODE (expr))
2587 {
2588 case EQ_EXPR:
2589 case NE_EXPR:
2590 case LE_EXPR:
2591 case GE_EXPR:
2592 case LT_EXPR:
2593 case GT_EXPR:
2594 case TRUTH_ANDIF_EXPR:
2595 case TRUTH_ORIF_EXPR:
2596 case TRUTH_AND_EXPR:
2597 case TRUTH_OR_EXPR:
2598 case TRUTH_XOR_EXPR:
2599 case TRUTH_NOT_EXPR:
2600 /* Conversion from boolean to a signed:1 bit-field (which only
2601 can hold the values 0 and -1) doesn't lose information - but
2602 it does change the value. */
2603 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2604 warning_at (loc, OPT_Wconversion,
2605 "conversion to %qT from boolean expression", type);
2606 return;
2607
2608 case REAL_CST:
2609 case INTEGER_CST:
7a37fa90 2610 case COMPLEX_CST:
68fca595 2611 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
2612 if (conversion_kind == UNSAFE_REAL)
2613 warning_at (loc, OPT_Wfloat_conversion,
2614 "conversion to %qT alters %qT constant value",
2615 type, expr_type);
2616 else if (conversion_kind)
0e3a99ae
AS
2617 warning_at (loc, OPT_Wconversion,
2618 "conversion to %qT alters %qT constant value",
2619 type, expr_type);
2620 return;
2621
2622 case COND_EXPR:
2623 {
3f46d6a5
MLI
2624 /* In case of COND_EXPR, we do not care about the type of
2625 COND_EXPR, only about the conversion of each operand. */
2626 tree op1 = TREE_OPERAND (expr, 1);
2627 tree op2 = TREE_OPERAND (expr, 2);
2628
68fca595
MP
2629 conversion_warning (loc, type, op1);
2630 conversion_warning (loc, type, op2);
3f46d6a5 2631 return;
0e3a99ae
AS
2632 }
2633
2634 default: /* 'expr' is not a constant. */
68fca595 2635 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
2636 if (conversion_kind == UNSAFE_REAL)
2637 warning_at (loc, OPT_Wfloat_conversion,
2638 "conversion to %qT from %qT may alter its value",
2639 type, expr_type);
7a37fa90
MM
2640 else if (conversion_kind == UNSAFE_IMAGINARY)
2641 warning_at (loc, OPT_Wconversion,
2642 "conversion to %qT from %qT discards imaginary component",
2643 type, expr_type);
49b0aa18 2644 else if (conversion_kind)
0e3a99ae 2645 warning_at (loc, OPT_Wconversion,
6312e84d
MLI
2646 "conversion to %qT from %qT may alter its value",
2647 type, expr_type);
422c3a54
MLI
2648 }
2649}
2650
07231d4f
MLI
2651/* Produce warnings after a conversion. RESULT is the result of
2652 converting EXPR to TYPE. This is a helper function for
2653 convert_and_check and cp_convert_and_check. */
d74154d5 2654
07231d4f 2655void
68fca595
MP
2656warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2657 tree result)
d74154d5 2658{
68fca595 2659 loc = expansion_point_location_if_in_system_header (loc);
5a3c9cf2 2660
91c41804
RS
2661 if (TREE_CODE (expr) == INTEGER_CST
2662 && (TREE_CODE (type) == INTEGER_TYPE
2663 || TREE_CODE (type) == ENUMERAL_TYPE)
2664 && !int_fits_type_p (expr, type))
2665 {
422c3a54
MLI
2666 /* Do not diagnose overflow in a constant expression merely
2667 because a conversion overflowed. */
91c41804 2668 if (TREE_OVERFLOW (result))
d95787e6
RS
2669 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2670
91c41804 2671 if (TYPE_UNSIGNED (type))
422c3a54 2672 {
91c41804
RS
2673 /* This detects cases like converting -129 or 256 to
2674 unsigned char. */
2675 if (!int_fits_type_p (expr, c_common_signed_type (type)))
5a3c9cf2
PC
2676 warning_at (loc, OPT_Woverflow,
2677 "large integer implicitly truncated to unsigned type");
7060db96 2678 else
68fca595 2679 conversion_warning (loc, type, expr);
91c41804 2680 }
b8698a0f 2681 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
68fca595 2682 warning_at (loc, OPT_Woverflow,
f73fe417
MLI
2683 "overflow in implicit constant conversion");
2684 /* No warning for converting 0x80000000 to int. */
2685 else if (pedantic
2686 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2687 || TYPE_PRECISION (TREE_TYPE (expr))
2688 != TYPE_PRECISION (type)))
5a3c9cf2
PC
2689 warning_at (loc, OPT_Woverflow,
2690 "overflow in implicit constant conversion");
f73fe417 2691
7060db96 2692 else
68fca595 2693 conversion_warning (loc, type, expr);
d74154d5 2694 }
ab22c1fa
CF
2695 else if ((TREE_CODE (result) == INTEGER_CST
2696 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
5a3c9cf2
PC
2697 warning_at (loc, OPT_Woverflow,
2698 "overflow in implicit constant conversion");
7060db96 2699 else
68fca595 2700 conversion_warning (loc, type, expr);
07231d4f
MLI
2701}
2702
2703
2704/* Convert EXPR to TYPE, warning about conversion problems with constants.
2705 Invoke this function on every expression that is converted implicitly,
2706 i.e. because of language rules and not because of an explicit cast. */
2707
2708tree
68fca595 2709convert_and_check (location_t loc, tree type, tree expr)
07231d4f
MLI
2710{
2711 tree result;
8ce94e44
JM
2712 tree expr_for_warning;
2713
2714 /* Convert from a value with possible excess precision rather than
2715 via the semantic type, but do not warn about values not fitting
2716 exactly in the semantic type. */
2717 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2718 {
2719 tree orig_type = TREE_TYPE (expr);
2720 expr = TREE_OPERAND (expr, 0);
2721 expr_for_warning = convert (orig_type, expr);
2722 if (orig_type == type)
2723 return expr_for_warning;
2724 }
2725 else
2726 expr_for_warning = expr;
07231d4f
MLI
2727
2728 if (TREE_TYPE (expr) == type)
2729 return expr;
b8698a0f 2730
07231d4f
MLI
2731 result = convert (type, expr);
2732
7d882b83
ILT
2733 if (c_inhibit_evaluation_warnings == 0
2734 && !TREE_OVERFLOW_P (expr)
2735 && result != error_mark_node)
68fca595 2736 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
07231d4f 2737
91c41804 2738 return result;
96571883
BK
2739}
2740\f
235cfbc4
BS
2741/* A node in a list that describes references to variables (EXPR), which are
2742 either read accesses if WRITER is zero, or write accesses, in which case
2743 WRITER is the parent of EXPR. */
2744struct tlist
2745{
2746 struct tlist *next;
2747 tree expr, writer;
2748};
2749
2750/* Used to implement a cache the results of a call to verify_tree. We only
2751 use this for SAVE_EXPRs. */
2752struct tlist_cache
2753{
2754 struct tlist_cache *next;
2755 struct tlist *cache_before_sp;
2756 struct tlist *cache_after_sp;
2757 tree expr;
2683ed8d
BS
2758};
2759
235cfbc4
BS
2760/* Obstack to use when allocating tlist structures, and corresponding
2761 firstobj. */
2762static struct obstack tlist_obstack;
2763static char *tlist_firstobj = 0;
2764
2765/* Keep track of the identifiers we've warned about, so we can avoid duplicate
2766 warnings. */
2767static struct tlist *warned_ids;
2768/* SAVE_EXPRs need special treatment. We process them only once and then
2769 cache the results. */
2770static struct tlist_cache *save_expr_cache;
2771
35b1a6fa
AJ
2772static void add_tlist (struct tlist **, struct tlist *, tree, int);
2773static void merge_tlist (struct tlist **, struct tlist *, int);
2774static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2775static int warning_candidate_p (tree);
1e4ae551 2776static bool candidate_equal_p (const_tree, const_tree);
35b1a6fa
AJ
2777static void warn_for_collisions (struct tlist *);
2778static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2779static struct tlist *new_tlist (struct tlist *, tree, tree);
2683ed8d 2780
235cfbc4
BS
2781/* Create a new struct tlist and fill in its fields. */
2782static struct tlist *
35b1a6fa 2783new_tlist (struct tlist *next, tree t, tree writer)
235cfbc4
BS
2784{
2785 struct tlist *l;
5d038c4c 2786 l = XOBNEW (&tlist_obstack, struct tlist);
235cfbc4
BS
2787 l->next = next;
2788 l->expr = t;
2789 l->writer = writer;
2790 return l;
2791}
2792
2793/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2794 is nonnull, we ignore any node we find which has a writer equal to it. */
2795
2796static void
35b1a6fa 2797add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
235cfbc4
BS
2798{
2799 while (add)
2800 {
2801 struct tlist *next = add->next;
3f75a254 2802 if (!copy)
235cfbc4 2803 add->next = *to;
1e4ae551 2804 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
235cfbc4
BS
2805 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2806 add = next;
2807 }
2808}
2809
2810/* Merge the nodes of ADD into TO. This merging process is done so that for
2811 each variable that already exists in TO, no new node is added; however if
2812 there is a write access recorded in ADD, and an occurrence on TO is only
2813 a read access, then the occurrence in TO will be modified to record the
2814 write. */
2683ed8d
BS
2815
2816static void
35b1a6fa 2817merge_tlist (struct tlist **to, struct tlist *add, int copy)
235cfbc4
BS
2818{
2819 struct tlist **end = to;
2820
2821 while (*end)
2822 end = &(*end)->next;
2823
2824 while (add)
2825 {
2826 int found = 0;
2827 struct tlist *tmp2;
2828 struct tlist *next = add->next;
2829
2830 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1e4ae551 2831 if (candidate_equal_p (tmp2->expr, add->expr))
235cfbc4
BS
2832 {
2833 found = 1;
3f75a254 2834 if (!tmp2->writer)
235cfbc4
BS
2835 tmp2->writer = add->writer;
2836 }
3f75a254 2837 if (!found)
235cfbc4 2838 {
c2bf53a1 2839 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
235cfbc4
BS
2840 end = &(*end)->next;
2841 *end = 0;
2842 }
2843 add = next;
2844 }
2845}
2846
2847/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2848 references in list LIST conflict with it, excluding reads if ONLY writers
2849 is nonzero. */
2850
2851static void
35b1a6fa
AJ
2852warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2853 int only_writes)
235cfbc4
BS
2854{
2855 struct tlist *tmp;
2856
2857 /* Avoid duplicate warnings. */
2858 for (tmp = warned_ids; tmp; tmp = tmp->next)
1e4ae551 2859 if (candidate_equal_p (tmp->expr, written))
235cfbc4
BS
2860 return;
2861
2862 while (list)
2863 {
1e4ae551
MLI
2864 if (candidate_equal_p (list->expr, written)
2865 && !candidate_equal_p (list->writer, writer)
2866 && (!only_writes || list->writer))
235cfbc4
BS
2867 {
2868 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
8400e75e 2869 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
ca085fd7
MLI
2870 OPT_Wsequence_point, "operation on %qE may be undefined",
2871 list->expr);
235cfbc4
BS
2872 }
2873 list = list->next;
2874 }
2875}
2876
2877/* Given a list LIST of references to variables, find whether any of these
2878 can cause conflicts due to missing sequence points. */
2879
2880static void
35b1a6fa 2881warn_for_collisions (struct tlist *list)
235cfbc4
BS
2882{
2883 struct tlist *tmp;
35b1a6fa 2884
235cfbc4
BS
2885 for (tmp = list; tmp; tmp = tmp->next)
2886 {
2887 if (tmp->writer)
2888 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2889 }
2890}
2891
684d9f3b 2892/* Return nonzero if X is a tree that can be verified by the sequence point
235cfbc4
BS
2893 warnings. */
2894static int
35b1a6fa 2895warning_candidate_p (tree x)
2683ed8d 2896{
07078664
JJ
2897 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2898 return 0;
2899
92e948a8
NF
2900 if (TREE_CODE (x) == BLOCK)
2901 return 0;
2902
07078664 2903 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1e4ae551 2904 (lvalue_p) crash on TRY/CATCH. */
07078664
JJ
2905 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2906 return 0;
2907
2908 if (!lvalue_p (x))
2909 return 0;
2910
2911 /* No point to track non-const calls, they will never satisfy
2912 operand_equal_p. */
2913 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2914 return 0;
2915
2916 if (TREE_CODE (x) == STRING_CST)
2917 return 0;
2918
2919 return 1;
1e4ae551
MLI
2920}
2921
2922/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2923static bool
2924candidate_equal_p (const_tree x, const_tree y)
2925{
2926 return (x == y) || (x && y && operand_equal_p (x, y, 0));
235cfbc4 2927}
2683ed8d 2928
235cfbc4
BS
2929/* Walk the tree X, and record accesses to variables. If X is written by the
2930 parent tree, WRITER is the parent.
2931 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2932 expression or its only operand forces a sequence point, then everything up
2933 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2934 in PNO_SP.
2935 Once we return, we will have emitted warnings if any subexpression before
2936 such a sequence point could be undefined. On a higher level, however, the
2937 sequence point may not be relevant, and we'll merge the two lists.
2938
2939 Example: (b++, a) + b;
2940 The call that processes the COMPOUND_EXPR will store the increment of B
2941 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2942 processes the PLUS_EXPR will need to merge the two lists so that
2943 eventually, all accesses end up on the same list (and we'll warn about the
2944 unordered subexpressions b++ and b.
2945
2946 A note on merging. If we modify the former example so that our expression
2947 becomes
2948 (b++, b) + a
2949 care must be taken not simply to add all three expressions into the final
2950 PNO_SP list. The function merge_tlist takes care of that by merging the
2951 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2952 way, so that no more than one access to B is recorded. */
2683ed8d 2953
235cfbc4 2954static void
35b1a6fa
AJ
2955verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2956 tree writer)
235cfbc4
BS
2957{
2958 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2959 enum tree_code code;
6615c446 2960 enum tree_code_class cl;
2683ed8d 2961
f9e1917e
JM
2962 /* X may be NULL if it is the operand of an empty statement expression
2963 ({ }). */
2964 if (x == NULL)
2965 return;
2966
235cfbc4
BS
2967 restart:
2968 code = TREE_CODE (x);
e3a64162 2969 cl = TREE_CODE_CLASS (code);
2683ed8d 2970
235cfbc4 2971 if (warning_candidate_p (x))
1e4ae551 2972 *pno_sp = new_tlist (*pno_sp, x, writer);
235cfbc4
BS
2973
2974 switch (code)
2975 {
52a84e42 2976 case CONSTRUCTOR:
f7716d57 2977 case SIZEOF_EXPR:
52a84e42
BS
2978 return;
2979
235cfbc4
BS
2980 case COMPOUND_EXPR:
2981 case TRUTH_ANDIF_EXPR:
2982 case TRUTH_ORIF_EXPR:
2983 tmp_before = tmp_nosp = tmp_list3 = 0;
2984 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2985 warn_for_collisions (tmp_nosp);
2986 merge_tlist (pbefore_sp, tmp_before, 0);
2987 merge_tlist (pbefore_sp, tmp_nosp, 0);
2988 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2989 merge_tlist (pbefore_sp, tmp_list3, 0);
2990 return;
2991
2992 case COND_EXPR:
2993 tmp_before = tmp_list2 = 0;
2994 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2995 warn_for_collisions (tmp_list2);
2996 merge_tlist (pbefore_sp, tmp_before, 0);
c2bf53a1 2997 merge_tlist (pbefore_sp, tmp_list2, 0);
235cfbc4
BS
2998
2999 tmp_list3 = tmp_nosp = 0;
3000 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3001 warn_for_collisions (tmp_nosp);
3002 merge_tlist (pbefore_sp, tmp_list3, 0);
3003
3004 tmp_list3 = tmp_list2 = 0;
3005 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3006 warn_for_collisions (tmp_list2);
3007 merge_tlist (pbefore_sp, tmp_list3, 0);
3008 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3009 two first, to avoid warning for (a ? b++ : b++). */
3010 merge_tlist (&tmp_nosp, tmp_list2, 0);
3011 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3012 return;
3013
2683ed8d
BS
3014 case PREDECREMENT_EXPR:
3015 case PREINCREMENT_EXPR:
3016 case POSTDECREMENT_EXPR:
3017 case POSTINCREMENT_EXPR:
235cfbc4
BS
3018 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3019 return;
3020
3021 case MODIFY_EXPR:
3022 tmp_before = tmp_nosp = tmp_list3 = 0;
3023 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3024 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3025 /* Expressions inside the LHS are not ordered wrt. the sequence points
3026 in the RHS. Example:
3027 *a = (a++, 2)
3028 Despite the fact that the modification of "a" is in the before_sp
3029 list (tmp_before), it conflicts with the use of "a" in the LHS.
3030 We can handle this by adding the contents of tmp_list3
3031 to those of tmp_before, and redoing the collision warnings for that
3032 list. */
3033 add_tlist (&tmp_before, tmp_list3, x, 1);
3034 warn_for_collisions (tmp_before);
3035 /* Exclude the LHS itself here; we first have to merge it into the
3036 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3037 didn't exclude the LHS, we'd get it twice, once as a read and once
3038 as a write. */
3039 add_tlist (pno_sp, tmp_list3, x, 0);
3040 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3041
3042 merge_tlist (pbefore_sp, tmp_before, 0);
3043 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3044 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3045 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3046 return;
2683ed8d
BS
3047
3048 case CALL_EXPR:
235cfbc4
BS
3049 /* We need to warn about conflicts among arguments and conflicts between
3050 args and the function address. Side effects of the function address,
3051 however, are not ordered by the sequence point of the call. */
5039610b
SL
3052 {
3053 call_expr_arg_iterator iter;
3054 tree arg;
b8698a0f 3055 tmp_before = tmp_nosp = 0;
5039610b
SL
3056 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3057 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3058 {
3059 tmp_list2 = tmp_list3 = 0;
3060 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3061 merge_tlist (&tmp_list3, tmp_list2, 0);
3062 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3063 }
3064 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3065 warn_for_collisions (tmp_before);
3066 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3067 return;
3068 }
2683ed8d
BS
3069
3070 case TREE_LIST:
3071 /* Scan all the list, e.g. indices of multi dimensional array. */
3072 while (x)
3073 {
235cfbc4
BS
3074 tmp_before = tmp_nosp = 0;
3075 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3076 merge_tlist (&tmp_nosp, tmp_before, 0);
3077 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2683ed8d
BS
3078 x = TREE_CHAIN (x);
3079 }
235cfbc4 3080 return;
2683ed8d 3081
235cfbc4
BS
3082 case SAVE_EXPR:
3083 {
3084 struct tlist_cache *t;
3085 for (t = save_expr_cache; t; t = t->next)
1e4ae551 3086 if (candidate_equal_p (t->expr, x))
235cfbc4 3087 break;
2683ed8d 3088
3f75a254 3089 if (!t)
2683ed8d 3090 {
5d038c4c 3091 t = XOBNEW (&tlist_obstack, struct tlist_cache);
235cfbc4
BS
3092 t->next = save_expr_cache;
3093 t->expr = x;
3094 save_expr_cache = t;
3095
3096 tmp_before = tmp_nosp = 0;
3097 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3098 warn_for_collisions (tmp_nosp);
3099
3100 tmp_list3 = 0;
c2bf53a1 3101 merge_tlist (&tmp_list3, tmp_nosp, 0);
235cfbc4
BS
3102 t->cache_before_sp = tmp_before;
3103 t->cache_after_sp = tmp_list3;
2683ed8d 3104 }
235cfbc4
BS
3105 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3106 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3107 return;
3108 }
2683ed8d 3109
528c22f4
MLI
3110 case ADDR_EXPR:
3111 x = TREE_OPERAND (x, 0);
3112 if (DECL_P (x))
3113 return;
3114 writer = 0;
3115 goto restart;
3116
6615c446
JO
3117 default:
3118 /* For other expressions, simply recurse on their operands.
c22cacf3 3119 Manual tail recursion for unary expressions.
6615c446
JO
3120 Other non-expressions need not be processed. */
3121 if (cl == tcc_unary)
3122 {
6615c446
JO
3123 x = TREE_OPERAND (x, 0);
3124 writer = 0;
3125 goto restart;
3126 }
3127 else if (IS_EXPR_CODE_CLASS (cl))
3128 {
3129 int lp;
5039610b 3130 int max = TREE_OPERAND_LENGTH (x);
6615c446
JO
3131 for (lp = 0; lp < max; lp++)
3132 {
3133 tmp_before = tmp_nosp = 0;
3134 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3135 merge_tlist (&tmp_nosp, tmp_before, 0);
3136 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3137 }
3138 }
3139 return;
2683ed8d 3140 }
2683ed8d
BS
3141}
3142
8d9afc4e 3143/* Try to warn for undefined behavior in EXPR due to missing sequence
2683ed8d
BS
3144 points. */
3145
24e47c76 3146DEBUG_FUNCTION void
35b1a6fa 3147verify_sequence_points (tree expr)
2683ed8d 3148{
235cfbc4 3149 struct tlist *before_sp = 0, *after_sp = 0;
2683ed8d 3150
235cfbc4
BS
3151 warned_ids = 0;
3152 save_expr_cache = 0;
3153 if (tlist_firstobj == 0)
2683ed8d 3154 {
235cfbc4 3155 gcc_obstack_init (&tlist_obstack);
28dab132 3156 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2683ed8d
BS
3157 }
3158
235cfbc4
BS
3159 verify_tree (expr, &before_sp, &after_sp, 0);
3160 warn_for_collisions (after_sp);
3161 obstack_free (&tlist_obstack, tlist_firstobj);
2683ed8d 3162}
b30f223b
RS
3163\f
3164/* Validate the expression after `case' and apply default promotions. */
3165
a6c0a76c 3166static tree
62e4eb35 3167check_case_value (location_t loc, tree value)
b30f223b
RS
3168{
3169 if (value == NULL_TREE)
3170 return value;
3171
522ddfa2
JM
3172 if (TREE_CODE (value) == INTEGER_CST)
3173 /* Promote char or short to int. */
3174 value = perform_integral_promotions (value);
3175 else if (value != error_mark_node)
b30f223b 3176 {
62e4eb35 3177 error_at (loc, "case label does not reduce to an integer constant");
b30f223b
RS
3178 value = error_mark_node;
3179 }
b30f223b 3180
bc690db1
RS
3181 constant_expression_warning (value);
3182
b30f223b
RS
3183 return value;
3184}
3185\f
a6c0a76c 3186/* See if the case values LOW and HIGH are in the range of the original
89dbed81 3187 type (i.e. before the default conversion to int) of the switch testing
a6c0a76c
SB
3188 expression.
3189 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2a7e31df 3190 the type before promoting it. CASE_LOW_P is a pointer to the lower
a6c0a76c
SB
3191 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3192 if the case is not a case range.
3193 The caller has to make sure that we are not called with NULL for
b155cfd9
MP
3194 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3195 was a case value that doesn't fit into the range of the ORIG_TYPE.
0fa2e4df 3196 Returns true if the case label is in range of ORIG_TYPE (saturated or
a6c0a76c
SB
3197 untouched) or false if the label is out of range. */
3198
3199static bool
9d548dfb 3200check_case_bounds (location_t loc, tree type, tree orig_type,
b155cfd9
MP
3201 tree *case_low_p, tree *case_high_p,
3202 bool *outside_range_p)
a6c0a76c
SB
3203{
3204 tree min_value, max_value;
3205 tree case_low = *case_low_p;
3206 tree case_high = case_high_p ? *case_high_p : case_low;
3207
3208 /* If there was a problem with the original type, do nothing. */
3209 if (orig_type == error_mark_node)
3210 return true;
3211
3212 min_value = TYPE_MIN_VALUE (orig_type);
3213 max_value = TYPE_MAX_VALUE (orig_type);
3214
0f62c7a0
MP
3215 /* We'll really need integer constants here. */
3216 case_low = fold (case_low);
3217 case_high = fold (case_high);
3218
a6c0a76c
SB
3219 /* Case label is less than minimum for type. */
3220 if (tree_int_cst_compare (case_low, min_value) < 0
3221 && tree_int_cst_compare (case_high, min_value) < 0)
3222 {
9d548dfb
MP
3223 warning_at (loc, 0, "case label value is less than minimum value "
3224 "for type");
b155cfd9 3225 *outside_range_p = true;
a6c0a76c
SB
3226 return false;
3227 }
9f63daea 3228
a6c0a76c
SB
3229 /* Case value is greater than maximum for type. */
3230 if (tree_int_cst_compare (case_low, max_value) > 0
3231 && tree_int_cst_compare (case_high, max_value) > 0)
3232 {
9d548dfb 3233 warning_at (loc, 0, "case label value exceeds maximum value for type");
b155cfd9 3234 *outside_range_p = true;
a6c0a76c
SB
3235 return false;
3236 }
3237
3238 /* Saturate lower case label value to minimum. */
3239 if (tree_int_cst_compare (case_high, min_value) >= 0
3240 && tree_int_cst_compare (case_low, min_value) < 0)
3241 {
9d548dfb
MP
3242 warning_at (loc, 0, "lower value in case label range"
3243 " less than minimum value for type");
b155cfd9 3244 *outside_range_p = true;
a6c0a76c
SB
3245 case_low = min_value;
3246 }
9f63daea 3247
a6c0a76c
SB
3248 /* Saturate upper case label value to maximum. */
3249 if (tree_int_cst_compare (case_low, max_value) <= 0
3250 && tree_int_cst_compare (case_high, max_value) > 0)
3251 {
9d548dfb
MP
3252 warning_at (loc, 0, "upper value in case label range"
3253 " exceeds maximum value for type");
b155cfd9 3254 *outside_range_p = true;
a6c0a76c
SB
3255 case_high = max_value;
3256 }
3257
3258 if (*case_low_p != case_low)
3259 *case_low_p = convert (type, case_low);
3260 if (case_high_p && *case_high_p != case_high)
3261 *case_high_p = convert (type, case_high);
3262
3263 return true;
3264}
3265\f
b30f223b
RS
3266/* Return an integer type with BITS bits of precision,
3267 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3268
3269tree
35b1a6fa 3270c_common_type_for_size (unsigned int bits, int unsignedp)
b30f223b 3271{
78a7c317
DD
3272 int i;
3273
a311b52c
JM
3274 if (bits == TYPE_PRECISION (integer_type_node))
3275 return unsignedp ? unsigned_type_node : integer_type_node;
3276
3fc7e390 3277 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
3278 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3279
3fc7e390 3280 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
3281 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3282
3fc7e390 3283 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
3284 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3285
3fc7e390 3286 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
3287 return (unsignedp ? long_long_unsigned_type_node
3288 : long_long_integer_type_node);
3289
78a7c317
DD
3290 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3291 if (int_n_enabled_p[i]
3292 && bits == int_n_data[i].bitsize)
3293 return (unsignedp ? int_n_trees[i].unsigned_type
3294 : int_n_trees[i].signed_type);
a6766312 3295
835f9b4d
GRK
3296 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3297 return (unsignedp ? widest_unsigned_literal_type_node
3298 : widest_integer_literal_type_node);
3299
3fc7e390
RS
3300 if (bits <= TYPE_PRECISION (intQI_type_node))
3301 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3302
3303 if (bits <= TYPE_PRECISION (intHI_type_node))
3304 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3305
3306 if (bits <= TYPE_PRECISION (intSI_type_node))
3307 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3308
3309 if (bits <= TYPE_PRECISION (intDI_type_node))
3310 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3311
b30f223b
RS
3312 return 0;
3313}
3314
ab22c1fa
CF
3315/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3316 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3317 and saturating if SATP is nonzero, otherwise not saturating. */
3318
3319tree
3320c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3321 int unsignedp, int satp)
3322{
ef4bddc2 3323 machine_mode mode;
ab22c1fa
CF
3324 if (ibit == 0)
3325 mode = unsignedp ? UQQmode : QQmode;
3326 else
3327 mode = unsignedp ? UHAmode : HAmode;
3328
3329 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3330 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3331 break;
3332
3333 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3334 {
3335 sorry ("GCC cannot support operators with integer types and "
3336 "fixed-point types that have too many integral and "
3337 "fractional bits together");
3338 return 0;
3339 }
3340
3341 return c_common_type_for_mode (mode, satp);
3342}
3343
d1d3865f
ZW
3344/* Used for communication between c_common_type_for_mode and
3345 c_register_builtin_type. */
793c625f 3346tree registered_builtin_types;
d1d3865f 3347
b30f223b
RS
3348/* Return a data type that has machine mode MODE.
3349 If the mode is an integer,
ab22c1fa
CF
3350 then UNSIGNEDP selects between signed and unsigned types.
3351 If the mode is a fixed-point mode,
3352 then UNSIGNEDP selects between saturating and nonsaturating types. */
b30f223b
RS
3353
3354tree
ef4bddc2 3355c_common_type_for_mode (machine_mode mode, int unsignedp)
b30f223b 3356{
d1d3865f 3357 tree t;
78a7c317 3358 int i;
d1d3865f 3359
a311b52c
JM
3360 if (mode == TYPE_MODE (integer_type_node))
3361 return unsignedp ? unsigned_type_node : integer_type_node;
3362
b30f223b
RS
3363 if (mode == TYPE_MODE (signed_char_type_node))
3364 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3365
3366 if (mode == TYPE_MODE (short_integer_type_node))
3367 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3368
b30f223b
RS
3369 if (mode == TYPE_MODE (long_integer_type_node))
3370 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3371
3372 if (mode == TYPE_MODE (long_long_integer_type_node))
3373 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3374
78a7c317
DD
3375 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3376 if (int_n_enabled_p[i]
3377 && mode == int_n_data[i].m)
3378 return (unsignedp ? int_n_trees[i].unsigned_type
3379 : int_n_trees[i].signed_type);
a6766312 3380
835f9b4d 3381 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 3382 return unsignedp ? widest_unsigned_literal_type_node
6de9cd9a 3383 : widest_integer_literal_type_node;
835f9b4d 3384
0afeef64 3385 if (mode == QImode)
3fc7e390
RS
3386 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3387
0afeef64 3388 if (mode == HImode)
3fc7e390
RS
3389 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3390
0afeef64 3391 if (mode == SImode)
3fc7e390
RS
3392 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3393
0afeef64 3394 if (mode == DImode)
3fc7e390
RS
3395 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3396
21a9616b 3397#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
3398 if (mode == TYPE_MODE (intTI_type_node))
3399 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 3400#endif
a6d7e156 3401
b30f223b
RS
3402 if (mode == TYPE_MODE (float_type_node))
3403 return float_type_node;
3404
3405 if (mode == TYPE_MODE (double_type_node))
3406 return double_type_node;
3407
3408 if (mode == TYPE_MODE (long_double_type_node))
3409 return long_double_type_node;
3410
ff42324e
NS
3411 if (mode == TYPE_MODE (void_type_node))
3412 return void_type_node;
9f63daea 3413
b30f223b 3414 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
19b3ffbc
DD
3415 return (unsignedp
3416 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3417 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b
RS
3418
3419 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
19b3ffbc
DD
3420 return (unsignedp
3421 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3422 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b 3423
7e7e470f
RH
3424 if (COMPLEX_MODE_P (mode))
3425 {
ef4bddc2 3426 machine_mode inner_mode;
7e7e470f
RH
3427 tree inner_type;
3428
3429 if (mode == TYPE_MODE (complex_float_type_node))
3430 return complex_float_type_node;
3431 if (mode == TYPE_MODE (complex_double_type_node))
3432 return complex_double_type_node;
3433 if (mode == TYPE_MODE (complex_long_double_type_node))
3434 return complex_long_double_type_node;
3435
3436 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3437 return complex_integer_type_node;
3438
3439 inner_mode = GET_MODE_INNER (mode);
3440 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3441 if (inner_type != NULL_TREE)
3442 return build_complex_type (inner_type);
3443 }
3444 else if (VECTOR_MODE_P (mode))
4a5eab38 3445 {
ef4bddc2 3446 machine_mode inner_mode = GET_MODE_INNER (mode);
4a5eab38
PB
3447 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3448 if (inner_type != NULL_TREE)
3449 return build_vector_type_for_mode (inner_type, mode);
0afeef64 3450 }
4061f623 3451
9a8ce21f
JG
3452 if (mode == TYPE_MODE (dfloat32_type_node))
3453 return dfloat32_type_node;
3454 if (mode == TYPE_MODE (dfloat64_type_node))
3455 return dfloat64_type_node;
3456 if (mode == TYPE_MODE (dfloat128_type_node))
3457 return dfloat128_type_node;
3458
ab22c1fa
CF
3459 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3460 {
3461 if (mode == TYPE_MODE (short_fract_type_node))
3462 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3463 if (mode == TYPE_MODE (fract_type_node))
3464 return unsignedp ? sat_fract_type_node : fract_type_node;
3465 if (mode == TYPE_MODE (long_fract_type_node))
3466 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3467 if (mode == TYPE_MODE (long_long_fract_type_node))
3468 return unsignedp ? sat_long_long_fract_type_node
3469 : long_long_fract_type_node;
3470
3471 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3472 return unsignedp ? sat_unsigned_short_fract_type_node
3473 : unsigned_short_fract_type_node;
3474 if (mode == TYPE_MODE (unsigned_fract_type_node))
3475 return unsignedp ? sat_unsigned_fract_type_node
3476 : unsigned_fract_type_node;
3477 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3478 return unsignedp ? sat_unsigned_long_fract_type_node
3479 : unsigned_long_fract_type_node;
3480 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3481 return unsignedp ? sat_unsigned_long_long_fract_type_node
3482 : unsigned_long_long_fract_type_node;
3483
3484 if (mode == TYPE_MODE (short_accum_type_node))
3485 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3486 if (mode == TYPE_MODE (accum_type_node))
3487 return unsignedp ? sat_accum_type_node : accum_type_node;
3488 if (mode == TYPE_MODE (long_accum_type_node))
3489 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3490 if (mode == TYPE_MODE (long_long_accum_type_node))
3491 return unsignedp ? sat_long_long_accum_type_node
3492 : long_long_accum_type_node;
3493
3494 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3495 return unsignedp ? sat_unsigned_short_accum_type_node
3496 : unsigned_short_accum_type_node;
3497 if (mode == TYPE_MODE (unsigned_accum_type_node))
3498 return unsignedp ? sat_unsigned_accum_type_node
3499 : unsigned_accum_type_node;
3500 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3501 return unsignedp ? sat_unsigned_long_accum_type_node
3502 : unsigned_long_accum_type_node;
3503 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3504 return unsignedp ? sat_unsigned_long_long_accum_type_node
3505 : unsigned_long_long_accum_type_node;
3506
3507 if (mode == QQmode)
3508 return unsignedp ? sat_qq_type_node : qq_type_node;
3509 if (mode == HQmode)
3510 return unsignedp ? sat_hq_type_node : hq_type_node;
3511 if (mode == SQmode)
3512 return unsignedp ? sat_sq_type_node : sq_type_node;
3513 if (mode == DQmode)
3514 return unsignedp ? sat_dq_type_node : dq_type_node;
3515 if (mode == TQmode)
3516 return unsignedp ? sat_tq_type_node : tq_type_node;
3517
3518 if (mode == UQQmode)
3519 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3520 if (mode == UHQmode)
3521 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3522 if (mode == USQmode)
3523 return unsignedp ? sat_usq_type_node : usq_type_node;
3524 if (mode == UDQmode)
3525 return unsignedp ? sat_udq_type_node : udq_type_node;
3526 if (mode == UTQmode)
3527 return unsignedp ? sat_utq_type_node : utq_type_node;
3528
3529 if (mode == HAmode)
3530 return unsignedp ? sat_ha_type_node : ha_type_node;
3531 if (mode == SAmode)
3532 return unsignedp ? sat_sa_type_node : sa_type_node;
3533 if (mode == DAmode)
3534 return unsignedp ? sat_da_type_node : da_type_node;
3535 if (mode == TAmode)
3536 return unsignedp ? sat_ta_type_node : ta_type_node;
3537
3538 if (mode == UHAmode)
3539 return unsignedp ? sat_uha_type_node : uha_type_node;
3540 if (mode == USAmode)
3541 return unsignedp ? sat_usa_type_node : usa_type_node;
3542 if (mode == UDAmode)
3543 return unsignedp ? sat_uda_type_node : uda_type_node;
3544 if (mode == UTAmode)
3545 return unsignedp ? sat_uta_type_node : uta_type_node;
3546 }
3547
d1d3865f 3548 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
7a421706
MS
3549 if (TYPE_MODE (TREE_VALUE (t)) == mode
3550 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
d1d3865f
ZW
3551 return TREE_VALUE (t);
3552
b30f223b
RS
3553 return 0;
3554}
693a6128 3555
12753674
RE
3556tree
3557c_common_unsigned_type (tree type)
3558{
3559 return c_common_signed_or_unsigned_type (1, type);
3560}
3561
693a6128
GRK
3562/* Return a signed type the same as TYPE in other respects. */
3563
3564tree
35b1a6fa 3565c_common_signed_type (tree type)
693a6128 3566{
ceef8ce4 3567 return c_common_signed_or_unsigned_type (0, type);
693a6128
GRK
3568}
3569
3570/* Return a type the same as TYPE except unsigned or
3571 signed according to UNSIGNEDP. */
3572
3573tree
35b1a6fa 3574c_common_signed_or_unsigned_type (int unsignedp, tree type)
693a6128 3575{
c74a03d2 3576 tree type1;
78a7c317 3577 int i;
693a6128 3578
c74a03d2
RAE
3579 /* This block of code emulates the behavior of the old
3580 c_common_unsigned_type. In particular, it returns
3581 long_unsigned_type_node if passed a long, even when a int would
3582 have the same size. This is necessary for warnings to work
3583 correctly in archs where sizeof(int) == sizeof(long) */
3584
3585 type1 = TYPE_MAIN_VARIANT (type);
3586 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3587 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3588 if (type1 == integer_type_node || type1 == unsigned_type_node)
3589 return unsignedp ? unsigned_type_node : integer_type_node;
3590 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3591 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3592 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3593 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3594 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3595 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
78a7c317
DD
3596
3597 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3598 if (int_n_enabled_p[i]
3599 && (type1 == int_n_trees[i].unsigned_type
3600 || type1 == int_n_trees[i].signed_type))
3601 return (unsignedp ? int_n_trees[i].unsigned_type
3602 : int_n_trees[i].signed_type);
3603
c74a03d2
RAE
3604 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3605 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3606#if HOST_BITS_PER_WIDE_INT >= 64
3607 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3608 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3609#endif
3610 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3611 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3612 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3613 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3614 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3615 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3616 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3617 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3618
70d3fcab
AH
3619#define C_COMMON_FIXED_TYPES(NAME) \
3620 if (type1 == short_ ## NAME ## _type_node \
3621 || type1 == unsigned_short_ ## NAME ## _type_node) \
3622 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3623 : short_ ## NAME ## _type_node; \
3624 if (type1 == NAME ## _type_node \
3625 || type1 == unsigned_ ## NAME ## _type_node) \
3626 return unsignedp ? unsigned_ ## NAME ## _type_node \
3627 : NAME ## _type_node; \
3628 if (type1 == long_ ## NAME ## _type_node \
3629 || type1 == unsigned_long_ ## NAME ## _type_node) \
3630 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3631 : long_ ## NAME ## _type_node; \
3632 if (type1 == long_long_ ## NAME ## _type_node \
3633 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3634 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3635 : long_long_ ## NAME ## _type_node;
3636
3637#define C_COMMON_FIXED_MODE_TYPES(NAME) \
3638 if (type1 == NAME ## _type_node \
3639 || type1 == u ## NAME ## _type_node) \
3640 return unsignedp ? u ## NAME ## _type_node \
3641 : NAME ## _type_node;
3642
3643#define C_COMMON_FIXED_TYPES_SAT(NAME) \
3644 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3645 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3646 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3647 : sat_ ## short_ ## NAME ## _type_node; \
3648 if (type1 == sat_ ## NAME ## _type_node \
3649 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3650 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3651 : sat_ ## NAME ## _type_node; \
3652 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3653 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3654 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3655 : sat_ ## long_ ## NAME ## _type_node; \
3656 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3657 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3658 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3659 : sat_ ## long_long_ ## NAME ## _type_node;
3660
3661#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3662 if (type1 == sat_ ## NAME ## _type_node \
3663 || type1 == sat_ ## u ## NAME ## _type_node) \
3664 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3665 : sat_ ## NAME ## _type_node;
3666
3667 C_COMMON_FIXED_TYPES (fract);
3668 C_COMMON_FIXED_TYPES_SAT (fract);
3669 C_COMMON_FIXED_TYPES (accum);
3670 C_COMMON_FIXED_TYPES_SAT (accum);
3671
3672 C_COMMON_FIXED_MODE_TYPES (qq);
3673 C_COMMON_FIXED_MODE_TYPES (hq);
3674 C_COMMON_FIXED_MODE_TYPES (sq);
3675 C_COMMON_FIXED_MODE_TYPES (dq);
3676 C_COMMON_FIXED_MODE_TYPES (tq);
3677 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3678 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3679 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3680 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3681 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3682 C_COMMON_FIXED_MODE_TYPES (ha);
3683 C_COMMON_FIXED_MODE_TYPES (sa);
3684 C_COMMON_FIXED_MODE_TYPES (da);
3685 C_COMMON_FIXED_MODE_TYPES (ta);
3686 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3687 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3688 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3689 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
ab22c1fa 3690
bc15d0ef
JM
3691 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3692 the precision; they have precision set to match their range, but
3693 may use a wider mode to match an ABI. If we change modes, we may
3694 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3695 the precision as well, so as to yield correct results for
3696 bit-field types. C++ does not have these separate bit-field
3697 types, and producing a signed or unsigned variant of an
3698 ENUMERAL_TYPE may cause other problems as well. */
3699
1e204133
RAE
3700 if (!INTEGRAL_TYPE_P (type)
3701 || TYPE_UNSIGNED (type) == unsignedp)
3702 return type;
3703
bc15d0ef
JM
3704#define TYPE_OK(node) \
3705 (TYPE_MODE (type) == TYPE_MODE (node) \
41b81065 3706 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
bc15d0ef 3707 if (TYPE_OK (signed_char_type_node))
693a6128 3708 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
bc15d0ef 3709 if (TYPE_OK (integer_type_node))
693a6128 3710 return unsignedp ? unsigned_type_node : integer_type_node;
bc15d0ef 3711 if (TYPE_OK (short_integer_type_node))
693a6128 3712 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
bc15d0ef 3713 if (TYPE_OK (long_integer_type_node))
693a6128 3714 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
bc15d0ef 3715 if (TYPE_OK (long_long_integer_type_node))
693a6128
GRK
3716 return (unsignedp ? long_long_unsigned_type_node
3717 : long_long_integer_type_node);
78a7c317
DD
3718
3719 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3720 if (int_n_enabled_p[i]
3721 && TYPE_MODE (type) == int_n_data[i].m
3722 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3723 return (unsignedp ? int_n_trees[i].unsigned_type
3724 : int_n_trees[i].signed_type);
3725
bc15d0ef 3726 if (TYPE_OK (widest_integer_literal_type_node))
693a6128
GRK
3727 return (unsignedp ? widest_unsigned_literal_type_node
3728 : widest_integer_literal_type_node);
4a063bec
RH
3729
3730#if HOST_BITS_PER_WIDE_INT >= 64
bc15d0ef 3731 if (TYPE_OK (intTI_type_node))
4a063bec
RH
3732 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3733#endif
bc15d0ef 3734 if (TYPE_OK (intDI_type_node))
4a063bec 3735 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
bc15d0ef 3736 if (TYPE_OK (intSI_type_node))
4a063bec 3737 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
bc15d0ef 3738 if (TYPE_OK (intHI_type_node))
4a063bec 3739 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
bc15d0ef 3740 if (TYPE_OK (intQI_type_node))
4a063bec 3741 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
bc15d0ef 3742#undef TYPE_OK
4a063bec 3743
41b81065 3744 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
693a6128 3745}
9649812a 3746
38a4afee
MM
3747/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3748
3749tree
3750c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3751{
78a7c317
DD
3752 int i;
3753
38a4afee
MM
3754 /* Extended integer types of the same width as a standard type have
3755 lesser rank, so those of the same width as int promote to int or
3756 unsigned int and are valid for printf formats expecting int or
3757 unsigned int. To avoid such special cases, avoid creating
3758 extended integer types for bit-fields if a standard integer type
3759 is available. */
3760 if (width == TYPE_PRECISION (integer_type_node))
3761 return unsignedp ? unsigned_type_node : integer_type_node;
3762 if (width == TYPE_PRECISION (signed_char_type_node))
3763 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3764 if (width == TYPE_PRECISION (short_integer_type_node))
3765 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3766 if (width == TYPE_PRECISION (long_integer_type_node))
3767 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3768 if (width == TYPE_PRECISION (long_long_integer_type_node))
3769 return (unsignedp ? long_long_unsigned_type_node
3770 : long_long_integer_type_node);
78a7c317
DD
3771 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3772 if (int_n_enabled_p[i]
3773 && width == int_n_data[i].bitsize)
3774 return (unsignedp ? int_n_trees[i].unsigned_type
3775 : int_n_trees[i].signed_type);
38a4afee
MM
3776 return build_nonstandard_integer_type (width, unsignedp);
3777}
3778
9649812a
MM
3779/* The C version of the register_builtin_type langhook. */
3780
3781void
3782c_register_builtin_type (tree type, const char* name)
3783{
3784 tree decl;
3785
c2255bc4
AH
3786 decl = build_decl (UNKNOWN_LOCATION,
3787 TYPE_DECL, get_identifier (name), type);
9649812a
MM
3788 DECL_ARTIFICIAL (decl) = 1;
3789 if (!TYPE_NAME (type))
3790 TYPE_NAME (type) = decl;
3791 pushdecl (decl);
d1d3865f
ZW
3792
3793 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
9649812a 3794}
6acfe908 3795\f
78ef5b89 3796/* Print an error message for invalid operands to arith operation
ba47d38d 3797 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
745e411d
DM
3798 RICHLOC is a rich location for the message, containing either
3799 three separate locations for each of the operator and operands
3800
3801 lhs op rhs
3802 ~~~ ^~ ~~~
3803
3804 (C FE), or one location ranging over all over them
3805
3806 lhs op rhs
3807 ~~~~^~~~~~
3808
3809 (C++ FE). */
b30f223b
RS
3810
3811void
745e411d 3812binary_op_error (rich_location *richloc, enum tree_code code,
ba47d38d 3813 tree type0, tree type1)
b30f223b 3814{
b3694847 3815 const char *opname;
89c78d7d 3816
b30f223b
RS
3817 switch (code)
3818 {
b30f223b
RS
3819 case PLUS_EXPR:
3820 opname = "+"; break;
3821 case MINUS_EXPR:
3822 opname = "-"; break;
3823 case MULT_EXPR:
3824 opname = "*"; break;
3825 case MAX_EXPR:
3826 opname = "max"; break;
3827 case MIN_EXPR:
3828 opname = "min"; break;
3829 case EQ_EXPR:
3830 opname = "=="; break;
3831 case NE_EXPR:
3832 opname = "!="; break;
3833 case LE_EXPR:
3834 opname = "<="; break;
3835 case GE_EXPR:
3836 opname = ">="; break;
3837 case LT_EXPR:
3838 opname = "<"; break;
3839 case GT_EXPR:
3840 opname = ">"; break;
3841 case LSHIFT_EXPR:
3842 opname = "<<"; break;
3843 case RSHIFT_EXPR:
3844 opname = ">>"; break;
3845 case TRUNC_MOD_EXPR:
047de90b 3846 case FLOOR_MOD_EXPR:
b30f223b
RS
3847 opname = "%"; break;
3848 case TRUNC_DIV_EXPR:
047de90b 3849 case FLOOR_DIV_EXPR:
b30f223b
RS
3850 opname = "/"; break;
3851 case BIT_AND_EXPR:
3852 opname = "&"; break;
3853 case BIT_IOR_EXPR:
3854 opname = "|"; break;
3855 case TRUTH_ANDIF_EXPR:
3856 opname = "&&"; break;
3857 case TRUTH_ORIF_EXPR:
3858 opname = "||"; break;
3859 case BIT_XOR_EXPR:
3860 opname = "^"; break;
6d819282 3861 default:
37b2f290 3862 gcc_unreachable ();
b30f223b 3863 }
745e411d
DM
3864 error_at_rich_loc (richloc,
3865 "invalid operands to binary %s (have %qT and %qT)",
3866 opname, type0, type1);
b30f223b
RS
3867}
3868\f
50f305ca
MLI
3869/* Given an expression as a tree, return its original type. Do this
3870 by stripping any conversion that preserves the sign and precision. */
3871static tree
3872expr_original_type (tree expr)
3873{
3874 STRIP_SIGN_NOPS (expr);
3875 return TREE_TYPE (expr);
3876}
3877
b30f223b
RS
3878/* Subroutine of build_binary_op, used for comparison operations.
3879 See if the operands have both been converted from subword integer types
3880 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
3881 This function is also responsible for converting the two operands
3882 to the proper common type for comparison.
b30f223b
RS
3883
3884 The arguments of this function are all pointers to local variables
3885 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3886 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3887
393e8e8b
MP
3888 LOC is the location of the comparison.
3889
b30f223b
RS
3890 If this function returns nonzero, it means that the comparison has
3891 a constant value. What this function returns is an expression for
3892 that value. */
3893
3894tree
393e8e8b
MP
3895shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3896 tree *restype_ptr, enum tree_code *rescode_ptr)
b30f223b 3897{
b3694847 3898 tree type;
b30f223b
RS
3899 tree op0 = *op0_ptr;
3900 tree op1 = *op1_ptr;
3901 int unsignedp0, unsignedp1;
3902 int real1, real2;
3903 tree primop0, primop1;
3904 enum tree_code code = *rescode_ptr;
3905
3906 /* Throw away any conversions to wider types
3907 already present in the operands. */
3908
828fb3ba
JM
3909 primop0 = c_common_get_narrower (op0, &unsignedp0);
3910 primop1 = c_common_get_narrower (op1, &unsignedp1);
b30f223b 3911
126e6609
JJ
3912 /* If primopN is first sign-extended from primopN's precision to opN's
3913 precision, then zero-extended from opN's precision to
3914 *restype_ptr precision, shortenings might be invalid. */
3915 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3916 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3917 && !unsignedp0
3918 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3919 primop0 = op0;
3920 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3921 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3922 && !unsignedp1
3923 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3924 primop1 = op1;
3925
b30f223b
RS
3926 /* Handle the case that OP0 does not *contain* a conversion
3927 but it *requires* conversion to FINAL_TYPE. */
3928
3929 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
8df83eae 3930 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b30f223b 3931 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
8df83eae 3932 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b30f223b
RS
3933
3934 /* If one of the operands must be floated, we cannot optimize. */
3935 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3936 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3937
3938 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
3939 so value is preserved), for canonicalization. Don't do this if
3940 the second arg is 0. */
b30f223b 3941
5af6001b 3942 if (TREE_CONSTANT (primop0)
ab22c1fa
CF
3943 && !integer_zerop (primop1) && !real_zerop (primop1)
3944 && !fixed_zerop (primop1))
b30f223b 3945 {
fab27f52
MM
3946 std::swap (primop0, primop1);
3947 std::swap (op0, op1);
b30f223b
RS
3948 *op0_ptr = op0;
3949 *op1_ptr = op1;
fab27f52
MM
3950 std::swap (unsignedp0, unsignedp1);
3951 std::swap (real1, real2);
b30f223b
RS
3952
3953 switch (code)
3954 {
3955 case LT_EXPR:
3956 code = GT_EXPR;
3957 break;
3958 case GT_EXPR:
3959 code = LT_EXPR;
3960 break;
3961 case LE_EXPR:
3962 code = GE_EXPR;
3963 break;
3964 case GE_EXPR:
3965 code = LE_EXPR;
3966 break;
6d819282
MK
3967 default:
3968 break;
b30f223b
RS
3969 }
3970 *rescode_ptr = code;
3971 }
3972
3973 /* If comparing an integer against a constant more bits wide,
3974 maybe we can deduce a value of 1 or 0 independent of the data.
3975 Or else truncate the constant now
3976 rather than extend the variable at run time.
3977
3978 This is only interesting if the constant is the wider arg.
3979 Also, it is not safe if the constant is unsigned and the
3980 variable arg is signed, since in this case the variable
3981 would be sign-extended and then regarded as unsigned.
3982 Our technique fails in this case because the lowest/highest
3983 possible unsigned results don't follow naturally from the
3984 lowest/highest possible values of the variable operand.
3985 For just EQ_EXPR and NE_EXPR there is another technique that
3986 could be used: see if the constant can be faithfully represented
3987 in the other operand's type, by truncating it and reextending it
3988 and see if that preserves the constant's value. */
3989
3990 if (!real1 && !real2
ab22c1fa 3991 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b30f223b
RS
3992 && TREE_CODE (primop1) == INTEGER_CST
3993 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3994 {
3995 int min_gt, max_gt, min_lt, max_lt;
3996 tree maxval, minval;
3997 /* 1 if comparison is nominally unsigned. */
8df83eae 3998 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b30f223b
RS
3999 tree val;
4000
ceef8ce4
NB
4001 type = c_common_signed_or_unsigned_type (unsignedp0,
4002 TREE_TYPE (primop0));
8bbd5685 4003
b30f223b
RS
4004 maxval = TYPE_MAX_VALUE (type);
4005 minval = TYPE_MIN_VALUE (type);
4006
4007 if (unsignedp && !unsignedp0)
ceef8ce4 4008 *restype_ptr = c_common_signed_type (*restype_ptr);
b30f223b
RS
4009
4010 if (TREE_TYPE (primop1) != *restype_ptr)
fae1b38d 4011 {
af9c6659
NS
4012 /* Convert primop1 to target type, but do not introduce
4013 additional overflow. We know primop1 is an int_cst. */
807e902e
KZ
4014 primop1 = force_fit_type (*restype_ptr,
4015 wide_int::from
4016 (primop1,
4017 TYPE_PRECISION (*restype_ptr),
4018 TYPE_SIGN (TREE_TYPE (primop1))),
4019 0, TREE_OVERFLOW (primop1));
fae1b38d 4020 }
b30f223b
RS
4021 if (type != *restype_ptr)
4022 {
4023 minval = convert (*restype_ptr, minval);
4024 maxval = convert (*restype_ptr, maxval);
4025 }
4026
807e902e
KZ
4027 min_gt = tree_int_cst_lt (primop1, minval);
4028 max_gt = tree_int_cst_lt (primop1, maxval);
4029 min_lt = tree_int_cst_lt (minval, primop1);
4030 max_lt = tree_int_cst_lt (maxval, primop1);
b30f223b
RS
4031
4032 val = 0;
4033 /* This used to be a switch, but Genix compiler can't handle that. */
4034 if (code == NE_EXPR)
4035 {
4036 if (max_lt || min_gt)
de7df9eb 4037 val = truthvalue_true_node;
b30f223b
RS
4038 }
4039 else if (code == EQ_EXPR)
4040 {
4041 if (max_lt || min_gt)
de7df9eb 4042 val = truthvalue_false_node;
b30f223b
RS
4043 }
4044 else if (code == LT_EXPR)
4045 {
4046 if (max_lt)
de7df9eb 4047 val = truthvalue_true_node;
b30f223b 4048 if (!min_lt)
de7df9eb 4049 val = truthvalue_false_node;
b30f223b
RS
4050 }
4051 else if (code == GT_EXPR)
4052 {
4053 if (min_gt)
de7df9eb 4054 val = truthvalue_true_node;
b30f223b 4055 if (!max_gt)
de7df9eb 4056 val = truthvalue_false_node;
b30f223b
RS
4057 }
4058 else if (code == LE_EXPR)
4059 {
4060 if (!max_gt)
de7df9eb 4061 val = truthvalue_true_node;
b30f223b 4062 if (min_gt)
de7df9eb 4063 val = truthvalue_false_node;
b30f223b
RS
4064 }
4065 else if (code == GE_EXPR)
4066 {
4067 if (!min_lt)
de7df9eb 4068 val = truthvalue_true_node;
b30f223b 4069 if (max_lt)
de7df9eb 4070 val = truthvalue_false_node;
b30f223b
RS
4071 }
4072
4073 /* If primop0 was sign-extended and unsigned comparison specd,
4074 we did a signed comparison above using the signed type bounds.
4075 But the comparison we output must be unsigned.
4076
4077 Also, for inequalities, VAL is no good; but if the signed
4078 comparison had *any* fixed result, it follows that the
4079 unsigned comparison just tests the sign in reverse
4080 (positive values are LE, negative ones GE).
4081 So we can generate an unsigned comparison
4082 against an extreme value of the signed type. */
4083
4084 if (unsignedp && !unsignedp0)
4085 {
4086 if (val != 0)
4087 switch (code)
4088 {
4089 case LT_EXPR:
4090 case GE_EXPR:
4091 primop1 = TYPE_MIN_VALUE (type);
4092 val = 0;
4093 break;
4094
4095 case LE_EXPR:
4096 case GT_EXPR:
4097 primop1 = TYPE_MAX_VALUE (type);
4098 val = 0;
4099 break;
6d819282
MK
4100
4101 default:
4102 break;
b30f223b 4103 }
12753674 4104 type = c_common_unsigned_type (type);
b30f223b
RS
4105 }
4106
d0eccfcd 4107 if (TREE_CODE (primop0) != INTEGER_CST
269adb9d
JM
4108 /* Don't warn if it's from a (non-system) macro. */
4109 && !(from_macro_expansion_at
4110 (expansion_point_location_if_in_system_header
4111 (EXPR_LOCATION (primop0)))))
b30f223b 4112 {
de7df9eb 4113 if (val == truthvalue_false_node)
50f305ca
MLI
4114 warning_at (loc, OPT_Wtype_limits,
4115 "comparison is always false due to limited range of data type");
de7df9eb 4116 if (val == truthvalue_true_node)
50f305ca
MLI
4117 warning_at (loc, OPT_Wtype_limits,
4118 "comparison is always true due to limited range of data type");
b30f223b
RS
4119 }
4120
4121 if (val != 0)
4122 {
4123 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4124 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3 4125 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b30f223b
RS
4126 return val;
4127 }
4128
4129 /* Value is not predetermined, but do the comparison
4130 in the type of the operand that is not constant.
4131 TYPE is already properly set. */
4132 }
9a8ce21f
JG
4133
4134 /* If either arg is decimal float and the other is float, find the
4135 proper common type to use for comparison. */
6f450181
RB
4136 else if (real1 && real2
4137 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4138 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4139 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4140
4141 /* If either arg is decimal float and the other is float, fail. */
9a8ce21f
JG
4142 else if (real1 && real2
4143 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4144 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
6f450181 4145 return 0;
9a8ce21f 4146
b30f223b 4147 else if (real1 && real2
766f6c30
RS
4148 && (TYPE_PRECISION (TREE_TYPE (primop0))
4149 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
4150 type = TREE_TYPE (primop0);
4151
4152 /* If args' natural types are both narrower than nominal type
4153 and both extend in the same manner, compare them
4154 in the type of the wider arg.
4155 Otherwise must actually extend both to the nominal
4156 common type lest different ways of extending
4157 alter the result.
4158 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4159
4160 else if (unsignedp0 == unsignedp1 && real1 == real2
4161 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4162 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4163 {
4164 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
ceef8ce4 4165 type = c_common_signed_or_unsigned_type (unsignedp0
8df83eae 4166 || TYPE_UNSIGNED (*restype_ptr),
ceef8ce4 4167 type);
b30f223b
RS
4168 /* Make sure shorter operand is extended the right way
4169 to match the longer operand. */
ceef8ce4
NB
4170 primop0
4171 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4172 TREE_TYPE (primop0)),
4173 primop0);
4174 primop1
4175 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4176 TREE_TYPE (primop1)),
4177 primop1);
b30f223b
RS
4178 }
4179 else
4180 {
4181 /* Here we must do the comparison on the nominal type
4182 using the args exactly as we received them. */
4183 type = *restype_ptr;
4184 primop0 = op0;
4185 primop1 = op1;
4186
4187 if (!real1 && !real2 && integer_zerop (primop1)
8df83eae 4188 && TYPE_UNSIGNED (*restype_ptr))
b30f223b
RS
4189 {
4190 tree value = 0;
50f305ca
MLI
4191 /* All unsigned values are >= 0, so we warn. However,
4192 if OP0 is a constant that is >= 0, the signedness of
4193 the comparison isn't an issue, so suppress the
4194 warning. */
4195 bool warn =
8400e75e 4196 warn_type_limits && !in_system_header_at (loc)
50f305ca
MLI
4197 && !(TREE_CODE (primop0) == INTEGER_CST
4198 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4199 primop0)))
4200 /* Do not warn for enumeration types. */
4201 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4202
b30f223b
RS
4203 switch (code)
4204 {
4205 case GE_EXPR:
50f305ca
MLI
4206 if (warn)
4207 warning_at (loc, OPT_Wtype_limits,
4208 "comparison of unsigned expression >= 0 is always true");
de7df9eb 4209 value = truthvalue_true_node;
b30f223b
RS
4210 break;
4211
4212 case LT_EXPR:
50f305ca
MLI
4213 if (warn)
4214 warning_at (loc, OPT_Wtype_limits,
4215 "comparison of unsigned expression < 0 is always false");
de7df9eb 4216 value = truthvalue_false_node;
6d819282
MK
4217 break;
4218
4219 default:
4220 break;
b30f223b
RS
4221 }
4222
4223 if (value != 0)
4224 {
4225 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4226 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3
RS
4227 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4228 primop0, value);
b30f223b
RS
4229 return value;
4230 }
4231 }
4232 }
4233
4234 *op0_ptr = convert (type, primop0);
4235 *op1_ptr = convert (type, primop1);
4236
de7df9eb 4237 *restype_ptr = truthvalue_type_node;
b30f223b
RS
4238
4239 return 0;
4240}
4241\f
7552da58
JJ
4242/* Return a tree for the sum or difference (RESULTCODE says which)
4243 of pointer PTROP and integer INTOP. */
4244
4245tree
db3927fb 4246pointer_int_sum (location_t loc, enum tree_code resultcode,
fd9b0f32 4247 tree ptrop, tree intop, bool complain)
7552da58 4248{
6ac01510 4249 tree size_exp, ret;
7552da58 4250
7552da58 4251 /* The result is a pointer of the same type that is being added. */
7552da58
JJ
4252 tree result_type = TREE_TYPE (ptrop);
4253
4254 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4255 {
fd9b0f32
PC
4256 if (complain && warn_pointer_arith)
4257 pedwarn (loc, OPT_Wpointer_arith,
4258 "pointer of type %<void *%> used in arithmetic");
4259 else if (!complain)
4260 return error_mark_node;
7552da58
JJ
4261 size_exp = integer_one_node;
4262 }
4263 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4264 {
fd9b0f32
PC
4265 if (complain && warn_pointer_arith)
4266 pedwarn (loc, OPT_Wpointer_arith,
4267 "pointer to a function used in arithmetic");
4268 else if (!complain)
4269 return error_mark_node;
7552da58
JJ
4270 size_exp = integer_one_node;
4271 }
7552da58
JJ
4272 else
4273 size_exp = size_in_bytes (TREE_TYPE (result_type));
4274
6ac01510
ILT
4275 /* We are manipulating pointer values, so we don't need to warn
4276 about relying on undefined signed overflow. We disable the
4277 warning here because we use integer types so fold won't know that
4278 they are really pointers. */
4279 fold_defer_overflow_warnings ();
4280
7552da58
JJ
4281 /* If what we are about to multiply by the size of the elements
4282 contains a constant term, apply distributive law
4283 and multiply that constant term separately.
4284 This helps produce common subexpressions. */
7552da58 4285 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3f75a254 4286 && !TREE_CONSTANT (intop)
7552da58
JJ
4287 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4288 && TREE_CONSTANT (size_exp)
4289 /* If the constant comes from pointer subtraction,
4290 skip this optimization--it would cause an error. */
4291 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4292 /* If the constant is unsigned, and smaller than the pointer size,
4293 then we must skip this optimization. This is because it could cause
4294 an overflow error if the constant is negative but INTOP is not. */
3f75a254 4295 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
7552da58
JJ
4296 || (TYPE_PRECISION (TREE_TYPE (intop))
4297 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4298 {
4299 enum tree_code subcode = resultcode;
4300 tree int_type = TREE_TYPE (intop);
4301 if (TREE_CODE (intop) == MINUS_EXPR)
4302 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4303 /* Convert both subexpression types to the type of intop,
4304 because weird cases involving pointer arithmetic
4305 can result in a sum or difference with different type args. */
ba47d38d
AH
4306 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4307 subcode, ptrop,
7552da58
JJ
4308 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4309 intop = convert (int_type, TREE_OPERAND (intop, 0));
4310 }
4311
4312 /* Convert the integer argument to a type the same size as sizetype
4313 so the multiply won't overflow spuriously. */
7552da58 4314 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
8df83eae 4315 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
35b1a6fa 4316 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
8df83eae 4317 TYPE_UNSIGNED (sizetype)), intop);
7552da58
JJ
4318
4319 /* Replace the integer argument with a suitable product by the object size.
9e9ef331 4320 Do this multiplication as signed, then convert to the appropriate type
65de6659 4321 for the pointer operation and disregard an overflow that occurred only
9e9ef331
EB
4322 because of the sign-extension change in the latter conversion. */
4323 {
d90ec4f2
JM
4324 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4325 convert (TREE_TYPE (intop), size_exp));
9e9ef331
EB
4326 intop = convert (sizetype, t);
4327 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
807e902e 4328 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
9e9ef331 4329 }
5be014d5 4330
280f1ffa 4331 /* Create the sum or difference. */
5be014d5 4332 if (resultcode == MINUS_EXPR)
db3927fb 4333 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
7552da58 4334
5d49b6a7 4335 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
6ac01510
ILT
4336
4337 fold_undefer_and_ignore_overflow_warnings ();
4338
4339 return ret;
7552da58
JJ
4340}
4341\f
e5a94231
JM
4342/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4343 and if NON_CONST is known not to be permitted in an evaluated part
4344 of a constant expression. */
4345
4346tree
4347c_wrap_maybe_const (tree expr, bool non_const)
4348{
4349 bool nowarning = TREE_NO_WARNING (expr);
4350 location_t loc = EXPR_LOCATION (expr);
4351
4352 /* This should never be called for C++. */
4353 if (c_dialect_cxx ())
4354 gcc_unreachable ();
4355
4356 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4357 STRIP_TYPE_NOPS (expr);
4358 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4359 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4360 if (nowarning)
4361 TREE_NO_WARNING (expr) = 1;
4362 protected_set_expr_location (expr, loc);
4363
4364 return expr;
4365}
4366
928c19bb
JM
4367/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4368 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4369 around the SAVE_EXPR if needed so that c_fully_fold does not need
4370 to look inside SAVE_EXPRs. */
4371
4372tree
4373c_save_expr (tree expr)
4374{
4375 bool maybe_const = true;
4376 if (c_dialect_cxx ())
4377 return save_expr (expr);
4378 expr = c_fully_fold (expr, false, &maybe_const);
4379 expr = save_expr (expr);
4380 if (!maybe_const)
e5a94231 4381 expr = c_wrap_maybe_const (expr, true);
928c19bb
JM
4382 return expr;
4383}
4384
b3c6d2ea
ILT
4385/* Return whether EXPR is a declaration whose address can never be
4386 NULL. */
4387
4388bool
58f9752a 4389decl_with_nonnull_addr_p (const_tree expr)
b3c6d2ea
ILT
4390{
4391 return (DECL_P (expr)
4392 && (TREE_CODE (expr) == PARM_DECL
4393 || TREE_CODE (expr) == LABEL_DECL
4394 || !DECL_WEAK (expr)));
4395}
4396
b30f223b 4397/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
85498824
JM
4398 or for an `if' or `while' statement or ?..: exp. It should already
4399 have been validated to be of suitable type; otherwise, a bad
4400 diagnostic may result.
b30f223b 4401
ba47d38d
AH
4402 The EXPR is located at LOCATION.
4403
b30f223b
RS
4404 This preparation consists of taking the ordinary
4405 representation of an expression expr and producing a valid tree
4406 boolean expression describing whether expr is nonzero. We could
de7df9eb 4407 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b30f223b
RS
4408 but we optimize comparisons, &&, ||, and !.
4409
de7df9eb 4410 The resulting type should always be `truthvalue_type_node'. */
b30f223b
RS
4411
4412tree
ba47d38d 4413c_common_truthvalue_conversion (location_t location, tree expr)
b30f223b 4414{
b30f223b
RS
4415 switch (TREE_CODE (expr))
4416 {
d1a7edaf 4417 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
6f312d18
ZW
4418 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4419 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4420 case ORDERED_EXPR: case UNORDERED_EXPR:
90ec750d
RS
4421 if (TREE_TYPE (expr) == truthvalue_type_node)
4422 return expr;
c2255bc4 4423 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
90ec750d 4424 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
c2255bc4 4425 goto ret;
90ec750d 4426
b30f223b
RS
4427 case TRUTH_ANDIF_EXPR:
4428 case TRUTH_ORIF_EXPR:
4429 case TRUTH_AND_EXPR:
4430 case TRUTH_OR_EXPR:
9379fac9 4431 case TRUTH_XOR_EXPR:
90ec750d
RS
4432 if (TREE_TYPE (expr) == truthvalue_type_node)
4433 return expr;
c2255bc4 4434 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
b8698a0f 4435 c_common_truthvalue_conversion (location,
c2255bc4
AH
4436 TREE_OPERAND (expr, 0)),
4437 c_common_truthvalue_conversion (location,
4438 TREE_OPERAND (expr, 1)));
4439 goto ret;
18c0f675 4440
18d00205 4441 case TRUTH_NOT_EXPR:
90ec750d
RS
4442 if (TREE_TYPE (expr) == truthvalue_type_node)
4443 return expr;
c2255bc4
AH
4444 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4445 c_common_truthvalue_conversion (location,
4446 TREE_OPERAND (expr, 0)));
4447 goto ret;
18d00205 4448
b30f223b
RS
4449 case ERROR_MARK:
4450 return expr;
4451
4452 case INTEGER_CST:
d95787e6
RS
4453 return integer_zerop (expr) ? truthvalue_false_node
4454 : truthvalue_true_node;
b30f223b
RS
4455
4456 case REAL_CST:
010c4d9c
RS
4457 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4458 ? truthvalue_true_node
4459 : truthvalue_false_node;
b30f223b 4460
ab22c1fa
CF
4461 case FIXED_CST:
4462 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4463 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4464 ? truthvalue_true_node
4465 : truthvalue_false_node;
4466
90ec750d 4467 case FUNCTION_DECL:
c9f9eb5d 4468 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
90ec750d
RS
4469 /* Fall through. */
4470
b30f223b 4471 case ADDR_EXPR:
1998463c 4472 {
f6f08360 4473 tree inner = TREE_OPERAND (expr, 0);
b3c6d2ea 4474 if (decl_with_nonnull_addr_p (inner))
1998463c 4475 {
b3c6d2ea 4476 /* Common Ada/Pascal programmer's mistake. */
ba47d38d
AH
4477 warning_at (location,
4478 OPT_Waddress,
4479 "the address of %qD will always evaluate as %<true%>",
4480 inner);
1998463c
SB
4481 return truthvalue_true_node;
4482 }
33766b66 4483 break;
1998463c 4484 }
b30f223b 4485
766f6c30 4486 case COMPLEX_EXPR:
c2255bc4 4487 expr = build_binary_op (EXPR_LOCATION (expr),
ba47d38d 4488 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 4489 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
ba47d38d
AH
4490 c_common_truthvalue_conversion (location,
4491 TREE_OPERAND (expr, 0)),
4492 c_common_truthvalue_conversion (location,
4493 TREE_OPERAND (expr, 1)),
766f6c30 4494 0);
c2255bc4 4495 goto ret;
766f6c30 4496
b30f223b
RS
4497 case NEGATE_EXPR:
4498 case ABS_EXPR:
4499 case FLOAT_EXPR:
8ce94e44 4500 case EXCESS_PRECISION_EXPR:
da7d8304 4501 /* These don't change whether an object is nonzero or zero. */
ba47d38d 4502 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b30f223b
RS
4503
4504 case LROTATE_EXPR:
4505 case RROTATE_EXPR:
da7d8304 4506 /* These don't change whether an object is zero or nonzero, but
b30f223b
RS
4507 we can't ignore them if their second arg has side-effects. */
4508 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
c2255bc4
AH
4509 {
4510 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4511 TREE_OPERAND (expr, 1),
b8698a0f 4512 c_common_truthvalue_conversion
c2255bc4
AH
4513 (location, TREE_OPERAND (expr, 0)));
4514 goto ret;
4515 }
b30f223b 4516 else
ba47d38d
AH
4517 return c_common_truthvalue_conversion (location,
4518 TREE_OPERAND (expr, 0));
b57062ca 4519
b30f223b
RS
4520 case COND_EXPR:
4521 /* Distribute the conversion into the arms of a COND_EXPR. */
928c19bb 4522 if (c_dialect_cxx ())
c2255bc4 4523 {
4cc4f2f4
JJ
4524 tree op1 = TREE_OPERAND (expr, 1);
4525 tree op2 = TREE_OPERAND (expr, 2);
4526 /* In C++ one of the arms might have void type if it is throw. */
4527 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4528 op1 = c_common_truthvalue_conversion (location, op1);
4529 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4530 op2 = c_common_truthvalue_conversion (location, op2);
db3927fb 4531 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4cc4f2f4 4532 TREE_OPERAND (expr, 0), op1, op2);
c2255bc4
AH
4533 goto ret;
4534 }
928c19bb 4535 else
c2255bc4
AH
4536 {
4537 /* Folding will happen later for C. */
4538 expr = build3 (COND_EXPR, truthvalue_type_node,
4539 TREE_OPERAND (expr, 0),
4540 c_common_truthvalue_conversion (location,
4541 TREE_OPERAND (expr, 1)),
4542 c_common_truthvalue_conversion (location,
4543 TREE_OPERAND (expr, 2)));
4544 goto ret;
4545 }
b30f223b 4546
1043771b 4547 CASE_CONVERT:
1ee44b26
JM
4548 {
4549 tree totype = TREE_TYPE (expr);
4550 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4551
076fecad
PP
4552 if (POINTER_TYPE_P (totype)
4553 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4554 {
4555 tree inner = expr;
4556 STRIP_NOPS (inner);
4557
4558 if (DECL_P (inner))
4559 warning_at (location,
4560 OPT_Waddress,
4561 "the compiler can assume that the address of "
4562 "%qD will always evaluate to %<true%>",
4563 inner);
4564 }
4565
1ee44b26
JM
4566 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4567 since that affects how `default_conversion' will behave. */
4568 if (TREE_CODE (totype) == REFERENCE_TYPE
4569 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4570 break;
4571 /* Don't strip a conversion from C++0x scoped enum, since they
4572 don't implicitly convert to other types. */
4573 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4574 && ENUM_IS_SCOPED (fromtype))
4575 break;
4576 /* If this isn't narrowing the argument, we can ignore it. */
4577 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4578 return c_common_truthvalue_conversion (location,
4579 TREE_OPERAND (expr, 0));
4580 }
b30f223b
RS
4581 break;
4582
e2aab13d 4583 case MODIFY_EXPR:
fbc8d2d3
ILT
4584 if (!TREE_NO_WARNING (expr)
4585 && warn_parentheses)
4586 {
4587 warning (OPT_Wparentheses,
4588 "suggest parentheses around assignment used as truth value");
4589 TREE_NO_WARNING (expr) = 1;
4590 }
e2aab13d 4591 break;
b57062ca 4592
6d819282
MK
4593 default:
4594 break;
b30f223b
RS
4595 }
4596
f0b996c5 4597 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa 4598 {
5386338c 4599 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
c2255bc4 4600 expr = (build_binary_op
ba47d38d
AH
4601 (EXPR_LOCATION (expr),
4602 (TREE_SIDE_EFFECTS (expr)
f0b8d9aa 4603 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
c9f9eb5d
AH
4604 c_common_truthvalue_conversion
4605 (location,
4606 build_unary_op (location, REALPART_EXPR, t, 0)),
4607 c_common_truthvalue_conversion
4608 (location,
4609 build_unary_op (location, IMAGPART_EXPR, t, 0)),
f0b8d9aa 4610 0));
c2255bc4 4611 goto ret;
f0b8d9aa 4612 }
f0b996c5 4613
ab22c1fa
CF
4614 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4615 {
4616 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4617 FCONST0 (TYPE_MODE
4618 (TREE_TYPE (expr))));
ca80e52b 4619 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
ab22c1fa 4620 }
c2255bc4
AH
4621 else
4622 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
ab22c1fa 4623
c2255bc4
AH
4624 ret:
4625 protected_set_expr_location (expr, location);
4626 return expr;
b30f223b
RS
4627}
4628\f
9bc15050
RG
4629static void def_builtin_1 (enum built_in_function fncode,
4630 const char *name,
4631 enum built_in_class fnclass,
4632 tree fntype, tree libtype,
4633 bool both_p, bool fallback_p, bool nonansi_p,
4634 tree fnattrs, bool implicit_p);
fc2aaf30 4635
3932261a
MM
4636
4637/* Apply the TYPE_QUALS to the new DECL. */
4638
4639void
35b1a6fa 4640c_apply_type_quals_to_decl (int type_quals, tree decl)
3932261a 4641{
4b011bbf 4642 tree type = TREE_TYPE (decl);
9f63daea 4643
5a6159dd
AP
4644 if (type == error_mark_node)
4645 return;
4b011bbf 4646
329af3c7
JM
4647 if ((type_quals & TYPE_QUAL_CONST)
4648 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4649 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4650 constructor can produce constant init, so rely on cp_finish_decl to
4651 clear TREE_READONLY if the variable has non-constant init. */
3932261a
MM
4652 TREE_READONLY (decl) = 1;
4653 if (type_quals & TYPE_QUAL_VOLATILE)
4654 {
4655 TREE_SIDE_EFFECTS (decl) = 1;
4656 TREE_THIS_VOLATILE (decl) = 1;
4657 }
6946bc60 4658 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 4659 {
4b011bbf
JM
4660 while (type && TREE_CODE (type) == ARRAY_TYPE)
4661 /* Allow 'restrict' on arrays of pointers.
4662 FIXME currently we just ignore it. */
4663 type = TREE_TYPE (type);
4664 if (!type
4665 || !POINTER_TYPE_P (type)
4666 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
bda67431 4667 error ("invalid use of %<restrict%>");
3932261a
MM
4668 }
4669}
4670
ca752f39 4671struct c_type_hasher : ggc_ptr_hash<tree_node>
2a22f99c
TS
4672{
4673 static hashval_t hash (tree);
4674 static bool equal (tree, tree);
4675};
4676
6de9cd9a
DN
4677/* Hash function for the problem of multiple type definitions in
4678 different files. This must hash all types that will compare
4679 equal via comptypes to the same value. In practice it hashes
9cf737f8 4680 on some of the simple stuff and leaves the details to comptypes. */
6de9cd9a 4681
2a22f99c
TS
4682hashval_t
4683c_type_hasher::hash (tree t)
6de9cd9a 4684{
a19e4d44 4685 int n_elements;
6de9cd9a 4686 int shift, size;
6de9cd9a
DN
4687 tree t2;
4688 switch (TREE_CODE (t))
4689 {
8c27b7d4 4690 /* For pointers, hash on pointee type plus some swizzling. */
325c3691 4691 case POINTER_TYPE:
2a22f99c 4692 return hash (TREE_TYPE (t)) ^ 0x3003003;
325c3691
RH
4693 /* Hash on number of elements and total size. */
4694 case ENUMERAL_TYPE:
4695 shift = 3;
4696 t2 = TYPE_VALUES (t);
4697 break;
4698 case RECORD_TYPE:
4699 shift = 0;
4700 t2 = TYPE_FIELDS (t);
4701 break;
4702 case QUAL_UNION_TYPE:
4703 shift = 1;
4704 t2 = TYPE_FIELDS (t);
4705 break;
4706 case UNION_TYPE:
4707 shift = 2;
4708 t2 = TYPE_FIELDS (t);
4709 break;
4710 default:
366de0ce 4711 gcc_unreachable ();
6de9cd9a 4712 }
a19e4d44
NF
4713 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4714 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4715 n_elements = list_length (t2);
6fc3c3c0
TT
4716 /* We might have a VLA here. */
4717 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4718 size = 0;
4719 else
4720 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
a19e4d44 4721 return ((size << 24) | (n_elements << shift));
6de9cd9a
DN
4722}
4723
2a22f99c
TS
4724bool
4725c_type_hasher::equal (tree t1, tree t2)
4726{
4727 return lang_hooks.types_compatible_p (t1, t2);
4728}
4729
4730static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4fe52ce9 4731
41472af8 4732/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 4733 or a type. Return -1 if we don't do anything special. */
41472af8 4734
4862826d 4735alias_set_type
35b1a6fa 4736c_common_get_alias_set (tree t)
41472af8 4737{
08bc2431 4738 tree u;
35b1a6fa 4739
cb9c2485
JM
4740 /* For VLAs, use the alias set of the element type rather than the
4741 default of alias set 0 for types compared structurally. */
4742 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4743 {
4744 if (TREE_CODE (t) == ARRAY_TYPE)
4745 return get_alias_set (TREE_TYPE (t));
4746 return -1;
4747 }
4748
08bc2431
MM
4749 /* Permit type-punning when accessing a union, provided the access
4750 is directly through the union. For example, this code does not
4751 permit taking the address of a union member and then storing
4752 through it. Even the type-punning allowed here is a GCC
4753 extension, albeit a common and useful one; the C standard says
4754 that such accesses have implementation-defined behavior. */
4755 for (u = t;
4756 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4757 u = TREE_OPERAND (u, 0))
4758 if (TREE_CODE (u) == COMPONENT_REF
4759 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4760 return 0;
ece32014 4761
74d86f4f 4762 /* That's all the expressions we handle specially. */
3f75a254 4763 if (!TYPE_P (t))
74d86f4f
RH
4764 return -1;
4765
95bd1dd7 4766 /* The C standard guarantees that any object may be accessed via an
74d86f4f
RH
4767 lvalue that has character type. */
4768 if (t == char_type_node
4769 || t == signed_char_type_node
4770 || t == unsigned_char_type_node)
3bdf5ad1 4771 return 0;
3932261a 4772
f824e5c3
RK
4773 /* The C standard specifically allows aliasing between signed and
4774 unsigned variants of the same type. We treat the signed
4775 variant as canonical. */
8df83eae 4776 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
8f215dce 4777 {
ceef8ce4 4778 tree t1 = c_common_signed_type (t);
f824e5c3 4779
8f215dce
JJ
4780 /* t1 == t can happen for boolean nodes which are always unsigned. */
4781 if (t1 != t)
4782 return get_alias_set (t1);
4783 }
ece32014 4784
6de9cd9a
DN
4785 /* Handle the case of multiple type nodes referring to "the same" type,
4786 which occurs with IMA. These share an alias set. FIXME: Currently only
4787 C90 is handled. (In C99 type compatibility is not transitive, which
4788 complicates things mightily. The alias set splay trees can theoretically
4789 represent this, but insertion is tricky when you consider all the
4790 different orders things might arrive in.) */
4791
4792 if (c_language != clk_c || flag_isoc99)
4793 return -1;
4794
9cf737f8 4795 /* Save time if there's only one input file. */
d974312d 4796 if (num_in_fnames == 1)
6de9cd9a
DN
4797 return -1;
4798
4799 /* Pointers need special handling if they point to any type that
4800 needs special handling (below). */
4801 if (TREE_CODE (t) == POINTER_TYPE)
4802 {
4803 tree t2;
4804 /* Find bottom type under any nested POINTERs. */
9f63daea 4805 for (t2 = TREE_TYPE (t);
762f7d9d
TT
4806 TREE_CODE (t2) == POINTER_TYPE;
4807 t2 = TREE_TYPE (t2))
4808 ;
a868811e
MP
4809 if (!RECORD_OR_UNION_TYPE_P (t2)
4810 && TREE_CODE (t2) != ENUMERAL_TYPE)
762f7d9d 4811 return -1;
6de9cd9a 4812 if (TYPE_SIZE (t2) == 0)
762f7d9d 4813 return -1;
6de9cd9a
DN
4814 }
4815 /* These are the only cases that need special handling. */
a868811e 4816 if (!RECORD_OR_UNION_TYPE_P (t)
6de9cd9a 4817 && TREE_CODE (t) != ENUMERAL_TYPE
6de9cd9a
DN
4818 && TREE_CODE (t) != POINTER_TYPE)
4819 return -1;
4820 /* Undefined? */
4821 if (TYPE_SIZE (t) == 0)
4822 return -1;
4823
9f63daea 4824 /* Look up t in hash table. Only one of the compatible types within each
6de9cd9a
DN
4825 alias set is recorded in the table. */
4826 if (!type_hash_table)
2a22f99c
TS
4827 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4828 tree *slot = type_hash_table->find_slot (t, INSERT);
6de9cd9a 4829 if (*slot != NULL)
6a3203c8
AP
4830 {
4831 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4832 return TYPE_ALIAS_SET ((tree)*slot);
4833 }
6de9cd9a
DN
4834 else
4835 /* Our caller will assign and record (in t) a new alias set; all we need
4836 to do is remember t in the hash table. */
4837 *slot = t;
4838
3bdf5ad1 4839 return -1;
41472af8 4840}
0213a355 4841\f
c2255bc4 4842/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
296674db 4843 the IS_SIZEOF parameter indicates which operator is being applied.
c2255bc4
AH
4844 The COMPLAIN flag controls whether we should diagnose possibly
4845 ill-formed constructs or not. LOC is the location of the SIZEOF or
296674db
JM
4846 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4847 a type in any context should be returned, rather than the normal
4848 alignment for that type. */
03a08664 4849
0213a355 4850tree
c2255bc4 4851c_sizeof_or_alignof_type (location_t loc,
296674db
JM
4852 tree type, bool is_sizeof, bool min_alignof,
4853 int complain)
0213a355 4854{
fa72b064
GDR
4855 const char *op_name;
4856 tree value = NULL;
4857 enum tree_code type_code = TREE_CODE (type);
35b1a6fa 4858
03a08664 4859 op_name = is_sizeof ? "sizeof" : "__alignof__";
35b1a6fa 4860
fa72b064 4861 if (type_code == FUNCTION_TYPE)
0213a355 4862 {
03a08664 4863 if (is_sizeof)
fa72b064 4864 {
44d90fe1
PC
4865 if (complain && warn_pointer_arith)
4866 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 4867 "invalid application of %<sizeof%> to a function type");
5ade1ed2
DG
4868 else if (!complain)
4869 return error_mark_node;
fa72b064
GDR
4870 value = size_one_node;
4871 }
4872 else
d19fa6b5
JM
4873 {
4874 if (complain)
4875 {
4876 if (c_dialect_cxx ())
c1771a20 4877 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
d19fa6b5
JM
4878 "%<alignof%> applied to a function type");
4879 else
c1771a20 4880 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
d19fa6b5
JM
4881 "%<_Alignof%> applied to a function type");
4882 }
4883 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4884 }
fa72b064
GDR
4885 }
4886 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4887 {
35b1a6fa 4888 if (type_code == VOID_TYPE
44d90fe1
PC
4889 && complain && warn_pointer_arith)
4890 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 4891 "invalid application of %qs to a void type", op_name);
5ade1ed2
DG
4892 else if (!complain)
4893 return error_mark_node;
fa72b064 4894 value = size_one_node;
0213a355 4895 }
73ac190a
PC
4896 else if (!COMPLETE_TYPE_P (type)
4897 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
0213a355 4898 {
ea793912 4899 if (complain)
73ac190a 4900 error_at (loc, "invalid application of %qs to incomplete type %qT",
c2255bc4 4901 op_name, type);
cb6addf4 4902 return error_mark_node;
0213a355 4903 }
73ac190a
PC
4904 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4905 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4906 {
4907 if (complain)
4908 error_at (loc, "invalid application of %qs to array type %qT of "
4909 "incomplete element type", op_name, type);
4910 return error_mark_node;
4911 }
0213a355 4912 else
fa72b064 4913 {
03a08664 4914 if (is_sizeof)
fa72b064 4915 /* Convert in case a char is more than one unit. */
db3927fb
AH
4916 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4917 size_int (TYPE_PRECISION (char_type_node)
4918 / BITS_PER_UNIT));
296674db 4919 else if (min_alignof)
2793eeab 4920 value = size_int (min_align_of_type (type));
fa72b064 4921 else
a4e9ffe5 4922 value = size_int (TYPE_ALIGN_UNIT (type));
fa72b064 4923 }
0213a355 4924
3ac8781c
RG
4925 /* VALUE will have the middle-end integer type sizetype.
4926 However, we should really return a value of type `size_t',
4927 which is just a typedef for an ordinary integer type. */
db3927fb 4928 value = fold_convert_loc (loc, size_type_node, value);
35b1a6fa 4929
fa72b064 4930 return value;
0213a355
JM
4931}
4932
4933/* Implement the __alignof keyword: Return the minimum required
837edd5f
GK
4934 alignment of EXPR, measured in bytes. For VAR_DECLs,
4935 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
c2255bc4
AH
4936 from an "aligned" __attribute__ specification). LOC is the
4937 location of the ALIGNOF operator. */
7f4edbcb 4938
0213a355 4939tree
c2255bc4 4940c_alignof_expr (location_t loc, tree expr)
0213a355
JM
4941{
4942 tree t;
4943
837edd5f 4944 if (VAR_OR_FUNCTION_DECL_P (expr))
a4e9ffe5 4945 t = size_int (DECL_ALIGN_UNIT (expr));
35b1a6fa 4946
0213a355
JM
4947 else if (TREE_CODE (expr) == COMPONENT_REF
4948 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4949 {
c2255bc4 4950 error_at (loc, "%<__alignof%> applied to a bit-field");
0213a355
JM
4951 t = size_one_node;
4952 }
4953 else if (TREE_CODE (expr) == COMPONENT_REF
173bf5be 4954 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
a4e9ffe5 4955 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
35b1a6fa 4956
22d03525 4957 else if (INDIRECT_REF_P (expr))
0213a355
JM
4958 {
4959 tree t = TREE_OPERAND (expr, 0);
4960 tree best = t;
4961 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
35b1a6fa 4962
1043771b 4963 while (CONVERT_EXPR_P (t)
173bf5be 4964 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
0213a355
JM
4965 {
4966 int thisalign;
4967
4968 t = TREE_OPERAND (t, 0);
4969 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4970 if (thisalign > bestalign)
4971 best = t, bestalign = thisalign;
4972 }
c2255bc4 4973 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
0213a355
JM
4974 }
4975 else
c2255bc4 4976 return c_alignof (loc, TREE_TYPE (expr));
0213a355 4977
db3927fb 4978 return fold_convert_loc (loc, size_type_node, t);
0213a355
JM
4979}
4980\f
df061a43
RS
4981/* Handle C and C++ default attributes. */
4982
4983enum built_in_attribute
4984{
4985#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4986#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
e384e6b5 4987#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
df061a43
RS
4988#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4989#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
df061a43
RS
4990#include "builtin-attrs.def"
4991#undef DEF_ATTR_NULL_TREE
4992#undef DEF_ATTR_INT
e384e6b5 4993#undef DEF_ATTR_STRING
df061a43
RS
4994#undef DEF_ATTR_IDENT
4995#undef DEF_ATTR_TREE_LIST
df061a43
RS
4996 ATTR_LAST
4997};
4998
4999static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5000
35b1a6fa 5001static void c_init_attributes (void);
df061a43 5002
a0274e3e 5003enum c_builtin_type
7f4edbcb 5004{
10841285
MM
5005#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5006#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5007#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5008#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5009#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5010#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
10a0d495 5011#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
f6a7cffc
TS
5012#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5013 ARG6) NAME,
5014#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5015 ARG6, ARG7) NAME,
5016#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5017 ARG6, ARG7, ARG8) NAME,
d9a6bd32
JJ
5018#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5019 ARG6, ARG7, ARG8, ARG9) NAME,
5020#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5021 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5022#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5023 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
10841285
MM
5024#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5025#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5026#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
08291658 5027#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
10a0d495 5028#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
f6a7cffc 5029#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
56a9f6bc 5030 NAME,
3e32ee19
NS
5031#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5032 ARG6) NAME,
56a9f6bc
TS
5033#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5034 ARG6, ARG7) NAME,
10841285
MM
5035#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5036#include "builtin-types.def"
5037#undef DEF_PRIMITIVE_TYPE
5038#undef DEF_FUNCTION_TYPE_0
5039#undef DEF_FUNCTION_TYPE_1
5040#undef DEF_FUNCTION_TYPE_2
5041#undef DEF_FUNCTION_TYPE_3
5042#undef DEF_FUNCTION_TYPE_4
10a0d495
JJ
5043#undef DEF_FUNCTION_TYPE_5
5044#undef DEF_FUNCTION_TYPE_6
a0274e3e 5045#undef DEF_FUNCTION_TYPE_7
acf0174b 5046#undef DEF_FUNCTION_TYPE_8
d9a6bd32
JJ
5047#undef DEF_FUNCTION_TYPE_9
5048#undef DEF_FUNCTION_TYPE_10
5049#undef DEF_FUNCTION_TYPE_11
10841285
MM
5050#undef DEF_FUNCTION_TYPE_VAR_0
5051#undef DEF_FUNCTION_TYPE_VAR_1
5052#undef DEF_FUNCTION_TYPE_VAR_2
08291658 5053#undef DEF_FUNCTION_TYPE_VAR_3
10a0d495
JJ
5054#undef DEF_FUNCTION_TYPE_VAR_4
5055#undef DEF_FUNCTION_TYPE_VAR_5
3e32ee19 5056#undef DEF_FUNCTION_TYPE_VAR_6
56a9f6bc 5057#undef DEF_FUNCTION_TYPE_VAR_7
10841285 5058#undef DEF_POINTER_TYPE
a0274e3e
JJ
5059 BT_LAST
5060};
5061
5062typedef enum c_builtin_type builtin_type;
10841285 5063
a0274e3e
JJ
5064/* A temporary array for c_common_nodes_and_builtins. Used in
5065 communication with def_fn_type. */
5066static tree builtin_types[(int) BT_LAST + 1];
10841285 5067
a0274e3e
JJ
5068/* A helper function for c_common_nodes_and_builtins. Build function type
5069 for DEF with return type RET and N arguments. If VAR is true, then the
5070 function should be variadic after those N arguments.
5071
5072 Takes special care not to ICE if any of the types involved are
5073 error_mark_node, which indicates that said type is not in fact available
5074 (see builtin_type_for_size). In which case the function type as a whole
5075 should be error_mark_node. */
5076
5077static void
5078def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5079{
8242dd04
NF
5080 tree t;
5081 tree *args = XALLOCAVEC (tree, n);
a0274e3e
JJ
5082 va_list list;
5083 int i;
5084
5085 va_start (list, n);
5086 for (i = 0; i < n; ++i)
5087 {
d75d71e0 5088 builtin_type a = (builtin_type) va_arg (list, int);
a0274e3e
JJ
5089 t = builtin_types[a];
5090 if (t == error_mark_node)
5091 goto egress;
8242dd04 5092 args[i] = t;
a0274e3e 5093 }
a0274e3e 5094
a0274e3e
JJ
5095 t = builtin_types[ret];
5096 if (t == error_mark_node)
5097 goto egress;
8242dd04
NF
5098 if (var)
5099 t = build_varargs_function_type_array (t, n, args);
5100 else
5101 t = build_function_type_array (t, n, args);
a0274e3e
JJ
5102
5103 egress:
5104 builtin_types[def] = t;
0edf1bb2 5105 va_end (list);
a0274e3e
JJ
5106}
5107
c6d86fce
ILT
5108/* Build builtin functions common to both C and C++ language
5109 frontends. */
5110
5111static void
5112c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5113{
5114#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5115 builtin_types[ENUM] = VALUE;
5116#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5117 def_fn_type (ENUM, RETURN, 0, 0);
5118#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5119 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5120#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5121 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5122#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5123 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5124#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5125 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5126#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5127 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5128#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5129 ARG6) \
5130 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5131#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5132 ARG6, ARG7) \
5133 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
acf0174b
JJ
5134#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5135 ARG6, ARG7, ARG8) \
5136 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5137 ARG7, ARG8);
d9a6bd32
JJ
5138#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5139 ARG6, ARG7, ARG8, ARG9) \
5140 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5141 ARG7, ARG8, ARG9);
5142#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5143 ARG6, ARG7, ARG8, ARG9, ARG10) \
5144 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5145 ARG7, ARG8, ARG9, ARG10);
5146#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5147 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5148 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5149 ARG7, ARG8, ARG9, ARG10, ARG11);
c6d86fce
ILT
5150#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5151 def_fn_type (ENUM, RETURN, 1, 0);
5152#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5153 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5154#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5155 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5156#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5157 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5158#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5159 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5160#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5161 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3e32ee19
NS
5162#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5163 ARG6) \
5164 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
56a9f6bc
TS
5165#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5166 ARG6, ARG7) \
5167 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
c6d86fce
ILT
5168#define DEF_POINTER_TYPE(ENUM, TYPE) \
5169 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5170
5171#include "builtin-types.def"
5172
5173#undef DEF_PRIMITIVE_TYPE
f6a7cffc 5174#undef DEF_FUNCTION_TYPE_0
c6d86fce
ILT
5175#undef DEF_FUNCTION_TYPE_1
5176#undef DEF_FUNCTION_TYPE_2
5177#undef DEF_FUNCTION_TYPE_3
5178#undef DEF_FUNCTION_TYPE_4
5179#undef DEF_FUNCTION_TYPE_5
5180#undef DEF_FUNCTION_TYPE_6
f6a7cffc
TS
5181#undef DEF_FUNCTION_TYPE_7
5182#undef DEF_FUNCTION_TYPE_8
d9a6bd32
JJ
5183#undef DEF_FUNCTION_TYPE_9
5184#undef DEF_FUNCTION_TYPE_10
5185#undef DEF_FUNCTION_TYPE_11
c6d86fce
ILT
5186#undef DEF_FUNCTION_TYPE_VAR_0
5187#undef DEF_FUNCTION_TYPE_VAR_1
5188#undef DEF_FUNCTION_TYPE_VAR_2
5189#undef DEF_FUNCTION_TYPE_VAR_3
5190#undef DEF_FUNCTION_TYPE_VAR_4
5191#undef DEF_FUNCTION_TYPE_VAR_5
3e32ee19 5192#undef DEF_FUNCTION_TYPE_VAR_6
56a9f6bc 5193#undef DEF_FUNCTION_TYPE_VAR_7
c6d86fce
ILT
5194#undef DEF_POINTER_TYPE
5195 builtin_types[(int) BT_LAST] = NULL_TREE;
5196
5197 c_init_attributes ();
5198
5199#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5200 NONANSI_P, ATTRS, IMPLICIT, COND) \
5201 if (NAME && COND) \
5202 def_builtin_1 (ENUM, NAME, CLASS, \
5203 builtin_types[(int) TYPE], \
5204 builtin_types[(int) LIBTYPE], \
5205 BOTH_P, FALLBACK_P, NONANSI_P, \
5206 built_in_attributes[(int) ATTRS], IMPLICIT);
5207#include "builtins.def"
c6d86fce 5208
8de7ef2a
UB
5209 targetm.init_builtins ();
5210
384c400a 5211 build_common_builtin_nodes ();
939b37da 5212
b72271b9 5213 if (flag_cilkplus)
939b37da 5214 cilk_init_builtins ();
c6d86fce
ILT
5215}
5216
c1b61fca
JM
5217/* Like get_identifier, but avoid warnings about null arguments when
5218 the argument may be NULL for targets where GCC lacks stdint.h type
5219 information. */
5220
5221static inline tree
5222c_get_ident (const char *id)
5223{
5224 return get_identifier (id);
5225}
5226
a0274e3e
JJ
5227/* Build tree nodes and builtin functions common to both C and C++ language
5228 frontends. */
5229
5230void
5231c_common_nodes_and_builtins (void)
5232{
b6baa67d
KVH
5233 int char16_type_size;
5234 int char32_type_size;
eaa7c03f
JM
5235 int wchar_type_size;
5236 tree array_domain_type;
9f720c3e 5237 tree va_list_ref_type_node;
daf68dd7 5238 tree va_list_arg_type_node;
78a7c317 5239 int i;
d3707adb 5240
a011cd92 5241 build_common_tree_nodes (flag_signed_char);
fce5dddd 5242
eaa7c03f 5243 /* Define `int' and `char' first so that dbx will output them first. */
6496a589 5244 record_builtin_type (RID_INT, NULL, integer_type_node);
eaa7c03f
JM
5245 record_builtin_type (RID_CHAR, "char", char_type_node);
5246
5247 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5248 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5249 but not C. Are the conditionals here needed? */
37fa72e9 5250 if (c_dialect_cxx ())
6496a589 5251 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
eaa7c03f
JM
5252 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5253 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5254 record_builtin_type (RID_MAX, "long unsigned int",
5255 long_unsigned_type_node);
78a7c317
DD
5256
5257 for (i = 0; i < NUM_INT_N_ENTS; i ++)
a6766312 5258 {
78a7c317
DD
5259 char name[25];
5260
5261 sprintf (name, "__int%d", int_n_data[i].bitsize);
17958621 5262 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
78a7c317
DD
5263 int_n_trees[i].signed_type);
5264 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
17958621 5265 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
a6766312 5266 }
78a7c317 5267
37fa72e9 5268 if (c_dialect_cxx ())
eaa7c03f
JM
5269 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5270 record_builtin_type (RID_MAX, "long long int",
5271 long_long_integer_type_node);
5272 record_builtin_type (RID_MAX, "long long unsigned int",
5273 long_long_unsigned_type_node);
37fa72e9 5274 if (c_dialect_cxx ())
eaa7c03f
JM
5275 record_builtin_type (RID_MAX, "long long unsigned",
5276 long_long_unsigned_type_node);
5277 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5278 record_builtin_type (RID_MAX, "short unsigned int",
5279 short_unsigned_type_node);
37fa72e9 5280 if (c_dialect_cxx ())
eaa7c03f
JM
5281 record_builtin_type (RID_MAX, "unsigned short",
5282 short_unsigned_type_node);
5283
5284 /* Define both `signed char' and `unsigned char'. */
5285 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5286 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5287
b0c48229
NB
5288 /* These are types that c_common_type_for_size and
5289 c_common_type_for_mode use. */
c2255bc4
AH
5290 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5291 TYPE_DECL, NULL_TREE,
ae2bcd98 5292 intQI_type_node));
c2255bc4
AH
5293 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5294 TYPE_DECL, NULL_TREE,
ae2bcd98 5295 intHI_type_node));
c2255bc4
AH
5296 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5297 TYPE_DECL, NULL_TREE,
ae2bcd98 5298 intSI_type_node));
c2255bc4
AH
5299 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5300 TYPE_DECL, NULL_TREE,
ae2bcd98 5301 intDI_type_node));
eaa7c03f 5302#if HOST_BITS_PER_WIDE_INT >= 64
78a7c317
DD
5303 /* Note that this is different than the __int128 type that's part of
5304 the generic __intN support. */
1e1b8649 5305 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5306 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5307 TYPE_DECL,
1e1b8649
AP
5308 get_identifier ("__int128_t"),
5309 intTI_type_node));
eaa7c03f 5310#endif
c2255bc4
AH
5311 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5312 TYPE_DECL, NULL_TREE,
ae2bcd98 5313 unsigned_intQI_type_node));
c2255bc4
AH
5314 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5315 TYPE_DECL, NULL_TREE,
ae2bcd98 5316 unsigned_intHI_type_node));
c2255bc4
AH
5317 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5318 TYPE_DECL, NULL_TREE,
ae2bcd98 5319 unsigned_intSI_type_node));
c2255bc4
AH
5320 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5321 TYPE_DECL, NULL_TREE,
ae2bcd98 5322 unsigned_intDI_type_node));
eaa7c03f 5323#if HOST_BITS_PER_WIDE_INT >= 64
1e1b8649 5324 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5325 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5326 TYPE_DECL,
1e1b8649
AP
5327 get_identifier ("__uint128_t"),
5328 unsigned_intTI_type_node));
eaa7c03f
JM
5329#endif
5330
5331 /* Create the widest literal types. */
5332 widest_integer_literal_type_node
5333 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5334 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5335 TYPE_DECL, NULL_TREE,
ae2bcd98 5336 widest_integer_literal_type_node));
eaa7c03f
JM
5337
5338 widest_unsigned_literal_type_node
5339 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5340 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5341 TYPE_DECL, NULL_TREE,
ae2bcd98 5342 widest_unsigned_literal_type_node));
eaa7c03f 5343
c9f8536c 5344 signed_size_type_node = c_common_signed_type (size_type_node);
eaa7c03f 5345
d1c38823
ZD
5346 pid_type_node =
5347 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5348
6496a589
KG
5349 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5350 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
eaa7c03f
JM
5351 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5352
9a8ce21f
JG
5353 /* Only supported decimal floating point extension if the target
5354 actually supports underlying modes. */
b8698a0f 5355 if (targetm.scalar_mode_supported_p (SDmode)
9a8ce21f
JG
5356 && targetm.scalar_mode_supported_p (DDmode)
5357 && targetm.scalar_mode_supported_p (TDmode))
5358 {
5359 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5360 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5361 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5362 }
5363
ab22c1fa
CF
5364 if (targetm.fixed_point_supported_p ())
5365 {
5366 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5367 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5368 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5369 record_builtin_type (RID_MAX, "long long _Fract",
5370 long_long_fract_type_node);
5371 record_builtin_type (RID_MAX, "unsigned short _Fract",
5372 unsigned_short_fract_type_node);
5373 record_builtin_type (RID_MAX, "unsigned _Fract",
5374 unsigned_fract_type_node);
5375 record_builtin_type (RID_MAX, "unsigned long _Fract",
5376 unsigned_long_fract_type_node);
5377 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5378 unsigned_long_long_fract_type_node);
5379 record_builtin_type (RID_MAX, "_Sat short _Fract",
5380 sat_short_fract_type_node);
5381 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5382 record_builtin_type (RID_MAX, "_Sat long _Fract",
5383 sat_long_fract_type_node);
5384 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5385 sat_long_long_fract_type_node);
5386 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5387 sat_unsigned_short_fract_type_node);
5388 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5389 sat_unsigned_fract_type_node);
5390 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5391 sat_unsigned_long_fract_type_node);
5392 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5393 sat_unsigned_long_long_fract_type_node);
5394 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5395 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5396 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5397 record_builtin_type (RID_MAX, "long long _Accum",
5398 long_long_accum_type_node);
5399 record_builtin_type (RID_MAX, "unsigned short _Accum",
5400 unsigned_short_accum_type_node);
5401 record_builtin_type (RID_MAX, "unsigned _Accum",
5402 unsigned_accum_type_node);
5403 record_builtin_type (RID_MAX, "unsigned long _Accum",
5404 unsigned_long_accum_type_node);
5405 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5406 unsigned_long_long_accum_type_node);
5407 record_builtin_type (RID_MAX, "_Sat short _Accum",
5408 sat_short_accum_type_node);
5409 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5410 record_builtin_type (RID_MAX, "_Sat long _Accum",
5411 sat_long_accum_type_node);
5412 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5413 sat_long_long_accum_type_node);
5414 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5415 sat_unsigned_short_accum_type_node);
5416 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5417 sat_unsigned_accum_type_node);
5418 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5419 sat_unsigned_long_accum_type_node);
5420 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5421 sat_unsigned_long_long_accum_type_node);
5422
5423 }
5424
c2255bc4
AH
5425 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5426 TYPE_DECL,
ae2bcd98
RS
5427 get_identifier ("complex int"),
5428 complex_integer_type_node));
c2255bc4
AH
5429 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5430 TYPE_DECL,
ae2bcd98
RS
5431 get_identifier ("complex float"),
5432 complex_float_type_node));
c2255bc4
AH
5433 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5434 TYPE_DECL,
ae2bcd98
RS
5435 get_identifier ("complex double"),
5436 complex_double_type_node));
5437 lang_hooks.decls.pushdecl
c2255bc4
AH
5438 (build_decl (UNKNOWN_LOCATION,
5439 TYPE_DECL, get_identifier ("complex long double"),
43577e6b 5440 complex_long_double_type_node));
eaa7c03f 5441
498c0f27
JJ
5442 if (c_dialect_cxx ())
5443 /* For C++, make fileptr_type_node a distinct void * type until
5444 FILE type is defined. */
8dd16ecc 5445 fileptr_type_node = build_variant_type_copy (ptr_type_node);
498c0f27 5446
6496a589 5447 record_builtin_type (RID_VOID, NULL, void_type_node);
eaa7c03f 5448
06d40de8
DG
5449 /* Set the TYPE_NAME for any variants that were built before
5450 record_builtin_type gave names to the built-in types. */
5451 {
5452 tree void_name = TYPE_NAME (void_type_node);
5453 TYPE_NAME (void_type_node) = NULL_TREE;
5454 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5455 = void_name;
5456 TYPE_NAME (void_type_node) = void_name;
5457 }
5458
eaa7c03f
JM
5459 void_list_node = build_void_list_node ();
5460
5461 /* Make a type to be the domain of a few array types
5462 whose domains don't really matter.
5463 200 is small enough that it always fits in size_t
5464 and large enough that it can hold most function names for the
5465 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5466 array_domain_type = build_index_type (size_int (200));
5467
5468 /* Make a type for arrays of characters.
5469 With luck nothing will ever really depend on the length of this
5470 array type. */
5471 char_array_type_node
5472 = build_array_type (char_type_node, array_domain_type);
5473
10841285
MM
5474 string_type_node = build_pointer_type (char_type_node);
5475 const_string_type_node
5476 = build_pointer_type (build_qualified_type
5477 (char_type_node, TYPE_QUAL_CONST));
5478
eaa7c03f 5479 /* This is special for C++ so functions can be overloaded. */
a11eba95 5480 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
eaa7c03f
JM
5481 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5482 wchar_type_size = TYPE_PRECISION (wchar_type_node);
207bf79d 5483 underlying_wchar_type_node = wchar_type_node;
37fa72e9 5484 if (c_dialect_cxx ())
eaa7c03f 5485 {
8df83eae 5486 if (TYPE_UNSIGNED (wchar_type_node))
eaa7c03f
JM
5487 wchar_type_node = make_unsigned_type (wchar_type_size);
5488 else
5489 wchar_type_node = make_signed_type (wchar_type_size);
5490 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5491 }
eaa7c03f
JM
5492
5493 /* This is for wide string constants. */
5494 wchar_array_type_node
5495 = build_array_type (wchar_type_node, array_domain_type);
5496
b6baa67d
KVH
5497 /* Define 'char16_t'. */
5498 char16_type_node = get_identifier (CHAR16_TYPE);
5499 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5500 char16_type_size = TYPE_PRECISION (char16_type_node);
5501 if (c_dialect_cxx ())
5502 {
5503 char16_type_node = make_unsigned_type (char16_type_size);
5504
604b2bfc 5505 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5506 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5507 }
5508
5509 /* This is for UTF-16 string constants. */
5510 char16_array_type_node
5511 = build_array_type (char16_type_node, array_domain_type);
5512
5513 /* Define 'char32_t'. */
5514 char32_type_node = get_identifier (CHAR32_TYPE);
5515 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5516 char32_type_size = TYPE_PRECISION (char32_type_node);
5517 if (c_dialect_cxx ())
5518 {
5519 char32_type_node = make_unsigned_type (char32_type_size);
5520
604b2bfc 5521 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5522 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5523 }
5524
5525 /* This is for UTF-32 string constants. */
5526 char32_array_type_node
5527 = build_array_type (char32_type_node, array_domain_type);
5528
5fd8e536
JM
5529 wint_type_node =
5530 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5531
5532 intmax_type_node =
5533 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5534 uintmax_type_node =
5535 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5536
207bf79d
JM
5537 if (SIG_ATOMIC_TYPE)
5538 sig_atomic_type_node =
c1b61fca 5539 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
207bf79d
JM
5540 if (INT8_TYPE)
5541 int8_type_node =
c1b61fca 5542 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
207bf79d
JM
5543 if (INT16_TYPE)
5544 int16_type_node =
c1b61fca 5545 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
207bf79d
JM
5546 if (INT32_TYPE)
5547 int32_type_node =
c1b61fca 5548 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
207bf79d
JM
5549 if (INT64_TYPE)
5550 int64_type_node =
c1b61fca 5551 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
207bf79d
JM
5552 if (UINT8_TYPE)
5553 uint8_type_node =
c1b61fca 5554 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
207bf79d 5555 if (UINT16_TYPE)
cca615af 5556 c_uint16_type_node = uint16_type_node =
c1b61fca 5557 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
207bf79d 5558 if (UINT32_TYPE)
cca615af 5559 c_uint32_type_node = uint32_type_node =
c1b61fca 5560 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
207bf79d 5561 if (UINT64_TYPE)
cca615af 5562 c_uint64_type_node = uint64_type_node =
c1b61fca 5563 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
207bf79d
JM
5564 if (INT_LEAST8_TYPE)
5565 int_least8_type_node =
c1b61fca 5566 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
207bf79d
JM
5567 if (INT_LEAST16_TYPE)
5568 int_least16_type_node =
c1b61fca 5569 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
207bf79d
JM
5570 if (INT_LEAST32_TYPE)
5571 int_least32_type_node =
c1b61fca 5572 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
207bf79d
JM
5573 if (INT_LEAST64_TYPE)
5574 int_least64_type_node =
c1b61fca 5575 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
207bf79d
JM
5576 if (UINT_LEAST8_TYPE)
5577 uint_least8_type_node =
c1b61fca 5578 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
207bf79d
JM
5579 if (UINT_LEAST16_TYPE)
5580 uint_least16_type_node =
c1b61fca 5581 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
207bf79d
JM
5582 if (UINT_LEAST32_TYPE)
5583 uint_least32_type_node =
c1b61fca 5584 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
207bf79d
JM
5585 if (UINT_LEAST64_TYPE)
5586 uint_least64_type_node =
c1b61fca 5587 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
207bf79d
JM
5588 if (INT_FAST8_TYPE)
5589 int_fast8_type_node =
c1b61fca 5590 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
207bf79d
JM
5591 if (INT_FAST16_TYPE)
5592 int_fast16_type_node =
c1b61fca 5593 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
207bf79d
JM
5594 if (INT_FAST32_TYPE)
5595 int_fast32_type_node =
c1b61fca 5596 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
207bf79d
JM
5597 if (INT_FAST64_TYPE)
5598 int_fast64_type_node =
c1b61fca 5599 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
207bf79d
JM
5600 if (UINT_FAST8_TYPE)
5601 uint_fast8_type_node =
c1b61fca 5602 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
207bf79d
JM
5603 if (UINT_FAST16_TYPE)
5604 uint_fast16_type_node =
c1b61fca 5605 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
207bf79d
JM
5606 if (UINT_FAST32_TYPE)
5607 uint_fast32_type_node =
c1b61fca 5608 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
207bf79d
JM
5609 if (UINT_FAST64_TYPE)
5610 uint_fast64_type_node =
c1b61fca 5611 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
207bf79d
JM
5612 if (INTPTR_TYPE)
5613 intptr_type_node =
c1b61fca 5614 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
207bf79d
JM
5615 if (UINTPTR_TYPE)
5616 uintptr_type_node =
c1b61fca 5617 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
207bf79d 5618
8242dd04
NF
5619 default_function_type
5620 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5fd8e536
JM
5621 ptrdiff_type_node
5622 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
12753674 5623 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5fd8e536 5624
ae2bcd98 5625 lang_hooks.decls.pushdecl
c2255bc4
AH
5626 (build_decl (UNKNOWN_LOCATION,
5627 TYPE_DECL, get_identifier ("__builtin_va_list"),
43577e6b 5628 va_list_type_node));
38f8b050 5629 if (targetm.enum_va_list_p)
d4048208
KT
5630 {
5631 int l;
5632 const char *pname;
5633 tree ptype;
35cbb299 5634
38f8b050 5635 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
d4048208
KT
5636 {
5637 lang_hooks.decls.pushdecl
5638 (build_decl (UNKNOWN_LOCATION,
5639 TYPE_DECL, get_identifier (pname),
5640 ptype));
5641
5642 }
5643 }
daf68dd7 5644
daf68dd7 5645 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
5646 {
5647 va_list_arg_type_node = va_list_ref_type_node =
5648 build_pointer_type (TREE_TYPE (va_list_type_node));
5649 }
daf68dd7 5650 else
9f720c3e
GK
5651 {
5652 va_list_arg_type_node = va_list_type_node;
5653 va_list_ref_type_node = build_reference_type (va_list_type_node);
5654 }
35b1a6fa 5655
c6d86fce
ILT
5656 if (!flag_preprocess_only)
5657 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4677862a 5658
5b47282c 5659 main_identifier_node = get_identifier ("main");
b2f97e4a
MM
5660
5661 /* Create the built-in __null node. It is important that this is
5662 not shared. */
807e902e 5663 null_node = make_int_cst (1, 1);
b2f97e4a 5664 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
a0274e3e
JJ
5665
5666 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5667 memset (builtin_types, 0, sizeof (builtin_types));
7f4edbcb 5668}
d3707adb 5669
3b2db49f
MM
5670/* The number of named compound-literals generated thus far. */
5671static GTY(()) int compound_literal_number;
5672
5673/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5674
5675void
5676set_compound_literal_name (tree decl)
5677{
5678 char *name;
5679 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5680 compound_literal_number);
5681 compound_literal_number++;
5682 DECL_NAME (decl) = get_identifier (name);
5683}
5684
2fe1d762
TV
5685/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5686 TYPE and operand OP. */
5687
5688static tree
5689build_va_arg_1 (location_t loc, tree type, tree op)
5690{
5691 tree expr = build1 (VA_ARG_EXPR, type, op);
5692 SET_EXPR_LOCATION (expr, loc);
5693 return expr;
5694}
5695
5696/* Return a VA_ARG_EXPR corresponding to a source-level expression
5697 va_arg (EXPR, TYPE) at source location LOC. */
5698
d3707adb 5699tree
c2255bc4 5700build_va_arg (location_t loc, tree expr, tree type)
d3707adb 5701{
c7b38fd5
TV
5702 tree va_type = TREE_TYPE (expr);
5703 tree canon_va_type = (va_type == error_mark_node
5704 ? NULL_TREE
5705 : targetm.canonical_va_list_type (va_type));
5706
2fe1d762
TV
5707 if (va_type == error_mark_node
5708 || canon_va_type == NULL_TREE)
c7b38fd5 5709 {
2fe1d762
TV
5710 /* Let's handle things neutrallly, if expr:
5711 - has undeclared type, or
5712 - is not an va_list type. */
5713 return build_va_arg_1 (loc, type, expr);
c7b38fd5 5714 }
ecd0e562 5715
2fe1d762
TV
5716 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5717 {
5718 /* Case 1: Not an array type. */
5719
5720 /* Take the address, to get '&ap'. */
5721 mark_addressable (expr);
5722 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5723
5724 /* Verify that &ap is still recognized as having va_list type. */
5725 tree canon_expr_type
5726 = targetm.canonical_va_list_type (TREE_TYPE (expr));
1e77281b
MP
5727 if (canon_expr_type == NULL_TREE)
5728 {
5729 error_at (loc,
5730 "first argument to %<va_arg%> not of type %<va_list%>");
5731 return error_mark_node;
5732 }
2fe1d762
TV
5733
5734 return build_va_arg_1 (loc, type, expr);
5735 }
5736
5737 /* Case 2: Array type.
5738
5739 Background:
5740
5741 For contrast, let's start with the simple case (case 1). If
5742 canon_va_type is not an array type, but say a char *, then when
5743 passing-by-value a va_list, the type of the va_list param decl is
5744 the same as for another va_list decl (all ap's are char *):
5745
5746 f2_1 (char * ap)
5747 D.1815 = VA_ARG (&ap, 0B, 1);
5748 return D.1815;
5749
5750 f2 (int i)
5751 char * ap.0;
5752 char * ap;
5753 __builtin_va_start (&ap, 0);
5754 ap.0 = ap;
5755 res = f2_1 (ap.0);
5756 __builtin_va_end (&ap);
5757 D.1812 = res;
5758 return D.1812;
5759
5760 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5761 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5762 the same as for another va_list decl (case 2a, struct ap[1]).
5763
5764 f2_1 (struct * ap)
5765 D.1844 = VA_ARG (ap, 0B, 0);
5766 return D.1844;
5767
5768 f2 (int i)
5769 struct ap[1];
5770 __builtin_va_start (&ap, 0);
5771 res = f2_1 (&ap);
5772 __builtin_va_end (&ap);
5773 D.1841 = res;
5774 return D.1841;
5775
5776 Case 2b is different because:
5777 - on the callee side, the parm decl has declared type va_list, but
5778 grokdeclarator changes the type of the parm decl to a pointer to the
5779 array elem type.
5780 - on the caller side, the pass-by-value uses &ap.
5781
5782 We unify these two cases (case 2a: va_list is array type,
5783 case 2b: va_list is pointer to array elem type), by adding '&' for the
5784 array type case, such that we have a pointer to array elem in both
5785 cases. */
5786
5787 if (TREE_CODE (va_type) == ARRAY_TYPE)
5788 {
5789 /* Case 2a: va_list is array type. */
5790
5791 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5792 elem type. */
5793 mark_addressable (expr);
5794 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5795 expr);
5796
5797 /* Verify that &ap is still recognized as having va_list type. */
5798 tree canon_expr_type
5799 = targetm.canonical_va_list_type (TREE_TYPE (expr));
1e77281b
MP
5800 if (canon_expr_type == NULL_TREE)
5801 {
5802 error_at (loc,
5803 "first argument to %<va_arg%> not of type %<va_list%>");
5804 return error_mark_node;
5805 }
2fe1d762
TV
5806 }
5807 else
5808 {
5809 /* Case 2b: va_list is pointer to array elem type. */
5810 gcc_assert (POINTER_TYPE_P (va_type));
5811 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
5812
5813 /* Don't take the address. We've already got '&ap'. */
5814 ;
5815 }
5816
5817 return build_va_arg_1 (loc, type, expr);
d3707adb 5818}
fc2aaf30
JM
5819
5820
7d14c755
JM
5821/* Linked list of disabled built-in functions. */
5822
a79683d5 5823struct disabled_builtin
7d14c755
JM
5824{
5825 const char *name;
5826 struct disabled_builtin *next;
a79683d5 5827};
7d14c755
JM
5828static disabled_builtin *disabled_builtins = NULL;
5829
35b1a6fa 5830static bool builtin_function_disabled_p (const char *);
7d14c755
JM
5831
5832/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5833 begins with "__builtin_", give an error. */
5834
5835void
35b1a6fa 5836disable_builtin_function (const char *name)
7d14c755
JM
5837{
5838 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
bda67431 5839 error ("cannot disable built-in function %qs", name);
7d14c755
JM
5840 else
5841 {
1ad463f4
BI
5842 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5843 new_disabled_builtin->name = name;
5844 new_disabled_builtin->next = disabled_builtins;
5845 disabled_builtins = new_disabled_builtin;
7d14c755
JM
5846 }
5847}
5848
5849
5850/* Return true if the built-in function NAME has been disabled, false
5851 otherwise. */
5852
5853static bool
35b1a6fa 5854builtin_function_disabled_p (const char *name)
7d14c755
JM
5855{
5856 disabled_builtin *p;
5857 for (p = disabled_builtins; p != NULL; p = p->next)
5858 {
5859 if (strcmp (name, p->name) == 0)
5860 return true;
5861 }
5862 return false;
5863}
5864
5865
9bc15050
RG
5866/* Worker for DEF_BUILTIN.
5867 Possibly define a builtin function with one or two names.
5868 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5869 nonansi_p and flag_no_nonansi_builtin. */
fc2aaf30 5870
9bc15050
RG
5871static void
5872def_builtin_1 (enum built_in_function fncode,
5873 const char *name,
5874 enum built_in_class fnclass,
5875 tree fntype, tree libtype,
5876 bool both_p, bool fallback_p, bool nonansi_p,
5877 tree fnattrs, bool implicit_p)
fc2aaf30 5878{
9bc15050
RG
5879 tree decl;
5880 const char *libname;
5881
a0274e3e
JJ
5882 if (fntype == error_mark_node)
5883 return;
5884
9bc15050
RG
5885 gcc_assert ((!both_p && !fallback_p)
5886 || !strncmp (name, "__builtin_",
5887 strlen ("__builtin_")));
5888
5889 libname = name + strlen ("__builtin_");
c79efc4d
RÁE
5890 decl = add_builtin_function (name, fntype, fncode, fnclass,
5891 (fallback_p ? libname : NULL),
5892 fnattrs);
e79983f4
MM
5893
5894 set_builtin_decl (fncode, decl, implicit_p);
5895
9bc15050
RG
5896 if (both_p
5897 && !flag_no_builtin && !builtin_function_disabled_p (libname)
7d14c755 5898 && !(nonansi_p && flag_no_nonansi_builtin))
c79efc4d
RÁE
5899 add_builtin_function (libname, libtype, fncode, fnclass,
5900 NULL, fnattrs);
fc2aaf30 5901}
c530479e 5902\f
d72040f5
RH
5903/* Nonzero if the type T promotes to int. This is (nearly) the
5904 integral promotions defined in ISO C99 6.3.1.1/2. */
5905
5906bool
58f9752a 5907c_promoting_integer_type_p (const_tree t)
d72040f5
RH
5908{
5909 switch (TREE_CODE (t))
5910 {
5911 case INTEGER_TYPE:
5912 return (TYPE_MAIN_VARIANT (t) == char_type_node
5913 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5914 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5915 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
c6c04fca
RL
5916 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5917 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d72040f5
RH
5918
5919 case ENUMERAL_TYPE:
5920 /* ??? Technically all enumerations not larger than an int
5921 promote to an int. But this is used along code paths
5922 that only want to notice a size change. */
5923 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5924
5925 case BOOLEAN_TYPE:
5926 return 1;
5927
5928 default:
5929 return 0;
5930 }
5931}
5932
c530479e
RH
5933/* Return 1 if PARMS specifies a fixed number of parameters
5934 and none of their types is affected by default promotions. */
5935
5936int
58f9752a 5937self_promoting_args_p (const_tree parms)
c530479e 5938{
58f9752a 5939 const_tree t;
c530479e
RH
5940 for (t = parms; t; t = TREE_CHAIN (t))
5941 {
b3694847 5942 tree type = TREE_VALUE (t);
7e8176d7 5943
694fea20
VR
5944 if (type == error_mark_node)
5945 continue;
5946
c530479e
RH
5947 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5948 return 0;
5949
5950 if (type == 0)
5951 return 0;
5952
5953 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5954 return 0;
5955
d72040f5 5956 if (c_promoting_integer_type_p (type))
c530479e
RH
5957 return 0;
5958 }
5959 return 1;
5960}
5eda3d66 5961
12ea3302
GDR
5962/* Recursively remove any '*' or '&' operator from TYPE. */
5963tree
5964strip_pointer_operator (tree t)
5965{
5966 while (POINTER_TYPE_P (t))
5967 t = TREE_TYPE (t);
5968 return t;
5969}
5970
ba992967
SP
5971/* Recursively remove pointer or array type from TYPE. */
5972tree
5973strip_pointer_or_array_types (tree t)
5974{
5975 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5976 t = TREE_TYPE (t);
5977 return t;
5978}
5979
8f17b5c5
MM
5980/* Used to compare case labels. K1 and K2 are actually tree nodes
5981 representing case labels, or NULL_TREE for a `default' label.
5982 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5983 K2, and 0 if K1 and K2 are equal. */
5984
5985int
35b1a6fa 5986case_compare (splay_tree_key k1, splay_tree_key k2)
8f17b5c5
MM
5987{
5988 /* Consider a NULL key (such as arises with a `default' label) to be
5989 smaller than anything else. */
5990 if (!k1)
5991 return k2 ? -1 : 0;
5992 else if (!k2)
5993 return k1 ? 1 : 0;
5994
5995 return tree_int_cst_compare ((tree) k1, (tree) k2);
5996}
5997
c2255bc4
AH
5998/* Process a case label, located at LOC, for the range LOW_VALUE
5999 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6000 then this case label is actually a `default' label. If only
6001 HIGH_VALUE is NULL_TREE, then case label was declared using the
6002 usual C/C++ syntax, rather than the GNU case range extension.
6003 CASES is a tree containing all the case ranges processed so far;
b155cfd9
MP
6004 COND is the condition for the switch-statement itself.
6005 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6006 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6007 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
8f17b5c5
MM
6008
6009tree
c2255bc4 6010c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
b155cfd9 6011 tree low_value, tree high_value, bool *outside_range_p)
8f17b5c5
MM
6012{
6013 tree type;
6014 tree label;
6015 tree case_label;
6016 splay_tree_node node;
6017
6018 /* Create the LABEL_DECL itself. */
c2255bc4 6019 label = create_artificial_label (loc);
8f17b5c5
MM
6020
6021 /* If there was an error processing the switch condition, bail now
6022 before we get more confused. */
6023 if (!cond || cond == error_mark_node)
6de9cd9a 6024 goto error_out;
8f17b5c5 6025
35b1a6fa
AJ
6026 if ((low_value && TREE_TYPE (low_value)
6027 && POINTER_TYPE_P (TREE_TYPE (low_value)))
8f17b5c5
MM
6028 || (high_value && TREE_TYPE (high_value)
6029 && POINTER_TYPE_P (TREE_TYPE (high_value))))
522ddfa2 6030 {
c2255bc4 6031 error_at (loc, "pointers are not permitted as case values");
522ddfa2
JM
6032 goto error_out;
6033 }
8f17b5c5
MM
6034
6035 /* Case ranges are a GNU extension. */
fcf73884 6036 if (high_value)
c1771a20 6037 pedwarn (loc, OPT_Wpedantic,
fcf73884 6038 "range expressions in switch statements are non-standard");
8f17b5c5
MM
6039
6040 type = TREE_TYPE (cond);
6041 if (low_value)
6042 {
62e4eb35 6043 low_value = check_case_value (loc, low_value);
68fca595 6044 low_value = convert_and_check (loc, type, low_value);
c0e22534
NS
6045 if (low_value == error_mark_node)
6046 goto error_out;
8f17b5c5
MM
6047 }
6048 if (high_value)
6049 {
62e4eb35 6050 high_value = check_case_value (loc, high_value);
68fca595 6051 high_value = convert_and_check (loc, type, high_value);
c0e22534
NS
6052 if (high_value == error_mark_node)
6053 goto error_out;
8f17b5c5
MM
6054 }
6055
c0e22534
NS
6056 if (low_value && high_value)
6057 {
6058 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
c22cacf3
MS
6059 really a case range, even though it was written that way.
6060 Remove the HIGH_VALUE to simplify later processing. */
c0e22534
NS
6061 if (tree_int_cst_equal (low_value, high_value))
6062 high_value = NULL_TREE;
6063 else if (!tree_int_cst_lt (low_value, high_value))
c2255bc4 6064 warning_at (loc, 0, "empty range specified");
c0e22534 6065 }
8f17b5c5 6066
a6c0a76c
SB
6067 /* See if the case is in range of the type of the original testing
6068 expression. If both low_value and high_value are out of range,
6069 don't insert the case label and return NULL_TREE. */
6070 if (low_value
9d548dfb 6071 && !check_case_bounds (loc, type, orig_type,
b155cfd9
MP
6072 &low_value, high_value ? &high_value : NULL,
6073 outside_range_p))
a6c0a76c
SB
6074 return NULL_TREE;
6075
8f17b5c5
MM
6076 /* Look up the LOW_VALUE in the table of case labels we already
6077 have. */
6078 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6079 /* If there was not an exact match, check for overlapping ranges.
6080 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6081 that's a `default' label and the only overlap is an exact match. */
6082 if (!node && (low_value || high_value))
6083 {
6084 splay_tree_node low_bound;
6085 splay_tree_node high_bound;
6086
6087 /* Even though there wasn't an exact match, there might be an
6088 overlap between this case range and another case range.
6089 Since we've (inductively) not allowed any overlapping case
6090 ranges, we simply need to find the greatest low case label
6091 that is smaller that LOW_VALUE, and the smallest low case
6092 label that is greater than LOW_VALUE. If there is an overlap
6093 it will occur in one of these two ranges. */
6094 low_bound = splay_tree_predecessor (cases,
6095 (splay_tree_key) low_value);
6096 high_bound = splay_tree_successor (cases,
6097 (splay_tree_key) low_value);
6098
6099 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6100 the LOW_VALUE, so there is no need to check unless the
6101 LOW_BOUND is in fact itself a case range. */
6102 if (low_bound
6103 && CASE_HIGH ((tree) low_bound->value)
6104 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6105 low_value) >= 0)
6106 node = low_bound;
6107 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6108 range is bigger than the low end of the current range, so we
6109 are only interested if the current range is a real range, and
6110 not an ordinary case label. */
35b1a6fa 6111 else if (high_bound
8f17b5c5
MM
6112 && high_value
6113 && (tree_int_cst_compare ((tree) high_bound->key,
6114 high_value)
6115 <= 0))
6116 node = high_bound;
6117 }
6118 /* If there was an overlap, issue an error. */
6119 if (node)
6120 {
8c161995 6121 tree duplicate = CASE_LABEL ((tree) node->value);
8f17b5c5
MM
6122
6123 if (high_value)
6124 {
c2255bc4
AH
6125 error_at (loc, "duplicate (or overlapping) case value");
6126 error_at (DECL_SOURCE_LOCATION (duplicate),
6127 "this is the first entry overlapping that value");
8f17b5c5
MM
6128 }
6129 else if (low_value)
6130 {
c2255bc4
AH
6131 error_at (loc, "duplicate case value") ;
6132 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
8f17b5c5
MM
6133 }
6134 else
6135 {
c2255bc4
AH
6136 error_at (loc, "multiple default labels in one switch");
6137 error_at (DECL_SOURCE_LOCATION (duplicate),
6138 "this is the first default label");
8f17b5c5 6139 }
6de9cd9a 6140 goto error_out;
8f17b5c5
MM
6141 }
6142
6143 /* Add a CASE_LABEL to the statement-tree. */
3d528853 6144 case_label = add_stmt (build_case_label (low_value, high_value, label));
8f17b5c5 6145 /* Register this case label in the splay tree. */
35b1a6fa 6146 splay_tree_insert (cases,
8f17b5c5
MM
6147 (splay_tree_key) low_value,
6148 (splay_tree_value) case_label);
6149
6150 return case_label;
6de9cd9a
DN
6151
6152 error_out:
9e14e18f 6153 /* Add a label so that the back-end doesn't think that the beginning of
6de9cd9a 6154 the switch is unreachable. Note that we do not add a case label, as
41806d92 6155 that just leads to duplicates and thence to failure later on. */
6de9cd9a
DN
6156 if (!cases->root)
6157 {
c2255bc4
AH
6158 tree t = create_artificial_label (loc);
6159 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6de9cd9a
DN
6160 }
6161 return error_mark_node;
6162}
6163
6164/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6165 Used to verify that case values match up with enumerator values. */
6166
6167static void
6168match_case_to_enum_1 (tree key, tree type, tree label)
6169{
807e902e
KZ
6170 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6171
6172 if (tree_fits_uhwi_p (key))
6173 print_dec (key, buf, UNSIGNED);
6174 else if (tree_fits_shwi_p (key))
6175 print_dec (key, buf, SIGNED);
6de9cd9a 6176 else
807e902e 6177 print_hex (key, buf);
6de9cd9a
DN
6178
6179 if (TYPE_NAME (type) == 0)
c5d75364
MLI
6180 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6181 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6182 "case value %qs not in enumerated type",
6183 buf);
6de9cd9a 6184 else
c5d75364
MLI
6185 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6186 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6187 "case value %qs not in enumerated type %qT",
6188 buf, type);
6de9cd9a
DN
6189}
6190
c782c2fe
RS
6191/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6192 Used to verify that case values match up with enumerator values. */
6193
6de9cd9a
DN
6194static int
6195match_case_to_enum (splay_tree_node node, void *data)
6196{
6197 tree label = (tree) node->value;
28dab132 6198 tree type = (tree) data;
6de9cd9a
DN
6199
6200 /* Skip default case. */
6201 if (!CASE_LOW (label))
6202 return 0;
6203
c782c2fe 6204 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6de9cd9a 6205 when we did our enum->case scan. Reset our scratch bit after. */
c782c2fe 6206 if (!CASE_LOW_SEEN (label))
6de9cd9a
DN
6207 match_case_to_enum_1 (CASE_LOW (label), type, label);
6208 else
c782c2fe 6209 CASE_LOW_SEEN (label) = 0;
6de9cd9a 6210
c782c2fe
RS
6211 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6212 not set, that means that CASE_HIGH did not appear when we did our
6213 enum->case scan. Reset our scratch bit after. */
6de9cd9a
DN
6214 if (CASE_HIGH (label))
6215 {
c782c2fe
RS
6216 if (!CASE_HIGH_SEEN (label))
6217 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6218 else
6219 CASE_HIGH_SEEN (label) = 0;
6de9cd9a
DN
6220 }
6221
6222 return 0;
6223}
6224
fbc315db
ILT
6225/* Handle -Wswitch*. Called from the front end after parsing the
6226 switch construct. */
6227/* ??? Should probably be somewhere generic, since other languages
6228 besides C and C++ would want this. At the moment, however, C/C++
6229 are the only tree-ssa languages that support enumerations at all,
6230 so the point is moot. */
6de9cd9a 6231
fbc315db
ILT
6232void
6233c_do_switch_warnings (splay_tree cases, location_t switch_location,
b155cfd9
MP
6234 tree type, tree cond, bool bool_cond_p,
6235 bool outside_range_p)
6de9cd9a 6236{
9f63daea 6237 splay_tree_node default_node;
c782c2fe
RS
6238 splay_tree_node node;
6239 tree chain;
6de9cd9a 6240
b155cfd9
MP
6241 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6242 && !warn_switch_bool)
6de9cd9a
DN
6243 return;
6244
6de9cd9a 6245 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
44c21c7f 6246 if (!default_node)
fab922b1
MLI
6247 warning_at (switch_location, OPT_Wswitch_default,
6248 "switch missing default case");
6de9cd9a 6249
b155cfd9
MP
6250 /* There are certain cases where -Wswitch-bool warnings aren't
6251 desirable, such as
6252 switch (boolean)
6253 {
6254 case true: ...
6255 case false: ...
6256 }
6257 so be careful here. */
6258 if (warn_switch_bool && bool_cond_p)
6259 {
6260 splay_tree_node min_node;
6261 /* If there's a default node, it's also the value with the minimal
6262 key. So look at the penultimate key (if any). */
6263 if (default_node)
6264 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6265 else
6266 min_node = splay_tree_min (cases);
6267 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6268
6269 splay_tree_node max_node = splay_tree_max (cases);
6270 /* This might be a case range, so look at the value with the
6271 maximal key and then check CASE_HIGH. */
6272 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6273 if (max)
6274 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6275
6276 /* If there's a case value > 1 or < 0, that is outside bool
6277 range, warn. */
6278 if (outside_range_p
6279 || (max && wi::gts_p (max, 1))
6280 || (min && wi::lts_p (min, 0))
6281 /* And handle the
6282 switch (boolean)
6283 {
6284 case true: ...
6285 case false: ...
6286 default: ...
6287 }
6288 case, where we want to warn. */
6289 || (default_node
6290 && max && wi::eq_p (max, 1)
6291 && min && wi::eq_p (min, 0)))
6292 warning_at (switch_location, OPT_Wswitch_bool,
6293 "switch condition has boolean value");
6294 }
6295
026c3cfd 6296 /* From here on, we only care about enumerated types. */
c782c2fe
RS
6297 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6298 return;
6299
cdb88468
ILT
6300 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6301 if (!warn_switch_enum && !warn_switch)
c782c2fe
RS
6302 return;
6303
cdb88468
ILT
6304 /* Check the cases. Warn about case values which are not members of
6305 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6306 there is no default case, check that exactly all enumeration
6307 literals are covered by the cases. */
6308
c782c2fe
RS
6309 /* Clearing COND if it is not an integer constant simplifies
6310 the tests inside the loop below. */
6311 if (TREE_CODE (cond) != INTEGER_CST)
6312 cond = NULL_TREE;
6313
6314 /* The time complexity here is O(N*lg(N)) worst case, but for the
6315 common case of monotonically increasing enumerators, it is
6316 O(N), since the nature of the splay tree will keep the next
6317 element adjacent to the root at all times. */
6de9cd9a 6318
c782c2fe
RS
6319 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6320 {
6321 tree value = TREE_VALUE (chain);
adf2edec
DG
6322 if (TREE_CODE (value) == CONST_DECL)
6323 value = DECL_INITIAL (value);
c782c2fe
RS
6324 node = splay_tree_lookup (cases, (splay_tree_key) value);
6325 if (node)
6de9cd9a 6326 {
c782c2fe
RS
6327 /* Mark the CASE_LOW part of the case entry as seen. */
6328 tree label = (tree) node->value;
6329 CASE_LOW_SEEN (label) = 1;
6330 continue;
6331 }
6332
6333 /* Even though there wasn't an exact match, there might be a
fa10beec 6334 case range which includes the enumerator's value. */
c782c2fe
RS
6335 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6336 if (node && CASE_HIGH ((tree) node->value))
6337 {
6338 tree label = (tree) node->value;
6339 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6340 if (cmp >= 0)
6de9cd9a 6341 {
c782c2fe
RS
6342 /* If we match the upper bound exactly, mark the CASE_HIGH
6343 part of the case entry as seen. */
6344 if (cmp == 0)
6345 CASE_HIGH_SEEN (label) = 1;
6346 continue;
6de9cd9a
DN
6347 }
6348 }
6349
c782c2fe
RS
6350 /* We've now determined that this enumerated literal isn't
6351 handled by the case labels of the switch statement. */
6de9cd9a 6352
c782c2fe
RS
6353 /* If the switch expression is a constant, we only really care
6354 about whether that constant is handled by the switch. */
6355 if (cond && tree_int_cst_compare (cond, value))
6356 continue;
6de9cd9a 6357
683d6ff9 6358 /* If there is a default_node, the only relevant option is
cdb88468 6359 Wswitch-enum. Otherwise, if both are enabled then we prefer
683d6ff9
MLI
6360 to warn using -Wswitch because -Wswitch is enabled by -Wall
6361 while -Wswitch-enum is explicit. */
cdb88468
ILT
6362 warning_at (switch_location,
6363 (default_node || !warn_switch
6364 ? OPT_Wswitch_enum
6365 : OPT_Wswitch),
6366 "enumeration value %qE not handled in switch",
6367 TREE_PURPOSE (chain));
6de9cd9a 6368 }
c782c2fe
RS
6369
6370 /* Warn if there are case expressions that don't correspond to
6371 enumerators. This can occur since C and C++ don't enforce
6372 type-checking of assignments to enumeration variables.
6373
6374 The time complexity here is now always O(N) worst case, since
6375 we should have marked both the lower bound and upper bound of
6376 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6377 above. This scan also resets those fields. */
683d6ff9 6378
c782c2fe 6379 splay_tree_foreach (cases, match_case_to_enum, type);
8f17b5c5
MM
6380}
6381
6b665219 6382/* Finish an expression taking the address of LABEL (an
6a3799eb
AH
6383 IDENTIFIER_NODE). Returns an expression for the address.
6384
6385 LOC is the location for the expression returned. */
15b732b2 6386
35b1a6fa 6387tree
6a3799eb 6388finish_label_address_expr (tree label, location_t loc)
15b732b2
NB
6389{
6390 tree result;
6391
c1771a20 6392 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
15b732b2 6393
6b665219
MM
6394 if (label == error_mark_node)
6395 return error_mark_node;
6396
15b732b2
NB
6397 label = lookup_label (label);
6398 if (label == NULL_TREE)
6399 result = null_pointer_node;
6400 else
6401 {
6402 TREE_USED (label) = 1;
6403 result = build1 (ADDR_EXPR, ptr_type_node, label);
5805e48d 6404 /* The current function is not necessarily uninlinable.
15b732b2
NB
6405 Computed gotos are incompatible with inlining, but the value
6406 here could be used only in a diagnostic, for example. */
6a3799eb 6407 protected_set_expr_location (result, loc);
15b732b2
NB
6408 }
6409
6410 return result;
6411}
19552aa5
JM
6412\f
6413
6414/* Given a boolean expression ARG, return a tree representing an increment
6415 or decrement (as indicated by CODE) of ARG. The front end must check for
6416 invalid cases (e.g., decrement in C++). */
6417tree
35b1a6fa 6418boolean_increment (enum tree_code code, tree arg)
19552aa5
JM
6419{
6420 tree val;
b5119fa1 6421 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
37fa72e9 6422
19552aa5
JM
6423 arg = stabilize_reference (arg);
6424 switch (code)
6425 {
6426 case PREINCREMENT_EXPR:
53fb4de3 6427 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5
JM
6428 break;
6429 case POSTINCREMENT_EXPR:
53fb4de3 6430 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5 6431 arg = save_expr (arg);
53fb4de3
RS
6432 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6433 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6434 break;
6435 case PREDECREMENT_EXPR:
53fb4de3 6436 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6437 invert_truthvalue_loc (input_location, arg));
19552aa5
JM
6438 break;
6439 case POSTDECREMENT_EXPR:
53fb4de3 6440 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6441 invert_truthvalue_loc (input_location, arg));
19552aa5 6442 arg = save_expr (arg);
53fb4de3
RS
6443 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6444 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6445 break;
6446 default:
366de0ce 6447 gcc_unreachable ();
19552aa5
JM
6448 }
6449 TREE_SIDE_EFFECTS (val) = 1;
6450 return val;
6451}
03dc0325 6452\f
207bf79d
JM
6453/* Built-in macros for stddef.h and stdint.h, that require macros
6454 defined in this file. */
460bd0e3 6455void
35b1a6fa 6456c_stddef_cpp_builtins(void)
3df89291 6457{
5279d739
ZW
6458 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6459 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6460 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6461 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
85291069
JM
6462 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6463 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
c466b2cd
KVH
6464 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6465 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
207bf79d
JM
6466 if (SIG_ATOMIC_TYPE)
6467 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6468 if (INT8_TYPE)
6469 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6470 if (INT16_TYPE)
6471 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6472 if (INT32_TYPE)
6473 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6474 if (INT64_TYPE)
6475 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6476 if (UINT8_TYPE)
6477 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6478 if (UINT16_TYPE)
6479 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6480 if (UINT32_TYPE)
6481 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6482 if (UINT64_TYPE)
6483 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6484 if (INT_LEAST8_TYPE)
6485 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6486 if (INT_LEAST16_TYPE)
6487 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6488 if (INT_LEAST32_TYPE)
6489 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6490 if (INT_LEAST64_TYPE)
6491 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6492 if (UINT_LEAST8_TYPE)
6493 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6494 if (UINT_LEAST16_TYPE)
6495 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6496 if (UINT_LEAST32_TYPE)
6497 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6498 if (UINT_LEAST64_TYPE)
6499 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6500 if (INT_FAST8_TYPE)
6501 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6502 if (INT_FAST16_TYPE)
6503 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6504 if (INT_FAST32_TYPE)
6505 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6506 if (INT_FAST64_TYPE)
6507 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6508 if (UINT_FAST8_TYPE)
6509 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6510 if (UINT_FAST16_TYPE)
6511 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6512 if (UINT_FAST32_TYPE)
6513 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6514 if (UINT_FAST64_TYPE)
6515 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6516 if (INTPTR_TYPE)
6517 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6518 if (UINTPTR_TYPE)
6519 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
676997cf
RH
6520}
6521
6431177a 6522static void
35b1a6fa 6523c_init_attributes (void)
6431177a
JM
6524{
6525 /* Fill in the built_in_attributes array. */
4a90aeeb 6526#define DEF_ATTR_NULL_TREE(ENUM) \
6431177a 6527 built_in_attributes[(int) ENUM] = NULL_TREE;
4a90aeeb 6528#define DEF_ATTR_INT(ENUM, VALUE) \
c62c040f 6529 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
e384e6b5
BS
6530#define DEF_ATTR_STRING(ENUM, VALUE) \
6531 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6431177a
JM
6532#define DEF_ATTR_IDENT(ENUM, STRING) \
6533 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6534#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6535 built_in_attributes[(int) ENUM] \
6536 = tree_cons (built_in_attributes[(int) PURPOSE], \
6537 built_in_attributes[(int) VALUE], \
6538 built_in_attributes[(int) CHAIN]);
6431177a
JM
6539#include "builtin-attrs.def"
6540#undef DEF_ATTR_NULL_TREE
6541#undef DEF_ATTR_INT
6542#undef DEF_ATTR_IDENT
6543#undef DEF_ATTR_TREE_LIST
03dc0325 6544}
26f943fd 6545
943f82e7
JM
6546/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6547 identifier as an argument, so the front end shouldn't look it up. */
6548
6549bool
564a129d 6550attribute_takes_identifier_p (const_tree attr_id)
943f82e7 6551{
f231b5ff 6552 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
1b9b91a6
JM
6553 if (spec == NULL)
6554 /* Unknown attribute that we'll end up ignoring, return true so we
6555 don't complain about an identifier argument. */
6556 return true;
6557 else if (!strcmp ("mode", spec->name)
6558 || !strcmp ("format", spec->name)
6559 || !strcmp ("cleanup", spec->name))
564a129d
JM
6560 return true;
6561 else
6562 return targetm.attribute_takes_identifier_p (attr_id);
943f82e7
JM
6563}
6564
349ae713
NB
6565/* Attribute handlers common to C front ends. */
6566
6567/* Handle a "packed" attribute; arguments as in
6568 struct attribute_spec.handler. */
6569
6570static tree
e18476eb 6571handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
a742c759 6572 int flags, bool *no_add_attrs)
349ae713 6573{
c6e4cc53 6574 if (TYPE_P (*node))
349ae713
NB
6575 {
6576 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 6577 *node = build_variant_type_copy (*node);
c6e4cc53 6578 TYPE_PACKED (*node) = 1;
349ae713
NB
6579 }
6580 else if (TREE_CODE (*node) == FIELD_DECL)
646c0835 6581 {
2cd36c22
AN
6582 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6583 /* Still pack bitfields. */
6584 && ! DECL_INITIAL (*node))
646c0835
NS
6585 warning (OPT_Wattributes,
6586 "%qE attribute ignored for field of type %qT",
6587 name, TREE_TYPE (*node));
6588 else
6589 DECL_PACKED (*node) = 1;
6590 }
349ae713 6591 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
c6e4cc53
NS
6592 used for DECL_REGISTER. It wouldn't mean anything anyway.
6593 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6594 that changes what the typedef is typing. */
349ae713
NB
6595 else
6596 {
5c498b10 6597 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6598 *no_add_attrs = true;
6599 }
6600
6601 return NULL_TREE;
6602}
6603
6604/* Handle a "nocommon" attribute; arguments as in
6605 struct attribute_spec.handler. */
6606
6607static tree
35b1a6fa 6608handle_nocommon_attribute (tree *node, tree name,
e18476eb
BI
6609 tree ARG_UNUSED (args),
6610 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 6611{
0ae9bd27 6612 if (VAR_P (*node))
349ae713
NB
6613 DECL_COMMON (*node) = 0;
6614 else
6615 {
5c498b10 6616 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6617 *no_add_attrs = true;
6618 }
6619
6620 return NULL_TREE;
6621}
6622
6623/* Handle a "common" attribute; arguments as in
6624 struct attribute_spec.handler. */
6625
6626static tree
e18476eb
BI
6627handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6628 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 6629{
0ae9bd27 6630 if (VAR_P (*node))
349ae713
NB
6631 DECL_COMMON (*node) = 1;
6632 else
6633 {
5c498b10 6634 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6635 *no_add_attrs = true;
6636 }
6637
6638 return NULL_TREE;
6639}
6640
6641/* Handle a "noreturn" attribute; arguments as in
6642 struct attribute_spec.handler. */
6643
6644static tree
e18476eb
BI
6645handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6646 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6647{
6648 tree type = TREE_TYPE (*node);
6649
6650 /* See FIXME comment in c_common_attribute_table. */
2debdb4f
NP
6651 if (TREE_CODE (*node) == FUNCTION_DECL
6652 || objc_method_decl (TREE_CODE (*node)))
349ae713
NB
6653 TREE_THIS_VOLATILE (*node) = 1;
6654 else if (TREE_CODE (type) == POINTER_TYPE
6655 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6656 TREE_TYPE (*node)
6e7ceb17
PC
6657 = (build_qualified_type
6658 (build_pointer_type
6659 (build_type_variant (TREE_TYPE (type),
6660 TYPE_READONLY (TREE_TYPE (type)), 1)),
6661 TYPE_QUALS (type)));
349ae713
NB
6662 else
6663 {
5c498b10 6664 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6665 *no_add_attrs = true;
6666 }
6667
6668 return NULL_TREE;
6669}
6670
52bf96d2
JH
6671/* Handle a "hot" and attribute; arguments as in
6672 struct attribute_spec.handler. */
6673
6674static tree
6675handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
ab442df7 6676 int ARG_UNUSED (flags), bool *no_add_attrs)
52bf96d2 6677{
e45abe1f
RH
6678 if (TREE_CODE (*node) == FUNCTION_DECL
6679 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
6680 {
6681 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6682 {
45484dcf
MP
6683 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6684 "with attribute %qs", name, "cold");
52bf96d2
JH
6685 *no_add_attrs = true;
6686 }
5779e713
MM
6687 /* Most of the rest of the hot processing is done later with
6688 lookup_attribute. */
52bf96d2
JH
6689 }
6690 else
6691 {
6692 warning (OPT_Wattributes, "%qE attribute ignored", name);
6693 *no_add_attrs = true;
6694 }
6695
6696 return NULL_TREE;
6697}
e45abe1f 6698
52bf96d2
JH
6699/* Handle a "cold" and attribute; arguments as in
6700 struct attribute_spec.handler. */
6701
6702static tree
6703handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6704 int ARG_UNUSED (flags), bool *no_add_attrs)
6705{
e45abe1f
RH
6706 if (TREE_CODE (*node) == FUNCTION_DECL
6707 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
6708 {
6709 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6710 {
45484dcf
MP
6711 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6712 "with attribute %qs", name, "hot");
52bf96d2
JH
6713 *no_add_attrs = true;
6714 }
5779e713
MM
6715 /* Most of the rest of the cold processing is done later with
6716 lookup_attribute. */
52bf96d2
JH
6717 }
6718 else
6719 {
6720 warning (OPT_Wattributes, "%qE attribute ignored", name);
6721 *no_add_attrs = true;
6722 }
6723
6724 return NULL_TREE;
6725}
6726
e664c61c 6727/* Handle a "no_sanitize_address" attribute; arguments as in
77bc5132
JJ
6728 struct attribute_spec.handler. */
6729
6730static tree
e664c61c
KS
6731handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6732 bool *no_add_attrs)
77bc5132
JJ
6733{
6734 if (TREE_CODE (*node) != FUNCTION_DECL)
6735 {
6736 warning (OPT_Wattributes, "%qE attribute ignored", name);
6737 *no_add_attrs = true;
6738 }
6739
6740 return NULL_TREE;
6741}
6742
e664c61c
KS
6743/* Handle a "no_address_safety_analysis" attribute; arguments as in
6744 struct attribute_spec.handler. */
6745
6746static tree
6747handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6748 bool *no_add_attrs)
6749{
6750 if (TREE_CODE (*node) != FUNCTION_DECL)
6751 warning (OPT_Wattributes, "%qE attribute ignored", name);
6752 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6753 DECL_ATTRIBUTES (*node)
6754 = tree_cons (get_identifier ("no_sanitize_address"),
6755 NULL_TREE, DECL_ATTRIBUTES (*node));
6756 *no_add_attrs = true;
6757 return NULL_TREE;
6758}
6759
ce6923c5
MP
6760/* Handle a "no_sanitize_undefined" attribute; arguments as in
6761 struct attribute_spec.handler. */
6762
6763static tree
6764handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6765 bool *no_add_attrs)
6766{
6767 if (TREE_CODE (*node) != FUNCTION_DECL)
6768 {
6769 warning (OPT_Wattributes, "%qE attribute ignored", name);
6770 *no_add_attrs = true;
6771 }
6772
6773 return NULL_TREE;
6774}
6775
5434dc07
MD
6776/* Handle a "stack_protect" attribute; arguments as in
6777 struct attribute_spec.handler. */
6778static tree
6779handle_stack_protect_attribute (tree *node, tree name, tree, int,
6780 bool *no_add_attrs)
6781{
6782 if (TREE_CODE (*node) != FUNCTION_DECL)
6783 {
6784 warning (OPT_Wattributes, "%qE attribute ignored", name);
6785 *no_add_attrs = true;
6786 }
6787 else
6788 DECL_ATTRIBUTES (*node)
6789 = tree_cons (get_identifier ("stack_protect"),
6790 NULL_TREE, DECL_ATTRIBUTES (*node));
6791
6792 return NULL_TREE;
6793}
6794
349ae713
NB
6795/* Handle a "noinline" attribute; arguments as in
6796 struct attribute_spec.handler. */
6797
6798static tree
35b1a6fa 6799handle_noinline_attribute (tree *node, tree name,
e18476eb
BI
6800 tree ARG_UNUSED (args),
6801 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6802{
6803 if (TREE_CODE (*node) == FUNCTION_DECL)
45484dcf
MP
6804 {
6805 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6806 {
6807 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6808 "with attribute %qs", name, "always_inline");
6809 *no_add_attrs = true;
6810 }
6811 else
6812 DECL_UNINLINABLE (*node) = 1;
6813 }
349ae713
NB
6814 else
6815 {
5c498b10 6816 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6817 *no_add_attrs = true;
6818 }
6819
6820 return NULL_TREE;
6821}
6822
86631ea3
MJ
6823/* Handle a "noclone" attribute; arguments as in
6824 struct attribute_spec.handler. */
6825
6826static tree
6827handle_noclone_attribute (tree *node, tree name,
6828 tree ARG_UNUSED (args),
6829 int ARG_UNUSED (flags), bool *no_add_attrs)
6830{
6831 if (TREE_CODE (*node) != FUNCTION_DECL)
6832 {
6833 warning (OPT_Wattributes, "%qE attribute ignored", name);
6834 *no_add_attrs = true;
6835 }
6836
6837 return NULL_TREE;
6838}
6839
185c9e56
ML
6840/* Handle a "no_icf" attribute; arguments as in
6841 struct attribute_spec.handler. */
6842
6843static tree
6844handle_noicf_attribute (tree *node, tree name,
6845 tree ARG_UNUSED (args),
6846 int ARG_UNUSED (flags), bool *no_add_attrs)
6847{
6848 if (TREE_CODE (*node) != FUNCTION_DECL)
6849 {
6850 warning (OPT_Wattributes, "%qE attribute ignored", name);
6851 *no_add_attrs = true;
6852 }
6853
6854 return NULL_TREE;
6855}
6856
6857
349ae713
NB
6858/* Handle a "always_inline" attribute; arguments as in
6859 struct attribute_spec.handler. */
6860
6861static tree
35b1a6fa 6862handle_always_inline_attribute (tree *node, tree name,
e18476eb
BI
6863 tree ARG_UNUSED (args),
6864 int ARG_UNUSED (flags),
a742c759 6865 bool *no_add_attrs)
349ae713
NB
6866{
6867 if (TREE_CODE (*node) == FUNCTION_DECL)
6868 {
45484dcf
MP
6869 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6870 {
6871 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6872 "with %qs attribute", name, "noinline");
6873 *no_add_attrs = true;
6874 }
3b1661a9
ES
6875 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
6876 {
6877 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6878 "with %qs attribute", name, "target_clones");
6879 *no_add_attrs = true;
6880 }
45484dcf
MP
6881 else
6882 /* Set the attribute and mark it for disregarding inline
6883 limits. */
6884 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
349ae713
NB
6885 }
6886 else
6887 {
5c498b10 6888 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713 6889 *no_add_attrs = true;
4eb7fd83
JJ
6890 }
6891
6892 return NULL_TREE;
6893}
6894
6895/* Handle a "gnu_inline" attribute; arguments as in
6896 struct attribute_spec.handler. */
6897
6898static tree
6899handle_gnu_inline_attribute (tree *node, tree name,
6900 tree ARG_UNUSED (args),
6901 int ARG_UNUSED (flags),
6902 bool *no_add_attrs)
6903{
6904 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6905 {
6906 /* Do nothing else, just set the attribute. We'll get at
6907 it later with lookup_attribute. */
6908 }
6909 else
6910 {
6911 warning (OPT_Wattributes, "%qE attribute ignored", name);
6912 *no_add_attrs = true;
46a4da10
JH
6913 }
6914
6915 return NULL_TREE;
6916}
6917
6918/* Handle a "leaf" attribute; arguments as in
6919 struct attribute_spec.handler. */
6920
6921static tree
6922handle_leaf_attribute (tree *node, tree name,
6923 tree ARG_UNUSED (args),
6924 int ARG_UNUSED (flags), bool *no_add_attrs)
6925{
6926 if (TREE_CODE (*node) != FUNCTION_DECL)
6927 {
6928 warning (OPT_Wattributes, "%qE attribute ignored", name);
6929 *no_add_attrs = true;
6930 }
6931 if (!TREE_PUBLIC (*node))
6932 {
6933 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6934 *no_add_attrs = true;
349ae713
NB
6935 }
6936
6937 return NULL_TREE;
6938}
6939
d752cfdb
JJ
6940/* Handle an "artificial" attribute; arguments as in
6941 struct attribute_spec.handler. */
6942
6943static tree
6944handle_artificial_attribute (tree *node, tree name,
6945 tree ARG_UNUSED (args),
6946 int ARG_UNUSED (flags),
6947 bool *no_add_attrs)
6948{
6949 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6950 {
6951 /* Do nothing else, just set the attribute. We'll get at
6952 it later with lookup_attribute. */
6953 }
6954 else
6955 {
6956 warning (OPT_Wattributes, "%qE attribute ignored", name);
6957 *no_add_attrs = true;
6958 }
6959
6960 return NULL_TREE;
6961}
6962
0691d1d4
RG
6963/* Handle a "flatten" attribute; arguments as in
6964 struct attribute_spec.handler. */
6965
6966static tree
6967handle_flatten_attribute (tree *node, tree name,
c22cacf3
MS
6968 tree args ATTRIBUTE_UNUSED,
6969 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0691d1d4
RG
6970{
6971 if (TREE_CODE (*node) == FUNCTION_DECL)
6972 /* Do nothing else, just set the attribute. We'll get at
6973 it later with lookup_attribute. */
6974 ;
6975 else
6976 {
6977 warning (OPT_Wattributes, "%qE attribute ignored", name);
6978 *no_add_attrs = true;
6979 }
6980
6981 return NULL_TREE;
6982}
6983
d2af6a68
JJ
6984/* Handle a "warning" or "error" attribute; arguments as in
6985 struct attribute_spec.handler. */
6986
6987static tree
6988handle_error_attribute (tree *node, tree name, tree args,
6989 int ARG_UNUSED (flags), bool *no_add_attrs)
6990{
6991 if (TREE_CODE (*node) == FUNCTION_DECL
cf35e2b1 6992 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
d2af6a68
JJ
6993 /* Do nothing else, just set the attribute. We'll get at
6994 it later with lookup_attribute. */
6995 ;
6996 else
6997 {
6998 warning (OPT_Wattributes, "%qE attribute ignored", name);
6999 *no_add_attrs = true;
7000 }
7001
7002 return NULL_TREE;
7003}
0691d1d4 7004
349ae713
NB
7005/* Handle a "used" attribute; arguments as in
7006 struct attribute_spec.handler. */
7007
7008static tree
e18476eb
BI
7009handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7010 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 7011{
d7ddbe24
RH
7012 tree node = *pnode;
7013
7014 if (TREE_CODE (node) == FUNCTION_DECL
0ae9bd27 7015 || (VAR_P (node) && TREE_STATIC (node))
3797cb21 7016 || (TREE_CODE (node) == TYPE_DECL))
4d7d0451 7017 {
4d7d0451 7018 TREE_USED (node) = 1;
8e3e233b 7019 DECL_PRESERVE_P (node) = 1;
0ae9bd27 7020 if (VAR_P (node))
ebfbbdc5 7021 DECL_READ_P (node) = 1;
4d7d0451 7022 }
349ae713
NB
7023 else
7024 {
5c498b10 7025 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7026 *no_add_attrs = true;
7027 }
7028
7029 return NULL_TREE;
7030}
7031
7032/* Handle a "unused" attribute; arguments as in
7033 struct attribute_spec.handler. */
7034
d067e05f 7035tree
e18476eb
BI
7036handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7037 int flags, bool *no_add_attrs)
349ae713
NB
7038{
7039 if (DECL_P (*node))
7040 {
7041 tree decl = *node;
7042
7043 if (TREE_CODE (decl) == PARM_DECL
af05e6e5 7044 || VAR_OR_FUNCTION_DECL_P (decl)
349ae713 7045 || TREE_CODE (decl) == LABEL_DECL
d067e05f 7046 || TREE_CODE (decl) == CONST_DECL
349ae713 7047 || TREE_CODE (decl) == TYPE_DECL)
ebfbbdc5
JJ
7048 {
7049 TREE_USED (decl) = 1;
0ae9bd27 7050 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
ebfbbdc5
JJ
7051 DECL_READ_P (decl) = 1;
7052 }
349ae713
NB
7053 else
7054 {
5c498b10 7055 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7056 *no_add_attrs = true;
7057 }
7058 }
7059 else
7060 {
7061 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 7062 *node = build_variant_type_copy (*node);
349ae713
NB
7063 TREE_USED (*node) = 1;
7064 }
7065
7066 return NULL_TREE;
7067}
7068
ce91e74c
JH
7069/* Handle a "externally_visible" attribute; arguments as in
7070 struct attribute_spec.handler. */
7071
7072static tree
7073handle_externally_visible_attribute (tree *pnode, tree name,
7074 tree ARG_UNUSED (args),
7075 int ARG_UNUSED (flags),
7076 bool *no_add_attrs)
7077{
7078 tree node = *pnode;
7079
21b634ae 7080 if (VAR_OR_FUNCTION_DECL_P (node))
ce91e74c 7081 {
343d4b27
JJ
7082 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7083 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7084 {
7085 warning (OPT_Wattributes,
7086 "%qE attribute have effect only on public objects", name);
7087 *no_add_attrs = true;
7088 }
ce91e74c 7089 }
ce91e74c
JH
7090 else
7091 {
7092 warning (OPT_Wattributes, "%qE attribute ignored", name);
7093 *no_add_attrs = true;
7094 }
7095
7096 return NULL_TREE;
7097}
7098
7861b648
AK
7099/* Handle the "no_reorder" attribute. Arguments as in
7100 struct attribute_spec.handler. */
7101
7102static tree
7103handle_no_reorder_attribute (tree *pnode,
7104 tree name,
7105 tree,
7106 int,
7107 bool *no_add_attrs)
7108{
7109 tree node = *pnode;
7110
21b634ae 7111 if (!VAR_OR_FUNCTION_DECL_P (node)
7861b648
AK
7112 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7113 {
7114 warning (OPT_Wattributes,
7115 "%qE attribute only affects top level objects",
7116 name);
7117 *no_add_attrs = true;
7118 }
7119
7120 return NULL_TREE;
7121}
7122
349ae713
NB
7123/* Handle a "const" attribute; arguments as in
7124 struct attribute_spec.handler. */
7125
7126static tree
e18476eb
BI
7127handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7128 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7129{
7130 tree type = TREE_TYPE (*node);
7131
7132 /* See FIXME comment on noreturn in c_common_attribute_table. */
7133 if (TREE_CODE (*node) == FUNCTION_DECL)
7134 TREE_READONLY (*node) = 1;
7135 else if (TREE_CODE (type) == POINTER_TYPE
7136 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7137 TREE_TYPE (*node)
6e7ceb17
PC
7138 = (build_qualified_type
7139 (build_pointer_type
7140 (build_type_variant (TREE_TYPE (type), 1,
7141 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7142 TYPE_QUALS (type)));
349ae713
NB
7143 else
7144 {
5c498b10 7145 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7146 *no_add_attrs = true;
7147 }
7148
7149 return NULL_TREE;
7150}
7151
ee45a32d
EB
7152/* Handle a "scalar_storage_order" attribute; arguments as in
7153 struct attribute_spec.handler. */
7154
7155static tree
7156handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7157 int flags, bool *no_add_attrs)
7158{
7159 tree id = TREE_VALUE (args);
7160 tree type;
7161
7162 if (TREE_CODE (*node) == TYPE_DECL
7163 && ! (flags & ATTR_FLAG_CXX11))
7164 node = &TREE_TYPE (*node);
7165 type = *node;
7166
7167 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7168 {
7169 error ("scalar_storage_order is not supported because endianness "
7170 "is not uniform");
7171 return NULL_TREE;
7172 }
7173
7174 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7175 {
7176 bool reverse = false;
7177
7178 if (TREE_CODE (id) == STRING_CST
7179 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7180 reverse = !BYTES_BIG_ENDIAN;
7181 else if (TREE_CODE (id) == STRING_CST
7182 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7183 reverse = BYTES_BIG_ENDIAN;
7184 else
7185 {
7186 error ("scalar_storage_order argument must be one of \"big-endian\""
7187 " or \"little-endian\"");
7188 return NULL_TREE;
7189 }
7190
7191 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7192 {
7193 if (reverse)
7194 /* A type variant isn't good enough, since we don't want a cast
7195 to such a type to be removed as a no-op. */
7196 *node = type = build_duplicate_type (type);
7197 }
7198
7199 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7200 return NULL_TREE;
7201 }
7202
7203 warning (OPT_Wattributes, "%qE attribute ignored", name);
7204 *no_add_attrs = true;
7205 return NULL_TREE;
7206}
7207
349ae713
NB
7208/* Handle a "transparent_union" attribute; arguments as in
7209 struct attribute_spec.handler. */
7210
7211static tree
35b1a6fa 7212handle_transparent_union_attribute (tree *node, tree name,
e18476eb 7213 tree ARG_UNUSED (args), int flags,
a742c759 7214 bool *no_add_attrs)
349ae713 7215{
4009f2e7 7216 tree type;
52dd234b
RH
7217
7218 *no_add_attrs = true;
349ae713 7219
e28d52cf
DS
7220 if (TREE_CODE (*node) == TYPE_DECL
7221 && ! (flags & ATTR_FLAG_CXX11))
4009f2e7
JM
7222 node = &TREE_TYPE (*node);
7223 type = *node;
349ae713 7224
52dd234b 7225 if (TREE_CODE (type) == UNION_TYPE)
349ae713 7226 {
d58d6eb5
JM
7227 /* Make sure that the first field will work for a transparent union.
7228 If the type isn't complete yet, leave the check to the code in
7229 finish_struct. */
7230 if (TYPE_SIZE (type))
7231 {
7232 tree first = first_field (type);
7233 if (first == NULL_TREE
7234 || DECL_ARTIFICIAL (first)
7235 || TYPE_MODE (type) != DECL_MODE (first))
7236 goto ignored;
7237 }
7238
349ae713 7239 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
52dd234b 7240 {
d58d6eb5
JM
7241 /* If the type isn't complete yet, setting the flag
7242 on a variant wouldn't ever be checked. */
7243 if (!TYPE_SIZE (type))
7244 goto ignored;
7245
7246 /* build_duplicate_type doesn't work for C++. */
7247 if (c_dialect_cxx ())
52dd234b
RH
7248 goto ignored;
7249
ee45a32d
EB
7250 /* A type variant isn't good enough, since we don't want a cast
7251 to such a type to be removed as a no-op. */
52dd234b
RH
7252 *node = type = build_duplicate_type (type);
7253 }
7254
75c8aac3
JH
7255 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7256 TYPE_TRANSPARENT_AGGR (t) = 1;
52dd234b 7257 return NULL_TREE;
349ae713
NB
7258 }
7259
52dd234b
RH
7260 ignored:
7261 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7262 return NULL_TREE;
7263}
7264
fc8600f9
MM
7265/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7266 get the requested priority for a constructor or destructor,
7267 possibly issuing diagnostics for invalid or reserved
7268 priorities. */
7269
7270static priority_type
7271get_priority (tree args, bool is_destructor)
7272{
7273 HOST_WIDE_INT pri;
b2f4bed8 7274 tree arg;
fc8600f9
MM
7275
7276 if (!args)
7277 return DEFAULT_INIT_PRIORITY;
b8698a0f 7278
f6fc5c86
MM
7279 if (!SUPPORTS_INIT_PRIORITY)
7280 {
7281 if (is_destructor)
7282 error ("destructor priorities are not supported");
7283 else
7284 error ("constructor priorities are not supported");
7285 return DEFAULT_INIT_PRIORITY;
7286 }
7287
b2f4bed8 7288 arg = TREE_VALUE (args);
fad7652e
JJ
7289 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7290 goto invalid;
7291 if (arg == error_mark_node)
7292 return DEFAULT_INIT_PRIORITY;
8d0d1915 7293 arg = default_conversion (arg);
9541ffee 7294 if (!tree_fits_shwi_p (arg)
b2f4bed8 7295 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
fc8600f9
MM
7296 goto invalid;
7297
9439e9a1 7298 pri = tree_to_shwi (arg);
fc8600f9
MM
7299 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7300 goto invalid;
7301
7302 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7303 {
7304 if (is_destructor)
7305 warning (0,
7306 "destructor priorities from 0 to %d are reserved "
b8698a0f 7307 "for the implementation",
fc8600f9
MM
7308 MAX_RESERVED_INIT_PRIORITY);
7309 else
7310 warning (0,
7311 "constructor priorities from 0 to %d are reserved "
b8698a0f 7312 "for the implementation",
fc8600f9
MM
7313 MAX_RESERVED_INIT_PRIORITY);
7314 }
7315 return pri;
7316
7317 invalid:
7318 if (is_destructor)
7319 error ("destructor priorities must be integers from 0 to %d inclusive",
7320 MAX_INIT_PRIORITY);
7321 else
7322 error ("constructor priorities must be integers from 0 to %d inclusive",
7323 MAX_INIT_PRIORITY);
7324 return DEFAULT_INIT_PRIORITY;
7325}
7326
349ae713
NB
7327/* Handle a "constructor" attribute; arguments as in
7328 struct attribute_spec.handler. */
7329
7330static tree
fc8600f9 7331handle_constructor_attribute (tree *node, tree name, tree args,
e18476eb 7332 int ARG_UNUSED (flags),
a742c759 7333 bool *no_add_attrs)
349ae713
NB
7334{
7335 tree decl = *node;
7336 tree type = TREE_TYPE (decl);
7337
7338 if (TREE_CODE (decl) == FUNCTION_DECL
7339 && TREE_CODE (type) == FUNCTION_TYPE
7340 && decl_function_context (decl) == 0)
7341 {
fc8600f9 7342 priority_type priority;
349ae713 7343 DECL_STATIC_CONSTRUCTOR (decl) = 1;
fc8600f9
MM
7344 priority = get_priority (args, /*is_destructor=*/false);
7345 SET_DECL_INIT_PRIORITY (decl, priority);
349ae713
NB
7346 TREE_USED (decl) = 1;
7347 }
7348 else
7349 {
5c498b10 7350 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7351 *no_add_attrs = true;
7352 }
7353
7354 return NULL_TREE;
7355}
7356
7357/* Handle a "destructor" attribute; arguments as in
7358 struct attribute_spec.handler. */
7359
7360static tree
fc8600f9 7361handle_destructor_attribute (tree *node, tree name, tree args,
e18476eb 7362 int ARG_UNUSED (flags),
a742c759 7363 bool *no_add_attrs)
349ae713
NB
7364{
7365 tree decl = *node;
7366 tree type = TREE_TYPE (decl);
7367
7368 if (TREE_CODE (decl) == FUNCTION_DECL
7369 && TREE_CODE (type) == FUNCTION_TYPE
7370 && decl_function_context (decl) == 0)
7371 {
fc8600f9 7372 priority_type priority;
349ae713 7373 DECL_STATIC_DESTRUCTOR (decl) = 1;
fc8600f9
MM
7374 priority = get_priority (args, /*is_destructor=*/true);
7375 SET_DECL_FINI_PRIORITY (decl, priority);
349ae713
NB
7376 TREE_USED (decl) = 1;
7377 }
7378 else
7379 {
5c498b10 7380 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7381 *no_add_attrs = true;
7382 }
7383
7384 return NULL_TREE;
7385}
7386
4d451982
MLI
7387/* Nonzero if the mode is a valid vector mode for this architecture.
7388 This returns nonzero even if there is no hardware support for the
7389 vector mode, but we can emulate with narrower modes. */
7390
7391static int
ef4bddc2 7392vector_mode_valid_p (machine_mode mode)
4d451982
MLI
7393{
7394 enum mode_class mclass = GET_MODE_CLASS (mode);
ef4bddc2 7395 machine_mode innermode;
4d451982
MLI
7396
7397 /* Doh! What's going on? */
7398 if (mclass != MODE_VECTOR_INT
7399 && mclass != MODE_VECTOR_FLOAT
7400 && mclass != MODE_VECTOR_FRACT
7401 && mclass != MODE_VECTOR_UFRACT
7402 && mclass != MODE_VECTOR_ACCUM
7403 && mclass != MODE_VECTOR_UACCUM)
7404 return 0;
7405
7406 /* Hardware support. Woo hoo! */
7407 if (targetm.vector_mode_supported_p (mode))
7408 return 1;
7409
7410 innermode = GET_MODE_INNER (mode);
7411
7412 /* We should probably return 1 if requesting V4DI and we have no DI,
7413 but we have V2DI, but this is probably very unlikely. */
7414
7415 /* If we have support for the inner mode, we can safely emulate it.
7416 We may not have V2DI, but me can emulate with a pair of DIs. */
7417 return targetm.scalar_mode_supported_p (innermode);
7418}
7419
7420
349ae713
NB
7421/* Handle a "mode" attribute; arguments as in
7422 struct attribute_spec.handler. */
7423
7424static tree
e18476eb
BI
7425handle_mode_attribute (tree *node, tree name, tree args,
7426 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7427{
7428 tree type = *node;
88388a52 7429 tree ident = TREE_VALUE (args);
349ae713
NB
7430
7431 *no_add_attrs = true;
7432
88388a52 7433 if (TREE_CODE (ident) != IDENTIFIER_NODE)
5c498b10 7434 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7435 else
7436 {
7437 int j;
88388a52 7438 const char *p = IDENTIFIER_POINTER (ident);
349ae713 7439 int len = strlen (p);
ef4bddc2 7440 machine_mode mode = VOIDmode;
349ae713 7441 tree typefm;
6dd53648 7442 bool valid_mode;
349ae713
NB
7443
7444 if (len > 4 && p[0] == '_' && p[1] == '_'
7445 && p[len - 1] == '_' && p[len - 2] == '_')
7446 {
28dab132 7447 char *newp = (char *) alloca (len - 1);
349ae713
NB
7448
7449 strcpy (newp, &p[2]);
7450 newp[len - 4] = '\0';
7451 p = newp;
7452 }
7453
7454 /* Change this type to have a type with the specified mode.
7455 First check for the special modes. */
3f75a254 7456 if (!strcmp (p, "byte"))
349ae713
NB
7457 mode = byte_mode;
7458 else if (!strcmp (p, "word"))
7459 mode = word_mode;
3f75a254 7460 else if (!strcmp (p, "pointer"))
349ae713 7461 mode = ptr_mode;
c7ff6e7a
AK
7462 else if (!strcmp (p, "libgcc_cmp_return"))
7463 mode = targetm.libgcc_cmp_return_mode ();
7464 else if (!strcmp (p, "libgcc_shift_count"))
7465 mode = targetm.libgcc_shift_count_mode ();
7b0518e3
UW
7466 else if (!strcmp (p, "unwind_word"))
7467 mode = targetm.unwind_word_mode ();
349ae713
NB
7468 else
7469 for (j = 0; j < NUM_MACHINE_MODES; j++)
7470 if (!strcmp (p, GET_MODE_NAME (j)))
61f03aba 7471 {
ef4bddc2 7472 mode = (machine_mode) j;
61f03aba
RH
7473 break;
7474 }
349ae713
NB
7475
7476 if (mode == VOIDmode)
4a5eab38 7477 {
88388a52 7478 error ("unknown machine mode %qE", ident);
4a5eab38
PB
7479 return NULL_TREE;
7480 }
7481
6dd53648
RH
7482 valid_mode = false;
7483 switch (GET_MODE_CLASS (mode))
4a5eab38 7484 {
6dd53648
RH
7485 case MODE_INT:
7486 case MODE_PARTIAL_INT:
7487 case MODE_FLOAT:
9a8ce21f 7488 case MODE_DECIMAL_FLOAT:
ab22c1fa
CF
7489 case MODE_FRACT:
7490 case MODE_UFRACT:
7491 case MODE_ACCUM:
7492 case MODE_UACCUM:
6dd53648
RH
7493 valid_mode = targetm.scalar_mode_supported_p (mode);
7494 break;
7495
7496 case MODE_COMPLEX_INT:
7497 case MODE_COMPLEX_FLOAT:
7498 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7499 break;
7500
7501 case MODE_VECTOR_INT:
7502 case MODE_VECTOR_FLOAT:
ab22c1fa
CF
7503 case MODE_VECTOR_FRACT:
7504 case MODE_VECTOR_UFRACT:
7505 case MODE_VECTOR_ACCUM:
7506 case MODE_VECTOR_UACCUM:
5c498b10
DD
7507 warning (OPT_Wattributes, "specifying vector types with "
7508 "__attribute__ ((mode)) is deprecated");
7509 warning (OPT_Wattributes,
7510 "use __attribute__ ((vector_size)) instead");
6dd53648
RH
7511 valid_mode = vector_mode_valid_p (mode);
7512 break;
4a5eab38 7513
6dd53648
RH
7514 default:
7515 break;
7516 }
7517 if (!valid_mode)
7518 {
9e637a26 7519 error ("unable to emulate %qs", p);
6dd53648
RH
7520 return NULL_TREE;
7521 }
4a5eab38 7522
6dd53648 7523 if (POINTER_TYPE_P (type))
cb2a532e 7524 {
36c5e70a 7525 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
ef4bddc2 7526 tree (*fn)(tree, machine_mode, bool);
6dd53648 7527
36c5e70a 7528 if (!targetm.addr_space.valid_pointer_mode (mode, as))
cb2a532e 7529 {
9e637a26 7530 error ("invalid pointer mode %qs", p);
cb2a532e
AH
7531 return NULL_TREE;
7532 }
7533
c22cacf3 7534 if (TREE_CODE (type) == POINTER_TYPE)
6dd53648 7535 fn = build_pointer_type_for_mode;
4977bab6 7536 else
6dd53648
RH
7537 fn = build_reference_type_for_mode;
7538 typefm = fn (TREE_TYPE (type), mode, false);
cb2a532e 7539 }
6dd53648 7540 else
ab22c1fa
CF
7541 {
7542 /* For fixed-point modes, we need to test if the signness of type
7543 and the machine mode are consistent. */
7544 if (ALL_FIXED_POINT_MODE_P (mode)
7545 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7546 {
d8a07487 7547 error ("signedness of type and machine mode %qs don%'t match", p);
ab22c1fa
CF
7548 return NULL_TREE;
7549 }
7550 /* For fixed-point modes, we need to pass saturating info. */
7551 typefm = lang_hooks.types.type_for_mode (mode,
7552 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7553 : TYPE_UNSIGNED (type));
7554 }
ec8465a5 7555
6dd53648
RH
7556 if (typefm == NULL_TREE)
7557 {
61f03aba 7558 error ("no data type for mode %qs", p);
6dd53648
RH
7559 return NULL_TREE;
7560 }
ec8465a5
RK
7561 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7562 {
7563 /* For enumeral types, copy the precision from the integer
7564 type returned above. If not an INTEGER_TYPE, we can't use
7565 this mode for this type. */
7566 if (TREE_CODE (typefm) != INTEGER_TYPE)
7567 {
61f03aba 7568 error ("cannot use mode %qs for enumeral types", p);
ec8465a5
RK
7569 return NULL_TREE;
7570 }
7571
99db1ef0
RH
7572 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7573 {
7574 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7575 typefm = type;
7576 }
7577 else
7578 {
7579 /* We cannot build a type variant, as there's code that assumes
7580 that TYPE_MAIN_VARIANT has the same mode. This includes the
7581 debug generators. Instead, create a subrange type. This
7582 results in all of the enumeral values being emitted only once
7583 in the original, and the subtype gets them by reference. */
7584 if (TYPE_UNSIGNED (type))
7585 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7586 else
7587 typefm = make_signed_type (TYPE_PRECISION (typefm));
7588 TREE_TYPE (typefm) = type;
7589 }
ec8465a5 7590 }
a2d36602
RH
7591 else if (VECTOR_MODE_P (mode)
7592 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7593 : TREE_CODE (type) != TREE_CODE (typefm))
61f03aba
RH
7594 {
7595 error ("mode %qs applied to inappropriate type", p);
7596 return NULL_TREE;
7597 }
7598
6dd53648 7599 *node = typefm;
349ae713
NB
7600 }
7601
7602 return NULL_TREE;
7603}
7604
7605/* Handle a "section" attribute; arguments as in
7606 struct attribute_spec.handler. */
7607
7608static tree
e18476eb
BI
7609handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7610 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7611{
7612 tree decl = *node;
7613
0373796b 7614 if (!targetm_common.have_named_sections)
349ae713 7615 {
0373796b
JT
7616 error_at (DECL_SOURCE_LOCATION (*node),
7617 "section attributes are not supported for this target");
7618 goto fail;
7619 }
9fb32434 7620
0373796b 7621 user_defined_section_attribute = true;
349ae713 7622
21b634ae 7623 if (!VAR_OR_FUNCTION_DECL_P (decl))
0373796b
JT
7624 {
7625 error ("section attribute not allowed for %q+D", *node);
7626 goto fail;
349ae713 7627 }
0373796b
JT
7628
7629 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
349ae713 7630 {
0373796b
JT
7631 error ("section attribute argument not a string constant");
7632 goto fail;
349ae713
NB
7633 }
7634
0ae9bd27 7635 if (VAR_P (decl)
0373796b
JT
7636 && current_function_decl != NULL_TREE
7637 && !TREE_STATIC (decl))
7638 {
7639 error_at (DECL_SOURCE_LOCATION (decl),
7640 "section attribute cannot be specified for local variables");
7641 goto fail;
7642 }
7643
7644 /* The decl may have already been given a section attribute
7645 from a previous declaration. Ensure they match. */
7646 if (DECL_SECTION_NAME (decl) != NULL
7647 && strcmp (DECL_SECTION_NAME (decl),
7648 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7649 {
7650 error ("section of %q+D conflicts with previous declaration", *node);
7651 goto fail;
7652 }
7653
0ae9bd27 7654 if (VAR_P (decl)
0373796b
JT
7655 && !targetm.have_tls && targetm.emutls.tmpl_section
7656 && DECL_THREAD_LOCAL_P (decl))
7657 {
7658 error ("section of %q+D cannot be overridden", *node);
7659 goto fail;
7660 }
7661
7662 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7663 return NULL_TREE;
7664
7665fail:
7666 *no_add_attrs = true;
349ae713
NB
7667 return NULL_TREE;
7668}
7669
d19fa6b5
JM
7670/* Check whether ALIGN is a valid user-specified alignment. If so,
7671 return its base-2 log; if not, output an error and return -1. If
7672 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7673 no error. */
7674int
7675check_user_alignment (const_tree align, bool allow_zero)
7676{
7677 int i;
7678
661a0813
MP
7679 if (error_operand_p (align))
7680 return -1;
a859517f
MP
7681 if (TREE_CODE (align) != INTEGER_CST
7682 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
d19fa6b5
JM
7683 {
7684 error ("requested alignment is not an integer constant");
7685 return -1;
7686 }
7687 else if (allow_zero && integer_zerop (align))
7688 return -1;
3f12f6e9
SKS
7689 else if (tree_int_cst_sgn (align) == -1
7690 || (i = tree_log2 (align)) == -1)
d19fa6b5 7691 {
3f12f6e9 7692 error ("requested alignment is not a positive power of 2");
d19fa6b5
JM
7693 return -1;
7694 }
7695 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7696 {
7697 error ("requested alignment is too large");
7698 return -1;
7699 }
7700 return i;
7701}
7702
e28d52cf
DS
7703/*
7704 If in c++-11, check if the c++-11 alignment constraint with respect
7705 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7706 c++-11 mode, does nothing.
7707
7708 [dcl.align]2/ says:
7709
7710 [* if the constant expression evaluates to a fundamental alignment,
7711 the alignment requirement of the declared entity shall be the
7712 specified fundamental alignment.
7713
7714 * if the constant expression evaluates to an extended alignment
7715 and the implementation supports that alignment in the context
7716 of the declaration, the alignment of the declared entity shall
7717 be that alignment
7718
7719 * if the constant expression evaluates to an extended alignment
7720 and the implementation does not support that alignment in the
7721 context of the declaration, the program is ill-formed]. */
7722
7723static bool
7724check_cxx_fundamental_alignment_constraints (tree node,
7725 unsigned align_log,
7726 int flags)
7727{
7728 bool alignment_too_large_p = false;
7729 unsigned requested_alignment = 1U << align_log;
7730 unsigned max_align = 0;
7731
7732 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7733 || (node == NULL_TREE || node == error_mark_node))
7734 return true;
7735
7736 if (cxx_fundamental_alignment_p (requested_alignment))
7737 return true;
7738
7739 if (DECL_P (node))
7740 {
7741 if (TREE_STATIC (node))
7742 {
7743 /* For file scope variables and static members, the target
7744 supports alignments that are at most
7745 MAX_OFILE_ALIGNMENT. */
7746 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7747 alignment_too_large_p = true;
7748 }
7749 else
7750 {
7751#ifdef BIGGEST_FIELD_ALIGNMENT
7752#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7753#else
7754#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7755#endif
7756 /* For non-static members, the target supports either
7757 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7758 if it is defined or BIGGEST_ALIGNMENT. */
7759 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7760 if (TREE_CODE (node) == FIELD_DECL
7761 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7762 alignment_too_large_p = true;
7763#undef MAX_TARGET_FIELD_ALIGNMENT
7764 /* For stack variables, the target supports at most
7765 MAX_STACK_ALIGNMENT. */
7766 else if (decl_function_context (node) != NULL
7767 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7768 alignment_too_large_p = true;
7769 }
7770 }
7771 else if (TYPE_P (node))
7772 {
7773 /* Let's be liberal for types. */
7774 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7775 alignment_too_large_p = true;
7776 }
7777
7778 if (alignment_too_large_p)
7779 pedwarn (input_location, OPT_Wattributes,
7780 "requested alignment %d is larger than %d",
7781 requested_alignment, max_align);
7782
7783 return !alignment_too_large_p;
7784}
7785
349ae713
NB
7786/* Handle a "aligned" attribute; arguments as in
7787 struct attribute_spec.handler. */
7788
7789static tree
e18476eb 7790handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
a742c759 7791 int flags, bool *no_add_attrs)
349ae713
NB
7792{
7793 tree decl = NULL_TREE;
7794 tree *type = NULL;
7795 int is_type = 0;
5d77fb19 7796 tree align_expr;
349ae713
NB
7797 int i;
7798
5d77fb19
MG
7799 if (args)
7800 {
7801 align_expr = TREE_VALUE (args);
661a0813
MP
7802 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7803 && TREE_CODE (align_expr) != FUNCTION_DECL)
5d77fb19
MG
7804 align_expr = default_conversion (align_expr);
7805 }
7806 else
7807 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7808
349ae713
NB
7809 if (DECL_P (*node))
7810 {
7811 decl = *node;
7812 type = &TREE_TYPE (decl);
7813 is_type = TREE_CODE (*node) == TYPE_DECL;
7814 }
7815 else if (TYPE_P (*node))
7816 type = node, is_type = 1;
7817
e28d52cf
DS
7818 if ((i = check_user_alignment (align_expr, false)) == -1
7819 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
d19fa6b5 7820 *no_add_attrs = true;
349ae713
NB
7821 else if (is_type)
7822 {
0f559c16
JM
7823 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7824 /* OK, modify the type in place. */;
349ae713
NB
7825 /* If we have a TYPE_DECL, then copy the type, so that we
7826 don't accidentally modify a builtin type. See pushdecl. */
0f559c16
JM
7827 else if (decl && TREE_TYPE (decl) != error_mark_node
7828 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
349ae713
NB
7829 {
7830 tree tt = TREE_TYPE (decl);
8dd16ecc 7831 *type = build_variant_type_copy (*type);
349ae713
NB
7832 DECL_ORIGINAL_TYPE (decl) = tt;
7833 TYPE_NAME (*type) = decl;
7834 TREE_USED (*type) = TREE_USED (decl);
7835 TREE_TYPE (decl) = *type;
7836 }
0f559c16 7837 else
8dd16ecc 7838 *type = build_variant_type_copy (*type);
349ae713 7839
fe37c7af 7840 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
349ae713
NB
7841 TYPE_USER_ALIGN (*type) = 1;
7842 }
837edd5f 7843 else if (! VAR_OR_FUNCTION_DECL_P (decl)
349ae713
NB
7844 && TREE_CODE (decl) != FIELD_DECL)
7845 {
dee15844 7846 error ("alignment may not be specified for %q+D", decl);
349ae713
NB
7847 *no_add_attrs = true;
7848 }
e28d52cf
DS
7849 else if (DECL_USER_ALIGN (decl)
7850 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7851 /* C++-11 [dcl.align/4]:
7852
7853 When multiple alignment-specifiers are specified for an
7854 entity, the alignment requirement shall be set to the
7855 strictest specified alignment.
7856
7857 This formally comes from the c++11 specification but we are
7858 doing it for the GNU attribute syntax as well. */
7859 *no_add_attrs = true;
837edd5f 7860 else if (TREE_CODE (decl) == FUNCTION_DECL
d9223014 7861 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
837edd5f
GK
7862 {
7863 if (DECL_USER_ALIGN (decl))
7864 error ("alignment for %q+D was previously specified as %d "
7865 "and may not be decreased", decl,
7866 DECL_ALIGN (decl) / BITS_PER_UNIT);
7867 else
7868 error ("alignment for %q+D must be at least %d", decl,
7869 DECL_ALIGN (decl) / BITS_PER_UNIT);
7870 *no_add_attrs = true;
7871 }
349ae713
NB
7872 else
7873 {
fe37c7af 7874 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
349ae713
NB
7875 DECL_USER_ALIGN (decl) = 1;
7876 }
7877
7878 return NULL_TREE;
7879}
7880
7881/* Handle a "weak" attribute; arguments as in
7882 struct attribute_spec.handler. */
7883
7884static tree
55af93a8 7885handle_weak_attribute (tree *node, tree name,
e18476eb
BI
7886 tree ARG_UNUSED (args),
7887 int ARG_UNUSED (flags),
7888 bool * ARG_UNUSED (no_add_attrs))
349ae713 7889{
55af93a8 7890 if (TREE_CODE (*node) == FUNCTION_DECL
6b4e94bc
RG
7891 && DECL_DECLARED_INLINE_P (*node))
7892 {
2aa9c6ae 7893 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6b4e94bc
RG
7894 *no_add_attrs = true;
7895 }
ba885ec5
NS
7896 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7897 {
7898 error ("indirect function %q+D cannot be declared weak", *node);
7899 *no_add_attrs = true;
7900 return NULL_TREE;
7901 }
21b634ae 7902 else if (VAR_OR_FUNCTION_DECL_P (*node))
c316b5e4 7903 declare_weak (*node);
55af93a8
DS
7904 else
7905 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7906
7907 return NULL_TREE;
7908}
7909
4bb794e2
ST
7910/* Handle a "noplt" attribute; arguments as in
7911 struct attribute_spec.handler. */
7912
7913static tree
7914handle_noplt_attribute (tree *node, tree name,
7915 tree ARG_UNUSED (args),
7916 int ARG_UNUSED (flags),
7917 bool * ARG_UNUSED (no_add_attrs))
7918{
7919 if (TREE_CODE (*node) != FUNCTION_DECL)
7920 {
7921 warning (OPT_Wattributes,
7922 "%qE attribute is only applicable on functions", name);
7923 *no_add_attrs = true;
7924 return NULL_TREE;
7925 }
7926 return NULL_TREE;
7927}
7928
ba885ec5
NS
7929/* Handle an "alias" or "ifunc" attribute; arguments as in
7930 struct attribute_spec.handler, except that IS_ALIAS tells us
7931 whether this is an alias as opposed to ifunc attribute. */
349ae713
NB
7932
7933static tree
ba885ec5
NS
7934handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7935 bool *no_add_attrs)
349ae713
NB
7936{
7937 tree decl = *node;
7938
ba885ec5 7939 if (TREE_CODE (decl) != FUNCTION_DECL
0ae9bd27 7940 && (!is_alias || !VAR_P (decl)))
feab5a67
JM
7941 {
7942 warning (OPT_Wattributes, "%qE attribute ignored", name);
7943 *no_add_attrs = true;
7944 }
7945 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
b8698a0f 7946 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825
GK
7947 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7948 /* A static variable declaration is always a tentative definition,
7949 but the alias is a non-tentative definition which overrides. */
b8698a0f 7950 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825 7951 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
349ae713 7952 {
ba885ec5 7953 error ("%q+D defined both normally and as %qE attribute", decl, name);
349ae713 7954 *no_add_attrs = true;
ba885ec5 7955 return NULL_TREE;
349ae713 7956 }
ba885ec5
NS
7957 else if (!is_alias
7958 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7959 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7960 {
7961 error ("weak %q+D cannot be defined %qE", decl, name);
7962 *no_add_attrs = true;
7963 return NULL_TREE;
7964 }
f6a76b9f
RH
7965
7966 /* Note that the very first time we process a nested declaration,
7967 decl_function_context will not be set. Indeed, *would* never
7968 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7969 we do below. After such frobbery, pushdecl would set the context.
7970 In any case, this is never what we want. */
7971 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
349ae713
NB
7972 {
7973 tree id;
7974
7975 id = TREE_VALUE (args);
7976 if (TREE_CODE (id) != STRING_CST)
7977 {
ba885ec5 7978 error ("attribute %qE argument not a string", name);
349ae713
NB
7979 *no_add_attrs = true;
7980 return NULL_TREE;
7981 }
7982 id = get_identifier (TREE_STRING_POINTER (id));
7983 /* This counts as a use of the object pointed to. */
7984 TREE_USED (id) = 1;
7985
7986 if (TREE_CODE (decl) == FUNCTION_DECL)
7987 DECL_INITIAL (decl) = error_mark_node;
7988 else
08346abd 7989 TREE_STATIC (decl) = 1;
ba885ec5
NS
7990
7991 if (!is_alias)
7992 /* ifuncs are also aliases, so set that attribute too. */
7993 DECL_ATTRIBUTES (decl)
7994 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
349ae713
NB
7995 }
7996 else
7997 {
5c498b10 7998 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7999 *no_add_attrs = true;
8000 }
8001
f7217cde
JH
8002 if (decl_in_symtab_p (*node))
8003 {
8004 struct symtab_node *n = symtab_node::get (decl);
8005 if (n && n->refuse_visibility_changes)
8006 {
8007 if (is_alias)
8008 error ("%+D declared alias after being used", decl);
8009 else
8010 error ("%+D declared ifunc after being used", decl);
8011 }
8012 }
8013
8014
349ae713
NB
8015 return NULL_TREE;
8016}
8017
ba885ec5
NS
8018/* Handle an "alias" or "ifunc" attribute; arguments as in
8019 struct attribute_spec.handler. */
8020
8021static tree
8022handle_ifunc_attribute (tree *node, tree name, tree args,
8023 int ARG_UNUSED (flags), bool *no_add_attrs)
8024{
8025 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8026}
8027
8028/* Handle an "alias" or "ifunc" attribute; arguments as in
8029 struct attribute_spec.handler. */
8030
8031static tree
8032handle_alias_attribute (tree *node, tree name, tree args,
8033 int ARG_UNUSED (flags), bool *no_add_attrs)
8034{
8035 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8036}
8037
a0203ca7
AO
8038/* Handle a "weakref" attribute; arguments as in struct
8039 attribute_spec.handler. */
8040
8041static tree
8042handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8043 int flags, bool *no_add_attrs)
8044{
8045 tree attr = NULL_TREE;
8046
e1cf56b1
AO
8047 /* We must ignore the attribute when it is associated with
8048 local-scoped decls, since attribute alias is ignored and many
8049 such symbols do not even have a DECL_WEAK field. */
e7b012c0
JJ
8050 if (decl_function_context (*node)
8051 || current_function_decl
21b634ae 8052 || !VAR_OR_FUNCTION_DECL_P (*node))
e1cf56b1
AO
8053 {
8054 warning (OPT_Wattributes, "%qE attribute ignored", name);
8055 *no_add_attrs = true;
8056 return NULL_TREE;
8057 }
8058
ba885ec5
NS
8059 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8060 {
8061 error ("indirect function %q+D cannot be declared weakref", *node);
8062 *no_add_attrs = true;
8063 return NULL_TREE;
8064 }
8065
a0203ca7
AO
8066 /* The idea here is that `weakref("name")' mutates into `weakref,
8067 alias("name")', and weakref without arguments, in turn,
8068 implicitly adds weak. */
8069
8070 if (args)
8071 {
8072 attr = tree_cons (get_identifier ("alias"), args, attr);
8073 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8074
8075 *no_add_attrs = true;
a9b0b825
GK
8076
8077 decl_attributes (node, attr, flags);
a0203ca7
AO
8078 }
8079 else
8080 {
8081 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
c5d75364
MLI
8082 error_at (DECL_SOURCE_LOCATION (*node),
8083 "weakref attribute must appear before alias attribute");
a0203ca7 8084
a9b0b825
GK
8085 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8086 and that isn't supported; and because it wants to add it to
8087 the list of weak decls, which isn't helpful. */
8088 DECL_WEAK (*node) = 1;
a0203ca7
AO
8089 }
8090
f7217cde
JH
8091 if (decl_in_symtab_p (*node))
8092 {
8093 struct symtab_node *n = symtab_node::get (*node);
8094 if (n && n->refuse_visibility_changes)
8095 error ("%+D declared weakref after being used", *node);
8096 }
8097
a0203ca7
AO
8098 return NULL_TREE;
8099}
8100
349ae713
NB
8101/* Handle an "visibility" attribute; arguments as in
8102 struct attribute_spec.handler. */
8103
8104static tree
35b1a6fa 8105handle_visibility_attribute (tree *node, tree name, tree args,
e18476eb 8106 int ARG_UNUSED (flags),
b9e75696 8107 bool *ARG_UNUSED (no_add_attrs))
349ae713
NB
8108{
8109 tree decl = *node;
968b41a1 8110 tree id = TREE_VALUE (args);
b9e75696 8111 enum symbol_visibility vis;
349ae713 8112
d7afec4b
ND
8113 if (TYPE_P (*node))
8114 {
b9e75696
JM
8115 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8116 /* OK */;
a868811e 8117 else if (!RECORD_OR_UNION_TYPE_P (*node))
b9e75696
JM
8118 {
8119 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8120 name);
8121 return NULL_TREE;
8122 }
8123 else if (TYPE_FIELDS (*node))
8124 {
8125 error ("%qE attribute ignored because %qT is already defined",
8126 name, *node);
8127 return NULL_TREE;
8128 }
d7afec4b 8129 }
3f75a254 8130 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
349ae713 8131 {
5c498b10 8132 warning (OPT_Wattributes, "%qE attribute ignored", name);
968b41a1 8133 return NULL_TREE;
349ae713 8134 }
349ae713 8135
968b41a1
MA
8136 if (TREE_CODE (id) != STRING_CST)
8137 {
40b97a2e 8138 error ("visibility argument not a string");
968b41a1 8139 return NULL_TREE;
349ae713 8140 }
9f63daea 8141
d7afec4b
ND
8142 /* If this is a type, set the visibility on the type decl. */
8143 if (TYPE_P (decl))
8144 {
8145 decl = TYPE_NAME (decl);
3f75a254 8146 if (!decl)
c22cacf3 8147 return NULL_TREE;
e8233ac2
AP
8148 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8149 {
5c498b10 8150 warning (OPT_Wattributes, "%qE attribute ignored on types",
e8233ac2
AP
8151 name);
8152 return NULL_TREE;
8153 }
d7afec4b 8154 }
349ae713 8155
968b41a1 8156 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
b9e75696 8157 vis = VISIBILITY_DEFAULT;
968b41a1 8158 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
b9e75696 8159 vis = VISIBILITY_INTERNAL;
968b41a1 8160 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
b9e75696 8161 vis = VISIBILITY_HIDDEN;
968b41a1 8162 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
b9e75696 8163 vis = VISIBILITY_PROTECTED;
968b41a1 8164 else
b9e75696
JM
8165 {
8166 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8167 vis = VISIBILITY_DEFAULT;
8168 }
8169
8170 if (DECL_VISIBILITY_SPECIFIED (decl)
3a687f8b
MM
8171 && vis != DECL_VISIBILITY (decl))
8172 {
8173 tree attributes = (TYPE_P (*node)
8174 ? TYPE_ATTRIBUTES (*node)
8175 : DECL_ATTRIBUTES (decl));
8176 if (lookup_attribute ("visibility", attributes))
8177 error ("%qD redeclared with different visibility", decl);
8178 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8179 && lookup_attribute ("dllimport", attributes))
8180 error ("%qD was declared %qs which implies default visibility",
8181 decl, "dllimport");
8182 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8183 && lookup_attribute ("dllexport", attributes))
8184 error ("%qD was declared %qs which implies default visibility",
8185 decl, "dllexport");
8186 }
b9e75696
JM
8187
8188 DECL_VISIBILITY (decl) = vis;
d7afec4b
ND
8189 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8190
b9e75696
JM
8191 /* Go ahead and attach the attribute to the node as well. This is needed
8192 so we can determine whether we have VISIBILITY_DEFAULT because the
8193 visibility was not specified, or because it was explicitly overridden
8194 from the containing scope. */
968b41a1 8195
349ae713
NB
8196 return NULL_TREE;
8197}
8198
b2ca3702
MM
8199/* Determine the ELF symbol visibility for DECL, which is either a
8200 variable or a function. It is an error to use this function if a
8201 definition of DECL is not available in this translation unit.
8202 Returns true if the final visibility has been determined by this
8203 function; false if the caller is free to make additional
8204 modifications. */
8205
8206bool
8207c_determine_visibility (tree decl)
8208{
21b634ae 8209 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
b2ca3702
MM
8210
8211 /* If the user explicitly specified the visibility with an
8212 attribute, honor that. DECL_VISIBILITY will have been set during
6d87092d
JM
8213 the processing of the attribute. We check for an explicit
8214 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8215 to distinguish the use of an attribute from the use of a "#pragma
8216 GCC visibility push(...)"; in the latter case we still want other
8217 considerations to be able to overrule the #pragma. */
8218 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8219 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8220 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8221 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
b2ca3702
MM
8222 return true;
8223
b9e75696
JM
8224 /* Set default visibility to whatever the user supplied with
8225 visibility_specified depending on #pragma GCC visibility. */
8226 if (!DECL_VISIBILITY_SPECIFIED (decl))
8227 {
09812622
JJ
8228 if (visibility_options.inpragma
8229 || DECL_VISIBILITY (decl) != default_visibility)
8230 {
8231 DECL_VISIBILITY (decl) = default_visibility;
8232 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8233 /* If visibility changed and DECL already has DECL_RTL, ensure
8234 symbol flags are updated. */
0ae9bd27 8235 if (((VAR_P (decl) && TREE_STATIC (decl))
09812622
JJ
8236 || TREE_CODE (decl) == FUNCTION_DECL)
8237 && DECL_RTL_SET_P (decl))
8238 make_decl_rtl (decl);
8239 }
b9e75696 8240 }
b2ca3702
MM
8241 return false;
8242}
8243
dce81a1a
JJ
8244/* Handle an "tls_model" attribute; arguments as in
8245 struct attribute_spec.handler. */
8246
8247static tree
35b1a6fa 8248handle_tls_model_attribute (tree *node, tree name, tree args,
e18476eb 8249 int ARG_UNUSED (flags), bool *no_add_attrs)
dce81a1a 8250{
c2f7fa15 8251 tree id;
dce81a1a 8252 tree decl = *node;
c2f7fa15 8253 enum tls_model kind;
dce81a1a 8254
c2f7fa15
SB
8255 *no_add_attrs = true;
8256
0ae9bd27 8257 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
dce81a1a 8258 {
5c498b10 8259 warning (OPT_Wattributes, "%qE attribute ignored", name);
c2f7fa15 8260 return NULL_TREE;
dce81a1a 8261 }
dce81a1a 8262
c2f7fa15
SB
8263 kind = DECL_TLS_MODEL (decl);
8264 id = TREE_VALUE (args);
8265 if (TREE_CODE (id) != STRING_CST)
8266 {
8267 error ("tls_model argument not a string");
8268 return NULL_TREE;
dce81a1a
JJ
8269 }
8270
c2f7fa15
SB
8271 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8272 kind = TLS_MODEL_LOCAL_EXEC;
8273 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8274 kind = TLS_MODEL_INITIAL_EXEC;
8275 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8276 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8277 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8278 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8279 else
8280 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8281
56363ffd 8282 set_decl_tls_model (decl, kind);
dce81a1a
JJ
8283 return NULL_TREE;
8284}
8285
349ae713
NB
8286/* Handle a "no_instrument_function" attribute; arguments as in
8287 struct attribute_spec.handler. */
8288
8289static tree
35b1a6fa 8290handle_no_instrument_function_attribute (tree *node, tree name,
e18476eb
BI
8291 tree ARG_UNUSED (args),
8292 int ARG_UNUSED (flags),
a742c759 8293 bool *no_add_attrs)
349ae713
NB
8294{
8295 tree decl = *node;
8296
8297 if (TREE_CODE (decl) != FUNCTION_DECL)
8298 {
c5d75364
MLI
8299 error_at (DECL_SOURCE_LOCATION (decl),
8300 "%qE attribute applies only to functions", name);
349ae713
NB
8301 *no_add_attrs = true;
8302 }
349ae713
NB
8303 else
8304 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8305
8306 return NULL_TREE;
8307}
8308
8309/* Handle a "malloc" attribute; arguments as in
8310 struct attribute_spec.handler. */
8311
8312static tree
e18476eb
BI
8313handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8314 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 8315{
3425638a
JM
8316 if (TREE_CODE (*node) == FUNCTION_DECL
8317 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
349ae713 8318 DECL_IS_MALLOC (*node) = 1;
349ae713
NB
8319 else
8320 {
5c498b10 8321 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8322 *no_add_attrs = true;
8323 }
8324
8325 return NULL_TREE;
8326}
8327
51bc54a6
DM
8328/* Handle a "alloc_size" attribute; arguments as in
8329 struct attribute_spec.handler. */
8330
8331static tree
8332handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8333 int ARG_UNUSED (flags), bool *no_add_attrs)
8334{
f3f75f69 8335 unsigned arg_count = type_num_arguments (*node);
51bc54a6
DM
8336 for (; args; args = TREE_CHAIN (args))
8337 {
8338 tree position = TREE_VALUE (args);
5d77fb19
MG
8339 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8340 && TREE_CODE (position) != FUNCTION_DECL)
8341 position = default_conversion (position);
51bc54a6 8342
8fcbce72
JJ
8343 if (!tree_fits_uhwi_p (position)
8344 || !arg_count
8345 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
51bc54a6 8346 {
b8698a0f 8347 warning (OPT_Wattributes,
51bc54a6
DM
8348 "alloc_size parameter outside range");
8349 *no_add_attrs = true;
8350 return NULL_TREE;
8351 }
8352 }
8353 return NULL_TREE;
8354}
8355
8fcbce72
JJ
8356/* Handle a "alloc_align" attribute; arguments as in
8357 struct attribute_spec.handler. */
8358
8359static tree
8360handle_alloc_align_attribute (tree *node, tree, tree args, int,
8361 bool *no_add_attrs)
8362{
8363 unsigned arg_count = type_num_arguments (*node);
8364 tree position = TREE_VALUE (args);
8365 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8366 position = default_conversion (position);
8367
8368 if (!tree_fits_uhwi_p (position)
8369 || !arg_count
8370 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8371 {
8372 warning (OPT_Wattributes,
8373 "alloc_align parameter outside range");
8374 *no_add_attrs = true;
8375 return NULL_TREE;
8376 }
8377 return NULL_TREE;
8378}
8379
8380/* Handle a "assume_aligned" attribute; arguments as in
8381 struct attribute_spec.handler. */
8382
8383static tree
8384handle_assume_aligned_attribute (tree *, tree, tree args, int,
8385 bool *no_add_attrs)
8386{
8387 for (; args; args = TREE_CHAIN (args))
8388 {
8389 tree position = TREE_VALUE (args);
8390 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8391 && TREE_CODE (position) != FUNCTION_DECL)
8392 position = default_conversion (position);
8393
8394 if (TREE_CODE (position) != INTEGER_CST)
8395 {
8396 warning (OPT_Wattributes,
8397 "assume_aligned parameter not integer constant");
8398 *no_add_attrs = true;
8399 return NULL_TREE;
8400 }
8401 }
8402 return NULL_TREE;
8403}
8404
0b7b376d
RG
8405/* Handle a "fn spec" attribute; arguments as in
8406 struct attribute_spec.handler. */
8407
8408static tree
8409handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8410 tree args, int ARG_UNUSED (flags),
8411 bool *no_add_attrs ATTRIBUTE_UNUSED)
8412{
8413 gcc_assert (args
8414 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8415 && !TREE_CHAIN (args));
8416 return NULL_TREE;
8417}
8418
d5e254e1
IE
8419/* Handle a "bnd_variable_size" attribute; arguments as in
8420 struct attribute_spec.handler. */
8421
8422static tree
8423handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8424 int ARG_UNUSED (flags), bool *no_add_attrs)
8425{
8426 if (TREE_CODE (*node) != FIELD_DECL)
8427 {
8428 warning (OPT_Wattributes, "%qE attribute ignored", name);
8429 *no_add_attrs = true;
8430 }
8431
8432 return NULL_TREE;
8433}
8434
8435/* Handle a "bnd_legacy" attribute; arguments as in
8436 struct attribute_spec.handler. */
8437
8438static tree
8439handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8440 int ARG_UNUSED (flags), bool *no_add_attrs)
8441{
8442 if (TREE_CODE (*node) != FUNCTION_DECL)
8443 {
8444 warning (OPT_Wattributes, "%qE attribute ignored", name);
8445 *no_add_attrs = true;
8446 }
8447
8448 return NULL_TREE;
8449}
8450
8451/* Handle a "bnd_instrument" attribute; arguments as in
8452 struct attribute_spec.handler. */
8453
8454static tree
8455handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8456 int ARG_UNUSED (flags), bool *no_add_attrs)
8457{
8458 if (TREE_CODE (*node) != FUNCTION_DECL)
8459 {
8460 warning (OPT_Wattributes, "%qE attribute ignored", name);
8461 *no_add_attrs = true;
8462 }
8463
8464 return NULL_TREE;
8465}
8466
2a99e5e6
LL
8467/* Handle a "warn_unused" attribute; arguments as in
8468 struct attribute_spec.handler. */
8469
8470static tree
8471handle_warn_unused_attribute (tree *node, tree name,
8472 tree args ATTRIBUTE_UNUSED,
8473 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8474{
8475 if (TYPE_P (*node))
8476 /* Do nothing else, just set the attribute. We'll get at
8477 it later with lookup_attribute. */
8478 ;
8479 else
8480 {
8481 warning (OPT_Wattributes, "%qE attribute ignored", name);
8482 *no_add_attrs = true;
8483 }
8484
8485 return NULL_TREE;
8486}
8487
acf0174b
JJ
8488/* Handle an "omp declare simd" attribute; arguments as in
8489 struct attribute_spec.handler. */
8490
8491static tree
8492handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8493{
8494 return NULL_TREE;
8495}
8496
fff77217
KY
8497/* Handle a "simd" attribute. */
8498
8499static tree
e7b69085 8500handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
fff77217
KY
8501{
8502 if (TREE_CODE (*node) == FUNCTION_DECL)
8503 {
8504 if (lookup_attribute ("cilk simd function",
8505 DECL_ATTRIBUTES (*node)) != NULL)
8506 {
8507 error_at (DECL_SOURCE_LOCATION (*node),
8508 "%<__simd__%> attribute cannot be used in the same "
8509 "function marked as a Cilk Plus SIMD-enabled function");
8510 *no_add_attrs = true;
8511 }
8512 else
e7b69085
KY
8513 {
8514 tree t = get_identifier ("omp declare simd");
8515 tree attr = NULL_TREE;
8516 if (args)
8517 {
8518 tree id = TREE_VALUE (args);
8519
8520 if (TREE_CODE (id) != STRING_CST)
8521 {
8522 error ("attribute %qE argument not a string", name);
8523 *no_add_attrs = true;
8524 return NULL_TREE;
8525 }
8526
8527 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8528 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8529 OMP_CLAUSE_NOTINBRANCH);
8530 else
8531 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8532 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8533 OMP_CLAUSE_INBRANCH);
8534 else
8535 {
8536 error ("only %<inbranch%> and %<notinbranch%> flags are "
8537 "allowed for %<__simd__%> attribute");
8538 *no_add_attrs = true;
8539 return NULL_TREE;
8540 }
8541 }
8542
8543 DECL_ATTRIBUTES (*node) = tree_cons (t,
8544 build_tree_list (NULL_TREE,
8545 attr),
8546 DECL_ATTRIBUTES (*node));
8547 }
fff77217
KY
8548 }
8549 else
8550 {
8551 warning (OPT_Wattributes, "%qE attribute ignored", name);
8552 *no_add_attrs = true;
8553 }
8554
8555 return NULL_TREE;
8556}
8557
acf0174b
JJ
8558/* Handle an "omp declare target" attribute; arguments as in
8559 struct attribute_spec.handler. */
8560
8561static tree
8562handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8563{
8564 return NULL_TREE;
8565}
8566
6e9a3221
AN
8567/* Handle a "returns_twice" attribute; arguments as in
8568 struct attribute_spec.handler. */
8569
8570static tree
8571handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8572 int ARG_UNUSED (flags), bool *no_add_attrs)
8573{
8574 if (TREE_CODE (*node) == FUNCTION_DECL)
8575 DECL_IS_RETURNS_TWICE (*node) = 1;
8576 else
8577 {
5c498b10 8578 warning (OPT_Wattributes, "%qE attribute ignored", name);
6e9a3221
AN
8579 *no_add_attrs = true;
8580 }
8581
8582 return NULL_TREE;
8583}
8584
349ae713
NB
8585/* Handle a "no_limit_stack" attribute; arguments as in
8586 struct attribute_spec.handler. */
8587
8588static tree
35b1a6fa 8589handle_no_limit_stack_attribute (tree *node, tree name,
e18476eb
BI
8590 tree ARG_UNUSED (args),
8591 int ARG_UNUSED (flags),
a742c759 8592 bool *no_add_attrs)
349ae713
NB
8593{
8594 tree decl = *node;
8595
8596 if (TREE_CODE (decl) != FUNCTION_DECL)
8597 {
c5d75364
MLI
8598 error_at (DECL_SOURCE_LOCATION (decl),
8599 "%qE attribute applies only to functions", name);
349ae713
NB
8600 *no_add_attrs = true;
8601 }
8602 else if (DECL_INITIAL (decl))
8603 {
c5d75364
MLI
8604 error_at (DECL_SOURCE_LOCATION (decl),
8605 "can%'t set %qE attribute after definition", name);
349ae713
NB
8606 *no_add_attrs = true;
8607 }
8608 else
8609 DECL_NO_LIMIT_STACK (decl) = 1;
8610
8611 return NULL_TREE;
8612}
8613
8614/* Handle a "pure" attribute; arguments as in
8615 struct attribute_spec.handler. */
8616
8617static tree
e18476eb
BI
8618handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8619 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
8620{
8621 if (TREE_CODE (*node) == FUNCTION_DECL)
becfd6e5 8622 DECL_PURE_P (*node) = 1;
349ae713
NB
8623 /* ??? TODO: Support types. */
8624 else
8625 {
5c498b10 8626 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8627 *no_add_attrs = true;
8628 }
8629
8630 return NULL_TREE;
8631}
8632
0a35513e
AH
8633/* Digest an attribute list destined for a transactional memory statement.
8634 ALLOWED is the set of attributes that are allowed for this statement;
8635 return the attribute we parsed. Multiple attributes are never allowed. */
8636
8637int
8638parse_tm_stmt_attr (tree attrs, int allowed)
8639{
8640 tree a_seen = NULL;
8641 int m_seen = 0;
8642
8643 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8644 {
8645 tree a = TREE_PURPOSE (attrs);
8646 int m = 0;
8647
8648 if (is_attribute_p ("outer", a))
8649 m = TM_STMT_ATTR_OUTER;
8650
8651 if ((m & allowed) == 0)
8652 {
8653 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8654 continue;
8655 }
8656
8657 if (m_seen == 0)
8658 {
8659 a_seen = a;
8660 m_seen = m;
8661 }
8662 else if (m_seen == m)
8663 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8664 else
8665 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8666 }
8667
8668 return m_seen;
8669}
8670
8671/* Transform a TM attribute name into a maskable integer and back.
8672 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8673 to how the lack of an attribute is treated. */
8674
8675int
8676tm_attr_to_mask (tree attr)
8677{
8678 if (attr == NULL)
8679 return 0;
8680 if (is_attribute_p ("transaction_safe", attr))
8681 return TM_ATTR_SAFE;
8682 if (is_attribute_p ("transaction_callable", attr))
8683 return TM_ATTR_CALLABLE;
8684 if (is_attribute_p ("transaction_pure", attr))
8685 return TM_ATTR_PURE;
8686 if (is_attribute_p ("transaction_unsafe", attr))
8687 return TM_ATTR_IRREVOCABLE;
8688 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8689 return TM_ATTR_MAY_CANCEL_OUTER;
8690 return 0;
8691}
8692
8693tree
8694tm_mask_to_attr (int mask)
8695{
8696 const char *str;
8697 switch (mask)
8698 {
8699 case TM_ATTR_SAFE:
8700 str = "transaction_safe";
8701 break;
8702 case TM_ATTR_CALLABLE:
8703 str = "transaction_callable";
8704 break;
8705 case TM_ATTR_PURE:
8706 str = "transaction_pure";
8707 break;
8708 case TM_ATTR_IRREVOCABLE:
8709 str = "transaction_unsafe";
8710 break;
8711 case TM_ATTR_MAY_CANCEL_OUTER:
8712 str = "transaction_may_cancel_outer";
8713 break;
8714 default:
8715 gcc_unreachable ();
8716 }
8717 return get_identifier (str);
8718}
8719
8720/* Return the first TM attribute seen in LIST. */
8721
8722tree
8723find_tm_attribute (tree list)
8724{
8725 for (; list ; list = TREE_CHAIN (list))
8726 {
8727 tree name = TREE_PURPOSE (list);
8728 if (tm_attr_to_mask (name) != 0)
8729 return name;
8730 }
8731 return NULL_TREE;
8732}
8733
8734/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8735 Here we accept only function types, and verify that none of the other
8736 function TM attributes are also applied. */
8737/* ??? We need to accept class types for C++, but not C. This greatly
8738 complicates this function, since we can no longer rely on the extra
8739 processing given by function_type_required. */
8740
8741static tree
8742handle_tm_attribute (tree *node, tree name, tree args,
8743 int flags, bool *no_add_attrs)
8744{
8745 /* Only one path adds the attribute; others don't. */
8746 *no_add_attrs = true;
8747
8748 switch (TREE_CODE (*node))
8749 {
8750 case RECORD_TYPE:
8751 case UNION_TYPE:
8752 /* Only tm_callable and tm_safe apply to classes. */
8753 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8754 goto ignored;
8755 /* FALLTHRU */
8756
8757 case FUNCTION_TYPE:
8758 case METHOD_TYPE:
8759 {
8760 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8761 if (old_name == name)
8762 ;
8763 else if (old_name != NULL_TREE)
8764 error ("type was previously declared %qE", old_name);
8765 else
8766 *no_add_attrs = false;
8767 }
8768 break;
8769
b8fd7909
JM
8770 case FUNCTION_DECL:
8771 {
8772 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8773 want to set transaction_safe on the type. */
8774 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8775 if (!TYPE_P (DECL_CONTEXT (*node)))
8776 error_at (DECL_SOURCE_LOCATION (*node),
8777 "%<transaction_safe_dynamic%> may only be specified for "
8778 "a virtual function");
8779 *no_add_attrs = false;
8780 decl_attributes (&TREE_TYPE (*node),
8781 build_tree_list (get_identifier ("transaction_safe"),
8782 NULL_TREE),
8783 0);
8784 break;
8785 }
8786
0a35513e
AH
8787 case POINTER_TYPE:
8788 {
8789 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8790 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8791 {
8792 tree fn_tmp = TREE_TYPE (*node);
8793 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8794 *node = build_pointer_type (fn_tmp);
8795 break;
8796 }
8797 }
8798 /* FALLTHRU */
8799
8800 default:
8801 /* If a function is next, pass it on to be tried next. */
8802 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8803 return tree_cons (name, args, NULL);
8804
8805 ignored:
8806 warning (OPT_Wattributes, "%qE attribute ignored", name);
8807 break;
8808 }
8809
8810 return NULL_TREE;
8811}
8812
8813/* Handle the TM_WRAP attribute; arguments as in
8814 struct attribute_spec.handler. */
8815
8816static tree
8817handle_tm_wrap_attribute (tree *node, tree name, tree args,
8818 int ARG_UNUSED (flags), bool *no_add_attrs)
8819{
8820 tree decl = *node;
8821
8822 /* We don't need the attribute even on success, since we
8823 record the entry in an external table. */
8824 *no_add_attrs = true;
8825
8826 if (TREE_CODE (decl) != FUNCTION_DECL)
8827 warning (OPT_Wattributes, "%qE attribute ignored", name);
8828 else
8829 {
8830 tree wrap_decl = TREE_VALUE (args);
661a0813
MP
8831 if (error_operand_p (wrap_decl))
8832 ;
8833 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
21b634ae 8834 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
0a35513e
AH
8835 error ("%qE argument not an identifier", name);
8836 else
8837 {
8838 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8839 wrap_decl = lookup_name (wrap_decl);
8840 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8841 {
8842 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8843 TREE_TYPE (wrap_decl)))
8844 record_tm_replacement (wrap_decl, decl);
8845 else
8846 error ("%qD is not compatible with %qD", wrap_decl, decl);
8847 }
8848 else
c07d7c02 8849 error ("%qE argument is not a function", name);
0a35513e
AH
8850 }
8851 }
8852
8853 return NULL_TREE;
8854}
8855
8856/* Ignore the given attribute. Used when this attribute may be usefully
8857 overridden by the target, but is not used generically. */
8858
8859static tree
8860ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8861 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8862 bool *no_add_attrs)
8863{
8864 *no_add_attrs = true;
8865 return NULL_TREE;
8866}
8867
dcd6de6d
ZD
8868/* Handle a "no vops" attribute; arguments as in
8869 struct attribute_spec.handler. */
8870
8871static tree
8872handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8873 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8874 bool *ARG_UNUSED (no_add_attrs))
8875{
8876 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8877 DECL_IS_NOVOPS (*node) = 1;
8878 return NULL_TREE;
8879}
8880
349ae713
NB
8881/* Handle a "deprecated" attribute; arguments as in
8882 struct attribute_spec.handler. */
35b1a6fa 8883
349ae713 8884static tree
35b1a6fa 8885handle_deprecated_attribute (tree *node, tree name,
9b86d6bb 8886 tree args, int flags,
a742c759 8887 bool *no_add_attrs)
349ae713
NB
8888{
8889 tree type = NULL_TREE;
8890 int warn = 0;
c51a1ba9 8891 tree what = NULL_TREE;
35b1a6fa 8892
9b86d6bb
L
8893 if (!args)
8894 *no_add_attrs = true;
8895 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8896 {
8897 error ("deprecated message is not a string");
8898 *no_add_attrs = true;
8899 }
8900
349ae713
NB
8901 if (DECL_P (*node))
8902 {
8903 tree decl = *node;
8904 type = TREE_TYPE (decl);
35b1a6fa 8905
349ae713
NB
8906 if (TREE_CODE (decl) == TYPE_DECL
8907 || TREE_CODE (decl) == PARM_DECL
21b634ae 8908 || VAR_OR_FUNCTION_DECL_P (decl)
a1178b30 8909 || TREE_CODE (decl) == FIELD_DECL
fd5c817a 8910 || TREE_CODE (decl) == CONST_DECL
a1178b30 8911 || objc_method_decl (TREE_CODE (decl)))
349ae713
NB
8912 TREE_DEPRECATED (decl) = 1;
8913 else
8914 warn = 1;
8915 }
8916 else if (TYPE_P (*node))
8917 {
8918 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 8919 *node = build_variant_type_copy (*node);
349ae713
NB
8920 TREE_DEPRECATED (*node) = 1;
8921 type = *node;
8922 }
8923 else
8924 warn = 1;
35b1a6fa 8925
349ae713
NB
8926 if (warn)
8927 {
8928 *no_add_attrs = true;
8929 if (type && TYPE_NAME (type))
8930 {
8931 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9 8932 what = TYPE_NAME (*node);
349ae713
NB
8933 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8934 && DECL_NAME (TYPE_NAME (type)))
c51a1ba9 8935 what = DECL_NAME (TYPE_NAME (type));
349ae713
NB
8936 }
8937 if (what)
5c498b10 8938 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
349ae713 8939 else
5c498b10 8940 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8941 }
8942
8943 return NULL_TREE;
8944}
8945
349ae713
NB
8946/* Handle a "vector_size" attribute; arguments as in
8947 struct attribute_spec.handler. */
8948
8949static tree
35b1a6fa 8950handle_vector_size_attribute (tree *node, tree name, tree args,
e18476eb 8951 int ARG_UNUSED (flags),
a742c759 8952 bool *no_add_attrs)
349ae713
NB
8953{
8954 unsigned HOST_WIDE_INT vecsize, nunits;
ef4bddc2 8955 machine_mode orig_mode;
4a5eab38 8956 tree type = *node, new_type, size;
349ae713
NB
8957
8958 *no_add_attrs = true;
8959
4a5eab38 8960 size = TREE_VALUE (args);
661a0813
MP
8961 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8962 && TREE_CODE (size) != FUNCTION_DECL)
5d77fb19 8963 size = default_conversion (size);
4a5eab38 8964
cc269bb6 8965 if (!tree_fits_uhwi_p (size))
349ae713 8966 {
5c498b10 8967 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8968 return NULL_TREE;
8969 }
8970
8971 /* Get the vector size (in bytes). */
ae7e9ddd 8972 vecsize = tree_to_uhwi (size);
349ae713
NB
8973
8974 /* We need to provide for vector pointers, vector arrays, and
8975 functions returning vectors. For example:
8976
8977 __attribute__((vector_size(16))) short *foo;
8978
8979 In this case, the mode is SI, but the type being modified is
8980 HI, so we need to look further. */
8981
8982 while (POINTER_TYPE_P (type)
8983 || TREE_CODE (type) == FUNCTION_TYPE
43dc123f 8984 || TREE_CODE (type) == METHOD_TYPE
270e749d
JJ
8985 || TREE_CODE (type) == ARRAY_TYPE
8986 || TREE_CODE (type) == OFFSET_TYPE)
349ae713
NB
8987 type = TREE_TYPE (type);
8988
8989 /* Get the mode of the type being modified. */
8990 orig_mode = TYPE_MODE (type);
8991
270e749d
JJ
8992 if ((!INTEGRAL_TYPE_P (type)
8993 && !SCALAR_FLOAT_TYPE_P (type)
8994 && !FIXED_POINT_TYPE_P (type))
3d8bf70f 8995 || (!SCALAR_FLOAT_MODE_P (orig_mode)
ab22c1fa
CF
8996 && GET_MODE_CLASS (orig_mode) != MODE_INT
8997 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
cc269bb6 8998 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
e4e5261f 8999 || TREE_CODE (type) == BOOLEAN_TYPE)
349ae713 9000 {
c51a1ba9 9001 error ("invalid vector type for attribute %qE", name);
349ae713
NB
9002 return NULL_TREE;
9003 }
9004
ae7e9ddd 9005 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
ee8960e5
JJ
9006 {
9007 error ("vector size not an integral multiple of component size");
9008 return NULL;
9009 }
9010
9011 if (vecsize == 0)
9012 {
9013 error ("zero vector size");
9014 return NULL;
9015 }
9016
349ae713 9017 /* Calculate how many units fit in the vector. */
ae7e9ddd 9018 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
26277d41 9019 if (nunits & (nunits - 1))
349ae713 9020 {
26277d41 9021 error ("number of components of the vector not a power of two");
349ae713
NB
9022 return NULL_TREE;
9023 }
9024
26277d41 9025 new_type = build_vector_type (type, nunits);
349ae713
NB
9026
9027 /* Build back pointers if needed. */
5dc11954 9028 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
349ae713
NB
9029
9030 return NULL_TREE;
9031}
9032
b34c7881
JT
9033/* Handle the "nonnull" attribute. */
9034static tree
e18476eb
BI
9035handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9036 tree args, int ARG_UNUSED (flags),
a742c759 9037 bool *no_add_attrs)
b34c7881
JT
9038{
9039 tree type = *node;
9040 unsigned HOST_WIDE_INT attr_arg_num;
9041
9042 /* If no arguments are specified, all pointer arguments should be
95bd1dd7 9043 non-null. Verify a full prototype is given so that the arguments
b34c7881 9044 will have the correct types when we actually check them later. */
3f75a254 9045 if (!args)
b34c7881 9046 {
f4da8dce 9047 if (!prototype_p (type))
b34c7881
JT
9048 {
9049 error ("nonnull attribute without arguments on a non-prototype");
6de9cd9a 9050 *no_add_attrs = true;
b34c7881
JT
9051 }
9052 return NULL_TREE;
9053 }
9054
9055 /* Argument list specified. Verify that each argument number references
9056 a pointer argument. */
5d77fb19 9057 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
b34c7881 9058 {
6de9cd9a 9059 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
b34c7881 9060
5d77fb19
MG
9061 tree arg = TREE_VALUE (args);
9062 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9063 && TREE_CODE (arg) != FUNCTION_DECL)
9064 arg = default_conversion (arg);
9065
9066 if (!get_nonnull_operand (arg, &arg_num))
b34c7881 9067 {
40b97a2e 9068 error ("nonnull argument has invalid operand number (argument %lu)",
b34c7881
JT
9069 (unsigned long) attr_arg_num);
9070 *no_add_attrs = true;
9071 return NULL_TREE;
9072 }
9073
e19a18d4 9074 if (prototype_p (type))
b34c7881 9075 {
e19a18d4
NF
9076 function_args_iterator iter;
9077 tree argument;
9078
9079 function_args_iter_init (&iter, type);
9080 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
b34c7881 9081 {
e19a18d4
NF
9082 argument = function_args_iter_cond (&iter);
9083 if (argument == NULL_TREE || ck_num == arg_num)
b34c7881 9084 break;
b34c7881
JT
9085 }
9086
3f75a254 9087 if (!argument
e19a18d4 9088 || TREE_CODE (argument) == VOID_TYPE)
b34c7881 9089 {
40b97a2e 9090 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
b34c7881
JT
9091 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9092 *no_add_attrs = true;
9093 return NULL_TREE;
9094 }
9095
e19a18d4 9096 if (TREE_CODE (argument) != POINTER_TYPE)
b34c7881 9097 {
40b97a2e 9098 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
b34c7881
JT
9099 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9100 *no_add_attrs = true;
9101 return NULL_TREE;
9102 }
9103 }
9104 }
9105
9106 return NULL_TREE;
9107}
9108
9109/* Check the argument list of a function call for null in argument slots
94a0dd7b 9110 that are marked as requiring a non-null pointer argument. The NARGS
3342fd71 9111 arguments are passed in the array ARGARRAY. */
b34c7881
JT
9112
9113static void
3342fd71 9114check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
b34c7881 9115{
332f1d24 9116 tree a;
94a0dd7b 9117 int i;
b34c7881 9118
332f1d24
JJ
9119 attrs = lookup_attribute ("nonnull", attrs);
9120 if (attrs == NULL_TREE)
9121 return;
9122
9123 a = attrs;
9124 /* See if any of the nonnull attributes has no arguments. If so,
9125 then every pointer argument is checked (in which case the check
9126 for pointer type is done in check_nonnull_arg). */
9127 if (TREE_VALUE (a) != NULL_TREE)
9128 do
9129 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9130 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9131
9132 if (a != NULL_TREE)
9133 for (i = 0; i < nargs; i++)
3342fd71 9134 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
332f1d24
JJ
9135 i + 1);
9136 else
b34c7881 9137 {
332f1d24
JJ
9138 /* Walk the argument list. If we encounter an argument number we
9139 should check for non-null, do it. */
9140 for (i = 0; i < nargs; i++)
b34c7881 9141 {
332f1d24 9142 for (a = attrs; ; a = TREE_CHAIN (a))
6de9cd9a 9143 {
332f1d24
JJ
9144 a = lookup_attribute ("nonnull", a);
9145 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9146 break;
6de9cd9a 9147 }
332f1d24
JJ
9148
9149 if (a != NULL_TREE)
3342fd71 9150 check_function_arguments_recurse (check_nonnull_arg, &loc,
332f1d24 9151 argarray[i], i + 1);
b34c7881
JT
9152 }
9153 }
9154}
9155
254986c7 9156/* Check that the Nth argument of a function call (counting backwards
94a0dd7b
SL
9157 from the end) is a (pointer)0. The NARGS arguments are passed in the
9158 array ARGARRAY. */
3d091dac
KG
9159
9160static void
dde05067 9161check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
3d091dac 9162{
dde05067 9163 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
3d091dac
KG
9164
9165 if (attr)
9166 {
94a0dd7b
SL
9167 int len = 0;
9168 int pos = 0;
9169 tree sentinel;
dde05067
NF
9170 function_args_iterator iter;
9171 tree t;
c22cacf3 9172
94a0dd7b 9173 /* Skip over the named arguments. */
dde05067 9174 FOREACH_FUNCTION_ARGS (fntype, t, iter)
c22cacf3 9175 {
dde05067
NF
9176 if (len == nargs)
9177 break;
94a0dd7b
SL
9178 len++;
9179 }
254986c7 9180
94a0dd7b
SL
9181 if (TREE_VALUE (attr))
9182 {
9183 tree p = TREE_VALUE (TREE_VALUE (attr));
9184 pos = TREE_INT_CST_LOW (p);
9185 }
254986c7 9186
94a0dd7b
SL
9187 /* The sentinel must be one of the varargs, i.e.
9188 in position >= the number of fixed arguments. */
9189 if ((nargs - 1 - pos) < len)
9190 {
7332899a 9191 warning (OPT_Wformat_,
94a0dd7b
SL
9192 "not enough variable arguments to fit a sentinel");
9193 return;
3d091dac 9194 }
94a0dd7b
SL
9195
9196 /* Validate the sentinel. */
9197 sentinel = argarray[nargs - 1 - pos];
9198 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9199 || !integer_zerop (sentinel))
9200 /* Although __null (in C++) is only an integer we allow it
9201 nevertheless, as we are guaranteed that it's exactly
9202 as wide as a pointer, and we don't want to force
9203 users to cast the NULL they have written there.
9204 We warn with -Wstrict-null-sentinel, though. */
9205 && (warn_strict_null_sentinel || null_node != sentinel))
7332899a 9206 warning (OPT_Wformat_, "missing sentinel in function call");
3d091dac
KG
9207 }
9208}
9209
b34c7881
JT
9210/* Helper for check_function_nonnull; given a list of operands which
9211 must be non-null in ARGS, determine if operand PARAM_NUM should be
9212 checked. */
9213
9214static bool
35b1a6fa 9215nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
b34c7881 9216{
6de9cd9a 9217 unsigned HOST_WIDE_INT arg_num = 0;
b34c7881
JT
9218
9219 for (; args; args = TREE_CHAIN (args))
9220 {
366de0ce
NS
9221 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9222
9223 gcc_assert (found);
b34c7881
JT
9224
9225 if (arg_num == param_num)
9226 return true;
9227 }
9228 return false;
9229}
9230
9231/* Check that the function argument PARAM (which is operand number
9232 PARAM_NUM) is non-null. This is called by check_function_nonnull
9233 via check_function_arguments_recurse. */
9234
9235static void
3342fd71 9236check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
b34c7881 9237{
3342fd71
BS
9238 location_t *ploc = (location_t *) ctx;
9239
b34c7881
JT
9240 /* Just skip checking the argument if it's not a pointer. This can
9241 happen if the "nonnull" attribute was given without an operand
9242 list (which means to check every pointer argument). */
9243
9244 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9245 return;
9246
9247 if (integer_zerop (param))
3342fd71
BS
9248 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9249 "(argument %lu)", (unsigned long) param_num);
b34c7881
JT
9250}
9251
9252/* Helper for nonnull attribute handling; fetch the operand number
9253 from the attribute argument list. */
9254
9255static bool
35b1a6fa 9256get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
b34c7881 9257{
807e902e
KZ
9258 /* Verify the arg number is a small constant. */
9259 if (tree_fits_uhwi_p (arg_num_expr))
9260 {
9261 *valp = TREE_INT_CST_LOW (arg_num_expr);
9262 return true;
9263 }
9264 else
b34c7881 9265 return false;
b34c7881 9266}
39f2f3c8
RS
9267
9268/* Handle a "nothrow" attribute; arguments as in
9269 struct attribute_spec.handler. */
9270
9271static tree
e18476eb
BI
9272handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9273 int ARG_UNUSED (flags), bool *no_add_attrs)
39f2f3c8
RS
9274{
9275 if (TREE_CODE (*node) == FUNCTION_DECL)
9276 TREE_NOTHROW (*node) = 1;
9277 /* ??? TODO: Support types. */
9278 else
9279 {
5c498b10 9280 warning (OPT_Wattributes, "%qE attribute ignored", name);
39f2f3c8
RS
9281 *no_add_attrs = true;
9282 }
9283
9284 return NULL_TREE;
9285}
0bfa5f65
RH
9286
9287/* Handle a "cleanup" attribute; arguments as in
9288 struct attribute_spec.handler. */
9289
9290static tree
35b1a6fa 9291handle_cleanup_attribute (tree *node, tree name, tree args,
e18476eb 9292 int ARG_UNUSED (flags), bool *no_add_attrs)
0bfa5f65
RH
9293{
9294 tree decl = *node;
9295 tree cleanup_id, cleanup_decl;
9296
9297 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9298 for global destructors in C++. This requires infrastructure that
9299 we don't have generically at the moment. It's also not a feature
9300 we'd be missing too much, since we do have attribute constructor. */
0ae9bd27 9301 if (!VAR_P (decl) || TREE_STATIC (decl))
0bfa5f65 9302 {
5c498b10 9303 warning (OPT_Wattributes, "%qE attribute ignored", name);
0bfa5f65
RH
9304 *no_add_attrs = true;
9305 return NULL_TREE;
9306 }
9307
9308 /* Verify that the argument is a function in scope. */
9309 /* ??? We could support pointers to functions here as well, if
9310 that was considered desirable. */
9311 cleanup_id = TREE_VALUE (args);
9312 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9313 {
40b97a2e 9314 error ("cleanup argument not an identifier");
0bfa5f65
RH
9315 *no_add_attrs = true;
9316 return NULL_TREE;
9317 }
10e6657a 9318 cleanup_decl = lookup_name (cleanup_id);
0bfa5f65
RH
9319 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9320 {
40b97a2e 9321 error ("cleanup argument not a function");
0bfa5f65
RH
9322 *no_add_attrs = true;
9323 return NULL_TREE;
9324 }
9325
35b1a6fa 9326 /* That the function has proper type is checked with the
0bfa5f65
RH
9327 eventual call to build_function_call. */
9328
9329 return NULL_TREE;
9330}
72954a4f
JM
9331
9332/* Handle a "warn_unused_result" attribute. No special handling. */
9333
9334static tree
9335handle_warn_unused_result_attribute (tree *node, tree name,
e18476eb
BI
9336 tree ARG_UNUSED (args),
9337 int ARG_UNUSED (flags), bool *no_add_attrs)
72954a4f
JM
9338{
9339 /* Ignore the attribute for functions not returning any value. */
9340 if (VOID_TYPE_P (TREE_TYPE (*node)))
9341 {
5c498b10 9342 warning (OPT_Wattributes, "%qE attribute ignored", name);
72954a4f
JM
9343 *no_add_attrs = true;
9344 }
9345
9346 return NULL_TREE;
9347}
3d091dac
KG
9348
9349/* Handle a "sentinel" attribute. */
9350
9351static tree
254986c7 9352handle_sentinel_attribute (tree *node, tree name, tree args,
3d091dac
KG
9353 int ARG_UNUSED (flags), bool *no_add_attrs)
9354{
f4da8dce 9355 if (!prototype_p (*node))
3d091dac 9356 {
5c498b10
DD
9357 warning (OPT_Wattributes,
9358 "%qE attribute requires prototypes with named arguments", name);
3d091dac 9359 *no_add_attrs = true;
3d091dac 9360 }
254986c7
KG
9361 else
9362 {
dcf0c47e 9363 if (!stdarg_p (*node))
c22cacf3 9364 {
5c498b10
DD
9365 warning (OPT_Wattributes,
9366 "%qE attribute only applies to variadic functions", name);
254986c7
KG
9367 *no_add_attrs = true;
9368 }
9369 }
c22cacf3 9370
254986c7 9371 if (args)
3d091dac 9372 {
254986c7 9373 tree position = TREE_VALUE (args);
661a0813
MP
9374 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9375 && TREE_CODE (position) != FUNCTION_DECL)
9376 position = default_conversion (position);
254986c7 9377
661a0813
MP
9378 if (TREE_CODE (position) != INTEGER_CST
9379 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
c22cacf3 9380 {
b8698a0f 9381 warning (OPT_Wattributes,
aa86a51b 9382 "requested position is not an integer constant");
254986c7
KG
9383 *no_add_attrs = true;
9384 }
9385 else
c22cacf3 9386 {
254986c7
KG
9387 if (tree_int_cst_lt (position, integer_zero_node))
9388 {
aa86a51b
DM
9389 warning (OPT_Wattributes,
9390 "requested position is less than zero");
254986c7
KG
9391 *no_add_attrs = true;
9392 }
9393 }
3d091dac 9394 }
c22cacf3 9395
3d091dac
KG
9396 return NULL_TREE;
9397}
b5d32c25
KG
9398
9399/* Handle a "type_generic" attribute. */
9400
9401static tree
9402handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9403 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9404 bool * ARG_UNUSED (no_add_attrs))
9405{
3bf5906b
KG
9406 /* Ensure we have a function type. */
9407 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
b8698a0f 9408
3bf5906b 9409 /* Ensure we have a variadic function. */
dcf0c47e 9410 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5d32c25
KG
9411
9412 return NULL_TREE;
9413}
ab442df7 9414
5779e713 9415/* Handle a "target" attribute. */
ab442df7
MM
9416
9417static tree
5779e713 9418handle_target_attribute (tree *node, tree name, tree args, int flags,
ab442df7
MM
9419 bool *no_add_attrs)
9420{
9421 /* Ensure we have a function type. */
9422 if (TREE_CODE (*node) != FUNCTION_DECL)
9423 {
9424 warning (OPT_Wattributes, "%qE attribute ignored", name);
9425 *no_add_attrs = true;
9426 }
3b1661a9
ES
9427 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9428 {
9429 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9430 "with %qs attribute", name, "target_clones");
9431 *no_add_attrs = true;
9432 }
ab442df7 9433 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
32887460 9434 flags))
ab442df7
MM
9435 *no_add_attrs = true;
9436
9437 return NULL_TREE;
9438}
9439
3b1661a9
ES
9440/* Handle a "target_clones" attribute. */
9441
9442static tree
9443handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9444 int ARG_UNUSED (flags), bool *no_add_attrs)
9445{
9446 /* Ensure we have a function type. */
9447 if (TREE_CODE (*node) == FUNCTION_DECL)
9448 {
9449 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9450 {
9451 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9452 "with %qs attribute", name, "always_inline");
9453 *no_add_attrs = true;
9454 }
9455 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9456 {
9457 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9458 "with %qs attribute", name, "target");
9459 *no_add_attrs = true;
9460 }
9461 else
9462 /* Do not inline functions with multiple clone targets. */
9463 DECL_UNINLINABLE (*node) = 1;
9464 }
9465 else
9466 {
9467 warning (OPT_Wattributes, "%qE attribute ignored", name);
9468 *no_add_attrs = true;
9469 }
9470 return NULL_TREE;
9471}
9472
ab442df7
MM
9473/* Arguments being collected for optimization. */
9474typedef const char *const_char_p; /* For DEF_VEC_P. */
9771b263 9475static GTY(()) vec<const_char_p, va_gc> *optimize_args;
ab442df7
MM
9476
9477
9478/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9479 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9480 false for #pragma GCC optimize. */
9481
9482bool
9483parse_optimize_options (tree args, bool attr_p)
9484{
9485 bool ret = true;
9486 unsigned opt_argc;
9487 unsigned i;
9488 const char **opt_argv;
6e2f1956
JM
9489 struct cl_decoded_option *decoded_options;
9490 unsigned int decoded_options_count;
ab442df7
MM
9491 tree ap;
9492
9493 /* Build up argv vector. Just in case the string is stored away, use garbage
9494 collected strings. */
9771b263
DN
9495 vec_safe_truncate (optimize_args, 0);
9496 vec_safe_push (optimize_args, (const char *) NULL);
ab442df7
MM
9497
9498 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9499 {
9500 tree value = TREE_VALUE (ap);
9501
9502 if (TREE_CODE (value) == INTEGER_CST)
9503 {
9504 char buffer[20];
9505 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9771b263 9506 vec_safe_push (optimize_args, ggc_strdup (buffer));
ab442df7
MM
9507 }
9508
9509 else if (TREE_CODE (value) == STRING_CST)
9510 {
9511 /* Split string into multiple substrings. */
9512 size_t len = TREE_STRING_LENGTH (value);
9513 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9514 char *end = p + len;
9515 char *comma;
9516 char *next_p = p;
9517
9518 while (next_p != NULL)
9519 {
9520 size_t len2;
9521 char *q, *r;
9522
9523 p = next_p;
9524 comma = strchr (p, ',');
9525 if (comma)
9526 {
9527 len2 = comma - p;
9528 *comma = '\0';
9529 next_p = comma+1;
9530 }
9531 else
9532 {
9533 len2 = end - p;
9534 next_p = NULL;
9535 }
9536
a9429e29 9537 r = q = (char *) ggc_alloc_atomic (len2 + 3);
ab442df7
MM
9538
9539 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9540 options. */
9541 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9542 {
9543 ret = false;
9544 if (attr_p)
9545 warning (OPT_Wattributes,
06730c5d 9546 "bad option %s to optimize attribute", p);
ab442df7
MM
9547 else
9548 warning (OPT_Wpragmas,
de621752 9549 "bad option %s to pragma attribute", p);
ab442df7
MM
9550 continue;
9551 }
9552
9553 if (*p != '-')
9554 {
9555 *r++ = '-';
9556
9557 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9558 itself is -Os, and any other switch begins with a -f. */
9559 if ((*p >= '0' && *p <= '9')
9560 || (p[0] == 's' && p[1] == '\0'))
9561 *r++ = 'O';
9562 else if (*p != 'O')
9563 *r++ = 'f';
9564 }
9565
9566 memcpy (r, p, len2);
9567 r[len2] = '\0';
9771b263 9568 vec_safe_push (optimize_args, (const char *) q);
ab442df7
MM
9569 }
9570
9571 }
9572 }
9573
9771b263 9574 opt_argc = optimize_args->length ();
ab442df7
MM
9575 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9576
9577 for (i = 1; i < opt_argc; i++)
9771b263 9578 opt_argv[i] = (*optimize_args)[i];
ab442df7
MM
9579
9580 /* Now parse the options. */
a75bfaa6
JM
9581 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9582 &decoded_options,
9583 &decoded_options_count);
9584 decode_options (&global_options, &global_options_set,
a4d8c676
JM
9585 decoded_options, decoded_options_count,
9586 input_location, global_dc);
ab442df7 9587
2b7e2984
SE
9588 targetm.override_options_after_change();
9589
9771b263 9590 optimize_args->truncate (0);
ab442df7
MM
9591 return ret;
9592}
9593
9594/* For handling "optimize" attribute. arguments as in
9595 struct attribute_spec.handler. */
9596
9597static tree
9598handle_optimize_attribute (tree *node, tree name, tree args,
9599 int ARG_UNUSED (flags), bool *no_add_attrs)
9600{
9601 /* Ensure we have a function type. */
9602 if (TREE_CODE (*node) != FUNCTION_DECL)
9603 {
9604 warning (OPT_Wattributes, "%qE attribute ignored", name);
9605 *no_add_attrs = true;
9606 }
9607 else
9608 {
9609 struct cl_optimization cur_opts;
9610 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9611
9612 /* Save current options. */
46625112 9613 cl_optimization_save (&cur_opts, &global_options);
ab442df7
MM
9614
9615 /* If we previously had some optimization options, use them as the
9616 default. */
9617 if (old_opts)
46625112
JM
9618 cl_optimization_restore (&global_options,
9619 TREE_OPTIMIZATION (old_opts));
ab442df7
MM
9620
9621 /* Parse options, and update the vector. */
9622 parse_optimize_options (args, true);
9623 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
bf7b5747 9624 = build_optimization_node (&global_options);
ab442df7
MM
9625
9626 /* Restore current options. */
46625112 9627 cl_optimization_restore (&global_options, &cur_opts);
ab442df7
MM
9628 }
9629
9630 return NULL_TREE;
9631}
7458026b
ILT
9632
9633/* Handle a "no_split_stack" attribute. */
9634
9635static tree
9636handle_no_split_stack_attribute (tree *node, tree name,
9637 tree ARG_UNUSED (args),
9638 int ARG_UNUSED (flags),
9639 bool *no_add_attrs)
9640{
9641 tree decl = *node;
9642
9643 if (TREE_CODE (decl) != FUNCTION_DECL)
9644 {
9645 error_at (DECL_SOURCE_LOCATION (decl),
9646 "%qE attribute applies only to functions", name);
9647 *no_add_attrs = true;
9648 }
9649 else if (DECL_INITIAL (decl))
9650 {
9651 error_at (DECL_SOURCE_LOCATION (decl),
9652 "can%'t set %qE attribute after definition", name);
9653 *no_add_attrs = true;
9654 }
9655
9656 return NULL_TREE;
9657}
826cacfe
MG
9658
9659/* Handle a "returns_nonnull" attribute; arguments as in
9660 struct attribute_spec.handler. */
9661
9662static tree
9663handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9664 bool *no_add_attrs)
9665{
9666 // Even without a prototype we still have a return type we can check.
9667 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9668 {
9669 error ("returns_nonnull attribute on a function not returning a pointer");
9670 *no_add_attrs = true;
9671 }
9672 return NULL_TREE;
9673}
9674
976d5a22
TT
9675/* Handle a "designated_init" attribute; arguments as in
9676 struct attribute_spec.handler. */
9677
9678static tree
9679handle_designated_init_attribute (tree *node, tree name, tree, int,
9680 bool *no_add_attrs)
9681{
9682 if (TREE_CODE (*node) != RECORD_TYPE)
9683 {
9684 error ("%qE attribute is only valid on %<struct%> type", name);
9685 *no_add_attrs = true;
9686 }
9687 return NULL_TREE;
9688}
9689
b34c7881 9690\f
dde05067 9691/* Check for valid arguments being passed to a function with FNTYPE.
3342fd71
BS
9692 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9693 diagnostics. */
b34c7881 9694void
3342fd71
BS
9695check_function_arguments (location_t loc, const_tree fntype, int nargs,
9696 tree *argarray)
b34c7881
JT
9697{
9698 /* Check for null being passed in a pointer argument that must be
9699 non-null. We also need to do this if format checking is enabled. */
9700
9701 if (warn_nonnull)
3342fd71 9702 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
b34c7881
JT
9703
9704 /* Check for errors in format strings. */
9705
e6c69da0 9706 if (warn_format || warn_suggest_attribute_format)
dde05067 9707 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
7876a414
KG
9708
9709 if (warn_format)
dde05067 9710 check_function_sentinel (fntype, nargs, argarray);
b34c7881
JT
9711}
9712
9713/* Generic argument checking recursion routine. PARAM is the argument to
9714 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9715 once the argument is resolved. CTX is context for the callback. */
9716void
35b1a6fa
AJ
9717check_function_arguments_recurse (void (*callback)
9718 (void *, tree, unsigned HOST_WIDE_INT),
9719 void *ctx, tree param,
9720 unsigned HOST_WIDE_INT param_num)
b34c7881 9721{
1043771b 9722 if (CONVERT_EXPR_P (param)
1344f9a3
JM
9723 && (TYPE_PRECISION (TREE_TYPE (param))
9724 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
b34c7881
JT
9725 {
9726 /* Strip coercion. */
9727 check_function_arguments_recurse (callback, ctx,
6de9cd9a 9728 TREE_OPERAND (param, 0), param_num);
b34c7881
JT
9729 return;
9730 }
9731
9732 if (TREE_CODE (param) == CALL_EXPR)
9733 {
5039610b 9734 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
b34c7881
JT
9735 tree attrs;
9736 bool found_format_arg = false;
9737
9738 /* See if this is a call to a known internationalization function
9739 that modifies a format arg. Such a function may have multiple
9740 format_arg attributes (for example, ngettext). */
9741
9742 for (attrs = TYPE_ATTRIBUTES (type);
9743 attrs;
9744 attrs = TREE_CHAIN (attrs))
9745 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9746 {
5039610b 9747 tree inner_arg;
b34c7881
JT
9748 tree format_num_expr;
9749 int format_num;
9750 int i;
5039610b 9751 call_expr_arg_iterator iter;
b34c7881
JT
9752
9753 /* Extract the argument number, which was previously checked
9754 to be valid. */
9755 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
b34c7881 9756
6b3b8c27 9757 format_num = tree_to_uhwi (format_num_expr);
b34c7881 9758
5039610b
SL
9759 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9760 inner_arg != 0;
9761 inner_arg = next_call_expr_arg (&iter), i++)
b34c7881
JT
9762 if (i == format_num)
9763 {
9764 check_function_arguments_recurse (callback, ctx,
5039610b 9765 inner_arg, param_num);
b34c7881
JT
9766 found_format_arg = true;
9767 break;
9768 }
9769 }
9770
9771 /* If we found a format_arg attribute and did a recursive check,
9772 we are done with checking this argument. Otherwise, we continue
9773 and this will be considered a non-literal. */
9774 if (found_format_arg)
9775 return;
9776 }
9777
9778 if (TREE_CODE (param) == COND_EXPR)
9779 {
7f26f7df
JM
9780 /* Simplify to avoid warning for an impossible case. */
9781 param = fold_for_warn (param);
9782 if (TREE_CODE (param) == COND_EXPR)
9783 {
9784 /* Check both halves of the conditional expression. */
9785 check_function_arguments_recurse (callback, ctx,
9786 TREE_OPERAND (param, 1),
9787 param_num);
9788 check_function_arguments_recurse (callback, ctx,
9789 TREE_OPERAND (param, 2),
9790 param_num);
9791 return;
9792 }
b34c7881
JT
9793 }
9794
9795 (*callback) (ctx, param, param_num);
9796}
e2500fed 9797
a98c2819
MLI
9798/* Checks for a builtin function FNDECL that the number of arguments
9799 NARGS against the required number REQUIRED and issues an error if
9800 there is a mismatch. Returns true if the number of arguments is
9801 correct, otherwise false. */
83322951
RG
9802
9803static bool
a98c2819 9804builtin_function_validate_nargs (tree fndecl, int nargs, int required)
83322951
RG
9805{
9806 if (nargs < required)
9807 {
a98c2819
MLI
9808 error_at (input_location,
9809 "not enough arguments to function %qE", fndecl);
83322951
RG
9810 return false;
9811 }
9812 else if (nargs > required)
9813 {
a98c2819
MLI
9814 error_at (input_location,
9815 "too many arguments to function %qE", fndecl);
83322951
RG
9816 return false;
9817 }
9818 return true;
9819}
9820
9821/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9822 Returns false if there was an error, otherwise true. */
9823
9824bool
9825check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9826{
9827 if (!DECL_BUILT_IN (fndecl)
9828 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9829 return true;
9830
9831 switch (DECL_FUNCTION_CODE (fndecl))
9832 {
35886f0b
MS
9833 case BUILT_IN_ALLOCA_WITH_ALIGN:
9834 {
9835 /* Get the requested alignment (in bits) if it's a constant
9836 integer expression. */
9837 unsigned HOST_WIDE_INT align
9838 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
9839
9840 /* Determine if the requested alignment is a power of 2. */
9841 if ((align & (align - 1)))
9842 align = 0;
9843
9844 /* The maximum alignment in bits corresponding to the same
9845 maximum in bytes enforced in check_user_alignment(). */
9846 unsigned maxalign = (UINT_MAX >> 1) + 1;
9847
9848 /* Reject invalid alignments. */
9849 if (align < BITS_PER_UNIT || maxalign < align)
9850 {
9851 error_at (EXPR_LOC_OR_LOC (args[1], input_location),
9852 "second argument to function %qE must be a constant "
9853 "integer power of 2 between %qi and %qu bits",
9854 fndecl, BITS_PER_UNIT, maxalign);
9855 return false;
9856 }
9857 return true;
9858 }
9859
83322951 9860 case BUILT_IN_CONSTANT_P:
a98c2819 9861 return builtin_function_validate_nargs (fndecl, nargs, 1);
83322951
RG
9862
9863 case BUILT_IN_ISFINITE:
9864 case BUILT_IN_ISINF:
05f41289 9865 case BUILT_IN_ISINF_SIGN:
83322951
RG
9866 case BUILT_IN_ISNAN:
9867 case BUILT_IN_ISNORMAL:
61717a45 9868 case BUILT_IN_SIGNBIT:
a98c2819 9869 if (builtin_function_validate_nargs (fndecl, nargs, 1))
83322951
RG
9870 {
9871 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9872 {
9873 error ("non-floating-point argument in call to "
9874 "function %qE", fndecl);
9875 return false;
9876 }
9877 return true;
9878 }
9879 return false;
9880
9881 case BUILT_IN_ISGREATER:
9882 case BUILT_IN_ISGREATEREQUAL:
9883 case BUILT_IN_ISLESS:
9884 case BUILT_IN_ISLESSEQUAL:
9885 case BUILT_IN_ISLESSGREATER:
9886 case BUILT_IN_ISUNORDERED:
a98c2819 9887 if (builtin_function_validate_nargs (fndecl, nargs, 2))
83322951
RG
9888 {
9889 enum tree_code code0, code1;
9890 code0 = TREE_CODE (TREE_TYPE (args[0]));
9891 code1 = TREE_CODE (TREE_TYPE (args[1]));
9892 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9893 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9894 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9895 {
9896 error ("non-floating-point arguments in call to "
9897 "function %qE", fndecl);
9898 return false;
9899 }
9900 return true;
9901 }
9902 return false;
9903
3bf5906b 9904 case BUILT_IN_FPCLASSIFY:
a98c2819 9905 if (builtin_function_validate_nargs (fndecl, nargs, 6))
3bf5906b
KG
9906 {
9907 unsigned i;
b8698a0f 9908
3bf5906b
KG
9909 for (i=0; i<5; i++)
9910 if (TREE_CODE (args[i]) != INTEGER_CST)
9911 {
9912 error ("non-const integer argument %u in call to function %qE",
9913 i+1, fndecl);
9914 return false;
9915 }
9916
9917 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9918 {
9919 error ("non-floating-point argument in call to function %qE",
9920 fndecl);
9921 return false;
9922 }
9923 return true;
9924 }
9925 return false;
9926
45d439ac
JJ
9927 case BUILT_IN_ASSUME_ALIGNED:
9928 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9929 {
9930 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9931 {
9932 error ("non-integer argument 3 in call to function %qE", fndecl);
9933 return false;
9934 }
9935 return true;
9936 }
9937 return false;
9938
1304953e
JJ
9939 case BUILT_IN_ADD_OVERFLOW:
9940 case BUILT_IN_SUB_OVERFLOW:
9941 case BUILT_IN_MUL_OVERFLOW:
9942 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9943 {
9944 unsigned i;
9945 for (i = 0; i < 2; i++)
9946 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9947 {
9948 error ("argument %u in call to function %qE does not have "
9949 "integral type", i + 1, fndecl);
9950 return false;
9951 }
9952 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9953 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9954 {
9955 error ("argument 3 in call to function %qE does not have "
9956 "pointer to integer type", fndecl);
9957 return false;
9958 }
9959 return true;
9960 }
9961 return false;
9962
83322951
RG
9963 default:
9964 return true;
9965 }
9966}
9967
d07605f5
AP
9968/* Function to help qsort sort FIELD_DECLs by name order. */
9969
9970int
9971field_decl_cmp (const void *x_p, const void *y_p)
9972{
28dab132
BI
9973 const tree *const x = (const tree *const) x_p;
9974 const tree *const y = (const tree *const) y_p;
9975
d07605f5
AP
9976 if (DECL_NAME (*x) == DECL_NAME (*y))
9977 /* A nontype is "greater" than a type. */
9978 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9979 if (DECL_NAME (*x) == NULL_TREE)
9980 return -1;
9981 if (DECL_NAME (*y) == NULL_TREE)
9982 return 1;
9983 if (DECL_NAME (*x) < DECL_NAME (*y))
9984 return -1;
9985 return 1;
9986}
9987
9988static struct {
9989 gt_pointer_operator new_value;
9990 void *cookie;
9991} resort_data;
9992
9993/* This routine compares two fields like field_decl_cmp but using the
9994pointer operator in resort_data. */
9995
9996static int
9997resort_field_decl_cmp (const void *x_p, const void *y_p)
9998{
28dab132
BI
9999 const tree *const x = (const tree *const) x_p;
10000 const tree *const y = (const tree *const) y_p;
d07605f5
AP
10001
10002 if (DECL_NAME (*x) == DECL_NAME (*y))
10003 /* A nontype is "greater" than a type. */
10004 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10005 if (DECL_NAME (*x) == NULL_TREE)
10006 return -1;
10007 if (DECL_NAME (*y) == NULL_TREE)
10008 return 1;
10009 {
10010 tree d1 = DECL_NAME (*x);
10011 tree d2 = DECL_NAME (*y);
10012 resort_data.new_value (&d1, resort_data.cookie);
10013 resort_data.new_value (&d2, resort_data.cookie);
10014 if (d1 < d2)
10015 return -1;
10016 }
10017 return 1;
10018}
10019
10020/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10021
10022void
10023resort_sorted_fields (void *obj,
e18476eb 10024 void * ARG_UNUSED (orig_obj),
6de9cd9a
DN
10025 gt_pointer_operator new_value,
10026 void *cookie)
d07605f5 10027{
e18476eb 10028 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
d07605f5
AP
10029 resort_data.new_value = new_value;
10030 resort_data.cookie = cookie;
10031 qsort (&sf->elts[0], sf->len, sizeof (tree),
6de9cd9a 10032 resort_field_decl_cmp);
d07605f5
AP
10033}
10034
0a3ee0fd
GDR
10035/* Subroutine of c_parse_error.
10036 Return the result of concatenating LHS and RHS. RHS is really
10037 a string literal, its first character is indicated by RHS_START and
3292fb42 10038 RHS_SIZE is its length (including the terminating NUL character).
0a3ee0fd
GDR
10039
10040 The caller is responsible for deleting the returned pointer. */
10041
10042static char *
10043catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10044{
10045 const int lhs_size = strlen (lhs);
10046 char *result = XNEWVEC (char, lhs_size + rhs_size);
10047 strncpy (result, lhs, lhs_size);
10048 strncpy (result + lhs_size, rhs_start, rhs_size);
10049 return result;
10050}
10051
4b794eaf 10052/* Issue the error given by GMSGID, indicating that it occurred before
4bb8ca28
MM
10053 TOKEN, which had the associated VALUE. */
10054
10055void
b8698a0f 10056c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
cfc93532 10057 tree value, unsigned char token_flags)
4bb8ca28 10058{
0a3ee0fd
GDR
10059#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10060
10061 char *message = NULL;
4bb8ca28 10062
cfc93532 10063 if (token_type == CPP_EOF)
4b794eaf 10064 message = catenate_messages (gmsgid, " at end of input");
b8698a0f
L
10065 else if (token_type == CPP_CHAR
10066 || token_type == CPP_WCHAR
cfc93532 10067 || token_type == CPP_CHAR16
fe95b036
ESR
10068 || token_type == CPP_CHAR32
10069 || token_type == CPP_UTF8CHAR)
4bb8ca28
MM
10070 {
10071 unsigned int val = TREE_INT_CST_LOW (value);
b6baa67d
KVH
10072 const char *prefix;
10073
cfc93532 10074 switch (token_type)
b6baa67d
KVH
10075 {
10076 default:
10077 prefix = "";
10078 break;
10079 case CPP_WCHAR:
10080 prefix = "L";
10081 break;
10082 case CPP_CHAR16:
10083 prefix = "u";
10084 break;
10085 case CPP_CHAR32:
10086 prefix = "U";
10087 break;
fe95b036
ESR
10088 case CPP_UTF8CHAR:
10089 prefix = "u8";
10090 break;
b6baa67d
KVH
10091 }
10092
4bb8ca28 10093 if (val <= UCHAR_MAX && ISGRAPH (val))
c22cacf3 10094 message = catenate_messages (gmsgid, " before %s'%c'");
4bb8ca28 10095 else
c22cacf3 10096 message = catenate_messages (gmsgid, " before %s'\\x%x'");
0a3ee0fd 10097
b6baa67d 10098 error (message, prefix, val);
0a3ee0fd
GDR
10099 free (message);
10100 message = NULL;
4bb8ca28 10101 }
65e5a578
ESR
10102 else if (token_type == CPP_CHAR_USERDEF
10103 || token_type == CPP_WCHAR_USERDEF
10104 || token_type == CPP_CHAR16_USERDEF
fe95b036
ESR
10105 || token_type == CPP_CHAR32_USERDEF
10106 || token_type == CPP_UTF8CHAR_USERDEF)
65e5a578
ESR
10107 message = catenate_messages (gmsgid,
10108 " before user-defined character literal");
10109 else if (token_type == CPP_STRING_USERDEF
10110 || token_type == CPP_WSTRING_USERDEF
10111 || token_type == CPP_STRING16_USERDEF
10112 || token_type == CPP_STRING32_USERDEF
10113 || token_type == CPP_UTF8STRING_USERDEF)
10114 message = catenate_messages (gmsgid, " before user-defined string literal");
b8698a0f
L
10115 else if (token_type == CPP_STRING
10116 || token_type == CPP_WSTRING
cfc93532 10117 || token_type == CPP_STRING16
2c6e3f55
JJ
10118 || token_type == CPP_STRING32
10119 || token_type == CPP_UTF8STRING)
4b794eaf 10120 message = catenate_messages (gmsgid, " before string constant");
cfc93532 10121 else if (token_type == CPP_NUMBER)
4b794eaf 10122 message = catenate_messages (gmsgid, " before numeric constant");
cfc93532 10123 else if (token_type == CPP_NAME)
0a3ee0fd 10124 {
4b794eaf 10125 message = catenate_messages (gmsgid, " before %qE");
c51a1ba9 10126 error (message, value);
0a3ee0fd
GDR
10127 free (message);
10128 message = NULL;
10129 }
cfc93532 10130 else if (token_type == CPP_PRAGMA)
bc4071dd 10131 message = catenate_messages (gmsgid, " before %<#pragma%>");
cfc93532 10132 else if (token_type == CPP_PRAGMA_EOL)
bc4071dd 10133 message = catenate_messages (gmsgid, " before end of line");
34429675
JM
10134 else if (token_type == CPP_DECLTYPE)
10135 message = catenate_messages (gmsgid, " before %<decltype%>");
cfc93532 10136 else if (token_type < N_TTYPES)
0a3ee0fd 10137 {
4b794eaf 10138 message = catenate_messages (gmsgid, " before %qs token");
cfc93532 10139 error (message, cpp_type2name (token_type, token_flags));
0a3ee0fd
GDR
10140 free (message);
10141 message = NULL;
10142 }
4bb8ca28 10143 else
4b794eaf 10144 error (gmsgid);
0a3ee0fd
GDR
10145
10146 if (message)
10147 {
10148 error (message);
10149 free (message);
10150 }
c22cacf3 10151#undef catenate_messages
4bb8ca28
MM
10152}
10153
87cf0651
SB
10154/* Return the gcc option code associated with the reason for a cpp
10155 message, or 0 if none. */
10156
10157static int
10158c_option_controlling_cpp_error (int reason)
10159{
b559c810 10160 const struct cpp_reason_option_codes_t *entry;
87cf0651 10161
b559c810 10162 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
87cf0651
SB
10163 {
10164 if (entry->reason == reason)
10165 return entry->option_code;
10166 }
10167 return 0;
10168}
10169
148e4216 10170/* Callback from cpp_error for PFILE to print diagnostics from the
87cf0651
SB
10171 preprocessor. The diagnostic is of type LEVEL, with REASON set
10172 to the reason code if LEVEL is represents a warning, at location
8a645150
DM
10173 RICHLOC unless this is after lexing and the compiler's location
10174 should be used instead; MSG is the translated message and AP
148e4216
JM
10175 the arguments. Returns true if a diagnostic was emitted, false
10176 otherwise. */
10177
10178bool
87cf0651 10179c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
8a645150 10180 rich_location *richloc,
148e4216
JM
10181 const char *msg, va_list *ap)
10182{
10183 diagnostic_info diagnostic;
10184 diagnostic_t dlevel;
e3339d0f 10185 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
148e4216
JM
10186 bool ret;
10187
10188 switch (level)
10189 {
10190 case CPP_DL_WARNING_SYSHDR:
10191 if (flag_no_output)
10192 return false;
e3339d0f 10193 global_dc->dc_warn_system_headers = 1;
148e4216
JM
10194 /* Fall through. */
10195 case CPP_DL_WARNING:
10196 if (flag_no_output)
10197 return false;
10198 dlevel = DK_WARNING;
10199 break;
10200 case CPP_DL_PEDWARN:
10201 if (flag_no_output && !flag_pedantic_errors)
10202 return false;
10203 dlevel = DK_PEDWARN;
10204 break;
10205 case CPP_DL_ERROR:
10206 dlevel = DK_ERROR;
10207 break;
10208 case CPP_DL_ICE:
10209 dlevel = DK_ICE;
10210 break;
10211 case CPP_DL_NOTE:
10212 dlevel = DK_NOTE;
10213 break;
47580d22
JM
10214 case CPP_DL_FATAL:
10215 dlevel = DK_FATAL;
10216 break;
148e4216
JM
10217 default:
10218 gcc_unreachable ();
10219 }
10220 if (done_lexing)
f79520bb 10221 richloc->set_range (line_table, 0, input_location, true);
148e4216 10222 diagnostic_set_info_translated (&diagnostic, msg, ap,
8a645150 10223 richloc, dlevel);
87cf0651
SB
10224 diagnostic_override_option_index (&diagnostic,
10225 c_option_controlling_cpp_error (reason));
148e4216
JM
10226 ret = report_diagnostic (&diagnostic);
10227 if (level == CPP_DL_WARNING_SYSHDR)
e3339d0f 10228 global_dc->dc_warn_system_headers = save_warn_system_headers;
148e4216
JM
10229 return ret;
10230}
10231
c5ff069d
ZW
10232/* Convert a character from the host to the target execution character
10233 set. cpplib handles this, mostly. */
10234
10235HOST_WIDE_INT
10236c_common_to_target_charset (HOST_WIDE_INT c)
10237{
10238 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10239 zero-extended under -fno-signed-char. cpplib insists that characters
10240 and character constants are always unsigned. Hence we must convert
10241 back and forth. */
10242 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10243
10244 uc = cpp_host_to_exec_charset (parse_in, uc);
10245
10246 if (flag_signed_char)
10247 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10248 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10249 else
10250 return uc;
10251}
10252
cf9e9959
EB
10253/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10254 references with an INDIRECT_REF of a constant at the bottom; much like the
10255 traditional rendering of offsetof as a macro. Return the folded result. */
ee8a6a3e 10256
cf9e9959 10257tree
c85158de 10258fold_offsetof_1 (tree expr, enum tree_code ctx)
ee8a6a3e 10259{
ee8a6a3e 10260 tree base, off, t;
c85158de
MS
10261 tree_code code = TREE_CODE (expr);
10262 switch (code)
ee8a6a3e
RH
10263 {
10264 case ERROR_MARK:
10265 return expr;
10266
545b7d8c
VR
10267 case VAR_DECL:
10268 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10269 return error_mark_node;
10270
6d4d7b0e 10271 case CALL_EXPR:
8d5f60ac 10272 case TARGET_EXPR:
6d4d7b0e
PB
10273 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10274 return error_mark_node;
10275
6d4d7b0e
PB
10276 case NOP_EXPR:
10277 case INDIRECT_REF:
cf9e9959 10278 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
61c3c490
DS
10279 {
10280 error ("cannot apply %<offsetof%> to a non constant address");
10281 return error_mark_node;
10282 }
cf9e9959 10283 return TREE_OPERAND (expr, 0);
6d4d7b0e 10284
ee8a6a3e 10285 case COMPONENT_REF:
c85158de 10286 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
ee8a6a3e
RH
10287 if (base == error_mark_node)
10288 return base;
10289
10290 t = TREE_OPERAND (expr, 1);
10291 if (DECL_C_BIT_FIELD (t))
10292 {
10293 error ("attempt to take address of bit-field structure "
c51a1ba9 10294 "member %qD", t);
ee8a6a3e
RH
10295 return error_mark_node;
10296 }
db3927fb 10297 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
386b1f1f 10298 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
db3927fb 10299 / BITS_PER_UNIT));
ee8a6a3e
RH
10300 break;
10301
10302 case ARRAY_REF:
c85158de 10303 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
ee8a6a3e
RH
10304 if (base == error_mark_node)
10305 return base;
10306
10307 t = TREE_OPERAND (expr, 1);
61c3c490
DS
10308
10309 /* Check if the offset goes beyond the upper bound of the array. */
cf9e9959 10310 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
d32599a6
JJ
10311 {
10312 tree upbound = array_ref_up_bound (expr);
10313 if (upbound != NULL_TREE
10314 && TREE_CODE (upbound) == INTEGER_CST
10315 && !tree_int_cst_equal (upbound,
10316 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10317 {
c85158de
MS
10318 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10319 upbound = size_binop (PLUS_EXPR, upbound,
10320 build_int_cst (TREE_TYPE (upbound), 1));
d32599a6
JJ
10321 if (tree_int_cst_lt (upbound, t))
10322 {
10323 tree v;
10324
10325 for (v = TREE_OPERAND (expr, 0);
10326 TREE_CODE (v) == COMPONENT_REF;
10327 v = TREE_OPERAND (v, 0))
10328 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10329 == RECORD_TYPE)
10330 {
910ad8de
NF
10331 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10332 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
d32599a6
JJ
10333 if (TREE_CODE (fld_chain) == FIELD_DECL)
10334 break;
10335
10336 if (fld_chain)
10337 break;
10338 }
10339 /* Don't warn if the array might be considered a poor
10340 man's flexible array member with a very permissive
10341 definition thereof. */
10342 if (TREE_CODE (v) == ARRAY_REF
10343 || TREE_CODE (v) == COMPONENT_REF)
10344 warning (OPT_Warray_bounds,
10345 "index %E denotes an offset "
10346 "greater than size of %qT",
10347 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10348 }
10349 }
10350 }
cf9e9959
EB
10351
10352 t = convert (sizetype, t);
10353 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
ee8a6a3e
RH
10354 break;
10355
1916c916
VR
10356 case COMPOUND_EXPR:
10357 /* Handle static members of volatile structs. */
10358 t = TREE_OPERAND (expr, 1);
0ae9bd27 10359 gcc_assert (VAR_P (t));
cf9e9959 10360 return fold_offsetof_1 (t);
1916c916 10361
ee8a6a3e 10362 default:
366de0ce 10363 gcc_unreachable ();
ee8a6a3e
RH
10364 }
10365
cf9e9959 10366 return fold_build_pointer_plus (base, off);
ee8a6a3e
RH
10367}
10368
cf9e9959
EB
10369/* Likewise, but convert it to the return type of offsetof. */
10370
ee8a6a3e 10371tree
cf9e9959 10372fold_offsetof (tree expr)
ee8a6a3e 10373{
cf9e9959 10374 return convert (size_type_node, fold_offsetof_1 (expr));
ee8a6a3e
RH
10375}
10376
d166d4c3
AK
10377/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10378 expression, because B will always be true. */
10379
10380void
10381warn_for_omitted_condop (location_t location, tree cond)
10382{
10383 if (truth_value_p (TREE_CODE (cond)))
10384 warning_at (location, OPT_Wparentheses,
10385 "the omitted middle operand in ?: will always be %<true%>, "
10386 "suggest explicit middle operand");
10387}
10388
4816c593
NF
10389/* Give an error for storing into ARG, which is 'const'. USE indicates
10390 how ARG was being used. */
10391
10392void
c02065fc 10393readonly_error (location_t loc, tree arg, enum lvalue_use use)
4816c593
NF
10394{
10395 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10396 || use == lv_asm);
10397 /* Using this macro rather than (for example) arrays of messages
10398 ensures that all the format strings are checked at compile
10399 time. */
10400#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10401 : (use == lv_increment ? (I) \
10402 : (use == lv_decrement ? (D) : (AS))))
10403 if (TREE_CODE (arg) == COMPONENT_REF)
10404 {
10405 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
c02065fc
AH
10406 error_at (loc, READONLY_MSG (G_("assignment of member "
10407 "%qD in read-only object"),
10408 G_("increment of member "
10409 "%qD in read-only object"),
10410 G_("decrement of member "
10411 "%qD in read-only object"),
10412 G_("member %qD in read-only object "
10413 "used as %<asm%> output")),
10414 TREE_OPERAND (arg, 1));
4816c593 10415 else
c02065fc
AH
10416 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10417 G_("increment of read-only member %qD"),
10418 G_("decrement of read-only member %qD"),
10419 G_("read-only member %qD used as %<asm%> output")),
10420 TREE_OPERAND (arg, 1));
4816c593 10421 }
0ae9bd27 10422 else if (VAR_P (arg))
c02065fc
AH
10423 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10424 G_("increment of read-only variable %qD"),
10425 G_("decrement of read-only variable %qD"),
10426 G_("read-only variable %qD used as %<asm%> output")),
10427 arg);
4816c593 10428 else if (TREE_CODE (arg) == PARM_DECL)
c02065fc
AH
10429 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10430 G_("increment of read-only parameter %qD"),
10431 G_("decrement of read-only parameter %qD"),
10432 G_("read-only parameter %qD use as %<asm%> output")),
10433 arg);
4816c593
NF
10434 else if (TREE_CODE (arg) == RESULT_DECL)
10435 {
10436 gcc_assert (c_dialect_cxx ());
c02065fc
AH
10437 error_at (loc, READONLY_MSG (G_("assignment of "
10438 "read-only named return value %qD"),
10439 G_("increment of "
10440 "read-only named return value %qD"),
10441 G_("decrement of "
10442 "read-only named return value %qD"),
10443 G_("read-only named return value %qD "
10444 "used as %<asm%>output")),
10445 arg);
4816c593
NF
10446 }
10447 else if (TREE_CODE (arg) == FUNCTION_DECL)
c02065fc
AH
10448 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10449 G_("increment of function %qD"),
10450 G_("decrement of function %qD"),
10451 G_("function %qD used as %<asm%> output")),
10452 arg);
4816c593 10453 else
c02065fc
AH
10454 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10455 G_("increment of read-only location %qE"),
10456 G_("decrement of read-only location %qE"),
10457 G_("read-only location %qE used as %<asm%> output")),
10458 arg);
4816c593
NF
10459}
10460
37dc0d8d 10461/* Print an error message for an invalid lvalue. USE says
7bd11157
TT
10462 how the lvalue is being used and so selects the error message. LOC
10463 is the location for the error. */
5ae9ba3e 10464
37dc0d8d 10465void
7bd11157 10466lvalue_error (location_t loc, enum lvalue_use use)
5ae9ba3e 10467{
37dc0d8d 10468 switch (use)
5ae9ba3e 10469 {
37dc0d8d 10470 case lv_assign:
7bd11157 10471 error_at (loc, "lvalue required as left operand of assignment");
37dc0d8d
JM
10472 break;
10473 case lv_increment:
7bd11157 10474 error_at (loc, "lvalue required as increment operand");
37dc0d8d
JM
10475 break;
10476 case lv_decrement:
7bd11157 10477 error_at (loc, "lvalue required as decrement operand");
37dc0d8d
JM
10478 break;
10479 case lv_addressof:
7bd11157 10480 error_at (loc, "lvalue required as unary %<&%> operand");
37dc0d8d
JM
10481 break;
10482 case lv_asm:
7bd11157 10483 error_at (loc, "lvalue required in asm statement");
37dc0d8d
JM
10484 break;
10485 default:
10486 gcc_unreachable ();
5ae9ba3e 10487 }
5ae9ba3e 10488}
7a6daeb0
NF
10489
10490/* Print an error message for an invalid indirection of type TYPE.
10491 ERRSTRING is the name of the operator for the indirection. */
10492
10493void
10494invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10495{
10496 switch (errstring)
10497 {
10498 case RO_NULL:
10499 gcc_assert (c_dialect_cxx ());
10500 error_at (loc, "invalid type argument (have %qT)", type);
10501 break;
10502 case RO_ARRAY_INDEXING:
10503 error_at (loc,
10504 "invalid type argument of array indexing (have %qT)",
10505 type);
10506 break;
10507 case RO_UNARY_STAR:
10508 error_at (loc,
10509 "invalid type argument of unary %<*%> (have %qT)",
10510 type);
10511 break;
10512 case RO_ARROW:
10513 error_at (loc,
10514 "invalid type argument of %<->%> (have %qT)",
10515 type);
10516 break;
55a7f02f
MP
10517 case RO_ARROW_STAR:
10518 error_at (loc,
10519 "invalid type argument of %<->*%> (have %qT)",
10520 type);
10521 break;
7a6daeb0
NF
10522 case RO_IMPLICIT_CONVERSION:
10523 error_at (loc,
10524 "invalid type argument of implicit conversion (have %qT)",
10525 type);
10526 break;
10527 default:
10528 gcc_unreachable ();
10529 }
10530}
aab038d5
RH
10531\f
10532/* *PTYPE is an incomplete array. Complete it with a domain based on
10533 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10534 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10535 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10536
10537int
10538complete_array_type (tree *ptype, tree initial_value, bool do_default)
10539{
10540 tree maxindex, type, main_type, elt, unqual_elt;
10541 int failure = 0, quals;
06d40de8 10542 hashval_t hashcode = 0;
40d3d530 10543 bool overflow_p = false;
aab038d5
RH
10544
10545 maxindex = size_zero_node;
10546 if (initial_value)
10547 {
10548 if (TREE_CODE (initial_value) == STRING_CST)
10549 {
10550 int eltsize
10551 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10552 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10553 }
10554 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10555 {
9771b263 10556 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
aab038d5 10557
9771b263 10558 if (vec_safe_is_empty (v))
aab038d5
RH
10559 {
10560 if (pedantic)
10561 failure = 3;
830c740f 10562 maxindex = ssize_int (-1);
aab038d5
RH
10563 }
10564 else
10565 {
10566 tree curindex;
4038c495
GB
10567 unsigned HOST_WIDE_INT cnt;
10568 constructor_elt *ce;
cff7525f 10569 bool fold_p = false;
aab038d5 10570
9771b263 10571 if ((*v)[0].index)
40d3d530
JR
10572 maxindex = (*v)[0].index, fold_p = true;
10573
aab038d5
RH
10574 curindex = maxindex;
10575
9771b263 10576 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
aab038d5 10577 {
cff7525f 10578 bool curfold_p = false;
4038c495 10579 if (ce->index)
cff7525f 10580 curindex = ce->index, curfold_p = true;
aab038d5 10581 else
cff7525f
JH
10582 {
10583 if (fold_p)
40d3d530
JR
10584 {
10585 /* Since we treat size types now as ordinary
10586 unsigned types, we need an explicit overflow
10587 check. */
10588 tree orig = curindex;
10589 curindex = fold_convert (sizetype, curindex);
10590 overflow_p |= tree_int_cst_lt (curindex, orig);
10591 }
db3927fb
AH
10592 curindex = size_binop (PLUS_EXPR, curindex,
10593 size_one_node);
cff7525f 10594 }
aab038d5 10595 if (tree_int_cst_lt (maxindex, curindex))
cff7525f 10596 maxindex = curindex, fold_p = curfold_p;
aab038d5 10597 }
40d3d530
JR
10598 if (fold_p)
10599 {
10600 tree orig = maxindex;
10601 maxindex = fold_convert (sizetype, maxindex);
10602 overflow_p |= tree_int_cst_lt (maxindex, orig);
10603 }
aab038d5
RH
10604 }
10605 }
10606 else
10607 {
10608 /* Make an error message unless that happened already. */
10609 if (initial_value != error_mark_node)
10610 failure = 1;
10611 }
10612 }
10613 else
10614 {
10615 failure = 2;
10616 if (!do_default)
10617 return failure;
10618 }
10619
10620 type = *ptype;
10621 elt = TREE_TYPE (type);
10622 quals = TYPE_QUALS (strip_array_types (elt));
10623 if (quals == 0)
10624 unqual_elt = elt;
10625 else
36c5e70a 10626 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
aab038d5
RH
10627
10628 /* Using build_distinct_type_copy and modifying things afterward instead
10629 of using build_array_type to create a new type preserves all of the
10630 TYPE_LANG_FLAG_? bits that the front end may have set. */
10631 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10632 TREE_TYPE (main_type) = unqual_elt;
e6313a78
RG
10633 TYPE_DOMAIN (main_type)
10634 = build_range_type (TREE_TYPE (maxindex),
10635 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
aab038d5
RH
10636 layout_type (main_type);
10637
06d40de8
DG
10638 /* Make sure we have the canonical MAIN_TYPE. */
10639 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
b8698a0f 10640 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
06d40de8
DG
10641 hashcode);
10642 main_type = type_hash_canon (hashcode, main_type);
10643
9ae165a0
DG
10644 /* Fix the canonical type. */
10645 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10646 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10647 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10648 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10649 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10650 != TYPE_DOMAIN (main_type)))
b8698a0f 10651 TYPE_CANONICAL (main_type)
9ae165a0
DG
10652 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10653 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10654 else
10655 TYPE_CANONICAL (main_type) = main_type;
10656
aab038d5
RH
10657 if (quals == 0)
10658 type = main_type;
10659 else
10660 type = c_build_qualified_type (main_type, quals);
10661
7bfcb402
JM
10662 if (COMPLETE_TYPE_P (type)
10663 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
40d3d530 10664 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7bfcb402
JM
10665 {
10666 error ("size of array is too large");
10667 /* If we proceed with the array type as it is, we'll eventually
386b1f1f 10668 crash in tree_to_[su]hwi(). */
7bfcb402
JM
10669 type = error_mark_node;
10670 }
10671
aab038d5
RH
10672 *ptype = type;
10673 return failure;
10674}
5ae9ba3e 10675
30cd1c5d
AS
10676/* Like c_mark_addressable but don't check register qualifier. */
10677void
10678c_common_mark_addressable_vec (tree t)
10679{
10680 while (handled_component_p (t))
10681 t = TREE_OPERAND (t, 0);
0ae9bd27 10682 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
30cd1c5d
AS
10683 return;
10684 TREE_ADDRESSABLE (t) = 1;
10685}
10686
10687
48ae6c13
RH
10688\f
10689/* Used to help initialize the builtin-types.def table. When a type of
10690 the correct size doesn't exist, use error_mark_node instead of NULL.
10691 The later results in segfaults even when a decl using the type doesn't
10692 get invoked. */
10693
10694tree
10695builtin_type_for_size (int size, bool unsignedp)
10696{
21fa2faf 10697 tree type = c_common_type_for_size (size, unsignedp);
48ae6c13
RH
10698 return type ? type : error_mark_node;
10699}
10700
10701/* A helper function for resolve_overloaded_builtin in resolving the
10702 overloaded __sync_ builtins. Returns a positive power of 2 if the
10703 first operand of PARAMS is a pointer to a supported data type.
7a127fa7
MS
10704 Returns 0 if an error is encountered.
10705 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10706 built-ins. */
48ae6c13
RH
10707
10708static int
7a127fa7 10709sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
48ae6c13 10710{
7a127fa7
MS
10711 /* Type of the argument. */
10712 tree argtype;
10713 /* Type the argument points to. */
48ae6c13
RH
10714 tree type;
10715 int size;
10716
ba6b3795 10717 if (vec_safe_is_empty (params))
48ae6c13
RH
10718 {
10719 error ("too few arguments to function %qE", function);
10720 return 0;
10721 }
10722
7a127fa7 10723 argtype = type = TREE_TYPE ((*params)[0]);
6415bd5d
JM
10724 if (TREE_CODE (type) == ARRAY_TYPE)
10725 {
10726 /* Force array-to-pointer decay for C++. */
10727 gcc_assert (c_dialect_cxx());
10728 (*params)[0] = default_conversion ((*params)[0]);
10729 type = TREE_TYPE ((*params)[0]);
10730 }
48ae6c13
RH
10731 if (TREE_CODE (type) != POINTER_TYPE)
10732 goto incompatible;
10733
10734 type = TREE_TYPE (type);
10735 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10736 goto incompatible;
10737
7a127fa7
MS
10738 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
10739 goto incompatible;
10740
ae7e9ddd 10741 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
a0274e3e 10742 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
48ae6c13
RH
10743 return size;
10744
10745 incompatible:
9f04a53e
MS
10746 /* Issue the diagnostic only if the argument is valid, otherwise
10747 it would be redundant at best and could be misleading. */
10748 if (argtype != error_mark_node)
10749 error ("operand type %qT is incompatible with argument %d of %qE",
10750 argtype, 1, function);
48ae6c13
RH
10751 return 0;
10752}
10753
c22cacf3 10754/* A helper function for resolve_overloaded_builtin. Adds casts to
48ae6c13
RH
10755 PARAMS to make arguments match up with those of FUNCTION. Drops
10756 the variadic arguments at the end. Returns false if some error
10757 was encountered; true on success. */
10758
10759static bool
86951993 10760sync_resolve_params (location_t loc, tree orig_function, tree function,
9771b263 10761 vec<tree, va_gc> *params, bool orig_format)
48ae6c13 10762{
e19a18d4 10763 function_args_iterator iter;
48ae6c13 10764 tree ptype;
bbbbb16a 10765 unsigned int parmnum;
48ae6c13 10766
e19a18d4 10767 function_args_iter_init (&iter, TREE_TYPE (function));
48ae6c13
RH
10768 /* We've declared the implementation functions to use "volatile void *"
10769 as the pointer parameter, so we shouldn't get any complaints from the
10770 call to check_function_arguments what ever type the user used. */
e19a18d4 10771 function_args_iter_next (&iter);
9771b263 10772 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
267bac10 10773 ptype = TYPE_MAIN_VARIANT (ptype);
48ae6c13
RH
10774
10775 /* For the rest of the values, we need to cast these to FTYPE, so that we
10776 don't get warnings for passing pointer types, etc. */
bbbbb16a 10777 parmnum = 0;
e19a18d4 10778 while (1)
48ae6c13 10779 {
e19a18d4
NF
10780 tree val, arg_type;
10781
10782 arg_type = function_args_iter_cond (&iter);
10783 /* XXX void_type_node belies the abstraction. */
10784 if (arg_type == void_type_node)
10785 break;
48ae6c13 10786
bbbbb16a 10787 ++parmnum;
9771b263 10788 if (params->length () <= parmnum)
48ae6c13 10789 {
86951993 10790 error_at (loc, "too few arguments to function %qE", orig_function);
48ae6c13
RH
10791 return false;
10792 }
10793
e3793c6f
JJ
10794 /* Only convert parameters if arg_type is unsigned integer type with
10795 new format sync routines, i.e. don't attempt to convert pointer
10796 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10797 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10798 kinds). */
10799 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
86951993
AM
10800 {
10801 /* Ideally for the first conversion we'd use convert_for_assignment
10802 so that we get warnings for anything that doesn't match the pointer
10803 type. This isn't portable across the C and C++ front ends atm. */
9771b263 10804 val = (*params)[parmnum];
86951993
AM
10805 val = convert (ptype, val);
10806 val = convert (arg_type, val);
9771b263 10807 (*params)[parmnum] = val;
86951993 10808 }
48ae6c13 10809
e19a18d4 10810 function_args_iter_next (&iter);
48ae6c13
RH
10811 }
10812
86951993 10813 /* __atomic routines are not variadic. */
9771b263 10814 if (!orig_format && params->length () != parmnum + 1)
86951993
AM
10815 {
10816 error_at (loc, "too many arguments to function %qE", orig_function);
10817 return false;
10818 }
10819
48ae6c13
RH
10820 /* The definition of these primitives is variadic, with the remaining
10821 being "an optional list of variables protected by the memory barrier".
10822 No clue what that's supposed to mean, precisely, but we consider all
10823 call-clobbered variables to be protected so we're safe. */
9771b263 10824 params->truncate (parmnum + 1);
48ae6c13
RH
10825
10826 return true;
10827}
10828
c22cacf3 10829/* A helper function for resolve_overloaded_builtin. Adds a cast to
48ae6c13
RH
10830 RESULT to make it match the type of the first pointer argument in
10831 PARAMS. */
10832
10833static tree
86951993 10834sync_resolve_return (tree first_param, tree result, bool orig_format)
48ae6c13 10835{
bbbbb16a 10836 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
86951993 10837 tree rtype = TREE_TYPE (result);
99db1ef0 10838 ptype = TYPE_MAIN_VARIANT (ptype);
86951993
AM
10839
10840 /* New format doesn't require casting unless the types are the same size. */
10841 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10842 return convert (ptype, result);
10843 else
10844 return result;
10845}
10846
10847/* This function verifies the PARAMS to generic atomic FUNCTION.
10848 It returns the size if all the parameters are the same size, otherwise
10849 0 is returned if the parameters are invalid. */
10850
10851static int
9771b263
DN
10852get_atomic_generic_size (location_t loc, tree function,
10853 vec<tree, va_gc> *params)
86951993
AM
10854{
10855 unsigned int n_param;
10856 unsigned int n_model;
10857 unsigned int x;
10858 int size_0;
10859 tree type_0;
10860
10861 /* Determine the parameter makeup. */
10862 switch (DECL_FUNCTION_CODE (function))
10863 {
10864 case BUILT_IN_ATOMIC_EXCHANGE:
10865 n_param = 4;
10866 n_model = 1;
10867 break;
10868 case BUILT_IN_ATOMIC_LOAD:
10869 case BUILT_IN_ATOMIC_STORE:
10870 n_param = 3;
10871 n_model = 1;
10872 break;
10873 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10874 n_param = 6;
10875 n_model = 2;
10876 break;
10877 default:
c466c4ff 10878 gcc_unreachable ();
86951993
AM
10879 }
10880
9771b263 10881 if (vec_safe_length (params) != n_param)
86951993
AM
10882 {
10883 error_at (loc, "incorrect number of arguments to function %qE", function);
10884 return 0;
10885 }
10886
10887 /* Get type of first parameter, and determine its size. */
9771b263 10888 type_0 = TREE_TYPE ((*params)[0]);
6415bd5d
JM
10889 if (TREE_CODE (type_0) == ARRAY_TYPE)
10890 {
10891 /* Force array-to-pointer decay for C++. */
10892 gcc_assert (c_dialect_cxx());
10893 (*params)[0] = default_conversion ((*params)[0]);
10894 type_0 = TREE_TYPE ((*params)[0]);
10895 }
c466c4ff
AM
10896 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10897 {
10898 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10899 function);
10900 return 0;
10901 }
10902
10903 /* Types must be compile time constant sizes. */
10904 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
86951993 10905 {
c466c4ff
AM
10906 error_at (loc,
10907 "argument 1 of %qE must be a pointer to a constant size type",
10908 function);
86951993
AM
10909 return 0;
10910 }
c466c4ff 10911
ae7e9ddd 10912 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
86951993 10913
c466c4ff
AM
10914 /* Zero size objects are not allowed. */
10915 if (size_0 == 0)
10916 {
10917 error_at (loc,
10918 "argument 1 of %qE must be a pointer to a nonzero size object",
10919 function);
10920 return 0;
10921 }
10922
86951993
AM
10923 /* Check each other parameter is a pointer and the same size. */
10924 for (x = 0; x < n_param - n_model; x++)
10925 {
10926 int size;
9771b263 10927 tree type = TREE_TYPE ((*params)[x]);
688010ba 10928 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
86951993
AM
10929 if (n_param == 6 && x == 3)
10930 continue;
10931 if (!POINTER_TYPE_P (type))
10932 {
10933 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10934 function);
10935 return 0;
10936 }
7b56b2f8
MP
10937 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10938 size = type_size ? tree_to_uhwi (type_size) : 0;
86951993
AM
10939 if (size != size_0)
10940 {
10941 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10942 function);
10943 return 0;
10944 }
10945 }
10946
10947 /* Check memory model parameters for validity. */
10948 for (x = n_param - n_model ; x < n_param; x++)
10949 {
9771b263 10950 tree p = (*params)[x];
86951993
AM
10951 if (TREE_CODE (p) == INTEGER_CST)
10952 {
ae7e9ddd 10953 int i = tree_to_uhwi (p);
46b35980 10954 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
86951993
AM
10955 {
10956 warning_at (loc, OPT_Winvalid_memory_model,
10957 "invalid memory model argument %d of %qE", x + 1,
10958 function);
86951993
AM
10959 }
10960 }
10961 else
10962 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10963 {
10964 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10965 function);
10966 return 0;
10967 }
10968 }
10969
10970 return size_0;
10971}
10972
10973
10974/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10975 at the beginning of the parameter list PARAMS representing the size of the
10976 objects. This is to match the library ABI requirement. LOC is the location
10977 of the function call.
10978 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10979 returned to allow the external call to be constructed. */
10980
10981static tree
10982add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9771b263 10983 vec<tree, va_gc> *params)
86951993
AM
10984{
10985 tree size_node;
10986
10987 /* Insert a SIZE_T parameter as the first param. If there isn't
10988 enough space, allocate a new vector and recursively re-build with that. */
9771b263 10989 if (!params->space (1))
86951993
AM
10990 {
10991 unsigned int z, len;
9771b263 10992 vec<tree, va_gc> *v;
86951993
AM
10993 tree f;
10994
9771b263
DN
10995 len = params->length ();
10996 vec_alloc (v, len + 1);
8edbfaa6 10997 v->quick_push (build_int_cst (size_type_node, n));
86951993 10998 for (z = 0; z < len; z++)
9771b263 10999 v->quick_push ((*params)[z]);
81e5eca8 11000 f = build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 11001 vec_free (v);
86951993
AM
11002 return f;
11003 }
11004
11005 /* Add the size parameter and leave as a function call for processing. */
11006 size_node = build_int_cst (size_type_node, n);
9771b263 11007 params->quick_insert (0, size_node);
86951993
AM
11008 return NULL_TREE;
11009}
11010
11011
6b28e197
JM
11012/* Return whether atomic operations for naturally aligned N-byte
11013 arguments are supported, whether inline or through libatomic. */
11014static bool
11015atomic_size_supported_p (int n)
11016{
11017 switch (n)
11018 {
11019 case 1:
11020 case 2:
11021 case 4:
11022 case 8:
11023 return true;
11024
11025 case 16:
11026 return targetm.scalar_mode_supported_p (TImode);
11027
11028 default:
11029 return false;
11030 }
11031}
11032
86951993
AM
11033/* This will process an __atomic_exchange function call, determine whether it
11034 needs to be mapped to the _N variation, or turned into a library call.
11035 LOC is the location of the builtin call.
11036 FUNCTION is the DECL that has been invoked;
11037 PARAMS is the argument list for the call. The return value is non-null
11038 TRUE is returned if it is translated into the proper format for a call to the
11039 external library, and NEW_RETURN is set the tree for that function.
11040 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11041 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11042static bool
11043resolve_overloaded_atomic_exchange (location_t loc, tree function,
9771b263 11044 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11045{
11046 tree p0, p1, p2, p3;
11047 tree I_type, I_type_ptr;
11048 int n = get_atomic_generic_size (loc, function, params);
11049
c466c4ff
AM
11050 /* Size of 0 is an error condition. */
11051 if (n == 0)
11052 {
11053 *new_return = error_mark_node;
11054 return true;
11055 }
11056
86951993 11057 /* If not a lock-free size, change to the library generic format. */
6b28e197 11058 if (!atomic_size_supported_p (n))
86951993
AM
11059 {
11060 *new_return = add_atomic_size_parameter (n, loc, function, params);
11061 return true;
11062 }
11063
11064 /* Otherwise there is a lockfree match, transform the call from:
11065 void fn(T* mem, T* desired, T* return, model)
11066 into
11067 *return = (T) (fn (In* mem, (In) *desired, model)) */
11068
9771b263
DN
11069 p0 = (*params)[0];
11070 p1 = (*params)[1];
11071 p2 = (*params)[2];
11072 p3 = (*params)[3];
86951993
AM
11073
11074 /* Create pointer to appropriate size. */
11075 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11076 I_type_ptr = build_pointer_type (I_type);
11077
11078 /* Convert object pointer to required type. */
11079 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11080 (*params)[0] = p0;
86951993
AM
11081 /* Convert new value to required type, and dereference it. */
11082 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11083 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11084 (*params)[1] = p1;
86951993
AM
11085
11086 /* Move memory model to the 3rd position, and end param list. */
9771b263
DN
11087 (*params)[2] = p3;
11088 params->truncate (3);
86951993
AM
11089
11090 /* Convert return pointer and dereference it for later assignment. */
11091 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11092
11093 return false;
48ae6c13
RH
11094}
11095
86951993
AM
11096
11097/* This will process an __atomic_compare_exchange function call, determine
11098 whether it needs to be mapped to the _N variation, or turned into a lib call.
11099 LOC is the location of the builtin call.
11100 FUNCTION is the DECL that has been invoked;
11101 PARAMS is the argument list for the call. The return value is non-null
11102 TRUE is returned if it is translated into the proper format for a call to the
11103 external library, and NEW_RETURN is set the tree for that function.
11104 FALSE is returned if processing for the _N variation is required. */
11105
11106static bool
11107resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9771b263 11108 vec<tree, va_gc> *params,
86951993
AM
11109 tree *new_return)
11110{
11111 tree p0, p1, p2;
11112 tree I_type, I_type_ptr;
11113 int n = get_atomic_generic_size (loc, function, params);
11114
c466c4ff
AM
11115 /* Size of 0 is an error condition. */
11116 if (n == 0)
11117 {
11118 *new_return = error_mark_node;
11119 return true;
11120 }
11121
86951993 11122 /* If not a lock-free size, change to the library generic format. */
6b28e197 11123 if (!atomic_size_supported_p (n))
86951993
AM
11124 {
11125 /* The library generic format does not have the weak parameter, so
11126 remove it from the param list. Since a parameter has been removed,
11127 we can be sure that there is room for the SIZE_T parameter, meaning
11128 there will not be a recursive rebuilding of the parameter list, so
11129 there is no danger this will be done twice. */
11130 if (n > 0)
11131 {
9771b263
DN
11132 (*params)[3] = (*params)[4];
11133 (*params)[4] = (*params)[5];
11134 params->truncate (5);
86951993
AM
11135 }
11136 *new_return = add_atomic_size_parameter (n, loc, function, params);
11137 return true;
11138 }
11139
11140 /* Otherwise, there is a match, so the call needs to be transformed from:
11141 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11142 into
11143 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11144
9771b263
DN
11145 p0 = (*params)[0];
11146 p1 = (*params)[1];
11147 p2 = (*params)[2];
86951993
AM
11148
11149 /* Create pointer to appropriate size. */
11150 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11151 I_type_ptr = build_pointer_type (I_type);
11152
11153 /* Convert object pointer to required type. */
11154 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11155 (*params)[0] = p0;
86951993
AM
11156
11157 /* Convert expected pointer to required type. */
11158 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9771b263 11159 (*params)[1] = p1;
86951993
AM
11160
11161 /* Convert desired value to required type, and dereference it. */
11162 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11163 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9771b263 11164 (*params)[2] = p2;
86951993
AM
11165
11166 /* The rest of the parameters are fine. NULL means no special return value
11167 processing.*/
11168 *new_return = NULL;
11169 return false;
11170}
11171
11172
11173/* This will process an __atomic_load function call, determine whether it
11174 needs to be mapped to the _N variation, or turned into a library call.
11175 LOC is the location of the builtin call.
11176 FUNCTION is the DECL that has been invoked;
11177 PARAMS is the argument list for the call. The return value is non-null
11178 TRUE is returned if it is translated into the proper format for a call to the
11179 external library, and NEW_RETURN is set the tree for that function.
11180 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11181 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11182
11183static bool
11184resolve_overloaded_atomic_load (location_t loc, tree function,
9771b263 11185 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11186{
11187 tree p0, p1, p2;
11188 tree I_type, I_type_ptr;
11189 int n = get_atomic_generic_size (loc, function, params);
11190
c466c4ff
AM
11191 /* Size of 0 is an error condition. */
11192 if (n == 0)
11193 {
11194 *new_return = error_mark_node;
11195 return true;
11196 }
11197
86951993 11198 /* If not a lock-free size, change to the library generic format. */
6b28e197 11199 if (!atomic_size_supported_p (n))
86951993
AM
11200 {
11201 *new_return = add_atomic_size_parameter (n, loc, function, params);
11202 return true;
11203 }
11204
11205 /* Otherwise, there is a match, so the call needs to be transformed from:
11206 void fn(T* mem, T* return, model)
11207 into
11208 *return = (T) (fn ((In *) mem, model)) */
11209
9771b263
DN
11210 p0 = (*params)[0];
11211 p1 = (*params)[1];
11212 p2 = (*params)[2];
86951993
AM
11213
11214 /* Create pointer to appropriate size. */
11215 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11216 I_type_ptr = build_pointer_type (I_type);
11217
11218 /* Convert object pointer to required type. */
11219 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11220 (*params)[0] = p0;
86951993
AM
11221
11222 /* Move memory model to the 2nd position, and end param list. */
9771b263
DN
11223 (*params)[1] = p2;
11224 params->truncate (2);
86951993
AM
11225
11226 /* Convert return pointer and dereference it for later assignment. */
11227 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11228
11229 return false;
11230}
11231
11232
11233/* This will process an __atomic_store function call, determine whether it
11234 needs to be mapped to the _N variation, or turned into a library call.
11235 LOC is the location of the builtin call.
11236 FUNCTION is the DECL that has been invoked;
11237 PARAMS is the argument list for the call. The return value is non-null
11238 TRUE is returned if it is translated into the proper format for a call to the
11239 external library, and NEW_RETURN is set the tree for that function.
11240 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11241 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11242
11243static bool
11244resolve_overloaded_atomic_store (location_t loc, tree function,
9771b263 11245 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11246{
11247 tree p0, p1;
11248 tree I_type, I_type_ptr;
11249 int n = get_atomic_generic_size (loc, function, params);
11250
c466c4ff
AM
11251 /* Size of 0 is an error condition. */
11252 if (n == 0)
11253 {
11254 *new_return = error_mark_node;
11255 return true;
11256 }
11257
86951993 11258 /* If not a lock-free size, change to the library generic format. */
6b28e197 11259 if (!atomic_size_supported_p (n))
86951993
AM
11260 {
11261 *new_return = add_atomic_size_parameter (n, loc, function, params);
11262 return true;
11263 }
11264
11265 /* Otherwise, there is a match, so the call needs to be transformed from:
11266 void fn(T* mem, T* value, model)
11267 into
11268 fn ((In *) mem, (In) *value, model) */
11269
9771b263
DN
11270 p0 = (*params)[0];
11271 p1 = (*params)[1];
86951993
AM
11272
11273 /* Create pointer to appropriate size. */
11274 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11275 I_type_ptr = build_pointer_type (I_type);
11276
11277 /* Convert object pointer to required type. */
11278 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11279 (*params)[0] = p0;
86951993
AM
11280
11281 /* Convert new value to required type, and dereference it. */
11282 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11283 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11284 (*params)[1] = p1;
86951993
AM
11285
11286 /* The memory model is in the right spot already. Return is void. */
11287 *new_return = NULL_TREE;
11288
11289 return false;
11290}
11291
11292
48ae6c13
RH
11293/* Some builtin functions are placeholders for other expressions. This
11294 function should be called immediately after parsing the call expression
11295 before surrounding code has committed to the type of the expression.
11296
c2255bc4
AH
11297 LOC is the location of the builtin call.
11298
48ae6c13
RH
11299 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11300 PARAMS is the argument list for the call. The return value is non-null
11301 when expansion is complete, and null if normal processing should
11302 continue. */
11303
11304tree
9771b263
DN
11305resolve_overloaded_builtin (location_t loc, tree function,
11306 vec<tree, va_gc> *params)
48ae6c13
RH
11307{
11308 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7a127fa7
MS
11309
11310 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11311 Those are not valid to call with a pointer to _Bool (or C++ bool)
11312 and so must be rejected. */
11313 bool fetch_op = true;
86951993
AM
11314 bool orig_format = true;
11315 tree new_return = NULL_TREE;
11316
58646b77
PB
11317 switch (DECL_BUILT_IN_CLASS (function))
11318 {
11319 case BUILT_IN_NORMAL:
11320 break;
11321 case BUILT_IN_MD:
11322 if (targetm.resolve_overloaded_builtin)
c2255bc4 11323 return targetm.resolve_overloaded_builtin (loc, function, params);
58646b77 11324 else
c22cacf3 11325 return NULL_TREE;
58646b77
PB
11326 default:
11327 return NULL_TREE;
11328 }
c22cacf3 11329
58646b77 11330 /* Handle BUILT_IN_NORMAL here. */
48ae6c13
RH
11331 switch (orig_code)
11332 {
86951993
AM
11333 case BUILT_IN_ATOMIC_EXCHANGE:
11334 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11335 case BUILT_IN_ATOMIC_LOAD:
11336 case BUILT_IN_ATOMIC_STORE:
11337 {
11338 /* Handle these 4 together so that they can fall through to the next
11339 case if the call is transformed to an _N variant. */
11340 switch (orig_code)
11341 {
11342 case BUILT_IN_ATOMIC_EXCHANGE:
11343 {
11344 if (resolve_overloaded_atomic_exchange (loc, function, params,
11345 &new_return))
11346 return new_return;
11347 /* Change to the _N variant. */
11348 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11349 break;
11350 }
11351
11352 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11353 {
11354 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11355 params,
11356 &new_return))
11357 return new_return;
11358 /* Change to the _N variant. */
11359 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11360 break;
11361 }
11362 case BUILT_IN_ATOMIC_LOAD:
11363 {
11364 if (resolve_overloaded_atomic_load (loc, function, params,
11365 &new_return))
11366 return new_return;
11367 /* Change to the _N variant. */
11368 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11369 break;
11370 }
11371 case BUILT_IN_ATOMIC_STORE:
11372 {
11373 if (resolve_overloaded_atomic_store (loc, function, params,
11374 &new_return))
11375 return new_return;
11376 /* Change to the _N variant. */
11377 orig_code = BUILT_IN_ATOMIC_STORE_N;
11378 break;
11379 }
11380 default:
11381 gcc_unreachable ();
11382 }
11383 /* Fallthrough to the normal processing. */
11384 }
11385 case BUILT_IN_ATOMIC_EXCHANGE_N:
11386 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11387 case BUILT_IN_ATOMIC_LOAD_N:
11388 case BUILT_IN_ATOMIC_STORE_N:
7a127fa7
MS
11389 {
11390 fetch_op = false;
11391 /* Fallthrough to further processing. */
11392 }
86951993
AM
11393 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11394 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11395 case BUILT_IN_ATOMIC_AND_FETCH_N:
11396 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11397 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11398 case BUILT_IN_ATOMIC_OR_FETCH_N:
11399 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11400 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11401 case BUILT_IN_ATOMIC_FETCH_AND_N:
11402 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11403 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11404 case BUILT_IN_ATOMIC_FETCH_OR_N:
11405 {
11406 orig_format = false;
11407 /* Fallthru for parameter processing. */
11408 }
e0a8ecf2
AM
11409 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11410 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11411 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11412 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11413 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11414 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11415 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11416 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11417 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11418 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11419 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11420 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11421 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11422 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11423 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11424 case BUILT_IN_SYNC_LOCK_RELEASE_N:
48ae6c13 11425 {
7a127fa7
MS
11426 /* The following are not _FETCH_OPs and must be accepted with
11427 pointers to _Bool (or C++ bool). */
11428 if (fetch_op)
11429 fetch_op =
11430 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11431 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11432 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11433 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11434
11435 int n = sync_resolve_size (function, params, fetch_op);
bbbbb16a 11436 tree new_function, first_param, result;
e79983f4 11437 enum built_in_function fncode;
48ae6c13
RH
11438
11439 if (n == 0)
11440 return error_mark_node;
11441
e79983f4
MM
11442 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11443 new_function = builtin_decl_explicit (fncode);
86951993
AM
11444 if (!sync_resolve_params (loc, function, new_function, params,
11445 orig_format))
48ae6c13
RH
11446 return error_mark_node;
11447
9771b263 11448 first_param = (*params)[0];
81e5eca8
MP
11449 result = build_function_call_vec (loc, vNULL, new_function, params,
11450 NULL);
86951993
AM
11451 if (result == error_mark_node)
11452 return result;
e0a8ecf2 11453 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
86951993
AM
11454 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11455 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11456 result = sync_resolve_return (first_param, result, orig_format);
48ae6c13 11457
14ba7b28
MP
11458 if (fetch_op)
11459 /* Prevent -Wunused-value warning. */
11460 TREE_USED (result) = true;
11461
86951993
AM
11462 /* If new_return is set, assign function to that expr and cast the
11463 result to void since the generic interface returned void. */
11464 if (new_return)
11465 {
11466 /* Cast function result from I{1,2,4,8,16} to the required type. */
11467 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11468 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11469 result);
11470 TREE_SIDE_EFFECTS (result) = 1;
11471 protected_set_expr_location (result, loc);
11472 result = convert (void_type_node, result);
11473 }
48ae6c13
RH
11474 return result;
11475 }
11476
11477 default:
58646b77 11478 return NULL_TREE;
48ae6c13
RH
11479 }
11480}
11481
0af94e6f
JR
11482/* vector_types_compatible_elements_p is used in type checks of vectors
11483 values used as operands of binary operators. Where it returns true, and
11484 the other checks of the caller succeed (being vector types in he first
11485 place, and matching number of elements), we can just treat the types
11486 as essentially the same.
11487 Contrast with vector_targets_convertible_p, which is used for vector
11488 pointer types, and vector_types_convertible_p, which will allow
11489 language-specific matches under the control of flag_lax_vector_conversions,
11490 and might still require a conversion. */
11491/* True if vector types T1 and T2 can be inputs to the same binary
11492 operator without conversion.
11493 We don't check the overall vector size here because some of our callers
11494 want to give different error messages when the vectors are compatible
11495 except for the element count. */
11496
5bed876a 11497bool
0af94e6f 11498vector_types_compatible_elements_p (tree t1, tree t2)
5bed876a 11499{
0af94e6f
JR
11500 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11501 t1 = TREE_TYPE (t1);
11502 t2 = TREE_TYPE (t2);
11503
5bed876a
AH
11504 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11505
ab22c1fa
CF
11506 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11507 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11508 || c2 == FIXED_POINT_TYPE));
5bed876a 11509
0af94e6f
JR
11510 t1 = c_common_signed_type (t1);
11511 t2 = c_common_signed_type (t2);
5bed876a
AH
11512 /* Equality works here because c_common_signed_type uses
11513 TYPE_MAIN_VARIANT. */
0af94e6f
JR
11514 if (t1 == t2)
11515 return true;
11516 if (opaque && c1 == c2
11517 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11518 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11519 return true;
11520 return false;
5bed876a
AH
11521}
11522
104f8784
KG
11523/* Check for missing format attributes on function pointers. LTYPE is
11524 the new type or left-hand side type. RTYPE is the old type or
11525 right-hand side type. Returns TRUE if LTYPE is missing the desired
11526 attribute. */
11527
11528bool
11529check_missing_format_attribute (tree ltype, tree rtype)
11530{
11531 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11532 tree ra;
11533
11534 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11535 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11536 break;
11537 if (ra)
11538 {
11539 tree la;
11540 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11541 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11542 break;
11543 return !la;
11544 }
11545 else
11546 return false;
11547}
11548
ff6b6641
GDR
11549/* Subscripting with type char is likely to lose on a machine where
11550 chars are signed. So warn on any machine, but optionally. Don't
11551 warn for unsigned char since that type is safe. Don't warn for
11552 signed char because anyone who uses that must have done so
11553 deliberately. Furthermore, we reduce the false positive load by
11554 warning only for non-constant value of type char. */
11555
11556void
5bd012f8 11557warn_array_subscript_with_type_char (location_t loc, tree index)
ff6b6641
GDR
11558{
11559 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11560 && TREE_CODE (index) != INTEGER_CST)
5bd012f8
MP
11561 warning_at (loc, OPT_Wchar_subscripts,
11562 "array subscript has type %<char%>");
ff6b6641
GDR
11563}
11564
2a67bec2
ILT
11565/* Implement -Wparentheses for the unexpected C precedence rules, to
11566 cover cases like x + y << z which readers are likely to
11567 misinterpret. We have seen an expression in which CODE is a binary
100d537d
MLI
11568 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11569 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11570 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11571 expression was not formed using a binary or unary operator, or it
11572 was enclosed in parentheses. */
2a67bec2
ILT
11573
11574void
5d9de0d0 11575warn_about_parentheses (location_t loc, enum tree_code code,
fb3e178a 11576 enum tree_code code_left, tree arg_left,
100d537d 11577 enum tree_code code_right, tree arg_right)
2a67bec2
ILT
11578{
11579 if (!warn_parentheses)
11580 return;
11581
100d537d
MLI
11582 /* This macro tests that the expression ARG with original tree code
11583 CODE appears to be a boolean expression. or the result of folding a
11584 boolean expression. */
11585#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11586 (truth_value_p (TREE_CODE (ARG)) \
11587 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11588 /* Folding may create 0 or 1 integers from other expressions. */ \
11589 || ((CODE) != INTEGER_CST \
11590 && (integer_onep (ARG) || integer_zerop (ARG))))
11591
b8698a0f 11592 switch (code)
2a67bec2 11593 {
100d537d 11594 case LSHIFT_EXPR:
5d9de0d0
PC
11595 if (code_left == PLUS_EXPR)
11596 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11597 "suggest parentheses around %<+%> inside %<<<%>");
11598 else if (code_right == PLUS_EXPR)
11599 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11600 "suggest parentheses around %<+%> inside %<<<%>");
11601 else if (code_left == MINUS_EXPR)
11602 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11603 "suggest parentheses around %<-%> inside %<<<%>");
11604 else if (code_right == MINUS_EXPR)
11605 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11606 "suggest parentheses around %<-%> inside %<<<%>");
100d537d 11607 return;
2a67bec2 11608
100d537d 11609 case RSHIFT_EXPR:
5d9de0d0
PC
11610 if (code_left == PLUS_EXPR)
11611 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11612 "suggest parentheses around %<+%> inside %<>>%>");
11613 else if (code_right == PLUS_EXPR)
11614 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11615 "suggest parentheses around %<+%> inside %<>>%>");
11616 else if (code_left == MINUS_EXPR)
11617 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11618 "suggest parentheses around %<-%> inside %<>>%>");
11619 else if (code_right == MINUS_EXPR)
11620 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11621 "suggest parentheses around %<-%> inside %<>>%>");
100d537d 11622 return;
2a67bec2 11623
100d537d 11624 case TRUTH_ORIF_EXPR:
5d9de0d0
PC
11625 if (code_left == TRUTH_ANDIF_EXPR)
11626 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11627 "suggest parentheses around %<&&%> within %<||%>");
11628 else if (code_right == TRUTH_ANDIF_EXPR)
11629 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11630 "suggest parentheses around %<&&%> within %<||%>");
100d537d
MLI
11631 return;
11632
11633 case BIT_IOR_EXPR:
2a67bec2 11634 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
5d9de0d0
PC
11635 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11636 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11637 "suggest parentheses around arithmetic in operand of %<|%>");
11638 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11639 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11640 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11641 "suggest parentheses around arithmetic in operand of %<|%>");
2a67bec2 11642 /* Check cases like x|y==z */
5d9de0d0
PC
11643 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11644 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11645 "suggest parentheses around comparison in operand of %<|%>");
11646 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11647 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11648 "suggest parentheses around comparison in operand of %<|%>");
11649 /* Check cases like !x | y */
11650 else if (code_left == TRUTH_NOT_EXPR
11651 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11652 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11653 "suggest parentheses around operand of "
11654 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
100d537d 11655 return;
2a67bec2 11656
100d537d 11657 case BIT_XOR_EXPR:
2a67bec2 11658 if (code_left == BIT_AND_EXPR
5d9de0d0
PC
11659 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11660 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11661 "suggest parentheses around arithmetic in operand of %<^%>");
11662 else if (code_right == BIT_AND_EXPR
11663 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11664 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11665 "suggest parentheses around arithmetic in operand of %<^%>");
2a67bec2 11666 /* Check cases like x^y==z */
5d9de0d0
PC
11667 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11668 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11669 "suggest parentheses around comparison in operand of %<^%>");
11670 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11671 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11672 "suggest parentheses around comparison in operand of %<^%>");
11673 return;
2a67bec2 11674
100d537d 11675 case BIT_AND_EXPR:
5d9de0d0
PC
11676 if (code_left == PLUS_EXPR)
11677 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11678 "suggest parentheses around %<+%> in operand of %<&%>");
11679 else if (code_right == PLUS_EXPR)
11680 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11681 "suggest parentheses around %<+%> in operand of %<&%>");
5d9de0d0
PC
11682 else if (code_left == MINUS_EXPR)
11683 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11684 "suggest parentheses around %<-%> in operand of %<&%>");
11685 else if (code_right == MINUS_EXPR)
11686 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11687 "suggest parentheses around %<-%> in operand of %<&%>");
2a67bec2 11688 /* Check cases like x&y==z */
5d9de0d0
PC
11689 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11690 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11691 "suggest parentheses around comparison in operand of %<&%>");
11692 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11693 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11694 "suggest parentheses around comparison in operand of %<&%>");
11695 /* Check cases like !x & y */
11696 else if (code_left == TRUTH_NOT_EXPR
11697 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11698 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11699 "suggest parentheses around operand of "
11700 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
100d537d 11701 return;
2a67bec2 11702
100d537d 11703 case EQ_EXPR:
5d9de0d0
PC
11704 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11705 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11706 "suggest parentheses around comparison in operand of %<==%>");
11707 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11708 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11709 "suggest parentheses around comparison in operand of %<==%>");
11710 return;
11711 case NE_EXPR:
5d9de0d0
PC
11712 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11713 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11714 "suggest parentheses around comparison in operand of %<!=%>");
11715 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11716 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11717 "suggest parentheses around comparison in operand of %<!=%>");
11718 return;
11719
11720 default:
5d9de0d0
PC
11721 if (TREE_CODE_CLASS (code) == tcc_comparison)
11722 {
11723 if (TREE_CODE_CLASS (code_left) == tcc_comparison
fb3e178a
JJ
11724 && code_left != NE_EXPR && code_left != EQ_EXPR
11725 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
5d9de0d0
PC
11726 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11727 "comparisons like %<X<=Y<=Z%> do not "
11728 "have their mathematical meaning");
11729 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
fb3e178a 11730 && code_right != NE_EXPR && code_right != EQ_EXPR
5d9de0d0
PC
11731 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11732 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11733 "comparisons like %<X<=Y<=Z%> do not "
11734 "have their mathematical meaning");
11735 }
100d537d 11736 return;
e7917d06 11737 }
100d537d 11738#undef NOT_A_BOOLEAN_EXPR_P
2a67bec2
ILT
11739}
11740
c616e51b
MLI
11741/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11742
11743void
11744warn_for_unused_label (tree label)
11745{
11746 if (!TREE_USED (label))
11747 {
11748 if (DECL_INITIAL (label))
11749 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11750 else
11751 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11752 }
11753}
ff6b6641 11754
c9f9eb5d
AH
11755/* Warn for division by zero according to the value of DIVISOR. LOC
11756 is the location of the division operator. */
2e9cb75e
MLI
11757
11758void
c9f9eb5d 11759warn_for_div_by_zero (location_t loc, tree divisor)
2e9cb75e 11760{
ab22c1fa
CF
11761 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11762 about division by zero. Do not issue a warning if DIVISOR has a
2e9cb75e
MLI
11763 floating-point type, since we consider 0.0/0.0 a valid way of
11764 generating a NaN. */
7d882b83 11765 if (c_inhibit_evaluation_warnings == 0
ab22c1fa 11766 && (integer_zerop (divisor) || fixed_zerop (divisor)))
c9f9eb5d 11767 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
2e9cb75e
MLI
11768}
11769
c1e1f433
BS
11770/* Warn for patterns where memset appears to be used incorrectly. The
11771 warning location should be LOC. ARG0, and ARG2 are the first and
11772 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
11773 each argument that was a literal zero. */
11774
11775void
11776warn_for_memset (location_t loc, tree arg0, tree arg2,
11777 int literal_zero_mask)
11778{
11779 if (warn_memset_transposed_args
11780 && integer_zerop (arg2)
11781 && (literal_zero_mask & (1 << 2)) != 0
11782 && (literal_zero_mask & (1 << 1)) == 0)
11783 warning_at (loc, OPT_Wmemset_transposed_args,
11784 "%<memset%> used with constant zero length "
11785 "parameter; this could be due to transposed "
11786 "parameters");
11787
11788 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
11789 {
11790 STRIP_NOPS (arg0);
11791 if (TREE_CODE (arg0) == ADDR_EXPR)
11792 arg0 = TREE_OPERAND (arg0, 0);
11793 tree type = TREE_TYPE (arg0);
11794 if (TREE_CODE (type) == ARRAY_TYPE)
11795 {
11796 tree elt_type = TREE_TYPE (type);
11797 tree domain = TYPE_DOMAIN (type);
11798 if (!integer_onep (TYPE_SIZE_UNIT (elt_type))
11799 && TYPE_MAXVAL (domain)
11800 && TYPE_MINVAL (domain)
11801 && integer_zerop (TYPE_MINVAL (domain))
11802 && integer_onep (fold_build2 (MINUS_EXPR, domain,
11803 arg2,
11804 TYPE_MAXVAL (domain))))
11805 warning_at (loc, OPT_Wmemset_elt_size,
11806 "%<memset%> used with length equal to "
11807 "number of elements without multiplication "
11808 "by element size");
11809 }
11810 }
11811}
11812
2d12797c
MLI
11813/* Subroutine of build_binary_op. Give warnings for comparisons
11814 between signed and unsigned quantities that may fail. Do the
11815 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11816 so that casts will be considered, but default promotions won't
ba47d38d
AH
11817 be.
11818
11819 LOCATION is the location of the comparison operator.
2d12797c
MLI
11820
11821 The arguments of this function map directly to local variables
11822 of build_binary_op. */
11823
b8698a0f 11824void
ba47d38d 11825warn_for_sign_compare (location_t location,
b8698a0f
L
11826 tree orig_op0, tree orig_op1,
11827 tree op0, tree op1,
2d12797c
MLI
11828 tree result_type, enum tree_code resultcode)
11829{
11830 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11831 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11832 int unsignedp0, unsignedp1;
b8698a0f 11833
2d12797c
MLI
11834 /* In C++, check for comparison of different enum types. */
11835 if (c_dialect_cxx()
11836 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11837 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11838 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
ead51d36 11839 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
2d12797c 11840 {
ba47d38d
AH
11841 warning_at (location,
11842 OPT_Wsign_compare, "comparison between types %qT and %qT",
11843 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
2d12797c
MLI
11844 }
11845
11846 /* Do not warn if the comparison is being done in a signed type,
11847 since the signed type will only be chosen if it can represent
11848 all the values of the unsigned type. */
11849 if (!TYPE_UNSIGNED (result_type))
11850 /* OK */;
11851 /* Do not warn if both operands are unsigned. */
11852 else if (op0_signed == op1_signed)
11853 /* OK */;
11854 else
11855 {
ead51d36 11856 tree sop, uop, base_type;
2d12797c 11857 bool ovf;
ead51d36 11858
2d12797c
MLI
11859 if (op0_signed)
11860 sop = orig_op0, uop = orig_op1;
b8698a0f 11861 else
2d12797c
MLI
11862 sop = orig_op1, uop = orig_op0;
11863
b8698a0f 11864 STRIP_TYPE_NOPS (sop);
2d12797c 11865 STRIP_TYPE_NOPS (uop);
ead51d36
JJ
11866 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11867 ? TREE_TYPE (result_type) : result_type);
2d12797c
MLI
11868
11869 /* Do not warn if the signed quantity is an unsuffixed integer
11870 literal (or some static constant expression involving such
11871 literals or a conditional expression involving such literals)
11872 and it is non-negative. */
11873 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11874 /* OK */;
11875 /* Do not warn if the comparison is an equality operation, the
11876 unsigned quantity is an integral constant, and it would fit
11877 in the result if the result were signed. */
11878 else if (TREE_CODE (uop) == INTEGER_CST
11879 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
ead51d36 11880 && int_fits_type_p (uop, c_common_signed_type (base_type)))
2d12797c
MLI
11881 /* OK */;
11882 /* In C, do not warn if the unsigned quantity is an enumeration
11883 constant and its maximum value would fit in the result if the
11884 result were signed. */
11885 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11886 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11887 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
ead51d36 11888 c_common_signed_type (base_type)))
2d12797c 11889 /* OK */;
b8698a0f 11890 else
ba47d38d 11891 warning_at (location,
b8698a0f 11892 OPT_Wsign_compare,
ba47d38d 11893 "comparison between signed and unsigned integer expressions");
2d12797c 11894 }
b8698a0f 11895
2d12797c
MLI
11896 /* Warn if two unsigned values are being compared in a size larger
11897 than their original size, and one (and only one) is the result of
11898 a `~' operator. This comparison will always fail.
b8698a0f 11899
2d12797c
MLI
11900 Also warn if one operand is a constant, and the constant does not
11901 have all bits set that are set in the ~ operand when it is
11902 extended. */
11903
828fb3ba
JM
11904 op0 = c_common_get_narrower (op0, &unsignedp0);
11905 op1 = c_common_get_narrower (op1, &unsignedp1);
b8698a0f 11906
2d12797c
MLI
11907 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11908 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11909 {
11910 if (TREE_CODE (op0) == BIT_NOT_EXPR)
828fb3ba 11911 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2d12797c 11912 if (TREE_CODE (op1) == BIT_NOT_EXPR)
828fb3ba 11913 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2d12797c 11914
9541ffee 11915 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2d12797c
MLI
11916 {
11917 tree primop;
11918 HOST_WIDE_INT constant, mask;
11919 int unsignedp;
11920 unsigned int bits;
b8698a0f 11921
9541ffee 11922 if (tree_fits_shwi_p (op0))
2d12797c
MLI
11923 {
11924 primop = op1;
11925 unsignedp = unsignedp1;
9439e9a1 11926 constant = tree_to_shwi (op0);
2d12797c
MLI
11927 }
11928 else
11929 {
11930 primop = op0;
11931 unsignedp = unsignedp0;
9439e9a1 11932 constant = tree_to_shwi (op1);
2d12797c 11933 }
b8698a0f 11934
2d12797c
MLI
11935 bits = TYPE_PRECISION (TREE_TYPE (primop));
11936 if (bits < TYPE_PRECISION (result_type)
11937 && bits < HOST_BITS_PER_LONG && unsignedp)
11938 {
aa256c4a 11939 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
2d12797c
MLI
11940 if ((mask & constant) != mask)
11941 {
11942 if (constant == 0)
0a756a3f
MP
11943 warning_at (location, OPT_Wsign_compare,
11944 "promoted ~unsigned is always non-zero");
2d12797c 11945 else
b8698a0f 11946 warning_at (location, OPT_Wsign_compare,
ba47d38d 11947 "comparison of promoted ~unsigned with constant");
2d12797c
MLI
11948 }
11949 }
11950 }
11951 else if (unsignedp0 && unsignedp1
11952 && (TYPE_PRECISION (TREE_TYPE (op0))
11953 < TYPE_PRECISION (result_type))
11954 && (TYPE_PRECISION (TREE_TYPE (op1))
11955 < TYPE_PRECISION (result_type)))
ba47d38d 11956 warning_at (location, OPT_Wsign_compare,
2d12797c
MLI
11957 "comparison of promoted ~unsigned with unsigned");
11958 }
11959}
11960
c5ee1358
MM
11961/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11962 type via c_common_type. If -Wdouble-promotion is in use, and the
11963 conditions for warning have been met, issue a warning. GMSGID is
11964 the warning message. It must have two %T specifiers for the type
11965 that was converted (generally "float") and the type to which it was
11966 converted (generally "double), respectively. LOC is the location
11967 to which the awrning should refer. */
11968
11969void
11970do_warn_double_promotion (tree result_type, tree type1, tree type2,
11971 const char *gmsgid, location_t loc)
11972{
11973 tree source_type;
11974
11975 if (!warn_double_promotion)
11976 return;
11977 /* If the conversion will not occur at run-time, there is no need to
11978 warn about it. */
11979 if (c_inhibit_evaluation_warnings)
11980 return;
11981 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11982 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11983 return;
11984 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11985 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11986 source_type = type1;
11987 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11988 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11989 source_type = type2;
11990 else
11991 return;
11992 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11993}
11994
da2e71c9
MLI
11995/* Possibly warn about unused parameters. */
11996
11997void
11998do_warn_unused_parameter (tree fn)
11999{
12000 tree decl;
12001
12002 for (decl = DECL_ARGUMENTS (fn);
12003 decl; decl = DECL_CHAIN (decl))
12004 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12005 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12006 && !TREE_NO_WARNING (decl))
12007 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12008 "unused parameter %qD", decl);
12009}
12010
12011
d0940d56
DS
12012/* Setup a TYPE_DECL node as a typedef representation.
12013
12014 X is a TYPE_DECL for a typedef statement. Create a brand new
12015 ..._TYPE node (which will be just a variant of the existing
12016 ..._TYPE node with identical properties) and then install X
12017 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12018
12019 The whole point here is to end up with a situation where each
12020 and every ..._TYPE node the compiler creates will be uniquely
12021 associated with AT MOST one node representing a typedef name.
12022 This way, even though the compiler substitutes corresponding
12023 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12024 early on, later parts of the compiler can always do the reverse
12025 translation and get back the corresponding typedef name. For
12026 example, given:
12027
12028 typedef struct S MY_TYPE;
12029 MY_TYPE object;
12030
12031 Later parts of the compiler might only know that `object' was of
12032 type `struct S' if it were not for code just below. With this
12033 code however, later parts of the compiler see something like:
12034
12035 struct S' == struct S
12036 typedef struct S' MY_TYPE;
12037 struct S' object;
12038
12039 And they can then deduce (from the node for type struct S') that
12040 the original object declaration was:
12041
12042 MY_TYPE object;
12043
12044 Being able to do this is important for proper support of protoize,
12045 and also for generating precise symbolic debugging information
12046 which takes full account of the programmer's (typedef) vocabulary.
12047
12048 Obviously, we don't want to generate a duplicate ..._TYPE node if
12049 the TYPE_DECL node that we are now processing really represents a
12050 standard built-in type. */
12051
12052void
12053set_underlying_type (tree x)
12054{
12055 if (x == error_mark_node)
12056 return;
12057 if (DECL_IS_BUILTIN (x))
12058 {
12059 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12060 TYPE_NAME (TREE_TYPE (x)) = x;
12061 }
12062 else if (TREE_TYPE (x) != error_mark_node
12063 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12064 {
12065 tree tt = TREE_TYPE (x);
12066 DECL_ORIGINAL_TYPE (x) = tt;
12067 tt = build_variant_type_copy (tt);
12068 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12069 TYPE_NAME (tt) = x;
12070 TREE_USED (tt) = TREE_USED (x);
12071 TREE_TYPE (x) = tt;
12072 }
12073}
12074
b646ba3f
DS
12075/* Record the types used by the current global variable declaration
12076 being parsed, so that we can decide later to emit their debug info.
12077 Those types are in types_used_by_cur_var_decl, and we are going to
12078 store them in the types_used_by_vars_hash hash table.
12079 DECL is the declaration of the global variable that has been parsed. */
12080
12081void
12082record_types_used_by_current_var_decl (tree decl)
12083{
12084 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12085
9771b263 12086 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
b646ba3f 12087 {
9771b263 12088 tree type = types_used_by_cur_var_decl->pop ();
bc87224e 12089 types_used_by_var_decl_insert (type, decl);
b646ba3f
DS
12090 }
12091}
12092
3797cb21
DS
12093/* If DECL is a typedef that is declared in the current function,
12094 record it for the purpose of -Wunused-local-typedefs. */
12095
12096void
12097record_locally_defined_typedef (tree decl)
12098{
12099 struct c_language_function *l;
12100
12101 if (!warn_unused_local_typedefs
12102 || cfun == NULL
12103 /* if this is not a locally defined typedef then we are not
12104 interested. */
12105 || !is_typedef_decl (decl)
12106 || !decl_function_context (decl))
12107 return;
12108
12109 l = (struct c_language_function *) cfun->language;
9771b263 12110 vec_safe_push (l->local_typedefs, decl);
3797cb21
DS
12111}
12112
12113/* If T is a TYPE_DECL declared locally, mark it as used. */
12114
12115void
12116maybe_record_typedef_use (tree t)
12117{
12118 if (!is_typedef_decl (t))
12119 return;
12120
12121 TREE_USED (t) = true;
12122}
12123
12124/* Warn if there are some unused locally defined typedefs in the
12125 current function. */
12126
12127void
12128maybe_warn_unused_local_typedefs (void)
12129{
12130 int i;
12131 tree decl;
12132 /* The number of times we have emitted -Wunused-local-typedefs
12133 warnings. If this is different from errorcount, that means some
12134 unrelated errors have been issued. In which case, we'll avoid
12135 emitting "unused-local-typedefs" warnings. */
12136 static int unused_local_typedefs_warn_count;
12137 struct c_language_function *l;
12138
12139 if (cfun == NULL)
12140 return;
12141
12142 if ((l = (struct c_language_function *) cfun->language) == NULL)
12143 return;
12144
12145 if (warn_unused_local_typedefs
12146 && errorcount == unused_local_typedefs_warn_count)
12147 {
9771b263 12148 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
3797cb21
DS
12149 if (!TREE_USED (decl))
12150 warning_at (DECL_SOURCE_LOCATION (decl),
12151 OPT_Wunused_local_typedefs,
12152 "typedef %qD locally defined but not used", decl);
12153 unused_local_typedefs_warn_count = errorcount;
12154 }
12155
9771b263 12156 vec_free (l->local_typedefs);
3797cb21
DS
12157}
12158
04159acf
MP
12159/* Warn about boolean expression compared with an integer value different
12160 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12161 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12162 are the operands of the comparison. The caller must ensure that
12163 either operand is a boolean expression. */
12164
12165void
12166maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12167 tree op1)
12168{
12169 if (TREE_CODE_CLASS (code) != tcc_comparison)
12170 return;
12171
f479b43d
JM
12172 tree f, cst;
12173 if (f = fold_for_warn (op0),
12174 TREE_CODE (f) == INTEGER_CST)
12175 cst = op0 = f;
12176 else if (f = fold_for_warn (op1),
12177 TREE_CODE (f) == INTEGER_CST)
12178 cst = op1 = f;
12179 else
04159acf
MP
12180 return;
12181
12182 if (!integer_zerop (cst) && !integer_onep (cst))
12183 {
716c0ba6
MP
12184 int sign = (TREE_CODE (op0) == INTEGER_CST
12185 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
04159acf
MP
12186 if (code == EQ_EXPR
12187 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12188 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12189 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12190 "with boolean expression is always false", cst);
12191 else
12192 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12193 "with boolean expression is always true", cst);
12194 }
716c0ba6
MP
12195 else if (integer_zerop (cst) || integer_onep (cst))
12196 {
577cd070
MP
12197 /* If the non-constant operand isn't of a boolean type, we
12198 don't want to warn here. */
12199 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12200 /* Handle booleans promoted to integers. */
12201 if (CONVERT_EXPR_P (noncst)
12202 && TREE_TYPE (noncst) == integer_type_node
12203 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12204 /* Warn. */;
12205 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12206 && !truth_value_p (TREE_CODE (noncst)))
12207 return;
716c0ba6
MP
12208 /* Do some magic to get the right diagnostics. */
12209 bool flag = TREE_CODE (op0) == INTEGER_CST;
12210 flag = integer_zerop (cst) ? flag : !flag;
12211 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12212 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12213 "with boolean expression is always true", cst);
12214 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12215 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12216 "with boolean expression is always false", cst);
12217 }
04159acf
MP
12218}
12219
451b5e48
MP
12220/* Warn if signed left shift overflows. We don't warn
12221 about left-shifting 1 into the sign bit in C++14; cf.
12222 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12223 LOC is a location of the shift; OP0 and OP1 are the operands.
12224 Return true if an overflow is detected, false otherwise. */
12225
12226bool
12227maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12228{
12229 if (TREE_CODE (op0) != INTEGER_CST
12230 || TREE_CODE (op1) != INTEGER_CST)
12231 return false;
12232
12233 tree type0 = TREE_TYPE (op0);
12234 unsigned int prec0 = TYPE_PRECISION (type0);
12235
12236 /* Left-hand operand must be signed. */
12237 if (TYPE_UNSIGNED (type0))
12238 return false;
12239
b893e375
MP
12240 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12241 + TREE_INT_CST_LOW (op1));
c5404f1c
PB
12242 /* Handle the case of left-shifting 1 into the sign bit.
12243 * However, shifting 1 _out_ of the sign bit, as in
12244 * INT_MIN << 1, is considered an overflow.
12245 */
12246 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
451b5e48
MP
12247 {
12248 /* Never warn for C++14 onwards. */
12249 if (cxx_dialect >= cxx14)
12250 return false;
12251 /* Otherwise only if -Wshift-overflow=2. But return
12252 true to signal an overflow for the sake of integer
12253 constant expressions. */
12254 if (warn_shift_overflow < 2)
12255 return true;
12256 }
12257
451b5e48
MP
12258 bool overflowed = min_prec > prec0;
12259 if (overflowed && c_inhibit_evaluation_warnings == 0)
12260 warning_at (loc, OPT_Wshift_overflow_,
12261 "result of %qE requires %u bits to represent, "
12262 "but %qT only has %u bits",
12263 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12264 min_prec, type0, prec0);
12265
12266 return overflowed;
12267}
12268
c166b898
ILT
12269/* The C and C++ parsers both use vectors to hold function arguments.
12270 For efficiency, we keep a cache of unused vectors. This is the
12271 cache. */
12272
9771b263
DN
12273typedef vec<tree, va_gc> *tree_gc_vec;
12274static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
c166b898
ILT
12275
12276/* Return a new vector from the cache. If the cache is empty,
12277 allocate a new vector. These vectors are GC'ed, so it is OK if the
12278 pointer is not released.. */
12279
9771b263 12280vec<tree, va_gc> *
c166b898
ILT
12281make_tree_vector (void)
12282{
9771b263
DN
12283 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12284 return tree_vector_cache->pop ();
c166b898
ILT
12285 else
12286 {
9771b263 12287 /* Passing 0 to vec::alloc returns NULL, and our callers require
c166b898
ILT
12288 that we always return a non-NULL value. The vector code uses
12289 4 when growing a NULL vector, so we do too. */
9771b263
DN
12290 vec<tree, va_gc> *v;
12291 vec_alloc (v, 4);
12292 return v;
c166b898
ILT
12293 }
12294}
12295
12296/* Release a vector of trees back to the cache. */
12297
12298void
9771b263 12299release_tree_vector (vec<tree, va_gc> *vec)
c166b898
ILT
12300{
12301 if (vec != NULL)
12302 {
9771b263
DN
12303 vec->truncate (0);
12304 vec_safe_push (tree_vector_cache, vec);
c166b898
ILT
12305 }
12306}
12307
12308/* Get a new tree vector holding a single tree. */
12309
9771b263 12310vec<tree, va_gc> *
c166b898
ILT
12311make_tree_vector_single (tree t)
12312{
9771b263
DN
12313 vec<tree, va_gc> *ret = make_tree_vector ();
12314 ret->quick_push (t);
c166b898
ILT
12315 return ret;
12316}
12317
c12ff9d8
JM
12318/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12319
9771b263 12320vec<tree, va_gc> *
c12ff9d8
JM
12321make_tree_vector_from_list (tree list)
12322{
9771b263 12323 vec<tree, va_gc> *ret = make_tree_vector ();
c12ff9d8 12324 for (; list; list = TREE_CHAIN (list))
9771b263 12325 vec_safe_push (ret, TREE_VALUE (list));
c12ff9d8
JM
12326 return ret;
12327}
12328
c166b898
ILT
12329/* Get a new tree vector which is a copy of an existing one. */
12330
9771b263
DN
12331vec<tree, va_gc> *
12332make_tree_vector_copy (const vec<tree, va_gc> *orig)
c166b898 12333{
9771b263 12334 vec<tree, va_gc> *ret;
c166b898
ILT
12335 unsigned int ix;
12336 tree t;
12337
12338 ret = make_tree_vector ();
9771b263
DN
12339 vec_safe_reserve (ret, vec_safe_length (orig));
12340 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12341 ret->quick_push (t);
c166b898
ILT
12342 return ret;
12343}
12344
a9aa2c3a
NF
12345/* Return true if KEYWORD starts a type specifier. */
12346
12347bool
12348keyword_begins_type_specifier (enum rid keyword)
12349{
12350 switch (keyword)
12351 {
38b7bc7f 12352 case RID_AUTO_TYPE:
a9aa2c3a
NF
12353 case RID_INT:
12354 case RID_CHAR:
12355 case RID_FLOAT:
12356 case RID_DOUBLE:
12357 case RID_VOID:
a9aa2c3a
NF
12358 case RID_UNSIGNED:
12359 case RID_LONG:
12360 case RID_SHORT:
12361 case RID_SIGNED:
12362 case RID_DFLOAT32:
12363 case RID_DFLOAT64:
12364 case RID_DFLOAT128:
12365 case RID_FRACT:
12366 case RID_ACCUM:
12367 case RID_BOOL:
12368 case RID_WCHAR:
12369 case RID_CHAR16:
12370 case RID_CHAR32:
12371 case RID_SAT:
12372 case RID_COMPLEX:
12373 case RID_TYPEOF:
12374 case RID_STRUCT:
12375 case RID_CLASS:
12376 case RID_UNION:
12377 case RID_ENUM:
12378 return true;
12379 default:
78a7c317
DD
12380 if (keyword >= RID_FIRST_INT_N
12381 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12382 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12383 return true;
a9aa2c3a
NF
12384 return false;
12385 }
12386}
12387
12388/* Return true if KEYWORD names a type qualifier. */
12389
12390bool
12391keyword_is_type_qualifier (enum rid keyword)
12392{
12393 switch (keyword)
12394 {
12395 case RID_CONST:
12396 case RID_VOLATILE:
12397 case RID_RESTRICT:
267bac10 12398 case RID_ATOMIC:
a9aa2c3a
NF
12399 return true;
12400 default:
12401 return false;
12402 }
12403}
12404
12405/* Return true if KEYWORD names a storage class specifier.
12406
12407 RID_TYPEDEF is not included in this list despite `typedef' being
12408 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12409 such for syntactic convenience only. */
12410
12411bool
12412keyword_is_storage_class_specifier (enum rid keyword)
12413{
12414 switch (keyword)
12415 {
12416 case RID_STATIC:
12417 case RID_EXTERN:
12418 case RID_REGISTER:
12419 case RID_AUTO:
12420 case RID_MUTABLE:
12421 case RID_THREAD:
12422 return true;
12423 default:
12424 return false;
12425 }
12426}
12427
ba9e6dd5
NF
12428/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12429
12430static bool
12431keyword_is_function_specifier (enum rid keyword)
12432{
12433 switch (keyword)
12434 {
12435 case RID_INLINE:
bbceee64 12436 case RID_NORETURN:
ba9e6dd5
NF
12437 case RID_VIRTUAL:
12438 case RID_EXPLICIT:
12439 return true;
12440 default:
12441 return false;
12442 }
12443}
12444
12445/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12446 declaration-specifier (C99 6.7). */
12447
12448bool
12449keyword_is_decl_specifier (enum rid keyword)
12450{
12451 if (keyword_is_storage_class_specifier (keyword)
12452 || keyword_is_type_qualifier (keyword)
12453 || keyword_is_function_specifier (keyword))
12454 return true;
12455
12456 switch (keyword)
12457 {
12458 case RID_TYPEDEF:
12459 case RID_FRIEND:
12460 case RID_CONSTEXPR:
12461 return true;
12462 default:
12463 return false;
12464 }
12465}
12466
81f653d6
NF
12467/* Initialize language-specific-bits of tree_contains_struct. */
12468
12469void
12470c_common_init_ts (void)
12471{
12472 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12473 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
36536d79 12474 MARK_TS_TYPED (ARRAY_NOTATION_REF);
81f653d6
NF
12475}
12476
3ce4f9e4
ESR
12477/* Build a user-defined numeric literal out of an integer constant type VALUE
12478 with identifier SUFFIX. */
12479
12480tree
2d7aa578
ESR
12481build_userdef_literal (tree suffix_id, tree value,
12482 enum overflow_type overflow, tree num_string)
3ce4f9e4
ESR
12483{
12484 tree literal = make_node (USERDEF_LITERAL);
12485 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12486 USERDEF_LITERAL_VALUE (literal) = value;
2d7aa578 12487 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
3ce4f9e4
ESR
12488 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12489 return literal;
12490}
12491
7edaa4d2 12492/* For vector[index], convert the vector to a
aa7da51a
JJ
12493 pointer of the underlying type. Return true if the resulting
12494 ARRAY_REF should not be an lvalue. */
12495
12496bool
7edaa4d2 12497convert_vector_to_pointer_for_subscript (location_t loc,
aa7da51a 12498 tree *vecp, tree index)
7edaa4d2 12499{
aa7da51a 12500 bool ret = false;
31521951 12501 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7edaa4d2
MG
12502 {
12503 tree type = TREE_TYPE (*vecp);
12504 tree type1;
12505
aa7da51a 12506 ret = !lvalue_p (*vecp);
7edaa4d2 12507 if (TREE_CODE (index) == INTEGER_CST)
cc269bb6 12508 if (!tree_fits_uhwi_p (index)
7d362f6c 12509 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7edaa4d2
MG
12510 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12511
aa7da51a
JJ
12512 if (ret)
12513 {
b731b390 12514 tree tmp = create_tmp_var_raw (type);
aa7da51a
JJ
12515 DECL_SOURCE_LOCATION (tmp) = loc;
12516 *vecp = c_save_expr (*vecp);
12517 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12518 {
12519 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12520 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12521 *vecp
12522 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12523 *vecp, NULL_TREE, NULL_TREE),
12524 non_const);
12525 }
12526 else
12527 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12528 NULL_TREE, NULL_TREE);
12529 SET_EXPR_LOCATION (*vecp, loc);
12530 c_common_mark_addressable_vec (tmp);
12531 }
12532 else
12533 c_common_mark_addressable_vec (*vecp);
7edaa4d2 12534 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7edaa4d2 12535 type1 = build_pointer_type (TREE_TYPE (*vecp));
b0f1bf36
RB
12536 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12537 if (!ref_all
12538 && !DECL_P (*vecp))
12539 {
12540 /* If the original vector isn't declared may_alias and it
12541 isn't a bare vector look if the subscripting would
12542 alias the vector we subscript, and if not, force ref-all. */
12543 alias_set_type vecset = get_alias_set (*vecp);
12544 alias_set_type sset = get_alias_set (type);
12545 if (!alias_sets_must_conflict_p (sset, vecset)
12546 && !alias_set_subset_of (sset, vecset))
12547 ref_all = true;
12548 }
12549 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7edaa4d2
MG
12550 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12551 *vecp = convert (type, *vecp);
12552 }
aa7da51a 12553 return ret;
7edaa4d2
MG
12554}
12555
a212e43f
MG
12556/* Determine which of the operands, if any, is a scalar that needs to be
12557 converted to a vector, for the range of operations. */
12558enum stv_conv
12559scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12560 bool complain)
12561{
12562 tree type0 = TREE_TYPE (op0);
12563 tree type1 = TREE_TYPE (op1);
12564 bool integer_only_op = false;
12565 enum stv_conv ret = stv_firstarg;
12566
31521951 12567 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
a212e43f
MG
12568 switch (code)
12569 {
12570 /* Most GENERIC binary expressions require homogeneous arguments.
12571 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12572 argument that is a vector and a second one that is a scalar, so
12573 we never return stv_secondarg for them. */
12574 case RSHIFT_EXPR:
12575 case LSHIFT_EXPR:
12576 if (TREE_CODE (type0) == INTEGER_TYPE
12577 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12578 {
68fca595 12579 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12580 {
12581 if (complain)
12582 error_at (loc, "conversion of scalar %qT to vector %qT "
12583 "involves truncation", type0, type1);
12584 return stv_error;
12585 }
12586 else
12587 return stv_firstarg;
12588 }
12589 break;
12590
12591 case BIT_IOR_EXPR:
12592 case BIT_XOR_EXPR:
12593 case BIT_AND_EXPR:
12594 integer_only_op = true;
12595 /* ... fall through ... */
12596
93100c6b
MG
12597 case VEC_COND_EXPR:
12598
a212e43f
MG
12599 case PLUS_EXPR:
12600 case MINUS_EXPR:
12601 case MULT_EXPR:
12602 case TRUNC_DIV_EXPR:
12603 case CEIL_DIV_EXPR:
12604 case FLOOR_DIV_EXPR:
12605 case ROUND_DIV_EXPR:
12606 case EXACT_DIV_EXPR:
12607 case TRUNC_MOD_EXPR:
12608 case FLOOR_MOD_EXPR:
12609 case RDIV_EXPR:
12610 case EQ_EXPR:
12611 case NE_EXPR:
12612 case LE_EXPR:
12613 case GE_EXPR:
12614 case LT_EXPR:
12615 case GT_EXPR:
12616 /* What about UNLT_EXPR? */
31521951 12617 if (VECTOR_TYPE_P (type0))
a212e43f 12618 {
a212e43f 12619 ret = stv_secondarg;
6b4db501
MM
12620 std::swap (type0, type1);
12621 std::swap (op0, op1);
a212e43f
MG
12622 }
12623
12624 if (TREE_CODE (type0) == INTEGER_TYPE
12625 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12626 {
68fca595 12627 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12628 {
12629 if (complain)
12630 error_at (loc, "conversion of scalar %qT to vector %qT "
12631 "involves truncation", type0, type1);
12632 return stv_error;
12633 }
12634 return ret;
12635 }
12636 else if (!integer_only_op
12637 /* Allow integer --> real conversion if safe. */
12638 && (TREE_CODE (type0) == REAL_TYPE
12639 || TREE_CODE (type0) == INTEGER_TYPE)
12640 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12641 {
68fca595 12642 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12643 {
12644 if (complain)
12645 error_at (loc, "conversion of scalar %qT to vector %qT "
12646 "involves truncation", type0, type1);
12647 return stv_error;
12648 }
12649 return ret;
12650 }
12651 default:
12652 break;
12653 }
12654
12655 return stv_nothing;
12656}
12657
e28d52cf
DS
12658/* Return true iff ALIGN is an integral constant that is a fundamental
12659 alignment, as defined by [basic.align] in the c++-11
12660 specifications.
12661
12662 That is:
12663
12664 [A fundamental alignment is represented by an alignment less than or
12665 equal to the greatest alignment supported by the implementation
12666 in all contexts, which is equal to
12667 alignof(max_align_t)]. */
12668
12669bool
12670cxx_fundamental_alignment_p (unsigned align)
12671{
12672 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12673 TYPE_ALIGN (long_double_type_node)));
12674}
12675
f04dda30
MP
12676/* Return true if T is a pointer to a zero-sized aggregate. */
12677
12678bool
12679pointer_to_zero_sized_aggr_p (tree t)
12680{
12681 if (!POINTER_TYPE_P (t))
12682 return false;
12683 t = TREE_TYPE (t);
12684 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12685}
12686
1807ffc1
MS
12687/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12688 with no library fallback or for an ADDR_EXPR whose operand is such type
12689 issues an error pointing to the location LOC.
12690 Returns true when the expression has been diagnosed and false
12691 otherwise. */
12692bool
12693reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12694{
12695 if (TREE_CODE (expr) == ADDR_EXPR)
12696 expr = TREE_OPERAND (expr, 0);
12697
12698 if (TREE_TYPE (expr)
12699 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
1be56bc5 12700 && TREE_CODE (expr) == FUNCTION_DECL
1807ffc1
MS
12701 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12702 false positives for user-declared built-ins such as abs or
12703 strlen, and for C++ operators new and delete.
12704 The c_decl_implicit() test avoids false positives for implicitly
12705 declared built-ins with library fallbacks (such as abs). */
12706 && DECL_BUILT_IN (expr)
12707 && DECL_IS_BUILTIN (expr)
12708 && !c_decl_implicit (expr)
12709 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12710 {
12711 if (loc == UNKNOWN_LOCATION)
12712 loc = EXPR_LOC_OR_LOC (expr, input_location);
12713
12714 /* Reject arguments that are built-in functions with
12715 no library fallback. */
12716 error_at (loc, "built-in function %qE must be directly called", expr);
12717
12718 return true;
12719 }
12720
12721 return false;
12722}
12723
3e3b8d63
MP
12724/* If we're creating an if-else-if condition chain, first see if we
12725 already have this COND in the CHAIN. If so, warn and don't add COND
12726 into the vector, otherwise add the COND there. LOC is the location
12727 of COND. */
12728
12729void
12730warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12731{
12732 /* No chain has been created yet. Do nothing. */
12733 if (*chain == NULL)
12734 return;
12735
12736 if (TREE_SIDE_EFFECTS (cond))
12737 {
12738 /* Uh-oh! This condition has a side-effect, thus invalidates
12739 the whole chain. */
12740 delete *chain;
12741 *chain = NULL;
12742 return;
12743 }
12744
12745 unsigned int ix;
12746 tree t;
12747 bool found = false;
12748 FOR_EACH_VEC_ELT (**chain, ix, t)
12749 if (operand_equal_p (cond, t, 0))
12750 {
12751 if (warning_at (loc, OPT_Wduplicated_cond,
12752 "duplicated %<if%> condition"))
12753 inform (EXPR_LOCATION (t), "previously used here");
12754 found = true;
12755 break;
12756 }
12757
12758 if (!found
12759 && !CONSTANT_CLASS_P (cond)
12760 /* Don't infinitely grow the chain. */
12761 && (*chain)->length () < 512)
12762 (*chain)->safe_push (cond);
12763}
12764
e78bede6
MP
12765/* Check if array size calculations overflow or if the array covers more
12766 than half of the address space. Return true if the size of the array
12767 is valid, false otherwise. TYPE is the type of the array and NAME is
12768 the name of the array, or NULL_TREE for unnamed arrays. */
12769
12770bool
12771valid_array_size_p (location_t loc, tree type, tree name)
12772{
12773 if (type != error_mark_node
12774 && COMPLETE_TYPE_P (type)
12775 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12776 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
12777 {
12778 if (name)
12779 error_at (loc, "size of array %qE is too large", name);
12780 else
12781 error_at (loc, "size of unnamed array is too large");
12782 return false;
12783 }
12784 return true;
12785}
12786
174f6622
ES
12787/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
12788 timestamp to replace embedded current dates to get reproducible
12789 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
12790time_t
12791get_source_date_epoch ()
12792{
12793 char *source_date_epoch;
12794 long long epoch;
12795 char *endptr;
12796
12797 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
12798 if (!source_date_epoch)
12799 return (time_t) -1;
12800
12801 errno = 0;
12802 epoch = strtoll (source_date_epoch, &endptr, 10);
12803 if ((errno == ERANGE && (epoch == LLONG_MAX || epoch == LLONG_MIN))
12804 || (errno != 0 && epoch == 0))
12805 fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
12806 "strtoll: %s\n", xstrerror(errno));
12807 if (endptr == source_date_epoch)
12808 fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
12809 "no digits were found: %s\n", endptr);
12810 if (*endptr != '\0')
12811 fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
12812 "trailing garbage: %s\n", endptr);
12813 if (epoch < 0)
12814 fatal_error (UNKNOWN_LOCATION, "environment variable $SOURCE_DATE_EPOCH: "
12815 "value must be nonnegative: %lld \n", epoch);
12816
12817 return (time_t) epoch;
12818}
12819
39dabefd 12820#include "gt-c-family-c-common.h"