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