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