]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
Daily bump.
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
5624e564 2 Copyright (C) 1992-2015 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 19
40e23961
MC
20#define GCC_C_COMMON_C
21
b30f223b 22#include "config.h"
670ee920 23#include "system.h"
4977bab6 24#include "coretypes.h"
b559c810 25#include "c-common.h"
4977bab6 26#include "tm.h"
d9b2742a 27#include "intl.h"
b30f223b 28#include "tree.h"
d8a2d370
DN
29#include "fold-const.h"
30#include "stor-layout.h"
31#include "calls.h"
32#include "stringpool.h"
33#include "attribs.h"
34#include "varasm.h"
35#include "trans-mem.h"
b30f223b 36#include "flags.h"
e2af664c 37#include "c-pragma.h"
61d3ce20 38#include "c-objc.h"
7bdb32b9 39#include "tm_p.h"
235cfbc4 40#include "obstack.h"
c8724862 41#include "cpplib.h"
12a68f1f 42#include "target.h"
677f3fa8 43#include "common/common-target.h"
7afff7cf 44#include "langhooks.h"
d57a4b98 45#include "tree-inline.h"
5f1989e6 46#include "toplev.h"
148e4216 47#include "diagnostic.h"
6de9cd9a 48#include "tree-iterator.h"
d974312d 49#include "opts.h"
c582198b 50#include "plugin-api.h"
c582198b 51#include "hard-reg-set.h"
c582198b
AM
52#include "function.h"
53#include "ipa-ref.h"
ce91e74c 54#include "cgraph.h"
1f1d5130 55#include "target-def.h"
45b0be94 56#include "gimplify.h"
807e902e 57#include "wide-int-print.h"
aa7da51a 58#include "gimple-expr.h"
cb60f38d 59
81a75f0f 60cpp_reader *parse_in; /* Declared in c-pragma.h. */
c8724862 61
8fba1830
BRF
62/* Mode used to build pointers (VOIDmode means ptr_mode). */
63
64machine_mode c_default_pointer_mode = VOIDmode;
65
7f4edbcb 66/* The following symbols are subsumed in the c_global_trees array, and
d125d268 67 listed here individually for documentation purposes.
7f4edbcb
BS
68
69 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
70
71 tree short_integer_type_node;
72 tree long_integer_type_node;
73 tree long_long_integer_type_node;
74
75 tree short_unsigned_type_node;
76 tree long_unsigned_type_node;
77 tree long_long_unsigned_type_node;
78
de7df9eb
JM
79 tree truthvalue_type_node;
80 tree truthvalue_false_node;
81 tree truthvalue_true_node;
7f4edbcb
BS
82
83 tree ptrdiff_type_node;
84
85 tree unsigned_char_type_node;
86 tree signed_char_type_node;
87 tree wchar_type_node;
7f4edbcb 88
b6baa67d
KVH
89 tree char16_type_node;
90 tree char32_type_node;
91
7f4edbcb
BS
92 tree float_type_node;
93 tree double_type_node;
94 tree long_double_type_node;
95
96 tree complex_integer_type_node;
97 tree complex_float_type_node;
98 tree complex_double_type_node;
99 tree complex_long_double_type_node;
100
9a8ce21f
JG
101 tree dfloat32_type_node;
102 tree dfloat64_type_node;
103 tree_dfloat128_type_node;
104
7f4edbcb
BS
105 tree intQI_type_node;
106 tree intHI_type_node;
107 tree intSI_type_node;
108 tree intDI_type_node;
109 tree intTI_type_node;
110
111 tree unsigned_intQI_type_node;
112 tree unsigned_intHI_type_node;
113 tree unsigned_intSI_type_node;
114 tree unsigned_intDI_type_node;
115 tree unsigned_intTI_type_node;
116
117 tree widest_integer_literal_type_node;
118 tree widest_unsigned_literal_type_node;
119
120 Nodes for types `void *' and `const void *'.
121
122 tree ptr_type_node, const_ptr_type_node;
123
124 Nodes for types `char *' and `const char *'.
125
126 tree string_type_node, const_string_type_node;
127
128 Type `char[SOMENUMBER]'.
129 Used when an array of char is needed and the size is irrelevant.
130
131 tree char_array_type_node;
132
7f4edbcb
BS
133 Type `wchar_t[SOMENUMBER]' or something like it.
134 Used when a wide string literal is created.
135
136 tree wchar_array_type_node;
137
b6baa67d
KVH
138 Type `char16_t[SOMENUMBER]' or something like it.
139 Used when a UTF-16 string literal is created.
140
141 tree char16_array_type_node;
142
143 Type `char32_t[SOMENUMBER]' or something like it.
144 Used when a UTF-32 string literal is created.
145
146 tree char32_array_type_node;
147
7f4edbcb
BS
148 Type `int ()' -- used for implicit declaration of functions.
149
150 tree default_function_type;
151
7f4edbcb
BS
152 A VOID_TYPE node, packaged in a TREE_LIST.
153
154 tree void_list_node;
155
684d9f3b 156 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
0ba8a114
NS
157 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
158 VAR_DECLS, but C++ does.)
63ad61ed 159
0ba8a114 160 tree function_name_decl_node;
684d9f3b 161 tree pretty_function_name_decl_node;
0ba8a114
NS
162 tree c99_function_name_decl_node;
163
164 Stack of nested function name VAR_DECLs.
35b1a6fa 165
0ba8a114 166 tree saved_function_name_decls;
63ad61ed 167
7f4edbcb
BS
168*/
169
170tree c_global_trees[CTI_MAX];
17211ab5 171\f
4078b403
NB
172/* Switches common to the C front ends. */
173
63973df3
NB
174/* Nonzero means don't output line number information. */
175
176char flag_no_line_commands;
177
178/* Nonzero causes -E output not to be done, but directives such as
179 #define that have side effects are still obeyed. */
180
181char flag_no_output;
182
183/* Nonzero means dump macros in some fashion. */
184
185char flag_dump_macros;
186
187/* Nonzero means pass #include lines through to the output. */
188
189char flag_dump_includes;
190
c0d578e6
GK
191/* Nonzero means process PCH files while preprocessing. */
192
193bool flag_pch_preprocess;
194
17211ab5
GK
195/* The file name to which we should write a precompiled header, or
196 NULL if no header will be written in this compile. */
197
198const char *pch_file;
199
3df89291
NB
200/* Nonzero if an ISO standard was selected. It rejects macros in the
201 user's namespace. */
202int flag_iso;
203
4078b403
NB
204/* C/ObjC language option variables. */
205
206
4078b403
NB
207/* Nonzero means allow type mismatches in conditional expressions;
208 just make their values `void'. */
209
210int flag_cond_mismatch;
211
212/* Nonzero means enable C89 Amendment 1 features. */
213
214int flag_isoc94;
215
48b0b196 216/* Nonzero means use the ISO C99 (or C11) dialect of C. */
4078b403
NB
217
218int flag_isoc99;
219
48b0b196 220/* Nonzero means use the ISO C11 dialect of C. */
2778d766 221
48b0b196 222int flag_isoc11;
2778d766 223
6614fd40 224/* Nonzero means that we have builtin functions, and main is an int. */
4078b403
NB
225
226int flag_hosted = 1;
227
4078b403
NB
228
229/* ObjC language option variables. */
230
231
4078b403
NB
232/* Tells the compiler that this is a special run. Do not perform any
233 compiling, instead we are to test some platform dependent features
234 and output a C header file with appropriate definitions. */
235
236int print_struct_values;
237
fa10beec 238/* Tells the compiler what is the constant string class for ObjC. */
4078b403
NB
239
240const char *constant_string_class_name;
241
4078b403
NB
242
243/* C++ language option variables. */
244
245
4078b403
NB
246/* Nonzero means generate separate instantiation control files and
247 juggle them at link time. */
248
249int flag_use_repository;
250
129211bc 251/* The C++ dialect being used. Default set in c_common_post_options. */
966541e3 252
129211bc 253enum cxx_dialect cxx_dialect = cxx_unset;
966541e3 254
4afe7ad7 255/* Maximum template instantiation depth. This limit exists to limit the
3ff60975 256 time it takes to notice excessively recursive template instantiations.
4078b403 257
3ff60975
JM
258 The default is lower than the 1024 recommended by the C++0x standard
259 because G++ runs out of stack before 1024 with highly recursive template
260 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
261
262int max_tinst_depth = 900;
4078b403 263
f09f1de5
MM
264/* The elements of `ridpointers' are identifier nodes for the reserved
265 type names and storage classes. It is indexed by a RID_... value. */
266tree *ridpointers;
267
c2255bc4 268tree (*make_fname_decl) (location_t, tree, int);
2ce07e2d 269
7d882b83
ILT
270/* Nonzero means don't warn about problems that occur when the code is
271 executed. */
272int c_inhibit_evaluation_warnings;
e78a3b42 273
5386338c
JM
274/* Whether we are building a boolean conversion inside
275 convert_for_assignment, or some other late binary operation. If
276 build_binary_op is called for C (from code shared by C and C++) in
277 this case, then the operands have already been folded and the
278 result will not be folded again, so C_MAYBE_CONST_EXPR should not
279 be generated. */
280bool in_late_binary_op;
281
148e4216
JM
282/* Whether lexing has been completed, so subsequent preprocessor
283 errors should use the compiler's input_location. */
284bool done_lexing = false;
285
ec5c56db 286/* Information about how a function name is generated. */
0ba8a114
NS
287struct fname_var_t
288{
8b60264b
KG
289 tree *const decl; /* pointer to the VAR_DECL. */
290 const unsigned rid; /* RID number for the identifier. */
291 const int pretty; /* How pretty is it? */
0ba8a114
NS
292};
293
ec5c56db 294/* The three ways of getting then name of the current function. */
0ba8a114
NS
295
296const struct fname_var_t fname_vars[] =
297{
ec5c56db 298 /* C99 compliant __func__, must be first. */
0ba8a114 299 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
ec5c56db 300 /* GCC __FUNCTION__ compliant. */
0ba8a114 301 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
ec5c56db 302 /* GCC __PRETTY_FUNCTION__ compliant. */
0ba8a114
NS
303 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
304 {NULL, 0, 0},
305};
306
c98cd5bf
JM
307/* Global visibility options. */
308struct visibility_flags visibility_options;
309
3aa3c9fc 310static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
62e4eb35 311static tree check_case_value (location_t, tree);
9d548dfb 312static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
4724b3de 313
35b1a6fa
AJ
314static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
315static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
316static tree handle_common_attribute (tree *, tree, tree, int, bool *);
317static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52bf96d2
JH
318static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
319static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
e664c61c
KS
320static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
321 int, bool *);
77bc5132
JJ
322static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
323 int, bool *);
ce6923c5
MP
324static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
325 bool *);
5434dc07 326static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 327static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
86631ea3 328static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
185c9e56 329static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
46a4da10 330static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
331static tree handle_always_inline_attribute (tree *, tree, tree, int,
332 bool *);
d752cfdb
JJ
333static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
334static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0691d1d4 335static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
d2af6a68 336static tree handle_error_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
337static tree handle_used_attribute (tree *, tree, tree, int, bool *);
338static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
ce91e74c
JH
339static tree handle_externally_visible_attribute (tree *, tree, tree, int,
340 bool *);
7861b648
AK
341static tree handle_no_reorder_attribute (tree *, tree, tree, int,
342 bool *);
35b1a6fa
AJ
343static tree handle_const_attribute (tree *, tree, tree, int, bool *);
344static tree handle_transparent_union_attribute (tree *, tree, tree,
345 int, bool *);
346static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
347static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
348static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
349static tree handle_section_attribute (tree *, tree, tree, int, bool *);
350static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
351static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4bb794e2 352static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
ba885ec5
NS
353static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
354static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 355static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
a0203ca7 356static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
35b1a6fa
AJ
357static tree handle_visibility_attribute (tree *, tree, tree, int,
358 bool *);
359static tree handle_tls_model_attribute (tree *, tree, tree, int,
360 bool *);
361static tree handle_no_instrument_function_attribute (tree *, tree,
362 tree, int, bool *);
363static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
6e9a3221 364static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
365static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
366 bool *);
367static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
0a35513e
AH
368static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
369static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
dcd6de6d 370static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
371static tree handle_deprecated_attribute (tree *, tree, tree, int,
372 bool *);
373static tree handle_vector_size_attribute (tree *, tree, tree, int,
374 bool *);
375static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
376static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
377static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
72954a4f
JM
378static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
379 bool *);
3d091dac 380static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5d32c25 381static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
51bc54a6 382static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
8fcbce72
JJ
383static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
384static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
5779e713 385static tree handle_target_attribute (tree *, tree, tree, int, bool *);
ab442df7 386static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
0a35513e 387static tree ignore_attribute (tree *, tree, tree, int, bool *);
7458026b 388static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
0b7b376d 389static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
2a99e5e6 390static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
826cacfe 391static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
392static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
393 bool *);
394static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
395 bool *);
976d5a22 396static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
d5e254e1
IE
397static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
398static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
399static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
35b1a6fa 400
94a0dd7b 401static void check_function_nonnull (tree, int, tree *);
35b1a6fa
AJ
402static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
403static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
404static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
d07605f5 405static int resort_field_decl_cmp (const void *, const void *);
b34c7881 406
eea1139b
ILT
407/* Reserved words. The third field is a mask: keywords are disabled
408 if they match the mask.
409
410 Masks for languages:
411 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
412 C --std=c99: D_CXXONLY | D_OBJC
413 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
414 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
415 C++ --std=c0x: D_CONLY | D_OBJC
416 ObjC++ is like C++ except that D_OBJC is not set
417
418 If -fno-asm is used, D_ASM is added to the mask. If
419 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
420 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
1973201f 421 In C with -Wc++-compat, we warn if D_CXXWARN is set.
eea1139b 422
1973201f
NP
423 Note the complication of the D_CXX_OBJC keywords. These are
424 reserved words such as 'class'. In C++, 'class' is a reserved
425 word. In Objective-C++ it is too. In Objective-C, it is a
426 reserved word too, but only if it follows an '@' sign.
427*/
eea1139b
ILT
428const struct c_common_resword c_common_reswords[] =
429{
d19fa6b5
JM
430 { "_Alignas", RID_ALIGNAS, D_CONLY },
431 { "_Alignof", RID_ALIGNOF, D_CONLY },
267bac10 432 { "_Atomic", RID_ATOMIC, D_CONLY },
eea1139b
ILT
433 { "_Bool", RID_BOOL, D_CONLY },
434 { "_Complex", RID_COMPLEX, 0 },
939b37da
BI
435 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
436 { "_Cilk_sync", RID_CILK_SYNC, 0 },
9a771876 437 { "_Cilk_for", RID_CILK_FOR, 0 },
fa5da7de 438 { "_Imaginary", RID_IMAGINARY, D_CONLY },
eea1139b
ILT
439 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
440 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
441 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
442 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
443 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
444 { "_Sat", RID_SAT, D_CONLY | D_EXT },
32912286 445 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
bbceee64 446 { "_Noreturn", RID_NORETURN, D_CONLY },
433cc7b0 447 { "_Generic", RID_GENERIC, D_CONLY },
582d9b50 448 { "_Thread_local", RID_THREAD, D_CONLY },
eea1139b
ILT
449 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
450 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
451 { "__alignof", RID_ALIGNOF, 0 },
452 { "__alignof__", RID_ALIGNOF, 0 },
453 { "__asm", RID_ASM, 0 },
454 { "__asm__", RID_ASM, 0 },
455 { "__attribute", RID_ATTRIBUTE, 0 },
456 { "__attribute__", RID_ATTRIBUTE, 0 },
38b7bc7f 457 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
4daba884 458 { "__bases", RID_BASES, D_CXXONLY },
74893f25
RH
459 { "__builtin_call_with_static_chain",
460 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
eea1139b 461 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
d4a83c10 462 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
9e1a8dd1 463 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
eea1139b
ILT
464 { "__builtin_offsetof", RID_OFFSETOF, 0 },
465 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
466 { "__builtin_va_arg", RID_VA_ARG, 0 },
467 { "__complex", RID_COMPLEX, 0 },
468 { "__complex__", RID_COMPLEX, 0 },
469 { "__const", RID_CONST, 0 },
470 { "__const__", RID_CONST, 0 },
471 { "__decltype", RID_DECLTYPE, D_CXXONLY },
4daba884 472 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
eea1139b
ILT
473 { "__extension__", RID_EXTENSION, 0 },
474 { "__func__", RID_C99_FUNCTION_NAME, 0 },
475 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
476 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
477 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
478 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
479 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
480 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
481 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
482 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
3c0d13bf
PC
483 { "__imag", RID_IMAGPART, 0 },
484 { "__imag__", RID_IMAGPART, 0 },
485 { "__inline", RID_INLINE, 0 },
486 { "__inline__", RID_INLINE, 0 },
eea1139b
ILT
487 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
488 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
489 { "__is_class", RID_IS_CLASS, D_CXXONLY },
eea1139b
ILT
490 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
491 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
b3908fcc 492 { "__is_final", RID_IS_FINAL, D_CXXONLY },
3c0d13bf 493 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
eea1139b
ILT
494 { "__is_pod", RID_IS_POD, D_CXXONLY },
495 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
c32097d8
JM
496 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
497 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
dd5d5481
JM
498 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
499 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
b752325e 500 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
eea1139b 501 { "__is_union", RID_IS_UNION, D_CXXONLY },
eea1139b
ILT
502 { "__label__", RID_LABEL, 0 },
503 { "__null", RID_NULL, 0 },
504 { "__real", RID_REALPART, 0 },
505 { "__real__", RID_REALPART, 0 },
506 { "__restrict", RID_RESTRICT, 0 },
507 { "__restrict__", RID_RESTRICT, 0 },
508 { "__signed", RID_SIGNED, 0 },
509 { "__signed__", RID_SIGNED, 0 },
510 { "__thread", RID_THREAD, 0 },
0a35513e
AH
511 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
512 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
513 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
eea1139b
ILT
514 { "__typeof", RID_TYPEOF, 0 },
515 { "__typeof__", RID_TYPEOF, 0 },
3c0d13bf 516 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
eea1139b
ILT
517 { "__volatile", RID_VOLATILE, 0 },
518 { "__volatile__", RID_VOLATILE, 0 },
e28d52cf 519 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
f4e8a943 520 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
eea1139b
ILT
521 { "asm", RID_ASM, D_ASM },
522 { "auto", RID_AUTO, 0 },
03c3034e 523 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
524 { "break", RID_BREAK, 0 },
525 { "case", RID_CASE, 0 },
2696a995 526 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
eea1139b 527 { "char", RID_CHAR, 0 },
2696a995
KG
528 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
529 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
530 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
eea1139b 531 { "const", RID_CONST, 0 },
7ecbca9d 532 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
eea1139b
ILT
533 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
534 { "continue", RID_CONTINUE, 0 },
2696a995 535 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
eea1139b 536 { "default", RID_DEFAULT, 0 },
2696a995 537 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
538 { "do", RID_DO, 0 },
539 { "double", RID_DOUBLE, 0 },
540 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
541 { "else", RID_ELSE, 0 },
542 { "enum", RID_ENUM, 0 },
2696a995
KG
543 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
544 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
eea1139b 545 { "extern", RID_EXTERN, 0 },
2696a995 546 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
547 { "float", RID_FLOAT, 0 },
548 { "for", RID_FOR, 0 },
2696a995 549 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
550 { "goto", RID_GOTO, 0 },
551 { "if", RID_IF, 0 },
552 { "inline", RID_INLINE, D_EXT89 },
553 { "int", RID_INT, 0 },
554 { "long", RID_LONG, 0 },
555 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
2696a995
KG
556 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
557 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
0a766368 558 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
14c2101d 559 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
2696a995
KG
560 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
561 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
562 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
563 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
eea1139b
ILT
564 { "register", RID_REGISTER, 0 },
565 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
566 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
567 { "return", RID_RETURN, 0 },
568 { "short", RID_SHORT, 0 },
569 { "signed", RID_SIGNED, 0 },
570 { "sizeof", RID_SIZEOF, 0 },
571 { "static", RID_STATIC, 0 },
572 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
573 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
574 { "struct", RID_STRUCT, 0 },
575 { "switch", RID_SWITCH, 0 },
2696a995
KG
576 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
577 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
b1db7f91 578 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
2696a995
KG
579 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
580 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
581 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
eea1139b 582 { "typedef", RID_TYPEDEF, 0 },
2696a995
KG
583 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
584 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
585 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
586 { "union", RID_UNION, 0 },
587 { "unsigned", RID_UNSIGNED, 0 },
2696a995
KG
588 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
589 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
590 { "void", RID_VOID, 0 },
591 { "volatile", RID_VOLATILE, 0 },
592 { "wchar_t", RID_WCHAR, D_CXXONLY },
593 { "while", RID_WHILE, 0 },
594 /* These Objective-C keywords are recognized only immediately after
595 an '@'. */
596 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
597 { "defs", RID_AT_DEFS, D_OBJC },
598 { "encode", RID_AT_ENCODE, D_OBJC },
599 { "end", RID_AT_END, D_OBJC },
600 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
601 { "interface", RID_AT_INTERFACE, D_OBJC },
602 { "protocol", RID_AT_PROTOCOL, D_OBJC },
603 { "selector", RID_AT_SELECTOR, D_OBJC },
604 { "finally", RID_AT_FINALLY, D_OBJC },
605 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
92902b1b
IS
606 { "optional", RID_AT_OPTIONAL, D_OBJC },
607 { "required", RID_AT_REQUIRED, D_OBJC },
668ea4b1 608 { "property", RID_AT_PROPERTY, D_OBJC },
c37d8c30 609 { "package", RID_AT_PACKAGE, D_OBJC },
da57d1b9
NP
610 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
611 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
eea1139b
ILT
612 /* These are recognized only in protocol-qualifier context
613 (see above) */
614 { "bycopy", RID_BYCOPY, D_OBJC },
615 { "byref", RID_BYREF, D_OBJC },
616 { "in", RID_IN, D_OBJC },
617 { "inout", RID_INOUT, D_OBJC },
618 { "oneway", RID_ONEWAY, D_OBJC },
619 { "out", RID_OUT, D_OBJC },
668ea4b1 620 /* These are recognized inside a property attribute list */
200290f2
NP
621 { "assign", RID_ASSIGN, D_OBJC },
622 { "copy", RID_COPY, D_OBJC },
668ea4b1 623 { "getter", RID_GETTER, D_OBJC },
200290f2
NP
624 { "nonatomic", RID_NONATOMIC, D_OBJC },
625 { "readonly", RID_READONLY, D_OBJC },
626 { "readwrite", RID_READWRITE, D_OBJC },
627 { "retain", RID_RETAIN, D_OBJC },
668ea4b1 628 { "setter", RID_SETTER, D_OBJC },
eea1139b
ILT
629};
630
631const unsigned int num_c_common_reswords =
632 sizeof c_common_reswords / sizeof (struct c_common_resword);
633
d5e254e1
IE
634/* Table of machine-independent attributes common to all C-like languages.
635
636 All attributes referencing arguments should be additionally processed
637 in chkp_copy_function_type_adding_bounds for correct instrumentation
638 by Pointer Bounds Checker.
639 Current list of processed common attributes: nonnull. */
349ae713
NB
640const struct attribute_spec c_common_attribute_table[] =
641{
62d784f7
KT
642 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
643 affects_type_identity } */
349ae713 644 { "packed", 0, 0, false, false, false,
62d784f7 645 handle_packed_attribute , false},
349ae713 646 { "nocommon", 0, 0, true, false, false,
62d784f7 647 handle_nocommon_attribute, false},
349ae713 648 { "common", 0, 0, true, false, false,
62d784f7 649 handle_common_attribute, false },
349ae713
NB
650 /* FIXME: logically, noreturn attributes should be listed as
651 "false, true, true" and apply to function types. But implementing this
652 would require all the places in the compiler that use TREE_THIS_VOLATILE
653 on a decl to identify non-returning functions to be located and fixed
654 to check the function type instead. */
655 { "noreturn", 0, 0, true, false, false,
62d784f7 656 handle_noreturn_attribute, false },
349ae713 657 { "volatile", 0, 0, true, false, false,
62d784f7 658 handle_noreturn_attribute, false },
5434dc07
MD
659 { "stack_protect", 0, 0, true, false, false,
660 handle_stack_protect_attribute, false },
349ae713 661 { "noinline", 0, 0, true, false, false,
62d784f7 662 handle_noinline_attribute, false },
86631ea3 663 { "noclone", 0, 0, true, false, false,
62d784f7 664 handle_noclone_attribute, false },
185c9e56
ML
665 { "no_icf", 0, 0, true, false, false,
666 handle_noicf_attribute, false },
46a4da10 667 { "leaf", 0, 0, true, false, false,
62d784f7 668 handle_leaf_attribute, false },
349ae713 669 { "always_inline", 0, 0, true, false, false,
62d784f7 670 handle_always_inline_attribute, false },
4eb7fd83 671 { "gnu_inline", 0, 0, true, false, false,
62d784f7 672 handle_gnu_inline_attribute, false },
d752cfdb 673 { "artificial", 0, 0, true, false, false,
62d784f7 674 handle_artificial_attribute, false },
0691d1d4 675 { "flatten", 0, 0, true, false, false,
62d784f7 676 handle_flatten_attribute, false },
349ae713 677 { "used", 0, 0, true, false, false,
62d784f7 678 handle_used_attribute, false },
349ae713 679 { "unused", 0, 0, false, false, false,
62d784f7 680 handle_unused_attribute, false },
ce91e74c 681 { "externally_visible", 0, 0, true, false, false,
62d784f7 682 handle_externally_visible_attribute, false },
7861b648
AK
683 { "no_reorder", 0, 0, true, false, false,
684 handle_no_reorder_attribute, false },
349ae713
NB
685 /* The same comments as for noreturn attributes apply to const ones. */
686 { "const", 0, 0, true, false, false,
62d784f7 687 handle_const_attribute, false },
349ae713 688 { "transparent_union", 0, 0, false, false, false,
62d784f7 689 handle_transparent_union_attribute, false },
fc8600f9 690 { "constructor", 0, 1, true, false, false,
62d784f7 691 handle_constructor_attribute, false },
fc8600f9 692 { "destructor", 0, 1, true, false, false,
62d784f7 693 handle_destructor_attribute, false },
349ae713 694 { "mode", 1, 1, false, true, false,
62d784f7 695 handle_mode_attribute, false },
349ae713 696 { "section", 1, 1, true, false, false,
62d784f7 697 handle_section_attribute, false },
349ae713 698 { "aligned", 0, 1, false, false, false,
62d784f7 699 handle_aligned_attribute, false },
349ae713 700 { "weak", 0, 0, true, false, false,
62d784f7 701 handle_weak_attribute, false },
4bb794e2
ST
702 { "noplt", 0, 0, true, false, false,
703 handle_noplt_attribute, false },
ba885ec5 704 { "ifunc", 1, 1, true, false, false,
62d784f7 705 handle_ifunc_attribute, false },
349ae713 706 { "alias", 1, 1, true, false, false,
62d784f7 707 handle_alias_attribute, false },
a0203ca7 708 { "weakref", 0, 1, true, false, false,
62d784f7 709 handle_weakref_attribute, false },
349ae713 710 { "no_instrument_function", 0, 0, true, false, false,
62d784f7
KT
711 handle_no_instrument_function_attribute,
712 false },
349ae713 713 { "malloc", 0, 0, true, false, false,
62d784f7 714 handle_malloc_attribute, false },
6e9a3221 715 { "returns_twice", 0, 0, true, false, false,
62d784f7 716 handle_returns_twice_attribute, false },
349ae713 717 { "no_stack_limit", 0, 0, true, false, false,
62d784f7 718 handle_no_limit_stack_attribute, false },
349ae713 719 { "pure", 0, 0, true, false, false,
62d784f7 720 handle_pure_attribute, false },
0a35513e
AH
721 { "transaction_callable", 0, 0, false, true, false,
722 handle_tm_attribute, false },
723 { "transaction_unsafe", 0, 0, false, true, false,
724 handle_tm_attribute, false },
725 { "transaction_safe", 0, 0, false, true, false,
726 handle_tm_attribute, false },
727 { "transaction_may_cancel_outer", 0, 0, false, true, false,
728 handle_tm_attribute, false },
729 /* ??? These two attributes didn't make the transition from the
730 Intel language document to the multi-vendor language document. */
731 { "transaction_pure", 0, 0, false, true, false,
732 handle_tm_attribute, false },
733 { "transaction_wrap", 1, 1, true, false, false,
734 handle_tm_wrap_attribute, false },
dcd6de6d
ZD
735 /* For internal use (marking of builtins) only. The name contains space
736 to prevent its usage in source code. */
737 { "no vops", 0, 0, true, false, false,
62d784f7 738 handle_novops_attribute, false },
9b86d6bb 739 { "deprecated", 0, 1, false, false, false,
62d784f7 740 handle_deprecated_attribute, false },
349ae713 741 { "vector_size", 1, 1, false, true, false,
62d784f7 742 handle_vector_size_attribute, false },
d7afec4b 743 { "visibility", 1, 1, false, false, false,
62d784f7 744 handle_visibility_attribute, false },
dce81a1a 745 { "tls_model", 1, 1, true, false, false,
62d784f7 746 handle_tls_model_attribute, false },
b34c7881 747 { "nonnull", 0, -1, false, true, true,
62d784f7 748 handle_nonnull_attribute, false },
39f2f3c8 749 { "nothrow", 0, 0, true, false, false,
62d784f7
KT
750 handle_nothrow_attribute, false },
751 { "may_alias", 0, 0, false, true, false, NULL, false },
0bfa5f65 752 { "cleanup", 1, 1, true, false, false,
62d784f7 753 handle_cleanup_attribute, false },
72954a4f 754 { "warn_unused_result", 0, 0, false, true, true,
62d784f7 755 handle_warn_unused_result_attribute, false },
254986c7 756 { "sentinel", 0, 1, false, true, true,
62d784f7 757 handle_sentinel_attribute, false },
b5d32c25
KG
758 /* For internal use (marking of builtins) only. The name contains space
759 to prevent its usage in source code. */
760 { "type generic", 0, 0, false, true, true,
62d784f7 761 handle_type_generic_attribute, false },
51bc54a6 762 { "alloc_size", 1, 2, false, true, true,
62d784f7 763 handle_alloc_size_attribute, false },
52bf96d2 764 { "cold", 0, 0, true, false, false,
62d784f7 765 handle_cold_attribute, false },
52bf96d2 766 { "hot", 0, 0, true, false, false,
62d784f7 767 handle_hot_attribute, false },
77bc5132
JJ
768 { "no_address_safety_analysis",
769 0, 0, true, false, false,
770 handle_no_address_safety_analysis_attribute,
771 false },
e664c61c
KS
772 { "no_sanitize_address", 0, 0, true, false, false,
773 handle_no_sanitize_address_attribute,
774 false },
de35aa66
MS
775 { "no_sanitize_thread", 0, 0, true, false, false,
776 handle_no_sanitize_address_attribute,
777 false },
ce6923c5
MP
778 { "no_sanitize_undefined", 0, 0, true, false, false,
779 handle_no_sanitize_undefined_attribute,
780 false },
d2af6a68 781 { "warning", 1, 1, true, false, false,
62d784f7 782 handle_error_attribute, false },
d2af6a68 783 { "error", 1, 1, true, false, false,
62d784f7 784 handle_error_attribute, false },
5779e713 785 { "target", 1, -1, true, false, false,
62d784f7 786 handle_target_attribute, false },
ab442df7 787 { "optimize", 1, -1, true, false, false,
62d784f7 788 handle_optimize_attribute, false },
0a35513e
AH
789 /* For internal use only. The leading '*' both prevents its usage in
790 source code and signals that it may be overridden by machine tables. */
791 { "*tm regparm", 0, 0, false, true, true,
792 ignore_attribute, false },
7458026b 793 { "no_split_stack", 0, 0, true, false, false,
62d784f7 794 handle_no_split_stack_attribute, false },
0b7b376d
RG
795 /* For internal use (marking of builtins and runtime functions) only.
796 The name contains space to prevent its usage in source code. */
797 { "fn spec", 1, 1, false, true, true,
62d784f7 798 handle_fnspec_attribute, false },
2a99e5e6
LL
799 { "warn_unused", 0, 0, false, false, false,
800 handle_warn_unused_attribute, false },
826cacfe
MG
801 { "returns_nonnull", 0, 0, false, true, true,
802 handle_returns_nonnull_attribute, false },
acf0174b
JJ
803 { "omp declare simd", 0, -1, true, false, false,
804 handle_omp_declare_simd_attribute, false },
41958c28
BI
805 { "cilk simd function", 0, -1, true, false, false,
806 handle_omp_declare_simd_attribute, false },
acf0174b
JJ
807 { "omp declare target", 0, 0, true, false, false,
808 handle_omp_declare_target_attribute, false },
8fcbce72
JJ
809 { "alloc_align", 1, 1, false, true, true,
810 handle_alloc_align_attribute, false },
811 { "assume_aligned", 1, 2, false, true, true,
812 handle_assume_aligned_attribute, false },
976d5a22
TT
813 { "designated_init", 0, 0, false, true, false,
814 handle_designated_init_attribute, false },
d5e254e1
IE
815 { "bnd_variable_size", 0, 0, true, false, false,
816 handle_bnd_variable_size_attribute, false },
817 { "bnd_legacy", 0, 0, true, false, false,
818 handle_bnd_legacy, false },
819 { "bnd_instrument", 0, 0, true, false, false,
820 handle_bnd_instrument, false },
62d784f7 821 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
822};
823
824/* Give the specifications for the format attributes, used by C and all
d5e254e1 825 descendants.
349ae713 826
d5e254e1
IE
827 All attributes referencing arguments should be additionally processed
828 in chkp_copy_function_type_adding_bounds for correct instrumentation
829 by Pointer Bounds Checker.
830 Current list of processed format attributes: format, format_arg. */
349ae713
NB
831const struct attribute_spec c_common_format_attribute_table[] =
832{
62d784f7
KT
833 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
834 affects_type_identity } */
349ae713 835 { "format", 3, 3, false, true, true,
62d784f7 836 handle_format_attribute, false },
349ae713 837 { "format_arg", 1, 1, false, true, true,
62d784f7
KT
838 handle_format_arg_attribute, false },
839 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
840};
841
36c5e70a 842/* Return identifier for address space AS. */
3ef0694c 843
36c5e70a
BE
844const char *
845c_addr_space_name (addr_space_t as)
846{
3ef0694c
UW
847 int rid = RID_FIRST_ADDR_SPACE + as;
848 gcc_assert (ridpointers [rid]);
849 return IDENTIFIER_POINTER (ridpointers [rid]);
36c5e70a
BE
850}
851
ec5c56db 852/* Push current bindings for the function name VAR_DECLS. */
7da551a2
RS
853
854void
35b1a6fa 855start_fname_decls (void)
7da551a2 856{
0ba8a114
NS
857 unsigned ix;
858 tree saved = NULL_TREE;
35b1a6fa 859
0ba8a114
NS
860 for (ix = 0; fname_vars[ix].decl; ix++)
861 {
862 tree decl = *fname_vars[ix].decl;
7da551a2 863
0ba8a114
NS
864 if (decl)
865 {
c62c040f
RG
866 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
867 saved);
0ba8a114
NS
868 *fname_vars[ix].decl = NULL_TREE;
869 }
870 }
871 if (saved || saved_function_name_decls)
872 /* Normally they'll have been NULL, so only push if we've got a
873 stack, or they are non-NULL. */
874 saved_function_name_decls = tree_cons (saved, NULL_TREE,
875 saved_function_name_decls);
876}
877
325c3691
RH
878/* Finish up the current bindings, adding them into the current function's
879 statement tree. This must be done _before_ finish_stmt_tree is called.
880 If there is no current function, we must be at file scope and no statements
881 are involved. Pop the previous bindings. */
0ba8a114
NS
882
883void
35b1a6fa 884finish_fname_decls (void)
0ba8a114
NS
885{
886 unsigned ix;
325c3691 887 tree stmts = NULL_TREE;
0ba8a114
NS
888 tree stack = saved_function_name_decls;
889
890 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
325c3691 891 append_to_statement_list (TREE_VALUE (stack), &stmts);
35b1a6fa 892
325c3691 893 if (stmts)
0ba8a114 894 {
325c3691 895 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
bfaba7a9 896
325c3691
RH
897 if (TREE_CODE (*bodyp) == BIND_EXPR)
898 bodyp = &BIND_EXPR_BODY (*bodyp);
6cce57b0 899
86ad3aa9 900 append_to_statement_list_force (*bodyp, &stmts);
325c3691 901 *bodyp = stmts;
0ba8a114 902 }
35b1a6fa 903
0ba8a114
NS
904 for (ix = 0; fname_vars[ix].decl; ix++)
905 *fname_vars[ix].decl = NULL_TREE;
35b1a6fa 906
0ba8a114 907 if (stack)
7da551a2 908 {
ec5c56db 909 /* We had saved values, restore them. */
0ba8a114
NS
910 tree saved;
911
912 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
913 {
914 tree decl = TREE_PURPOSE (saved);
915 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
35b1a6fa 916
0ba8a114
NS
917 *fname_vars[ix].decl = decl;
918 }
919 stack = TREE_CHAIN (stack);
7da551a2 920 }
0ba8a114
NS
921 saved_function_name_decls = stack;
922}
923
6cce57b0 924/* Return the text name of the current function, suitably prettified
0d0bc036 925 by PRETTY_P. Return string must be freed by caller. */
0ba8a114
NS
926
927const char *
35b1a6fa 928fname_as_string (int pretty_p)
0ba8a114 929{
47ab33b2 930 const char *name = "top level";
0d0bc036 931 char *namep;
46c2514e
TT
932 int vrb = 2, len;
933 cpp_string cstr = { 0, 0 }, strname;
47ab33b2 934
3f75a254 935 if (!pretty_p)
47ab33b2
MA
936 {
937 name = "";
938 vrb = 0;
939 }
940
941 if (current_function_decl)
ae2bcd98 942 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
47ab33b2 943
46c2514e 944 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
0d0bc036 945
46c2514e
TT
946 namep = XNEWVEC (char, len);
947 snprintf (namep, len, "\"%s\"", name);
948 strname.text = (unsigned char *) namep;
949 strname.len = len - 1;
0d0bc036 950
b6baa67d 951 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
46c2514e
TT
952 {
953 XDELETEVEC (namep);
954 return (const char *) cstr.text;
0d0bc036 955 }
0d0bc036
AH
956
957 return namep;
0ba8a114
NS
958}
959
0ba8a114
NS
960/* Return the VAR_DECL for a const char array naming the current
961 function. If the VAR_DECL has not yet been created, create it
962 now. RID indicates how it should be formatted and IDENTIFIER_NODE
963 ID is its name (unfortunately C and C++ hold the RID values of
964 keywords in different places, so we can't derive RID from ID in
3ba09659
AH
965 this language independent code. LOC is the location of the
966 function. */
0ba8a114
NS
967
968tree
3ba09659 969fname_decl (location_t loc, unsigned int rid, tree id)
0ba8a114
NS
970{
971 unsigned ix;
972 tree decl = NULL_TREE;
973
974 for (ix = 0; fname_vars[ix].decl; ix++)
975 if (fname_vars[ix].rid == rid)
976 break;
977
978 decl = *fname_vars[ix].decl;
979 if (!decl)
7da551a2 980 {
8d3e27d1
DJ
981 /* If a tree is built here, it would normally have the lineno of
982 the current statement. Later this tree will be moved to the
983 beginning of the function and this line number will be wrong.
984 To avoid this problem set the lineno to 0 here; that prevents
4b7e68e7 985 it from appearing in the RTL. */
325c3691 986 tree stmts;
3c20847b 987 location_t saved_location = input_location;
3c20847b 988 input_location = UNKNOWN_LOCATION;
35b1a6fa 989
325c3691 990 stmts = push_stmt_list ();
c2255bc4 991 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
325c3691
RH
992 stmts = pop_stmt_list (stmts);
993 if (!IS_EMPTY_STMT (stmts))
994 saved_function_name_decls
995 = tree_cons (decl, stmts, saved_function_name_decls);
0ba8a114 996 *fname_vars[ix].decl = decl;
3c20847b 997 input_location = saved_location;
7da551a2 998 }
0ba8a114 999 if (!ix && !current_function_decl)
3ba09659 1000 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
6cce57b0 1001
0ba8a114 1002 return decl;
7da551a2
RS
1003}
1004
b84a3874 1005/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b30f223b
RS
1006
1007tree
35b1a6fa 1008fix_string_type (tree value)
b30f223b 1009{
b84a3874
RH
1010 int length = TREE_STRING_LENGTH (value);
1011 int nchars;
c162c75e
MA
1012 tree e_type, i_type, a_type;
1013
b57062ca 1014 /* Compute the number of elements, for the array type. */
b6baa67d
KVH
1015 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1016 {
1017 nchars = length;
1018 e_type = char_type_node;
1019 }
1020 else if (TREE_TYPE (value) == char16_array_type_node)
1021 {
1022 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1023 e_type = char16_type_node;
1024 }
1025 else if (TREE_TYPE (value) == char32_array_type_node)
1026 {
1027 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1028 e_type = char32_type_node;
1029 }
1030 else
1031 {
1032 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1033 e_type = wchar_type_node;
1034 }
b30f223b 1035
89a42ac8
ZW
1036 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1037 limit in C++98 Annex B is very large (65536) and is not normative,
1038 so we do not diagnose it (warn_overlength_strings is forced off
1039 in c_common_post_options). */
1040 if (warn_overlength_strings)
1041 {
1042 const int nchars_max = flag_isoc99 ? 4095 : 509;
1043 const int relevant_std = flag_isoc99 ? 99 : 90;
1044 if (nchars - 1 > nchars_max)
1045 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1046 separate the %d from the 'C'. 'ISO' should not be
1047 translated, but it may be moved after 'C%d' in languages
1048 where modifiers follow nouns. */
509c9d60 1049 pedwarn (input_location, OPT_Woverlength_strings,
fcf73884 1050 "string length %qd is greater than the length %qd "
89a42ac8
ZW
1051 "ISO C%d compilers are required to support",
1052 nchars - 1, nchars_max, relevant_std);
1053 }
1326a48b 1054
cfb10bd3
GDR
1055 /* Create the array type for the string constant. The ISO C++
1056 standard says that a string literal has type `const char[N]' or
1057 `const wchar_t[N]'. We use the same logic when invoked as a C
1058 front-end with -Wwrite-strings.
1059 ??? We should change the type of an expression depending on the
1060 state of a warning flag. We should just be warning -- see how
1061 this is handled in the C++ front-end for the deprecated implicit
1062 conversion from string literals to `char*' or `wchar_t*'.
c162c75e
MA
1063
1064 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1065 array type being the unqualified version of that type.
1066 Therefore, if we are constructing an array of const char, we must
1067 construct the matching unqualified array type first. The C front
1068 end does not require this, but it does no harm, so we do it
1069 unconditionally. */
c62c040f 1070 i_type = build_index_type (size_int (nchars - 1));
c162c75e 1071 a_type = build_array_type (e_type, i_type);
cfb10bd3 1072 if (c_dialect_cxx() || warn_write_strings)
46df2823 1073 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
d9cf7c82 1074
c162c75e 1075 TREE_TYPE (value) = a_type;
ccd4c832 1076 TREE_CONSTANT (value) = 1;
3521b33c 1077 TREE_READONLY (value) = 1;
b30f223b
RS
1078 TREE_STATIC (value) = 1;
1079 return value;
1080}
1081\f
02614448
ILT
1082/* If DISABLE is true, stop issuing warnings. This is used when
1083 parsing code that we know will not be executed. This function may
1084 be called multiple times, and works as a stack. */
1085
1086static void
1087c_disable_warnings (bool disable)
1088{
1089 if (disable)
1090 {
1091 ++c_inhibit_evaluation_warnings;
1092 fold_defer_overflow_warnings ();
1093 }
1094}
1095
1096/* If ENABLE is true, reenable issuing warnings. */
1097
1098static void
1099c_enable_warnings (bool enable)
1100{
1101 if (enable)
1102 {
1103 --c_inhibit_evaluation_warnings;
1104 fold_undefer_and_ignore_overflow_warnings ();
1105 }
1106}
1107
928c19bb
JM
1108/* Fully fold EXPR, an expression that was not folded (beyond integer
1109 constant expressions and null pointer constants) when being built
1110 up. If IN_INIT, this is in a static initializer and certain
1111 changes are made to the folding done. Clear *MAYBE_CONST if
1112 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1113 expression because it contains an evaluated operator (in C99) or an
1114 operator outside of sizeof returning an integer constant (in C90)
1115 not permitted in constant expressions, or because it contains an
1116 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1117 set to true by callers before calling this function.) Return the
1118 folded expression. Function arguments have already been folded
1119 before calling this function, as have the contents of SAVE_EXPR,
1120 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1121 C_MAYBE_CONST_EXPR. */
1122
1123tree
1124c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1125{
1126 tree ret;
8ce94e44 1127 tree eptype = NULL_TREE;
928c19bb
JM
1128 bool dummy = true;
1129 bool maybe_const_itself = true;
db3927fb 1130 location_t loc = EXPR_LOCATION (expr);
928c19bb
JM
1131
1132 /* This function is not relevant to C++ because C++ folds while
1133 parsing, and may need changes to be correct for C++ when C++
1134 stops folding while parsing. */
1135 if (c_dialect_cxx ())
1136 gcc_unreachable ();
1137
1138 if (!maybe_const)
1139 maybe_const = &dummy;
8ce94e44
JM
1140 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1141 {
1142 eptype = TREE_TYPE (expr);
1143 expr = TREE_OPERAND (expr, 0);
1144 }
928c19bb 1145 ret = c_fully_fold_internal (expr, in_init, maybe_const,
3aa3c9fc 1146 &maybe_const_itself, false);
8ce94e44 1147 if (eptype)
db3927fb 1148 ret = fold_convert_loc (loc, eptype, ret);
928c19bb
JM
1149 *maybe_const &= maybe_const_itself;
1150 return ret;
1151}
1152
1153/* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1154 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1155 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1156 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1157 both evaluated and unevaluated subexpressions while
1158 *MAYBE_CONST_ITSELF is carried from only evaluated
3aa3c9fc
MP
1159 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1160 with integer constant operands, and if any of the operands doesn't
1161 get folded to an integer constant, don't fold the expression itself. */
928c19bb
JM
1162
1163static tree
1164c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
3aa3c9fc 1165 bool *maybe_const_itself, bool for_int_const)
928c19bb
JM
1166{
1167 tree ret = expr;
1168 enum tree_code code = TREE_CODE (expr);
1169 enum tree_code_class kind = TREE_CODE_CLASS (code);
1170 location_t loc = EXPR_LOCATION (expr);
1171 tree op0, op1, op2, op3;
1172 tree orig_op0, orig_op1, orig_op2;
1173 bool op0_const = true, op1_const = true, op2_const = true;
1174 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1175 bool nowarning = TREE_NO_WARNING (expr);
02614448 1176 bool unused_p;
928c19bb
JM
1177
1178 /* This function is not relevant to C++ because C++ folds while
1179 parsing, and may need changes to be correct for C++ when C++
1180 stops folding while parsing. */
1181 if (c_dialect_cxx ())
1182 gcc_unreachable ();
1183
1184 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1185 anything else not counted as an expression cannot usefully be
1186 folded further at this point. */
1187 if (!IS_EXPR_CODE_CLASS (kind)
1188 || kind == tcc_statement
1189 || code == SAVE_EXPR)
1190 return expr;
1191
1192 /* Operands of variable-length expressions (function calls) have
1193 already been folded, as have __builtin_* function calls, and such
1194 expressions cannot occur in constant expressions. */
1195 if (kind == tcc_vl_exp)
1196 {
1197 *maybe_const_operands = false;
1198 ret = fold (expr);
1199 goto out;
1200 }
1201
1202 if (code == C_MAYBE_CONST_EXPR)
1203 {
1204 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1205 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1206 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1207 *maybe_const_operands = false;
1208 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
3aa3c9fc
MP
1209 {
1210 *maybe_const_itself = false;
1211 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1212 maybe_const_itself, true);
1213 }
928c19bb
JM
1214 if (pre && !in_init)
1215 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1216 else
1217 ret = inner;
1218 goto out;
1219 }
1220
1221 /* Assignment, increment, decrement, function call and comma
1222 operators, and statement expressions, cannot occur in constant
1223 expressions if evaluated / outside of sizeof. (Function calls
1224 were handled above, though VA_ARG_EXPR is treated like a function
1225 call here, and statement expressions are handled through
1226 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1227 switch (code)
1228 {
1229 case MODIFY_EXPR:
1230 case PREDECREMENT_EXPR:
1231 case PREINCREMENT_EXPR:
1232 case POSTDECREMENT_EXPR:
1233 case POSTINCREMENT_EXPR:
1234 case COMPOUND_EXPR:
1235 *maybe_const_operands = false;
1236 break;
1237
1238 case VA_ARG_EXPR:
1239 case TARGET_EXPR:
1240 case BIND_EXPR:
1241 case OBJ_TYPE_REF:
1242 *maybe_const_operands = false;
1243 ret = fold (expr);
1244 goto out;
1245
1246 default:
1247 break;
1248 }
1249
1250 /* Fold individual tree codes as appropriate. */
1251 switch (code)
1252 {
1253 case COMPOUND_LITERAL_EXPR:
1254 /* Any non-constancy will have been marked in a containing
1255 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1256 goto out;
1257
1258 case COMPONENT_REF:
1259 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1260 op1 = TREE_OPERAND (expr, 1);
1261 op2 = TREE_OPERAND (expr, 2);
1262 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
3aa3c9fc 1263 maybe_const_itself, for_int_const);
e5a94231 1264 STRIP_TYPE_NOPS (op0);
928c19bb
JM
1265 if (op0 != orig_op0)
1266 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1267 if (ret != expr)
1268 {
1269 TREE_READONLY (ret) = TREE_READONLY (expr);
1270 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1271 }
1272 goto out;
1273
1274 case ARRAY_REF:
1275 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1276 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1277 op2 = TREE_OPERAND (expr, 2);
1278 op3 = TREE_OPERAND (expr, 3);
1279 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
3aa3c9fc 1280 maybe_const_itself, for_int_const);
e5a94231 1281 STRIP_TYPE_NOPS (op0);
928c19bb 1282 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
3aa3c9fc 1283 maybe_const_itself, for_int_const);
e5a94231 1284 STRIP_TYPE_NOPS (op1);
928c19bb
JM
1285 op1 = decl_constant_value_for_optimization (op1);
1286 if (op0 != orig_op0 || op1 != orig_op1)
1287 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1288 if (ret != expr)
1289 {
1290 TREE_READONLY (ret) = TREE_READONLY (expr);
1291 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1292 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1293 }
1294 ret = fold (ret);
1295 goto out;
1296
1297 case COMPOUND_EXPR:
1298 case MODIFY_EXPR:
1299 case PREDECREMENT_EXPR:
1300 case PREINCREMENT_EXPR:
1301 case POSTDECREMENT_EXPR:
1302 case POSTINCREMENT_EXPR:
1303 case PLUS_EXPR:
1304 case MINUS_EXPR:
1305 case MULT_EXPR:
1306 case POINTER_PLUS_EXPR:
1307 case TRUNC_DIV_EXPR:
1308 case CEIL_DIV_EXPR:
1309 case FLOOR_DIV_EXPR:
1310 case TRUNC_MOD_EXPR:
1311 case RDIV_EXPR:
1312 case EXACT_DIV_EXPR:
1313 case LSHIFT_EXPR:
1314 case RSHIFT_EXPR:
1315 case BIT_IOR_EXPR:
1316 case BIT_XOR_EXPR:
1317 case BIT_AND_EXPR:
1318 case LT_EXPR:
1319 case LE_EXPR:
1320 case GT_EXPR:
1321 case GE_EXPR:
1322 case EQ_EXPR:
1323 case NE_EXPR:
1324 case COMPLEX_EXPR:
1325 case TRUTH_AND_EXPR:
1326 case TRUTH_OR_EXPR:
1327 case TRUTH_XOR_EXPR:
1328 case UNORDERED_EXPR:
1329 case ORDERED_EXPR:
1330 case UNLT_EXPR:
1331 case UNLE_EXPR:
1332 case UNGT_EXPR:
1333 case UNGE_EXPR:
1334 case UNEQ_EXPR:
1335 /* Binary operations evaluating both arguments (increment and
1336 decrement are binary internally in GCC). */
1337 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1338 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1339 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
3aa3c9fc 1340 maybe_const_itself, for_int_const);
e5a94231 1341 STRIP_TYPE_NOPS (op0);
928c19bb
JM
1342 if (code != MODIFY_EXPR
1343 && code != PREDECREMENT_EXPR
1344 && code != PREINCREMENT_EXPR
1345 && code != POSTDECREMENT_EXPR
1346 && code != POSTINCREMENT_EXPR)
1347 op0 = decl_constant_value_for_optimization (op0);
1348 /* The RHS of a MODIFY_EXPR was fully folded when building that
1349 expression for the sake of conversion warnings. */
1350 if (code != MODIFY_EXPR)
1351 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
3aa3c9fc 1352 maybe_const_itself, for_int_const);
e5a94231 1353 STRIP_TYPE_NOPS (op1);
928c19bb 1354 op1 = decl_constant_value_for_optimization (op1);
3aa3c9fc
MP
1355
1356 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1357 || TREE_CODE (op1) != INTEGER_CST))
1358 goto out;
1359
928c19bb
JM
1360 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1361 ret = in_init
db3927fb
AH
1362 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1363 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
928c19bb
JM
1364 else
1365 ret = fold (expr);
f5178456
RS
1366 if (TREE_OVERFLOW_P (ret)
1367 && !TREE_OVERFLOW_P (op0)
1368 && !TREE_OVERFLOW_P (op1))
1369 overflow_warning (EXPR_LOCATION (expr), ret);
0173bd2a
MP
1370 if (code == LSHIFT_EXPR
1371 && TREE_CODE (orig_op0) != INTEGER_CST
1372 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1373 && TREE_CODE (op0) == INTEGER_CST
1374 && c_inhibit_evaluation_warnings == 0
1375 && tree_int_cst_sgn (op0) < 0)
1376 warning_at (loc, OPT_Wshift_negative_value,
1377 "left shift of negative value");
cc83c823
JJ
1378 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1379 && TREE_CODE (orig_op1) != INTEGER_CST
1380 && TREE_CODE (op1) == INTEGER_CST
1381 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1382 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1383 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1384 && c_inhibit_evaluation_warnings == 0)
1385 {
1386 if (tree_int_cst_sgn (op1) < 0)
4853031e
MP
1387 warning_at (loc, OPT_Wshift_count_negative,
1388 (code == LSHIFT_EXPR
1389 ? G_("left shift count is negative")
1390 : G_("right shift count is negative")));
cc83c823
JJ
1391 else if (compare_tree_int (op1,
1392 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1393 >= 0)
4853031e
MP
1394 warning_at (loc, OPT_Wshift_count_overflow,
1395 (code == LSHIFT_EXPR
1396 ? G_("left shift count >= width of type")
1397 : G_("right shift count >= width of type")));
cc83c823 1398 }
4a9a42ab
MP
1399 if ((code == TRUNC_DIV_EXPR
1400 || code == CEIL_DIV_EXPR
1401 || code == FLOOR_DIV_EXPR
1402 || code == EXACT_DIV_EXPR
1403 || code == TRUNC_MOD_EXPR)
1404 && TREE_CODE (orig_op1) != INTEGER_CST
1405 && TREE_CODE (op1) == INTEGER_CST
1406 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1407 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1408 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1409 warn_for_div_by_zero (loc, op1);
928c19bb
JM
1410 goto out;
1411
1412 case INDIRECT_REF:
1413 case FIX_TRUNC_EXPR:
1414 case FLOAT_EXPR:
1415 CASE_CONVERT:
2ce064c3 1416 case ADDR_SPACE_CONVERT_EXPR:
982d62f6 1417 case VIEW_CONVERT_EXPR:
928c19bb
JM
1418 case NON_LVALUE_EXPR:
1419 case NEGATE_EXPR:
1420 case BIT_NOT_EXPR:
1421 case TRUTH_NOT_EXPR:
1422 case ADDR_EXPR:
1423 case CONJ_EXPR:
1424 case REALPART_EXPR:
1425 case IMAGPART_EXPR:
1426 /* Unary operations. */
1427 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1428 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
3aa3c9fc 1429 maybe_const_itself, for_int_const);
e5a94231 1430 STRIP_TYPE_NOPS (op0);
928c19bb
JM
1431 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1432 op0 = decl_constant_value_for_optimization (op0);
3aa3c9fc
MP
1433
1434 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1435 goto out;
1436
693ddb1b
EB
1437 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1438 not prepared to deal with them if they occur in initializers. */
1439 if (op0 != orig_op0
1440 && code == ADDR_EXPR
1441 && (op1 = get_base_address (op0)) != NULL_TREE
1442 && TREE_CODE (op1) == INDIRECT_REF
1443 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
cf9e9959 1444 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
693ddb1b 1445 else if (op0 != orig_op0 || in_init)
928c19bb 1446 ret = in_init
db3927fb
AH
1447 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1448 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
928c19bb
JM
1449 else
1450 ret = fold (expr);
1451 if (code == INDIRECT_REF
1452 && ret != expr
1453 && TREE_CODE (ret) == INDIRECT_REF)
1454 {
1455 TREE_READONLY (ret) = TREE_READONLY (expr);
1456 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1457 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1458 }
f5178456
RS
1459 switch (code)
1460 {
1461 case FIX_TRUNC_EXPR:
1462 case FLOAT_EXPR:
1463 CASE_CONVERT:
1464 /* Don't warn about explicit conversions. We will already
1465 have warned about suspect implicit conversions. */
1466 break;
1467
1468 default:
1469 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1470 overflow_warning (EXPR_LOCATION (expr), ret);
1471 break;
1472 }
928c19bb
JM
1473 goto out;
1474
1475 case TRUTH_ANDIF_EXPR:
1476 case TRUTH_ORIF_EXPR:
1477 /* Binary operations not necessarily evaluating both
1478 arguments. */
1479 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1480 orig_op1 = op1 = TREE_OPERAND (expr, 1);
3aa3c9fc
MP
1481 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1482 for_int_const);
e5a94231 1483 STRIP_TYPE_NOPS (op0);
f5178456
RS
1484
1485 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1486 ? truthvalue_false_node
1487 : truthvalue_true_node));
02614448 1488 c_disable_warnings (unused_p);
3aa3c9fc
MP
1489 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1490 for_int_const);
e5a94231 1491 STRIP_TYPE_NOPS (op1);
02614448 1492 c_enable_warnings (unused_p);
f5178456 1493
3aa3c9fc
MP
1494 if (for_int_const
1495 && (TREE_CODE (op0) != INTEGER_CST
1496 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1497 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1498 goto out;
1499
928c19bb
JM
1500 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1501 ret = in_init
db3927fb
AH
1502 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1503 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
928c19bb
JM
1504 else
1505 ret = fold (expr);
1506 *maybe_const_operands &= op0_const;
1507 *maybe_const_itself &= op0_const_self;
1508 if (!(flag_isoc99
1509 && op0_const
1510 && op0_const_self
1511 && (code == TRUTH_ANDIF_EXPR
1512 ? op0 == truthvalue_false_node
1513 : op0 == truthvalue_true_node)))
1514 *maybe_const_operands &= op1_const;
1515 if (!(op0_const
1516 && op0_const_self
1517 && (code == TRUTH_ANDIF_EXPR
1518 ? op0 == truthvalue_false_node
1519 : op0 == truthvalue_true_node)))
1520 *maybe_const_itself &= op1_const_self;
1521 goto out;
1522
1523 case COND_EXPR:
1524 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1525 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1526 orig_op2 = op2 = TREE_OPERAND (expr, 2);
3aa3c9fc
MP
1527 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1528 for_int_const);
f5178456 1529
e5a94231 1530 STRIP_TYPE_NOPS (op0);
02614448 1531 c_disable_warnings (op0 == truthvalue_false_node);
3aa3c9fc
MP
1532 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1533 for_int_const);
e5a94231 1534 STRIP_TYPE_NOPS (op1);
02614448 1535 c_enable_warnings (op0 == truthvalue_false_node);
f5178456 1536
02614448 1537 c_disable_warnings (op0 == truthvalue_true_node);
3aa3c9fc
MP
1538 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1539 for_int_const);
e5a94231 1540 STRIP_TYPE_NOPS (op2);
02614448 1541 c_enable_warnings (op0 == truthvalue_true_node);
f5178456 1542
3aa3c9fc
MP
1543 if (for_int_const
1544 && (TREE_CODE (op0) != INTEGER_CST
1545 /* Only the evaluated operand must be an INTEGER_CST. */
1546 || (op0 == truthvalue_true_node
1547 ? TREE_CODE (op1) != INTEGER_CST
1548 : TREE_CODE (op2) != INTEGER_CST)))
1549 goto out;
1550
928c19bb 1551 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
db3927fb 1552 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
928c19bb
JM
1553 else
1554 ret = fold (expr);
1555 *maybe_const_operands &= op0_const;
1556 *maybe_const_itself &= op0_const_self;
1557 if (!(flag_isoc99
1558 && op0_const
1559 && op0_const_self
1560 && op0 == truthvalue_false_node))
1561 *maybe_const_operands &= op1_const;
1562 if (!(op0_const
1563 && op0_const_self
1564 && op0 == truthvalue_false_node))
1565 *maybe_const_itself &= op1_const_self;
1566 if (!(flag_isoc99
1567 && op0_const
1568 && op0_const_self
1569 && op0 == truthvalue_true_node))
1570 *maybe_const_operands &= op2_const;
1571 if (!(op0_const
1572 && op0_const_self
1573 && op0 == truthvalue_true_node))
1574 *maybe_const_itself &= op2_const_self;
1575 goto out;
1576
8ce94e44
JM
1577 case EXCESS_PRECISION_EXPR:
1578 /* Each case where an operand with excess precision may be
1579 encountered must remove the EXCESS_PRECISION_EXPR around
1580 inner operands and possibly put one around the whole
1581 expression or possibly convert to the semantic type (which
1582 c_fully_fold does); we cannot tell at this stage which is
1583 appropriate in any particular case. */
1584 gcc_unreachable ();
1585
928c19bb
JM
1586 default:
1587 /* Various codes may appear through folding built-in functions
1588 and their arguments. */
1589 goto out;
1590 }
1591
1592 out:
1593 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1594 have been done by this point, so remove them again. */
1595 nowarning |= TREE_NO_WARNING (ret);
1596 STRIP_TYPE_NOPS (ret);
1597 if (nowarning && !TREE_NO_WARNING (ret))
1598 {
1599 if (!CAN_HAVE_LOCATION_P (ret))
1600 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1601 TREE_NO_WARNING (ret) = 1;
1602 }
1603 if (ret != expr)
1604 protected_set_expr_location (ret, loc);
1605 return ret;
1606}
1607
1608/* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1609 return EXP. Otherwise, return either EXP or its known constant
1610 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1611 Is the BLKmode test appropriate? */
1612
1613tree
1614decl_constant_value_for_optimization (tree exp)
1615{
1616 tree ret;
1617
1618 /* This function is only used by C, for c_fully_fold and other
1619 optimization, and may not be correct for C++. */
1620 if (c_dialect_cxx ())
1621 gcc_unreachable ();
1622
1623 if (!optimize
1624 || TREE_CODE (exp) != VAR_DECL
1625 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1626 || DECL_MODE (exp) == BLKmode)
1627 return exp;
1628
1629 ret = decl_constant_value (exp);
1630 /* Avoid unwanted tree sharing between the initializer and current
1631 function's body where the tree can be modified e.g. by the
1632 gimplifier. */
1633 if (ret != exp && TREE_STATIC (exp))
1634 ret = unshare_expr (ret);
1635 return ret;
1636}
1637
d74154d5
RS
1638/* Print a warning if a constant expression had overflow in folding.
1639 Invoke this function on every expression that the language
1640 requires to be a constant expression.
1641 Note the ANSI C standard says it is erroneous for a
1642 constant expression to overflow. */
96571883
BK
1643
1644void
35b1a6fa 1645constant_expression_warning (tree value)
393eda6a 1646{
b8698a0f 1647 if (warn_overflow && pedantic
393eda6a
MLI
1648 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1649 || TREE_CODE (value) == FIXED_CST
1650 || TREE_CODE (value) == VECTOR_CST
1651 || TREE_CODE (value) == COMPLEX_CST)
1652 && TREE_OVERFLOW (value))
509c9d60 1653 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
393eda6a
MLI
1654}
1655
1656/* The same as above but print an unconditional error. */
1657void
1658constant_expression_error (tree value)
96571883 1659{
c05f751c 1660 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
ab22c1fa 1661 || TREE_CODE (value) == FIXED_CST
69ef87e2 1662 || TREE_CODE (value) == VECTOR_CST
c05f751c 1663 || TREE_CODE (value) == COMPLEX_CST)
393eda6a
MLI
1664 && TREE_OVERFLOW (value))
1665 error ("overflow in constant expression");
d74154d5
RS
1666}
1667
59c0753d
MLI
1668/* Print a warning if an expression had overflow in folding and its
1669 operands hadn't.
1670
d74154d5
RS
1671 Invoke this function on every expression that
1672 (1) appears in the source code, and
59c0753d 1673 (2) is a constant expression that overflowed, and
d74154d5 1674 (3) is not already checked by convert_and_check;
59c0753d
MLI
1675 however, do not invoke this function on operands of explicit casts
1676 or when the expression is the result of an operator and any operand
1677 already overflowed. */
d74154d5
RS
1678
1679void
c2255bc4 1680overflow_warning (location_t loc, tree value)
d74154d5 1681{
7d882b83
ILT
1682 if (c_inhibit_evaluation_warnings != 0)
1683 return;
59c0753d
MLI
1684
1685 switch (TREE_CODE (value))
69ef87e2 1686 {
59c0753d 1687 case INTEGER_CST:
c2255bc4 1688 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
59c0753d 1689 break;
b8698a0f 1690
59c0753d 1691 case REAL_CST:
c2255bc4
AH
1692 warning_at (loc, OPT_Woverflow,
1693 "floating point overflow in expression");
59c0753d 1694 break;
b8698a0f 1695
ab22c1fa 1696 case FIXED_CST:
c2255bc4 1697 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
ab22c1fa
CF
1698 break;
1699
59c0753d 1700 case VECTOR_CST:
c2255bc4 1701 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
59c0753d 1702 break;
b8698a0f 1703
59c0753d
MLI
1704 case COMPLEX_CST:
1705 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
c2255bc4
AH
1706 warning_at (loc, OPT_Woverflow,
1707 "complex integer overflow in expression");
59c0753d 1708 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
c2255bc4
AH
1709 warning_at (loc, OPT_Woverflow,
1710 "complex floating point overflow in expression");
59c0753d
MLI
1711 break;
1712
1713 default:
1714 break;
69ef87e2 1715 }
d74154d5
RS
1716}
1717
ca409efd
MLI
1718/* Warn about uses of logical || / && operator in a context where it
1719 is likely that the bitwise equivalent was intended by the
1720 programmer. We have seen an expression in which CODE is a binary
a243fb4a
MLI
1721 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1722 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
63a08740 1723void
a243fb4a 1724warn_logical_operator (location_t location, enum tree_code code, tree type,
b8698a0f 1725 enum tree_code code_left, tree op_left,
ca409efd 1726 enum tree_code ARG_UNUSED (code_right), tree op_right)
63a08740 1727{
a243fb4a
MLI
1728 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1729 int in0_p, in1_p, in_p;
1730 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1731 bool strict_overflow_p = false;
1732
ca409efd
MLI
1733 if (code != TRUTH_ANDIF_EXPR
1734 && code != TRUTH_AND_EXPR
1735 && code != TRUTH_ORIF_EXPR
1736 && code != TRUTH_OR_EXPR)
1737 return;
1738
b8787813
MP
1739 /* We don't want to warn if either operand comes from a macro
1740 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1741 see PR61534. */
1742 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1743 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1744 return;
1745
ca409efd
MLI
1746 /* Warn if &&/|| are being used in a context where it is
1747 likely that the bitwise equivalent was intended by the
1748 programmer. That is, an expression such as op && MASK
1749 where op should not be any boolean expression, nor a
1750 constant, and mask seems to be a non-boolean integer constant. */
1751 if (!truth_value_p (code_left)
1752 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1753 && !CONSTANT_CLASS_P (op_left)
1754 && !TREE_NO_WARNING (op_left)
1755 && TREE_CODE (op_right) == INTEGER_CST
1756 && !integer_zerop (op_right)
1757 && !integer_onep (op_right))
63a08740 1758 {
a243fb4a 1759 if (or_op)
ca409efd
MLI
1760 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1761 " applied to non-boolean constant");
1762 else
1763 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1764 " applied to non-boolean constant");
1765 TREE_NO_WARNING (op_left) = true;
a243fb4a
MLI
1766 return;
1767 }
1768
1769 /* We do not warn for constants because they are typical of macro
1770 expansions that test for features. */
1771 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1772 return;
1773
1774 /* This warning only makes sense with logical operands. */
1775 if (!(truth_value_p (TREE_CODE (op_left))
1776 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1777 || !(truth_value_p (TREE_CODE (op_right))
1778 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1779 return;
1780
3c9aabbd
MG
1781 /* The range computations only work with scalars. */
1782 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1783 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1784 return;
a243fb4a 1785
f2c4a785
MLI
1786 /* We first test whether either side separately is trivially true
1787 (with OR) or trivially false (with AND). If so, do not warn.
1788 This is a common idiom for testing ranges of data types in
1789 portable code. */
1790 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1791 if (!lhs)
1792 return;
1793 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
a243fb4a
MLI
1794 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1795
f2c4a785
MLI
1796 /* If this is an OR operation, invert both sides; now, the result
1797 should be always false to get a warning. */
1798 if (or_op)
1799 in0_p = !in0_p;
1800
1801 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1e537948 1802 if (tem && integer_zerop (tem))
f2c4a785
MLI
1803 return;
1804
1805 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1806 if (!rhs)
1807 return;
1808 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
a243fb4a 1809 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
b8698a0f 1810
f2c4a785
MLI
1811 /* If this is an OR operation, invert both sides; now, the result
1812 should be always false to get a warning. */
a243fb4a 1813 if (or_op)
f2c4a785 1814 in1_p = !in1_p;
b8698a0f 1815
f2c4a785 1816 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1e537948 1817 if (tem && integer_zerop (tem))
f2c4a785
MLI
1818 return;
1819
1820 /* If both expressions have the same operand, if we can merge the
8c2b7f79 1821 ranges, ... */
f2c4a785 1822 if (operand_equal_p (lhs, rhs, 0)
a243fb4a 1823 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
8c2b7f79 1824 in1_p, low1, high1))
a243fb4a 1825 {
8c2b7f79
MP
1826 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1827 /* ... and if the range test is always false, then warn. */
1828 if (tem && integer_zerop (tem))
1829 {
1830 if (or_op)
1831 warning_at (location, OPT_Wlogical_op,
1832 "logical %<or%> of collectively exhaustive tests is "
1833 "always true");
1834 else
1835 warning_at (location, OPT_Wlogical_op,
1836 "logical %<and%> of mutually exclusive tests is "
1837 "always false");
1838 }
1839 /* Or warn if the operands have exactly the same range, e.g.
1840 A > 0 && A > 0. */
1841 else if (low0 == low1 && high0 == high1)
1842 {
1843 if (or_op)
1844 warning_at (location, OPT_Wlogical_op,
1845 "logical %<or%> of equal expressions");
1846 else
1847 warning_at (location, OPT_Wlogical_op,
1848 "logical %<and%> of equal expressions");
1849 }
63a08740
DM
1850 }
1851}
1852
742938c9
MP
1853/* Warn about logical not used on the left hand side operand of a comparison.
1854 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
59ea0364 1855 Do not warn if RHS is of a boolean type. */
742938c9
MP
1856
1857void
1858warn_logical_not_parentheses (location_t location, enum tree_code code,
59ea0364 1859 tree rhs)
742938c9 1860{
59ea0364
MP
1861 if (TREE_CODE_CLASS (code) != tcc_comparison
1862 || TREE_TYPE (rhs) == NULL_TREE
1863 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
742938c9
MP
1864 return;
1865
7ccb1a11
JJ
1866 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1867 !(x == 0) or !(y != 0). */
1868 if ((code == EQ_EXPR || code == NE_EXPR)
1869 && integer_zerop (rhs))
1870 return;
1871
742938c9
MP
1872 warning_at (location, OPT_Wlogical_not_parentheses,
1873 "logical not is only applied to the left hand side of "
1874 "comparison");
1875}
63a08740 1876
fd4116f4
MLI
1877/* Warn if EXP contains any computations whose results are not used.
1878 Return true if a warning is printed; false otherwise. LOCUS is the
1879 (potential) location of the expression. */
1880
1881bool
1882warn_if_unused_value (const_tree exp, location_t locus)
1883{
1884 restart:
1885 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1886 return false;
1887
1888 /* Don't warn about void constructs. This includes casting to void,
1889 void function calls, and statement expressions with a final cast
1890 to void. */
1891 if (VOID_TYPE_P (TREE_TYPE (exp)))
1892 return false;
1893
1894 if (EXPR_HAS_LOCATION (exp))
1895 locus = EXPR_LOCATION (exp);
1896
1897 switch (TREE_CODE (exp))
1898 {
1899 case PREINCREMENT_EXPR:
1900 case POSTINCREMENT_EXPR:
1901 case PREDECREMENT_EXPR:
1902 case POSTDECREMENT_EXPR:
1903 case MODIFY_EXPR:
1904 case INIT_EXPR:
1905 case TARGET_EXPR:
1906 case CALL_EXPR:
1907 case TRY_CATCH_EXPR:
1908 case WITH_CLEANUP_EXPR:
1909 case EXIT_EXPR:
1910 case VA_ARG_EXPR:
1911 return false;
1912
1913 case BIND_EXPR:
1914 /* For a binding, warn if no side effect within it. */
1915 exp = BIND_EXPR_BODY (exp);
1916 goto restart;
1917
1918 case SAVE_EXPR:
1919 case NON_LVALUE_EXPR:
007a787d 1920 case NOP_EXPR:
fd4116f4
MLI
1921 exp = TREE_OPERAND (exp, 0);
1922 goto restart;
1923
1924 case TRUTH_ORIF_EXPR:
1925 case TRUTH_ANDIF_EXPR:
1926 /* In && or ||, warn if 2nd operand has no side effect. */
1927 exp = TREE_OPERAND (exp, 1);
1928 goto restart;
1929
1930 case COMPOUND_EXPR:
1931 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1932 return true;
1933 /* Let people do `(foo (), 0)' without a warning. */
1934 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1935 return false;
1936 exp = TREE_OPERAND (exp, 1);
1937 goto restart;
1938
1939 case COND_EXPR:
1940 /* If this is an expression with side effects, don't warn; this
1941 case commonly appears in macro expansions. */
1942 if (TREE_SIDE_EFFECTS (exp))
1943 return false;
1944 goto warn;
1945
1946 case INDIRECT_REF:
1947 /* Don't warn about automatic dereferencing of references, since
1948 the user cannot control it. */
1949 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1950 {
1951 exp = TREE_OPERAND (exp, 0);
1952 goto restart;
1953 }
1954 /* Fall through. */
1955
1956 default:
1957 /* Referencing a volatile value is a side effect, so don't warn. */
1958 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1959 && TREE_THIS_VOLATILE (exp))
1960 return false;
1961
1962 /* If this is an expression which has no operands, there is no value
1963 to be unused. There are no such language-independent codes,
1964 but front ends may define such. */
1965 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1966 return false;
1967
1968 warn:
1969 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1970 }
1971}
1972
1973
de9c56a4
RG
1974/* Print a warning about casts that might indicate violation
1975 of strict aliasing rules if -Wstrict-aliasing is used and
3f0a2a47
DM
1976 strict aliasing mode is in effect. OTYPE is the original
1977 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
de9c56a4 1978
79bedddc 1979bool
3f0a2a47 1980strict_aliasing_warning (tree otype, tree type, tree expr)
de9c56a4 1981{
255d3827
RG
1982 /* Strip pointer conversion chains and get to the correct original type. */
1983 STRIP_NOPS (expr);
1984 otype = TREE_TYPE (expr);
1985
ac7ee6ad
RG
1986 if (!(flag_strict_aliasing
1987 && POINTER_TYPE_P (type)
1988 && POINTER_TYPE_P (otype)
1989 && !VOID_TYPE_P (TREE_TYPE (type)))
1990 /* If the type we are casting to is a ref-all pointer
1991 dereferencing it is always valid. */
1992 || TYPE_REF_CAN_ALIAS_ALL (type))
79bedddc
SR
1993 return false;
1994
1995 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
de9c56a4 1996 && (DECL_P (TREE_OPERAND (expr, 0))
79bedddc 1997 || handled_component_p (TREE_OPERAND (expr, 0))))
de9c56a4
RG
1998 {
1999 /* Casting the address of an object to non void pointer. Warn
2000 if the cast breaks type based aliasing. */
79bedddc
SR
2001 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2002 {
2003 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2004 "might break strict-aliasing rules");
2005 return true;
2006 }
de9c56a4
RG
2007 else
2008 {
b8698a0f 2009 /* warn_strict_aliasing >= 3. This includes the default (3).
79bedddc 2010 Only warn if the cast is dereferenced immediately. */
4862826d 2011 alias_set_type set1 =
79bedddc 2012 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
4862826d 2013 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
de9c56a4 2014
4653cae5
RG
2015 if (set1 != set2 && set2 != 0
2016 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
79bedddc
SR
2017 {
2018 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2019 "pointer will break strict-aliasing rules");
2020 return true;
2021 }
2022 else if (warn_strict_aliasing == 2
836f7794 2023 && !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
2024 {
2025 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2026 "pointer might break strict-aliasing rules");
2027 return true;
2028 }
de9c56a4
RG
2029 }
2030 }
79bedddc
SR
2031 else
2032 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2033 {
2034 /* At this level, warn for any conversions, even if an address is
2035 not taken in the same statement. This will likely produce many
2036 false positives, but could be useful to pinpoint problems that
2037 are not revealed at higher levels. */
4862826d
ILT
2038 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2039 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2040 if (!COMPLETE_TYPE_P (type)
836f7794 2041 || !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
2042 {
2043 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2044 "pointer might break strict-aliasing rules");
2045 return true;
2046 }
2047 }
2048
2049 return false;
de9c56a4
RG
2050}
2051
1a4049e7
JJ
2052/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2053 sizeof as last operand of certain builtins. */
2054
2055void
3a785c97 2056sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
9771b263 2057 vec<tree, va_gc> *params, tree *sizeof_arg,
1a4049e7
JJ
2058 bool (*comp_types) (tree, tree))
2059{
2060 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
3a785c97
JJ
2061 bool strop = false, cmp = false;
2062 unsigned int idx = ~0;
2063 location_t loc;
1a4049e7
JJ
2064
2065 if (TREE_CODE (callee) != FUNCTION_DECL
2066 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
9771b263 2067 || vec_safe_length (params) <= 1)
1a4049e7
JJ
2068 return;
2069
1a4049e7
JJ
2070 switch (DECL_FUNCTION_CODE (callee))
2071 {
2072 case BUILT_IN_STRNCMP:
2073 case BUILT_IN_STRNCASECMP:
3a785c97
JJ
2074 cmp = true;
2075 /* FALLTHRU */
1a4049e7 2076 case BUILT_IN_STRNCPY:
3a785c97 2077 case BUILT_IN_STRNCPY_CHK:
1a4049e7 2078 case BUILT_IN_STRNCAT:
3a785c97
JJ
2079 case BUILT_IN_STRNCAT_CHK:
2080 case BUILT_IN_STPNCPY:
2081 case BUILT_IN_STPNCPY_CHK:
1a4049e7
JJ
2082 strop = true;
2083 /* FALLTHRU */
2084 case BUILT_IN_MEMCPY:
3a785c97 2085 case BUILT_IN_MEMCPY_CHK:
1a4049e7 2086 case BUILT_IN_MEMMOVE:
3a785c97 2087 case BUILT_IN_MEMMOVE_CHK:
9771b263 2088 if (params->length () < 3)
3a785c97 2089 return;
9771b263
DN
2090 src = (*params)[1];
2091 dest = (*params)[0];
3a785c97
JJ
2092 idx = 2;
2093 break;
2094 case BUILT_IN_BCOPY:
9771b263 2095 if (params->length () < 3)
3a785c97 2096 return;
9771b263
DN
2097 src = (*params)[0];
2098 dest = (*params)[1];
3a785c97
JJ
2099 idx = 2;
2100 break;
1a4049e7 2101 case BUILT_IN_MEMCMP:
3a785c97 2102 case BUILT_IN_BCMP:
9771b263 2103 if (params->length () < 3)
1a4049e7 2104 return;
9771b263
DN
2105 src = (*params)[1];
2106 dest = (*params)[0];
3a785c97
JJ
2107 idx = 2;
2108 cmp = true;
1a4049e7
JJ
2109 break;
2110 case BUILT_IN_MEMSET:
3a785c97 2111 case BUILT_IN_MEMSET_CHK:
9771b263 2112 if (params->length () < 3)
1a4049e7 2113 return;
9771b263 2114 dest = (*params)[0];
3a785c97
JJ
2115 idx = 2;
2116 break;
2117 case BUILT_IN_BZERO:
9771b263 2118 dest = (*params)[0];
3a785c97 2119 idx = 1;
1a4049e7
JJ
2120 break;
2121 case BUILT_IN_STRNDUP:
9771b263 2122 src = (*params)[0];
1a4049e7 2123 strop = true;
3a785c97
JJ
2124 idx = 1;
2125 break;
2126 case BUILT_IN_MEMCHR:
9771b263 2127 if (params->length () < 3)
3a785c97 2128 return;
9771b263 2129 src = (*params)[0];
3a785c97
JJ
2130 idx = 2;
2131 break;
2132 case BUILT_IN_SNPRINTF:
2133 case BUILT_IN_SNPRINTF_CHK:
2134 case BUILT_IN_VSNPRINTF:
2135 case BUILT_IN_VSNPRINTF_CHK:
9771b263 2136 dest = (*params)[0];
3a785c97
JJ
2137 idx = 1;
2138 strop = true;
1a4049e7
JJ
2139 break;
2140 default:
2141 break;
2142 }
2143
3a785c97
JJ
2144 if (idx >= 3)
2145 return;
2146
2147 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2148 return;
2149
2150 type = TYPE_P (sizeof_arg[idx])
2151 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2152 if (!POINTER_TYPE_P (type))
2153 return;
2154
1a4049e7
JJ
2155 if (dest
2156 && (tem = tree_strip_nop_conversions (dest))
2157 && POINTER_TYPE_P (TREE_TYPE (tem))
2158 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2159 return;
2160
2161 if (src
2162 && (tem = tree_strip_nop_conversions (src))
2163 && POINTER_TYPE_P (TREE_TYPE (tem))
2164 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2165 return;
2166
3a785c97
JJ
2167 loc = sizeof_arg_loc[idx];
2168
2169 if (dest && !cmp)
1a4049e7 2170 {
3a785c97
JJ
2171 if (!TYPE_P (sizeof_arg[idx])
2172 && operand_equal_p (dest, sizeof_arg[idx], 0)
1a4049e7
JJ
2173 && comp_types (TREE_TYPE (dest), type))
2174 {
3a785c97 2175 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
2176 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2177 "argument to %<sizeof%> in %qD call is the same "
2178 "expression as the destination; did you mean to "
2179 "remove the addressof?", callee);
2180 else if ((TYPE_PRECISION (TREE_TYPE (type))
2181 == TYPE_PRECISION (char_type_node))
2182 || strop)
2183 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2184 "argument to %<sizeof%> in %qD call is the same "
2185 "expression as the destination; did you mean to "
2186 "provide an explicit length?", callee);
2187 else
2188 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2189 "argument to %<sizeof%> in %qD call is the same "
2190 "expression as the destination; did you mean to "
2191 "dereference it?", callee);
2192 return;
2193 }
2194
2195 if (POINTER_TYPE_P (TREE_TYPE (dest))
2196 && !strop
2197 && comp_types (TREE_TYPE (dest), type)
2198 && !VOID_TYPE_P (TREE_TYPE (type)))
2199 {
2200 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2201 "argument to %<sizeof%> in %qD call is the same "
2202 "pointer type %qT as the destination; expected %qT "
2203 "or an explicit length", callee, TREE_TYPE (dest),
2204 TREE_TYPE (TREE_TYPE (dest)));
2205 return;
2206 }
2207 }
2208
3a785c97 2209 if (src && !cmp)
1a4049e7 2210 {
3a785c97
JJ
2211 if (!TYPE_P (sizeof_arg[idx])
2212 && operand_equal_p (src, sizeof_arg[idx], 0)
1a4049e7
JJ
2213 && comp_types (TREE_TYPE (src), type))
2214 {
3a785c97 2215 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
2216 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2217 "argument to %<sizeof%> in %qD call is the same "
2218 "expression as the source; did you mean to "
2219 "remove the addressof?", callee);
2220 else if ((TYPE_PRECISION (TREE_TYPE (type))
2221 == TYPE_PRECISION (char_type_node))
2222 || strop)
2223 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2224 "argument to %<sizeof%> in %qD call is the same "
2225 "expression as the source; did you mean to "
2226 "provide an explicit length?", callee);
2227 else
2228 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2229 "argument to %<sizeof%> in %qD call is the same "
2230 "expression as the source; did you mean to "
2231 "dereference it?", callee);
2232 return;
2233 }
2234
2235 if (POINTER_TYPE_P (TREE_TYPE (src))
2236 && !strop
2237 && comp_types (TREE_TYPE (src), type)
2238 && !VOID_TYPE_P (TREE_TYPE (type)))
2239 {
2240 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2241 "argument to %<sizeof%> in %qD call is the same "
2242 "pointer type %qT as the source; expected %qT "
2243 "or an explicit length", callee, TREE_TYPE (src),
2244 TREE_TYPE (TREE_TYPE (src)));
2245 return;
2246 }
2247 }
3a785c97
JJ
2248
2249 if (dest)
2250 {
2251 if (!TYPE_P (sizeof_arg[idx])
2252 && operand_equal_p (dest, sizeof_arg[idx], 0)
2253 && comp_types (TREE_TYPE (dest), type))
2254 {
2255 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2256 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2257 "argument to %<sizeof%> in %qD call is the same "
2258 "expression as the first source; did you mean to "
2259 "remove the addressof?", callee);
2260 else if ((TYPE_PRECISION (TREE_TYPE (type))
2261 == TYPE_PRECISION (char_type_node))
2262 || strop)
2263 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2264 "argument to %<sizeof%> in %qD call is the same "
2265 "expression as the first source; did you mean to "
2266 "provide an explicit length?", callee);
2267 else
2268 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2269 "argument to %<sizeof%> in %qD call is the same "
2270 "expression as the first source; did you mean to "
2271 "dereference it?", callee);
2272 return;
2273 }
2274
2275 if (POINTER_TYPE_P (TREE_TYPE (dest))
2276 && !strop
2277 && comp_types (TREE_TYPE (dest), type)
2278 && !VOID_TYPE_P (TREE_TYPE (type)))
2279 {
2280 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2281 "argument to %<sizeof%> in %qD call is the same "
2282 "pointer type %qT as the first source; expected %qT "
2283 "or an explicit length", callee, TREE_TYPE (dest),
2284 TREE_TYPE (TREE_TYPE (dest)));
2285 return;
2286 }
2287 }
2288
2289 if (src)
2290 {
2291 if (!TYPE_P (sizeof_arg[idx])
2292 && operand_equal_p (src, sizeof_arg[idx], 0)
2293 && comp_types (TREE_TYPE (src), type))
2294 {
2295 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2296 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2297 "argument to %<sizeof%> in %qD call is the same "
2298 "expression as the second source; did you mean to "
2299 "remove the addressof?", callee);
2300 else if ((TYPE_PRECISION (TREE_TYPE (type))
2301 == TYPE_PRECISION (char_type_node))
2302 || strop)
2303 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2304 "argument to %<sizeof%> in %qD call is the same "
2305 "expression as the second source; did you mean to "
2306 "provide an explicit length?", callee);
2307 else
2308 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2309 "argument to %<sizeof%> in %qD call is the same "
2310 "expression as the second source; did you mean to "
2311 "dereference it?", callee);
2312 return;
2313 }
2314
2315 if (POINTER_TYPE_P (TREE_TYPE (src))
2316 && !strop
2317 && comp_types (TREE_TYPE (src), type)
2318 && !VOID_TYPE_P (TREE_TYPE (type)))
2319 {
2320 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2321 "argument to %<sizeof%> in %qD call is the same "
2322 "pointer type %qT as the second source; expected %qT "
2323 "or an explicit length", callee, TREE_TYPE (src),
2324 TREE_TYPE (TREE_TYPE (src)));
2325 return;
2326 }
2327 }
2328
1a4049e7
JJ
2329}
2330
a1e45ff0
DM
2331/* Warn for unlikely, improbable, or stupid DECL declarations
2332 of `main'. */
2333
2334void
2335check_main_parameter_types (tree decl)
2336{
e19a18d4
NF
2337 function_args_iterator iter;
2338 tree type;
a1e45ff0
DM
2339 int argct = 0;
2340
e19a18d4
NF
2341 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2342 {
2343 /* XXX void_type_node belies the abstraction. */
2344 if (type == void_type_node || type == error_mark_node )
2345 break;
2346
f827930a
MP
2347 tree t = type;
2348 if (TYPE_ATOMIC (t))
2349 pedwarn (input_location, OPT_Wmain,
2350 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2351 type, decl);
2352 while (POINTER_TYPE_P (t))
2353 {
2354 t = TREE_TYPE (t);
2355 if (TYPE_ATOMIC (t))
2356 pedwarn (input_location, OPT_Wmain,
2357 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2358 type, decl);
2359 }
2360
e19a18d4
NF
2361 ++argct;
2362 switch (argct)
2363 {
2364 case 1:
2365 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2366 pedwarn (input_location, OPT_Wmain,
2367 "first argument of %q+D should be %<int%>", decl);
2368 break;
2369
2370 case 2:
2371 if (TREE_CODE (type) != POINTER_TYPE
2372 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2373 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2374 != char_type_node))
2375 pedwarn (input_location, OPT_Wmain,
2376 "second argument of %q+D should be %<char **%>", decl);
2377 break;
2378
2379 case 3:
2380 if (TREE_CODE (type) != POINTER_TYPE
2381 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2382 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2383 != char_type_node))
2384 pedwarn (input_location, OPT_Wmain,
2385 "third argument of %q+D should probably be "
2386 "%<char **%>", decl);
2387 break;
2388 }
2389 }
a1e45ff0
DM
2390
2391 /* It is intentional that this message does not mention the third
2392 argument because it's only mentioned in an appendix of the
2393 standard. */
2394 if (argct > 0 && (argct < 2 || argct > 3))
e19a18d4
NF
2395 pedwarn (input_location, OPT_Wmain,
2396 "%q+D takes only zero or two arguments", decl);
38e514c0
MP
2397
2398 if (stdarg_p (TREE_TYPE (decl)))
2399 pedwarn (input_location, OPT_Wmain,
2400 "%q+D declared as variadic function", decl);
a1e45ff0
DM
2401}
2402
0af94e6f
JR
2403/* vector_targets_convertible_p is used for vector pointer types. The
2404 callers perform various checks that the qualifiers are satisfactory,
2405 while OTOH vector_targets_convertible_p ignores the number of elements
2406 in the vectors. That's fine with vector pointers as we can consider,
2407 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2408 and that does not require and conversion of the pointer values.
2409 In contrast, vector_types_convertible_p and
2410 vector_types_compatible_elements_p are used for vector value types. */
f83c7f63
DJ
2411/* True if pointers to distinct types T1 and T2 can be converted to
2412 each other without an explicit cast. Only returns true for opaque
2413 vector types. */
2414bool
2415vector_targets_convertible_p (const_tree t1, const_tree t2)
2416{
2417 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
b6fc2cdb 2418 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
f83c7f63
DJ
2419 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2420 return true;
2421
2422 return false;
2423}
2424
0af94e6f
JR
2425/* vector_types_convertible_p is used for vector value types.
2426 It could in principle call vector_targets_convertible_p as a subroutine,
2427 but then the check for vector type would be duplicated with its callers,
2428 and also the purpose of vector_targets_convertible_p would become
2429 muddled.
2430 Where vector_types_convertible_p returns true, a conversion might still be
2431 needed to make the types match.
2432 In contrast, vector_targets_convertible_p is used for vector pointer
2433 values, and vector_types_compatible_elements_p is used specifically
2434 in the context for binary operators, as a check if use is possible without
2435 conversion. */
00c8e9f6
MS
2436/* True if vector types T1 and T2 can be converted to each other
2437 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2438 can only be converted with -flax-vector-conversions yet that is not
2439 in effect, emit a note telling the user about that option if such
2440 a note has not previously been emitted. */
2441bool
58f9752a 2442vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
cc27e657 2443{
00c8e9f6 2444 static bool emitted_lax_note = false;
14e765da
JM
2445 bool convertible_lax;
2446
b6fc2cdb 2447 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
14e765da
JM
2448 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2449 return true;
2450
2451 convertible_lax =
2452 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2453 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
a5e0cd1d 2454 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
14e765da
JM
2455 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2456 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
00c8e9f6
MS
2457
2458 if (!convertible_lax || flag_lax_vector_conversions)
2459 return convertible_lax;
2460
2461 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
cf7bc668 2462 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
00c8e9f6
MS
2463 return true;
2464
2465 if (emit_lax_note && !emitted_lax_note)
2466 {
2467 emitted_lax_note = true;
1f5b3869 2468 inform (input_location, "use -flax-vector-conversions to permit "
00c8e9f6
MS
2469 "conversions between vectors with differing "
2470 "element types or numbers of subparts");
2471 }
2472
2473 return false;
cc27e657
PB
2474}
2475
9e1a8dd1
RR
2476/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2477 and have vector types, V0 has the same type as V1, and the number of
2478 elements of V0, V1, MASK is the same.
2479
2480 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2481 called with two arguments. In this case implementation passes the
2482 first argument twice in order to share the same tree code. This fact
2483 could enable the mask-values being twice the vector length. This is
2484 an implementation accident and this semantics is not guaranteed to
2485 the user. */
2486tree
bedc293e
MG
2487c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2488 bool complain)
9e1a8dd1
RR
2489{
2490 tree ret;
2491 bool wrap = true;
2492 bool maybe_const = false;
2493 bool two_arguments = false;
2494
2495 if (v1 == NULL_TREE)
2496 {
2497 two_arguments = true;
2498 v1 = v0;
2499 }
2500
2501 if (v0 == error_mark_node || v1 == error_mark_node
2502 || mask == error_mark_node)
2503 return error_mark_node;
2504
2505 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2506 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2507 {
bedc293e
MG
2508 if (complain)
2509 error_at (loc, "__builtin_shuffle last argument must "
2510 "be an integer vector");
9e1a8dd1
RR
2511 return error_mark_node;
2512 }
2513
2514 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2515 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2516 {
bedc293e
MG
2517 if (complain)
2518 error_at (loc, "__builtin_shuffle arguments must be vectors");
9e1a8dd1
RR
2519 return error_mark_node;
2520 }
2521
2522 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2523 {
bedc293e
MG
2524 if (complain)
2525 error_at (loc, "__builtin_shuffle argument vectors must be of "
2526 "the same type");
9e1a8dd1
RR
2527 return error_mark_node;
2528 }
2529
2530 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2531 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2532 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2533 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2534 {
bedc293e
MG
2535 if (complain)
2536 error_at (loc, "__builtin_shuffle number of elements of the "
2537 "argument vector(s) and the mask vector should "
2538 "be the same");
9e1a8dd1
RR
2539 return error_mark_node;
2540 }
2541
2542 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2543 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2544 {
bedc293e
MG
2545 if (complain)
2546 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2547 "must have the same size as inner type of the mask");
9e1a8dd1
RR
2548 return error_mark_node;
2549 }
2550
2551 if (!c_dialect_cxx ())
2552 {
2553 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2554 v0 = c_fully_fold (v0, false, &maybe_const);
2555 wrap &= maybe_const;
2556
2557 if (two_arguments)
2558 v1 = v0 = save_expr (v0);
2559 else
2560 {
2561 v1 = c_fully_fold (v1, false, &maybe_const);
2562 wrap &= maybe_const;
2563 }
2564
2565 mask = c_fully_fold (mask, false, &maybe_const);
2566 wrap &= maybe_const;
2567 }
bedc293e
MG
2568 else if (two_arguments)
2569 v1 = v0 = save_expr (v0);
9e1a8dd1
RR
2570
2571 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2572
2573 if (!c_dialect_cxx () && !wrap)
2574 ret = c_wrap_maybe_const (ret, true);
2575
2576 return ret;
2577}
2578
828fb3ba
JM
2579/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2580 to integral type. */
2581
2582static tree
2583c_common_get_narrower (tree op, int *unsignedp_ptr)
2584{
2585 op = get_narrower (op, unsignedp_ptr);
2586
2587 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2588 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2589 {
2590 /* C++0x scoped enumerations don't implicitly convert to integral
2591 type; if we stripped an explicit conversion to a larger type we
2592 need to replace it so common_type will still work. */
21fa2faf
RG
2593 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2594 TYPE_UNSIGNED (TREE_TYPE (op)));
828fb3ba
JM
2595 op = fold_convert (type, op);
2596 }
2597 return op;
2598}
2599
6715192c
MLI
2600/* This is a helper function of build_binary_op.
2601
2602 For certain operations if both args were extended from the same
2603 smaller type, do the arithmetic in that type and then extend.
2604
2605 BITWISE indicates a bitwise operation.
2606 For them, this optimization is safe only if
2607 both args are zero-extended or both are sign-extended.
2608 Otherwise, we might change the result.
2609 Eg, (short)-1 | (unsigned short)-1 is (int)-1
b8698a0f 2610 but calculated in (unsigned short) it would be (unsigned short)-1.
6715192c 2611*/
828fb3ba
JM
2612tree
2613shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
6715192c
MLI
2614{
2615 int unsigned0, unsigned1;
2616 tree arg0, arg1;
2617 int uns;
2618 tree type;
2619
2620 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2621 excessive narrowing when we call get_narrower below. For
2622 example, suppose that OP0 is of unsigned int extended
2623 from signed char and that RESULT_TYPE is long long int.
2624 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2625 like
b8698a0f 2626
6715192c
MLI
2627 (long long int) (unsigned int) signed_char
2628
2629 which get_narrower would narrow down to
b8698a0f 2630
6715192c 2631 (unsigned int) signed char
b8698a0f 2632
6715192c
MLI
2633 If we do not cast OP0 first, get_narrower would return
2634 signed_char, which is inconsistent with the case of the
2635 explicit cast. */
2636 op0 = convert (result_type, op0);
2637 op1 = convert (result_type, op1);
2638
828fb3ba
JM
2639 arg0 = c_common_get_narrower (op0, &unsigned0);
2640 arg1 = c_common_get_narrower (op1, &unsigned1);
6715192c
MLI
2641
2642 /* UNS is 1 if the operation to be done is an unsigned one. */
2643 uns = TYPE_UNSIGNED (result_type);
2644
2645 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2646 but it *requires* conversion to FINAL_TYPE. */
b8698a0f 2647
6715192c
MLI
2648 if ((TYPE_PRECISION (TREE_TYPE (op0))
2649 == TYPE_PRECISION (TREE_TYPE (arg0)))
2650 && TREE_TYPE (op0) != result_type)
2651 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2652 if ((TYPE_PRECISION (TREE_TYPE (op1))
2653 == TYPE_PRECISION (TREE_TYPE (arg1)))
2654 && TREE_TYPE (op1) != result_type)
2655 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b8698a0f 2656
6715192c 2657 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
b8698a0f 2658
6715192c
MLI
2659 /* For bitwise operations, signedness of nominal type
2660 does not matter. Consider only how operands were extended. */
2661 if (bitwise)
2662 uns = unsigned0;
b8698a0f 2663
6715192c
MLI
2664 /* Note that in all three cases below we refrain from optimizing
2665 an unsigned operation on sign-extended args.
2666 That would not be valid. */
b8698a0f 2667
6715192c
MLI
2668 /* Both args variable: if both extended in same way
2669 from same width, do it in that width.
2670 Do it unsigned if args were zero-extended. */
2671 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2672 < TYPE_PRECISION (result_type))
2673 && (TYPE_PRECISION (TREE_TYPE (arg1))
2674 == TYPE_PRECISION (TREE_TYPE (arg0)))
2675 && unsigned0 == unsigned1
2676 && (unsigned0 || !uns))
2677 return c_common_signed_or_unsigned_type
2678 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2679
2680 else if (TREE_CODE (arg0) == INTEGER_CST
2681 && (unsigned1 || !uns)
2682 && (TYPE_PRECISION (TREE_TYPE (arg1))
2683 < TYPE_PRECISION (result_type))
2684 && (type
2685 = c_common_signed_or_unsigned_type (unsigned1,
2686 TREE_TYPE (arg1)))
2687 && !POINTER_TYPE_P (type)
2688 && int_fits_type_p (arg0, type))
2689 return type;
2690
2691 else if (TREE_CODE (arg1) == INTEGER_CST
2692 && (unsigned0 || !uns)
2693 && (TYPE_PRECISION (TREE_TYPE (arg0))
2694 < TYPE_PRECISION (result_type))
2695 && (type
2696 = c_common_signed_or_unsigned_type (unsigned0,
2697 TREE_TYPE (arg0)))
2698 && !POINTER_TYPE_P (type)
2699 && int_fits_type_p (arg1, type))
2700 return type;
2701
2702 return result_type;
2703}
2704
7a37fa90
MM
2705/* Returns true iff any integer value of type FROM_TYPE can be represented as
2706 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2707
2708static bool
2709int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2710{
2711 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2712 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2713 REAL_VALUE_TYPE real_low_bound =
2714 real_value_from_int_cst (0, type_low_bound);
2715 REAL_VALUE_TYPE real_high_bound =
2716 real_value_from_int_cst (0, type_high_bound);
2717
2718 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2719 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2720}
2721
2722/* Checks if expression EXPR of complex/real/integer type cannot be converted
2723 to the complex/real/integer type TYPE. Function returns non-zero when:
68fca595
MP
2724 * EXPR is a constant which cannot be exactly converted to TYPE.
2725 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
7a37fa90
MM
2726 for EXPR type and TYPE being both integers or both real, or both
2727 complex.
2728 * EXPR is not a constant of complex type and TYPE is a real or
2729 an integer.
68fca595
MP
2730 * EXPR is not a constant of real type and TYPE is an integer.
2731 * EXPR is not a constant of integer type which cannot be
2732 exactly converted to real type.
7a37fa90 2733
0e3a99ae 2734 Function allows conversions between types of different signedness and
49b0aa18 2735 can return SAFE_CONVERSION (zero) in that case. Function can produce
7a37fa90
MM
2736 signedness warnings if PRODUCE_WARNS is true.
2737
2738 Function allows conversions from complex constants to non-complex types,
2739 provided that imaginary part is zero and real part can be safely converted
2740 to TYPE. */
68fca595 2741
49b0aa18 2742enum conversion_safety
68fca595 2743unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
422c3a54 2744{
49b0aa18 2745 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
374035cb 2746 tree expr_type = TREE_TYPE (expr);
68fca595 2747 loc = expansion_point_location_if_in_system_header (loc);
422c3a54 2748
0e3a99ae 2749 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
0011dedb 2750 {
7a37fa90
MM
2751 /* If type is complex, we are interested in compatibility with
2752 underlying type. */
2753 if (TREE_CODE (type) == COMPLEX_TYPE)
2754 type = TREE_TYPE (type);
2755
422c3a54 2756 /* Warn for real constant that is not an exact integer converted
0e3a99ae 2757 to integer type. */
374035cb 2758 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2759 && TREE_CODE (type) == INTEGER_TYPE)
2760 {
2761 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
49b0aa18 2762 give_warning = UNSAFE_REAL;
0e3a99ae 2763 }
91c41804 2764 /* Warn for an integer constant that does not fit into integer type. */
374035cb 2765 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2766 && TREE_CODE (type) == INTEGER_TYPE
2767 && !int_fits_type_p (expr, type))
2768 {
2769 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
374035cb 2770 && tree_int_cst_sgn (expr) < 0)
0e3a99ae
AS
2771 {
2772 if (produce_warns)
2773 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2774 " implicitly converted to unsigned type");
2775 }
2776 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2777 {
2778 if (produce_warns)
2779 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2780 " constant value to negative integer");
2781 }
7060db96 2782 else
49b0aa18 2783 give_warning = UNSAFE_OTHER;
0e3a99ae 2784 }
422c3a54 2785 else if (TREE_CODE (type) == REAL_TYPE)
0e3a99ae
AS
2786 {
2787 /* Warn for an integer constant that does not fit into real type. */
2788 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2789 {
2790 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2791 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2792 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2793 }
2794 /* Warn for a real constant that does not fit into a smaller
2795 real type. */
2796 else if (TREE_CODE (expr_type) == REAL_TYPE
2797 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2798 {
2799 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2800 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2801 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2802 }
2803 }
2804 }
7a37fa90
MM
2805
2806 else if (TREE_CODE (expr) == COMPLEX_CST)
2807 {
2808 tree imag_part = TREE_IMAGPART (expr);
2809 /* Conversion from complex constant with zero imaginary part,
2810 perform check for conversion of real part. */
2811 if ((TREE_CODE (imag_part) == REAL_CST
2812 && real_zerop (imag_part))
2813 || (TREE_CODE (imag_part) == INTEGER_CST
2814 && integer_zerop (imag_part)))
2815 /* Note: in this branch we use recursive call to unsafe_conversion_p
2816 with different type of EXPR, but it is still safe, because when EXPR
2817 is a constant, it's type is not used in text of generated warnings
2818 (otherwise they could sound misleading). */
2819 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2820 produce_warns);
2821 /* Conversion from complex constant with non-zero imaginary part. */
2822 else
2823 {
2824 /* Conversion to complex type.
2825 Perform checks for both real and imaginary parts. */
2826 if (TREE_CODE (type) == COMPLEX_TYPE)
2827 {
2828 /* Unfortunately, produce_warns must be false in two subsequent
2829 calls of unsafe_conversion_p, because otherwise we could
2830 produce strange "double" warnings, if both real and imaginary
2831 parts have conversion problems related to signedness.
2832
2833 For example:
2834 int32_t _Complex a = 0x80000000 + 0x80000000i;
2835
2836 Possible solution: add a separate function for checking
2837 constants and combine result of two calls appropriately. */
2838 enum conversion_safety re_safety =
2839 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2840 enum conversion_safety im_safety =
2841 unsafe_conversion_p (loc, type, imag_part, false);
2842
2843 /* Merge the results into appropriate single warning. */
2844
2845 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2846 if (re_safety == im_safety)
2847 give_warning = re_safety;
2848 else if (!re_safety && im_safety)
2849 give_warning = im_safety;
2850 else if (re_safety && !im_safety)
2851 give_warning = re_safety;
2852 else
2853 give_warning = UNSAFE_OTHER;
2854 }
2855 /* Warn about conversion from complex to real or integer type. */
2856 else
2857 give_warning = UNSAFE_IMAGINARY;
2858 }
2859 }
2860
2861 /* Checks for remaining case: EXPR is not constant. */
0e3a99ae
AS
2862 else
2863 {
422c3a54 2864 /* Warn for real types converted to integer types. */
6715192c 2865 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae 2866 && TREE_CODE (type) == INTEGER_TYPE)
49b0aa18 2867 give_warning = UNSAFE_REAL;
422c3a54 2868
6715192c 2869 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2870 && TREE_CODE (type) == INTEGER_TYPE)
2871 {
cfdaefec 2872 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
c00e8b06 2873 expr = get_unwidened (expr, 0);
6715192c 2874 expr_type = TREE_TYPE (expr);
cfdaefec 2875
6715192c 2876 /* Don't warn for short y; short x = ((int)y & 0xff); */
b8698a0f 2877 if (TREE_CODE (expr) == BIT_AND_EXPR
0e3a99ae 2878 || TREE_CODE (expr) == BIT_IOR_EXPR
6715192c
MLI
2879 || TREE_CODE (expr) == BIT_XOR_EXPR)
2880 {
374035cb
MLI
2881 /* If both args were extended from a shortest type,
2882 use that type if that is safe. */
b8698a0f
L
2883 expr_type = shorten_binary_op (expr_type,
2884 TREE_OPERAND (expr, 0),
2885 TREE_OPERAND (expr, 1),
6715192c
MLI
2886 /* bitwise */1);
2887
6715192c
MLI
2888 if (TREE_CODE (expr) == BIT_AND_EXPR)
2889 {
2890 tree op0 = TREE_OPERAND (expr, 0);
2891 tree op1 = TREE_OPERAND (expr, 1);
9c591bd0
MLI
2892 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2893 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2894
2895 /* If one of the operands is a non-negative constant
2896 that fits in the target type, then the type of the
2897 other operand does not matter. */
6715192c
MLI
2898 if ((TREE_CODE (op0) == INTEGER_CST
2899 && int_fits_type_p (op0, c_common_signed_type (type))
2900 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2901 || (TREE_CODE (op1) == INTEGER_CST
374035cb 2902 && int_fits_type_p (op1, c_common_signed_type (type))
b8698a0f 2903 && int_fits_type_p (op1,
374035cb 2904 c_common_unsigned_type (type))))
49b0aa18 2905 return SAFE_CONVERSION;
9c591bd0
MLI
2906 /* If constant is unsigned and fits in the target
2907 type, then the result will also fit. */
2908 else if ((TREE_CODE (op0) == INTEGER_CST
b8698a0f 2909 && unsigned0
9c591bd0
MLI
2910 && int_fits_type_p (op0, type))
2911 || (TREE_CODE (op1) == INTEGER_CST
2912 && unsigned1
2913 && int_fits_type_p (op1, type)))
49b0aa18 2914 return SAFE_CONVERSION;
6715192c
MLI
2915 }
2916 }
0e3a99ae 2917 /* Warn for integer types converted to smaller integer types. */
b8698a0f 2918 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2919 give_warning = UNSAFE_OTHER;
7060db96
MLI
2920
2921 /* When they are the same width but different signedness,
2922 then the value may change. */
0e3a99ae 2923 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
6715192c 2924 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7060db96
MLI
2925 /* Even when converted to a bigger type, if the type is
2926 unsigned but expr is signed, then negative values
2927 will be changed. */
0e3a99ae
AS
2928 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2929 && produce_warns)
6312e84d
MLI
2930 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2931 "may change the sign of the result",
2932 type, expr_type);
0e3a99ae 2933 }
422c3a54
MLI
2934
2935 /* Warn for integer types converted to real types if and only if
0e3a99ae
AS
2936 all the range of values of the integer type cannot be
2937 represented by the real type. */
6715192c 2938 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2939 && TREE_CODE (type) == REAL_TYPE)
2940 {
58076e21
MLI
2941 /* Don't warn about char y = 0xff; float x = (int) y; */
2942 expr = get_unwidened (expr, 0);
2943 expr_type = TREE_TYPE (expr);
2944
7a37fa90 2945 if (!int_safely_convertible_to_real_p (expr_type, type))
49b0aa18 2946 give_warning = UNSAFE_OTHER;
0e3a99ae 2947 }
422c3a54
MLI
2948
2949 /* Warn for real types converted to smaller real types. */
6715192c 2950 else if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2951 && TREE_CODE (type) == REAL_TYPE
2952 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2953 give_warning = UNSAFE_REAL;
7a37fa90
MM
2954
2955 /* Check conversion between two complex types. */
2956 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2957 && TREE_CODE (type) == COMPLEX_TYPE)
2958 {
2959 /* Extract underlying types (i.e., type of real and imaginary
2960 parts) of expr_type and type. */
2961 tree from_type = TREE_TYPE (expr_type);
2962 tree to_type = TREE_TYPE (type);
2963
2964 /* Warn for real types converted to integer types. */
2965 if (TREE_CODE (from_type) == REAL_TYPE
2966 && TREE_CODE (to_type) == INTEGER_TYPE)
2967 give_warning = UNSAFE_REAL;
2968
2969 /* Warn for real types converted to smaller real types. */
2970 else if (TREE_CODE (from_type) == REAL_TYPE
2971 && TREE_CODE (to_type) == REAL_TYPE
2972 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2973 give_warning = UNSAFE_REAL;
2974
2975 /* Check conversion for complex integer types. Here implementation
2976 is simpler than for real-domain integers because it does not
2977 involve sophisticated cases, such as bitmasks, casts, etc. */
2978 else if (TREE_CODE (from_type) == INTEGER_TYPE
2979 && TREE_CODE (to_type) == INTEGER_TYPE)
2980 {
2981 /* Warn for integer types converted to smaller integer types. */
2982 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2983 give_warning = UNSAFE_OTHER;
2984
2985 /* Check for different signedness, see case for real-domain
2986 integers (above) for a more detailed comment. */
2987 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2988 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2989 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2990 && produce_warns)
2991 warning_at (loc, OPT_Wsign_conversion,
2992 "conversion to %qT from %qT "
2993 "may change the sign of the result",
2994 type, expr_type);
2995 }
2996 else if (TREE_CODE (from_type) == INTEGER_TYPE
2997 && TREE_CODE (to_type) == REAL_TYPE
2998 && !int_safely_convertible_to_real_p (from_type, to_type))
2999 give_warning = UNSAFE_OTHER;
3000 }
3001
3002 /* Warn for complex types converted to real or integer types. */
3003 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3004 && TREE_CODE (type) != COMPLEX_TYPE)
3005 give_warning = UNSAFE_IMAGINARY;
0e3a99ae
AS
3006 }
3007
3008 return give_warning;
3009}
3010
3011/* Warns if the conversion of EXPR to TYPE may alter a value.
3012 This is a helper function for warnings_for_convert_and_check. */
3013
3014static void
68fca595 3015conversion_warning (location_t loc, tree type, tree expr)
0e3a99ae 3016{
0e3a99ae 3017 tree expr_type = TREE_TYPE (expr);
49b0aa18 3018 enum conversion_safety conversion_kind;
422c3a54 3019
49b0aa18 3020 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
0e3a99ae 3021 return;
422c3a54 3022
66f20604
MP
3023 /* This may happen, because for LHS op= RHS we preevaluate
3024 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3025 means we could no longer see the code of the EXPR. */
3026 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3027 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3028 if (TREE_CODE (expr) == SAVE_EXPR)
3029 expr = TREE_OPERAND (expr, 0);
3030
0e3a99ae
AS
3031 switch (TREE_CODE (expr))
3032 {
3033 case EQ_EXPR:
3034 case NE_EXPR:
3035 case LE_EXPR:
3036 case GE_EXPR:
3037 case LT_EXPR:
3038 case GT_EXPR:
3039 case TRUTH_ANDIF_EXPR:
3040 case TRUTH_ORIF_EXPR:
3041 case TRUTH_AND_EXPR:
3042 case TRUTH_OR_EXPR:
3043 case TRUTH_XOR_EXPR:
3044 case TRUTH_NOT_EXPR:
3045 /* Conversion from boolean to a signed:1 bit-field (which only
3046 can hold the values 0 and -1) doesn't lose information - but
3047 it does change the value. */
3048 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3049 warning_at (loc, OPT_Wconversion,
3050 "conversion to %qT from boolean expression", type);
3051 return;
3052
3053 case REAL_CST:
3054 case INTEGER_CST:
7a37fa90 3055 case COMPLEX_CST:
68fca595 3056 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
3057 if (conversion_kind == UNSAFE_REAL)
3058 warning_at (loc, OPT_Wfloat_conversion,
3059 "conversion to %qT alters %qT constant value",
3060 type, expr_type);
3061 else if (conversion_kind)
0e3a99ae
AS
3062 warning_at (loc, OPT_Wconversion,
3063 "conversion to %qT alters %qT constant value",
3064 type, expr_type);
3065 return;
3066
3067 case COND_EXPR:
3068 {
3f46d6a5
MLI
3069 /* In case of COND_EXPR, we do not care about the type of
3070 COND_EXPR, only about the conversion of each operand. */
3071 tree op1 = TREE_OPERAND (expr, 1);
3072 tree op2 = TREE_OPERAND (expr, 2);
3073
68fca595
MP
3074 conversion_warning (loc, type, op1);
3075 conversion_warning (loc, type, op2);
3f46d6a5 3076 return;
0e3a99ae
AS
3077 }
3078
3079 default: /* 'expr' is not a constant. */
68fca595 3080 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
3081 if (conversion_kind == UNSAFE_REAL)
3082 warning_at (loc, OPT_Wfloat_conversion,
3083 "conversion to %qT from %qT may alter its value",
3084 type, expr_type);
7a37fa90
MM
3085 else if (conversion_kind == UNSAFE_IMAGINARY)
3086 warning_at (loc, OPT_Wconversion,
3087 "conversion to %qT from %qT discards imaginary component",
3088 type, expr_type);
49b0aa18 3089 else if (conversion_kind)
0e3a99ae 3090 warning_at (loc, OPT_Wconversion,
6312e84d
MLI
3091 "conversion to %qT from %qT may alter its value",
3092 type, expr_type);
422c3a54
MLI
3093 }
3094}
3095
07231d4f
MLI
3096/* Produce warnings after a conversion. RESULT is the result of
3097 converting EXPR to TYPE. This is a helper function for
3098 convert_and_check and cp_convert_and_check. */
d74154d5 3099
07231d4f 3100void
68fca595
MP
3101warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3102 tree result)
d74154d5 3103{
68fca595 3104 loc = expansion_point_location_if_in_system_header (loc);
5a3c9cf2 3105
91c41804
RS
3106 if (TREE_CODE (expr) == INTEGER_CST
3107 && (TREE_CODE (type) == INTEGER_TYPE
3108 || TREE_CODE (type) == ENUMERAL_TYPE)
3109 && !int_fits_type_p (expr, type))
3110 {
422c3a54
MLI
3111 /* Do not diagnose overflow in a constant expression merely
3112 because a conversion overflowed. */
91c41804 3113 if (TREE_OVERFLOW (result))
d95787e6
RS
3114 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3115
91c41804 3116 if (TYPE_UNSIGNED (type))
422c3a54 3117 {
91c41804
RS
3118 /* This detects cases like converting -129 or 256 to
3119 unsigned char. */
3120 if (!int_fits_type_p (expr, c_common_signed_type (type)))
5a3c9cf2
PC
3121 warning_at (loc, OPT_Woverflow,
3122 "large integer implicitly truncated to unsigned type");
7060db96 3123 else
68fca595 3124 conversion_warning (loc, type, expr);
91c41804 3125 }
b8698a0f 3126 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
68fca595 3127 warning_at (loc, OPT_Woverflow,
f73fe417
MLI
3128 "overflow in implicit constant conversion");
3129 /* No warning for converting 0x80000000 to int. */
3130 else if (pedantic
3131 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3132 || TYPE_PRECISION (TREE_TYPE (expr))
3133 != TYPE_PRECISION (type)))
5a3c9cf2
PC
3134 warning_at (loc, OPT_Woverflow,
3135 "overflow in implicit constant conversion");
f73fe417 3136
7060db96 3137 else
68fca595 3138 conversion_warning (loc, type, expr);
d74154d5 3139 }
ab22c1fa
CF
3140 else if ((TREE_CODE (result) == INTEGER_CST
3141 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
5a3c9cf2
PC
3142 warning_at (loc, OPT_Woverflow,
3143 "overflow in implicit constant conversion");
7060db96 3144 else
68fca595 3145 conversion_warning (loc, type, expr);
07231d4f
MLI
3146}
3147
3148
3149/* Convert EXPR to TYPE, warning about conversion problems with constants.
3150 Invoke this function on every expression that is converted implicitly,
3151 i.e. because of language rules and not because of an explicit cast. */
3152
3153tree
68fca595 3154convert_and_check (location_t loc, tree type, tree expr)
07231d4f
MLI
3155{
3156 tree result;
8ce94e44
JM
3157 tree expr_for_warning;
3158
3159 /* Convert from a value with possible excess precision rather than
3160 via the semantic type, but do not warn about values not fitting
3161 exactly in the semantic type. */
3162 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3163 {
3164 tree orig_type = TREE_TYPE (expr);
3165 expr = TREE_OPERAND (expr, 0);
3166 expr_for_warning = convert (orig_type, expr);
3167 if (orig_type == type)
3168 return expr_for_warning;
3169 }
3170 else
3171 expr_for_warning = expr;
07231d4f
MLI
3172
3173 if (TREE_TYPE (expr) == type)
3174 return expr;
b8698a0f 3175
07231d4f
MLI
3176 result = convert (type, expr);
3177
7d882b83
ILT
3178 if (c_inhibit_evaluation_warnings == 0
3179 && !TREE_OVERFLOW_P (expr)
3180 && result != error_mark_node)
68fca595 3181 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
07231d4f 3182
91c41804 3183 return result;
96571883
BK
3184}
3185\f
235cfbc4
BS
3186/* A node in a list that describes references to variables (EXPR), which are
3187 either read accesses if WRITER is zero, or write accesses, in which case
3188 WRITER is the parent of EXPR. */
3189struct tlist
3190{
3191 struct tlist *next;
3192 tree expr, writer;
3193};
3194
3195/* Used to implement a cache the results of a call to verify_tree. We only
3196 use this for SAVE_EXPRs. */
3197struct tlist_cache
3198{
3199 struct tlist_cache *next;
3200 struct tlist *cache_before_sp;
3201 struct tlist *cache_after_sp;
3202 tree expr;
2683ed8d
BS
3203};
3204
235cfbc4
BS
3205/* Obstack to use when allocating tlist structures, and corresponding
3206 firstobj. */
3207static struct obstack tlist_obstack;
3208static char *tlist_firstobj = 0;
3209
3210/* Keep track of the identifiers we've warned about, so we can avoid duplicate
3211 warnings. */
3212static struct tlist *warned_ids;
3213/* SAVE_EXPRs need special treatment. We process them only once and then
3214 cache the results. */
3215static struct tlist_cache *save_expr_cache;
3216
35b1a6fa
AJ
3217static void add_tlist (struct tlist **, struct tlist *, tree, int);
3218static void merge_tlist (struct tlist **, struct tlist *, int);
3219static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3220static int warning_candidate_p (tree);
1e4ae551 3221static bool candidate_equal_p (const_tree, const_tree);
35b1a6fa
AJ
3222static void warn_for_collisions (struct tlist *);
3223static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3224static struct tlist *new_tlist (struct tlist *, tree, tree);
2683ed8d 3225
235cfbc4
BS
3226/* Create a new struct tlist and fill in its fields. */
3227static struct tlist *
35b1a6fa 3228new_tlist (struct tlist *next, tree t, tree writer)
235cfbc4
BS
3229{
3230 struct tlist *l;
5d038c4c 3231 l = XOBNEW (&tlist_obstack, struct tlist);
235cfbc4
BS
3232 l->next = next;
3233 l->expr = t;
3234 l->writer = writer;
3235 return l;
3236}
3237
3238/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3239 is nonnull, we ignore any node we find which has a writer equal to it. */
3240
3241static void
35b1a6fa 3242add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
235cfbc4
BS
3243{
3244 while (add)
3245 {
3246 struct tlist *next = add->next;
3f75a254 3247 if (!copy)
235cfbc4 3248 add->next = *to;
1e4ae551 3249 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
235cfbc4
BS
3250 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3251 add = next;
3252 }
3253}
3254
3255/* Merge the nodes of ADD into TO. This merging process is done so that for
3256 each variable that already exists in TO, no new node is added; however if
3257 there is a write access recorded in ADD, and an occurrence on TO is only
3258 a read access, then the occurrence in TO will be modified to record the
3259 write. */
2683ed8d
BS
3260
3261static void
35b1a6fa 3262merge_tlist (struct tlist **to, struct tlist *add, int copy)
235cfbc4
BS
3263{
3264 struct tlist **end = to;
3265
3266 while (*end)
3267 end = &(*end)->next;
3268
3269 while (add)
3270 {
3271 int found = 0;
3272 struct tlist *tmp2;
3273 struct tlist *next = add->next;
3274
3275 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1e4ae551 3276 if (candidate_equal_p (tmp2->expr, add->expr))
235cfbc4
BS
3277 {
3278 found = 1;
3f75a254 3279 if (!tmp2->writer)
235cfbc4
BS
3280 tmp2->writer = add->writer;
3281 }
3f75a254 3282 if (!found)
235cfbc4 3283 {
c2bf53a1 3284 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
235cfbc4
BS
3285 end = &(*end)->next;
3286 *end = 0;
3287 }
3288 add = next;
3289 }
3290}
3291
3292/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3293 references in list LIST conflict with it, excluding reads if ONLY writers
3294 is nonzero. */
3295
3296static void
35b1a6fa
AJ
3297warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3298 int only_writes)
235cfbc4
BS
3299{
3300 struct tlist *tmp;
3301
3302 /* Avoid duplicate warnings. */
3303 for (tmp = warned_ids; tmp; tmp = tmp->next)
1e4ae551 3304 if (candidate_equal_p (tmp->expr, written))
235cfbc4
BS
3305 return;
3306
3307 while (list)
3308 {
1e4ae551
MLI
3309 if (candidate_equal_p (list->expr, written)
3310 && !candidate_equal_p (list->writer, writer)
3311 && (!only_writes || list->writer))
235cfbc4
BS
3312 {
3313 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
8400e75e 3314 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
ca085fd7
MLI
3315 OPT_Wsequence_point, "operation on %qE may be undefined",
3316 list->expr);
235cfbc4
BS
3317 }
3318 list = list->next;
3319 }
3320}
3321
3322/* Given a list LIST of references to variables, find whether any of these
3323 can cause conflicts due to missing sequence points. */
3324
3325static void
35b1a6fa 3326warn_for_collisions (struct tlist *list)
235cfbc4
BS
3327{
3328 struct tlist *tmp;
35b1a6fa 3329
235cfbc4
BS
3330 for (tmp = list; tmp; tmp = tmp->next)
3331 {
3332 if (tmp->writer)
3333 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3334 }
3335}
3336
684d9f3b 3337/* Return nonzero if X is a tree that can be verified by the sequence point
235cfbc4
BS
3338 warnings. */
3339static int
35b1a6fa 3340warning_candidate_p (tree x)
2683ed8d 3341{
07078664
JJ
3342 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3343 return 0;
3344
92e948a8
NF
3345 if (TREE_CODE (x) == BLOCK)
3346 return 0;
3347
07078664 3348 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1e4ae551 3349 (lvalue_p) crash on TRY/CATCH. */
07078664
JJ
3350 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3351 return 0;
3352
3353 if (!lvalue_p (x))
3354 return 0;
3355
3356 /* No point to track non-const calls, they will never satisfy
3357 operand_equal_p. */
3358 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3359 return 0;
3360
3361 if (TREE_CODE (x) == STRING_CST)
3362 return 0;
3363
3364 return 1;
1e4ae551
MLI
3365}
3366
3367/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3368static bool
3369candidate_equal_p (const_tree x, const_tree y)
3370{
3371 return (x == y) || (x && y && operand_equal_p (x, y, 0));
235cfbc4 3372}
2683ed8d 3373
235cfbc4
BS
3374/* Walk the tree X, and record accesses to variables. If X is written by the
3375 parent tree, WRITER is the parent.
3376 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3377 expression or its only operand forces a sequence point, then everything up
3378 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3379 in PNO_SP.
3380 Once we return, we will have emitted warnings if any subexpression before
3381 such a sequence point could be undefined. On a higher level, however, the
3382 sequence point may not be relevant, and we'll merge the two lists.
3383
3384 Example: (b++, a) + b;
3385 The call that processes the COMPOUND_EXPR will store the increment of B
3386 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3387 processes the PLUS_EXPR will need to merge the two lists so that
3388 eventually, all accesses end up on the same list (and we'll warn about the
3389 unordered subexpressions b++ and b.
3390
3391 A note on merging. If we modify the former example so that our expression
3392 becomes
3393 (b++, b) + a
3394 care must be taken not simply to add all three expressions into the final
3395 PNO_SP list. The function merge_tlist takes care of that by merging the
3396 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3397 way, so that no more than one access to B is recorded. */
2683ed8d 3398
235cfbc4 3399static void
35b1a6fa
AJ
3400verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3401 tree writer)
235cfbc4
BS
3402{
3403 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3404 enum tree_code code;
6615c446 3405 enum tree_code_class cl;
2683ed8d 3406
f9e1917e
JM
3407 /* X may be NULL if it is the operand of an empty statement expression
3408 ({ }). */
3409 if (x == NULL)
3410 return;
3411
235cfbc4
BS
3412 restart:
3413 code = TREE_CODE (x);
e3a64162 3414 cl = TREE_CODE_CLASS (code);
2683ed8d 3415
235cfbc4 3416 if (warning_candidate_p (x))
1e4ae551 3417 *pno_sp = new_tlist (*pno_sp, x, writer);
235cfbc4
BS
3418
3419 switch (code)
3420 {
52a84e42 3421 case CONSTRUCTOR:
f7716d57 3422 case SIZEOF_EXPR:
52a84e42
BS
3423 return;
3424
235cfbc4
BS
3425 case COMPOUND_EXPR:
3426 case TRUTH_ANDIF_EXPR:
3427 case TRUTH_ORIF_EXPR:
3428 tmp_before = tmp_nosp = tmp_list3 = 0;
3429 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3430 warn_for_collisions (tmp_nosp);
3431 merge_tlist (pbefore_sp, tmp_before, 0);
3432 merge_tlist (pbefore_sp, tmp_nosp, 0);
3433 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3434 merge_tlist (pbefore_sp, tmp_list3, 0);
3435 return;
3436
3437 case COND_EXPR:
3438 tmp_before = tmp_list2 = 0;
3439 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3440 warn_for_collisions (tmp_list2);
3441 merge_tlist (pbefore_sp, tmp_before, 0);
c2bf53a1 3442 merge_tlist (pbefore_sp, tmp_list2, 0);
235cfbc4
BS
3443
3444 tmp_list3 = tmp_nosp = 0;
3445 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3446 warn_for_collisions (tmp_nosp);
3447 merge_tlist (pbefore_sp, tmp_list3, 0);
3448
3449 tmp_list3 = tmp_list2 = 0;
3450 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3451 warn_for_collisions (tmp_list2);
3452 merge_tlist (pbefore_sp, tmp_list3, 0);
3453 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3454 two first, to avoid warning for (a ? b++ : b++). */
3455 merge_tlist (&tmp_nosp, tmp_list2, 0);
3456 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3457 return;
3458
2683ed8d
BS
3459 case PREDECREMENT_EXPR:
3460 case PREINCREMENT_EXPR:
3461 case POSTDECREMENT_EXPR:
3462 case POSTINCREMENT_EXPR:
235cfbc4
BS
3463 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3464 return;
3465
3466 case MODIFY_EXPR:
3467 tmp_before = tmp_nosp = tmp_list3 = 0;
3468 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3469 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3470 /* Expressions inside the LHS are not ordered wrt. the sequence points
3471 in the RHS. Example:
3472 *a = (a++, 2)
3473 Despite the fact that the modification of "a" is in the before_sp
3474 list (tmp_before), it conflicts with the use of "a" in the LHS.
3475 We can handle this by adding the contents of tmp_list3
3476 to those of tmp_before, and redoing the collision warnings for that
3477 list. */
3478 add_tlist (&tmp_before, tmp_list3, x, 1);
3479 warn_for_collisions (tmp_before);
3480 /* Exclude the LHS itself here; we first have to merge it into the
3481 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3482 didn't exclude the LHS, we'd get it twice, once as a read and once
3483 as a write. */
3484 add_tlist (pno_sp, tmp_list3, x, 0);
3485 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3486
3487 merge_tlist (pbefore_sp, tmp_before, 0);
3488 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3489 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3490 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3491 return;
2683ed8d
BS
3492
3493 case CALL_EXPR:
235cfbc4
BS
3494 /* We need to warn about conflicts among arguments and conflicts between
3495 args and the function address. Side effects of the function address,
3496 however, are not ordered by the sequence point of the call. */
5039610b
SL
3497 {
3498 call_expr_arg_iterator iter;
3499 tree arg;
b8698a0f 3500 tmp_before = tmp_nosp = 0;
5039610b
SL
3501 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3502 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3503 {
3504 tmp_list2 = tmp_list3 = 0;
3505 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3506 merge_tlist (&tmp_list3, tmp_list2, 0);
3507 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3508 }
3509 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3510 warn_for_collisions (tmp_before);
3511 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3512 return;
3513 }
2683ed8d
BS
3514
3515 case TREE_LIST:
3516 /* Scan all the list, e.g. indices of multi dimensional array. */
3517 while (x)
3518 {
235cfbc4
BS
3519 tmp_before = tmp_nosp = 0;
3520 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3521 merge_tlist (&tmp_nosp, tmp_before, 0);
3522 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2683ed8d
BS
3523 x = TREE_CHAIN (x);
3524 }
235cfbc4 3525 return;
2683ed8d 3526
235cfbc4
BS
3527 case SAVE_EXPR:
3528 {
3529 struct tlist_cache *t;
3530 for (t = save_expr_cache; t; t = t->next)
1e4ae551 3531 if (candidate_equal_p (t->expr, x))
235cfbc4 3532 break;
2683ed8d 3533
3f75a254 3534 if (!t)
2683ed8d 3535 {
5d038c4c 3536 t = XOBNEW (&tlist_obstack, struct tlist_cache);
235cfbc4
BS
3537 t->next = save_expr_cache;
3538 t->expr = x;
3539 save_expr_cache = t;
3540
3541 tmp_before = tmp_nosp = 0;
3542 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3543 warn_for_collisions (tmp_nosp);
3544
3545 tmp_list3 = 0;
c2bf53a1 3546 merge_tlist (&tmp_list3, tmp_nosp, 0);
235cfbc4
BS
3547 t->cache_before_sp = tmp_before;
3548 t->cache_after_sp = tmp_list3;
2683ed8d 3549 }
235cfbc4
BS
3550 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3551 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3552 return;
3553 }
2683ed8d 3554
528c22f4
MLI
3555 case ADDR_EXPR:
3556 x = TREE_OPERAND (x, 0);
3557 if (DECL_P (x))
3558 return;
3559 writer = 0;
3560 goto restart;
3561
6615c446
JO
3562 default:
3563 /* For other expressions, simply recurse on their operands.
c22cacf3 3564 Manual tail recursion for unary expressions.
6615c446
JO
3565 Other non-expressions need not be processed. */
3566 if (cl == tcc_unary)
3567 {
6615c446
JO
3568 x = TREE_OPERAND (x, 0);
3569 writer = 0;
3570 goto restart;
3571 }
3572 else if (IS_EXPR_CODE_CLASS (cl))
3573 {
3574 int lp;
5039610b 3575 int max = TREE_OPERAND_LENGTH (x);
6615c446
JO
3576 for (lp = 0; lp < max; lp++)
3577 {
3578 tmp_before = tmp_nosp = 0;
3579 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3580 merge_tlist (&tmp_nosp, tmp_before, 0);
3581 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3582 }
3583 }
3584 return;
2683ed8d 3585 }
2683ed8d
BS
3586}
3587
8d9afc4e 3588/* Try to warn for undefined behavior in EXPR due to missing sequence
2683ed8d
BS
3589 points. */
3590
24e47c76 3591DEBUG_FUNCTION void
35b1a6fa 3592verify_sequence_points (tree expr)
2683ed8d 3593{
235cfbc4 3594 struct tlist *before_sp = 0, *after_sp = 0;
2683ed8d 3595
235cfbc4
BS
3596 warned_ids = 0;
3597 save_expr_cache = 0;
3598 if (tlist_firstobj == 0)
2683ed8d 3599 {
235cfbc4 3600 gcc_obstack_init (&tlist_obstack);
28dab132 3601 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2683ed8d
BS
3602 }
3603
235cfbc4
BS
3604 verify_tree (expr, &before_sp, &after_sp, 0);
3605 warn_for_collisions (after_sp);
3606 obstack_free (&tlist_obstack, tlist_firstobj);
2683ed8d 3607}
b30f223b
RS
3608\f
3609/* Validate the expression after `case' and apply default promotions. */
3610
a6c0a76c 3611static tree
62e4eb35 3612check_case_value (location_t loc, tree value)
b30f223b
RS
3613{
3614 if (value == NULL_TREE)
3615 return value;
3616
522ddfa2
JM
3617 if (TREE_CODE (value) == INTEGER_CST)
3618 /* Promote char or short to int. */
3619 value = perform_integral_promotions (value);
3620 else if (value != error_mark_node)
b30f223b 3621 {
62e4eb35 3622 error_at (loc, "case label does not reduce to an integer constant");
b30f223b
RS
3623 value = error_mark_node;
3624 }
b30f223b 3625
bc690db1
RS
3626 constant_expression_warning (value);
3627
b30f223b
RS
3628 return value;
3629}
3630\f
a6c0a76c 3631/* See if the case values LOW and HIGH are in the range of the original
89dbed81 3632 type (i.e. before the default conversion to int) of the switch testing
a6c0a76c
SB
3633 expression.
3634 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2a7e31df 3635 the type before promoting it. CASE_LOW_P is a pointer to the lower
a6c0a76c
SB
3636 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3637 if the case is not a case range.
3638 The caller has to make sure that we are not called with NULL for
89dbed81 3639 CASE_LOW_P (i.e. the default case).
0fa2e4df 3640 Returns true if the case label is in range of ORIG_TYPE (saturated or
a6c0a76c
SB
3641 untouched) or false if the label is out of range. */
3642
3643static bool
9d548dfb 3644check_case_bounds (location_t loc, tree type, tree orig_type,
a6c0a76c
SB
3645 tree *case_low_p, tree *case_high_p)
3646{
3647 tree min_value, max_value;
3648 tree case_low = *case_low_p;
3649 tree case_high = case_high_p ? *case_high_p : case_low;
3650
3651 /* If there was a problem with the original type, do nothing. */
3652 if (orig_type == error_mark_node)
3653 return true;
3654
3655 min_value = TYPE_MIN_VALUE (orig_type);
3656 max_value = TYPE_MAX_VALUE (orig_type);
3657
3658 /* Case label is less than minimum for type. */
3659 if (tree_int_cst_compare (case_low, min_value) < 0
3660 && tree_int_cst_compare (case_high, min_value) < 0)
3661 {
9d548dfb
MP
3662 warning_at (loc, 0, "case label value is less than minimum value "
3663 "for type");
a6c0a76c
SB
3664 return false;
3665 }
9f63daea 3666
a6c0a76c
SB
3667 /* Case value is greater than maximum for type. */
3668 if (tree_int_cst_compare (case_low, max_value) > 0
3669 && tree_int_cst_compare (case_high, max_value) > 0)
3670 {
9d548dfb 3671 warning_at (loc, 0, "case label value exceeds maximum value for type");
a6c0a76c
SB
3672 return false;
3673 }
3674
3675 /* Saturate lower case label value to minimum. */
3676 if (tree_int_cst_compare (case_high, min_value) >= 0
3677 && tree_int_cst_compare (case_low, min_value) < 0)
3678 {
9d548dfb
MP
3679 warning_at (loc, 0, "lower value in case label range"
3680 " less than minimum value for type");
a6c0a76c
SB
3681 case_low = min_value;
3682 }
9f63daea 3683
a6c0a76c
SB
3684 /* Saturate upper case label value to maximum. */
3685 if (tree_int_cst_compare (case_low, max_value) <= 0
3686 && tree_int_cst_compare (case_high, max_value) > 0)
3687 {
9d548dfb
MP
3688 warning_at (loc, 0, "upper value in case label range"
3689 " exceeds maximum value for type");
a6c0a76c
SB
3690 case_high = max_value;
3691 }
3692
3693 if (*case_low_p != case_low)
3694 *case_low_p = convert (type, case_low);
3695 if (case_high_p && *case_high_p != case_high)
3696 *case_high_p = convert (type, case_high);
3697
3698 return true;
3699}
3700\f
b30f223b
RS
3701/* Return an integer type with BITS bits of precision,
3702 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3703
3704tree
35b1a6fa 3705c_common_type_for_size (unsigned int bits, int unsignedp)
b30f223b 3706{
78a7c317
DD
3707 int i;
3708
a311b52c
JM
3709 if (bits == TYPE_PRECISION (integer_type_node))
3710 return unsignedp ? unsigned_type_node : integer_type_node;
3711
3fc7e390 3712 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
3713 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3714
3fc7e390 3715 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
3716 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3717
3fc7e390 3718 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
3719 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3720
3fc7e390 3721 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
3722 return (unsignedp ? long_long_unsigned_type_node
3723 : long_long_integer_type_node);
3724
78a7c317
DD
3725 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3726 if (int_n_enabled_p[i]
3727 && bits == int_n_data[i].bitsize)
3728 return (unsignedp ? int_n_trees[i].unsigned_type
3729 : int_n_trees[i].signed_type);
a6766312 3730
835f9b4d
GRK
3731 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3732 return (unsignedp ? widest_unsigned_literal_type_node
3733 : widest_integer_literal_type_node);
3734
3fc7e390
RS
3735 if (bits <= TYPE_PRECISION (intQI_type_node))
3736 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3737
3738 if (bits <= TYPE_PRECISION (intHI_type_node))
3739 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3740
3741 if (bits <= TYPE_PRECISION (intSI_type_node))
3742 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3743
3744 if (bits <= TYPE_PRECISION (intDI_type_node))
3745 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3746
b30f223b
RS
3747 return 0;
3748}
3749
ab22c1fa
CF
3750/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3751 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3752 and saturating if SATP is nonzero, otherwise not saturating. */
3753
3754tree
3755c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3756 int unsignedp, int satp)
3757{
ef4bddc2 3758 machine_mode mode;
ab22c1fa
CF
3759 if (ibit == 0)
3760 mode = unsignedp ? UQQmode : QQmode;
3761 else
3762 mode = unsignedp ? UHAmode : HAmode;
3763
3764 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3765 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3766 break;
3767
3768 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3769 {
3770 sorry ("GCC cannot support operators with integer types and "
3771 "fixed-point types that have too many integral and "
3772 "fractional bits together");
3773 return 0;
3774 }
3775
3776 return c_common_type_for_mode (mode, satp);
3777}
3778
d1d3865f
ZW
3779/* Used for communication between c_common_type_for_mode and
3780 c_register_builtin_type. */
793c625f 3781tree registered_builtin_types;
d1d3865f 3782
b30f223b
RS
3783/* Return a data type that has machine mode MODE.
3784 If the mode is an integer,
ab22c1fa
CF
3785 then UNSIGNEDP selects between signed and unsigned types.
3786 If the mode is a fixed-point mode,
3787 then UNSIGNEDP selects between saturating and nonsaturating types. */
b30f223b
RS
3788
3789tree
ef4bddc2 3790c_common_type_for_mode (machine_mode mode, int unsignedp)
b30f223b 3791{
d1d3865f 3792 tree t;
78a7c317 3793 int i;
d1d3865f 3794
a311b52c
JM
3795 if (mode == TYPE_MODE (integer_type_node))
3796 return unsignedp ? unsigned_type_node : integer_type_node;
3797
b30f223b
RS
3798 if (mode == TYPE_MODE (signed_char_type_node))
3799 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3800
3801 if (mode == TYPE_MODE (short_integer_type_node))
3802 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3803
b30f223b
RS
3804 if (mode == TYPE_MODE (long_integer_type_node))
3805 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3806
3807 if (mode == TYPE_MODE (long_long_integer_type_node))
3808 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3809
78a7c317
DD
3810 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3811 if (int_n_enabled_p[i]
3812 && mode == int_n_data[i].m)
3813 return (unsignedp ? int_n_trees[i].unsigned_type
3814 : int_n_trees[i].signed_type);
a6766312 3815
835f9b4d 3816 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 3817 return unsignedp ? widest_unsigned_literal_type_node
6de9cd9a 3818 : widest_integer_literal_type_node;
835f9b4d 3819
0afeef64 3820 if (mode == QImode)
3fc7e390
RS
3821 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3822
0afeef64 3823 if (mode == HImode)
3fc7e390
RS
3824 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3825
0afeef64 3826 if (mode == SImode)
3fc7e390
RS
3827 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3828
0afeef64 3829 if (mode == DImode)
3fc7e390
RS
3830 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3831
21a9616b 3832#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
3833 if (mode == TYPE_MODE (intTI_type_node))
3834 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 3835#endif
a6d7e156 3836
b30f223b
RS
3837 if (mode == TYPE_MODE (float_type_node))
3838 return float_type_node;
3839
3840 if (mode == TYPE_MODE (double_type_node))
3841 return double_type_node;
3842
3843 if (mode == TYPE_MODE (long_double_type_node))
3844 return long_double_type_node;
3845
ff42324e
NS
3846 if (mode == TYPE_MODE (void_type_node))
3847 return void_type_node;
9f63daea 3848
b30f223b 3849 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
19b3ffbc
DD
3850 return (unsignedp
3851 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3852 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b
RS
3853
3854 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
19b3ffbc
DD
3855 return (unsignedp
3856 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3857 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b 3858
7e7e470f
RH
3859 if (COMPLEX_MODE_P (mode))
3860 {
ef4bddc2 3861 machine_mode inner_mode;
7e7e470f
RH
3862 tree inner_type;
3863
3864 if (mode == TYPE_MODE (complex_float_type_node))
3865 return complex_float_type_node;
3866 if (mode == TYPE_MODE (complex_double_type_node))
3867 return complex_double_type_node;
3868 if (mode == TYPE_MODE (complex_long_double_type_node))
3869 return complex_long_double_type_node;
3870
3871 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3872 return complex_integer_type_node;
3873
3874 inner_mode = GET_MODE_INNER (mode);
3875 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3876 if (inner_type != NULL_TREE)
3877 return build_complex_type (inner_type);
3878 }
3879 else if (VECTOR_MODE_P (mode))
4a5eab38 3880 {
ef4bddc2 3881 machine_mode inner_mode = GET_MODE_INNER (mode);
4a5eab38
PB
3882 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3883 if (inner_type != NULL_TREE)
3884 return build_vector_type_for_mode (inner_type, mode);
0afeef64 3885 }
4061f623 3886
9a8ce21f
JG
3887 if (mode == TYPE_MODE (dfloat32_type_node))
3888 return dfloat32_type_node;
3889 if (mode == TYPE_MODE (dfloat64_type_node))
3890 return dfloat64_type_node;
3891 if (mode == TYPE_MODE (dfloat128_type_node))
3892 return dfloat128_type_node;
3893
ab22c1fa
CF
3894 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3895 {
3896 if (mode == TYPE_MODE (short_fract_type_node))
3897 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3898 if (mode == TYPE_MODE (fract_type_node))
3899 return unsignedp ? sat_fract_type_node : fract_type_node;
3900 if (mode == TYPE_MODE (long_fract_type_node))
3901 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3902 if (mode == TYPE_MODE (long_long_fract_type_node))
3903 return unsignedp ? sat_long_long_fract_type_node
3904 : long_long_fract_type_node;
3905
3906 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3907 return unsignedp ? sat_unsigned_short_fract_type_node
3908 : unsigned_short_fract_type_node;
3909 if (mode == TYPE_MODE (unsigned_fract_type_node))
3910 return unsignedp ? sat_unsigned_fract_type_node
3911 : unsigned_fract_type_node;
3912 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3913 return unsignedp ? sat_unsigned_long_fract_type_node
3914 : unsigned_long_fract_type_node;
3915 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3916 return unsignedp ? sat_unsigned_long_long_fract_type_node
3917 : unsigned_long_long_fract_type_node;
3918
3919 if (mode == TYPE_MODE (short_accum_type_node))
3920 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3921 if (mode == TYPE_MODE (accum_type_node))
3922 return unsignedp ? sat_accum_type_node : accum_type_node;
3923 if (mode == TYPE_MODE (long_accum_type_node))
3924 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3925 if (mode == TYPE_MODE (long_long_accum_type_node))
3926 return unsignedp ? sat_long_long_accum_type_node
3927 : long_long_accum_type_node;
3928
3929 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3930 return unsignedp ? sat_unsigned_short_accum_type_node
3931 : unsigned_short_accum_type_node;
3932 if (mode == TYPE_MODE (unsigned_accum_type_node))
3933 return unsignedp ? sat_unsigned_accum_type_node
3934 : unsigned_accum_type_node;
3935 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3936 return unsignedp ? sat_unsigned_long_accum_type_node
3937 : unsigned_long_accum_type_node;
3938 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3939 return unsignedp ? sat_unsigned_long_long_accum_type_node
3940 : unsigned_long_long_accum_type_node;
3941
3942 if (mode == QQmode)
3943 return unsignedp ? sat_qq_type_node : qq_type_node;
3944 if (mode == HQmode)
3945 return unsignedp ? sat_hq_type_node : hq_type_node;
3946 if (mode == SQmode)
3947 return unsignedp ? sat_sq_type_node : sq_type_node;
3948 if (mode == DQmode)
3949 return unsignedp ? sat_dq_type_node : dq_type_node;
3950 if (mode == TQmode)
3951 return unsignedp ? sat_tq_type_node : tq_type_node;
3952
3953 if (mode == UQQmode)
3954 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3955 if (mode == UHQmode)
3956 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3957 if (mode == USQmode)
3958 return unsignedp ? sat_usq_type_node : usq_type_node;
3959 if (mode == UDQmode)
3960 return unsignedp ? sat_udq_type_node : udq_type_node;
3961 if (mode == UTQmode)
3962 return unsignedp ? sat_utq_type_node : utq_type_node;
3963
3964 if (mode == HAmode)
3965 return unsignedp ? sat_ha_type_node : ha_type_node;
3966 if (mode == SAmode)
3967 return unsignedp ? sat_sa_type_node : sa_type_node;
3968 if (mode == DAmode)
3969 return unsignedp ? sat_da_type_node : da_type_node;
3970 if (mode == TAmode)
3971 return unsignedp ? sat_ta_type_node : ta_type_node;
3972
3973 if (mode == UHAmode)
3974 return unsignedp ? sat_uha_type_node : uha_type_node;
3975 if (mode == USAmode)
3976 return unsignedp ? sat_usa_type_node : usa_type_node;
3977 if (mode == UDAmode)
3978 return unsignedp ? sat_uda_type_node : uda_type_node;
3979 if (mode == UTAmode)
3980 return unsignedp ? sat_uta_type_node : uta_type_node;
3981 }
3982
d1d3865f 3983 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
7a421706
MS
3984 if (TYPE_MODE (TREE_VALUE (t)) == mode
3985 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
d1d3865f
ZW
3986 return TREE_VALUE (t);
3987
b30f223b
RS
3988 return 0;
3989}
693a6128 3990
12753674
RE
3991tree
3992c_common_unsigned_type (tree type)
3993{
3994 return c_common_signed_or_unsigned_type (1, type);
3995}
3996
693a6128
GRK
3997/* Return a signed type the same as TYPE in other respects. */
3998
3999tree
35b1a6fa 4000c_common_signed_type (tree type)
693a6128 4001{
ceef8ce4 4002 return c_common_signed_or_unsigned_type (0, type);
693a6128
GRK
4003}
4004
4005/* Return a type the same as TYPE except unsigned or
4006 signed according to UNSIGNEDP. */
4007
4008tree
35b1a6fa 4009c_common_signed_or_unsigned_type (int unsignedp, tree type)
693a6128 4010{
c74a03d2 4011 tree type1;
78a7c317 4012 int i;
693a6128 4013
c74a03d2
RAE
4014 /* This block of code emulates the behavior of the old
4015 c_common_unsigned_type. In particular, it returns
4016 long_unsigned_type_node if passed a long, even when a int would
4017 have the same size. This is necessary for warnings to work
4018 correctly in archs where sizeof(int) == sizeof(long) */
4019
4020 type1 = TYPE_MAIN_VARIANT (type);
4021 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4022 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4023 if (type1 == integer_type_node || type1 == unsigned_type_node)
4024 return unsignedp ? unsigned_type_node : integer_type_node;
4025 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4026 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4027 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4028 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4029 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4030 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
78a7c317
DD
4031
4032 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4033 if (int_n_enabled_p[i]
4034 && (type1 == int_n_trees[i].unsigned_type
4035 || type1 == int_n_trees[i].signed_type))
4036 return (unsignedp ? int_n_trees[i].unsigned_type
4037 : int_n_trees[i].signed_type);
4038
c74a03d2
RAE
4039 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4040 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4041#if HOST_BITS_PER_WIDE_INT >= 64
4042 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4043 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4044#endif
4045 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4046 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4047 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4048 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4049 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4050 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4051 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4052 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4053
70d3fcab
AH
4054#define C_COMMON_FIXED_TYPES(NAME) \
4055 if (type1 == short_ ## NAME ## _type_node \
4056 || type1 == unsigned_short_ ## NAME ## _type_node) \
4057 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4058 : short_ ## NAME ## _type_node; \
4059 if (type1 == NAME ## _type_node \
4060 || type1 == unsigned_ ## NAME ## _type_node) \
4061 return unsignedp ? unsigned_ ## NAME ## _type_node \
4062 : NAME ## _type_node; \
4063 if (type1 == long_ ## NAME ## _type_node \
4064 || type1 == unsigned_long_ ## NAME ## _type_node) \
4065 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4066 : long_ ## NAME ## _type_node; \
4067 if (type1 == long_long_ ## NAME ## _type_node \
4068 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4069 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4070 : long_long_ ## NAME ## _type_node;
4071
4072#define C_COMMON_FIXED_MODE_TYPES(NAME) \
4073 if (type1 == NAME ## _type_node \
4074 || type1 == u ## NAME ## _type_node) \
4075 return unsignedp ? u ## NAME ## _type_node \
4076 : NAME ## _type_node;
4077
4078#define C_COMMON_FIXED_TYPES_SAT(NAME) \
4079 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4080 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4081 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4082 : sat_ ## short_ ## NAME ## _type_node; \
4083 if (type1 == sat_ ## NAME ## _type_node \
4084 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4085 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4086 : sat_ ## NAME ## _type_node; \
4087 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4088 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4089 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4090 : sat_ ## long_ ## NAME ## _type_node; \
4091 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4092 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4093 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4094 : sat_ ## long_long_ ## NAME ## _type_node;
4095
4096#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4097 if (type1 == sat_ ## NAME ## _type_node \
4098 || type1 == sat_ ## u ## NAME ## _type_node) \
4099 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4100 : sat_ ## NAME ## _type_node;
4101
4102 C_COMMON_FIXED_TYPES (fract);
4103 C_COMMON_FIXED_TYPES_SAT (fract);
4104 C_COMMON_FIXED_TYPES (accum);
4105 C_COMMON_FIXED_TYPES_SAT (accum);
4106
4107 C_COMMON_FIXED_MODE_TYPES (qq);
4108 C_COMMON_FIXED_MODE_TYPES (hq);
4109 C_COMMON_FIXED_MODE_TYPES (sq);
4110 C_COMMON_FIXED_MODE_TYPES (dq);
4111 C_COMMON_FIXED_MODE_TYPES (tq);
4112 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4113 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4114 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4115 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4116 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4117 C_COMMON_FIXED_MODE_TYPES (ha);
4118 C_COMMON_FIXED_MODE_TYPES (sa);
4119 C_COMMON_FIXED_MODE_TYPES (da);
4120 C_COMMON_FIXED_MODE_TYPES (ta);
4121 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4122 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4123 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4124 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
ab22c1fa 4125
bc15d0ef
JM
4126 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4127 the precision; they have precision set to match their range, but
4128 may use a wider mode to match an ABI. If we change modes, we may
4129 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4130 the precision as well, so as to yield correct results for
4131 bit-field types. C++ does not have these separate bit-field
4132 types, and producing a signed or unsigned variant of an
4133 ENUMERAL_TYPE may cause other problems as well. */
4134
1e204133
RAE
4135 if (!INTEGRAL_TYPE_P (type)
4136 || TYPE_UNSIGNED (type) == unsignedp)
4137 return type;
4138
bc15d0ef
JM
4139#define TYPE_OK(node) \
4140 (TYPE_MODE (type) == TYPE_MODE (node) \
41b81065 4141 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
bc15d0ef 4142 if (TYPE_OK (signed_char_type_node))
693a6128 4143 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
bc15d0ef 4144 if (TYPE_OK (integer_type_node))
693a6128 4145 return unsignedp ? unsigned_type_node : integer_type_node;
bc15d0ef 4146 if (TYPE_OK (short_integer_type_node))
693a6128 4147 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
bc15d0ef 4148 if (TYPE_OK (long_integer_type_node))
693a6128 4149 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
bc15d0ef 4150 if (TYPE_OK (long_long_integer_type_node))
693a6128
GRK
4151 return (unsignedp ? long_long_unsigned_type_node
4152 : long_long_integer_type_node);
78a7c317
DD
4153
4154 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4155 if (int_n_enabled_p[i]
4156 && TYPE_MODE (type) == int_n_data[i].m
4157 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4158 return (unsignedp ? int_n_trees[i].unsigned_type
4159 : int_n_trees[i].signed_type);
4160
bc15d0ef 4161 if (TYPE_OK (widest_integer_literal_type_node))
693a6128
GRK
4162 return (unsignedp ? widest_unsigned_literal_type_node
4163 : widest_integer_literal_type_node);
4a063bec
RH
4164
4165#if HOST_BITS_PER_WIDE_INT >= 64
bc15d0ef 4166 if (TYPE_OK (intTI_type_node))
4a063bec
RH
4167 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4168#endif
bc15d0ef 4169 if (TYPE_OK (intDI_type_node))
4a063bec 4170 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
bc15d0ef 4171 if (TYPE_OK (intSI_type_node))
4a063bec 4172 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
bc15d0ef 4173 if (TYPE_OK (intHI_type_node))
4a063bec 4174 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
bc15d0ef 4175 if (TYPE_OK (intQI_type_node))
4a063bec 4176 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
bc15d0ef 4177#undef TYPE_OK
4a063bec 4178
41b81065 4179 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
693a6128 4180}
9649812a 4181
38a4afee
MM
4182/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4183
4184tree
4185c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4186{
78a7c317
DD
4187 int i;
4188
38a4afee
MM
4189 /* Extended integer types of the same width as a standard type have
4190 lesser rank, so those of the same width as int promote to int or
4191 unsigned int and are valid for printf formats expecting int or
4192 unsigned int. To avoid such special cases, avoid creating
4193 extended integer types for bit-fields if a standard integer type
4194 is available. */
4195 if (width == TYPE_PRECISION (integer_type_node))
4196 return unsignedp ? unsigned_type_node : integer_type_node;
4197 if (width == TYPE_PRECISION (signed_char_type_node))
4198 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4199 if (width == TYPE_PRECISION (short_integer_type_node))
4200 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4201 if (width == TYPE_PRECISION (long_integer_type_node))
4202 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4203 if (width == TYPE_PRECISION (long_long_integer_type_node))
4204 return (unsignedp ? long_long_unsigned_type_node
4205 : long_long_integer_type_node);
78a7c317
DD
4206 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4207 if (int_n_enabled_p[i]
4208 && width == int_n_data[i].bitsize)
4209 return (unsignedp ? int_n_trees[i].unsigned_type
4210 : int_n_trees[i].signed_type);
38a4afee
MM
4211 return build_nonstandard_integer_type (width, unsignedp);
4212}
4213
9649812a
MM
4214/* The C version of the register_builtin_type langhook. */
4215
4216void
4217c_register_builtin_type (tree type, const char* name)
4218{
4219 tree decl;
4220
c2255bc4
AH
4221 decl = build_decl (UNKNOWN_LOCATION,
4222 TYPE_DECL, get_identifier (name), type);
9649812a
MM
4223 DECL_ARTIFICIAL (decl) = 1;
4224 if (!TYPE_NAME (type))
4225 TYPE_NAME (type) = decl;
4226 pushdecl (decl);
d1d3865f
ZW
4227
4228 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
9649812a 4229}
6acfe908 4230\f
78ef5b89 4231/* Print an error message for invalid operands to arith operation
ba47d38d
AH
4232 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4233 LOCATION is the location of the message. */
b30f223b
RS
4234
4235void
ba47d38d
AH
4236binary_op_error (location_t location, enum tree_code code,
4237 tree type0, tree type1)
b30f223b 4238{
b3694847 4239 const char *opname;
89c78d7d 4240
b30f223b
RS
4241 switch (code)
4242 {
b30f223b
RS
4243 case PLUS_EXPR:
4244 opname = "+"; break;
4245 case MINUS_EXPR:
4246 opname = "-"; break;
4247 case MULT_EXPR:
4248 opname = "*"; break;
4249 case MAX_EXPR:
4250 opname = "max"; break;
4251 case MIN_EXPR:
4252 opname = "min"; break;
4253 case EQ_EXPR:
4254 opname = "=="; break;
4255 case NE_EXPR:
4256 opname = "!="; break;
4257 case LE_EXPR:
4258 opname = "<="; break;
4259 case GE_EXPR:
4260 opname = ">="; break;
4261 case LT_EXPR:
4262 opname = "<"; break;
4263 case GT_EXPR:
4264 opname = ">"; break;
4265 case LSHIFT_EXPR:
4266 opname = "<<"; break;
4267 case RSHIFT_EXPR:
4268 opname = ">>"; break;
4269 case TRUNC_MOD_EXPR:
047de90b 4270 case FLOOR_MOD_EXPR:
b30f223b
RS
4271 opname = "%"; break;
4272 case TRUNC_DIV_EXPR:
047de90b 4273 case FLOOR_DIV_EXPR:
b30f223b
RS
4274 opname = "/"; break;
4275 case BIT_AND_EXPR:
4276 opname = "&"; break;
4277 case BIT_IOR_EXPR:
4278 opname = "|"; break;
4279 case TRUTH_ANDIF_EXPR:
4280 opname = "&&"; break;
4281 case TRUTH_ORIF_EXPR:
4282 opname = "||"; break;
4283 case BIT_XOR_EXPR:
4284 opname = "^"; break;
6d819282 4285 default:
37b2f290 4286 gcc_unreachable ();
b30f223b 4287 }
ba47d38d
AH
4288 error_at (location,
4289 "invalid operands to binary %s (have %qT and %qT)", opname,
4290 type0, type1);
b30f223b
RS
4291}
4292\f
50f305ca
MLI
4293/* Given an expression as a tree, return its original type. Do this
4294 by stripping any conversion that preserves the sign and precision. */
4295static tree
4296expr_original_type (tree expr)
4297{
4298 STRIP_SIGN_NOPS (expr);
4299 return TREE_TYPE (expr);
4300}
4301
b30f223b
RS
4302/* Subroutine of build_binary_op, used for comparison operations.
4303 See if the operands have both been converted from subword integer types
4304 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
4305 This function is also responsible for converting the two operands
4306 to the proper common type for comparison.
b30f223b
RS
4307
4308 The arguments of this function are all pointers to local variables
4309 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4310 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4311
393e8e8b
MP
4312 LOC is the location of the comparison.
4313
b30f223b
RS
4314 If this function returns nonzero, it means that the comparison has
4315 a constant value. What this function returns is an expression for
4316 that value. */
4317
4318tree
393e8e8b
MP
4319shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4320 tree *restype_ptr, enum tree_code *rescode_ptr)
b30f223b 4321{
b3694847 4322 tree type;
b30f223b
RS
4323 tree op0 = *op0_ptr;
4324 tree op1 = *op1_ptr;
4325 int unsignedp0, unsignedp1;
4326 int real1, real2;
4327 tree primop0, primop1;
4328 enum tree_code code = *rescode_ptr;
4329
4330 /* Throw away any conversions to wider types
4331 already present in the operands. */
4332
828fb3ba
JM
4333 primop0 = c_common_get_narrower (op0, &unsignedp0);
4334 primop1 = c_common_get_narrower (op1, &unsignedp1);
b30f223b 4335
126e6609
JJ
4336 /* If primopN is first sign-extended from primopN's precision to opN's
4337 precision, then zero-extended from opN's precision to
4338 *restype_ptr precision, shortenings might be invalid. */
4339 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4340 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4341 && !unsignedp0
4342 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4343 primop0 = op0;
4344 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4345 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4346 && !unsignedp1
4347 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4348 primop1 = op1;
4349
b30f223b
RS
4350 /* Handle the case that OP0 does not *contain* a conversion
4351 but it *requires* conversion to FINAL_TYPE. */
4352
4353 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
8df83eae 4354 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b30f223b 4355 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
8df83eae 4356 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b30f223b
RS
4357
4358 /* If one of the operands must be floated, we cannot optimize. */
4359 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4360 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4361
4362 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
4363 so value is preserved), for canonicalization. Don't do this if
4364 the second arg is 0. */
b30f223b 4365
5af6001b 4366 if (TREE_CONSTANT (primop0)
ab22c1fa
CF
4367 && !integer_zerop (primop1) && !real_zerop (primop1)
4368 && !fixed_zerop (primop1))
b30f223b 4369 {
fab27f52
MM
4370 std::swap (primop0, primop1);
4371 std::swap (op0, op1);
b30f223b
RS
4372 *op0_ptr = op0;
4373 *op1_ptr = op1;
fab27f52
MM
4374 std::swap (unsignedp0, unsignedp1);
4375 std::swap (real1, real2);
b30f223b
RS
4376
4377 switch (code)
4378 {
4379 case LT_EXPR:
4380 code = GT_EXPR;
4381 break;
4382 case GT_EXPR:
4383 code = LT_EXPR;
4384 break;
4385 case LE_EXPR:
4386 code = GE_EXPR;
4387 break;
4388 case GE_EXPR:
4389 code = LE_EXPR;
4390 break;
6d819282
MK
4391 default:
4392 break;
b30f223b
RS
4393 }
4394 *rescode_ptr = code;
4395 }
4396
4397 /* If comparing an integer against a constant more bits wide,
4398 maybe we can deduce a value of 1 or 0 independent of the data.
4399 Or else truncate the constant now
4400 rather than extend the variable at run time.
4401
4402 This is only interesting if the constant is the wider arg.
4403 Also, it is not safe if the constant is unsigned and the
4404 variable arg is signed, since in this case the variable
4405 would be sign-extended and then regarded as unsigned.
4406 Our technique fails in this case because the lowest/highest
4407 possible unsigned results don't follow naturally from the
4408 lowest/highest possible values of the variable operand.
4409 For just EQ_EXPR and NE_EXPR there is another technique that
4410 could be used: see if the constant can be faithfully represented
4411 in the other operand's type, by truncating it and reextending it
4412 and see if that preserves the constant's value. */
4413
4414 if (!real1 && !real2
ab22c1fa 4415 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b30f223b
RS
4416 && TREE_CODE (primop1) == INTEGER_CST
4417 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4418 {
4419 int min_gt, max_gt, min_lt, max_lt;
4420 tree maxval, minval;
4421 /* 1 if comparison is nominally unsigned. */
8df83eae 4422 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b30f223b
RS
4423 tree val;
4424
ceef8ce4
NB
4425 type = c_common_signed_or_unsigned_type (unsignedp0,
4426 TREE_TYPE (primop0));
8bbd5685 4427
b30f223b
RS
4428 maxval = TYPE_MAX_VALUE (type);
4429 minval = TYPE_MIN_VALUE (type);
4430
4431 if (unsignedp && !unsignedp0)
ceef8ce4 4432 *restype_ptr = c_common_signed_type (*restype_ptr);
b30f223b
RS
4433
4434 if (TREE_TYPE (primop1) != *restype_ptr)
fae1b38d 4435 {
af9c6659
NS
4436 /* Convert primop1 to target type, but do not introduce
4437 additional overflow. We know primop1 is an int_cst. */
807e902e
KZ
4438 primop1 = force_fit_type (*restype_ptr,
4439 wide_int::from
4440 (primop1,
4441 TYPE_PRECISION (*restype_ptr),
4442 TYPE_SIGN (TREE_TYPE (primop1))),
4443 0, TREE_OVERFLOW (primop1));
fae1b38d 4444 }
b30f223b
RS
4445 if (type != *restype_ptr)
4446 {
4447 minval = convert (*restype_ptr, minval);
4448 maxval = convert (*restype_ptr, maxval);
4449 }
4450
807e902e
KZ
4451 min_gt = tree_int_cst_lt (primop1, minval);
4452 max_gt = tree_int_cst_lt (primop1, maxval);
4453 min_lt = tree_int_cst_lt (minval, primop1);
4454 max_lt = tree_int_cst_lt (maxval, primop1);
b30f223b
RS
4455
4456 val = 0;
4457 /* This used to be a switch, but Genix compiler can't handle that. */
4458 if (code == NE_EXPR)
4459 {
4460 if (max_lt || min_gt)
de7df9eb 4461 val = truthvalue_true_node;
b30f223b
RS
4462 }
4463 else if (code == EQ_EXPR)
4464 {
4465 if (max_lt || min_gt)
de7df9eb 4466 val = truthvalue_false_node;
b30f223b
RS
4467 }
4468 else if (code == LT_EXPR)
4469 {
4470 if (max_lt)
de7df9eb 4471 val = truthvalue_true_node;
b30f223b 4472 if (!min_lt)
de7df9eb 4473 val = truthvalue_false_node;
b30f223b
RS
4474 }
4475 else if (code == GT_EXPR)
4476 {
4477 if (min_gt)
de7df9eb 4478 val = truthvalue_true_node;
b30f223b 4479 if (!max_gt)
de7df9eb 4480 val = truthvalue_false_node;
b30f223b
RS
4481 }
4482 else if (code == LE_EXPR)
4483 {
4484 if (!max_gt)
de7df9eb 4485 val = truthvalue_true_node;
b30f223b 4486 if (min_gt)
de7df9eb 4487 val = truthvalue_false_node;
b30f223b
RS
4488 }
4489 else if (code == GE_EXPR)
4490 {
4491 if (!min_lt)
de7df9eb 4492 val = truthvalue_true_node;
b30f223b 4493 if (max_lt)
de7df9eb 4494 val = truthvalue_false_node;
b30f223b
RS
4495 }
4496
4497 /* If primop0 was sign-extended and unsigned comparison specd,
4498 we did a signed comparison above using the signed type bounds.
4499 But the comparison we output must be unsigned.
4500
4501 Also, for inequalities, VAL is no good; but if the signed
4502 comparison had *any* fixed result, it follows that the
4503 unsigned comparison just tests the sign in reverse
4504 (positive values are LE, negative ones GE).
4505 So we can generate an unsigned comparison
4506 against an extreme value of the signed type. */
4507
4508 if (unsignedp && !unsignedp0)
4509 {
4510 if (val != 0)
4511 switch (code)
4512 {
4513 case LT_EXPR:
4514 case GE_EXPR:
4515 primop1 = TYPE_MIN_VALUE (type);
4516 val = 0;
4517 break;
4518
4519 case LE_EXPR:
4520 case GT_EXPR:
4521 primop1 = TYPE_MAX_VALUE (type);
4522 val = 0;
4523 break;
6d819282
MK
4524
4525 default:
4526 break;
b30f223b 4527 }
12753674 4528 type = c_common_unsigned_type (type);
b30f223b
RS
4529 }
4530
ca7e759d 4531 if (TREE_CODE (primop0) != INTEGER_CST)
b30f223b 4532 {
de7df9eb 4533 if (val == truthvalue_false_node)
50f305ca
MLI
4534 warning_at (loc, OPT_Wtype_limits,
4535 "comparison is always false due to limited range of data type");
de7df9eb 4536 if (val == truthvalue_true_node)
50f305ca
MLI
4537 warning_at (loc, OPT_Wtype_limits,
4538 "comparison is always true due to limited range of data type");
b30f223b
RS
4539 }
4540
4541 if (val != 0)
4542 {
4543 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4544 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3 4545 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b30f223b
RS
4546 return val;
4547 }
4548
4549 /* Value is not predetermined, but do the comparison
4550 in the type of the operand that is not constant.
4551 TYPE is already properly set. */
4552 }
9a8ce21f
JG
4553
4554 /* If either arg is decimal float and the other is float, find the
4555 proper common type to use for comparison. */
6f450181
RB
4556 else if (real1 && real2
4557 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4558 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4559 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4560
4561 /* If either arg is decimal float and the other is float, fail. */
9a8ce21f
JG
4562 else if (real1 && real2
4563 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4564 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
6f450181 4565 return 0;
9a8ce21f 4566
b30f223b 4567 else if (real1 && real2
766f6c30
RS
4568 && (TYPE_PRECISION (TREE_TYPE (primop0))
4569 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
4570 type = TREE_TYPE (primop0);
4571
4572 /* If args' natural types are both narrower than nominal type
4573 and both extend in the same manner, compare them
4574 in the type of the wider arg.
4575 Otherwise must actually extend both to the nominal
4576 common type lest different ways of extending
4577 alter the result.
4578 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4579
4580 else if (unsignedp0 == unsignedp1 && real1 == real2
4581 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4582 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4583 {
4584 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
ceef8ce4 4585 type = c_common_signed_or_unsigned_type (unsignedp0
8df83eae 4586 || TYPE_UNSIGNED (*restype_ptr),
ceef8ce4 4587 type);
b30f223b
RS
4588 /* Make sure shorter operand is extended the right way
4589 to match the longer operand. */
ceef8ce4
NB
4590 primop0
4591 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4592 TREE_TYPE (primop0)),
4593 primop0);
4594 primop1
4595 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4596 TREE_TYPE (primop1)),
4597 primop1);
b30f223b
RS
4598 }
4599 else
4600 {
4601 /* Here we must do the comparison on the nominal type
4602 using the args exactly as we received them. */
4603 type = *restype_ptr;
4604 primop0 = op0;
4605 primop1 = op1;
4606
4607 if (!real1 && !real2 && integer_zerop (primop1)
8df83eae 4608 && TYPE_UNSIGNED (*restype_ptr))
b30f223b
RS
4609 {
4610 tree value = 0;
50f305ca
MLI
4611 /* All unsigned values are >= 0, so we warn. However,
4612 if OP0 is a constant that is >= 0, the signedness of
4613 the comparison isn't an issue, so suppress the
4614 warning. */
4615 bool warn =
8400e75e 4616 warn_type_limits && !in_system_header_at (loc)
50f305ca
MLI
4617 && !(TREE_CODE (primop0) == INTEGER_CST
4618 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4619 primop0)))
4620 /* Do not warn for enumeration types. */
4621 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4622
b30f223b
RS
4623 switch (code)
4624 {
4625 case GE_EXPR:
50f305ca
MLI
4626 if (warn)
4627 warning_at (loc, OPT_Wtype_limits,
4628 "comparison of unsigned expression >= 0 is always true");
de7df9eb 4629 value = truthvalue_true_node;
b30f223b
RS
4630 break;
4631
4632 case LT_EXPR:
50f305ca
MLI
4633 if (warn)
4634 warning_at (loc, OPT_Wtype_limits,
4635 "comparison of unsigned expression < 0 is always false");
de7df9eb 4636 value = truthvalue_false_node;
6d819282
MK
4637 break;
4638
4639 default:
4640 break;
b30f223b
RS
4641 }
4642
4643 if (value != 0)
4644 {
4645 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4646 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3
RS
4647 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4648 primop0, value);
b30f223b
RS
4649 return value;
4650 }
4651 }
4652 }
4653
4654 *op0_ptr = convert (type, primop0);
4655 *op1_ptr = convert (type, primop1);
4656
de7df9eb 4657 *restype_ptr = truthvalue_type_node;
b30f223b
RS
4658
4659 return 0;
4660}
4661\f
7552da58
JJ
4662/* Return a tree for the sum or difference (RESULTCODE says which)
4663 of pointer PTROP and integer INTOP. */
4664
4665tree
db3927fb 4666pointer_int_sum (location_t loc, enum tree_code resultcode,
fd9b0f32 4667 tree ptrop, tree intop, bool complain)
7552da58 4668{
6ac01510 4669 tree size_exp, ret;
7552da58 4670
7552da58 4671 /* The result is a pointer of the same type that is being added. */
7552da58
JJ
4672 tree result_type = TREE_TYPE (ptrop);
4673
4674 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4675 {
fd9b0f32
PC
4676 if (complain && warn_pointer_arith)
4677 pedwarn (loc, OPT_Wpointer_arith,
4678 "pointer of type %<void *%> used in arithmetic");
4679 else if (!complain)
4680 return error_mark_node;
7552da58
JJ
4681 size_exp = integer_one_node;
4682 }
4683 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4684 {
fd9b0f32
PC
4685 if (complain && warn_pointer_arith)
4686 pedwarn (loc, OPT_Wpointer_arith,
4687 "pointer to a function used in arithmetic");
4688 else if (!complain)
4689 return error_mark_node;
7552da58
JJ
4690 size_exp = integer_one_node;
4691 }
7552da58
JJ
4692 else
4693 size_exp = size_in_bytes (TREE_TYPE (result_type));
4694
6ac01510
ILT
4695 /* We are manipulating pointer values, so we don't need to warn
4696 about relying on undefined signed overflow. We disable the
4697 warning here because we use integer types so fold won't know that
4698 they are really pointers. */
4699 fold_defer_overflow_warnings ();
4700
7552da58
JJ
4701 /* If what we are about to multiply by the size of the elements
4702 contains a constant term, apply distributive law
4703 and multiply that constant term separately.
4704 This helps produce common subexpressions. */
7552da58 4705 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3f75a254 4706 && !TREE_CONSTANT (intop)
7552da58
JJ
4707 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4708 && TREE_CONSTANT (size_exp)
4709 /* If the constant comes from pointer subtraction,
4710 skip this optimization--it would cause an error. */
4711 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4712 /* If the constant is unsigned, and smaller than the pointer size,
4713 then we must skip this optimization. This is because it could cause
4714 an overflow error if the constant is negative but INTOP is not. */
3f75a254 4715 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
7552da58
JJ
4716 || (TYPE_PRECISION (TREE_TYPE (intop))
4717 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4718 {
4719 enum tree_code subcode = resultcode;
4720 tree int_type = TREE_TYPE (intop);
4721 if (TREE_CODE (intop) == MINUS_EXPR)
4722 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4723 /* Convert both subexpression types to the type of intop,
4724 because weird cases involving pointer arithmetic
4725 can result in a sum or difference with different type args. */
ba47d38d
AH
4726 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4727 subcode, ptrop,
7552da58
JJ
4728 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4729 intop = convert (int_type, TREE_OPERAND (intop, 0));
4730 }
4731
4732 /* Convert the integer argument to a type the same size as sizetype
4733 so the multiply won't overflow spuriously. */
7552da58 4734 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
8df83eae 4735 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
35b1a6fa 4736 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
8df83eae 4737 TYPE_UNSIGNED (sizetype)), intop);
7552da58
JJ
4738
4739 /* Replace the integer argument with a suitable product by the object size.
9e9ef331 4740 Do this multiplication as signed, then convert to the appropriate type
65de6659 4741 for the pointer operation and disregard an overflow that occurred only
9e9ef331
EB
4742 because of the sign-extension change in the latter conversion. */
4743 {
4744 tree t = build_binary_op (loc,
4745 MULT_EXPR, intop,
4746 convert (TREE_TYPE (intop), size_exp), 1);
4747 intop = convert (sizetype, t);
4748 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
807e902e 4749 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
9e9ef331 4750 }
5be014d5 4751
280f1ffa 4752 /* Create the sum or difference. */
5be014d5 4753 if (resultcode == MINUS_EXPR)
db3927fb 4754 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
7552da58 4755
5d49b6a7 4756 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
6ac01510
ILT
4757
4758 fold_undefer_and_ignore_overflow_warnings ();
4759
4760 return ret;
7552da58
JJ
4761}
4762\f
e5a94231
JM
4763/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4764 and if NON_CONST is known not to be permitted in an evaluated part
4765 of a constant expression. */
4766
4767tree
4768c_wrap_maybe_const (tree expr, bool non_const)
4769{
4770 bool nowarning = TREE_NO_WARNING (expr);
4771 location_t loc = EXPR_LOCATION (expr);
4772
4773 /* This should never be called for C++. */
4774 if (c_dialect_cxx ())
4775 gcc_unreachable ();
4776
4777 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4778 STRIP_TYPE_NOPS (expr);
4779 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4780 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4781 if (nowarning)
4782 TREE_NO_WARNING (expr) = 1;
4783 protected_set_expr_location (expr, loc);
4784
4785 return expr;
4786}
4787
928c19bb
JM
4788/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4789 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4790 around the SAVE_EXPR if needed so that c_fully_fold does not need
4791 to look inside SAVE_EXPRs. */
4792
4793tree
4794c_save_expr (tree expr)
4795{
4796 bool maybe_const = true;
4797 if (c_dialect_cxx ())
4798 return save_expr (expr);
4799 expr = c_fully_fold (expr, false, &maybe_const);
4800 expr = save_expr (expr);
4801 if (!maybe_const)
e5a94231 4802 expr = c_wrap_maybe_const (expr, true);
928c19bb
JM
4803 return expr;
4804}
4805
b3c6d2ea
ILT
4806/* Return whether EXPR is a declaration whose address can never be
4807 NULL. */
4808
4809bool
58f9752a 4810decl_with_nonnull_addr_p (const_tree expr)
b3c6d2ea
ILT
4811{
4812 return (DECL_P (expr)
4813 && (TREE_CODE (expr) == PARM_DECL
4814 || TREE_CODE (expr) == LABEL_DECL
4815 || !DECL_WEAK (expr)));
4816}
4817
b30f223b 4818/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
85498824
JM
4819 or for an `if' or `while' statement or ?..: exp. It should already
4820 have been validated to be of suitable type; otherwise, a bad
4821 diagnostic may result.
b30f223b 4822
ba47d38d
AH
4823 The EXPR is located at LOCATION.
4824
b30f223b
RS
4825 This preparation consists of taking the ordinary
4826 representation of an expression expr and producing a valid tree
4827 boolean expression describing whether expr is nonzero. We could
de7df9eb 4828 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b30f223b
RS
4829 but we optimize comparisons, &&, ||, and !.
4830
de7df9eb 4831 The resulting type should always be `truthvalue_type_node'. */
b30f223b
RS
4832
4833tree
ba47d38d 4834c_common_truthvalue_conversion (location_t location, tree expr)
b30f223b 4835{
b30f223b
RS
4836 switch (TREE_CODE (expr))
4837 {
d1a7edaf 4838 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
6f312d18
ZW
4839 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4840 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4841 case ORDERED_EXPR: case UNORDERED_EXPR:
90ec750d
RS
4842 if (TREE_TYPE (expr) == truthvalue_type_node)
4843 return expr;
c2255bc4 4844 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
90ec750d 4845 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
c2255bc4 4846 goto ret;
90ec750d 4847
b30f223b
RS
4848 case TRUTH_ANDIF_EXPR:
4849 case TRUTH_ORIF_EXPR:
4850 case TRUTH_AND_EXPR:
4851 case TRUTH_OR_EXPR:
9379fac9 4852 case TRUTH_XOR_EXPR:
90ec750d
RS
4853 if (TREE_TYPE (expr) == truthvalue_type_node)
4854 return expr;
c2255bc4 4855 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
b8698a0f 4856 c_common_truthvalue_conversion (location,
c2255bc4
AH
4857 TREE_OPERAND (expr, 0)),
4858 c_common_truthvalue_conversion (location,
4859 TREE_OPERAND (expr, 1)));
4860 goto ret;
18c0f675 4861
18d00205 4862 case TRUTH_NOT_EXPR:
90ec750d
RS
4863 if (TREE_TYPE (expr) == truthvalue_type_node)
4864 return expr;
c2255bc4
AH
4865 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4866 c_common_truthvalue_conversion (location,
4867 TREE_OPERAND (expr, 0)));
4868 goto ret;
18d00205 4869
b30f223b
RS
4870 case ERROR_MARK:
4871 return expr;
4872
4873 case INTEGER_CST:
d95787e6
RS
4874 return integer_zerop (expr) ? truthvalue_false_node
4875 : truthvalue_true_node;
b30f223b
RS
4876
4877 case REAL_CST:
010c4d9c
RS
4878 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4879 ? truthvalue_true_node
4880 : truthvalue_false_node;
b30f223b 4881
ab22c1fa
CF
4882 case FIXED_CST:
4883 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4884 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4885 ? truthvalue_true_node
4886 : truthvalue_false_node;
4887
90ec750d 4888 case FUNCTION_DECL:
c9f9eb5d 4889 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
90ec750d
RS
4890 /* Fall through. */
4891
b30f223b 4892 case ADDR_EXPR:
1998463c 4893 {
f6f08360 4894 tree inner = TREE_OPERAND (expr, 0);
b3c6d2ea 4895 if (decl_with_nonnull_addr_p (inner))
1998463c 4896 {
b3c6d2ea 4897 /* Common Ada/Pascal programmer's mistake. */
ba47d38d
AH
4898 warning_at (location,
4899 OPT_Waddress,
4900 "the address of %qD will always evaluate as %<true%>",
4901 inner);
1998463c
SB
4902 return truthvalue_true_node;
4903 }
33766b66 4904 break;
1998463c 4905 }
b30f223b 4906
766f6c30 4907 case COMPLEX_EXPR:
c2255bc4 4908 expr = build_binary_op (EXPR_LOCATION (expr),
ba47d38d 4909 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 4910 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
ba47d38d
AH
4911 c_common_truthvalue_conversion (location,
4912 TREE_OPERAND (expr, 0)),
4913 c_common_truthvalue_conversion (location,
4914 TREE_OPERAND (expr, 1)),
766f6c30 4915 0);
c2255bc4 4916 goto ret;
766f6c30 4917
b30f223b
RS
4918 case NEGATE_EXPR:
4919 case ABS_EXPR:
4920 case FLOAT_EXPR:
8ce94e44 4921 case EXCESS_PRECISION_EXPR:
da7d8304 4922 /* These don't change whether an object is nonzero or zero. */
ba47d38d 4923 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b30f223b
RS
4924
4925 case LROTATE_EXPR:
4926 case RROTATE_EXPR:
da7d8304 4927 /* These don't change whether an object is zero or nonzero, but
b30f223b
RS
4928 we can't ignore them if their second arg has side-effects. */
4929 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
c2255bc4
AH
4930 {
4931 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4932 TREE_OPERAND (expr, 1),
b8698a0f 4933 c_common_truthvalue_conversion
c2255bc4
AH
4934 (location, TREE_OPERAND (expr, 0)));
4935 goto ret;
4936 }
b30f223b 4937 else
ba47d38d
AH
4938 return c_common_truthvalue_conversion (location,
4939 TREE_OPERAND (expr, 0));
b57062ca 4940
b30f223b
RS
4941 case COND_EXPR:
4942 /* Distribute the conversion into the arms of a COND_EXPR. */
928c19bb 4943 if (c_dialect_cxx ())
c2255bc4 4944 {
4cc4f2f4
JJ
4945 tree op1 = TREE_OPERAND (expr, 1);
4946 tree op2 = TREE_OPERAND (expr, 2);
4947 /* In C++ one of the arms might have void type if it is throw. */
4948 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4949 op1 = c_common_truthvalue_conversion (location, op1);
4950 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4951 op2 = c_common_truthvalue_conversion (location, op2);
db3927fb 4952 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4cc4f2f4 4953 TREE_OPERAND (expr, 0), op1, op2);
c2255bc4
AH
4954 goto ret;
4955 }
928c19bb 4956 else
c2255bc4
AH
4957 {
4958 /* Folding will happen later for C. */
4959 expr = build3 (COND_EXPR, truthvalue_type_node,
4960 TREE_OPERAND (expr, 0),
4961 c_common_truthvalue_conversion (location,
4962 TREE_OPERAND (expr, 1)),
4963 c_common_truthvalue_conversion (location,
4964 TREE_OPERAND (expr, 2)));
4965 goto ret;
4966 }
b30f223b 4967
1043771b 4968 CASE_CONVERT:
1ee44b26
JM
4969 {
4970 tree totype = TREE_TYPE (expr);
4971 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4972
076fecad
PP
4973 if (POINTER_TYPE_P (totype)
4974 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4975 {
4976 tree inner = expr;
4977 STRIP_NOPS (inner);
4978
4979 if (DECL_P (inner))
4980 warning_at (location,
4981 OPT_Waddress,
4982 "the compiler can assume that the address of "
4983 "%qD will always evaluate to %<true%>",
4984 inner);
4985 }
4986
1ee44b26
JM
4987 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4988 since that affects how `default_conversion' will behave. */
4989 if (TREE_CODE (totype) == REFERENCE_TYPE
4990 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4991 break;
4992 /* Don't strip a conversion from C++0x scoped enum, since they
4993 don't implicitly convert to other types. */
4994 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4995 && ENUM_IS_SCOPED (fromtype))
4996 break;
4997 /* If this isn't narrowing the argument, we can ignore it. */
4998 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4999 return c_common_truthvalue_conversion (location,
5000 TREE_OPERAND (expr, 0));
5001 }
b30f223b
RS
5002 break;
5003
e2aab13d 5004 case MODIFY_EXPR:
fbc8d2d3
ILT
5005 if (!TREE_NO_WARNING (expr)
5006 && warn_parentheses)
5007 {
5008 warning (OPT_Wparentheses,
5009 "suggest parentheses around assignment used as truth value");
5010 TREE_NO_WARNING (expr) = 1;
5011 }
e2aab13d 5012 break;
b57062ca 5013
6d819282
MK
5014 default:
5015 break;
b30f223b
RS
5016 }
5017
f0b996c5 5018 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa 5019 {
5386338c 5020 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
c2255bc4 5021 expr = (build_binary_op
ba47d38d
AH
5022 (EXPR_LOCATION (expr),
5023 (TREE_SIDE_EFFECTS (expr)
f0b8d9aa 5024 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
c9f9eb5d
AH
5025 c_common_truthvalue_conversion
5026 (location,
5027 build_unary_op (location, REALPART_EXPR, t, 0)),
5028 c_common_truthvalue_conversion
5029 (location,
5030 build_unary_op (location, IMAGPART_EXPR, t, 0)),
f0b8d9aa 5031 0));
c2255bc4 5032 goto ret;
f0b8d9aa 5033 }
f0b996c5 5034
ab22c1fa
CF
5035 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5036 {
5037 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5038 FCONST0 (TYPE_MODE
5039 (TREE_TYPE (expr))));
ca80e52b 5040 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
ab22c1fa 5041 }
c2255bc4
AH
5042 else
5043 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
ab22c1fa 5044
c2255bc4
AH
5045 ret:
5046 protected_set_expr_location (expr, location);
5047 return expr;
b30f223b
RS
5048}
5049\f
9bc15050
RG
5050static void def_builtin_1 (enum built_in_function fncode,
5051 const char *name,
5052 enum built_in_class fnclass,
5053 tree fntype, tree libtype,
5054 bool both_p, bool fallback_p, bool nonansi_p,
5055 tree fnattrs, bool implicit_p);
fc2aaf30 5056
3932261a
MM
5057
5058/* Apply the TYPE_QUALS to the new DECL. */
5059
5060void
35b1a6fa 5061c_apply_type_quals_to_decl (int type_quals, tree decl)
3932261a 5062{
4b011bbf 5063 tree type = TREE_TYPE (decl);
9f63daea 5064
5a6159dd
AP
5065 if (type == error_mark_node)
5066 return;
4b011bbf 5067
329af3c7
JM
5068 if ((type_quals & TYPE_QUAL_CONST)
5069 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5070 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5071 constructor can produce constant init, so rely on cp_finish_decl to
5072 clear TREE_READONLY if the variable has non-constant init. */
3932261a
MM
5073 TREE_READONLY (decl) = 1;
5074 if (type_quals & TYPE_QUAL_VOLATILE)
5075 {
5076 TREE_SIDE_EFFECTS (decl) = 1;
5077 TREE_THIS_VOLATILE (decl) = 1;
5078 }
6946bc60 5079 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 5080 {
4b011bbf
JM
5081 while (type && TREE_CODE (type) == ARRAY_TYPE)
5082 /* Allow 'restrict' on arrays of pointers.
5083 FIXME currently we just ignore it. */
5084 type = TREE_TYPE (type);
5085 if (!type
5086 || !POINTER_TYPE_P (type)
5087 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
bda67431 5088 error ("invalid use of %<restrict%>");
3932261a
MM
5089 }
5090}
5091
2a22f99c
TS
5092struct c_type_hasher : ggc_hasher<tree>
5093{
5094 static hashval_t hash (tree);
5095 static bool equal (tree, tree);
5096};
5097
6de9cd9a
DN
5098/* Hash function for the problem of multiple type definitions in
5099 different files. This must hash all types that will compare
5100 equal via comptypes to the same value. In practice it hashes
9cf737f8 5101 on some of the simple stuff and leaves the details to comptypes. */
6de9cd9a 5102
2a22f99c
TS
5103hashval_t
5104c_type_hasher::hash (tree t)
6de9cd9a 5105{
a19e4d44 5106 int n_elements;
6de9cd9a 5107 int shift, size;
6de9cd9a
DN
5108 tree t2;
5109 switch (TREE_CODE (t))
5110 {
8c27b7d4 5111 /* For pointers, hash on pointee type plus some swizzling. */
325c3691 5112 case POINTER_TYPE:
2a22f99c 5113 return hash (TREE_TYPE (t)) ^ 0x3003003;
325c3691
RH
5114 /* Hash on number of elements and total size. */
5115 case ENUMERAL_TYPE:
5116 shift = 3;
5117 t2 = TYPE_VALUES (t);
5118 break;
5119 case RECORD_TYPE:
5120 shift = 0;
5121 t2 = TYPE_FIELDS (t);
5122 break;
5123 case QUAL_UNION_TYPE:
5124 shift = 1;
5125 t2 = TYPE_FIELDS (t);
5126 break;
5127 case UNION_TYPE:
5128 shift = 2;
5129 t2 = TYPE_FIELDS (t);
5130 break;
5131 default:
366de0ce 5132 gcc_unreachable ();
6de9cd9a 5133 }
a19e4d44
NF
5134 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5135 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5136 n_elements = list_length (t2);
6fc3c3c0
TT
5137 /* We might have a VLA here. */
5138 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5139 size = 0;
5140 else
5141 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
a19e4d44 5142 return ((size << 24) | (n_elements << shift));
6de9cd9a
DN
5143}
5144
2a22f99c
TS
5145bool
5146c_type_hasher::equal (tree t1, tree t2)
5147{
5148 return lang_hooks.types_compatible_p (t1, t2);
5149}
5150
5151static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4fe52ce9 5152
41472af8 5153/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 5154 or a type. Return -1 if we don't do anything special. */
41472af8 5155
4862826d 5156alias_set_type
35b1a6fa 5157c_common_get_alias_set (tree t)
41472af8 5158{
08bc2431 5159 tree u;
35b1a6fa 5160
cb9c2485
JM
5161 /* For VLAs, use the alias set of the element type rather than the
5162 default of alias set 0 for types compared structurally. */
5163 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5164 {
5165 if (TREE_CODE (t) == ARRAY_TYPE)
5166 return get_alias_set (TREE_TYPE (t));
5167 return -1;
5168 }
5169
08bc2431
MM
5170 /* Permit type-punning when accessing a union, provided the access
5171 is directly through the union. For example, this code does not
5172 permit taking the address of a union member and then storing
5173 through it. Even the type-punning allowed here is a GCC
5174 extension, albeit a common and useful one; the C standard says
5175 that such accesses have implementation-defined behavior. */
5176 for (u = t;
5177 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5178 u = TREE_OPERAND (u, 0))
5179 if (TREE_CODE (u) == COMPONENT_REF
5180 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5181 return 0;
ece32014 5182
74d86f4f 5183 /* That's all the expressions we handle specially. */
3f75a254 5184 if (!TYPE_P (t))
74d86f4f
RH
5185 return -1;
5186
95bd1dd7 5187 /* The C standard guarantees that any object may be accessed via an
74d86f4f
RH
5188 lvalue that has character type. */
5189 if (t == char_type_node
5190 || t == signed_char_type_node
5191 || t == unsigned_char_type_node)
3bdf5ad1 5192 return 0;
3932261a 5193
f824e5c3
RK
5194 /* The C standard specifically allows aliasing between signed and
5195 unsigned variants of the same type. We treat the signed
5196 variant as canonical. */
8df83eae 5197 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
8f215dce 5198 {
ceef8ce4 5199 tree t1 = c_common_signed_type (t);
f824e5c3 5200
8f215dce
JJ
5201 /* t1 == t can happen for boolean nodes which are always unsigned. */
5202 if (t1 != t)
5203 return get_alias_set (t1);
5204 }
ece32014 5205
6de9cd9a
DN
5206 /* Handle the case of multiple type nodes referring to "the same" type,
5207 which occurs with IMA. These share an alias set. FIXME: Currently only
5208 C90 is handled. (In C99 type compatibility is not transitive, which
5209 complicates things mightily. The alias set splay trees can theoretically
5210 represent this, but insertion is tricky when you consider all the
5211 different orders things might arrive in.) */
5212
5213 if (c_language != clk_c || flag_isoc99)
5214 return -1;
5215
9cf737f8 5216 /* Save time if there's only one input file. */
d974312d 5217 if (num_in_fnames == 1)
6de9cd9a
DN
5218 return -1;
5219
5220 /* Pointers need special handling if they point to any type that
5221 needs special handling (below). */
5222 if (TREE_CODE (t) == POINTER_TYPE)
5223 {
5224 tree t2;
5225 /* Find bottom type under any nested POINTERs. */
9f63daea 5226 for (t2 = TREE_TYPE (t);
762f7d9d
TT
5227 TREE_CODE (t2) == POINTER_TYPE;
5228 t2 = TREE_TYPE (t2))
5229 ;
9f63daea 5230 if (TREE_CODE (t2) != RECORD_TYPE
762f7d9d
TT
5231 && TREE_CODE (t2) != ENUMERAL_TYPE
5232 && TREE_CODE (t2) != QUAL_UNION_TYPE
5233 && TREE_CODE (t2) != UNION_TYPE)
5234 return -1;
6de9cd9a 5235 if (TYPE_SIZE (t2) == 0)
762f7d9d 5236 return -1;
6de9cd9a
DN
5237 }
5238 /* These are the only cases that need special handling. */
9f63daea 5239 if (TREE_CODE (t) != RECORD_TYPE
6de9cd9a
DN
5240 && TREE_CODE (t) != ENUMERAL_TYPE
5241 && TREE_CODE (t) != QUAL_UNION_TYPE
5242 && TREE_CODE (t) != UNION_TYPE
5243 && TREE_CODE (t) != POINTER_TYPE)
5244 return -1;
5245 /* Undefined? */
5246 if (TYPE_SIZE (t) == 0)
5247 return -1;
5248
9f63daea 5249 /* Look up t in hash table. Only one of the compatible types within each
6de9cd9a
DN
5250 alias set is recorded in the table. */
5251 if (!type_hash_table)
2a22f99c
TS
5252 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5253 tree *slot = type_hash_table->find_slot (t, INSERT);
6de9cd9a 5254 if (*slot != NULL)
6a3203c8
AP
5255 {
5256 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5257 return TYPE_ALIAS_SET ((tree)*slot);
5258 }
6de9cd9a
DN
5259 else
5260 /* Our caller will assign and record (in t) a new alias set; all we need
5261 to do is remember t in the hash table. */
5262 *slot = t;
5263
3bdf5ad1 5264 return -1;
41472af8 5265}
0213a355 5266\f
c2255bc4 5267/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
296674db 5268 the IS_SIZEOF parameter indicates which operator is being applied.
c2255bc4
AH
5269 The COMPLAIN flag controls whether we should diagnose possibly
5270 ill-formed constructs or not. LOC is the location of the SIZEOF or
296674db
JM
5271 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5272 a type in any context should be returned, rather than the normal
5273 alignment for that type. */
03a08664 5274
0213a355 5275tree
c2255bc4 5276c_sizeof_or_alignof_type (location_t loc,
296674db
JM
5277 tree type, bool is_sizeof, bool min_alignof,
5278 int complain)
0213a355 5279{
fa72b064
GDR
5280 const char *op_name;
5281 tree value = NULL;
5282 enum tree_code type_code = TREE_CODE (type);
35b1a6fa 5283
03a08664 5284 op_name = is_sizeof ? "sizeof" : "__alignof__";
35b1a6fa 5285
fa72b064 5286 if (type_code == FUNCTION_TYPE)
0213a355 5287 {
03a08664 5288 if (is_sizeof)
fa72b064 5289 {
44d90fe1
PC
5290 if (complain && warn_pointer_arith)
5291 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 5292 "invalid application of %<sizeof%> to a function type");
5ade1ed2
DG
5293 else if (!complain)
5294 return error_mark_node;
fa72b064
GDR
5295 value = size_one_node;
5296 }
5297 else
d19fa6b5
JM
5298 {
5299 if (complain)
5300 {
5301 if (c_dialect_cxx ())
c1771a20 5302 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
d19fa6b5
JM
5303 "%<alignof%> applied to a function type");
5304 else
c1771a20 5305 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
d19fa6b5
JM
5306 "%<_Alignof%> applied to a function type");
5307 }
5308 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5309 }
fa72b064
GDR
5310 }
5311 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5312 {
35b1a6fa 5313 if (type_code == VOID_TYPE
44d90fe1
PC
5314 && complain && warn_pointer_arith)
5315 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 5316 "invalid application of %qs to a void type", op_name);
5ade1ed2
DG
5317 else if (!complain)
5318 return error_mark_node;
fa72b064 5319 value = size_one_node;
0213a355 5320 }
73ac190a
PC
5321 else if (!COMPLETE_TYPE_P (type)
5322 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
0213a355 5323 {
ea793912 5324 if (complain)
73ac190a 5325 error_at (loc, "invalid application of %qs to incomplete type %qT",
c2255bc4 5326 op_name, type);
cb6addf4 5327 return error_mark_node;
0213a355 5328 }
73ac190a
PC
5329 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5330 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5331 {
5332 if (complain)
5333 error_at (loc, "invalid application of %qs to array type %qT of "
5334 "incomplete element type", op_name, type);
5335 return error_mark_node;
5336 }
0213a355 5337 else
fa72b064 5338 {
03a08664 5339 if (is_sizeof)
fa72b064 5340 /* Convert in case a char is more than one unit. */
db3927fb
AH
5341 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5342 size_int (TYPE_PRECISION (char_type_node)
5343 / BITS_PER_UNIT));
296674db 5344 else if (min_alignof)
2793eeab 5345 value = size_int (min_align_of_type (type));
fa72b064 5346 else
a4e9ffe5 5347 value = size_int (TYPE_ALIGN_UNIT (type));
fa72b064 5348 }
0213a355 5349
3ac8781c
RG
5350 /* VALUE will have the middle-end integer type sizetype.
5351 However, we should really return a value of type `size_t',
5352 which is just a typedef for an ordinary integer type. */
db3927fb 5353 value = fold_convert_loc (loc, size_type_node, value);
35b1a6fa 5354
fa72b064 5355 return value;
0213a355
JM
5356}
5357
5358/* Implement the __alignof keyword: Return the minimum required
837edd5f
GK
5359 alignment of EXPR, measured in bytes. For VAR_DECLs,
5360 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
c2255bc4
AH
5361 from an "aligned" __attribute__ specification). LOC is the
5362 location of the ALIGNOF operator. */
7f4edbcb 5363
0213a355 5364tree
c2255bc4 5365c_alignof_expr (location_t loc, tree expr)
0213a355
JM
5366{
5367 tree t;
5368
837edd5f 5369 if (VAR_OR_FUNCTION_DECL_P (expr))
a4e9ffe5 5370 t = size_int (DECL_ALIGN_UNIT (expr));
35b1a6fa 5371
0213a355
JM
5372 else if (TREE_CODE (expr) == COMPONENT_REF
5373 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5374 {
c2255bc4 5375 error_at (loc, "%<__alignof%> applied to a bit-field");
0213a355
JM
5376 t = size_one_node;
5377 }
5378 else if (TREE_CODE (expr) == COMPONENT_REF
173bf5be 5379 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
a4e9ffe5 5380 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
35b1a6fa 5381
0213a355
JM
5382 else if (TREE_CODE (expr) == INDIRECT_REF)
5383 {
5384 tree t = TREE_OPERAND (expr, 0);
5385 tree best = t;
5386 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
35b1a6fa 5387
1043771b 5388 while (CONVERT_EXPR_P (t)
173bf5be 5389 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
0213a355
JM
5390 {
5391 int thisalign;
5392
5393 t = TREE_OPERAND (t, 0);
5394 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5395 if (thisalign > bestalign)
5396 best = t, bestalign = thisalign;
5397 }
c2255bc4 5398 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
0213a355
JM
5399 }
5400 else
c2255bc4 5401 return c_alignof (loc, TREE_TYPE (expr));
0213a355 5402
db3927fb 5403 return fold_convert_loc (loc, size_type_node, t);
0213a355
JM
5404}
5405\f
df061a43
RS
5406/* Handle C and C++ default attributes. */
5407
5408enum built_in_attribute
5409{
5410#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5411#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
e384e6b5 5412#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
df061a43
RS
5413#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5414#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
df061a43
RS
5415#include "builtin-attrs.def"
5416#undef DEF_ATTR_NULL_TREE
5417#undef DEF_ATTR_INT
e384e6b5 5418#undef DEF_ATTR_STRING
df061a43
RS
5419#undef DEF_ATTR_IDENT
5420#undef DEF_ATTR_TREE_LIST
df061a43
RS
5421 ATTR_LAST
5422};
5423
5424static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5425
35b1a6fa 5426static void c_init_attributes (void);
df061a43 5427
a0274e3e 5428enum c_builtin_type
7f4edbcb 5429{
10841285
MM
5430#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5431#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5432#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5433#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5434#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5435#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
10a0d495 5436#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
f6a7cffc
TS
5437#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5438 ARG6) NAME,
5439#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5440 ARG6, ARG7) NAME,
5441#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5442 ARG6, ARG7, ARG8) NAME,
10841285
MM
5443#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5444#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5445#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
08291658 5446#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
10a0d495 5447#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
f6a7cffc 5448#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
56a9f6bc
TS
5449 NAME,
5450#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5451 ARG6, ARG7) NAME,
5452#define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5453 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
10841285
MM
5454#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5455#include "builtin-types.def"
5456#undef DEF_PRIMITIVE_TYPE
5457#undef DEF_FUNCTION_TYPE_0
5458#undef DEF_FUNCTION_TYPE_1
5459#undef DEF_FUNCTION_TYPE_2
5460#undef DEF_FUNCTION_TYPE_3
5461#undef DEF_FUNCTION_TYPE_4
10a0d495
JJ
5462#undef DEF_FUNCTION_TYPE_5
5463#undef DEF_FUNCTION_TYPE_6
a0274e3e 5464#undef DEF_FUNCTION_TYPE_7
acf0174b 5465#undef DEF_FUNCTION_TYPE_8
10841285
MM
5466#undef DEF_FUNCTION_TYPE_VAR_0
5467#undef DEF_FUNCTION_TYPE_VAR_1
5468#undef DEF_FUNCTION_TYPE_VAR_2
08291658 5469#undef DEF_FUNCTION_TYPE_VAR_3
10a0d495
JJ
5470#undef DEF_FUNCTION_TYPE_VAR_4
5471#undef DEF_FUNCTION_TYPE_VAR_5
56a9f6bc
TS
5472#undef DEF_FUNCTION_TYPE_VAR_7
5473#undef DEF_FUNCTION_TYPE_VAR_11
10841285 5474#undef DEF_POINTER_TYPE
a0274e3e
JJ
5475 BT_LAST
5476};
5477
5478typedef enum c_builtin_type builtin_type;
10841285 5479
a0274e3e
JJ
5480/* A temporary array for c_common_nodes_and_builtins. Used in
5481 communication with def_fn_type. */
5482static tree builtin_types[(int) BT_LAST + 1];
10841285 5483
a0274e3e
JJ
5484/* A helper function for c_common_nodes_and_builtins. Build function type
5485 for DEF with return type RET and N arguments. If VAR is true, then the
5486 function should be variadic after those N arguments.
5487
5488 Takes special care not to ICE if any of the types involved are
5489 error_mark_node, which indicates that said type is not in fact available
5490 (see builtin_type_for_size). In which case the function type as a whole
5491 should be error_mark_node. */
5492
5493static void
5494def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5495{
8242dd04
NF
5496 tree t;
5497 tree *args = XALLOCAVEC (tree, n);
a0274e3e
JJ
5498 va_list list;
5499 int i;
5500
5501 va_start (list, n);
5502 for (i = 0; i < n; ++i)
5503 {
d75d71e0 5504 builtin_type a = (builtin_type) va_arg (list, int);
a0274e3e
JJ
5505 t = builtin_types[a];
5506 if (t == error_mark_node)
5507 goto egress;
8242dd04 5508 args[i] = t;
a0274e3e 5509 }
a0274e3e 5510
a0274e3e
JJ
5511 t = builtin_types[ret];
5512 if (t == error_mark_node)
5513 goto egress;
8242dd04
NF
5514 if (var)
5515 t = build_varargs_function_type_array (t, n, args);
5516 else
5517 t = build_function_type_array (t, n, args);
a0274e3e
JJ
5518
5519 egress:
5520 builtin_types[def] = t;
0edf1bb2 5521 va_end (list);
a0274e3e
JJ
5522}
5523
c6d86fce
ILT
5524/* Build builtin functions common to both C and C++ language
5525 frontends. */
5526
5527static void
5528c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5529{
5530#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5531 builtin_types[ENUM] = VALUE;
5532#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5533 def_fn_type (ENUM, RETURN, 0, 0);
5534#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5535 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5536#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5537 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5538#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5539 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5540#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5541 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5542#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5543 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5544#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5545 ARG6) \
5546 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5547#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5548 ARG6, ARG7) \
5549 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
acf0174b
JJ
5550#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5551 ARG6, ARG7, ARG8) \
5552 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5553 ARG7, ARG8);
c6d86fce
ILT
5554#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5555 def_fn_type (ENUM, RETURN, 1, 0);
5556#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5557 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5558#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5559 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5560#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5561 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5562#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5563 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5564#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5565 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
56a9f6bc
TS
5566#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5567 ARG6, ARG7) \
5568 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5569#define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5570 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5571 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5572 ARG7, ARG8, ARG9, ARG10, ARG11);
c6d86fce
ILT
5573#define DEF_POINTER_TYPE(ENUM, TYPE) \
5574 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5575
5576#include "builtin-types.def"
5577
5578#undef DEF_PRIMITIVE_TYPE
f6a7cffc 5579#undef DEF_FUNCTION_TYPE_0
c6d86fce
ILT
5580#undef DEF_FUNCTION_TYPE_1
5581#undef DEF_FUNCTION_TYPE_2
5582#undef DEF_FUNCTION_TYPE_3
5583#undef DEF_FUNCTION_TYPE_4
5584#undef DEF_FUNCTION_TYPE_5
5585#undef DEF_FUNCTION_TYPE_6
f6a7cffc
TS
5586#undef DEF_FUNCTION_TYPE_7
5587#undef DEF_FUNCTION_TYPE_8
c6d86fce
ILT
5588#undef DEF_FUNCTION_TYPE_VAR_0
5589#undef DEF_FUNCTION_TYPE_VAR_1
5590#undef DEF_FUNCTION_TYPE_VAR_2
5591#undef DEF_FUNCTION_TYPE_VAR_3
5592#undef DEF_FUNCTION_TYPE_VAR_4
5593#undef DEF_FUNCTION_TYPE_VAR_5
56a9f6bc
TS
5594#undef DEF_FUNCTION_TYPE_VAR_7
5595#undef DEF_FUNCTION_TYPE_VAR_11
c6d86fce
ILT
5596#undef DEF_POINTER_TYPE
5597 builtin_types[(int) BT_LAST] = NULL_TREE;
5598
5599 c_init_attributes ();
5600
5601#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5602 NONANSI_P, ATTRS, IMPLICIT, COND) \
5603 if (NAME && COND) \
5604 def_builtin_1 (ENUM, NAME, CLASS, \
5605 builtin_types[(int) TYPE], \
5606 builtin_types[(int) LIBTYPE], \
5607 BOTH_P, FALLBACK_P, NONANSI_P, \
5608 built_in_attributes[(int) ATTRS], IMPLICIT);
5609#include "builtins.def"
5610#undef DEF_BUILTIN
5611
8de7ef2a
UB
5612 targetm.init_builtins ();
5613
384c400a 5614 build_common_builtin_nodes ();
939b37da 5615
b72271b9 5616 if (flag_cilkplus)
939b37da 5617 cilk_init_builtins ();
c6d86fce
ILT
5618}
5619
c1b61fca
JM
5620/* Like get_identifier, but avoid warnings about null arguments when
5621 the argument may be NULL for targets where GCC lacks stdint.h type
5622 information. */
5623
5624static inline tree
5625c_get_ident (const char *id)
5626{
5627 return get_identifier (id);
5628}
5629
a0274e3e
JJ
5630/* Build tree nodes and builtin functions common to both C and C++ language
5631 frontends. */
5632
5633void
5634c_common_nodes_and_builtins (void)
5635{
b6baa67d
KVH
5636 int char16_type_size;
5637 int char32_type_size;
eaa7c03f
JM
5638 int wchar_type_size;
5639 tree array_domain_type;
9f720c3e 5640 tree va_list_ref_type_node;
daf68dd7 5641 tree va_list_arg_type_node;
78a7c317 5642 int i;
d3707adb 5643
1a072294 5644 build_common_tree_nodes (flag_signed_char, flag_short_double);
fce5dddd 5645
eaa7c03f 5646 /* Define `int' and `char' first so that dbx will output them first. */
6496a589 5647 record_builtin_type (RID_INT, NULL, integer_type_node);
eaa7c03f
JM
5648 record_builtin_type (RID_CHAR, "char", char_type_node);
5649
5650 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5651 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5652 but not C. Are the conditionals here needed? */
37fa72e9 5653 if (c_dialect_cxx ())
6496a589 5654 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
eaa7c03f
JM
5655 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5656 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5657 record_builtin_type (RID_MAX, "long unsigned int",
5658 long_unsigned_type_node);
78a7c317
DD
5659
5660 for (i = 0; i < NUM_INT_N_ENTS; i ++)
a6766312 5661 {
78a7c317
DD
5662 char name[25];
5663
5664 sprintf (name, "__int%d", int_n_data[i].bitsize);
17958621 5665 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
78a7c317
DD
5666 int_n_trees[i].signed_type);
5667 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
17958621 5668 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
a6766312 5669 }
78a7c317 5670
37fa72e9 5671 if (c_dialect_cxx ())
eaa7c03f
JM
5672 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5673 record_builtin_type (RID_MAX, "long long int",
5674 long_long_integer_type_node);
5675 record_builtin_type (RID_MAX, "long long unsigned int",
5676 long_long_unsigned_type_node);
37fa72e9 5677 if (c_dialect_cxx ())
eaa7c03f
JM
5678 record_builtin_type (RID_MAX, "long long unsigned",
5679 long_long_unsigned_type_node);
5680 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5681 record_builtin_type (RID_MAX, "short unsigned int",
5682 short_unsigned_type_node);
37fa72e9 5683 if (c_dialect_cxx ())
eaa7c03f
JM
5684 record_builtin_type (RID_MAX, "unsigned short",
5685 short_unsigned_type_node);
5686
5687 /* Define both `signed char' and `unsigned char'. */
5688 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5689 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5690
b0c48229
NB
5691 /* These are types that c_common_type_for_size and
5692 c_common_type_for_mode use. */
c2255bc4
AH
5693 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5694 TYPE_DECL, NULL_TREE,
ae2bcd98 5695 intQI_type_node));
c2255bc4
AH
5696 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5697 TYPE_DECL, NULL_TREE,
ae2bcd98 5698 intHI_type_node));
c2255bc4
AH
5699 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5700 TYPE_DECL, NULL_TREE,
ae2bcd98 5701 intSI_type_node));
c2255bc4
AH
5702 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5703 TYPE_DECL, NULL_TREE,
ae2bcd98 5704 intDI_type_node));
eaa7c03f 5705#if HOST_BITS_PER_WIDE_INT >= 64
78a7c317
DD
5706 /* Note that this is different than the __int128 type that's part of
5707 the generic __intN support. */
1e1b8649 5708 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5709 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5710 TYPE_DECL,
1e1b8649
AP
5711 get_identifier ("__int128_t"),
5712 intTI_type_node));
eaa7c03f 5713#endif
c2255bc4
AH
5714 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5715 TYPE_DECL, NULL_TREE,
ae2bcd98 5716 unsigned_intQI_type_node));
c2255bc4
AH
5717 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5718 TYPE_DECL, NULL_TREE,
ae2bcd98 5719 unsigned_intHI_type_node));
c2255bc4
AH
5720 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5721 TYPE_DECL, NULL_TREE,
ae2bcd98 5722 unsigned_intSI_type_node));
c2255bc4
AH
5723 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5724 TYPE_DECL, NULL_TREE,
ae2bcd98 5725 unsigned_intDI_type_node));
eaa7c03f 5726#if HOST_BITS_PER_WIDE_INT >= 64
1e1b8649 5727 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5728 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5729 TYPE_DECL,
1e1b8649
AP
5730 get_identifier ("__uint128_t"),
5731 unsigned_intTI_type_node));
eaa7c03f
JM
5732#endif
5733
5734 /* Create the widest literal types. */
5735 widest_integer_literal_type_node
5736 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5737 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5738 TYPE_DECL, NULL_TREE,
ae2bcd98 5739 widest_integer_literal_type_node));
eaa7c03f
JM
5740
5741 widest_unsigned_literal_type_node
5742 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5743 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5744 TYPE_DECL, NULL_TREE,
ae2bcd98 5745 widest_unsigned_literal_type_node));
eaa7c03f 5746
c9f8536c 5747 signed_size_type_node = c_common_signed_type (size_type_node);
eaa7c03f 5748
d1c38823
ZD
5749 pid_type_node =
5750 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5751
6496a589
KG
5752 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5753 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
eaa7c03f
JM
5754 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5755
9a8ce21f
JG
5756 /* Only supported decimal floating point extension if the target
5757 actually supports underlying modes. */
b8698a0f 5758 if (targetm.scalar_mode_supported_p (SDmode)
9a8ce21f
JG
5759 && targetm.scalar_mode_supported_p (DDmode)
5760 && targetm.scalar_mode_supported_p (TDmode))
5761 {
5762 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5763 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5764 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5765 }
5766
ab22c1fa
CF
5767 if (targetm.fixed_point_supported_p ())
5768 {
5769 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5770 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5771 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5772 record_builtin_type (RID_MAX, "long long _Fract",
5773 long_long_fract_type_node);
5774 record_builtin_type (RID_MAX, "unsigned short _Fract",
5775 unsigned_short_fract_type_node);
5776 record_builtin_type (RID_MAX, "unsigned _Fract",
5777 unsigned_fract_type_node);
5778 record_builtin_type (RID_MAX, "unsigned long _Fract",
5779 unsigned_long_fract_type_node);
5780 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5781 unsigned_long_long_fract_type_node);
5782 record_builtin_type (RID_MAX, "_Sat short _Fract",
5783 sat_short_fract_type_node);
5784 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5785 record_builtin_type (RID_MAX, "_Sat long _Fract",
5786 sat_long_fract_type_node);
5787 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5788 sat_long_long_fract_type_node);
5789 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5790 sat_unsigned_short_fract_type_node);
5791 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5792 sat_unsigned_fract_type_node);
5793 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5794 sat_unsigned_long_fract_type_node);
5795 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5796 sat_unsigned_long_long_fract_type_node);
5797 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5798 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5799 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5800 record_builtin_type (RID_MAX, "long long _Accum",
5801 long_long_accum_type_node);
5802 record_builtin_type (RID_MAX, "unsigned short _Accum",
5803 unsigned_short_accum_type_node);
5804 record_builtin_type (RID_MAX, "unsigned _Accum",
5805 unsigned_accum_type_node);
5806 record_builtin_type (RID_MAX, "unsigned long _Accum",
5807 unsigned_long_accum_type_node);
5808 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5809 unsigned_long_long_accum_type_node);
5810 record_builtin_type (RID_MAX, "_Sat short _Accum",
5811 sat_short_accum_type_node);
5812 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5813 record_builtin_type (RID_MAX, "_Sat long _Accum",
5814 sat_long_accum_type_node);
5815 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5816 sat_long_long_accum_type_node);
5817 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5818 sat_unsigned_short_accum_type_node);
5819 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5820 sat_unsigned_accum_type_node);
5821 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5822 sat_unsigned_long_accum_type_node);
5823 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5824 sat_unsigned_long_long_accum_type_node);
5825
5826 }
5827
c2255bc4
AH
5828 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5829 TYPE_DECL,
ae2bcd98
RS
5830 get_identifier ("complex int"),
5831 complex_integer_type_node));
c2255bc4
AH
5832 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5833 TYPE_DECL,
ae2bcd98
RS
5834 get_identifier ("complex float"),
5835 complex_float_type_node));
c2255bc4
AH
5836 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5837 TYPE_DECL,
ae2bcd98
RS
5838 get_identifier ("complex double"),
5839 complex_double_type_node));
5840 lang_hooks.decls.pushdecl
c2255bc4
AH
5841 (build_decl (UNKNOWN_LOCATION,
5842 TYPE_DECL, get_identifier ("complex long double"),
43577e6b 5843 complex_long_double_type_node));
eaa7c03f 5844
498c0f27
JJ
5845 if (c_dialect_cxx ())
5846 /* For C++, make fileptr_type_node a distinct void * type until
5847 FILE type is defined. */
8dd16ecc 5848 fileptr_type_node = build_variant_type_copy (ptr_type_node);
498c0f27 5849
6496a589 5850 record_builtin_type (RID_VOID, NULL, void_type_node);
eaa7c03f 5851
06d40de8
DG
5852 /* Set the TYPE_NAME for any variants that were built before
5853 record_builtin_type gave names to the built-in types. */
5854 {
5855 tree void_name = TYPE_NAME (void_type_node);
5856 TYPE_NAME (void_type_node) = NULL_TREE;
5857 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5858 = void_name;
5859 TYPE_NAME (void_type_node) = void_name;
5860 }
5861
eaa7c03f
JM
5862 void_list_node = build_void_list_node ();
5863
5864 /* Make a type to be the domain of a few array types
5865 whose domains don't really matter.
5866 200 is small enough that it always fits in size_t
5867 and large enough that it can hold most function names for the
5868 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5869 array_domain_type = build_index_type (size_int (200));
5870
5871 /* Make a type for arrays of characters.
5872 With luck nothing will ever really depend on the length of this
5873 array type. */
5874 char_array_type_node
5875 = build_array_type (char_type_node, array_domain_type);
5876
10841285
MM
5877 string_type_node = build_pointer_type (char_type_node);
5878 const_string_type_node
5879 = build_pointer_type (build_qualified_type
5880 (char_type_node, TYPE_QUAL_CONST));
5881
eaa7c03f 5882 /* This is special for C++ so functions can be overloaded. */
a11eba95 5883 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
eaa7c03f
JM
5884 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5885 wchar_type_size = TYPE_PRECISION (wchar_type_node);
207bf79d 5886 underlying_wchar_type_node = wchar_type_node;
37fa72e9 5887 if (c_dialect_cxx ())
eaa7c03f 5888 {
8df83eae 5889 if (TYPE_UNSIGNED (wchar_type_node))
eaa7c03f
JM
5890 wchar_type_node = make_unsigned_type (wchar_type_size);
5891 else
5892 wchar_type_node = make_signed_type (wchar_type_size);
5893 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5894 }
eaa7c03f
JM
5895
5896 /* This is for wide string constants. */
5897 wchar_array_type_node
5898 = build_array_type (wchar_type_node, array_domain_type);
5899
b6baa67d
KVH
5900 /* Define 'char16_t'. */
5901 char16_type_node = get_identifier (CHAR16_TYPE);
5902 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5903 char16_type_size = TYPE_PRECISION (char16_type_node);
5904 if (c_dialect_cxx ())
5905 {
5906 char16_type_node = make_unsigned_type (char16_type_size);
5907
604b2bfc 5908 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5909 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5910 }
5911
5912 /* This is for UTF-16 string constants. */
5913 char16_array_type_node
5914 = build_array_type (char16_type_node, array_domain_type);
5915
5916 /* Define 'char32_t'. */
5917 char32_type_node = get_identifier (CHAR32_TYPE);
5918 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5919 char32_type_size = TYPE_PRECISION (char32_type_node);
5920 if (c_dialect_cxx ())
5921 {
5922 char32_type_node = make_unsigned_type (char32_type_size);
5923
604b2bfc 5924 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5925 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5926 }
5927
5928 /* This is for UTF-32 string constants. */
5929 char32_array_type_node
5930 = build_array_type (char32_type_node, array_domain_type);
5931
5fd8e536
JM
5932 wint_type_node =
5933 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5934
5935 intmax_type_node =
5936 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5937 uintmax_type_node =
5938 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5939
207bf79d
JM
5940 if (SIG_ATOMIC_TYPE)
5941 sig_atomic_type_node =
c1b61fca 5942 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
207bf79d
JM
5943 if (INT8_TYPE)
5944 int8_type_node =
c1b61fca 5945 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
207bf79d
JM
5946 if (INT16_TYPE)
5947 int16_type_node =
c1b61fca 5948 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
207bf79d
JM
5949 if (INT32_TYPE)
5950 int32_type_node =
c1b61fca 5951 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
207bf79d
JM
5952 if (INT64_TYPE)
5953 int64_type_node =
c1b61fca 5954 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
207bf79d
JM
5955 if (UINT8_TYPE)
5956 uint8_type_node =
c1b61fca 5957 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
207bf79d 5958 if (UINT16_TYPE)
cca615af 5959 c_uint16_type_node = uint16_type_node =
c1b61fca 5960 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
207bf79d 5961 if (UINT32_TYPE)
cca615af 5962 c_uint32_type_node = uint32_type_node =
c1b61fca 5963 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
207bf79d 5964 if (UINT64_TYPE)
cca615af 5965 c_uint64_type_node = uint64_type_node =
c1b61fca 5966 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
207bf79d
JM
5967 if (INT_LEAST8_TYPE)
5968 int_least8_type_node =
c1b61fca 5969 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
207bf79d
JM
5970 if (INT_LEAST16_TYPE)
5971 int_least16_type_node =
c1b61fca 5972 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
207bf79d
JM
5973 if (INT_LEAST32_TYPE)
5974 int_least32_type_node =
c1b61fca 5975 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
207bf79d
JM
5976 if (INT_LEAST64_TYPE)
5977 int_least64_type_node =
c1b61fca 5978 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
207bf79d
JM
5979 if (UINT_LEAST8_TYPE)
5980 uint_least8_type_node =
c1b61fca 5981 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
207bf79d
JM
5982 if (UINT_LEAST16_TYPE)
5983 uint_least16_type_node =
c1b61fca 5984 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
207bf79d
JM
5985 if (UINT_LEAST32_TYPE)
5986 uint_least32_type_node =
c1b61fca 5987 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
207bf79d
JM
5988 if (UINT_LEAST64_TYPE)
5989 uint_least64_type_node =
c1b61fca 5990 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
207bf79d
JM
5991 if (INT_FAST8_TYPE)
5992 int_fast8_type_node =
c1b61fca 5993 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
207bf79d
JM
5994 if (INT_FAST16_TYPE)
5995 int_fast16_type_node =
c1b61fca 5996 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
207bf79d
JM
5997 if (INT_FAST32_TYPE)
5998 int_fast32_type_node =
c1b61fca 5999 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
207bf79d
JM
6000 if (INT_FAST64_TYPE)
6001 int_fast64_type_node =
c1b61fca 6002 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
207bf79d
JM
6003 if (UINT_FAST8_TYPE)
6004 uint_fast8_type_node =
c1b61fca 6005 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
207bf79d
JM
6006 if (UINT_FAST16_TYPE)
6007 uint_fast16_type_node =
c1b61fca 6008 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
207bf79d
JM
6009 if (UINT_FAST32_TYPE)
6010 uint_fast32_type_node =
c1b61fca 6011 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
207bf79d
JM
6012 if (UINT_FAST64_TYPE)
6013 uint_fast64_type_node =
c1b61fca 6014 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
207bf79d
JM
6015 if (INTPTR_TYPE)
6016 intptr_type_node =
c1b61fca 6017 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
207bf79d
JM
6018 if (UINTPTR_TYPE)
6019 uintptr_type_node =
c1b61fca 6020 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
207bf79d 6021
8242dd04
NF
6022 default_function_type
6023 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5fd8e536
JM
6024 ptrdiff_type_node
6025 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
12753674 6026 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5fd8e536 6027
ae2bcd98 6028 lang_hooks.decls.pushdecl
c2255bc4
AH
6029 (build_decl (UNKNOWN_LOCATION,
6030 TYPE_DECL, get_identifier ("__builtin_va_list"),
43577e6b 6031 va_list_type_node));
38f8b050 6032 if (targetm.enum_va_list_p)
d4048208
KT
6033 {
6034 int l;
6035 const char *pname;
6036 tree ptype;
35cbb299 6037
38f8b050 6038 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
d4048208
KT
6039 {
6040 lang_hooks.decls.pushdecl
6041 (build_decl (UNKNOWN_LOCATION,
6042 TYPE_DECL, get_identifier (pname),
6043 ptype));
6044
6045 }
6046 }
daf68dd7 6047
daf68dd7 6048 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
6049 {
6050 va_list_arg_type_node = va_list_ref_type_node =
6051 build_pointer_type (TREE_TYPE (va_list_type_node));
6052 }
daf68dd7 6053 else
9f720c3e
GK
6054 {
6055 va_list_arg_type_node = va_list_type_node;
6056 va_list_ref_type_node = build_reference_type (va_list_type_node);
6057 }
35b1a6fa 6058
c6d86fce
ILT
6059 if (!flag_preprocess_only)
6060 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4677862a 6061
5b47282c 6062 main_identifier_node = get_identifier ("main");
b2f97e4a
MM
6063
6064 /* Create the built-in __null node. It is important that this is
6065 not shared. */
807e902e 6066 null_node = make_int_cst (1, 1);
b2f97e4a 6067 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
a0274e3e
JJ
6068
6069 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6070 memset (builtin_types, 0, sizeof (builtin_types));
7f4edbcb 6071}
d3707adb 6072
3b2db49f
MM
6073/* The number of named compound-literals generated thus far. */
6074static GTY(()) int compound_literal_number;
6075
6076/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6077
6078void
6079set_compound_literal_name (tree decl)
6080{
6081 char *name;
6082 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6083 compound_literal_number);
6084 compound_literal_number++;
6085 DECL_NAME (decl) = get_identifier (name);
6086}
6087
2fe1d762
TV
6088/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6089 TYPE and operand OP. */
6090
6091static tree
6092build_va_arg_1 (location_t loc, tree type, tree op)
6093{
6094 tree expr = build1 (VA_ARG_EXPR, type, op);
6095 SET_EXPR_LOCATION (expr, loc);
6096 return expr;
6097}
6098
6099/* Return a VA_ARG_EXPR corresponding to a source-level expression
6100 va_arg (EXPR, TYPE) at source location LOC. */
6101
d3707adb 6102tree
c2255bc4 6103build_va_arg (location_t loc, tree expr, tree type)
d3707adb 6104{
c7b38fd5
TV
6105 tree va_type = TREE_TYPE (expr);
6106 tree canon_va_type = (va_type == error_mark_node
6107 ? NULL_TREE
6108 : targetm.canonical_va_list_type (va_type));
6109
2fe1d762
TV
6110 if (va_type == error_mark_node
6111 || canon_va_type == NULL_TREE)
c7b38fd5 6112 {
2fe1d762
TV
6113 /* Let's handle things neutrallly, if expr:
6114 - has undeclared type, or
6115 - is not an va_list type. */
6116 return build_va_arg_1 (loc, type, expr);
c7b38fd5 6117 }
ecd0e562 6118
2fe1d762
TV
6119 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6120 {
6121 /* Case 1: Not an array type. */
6122
6123 /* Take the address, to get '&ap'. */
6124 mark_addressable (expr);
6125 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6126
6127 /* Verify that &ap is still recognized as having va_list type. */
6128 tree canon_expr_type
6129 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6130 gcc_assert (canon_expr_type != NULL_TREE);
6131
6132 return build_va_arg_1 (loc, type, expr);
6133 }
6134
6135 /* Case 2: Array type.
6136
6137 Background:
6138
6139 For contrast, let's start with the simple case (case 1). If
6140 canon_va_type is not an array type, but say a char *, then when
6141 passing-by-value a va_list, the type of the va_list param decl is
6142 the same as for another va_list decl (all ap's are char *):
6143
6144 f2_1 (char * ap)
6145 D.1815 = VA_ARG (&ap, 0B, 1);
6146 return D.1815;
6147
6148 f2 (int i)
6149 char * ap.0;
6150 char * ap;
6151 __builtin_va_start (&ap, 0);
6152 ap.0 = ap;
6153 res = f2_1 (ap.0);
6154 __builtin_va_end (&ap);
6155 D.1812 = res;
6156 return D.1812;
6157
6158 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6159 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6160 the same as for another va_list decl (case 2a, struct ap[1]).
6161
6162 f2_1 (struct * ap)
6163 D.1844 = VA_ARG (ap, 0B, 0);
6164 return D.1844;
6165
6166 f2 (int i)
6167 struct ap[1];
6168 __builtin_va_start (&ap, 0);
6169 res = f2_1 (&ap);
6170 __builtin_va_end (&ap);
6171 D.1841 = res;
6172 return D.1841;
6173
6174 Case 2b is different because:
6175 - on the callee side, the parm decl has declared type va_list, but
6176 grokdeclarator changes the type of the parm decl to a pointer to the
6177 array elem type.
6178 - on the caller side, the pass-by-value uses &ap.
6179
6180 We unify these two cases (case 2a: va_list is array type,
6181 case 2b: va_list is pointer to array elem type), by adding '&' for the
6182 array type case, such that we have a pointer to array elem in both
6183 cases. */
6184
6185 if (TREE_CODE (va_type) == ARRAY_TYPE)
6186 {
6187 /* Case 2a: va_list is array type. */
6188
6189 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6190 elem type. */
6191 mark_addressable (expr);
6192 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6193 expr);
6194
6195 /* Verify that &ap is still recognized as having va_list type. */
6196 tree canon_expr_type
6197 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6198 gcc_assert (canon_expr_type != NULL_TREE);
6199 }
6200 else
6201 {
6202 /* Case 2b: va_list is pointer to array elem type. */
6203 gcc_assert (POINTER_TYPE_P (va_type));
6204 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6205
6206 /* Don't take the address. We've already got '&ap'. */
6207 ;
6208 }
6209
6210 return build_va_arg_1 (loc, type, expr);
d3707adb 6211}
fc2aaf30
JM
6212
6213
7d14c755
JM
6214/* Linked list of disabled built-in functions. */
6215
6216typedef struct disabled_builtin
6217{
6218 const char *name;
6219 struct disabled_builtin *next;
6220} disabled_builtin;
6221static disabled_builtin *disabled_builtins = NULL;
6222
35b1a6fa 6223static bool builtin_function_disabled_p (const char *);
7d14c755
JM
6224
6225/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6226 begins with "__builtin_", give an error. */
6227
6228void
35b1a6fa 6229disable_builtin_function (const char *name)
7d14c755
JM
6230{
6231 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
bda67431 6232 error ("cannot disable built-in function %qs", name);
7d14c755
JM
6233 else
6234 {
1ad463f4
BI
6235 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6236 new_disabled_builtin->name = name;
6237 new_disabled_builtin->next = disabled_builtins;
6238 disabled_builtins = new_disabled_builtin;
7d14c755
JM
6239 }
6240}
6241
6242
6243/* Return true if the built-in function NAME has been disabled, false
6244 otherwise. */
6245
6246static bool
35b1a6fa 6247builtin_function_disabled_p (const char *name)
7d14c755
JM
6248{
6249 disabled_builtin *p;
6250 for (p = disabled_builtins; p != NULL; p = p->next)
6251 {
6252 if (strcmp (name, p->name) == 0)
6253 return true;
6254 }
6255 return false;
6256}
6257
6258
9bc15050
RG
6259/* Worker for DEF_BUILTIN.
6260 Possibly define a builtin function with one or two names.
6261 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6262 nonansi_p and flag_no_nonansi_builtin. */
fc2aaf30 6263
9bc15050
RG
6264static void
6265def_builtin_1 (enum built_in_function fncode,
6266 const char *name,
6267 enum built_in_class fnclass,
6268 tree fntype, tree libtype,
6269 bool both_p, bool fallback_p, bool nonansi_p,
6270 tree fnattrs, bool implicit_p)
fc2aaf30 6271{
9bc15050
RG
6272 tree decl;
6273 const char *libname;
6274
a0274e3e
JJ
6275 if (fntype == error_mark_node)
6276 return;
6277
9bc15050
RG
6278 gcc_assert ((!both_p && !fallback_p)
6279 || !strncmp (name, "__builtin_",
6280 strlen ("__builtin_")));
6281
6282 libname = name + strlen ("__builtin_");
c79efc4d
RÁE
6283 decl = add_builtin_function (name, fntype, fncode, fnclass,
6284 (fallback_p ? libname : NULL),
6285 fnattrs);
e79983f4
MM
6286
6287 set_builtin_decl (fncode, decl, implicit_p);
6288
9bc15050
RG
6289 if (both_p
6290 && !flag_no_builtin && !builtin_function_disabled_p (libname)
7d14c755 6291 && !(nonansi_p && flag_no_nonansi_builtin))
c79efc4d
RÁE
6292 add_builtin_function (libname, libtype, fncode, fnclass,
6293 NULL, fnattrs);
fc2aaf30 6294}
c530479e 6295\f
d72040f5
RH
6296/* Nonzero if the type T promotes to int. This is (nearly) the
6297 integral promotions defined in ISO C99 6.3.1.1/2. */
6298
6299bool
58f9752a 6300c_promoting_integer_type_p (const_tree t)
d72040f5
RH
6301{
6302 switch (TREE_CODE (t))
6303 {
6304 case INTEGER_TYPE:
6305 return (TYPE_MAIN_VARIANT (t) == char_type_node
6306 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6307 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6308 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
c6c04fca
RL
6309 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6310 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d72040f5
RH
6311
6312 case ENUMERAL_TYPE:
6313 /* ??? Technically all enumerations not larger than an int
6314 promote to an int. But this is used along code paths
6315 that only want to notice a size change. */
6316 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6317
6318 case BOOLEAN_TYPE:
6319 return 1;
6320
6321 default:
6322 return 0;
6323 }
6324}
6325
c530479e
RH
6326/* Return 1 if PARMS specifies a fixed number of parameters
6327 and none of their types is affected by default promotions. */
6328
6329int
58f9752a 6330self_promoting_args_p (const_tree parms)
c530479e 6331{
58f9752a 6332 const_tree t;
c530479e
RH
6333 for (t = parms; t; t = TREE_CHAIN (t))
6334 {
b3694847 6335 tree type = TREE_VALUE (t);
7e8176d7 6336
694fea20
VR
6337 if (type == error_mark_node)
6338 continue;
6339
c530479e
RH
6340 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6341 return 0;
6342
6343 if (type == 0)
6344 return 0;
6345
6346 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6347 return 0;
6348
d72040f5 6349 if (c_promoting_integer_type_p (type))
c530479e
RH
6350 return 0;
6351 }
6352 return 1;
6353}
5eda3d66 6354
12ea3302
GDR
6355/* Recursively remove any '*' or '&' operator from TYPE. */
6356tree
6357strip_pointer_operator (tree t)
6358{
6359 while (POINTER_TYPE_P (t))
6360 t = TREE_TYPE (t);
6361 return t;
6362}
6363
ba992967
SP
6364/* Recursively remove pointer or array type from TYPE. */
6365tree
6366strip_pointer_or_array_types (tree t)
6367{
6368 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6369 t = TREE_TYPE (t);
6370 return t;
6371}
6372
8f17b5c5
MM
6373/* Used to compare case labels. K1 and K2 are actually tree nodes
6374 representing case labels, or NULL_TREE for a `default' label.
6375 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6376 K2, and 0 if K1 and K2 are equal. */
6377
6378int
35b1a6fa 6379case_compare (splay_tree_key k1, splay_tree_key k2)
8f17b5c5
MM
6380{
6381 /* Consider a NULL key (such as arises with a `default' label) to be
6382 smaller than anything else. */
6383 if (!k1)
6384 return k2 ? -1 : 0;
6385 else if (!k2)
6386 return k1 ? 1 : 0;
6387
6388 return tree_int_cst_compare ((tree) k1, (tree) k2);
6389}
6390
c2255bc4
AH
6391/* Process a case label, located at LOC, for the range LOW_VALUE
6392 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6393 then this case label is actually a `default' label. If only
6394 HIGH_VALUE is NULL_TREE, then case label was declared using the
6395 usual C/C++ syntax, rather than the GNU case range extension.
6396 CASES is a tree containing all the case ranges processed so far;
6397 COND is the condition for the switch-statement itself. Returns the
6398 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6399 is created. */
8f17b5c5
MM
6400
6401tree
c2255bc4 6402c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
a6c0a76c 6403 tree low_value, tree high_value)
8f17b5c5
MM
6404{
6405 tree type;
6406 tree label;
6407 tree case_label;
6408 splay_tree_node node;
6409
6410 /* Create the LABEL_DECL itself. */
c2255bc4 6411 label = create_artificial_label (loc);
8f17b5c5
MM
6412
6413 /* If there was an error processing the switch condition, bail now
6414 before we get more confused. */
6415 if (!cond || cond == error_mark_node)
6de9cd9a 6416 goto error_out;
8f17b5c5 6417
35b1a6fa
AJ
6418 if ((low_value && TREE_TYPE (low_value)
6419 && POINTER_TYPE_P (TREE_TYPE (low_value)))
8f17b5c5
MM
6420 || (high_value && TREE_TYPE (high_value)
6421 && POINTER_TYPE_P (TREE_TYPE (high_value))))
522ddfa2 6422 {
c2255bc4 6423 error_at (loc, "pointers are not permitted as case values");
522ddfa2
JM
6424 goto error_out;
6425 }
8f17b5c5
MM
6426
6427 /* Case ranges are a GNU extension. */
fcf73884 6428 if (high_value)
c1771a20 6429 pedwarn (loc, OPT_Wpedantic,
fcf73884 6430 "range expressions in switch statements are non-standard");
8f17b5c5
MM
6431
6432 type = TREE_TYPE (cond);
6433 if (low_value)
6434 {
62e4eb35 6435 low_value = check_case_value (loc, low_value);
68fca595 6436 low_value = convert_and_check (loc, type, low_value);
c0e22534
NS
6437 if (low_value == error_mark_node)
6438 goto error_out;
8f17b5c5
MM
6439 }
6440 if (high_value)
6441 {
62e4eb35 6442 high_value = check_case_value (loc, high_value);
68fca595 6443 high_value = convert_and_check (loc, type, high_value);
c0e22534
NS
6444 if (high_value == error_mark_node)
6445 goto error_out;
8f17b5c5
MM
6446 }
6447
c0e22534
NS
6448 if (low_value && high_value)
6449 {
6450 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
c22cacf3
MS
6451 really a case range, even though it was written that way.
6452 Remove the HIGH_VALUE to simplify later processing. */
c0e22534
NS
6453 if (tree_int_cst_equal (low_value, high_value))
6454 high_value = NULL_TREE;
6455 else if (!tree_int_cst_lt (low_value, high_value))
c2255bc4 6456 warning_at (loc, 0, "empty range specified");
c0e22534 6457 }
8f17b5c5 6458
a6c0a76c
SB
6459 /* See if the case is in range of the type of the original testing
6460 expression. If both low_value and high_value are out of range,
6461 don't insert the case label and return NULL_TREE. */
6462 if (low_value
9d548dfb 6463 && !check_case_bounds (loc, type, orig_type,
3f75a254 6464 &low_value, high_value ? &high_value : NULL))
a6c0a76c
SB
6465 return NULL_TREE;
6466
8f17b5c5
MM
6467 /* Look up the LOW_VALUE in the table of case labels we already
6468 have. */
6469 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6470 /* If there was not an exact match, check for overlapping ranges.
6471 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6472 that's a `default' label and the only overlap is an exact match. */
6473 if (!node && (low_value || high_value))
6474 {
6475 splay_tree_node low_bound;
6476 splay_tree_node high_bound;
6477
6478 /* Even though there wasn't an exact match, there might be an
6479 overlap between this case range and another case range.
6480 Since we've (inductively) not allowed any overlapping case
6481 ranges, we simply need to find the greatest low case label
6482 that is smaller that LOW_VALUE, and the smallest low case
6483 label that is greater than LOW_VALUE. If there is an overlap
6484 it will occur in one of these two ranges. */
6485 low_bound = splay_tree_predecessor (cases,
6486 (splay_tree_key) low_value);
6487 high_bound = splay_tree_successor (cases,
6488 (splay_tree_key) low_value);
6489
6490 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6491 the LOW_VALUE, so there is no need to check unless the
6492 LOW_BOUND is in fact itself a case range. */
6493 if (low_bound
6494 && CASE_HIGH ((tree) low_bound->value)
6495 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6496 low_value) >= 0)
6497 node = low_bound;
6498 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6499 range is bigger than the low end of the current range, so we
6500 are only interested if the current range is a real range, and
6501 not an ordinary case label. */
35b1a6fa 6502 else if (high_bound
8f17b5c5
MM
6503 && high_value
6504 && (tree_int_cst_compare ((tree) high_bound->key,
6505 high_value)
6506 <= 0))
6507 node = high_bound;
6508 }
6509 /* If there was an overlap, issue an error. */
6510 if (node)
6511 {
8c161995 6512 tree duplicate = CASE_LABEL ((tree) node->value);
8f17b5c5
MM
6513
6514 if (high_value)
6515 {
c2255bc4
AH
6516 error_at (loc, "duplicate (or overlapping) case value");
6517 error_at (DECL_SOURCE_LOCATION (duplicate),
6518 "this is the first entry overlapping that value");
8f17b5c5
MM
6519 }
6520 else if (low_value)
6521 {
c2255bc4
AH
6522 error_at (loc, "duplicate case value") ;
6523 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
8f17b5c5
MM
6524 }
6525 else
6526 {
c2255bc4
AH
6527 error_at (loc, "multiple default labels in one switch");
6528 error_at (DECL_SOURCE_LOCATION (duplicate),
6529 "this is the first default label");
8f17b5c5 6530 }
6de9cd9a 6531 goto error_out;
8f17b5c5
MM
6532 }
6533
6534 /* Add a CASE_LABEL to the statement-tree. */
3d528853 6535 case_label = add_stmt (build_case_label (low_value, high_value, label));
8f17b5c5 6536 /* Register this case label in the splay tree. */
35b1a6fa 6537 splay_tree_insert (cases,
8f17b5c5
MM
6538 (splay_tree_key) low_value,
6539 (splay_tree_value) case_label);
6540
6541 return case_label;
6de9cd9a
DN
6542
6543 error_out:
9e14e18f 6544 /* Add a label so that the back-end doesn't think that the beginning of
6de9cd9a 6545 the switch is unreachable. Note that we do not add a case label, as
41806d92 6546 that just leads to duplicates and thence to failure later on. */
6de9cd9a
DN
6547 if (!cases->root)
6548 {
c2255bc4
AH
6549 tree t = create_artificial_label (loc);
6550 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6de9cd9a
DN
6551 }
6552 return error_mark_node;
6553}
6554
6555/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6556 Used to verify that case values match up with enumerator values. */
6557
6558static void
6559match_case_to_enum_1 (tree key, tree type, tree label)
6560{
807e902e
KZ
6561 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6562
6563 if (tree_fits_uhwi_p (key))
6564 print_dec (key, buf, UNSIGNED);
6565 else if (tree_fits_shwi_p (key))
6566 print_dec (key, buf, SIGNED);
6de9cd9a 6567 else
807e902e 6568 print_hex (key, buf);
6de9cd9a
DN
6569
6570 if (TYPE_NAME (type) == 0)
c5d75364
MLI
6571 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6572 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6573 "case value %qs not in enumerated type",
6574 buf);
6de9cd9a 6575 else
c5d75364
MLI
6576 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6577 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6578 "case value %qs not in enumerated type %qT",
6579 buf, type);
6de9cd9a
DN
6580}
6581
c782c2fe
RS
6582/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6583 Used to verify that case values match up with enumerator values. */
6584
6de9cd9a
DN
6585static int
6586match_case_to_enum (splay_tree_node node, void *data)
6587{
6588 tree label = (tree) node->value;
28dab132 6589 tree type = (tree) data;
6de9cd9a
DN
6590
6591 /* Skip default case. */
6592 if (!CASE_LOW (label))
6593 return 0;
6594
c782c2fe 6595 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6de9cd9a 6596 when we did our enum->case scan. Reset our scratch bit after. */
c782c2fe 6597 if (!CASE_LOW_SEEN (label))
6de9cd9a
DN
6598 match_case_to_enum_1 (CASE_LOW (label), type, label);
6599 else
c782c2fe 6600 CASE_LOW_SEEN (label) = 0;
6de9cd9a 6601
c782c2fe
RS
6602 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6603 not set, that means that CASE_HIGH did not appear when we did our
6604 enum->case scan. Reset our scratch bit after. */
6de9cd9a
DN
6605 if (CASE_HIGH (label))
6606 {
c782c2fe
RS
6607 if (!CASE_HIGH_SEEN (label))
6608 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6609 else
6610 CASE_HIGH_SEEN (label) = 0;
6de9cd9a
DN
6611 }
6612
6613 return 0;
6614}
6615
fbc315db
ILT
6616/* Handle -Wswitch*. Called from the front end after parsing the
6617 switch construct. */
6618/* ??? Should probably be somewhere generic, since other languages
6619 besides C and C++ would want this. At the moment, however, C/C++
6620 are the only tree-ssa languages that support enumerations at all,
6621 so the point is moot. */
6de9cd9a 6622
fbc315db
ILT
6623void
6624c_do_switch_warnings (splay_tree cases, location_t switch_location,
6625 tree type, tree cond)
6de9cd9a 6626{
9f63daea 6627 splay_tree_node default_node;
c782c2fe
RS
6628 splay_tree_node node;
6629 tree chain;
6de9cd9a
DN
6630
6631 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6632 return;
6633
6de9cd9a 6634 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
44c21c7f 6635 if (!default_node)
fab922b1
MLI
6636 warning_at (switch_location, OPT_Wswitch_default,
6637 "switch missing default case");
6de9cd9a 6638
c782c2fe
RS
6639 /* From here on, we only care about about enumerated types. */
6640 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6641 return;
6642
cdb88468
ILT
6643 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6644 if (!warn_switch_enum && !warn_switch)
c782c2fe
RS
6645 return;
6646
cdb88468
ILT
6647 /* Check the cases. Warn about case values which are not members of
6648 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6649 there is no default case, check that exactly all enumeration
6650 literals are covered by the cases. */
6651
c782c2fe
RS
6652 /* Clearing COND if it is not an integer constant simplifies
6653 the tests inside the loop below. */
6654 if (TREE_CODE (cond) != INTEGER_CST)
6655 cond = NULL_TREE;
6656
6657 /* The time complexity here is O(N*lg(N)) worst case, but for the
6658 common case of monotonically increasing enumerators, it is
6659 O(N), since the nature of the splay tree will keep the next
6660 element adjacent to the root at all times. */
6de9cd9a 6661
c782c2fe
RS
6662 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6663 {
6664 tree value = TREE_VALUE (chain);
adf2edec
DG
6665 if (TREE_CODE (value) == CONST_DECL)
6666 value = DECL_INITIAL (value);
c782c2fe
RS
6667 node = splay_tree_lookup (cases, (splay_tree_key) value);
6668 if (node)
6de9cd9a 6669 {
c782c2fe
RS
6670 /* Mark the CASE_LOW part of the case entry as seen. */
6671 tree label = (tree) node->value;
6672 CASE_LOW_SEEN (label) = 1;
6673 continue;
6674 }
6675
6676 /* Even though there wasn't an exact match, there might be a
fa10beec 6677 case range which includes the enumerator's value. */
c782c2fe
RS
6678 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6679 if (node && CASE_HIGH ((tree) node->value))
6680 {
6681 tree label = (tree) node->value;
6682 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6683 if (cmp >= 0)
6de9cd9a 6684 {
c782c2fe
RS
6685 /* If we match the upper bound exactly, mark the CASE_HIGH
6686 part of the case entry as seen. */
6687 if (cmp == 0)
6688 CASE_HIGH_SEEN (label) = 1;
6689 continue;
6de9cd9a
DN
6690 }
6691 }
6692
c782c2fe
RS
6693 /* We've now determined that this enumerated literal isn't
6694 handled by the case labels of the switch statement. */
6de9cd9a 6695
c782c2fe
RS
6696 /* If the switch expression is a constant, we only really care
6697 about whether that constant is handled by the switch. */
6698 if (cond && tree_int_cst_compare (cond, value))
6699 continue;
6de9cd9a 6700
683d6ff9 6701 /* If there is a default_node, the only relevant option is
cdb88468 6702 Wswitch-enum. Otherwise, if both are enabled then we prefer
683d6ff9
MLI
6703 to warn using -Wswitch because -Wswitch is enabled by -Wall
6704 while -Wswitch-enum is explicit. */
cdb88468
ILT
6705 warning_at (switch_location,
6706 (default_node || !warn_switch
6707 ? OPT_Wswitch_enum
6708 : OPT_Wswitch),
6709 "enumeration value %qE not handled in switch",
6710 TREE_PURPOSE (chain));
6de9cd9a 6711 }
c782c2fe
RS
6712
6713 /* Warn if there are case expressions that don't correspond to
6714 enumerators. This can occur since C and C++ don't enforce
6715 type-checking of assignments to enumeration variables.
6716
6717 The time complexity here is now always O(N) worst case, since
6718 we should have marked both the lower bound and upper bound of
6719 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6720 above. This scan also resets those fields. */
683d6ff9 6721
c782c2fe 6722 splay_tree_foreach (cases, match_case_to_enum, type);
8f17b5c5
MM
6723}
6724
6b665219 6725/* Finish an expression taking the address of LABEL (an
6a3799eb
AH
6726 IDENTIFIER_NODE). Returns an expression for the address.
6727
6728 LOC is the location for the expression returned. */
15b732b2 6729
35b1a6fa 6730tree
6a3799eb 6731finish_label_address_expr (tree label, location_t loc)
15b732b2
NB
6732{
6733 tree result;
6734
c1771a20 6735 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
15b732b2 6736
6b665219
MM
6737 if (label == error_mark_node)
6738 return error_mark_node;
6739
15b732b2
NB
6740 label = lookup_label (label);
6741 if (label == NULL_TREE)
6742 result = null_pointer_node;
6743 else
6744 {
6745 TREE_USED (label) = 1;
6746 result = build1 (ADDR_EXPR, ptr_type_node, label);
5805e48d 6747 /* The current function is not necessarily uninlinable.
15b732b2
NB
6748 Computed gotos are incompatible with inlining, but the value
6749 here could be used only in a diagnostic, for example. */
6a3799eb 6750 protected_set_expr_location (result, loc);
15b732b2
NB
6751 }
6752
6753 return result;
6754}
19552aa5
JM
6755\f
6756
6757/* Given a boolean expression ARG, return a tree representing an increment
6758 or decrement (as indicated by CODE) of ARG. The front end must check for
6759 invalid cases (e.g., decrement in C++). */
6760tree
35b1a6fa 6761boolean_increment (enum tree_code code, tree arg)
19552aa5
JM
6762{
6763 tree val;
b5119fa1 6764 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
37fa72e9 6765
19552aa5
JM
6766 arg = stabilize_reference (arg);
6767 switch (code)
6768 {
6769 case PREINCREMENT_EXPR:
53fb4de3 6770 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5
JM
6771 break;
6772 case POSTINCREMENT_EXPR:
53fb4de3 6773 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5 6774 arg = save_expr (arg);
53fb4de3
RS
6775 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6776 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6777 break;
6778 case PREDECREMENT_EXPR:
53fb4de3 6779 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6780 invert_truthvalue_loc (input_location, arg));
19552aa5
JM
6781 break;
6782 case POSTDECREMENT_EXPR:
53fb4de3 6783 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6784 invert_truthvalue_loc (input_location, arg));
19552aa5 6785 arg = save_expr (arg);
53fb4de3
RS
6786 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6787 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6788 break;
6789 default:
366de0ce 6790 gcc_unreachable ();
19552aa5
JM
6791 }
6792 TREE_SIDE_EFFECTS (val) = 1;
6793 return val;
6794}
03dc0325 6795\f
207bf79d
JM
6796/* Built-in macros for stddef.h and stdint.h, that require macros
6797 defined in this file. */
460bd0e3 6798void
35b1a6fa 6799c_stddef_cpp_builtins(void)
3df89291 6800{
5279d739
ZW
6801 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6802 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6803 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6804 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
85291069
JM
6805 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6806 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
c466b2cd
KVH
6807 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6808 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
207bf79d
JM
6809 if (SIG_ATOMIC_TYPE)
6810 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6811 if (INT8_TYPE)
6812 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6813 if (INT16_TYPE)
6814 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6815 if (INT32_TYPE)
6816 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6817 if (INT64_TYPE)
6818 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6819 if (UINT8_TYPE)
6820 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6821 if (UINT16_TYPE)
6822 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6823 if (UINT32_TYPE)
6824 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6825 if (UINT64_TYPE)
6826 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6827 if (INT_LEAST8_TYPE)
6828 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6829 if (INT_LEAST16_TYPE)
6830 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6831 if (INT_LEAST32_TYPE)
6832 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6833 if (INT_LEAST64_TYPE)
6834 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6835 if (UINT_LEAST8_TYPE)
6836 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6837 if (UINT_LEAST16_TYPE)
6838 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6839 if (UINT_LEAST32_TYPE)
6840 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6841 if (UINT_LEAST64_TYPE)
6842 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6843 if (INT_FAST8_TYPE)
6844 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6845 if (INT_FAST16_TYPE)
6846 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6847 if (INT_FAST32_TYPE)
6848 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6849 if (INT_FAST64_TYPE)
6850 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6851 if (UINT_FAST8_TYPE)
6852 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6853 if (UINT_FAST16_TYPE)
6854 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6855 if (UINT_FAST32_TYPE)
6856 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6857 if (UINT_FAST64_TYPE)
6858 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6859 if (INTPTR_TYPE)
6860 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6861 if (UINTPTR_TYPE)
6862 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
676997cf
RH
6863}
6864
6431177a 6865static void
35b1a6fa 6866c_init_attributes (void)
6431177a
JM
6867{
6868 /* Fill in the built_in_attributes array. */
4a90aeeb 6869#define DEF_ATTR_NULL_TREE(ENUM) \
6431177a 6870 built_in_attributes[(int) ENUM] = NULL_TREE;
4a90aeeb 6871#define DEF_ATTR_INT(ENUM, VALUE) \
c62c040f 6872 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
e384e6b5
BS
6873#define DEF_ATTR_STRING(ENUM, VALUE) \
6874 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6431177a
JM
6875#define DEF_ATTR_IDENT(ENUM, STRING) \
6876 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6877#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6878 built_in_attributes[(int) ENUM] \
6879 = tree_cons (built_in_attributes[(int) PURPOSE], \
6880 built_in_attributes[(int) VALUE], \
6881 built_in_attributes[(int) CHAIN]);
6431177a
JM
6882#include "builtin-attrs.def"
6883#undef DEF_ATTR_NULL_TREE
6884#undef DEF_ATTR_INT
6885#undef DEF_ATTR_IDENT
6886#undef DEF_ATTR_TREE_LIST
03dc0325 6887}
26f943fd 6888
943f82e7
JM
6889/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6890 identifier as an argument, so the front end shouldn't look it up. */
6891
6892bool
564a129d 6893attribute_takes_identifier_p (const_tree attr_id)
943f82e7 6894{
f231b5ff 6895 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
1b9b91a6
JM
6896 if (spec == NULL)
6897 /* Unknown attribute that we'll end up ignoring, return true so we
6898 don't complain about an identifier argument. */
6899 return true;
6900 else if (!strcmp ("mode", spec->name)
6901 || !strcmp ("format", spec->name)
6902 || !strcmp ("cleanup", spec->name))
564a129d
JM
6903 return true;
6904 else
6905 return targetm.attribute_takes_identifier_p (attr_id);
943f82e7
JM
6906}
6907
349ae713
NB
6908/* Attribute handlers common to C front ends. */
6909
6910/* Handle a "packed" attribute; arguments as in
6911 struct attribute_spec.handler. */
6912
6913static tree
e18476eb 6914handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
a742c759 6915 int flags, bool *no_add_attrs)
349ae713 6916{
c6e4cc53 6917 if (TYPE_P (*node))
349ae713
NB
6918 {
6919 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 6920 *node = build_variant_type_copy (*node);
c6e4cc53 6921 TYPE_PACKED (*node) = 1;
349ae713
NB
6922 }
6923 else if (TREE_CODE (*node) == FIELD_DECL)
646c0835 6924 {
2cd36c22
AN
6925 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6926 /* Still pack bitfields. */
6927 && ! DECL_INITIAL (*node))
646c0835
NS
6928 warning (OPT_Wattributes,
6929 "%qE attribute ignored for field of type %qT",
6930 name, TREE_TYPE (*node));
6931 else
6932 DECL_PACKED (*node) = 1;
6933 }
349ae713 6934 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
c6e4cc53
NS
6935 used for DECL_REGISTER. It wouldn't mean anything anyway.
6936 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6937 that changes what the typedef is typing. */
349ae713
NB
6938 else
6939 {
5c498b10 6940 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6941 *no_add_attrs = true;
6942 }
6943
6944 return NULL_TREE;
6945}
6946
6947/* Handle a "nocommon" attribute; arguments as in
6948 struct attribute_spec.handler. */
6949
6950static tree
35b1a6fa 6951handle_nocommon_attribute (tree *node, tree name,
e18476eb
BI
6952 tree ARG_UNUSED (args),
6953 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6954{
6955 if (TREE_CODE (*node) == VAR_DECL)
6956 DECL_COMMON (*node) = 0;
6957 else
6958 {
5c498b10 6959 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6960 *no_add_attrs = true;
6961 }
6962
6963 return NULL_TREE;
6964}
6965
6966/* Handle a "common" attribute; arguments as in
6967 struct attribute_spec.handler. */
6968
6969static tree
e18476eb
BI
6970handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6971 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6972{
6973 if (TREE_CODE (*node) == VAR_DECL)
6974 DECL_COMMON (*node) = 1;
6975 else
6976 {
5c498b10 6977 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6978 *no_add_attrs = true;
6979 }
6980
6981 return NULL_TREE;
6982}
6983
6984/* Handle a "noreturn" attribute; arguments as in
6985 struct attribute_spec.handler. */
6986
6987static tree
e18476eb
BI
6988handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6989 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6990{
6991 tree type = TREE_TYPE (*node);
6992
6993 /* See FIXME comment in c_common_attribute_table. */
2debdb4f
NP
6994 if (TREE_CODE (*node) == FUNCTION_DECL
6995 || objc_method_decl (TREE_CODE (*node)))
349ae713
NB
6996 TREE_THIS_VOLATILE (*node) = 1;
6997 else if (TREE_CODE (type) == POINTER_TYPE
6998 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6999 TREE_TYPE (*node)
6e7ceb17
PC
7000 = (build_qualified_type
7001 (build_pointer_type
7002 (build_type_variant (TREE_TYPE (type),
7003 TYPE_READONLY (TREE_TYPE (type)), 1)),
7004 TYPE_QUALS (type)));
349ae713
NB
7005 else
7006 {
5c498b10 7007 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7008 *no_add_attrs = true;
7009 }
7010
7011 return NULL_TREE;
7012}
7013
52bf96d2
JH
7014/* Handle a "hot" and attribute; arguments as in
7015 struct attribute_spec.handler. */
7016
7017static tree
7018handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
ab442df7 7019 int ARG_UNUSED (flags), bool *no_add_attrs)
52bf96d2 7020{
e45abe1f
RH
7021 if (TREE_CODE (*node) == FUNCTION_DECL
7022 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
7023 {
7024 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7025 {
45484dcf
MP
7026 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7027 "with attribute %qs", name, "cold");
52bf96d2
JH
7028 *no_add_attrs = true;
7029 }
5779e713
MM
7030 /* Most of the rest of the hot processing is done later with
7031 lookup_attribute. */
52bf96d2
JH
7032 }
7033 else
7034 {
7035 warning (OPT_Wattributes, "%qE attribute ignored", name);
7036 *no_add_attrs = true;
7037 }
7038
7039 return NULL_TREE;
7040}
e45abe1f 7041
52bf96d2
JH
7042/* Handle a "cold" and attribute; arguments as in
7043 struct attribute_spec.handler. */
7044
7045static tree
7046handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7047 int ARG_UNUSED (flags), bool *no_add_attrs)
7048{
e45abe1f
RH
7049 if (TREE_CODE (*node) == FUNCTION_DECL
7050 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
7051 {
7052 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7053 {
45484dcf
MP
7054 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7055 "with attribute %qs", name, "hot");
52bf96d2
JH
7056 *no_add_attrs = true;
7057 }
5779e713
MM
7058 /* Most of the rest of the cold processing is done later with
7059 lookup_attribute. */
52bf96d2
JH
7060 }
7061 else
7062 {
7063 warning (OPT_Wattributes, "%qE attribute ignored", name);
7064 *no_add_attrs = true;
7065 }
7066
7067 return NULL_TREE;
7068}
7069
e664c61c 7070/* Handle a "no_sanitize_address" attribute; arguments as in
77bc5132
JJ
7071 struct attribute_spec.handler. */
7072
7073static tree
e664c61c
KS
7074handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7075 bool *no_add_attrs)
77bc5132
JJ
7076{
7077 if (TREE_CODE (*node) != FUNCTION_DECL)
7078 {
7079 warning (OPT_Wattributes, "%qE attribute ignored", name);
7080 *no_add_attrs = true;
7081 }
7082
7083 return NULL_TREE;
7084}
7085
e664c61c
KS
7086/* Handle a "no_address_safety_analysis" attribute; arguments as in
7087 struct attribute_spec.handler. */
7088
7089static tree
7090handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7091 bool *no_add_attrs)
7092{
7093 if (TREE_CODE (*node) != FUNCTION_DECL)
7094 warning (OPT_Wattributes, "%qE attribute ignored", name);
7095 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7096 DECL_ATTRIBUTES (*node)
7097 = tree_cons (get_identifier ("no_sanitize_address"),
7098 NULL_TREE, DECL_ATTRIBUTES (*node));
7099 *no_add_attrs = true;
7100 return NULL_TREE;
7101}
7102
ce6923c5
MP
7103/* Handle a "no_sanitize_undefined" attribute; arguments as in
7104 struct attribute_spec.handler. */
7105
7106static tree
7107handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7108 bool *no_add_attrs)
7109{
7110 if (TREE_CODE (*node) != FUNCTION_DECL)
7111 {
7112 warning (OPT_Wattributes, "%qE attribute ignored", name);
7113 *no_add_attrs = true;
7114 }
7115
7116 return NULL_TREE;
7117}
7118
5434dc07
MD
7119/* Handle a "stack_protect" attribute; arguments as in
7120 struct attribute_spec.handler. */
7121static tree
7122handle_stack_protect_attribute (tree *node, tree name, tree, int,
7123 bool *no_add_attrs)
7124{
7125 if (TREE_CODE (*node) != FUNCTION_DECL)
7126 {
7127 warning (OPT_Wattributes, "%qE attribute ignored", name);
7128 *no_add_attrs = true;
7129 }
7130 else
7131 DECL_ATTRIBUTES (*node)
7132 = tree_cons (get_identifier ("stack_protect"),
7133 NULL_TREE, DECL_ATTRIBUTES (*node));
7134
7135 return NULL_TREE;
7136}
7137
349ae713
NB
7138/* Handle a "noinline" attribute; arguments as in
7139 struct attribute_spec.handler. */
7140
7141static tree
35b1a6fa 7142handle_noinline_attribute (tree *node, tree name,
e18476eb
BI
7143 tree ARG_UNUSED (args),
7144 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7145{
7146 if (TREE_CODE (*node) == FUNCTION_DECL)
45484dcf
MP
7147 {
7148 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7149 {
7150 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7151 "with attribute %qs", name, "always_inline");
7152 *no_add_attrs = true;
7153 }
7154 else
7155 DECL_UNINLINABLE (*node) = 1;
7156 }
349ae713
NB
7157 else
7158 {
5c498b10 7159 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7160 *no_add_attrs = true;
7161 }
7162
7163 return NULL_TREE;
7164}
7165
86631ea3
MJ
7166/* Handle a "noclone" attribute; arguments as in
7167 struct attribute_spec.handler. */
7168
7169static tree
7170handle_noclone_attribute (tree *node, tree name,
7171 tree ARG_UNUSED (args),
7172 int ARG_UNUSED (flags), bool *no_add_attrs)
7173{
7174 if (TREE_CODE (*node) != FUNCTION_DECL)
7175 {
7176 warning (OPT_Wattributes, "%qE attribute ignored", name);
7177 *no_add_attrs = true;
7178 }
7179
7180 return NULL_TREE;
7181}
7182
185c9e56
ML
7183/* Handle a "no_icf" attribute; arguments as in
7184 struct attribute_spec.handler. */
7185
7186static tree
7187handle_noicf_attribute (tree *node, tree name,
7188 tree ARG_UNUSED (args),
7189 int ARG_UNUSED (flags), bool *no_add_attrs)
7190{
7191 if (TREE_CODE (*node) != FUNCTION_DECL)
7192 {
7193 warning (OPT_Wattributes, "%qE attribute ignored", name);
7194 *no_add_attrs = true;
7195 }
7196
7197 return NULL_TREE;
7198}
7199
7200
349ae713
NB
7201/* Handle a "always_inline" attribute; arguments as in
7202 struct attribute_spec.handler. */
7203
7204static tree
35b1a6fa 7205handle_always_inline_attribute (tree *node, tree name,
e18476eb
BI
7206 tree ARG_UNUSED (args),
7207 int ARG_UNUSED (flags),
a742c759 7208 bool *no_add_attrs)
349ae713
NB
7209{
7210 if (TREE_CODE (*node) == FUNCTION_DECL)
7211 {
45484dcf
MP
7212 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7213 {
7214 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7215 "with %qs attribute", name, "noinline");
7216 *no_add_attrs = true;
7217 }
7218 else
7219 /* Set the attribute and mark it for disregarding inline
7220 limits. */
7221 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
349ae713
NB
7222 }
7223 else
7224 {
5c498b10 7225 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713 7226 *no_add_attrs = true;
4eb7fd83
JJ
7227 }
7228
7229 return NULL_TREE;
7230}
7231
7232/* Handle a "gnu_inline" attribute; arguments as in
7233 struct attribute_spec.handler. */
7234
7235static tree
7236handle_gnu_inline_attribute (tree *node, tree name,
7237 tree ARG_UNUSED (args),
7238 int ARG_UNUSED (flags),
7239 bool *no_add_attrs)
7240{
7241 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7242 {
7243 /* Do nothing else, just set the attribute. We'll get at
7244 it later with lookup_attribute. */
7245 }
7246 else
7247 {
7248 warning (OPT_Wattributes, "%qE attribute ignored", name);
7249 *no_add_attrs = true;
46a4da10
JH
7250 }
7251
7252 return NULL_TREE;
7253}
7254
7255/* Handle a "leaf" attribute; arguments as in
7256 struct attribute_spec.handler. */
7257
7258static tree
7259handle_leaf_attribute (tree *node, tree name,
7260 tree ARG_UNUSED (args),
7261 int ARG_UNUSED (flags), bool *no_add_attrs)
7262{
7263 if (TREE_CODE (*node) != FUNCTION_DECL)
7264 {
7265 warning (OPT_Wattributes, "%qE attribute ignored", name);
7266 *no_add_attrs = true;
7267 }
7268 if (!TREE_PUBLIC (*node))
7269 {
7270 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7271 *no_add_attrs = true;
349ae713
NB
7272 }
7273
7274 return NULL_TREE;
7275}
7276
d752cfdb
JJ
7277/* Handle an "artificial" attribute; arguments as in
7278 struct attribute_spec.handler. */
7279
7280static tree
7281handle_artificial_attribute (tree *node, tree name,
7282 tree ARG_UNUSED (args),
7283 int ARG_UNUSED (flags),
7284 bool *no_add_attrs)
7285{
7286 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7287 {
7288 /* Do nothing else, just set the attribute. We'll get at
7289 it later with lookup_attribute. */
7290 }
7291 else
7292 {
7293 warning (OPT_Wattributes, "%qE attribute ignored", name);
7294 *no_add_attrs = true;
7295 }
7296
7297 return NULL_TREE;
7298}
7299
0691d1d4
RG
7300/* Handle a "flatten" attribute; arguments as in
7301 struct attribute_spec.handler. */
7302
7303static tree
7304handle_flatten_attribute (tree *node, tree name,
c22cacf3
MS
7305 tree args ATTRIBUTE_UNUSED,
7306 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0691d1d4
RG
7307{
7308 if (TREE_CODE (*node) == FUNCTION_DECL)
7309 /* Do nothing else, just set the attribute. We'll get at
7310 it later with lookup_attribute. */
7311 ;
7312 else
7313 {
7314 warning (OPT_Wattributes, "%qE attribute ignored", name);
7315 *no_add_attrs = true;
7316 }
7317
7318 return NULL_TREE;
7319}
7320
d2af6a68
JJ
7321/* Handle a "warning" or "error" attribute; arguments as in
7322 struct attribute_spec.handler. */
7323
7324static tree
7325handle_error_attribute (tree *node, tree name, tree args,
7326 int ARG_UNUSED (flags), bool *no_add_attrs)
7327{
7328 if (TREE_CODE (*node) == FUNCTION_DECL
cf35e2b1 7329 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
d2af6a68
JJ
7330 /* Do nothing else, just set the attribute. We'll get at
7331 it later with lookup_attribute. */
7332 ;
7333 else
7334 {
7335 warning (OPT_Wattributes, "%qE attribute ignored", name);
7336 *no_add_attrs = true;
7337 }
7338
7339 return NULL_TREE;
7340}
0691d1d4 7341
349ae713
NB
7342/* Handle a "used" attribute; arguments as in
7343 struct attribute_spec.handler. */
7344
7345static tree
e18476eb
BI
7346handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7347 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 7348{
d7ddbe24
RH
7349 tree node = *pnode;
7350
7351 if (TREE_CODE (node) == FUNCTION_DECL
3797cb21
DS
7352 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7353 || (TREE_CODE (node) == TYPE_DECL))
4d7d0451 7354 {
4d7d0451 7355 TREE_USED (node) = 1;
8e3e233b 7356 DECL_PRESERVE_P (node) = 1;
ebfbbdc5
JJ
7357 if (TREE_CODE (node) == VAR_DECL)
7358 DECL_READ_P (node) = 1;
4d7d0451 7359 }
349ae713
NB
7360 else
7361 {
5c498b10 7362 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7363 *no_add_attrs = true;
7364 }
7365
7366 return NULL_TREE;
7367}
7368
7369/* Handle a "unused" attribute; arguments as in
7370 struct attribute_spec.handler. */
7371
7372static tree
e18476eb
BI
7373handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7374 int flags, bool *no_add_attrs)
349ae713
NB
7375{
7376 if (DECL_P (*node))
7377 {
7378 tree decl = *node;
7379
7380 if (TREE_CODE (decl) == PARM_DECL
7381 || TREE_CODE (decl) == VAR_DECL
7382 || TREE_CODE (decl) == FUNCTION_DECL
7383 || TREE_CODE (decl) == LABEL_DECL
7384 || TREE_CODE (decl) == TYPE_DECL)
ebfbbdc5
JJ
7385 {
7386 TREE_USED (decl) = 1;
7387 if (TREE_CODE (decl) == VAR_DECL
7388 || TREE_CODE (decl) == PARM_DECL)
7389 DECL_READ_P (decl) = 1;
7390 }
349ae713
NB
7391 else
7392 {
5c498b10 7393 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7394 *no_add_attrs = true;
7395 }
7396 }
7397 else
7398 {
7399 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 7400 *node = build_variant_type_copy (*node);
349ae713
NB
7401 TREE_USED (*node) = 1;
7402 }
7403
7404 return NULL_TREE;
7405}
7406
ce91e74c
JH
7407/* Handle a "externally_visible" attribute; arguments as in
7408 struct attribute_spec.handler. */
7409
7410static tree
7411handle_externally_visible_attribute (tree *pnode, tree name,
7412 tree ARG_UNUSED (args),
7413 int ARG_UNUSED (flags),
7414 bool *no_add_attrs)
7415{
7416 tree node = *pnode;
7417
21b634ae 7418 if (VAR_OR_FUNCTION_DECL_P (node))
ce91e74c 7419 {
343d4b27
JJ
7420 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7421 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7422 {
7423 warning (OPT_Wattributes,
7424 "%qE attribute have effect only on public objects", name);
7425 *no_add_attrs = true;
7426 }
ce91e74c 7427 }
ce91e74c
JH
7428 else
7429 {
7430 warning (OPT_Wattributes, "%qE attribute ignored", name);
7431 *no_add_attrs = true;
7432 }
7433
7434 return NULL_TREE;
7435}
7436
7861b648
AK
7437/* Handle the "no_reorder" attribute. Arguments as in
7438 struct attribute_spec.handler. */
7439
7440static tree
7441handle_no_reorder_attribute (tree *pnode,
7442 tree name,
7443 tree,
7444 int,
7445 bool *no_add_attrs)
7446{
7447 tree node = *pnode;
7448
21b634ae 7449 if (!VAR_OR_FUNCTION_DECL_P (node)
7861b648
AK
7450 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7451 {
7452 warning (OPT_Wattributes,
7453 "%qE attribute only affects top level objects",
7454 name);
7455 *no_add_attrs = true;
7456 }
7457
7458 return NULL_TREE;
7459}
7460
349ae713
NB
7461/* Handle a "const" attribute; arguments as in
7462 struct attribute_spec.handler. */
7463
7464static tree
e18476eb
BI
7465handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7466 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7467{
7468 tree type = TREE_TYPE (*node);
7469
7470 /* See FIXME comment on noreturn in c_common_attribute_table. */
7471 if (TREE_CODE (*node) == FUNCTION_DECL)
7472 TREE_READONLY (*node) = 1;
7473 else if (TREE_CODE (type) == POINTER_TYPE
7474 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7475 TREE_TYPE (*node)
6e7ceb17
PC
7476 = (build_qualified_type
7477 (build_pointer_type
7478 (build_type_variant (TREE_TYPE (type), 1,
7479 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7480 TYPE_QUALS (type)));
349ae713
NB
7481 else
7482 {
5c498b10 7483 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7484 *no_add_attrs = true;
7485 }
7486
7487 return NULL_TREE;
7488}
7489
7490/* Handle a "transparent_union" attribute; arguments as in
7491 struct attribute_spec.handler. */
7492
7493static tree
35b1a6fa 7494handle_transparent_union_attribute (tree *node, tree name,
e18476eb 7495 tree ARG_UNUSED (args), int flags,
a742c759 7496 bool *no_add_attrs)
349ae713 7497{
4009f2e7 7498 tree type;
52dd234b
RH
7499
7500 *no_add_attrs = true;
349ae713 7501
e28d52cf
DS
7502
7503 if (TREE_CODE (*node) == TYPE_DECL
7504 && ! (flags & ATTR_FLAG_CXX11))
4009f2e7
JM
7505 node = &TREE_TYPE (*node);
7506 type = *node;
349ae713 7507
52dd234b 7508 if (TREE_CODE (type) == UNION_TYPE)
349ae713 7509 {
d58d6eb5
JM
7510 /* Make sure that the first field will work for a transparent union.
7511 If the type isn't complete yet, leave the check to the code in
7512 finish_struct. */
7513 if (TYPE_SIZE (type))
7514 {
7515 tree first = first_field (type);
7516 if (first == NULL_TREE
7517 || DECL_ARTIFICIAL (first)
7518 || TYPE_MODE (type) != DECL_MODE (first))
7519 goto ignored;
7520 }
7521
349ae713 7522 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
52dd234b 7523 {
d58d6eb5
JM
7524 /* If the type isn't complete yet, setting the flag
7525 on a variant wouldn't ever be checked. */
7526 if (!TYPE_SIZE (type))
7527 goto ignored;
7528
7529 /* build_duplicate_type doesn't work for C++. */
7530 if (c_dialect_cxx ())
52dd234b
RH
7531 goto ignored;
7532
7533 /* A type variant isn't good enough, since we don't a cast
7534 to such a type removed as a no-op. */
7535 *node = type = build_duplicate_type (type);
7536 }
7537
ebf0bf7f 7538 TYPE_TRANSPARENT_AGGR (type) = 1;
52dd234b 7539 return NULL_TREE;
349ae713
NB
7540 }
7541
52dd234b
RH
7542 ignored:
7543 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7544 return NULL_TREE;
7545}
7546
fc8600f9
MM
7547/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7548 get the requested priority for a constructor or destructor,
7549 possibly issuing diagnostics for invalid or reserved
7550 priorities. */
7551
7552static priority_type
7553get_priority (tree args, bool is_destructor)
7554{
7555 HOST_WIDE_INT pri;
b2f4bed8 7556 tree arg;
fc8600f9
MM
7557
7558 if (!args)
7559 return DEFAULT_INIT_PRIORITY;
b8698a0f 7560
f6fc5c86
MM
7561 if (!SUPPORTS_INIT_PRIORITY)
7562 {
7563 if (is_destructor)
7564 error ("destructor priorities are not supported");
7565 else
7566 error ("constructor priorities are not supported");
7567 return DEFAULT_INIT_PRIORITY;
7568 }
7569
b2f4bed8 7570 arg = TREE_VALUE (args);
fad7652e
JJ
7571 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7572 goto invalid;
7573 if (arg == error_mark_node)
7574 return DEFAULT_INIT_PRIORITY;
8d0d1915 7575 arg = default_conversion (arg);
9541ffee 7576 if (!tree_fits_shwi_p (arg)
b2f4bed8 7577 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
fc8600f9
MM
7578 goto invalid;
7579
9439e9a1 7580 pri = tree_to_shwi (arg);
fc8600f9
MM
7581 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7582 goto invalid;
7583
7584 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7585 {
7586 if (is_destructor)
7587 warning (0,
7588 "destructor priorities from 0 to %d are reserved "
b8698a0f 7589 "for the implementation",
fc8600f9
MM
7590 MAX_RESERVED_INIT_PRIORITY);
7591 else
7592 warning (0,
7593 "constructor priorities from 0 to %d are reserved "
b8698a0f 7594 "for the implementation",
fc8600f9
MM
7595 MAX_RESERVED_INIT_PRIORITY);
7596 }
7597 return pri;
7598
7599 invalid:
7600 if (is_destructor)
7601 error ("destructor priorities must be integers from 0 to %d inclusive",
7602 MAX_INIT_PRIORITY);
7603 else
7604 error ("constructor priorities must be integers from 0 to %d inclusive",
7605 MAX_INIT_PRIORITY);
7606 return DEFAULT_INIT_PRIORITY;
7607}
7608
349ae713
NB
7609/* Handle a "constructor" attribute; arguments as in
7610 struct attribute_spec.handler. */
7611
7612static tree
fc8600f9 7613handle_constructor_attribute (tree *node, tree name, tree args,
e18476eb 7614 int ARG_UNUSED (flags),
a742c759 7615 bool *no_add_attrs)
349ae713
NB
7616{
7617 tree decl = *node;
7618 tree type = TREE_TYPE (decl);
7619
7620 if (TREE_CODE (decl) == FUNCTION_DECL
7621 && TREE_CODE (type) == FUNCTION_TYPE
7622 && decl_function_context (decl) == 0)
7623 {
fc8600f9 7624 priority_type priority;
349ae713 7625 DECL_STATIC_CONSTRUCTOR (decl) = 1;
fc8600f9
MM
7626 priority = get_priority (args, /*is_destructor=*/false);
7627 SET_DECL_INIT_PRIORITY (decl, priority);
349ae713
NB
7628 TREE_USED (decl) = 1;
7629 }
7630 else
7631 {
5c498b10 7632 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7633 *no_add_attrs = true;
7634 }
7635
7636 return NULL_TREE;
7637}
7638
7639/* Handle a "destructor" attribute; arguments as in
7640 struct attribute_spec.handler. */
7641
7642static tree
fc8600f9 7643handle_destructor_attribute (tree *node, tree name, tree args,
e18476eb 7644 int ARG_UNUSED (flags),
a742c759 7645 bool *no_add_attrs)
349ae713
NB
7646{
7647 tree decl = *node;
7648 tree type = TREE_TYPE (decl);
7649
7650 if (TREE_CODE (decl) == FUNCTION_DECL
7651 && TREE_CODE (type) == FUNCTION_TYPE
7652 && decl_function_context (decl) == 0)
7653 {
fc8600f9 7654 priority_type priority;
349ae713 7655 DECL_STATIC_DESTRUCTOR (decl) = 1;
fc8600f9
MM
7656 priority = get_priority (args, /*is_destructor=*/true);
7657 SET_DECL_FINI_PRIORITY (decl, priority);
349ae713
NB
7658 TREE_USED (decl) = 1;
7659 }
7660 else
7661 {
5c498b10 7662 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7663 *no_add_attrs = true;
7664 }
7665
7666 return NULL_TREE;
7667}
7668
4d451982
MLI
7669/* Nonzero if the mode is a valid vector mode for this architecture.
7670 This returns nonzero even if there is no hardware support for the
7671 vector mode, but we can emulate with narrower modes. */
7672
7673static int
ef4bddc2 7674vector_mode_valid_p (machine_mode mode)
4d451982
MLI
7675{
7676 enum mode_class mclass = GET_MODE_CLASS (mode);
ef4bddc2 7677 machine_mode innermode;
4d451982
MLI
7678
7679 /* Doh! What's going on? */
7680 if (mclass != MODE_VECTOR_INT
7681 && mclass != MODE_VECTOR_FLOAT
7682 && mclass != MODE_VECTOR_FRACT
7683 && mclass != MODE_VECTOR_UFRACT
7684 && mclass != MODE_VECTOR_ACCUM
7685 && mclass != MODE_VECTOR_UACCUM)
7686 return 0;
7687
7688 /* Hardware support. Woo hoo! */
7689 if (targetm.vector_mode_supported_p (mode))
7690 return 1;
7691
7692 innermode = GET_MODE_INNER (mode);
7693
7694 /* We should probably return 1 if requesting V4DI and we have no DI,
7695 but we have V2DI, but this is probably very unlikely. */
7696
7697 /* If we have support for the inner mode, we can safely emulate it.
7698 We may not have V2DI, but me can emulate with a pair of DIs. */
7699 return targetm.scalar_mode_supported_p (innermode);
7700}
7701
7702
349ae713
NB
7703/* Handle a "mode" attribute; arguments as in
7704 struct attribute_spec.handler. */
7705
7706static tree
e18476eb
BI
7707handle_mode_attribute (tree *node, tree name, tree args,
7708 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7709{
7710 tree type = *node;
88388a52 7711 tree ident = TREE_VALUE (args);
349ae713
NB
7712
7713 *no_add_attrs = true;
7714
88388a52 7715 if (TREE_CODE (ident) != IDENTIFIER_NODE)
5c498b10 7716 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7717 else
7718 {
7719 int j;
88388a52 7720 const char *p = IDENTIFIER_POINTER (ident);
349ae713 7721 int len = strlen (p);
ef4bddc2 7722 machine_mode mode = VOIDmode;
349ae713 7723 tree typefm;
6dd53648 7724 bool valid_mode;
349ae713
NB
7725
7726 if (len > 4 && p[0] == '_' && p[1] == '_'
7727 && p[len - 1] == '_' && p[len - 2] == '_')
7728 {
28dab132 7729 char *newp = (char *) alloca (len - 1);
349ae713
NB
7730
7731 strcpy (newp, &p[2]);
7732 newp[len - 4] = '\0';
7733 p = newp;
7734 }
7735
7736 /* Change this type to have a type with the specified mode.
7737 First check for the special modes. */
3f75a254 7738 if (!strcmp (p, "byte"))
349ae713
NB
7739 mode = byte_mode;
7740 else if (!strcmp (p, "word"))
7741 mode = word_mode;
3f75a254 7742 else if (!strcmp (p, "pointer"))
349ae713 7743 mode = ptr_mode;
c7ff6e7a
AK
7744 else if (!strcmp (p, "libgcc_cmp_return"))
7745 mode = targetm.libgcc_cmp_return_mode ();
7746 else if (!strcmp (p, "libgcc_shift_count"))
7747 mode = targetm.libgcc_shift_count_mode ();
7b0518e3
UW
7748 else if (!strcmp (p, "unwind_word"))
7749 mode = targetm.unwind_word_mode ();
349ae713
NB
7750 else
7751 for (j = 0; j < NUM_MACHINE_MODES; j++)
7752 if (!strcmp (p, GET_MODE_NAME (j)))
61f03aba 7753 {
ef4bddc2 7754 mode = (machine_mode) j;
61f03aba
RH
7755 break;
7756 }
349ae713
NB
7757
7758 if (mode == VOIDmode)
4a5eab38 7759 {
88388a52 7760 error ("unknown machine mode %qE", ident);
4a5eab38
PB
7761 return NULL_TREE;
7762 }
7763
6dd53648
RH
7764 valid_mode = false;
7765 switch (GET_MODE_CLASS (mode))
4a5eab38 7766 {
6dd53648
RH
7767 case MODE_INT:
7768 case MODE_PARTIAL_INT:
7769 case MODE_FLOAT:
9a8ce21f 7770 case MODE_DECIMAL_FLOAT:
ab22c1fa
CF
7771 case MODE_FRACT:
7772 case MODE_UFRACT:
7773 case MODE_ACCUM:
7774 case MODE_UACCUM:
6dd53648
RH
7775 valid_mode = targetm.scalar_mode_supported_p (mode);
7776 break;
7777
7778 case MODE_COMPLEX_INT:
7779 case MODE_COMPLEX_FLOAT:
7780 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7781 break;
7782
7783 case MODE_VECTOR_INT:
7784 case MODE_VECTOR_FLOAT:
ab22c1fa
CF
7785 case MODE_VECTOR_FRACT:
7786 case MODE_VECTOR_UFRACT:
7787 case MODE_VECTOR_ACCUM:
7788 case MODE_VECTOR_UACCUM:
5c498b10
DD
7789 warning (OPT_Wattributes, "specifying vector types with "
7790 "__attribute__ ((mode)) is deprecated");
7791 warning (OPT_Wattributes,
7792 "use __attribute__ ((vector_size)) instead");
6dd53648
RH
7793 valid_mode = vector_mode_valid_p (mode);
7794 break;
4a5eab38 7795
6dd53648
RH
7796 default:
7797 break;
7798 }
7799 if (!valid_mode)
7800 {
9e637a26 7801 error ("unable to emulate %qs", p);
6dd53648
RH
7802 return NULL_TREE;
7803 }
4a5eab38 7804
6dd53648 7805 if (POINTER_TYPE_P (type))
cb2a532e 7806 {
36c5e70a 7807 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
ef4bddc2 7808 tree (*fn)(tree, machine_mode, bool);
6dd53648 7809
36c5e70a 7810 if (!targetm.addr_space.valid_pointer_mode (mode, as))
cb2a532e 7811 {
9e637a26 7812 error ("invalid pointer mode %qs", p);
cb2a532e
AH
7813 return NULL_TREE;
7814 }
7815
c22cacf3 7816 if (TREE_CODE (type) == POINTER_TYPE)
6dd53648 7817 fn = build_pointer_type_for_mode;
4977bab6 7818 else
6dd53648
RH
7819 fn = build_reference_type_for_mode;
7820 typefm = fn (TREE_TYPE (type), mode, false);
cb2a532e 7821 }
6dd53648 7822 else
ab22c1fa
CF
7823 {
7824 /* For fixed-point modes, we need to test if the signness of type
7825 and the machine mode are consistent. */
7826 if (ALL_FIXED_POINT_MODE_P (mode)
7827 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7828 {
d8a07487 7829 error ("signedness of type and machine mode %qs don%'t match", p);
ab22c1fa
CF
7830 return NULL_TREE;
7831 }
7832 /* For fixed-point modes, we need to pass saturating info. */
7833 typefm = lang_hooks.types.type_for_mode (mode,
7834 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7835 : TYPE_UNSIGNED (type));
7836 }
ec8465a5 7837
6dd53648
RH
7838 if (typefm == NULL_TREE)
7839 {
61f03aba 7840 error ("no data type for mode %qs", p);
6dd53648
RH
7841 return NULL_TREE;
7842 }
ec8465a5
RK
7843 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7844 {
7845 /* For enumeral types, copy the precision from the integer
7846 type returned above. If not an INTEGER_TYPE, we can't use
7847 this mode for this type. */
7848 if (TREE_CODE (typefm) != INTEGER_TYPE)
7849 {
61f03aba 7850 error ("cannot use mode %qs for enumeral types", p);
ec8465a5
RK
7851 return NULL_TREE;
7852 }
7853
99db1ef0
RH
7854 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7855 {
7856 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7857 typefm = type;
7858 }
7859 else
7860 {
7861 /* We cannot build a type variant, as there's code that assumes
7862 that TYPE_MAIN_VARIANT has the same mode. This includes the
7863 debug generators. Instead, create a subrange type. This
7864 results in all of the enumeral values being emitted only once
7865 in the original, and the subtype gets them by reference. */
7866 if (TYPE_UNSIGNED (type))
7867 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7868 else
7869 typefm = make_signed_type (TYPE_PRECISION (typefm));
7870 TREE_TYPE (typefm) = type;
7871 }
ec8465a5 7872 }
a2d36602
RH
7873 else if (VECTOR_MODE_P (mode)
7874 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7875 : TREE_CODE (type) != TREE_CODE (typefm))
61f03aba
RH
7876 {
7877 error ("mode %qs applied to inappropriate type", p);
7878 return NULL_TREE;
7879 }
7880
6dd53648 7881 *node = typefm;
349ae713
NB
7882 }
7883
7884 return NULL_TREE;
7885}
7886
7887/* Handle a "section" attribute; arguments as in
7888 struct attribute_spec.handler. */
7889
7890static tree
e18476eb
BI
7891handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7892 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7893{
7894 tree decl = *node;
7895
0373796b 7896 if (!targetm_common.have_named_sections)
349ae713 7897 {
0373796b
JT
7898 error_at (DECL_SOURCE_LOCATION (*node),
7899 "section attributes are not supported for this target");
7900 goto fail;
7901 }
9fb32434 7902
0373796b 7903 user_defined_section_attribute = true;
349ae713 7904
21b634ae 7905 if (!VAR_OR_FUNCTION_DECL_P (decl))
0373796b
JT
7906 {
7907 error ("section attribute not allowed for %q+D", *node);
7908 goto fail;
349ae713 7909 }
0373796b
JT
7910
7911 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
349ae713 7912 {
0373796b
JT
7913 error ("section attribute argument not a string constant");
7914 goto fail;
349ae713
NB
7915 }
7916
0373796b
JT
7917 if (TREE_CODE (decl) == VAR_DECL
7918 && current_function_decl != NULL_TREE
7919 && !TREE_STATIC (decl))
7920 {
7921 error_at (DECL_SOURCE_LOCATION (decl),
7922 "section attribute cannot be specified for local variables");
7923 goto fail;
7924 }
7925
7926 /* The decl may have already been given a section attribute
7927 from a previous declaration. Ensure they match. */
7928 if (DECL_SECTION_NAME (decl) != NULL
7929 && strcmp (DECL_SECTION_NAME (decl),
7930 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7931 {
7932 error ("section of %q+D conflicts with previous declaration", *node);
7933 goto fail;
7934 }
7935
7936 if (TREE_CODE (decl) == VAR_DECL
7937 && !targetm.have_tls && targetm.emutls.tmpl_section
7938 && DECL_THREAD_LOCAL_P (decl))
7939 {
7940 error ("section of %q+D cannot be overridden", *node);
7941 goto fail;
7942 }
7943
7944 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7945 return NULL_TREE;
7946
7947fail:
7948 *no_add_attrs = true;
349ae713
NB
7949 return NULL_TREE;
7950}
7951
d19fa6b5
JM
7952/* Check whether ALIGN is a valid user-specified alignment. If so,
7953 return its base-2 log; if not, output an error and return -1. If
7954 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7955 no error. */
7956int
7957check_user_alignment (const_tree align, bool allow_zero)
7958{
7959 int i;
7960
661a0813
MP
7961 if (error_operand_p (align))
7962 return -1;
a859517f
MP
7963 if (TREE_CODE (align) != INTEGER_CST
7964 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
d19fa6b5
JM
7965 {
7966 error ("requested alignment is not an integer constant");
7967 return -1;
7968 }
7969 else if (allow_zero && integer_zerop (align))
7970 return -1;
3f12f6e9
SKS
7971 else if (tree_int_cst_sgn (align) == -1
7972 || (i = tree_log2 (align)) == -1)
d19fa6b5 7973 {
3f12f6e9 7974 error ("requested alignment is not a positive power of 2");
d19fa6b5
JM
7975 return -1;
7976 }
7977 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7978 {
7979 error ("requested alignment is too large");
7980 return -1;
7981 }
7982 return i;
7983}
7984
e28d52cf
DS
7985/*
7986 If in c++-11, check if the c++-11 alignment constraint with respect
7987 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7988 c++-11 mode, does nothing.
7989
7990 [dcl.align]2/ says:
7991
7992 [* if the constant expression evaluates to a fundamental alignment,
7993 the alignment requirement of the declared entity shall be the
7994 specified fundamental alignment.
7995
7996 * if the constant expression evaluates to an extended alignment
7997 and the implementation supports that alignment in the context
7998 of the declaration, the alignment of the declared entity shall
7999 be that alignment
8000
8001 * if the constant expression evaluates to an extended alignment
8002 and the implementation does not support that alignment in the
8003 context of the declaration, the program is ill-formed]. */
8004
8005static bool
8006check_cxx_fundamental_alignment_constraints (tree node,
8007 unsigned align_log,
8008 int flags)
8009{
8010 bool alignment_too_large_p = false;
8011 unsigned requested_alignment = 1U << align_log;
8012 unsigned max_align = 0;
8013
8014 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8015 || (node == NULL_TREE || node == error_mark_node))
8016 return true;
8017
8018 if (cxx_fundamental_alignment_p (requested_alignment))
8019 return true;
8020
8021 if (DECL_P (node))
8022 {
8023 if (TREE_STATIC (node))
8024 {
8025 /* For file scope variables and static members, the target
8026 supports alignments that are at most
8027 MAX_OFILE_ALIGNMENT. */
8028 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8029 alignment_too_large_p = true;
8030 }
8031 else
8032 {
8033#ifdef BIGGEST_FIELD_ALIGNMENT
8034#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8035#else
8036#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8037#endif
8038 /* For non-static members, the target supports either
8039 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8040 if it is defined or BIGGEST_ALIGNMENT. */
8041 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8042 if (TREE_CODE (node) == FIELD_DECL
8043 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8044 alignment_too_large_p = true;
8045#undef MAX_TARGET_FIELD_ALIGNMENT
8046 /* For stack variables, the target supports at most
8047 MAX_STACK_ALIGNMENT. */
8048 else if (decl_function_context (node) != NULL
8049 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8050 alignment_too_large_p = true;
8051 }
8052 }
8053 else if (TYPE_P (node))
8054 {
8055 /* Let's be liberal for types. */
8056 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8057 alignment_too_large_p = true;
8058 }
8059
8060 if (alignment_too_large_p)
8061 pedwarn (input_location, OPT_Wattributes,
8062 "requested alignment %d is larger than %d",
8063 requested_alignment, max_align);
8064
8065 return !alignment_too_large_p;
8066}
8067
349ae713
NB
8068/* Handle a "aligned" attribute; arguments as in
8069 struct attribute_spec.handler. */
8070
8071static tree
e18476eb 8072handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
a742c759 8073 int flags, bool *no_add_attrs)
349ae713
NB
8074{
8075 tree decl = NULL_TREE;
8076 tree *type = NULL;
8077 int is_type = 0;
5d77fb19 8078 tree align_expr;
349ae713
NB
8079 int i;
8080
5d77fb19
MG
8081 if (args)
8082 {
8083 align_expr = TREE_VALUE (args);
661a0813
MP
8084 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8085 && TREE_CODE (align_expr) != FUNCTION_DECL)
5d77fb19
MG
8086 align_expr = default_conversion (align_expr);
8087 }
8088 else
8089 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8090
349ae713
NB
8091 if (DECL_P (*node))
8092 {
8093 decl = *node;
8094 type = &TREE_TYPE (decl);
8095 is_type = TREE_CODE (*node) == TYPE_DECL;
8096 }
8097 else if (TYPE_P (*node))
8098 type = node, is_type = 1;
8099
e28d52cf
DS
8100 if ((i = check_user_alignment (align_expr, false)) == -1
8101 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
d19fa6b5 8102 *no_add_attrs = true;
349ae713
NB
8103 else if (is_type)
8104 {
0f559c16
JM
8105 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8106 /* OK, modify the type in place. */;
349ae713
NB
8107 /* If we have a TYPE_DECL, then copy the type, so that we
8108 don't accidentally modify a builtin type. See pushdecl. */
0f559c16
JM
8109 else if (decl && TREE_TYPE (decl) != error_mark_node
8110 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
349ae713
NB
8111 {
8112 tree tt = TREE_TYPE (decl);
8dd16ecc 8113 *type = build_variant_type_copy (*type);
349ae713
NB
8114 DECL_ORIGINAL_TYPE (decl) = tt;
8115 TYPE_NAME (*type) = decl;
8116 TREE_USED (*type) = TREE_USED (decl);
8117 TREE_TYPE (decl) = *type;
8118 }
0f559c16 8119 else
8dd16ecc 8120 *type = build_variant_type_copy (*type);
349ae713 8121
d9223014 8122 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
349ae713
NB
8123 TYPE_USER_ALIGN (*type) = 1;
8124 }
837edd5f 8125 else if (! VAR_OR_FUNCTION_DECL_P (decl)
349ae713
NB
8126 && TREE_CODE (decl) != FIELD_DECL)
8127 {
dee15844 8128 error ("alignment may not be specified for %q+D", decl);
349ae713
NB
8129 *no_add_attrs = true;
8130 }
e28d52cf
DS
8131 else if (DECL_USER_ALIGN (decl)
8132 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8133 /* C++-11 [dcl.align/4]:
8134
8135 When multiple alignment-specifiers are specified for an
8136 entity, the alignment requirement shall be set to the
8137 strictest specified alignment.
8138
8139 This formally comes from the c++11 specification but we are
8140 doing it for the GNU attribute syntax as well. */
8141 *no_add_attrs = true;
837edd5f 8142 else if (TREE_CODE (decl) == FUNCTION_DECL
d9223014 8143 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
837edd5f
GK
8144 {
8145 if (DECL_USER_ALIGN (decl))
8146 error ("alignment for %q+D was previously specified as %d "
8147 "and may not be decreased", decl,
8148 DECL_ALIGN (decl) / BITS_PER_UNIT);
8149 else
8150 error ("alignment for %q+D must be at least %d", decl,
8151 DECL_ALIGN (decl) / BITS_PER_UNIT);
8152 *no_add_attrs = true;
8153 }
349ae713
NB
8154 else
8155 {
d9223014 8156 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
349ae713
NB
8157 DECL_USER_ALIGN (decl) = 1;
8158 }
8159
8160 return NULL_TREE;
8161}
8162
8163/* Handle a "weak" attribute; arguments as in
8164 struct attribute_spec.handler. */
8165
8166static tree
55af93a8 8167handle_weak_attribute (tree *node, tree name,
e18476eb
BI
8168 tree ARG_UNUSED (args),
8169 int ARG_UNUSED (flags),
8170 bool * ARG_UNUSED (no_add_attrs))
349ae713 8171{
55af93a8 8172 if (TREE_CODE (*node) == FUNCTION_DECL
6b4e94bc
RG
8173 && DECL_DECLARED_INLINE_P (*node))
8174 {
2aa9c6ae 8175 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6b4e94bc
RG
8176 *no_add_attrs = true;
8177 }
ba885ec5
NS
8178 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8179 {
8180 error ("indirect function %q+D cannot be declared weak", *node);
8181 *no_add_attrs = true;
8182 return NULL_TREE;
8183 }
21b634ae 8184 else if (VAR_OR_FUNCTION_DECL_P (*node))
3cb251b7
JH
8185 {
8186 struct symtab_node *n = symtab_node::get (*node);
8187 if (n && n->refuse_visibility_changes)
8188 error ("%+D declared weak after being used", *node);
8189 declare_weak (*node);
8190 }
55af93a8
DS
8191 else
8192 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8193
8194 return NULL_TREE;
8195}
8196
4bb794e2
ST
8197/* Handle a "noplt" attribute; arguments as in
8198 struct attribute_spec.handler. */
8199
8200static tree
8201handle_noplt_attribute (tree *node, tree name,
8202 tree ARG_UNUSED (args),
8203 int ARG_UNUSED (flags),
8204 bool * ARG_UNUSED (no_add_attrs))
8205{
8206 if (TREE_CODE (*node) != FUNCTION_DECL)
8207 {
8208 warning (OPT_Wattributes,
8209 "%qE attribute is only applicable on functions", name);
8210 *no_add_attrs = true;
8211 return NULL_TREE;
8212 }
8213 return NULL_TREE;
8214}
8215
ba885ec5
NS
8216/* Handle an "alias" or "ifunc" attribute; arguments as in
8217 struct attribute_spec.handler, except that IS_ALIAS tells us
8218 whether this is an alias as opposed to ifunc attribute. */
349ae713
NB
8219
8220static tree
ba885ec5
NS
8221handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8222 bool *no_add_attrs)
349ae713
NB
8223{
8224 tree decl = *node;
8225
ba885ec5
NS
8226 if (TREE_CODE (decl) != FUNCTION_DECL
8227 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
feab5a67
JM
8228 {
8229 warning (OPT_Wattributes, "%qE attribute ignored", name);
8230 *no_add_attrs = true;
8231 }
8232 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
b8698a0f 8233 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825
GK
8234 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8235 /* A static variable declaration is always a tentative definition,
8236 but the alias is a non-tentative definition which overrides. */
b8698a0f 8237 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825 8238 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
349ae713 8239 {
ba885ec5 8240 error ("%q+D defined both normally and as %qE attribute", decl, name);
349ae713 8241 *no_add_attrs = true;
ba885ec5 8242 return NULL_TREE;
349ae713 8243 }
ba885ec5
NS
8244 else if (!is_alias
8245 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8246 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8247 {
8248 error ("weak %q+D cannot be defined %qE", decl, name);
8249 *no_add_attrs = true;
8250 return NULL_TREE;
8251 }
f6a76b9f
RH
8252
8253 /* Note that the very first time we process a nested declaration,
8254 decl_function_context will not be set. Indeed, *would* never
8255 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8256 we do below. After such frobbery, pushdecl would set the context.
8257 In any case, this is never what we want. */
8258 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
349ae713
NB
8259 {
8260 tree id;
8261
8262 id = TREE_VALUE (args);
8263 if (TREE_CODE (id) != STRING_CST)
8264 {
ba885ec5 8265 error ("attribute %qE argument not a string", name);
349ae713
NB
8266 *no_add_attrs = true;
8267 return NULL_TREE;
8268 }
8269 id = get_identifier (TREE_STRING_POINTER (id));
8270 /* This counts as a use of the object pointed to. */
8271 TREE_USED (id) = 1;
8272
8273 if (TREE_CODE (decl) == FUNCTION_DECL)
8274 DECL_INITIAL (decl) = error_mark_node;
8275 else
08346abd 8276 TREE_STATIC (decl) = 1;
ba885ec5
NS
8277
8278 if (!is_alias)
8279 /* ifuncs are also aliases, so set that attribute too. */
8280 DECL_ATTRIBUTES (decl)
8281 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
349ae713
NB
8282 }
8283 else
8284 {
5c498b10 8285 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8286 *no_add_attrs = true;
8287 }
8288
f7217cde
JH
8289 if (decl_in_symtab_p (*node))
8290 {
8291 struct symtab_node *n = symtab_node::get (decl);
8292 if (n && n->refuse_visibility_changes)
8293 {
8294 if (is_alias)
8295 error ("%+D declared alias after being used", decl);
8296 else
8297 error ("%+D declared ifunc after being used", decl);
8298 }
8299 }
8300
8301
349ae713
NB
8302 return NULL_TREE;
8303}
8304
ba885ec5
NS
8305/* Handle an "alias" or "ifunc" attribute; arguments as in
8306 struct attribute_spec.handler. */
8307
8308static tree
8309handle_ifunc_attribute (tree *node, tree name, tree args,
8310 int ARG_UNUSED (flags), bool *no_add_attrs)
8311{
8312 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8313}
8314
8315/* Handle an "alias" or "ifunc" attribute; arguments as in
8316 struct attribute_spec.handler. */
8317
8318static tree
8319handle_alias_attribute (tree *node, tree name, tree args,
8320 int ARG_UNUSED (flags), bool *no_add_attrs)
8321{
8322 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8323}
8324
a0203ca7
AO
8325/* Handle a "weakref" attribute; arguments as in struct
8326 attribute_spec.handler. */
8327
8328static tree
8329handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8330 int flags, bool *no_add_attrs)
8331{
8332 tree attr = NULL_TREE;
8333
e1cf56b1
AO
8334 /* We must ignore the attribute when it is associated with
8335 local-scoped decls, since attribute alias is ignored and many
8336 such symbols do not even have a DECL_WEAK field. */
e7b012c0
JJ
8337 if (decl_function_context (*node)
8338 || current_function_decl
21b634ae 8339 || !VAR_OR_FUNCTION_DECL_P (*node))
e1cf56b1
AO
8340 {
8341 warning (OPT_Wattributes, "%qE attribute ignored", name);
8342 *no_add_attrs = true;
8343 return NULL_TREE;
8344 }
8345
ba885ec5
NS
8346 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8347 {
8348 error ("indirect function %q+D cannot be declared weakref", *node);
8349 *no_add_attrs = true;
8350 return NULL_TREE;
8351 }
8352
a0203ca7
AO
8353 /* The idea here is that `weakref("name")' mutates into `weakref,
8354 alias("name")', and weakref without arguments, in turn,
8355 implicitly adds weak. */
8356
8357 if (args)
8358 {
8359 attr = tree_cons (get_identifier ("alias"), args, attr);
8360 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8361
8362 *no_add_attrs = true;
a9b0b825
GK
8363
8364 decl_attributes (node, attr, flags);
a0203ca7
AO
8365 }
8366 else
8367 {
8368 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
c5d75364
MLI
8369 error_at (DECL_SOURCE_LOCATION (*node),
8370 "weakref attribute must appear before alias attribute");
a0203ca7 8371
a9b0b825
GK
8372 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8373 and that isn't supported; and because it wants to add it to
8374 the list of weak decls, which isn't helpful. */
8375 DECL_WEAK (*node) = 1;
a0203ca7
AO
8376 }
8377
f7217cde
JH
8378 if (decl_in_symtab_p (*node))
8379 {
8380 struct symtab_node *n = symtab_node::get (*node);
8381 if (n && n->refuse_visibility_changes)
8382 error ("%+D declared weakref after being used", *node);
8383 }
8384
a0203ca7
AO
8385 return NULL_TREE;
8386}
8387
349ae713
NB
8388/* Handle an "visibility" attribute; arguments as in
8389 struct attribute_spec.handler. */
8390
8391static tree
35b1a6fa 8392handle_visibility_attribute (tree *node, tree name, tree args,
e18476eb 8393 int ARG_UNUSED (flags),
b9e75696 8394 bool *ARG_UNUSED (no_add_attrs))
349ae713
NB
8395{
8396 tree decl = *node;
968b41a1 8397 tree id = TREE_VALUE (args);
b9e75696 8398 enum symbol_visibility vis;
349ae713 8399
d7afec4b
ND
8400 if (TYPE_P (*node))
8401 {
b9e75696
JM
8402 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8403 /* OK */;
8404 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8405 {
8406 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8407 name);
8408 return NULL_TREE;
8409 }
8410 else if (TYPE_FIELDS (*node))
8411 {
8412 error ("%qE attribute ignored because %qT is already defined",
8413 name, *node);
8414 return NULL_TREE;
8415 }
d7afec4b 8416 }
3f75a254 8417 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
349ae713 8418 {
5c498b10 8419 warning (OPT_Wattributes, "%qE attribute ignored", name);
968b41a1 8420 return NULL_TREE;
349ae713 8421 }
349ae713 8422
968b41a1
MA
8423 if (TREE_CODE (id) != STRING_CST)
8424 {
40b97a2e 8425 error ("visibility argument not a string");
968b41a1 8426 return NULL_TREE;
349ae713 8427 }
9f63daea 8428
d7afec4b
ND
8429 /* If this is a type, set the visibility on the type decl. */
8430 if (TYPE_P (decl))
8431 {
8432 decl = TYPE_NAME (decl);
3f75a254 8433 if (!decl)
c22cacf3 8434 return NULL_TREE;
e8233ac2
AP
8435 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8436 {
5c498b10 8437 warning (OPT_Wattributes, "%qE attribute ignored on types",
e8233ac2
AP
8438 name);
8439 return NULL_TREE;
8440 }
d7afec4b 8441 }
349ae713 8442
968b41a1 8443 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
b9e75696 8444 vis = VISIBILITY_DEFAULT;
968b41a1 8445 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
b9e75696 8446 vis = VISIBILITY_INTERNAL;
968b41a1 8447 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
b9e75696 8448 vis = VISIBILITY_HIDDEN;
968b41a1 8449 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
b9e75696 8450 vis = VISIBILITY_PROTECTED;
968b41a1 8451 else
b9e75696
JM
8452 {
8453 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8454 vis = VISIBILITY_DEFAULT;
8455 }
8456
8457 if (DECL_VISIBILITY_SPECIFIED (decl)
3a687f8b
MM
8458 && vis != DECL_VISIBILITY (decl))
8459 {
8460 tree attributes = (TYPE_P (*node)
8461 ? TYPE_ATTRIBUTES (*node)
8462 : DECL_ATTRIBUTES (decl));
8463 if (lookup_attribute ("visibility", attributes))
8464 error ("%qD redeclared with different visibility", decl);
8465 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8466 && lookup_attribute ("dllimport", attributes))
8467 error ("%qD was declared %qs which implies default visibility",
8468 decl, "dllimport");
8469 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8470 && lookup_attribute ("dllexport", attributes))
8471 error ("%qD was declared %qs which implies default visibility",
8472 decl, "dllexport");
8473 }
b9e75696
JM
8474
8475 DECL_VISIBILITY (decl) = vis;
d7afec4b
ND
8476 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8477
b9e75696
JM
8478 /* Go ahead and attach the attribute to the node as well. This is needed
8479 so we can determine whether we have VISIBILITY_DEFAULT because the
8480 visibility was not specified, or because it was explicitly overridden
8481 from the containing scope. */
968b41a1 8482
349ae713
NB
8483 return NULL_TREE;
8484}
8485
b2ca3702
MM
8486/* Determine the ELF symbol visibility for DECL, which is either a
8487 variable or a function. It is an error to use this function if a
8488 definition of DECL is not available in this translation unit.
8489 Returns true if the final visibility has been determined by this
8490 function; false if the caller is free to make additional
8491 modifications. */
8492
8493bool
8494c_determine_visibility (tree decl)
8495{
21b634ae 8496 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
b2ca3702
MM
8497
8498 /* If the user explicitly specified the visibility with an
8499 attribute, honor that. DECL_VISIBILITY will have been set during
6d87092d
JM
8500 the processing of the attribute. We check for an explicit
8501 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8502 to distinguish the use of an attribute from the use of a "#pragma
8503 GCC visibility push(...)"; in the latter case we still want other
8504 considerations to be able to overrule the #pragma. */
8505 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8506 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8507 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8508 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
b2ca3702
MM
8509 return true;
8510
b9e75696
JM
8511 /* Set default visibility to whatever the user supplied with
8512 visibility_specified depending on #pragma GCC visibility. */
8513 if (!DECL_VISIBILITY_SPECIFIED (decl))
8514 {
09812622
JJ
8515 if (visibility_options.inpragma
8516 || DECL_VISIBILITY (decl) != default_visibility)
8517 {
8518 DECL_VISIBILITY (decl) = default_visibility;
8519 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8520 /* If visibility changed and DECL already has DECL_RTL, ensure
8521 symbol flags are updated. */
8522 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8523 || TREE_CODE (decl) == FUNCTION_DECL)
8524 && DECL_RTL_SET_P (decl))
8525 make_decl_rtl (decl);
8526 }
b9e75696 8527 }
b2ca3702
MM
8528 return false;
8529}
8530
dce81a1a
JJ
8531/* Handle an "tls_model" attribute; arguments as in
8532 struct attribute_spec.handler. */
8533
8534static tree
35b1a6fa 8535handle_tls_model_attribute (tree *node, tree name, tree args,
e18476eb 8536 int ARG_UNUSED (flags), bool *no_add_attrs)
dce81a1a 8537{
c2f7fa15 8538 tree id;
dce81a1a 8539 tree decl = *node;
c2f7fa15 8540 enum tls_model kind;
dce81a1a 8541
c2f7fa15
SB
8542 *no_add_attrs = true;
8543
5006381c 8544 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
dce81a1a 8545 {
5c498b10 8546 warning (OPT_Wattributes, "%qE attribute ignored", name);
c2f7fa15 8547 return NULL_TREE;
dce81a1a 8548 }
dce81a1a 8549
c2f7fa15
SB
8550 kind = DECL_TLS_MODEL (decl);
8551 id = TREE_VALUE (args);
8552 if (TREE_CODE (id) != STRING_CST)
8553 {
8554 error ("tls_model argument not a string");
8555 return NULL_TREE;
dce81a1a
JJ
8556 }
8557
c2f7fa15
SB
8558 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8559 kind = TLS_MODEL_LOCAL_EXEC;
8560 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8561 kind = TLS_MODEL_INITIAL_EXEC;
8562 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8563 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8564 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8565 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8566 else
8567 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8568
56363ffd 8569 set_decl_tls_model (decl, kind);
dce81a1a
JJ
8570 return NULL_TREE;
8571}
8572
349ae713
NB
8573/* Handle a "no_instrument_function" attribute; arguments as in
8574 struct attribute_spec.handler. */
8575
8576static tree
35b1a6fa 8577handle_no_instrument_function_attribute (tree *node, tree name,
e18476eb
BI
8578 tree ARG_UNUSED (args),
8579 int ARG_UNUSED (flags),
a742c759 8580 bool *no_add_attrs)
349ae713
NB
8581{
8582 tree decl = *node;
8583
8584 if (TREE_CODE (decl) != FUNCTION_DECL)
8585 {
c5d75364
MLI
8586 error_at (DECL_SOURCE_LOCATION (decl),
8587 "%qE attribute applies only to functions", name);
349ae713
NB
8588 *no_add_attrs = true;
8589 }
349ae713
NB
8590 else
8591 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8592
8593 return NULL_TREE;
8594}
8595
8596/* Handle a "malloc" attribute; arguments as in
8597 struct attribute_spec.handler. */
8598
8599static tree
e18476eb
BI
8600handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8601 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 8602{
3425638a
JM
8603 if (TREE_CODE (*node) == FUNCTION_DECL
8604 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
349ae713 8605 DECL_IS_MALLOC (*node) = 1;
349ae713
NB
8606 else
8607 {
5c498b10 8608 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8609 *no_add_attrs = true;
8610 }
8611
8612 return NULL_TREE;
8613}
8614
51bc54a6
DM
8615/* Handle a "alloc_size" attribute; arguments as in
8616 struct attribute_spec.handler. */
8617
8618static tree
8619handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8620 int ARG_UNUSED (flags), bool *no_add_attrs)
8621{
f3f75f69 8622 unsigned arg_count = type_num_arguments (*node);
51bc54a6
DM
8623 for (; args; args = TREE_CHAIN (args))
8624 {
8625 tree position = TREE_VALUE (args);
5d77fb19
MG
8626 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8627 && TREE_CODE (position) != FUNCTION_DECL)
8628 position = default_conversion (position);
51bc54a6 8629
8fcbce72
JJ
8630 if (!tree_fits_uhwi_p (position)
8631 || !arg_count
8632 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
51bc54a6 8633 {
b8698a0f 8634 warning (OPT_Wattributes,
51bc54a6
DM
8635 "alloc_size parameter outside range");
8636 *no_add_attrs = true;
8637 return NULL_TREE;
8638 }
8639 }
8640 return NULL_TREE;
8641}
8642
8fcbce72
JJ
8643/* Handle a "alloc_align" attribute; arguments as in
8644 struct attribute_spec.handler. */
8645
8646static tree
8647handle_alloc_align_attribute (tree *node, tree, tree args, int,
8648 bool *no_add_attrs)
8649{
8650 unsigned arg_count = type_num_arguments (*node);
8651 tree position = TREE_VALUE (args);
8652 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8653 position = default_conversion (position);
8654
8655 if (!tree_fits_uhwi_p (position)
8656 || !arg_count
8657 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8658 {
8659 warning (OPT_Wattributes,
8660 "alloc_align parameter outside range");
8661 *no_add_attrs = true;
8662 return NULL_TREE;
8663 }
8664 return NULL_TREE;
8665}
8666
8667/* Handle a "assume_aligned" attribute; arguments as in
8668 struct attribute_spec.handler. */
8669
8670static tree
8671handle_assume_aligned_attribute (tree *, tree, tree args, int,
8672 bool *no_add_attrs)
8673{
8674 for (; args; args = TREE_CHAIN (args))
8675 {
8676 tree position = TREE_VALUE (args);
8677 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8678 && TREE_CODE (position) != FUNCTION_DECL)
8679 position = default_conversion (position);
8680
8681 if (TREE_CODE (position) != INTEGER_CST)
8682 {
8683 warning (OPT_Wattributes,
8684 "assume_aligned parameter not integer constant");
8685 *no_add_attrs = true;
8686 return NULL_TREE;
8687 }
8688 }
8689 return NULL_TREE;
8690}
8691
0b7b376d
RG
8692/* Handle a "fn spec" attribute; arguments as in
8693 struct attribute_spec.handler. */
8694
8695static tree
8696handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8697 tree args, int ARG_UNUSED (flags),
8698 bool *no_add_attrs ATTRIBUTE_UNUSED)
8699{
8700 gcc_assert (args
8701 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8702 && !TREE_CHAIN (args));
8703 return NULL_TREE;
8704}
8705
d5e254e1
IE
8706/* Handle a "bnd_variable_size" attribute; arguments as in
8707 struct attribute_spec.handler. */
8708
8709static tree
8710handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8711 int ARG_UNUSED (flags), bool *no_add_attrs)
8712{
8713 if (TREE_CODE (*node) != FIELD_DECL)
8714 {
8715 warning (OPT_Wattributes, "%qE attribute ignored", name);
8716 *no_add_attrs = true;
8717 }
8718
8719 return NULL_TREE;
8720}
8721
8722/* Handle a "bnd_legacy" attribute; arguments as in
8723 struct attribute_spec.handler. */
8724
8725static tree
8726handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8727 int ARG_UNUSED (flags), bool *no_add_attrs)
8728{
8729 if (TREE_CODE (*node) != FUNCTION_DECL)
8730 {
8731 warning (OPT_Wattributes, "%qE attribute ignored", name);
8732 *no_add_attrs = true;
8733 }
8734
8735 return NULL_TREE;
8736}
8737
8738/* Handle a "bnd_instrument" attribute; arguments as in
8739 struct attribute_spec.handler. */
8740
8741static tree
8742handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8743 int ARG_UNUSED (flags), bool *no_add_attrs)
8744{
8745 if (TREE_CODE (*node) != FUNCTION_DECL)
8746 {
8747 warning (OPT_Wattributes, "%qE attribute ignored", name);
8748 *no_add_attrs = true;
8749 }
8750
8751 return NULL_TREE;
8752}
8753
2a99e5e6
LL
8754/* Handle a "warn_unused" attribute; arguments as in
8755 struct attribute_spec.handler. */
8756
8757static tree
8758handle_warn_unused_attribute (tree *node, tree name,
8759 tree args ATTRIBUTE_UNUSED,
8760 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8761{
8762 if (TYPE_P (*node))
8763 /* Do nothing else, just set the attribute. We'll get at
8764 it later with lookup_attribute. */
8765 ;
8766 else
8767 {
8768 warning (OPT_Wattributes, "%qE attribute ignored", name);
8769 *no_add_attrs = true;
8770 }
8771
8772 return NULL_TREE;
8773}
8774
acf0174b
JJ
8775/* Handle an "omp declare simd" attribute; arguments as in
8776 struct attribute_spec.handler. */
8777
8778static tree
8779handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8780{
8781 return NULL_TREE;
8782}
8783
8784/* Handle an "omp declare target" attribute; arguments as in
8785 struct attribute_spec.handler. */
8786
8787static tree
8788handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8789{
8790 return NULL_TREE;
8791}
8792
6e9a3221
AN
8793/* Handle a "returns_twice" attribute; arguments as in
8794 struct attribute_spec.handler. */
8795
8796static tree
8797handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8798 int ARG_UNUSED (flags), bool *no_add_attrs)
8799{
8800 if (TREE_CODE (*node) == FUNCTION_DECL)
8801 DECL_IS_RETURNS_TWICE (*node) = 1;
8802 else
8803 {
5c498b10 8804 warning (OPT_Wattributes, "%qE attribute ignored", name);
6e9a3221
AN
8805 *no_add_attrs = true;
8806 }
8807
8808 return NULL_TREE;
8809}
8810
349ae713
NB
8811/* Handle a "no_limit_stack" attribute; arguments as in
8812 struct attribute_spec.handler. */
8813
8814static tree
35b1a6fa 8815handle_no_limit_stack_attribute (tree *node, tree name,
e18476eb
BI
8816 tree ARG_UNUSED (args),
8817 int ARG_UNUSED (flags),
a742c759 8818 bool *no_add_attrs)
349ae713
NB
8819{
8820 tree decl = *node;
8821
8822 if (TREE_CODE (decl) != FUNCTION_DECL)
8823 {
c5d75364
MLI
8824 error_at (DECL_SOURCE_LOCATION (decl),
8825 "%qE attribute applies only to functions", name);
349ae713
NB
8826 *no_add_attrs = true;
8827 }
8828 else if (DECL_INITIAL (decl))
8829 {
c5d75364
MLI
8830 error_at (DECL_SOURCE_LOCATION (decl),
8831 "can%'t set %qE attribute after definition", name);
349ae713
NB
8832 *no_add_attrs = true;
8833 }
8834 else
8835 DECL_NO_LIMIT_STACK (decl) = 1;
8836
8837 return NULL_TREE;
8838}
8839
8840/* Handle a "pure" attribute; arguments as in
8841 struct attribute_spec.handler. */
8842
8843static tree
e18476eb
BI
8844handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8845 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
8846{
8847 if (TREE_CODE (*node) == FUNCTION_DECL)
becfd6e5 8848 DECL_PURE_P (*node) = 1;
349ae713
NB
8849 /* ??? TODO: Support types. */
8850 else
8851 {
5c498b10 8852 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8853 *no_add_attrs = true;
8854 }
8855
8856 return NULL_TREE;
8857}
8858
0a35513e
AH
8859/* Digest an attribute list destined for a transactional memory statement.
8860 ALLOWED is the set of attributes that are allowed for this statement;
8861 return the attribute we parsed. Multiple attributes are never allowed. */
8862
8863int
8864parse_tm_stmt_attr (tree attrs, int allowed)
8865{
8866 tree a_seen = NULL;
8867 int m_seen = 0;
8868
8869 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8870 {
8871 tree a = TREE_PURPOSE (attrs);
8872 int m = 0;
8873
8874 if (is_attribute_p ("outer", a))
8875 m = TM_STMT_ATTR_OUTER;
8876
8877 if ((m & allowed) == 0)
8878 {
8879 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8880 continue;
8881 }
8882
8883 if (m_seen == 0)
8884 {
8885 a_seen = a;
8886 m_seen = m;
8887 }
8888 else if (m_seen == m)
8889 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8890 else
8891 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8892 }
8893
8894 return m_seen;
8895}
8896
8897/* Transform a TM attribute name into a maskable integer and back.
8898 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8899 to how the lack of an attribute is treated. */
8900
8901int
8902tm_attr_to_mask (tree attr)
8903{
8904 if (attr == NULL)
8905 return 0;
8906 if (is_attribute_p ("transaction_safe", attr))
8907 return TM_ATTR_SAFE;
8908 if (is_attribute_p ("transaction_callable", attr))
8909 return TM_ATTR_CALLABLE;
8910 if (is_attribute_p ("transaction_pure", attr))
8911 return TM_ATTR_PURE;
8912 if (is_attribute_p ("transaction_unsafe", attr))
8913 return TM_ATTR_IRREVOCABLE;
8914 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8915 return TM_ATTR_MAY_CANCEL_OUTER;
8916 return 0;
8917}
8918
8919tree
8920tm_mask_to_attr (int mask)
8921{
8922 const char *str;
8923 switch (mask)
8924 {
8925 case TM_ATTR_SAFE:
8926 str = "transaction_safe";
8927 break;
8928 case TM_ATTR_CALLABLE:
8929 str = "transaction_callable";
8930 break;
8931 case TM_ATTR_PURE:
8932 str = "transaction_pure";
8933 break;
8934 case TM_ATTR_IRREVOCABLE:
8935 str = "transaction_unsafe";
8936 break;
8937 case TM_ATTR_MAY_CANCEL_OUTER:
8938 str = "transaction_may_cancel_outer";
8939 break;
8940 default:
8941 gcc_unreachable ();
8942 }
8943 return get_identifier (str);
8944}
8945
8946/* Return the first TM attribute seen in LIST. */
8947
8948tree
8949find_tm_attribute (tree list)
8950{
8951 for (; list ; list = TREE_CHAIN (list))
8952 {
8953 tree name = TREE_PURPOSE (list);
8954 if (tm_attr_to_mask (name) != 0)
8955 return name;
8956 }
8957 return NULL_TREE;
8958}
8959
8960/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8961 Here we accept only function types, and verify that none of the other
8962 function TM attributes are also applied. */
8963/* ??? We need to accept class types for C++, but not C. This greatly
8964 complicates this function, since we can no longer rely on the extra
8965 processing given by function_type_required. */
8966
8967static tree
8968handle_tm_attribute (tree *node, tree name, tree args,
8969 int flags, bool *no_add_attrs)
8970{
8971 /* Only one path adds the attribute; others don't. */
8972 *no_add_attrs = true;
8973
8974 switch (TREE_CODE (*node))
8975 {
8976 case RECORD_TYPE:
8977 case UNION_TYPE:
8978 /* Only tm_callable and tm_safe apply to classes. */
8979 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8980 goto ignored;
8981 /* FALLTHRU */
8982
8983 case FUNCTION_TYPE:
8984 case METHOD_TYPE:
8985 {
8986 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8987 if (old_name == name)
8988 ;
8989 else if (old_name != NULL_TREE)
8990 error ("type was previously declared %qE", old_name);
8991 else
8992 *no_add_attrs = false;
8993 }
8994 break;
8995
8996 case POINTER_TYPE:
8997 {
8998 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8999 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
9000 {
9001 tree fn_tmp = TREE_TYPE (*node);
9002 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
9003 *node = build_pointer_type (fn_tmp);
9004 break;
9005 }
9006 }
9007 /* FALLTHRU */
9008
9009 default:
9010 /* If a function is next, pass it on to be tried next. */
9011 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
9012 return tree_cons (name, args, NULL);
9013
9014 ignored:
9015 warning (OPT_Wattributes, "%qE attribute ignored", name);
9016 break;
9017 }
9018
9019 return NULL_TREE;
9020}
9021
9022/* Handle the TM_WRAP attribute; arguments as in
9023 struct attribute_spec.handler. */
9024
9025static tree
9026handle_tm_wrap_attribute (tree *node, tree name, tree args,
9027 int ARG_UNUSED (flags), bool *no_add_attrs)
9028{
9029 tree decl = *node;
9030
9031 /* We don't need the attribute even on success, since we
9032 record the entry in an external table. */
9033 *no_add_attrs = true;
9034
9035 if (TREE_CODE (decl) != FUNCTION_DECL)
9036 warning (OPT_Wattributes, "%qE attribute ignored", name);
9037 else
9038 {
9039 tree wrap_decl = TREE_VALUE (args);
661a0813
MP
9040 if (error_operand_p (wrap_decl))
9041 ;
9042 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
21b634ae 9043 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
0a35513e
AH
9044 error ("%qE argument not an identifier", name);
9045 else
9046 {
9047 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9048 wrap_decl = lookup_name (wrap_decl);
9049 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9050 {
9051 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9052 TREE_TYPE (wrap_decl)))
9053 record_tm_replacement (wrap_decl, decl);
9054 else
9055 error ("%qD is not compatible with %qD", wrap_decl, decl);
9056 }
9057 else
c07d7c02 9058 error ("%qE argument is not a function", name);
0a35513e
AH
9059 }
9060 }
9061
9062 return NULL_TREE;
9063}
9064
9065/* Ignore the given attribute. Used when this attribute may be usefully
9066 overridden by the target, but is not used generically. */
9067
9068static tree
9069ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9070 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9071 bool *no_add_attrs)
9072{
9073 *no_add_attrs = true;
9074 return NULL_TREE;
9075}
9076
dcd6de6d
ZD
9077/* Handle a "no vops" attribute; arguments as in
9078 struct attribute_spec.handler. */
9079
9080static tree
9081handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9082 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9083 bool *ARG_UNUSED (no_add_attrs))
9084{
9085 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9086 DECL_IS_NOVOPS (*node) = 1;
9087 return NULL_TREE;
9088}
9089
349ae713
NB
9090/* Handle a "deprecated" attribute; arguments as in
9091 struct attribute_spec.handler. */
35b1a6fa 9092
349ae713 9093static tree
35b1a6fa 9094handle_deprecated_attribute (tree *node, tree name,
9b86d6bb 9095 tree args, int flags,
a742c759 9096 bool *no_add_attrs)
349ae713
NB
9097{
9098 tree type = NULL_TREE;
9099 int warn = 0;
c51a1ba9 9100 tree what = NULL_TREE;
35b1a6fa 9101
9b86d6bb
L
9102 if (!args)
9103 *no_add_attrs = true;
9104 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9105 {
9106 error ("deprecated message is not a string");
9107 *no_add_attrs = true;
9108 }
9109
349ae713
NB
9110 if (DECL_P (*node))
9111 {
9112 tree decl = *node;
9113 type = TREE_TYPE (decl);
35b1a6fa 9114
349ae713
NB
9115 if (TREE_CODE (decl) == TYPE_DECL
9116 || TREE_CODE (decl) == PARM_DECL
21b634ae 9117 || VAR_OR_FUNCTION_DECL_P (decl)
a1178b30 9118 || TREE_CODE (decl) == FIELD_DECL
fd5c817a 9119 || TREE_CODE (decl) == CONST_DECL
a1178b30 9120 || objc_method_decl (TREE_CODE (decl)))
349ae713
NB
9121 TREE_DEPRECATED (decl) = 1;
9122 else
9123 warn = 1;
9124 }
9125 else if (TYPE_P (*node))
9126 {
9127 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 9128 *node = build_variant_type_copy (*node);
349ae713
NB
9129 TREE_DEPRECATED (*node) = 1;
9130 type = *node;
9131 }
9132 else
9133 warn = 1;
35b1a6fa 9134
349ae713
NB
9135 if (warn)
9136 {
9137 *no_add_attrs = true;
9138 if (type && TYPE_NAME (type))
9139 {
9140 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9 9141 what = TYPE_NAME (*node);
349ae713
NB
9142 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9143 && DECL_NAME (TYPE_NAME (type)))
c51a1ba9 9144 what = DECL_NAME (TYPE_NAME (type));
349ae713
NB
9145 }
9146 if (what)
5c498b10 9147 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
349ae713 9148 else
5c498b10 9149 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
9150 }
9151
9152 return NULL_TREE;
9153}
9154
349ae713
NB
9155/* Handle a "vector_size" attribute; arguments as in
9156 struct attribute_spec.handler. */
9157
9158static tree
35b1a6fa 9159handle_vector_size_attribute (tree *node, tree name, tree args,
e18476eb 9160 int ARG_UNUSED (flags),
a742c759 9161 bool *no_add_attrs)
349ae713
NB
9162{
9163 unsigned HOST_WIDE_INT vecsize, nunits;
ef4bddc2 9164 machine_mode orig_mode;
4a5eab38 9165 tree type = *node, new_type, size;
349ae713
NB
9166
9167 *no_add_attrs = true;
9168
4a5eab38 9169 size = TREE_VALUE (args);
661a0813
MP
9170 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9171 && TREE_CODE (size) != FUNCTION_DECL)
5d77fb19 9172 size = default_conversion (size);
4a5eab38 9173
cc269bb6 9174 if (!tree_fits_uhwi_p (size))
349ae713 9175 {
5c498b10 9176 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
9177 return NULL_TREE;
9178 }
9179
9180 /* Get the vector size (in bytes). */
ae7e9ddd 9181 vecsize = tree_to_uhwi (size);
349ae713
NB
9182
9183 /* We need to provide for vector pointers, vector arrays, and
9184 functions returning vectors. For example:
9185
9186 __attribute__((vector_size(16))) short *foo;
9187
9188 In this case, the mode is SI, but the type being modified is
9189 HI, so we need to look further. */
9190
9191 while (POINTER_TYPE_P (type)
9192 || TREE_CODE (type) == FUNCTION_TYPE
43dc123f 9193 || TREE_CODE (type) == METHOD_TYPE
270e749d
JJ
9194 || TREE_CODE (type) == ARRAY_TYPE
9195 || TREE_CODE (type) == OFFSET_TYPE)
349ae713
NB
9196 type = TREE_TYPE (type);
9197
9198 /* Get the mode of the type being modified. */
9199 orig_mode = TYPE_MODE (type);
9200
270e749d
JJ
9201 if ((!INTEGRAL_TYPE_P (type)
9202 && !SCALAR_FLOAT_TYPE_P (type)
9203 && !FIXED_POINT_TYPE_P (type))
3d8bf70f 9204 || (!SCALAR_FLOAT_MODE_P (orig_mode)
ab22c1fa
CF
9205 && GET_MODE_CLASS (orig_mode) != MODE_INT
9206 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
cc269bb6 9207 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
e4e5261f 9208 || TREE_CODE (type) == BOOLEAN_TYPE)
349ae713 9209 {
c51a1ba9 9210 error ("invalid vector type for attribute %qE", name);
349ae713
NB
9211 return NULL_TREE;
9212 }
9213
ae7e9ddd 9214 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
ee8960e5
JJ
9215 {
9216 error ("vector size not an integral multiple of component size");
9217 return NULL;
9218 }
9219
9220 if (vecsize == 0)
9221 {
9222 error ("zero vector size");
9223 return NULL;
9224 }
9225
349ae713 9226 /* Calculate how many units fit in the vector. */
ae7e9ddd 9227 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
26277d41 9228 if (nunits & (nunits - 1))
349ae713 9229 {
26277d41 9230 error ("number of components of the vector not a power of two");
349ae713
NB
9231 return NULL_TREE;
9232 }
9233
26277d41 9234 new_type = build_vector_type (type, nunits);
349ae713
NB
9235
9236 /* Build back pointers if needed. */
5dc11954 9237 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
349ae713
NB
9238
9239 return NULL_TREE;
9240}
9241
b34c7881
JT
9242/* Handle the "nonnull" attribute. */
9243static tree
e18476eb
BI
9244handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9245 tree args, int ARG_UNUSED (flags),
a742c759 9246 bool *no_add_attrs)
b34c7881
JT
9247{
9248 tree type = *node;
9249 unsigned HOST_WIDE_INT attr_arg_num;
9250
9251 /* If no arguments are specified, all pointer arguments should be
95bd1dd7 9252 non-null. Verify a full prototype is given so that the arguments
b34c7881 9253 will have the correct types when we actually check them later. */
3f75a254 9254 if (!args)
b34c7881 9255 {
f4da8dce 9256 if (!prototype_p (type))
b34c7881
JT
9257 {
9258 error ("nonnull attribute without arguments on a non-prototype");
6de9cd9a 9259 *no_add_attrs = true;
b34c7881
JT
9260 }
9261 return NULL_TREE;
9262 }
9263
9264 /* Argument list specified. Verify that each argument number references
9265 a pointer argument. */
5d77fb19 9266 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
b34c7881 9267 {
6de9cd9a 9268 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
b34c7881 9269
5d77fb19
MG
9270 tree arg = TREE_VALUE (args);
9271 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9272 && TREE_CODE (arg) != FUNCTION_DECL)
9273 arg = default_conversion (arg);
9274
9275 if (!get_nonnull_operand (arg, &arg_num))
b34c7881 9276 {
40b97a2e 9277 error ("nonnull argument has invalid operand number (argument %lu)",
b34c7881
JT
9278 (unsigned long) attr_arg_num);
9279 *no_add_attrs = true;
9280 return NULL_TREE;
9281 }
9282
e19a18d4 9283 if (prototype_p (type))
b34c7881 9284 {
e19a18d4
NF
9285 function_args_iterator iter;
9286 tree argument;
9287
9288 function_args_iter_init (&iter, type);
9289 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
b34c7881 9290 {
e19a18d4
NF
9291 argument = function_args_iter_cond (&iter);
9292 if (argument == NULL_TREE || ck_num == arg_num)
b34c7881 9293 break;
b34c7881
JT
9294 }
9295
3f75a254 9296 if (!argument
e19a18d4 9297 || TREE_CODE (argument) == VOID_TYPE)
b34c7881 9298 {
40b97a2e 9299 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
b34c7881
JT
9300 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9301 *no_add_attrs = true;
9302 return NULL_TREE;
9303 }
9304
e19a18d4 9305 if (TREE_CODE (argument) != POINTER_TYPE)
b34c7881 9306 {
40b97a2e 9307 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
b34c7881
JT
9308 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9309 *no_add_attrs = true;
9310 return NULL_TREE;
9311 }
9312 }
9313 }
9314
9315 return NULL_TREE;
9316}
9317
9318/* Check the argument list of a function call for null in argument slots
94a0dd7b
SL
9319 that are marked as requiring a non-null pointer argument. The NARGS
9320 arguments are passed in the array ARGARRAY.
9321*/
b34c7881
JT
9322
9323static void
94a0dd7b 9324check_function_nonnull (tree attrs, int nargs, tree *argarray)
b34c7881 9325{
332f1d24 9326 tree a;
94a0dd7b 9327 int i;
b34c7881 9328
332f1d24
JJ
9329 attrs = lookup_attribute ("nonnull", attrs);
9330 if (attrs == NULL_TREE)
9331 return;
9332
9333 a = attrs;
9334 /* See if any of the nonnull attributes has no arguments. If so,
9335 then every pointer argument is checked (in which case the check
9336 for pointer type is done in check_nonnull_arg). */
9337 if (TREE_VALUE (a) != NULL_TREE)
9338 do
9339 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9340 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9341
9342 if (a != NULL_TREE)
9343 for (i = 0; i < nargs; i++)
9344 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9345 i + 1);
9346 else
b34c7881 9347 {
332f1d24
JJ
9348 /* Walk the argument list. If we encounter an argument number we
9349 should check for non-null, do it. */
9350 for (i = 0; i < nargs; i++)
b34c7881 9351 {
332f1d24 9352 for (a = attrs; ; a = TREE_CHAIN (a))
6de9cd9a 9353 {
332f1d24
JJ
9354 a = lookup_attribute ("nonnull", a);
9355 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9356 break;
6de9cd9a 9357 }
332f1d24
JJ
9358
9359 if (a != NULL_TREE)
9360 check_function_arguments_recurse (check_nonnull_arg, NULL,
9361 argarray[i], i + 1);
b34c7881
JT
9362 }
9363 }
9364}
9365
254986c7 9366/* Check that the Nth argument of a function call (counting backwards
94a0dd7b
SL
9367 from the end) is a (pointer)0. The NARGS arguments are passed in the
9368 array ARGARRAY. */
3d091dac
KG
9369
9370static void
dde05067 9371check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
3d091dac 9372{
dde05067 9373 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
3d091dac
KG
9374
9375 if (attr)
9376 {
94a0dd7b
SL
9377 int len = 0;
9378 int pos = 0;
9379 tree sentinel;
dde05067
NF
9380 function_args_iterator iter;
9381 tree t;
c22cacf3 9382
94a0dd7b 9383 /* Skip over the named arguments. */
dde05067 9384 FOREACH_FUNCTION_ARGS (fntype, t, iter)
c22cacf3 9385 {
dde05067
NF
9386 if (len == nargs)
9387 break;
94a0dd7b
SL
9388 len++;
9389 }
254986c7 9390
94a0dd7b
SL
9391 if (TREE_VALUE (attr))
9392 {
9393 tree p = TREE_VALUE (TREE_VALUE (attr));
9394 pos = TREE_INT_CST_LOW (p);
9395 }
254986c7 9396
94a0dd7b
SL
9397 /* The sentinel must be one of the varargs, i.e.
9398 in position >= the number of fixed arguments. */
9399 if ((nargs - 1 - pos) < len)
9400 {
7332899a 9401 warning (OPT_Wformat_,
94a0dd7b
SL
9402 "not enough variable arguments to fit a sentinel");
9403 return;
3d091dac 9404 }
94a0dd7b
SL
9405
9406 /* Validate the sentinel. */
9407 sentinel = argarray[nargs - 1 - pos];
9408 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9409 || !integer_zerop (sentinel))
9410 /* Although __null (in C++) is only an integer we allow it
9411 nevertheless, as we are guaranteed that it's exactly
9412 as wide as a pointer, and we don't want to force
9413 users to cast the NULL they have written there.
9414 We warn with -Wstrict-null-sentinel, though. */
9415 && (warn_strict_null_sentinel || null_node != sentinel))
7332899a 9416 warning (OPT_Wformat_, "missing sentinel in function call");
3d091dac
KG
9417 }
9418}
9419
b34c7881
JT
9420/* Helper for check_function_nonnull; given a list of operands which
9421 must be non-null in ARGS, determine if operand PARAM_NUM should be
9422 checked. */
9423
9424static bool
35b1a6fa 9425nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
b34c7881 9426{
6de9cd9a 9427 unsigned HOST_WIDE_INT arg_num = 0;
b34c7881
JT
9428
9429 for (; args; args = TREE_CHAIN (args))
9430 {
366de0ce
NS
9431 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9432
9433 gcc_assert (found);
b34c7881
JT
9434
9435 if (arg_num == param_num)
9436 return true;
9437 }
9438 return false;
9439}
9440
9441/* Check that the function argument PARAM (which is operand number
9442 PARAM_NUM) is non-null. This is called by check_function_nonnull
9443 via check_function_arguments_recurse. */
9444
9445static void
e18476eb 9446check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
35b1a6fa 9447 unsigned HOST_WIDE_INT param_num)
b34c7881
JT
9448{
9449 /* Just skip checking the argument if it's not a pointer. This can
9450 happen if the "nonnull" attribute was given without an operand
9451 list (which means to check every pointer argument). */
9452
9453 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9454 return;
9455
9456 if (integer_zerop (param))
30480ec4
DD
9457 warning (OPT_Wnonnull, "null argument where non-null required "
9458 "(argument %lu)", (unsigned long) param_num);
b34c7881
JT
9459}
9460
9461/* Helper for nonnull attribute handling; fetch the operand number
9462 from the attribute argument list. */
9463
9464static bool
35b1a6fa 9465get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
b34c7881 9466{
807e902e
KZ
9467 /* Verify the arg number is a small constant. */
9468 if (tree_fits_uhwi_p (arg_num_expr))
9469 {
9470 *valp = TREE_INT_CST_LOW (arg_num_expr);
9471 return true;
9472 }
9473 else
b34c7881 9474 return false;
b34c7881 9475}
39f2f3c8
RS
9476
9477/* Handle a "nothrow" attribute; arguments as in
9478 struct attribute_spec.handler. */
9479
9480static tree
e18476eb
BI
9481handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9482 int ARG_UNUSED (flags), bool *no_add_attrs)
39f2f3c8
RS
9483{
9484 if (TREE_CODE (*node) == FUNCTION_DECL)
9485 TREE_NOTHROW (*node) = 1;
9486 /* ??? TODO: Support types. */
9487 else
9488 {
5c498b10 9489 warning (OPT_Wattributes, "%qE attribute ignored", name);
39f2f3c8
RS
9490 *no_add_attrs = true;
9491 }
9492
9493 return NULL_TREE;
9494}
0bfa5f65
RH
9495
9496/* Handle a "cleanup" attribute; arguments as in
9497 struct attribute_spec.handler. */
9498
9499static tree
35b1a6fa 9500handle_cleanup_attribute (tree *node, tree name, tree args,
e18476eb 9501 int ARG_UNUSED (flags), bool *no_add_attrs)
0bfa5f65
RH
9502{
9503 tree decl = *node;
9504 tree cleanup_id, cleanup_decl;
9505
9506 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9507 for global destructors in C++. This requires infrastructure that
9508 we don't have generically at the moment. It's also not a feature
9509 we'd be missing too much, since we do have attribute constructor. */
9510 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9511 {
5c498b10 9512 warning (OPT_Wattributes, "%qE attribute ignored", name);
0bfa5f65
RH
9513 *no_add_attrs = true;
9514 return NULL_TREE;
9515 }
9516
9517 /* Verify that the argument is a function in scope. */
9518 /* ??? We could support pointers to functions here as well, if
9519 that was considered desirable. */
9520 cleanup_id = TREE_VALUE (args);
9521 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9522 {
40b97a2e 9523 error ("cleanup argument not an identifier");
0bfa5f65
RH
9524 *no_add_attrs = true;
9525 return NULL_TREE;
9526 }
10e6657a 9527 cleanup_decl = lookup_name (cleanup_id);
0bfa5f65
RH
9528 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9529 {
40b97a2e 9530 error ("cleanup argument not a function");
0bfa5f65
RH
9531 *no_add_attrs = true;
9532 return NULL_TREE;
9533 }
9534
35b1a6fa 9535 /* That the function has proper type is checked with the
0bfa5f65
RH
9536 eventual call to build_function_call. */
9537
9538 return NULL_TREE;
9539}
72954a4f
JM
9540
9541/* Handle a "warn_unused_result" attribute. No special handling. */
9542
9543static tree
9544handle_warn_unused_result_attribute (tree *node, tree name,
e18476eb
BI
9545 tree ARG_UNUSED (args),
9546 int ARG_UNUSED (flags), bool *no_add_attrs)
72954a4f
JM
9547{
9548 /* Ignore the attribute for functions not returning any value. */
9549 if (VOID_TYPE_P (TREE_TYPE (*node)))
9550 {
5c498b10 9551 warning (OPT_Wattributes, "%qE attribute ignored", name);
72954a4f
JM
9552 *no_add_attrs = true;
9553 }
9554
9555 return NULL_TREE;
9556}
3d091dac
KG
9557
9558/* Handle a "sentinel" attribute. */
9559
9560static tree
254986c7 9561handle_sentinel_attribute (tree *node, tree name, tree args,
3d091dac
KG
9562 int ARG_UNUSED (flags), bool *no_add_attrs)
9563{
f4da8dce 9564 if (!prototype_p (*node))
3d091dac 9565 {
5c498b10
DD
9566 warning (OPT_Wattributes,
9567 "%qE attribute requires prototypes with named arguments", name);
3d091dac 9568 *no_add_attrs = true;
3d091dac 9569 }
254986c7
KG
9570 else
9571 {
dcf0c47e 9572 if (!stdarg_p (*node))
c22cacf3 9573 {
5c498b10
DD
9574 warning (OPT_Wattributes,
9575 "%qE attribute only applies to variadic functions", name);
254986c7
KG
9576 *no_add_attrs = true;
9577 }
9578 }
c22cacf3 9579
254986c7 9580 if (args)
3d091dac 9581 {
254986c7 9582 tree position = TREE_VALUE (args);
661a0813
MP
9583 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9584 && TREE_CODE (position) != FUNCTION_DECL)
9585 position = default_conversion (position);
254986c7 9586
661a0813
MP
9587 if (TREE_CODE (position) != INTEGER_CST
9588 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
c22cacf3 9589 {
b8698a0f 9590 warning (OPT_Wattributes,
aa86a51b 9591 "requested position is not an integer constant");
254986c7
KG
9592 *no_add_attrs = true;
9593 }
9594 else
c22cacf3 9595 {
254986c7
KG
9596 if (tree_int_cst_lt (position, integer_zero_node))
9597 {
aa86a51b
DM
9598 warning (OPT_Wattributes,
9599 "requested position is less than zero");
254986c7
KG
9600 *no_add_attrs = true;
9601 }
9602 }
3d091dac 9603 }
c22cacf3 9604
3d091dac
KG
9605 return NULL_TREE;
9606}
b5d32c25
KG
9607
9608/* Handle a "type_generic" attribute. */
9609
9610static tree
9611handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9612 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9613 bool * ARG_UNUSED (no_add_attrs))
9614{
3bf5906b
KG
9615 /* Ensure we have a function type. */
9616 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
b8698a0f 9617
3bf5906b 9618 /* Ensure we have a variadic function. */
dcf0c47e 9619 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5d32c25
KG
9620
9621 return NULL_TREE;
9622}
ab442df7 9623
5779e713 9624/* Handle a "target" attribute. */
ab442df7
MM
9625
9626static tree
5779e713 9627handle_target_attribute (tree *node, tree name, tree args, int flags,
ab442df7
MM
9628 bool *no_add_attrs)
9629{
9630 /* Ensure we have a function type. */
9631 if (TREE_CODE (*node) != FUNCTION_DECL)
9632 {
9633 warning (OPT_Wattributes, "%qE attribute ignored", name);
9634 *no_add_attrs = true;
9635 }
ab442df7 9636 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
32887460 9637 flags))
ab442df7
MM
9638 *no_add_attrs = true;
9639
9640 return NULL_TREE;
9641}
9642
9643/* Arguments being collected for optimization. */
9644typedef const char *const_char_p; /* For DEF_VEC_P. */
9771b263 9645static GTY(()) vec<const_char_p, va_gc> *optimize_args;
ab442df7
MM
9646
9647
9648/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9649 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9650 false for #pragma GCC optimize. */
9651
9652bool
9653parse_optimize_options (tree args, bool attr_p)
9654{
9655 bool ret = true;
9656 unsigned opt_argc;
9657 unsigned i;
cbc19f39 9658 int saved_flag_strict_aliasing;
ab442df7 9659 const char **opt_argv;
6e2f1956
JM
9660 struct cl_decoded_option *decoded_options;
9661 unsigned int decoded_options_count;
ab442df7
MM
9662 tree ap;
9663
9664 /* Build up argv vector. Just in case the string is stored away, use garbage
9665 collected strings. */
9771b263
DN
9666 vec_safe_truncate (optimize_args, 0);
9667 vec_safe_push (optimize_args, (const char *) NULL);
ab442df7
MM
9668
9669 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9670 {
9671 tree value = TREE_VALUE (ap);
9672
9673 if (TREE_CODE (value) == INTEGER_CST)
9674 {
9675 char buffer[20];
9676 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9771b263 9677 vec_safe_push (optimize_args, ggc_strdup (buffer));
ab442df7
MM
9678 }
9679
9680 else if (TREE_CODE (value) == STRING_CST)
9681 {
9682 /* Split string into multiple substrings. */
9683 size_t len = TREE_STRING_LENGTH (value);
9684 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9685 char *end = p + len;
9686 char *comma;
9687 char *next_p = p;
9688
9689 while (next_p != NULL)
9690 {
9691 size_t len2;
9692 char *q, *r;
9693
9694 p = next_p;
9695 comma = strchr (p, ',');
9696 if (comma)
9697 {
9698 len2 = comma - p;
9699 *comma = '\0';
9700 next_p = comma+1;
9701 }
9702 else
9703 {
9704 len2 = end - p;
9705 next_p = NULL;
9706 }
9707
a9429e29 9708 r = q = (char *) ggc_alloc_atomic (len2 + 3);
ab442df7
MM
9709
9710 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9711 options. */
9712 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9713 {
9714 ret = false;
9715 if (attr_p)
9716 warning (OPT_Wattributes,
06730c5d 9717 "bad option %s to optimize attribute", p);
ab442df7
MM
9718 else
9719 warning (OPT_Wpragmas,
de621752 9720 "bad option %s to pragma attribute", p);
ab442df7
MM
9721 continue;
9722 }
9723
9724 if (*p != '-')
9725 {
9726 *r++ = '-';
9727
9728 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9729 itself is -Os, and any other switch begins with a -f. */
9730 if ((*p >= '0' && *p <= '9')
9731 || (p[0] == 's' && p[1] == '\0'))
9732 *r++ = 'O';
9733 else if (*p != 'O')
9734 *r++ = 'f';
9735 }
9736
9737 memcpy (r, p, len2);
9738 r[len2] = '\0';
9771b263 9739 vec_safe_push (optimize_args, (const char *) q);
ab442df7
MM
9740 }
9741
9742 }
9743 }
9744
9771b263 9745 opt_argc = optimize_args->length ();
ab442df7
MM
9746 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9747
9748 for (i = 1; i < opt_argc; i++)
9771b263 9749 opt_argv[i] = (*optimize_args)[i];
ab442df7 9750
cbc19f39
JJ
9751 saved_flag_strict_aliasing = flag_strict_aliasing;
9752
ab442df7 9753 /* Now parse the options. */
a75bfaa6
JM
9754 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9755 &decoded_options,
9756 &decoded_options_count);
9757 decode_options (&global_options, &global_options_set,
a4d8c676
JM
9758 decoded_options, decoded_options_count,
9759 input_location, global_dc);
ab442df7 9760
2b7e2984
SE
9761 targetm.override_options_after_change();
9762
cbc19f39
JJ
9763 /* Don't allow changing -fstrict-aliasing. */
9764 flag_strict_aliasing = saved_flag_strict_aliasing;
9765
9771b263 9766 optimize_args->truncate (0);
ab442df7
MM
9767 return ret;
9768}
9769
9770/* For handling "optimize" attribute. arguments as in
9771 struct attribute_spec.handler. */
9772
9773static tree
9774handle_optimize_attribute (tree *node, tree name, tree args,
9775 int ARG_UNUSED (flags), bool *no_add_attrs)
9776{
9777 /* Ensure we have a function type. */
9778 if (TREE_CODE (*node) != FUNCTION_DECL)
9779 {
9780 warning (OPT_Wattributes, "%qE attribute ignored", name);
9781 *no_add_attrs = true;
9782 }
9783 else
9784 {
9785 struct cl_optimization cur_opts;
9786 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9787
9788 /* Save current options. */
46625112 9789 cl_optimization_save (&cur_opts, &global_options);
ab442df7
MM
9790
9791 /* If we previously had some optimization options, use them as the
9792 default. */
9793 if (old_opts)
46625112
JM
9794 cl_optimization_restore (&global_options,
9795 TREE_OPTIMIZATION (old_opts));
ab442df7
MM
9796
9797 /* Parse options, and update the vector. */
9798 parse_optimize_options (args, true);
9799 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
bf7b5747 9800 = build_optimization_node (&global_options);
ab442df7
MM
9801
9802 /* Restore current options. */
46625112 9803 cl_optimization_restore (&global_options, &cur_opts);
ab442df7
MM
9804 }
9805
9806 return NULL_TREE;
9807}
7458026b
ILT
9808
9809/* Handle a "no_split_stack" attribute. */
9810
9811static tree
9812handle_no_split_stack_attribute (tree *node, tree name,
9813 tree ARG_UNUSED (args),
9814 int ARG_UNUSED (flags),
9815 bool *no_add_attrs)
9816{
9817 tree decl = *node;
9818
9819 if (TREE_CODE (decl) != FUNCTION_DECL)
9820 {
9821 error_at (DECL_SOURCE_LOCATION (decl),
9822 "%qE attribute applies only to functions", name);
9823 *no_add_attrs = true;
9824 }
9825 else if (DECL_INITIAL (decl))
9826 {
9827 error_at (DECL_SOURCE_LOCATION (decl),
9828 "can%'t set %qE attribute after definition", name);
9829 *no_add_attrs = true;
9830 }
9831
9832 return NULL_TREE;
9833}
826cacfe
MG
9834
9835/* Handle a "returns_nonnull" attribute; arguments as in
9836 struct attribute_spec.handler. */
9837
9838static tree
9839handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9840 bool *no_add_attrs)
9841{
9842 // Even without a prototype we still have a return type we can check.
9843 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9844 {
9845 error ("returns_nonnull attribute on a function not returning a pointer");
9846 *no_add_attrs = true;
9847 }
9848 return NULL_TREE;
9849}
9850
976d5a22
TT
9851/* Handle a "designated_init" attribute; arguments as in
9852 struct attribute_spec.handler. */
9853
9854static tree
9855handle_designated_init_attribute (tree *node, tree name, tree, int,
9856 bool *no_add_attrs)
9857{
9858 if (TREE_CODE (*node) != RECORD_TYPE)
9859 {
9860 error ("%qE attribute is only valid on %<struct%> type", name);
9861 *no_add_attrs = true;
9862 }
9863 return NULL_TREE;
9864}
9865
b34c7881 9866\f
dde05067
NF
9867/* Check for valid arguments being passed to a function with FNTYPE.
9868 There are NARGS arguments in the array ARGARRAY. */
b34c7881 9869void
dde05067 9870check_function_arguments (const_tree fntype, int nargs, tree *argarray)
b34c7881
JT
9871{
9872 /* Check for null being passed in a pointer argument that must be
9873 non-null. We also need to do this if format checking is enabled. */
9874
9875 if (warn_nonnull)
dde05067 9876 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
b34c7881
JT
9877
9878 /* Check for errors in format strings. */
9879
e6c69da0 9880 if (warn_format || warn_suggest_attribute_format)
dde05067 9881 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
7876a414
KG
9882
9883 if (warn_format)
dde05067 9884 check_function_sentinel (fntype, nargs, argarray);
b34c7881
JT
9885}
9886
9887/* Generic argument checking recursion routine. PARAM is the argument to
9888 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9889 once the argument is resolved. CTX is context for the callback. */
9890void
35b1a6fa
AJ
9891check_function_arguments_recurse (void (*callback)
9892 (void *, tree, unsigned HOST_WIDE_INT),
9893 void *ctx, tree param,
9894 unsigned HOST_WIDE_INT param_num)
b34c7881 9895{
1043771b 9896 if (CONVERT_EXPR_P (param)
1344f9a3
JM
9897 && (TYPE_PRECISION (TREE_TYPE (param))
9898 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
b34c7881
JT
9899 {
9900 /* Strip coercion. */
9901 check_function_arguments_recurse (callback, ctx,
6de9cd9a 9902 TREE_OPERAND (param, 0), param_num);
b34c7881
JT
9903 return;
9904 }
9905
9906 if (TREE_CODE (param) == CALL_EXPR)
9907 {
5039610b 9908 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
b34c7881
JT
9909 tree attrs;
9910 bool found_format_arg = false;
9911
9912 /* See if this is a call to a known internationalization function
9913 that modifies a format arg. Such a function may have multiple
9914 format_arg attributes (for example, ngettext). */
9915
9916 for (attrs = TYPE_ATTRIBUTES (type);
9917 attrs;
9918 attrs = TREE_CHAIN (attrs))
9919 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9920 {
5039610b 9921 tree inner_arg;
b34c7881
JT
9922 tree format_num_expr;
9923 int format_num;
9924 int i;
5039610b 9925 call_expr_arg_iterator iter;
b34c7881
JT
9926
9927 /* Extract the argument number, which was previously checked
9928 to be valid. */
9929 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
b34c7881 9930
6b3b8c27 9931 format_num = tree_to_uhwi (format_num_expr);
b34c7881 9932
5039610b
SL
9933 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9934 inner_arg != 0;
9935 inner_arg = next_call_expr_arg (&iter), i++)
b34c7881
JT
9936 if (i == format_num)
9937 {
9938 check_function_arguments_recurse (callback, ctx,
5039610b 9939 inner_arg, param_num);
b34c7881
JT
9940 found_format_arg = true;
9941 break;
9942 }
9943 }
9944
9945 /* If we found a format_arg attribute and did a recursive check,
9946 we are done with checking this argument. Otherwise, we continue
9947 and this will be considered a non-literal. */
9948 if (found_format_arg)
9949 return;
9950 }
9951
9952 if (TREE_CODE (param) == COND_EXPR)
9953 {
9954 /* Check both halves of the conditional expression. */
9955 check_function_arguments_recurse (callback, ctx,
6de9cd9a 9956 TREE_OPERAND (param, 1), param_num);
b34c7881 9957 check_function_arguments_recurse (callback, ctx,
6de9cd9a 9958 TREE_OPERAND (param, 2), param_num);
b34c7881
JT
9959 return;
9960 }
9961
9962 (*callback) (ctx, param, param_num);
9963}
e2500fed 9964
a98c2819
MLI
9965/* Checks for a builtin function FNDECL that the number of arguments
9966 NARGS against the required number REQUIRED and issues an error if
9967 there is a mismatch. Returns true if the number of arguments is
9968 correct, otherwise false. */
83322951
RG
9969
9970static bool
a98c2819 9971builtin_function_validate_nargs (tree fndecl, int nargs, int required)
83322951
RG
9972{
9973 if (nargs < required)
9974 {
a98c2819
MLI
9975 error_at (input_location,
9976 "not enough arguments to function %qE", fndecl);
83322951
RG
9977 return false;
9978 }
9979 else if (nargs > required)
9980 {
a98c2819
MLI
9981 error_at (input_location,
9982 "too many arguments to function %qE", fndecl);
83322951
RG
9983 return false;
9984 }
9985 return true;
9986}
9987
9988/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9989 Returns false if there was an error, otherwise true. */
9990
9991bool
9992check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9993{
9994 if (!DECL_BUILT_IN (fndecl)
9995 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9996 return true;
9997
9998 switch (DECL_FUNCTION_CODE (fndecl))
9999 {
10000 case BUILT_IN_CONSTANT_P:
a98c2819 10001 return builtin_function_validate_nargs (fndecl, nargs, 1);
83322951
RG
10002
10003 case BUILT_IN_ISFINITE:
10004 case BUILT_IN_ISINF:
05f41289 10005 case BUILT_IN_ISINF_SIGN:
83322951
RG
10006 case BUILT_IN_ISNAN:
10007 case BUILT_IN_ISNORMAL:
a98c2819 10008 if (builtin_function_validate_nargs (fndecl, nargs, 1))
83322951
RG
10009 {
10010 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10011 {
10012 error ("non-floating-point argument in call to "
10013 "function %qE", fndecl);
10014 return false;
10015 }
10016 return true;
10017 }
10018 return false;
10019
10020 case BUILT_IN_ISGREATER:
10021 case BUILT_IN_ISGREATEREQUAL:
10022 case BUILT_IN_ISLESS:
10023 case BUILT_IN_ISLESSEQUAL:
10024 case BUILT_IN_ISLESSGREATER:
10025 case BUILT_IN_ISUNORDERED:
a98c2819 10026 if (builtin_function_validate_nargs (fndecl, nargs, 2))
83322951
RG
10027 {
10028 enum tree_code code0, code1;
10029 code0 = TREE_CODE (TREE_TYPE (args[0]));
10030 code1 = TREE_CODE (TREE_TYPE (args[1]));
10031 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10032 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10033 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10034 {
10035 error ("non-floating-point arguments in call to "
10036 "function %qE", fndecl);
10037 return false;
10038 }
10039 return true;
10040 }
10041 return false;
10042
3bf5906b 10043 case BUILT_IN_FPCLASSIFY:
a98c2819 10044 if (builtin_function_validate_nargs (fndecl, nargs, 6))
3bf5906b
KG
10045 {
10046 unsigned i;
b8698a0f 10047
3bf5906b
KG
10048 for (i=0; i<5; i++)
10049 if (TREE_CODE (args[i]) != INTEGER_CST)
10050 {
10051 error ("non-const integer argument %u in call to function %qE",
10052 i+1, fndecl);
10053 return false;
10054 }
10055
10056 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10057 {
10058 error ("non-floating-point argument in call to function %qE",
10059 fndecl);
10060 return false;
10061 }
10062 return true;
10063 }
10064 return false;
10065
45d439ac
JJ
10066 case BUILT_IN_ASSUME_ALIGNED:
10067 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10068 {
10069 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10070 {
10071 error ("non-integer argument 3 in call to function %qE", fndecl);
10072 return false;
10073 }
10074 return true;
10075 }
10076 return false;
10077
1304953e
JJ
10078 case BUILT_IN_ADD_OVERFLOW:
10079 case BUILT_IN_SUB_OVERFLOW:
10080 case BUILT_IN_MUL_OVERFLOW:
10081 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10082 {
10083 unsigned i;
10084 for (i = 0; i < 2; i++)
10085 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10086 {
10087 error ("argument %u in call to function %qE does not have "
10088 "integral type", i + 1, fndecl);
10089 return false;
10090 }
10091 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10092 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10093 {
10094 error ("argument 3 in call to function %qE does not have "
10095 "pointer to integer type", fndecl);
10096 return false;
10097 }
10098 return true;
10099 }
10100 return false;
10101
83322951
RG
10102 default:
10103 return true;
10104 }
10105}
10106
d07605f5
AP
10107/* Function to help qsort sort FIELD_DECLs by name order. */
10108
10109int
10110field_decl_cmp (const void *x_p, const void *y_p)
10111{
28dab132
BI
10112 const tree *const x = (const tree *const) x_p;
10113 const tree *const y = (const tree *const) y_p;
10114
d07605f5
AP
10115 if (DECL_NAME (*x) == DECL_NAME (*y))
10116 /* A nontype is "greater" than a type. */
10117 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10118 if (DECL_NAME (*x) == NULL_TREE)
10119 return -1;
10120 if (DECL_NAME (*y) == NULL_TREE)
10121 return 1;
10122 if (DECL_NAME (*x) < DECL_NAME (*y))
10123 return -1;
10124 return 1;
10125}
10126
10127static struct {
10128 gt_pointer_operator new_value;
10129 void *cookie;
10130} resort_data;
10131
10132/* This routine compares two fields like field_decl_cmp but using the
10133pointer operator in resort_data. */
10134
10135static int
10136resort_field_decl_cmp (const void *x_p, const void *y_p)
10137{
28dab132
BI
10138 const tree *const x = (const tree *const) x_p;
10139 const tree *const y = (const tree *const) y_p;
d07605f5
AP
10140
10141 if (DECL_NAME (*x) == DECL_NAME (*y))
10142 /* A nontype is "greater" than a type. */
10143 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10144 if (DECL_NAME (*x) == NULL_TREE)
10145 return -1;
10146 if (DECL_NAME (*y) == NULL_TREE)
10147 return 1;
10148 {
10149 tree d1 = DECL_NAME (*x);
10150 tree d2 = DECL_NAME (*y);
10151 resort_data.new_value (&d1, resort_data.cookie);
10152 resort_data.new_value (&d2, resort_data.cookie);
10153 if (d1 < d2)
10154 return -1;
10155 }
10156 return 1;
10157}
10158
10159/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10160
10161void
10162resort_sorted_fields (void *obj,
e18476eb 10163 void * ARG_UNUSED (orig_obj),
6de9cd9a
DN
10164 gt_pointer_operator new_value,
10165 void *cookie)
d07605f5 10166{
e18476eb 10167 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
d07605f5
AP
10168 resort_data.new_value = new_value;
10169 resort_data.cookie = cookie;
10170 qsort (&sf->elts[0], sf->len, sizeof (tree),
6de9cd9a 10171 resort_field_decl_cmp);
d07605f5
AP
10172}
10173
0a3ee0fd
GDR
10174/* Subroutine of c_parse_error.
10175 Return the result of concatenating LHS and RHS. RHS is really
10176 a string literal, its first character is indicated by RHS_START and
3292fb42 10177 RHS_SIZE is its length (including the terminating NUL character).
0a3ee0fd
GDR
10178
10179 The caller is responsible for deleting the returned pointer. */
10180
10181static char *
10182catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10183{
10184 const int lhs_size = strlen (lhs);
10185 char *result = XNEWVEC (char, lhs_size + rhs_size);
10186 strncpy (result, lhs, lhs_size);
10187 strncpy (result + lhs_size, rhs_start, rhs_size);
10188 return result;
10189}
10190
4b794eaf 10191/* Issue the error given by GMSGID, indicating that it occurred before
4bb8ca28
MM
10192 TOKEN, which had the associated VALUE. */
10193
10194void
b8698a0f 10195c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
cfc93532 10196 tree value, unsigned char token_flags)
4bb8ca28 10197{
0a3ee0fd
GDR
10198#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10199
10200 char *message = NULL;
4bb8ca28 10201
cfc93532 10202 if (token_type == CPP_EOF)
4b794eaf 10203 message = catenate_messages (gmsgid, " at end of input");
b8698a0f
L
10204 else if (token_type == CPP_CHAR
10205 || token_type == CPP_WCHAR
cfc93532
MLI
10206 || token_type == CPP_CHAR16
10207 || token_type == CPP_CHAR32)
4bb8ca28
MM
10208 {
10209 unsigned int val = TREE_INT_CST_LOW (value);
b6baa67d
KVH
10210 const char *prefix;
10211
cfc93532 10212 switch (token_type)
b6baa67d
KVH
10213 {
10214 default:
10215 prefix = "";
10216 break;
10217 case CPP_WCHAR:
10218 prefix = "L";
10219 break;
10220 case CPP_CHAR16:
10221 prefix = "u";
10222 break;
10223 case CPP_CHAR32:
10224 prefix = "U";
10225 break;
10226 }
10227
4bb8ca28 10228 if (val <= UCHAR_MAX && ISGRAPH (val))
c22cacf3 10229 message = catenate_messages (gmsgid, " before %s'%c'");
4bb8ca28 10230 else
c22cacf3 10231 message = catenate_messages (gmsgid, " before %s'\\x%x'");
0a3ee0fd 10232
b6baa67d 10233 error (message, prefix, val);
0a3ee0fd
GDR
10234 free (message);
10235 message = NULL;
4bb8ca28 10236 }
65e5a578
ESR
10237 else if (token_type == CPP_CHAR_USERDEF
10238 || token_type == CPP_WCHAR_USERDEF
10239 || token_type == CPP_CHAR16_USERDEF
10240 || token_type == CPP_CHAR32_USERDEF)
10241 message = catenate_messages (gmsgid,
10242 " before user-defined character literal");
10243 else if (token_type == CPP_STRING_USERDEF
10244 || token_type == CPP_WSTRING_USERDEF
10245 || token_type == CPP_STRING16_USERDEF
10246 || token_type == CPP_STRING32_USERDEF
10247 || token_type == CPP_UTF8STRING_USERDEF)
10248 message = catenate_messages (gmsgid, " before user-defined string literal");
b8698a0f
L
10249 else if (token_type == CPP_STRING
10250 || token_type == CPP_WSTRING
cfc93532 10251 || token_type == CPP_STRING16
2c6e3f55
JJ
10252 || token_type == CPP_STRING32
10253 || token_type == CPP_UTF8STRING)
4b794eaf 10254 message = catenate_messages (gmsgid, " before string constant");
cfc93532 10255 else if (token_type == CPP_NUMBER)
4b794eaf 10256 message = catenate_messages (gmsgid, " before numeric constant");
cfc93532 10257 else if (token_type == CPP_NAME)
0a3ee0fd 10258 {
4b794eaf 10259 message = catenate_messages (gmsgid, " before %qE");
c51a1ba9 10260 error (message, value);
0a3ee0fd
GDR
10261 free (message);
10262 message = NULL;
10263 }
cfc93532 10264 else if (token_type == CPP_PRAGMA)
bc4071dd 10265 message = catenate_messages (gmsgid, " before %<#pragma%>");
cfc93532 10266 else if (token_type == CPP_PRAGMA_EOL)
bc4071dd 10267 message = catenate_messages (gmsgid, " before end of line");
34429675
JM
10268 else if (token_type == CPP_DECLTYPE)
10269 message = catenate_messages (gmsgid, " before %<decltype%>");
cfc93532 10270 else if (token_type < N_TTYPES)
0a3ee0fd 10271 {
4b794eaf 10272 message = catenate_messages (gmsgid, " before %qs token");
cfc93532 10273 error (message, cpp_type2name (token_type, token_flags));
0a3ee0fd
GDR
10274 free (message);
10275 message = NULL;
10276 }
4bb8ca28 10277 else
4b794eaf 10278 error (gmsgid);
0a3ee0fd
GDR
10279
10280 if (message)
10281 {
10282 error (message);
10283 free (message);
10284 }
c22cacf3 10285#undef catenate_messages
4bb8ca28
MM
10286}
10287
87cf0651
SB
10288/* Return the gcc option code associated with the reason for a cpp
10289 message, or 0 if none. */
10290
10291static int
10292c_option_controlling_cpp_error (int reason)
10293{
b559c810 10294 const struct cpp_reason_option_codes_t *entry;
87cf0651 10295
b559c810 10296 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
87cf0651
SB
10297 {
10298 if (entry->reason == reason)
10299 return entry->option_code;
10300 }
10301 return 0;
10302}
10303
148e4216 10304/* Callback from cpp_error for PFILE to print diagnostics from the
87cf0651
SB
10305 preprocessor. The diagnostic is of type LEVEL, with REASON set
10306 to the reason code if LEVEL is represents a warning, at location
148e4216
JM
10307 LOCATION unless this is after lexing and the compiler's location
10308 should be used instead, with column number possibly overridden by
10309 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10310 the arguments. Returns true if a diagnostic was emitted, false
10311 otherwise. */
10312
10313bool
87cf0651 10314c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
148e4216
JM
10315 location_t location, unsigned int column_override,
10316 const char *msg, va_list *ap)
10317{
10318 diagnostic_info diagnostic;
10319 diagnostic_t dlevel;
e3339d0f 10320 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
148e4216
JM
10321 bool ret;
10322
10323 switch (level)
10324 {
10325 case CPP_DL_WARNING_SYSHDR:
10326 if (flag_no_output)
10327 return false;
e3339d0f 10328 global_dc->dc_warn_system_headers = 1;
148e4216
JM
10329 /* Fall through. */
10330 case CPP_DL_WARNING:
10331 if (flag_no_output)
10332 return false;
10333 dlevel = DK_WARNING;
10334 break;
10335 case CPP_DL_PEDWARN:
10336 if (flag_no_output && !flag_pedantic_errors)
10337 return false;
10338 dlevel = DK_PEDWARN;
10339 break;
10340 case CPP_DL_ERROR:
10341 dlevel = DK_ERROR;
10342 break;
10343 case CPP_DL_ICE:
10344 dlevel = DK_ICE;
10345 break;
10346 case CPP_DL_NOTE:
10347 dlevel = DK_NOTE;
10348 break;
47580d22
JM
10349 case CPP_DL_FATAL:
10350 dlevel = DK_FATAL;
10351 break;
148e4216
JM
10352 default:
10353 gcc_unreachable ();
10354 }
10355 if (done_lexing)
10356 location = input_location;
10357 diagnostic_set_info_translated (&diagnostic, msg, ap,
10358 location, dlevel);
10359 if (column_override)
10360 diagnostic_override_column (&diagnostic, column_override);
87cf0651
SB
10361 diagnostic_override_option_index (&diagnostic,
10362 c_option_controlling_cpp_error (reason));
148e4216
JM
10363 ret = report_diagnostic (&diagnostic);
10364 if (level == CPP_DL_WARNING_SYSHDR)
e3339d0f 10365 global_dc->dc_warn_system_headers = save_warn_system_headers;
148e4216
JM
10366 return ret;
10367}
10368
c5ff069d
ZW
10369/* Convert a character from the host to the target execution character
10370 set. cpplib handles this, mostly. */
10371
10372HOST_WIDE_INT
10373c_common_to_target_charset (HOST_WIDE_INT c)
10374{
10375 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10376 zero-extended under -fno-signed-char. cpplib insists that characters
10377 and character constants are always unsigned. Hence we must convert
10378 back and forth. */
10379 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10380
10381 uc = cpp_host_to_exec_charset (parse_in, uc);
10382
10383 if (flag_signed_char)
10384 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10385 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10386 else
10387 return uc;
10388}
10389
cf9e9959
EB
10390/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10391 references with an INDIRECT_REF of a constant at the bottom; much like the
10392 traditional rendering of offsetof as a macro. Return the folded result. */
ee8a6a3e 10393
cf9e9959
EB
10394tree
10395fold_offsetof_1 (tree expr)
ee8a6a3e 10396{
ee8a6a3e
RH
10397 tree base, off, t;
10398
10399 switch (TREE_CODE (expr))
10400 {
10401 case ERROR_MARK:
10402 return expr;
10403
545b7d8c
VR
10404 case VAR_DECL:
10405 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10406 return error_mark_node;
10407
6d4d7b0e 10408 case CALL_EXPR:
8d5f60ac 10409 case TARGET_EXPR:
6d4d7b0e
PB
10410 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10411 return error_mark_node;
10412
6d4d7b0e
PB
10413 case NOP_EXPR:
10414 case INDIRECT_REF:
cf9e9959 10415 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
61c3c490
DS
10416 {
10417 error ("cannot apply %<offsetof%> to a non constant address");
10418 return error_mark_node;
10419 }
cf9e9959 10420 return TREE_OPERAND (expr, 0);
6d4d7b0e 10421
ee8a6a3e 10422 case COMPONENT_REF:
cf9e9959 10423 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
ee8a6a3e
RH
10424 if (base == error_mark_node)
10425 return base;
10426
10427 t = TREE_OPERAND (expr, 1);
10428 if (DECL_C_BIT_FIELD (t))
10429 {
10430 error ("attempt to take address of bit-field structure "
c51a1ba9 10431 "member %qD", t);
ee8a6a3e
RH
10432 return error_mark_node;
10433 }
db3927fb 10434 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
386b1f1f 10435 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
db3927fb 10436 / BITS_PER_UNIT));
ee8a6a3e
RH
10437 break;
10438
10439 case ARRAY_REF:
cf9e9959 10440 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
ee8a6a3e
RH
10441 if (base == error_mark_node)
10442 return base;
10443
10444 t = TREE_OPERAND (expr, 1);
61c3c490
DS
10445
10446 /* Check if the offset goes beyond the upper bound of the array. */
cf9e9959 10447 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
d32599a6
JJ
10448 {
10449 tree upbound = array_ref_up_bound (expr);
10450 if (upbound != NULL_TREE
10451 && TREE_CODE (upbound) == INTEGER_CST
10452 && !tree_int_cst_equal (upbound,
10453 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10454 {
10455 upbound = size_binop (PLUS_EXPR, upbound,
10456 build_int_cst (TREE_TYPE (upbound), 1));
10457 if (tree_int_cst_lt (upbound, t))
10458 {
10459 tree v;
10460
10461 for (v = TREE_OPERAND (expr, 0);
10462 TREE_CODE (v) == COMPONENT_REF;
10463 v = TREE_OPERAND (v, 0))
10464 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10465 == RECORD_TYPE)
10466 {
910ad8de
NF
10467 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10468 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
d32599a6
JJ
10469 if (TREE_CODE (fld_chain) == FIELD_DECL)
10470 break;
10471
10472 if (fld_chain)
10473 break;
10474 }
10475 /* Don't warn if the array might be considered a poor
10476 man's flexible array member with a very permissive
10477 definition thereof. */
10478 if (TREE_CODE (v) == ARRAY_REF
10479 || TREE_CODE (v) == COMPONENT_REF)
10480 warning (OPT_Warray_bounds,
10481 "index %E denotes an offset "
10482 "greater than size of %qT",
10483 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10484 }
10485 }
10486 }
cf9e9959
EB
10487
10488 t = convert (sizetype, t);
10489 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
ee8a6a3e
RH
10490 break;
10491
1916c916
VR
10492 case COMPOUND_EXPR:
10493 /* Handle static members of volatile structs. */
10494 t = TREE_OPERAND (expr, 1);
10495 gcc_assert (TREE_CODE (t) == VAR_DECL);
cf9e9959 10496 return fold_offsetof_1 (t);
1916c916 10497
ee8a6a3e 10498 default:
366de0ce 10499 gcc_unreachable ();
ee8a6a3e
RH
10500 }
10501
cf9e9959 10502 return fold_build_pointer_plus (base, off);
ee8a6a3e
RH
10503}
10504
cf9e9959
EB
10505/* Likewise, but convert it to the return type of offsetof. */
10506
ee8a6a3e 10507tree
cf9e9959 10508fold_offsetof (tree expr)
ee8a6a3e 10509{
cf9e9959 10510 return convert (size_type_node, fold_offsetof_1 (expr));
ee8a6a3e
RH
10511}
10512
d166d4c3
AK
10513/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10514 expression, because B will always be true. */
10515
10516void
10517warn_for_omitted_condop (location_t location, tree cond)
10518{
10519 if (truth_value_p (TREE_CODE (cond)))
10520 warning_at (location, OPT_Wparentheses,
10521 "the omitted middle operand in ?: will always be %<true%>, "
10522 "suggest explicit middle operand");
10523}
10524
4816c593
NF
10525/* Give an error for storing into ARG, which is 'const'. USE indicates
10526 how ARG was being used. */
10527
10528void
c02065fc 10529readonly_error (location_t loc, tree arg, enum lvalue_use use)
4816c593
NF
10530{
10531 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10532 || use == lv_asm);
10533 /* Using this macro rather than (for example) arrays of messages
10534 ensures that all the format strings are checked at compile
10535 time. */
10536#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10537 : (use == lv_increment ? (I) \
10538 : (use == lv_decrement ? (D) : (AS))))
10539 if (TREE_CODE (arg) == COMPONENT_REF)
10540 {
10541 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
c02065fc
AH
10542 error_at (loc, READONLY_MSG (G_("assignment of member "
10543 "%qD in read-only object"),
10544 G_("increment of member "
10545 "%qD in read-only object"),
10546 G_("decrement of member "
10547 "%qD in read-only object"),
10548 G_("member %qD in read-only object "
10549 "used as %<asm%> output")),
10550 TREE_OPERAND (arg, 1));
4816c593 10551 else
c02065fc
AH
10552 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10553 G_("increment of read-only member %qD"),
10554 G_("decrement of read-only member %qD"),
10555 G_("read-only member %qD used as %<asm%> output")),
10556 TREE_OPERAND (arg, 1));
4816c593
NF
10557 }
10558 else if (TREE_CODE (arg) == VAR_DECL)
c02065fc
AH
10559 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10560 G_("increment of read-only variable %qD"),
10561 G_("decrement of read-only variable %qD"),
10562 G_("read-only variable %qD used as %<asm%> output")),
10563 arg);
4816c593 10564 else if (TREE_CODE (arg) == PARM_DECL)
c02065fc
AH
10565 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10566 G_("increment of read-only parameter %qD"),
10567 G_("decrement of read-only parameter %qD"),
10568 G_("read-only parameter %qD use as %<asm%> output")),
10569 arg);
4816c593
NF
10570 else if (TREE_CODE (arg) == RESULT_DECL)
10571 {
10572 gcc_assert (c_dialect_cxx ());
c02065fc
AH
10573 error_at (loc, READONLY_MSG (G_("assignment of "
10574 "read-only named return value %qD"),
10575 G_("increment of "
10576 "read-only named return value %qD"),
10577 G_("decrement of "
10578 "read-only named return value %qD"),
10579 G_("read-only named return value %qD "
10580 "used as %<asm%>output")),
10581 arg);
4816c593
NF
10582 }
10583 else if (TREE_CODE (arg) == FUNCTION_DECL)
c02065fc
AH
10584 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10585 G_("increment of function %qD"),
10586 G_("decrement of function %qD"),
10587 G_("function %qD used as %<asm%> output")),
10588 arg);
4816c593 10589 else
c02065fc
AH
10590 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10591 G_("increment of read-only location %qE"),
10592 G_("decrement of read-only location %qE"),
10593 G_("read-only location %qE used as %<asm%> output")),
10594 arg);
4816c593
NF
10595}
10596
37dc0d8d 10597/* Print an error message for an invalid lvalue. USE says
7bd11157
TT
10598 how the lvalue is being used and so selects the error message. LOC
10599 is the location for the error. */
5ae9ba3e 10600
37dc0d8d 10601void
7bd11157 10602lvalue_error (location_t loc, enum lvalue_use use)
5ae9ba3e 10603{
37dc0d8d 10604 switch (use)
5ae9ba3e 10605 {
37dc0d8d 10606 case lv_assign:
7bd11157 10607 error_at (loc, "lvalue required as left operand of assignment");
37dc0d8d
JM
10608 break;
10609 case lv_increment:
7bd11157 10610 error_at (loc, "lvalue required as increment operand");
37dc0d8d
JM
10611 break;
10612 case lv_decrement:
7bd11157 10613 error_at (loc, "lvalue required as decrement operand");
37dc0d8d
JM
10614 break;
10615 case lv_addressof:
7bd11157 10616 error_at (loc, "lvalue required as unary %<&%> operand");
37dc0d8d
JM
10617 break;
10618 case lv_asm:
7bd11157 10619 error_at (loc, "lvalue required in asm statement");
37dc0d8d
JM
10620 break;
10621 default:
10622 gcc_unreachable ();
5ae9ba3e 10623 }
5ae9ba3e 10624}
7a6daeb0
NF
10625
10626/* Print an error message for an invalid indirection of type TYPE.
10627 ERRSTRING is the name of the operator for the indirection. */
10628
10629void
10630invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10631{
10632 switch (errstring)
10633 {
10634 case RO_NULL:
10635 gcc_assert (c_dialect_cxx ());
10636 error_at (loc, "invalid type argument (have %qT)", type);
10637 break;
10638 case RO_ARRAY_INDEXING:
10639 error_at (loc,
10640 "invalid type argument of array indexing (have %qT)",
10641 type);
10642 break;
10643 case RO_UNARY_STAR:
10644 error_at (loc,
10645 "invalid type argument of unary %<*%> (have %qT)",
10646 type);
10647 break;
10648 case RO_ARROW:
10649 error_at (loc,
10650 "invalid type argument of %<->%> (have %qT)",
10651 type);
10652 break;
55a7f02f
MP
10653 case RO_ARROW_STAR:
10654 error_at (loc,
10655 "invalid type argument of %<->*%> (have %qT)",
10656 type);
10657 break;
7a6daeb0
NF
10658 case RO_IMPLICIT_CONVERSION:
10659 error_at (loc,
10660 "invalid type argument of implicit conversion (have %qT)",
10661 type);
10662 break;
10663 default:
10664 gcc_unreachable ();
10665 }
10666}
aab038d5
RH
10667\f
10668/* *PTYPE is an incomplete array. Complete it with a domain based on
10669 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10670 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10671 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10672
10673int
10674complete_array_type (tree *ptype, tree initial_value, bool do_default)
10675{
10676 tree maxindex, type, main_type, elt, unqual_elt;
10677 int failure = 0, quals;
06d40de8 10678 hashval_t hashcode = 0;
40d3d530 10679 bool overflow_p = false;
aab038d5
RH
10680
10681 maxindex = size_zero_node;
10682 if (initial_value)
10683 {
10684 if (TREE_CODE (initial_value) == STRING_CST)
10685 {
10686 int eltsize
10687 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10688 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10689 }
10690 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10691 {
9771b263 10692 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
aab038d5 10693
9771b263 10694 if (vec_safe_is_empty (v))
aab038d5
RH
10695 {
10696 if (pedantic)
10697 failure = 3;
830c740f 10698 maxindex = ssize_int (-1);
aab038d5
RH
10699 }
10700 else
10701 {
10702 tree curindex;
4038c495
GB
10703 unsigned HOST_WIDE_INT cnt;
10704 constructor_elt *ce;
cff7525f 10705 bool fold_p = false;
aab038d5 10706
9771b263 10707 if ((*v)[0].index)
40d3d530
JR
10708 maxindex = (*v)[0].index, fold_p = true;
10709
aab038d5
RH
10710 curindex = maxindex;
10711
9771b263 10712 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
aab038d5 10713 {
cff7525f 10714 bool curfold_p = false;
4038c495 10715 if (ce->index)
cff7525f 10716 curindex = ce->index, curfold_p = true;
aab038d5 10717 else
cff7525f
JH
10718 {
10719 if (fold_p)
40d3d530
JR
10720 {
10721 /* Since we treat size types now as ordinary
10722 unsigned types, we need an explicit overflow
10723 check. */
10724 tree orig = curindex;
10725 curindex = fold_convert (sizetype, curindex);
10726 overflow_p |= tree_int_cst_lt (curindex, orig);
10727 }
db3927fb
AH
10728 curindex = size_binop (PLUS_EXPR, curindex,
10729 size_one_node);
cff7525f 10730 }
aab038d5 10731 if (tree_int_cst_lt (maxindex, curindex))
cff7525f 10732 maxindex = curindex, fold_p = curfold_p;
aab038d5 10733 }
40d3d530
JR
10734 if (fold_p)
10735 {
10736 tree orig = maxindex;
10737 maxindex = fold_convert (sizetype, maxindex);
10738 overflow_p |= tree_int_cst_lt (maxindex, orig);
10739 }
aab038d5
RH
10740 }
10741 }
10742 else
10743 {
10744 /* Make an error message unless that happened already. */
10745 if (initial_value != error_mark_node)
10746 failure = 1;
10747 }
10748 }
10749 else
10750 {
10751 failure = 2;
10752 if (!do_default)
10753 return failure;
10754 }
10755
10756 type = *ptype;
10757 elt = TREE_TYPE (type);
10758 quals = TYPE_QUALS (strip_array_types (elt));
10759 if (quals == 0)
10760 unqual_elt = elt;
10761 else
36c5e70a 10762 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
aab038d5
RH
10763
10764 /* Using build_distinct_type_copy and modifying things afterward instead
10765 of using build_array_type to create a new type preserves all of the
10766 TYPE_LANG_FLAG_? bits that the front end may have set. */
10767 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10768 TREE_TYPE (main_type) = unqual_elt;
e6313a78
RG
10769 TYPE_DOMAIN (main_type)
10770 = build_range_type (TREE_TYPE (maxindex),
10771 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
aab038d5
RH
10772 layout_type (main_type);
10773
06d40de8
DG
10774 /* Make sure we have the canonical MAIN_TYPE. */
10775 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
b8698a0f 10776 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
06d40de8
DG
10777 hashcode);
10778 main_type = type_hash_canon (hashcode, main_type);
10779
9ae165a0
DG
10780 /* Fix the canonical type. */
10781 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10782 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10783 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10784 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10785 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10786 != TYPE_DOMAIN (main_type)))
b8698a0f 10787 TYPE_CANONICAL (main_type)
9ae165a0
DG
10788 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10789 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10790 else
10791 TYPE_CANONICAL (main_type) = main_type;
10792
aab038d5
RH
10793 if (quals == 0)
10794 type = main_type;
10795 else
10796 type = c_build_qualified_type (main_type, quals);
10797
7bfcb402
JM
10798 if (COMPLETE_TYPE_P (type)
10799 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
40d3d530 10800 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7bfcb402
JM
10801 {
10802 error ("size of array is too large");
10803 /* If we proceed with the array type as it is, we'll eventually
386b1f1f 10804 crash in tree_to_[su]hwi(). */
7bfcb402
JM
10805 type = error_mark_node;
10806 }
10807
aab038d5
RH
10808 *ptype = type;
10809 return failure;
10810}
5ae9ba3e 10811
30cd1c5d
AS
10812/* Like c_mark_addressable but don't check register qualifier. */
10813void
10814c_common_mark_addressable_vec (tree t)
10815{
10816 while (handled_component_p (t))
10817 t = TREE_OPERAND (t, 0);
10818 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10819 return;
10820 TREE_ADDRESSABLE (t) = 1;
10821}
10822
10823
48ae6c13
RH
10824\f
10825/* Used to help initialize the builtin-types.def table. When a type of
10826 the correct size doesn't exist, use error_mark_node instead of NULL.
10827 The later results in segfaults even when a decl using the type doesn't
10828 get invoked. */
10829
10830tree
10831builtin_type_for_size (int size, bool unsignedp)
10832{
21fa2faf 10833 tree type = c_common_type_for_size (size, unsignedp);
48ae6c13
RH
10834 return type ? type : error_mark_node;
10835}
10836
10837/* A helper function for resolve_overloaded_builtin in resolving the
10838 overloaded __sync_ builtins. Returns a positive power of 2 if the
10839 first operand of PARAMS is a pointer to a supported data type.
10840 Returns 0 if an error is encountered. */
10841
10842static int
9771b263 10843sync_resolve_size (tree function, vec<tree, va_gc> *params)
48ae6c13
RH
10844{
10845 tree type;
10846 int size;
10847
9771b263 10848 if (!params)
48ae6c13
RH
10849 {
10850 error ("too few arguments to function %qE", function);
10851 return 0;
10852 }
10853
9771b263 10854 type = TREE_TYPE ((*params)[0]);
6415bd5d
JM
10855 if (TREE_CODE (type) == ARRAY_TYPE)
10856 {
10857 /* Force array-to-pointer decay for C++. */
10858 gcc_assert (c_dialect_cxx());
10859 (*params)[0] = default_conversion ((*params)[0]);
10860 type = TREE_TYPE ((*params)[0]);
10861 }
48ae6c13
RH
10862 if (TREE_CODE (type) != POINTER_TYPE)
10863 goto incompatible;
10864
10865 type = TREE_TYPE (type);
10866 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10867 goto incompatible;
10868
ae7e9ddd 10869 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
a0274e3e 10870 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
48ae6c13
RH
10871 return size;
10872
10873 incompatible:
10874 error ("incompatible type for argument %d of %qE", 1, function);
10875 return 0;
10876}
10877
c22cacf3 10878/* A helper function for resolve_overloaded_builtin. Adds casts to
48ae6c13
RH
10879 PARAMS to make arguments match up with those of FUNCTION. Drops
10880 the variadic arguments at the end. Returns false if some error
10881 was encountered; true on success. */
10882
10883static bool
86951993 10884sync_resolve_params (location_t loc, tree orig_function, tree function,
9771b263 10885 vec<tree, va_gc> *params, bool orig_format)
48ae6c13 10886{
e19a18d4 10887 function_args_iterator iter;
48ae6c13 10888 tree ptype;
bbbbb16a 10889 unsigned int parmnum;
48ae6c13 10890
e19a18d4 10891 function_args_iter_init (&iter, TREE_TYPE (function));
48ae6c13
RH
10892 /* We've declared the implementation functions to use "volatile void *"
10893 as the pointer parameter, so we shouldn't get any complaints from the
10894 call to check_function_arguments what ever type the user used. */
e19a18d4 10895 function_args_iter_next (&iter);
9771b263 10896 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
267bac10 10897 ptype = TYPE_MAIN_VARIANT (ptype);
48ae6c13
RH
10898
10899 /* For the rest of the values, we need to cast these to FTYPE, so that we
10900 don't get warnings for passing pointer types, etc. */
bbbbb16a 10901 parmnum = 0;
e19a18d4 10902 while (1)
48ae6c13 10903 {
e19a18d4
NF
10904 tree val, arg_type;
10905
10906 arg_type = function_args_iter_cond (&iter);
10907 /* XXX void_type_node belies the abstraction. */
10908 if (arg_type == void_type_node)
10909 break;
48ae6c13 10910
bbbbb16a 10911 ++parmnum;
9771b263 10912 if (params->length () <= parmnum)
48ae6c13 10913 {
86951993 10914 error_at (loc, "too few arguments to function %qE", orig_function);
48ae6c13
RH
10915 return false;
10916 }
10917
e3793c6f
JJ
10918 /* Only convert parameters if arg_type is unsigned integer type with
10919 new format sync routines, i.e. don't attempt to convert pointer
10920 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10921 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10922 kinds). */
10923 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
86951993
AM
10924 {
10925 /* Ideally for the first conversion we'd use convert_for_assignment
10926 so that we get warnings for anything that doesn't match the pointer
10927 type. This isn't portable across the C and C++ front ends atm. */
9771b263 10928 val = (*params)[parmnum];
86951993
AM
10929 val = convert (ptype, val);
10930 val = convert (arg_type, val);
9771b263 10931 (*params)[parmnum] = val;
86951993 10932 }
48ae6c13 10933
e19a18d4 10934 function_args_iter_next (&iter);
48ae6c13
RH
10935 }
10936
86951993 10937 /* __atomic routines are not variadic. */
9771b263 10938 if (!orig_format && params->length () != parmnum + 1)
86951993
AM
10939 {
10940 error_at (loc, "too many arguments to function %qE", orig_function);
10941 return false;
10942 }
10943
48ae6c13
RH
10944 /* The definition of these primitives is variadic, with the remaining
10945 being "an optional list of variables protected by the memory barrier".
10946 No clue what that's supposed to mean, precisely, but we consider all
10947 call-clobbered variables to be protected so we're safe. */
9771b263 10948 params->truncate (parmnum + 1);
48ae6c13
RH
10949
10950 return true;
10951}
10952
c22cacf3 10953/* A helper function for resolve_overloaded_builtin. Adds a cast to
48ae6c13
RH
10954 RESULT to make it match the type of the first pointer argument in
10955 PARAMS. */
10956
10957static tree
86951993 10958sync_resolve_return (tree first_param, tree result, bool orig_format)
48ae6c13 10959{
bbbbb16a 10960 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
86951993 10961 tree rtype = TREE_TYPE (result);
99db1ef0 10962 ptype = TYPE_MAIN_VARIANT (ptype);
86951993
AM
10963
10964 /* New format doesn't require casting unless the types are the same size. */
10965 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10966 return convert (ptype, result);
10967 else
10968 return result;
10969}
10970
10971/* This function verifies the PARAMS to generic atomic FUNCTION.
10972 It returns the size if all the parameters are the same size, otherwise
10973 0 is returned if the parameters are invalid. */
10974
10975static int
9771b263
DN
10976get_atomic_generic_size (location_t loc, tree function,
10977 vec<tree, va_gc> *params)
86951993
AM
10978{
10979 unsigned int n_param;
10980 unsigned int n_model;
10981 unsigned int x;
10982 int size_0;
10983 tree type_0;
10984
10985 /* Determine the parameter makeup. */
10986 switch (DECL_FUNCTION_CODE (function))
10987 {
10988 case BUILT_IN_ATOMIC_EXCHANGE:
10989 n_param = 4;
10990 n_model = 1;
10991 break;
10992 case BUILT_IN_ATOMIC_LOAD:
10993 case BUILT_IN_ATOMIC_STORE:
10994 n_param = 3;
10995 n_model = 1;
10996 break;
10997 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10998 n_param = 6;
10999 n_model = 2;
11000 break;
11001 default:
c466c4ff 11002 gcc_unreachable ();
86951993
AM
11003 }
11004
9771b263 11005 if (vec_safe_length (params) != n_param)
86951993
AM
11006 {
11007 error_at (loc, "incorrect number of arguments to function %qE", function);
11008 return 0;
11009 }
11010
11011 /* Get type of first parameter, and determine its size. */
9771b263 11012 type_0 = TREE_TYPE ((*params)[0]);
6415bd5d
JM
11013 if (TREE_CODE (type_0) == ARRAY_TYPE)
11014 {
11015 /* Force array-to-pointer decay for C++. */
11016 gcc_assert (c_dialect_cxx());
11017 (*params)[0] = default_conversion ((*params)[0]);
11018 type_0 = TREE_TYPE ((*params)[0]);
11019 }
c466c4ff
AM
11020 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11021 {
11022 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11023 function);
11024 return 0;
11025 }
11026
11027 /* Types must be compile time constant sizes. */
11028 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
86951993 11029 {
c466c4ff
AM
11030 error_at (loc,
11031 "argument 1 of %qE must be a pointer to a constant size type",
11032 function);
86951993
AM
11033 return 0;
11034 }
c466c4ff 11035
ae7e9ddd 11036 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
86951993 11037
c466c4ff
AM
11038 /* Zero size objects are not allowed. */
11039 if (size_0 == 0)
11040 {
11041 error_at (loc,
11042 "argument 1 of %qE must be a pointer to a nonzero size object",
11043 function);
11044 return 0;
11045 }
11046
86951993
AM
11047 /* Check each other parameter is a pointer and the same size. */
11048 for (x = 0; x < n_param - n_model; x++)
11049 {
11050 int size;
9771b263 11051 tree type = TREE_TYPE ((*params)[x]);
688010ba 11052 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
86951993
AM
11053 if (n_param == 6 && x == 3)
11054 continue;
11055 if (!POINTER_TYPE_P (type))
11056 {
11057 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11058 function);
11059 return 0;
11060 }
7b56b2f8
MP
11061 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11062 size = type_size ? tree_to_uhwi (type_size) : 0;
86951993
AM
11063 if (size != size_0)
11064 {
11065 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11066 function);
11067 return 0;
11068 }
11069 }
11070
11071 /* Check memory model parameters for validity. */
11072 for (x = n_param - n_model ; x < n_param; x++)
11073 {
9771b263 11074 tree p = (*params)[x];
86951993
AM
11075 if (TREE_CODE (p) == INTEGER_CST)
11076 {
ae7e9ddd 11077 int i = tree_to_uhwi (p);
46b35980 11078 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
86951993
AM
11079 {
11080 warning_at (loc, OPT_Winvalid_memory_model,
11081 "invalid memory model argument %d of %qE", x + 1,
11082 function);
86951993
AM
11083 }
11084 }
11085 else
11086 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11087 {
11088 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11089 function);
11090 return 0;
11091 }
11092 }
11093
11094 return size_0;
11095}
11096
11097
11098/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11099 at the beginning of the parameter list PARAMS representing the size of the
11100 objects. This is to match the library ABI requirement. LOC is the location
11101 of the function call.
11102 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11103 returned to allow the external call to be constructed. */
11104
11105static tree
11106add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9771b263 11107 vec<tree, va_gc> *params)
86951993
AM
11108{
11109 tree size_node;
11110
11111 /* Insert a SIZE_T parameter as the first param. If there isn't
11112 enough space, allocate a new vector and recursively re-build with that. */
9771b263 11113 if (!params->space (1))
86951993
AM
11114 {
11115 unsigned int z, len;
9771b263 11116 vec<tree, va_gc> *v;
86951993
AM
11117 tree f;
11118
9771b263
DN
11119 len = params->length ();
11120 vec_alloc (v, len + 1);
8edbfaa6 11121 v->quick_push (build_int_cst (size_type_node, n));
86951993 11122 for (z = 0; z < len; z++)
9771b263 11123 v->quick_push ((*params)[z]);
81e5eca8 11124 f = build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 11125 vec_free (v);
86951993
AM
11126 return f;
11127 }
11128
11129 /* Add the size parameter and leave as a function call for processing. */
11130 size_node = build_int_cst (size_type_node, n);
9771b263 11131 params->quick_insert (0, size_node);
86951993
AM
11132 return NULL_TREE;
11133}
11134
11135
6b28e197
JM
11136/* Return whether atomic operations for naturally aligned N-byte
11137 arguments are supported, whether inline or through libatomic. */
11138static bool
11139atomic_size_supported_p (int n)
11140{
11141 switch (n)
11142 {
11143 case 1:
11144 case 2:
11145 case 4:
11146 case 8:
11147 return true;
11148
11149 case 16:
11150 return targetm.scalar_mode_supported_p (TImode);
11151
11152 default:
11153 return false;
11154 }
11155}
11156
86951993
AM
11157/* This will process an __atomic_exchange function call, determine whether it
11158 needs to be mapped to the _N variation, or turned into a library call.
11159 LOC is the location of the builtin call.
11160 FUNCTION is the DECL that has been invoked;
11161 PARAMS is the argument list for the call. The return value is non-null
11162 TRUE is returned if it is translated into the proper format for a call to the
11163 external library, and NEW_RETURN is set the tree for that function.
11164 FALSE is returned if processing for the _N variation is required, and
11165 NEW_RETURN is set to the the return value the result is copied into. */
11166static bool
11167resolve_overloaded_atomic_exchange (location_t loc, tree function,
9771b263 11168 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11169{
11170 tree p0, p1, p2, p3;
11171 tree I_type, I_type_ptr;
11172 int n = get_atomic_generic_size (loc, function, params);
11173
c466c4ff
AM
11174 /* Size of 0 is an error condition. */
11175 if (n == 0)
11176 {
11177 *new_return = error_mark_node;
11178 return true;
11179 }
11180
86951993 11181 /* If not a lock-free size, change to the library generic format. */
6b28e197 11182 if (!atomic_size_supported_p (n))
86951993
AM
11183 {
11184 *new_return = add_atomic_size_parameter (n, loc, function, params);
11185 return true;
11186 }
11187
11188 /* Otherwise there is a lockfree match, transform the call from:
11189 void fn(T* mem, T* desired, T* return, model)
11190 into
11191 *return = (T) (fn (In* mem, (In) *desired, model)) */
11192
9771b263
DN
11193 p0 = (*params)[0];
11194 p1 = (*params)[1];
11195 p2 = (*params)[2];
11196 p3 = (*params)[3];
86951993
AM
11197
11198 /* Create pointer to appropriate size. */
11199 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11200 I_type_ptr = build_pointer_type (I_type);
11201
11202 /* Convert object pointer to required type. */
11203 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11204 (*params)[0] = p0;
86951993
AM
11205 /* Convert new value to required type, and dereference it. */
11206 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11207 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11208 (*params)[1] = p1;
86951993
AM
11209
11210 /* Move memory model to the 3rd position, and end param list. */
9771b263
DN
11211 (*params)[2] = p3;
11212 params->truncate (3);
86951993
AM
11213
11214 /* Convert return pointer and dereference it for later assignment. */
11215 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11216
11217 return false;
48ae6c13
RH
11218}
11219
86951993
AM
11220
11221/* This will process an __atomic_compare_exchange function call, determine
11222 whether it needs to be mapped to the _N variation, or turned into a lib call.
11223 LOC is the location of the builtin call.
11224 FUNCTION is the DECL that has been invoked;
11225 PARAMS is the argument list for the call. The return value is non-null
11226 TRUE is returned if it is translated into the proper format for a call to the
11227 external library, and NEW_RETURN is set the tree for that function.
11228 FALSE is returned if processing for the _N variation is required. */
11229
11230static bool
11231resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9771b263 11232 vec<tree, va_gc> *params,
86951993
AM
11233 tree *new_return)
11234{
11235 tree p0, p1, p2;
11236 tree I_type, I_type_ptr;
11237 int n = get_atomic_generic_size (loc, function, params);
11238
c466c4ff
AM
11239 /* Size of 0 is an error condition. */
11240 if (n == 0)
11241 {
11242 *new_return = error_mark_node;
11243 return true;
11244 }
11245
86951993 11246 /* If not a lock-free size, change to the library generic format. */
6b28e197 11247 if (!atomic_size_supported_p (n))
86951993
AM
11248 {
11249 /* The library generic format does not have the weak parameter, so
11250 remove it from the param list. Since a parameter has been removed,
11251 we can be sure that there is room for the SIZE_T parameter, meaning
11252 there will not be a recursive rebuilding of the parameter list, so
11253 there is no danger this will be done twice. */
11254 if (n > 0)
11255 {
9771b263
DN
11256 (*params)[3] = (*params)[4];
11257 (*params)[4] = (*params)[5];
11258 params->truncate (5);
86951993
AM
11259 }
11260 *new_return = add_atomic_size_parameter (n, loc, function, params);
11261 return true;
11262 }
11263
11264 /* Otherwise, there is a match, so the call needs to be transformed from:
11265 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11266 into
11267 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11268
9771b263
DN
11269 p0 = (*params)[0];
11270 p1 = (*params)[1];
11271 p2 = (*params)[2];
86951993
AM
11272
11273 /* Create pointer to appropriate size. */
11274 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11275 I_type_ptr = build_pointer_type (I_type);
11276
11277 /* Convert object pointer to required type. */
11278 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11279 (*params)[0] = p0;
86951993
AM
11280
11281 /* Convert expected pointer to required type. */
11282 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9771b263 11283 (*params)[1] = p1;
86951993
AM
11284
11285 /* Convert desired value to required type, and dereference it. */
11286 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11287 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9771b263 11288 (*params)[2] = p2;
86951993
AM
11289
11290 /* The rest of the parameters are fine. NULL means no special return value
11291 processing.*/
11292 *new_return = NULL;
11293 return false;
11294}
11295
11296
11297/* This will process an __atomic_load function call, determine whether it
11298 needs to be mapped to the _N variation, or turned into a library call.
11299 LOC is the location of the builtin call.
11300 FUNCTION is the DECL that has been invoked;
11301 PARAMS is the argument list for the call. The return value is non-null
11302 TRUE is returned if it is translated into the proper format for a call to the
11303 external library, and NEW_RETURN is set the tree for that function.
11304 FALSE is returned if processing for the _N variation is required, and
11305 NEW_RETURN is set to the the return value the result is copied into. */
11306
11307static bool
11308resolve_overloaded_atomic_load (location_t loc, tree function,
9771b263 11309 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11310{
11311 tree p0, p1, p2;
11312 tree I_type, I_type_ptr;
11313 int n = get_atomic_generic_size (loc, function, params);
11314
c466c4ff
AM
11315 /* Size of 0 is an error condition. */
11316 if (n == 0)
11317 {
11318 *new_return = error_mark_node;
11319 return true;
11320 }
11321
86951993 11322 /* If not a lock-free size, change to the library generic format. */
6b28e197 11323 if (!atomic_size_supported_p (n))
86951993
AM
11324 {
11325 *new_return = add_atomic_size_parameter (n, loc, function, params);
11326 return true;
11327 }
11328
11329 /* Otherwise, there is a match, so the call needs to be transformed from:
11330 void fn(T* mem, T* return, model)
11331 into
11332 *return = (T) (fn ((In *) mem, model)) */
11333
9771b263
DN
11334 p0 = (*params)[0];
11335 p1 = (*params)[1];
11336 p2 = (*params)[2];
86951993
AM
11337
11338 /* Create pointer to appropriate size. */
11339 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11340 I_type_ptr = build_pointer_type (I_type);
11341
11342 /* Convert object pointer to required type. */
11343 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11344 (*params)[0] = p0;
86951993
AM
11345
11346 /* Move memory model to the 2nd position, and end param list. */
9771b263
DN
11347 (*params)[1] = p2;
11348 params->truncate (2);
86951993
AM
11349
11350 /* Convert return pointer and dereference it for later assignment. */
11351 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11352
11353 return false;
11354}
11355
11356
11357/* This will process an __atomic_store function call, determine whether it
11358 needs to be mapped to the _N variation, or turned into a library call.
11359 LOC is the location of the builtin call.
11360 FUNCTION is the DECL that has been invoked;
11361 PARAMS is the argument list for the call. The return value is non-null
11362 TRUE is returned if it is translated into the proper format for a call to the
11363 external library, and NEW_RETURN is set the tree for that function.
11364 FALSE is returned if processing for the _N variation is required, and
11365 NEW_RETURN is set to the the return value the result is copied into. */
11366
11367static bool
11368resolve_overloaded_atomic_store (location_t loc, tree function,
9771b263 11369 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11370{
11371 tree p0, p1;
11372 tree I_type, I_type_ptr;
11373 int n = get_atomic_generic_size (loc, function, params);
11374
c466c4ff
AM
11375 /* Size of 0 is an error condition. */
11376 if (n == 0)
11377 {
11378 *new_return = error_mark_node;
11379 return true;
11380 }
11381
86951993 11382 /* If not a lock-free size, change to the library generic format. */
6b28e197 11383 if (!atomic_size_supported_p (n))
86951993
AM
11384 {
11385 *new_return = add_atomic_size_parameter (n, loc, function, params);
11386 return true;
11387 }
11388
11389 /* Otherwise, there is a match, so the call needs to be transformed from:
11390 void fn(T* mem, T* value, model)
11391 into
11392 fn ((In *) mem, (In) *value, model) */
11393
9771b263
DN
11394 p0 = (*params)[0];
11395 p1 = (*params)[1];
86951993
AM
11396
11397 /* Create pointer to appropriate size. */
11398 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11399 I_type_ptr = build_pointer_type (I_type);
11400
11401 /* Convert object pointer to required type. */
11402 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11403 (*params)[0] = p0;
86951993
AM
11404
11405 /* Convert new value to required type, and dereference it. */
11406 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11407 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11408 (*params)[1] = p1;
86951993
AM
11409
11410 /* The memory model is in the right spot already. Return is void. */
11411 *new_return = NULL_TREE;
11412
11413 return false;
11414}
11415
11416
48ae6c13
RH
11417/* Some builtin functions are placeholders for other expressions. This
11418 function should be called immediately after parsing the call expression
11419 before surrounding code has committed to the type of the expression.
11420
c2255bc4
AH
11421 LOC is the location of the builtin call.
11422
48ae6c13
RH
11423 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11424 PARAMS is the argument list for the call. The return value is non-null
11425 when expansion is complete, and null if normal processing should
11426 continue. */
11427
11428tree
9771b263
DN
11429resolve_overloaded_builtin (location_t loc, tree function,
11430 vec<tree, va_gc> *params)
48ae6c13
RH
11431{
11432 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
86951993
AM
11433 bool orig_format = true;
11434 tree new_return = NULL_TREE;
11435
58646b77
PB
11436 switch (DECL_BUILT_IN_CLASS (function))
11437 {
11438 case BUILT_IN_NORMAL:
11439 break;
11440 case BUILT_IN_MD:
11441 if (targetm.resolve_overloaded_builtin)
c2255bc4 11442 return targetm.resolve_overloaded_builtin (loc, function, params);
58646b77 11443 else
c22cacf3 11444 return NULL_TREE;
58646b77
PB
11445 default:
11446 return NULL_TREE;
11447 }
c22cacf3 11448
58646b77 11449 /* Handle BUILT_IN_NORMAL here. */
48ae6c13
RH
11450 switch (orig_code)
11451 {
86951993
AM
11452 case BUILT_IN_ATOMIC_EXCHANGE:
11453 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11454 case BUILT_IN_ATOMIC_LOAD:
11455 case BUILT_IN_ATOMIC_STORE:
11456 {
11457 /* Handle these 4 together so that they can fall through to the next
11458 case if the call is transformed to an _N variant. */
11459 switch (orig_code)
11460 {
11461 case BUILT_IN_ATOMIC_EXCHANGE:
11462 {
11463 if (resolve_overloaded_atomic_exchange (loc, function, params,
11464 &new_return))
11465 return new_return;
11466 /* Change to the _N variant. */
11467 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11468 break;
11469 }
11470
11471 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11472 {
11473 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11474 params,
11475 &new_return))
11476 return new_return;
11477 /* Change to the _N variant. */
11478 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11479 break;
11480 }
11481 case BUILT_IN_ATOMIC_LOAD:
11482 {
11483 if (resolve_overloaded_atomic_load (loc, function, params,
11484 &new_return))
11485 return new_return;
11486 /* Change to the _N variant. */
11487 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11488 break;
11489 }
11490 case BUILT_IN_ATOMIC_STORE:
11491 {
11492 if (resolve_overloaded_atomic_store (loc, function, params,
11493 &new_return))
11494 return new_return;
11495 /* Change to the _N variant. */
11496 orig_code = BUILT_IN_ATOMIC_STORE_N;
11497 break;
11498 }
11499 default:
11500 gcc_unreachable ();
11501 }
11502 /* Fallthrough to the normal processing. */
11503 }
11504 case BUILT_IN_ATOMIC_EXCHANGE_N:
11505 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11506 case BUILT_IN_ATOMIC_LOAD_N:
11507 case BUILT_IN_ATOMIC_STORE_N:
11508 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11509 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11510 case BUILT_IN_ATOMIC_AND_FETCH_N:
11511 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11512 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11513 case BUILT_IN_ATOMIC_OR_FETCH_N:
11514 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11515 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11516 case BUILT_IN_ATOMIC_FETCH_AND_N:
11517 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11518 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11519 case BUILT_IN_ATOMIC_FETCH_OR_N:
11520 {
11521 orig_format = false;
11522 /* Fallthru for parameter processing. */
11523 }
e0a8ecf2
AM
11524 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11525 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11526 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11527 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11528 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11529 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11530 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11531 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11532 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11533 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11534 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11535 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11536 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11537 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11538 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11539 case BUILT_IN_SYNC_LOCK_RELEASE_N:
48ae6c13
RH
11540 {
11541 int n = sync_resolve_size (function, params);
bbbbb16a 11542 tree new_function, first_param, result;
e79983f4 11543 enum built_in_function fncode;
48ae6c13
RH
11544
11545 if (n == 0)
11546 return error_mark_node;
11547
e79983f4
MM
11548 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11549 new_function = builtin_decl_explicit (fncode);
86951993
AM
11550 if (!sync_resolve_params (loc, function, new_function, params,
11551 orig_format))
48ae6c13
RH
11552 return error_mark_node;
11553
9771b263 11554 first_param = (*params)[0];
81e5eca8
MP
11555 result = build_function_call_vec (loc, vNULL, new_function, params,
11556 NULL);
86951993
AM
11557 if (result == error_mark_node)
11558 return result;
e0a8ecf2 11559 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
86951993
AM
11560 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11561 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11562 result = sync_resolve_return (first_param, result, orig_format);
48ae6c13 11563
86951993
AM
11564 /* If new_return is set, assign function to that expr and cast the
11565 result to void since the generic interface returned void. */
11566 if (new_return)
11567 {
11568 /* Cast function result from I{1,2,4,8,16} to the required type. */
11569 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11570 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11571 result);
11572 TREE_SIDE_EFFECTS (result) = 1;
11573 protected_set_expr_location (result, loc);
11574 result = convert (void_type_node, result);
11575 }
48ae6c13
RH
11576 return result;
11577 }
11578
11579 default:
58646b77 11580 return NULL_TREE;
48ae6c13
RH
11581 }
11582}
11583
0af94e6f
JR
11584/* vector_types_compatible_elements_p is used in type checks of vectors
11585 values used as operands of binary operators. Where it returns true, and
11586 the other checks of the caller succeed (being vector types in he first
11587 place, and matching number of elements), we can just treat the types
11588 as essentially the same.
11589 Contrast with vector_targets_convertible_p, which is used for vector
11590 pointer types, and vector_types_convertible_p, which will allow
11591 language-specific matches under the control of flag_lax_vector_conversions,
11592 and might still require a conversion. */
11593/* True if vector types T1 and T2 can be inputs to the same binary
11594 operator without conversion.
11595 We don't check the overall vector size here because some of our callers
11596 want to give different error messages when the vectors are compatible
11597 except for the element count. */
11598
5bed876a 11599bool
0af94e6f 11600vector_types_compatible_elements_p (tree t1, tree t2)
5bed876a 11601{
0af94e6f
JR
11602 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11603 t1 = TREE_TYPE (t1);
11604 t2 = TREE_TYPE (t2);
11605
5bed876a
AH
11606 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11607
ab22c1fa
CF
11608 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11609 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11610 || c2 == FIXED_POINT_TYPE));
5bed876a 11611
0af94e6f
JR
11612 t1 = c_common_signed_type (t1);
11613 t2 = c_common_signed_type (t2);
5bed876a
AH
11614 /* Equality works here because c_common_signed_type uses
11615 TYPE_MAIN_VARIANT. */
0af94e6f
JR
11616 if (t1 == t2)
11617 return true;
11618 if (opaque && c1 == c2
11619 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11620 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11621 return true;
11622 return false;
5bed876a
AH
11623}
11624
104f8784
KG
11625/* Check for missing format attributes on function pointers. LTYPE is
11626 the new type or left-hand side type. RTYPE is the old type or
11627 right-hand side type. Returns TRUE if LTYPE is missing the desired
11628 attribute. */
11629
11630bool
11631check_missing_format_attribute (tree ltype, tree rtype)
11632{
11633 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11634 tree ra;
11635
11636 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11637 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11638 break;
11639 if (ra)
11640 {
11641 tree la;
11642 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11643 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11644 break;
11645 return !la;
11646 }
11647 else
11648 return false;
11649}
11650
ff6b6641
GDR
11651/* Subscripting with type char is likely to lose on a machine where
11652 chars are signed. So warn on any machine, but optionally. Don't
11653 warn for unsigned char since that type is safe. Don't warn for
11654 signed char because anyone who uses that must have done so
11655 deliberately. Furthermore, we reduce the false positive load by
11656 warning only for non-constant value of type char. */
11657
11658void
5bd012f8 11659warn_array_subscript_with_type_char (location_t loc, tree index)
ff6b6641
GDR
11660{
11661 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11662 && TREE_CODE (index) != INTEGER_CST)
5bd012f8
MP
11663 warning_at (loc, OPT_Wchar_subscripts,
11664 "array subscript has type %<char%>");
ff6b6641
GDR
11665}
11666
2a67bec2
ILT
11667/* Implement -Wparentheses for the unexpected C precedence rules, to
11668 cover cases like x + y << z which readers are likely to
11669 misinterpret. We have seen an expression in which CODE is a binary
100d537d
MLI
11670 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11671 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11672 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11673 expression was not formed using a binary or unary operator, or it
11674 was enclosed in parentheses. */
2a67bec2
ILT
11675
11676void
5d9de0d0 11677warn_about_parentheses (location_t loc, enum tree_code code,
fb3e178a 11678 enum tree_code code_left, tree arg_left,
100d537d 11679 enum tree_code code_right, tree arg_right)
2a67bec2
ILT
11680{
11681 if (!warn_parentheses)
11682 return;
11683
100d537d
MLI
11684 /* This macro tests that the expression ARG with original tree code
11685 CODE appears to be a boolean expression. or the result of folding a
11686 boolean expression. */
11687#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11688 (truth_value_p (TREE_CODE (ARG)) \
11689 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11690 /* Folding may create 0 or 1 integers from other expressions. */ \
11691 || ((CODE) != INTEGER_CST \
11692 && (integer_onep (ARG) || integer_zerop (ARG))))
11693
b8698a0f 11694 switch (code)
2a67bec2 11695 {
100d537d 11696 case LSHIFT_EXPR:
5d9de0d0
PC
11697 if (code_left == PLUS_EXPR)
11698 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11699 "suggest parentheses around %<+%> inside %<<<%>");
11700 else if (code_right == PLUS_EXPR)
11701 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11702 "suggest parentheses around %<+%> inside %<<<%>");
11703 else if (code_left == MINUS_EXPR)
11704 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11705 "suggest parentheses around %<-%> inside %<<<%>");
11706 else if (code_right == MINUS_EXPR)
11707 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11708 "suggest parentheses around %<-%> inside %<<<%>");
100d537d 11709 return;
2a67bec2 11710
100d537d 11711 case RSHIFT_EXPR:
5d9de0d0
PC
11712 if (code_left == PLUS_EXPR)
11713 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11714 "suggest parentheses around %<+%> inside %<>>%>");
11715 else if (code_right == PLUS_EXPR)
11716 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11717 "suggest parentheses around %<+%> inside %<>>%>");
11718 else if (code_left == MINUS_EXPR)
11719 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11720 "suggest parentheses around %<-%> inside %<>>%>");
11721 else if (code_right == MINUS_EXPR)
11722 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11723 "suggest parentheses around %<-%> inside %<>>%>");
100d537d 11724 return;
2a67bec2 11725
100d537d 11726 case TRUTH_ORIF_EXPR:
5d9de0d0
PC
11727 if (code_left == TRUTH_ANDIF_EXPR)
11728 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11729 "suggest parentheses around %<&&%> within %<||%>");
11730 else if (code_right == TRUTH_ANDIF_EXPR)
11731 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11732 "suggest parentheses around %<&&%> within %<||%>");
100d537d
MLI
11733 return;
11734
11735 case BIT_IOR_EXPR:
2a67bec2 11736 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
5d9de0d0
PC
11737 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11738 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11739 "suggest parentheses around arithmetic in operand of %<|%>");
11740 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11741 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11742 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11743 "suggest parentheses around arithmetic in operand of %<|%>");
2a67bec2 11744 /* Check cases like x|y==z */
5d9de0d0
PC
11745 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11746 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11747 "suggest parentheses around comparison in operand of %<|%>");
11748 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11749 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11750 "suggest parentheses around comparison in operand of %<|%>");
11751 /* Check cases like !x | y */
11752 else if (code_left == TRUTH_NOT_EXPR
11753 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11754 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11755 "suggest parentheses around operand of "
11756 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
100d537d 11757 return;
2a67bec2 11758
100d537d 11759 case BIT_XOR_EXPR:
2a67bec2 11760 if (code_left == BIT_AND_EXPR
5d9de0d0
PC
11761 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11762 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11763 "suggest parentheses around arithmetic in operand of %<^%>");
11764 else if (code_right == BIT_AND_EXPR
11765 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11766 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11767 "suggest parentheses around arithmetic in operand of %<^%>");
2a67bec2 11768 /* Check cases like x^y==z */
5d9de0d0
PC
11769 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11770 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11771 "suggest parentheses around comparison in operand of %<^%>");
11772 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11773 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11774 "suggest parentheses around comparison in operand of %<^%>");
11775 return;
2a67bec2 11776
100d537d 11777 case BIT_AND_EXPR:
5d9de0d0
PC
11778 if (code_left == PLUS_EXPR)
11779 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11780 "suggest parentheses around %<+%> in operand of %<&%>");
11781 else if (code_right == PLUS_EXPR)
11782 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11783 "suggest parentheses around %<+%> in operand of %<&%>");
5d9de0d0
PC
11784 else if (code_left == MINUS_EXPR)
11785 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11786 "suggest parentheses around %<-%> in operand of %<&%>");
11787 else if (code_right == MINUS_EXPR)
11788 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11789 "suggest parentheses around %<-%> in operand of %<&%>");
2a67bec2 11790 /* Check cases like x&y==z */
5d9de0d0
PC
11791 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11792 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11793 "suggest parentheses around comparison in operand of %<&%>");
11794 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11795 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11796 "suggest parentheses around comparison in operand of %<&%>");
11797 /* Check cases like !x & y */
11798 else if (code_left == TRUTH_NOT_EXPR
11799 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11800 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11801 "suggest parentheses around operand of "
11802 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
100d537d 11803 return;
2a67bec2 11804
100d537d 11805 case EQ_EXPR:
5d9de0d0
PC
11806 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11807 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11808 "suggest parentheses around comparison in operand of %<==%>");
11809 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11810 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11811 "suggest parentheses around comparison in operand of %<==%>");
11812 return;
11813 case NE_EXPR:
5d9de0d0
PC
11814 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11815 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11816 "suggest parentheses around comparison in operand of %<!=%>");
11817 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11818 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11819 "suggest parentheses around comparison in operand of %<!=%>");
11820 return;
11821
11822 default:
5d9de0d0
PC
11823 if (TREE_CODE_CLASS (code) == tcc_comparison)
11824 {
11825 if (TREE_CODE_CLASS (code_left) == tcc_comparison
fb3e178a
JJ
11826 && code_left != NE_EXPR && code_left != EQ_EXPR
11827 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
5d9de0d0
PC
11828 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11829 "comparisons like %<X<=Y<=Z%> do not "
11830 "have their mathematical meaning");
11831 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
fb3e178a 11832 && code_right != NE_EXPR && code_right != EQ_EXPR
5d9de0d0
PC
11833 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11834 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11835 "comparisons like %<X<=Y<=Z%> do not "
11836 "have their mathematical meaning");
11837 }
100d537d 11838 return;
e7917d06 11839 }
100d537d 11840#undef NOT_A_BOOLEAN_EXPR_P
2a67bec2
ILT
11841}
11842
c616e51b
MLI
11843/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11844
11845void
11846warn_for_unused_label (tree label)
11847{
11848 if (!TREE_USED (label))
11849 {
11850 if (DECL_INITIAL (label))
11851 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11852 else
11853 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11854 }
11855}
ff6b6641 11856
c9f9eb5d
AH
11857/* Warn for division by zero according to the value of DIVISOR. LOC
11858 is the location of the division operator. */
2e9cb75e
MLI
11859
11860void
c9f9eb5d 11861warn_for_div_by_zero (location_t loc, tree divisor)
2e9cb75e 11862{
ab22c1fa
CF
11863 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11864 about division by zero. Do not issue a warning if DIVISOR has a
2e9cb75e
MLI
11865 floating-point type, since we consider 0.0/0.0 a valid way of
11866 generating a NaN. */
7d882b83 11867 if (c_inhibit_evaluation_warnings == 0
ab22c1fa 11868 && (integer_zerop (divisor) || fixed_zerop (divisor)))
c9f9eb5d 11869 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
2e9cb75e
MLI
11870}
11871
2d12797c
MLI
11872/* Subroutine of build_binary_op. Give warnings for comparisons
11873 between signed and unsigned quantities that may fail. Do the
11874 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11875 so that casts will be considered, but default promotions won't
ba47d38d
AH
11876 be.
11877
11878 LOCATION is the location of the comparison operator.
2d12797c
MLI
11879
11880 The arguments of this function map directly to local variables
11881 of build_binary_op. */
11882
b8698a0f 11883void
ba47d38d 11884warn_for_sign_compare (location_t location,
b8698a0f
L
11885 tree orig_op0, tree orig_op1,
11886 tree op0, tree op1,
2d12797c
MLI
11887 tree result_type, enum tree_code resultcode)
11888{
11889 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11890 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11891 int unsignedp0, unsignedp1;
b8698a0f 11892
2d12797c
MLI
11893 /* In C++, check for comparison of different enum types. */
11894 if (c_dialect_cxx()
11895 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11896 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11897 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
ead51d36 11898 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
2d12797c 11899 {
ba47d38d
AH
11900 warning_at (location,
11901 OPT_Wsign_compare, "comparison between types %qT and %qT",
11902 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
2d12797c
MLI
11903 }
11904
11905 /* Do not warn if the comparison is being done in a signed type,
11906 since the signed type will only be chosen if it can represent
11907 all the values of the unsigned type. */
11908 if (!TYPE_UNSIGNED (result_type))
11909 /* OK */;
11910 /* Do not warn if both operands are unsigned. */
11911 else if (op0_signed == op1_signed)
11912 /* OK */;
11913 else
11914 {
ead51d36 11915 tree sop, uop, base_type;
2d12797c 11916 bool ovf;
ead51d36 11917
2d12797c
MLI
11918 if (op0_signed)
11919 sop = orig_op0, uop = orig_op1;
b8698a0f 11920 else
2d12797c
MLI
11921 sop = orig_op1, uop = orig_op0;
11922
b8698a0f 11923 STRIP_TYPE_NOPS (sop);
2d12797c 11924 STRIP_TYPE_NOPS (uop);
ead51d36
JJ
11925 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11926 ? TREE_TYPE (result_type) : result_type);
2d12797c
MLI
11927
11928 /* Do not warn if the signed quantity is an unsuffixed integer
11929 literal (or some static constant expression involving such
11930 literals or a conditional expression involving such literals)
11931 and it is non-negative. */
11932 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11933 /* OK */;
11934 /* Do not warn if the comparison is an equality operation, the
11935 unsigned quantity is an integral constant, and it would fit
11936 in the result if the result were signed. */
11937 else if (TREE_CODE (uop) == INTEGER_CST
11938 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
ead51d36 11939 && int_fits_type_p (uop, c_common_signed_type (base_type)))
2d12797c
MLI
11940 /* OK */;
11941 /* In C, do not warn if the unsigned quantity is an enumeration
11942 constant and its maximum value would fit in the result if the
11943 result were signed. */
11944 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11945 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11946 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
ead51d36 11947 c_common_signed_type (base_type)))
2d12797c 11948 /* OK */;
b8698a0f 11949 else
ba47d38d 11950 warning_at (location,
b8698a0f 11951 OPT_Wsign_compare,
ba47d38d 11952 "comparison between signed and unsigned integer expressions");
2d12797c 11953 }
b8698a0f 11954
2d12797c
MLI
11955 /* Warn if two unsigned values are being compared in a size larger
11956 than their original size, and one (and only one) is the result of
11957 a `~' operator. This comparison will always fail.
b8698a0f 11958
2d12797c
MLI
11959 Also warn if one operand is a constant, and the constant does not
11960 have all bits set that are set in the ~ operand when it is
11961 extended. */
11962
828fb3ba
JM
11963 op0 = c_common_get_narrower (op0, &unsignedp0);
11964 op1 = c_common_get_narrower (op1, &unsignedp1);
b8698a0f 11965
2d12797c
MLI
11966 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11967 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11968 {
11969 if (TREE_CODE (op0) == BIT_NOT_EXPR)
828fb3ba 11970 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2d12797c 11971 if (TREE_CODE (op1) == BIT_NOT_EXPR)
828fb3ba 11972 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2d12797c 11973
9541ffee 11974 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2d12797c
MLI
11975 {
11976 tree primop;
11977 HOST_WIDE_INT constant, mask;
11978 int unsignedp;
11979 unsigned int bits;
b8698a0f 11980
9541ffee 11981 if (tree_fits_shwi_p (op0))
2d12797c
MLI
11982 {
11983 primop = op1;
11984 unsignedp = unsignedp1;
9439e9a1 11985 constant = tree_to_shwi (op0);
2d12797c
MLI
11986 }
11987 else
11988 {
11989 primop = op0;
11990 unsignedp = unsignedp0;
9439e9a1 11991 constant = tree_to_shwi (op1);
2d12797c 11992 }
b8698a0f 11993
2d12797c
MLI
11994 bits = TYPE_PRECISION (TREE_TYPE (primop));
11995 if (bits < TYPE_PRECISION (result_type)
11996 && bits < HOST_BITS_PER_LONG && unsignedp)
11997 {
11998 mask = (~ (HOST_WIDE_INT) 0) << bits;
11999 if ((mask & constant) != mask)
12000 {
12001 if (constant == 0)
0a756a3f
MP
12002 warning_at (location, OPT_Wsign_compare,
12003 "promoted ~unsigned is always non-zero");
2d12797c 12004 else
b8698a0f 12005 warning_at (location, OPT_Wsign_compare,
ba47d38d 12006 "comparison of promoted ~unsigned with constant");
2d12797c
MLI
12007 }
12008 }
12009 }
12010 else if (unsignedp0 && unsignedp1
12011 && (TYPE_PRECISION (TREE_TYPE (op0))
12012 < TYPE_PRECISION (result_type))
12013 && (TYPE_PRECISION (TREE_TYPE (op1))
12014 < TYPE_PRECISION (result_type)))
ba47d38d 12015 warning_at (location, OPT_Wsign_compare,
2d12797c
MLI
12016 "comparison of promoted ~unsigned with unsigned");
12017 }
12018}
12019
c5ee1358
MM
12020/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12021 type via c_common_type. If -Wdouble-promotion is in use, and the
12022 conditions for warning have been met, issue a warning. GMSGID is
12023 the warning message. It must have two %T specifiers for the type
12024 that was converted (generally "float") and the type to which it was
12025 converted (generally "double), respectively. LOC is the location
12026 to which the awrning should refer. */
12027
12028void
12029do_warn_double_promotion (tree result_type, tree type1, tree type2,
12030 const char *gmsgid, location_t loc)
12031{
12032 tree source_type;
12033
12034 if (!warn_double_promotion)
12035 return;
12036 /* If the conversion will not occur at run-time, there is no need to
12037 warn about it. */
12038 if (c_inhibit_evaluation_warnings)
12039 return;
12040 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12041 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12042 return;
12043 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12044 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12045 source_type = type1;
12046 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12047 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12048 source_type = type2;
12049 else
12050 return;
12051 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12052}
12053
d0940d56
DS
12054/* Setup a TYPE_DECL node as a typedef representation.
12055
12056 X is a TYPE_DECL for a typedef statement. Create a brand new
12057 ..._TYPE node (which will be just a variant of the existing
12058 ..._TYPE node with identical properties) and then install X
12059 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12060
12061 The whole point here is to end up with a situation where each
12062 and every ..._TYPE node the compiler creates will be uniquely
12063 associated with AT MOST one node representing a typedef name.
12064 This way, even though the compiler substitutes corresponding
12065 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12066 early on, later parts of the compiler can always do the reverse
12067 translation and get back the corresponding typedef name. For
12068 example, given:
12069
12070 typedef struct S MY_TYPE;
12071 MY_TYPE object;
12072
12073 Later parts of the compiler might only know that `object' was of
12074 type `struct S' if it were not for code just below. With this
12075 code however, later parts of the compiler see something like:
12076
12077 struct S' == struct S
12078 typedef struct S' MY_TYPE;
12079 struct S' object;
12080
12081 And they can then deduce (from the node for type struct S') that
12082 the original object declaration was:
12083
12084 MY_TYPE object;
12085
12086 Being able to do this is important for proper support of protoize,
12087 and also for generating precise symbolic debugging information
12088 which takes full account of the programmer's (typedef) vocabulary.
12089
12090 Obviously, we don't want to generate a duplicate ..._TYPE node if
12091 the TYPE_DECL node that we are now processing really represents a
12092 standard built-in type. */
12093
12094void
12095set_underlying_type (tree x)
12096{
12097 if (x == error_mark_node)
12098 return;
12099 if (DECL_IS_BUILTIN (x))
12100 {
12101 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12102 TYPE_NAME (TREE_TYPE (x)) = x;
12103 }
12104 else if (TREE_TYPE (x) != error_mark_node
12105 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12106 {
12107 tree tt = TREE_TYPE (x);
12108 DECL_ORIGINAL_TYPE (x) = tt;
12109 tt = build_variant_type_copy (tt);
12110 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12111 TYPE_NAME (tt) = x;
12112 TREE_USED (tt) = TREE_USED (x);
12113 TREE_TYPE (x) = tt;
12114 }
12115}
12116
b646ba3f
DS
12117/* Record the types used by the current global variable declaration
12118 being parsed, so that we can decide later to emit their debug info.
12119 Those types are in types_used_by_cur_var_decl, and we are going to
12120 store them in the types_used_by_vars_hash hash table.
12121 DECL is the declaration of the global variable that has been parsed. */
12122
12123void
12124record_types_used_by_current_var_decl (tree decl)
12125{
12126 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12127
9771b263 12128 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
b646ba3f 12129 {
9771b263 12130 tree type = types_used_by_cur_var_decl->pop ();
bc87224e 12131 types_used_by_var_decl_insert (type, decl);
b646ba3f
DS
12132 }
12133}
12134
3797cb21
DS
12135/* If DECL is a typedef that is declared in the current function,
12136 record it for the purpose of -Wunused-local-typedefs. */
12137
12138void
12139record_locally_defined_typedef (tree decl)
12140{
12141 struct c_language_function *l;
12142
12143 if (!warn_unused_local_typedefs
12144 || cfun == NULL
12145 /* if this is not a locally defined typedef then we are not
12146 interested. */
12147 || !is_typedef_decl (decl)
12148 || !decl_function_context (decl))
12149 return;
12150
12151 l = (struct c_language_function *) cfun->language;
9771b263 12152 vec_safe_push (l->local_typedefs, decl);
3797cb21
DS
12153}
12154
12155/* If T is a TYPE_DECL declared locally, mark it as used. */
12156
12157void
12158maybe_record_typedef_use (tree t)
12159{
12160 if (!is_typedef_decl (t))
12161 return;
12162
12163 TREE_USED (t) = true;
12164}
12165
12166/* Warn if there are some unused locally defined typedefs in the
12167 current function. */
12168
12169void
12170maybe_warn_unused_local_typedefs (void)
12171{
12172 int i;
12173 tree decl;
12174 /* The number of times we have emitted -Wunused-local-typedefs
12175 warnings. If this is different from errorcount, that means some
12176 unrelated errors have been issued. In which case, we'll avoid
12177 emitting "unused-local-typedefs" warnings. */
12178 static int unused_local_typedefs_warn_count;
12179 struct c_language_function *l;
12180
12181 if (cfun == NULL)
12182 return;
12183
12184 if ((l = (struct c_language_function *) cfun->language) == NULL)
12185 return;
12186
12187 if (warn_unused_local_typedefs
12188 && errorcount == unused_local_typedefs_warn_count)
12189 {
9771b263 12190 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
3797cb21
DS
12191 if (!TREE_USED (decl))
12192 warning_at (DECL_SOURCE_LOCATION (decl),
12193 OPT_Wunused_local_typedefs,
12194 "typedef %qD locally defined but not used", decl);
12195 unused_local_typedefs_warn_count = errorcount;
12196 }
12197
9771b263 12198 vec_free (l->local_typedefs);
3797cb21
DS
12199}
12200
04159acf
MP
12201/* Warn about boolean expression compared with an integer value different
12202 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12203 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12204 are the operands of the comparison. The caller must ensure that
12205 either operand is a boolean expression. */
12206
12207void
12208maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12209 tree op1)
12210{
12211 if (TREE_CODE_CLASS (code) != tcc_comparison)
12212 return;
12213
12214 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12215 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12216 if (!cst)
12217 return;
12218
12219 if (!integer_zerop (cst) && !integer_onep (cst))
12220 {
716c0ba6
MP
12221 int sign = (TREE_CODE (op0) == INTEGER_CST
12222 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
04159acf
MP
12223 if (code == EQ_EXPR
12224 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12225 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12226 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12227 "with boolean expression is always false", cst);
12228 else
12229 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12230 "with boolean expression is always true", cst);
12231 }
716c0ba6
MP
12232 else if (integer_zerop (cst) || integer_onep (cst))
12233 {
577cd070
MP
12234 /* If the non-constant operand isn't of a boolean type, we
12235 don't want to warn here. */
12236 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12237 /* Handle booleans promoted to integers. */
12238 if (CONVERT_EXPR_P (noncst)
12239 && TREE_TYPE (noncst) == integer_type_node
12240 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12241 /* Warn. */;
12242 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12243 && !truth_value_p (TREE_CODE (noncst)))
12244 return;
716c0ba6
MP
12245 /* Do some magic to get the right diagnostics. */
12246 bool flag = TREE_CODE (op0) == INTEGER_CST;
12247 flag = integer_zerop (cst) ? flag : !flag;
12248 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12249 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12250 "with boolean expression is always true", cst);
12251 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12252 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12253 "with boolean expression is always false", cst);
12254 }
04159acf
MP
12255}
12256
c166b898
ILT
12257/* The C and C++ parsers both use vectors to hold function arguments.
12258 For efficiency, we keep a cache of unused vectors. This is the
12259 cache. */
12260
9771b263
DN
12261typedef vec<tree, va_gc> *tree_gc_vec;
12262static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
c166b898
ILT
12263
12264/* Return a new vector from the cache. If the cache is empty,
12265 allocate a new vector. These vectors are GC'ed, so it is OK if the
12266 pointer is not released.. */
12267
9771b263 12268vec<tree, va_gc> *
c166b898
ILT
12269make_tree_vector (void)
12270{
9771b263
DN
12271 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12272 return tree_vector_cache->pop ();
c166b898
ILT
12273 else
12274 {
9771b263 12275 /* Passing 0 to vec::alloc returns NULL, and our callers require
c166b898
ILT
12276 that we always return a non-NULL value. The vector code uses
12277 4 when growing a NULL vector, so we do too. */
9771b263
DN
12278 vec<tree, va_gc> *v;
12279 vec_alloc (v, 4);
12280 return v;
c166b898
ILT
12281 }
12282}
12283
12284/* Release a vector of trees back to the cache. */
12285
12286void
9771b263 12287release_tree_vector (vec<tree, va_gc> *vec)
c166b898
ILT
12288{
12289 if (vec != NULL)
12290 {
9771b263
DN
12291 vec->truncate (0);
12292 vec_safe_push (tree_vector_cache, vec);
c166b898
ILT
12293 }
12294}
12295
12296/* Get a new tree vector holding a single tree. */
12297
9771b263 12298vec<tree, va_gc> *
c166b898
ILT
12299make_tree_vector_single (tree t)
12300{
9771b263
DN
12301 vec<tree, va_gc> *ret = make_tree_vector ();
12302 ret->quick_push (t);
c166b898
ILT
12303 return ret;
12304}
12305
c12ff9d8
JM
12306/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12307
9771b263 12308vec<tree, va_gc> *
c12ff9d8
JM
12309make_tree_vector_from_list (tree list)
12310{
9771b263 12311 vec<tree, va_gc> *ret = make_tree_vector ();
c12ff9d8 12312 for (; list; list = TREE_CHAIN (list))
9771b263 12313 vec_safe_push (ret, TREE_VALUE (list));
c12ff9d8
JM
12314 return ret;
12315}
12316
c166b898
ILT
12317/* Get a new tree vector which is a copy of an existing one. */
12318
9771b263
DN
12319vec<tree, va_gc> *
12320make_tree_vector_copy (const vec<tree, va_gc> *orig)
c166b898 12321{
9771b263 12322 vec<tree, va_gc> *ret;
c166b898
ILT
12323 unsigned int ix;
12324 tree t;
12325
12326 ret = make_tree_vector ();
9771b263
DN
12327 vec_safe_reserve (ret, vec_safe_length (orig));
12328 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12329 ret->quick_push (t);
c166b898
ILT
12330 return ret;
12331}
12332
a9aa2c3a
NF
12333/* Return true if KEYWORD starts a type specifier. */
12334
12335bool
12336keyword_begins_type_specifier (enum rid keyword)
12337{
12338 switch (keyword)
12339 {
38b7bc7f 12340 case RID_AUTO_TYPE:
a9aa2c3a
NF
12341 case RID_INT:
12342 case RID_CHAR:
12343 case RID_FLOAT:
12344 case RID_DOUBLE:
12345 case RID_VOID:
a9aa2c3a
NF
12346 case RID_UNSIGNED:
12347 case RID_LONG:
12348 case RID_SHORT:
12349 case RID_SIGNED:
12350 case RID_DFLOAT32:
12351 case RID_DFLOAT64:
12352 case RID_DFLOAT128:
12353 case RID_FRACT:
12354 case RID_ACCUM:
12355 case RID_BOOL:
12356 case RID_WCHAR:
12357 case RID_CHAR16:
12358 case RID_CHAR32:
12359 case RID_SAT:
12360 case RID_COMPLEX:
12361 case RID_TYPEOF:
12362 case RID_STRUCT:
12363 case RID_CLASS:
12364 case RID_UNION:
12365 case RID_ENUM:
12366 return true;
12367 default:
78a7c317
DD
12368 if (keyword >= RID_FIRST_INT_N
12369 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12370 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12371 return true;
a9aa2c3a
NF
12372 return false;
12373 }
12374}
12375
12376/* Return true if KEYWORD names a type qualifier. */
12377
12378bool
12379keyword_is_type_qualifier (enum rid keyword)
12380{
12381 switch (keyword)
12382 {
12383 case RID_CONST:
12384 case RID_VOLATILE:
12385 case RID_RESTRICT:
267bac10 12386 case RID_ATOMIC:
a9aa2c3a
NF
12387 return true;
12388 default:
12389 return false;
12390 }
12391}
12392
12393/* Return true if KEYWORD names a storage class specifier.
12394
12395 RID_TYPEDEF is not included in this list despite `typedef' being
12396 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12397 such for syntactic convenience only. */
12398
12399bool
12400keyword_is_storage_class_specifier (enum rid keyword)
12401{
12402 switch (keyword)
12403 {
12404 case RID_STATIC:
12405 case RID_EXTERN:
12406 case RID_REGISTER:
12407 case RID_AUTO:
12408 case RID_MUTABLE:
12409 case RID_THREAD:
12410 return true;
12411 default:
12412 return false;
12413 }
12414}
12415
ba9e6dd5
NF
12416/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12417
12418static bool
12419keyword_is_function_specifier (enum rid keyword)
12420{
12421 switch (keyword)
12422 {
12423 case RID_INLINE:
bbceee64 12424 case RID_NORETURN:
ba9e6dd5
NF
12425 case RID_VIRTUAL:
12426 case RID_EXPLICIT:
12427 return true;
12428 default:
12429 return false;
12430 }
12431}
12432
12433/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12434 declaration-specifier (C99 6.7). */
12435
12436bool
12437keyword_is_decl_specifier (enum rid keyword)
12438{
12439 if (keyword_is_storage_class_specifier (keyword)
12440 || keyword_is_type_qualifier (keyword)
12441 || keyword_is_function_specifier (keyword))
12442 return true;
12443
12444 switch (keyword)
12445 {
12446 case RID_TYPEDEF:
12447 case RID_FRIEND:
12448 case RID_CONSTEXPR:
12449 return true;
12450 default:
12451 return false;
12452 }
12453}
12454
81f653d6
NF
12455/* Initialize language-specific-bits of tree_contains_struct. */
12456
12457void
12458c_common_init_ts (void)
12459{
12460 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12461 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
36536d79 12462 MARK_TS_TYPED (ARRAY_NOTATION_REF);
81f653d6
NF
12463}
12464
3ce4f9e4
ESR
12465/* Build a user-defined numeric literal out of an integer constant type VALUE
12466 with identifier SUFFIX. */
12467
12468tree
2d7aa578
ESR
12469build_userdef_literal (tree suffix_id, tree value,
12470 enum overflow_type overflow, tree num_string)
3ce4f9e4
ESR
12471{
12472 tree literal = make_node (USERDEF_LITERAL);
12473 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12474 USERDEF_LITERAL_VALUE (literal) = value;
2d7aa578 12475 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
3ce4f9e4
ESR
12476 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12477 return literal;
12478}
12479
7edaa4d2 12480/* For vector[index], convert the vector to a
aa7da51a
JJ
12481 pointer of the underlying type. Return true if the resulting
12482 ARRAY_REF should not be an lvalue. */
12483
12484bool
7edaa4d2 12485convert_vector_to_pointer_for_subscript (location_t loc,
aa7da51a 12486 tree *vecp, tree index)
7edaa4d2 12487{
aa7da51a 12488 bool ret = false;
7edaa4d2
MG
12489 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12490 {
12491 tree type = TREE_TYPE (*vecp);
12492 tree type1;
12493
aa7da51a 12494 ret = !lvalue_p (*vecp);
7edaa4d2 12495 if (TREE_CODE (index) == INTEGER_CST)
cc269bb6 12496 if (!tree_fits_uhwi_p (index)
7d362f6c 12497 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7edaa4d2
MG
12498 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12499
aa7da51a
JJ
12500 if (ret)
12501 {
b731b390 12502 tree tmp = create_tmp_var_raw (type);
aa7da51a
JJ
12503 DECL_SOURCE_LOCATION (tmp) = loc;
12504 *vecp = c_save_expr (*vecp);
12505 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12506 {
12507 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12508 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12509 *vecp
12510 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12511 *vecp, NULL_TREE, NULL_TREE),
12512 non_const);
12513 }
12514 else
12515 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12516 NULL_TREE, NULL_TREE);
12517 SET_EXPR_LOCATION (*vecp, loc);
12518 c_common_mark_addressable_vec (tmp);
12519 }
12520 else
12521 c_common_mark_addressable_vec (*vecp);
7edaa4d2 12522 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7edaa4d2 12523 type1 = build_pointer_type (TREE_TYPE (*vecp));
b0f1bf36
RB
12524 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12525 if (!ref_all
12526 && !DECL_P (*vecp))
12527 {
12528 /* If the original vector isn't declared may_alias and it
12529 isn't a bare vector look if the subscripting would
12530 alias the vector we subscript, and if not, force ref-all. */
12531 alias_set_type vecset = get_alias_set (*vecp);
12532 alias_set_type sset = get_alias_set (type);
12533 if (!alias_sets_must_conflict_p (sset, vecset)
12534 && !alias_set_subset_of (sset, vecset))
12535 ref_all = true;
12536 }
12537 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7edaa4d2
MG
12538 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12539 *vecp = convert (type, *vecp);
12540 }
aa7da51a 12541 return ret;
7edaa4d2
MG
12542}
12543
a212e43f
MG
12544/* Determine which of the operands, if any, is a scalar that needs to be
12545 converted to a vector, for the range of operations. */
12546enum stv_conv
12547scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12548 bool complain)
12549{
12550 tree type0 = TREE_TYPE (op0);
12551 tree type1 = TREE_TYPE (op1);
12552 bool integer_only_op = false;
12553 enum stv_conv ret = stv_firstarg;
12554
12555 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12556 || TREE_CODE (type1) == VECTOR_TYPE);
12557 switch (code)
12558 {
12559 /* Most GENERIC binary expressions require homogeneous arguments.
12560 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12561 argument that is a vector and a second one that is a scalar, so
12562 we never return stv_secondarg for them. */
12563 case RSHIFT_EXPR:
12564 case LSHIFT_EXPR:
12565 if (TREE_CODE (type0) == INTEGER_TYPE
12566 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12567 {
68fca595 12568 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12569 {
12570 if (complain)
12571 error_at (loc, "conversion of scalar %qT to vector %qT "
12572 "involves truncation", type0, type1);
12573 return stv_error;
12574 }
12575 else
12576 return stv_firstarg;
12577 }
12578 break;
12579
12580 case BIT_IOR_EXPR:
12581 case BIT_XOR_EXPR:
12582 case BIT_AND_EXPR:
12583 integer_only_op = true;
12584 /* ... fall through ... */
12585
93100c6b
MG
12586 case VEC_COND_EXPR:
12587
a212e43f
MG
12588 case PLUS_EXPR:
12589 case MINUS_EXPR:
12590 case MULT_EXPR:
12591 case TRUNC_DIV_EXPR:
12592 case CEIL_DIV_EXPR:
12593 case FLOOR_DIV_EXPR:
12594 case ROUND_DIV_EXPR:
12595 case EXACT_DIV_EXPR:
12596 case TRUNC_MOD_EXPR:
12597 case FLOOR_MOD_EXPR:
12598 case RDIV_EXPR:
12599 case EQ_EXPR:
12600 case NE_EXPR:
12601 case LE_EXPR:
12602 case GE_EXPR:
12603 case LT_EXPR:
12604 case GT_EXPR:
12605 /* What about UNLT_EXPR? */
12606 if (TREE_CODE (type0) == VECTOR_TYPE)
12607 {
12608 tree tmp;
12609 ret = stv_secondarg;
12610 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12611 tmp = type0; type0 = type1; type1 = tmp;
12612 tmp = op0; op0 = op1; op1 = tmp;
12613 }
12614
12615 if (TREE_CODE (type0) == INTEGER_TYPE
12616 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12617 {
68fca595 12618 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12619 {
12620 if (complain)
12621 error_at (loc, "conversion of scalar %qT to vector %qT "
12622 "involves truncation", type0, type1);
12623 return stv_error;
12624 }
12625 return ret;
12626 }
12627 else if (!integer_only_op
12628 /* Allow integer --> real conversion if safe. */
12629 && (TREE_CODE (type0) == REAL_TYPE
12630 || TREE_CODE (type0) == INTEGER_TYPE)
12631 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12632 {
68fca595 12633 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12634 {
12635 if (complain)
12636 error_at (loc, "conversion of scalar %qT to vector %qT "
12637 "involves truncation", type0, type1);
12638 return stv_error;
12639 }
12640 return ret;
12641 }
12642 default:
12643 break;
12644 }
12645
12646 return stv_nothing;
12647}
12648
e28d52cf
DS
12649/* Return true iff ALIGN is an integral constant that is a fundamental
12650 alignment, as defined by [basic.align] in the c++-11
12651 specifications.
12652
12653 That is:
12654
12655 [A fundamental alignment is represented by an alignment less than or
12656 equal to the greatest alignment supported by the implementation
12657 in all contexts, which is equal to
12658 alignof(max_align_t)]. */
12659
12660bool
12661cxx_fundamental_alignment_p (unsigned align)
12662{
12663 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12664 TYPE_ALIGN (long_double_type_node)));
12665}
12666
f04dda30
MP
12667/* Return true if T is a pointer to a zero-sized aggregate. */
12668
12669bool
12670pointer_to_zero_sized_aggr_p (tree t)
12671{
12672 if (!POINTER_TYPE_P (t))
12673 return false;
12674 t = TREE_TYPE (t);
12675 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12676}
12677
39dabefd 12678#include "gt-c-family-c-common.h"