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