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