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