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