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