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