]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
Fix libgcc installation for offload targets.
[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"
40e23961 25#include "input.h"
b559c810 26#include "c-common.h"
4977bab6 27#include "tm.h"
d9b2742a 28#include "intl.h"
b30f223b 29#include "tree.h"
d8a2d370
DN
30#include "fold-const.h"
31#include "stor-layout.h"
32#include "calls.h"
33#include "stringpool.h"
34#include "attribs.h"
35#include "varasm.h"
36#include "trans-mem.h"
b30f223b 37#include "flags.h"
e2af664c 38#include "c-pragma.h"
61d3ce20 39#include "c-objc.h"
7bdb32b9 40#include "tm_p.h"
235cfbc4 41#include "obstack.h"
c8724862 42#include "cpplib.h"
12a68f1f 43#include "target.h"
677f3fa8 44#include "common/common-target.h"
7afff7cf 45#include "langhooks.h"
d57a4b98 46#include "tree-inline.h"
5f1989e6 47#include "toplev.h"
148e4216 48#include "diagnostic.h"
6de9cd9a
DN
49#include "tree-iterator.h"
50#include "hashtab.h"
d974312d 51#include "opts.h"
c582198b
AM
52#include "hash-map.h"
53#include "is-a.h"
54#include "plugin-api.h"
55#include "vec.h"
56#include "hash-set.h"
57#include "machmode.h"
58#include "hard-reg-set.h"
59#include "input.h"
60#include "function.h"
61#include "ipa-ref.h"
ce91e74c 62#include "cgraph.h"
1f1d5130 63#include "target-def.h"
45b0be94 64#include "gimplify.h"
807e902e 65#include "wide-int-print.h"
aa7da51a 66#include "gimple-expr.h"
cb60f38d 67
81a75f0f 68cpp_reader *parse_in; /* Declared in c-pragma.h. */
c8724862 69
8fba1830
BRF
70/* Mode used to build pointers (VOIDmode means ptr_mode). */
71
72machine_mode c_default_pointer_mode = VOIDmode;
73
7f4edbcb 74/* The following symbols are subsumed in the c_global_trees array, and
d125d268 75 listed here individually for documentation purposes.
7f4edbcb
BS
76
77 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
78
79 tree short_integer_type_node;
80 tree long_integer_type_node;
81 tree long_long_integer_type_node;
82
83 tree short_unsigned_type_node;
84 tree long_unsigned_type_node;
85 tree long_long_unsigned_type_node;
86
de7df9eb
JM
87 tree truthvalue_type_node;
88 tree truthvalue_false_node;
89 tree truthvalue_true_node;
7f4edbcb
BS
90
91 tree ptrdiff_type_node;
92
93 tree unsigned_char_type_node;
94 tree signed_char_type_node;
95 tree wchar_type_node;
7f4edbcb 96
b6baa67d
KVH
97 tree char16_type_node;
98 tree char32_type_node;
99
7f4edbcb
BS
100 tree float_type_node;
101 tree double_type_node;
102 tree long_double_type_node;
103
104 tree complex_integer_type_node;
105 tree complex_float_type_node;
106 tree complex_double_type_node;
107 tree complex_long_double_type_node;
108
9a8ce21f
JG
109 tree dfloat32_type_node;
110 tree dfloat64_type_node;
111 tree_dfloat128_type_node;
112
7f4edbcb
BS
113 tree intQI_type_node;
114 tree intHI_type_node;
115 tree intSI_type_node;
116 tree intDI_type_node;
117 tree intTI_type_node;
118
119 tree unsigned_intQI_type_node;
120 tree unsigned_intHI_type_node;
121 tree unsigned_intSI_type_node;
122 tree unsigned_intDI_type_node;
123 tree unsigned_intTI_type_node;
124
125 tree widest_integer_literal_type_node;
126 tree widest_unsigned_literal_type_node;
127
128 Nodes for types `void *' and `const void *'.
129
130 tree ptr_type_node, const_ptr_type_node;
131
132 Nodes for types `char *' and `const char *'.
133
134 tree string_type_node, const_string_type_node;
135
136 Type `char[SOMENUMBER]'.
137 Used when an array of char is needed and the size is irrelevant.
138
139 tree char_array_type_node;
140
7f4edbcb
BS
141 Type `wchar_t[SOMENUMBER]' or something like it.
142 Used when a wide string literal is created.
143
144 tree wchar_array_type_node;
145
b6baa67d
KVH
146 Type `char16_t[SOMENUMBER]' or something like it.
147 Used when a UTF-16 string literal is created.
148
149 tree char16_array_type_node;
150
151 Type `char32_t[SOMENUMBER]' or something like it.
152 Used when a UTF-32 string literal is created.
153
154 tree char32_array_type_node;
155
7f4edbcb
BS
156 Type `int ()' -- used for implicit declaration of functions.
157
158 tree default_function_type;
159
7f4edbcb
BS
160 A VOID_TYPE node, packaged in a TREE_LIST.
161
162 tree void_list_node;
163
684d9f3b 164 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
0ba8a114
NS
165 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
166 VAR_DECLS, but C++ does.)
63ad61ed 167
0ba8a114 168 tree function_name_decl_node;
684d9f3b 169 tree pretty_function_name_decl_node;
0ba8a114
NS
170 tree c99_function_name_decl_node;
171
172 Stack of nested function name VAR_DECLs.
35b1a6fa 173
0ba8a114 174 tree saved_function_name_decls;
63ad61ed 175
7f4edbcb
BS
176*/
177
178tree c_global_trees[CTI_MAX];
17211ab5 179\f
4078b403
NB
180/* Switches common to the C front ends. */
181
63973df3
NB
182/* Nonzero means don't output line number information. */
183
184char flag_no_line_commands;
185
186/* Nonzero causes -E output not to be done, but directives such as
187 #define that have side effects are still obeyed. */
188
189char flag_no_output;
190
191/* Nonzero means dump macros in some fashion. */
192
193char flag_dump_macros;
194
195/* Nonzero means pass #include lines through to the output. */
196
197char flag_dump_includes;
198
c0d578e6
GK
199/* Nonzero means process PCH files while preprocessing. */
200
201bool flag_pch_preprocess;
202
17211ab5
GK
203/* The file name to which we should write a precompiled header, or
204 NULL if no header will be written in this compile. */
205
206const char *pch_file;
207
3df89291
NB
208/* Nonzero if an ISO standard was selected. It rejects macros in the
209 user's namespace. */
210int flag_iso;
211
4078b403
NB
212/* C/ObjC language option variables. */
213
214
4078b403
NB
215/* Nonzero means allow type mismatches in conditional expressions;
216 just make their values `void'. */
217
218int flag_cond_mismatch;
219
220/* Nonzero means enable C89 Amendment 1 features. */
221
222int flag_isoc94;
223
48b0b196 224/* Nonzero means use the ISO C99 (or C11) dialect of C. */
4078b403
NB
225
226int flag_isoc99;
227
48b0b196 228/* Nonzero means use the ISO C11 dialect of C. */
2778d766 229
48b0b196 230int flag_isoc11;
2778d766 231
6614fd40 232/* Nonzero means that we have builtin functions, and main is an int. */
4078b403
NB
233
234int flag_hosted = 1;
235
4078b403
NB
236
237/* ObjC language option variables. */
238
239
4078b403
NB
240/* Tells the compiler that this is a special run. Do not perform any
241 compiling, instead we are to test some platform dependent features
242 and output a C header file with appropriate definitions. */
243
244int print_struct_values;
245
fa10beec 246/* Tells the compiler what is the constant string class for ObjC. */
4078b403
NB
247
248const char *constant_string_class_name;
249
4078b403
NB
250
251/* C++ language option variables. */
252
253
4078b403
NB
254/* Nonzero means generate separate instantiation control files and
255 juggle them at link time. */
256
257int flag_use_repository;
258
c1ae8be5 259/* The C++ dialect being used. C++98 is the default. */
966541e3 260
c1ae8be5 261enum cxx_dialect cxx_dialect = cxx98;
966541e3 262
4afe7ad7 263/* Maximum template instantiation depth. This limit exists to limit the
3ff60975 264 time it takes to notice excessively recursive template instantiations.
4078b403 265
3ff60975
JM
266 The default is lower than the 1024 recommended by the C++0x standard
267 because G++ runs out of stack before 1024 with highly recursive template
268 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
269
270int max_tinst_depth = 900;
4078b403 271
f09f1de5
MM
272/* The elements of `ridpointers' are identifier nodes for the reserved
273 type names and storage classes. It is indexed by a RID_... value. */
274tree *ridpointers;
275
c2255bc4 276tree (*make_fname_decl) (location_t, tree, int);
2ce07e2d 277
7d882b83
ILT
278/* Nonzero means don't warn about problems that occur when the code is
279 executed. */
280int c_inhibit_evaluation_warnings;
e78a3b42 281
5386338c
JM
282/* Whether we are building a boolean conversion inside
283 convert_for_assignment, or some other late binary operation. If
284 build_binary_op is called for C (from code shared by C and C++) in
285 this case, then the operands have already been folded and the
286 result will not be folded again, so C_MAYBE_CONST_EXPR should not
287 be generated. */
288bool in_late_binary_op;
289
148e4216
JM
290/* Whether lexing has been completed, so subsequent preprocessor
291 errors should use the compiler's input_location. */
292bool done_lexing = false;
293
ec5c56db 294/* Information about how a function name is generated. */
0ba8a114
NS
295struct fname_var_t
296{
8b60264b
KG
297 tree *const decl; /* pointer to the VAR_DECL. */
298 const unsigned rid; /* RID number for the identifier. */
299 const int pretty; /* How pretty is it? */
0ba8a114
NS
300};
301
ec5c56db 302/* The three ways of getting then name of the current function. */
0ba8a114
NS
303
304const struct fname_var_t fname_vars[] =
305{
ec5c56db 306 /* C99 compliant __func__, must be first. */
0ba8a114 307 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
ec5c56db 308 /* GCC __FUNCTION__ compliant. */
0ba8a114 309 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
ec5c56db 310 /* GCC __PRETTY_FUNCTION__ compliant. */
0ba8a114
NS
311 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
312 {NULL, 0, 0},
313};
314
c98cd5bf
JM
315/* Global visibility options. */
316struct visibility_flags visibility_options;
317
928c19bb 318static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
62e4eb35 319static tree check_case_value (location_t, tree);
9d548dfb 320static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
4724b3de 321
35b1a6fa
AJ
322static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
323static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
324static tree handle_common_attribute (tree *, tree, tree, int, bool *);
325static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52bf96d2
JH
326static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
327static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
e664c61c
KS
328static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
329 int, bool *);
77bc5132
JJ
330static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
331 int, bool *);
ce6923c5
MP
332static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
333 bool *);
5434dc07 334static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 335static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
86631ea3 336static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
185c9e56 337static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
46a4da10 338static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
339static tree handle_always_inline_attribute (tree *, tree, tree, int,
340 bool *);
d752cfdb
JJ
341static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
342static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0691d1d4 343static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
d2af6a68 344static tree handle_error_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
345static tree handle_used_attribute (tree *, tree, tree, int, bool *);
346static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
ce91e74c
JH
347static tree handle_externally_visible_attribute (tree *, tree, tree, int,
348 bool *);
7861b648
AK
349static tree handle_no_reorder_attribute (tree *, tree, tree, int,
350 bool *);
35b1a6fa
AJ
351static tree handle_const_attribute (tree *, tree, tree, int, bool *);
352static tree handle_transparent_union_attribute (tree *, tree, tree,
353 int, bool *);
354static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
355static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
356static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
357static tree handle_section_attribute (tree *, tree, tree, int, bool *);
358static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
359static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
ba885ec5
NS
360static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
361static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 362static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
a0203ca7 363static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
35b1a6fa
AJ
364static tree handle_visibility_attribute (tree *, tree, tree, int,
365 bool *);
366static tree handle_tls_model_attribute (tree *, tree, tree, int,
367 bool *);
368static tree handle_no_instrument_function_attribute (tree *, tree,
369 tree, int, bool *);
370static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
6e9a3221 371static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
372static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
373 bool *);
374static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
0a35513e
AH
375static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
376static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
dcd6de6d 377static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
378static tree handle_deprecated_attribute (tree *, tree, tree, int,
379 bool *);
380static tree handle_vector_size_attribute (tree *, tree, tree, int,
381 bool *);
382static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
383static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
384static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
72954a4f
JM
385static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
386 bool *);
3d091dac 387static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5d32c25 388static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
51bc54a6 389static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
8fcbce72
JJ
390static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
391static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
5779e713 392static tree handle_target_attribute (tree *, tree, tree, int, bool *);
ab442df7 393static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
0a35513e 394static tree ignore_attribute (tree *, tree, tree, int, bool *);
7458026b 395static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
0b7b376d 396static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
2a99e5e6 397static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
826cacfe 398static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
399static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
400 bool *);
401static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
402 bool *);
976d5a22 403static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
d5e254e1
IE
404static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
405static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
406static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
35b1a6fa 407
94a0dd7b 408static void check_function_nonnull (tree, int, tree *);
35b1a6fa
AJ
409static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
410static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
411static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
d07605f5 412static int resort_field_decl_cmp (const void *, const void *);
b34c7881 413
eea1139b
ILT
414/* Reserved words. The third field is a mask: keywords are disabled
415 if they match the mask.
416
417 Masks for languages:
418 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
419 C --std=c99: D_CXXONLY | D_OBJC
420 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
421 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
422 C++ --std=c0x: D_CONLY | D_OBJC
423 ObjC++ is like C++ except that D_OBJC is not set
424
425 If -fno-asm is used, D_ASM is added to the mask. If
426 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
427 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
1973201f 428 In C with -Wc++-compat, we warn if D_CXXWARN is set.
eea1139b 429
1973201f
NP
430 Note the complication of the D_CXX_OBJC keywords. These are
431 reserved words such as 'class'. In C++, 'class' is a reserved
432 word. In Objective-C++ it is too. In Objective-C, it is a
433 reserved word too, but only if it follows an '@' sign.
434*/
eea1139b
ILT
435const struct c_common_resword c_common_reswords[] =
436{
d19fa6b5
JM
437 { "_Alignas", RID_ALIGNAS, D_CONLY },
438 { "_Alignof", RID_ALIGNOF, D_CONLY },
267bac10 439 { "_Atomic", RID_ATOMIC, D_CONLY },
eea1139b
ILT
440 { "_Bool", RID_BOOL, D_CONLY },
441 { "_Complex", RID_COMPLEX, 0 },
939b37da
BI
442 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
443 { "_Cilk_sync", RID_CILK_SYNC, 0 },
9a771876 444 { "_Cilk_for", RID_CILK_FOR, 0 },
fa5da7de 445 { "_Imaginary", RID_IMAGINARY, D_CONLY },
eea1139b
ILT
446 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
447 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
448 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
449 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
450 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
451 { "_Sat", RID_SAT, D_CONLY | D_EXT },
32912286 452 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
bbceee64 453 { "_Noreturn", RID_NORETURN, D_CONLY },
433cc7b0 454 { "_Generic", RID_GENERIC, D_CONLY },
582d9b50 455 { "_Thread_local", RID_THREAD, D_CONLY },
eea1139b
ILT
456 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
457 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
458 { "__alignof", RID_ALIGNOF, 0 },
459 { "__alignof__", RID_ALIGNOF, 0 },
460 { "__asm", RID_ASM, 0 },
461 { "__asm__", RID_ASM, 0 },
462 { "__attribute", RID_ATTRIBUTE, 0 },
463 { "__attribute__", RID_ATTRIBUTE, 0 },
38b7bc7f 464 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
4daba884 465 { "__bases", RID_BASES, D_CXXONLY },
74893f25
RH
466 { "__builtin_call_with_static_chain",
467 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
eea1139b 468 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
d4a83c10 469 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
9e1a8dd1 470 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
eea1139b
ILT
471 { "__builtin_offsetof", RID_OFFSETOF, 0 },
472 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
473 { "__builtin_va_arg", RID_VA_ARG, 0 },
474 { "__complex", RID_COMPLEX, 0 },
475 { "__complex__", RID_COMPLEX, 0 },
476 { "__const", RID_CONST, 0 },
477 { "__const__", RID_CONST, 0 },
478 { "__decltype", RID_DECLTYPE, D_CXXONLY },
4daba884 479 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
eea1139b
ILT
480 { "__extension__", RID_EXTENSION, 0 },
481 { "__func__", RID_C99_FUNCTION_NAME, 0 },
482 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
483 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
484 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
485 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
486 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
487 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
488 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
489 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
3c0d13bf
PC
490 { "__imag", RID_IMAGPART, 0 },
491 { "__imag__", RID_IMAGPART, 0 },
492 { "__inline", RID_INLINE, 0 },
493 { "__inline__", RID_INLINE, 0 },
eea1139b
ILT
494 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
495 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
496 { "__is_class", RID_IS_CLASS, D_CXXONLY },
eea1139b
ILT
497 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
498 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
b3908fcc 499 { "__is_final", RID_IS_FINAL, D_CXXONLY },
3c0d13bf 500 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
eea1139b
ILT
501 { "__is_pod", RID_IS_POD, D_CXXONLY },
502 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
c32097d8
JM
503 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
504 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
dd5d5481
JM
505 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
506 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
b752325e 507 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
eea1139b 508 { "__is_union", RID_IS_UNION, D_CXXONLY },
eea1139b
ILT
509 { "__label__", RID_LABEL, 0 },
510 { "__null", RID_NULL, 0 },
511 { "__real", RID_REALPART, 0 },
512 { "__real__", RID_REALPART, 0 },
513 { "__restrict", RID_RESTRICT, 0 },
514 { "__restrict__", RID_RESTRICT, 0 },
515 { "__signed", RID_SIGNED, 0 },
516 { "__signed__", RID_SIGNED, 0 },
517 { "__thread", RID_THREAD, 0 },
0a35513e
AH
518 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
519 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
520 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
eea1139b
ILT
521 { "__typeof", RID_TYPEOF, 0 },
522 { "__typeof__", RID_TYPEOF, 0 },
3c0d13bf 523 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
eea1139b
ILT
524 { "__volatile", RID_VOLATILE, 0 },
525 { "__volatile__", RID_VOLATILE, 0 },
e28d52cf 526 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
f4e8a943 527 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
eea1139b
ILT
528 { "asm", RID_ASM, D_ASM },
529 { "auto", RID_AUTO, 0 },
03c3034e 530 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
531 { "break", RID_BREAK, 0 },
532 { "case", RID_CASE, 0 },
2696a995 533 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
eea1139b 534 { "char", RID_CHAR, 0 },
2696a995
KG
535 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
536 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
537 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
eea1139b 538 { "const", RID_CONST, 0 },
7ecbca9d 539 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
eea1139b
ILT
540 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
541 { "continue", RID_CONTINUE, 0 },
2696a995 542 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
eea1139b 543 { "default", RID_DEFAULT, 0 },
2696a995 544 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
545 { "do", RID_DO, 0 },
546 { "double", RID_DOUBLE, 0 },
547 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
548 { "else", RID_ELSE, 0 },
549 { "enum", RID_ENUM, 0 },
2696a995
KG
550 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
551 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
eea1139b 552 { "extern", RID_EXTERN, 0 },
2696a995 553 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
554 { "float", RID_FLOAT, 0 },
555 { "for", RID_FOR, 0 },
2696a995 556 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
557 { "goto", RID_GOTO, 0 },
558 { "if", RID_IF, 0 },
559 { "inline", RID_INLINE, D_EXT89 },
560 { "int", RID_INT, 0 },
561 { "long", RID_LONG, 0 },
562 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
2696a995
KG
563 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
564 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
0a766368 565 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
14c2101d 566 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
2696a995
KG
567 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
568 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
569 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
570 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
eea1139b
ILT
571 { "register", RID_REGISTER, 0 },
572 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
573 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
574 { "return", RID_RETURN, 0 },
575 { "short", RID_SHORT, 0 },
576 { "signed", RID_SIGNED, 0 },
577 { "sizeof", RID_SIZEOF, 0 },
578 { "static", RID_STATIC, 0 },
579 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
580 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
581 { "struct", RID_STRUCT, 0 },
582 { "switch", RID_SWITCH, 0 },
2696a995
KG
583 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
584 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
b1db7f91 585 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
2696a995
KG
586 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
587 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
588 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
eea1139b 589 { "typedef", RID_TYPEDEF, 0 },
2696a995
KG
590 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
591 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
592 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
593 { "union", RID_UNION, 0 },
594 { "unsigned", RID_UNSIGNED, 0 },
2696a995
KG
595 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
596 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
597 { "void", RID_VOID, 0 },
598 { "volatile", RID_VOLATILE, 0 },
599 { "wchar_t", RID_WCHAR, D_CXXONLY },
600 { "while", RID_WHILE, 0 },
601 /* These Objective-C keywords are recognized only immediately after
602 an '@'. */
603 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
604 { "defs", RID_AT_DEFS, D_OBJC },
605 { "encode", RID_AT_ENCODE, D_OBJC },
606 { "end", RID_AT_END, D_OBJC },
607 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
608 { "interface", RID_AT_INTERFACE, D_OBJC },
609 { "protocol", RID_AT_PROTOCOL, D_OBJC },
610 { "selector", RID_AT_SELECTOR, D_OBJC },
611 { "finally", RID_AT_FINALLY, D_OBJC },
612 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
92902b1b
IS
613 { "optional", RID_AT_OPTIONAL, D_OBJC },
614 { "required", RID_AT_REQUIRED, D_OBJC },
668ea4b1 615 { "property", RID_AT_PROPERTY, D_OBJC },
c37d8c30 616 { "package", RID_AT_PACKAGE, D_OBJC },
da57d1b9
NP
617 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
618 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
eea1139b
ILT
619 /* These are recognized only in protocol-qualifier context
620 (see above) */
621 { "bycopy", RID_BYCOPY, D_OBJC },
622 { "byref", RID_BYREF, D_OBJC },
623 { "in", RID_IN, D_OBJC },
624 { "inout", RID_INOUT, D_OBJC },
625 { "oneway", RID_ONEWAY, D_OBJC },
626 { "out", RID_OUT, D_OBJC },
668ea4b1 627 /* These are recognized inside a property attribute list */
200290f2
NP
628 { "assign", RID_ASSIGN, D_OBJC },
629 { "copy", RID_COPY, D_OBJC },
668ea4b1 630 { "getter", RID_GETTER, D_OBJC },
200290f2
NP
631 { "nonatomic", RID_NONATOMIC, D_OBJC },
632 { "readonly", RID_READONLY, D_OBJC },
633 { "readwrite", RID_READWRITE, D_OBJC },
634 { "retain", RID_RETAIN, D_OBJC },
668ea4b1 635 { "setter", RID_SETTER, D_OBJC },
eea1139b
ILT
636};
637
638const unsigned int num_c_common_reswords =
639 sizeof c_common_reswords / sizeof (struct c_common_resword);
640
d5e254e1
IE
641/* Table of machine-independent attributes common to all C-like languages.
642
643 All attributes referencing arguments should be additionally processed
644 in chkp_copy_function_type_adding_bounds for correct instrumentation
645 by Pointer Bounds Checker.
646 Current list of processed common attributes: nonnull. */
349ae713
NB
647const struct attribute_spec c_common_attribute_table[] =
648{
62d784f7
KT
649 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
650 affects_type_identity } */
349ae713 651 { "packed", 0, 0, false, false, false,
62d784f7 652 handle_packed_attribute , false},
349ae713 653 { "nocommon", 0, 0, true, false, false,
62d784f7 654 handle_nocommon_attribute, false},
349ae713 655 { "common", 0, 0, true, false, false,
62d784f7 656 handle_common_attribute, false },
349ae713
NB
657 /* FIXME: logically, noreturn attributes should be listed as
658 "false, true, true" and apply to function types. But implementing this
659 would require all the places in the compiler that use TREE_THIS_VOLATILE
660 on a decl to identify non-returning functions to be located and fixed
661 to check the function type instead. */
662 { "noreturn", 0, 0, true, false, false,
62d784f7 663 handle_noreturn_attribute, false },
349ae713 664 { "volatile", 0, 0, true, false, false,
62d784f7 665 handle_noreturn_attribute, false },
5434dc07
MD
666 { "stack_protect", 0, 0, true, false, false,
667 handle_stack_protect_attribute, false },
349ae713 668 { "noinline", 0, 0, true, false, false,
62d784f7 669 handle_noinline_attribute, false },
86631ea3 670 { "noclone", 0, 0, true, false, false,
62d784f7 671 handle_noclone_attribute, false },
185c9e56
ML
672 { "no_icf", 0, 0, true, false, false,
673 handle_noicf_attribute, false },
46a4da10 674 { "leaf", 0, 0, true, false, false,
62d784f7 675 handle_leaf_attribute, false },
349ae713 676 { "always_inline", 0, 0, true, false, false,
62d784f7 677 handle_always_inline_attribute, false },
4eb7fd83 678 { "gnu_inline", 0, 0, true, false, false,
62d784f7 679 handle_gnu_inline_attribute, false },
d752cfdb 680 { "artificial", 0, 0, true, false, false,
62d784f7 681 handle_artificial_attribute, false },
0691d1d4 682 { "flatten", 0, 0, true, false, false,
62d784f7 683 handle_flatten_attribute, false },
349ae713 684 { "used", 0, 0, true, false, false,
62d784f7 685 handle_used_attribute, false },
349ae713 686 { "unused", 0, 0, false, false, false,
62d784f7 687 handle_unused_attribute, false },
ce91e74c 688 { "externally_visible", 0, 0, true, false, false,
62d784f7 689 handle_externally_visible_attribute, false },
7861b648
AK
690 { "no_reorder", 0, 0, true, false, false,
691 handle_no_reorder_attribute, false },
349ae713
NB
692 /* The same comments as for noreturn attributes apply to const ones. */
693 { "const", 0, 0, true, false, false,
62d784f7 694 handle_const_attribute, false },
349ae713 695 { "transparent_union", 0, 0, false, false, false,
62d784f7 696 handle_transparent_union_attribute, false },
fc8600f9 697 { "constructor", 0, 1, true, false, false,
62d784f7 698 handle_constructor_attribute, false },
fc8600f9 699 { "destructor", 0, 1, true, false, false,
62d784f7 700 handle_destructor_attribute, false },
349ae713 701 { "mode", 1, 1, false, true, false,
62d784f7 702 handle_mode_attribute, false },
349ae713 703 { "section", 1, 1, true, false, false,
62d784f7 704 handle_section_attribute, false },
349ae713 705 { "aligned", 0, 1, false, false, false,
62d784f7 706 handle_aligned_attribute, false },
349ae713 707 { "weak", 0, 0, true, false, false,
62d784f7 708 handle_weak_attribute, false },
ba885ec5 709 { "ifunc", 1, 1, true, false, false,
62d784f7 710 handle_ifunc_attribute, false },
349ae713 711 { "alias", 1, 1, true, false, false,
62d784f7 712 handle_alias_attribute, false },
a0203ca7 713 { "weakref", 0, 1, true, false, false,
62d784f7 714 handle_weakref_attribute, false },
349ae713 715 { "no_instrument_function", 0, 0, true, false, false,
62d784f7
KT
716 handle_no_instrument_function_attribute,
717 false },
349ae713 718 { "malloc", 0, 0, true, false, false,
62d784f7 719 handle_malloc_attribute, false },
6e9a3221 720 { "returns_twice", 0, 0, true, false, false,
62d784f7 721 handle_returns_twice_attribute, false },
349ae713 722 { "no_stack_limit", 0, 0, true, false, false,
62d784f7 723 handle_no_limit_stack_attribute, false },
349ae713 724 { "pure", 0, 0, true, false, false,
62d784f7 725 handle_pure_attribute, false },
0a35513e
AH
726 { "transaction_callable", 0, 0, false, true, false,
727 handle_tm_attribute, false },
728 { "transaction_unsafe", 0, 0, false, true, false,
729 handle_tm_attribute, false },
730 { "transaction_safe", 0, 0, false, true, false,
731 handle_tm_attribute, false },
732 { "transaction_may_cancel_outer", 0, 0, false, true, false,
733 handle_tm_attribute, false },
734 /* ??? These two attributes didn't make the transition from the
735 Intel language document to the multi-vendor language document. */
736 { "transaction_pure", 0, 0, false, true, false,
737 handle_tm_attribute, false },
738 { "transaction_wrap", 1, 1, true, false, false,
739 handle_tm_wrap_attribute, false },
dcd6de6d
ZD
740 /* For internal use (marking of builtins) only. The name contains space
741 to prevent its usage in source code. */
742 { "no vops", 0, 0, true, false, false,
62d784f7 743 handle_novops_attribute, false },
9b86d6bb 744 { "deprecated", 0, 1, false, false, false,
62d784f7 745 handle_deprecated_attribute, false },
349ae713 746 { "vector_size", 1, 1, false, true, false,
62d784f7 747 handle_vector_size_attribute, false },
d7afec4b 748 { "visibility", 1, 1, false, false, false,
62d784f7 749 handle_visibility_attribute, false },
dce81a1a 750 { "tls_model", 1, 1, true, false, false,
62d784f7 751 handle_tls_model_attribute, false },
b34c7881 752 { "nonnull", 0, -1, false, true, true,
62d784f7 753 handle_nonnull_attribute, false },
39f2f3c8 754 { "nothrow", 0, 0, true, false, false,
62d784f7
KT
755 handle_nothrow_attribute, false },
756 { "may_alias", 0, 0, false, true, false, NULL, false },
0bfa5f65 757 { "cleanup", 1, 1, true, false, false,
62d784f7 758 handle_cleanup_attribute, false },
72954a4f 759 { "warn_unused_result", 0, 0, false, true, true,
62d784f7 760 handle_warn_unused_result_attribute, false },
254986c7 761 { "sentinel", 0, 1, false, true, true,
62d784f7 762 handle_sentinel_attribute, false },
b5d32c25
KG
763 /* For internal use (marking of builtins) only. The name contains space
764 to prevent its usage in source code. */
765 { "type generic", 0, 0, false, true, true,
62d784f7 766 handle_type_generic_attribute, false },
51bc54a6 767 { "alloc_size", 1, 2, false, true, true,
62d784f7 768 handle_alloc_size_attribute, false },
52bf96d2 769 { "cold", 0, 0, true, false, false,
62d784f7 770 handle_cold_attribute, false },
52bf96d2 771 { "hot", 0, 0, true, false, false,
62d784f7 772 handle_hot_attribute, false },
77bc5132
JJ
773 { "no_address_safety_analysis",
774 0, 0, true, false, false,
775 handle_no_address_safety_analysis_attribute,
776 false },
e664c61c
KS
777 { "no_sanitize_address", 0, 0, true, false, false,
778 handle_no_sanitize_address_attribute,
779 false },
de35aa66
MS
780 { "no_sanitize_thread", 0, 0, true, false, false,
781 handle_no_sanitize_address_attribute,
782 false },
ce6923c5
MP
783 { "no_sanitize_undefined", 0, 0, true, false, false,
784 handle_no_sanitize_undefined_attribute,
785 false },
d2af6a68 786 { "warning", 1, 1, true, false, false,
62d784f7 787 handle_error_attribute, false },
d2af6a68 788 { "error", 1, 1, true, false, false,
62d784f7 789 handle_error_attribute, false },
5779e713 790 { "target", 1, -1, true, false, false,
62d784f7 791 handle_target_attribute, false },
ab442df7 792 { "optimize", 1, -1, true, false, false,
62d784f7 793 handle_optimize_attribute, false },
0a35513e
AH
794 /* For internal use only. The leading '*' both prevents its usage in
795 source code and signals that it may be overridden by machine tables. */
796 { "*tm regparm", 0, 0, false, true, true,
797 ignore_attribute, false },
7458026b 798 { "no_split_stack", 0, 0, true, false, false,
62d784f7 799 handle_no_split_stack_attribute, false },
0b7b376d
RG
800 /* For internal use (marking of builtins and runtime functions) only.
801 The name contains space to prevent its usage in source code. */
802 { "fn spec", 1, 1, false, true, true,
62d784f7 803 handle_fnspec_attribute, false },
2a99e5e6
LL
804 { "warn_unused", 0, 0, false, false, false,
805 handle_warn_unused_attribute, false },
826cacfe
MG
806 { "returns_nonnull", 0, 0, false, true, true,
807 handle_returns_nonnull_attribute, false },
acf0174b
JJ
808 { "omp declare simd", 0, -1, true, false, false,
809 handle_omp_declare_simd_attribute, false },
41958c28
BI
810 { "cilk simd function", 0, -1, true, false, false,
811 handle_omp_declare_simd_attribute, false },
acf0174b
JJ
812 { "omp declare target", 0, 0, true, false, false,
813 handle_omp_declare_target_attribute, false },
8fcbce72
JJ
814 { "alloc_align", 1, 1, false, true, true,
815 handle_alloc_align_attribute, false },
816 { "assume_aligned", 1, 2, false, true, true,
817 handle_assume_aligned_attribute, false },
976d5a22
TT
818 { "designated_init", 0, 0, false, true, false,
819 handle_designated_init_attribute, false },
d5e254e1
IE
820 { "bnd_variable_size", 0, 0, true, false, false,
821 handle_bnd_variable_size_attribute, false },
822 { "bnd_legacy", 0, 0, true, false, false,
823 handle_bnd_legacy, false },
824 { "bnd_instrument", 0, 0, true, false, false,
825 handle_bnd_instrument, false },
62d784f7 826 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
827};
828
829/* Give the specifications for the format attributes, used by C and all
d5e254e1 830 descendants.
349ae713 831
d5e254e1
IE
832 All attributes referencing arguments should be additionally processed
833 in chkp_copy_function_type_adding_bounds for correct instrumentation
834 by Pointer Bounds Checker.
835 Current list of processed format attributes: format, format_arg. */
349ae713
NB
836const struct attribute_spec c_common_format_attribute_table[] =
837{
62d784f7
KT
838 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
839 affects_type_identity } */
349ae713 840 { "format", 3, 3, false, true, true,
62d784f7 841 handle_format_attribute, false },
349ae713 842 { "format_arg", 1, 1, false, true, true,
62d784f7
KT
843 handle_format_arg_attribute, false },
844 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
845};
846
36c5e70a 847/* Return identifier for address space AS. */
3ef0694c 848
36c5e70a
BE
849const char *
850c_addr_space_name (addr_space_t as)
851{
3ef0694c
UW
852 int rid = RID_FIRST_ADDR_SPACE + as;
853 gcc_assert (ridpointers [rid]);
854 return IDENTIFIER_POINTER (ridpointers [rid]);
36c5e70a
BE
855}
856
ec5c56db 857/* Push current bindings for the function name VAR_DECLS. */
7da551a2
RS
858
859void
35b1a6fa 860start_fname_decls (void)
7da551a2 861{
0ba8a114
NS
862 unsigned ix;
863 tree saved = NULL_TREE;
35b1a6fa 864
0ba8a114
NS
865 for (ix = 0; fname_vars[ix].decl; ix++)
866 {
867 tree decl = *fname_vars[ix].decl;
7da551a2 868
0ba8a114
NS
869 if (decl)
870 {
c62c040f
RG
871 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
872 saved);
0ba8a114
NS
873 *fname_vars[ix].decl = NULL_TREE;
874 }
875 }
876 if (saved || saved_function_name_decls)
877 /* Normally they'll have been NULL, so only push if we've got a
878 stack, or they are non-NULL. */
879 saved_function_name_decls = tree_cons (saved, NULL_TREE,
880 saved_function_name_decls);
881}
882
325c3691
RH
883/* Finish up the current bindings, adding them into the current function's
884 statement tree. This must be done _before_ finish_stmt_tree is called.
885 If there is no current function, we must be at file scope and no statements
886 are involved. Pop the previous bindings. */
0ba8a114
NS
887
888void
35b1a6fa 889finish_fname_decls (void)
0ba8a114
NS
890{
891 unsigned ix;
325c3691 892 tree stmts = NULL_TREE;
0ba8a114
NS
893 tree stack = saved_function_name_decls;
894
895 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
325c3691 896 append_to_statement_list (TREE_VALUE (stack), &stmts);
35b1a6fa 897
325c3691 898 if (stmts)
0ba8a114 899 {
325c3691 900 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
bfaba7a9 901
325c3691
RH
902 if (TREE_CODE (*bodyp) == BIND_EXPR)
903 bodyp = &BIND_EXPR_BODY (*bodyp);
6cce57b0 904
86ad3aa9 905 append_to_statement_list_force (*bodyp, &stmts);
325c3691 906 *bodyp = stmts;
0ba8a114 907 }
35b1a6fa 908
0ba8a114
NS
909 for (ix = 0; fname_vars[ix].decl; ix++)
910 *fname_vars[ix].decl = NULL_TREE;
35b1a6fa 911
0ba8a114 912 if (stack)
7da551a2 913 {
ec5c56db 914 /* We had saved values, restore them. */
0ba8a114
NS
915 tree saved;
916
917 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
918 {
919 tree decl = TREE_PURPOSE (saved);
920 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
35b1a6fa 921
0ba8a114
NS
922 *fname_vars[ix].decl = decl;
923 }
924 stack = TREE_CHAIN (stack);
7da551a2 925 }
0ba8a114
NS
926 saved_function_name_decls = stack;
927}
928
6cce57b0 929/* Return the text name of the current function, suitably prettified
0d0bc036 930 by PRETTY_P. Return string must be freed by caller. */
0ba8a114
NS
931
932const char *
35b1a6fa 933fname_as_string (int pretty_p)
0ba8a114 934{
47ab33b2 935 const char *name = "top level";
0d0bc036 936 char *namep;
46c2514e
TT
937 int vrb = 2, len;
938 cpp_string cstr = { 0, 0 }, strname;
47ab33b2 939
3f75a254 940 if (!pretty_p)
47ab33b2
MA
941 {
942 name = "";
943 vrb = 0;
944 }
945
946 if (current_function_decl)
ae2bcd98 947 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
47ab33b2 948
46c2514e 949 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
0d0bc036 950
46c2514e
TT
951 namep = XNEWVEC (char, len);
952 snprintf (namep, len, "\"%s\"", name);
953 strname.text = (unsigned char *) namep;
954 strname.len = len - 1;
0d0bc036 955
b6baa67d 956 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
46c2514e
TT
957 {
958 XDELETEVEC (namep);
959 return (const char *) cstr.text;
0d0bc036 960 }
0d0bc036
AH
961
962 return namep;
0ba8a114
NS
963}
964
0ba8a114
NS
965/* Return the VAR_DECL for a const char array naming the current
966 function. If the VAR_DECL has not yet been created, create it
967 now. RID indicates how it should be formatted and IDENTIFIER_NODE
968 ID is its name (unfortunately C and C++ hold the RID values of
969 keywords in different places, so we can't derive RID from ID in
3ba09659
AH
970 this language independent code. LOC is the location of the
971 function. */
0ba8a114
NS
972
973tree
3ba09659 974fname_decl (location_t loc, unsigned int rid, tree id)
0ba8a114
NS
975{
976 unsigned ix;
977 tree decl = NULL_TREE;
978
979 for (ix = 0; fname_vars[ix].decl; ix++)
980 if (fname_vars[ix].rid == rid)
981 break;
982
983 decl = *fname_vars[ix].decl;
984 if (!decl)
7da551a2 985 {
8d3e27d1
DJ
986 /* If a tree is built here, it would normally have the lineno of
987 the current statement. Later this tree will be moved to the
988 beginning of the function and this line number will be wrong.
989 To avoid this problem set the lineno to 0 here; that prevents
4b7e68e7 990 it from appearing in the RTL. */
325c3691 991 tree stmts;
3c20847b 992 location_t saved_location = input_location;
3c20847b 993 input_location = UNKNOWN_LOCATION;
35b1a6fa 994
325c3691 995 stmts = push_stmt_list ();
c2255bc4 996 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
325c3691
RH
997 stmts = pop_stmt_list (stmts);
998 if (!IS_EMPTY_STMT (stmts))
999 saved_function_name_decls
1000 = tree_cons (decl, stmts, saved_function_name_decls);
0ba8a114 1001 *fname_vars[ix].decl = decl;
3c20847b 1002 input_location = saved_location;
7da551a2 1003 }
0ba8a114 1004 if (!ix && !current_function_decl)
3ba09659 1005 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
6cce57b0 1006
0ba8a114 1007 return decl;
7da551a2
RS
1008}
1009
b84a3874 1010/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b30f223b
RS
1011
1012tree
35b1a6fa 1013fix_string_type (tree value)
b30f223b 1014{
b84a3874
RH
1015 int length = TREE_STRING_LENGTH (value);
1016 int nchars;
c162c75e
MA
1017 tree e_type, i_type, a_type;
1018
b57062ca 1019 /* Compute the number of elements, for the array type. */
b6baa67d
KVH
1020 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1021 {
1022 nchars = length;
1023 e_type = char_type_node;
1024 }
1025 else if (TREE_TYPE (value) == char16_array_type_node)
1026 {
1027 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1028 e_type = char16_type_node;
1029 }
1030 else if (TREE_TYPE (value) == char32_array_type_node)
1031 {
1032 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1033 e_type = char32_type_node;
1034 }
1035 else
1036 {
1037 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1038 e_type = wchar_type_node;
1039 }
b30f223b 1040
89a42ac8
ZW
1041 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1042 limit in C++98 Annex B is very large (65536) and is not normative,
1043 so we do not diagnose it (warn_overlength_strings is forced off
1044 in c_common_post_options). */
1045 if (warn_overlength_strings)
1046 {
1047 const int nchars_max = flag_isoc99 ? 4095 : 509;
1048 const int relevant_std = flag_isoc99 ? 99 : 90;
1049 if (nchars - 1 > nchars_max)
1050 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1051 separate the %d from the 'C'. 'ISO' should not be
1052 translated, but it may be moved after 'C%d' in languages
1053 where modifiers follow nouns. */
509c9d60 1054 pedwarn (input_location, OPT_Woverlength_strings,
fcf73884 1055 "string length %qd is greater than the length %qd "
89a42ac8
ZW
1056 "ISO C%d compilers are required to support",
1057 nchars - 1, nchars_max, relevant_std);
1058 }
1326a48b 1059
cfb10bd3
GDR
1060 /* Create the array type for the string constant. The ISO C++
1061 standard says that a string literal has type `const char[N]' or
1062 `const wchar_t[N]'. We use the same logic when invoked as a C
1063 front-end with -Wwrite-strings.
1064 ??? We should change the type of an expression depending on the
1065 state of a warning flag. We should just be warning -- see how
1066 this is handled in the C++ front-end for the deprecated implicit
1067 conversion from string literals to `char*' or `wchar_t*'.
c162c75e
MA
1068
1069 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1070 array type being the unqualified version of that type.
1071 Therefore, if we are constructing an array of const char, we must
1072 construct the matching unqualified array type first. The C front
1073 end does not require this, but it does no harm, so we do it
1074 unconditionally. */
c62c040f 1075 i_type = build_index_type (size_int (nchars - 1));
c162c75e 1076 a_type = build_array_type (e_type, i_type);
cfb10bd3 1077 if (c_dialect_cxx() || warn_write_strings)
46df2823 1078 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
d9cf7c82 1079
c162c75e 1080 TREE_TYPE (value) = a_type;
ccd4c832 1081 TREE_CONSTANT (value) = 1;
3521b33c 1082 TREE_READONLY (value) = 1;
b30f223b
RS
1083 TREE_STATIC (value) = 1;
1084 return value;
1085}
1086\f
02614448
ILT
1087/* If DISABLE is true, stop issuing warnings. This is used when
1088 parsing code that we know will not be executed. This function may
1089 be called multiple times, and works as a stack. */
1090
1091static void
1092c_disable_warnings (bool disable)
1093{
1094 if (disable)
1095 {
1096 ++c_inhibit_evaluation_warnings;
1097 fold_defer_overflow_warnings ();
1098 }
1099}
1100
1101/* If ENABLE is true, reenable issuing warnings. */
1102
1103static void
1104c_enable_warnings (bool enable)
1105{
1106 if (enable)
1107 {
1108 --c_inhibit_evaluation_warnings;
1109 fold_undefer_and_ignore_overflow_warnings ();
1110 }
1111}
1112
928c19bb
JM
1113/* Fully fold EXPR, an expression that was not folded (beyond integer
1114 constant expressions and null pointer constants) when being built
1115 up. If IN_INIT, this is in a static initializer and certain
1116 changes are made to the folding done. Clear *MAYBE_CONST if
1117 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1118 expression because it contains an evaluated operator (in C99) or an
1119 operator outside of sizeof returning an integer constant (in C90)
1120 not permitted in constant expressions, or because it contains an
1121 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1122 set to true by callers before calling this function.) Return the
1123 folded expression. Function arguments have already been folded
1124 before calling this function, as have the contents of SAVE_EXPR,
1125 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1126 C_MAYBE_CONST_EXPR. */
1127
1128tree
1129c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1130{
1131 tree ret;
8ce94e44 1132 tree eptype = NULL_TREE;
928c19bb
JM
1133 bool dummy = true;
1134 bool maybe_const_itself = true;
db3927fb 1135 location_t loc = EXPR_LOCATION (expr);
928c19bb
JM
1136
1137 /* This function is not relevant to C++ because C++ folds while
1138 parsing, and may need changes to be correct for C++ when C++
1139 stops folding while parsing. */
1140 if (c_dialect_cxx ())
1141 gcc_unreachable ();
1142
1143 if (!maybe_const)
1144 maybe_const = &dummy;
8ce94e44
JM
1145 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1146 {
1147 eptype = TREE_TYPE (expr);
1148 expr = TREE_OPERAND (expr, 0);
1149 }
928c19bb
JM
1150 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1151 &maybe_const_itself);
8ce94e44 1152 if (eptype)
db3927fb 1153 ret = fold_convert_loc (loc, eptype, ret);
928c19bb
JM
1154 *maybe_const &= maybe_const_itself;
1155 return ret;
1156}
1157
1158/* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1159 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1160 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1161 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1162 both evaluated and unevaluated subexpressions while
1163 *MAYBE_CONST_ITSELF is carried from only evaluated
1164 subexpressions). */
1165
1166static tree
1167c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1168 bool *maybe_const_itself)
1169{
1170 tree ret = expr;
1171 enum tree_code code = TREE_CODE (expr);
1172 enum tree_code_class kind = TREE_CODE_CLASS (code);
1173 location_t loc = EXPR_LOCATION (expr);
1174 tree op0, op1, op2, op3;
1175 tree orig_op0, orig_op1, orig_op2;
1176 bool op0_const = true, op1_const = true, op2_const = true;
1177 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1178 bool nowarning = TREE_NO_WARNING (expr);
02614448 1179 bool unused_p;
928c19bb
JM
1180
1181 /* This function is not relevant to C++ because C++ folds while
1182 parsing, and may need changes to be correct for C++ when C++
1183 stops folding while parsing. */
1184 if (c_dialect_cxx ())
1185 gcc_unreachable ();
1186
1187 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1188 anything else not counted as an expression cannot usefully be
1189 folded further at this point. */
1190 if (!IS_EXPR_CODE_CLASS (kind)
1191 || kind == tcc_statement
1192 || code == SAVE_EXPR)
1193 return expr;
1194
1195 /* Operands of variable-length expressions (function calls) have
1196 already been folded, as have __builtin_* function calls, and such
1197 expressions cannot occur in constant expressions. */
1198 if (kind == tcc_vl_exp)
1199 {
1200 *maybe_const_operands = false;
1201 ret = fold (expr);
1202 goto out;
1203 }
1204
1205 if (code == C_MAYBE_CONST_EXPR)
1206 {
1207 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1208 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1209 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1210 *maybe_const_operands = false;
1211 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1212 *maybe_const_itself = false;
1213 if (pre && !in_init)
1214 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1215 else
1216 ret = inner;
1217 goto out;
1218 }
1219
1220 /* Assignment, increment, decrement, function call and comma
1221 operators, and statement expressions, cannot occur in constant
1222 expressions if evaluated / outside of sizeof. (Function calls
1223 were handled above, though VA_ARG_EXPR is treated like a function
1224 call here, and statement expressions are handled through
1225 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1226 switch (code)
1227 {
1228 case MODIFY_EXPR:
1229 case PREDECREMENT_EXPR:
1230 case PREINCREMENT_EXPR:
1231 case POSTDECREMENT_EXPR:
1232 case POSTINCREMENT_EXPR:
1233 case COMPOUND_EXPR:
1234 *maybe_const_operands = false;
1235 break;
1236
1237 case VA_ARG_EXPR:
1238 case TARGET_EXPR:
1239 case BIND_EXPR:
1240 case OBJ_TYPE_REF:
1241 *maybe_const_operands = false;
1242 ret = fold (expr);
1243 goto out;
1244
1245 default:
1246 break;
1247 }
1248
1249 /* Fold individual tree codes as appropriate. */
1250 switch (code)
1251 {
1252 case COMPOUND_LITERAL_EXPR:
1253 /* Any non-constancy will have been marked in a containing
1254 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1255 goto out;
1256
1257 case COMPONENT_REF:
1258 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1259 op1 = TREE_OPERAND (expr, 1);
1260 op2 = TREE_OPERAND (expr, 2);
1261 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1262 maybe_const_itself);
e5a94231 1263 STRIP_TYPE_NOPS (op0);
928c19bb
JM
1264 if (op0 != orig_op0)
1265 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1266 if (ret != expr)
1267 {
1268 TREE_READONLY (ret) = TREE_READONLY (expr);
1269 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1270 }
1271 goto out;
1272
1273 case ARRAY_REF:
1274 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1275 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1276 op2 = TREE_OPERAND (expr, 2);
1277 op3 = TREE_OPERAND (expr, 3);
1278 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1279 maybe_const_itself);
e5a94231 1280 STRIP_TYPE_NOPS (op0);
928c19bb
JM
1281 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1282 maybe_const_itself);
e5a94231 1283 STRIP_TYPE_NOPS (op1);
928c19bb
JM
1284 op1 = decl_constant_value_for_optimization (op1);
1285 if (op0 != orig_op0 || op1 != orig_op1)
1286 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1287 if (ret != expr)
1288 {
1289 TREE_READONLY (ret) = TREE_READONLY (expr);
1290 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1291 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1292 }
1293 ret = fold (ret);
1294 goto out;
1295
1296 case COMPOUND_EXPR:
1297 case MODIFY_EXPR:
1298 case PREDECREMENT_EXPR:
1299 case PREINCREMENT_EXPR:
1300 case POSTDECREMENT_EXPR:
1301 case POSTINCREMENT_EXPR:
1302 case PLUS_EXPR:
1303 case MINUS_EXPR:
1304 case MULT_EXPR:
1305 case POINTER_PLUS_EXPR:
1306 case TRUNC_DIV_EXPR:
1307 case CEIL_DIV_EXPR:
1308 case FLOOR_DIV_EXPR:
1309 case TRUNC_MOD_EXPR:
1310 case RDIV_EXPR:
1311 case EXACT_DIV_EXPR:
1312 case LSHIFT_EXPR:
1313 case RSHIFT_EXPR:
1314 case BIT_IOR_EXPR:
1315 case BIT_XOR_EXPR:
1316 case BIT_AND_EXPR:
1317 case LT_EXPR:
1318 case LE_EXPR:
1319 case GT_EXPR:
1320 case GE_EXPR:
1321 case EQ_EXPR:
1322 case NE_EXPR:
1323 case COMPLEX_EXPR:
1324 case TRUTH_AND_EXPR:
1325 case TRUTH_OR_EXPR:
1326 case TRUTH_XOR_EXPR:
1327 case UNORDERED_EXPR:
1328 case ORDERED_EXPR:
1329 case UNLT_EXPR:
1330 case UNLE_EXPR:
1331 case UNGT_EXPR:
1332 case UNGE_EXPR:
1333 case UNEQ_EXPR:
1334 /* Binary operations evaluating both arguments (increment and
1335 decrement are binary internally in GCC). */
1336 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1337 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1338 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1339 maybe_const_itself);
e5a94231 1340 STRIP_TYPE_NOPS (op0);
928c19bb
JM
1341 if (code != MODIFY_EXPR
1342 && code != PREDECREMENT_EXPR
1343 && code != PREINCREMENT_EXPR
1344 && code != POSTDECREMENT_EXPR
1345 && code != POSTINCREMENT_EXPR)
1346 op0 = decl_constant_value_for_optimization (op0);
1347 /* The RHS of a MODIFY_EXPR was fully folded when building that
1348 expression for the sake of conversion warnings. */
1349 if (code != MODIFY_EXPR)
1350 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1351 maybe_const_itself);
e5a94231 1352 STRIP_TYPE_NOPS (op1);
928c19bb
JM
1353 op1 = decl_constant_value_for_optimization (op1);
1354 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1355 ret = in_init
db3927fb
AH
1356 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1357 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
928c19bb
JM
1358 else
1359 ret = fold (expr);
f5178456
RS
1360 if (TREE_OVERFLOW_P (ret)
1361 && !TREE_OVERFLOW_P (op0)
1362 && !TREE_OVERFLOW_P (op1))
1363 overflow_warning (EXPR_LOCATION (expr), ret);
cc83c823
JJ
1364 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1365 && TREE_CODE (orig_op1) != INTEGER_CST
1366 && TREE_CODE (op1) == INTEGER_CST
1367 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1368 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1369 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1370 && c_inhibit_evaluation_warnings == 0)
1371 {
1372 if (tree_int_cst_sgn (op1) < 0)
4853031e
MP
1373 warning_at (loc, OPT_Wshift_count_negative,
1374 (code == LSHIFT_EXPR
1375 ? G_("left shift count is negative")
1376 : G_("right shift count is negative")));
cc83c823
JJ
1377 else if (compare_tree_int (op1,
1378 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1379 >= 0)
4853031e
MP
1380 warning_at (loc, OPT_Wshift_count_overflow,
1381 (code == LSHIFT_EXPR
1382 ? G_("left shift count >= width of type")
1383 : G_("right shift count >= width of type")));
cc83c823 1384 }
4a9a42ab
MP
1385 if ((code == TRUNC_DIV_EXPR
1386 || code == CEIL_DIV_EXPR
1387 || code == FLOOR_DIV_EXPR
1388 || code == EXACT_DIV_EXPR
1389 || code == TRUNC_MOD_EXPR)
1390 && TREE_CODE (orig_op1) != INTEGER_CST
1391 && TREE_CODE (op1) == INTEGER_CST
1392 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1393 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1394 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1395 warn_for_div_by_zero (loc, op1);
928c19bb
JM
1396 goto out;
1397
1398 case INDIRECT_REF:
1399 case FIX_TRUNC_EXPR:
1400 case FLOAT_EXPR:
1401 CASE_CONVERT:
2ce064c3 1402 case ADDR_SPACE_CONVERT_EXPR:
982d62f6 1403 case VIEW_CONVERT_EXPR:
928c19bb
JM
1404 case NON_LVALUE_EXPR:
1405 case NEGATE_EXPR:
1406 case BIT_NOT_EXPR:
1407 case TRUTH_NOT_EXPR:
1408 case ADDR_EXPR:
1409 case CONJ_EXPR:
1410 case REALPART_EXPR:
1411 case IMAGPART_EXPR:
1412 /* Unary operations. */
1413 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1414 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1415 maybe_const_itself);
e5a94231 1416 STRIP_TYPE_NOPS (op0);
928c19bb
JM
1417 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1418 op0 = decl_constant_value_for_optimization (op0);
693ddb1b
EB
1419 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1420 not prepared to deal with them if they occur in initializers. */
1421 if (op0 != orig_op0
1422 && code == ADDR_EXPR
1423 && (op1 = get_base_address (op0)) != NULL_TREE
1424 && TREE_CODE (op1) == INDIRECT_REF
1425 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
cf9e9959 1426 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
693ddb1b 1427 else if (op0 != orig_op0 || in_init)
928c19bb 1428 ret = in_init
db3927fb
AH
1429 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1430 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
928c19bb
JM
1431 else
1432 ret = fold (expr);
1433 if (code == INDIRECT_REF
1434 && ret != expr
1435 && TREE_CODE (ret) == INDIRECT_REF)
1436 {
1437 TREE_READONLY (ret) = TREE_READONLY (expr);
1438 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1439 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1440 }
f5178456
RS
1441 switch (code)
1442 {
1443 case FIX_TRUNC_EXPR:
1444 case FLOAT_EXPR:
1445 CASE_CONVERT:
1446 /* Don't warn about explicit conversions. We will already
1447 have warned about suspect implicit conversions. */
1448 break;
1449
1450 default:
1451 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1452 overflow_warning (EXPR_LOCATION (expr), ret);
1453 break;
1454 }
928c19bb
JM
1455 goto out;
1456
1457 case TRUTH_ANDIF_EXPR:
1458 case TRUTH_ORIF_EXPR:
1459 /* Binary operations not necessarily evaluating both
1460 arguments. */
1461 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1462 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1463 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
e5a94231 1464 STRIP_TYPE_NOPS (op0);
f5178456
RS
1465
1466 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1467 ? truthvalue_false_node
1468 : truthvalue_true_node));
02614448 1469 c_disable_warnings (unused_p);
928c19bb 1470 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
e5a94231 1471 STRIP_TYPE_NOPS (op1);
02614448 1472 c_enable_warnings (unused_p);
f5178456 1473
928c19bb
JM
1474 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1475 ret = in_init
db3927fb
AH
1476 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1477 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
928c19bb
JM
1478 else
1479 ret = fold (expr);
1480 *maybe_const_operands &= op0_const;
1481 *maybe_const_itself &= op0_const_self;
1482 if (!(flag_isoc99
1483 && op0_const
1484 && op0_const_self
1485 && (code == TRUTH_ANDIF_EXPR
1486 ? op0 == truthvalue_false_node
1487 : op0 == truthvalue_true_node)))
1488 *maybe_const_operands &= op1_const;
1489 if (!(op0_const
1490 && op0_const_self
1491 && (code == TRUTH_ANDIF_EXPR
1492 ? op0 == truthvalue_false_node
1493 : op0 == truthvalue_true_node)))
1494 *maybe_const_itself &= op1_const_self;
1495 goto out;
1496
1497 case COND_EXPR:
1498 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1499 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1500 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1501 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
f5178456 1502
e5a94231 1503 STRIP_TYPE_NOPS (op0);
02614448 1504 c_disable_warnings (op0 == truthvalue_false_node);
928c19bb 1505 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
e5a94231 1506 STRIP_TYPE_NOPS (op1);
02614448 1507 c_enable_warnings (op0 == truthvalue_false_node);
f5178456 1508
02614448 1509 c_disable_warnings (op0 == truthvalue_true_node);
928c19bb 1510 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
e5a94231 1511 STRIP_TYPE_NOPS (op2);
02614448 1512 c_enable_warnings (op0 == truthvalue_true_node);
f5178456 1513
928c19bb 1514 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
db3927fb 1515 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
928c19bb
JM
1516 else
1517 ret = fold (expr);
1518 *maybe_const_operands &= op0_const;
1519 *maybe_const_itself &= op0_const_self;
1520 if (!(flag_isoc99
1521 && op0_const
1522 && op0_const_self
1523 && op0 == truthvalue_false_node))
1524 *maybe_const_operands &= op1_const;
1525 if (!(op0_const
1526 && op0_const_self
1527 && op0 == truthvalue_false_node))
1528 *maybe_const_itself &= op1_const_self;
1529 if (!(flag_isoc99
1530 && op0_const
1531 && op0_const_self
1532 && op0 == truthvalue_true_node))
1533 *maybe_const_operands &= op2_const;
1534 if (!(op0_const
1535 && op0_const_self
1536 && op0 == truthvalue_true_node))
1537 *maybe_const_itself &= op2_const_self;
1538 goto out;
1539
8ce94e44
JM
1540 case EXCESS_PRECISION_EXPR:
1541 /* Each case where an operand with excess precision may be
1542 encountered must remove the EXCESS_PRECISION_EXPR around
1543 inner operands and possibly put one around the whole
1544 expression or possibly convert to the semantic type (which
1545 c_fully_fold does); we cannot tell at this stage which is
1546 appropriate in any particular case. */
1547 gcc_unreachable ();
1548
928c19bb
JM
1549 default:
1550 /* Various codes may appear through folding built-in functions
1551 and their arguments. */
1552 goto out;
1553 }
1554
1555 out:
1556 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1557 have been done by this point, so remove them again. */
1558 nowarning |= TREE_NO_WARNING (ret);
1559 STRIP_TYPE_NOPS (ret);
1560 if (nowarning && !TREE_NO_WARNING (ret))
1561 {
1562 if (!CAN_HAVE_LOCATION_P (ret))
1563 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1564 TREE_NO_WARNING (ret) = 1;
1565 }
1566 if (ret != expr)
1567 protected_set_expr_location (ret, loc);
1568 return ret;
1569}
1570
1571/* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1572 return EXP. Otherwise, return either EXP or its known constant
1573 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1574 Is the BLKmode test appropriate? */
1575
1576tree
1577decl_constant_value_for_optimization (tree exp)
1578{
1579 tree ret;
1580
1581 /* This function is only used by C, for c_fully_fold and other
1582 optimization, and may not be correct for C++. */
1583 if (c_dialect_cxx ())
1584 gcc_unreachable ();
1585
1586 if (!optimize
1587 || TREE_CODE (exp) != VAR_DECL
1588 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1589 || DECL_MODE (exp) == BLKmode)
1590 return exp;
1591
1592 ret = decl_constant_value (exp);
1593 /* Avoid unwanted tree sharing between the initializer and current
1594 function's body where the tree can be modified e.g. by the
1595 gimplifier. */
1596 if (ret != exp && TREE_STATIC (exp))
1597 ret = unshare_expr (ret);
1598 return ret;
1599}
1600
d74154d5
RS
1601/* Print a warning if a constant expression had overflow in folding.
1602 Invoke this function on every expression that the language
1603 requires to be a constant expression.
1604 Note the ANSI C standard says it is erroneous for a
1605 constant expression to overflow. */
96571883
BK
1606
1607void
35b1a6fa 1608constant_expression_warning (tree value)
393eda6a 1609{
b8698a0f 1610 if (warn_overflow && pedantic
393eda6a
MLI
1611 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1612 || TREE_CODE (value) == FIXED_CST
1613 || TREE_CODE (value) == VECTOR_CST
1614 || TREE_CODE (value) == COMPLEX_CST)
1615 && TREE_OVERFLOW (value))
509c9d60 1616 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
393eda6a
MLI
1617}
1618
1619/* The same as above but print an unconditional error. */
1620void
1621constant_expression_error (tree value)
96571883 1622{
c05f751c 1623 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
ab22c1fa 1624 || TREE_CODE (value) == FIXED_CST
69ef87e2 1625 || TREE_CODE (value) == VECTOR_CST
c05f751c 1626 || TREE_CODE (value) == COMPLEX_CST)
393eda6a
MLI
1627 && TREE_OVERFLOW (value))
1628 error ("overflow in constant expression");
d74154d5
RS
1629}
1630
59c0753d
MLI
1631/* Print a warning if an expression had overflow in folding and its
1632 operands hadn't.
1633
d74154d5
RS
1634 Invoke this function on every expression that
1635 (1) appears in the source code, and
59c0753d 1636 (2) is a constant expression that overflowed, and
d74154d5 1637 (3) is not already checked by convert_and_check;
59c0753d
MLI
1638 however, do not invoke this function on operands of explicit casts
1639 or when the expression is the result of an operator and any operand
1640 already overflowed. */
d74154d5
RS
1641
1642void
c2255bc4 1643overflow_warning (location_t loc, tree value)
d74154d5 1644{
7d882b83
ILT
1645 if (c_inhibit_evaluation_warnings != 0)
1646 return;
59c0753d
MLI
1647
1648 switch (TREE_CODE (value))
69ef87e2 1649 {
59c0753d 1650 case INTEGER_CST:
c2255bc4 1651 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
59c0753d 1652 break;
b8698a0f 1653
59c0753d 1654 case REAL_CST:
c2255bc4
AH
1655 warning_at (loc, OPT_Woverflow,
1656 "floating point overflow in expression");
59c0753d 1657 break;
b8698a0f 1658
ab22c1fa 1659 case FIXED_CST:
c2255bc4 1660 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
ab22c1fa
CF
1661 break;
1662
59c0753d 1663 case VECTOR_CST:
c2255bc4 1664 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
59c0753d 1665 break;
b8698a0f 1666
59c0753d
MLI
1667 case COMPLEX_CST:
1668 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
c2255bc4
AH
1669 warning_at (loc, OPT_Woverflow,
1670 "complex integer overflow in expression");
59c0753d 1671 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
c2255bc4
AH
1672 warning_at (loc, OPT_Woverflow,
1673 "complex floating point overflow in expression");
59c0753d
MLI
1674 break;
1675
1676 default:
1677 break;
69ef87e2 1678 }
d74154d5
RS
1679}
1680
ca409efd
MLI
1681/* Warn about uses of logical || / && operator in a context where it
1682 is likely that the bitwise equivalent was intended by the
1683 programmer. We have seen an expression in which CODE is a binary
a243fb4a
MLI
1684 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1685 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
63a08740 1686void
a243fb4a 1687warn_logical_operator (location_t location, enum tree_code code, tree type,
b8698a0f 1688 enum tree_code code_left, tree op_left,
ca409efd 1689 enum tree_code ARG_UNUSED (code_right), tree op_right)
63a08740 1690{
a243fb4a
MLI
1691 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1692 int in0_p, in1_p, in_p;
1693 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1694 bool strict_overflow_p = false;
1695
ca409efd
MLI
1696 if (code != TRUTH_ANDIF_EXPR
1697 && code != TRUTH_AND_EXPR
1698 && code != TRUTH_ORIF_EXPR
1699 && code != TRUTH_OR_EXPR)
1700 return;
1701
b8787813
MP
1702 /* We don't want to warn if either operand comes from a macro
1703 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1704 see PR61534. */
1705 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1706 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1707 return;
1708
ca409efd
MLI
1709 /* Warn if &&/|| are being used in a context where it is
1710 likely that the bitwise equivalent was intended by the
1711 programmer. That is, an expression such as op && MASK
1712 where op should not be any boolean expression, nor a
1713 constant, and mask seems to be a non-boolean integer constant. */
1714 if (!truth_value_p (code_left)
1715 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1716 && !CONSTANT_CLASS_P (op_left)
1717 && !TREE_NO_WARNING (op_left)
1718 && TREE_CODE (op_right) == INTEGER_CST
1719 && !integer_zerop (op_right)
1720 && !integer_onep (op_right))
63a08740 1721 {
a243fb4a 1722 if (or_op)
ca409efd
MLI
1723 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1724 " applied to non-boolean constant");
1725 else
1726 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1727 " applied to non-boolean constant");
1728 TREE_NO_WARNING (op_left) = true;
a243fb4a
MLI
1729 return;
1730 }
1731
1732 /* We do not warn for constants because they are typical of macro
1733 expansions that test for features. */
1734 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1735 return;
1736
1737 /* This warning only makes sense with logical operands. */
1738 if (!(truth_value_p (TREE_CODE (op_left))
1739 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1740 || !(truth_value_p (TREE_CODE (op_right))
1741 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1742 return;
1743
3c9aabbd
MG
1744 /* The range computations only work with scalars. */
1745 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1746 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1747 return;
a243fb4a 1748
f2c4a785
MLI
1749 /* We first test whether either side separately is trivially true
1750 (with OR) or trivially false (with AND). If so, do not warn.
1751 This is a common idiom for testing ranges of data types in
1752 portable code. */
1753 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1754 if (!lhs)
1755 return;
1756 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
a243fb4a
MLI
1757 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1758
f2c4a785
MLI
1759 /* If this is an OR operation, invert both sides; now, the result
1760 should be always false to get a warning. */
1761 if (or_op)
1762 in0_p = !in0_p;
1763
1764 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1e537948 1765 if (tem && integer_zerop (tem))
f2c4a785
MLI
1766 return;
1767
1768 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1769 if (!rhs)
1770 return;
1771 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
a243fb4a 1772 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
b8698a0f 1773
f2c4a785
MLI
1774 /* If this is an OR operation, invert both sides; now, the result
1775 should be always false to get a warning. */
a243fb4a 1776 if (or_op)
f2c4a785 1777 in1_p = !in1_p;
b8698a0f 1778
f2c4a785 1779 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1e537948 1780 if (tem && integer_zerop (tem))
f2c4a785
MLI
1781 return;
1782
1783 /* If both expressions have the same operand, if we can merge the
8c2b7f79 1784 ranges, ... */
f2c4a785 1785 if (operand_equal_p (lhs, rhs, 0)
a243fb4a 1786 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
8c2b7f79 1787 in1_p, low1, high1))
a243fb4a 1788 {
8c2b7f79
MP
1789 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1790 /* ... and if the range test is always false, then warn. */
1791 if (tem && integer_zerop (tem))
1792 {
1793 if (or_op)
1794 warning_at (location, OPT_Wlogical_op,
1795 "logical %<or%> of collectively exhaustive tests is "
1796 "always true");
1797 else
1798 warning_at (location, OPT_Wlogical_op,
1799 "logical %<and%> of mutually exclusive tests is "
1800 "always false");
1801 }
1802 /* Or warn if the operands have exactly the same range, e.g.
1803 A > 0 && A > 0. */
1804 else if (low0 == low1 && high0 == high1)
1805 {
1806 if (or_op)
1807 warning_at (location, OPT_Wlogical_op,
1808 "logical %<or%> of equal expressions");
1809 else
1810 warning_at (location, OPT_Wlogical_op,
1811 "logical %<and%> of equal expressions");
1812 }
63a08740
DM
1813 }
1814}
1815
742938c9
MP
1816/* Warn about logical not used on the left hand side operand of a comparison.
1817 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
59ea0364 1818 Do not warn if RHS is of a boolean type. */
742938c9
MP
1819
1820void
1821warn_logical_not_parentheses (location_t location, enum tree_code code,
59ea0364 1822 tree rhs)
742938c9 1823{
59ea0364
MP
1824 if (TREE_CODE_CLASS (code) != tcc_comparison
1825 || TREE_TYPE (rhs) == NULL_TREE
1826 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
742938c9
MP
1827 return;
1828
7ccb1a11
JJ
1829 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1830 !(x == 0) or !(y != 0). */
1831 if ((code == EQ_EXPR || code == NE_EXPR)
1832 && integer_zerop (rhs))
1833 return;
1834
742938c9
MP
1835 warning_at (location, OPT_Wlogical_not_parentheses,
1836 "logical not is only applied to the left hand side of "
1837 "comparison");
1838}
63a08740 1839
fd4116f4
MLI
1840/* Warn if EXP contains any computations whose results are not used.
1841 Return true if a warning is printed; false otherwise. LOCUS is the
1842 (potential) location of the expression. */
1843
1844bool
1845warn_if_unused_value (const_tree exp, location_t locus)
1846{
1847 restart:
1848 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1849 return false;
1850
1851 /* Don't warn about void constructs. This includes casting to void,
1852 void function calls, and statement expressions with a final cast
1853 to void. */
1854 if (VOID_TYPE_P (TREE_TYPE (exp)))
1855 return false;
1856
1857 if (EXPR_HAS_LOCATION (exp))
1858 locus = EXPR_LOCATION (exp);
1859
1860 switch (TREE_CODE (exp))
1861 {
1862 case PREINCREMENT_EXPR:
1863 case POSTINCREMENT_EXPR:
1864 case PREDECREMENT_EXPR:
1865 case POSTDECREMENT_EXPR:
1866 case MODIFY_EXPR:
1867 case INIT_EXPR:
1868 case TARGET_EXPR:
1869 case CALL_EXPR:
1870 case TRY_CATCH_EXPR:
1871 case WITH_CLEANUP_EXPR:
1872 case EXIT_EXPR:
1873 case VA_ARG_EXPR:
1874 return false;
1875
1876 case BIND_EXPR:
1877 /* For a binding, warn if no side effect within it. */
1878 exp = BIND_EXPR_BODY (exp);
1879 goto restart;
1880
1881 case SAVE_EXPR:
1882 case NON_LVALUE_EXPR:
007a787d 1883 case NOP_EXPR:
fd4116f4
MLI
1884 exp = TREE_OPERAND (exp, 0);
1885 goto restart;
1886
1887 case TRUTH_ORIF_EXPR:
1888 case TRUTH_ANDIF_EXPR:
1889 /* In && or ||, warn if 2nd operand has no side effect. */
1890 exp = TREE_OPERAND (exp, 1);
1891 goto restart;
1892
1893 case COMPOUND_EXPR:
1894 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1895 return true;
1896 /* Let people do `(foo (), 0)' without a warning. */
1897 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1898 return false;
1899 exp = TREE_OPERAND (exp, 1);
1900 goto restart;
1901
1902 case COND_EXPR:
1903 /* If this is an expression with side effects, don't warn; this
1904 case commonly appears in macro expansions. */
1905 if (TREE_SIDE_EFFECTS (exp))
1906 return false;
1907 goto warn;
1908
1909 case INDIRECT_REF:
1910 /* Don't warn about automatic dereferencing of references, since
1911 the user cannot control it. */
1912 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1913 {
1914 exp = TREE_OPERAND (exp, 0);
1915 goto restart;
1916 }
1917 /* Fall through. */
1918
1919 default:
1920 /* Referencing a volatile value is a side effect, so don't warn. */
1921 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1922 && TREE_THIS_VOLATILE (exp))
1923 return false;
1924
1925 /* If this is an expression which has no operands, there is no value
1926 to be unused. There are no such language-independent codes,
1927 but front ends may define such. */
1928 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1929 return false;
1930
1931 warn:
1932 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1933 }
1934}
1935
1936
de9c56a4
RG
1937/* Print a warning about casts that might indicate violation
1938 of strict aliasing rules if -Wstrict-aliasing is used and
3f0a2a47
DM
1939 strict aliasing mode is in effect. OTYPE is the original
1940 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
de9c56a4 1941
79bedddc 1942bool
3f0a2a47 1943strict_aliasing_warning (tree otype, tree type, tree expr)
de9c56a4 1944{
255d3827
RG
1945 /* Strip pointer conversion chains and get to the correct original type. */
1946 STRIP_NOPS (expr);
1947 otype = TREE_TYPE (expr);
1948
ac7ee6ad
RG
1949 if (!(flag_strict_aliasing
1950 && POINTER_TYPE_P (type)
1951 && POINTER_TYPE_P (otype)
1952 && !VOID_TYPE_P (TREE_TYPE (type)))
1953 /* If the type we are casting to is a ref-all pointer
1954 dereferencing it is always valid. */
1955 || TYPE_REF_CAN_ALIAS_ALL (type))
79bedddc
SR
1956 return false;
1957
1958 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
de9c56a4 1959 && (DECL_P (TREE_OPERAND (expr, 0))
79bedddc 1960 || handled_component_p (TREE_OPERAND (expr, 0))))
de9c56a4
RG
1961 {
1962 /* Casting the address of an object to non void pointer. Warn
1963 if the cast breaks type based aliasing. */
79bedddc
SR
1964 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1965 {
1966 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1967 "might break strict-aliasing rules");
1968 return true;
1969 }
de9c56a4
RG
1970 else
1971 {
b8698a0f 1972 /* warn_strict_aliasing >= 3. This includes the default (3).
79bedddc 1973 Only warn if the cast is dereferenced immediately. */
4862826d 1974 alias_set_type set1 =
79bedddc 1975 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
4862826d 1976 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
de9c56a4 1977
4653cae5
RG
1978 if (set1 != set2 && set2 != 0
1979 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
79bedddc
SR
1980 {
1981 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1982 "pointer will break strict-aliasing rules");
1983 return true;
1984 }
1985 else if (warn_strict_aliasing == 2
836f7794 1986 && !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1987 {
1988 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1989 "pointer might break strict-aliasing rules");
1990 return true;
1991 }
de9c56a4
RG
1992 }
1993 }
79bedddc
SR
1994 else
1995 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1996 {
1997 /* At this level, warn for any conversions, even if an address is
1998 not taken in the same statement. This will likely produce many
1999 false positives, but could be useful to pinpoint problems that
2000 are not revealed at higher levels. */
4862826d
ILT
2001 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2002 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2003 if (!COMPLETE_TYPE_P (type)
836f7794 2004 || !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
2005 {
2006 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2007 "pointer might break strict-aliasing rules");
2008 return true;
2009 }
2010 }
2011
2012 return false;
de9c56a4
RG
2013}
2014
1a4049e7
JJ
2015/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2016 sizeof as last operand of certain builtins. */
2017
2018void
3a785c97 2019sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
9771b263 2020 vec<tree, va_gc> *params, tree *sizeof_arg,
1a4049e7
JJ
2021 bool (*comp_types) (tree, tree))
2022{
2023 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
3a785c97
JJ
2024 bool strop = false, cmp = false;
2025 unsigned int idx = ~0;
2026 location_t loc;
1a4049e7
JJ
2027
2028 if (TREE_CODE (callee) != FUNCTION_DECL
2029 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
9771b263 2030 || vec_safe_length (params) <= 1)
1a4049e7
JJ
2031 return;
2032
1a4049e7
JJ
2033 switch (DECL_FUNCTION_CODE (callee))
2034 {
2035 case BUILT_IN_STRNCMP:
2036 case BUILT_IN_STRNCASECMP:
3a785c97
JJ
2037 cmp = true;
2038 /* FALLTHRU */
1a4049e7 2039 case BUILT_IN_STRNCPY:
3a785c97 2040 case BUILT_IN_STRNCPY_CHK:
1a4049e7 2041 case BUILT_IN_STRNCAT:
3a785c97
JJ
2042 case BUILT_IN_STRNCAT_CHK:
2043 case BUILT_IN_STPNCPY:
2044 case BUILT_IN_STPNCPY_CHK:
1a4049e7
JJ
2045 strop = true;
2046 /* FALLTHRU */
2047 case BUILT_IN_MEMCPY:
3a785c97 2048 case BUILT_IN_MEMCPY_CHK:
1a4049e7 2049 case BUILT_IN_MEMMOVE:
3a785c97 2050 case BUILT_IN_MEMMOVE_CHK:
9771b263 2051 if (params->length () < 3)
3a785c97 2052 return;
9771b263
DN
2053 src = (*params)[1];
2054 dest = (*params)[0];
3a785c97
JJ
2055 idx = 2;
2056 break;
2057 case BUILT_IN_BCOPY:
9771b263 2058 if (params->length () < 3)
3a785c97 2059 return;
9771b263
DN
2060 src = (*params)[0];
2061 dest = (*params)[1];
3a785c97
JJ
2062 idx = 2;
2063 break;
1a4049e7 2064 case BUILT_IN_MEMCMP:
3a785c97 2065 case BUILT_IN_BCMP:
9771b263 2066 if (params->length () < 3)
1a4049e7 2067 return;
9771b263
DN
2068 src = (*params)[1];
2069 dest = (*params)[0];
3a785c97
JJ
2070 idx = 2;
2071 cmp = true;
1a4049e7
JJ
2072 break;
2073 case BUILT_IN_MEMSET:
3a785c97 2074 case BUILT_IN_MEMSET_CHK:
9771b263 2075 if (params->length () < 3)
1a4049e7 2076 return;
9771b263 2077 dest = (*params)[0];
3a785c97
JJ
2078 idx = 2;
2079 break;
2080 case BUILT_IN_BZERO:
9771b263 2081 dest = (*params)[0];
3a785c97 2082 idx = 1;
1a4049e7
JJ
2083 break;
2084 case BUILT_IN_STRNDUP:
9771b263 2085 src = (*params)[0];
1a4049e7 2086 strop = true;
3a785c97
JJ
2087 idx = 1;
2088 break;
2089 case BUILT_IN_MEMCHR:
9771b263 2090 if (params->length () < 3)
3a785c97 2091 return;
9771b263 2092 src = (*params)[0];
3a785c97
JJ
2093 idx = 2;
2094 break;
2095 case BUILT_IN_SNPRINTF:
2096 case BUILT_IN_SNPRINTF_CHK:
2097 case BUILT_IN_VSNPRINTF:
2098 case BUILT_IN_VSNPRINTF_CHK:
9771b263 2099 dest = (*params)[0];
3a785c97
JJ
2100 idx = 1;
2101 strop = true;
1a4049e7
JJ
2102 break;
2103 default:
2104 break;
2105 }
2106
3a785c97
JJ
2107 if (idx >= 3)
2108 return;
2109
2110 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2111 return;
2112
2113 type = TYPE_P (sizeof_arg[idx])
2114 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2115 if (!POINTER_TYPE_P (type))
2116 return;
2117
1a4049e7
JJ
2118 if (dest
2119 && (tem = tree_strip_nop_conversions (dest))
2120 && POINTER_TYPE_P (TREE_TYPE (tem))
2121 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2122 return;
2123
2124 if (src
2125 && (tem = tree_strip_nop_conversions (src))
2126 && POINTER_TYPE_P (TREE_TYPE (tem))
2127 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2128 return;
2129
3a785c97
JJ
2130 loc = sizeof_arg_loc[idx];
2131
2132 if (dest && !cmp)
1a4049e7 2133 {
3a785c97
JJ
2134 if (!TYPE_P (sizeof_arg[idx])
2135 && operand_equal_p (dest, sizeof_arg[idx], 0)
1a4049e7
JJ
2136 && comp_types (TREE_TYPE (dest), type))
2137 {
3a785c97 2138 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
2139 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2140 "argument to %<sizeof%> in %qD call is the same "
2141 "expression as the destination; did you mean to "
2142 "remove the addressof?", callee);
2143 else if ((TYPE_PRECISION (TREE_TYPE (type))
2144 == TYPE_PRECISION (char_type_node))
2145 || strop)
2146 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2147 "argument to %<sizeof%> in %qD call is the same "
2148 "expression as the destination; did you mean to "
2149 "provide an explicit length?", callee);
2150 else
2151 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2152 "argument to %<sizeof%> in %qD call is the same "
2153 "expression as the destination; did you mean to "
2154 "dereference it?", callee);
2155 return;
2156 }
2157
2158 if (POINTER_TYPE_P (TREE_TYPE (dest))
2159 && !strop
2160 && comp_types (TREE_TYPE (dest), type)
2161 && !VOID_TYPE_P (TREE_TYPE (type)))
2162 {
2163 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2164 "argument to %<sizeof%> in %qD call is the same "
2165 "pointer type %qT as the destination; expected %qT "
2166 "or an explicit length", callee, TREE_TYPE (dest),
2167 TREE_TYPE (TREE_TYPE (dest)));
2168 return;
2169 }
2170 }
2171
3a785c97 2172 if (src && !cmp)
1a4049e7 2173 {
3a785c97
JJ
2174 if (!TYPE_P (sizeof_arg[idx])
2175 && operand_equal_p (src, sizeof_arg[idx], 0)
1a4049e7
JJ
2176 && comp_types (TREE_TYPE (src), type))
2177 {
3a785c97 2178 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
2179 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2180 "argument to %<sizeof%> in %qD call is the same "
2181 "expression as the source; did you mean to "
2182 "remove the addressof?", callee);
2183 else if ((TYPE_PRECISION (TREE_TYPE (type))
2184 == TYPE_PRECISION (char_type_node))
2185 || strop)
2186 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2187 "argument to %<sizeof%> in %qD call is the same "
2188 "expression as the source; did you mean to "
2189 "provide an explicit length?", callee);
2190 else
2191 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2192 "argument to %<sizeof%> in %qD call is the same "
2193 "expression as the source; did you mean to "
2194 "dereference it?", callee);
2195 return;
2196 }
2197
2198 if (POINTER_TYPE_P (TREE_TYPE (src))
2199 && !strop
2200 && comp_types (TREE_TYPE (src), type)
2201 && !VOID_TYPE_P (TREE_TYPE (type)))
2202 {
2203 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2204 "argument to %<sizeof%> in %qD call is the same "
2205 "pointer type %qT as the source; expected %qT "
2206 "or an explicit length", callee, TREE_TYPE (src),
2207 TREE_TYPE (TREE_TYPE (src)));
2208 return;
2209 }
2210 }
3a785c97
JJ
2211
2212 if (dest)
2213 {
2214 if (!TYPE_P (sizeof_arg[idx])
2215 && operand_equal_p (dest, sizeof_arg[idx], 0)
2216 && comp_types (TREE_TYPE (dest), type))
2217 {
2218 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2219 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2220 "argument to %<sizeof%> in %qD call is the same "
2221 "expression as the first source; did you mean to "
2222 "remove the addressof?", callee);
2223 else if ((TYPE_PRECISION (TREE_TYPE (type))
2224 == TYPE_PRECISION (char_type_node))
2225 || strop)
2226 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2227 "argument to %<sizeof%> in %qD call is the same "
2228 "expression as the first source; did you mean to "
2229 "provide an explicit length?", callee);
2230 else
2231 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2232 "argument to %<sizeof%> in %qD call is the same "
2233 "expression as the first source; did you mean to "
2234 "dereference it?", callee);
2235 return;
2236 }
2237
2238 if (POINTER_TYPE_P (TREE_TYPE (dest))
2239 && !strop
2240 && comp_types (TREE_TYPE (dest), type)
2241 && !VOID_TYPE_P (TREE_TYPE (type)))
2242 {
2243 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2244 "argument to %<sizeof%> in %qD call is the same "
2245 "pointer type %qT as the first source; expected %qT "
2246 "or an explicit length", callee, TREE_TYPE (dest),
2247 TREE_TYPE (TREE_TYPE (dest)));
2248 return;
2249 }
2250 }
2251
2252 if (src)
2253 {
2254 if (!TYPE_P (sizeof_arg[idx])
2255 && operand_equal_p (src, sizeof_arg[idx], 0)
2256 && comp_types (TREE_TYPE (src), type))
2257 {
2258 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2259 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2260 "argument to %<sizeof%> in %qD call is the same "
2261 "expression as the second source; did you mean to "
2262 "remove the addressof?", callee);
2263 else if ((TYPE_PRECISION (TREE_TYPE (type))
2264 == TYPE_PRECISION (char_type_node))
2265 || strop)
2266 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2267 "argument to %<sizeof%> in %qD call is the same "
2268 "expression as the second source; did you mean to "
2269 "provide an explicit length?", callee);
2270 else
2271 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2272 "argument to %<sizeof%> in %qD call is the same "
2273 "expression as the second source; did you mean to "
2274 "dereference it?", callee);
2275 return;
2276 }
2277
2278 if (POINTER_TYPE_P (TREE_TYPE (src))
2279 && !strop
2280 && comp_types (TREE_TYPE (src), type)
2281 && !VOID_TYPE_P (TREE_TYPE (type)))
2282 {
2283 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2284 "argument to %<sizeof%> in %qD call is the same "
2285 "pointer type %qT as the second source; expected %qT "
2286 "or an explicit length", callee, TREE_TYPE (src),
2287 TREE_TYPE (TREE_TYPE (src)));
2288 return;
2289 }
2290 }
2291
1a4049e7
JJ
2292}
2293
a1e45ff0
DM
2294/* Warn for unlikely, improbable, or stupid DECL declarations
2295 of `main'. */
2296
2297void
2298check_main_parameter_types (tree decl)
2299{
e19a18d4
NF
2300 function_args_iterator iter;
2301 tree type;
a1e45ff0
DM
2302 int argct = 0;
2303
e19a18d4
NF
2304 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2305 {
2306 /* XXX void_type_node belies the abstraction. */
2307 if (type == void_type_node || type == error_mark_node )
2308 break;
2309
f827930a
MP
2310 tree t = type;
2311 if (TYPE_ATOMIC (t))
2312 pedwarn (input_location, OPT_Wmain,
2313 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2314 type, decl);
2315 while (POINTER_TYPE_P (t))
2316 {
2317 t = TREE_TYPE (t);
2318 if (TYPE_ATOMIC (t))
2319 pedwarn (input_location, OPT_Wmain,
2320 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2321 type, decl);
2322 }
2323
e19a18d4
NF
2324 ++argct;
2325 switch (argct)
2326 {
2327 case 1:
2328 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2329 pedwarn (input_location, OPT_Wmain,
2330 "first argument of %q+D should be %<int%>", decl);
2331 break;
2332
2333 case 2:
2334 if (TREE_CODE (type) != POINTER_TYPE
2335 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2336 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2337 != char_type_node))
2338 pedwarn (input_location, OPT_Wmain,
2339 "second argument of %q+D should be %<char **%>", decl);
2340 break;
2341
2342 case 3:
2343 if (TREE_CODE (type) != POINTER_TYPE
2344 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2345 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2346 != char_type_node))
2347 pedwarn (input_location, OPT_Wmain,
2348 "third argument of %q+D should probably be "
2349 "%<char **%>", decl);
2350 break;
2351 }
2352 }
a1e45ff0
DM
2353
2354 /* It is intentional that this message does not mention the third
2355 argument because it's only mentioned in an appendix of the
2356 standard. */
2357 if (argct > 0 && (argct < 2 || argct > 3))
e19a18d4
NF
2358 pedwarn (input_location, OPT_Wmain,
2359 "%q+D takes only zero or two arguments", decl);
38e514c0
MP
2360
2361 if (stdarg_p (TREE_TYPE (decl)))
2362 pedwarn (input_location, OPT_Wmain,
2363 "%q+D declared as variadic function", decl);
a1e45ff0
DM
2364}
2365
0af94e6f
JR
2366/* vector_targets_convertible_p is used for vector pointer types. The
2367 callers perform various checks that the qualifiers are satisfactory,
2368 while OTOH vector_targets_convertible_p ignores the number of elements
2369 in the vectors. That's fine with vector pointers as we can consider,
2370 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2371 and that does not require and conversion of the pointer values.
2372 In contrast, vector_types_convertible_p and
2373 vector_types_compatible_elements_p are used for vector value types. */
f83c7f63
DJ
2374/* True if pointers to distinct types T1 and T2 can be converted to
2375 each other without an explicit cast. Only returns true for opaque
2376 vector types. */
2377bool
2378vector_targets_convertible_p (const_tree t1, const_tree t2)
2379{
2380 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
b6fc2cdb 2381 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
f83c7f63
DJ
2382 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2383 return true;
2384
2385 return false;
2386}
2387
0af94e6f
JR
2388/* vector_types_convertible_p is used for vector value types.
2389 It could in principle call vector_targets_convertible_p as a subroutine,
2390 but then the check for vector type would be duplicated with its callers,
2391 and also the purpose of vector_targets_convertible_p would become
2392 muddled.
2393 Where vector_types_convertible_p returns true, a conversion might still be
2394 needed to make the types match.
2395 In contrast, vector_targets_convertible_p is used for vector pointer
2396 values, and vector_types_compatible_elements_p is used specifically
2397 in the context for binary operators, as a check if use is possible without
2398 conversion. */
00c8e9f6
MS
2399/* True if vector types T1 and T2 can be converted to each other
2400 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2401 can only be converted with -flax-vector-conversions yet that is not
2402 in effect, emit a note telling the user about that option if such
2403 a note has not previously been emitted. */
2404bool
58f9752a 2405vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
cc27e657 2406{
00c8e9f6 2407 static bool emitted_lax_note = false;
14e765da
JM
2408 bool convertible_lax;
2409
b6fc2cdb 2410 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
14e765da
JM
2411 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2412 return true;
2413
2414 convertible_lax =
2415 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2416 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
a5e0cd1d 2417 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
14e765da
JM
2418 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2419 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
00c8e9f6
MS
2420
2421 if (!convertible_lax || flag_lax_vector_conversions)
2422 return convertible_lax;
2423
2424 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
cf7bc668 2425 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
00c8e9f6
MS
2426 return true;
2427
2428 if (emit_lax_note && !emitted_lax_note)
2429 {
2430 emitted_lax_note = true;
1f5b3869 2431 inform (input_location, "use -flax-vector-conversions to permit "
00c8e9f6
MS
2432 "conversions between vectors with differing "
2433 "element types or numbers of subparts");
2434 }
2435
2436 return false;
cc27e657
PB
2437}
2438
9e1a8dd1
RR
2439/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2440 and have vector types, V0 has the same type as V1, and the number of
2441 elements of V0, V1, MASK is the same.
2442
2443 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2444 called with two arguments. In this case implementation passes the
2445 first argument twice in order to share the same tree code. This fact
2446 could enable the mask-values being twice the vector length. This is
2447 an implementation accident and this semantics is not guaranteed to
2448 the user. */
2449tree
bedc293e
MG
2450c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2451 bool complain)
9e1a8dd1
RR
2452{
2453 tree ret;
2454 bool wrap = true;
2455 bool maybe_const = false;
2456 bool two_arguments = false;
2457
2458 if (v1 == NULL_TREE)
2459 {
2460 two_arguments = true;
2461 v1 = v0;
2462 }
2463
2464 if (v0 == error_mark_node || v1 == error_mark_node
2465 || mask == error_mark_node)
2466 return error_mark_node;
2467
2468 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2469 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2470 {
bedc293e
MG
2471 if (complain)
2472 error_at (loc, "__builtin_shuffle last argument must "
2473 "be an integer vector");
9e1a8dd1
RR
2474 return error_mark_node;
2475 }
2476
2477 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2478 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2479 {
bedc293e
MG
2480 if (complain)
2481 error_at (loc, "__builtin_shuffle arguments must be vectors");
9e1a8dd1
RR
2482 return error_mark_node;
2483 }
2484
2485 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2486 {
bedc293e
MG
2487 if (complain)
2488 error_at (loc, "__builtin_shuffle argument vectors must be of "
2489 "the same type");
9e1a8dd1
RR
2490 return error_mark_node;
2491 }
2492
2493 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2494 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2495 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2496 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2497 {
bedc293e
MG
2498 if (complain)
2499 error_at (loc, "__builtin_shuffle number of elements of the "
2500 "argument vector(s) and the mask vector should "
2501 "be the same");
9e1a8dd1
RR
2502 return error_mark_node;
2503 }
2504
2505 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2506 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2507 {
bedc293e
MG
2508 if (complain)
2509 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2510 "must have the same size as inner type of the mask");
9e1a8dd1
RR
2511 return error_mark_node;
2512 }
2513
2514 if (!c_dialect_cxx ())
2515 {
2516 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2517 v0 = c_fully_fold (v0, false, &maybe_const);
2518 wrap &= maybe_const;
2519
2520 if (two_arguments)
2521 v1 = v0 = save_expr (v0);
2522 else
2523 {
2524 v1 = c_fully_fold (v1, false, &maybe_const);
2525 wrap &= maybe_const;
2526 }
2527
2528 mask = c_fully_fold (mask, false, &maybe_const);
2529 wrap &= maybe_const;
2530 }
bedc293e
MG
2531 else if (two_arguments)
2532 v1 = v0 = save_expr (v0);
9e1a8dd1
RR
2533
2534 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2535
2536 if (!c_dialect_cxx () && !wrap)
2537 ret = c_wrap_maybe_const (ret, true);
2538
2539 return ret;
2540}
2541
828fb3ba
JM
2542/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2543 to integral type. */
2544
2545static tree
2546c_common_get_narrower (tree op, int *unsignedp_ptr)
2547{
2548 op = get_narrower (op, unsignedp_ptr);
2549
2550 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2551 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2552 {
2553 /* C++0x scoped enumerations don't implicitly convert to integral
2554 type; if we stripped an explicit conversion to a larger type we
2555 need to replace it so common_type will still work. */
21fa2faf
RG
2556 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2557 TYPE_UNSIGNED (TREE_TYPE (op)));
828fb3ba
JM
2558 op = fold_convert (type, op);
2559 }
2560 return op;
2561}
2562
6715192c
MLI
2563/* This is a helper function of build_binary_op.
2564
2565 For certain operations if both args were extended from the same
2566 smaller type, do the arithmetic in that type and then extend.
2567
2568 BITWISE indicates a bitwise operation.
2569 For them, this optimization is safe only if
2570 both args are zero-extended or both are sign-extended.
2571 Otherwise, we might change the result.
2572 Eg, (short)-1 | (unsigned short)-1 is (int)-1
b8698a0f 2573 but calculated in (unsigned short) it would be (unsigned short)-1.
6715192c 2574*/
828fb3ba
JM
2575tree
2576shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
6715192c
MLI
2577{
2578 int unsigned0, unsigned1;
2579 tree arg0, arg1;
2580 int uns;
2581 tree type;
2582
2583 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2584 excessive narrowing when we call get_narrower below. For
2585 example, suppose that OP0 is of unsigned int extended
2586 from signed char and that RESULT_TYPE is long long int.
2587 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2588 like
b8698a0f 2589
6715192c
MLI
2590 (long long int) (unsigned int) signed_char
2591
2592 which get_narrower would narrow down to
b8698a0f 2593
6715192c 2594 (unsigned int) signed char
b8698a0f 2595
6715192c
MLI
2596 If we do not cast OP0 first, get_narrower would return
2597 signed_char, which is inconsistent with the case of the
2598 explicit cast. */
2599 op0 = convert (result_type, op0);
2600 op1 = convert (result_type, op1);
2601
828fb3ba
JM
2602 arg0 = c_common_get_narrower (op0, &unsigned0);
2603 arg1 = c_common_get_narrower (op1, &unsigned1);
6715192c
MLI
2604
2605 /* UNS is 1 if the operation to be done is an unsigned one. */
2606 uns = TYPE_UNSIGNED (result_type);
2607
2608 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2609 but it *requires* conversion to FINAL_TYPE. */
b8698a0f 2610
6715192c
MLI
2611 if ((TYPE_PRECISION (TREE_TYPE (op0))
2612 == TYPE_PRECISION (TREE_TYPE (arg0)))
2613 && TREE_TYPE (op0) != result_type)
2614 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2615 if ((TYPE_PRECISION (TREE_TYPE (op1))
2616 == TYPE_PRECISION (TREE_TYPE (arg1)))
2617 && TREE_TYPE (op1) != result_type)
2618 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b8698a0f 2619
6715192c 2620 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
b8698a0f 2621
6715192c
MLI
2622 /* For bitwise operations, signedness of nominal type
2623 does not matter. Consider only how operands were extended. */
2624 if (bitwise)
2625 uns = unsigned0;
b8698a0f 2626
6715192c
MLI
2627 /* Note that in all three cases below we refrain from optimizing
2628 an unsigned operation on sign-extended args.
2629 That would not be valid. */
b8698a0f 2630
6715192c
MLI
2631 /* Both args variable: if both extended in same way
2632 from same width, do it in that width.
2633 Do it unsigned if args were zero-extended. */
2634 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2635 < TYPE_PRECISION (result_type))
2636 && (TYPE_PRECISION (TREE_TYPE (arg1))
2637 == TYPE_PRECISION (TREE_TYPE (arg0)))
2638 && unsigned0 == unsigned1
2639 && (unsigned0 || !uns))
2640 return c_common_signed_or_unsigned_type
2641 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2642
2643 else if (TREE_CODE (arg0) == INTEGER_CST
2644 && (unsigned1 || !uns)
2645 && (TYPE_PRECISION (TREE_TYPE (arg1))
2646 < TYPE_PRECISION (result_type))
2647 && (type
2648 = c_common_signed_or_unsigned_type (unsigned1,
2649 TREE_TYPE (arg1)))
2650 && !POINTER_TYPE_P (type)
2651 && int_fits_type_p (arg0, type))
2652 return type;
2653
2654 else if (TREE_CODE (arg1) == INTEGER_CST
2655 && (unsigned0 || !uns)
2656 && (TYPE_PRECISION (TREE_TYPE (arg0))
2657 < TYPE_PRECISION (result_type))
2658 && (type
2659 = c_common_signed_or_unsigned_type (unsigned0,
2660 TREE_TYPE (arg0)))
2661 && !POINTER_TYPE_P (type)
2662 && int_fits_type_p (arg1, type))
2663 return type;
2664
2665 return result_type;
2666}
2667
68fca595 2668/* Checks if expression EXPR of real/integer type cannot be converted
49b0aa18 2669 to the real/integer type TYPE. Function returns non-zero when:
68fca595
MP
2670 * EXPR is a constant which cannot be exactly converted to TYPE.
2671 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
0e3a99ae 2672 for EXPR type and TYPE being both integers or both real.
68fca595
MP
2673 * EXPR is not a constant of real type and TYPE is an integer.
2674 * EXPR is not a constant of integer type which cannot be
2675 exactly converted to real type.
0e3a99ae 2676 Function allows conversions between types of different signedness and
49b0aa18
JC
2677 can return SAFE_CONVERSION (zero) in that case. Function can produce
2678 signedness warnings if PRODUCE_WARNS is true. */
68fca595 2679
49b0aa18 2680enum conversion_safety
68fca595 2681unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
422c3a54 2682{
49b0aa18 2683 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
374035cb 2684 tree expr_type = TREE_TYPE (expr);
68fca595 2685 loc = expansion_point_location_if_in_system_header (loc);
422c3a54 2686
0e3a99ae 2687 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
0011dedb 2688 {
422c3a54 2689 /* Warn for real constant that is not an exact integer converted
0e3a99ae 2690 to integer type. */
374035cb 2691 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2692 && TREE_CODE (type) == INTEGER_TYPE)
2693 {
2694 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
49b0aa18 2695 give_warning = UNSAFE_REAL;
0e3a99ae 2696 }
91c41804 2697 /* Warn for an integer constant that does not fit into integer type. */
374035cb 2698 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2699 && TREE_CODE (type) == INTEGER_TYPE
2700 && !int_fits_type_p (expr, type))
2701 {
2702 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
374035cb 2703 && tree_int_cst_sgn (expr) < 0)
0e3a99ae
AS
2704 {
2705 if (produce_warns)
2706 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2707 " implicitly converted to unsigned type");
2708 }
2709 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2710 {
2711 if (produce_warns)
2712 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2713 " constant value to negative integer");
2714 }
7060db96 2715 else
49b0aa18 2716 give_warning = UNSAFE_OTHER;
0e3a99ae 2717 }
422c3a54 2718 else if (TREE_CODE (type) == REAL_TYPE)
0e3a99ae
AS
2719 {
2720 /* Warn for an integer constant that does not fit into real type. */
2721 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2722 {
2723 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2724 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2725 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2726 }
2727 /* Warn for a real constant that does not fit into a smaller
2728 real type. */
2729 else if (TREE_CODE (expr_type) == REAL_TYPE
2730 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2731 {
2732 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2733 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2734 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2735 }
2736 }
2737 }
2738 else
2739 {
422c3a54 2740 /* Warn for real types converted to integer types. */
6715192c 2741 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae 2742 && TREE_CODE (type) == INTEGER_TYPE)
49b0aa18 2743 give_warning = UNSAFE_REAL;
422c3a54 2744
6715192c 2745 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2746 && TREE_CODE (type) == INTEGER_TYPE)
2747 {
cfdaefec 2748 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
c00e8b06 2749 expr = get_unwidened (expr, 0);
6715192c 2750 expr_type = TREE_TYPE (expr);
cfdaefec 2751
6715192c 2752 /* Don't warn for short y; short x = ((int)y & 0xff); */
b8698a0f 2753 if (TREE_CODE (expr) == BIT_AND_EXPR
0e3a99ae 2754 || TREE_CODE (expr) == BIT_IOR_EXPR
6715192c
MLI
2755 || TREE_CODE (expr) == BIT_XOR_EXPR)
2756 {
374035cb
MLI
2757 /* If both args were extended from a shortest type,
2758 use that type if that is safe. */
b8698a0f
L
2759 expr_type = shorten_binary_op (expr_type,
2760 TREE_OPERAND (expr, 0),
2761 TREE_OPERAND (expr, 1),
6715192c
MLI
2762 /* bitwise */1);
2763
6715192c
MLI
2764 if (TREE_CODE (expr) == BIT_AND_EXPR)
2765 {
2766 tree op0 = TREE_OPERAND (expr, 0);
2767 tree op1 = TREE_OPERAND (expr, 1);
9c591bd0
MLI
2768 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2769 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2770
2771 /* If one of the operands is a non-negative constant
2772 that fits in the target type, then the type of the
2773 other operand does not matter. */
6715192c
MLI
2774 if ((TREE_CODE (op0) == INTEGER_CST
2775 && int_fits_type_p (op0, c_common_signed_type (type))
2776 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2777 || (TREE_CODE (op1) == INTEGER_CST
374035cb 2778 && int_fits_type_p (op1, c_common_signed_type (type))
b8698a0f 2779 && int_fits_type_p (op1,
374035cb 2780 c_common_unsigned_type (type))))
49b0aa18 2781 return SAFE_CONVERSION;
9c591bd0
MLI
2782 /* If constant is unsigned and fits in the target
2783 type, then the result will also fit. */
2784 else if ((TREE_CODE (op0) == INTEGER_CST
b8698a0f 2785 && unsigned0
9c591bd0
MLI
2786 && int_fits_type_p (op0, type))
2787 || (TREE_CODE (op1) == INTEGER_CST
2788 && unsigned1
2789 && int_fits_type_p (op1, type)))
49b0aa18 2790 return SAFE_CONVERSION;
6715192c
MLI
2791 }
2792 }
0e3a99ae 2793 /* Warn for integer types converted to smaller integer types. */
b8698a0f 2794 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2795 give_warning = UNSAFE_OTHER;
7060db96
MLI
2796
2797 /* When they are the same width but different signedness,
2798 then the value may change. */
0e3a99ae 2799 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
6715192c 2800 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7060db96
MLI
2801 /* Even when converted to a bigger type, if the type is
2802 unsigned but expr is signed, then negative values
2803 will be changed. */
0e3a99ae
AS
2804 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2805 && produce_warns)
6312e84d
MLI
2806 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2807 "may change the sign of the result",
2808 type, expr_type);
0e3a99ae 2809 }
422c3a54
MLI
2810
2811 /* Warn for integer types converted to real types if and only if
0e3a99ae
AS
2812 all the range of values of the integer type cannot be
2813 represented by the real type. */
6715192c 2814 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2815 && TREE_CODE (type) == REAL_TYPE)
2816 {
58076e21 2817 tree type_low_bound, type_high_bound;
0e3a99ae 2818 REAL_VALUE_TYPE real_low_bound, real_high_bound;
58076e21
MLI
2819
2820 /* Don't warn about char y = 0xff; float x = (int) y; */
2821 expr = get_unwidened (expr, 0);
2822 expr_type = TREE_TYPE (expr);
2823
0e3a99ae
AS
2824 type_low_bound = TYPE_MIN_VALUE (expr_type);
2825 type_high_bound = TYPE_MAX_VALUE (expr_type);
2826 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2827 real_high_bound = real_value_from_int_cst (0, type_high_bound);
422c3a54 2828
0e3a99ae
AS
2829 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2830 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
49b0aa18 2831 give_warning = UNSAFE_OTHER;
0e3a99ae 2832 }
422c3a54
MLI
2833
2834 /* Warn for real types converted to smaller real types. */
6715192c 2835 else if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2836 && TREE_CODE (type) == REAL_TYPE
2837 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2838 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2839 }
2840
2841 return give_warning;
2842}
2843
2844/* Warns if the conversion of EXPR to TYPE may alter a value.
2845 This is a helper function for warnings_for_convert_and_check. */
2846
2847static void
68fca595 2848conversion_warning (location_t loc, tree type, tree expr)
0e3a99ae 2849{
0e3a99ae 2850 tree expr_type = TREE_TYPE (expr);
49b0aa18 2851 enum conversion_safety conversion_kind;
422c3a54 2852
49b0aa18 2853 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
0e3a99ae 2854 return;
422c3a54 2855
66f20604
MP
2856 /* This may happen, because for LHS op= RHS we preevaluate
2857 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2858 means we could no longer see the code of the EXPR. */
2859 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2860 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2861 if (TREE_CODE (expr) == SAVE_EXPR)
2862 expr = TREE_OPERAND (expr, 0);
2863
0e3a99ae
AS
2864 switch (TREE_CODE (expr))
2865 {
2866 case EQ_EXPR:
2867 case NE_EXPR:
2868 case LE_EXPR:
2869 case GE_EXPR:
2870 case LT_EXPR:
2871 case GT_EXPR:
2872 case TRUTH_ANDIF_EXPR:
2873 case TRUTH_ORIF_EXPR:
2874 case TRUTH_AND_EXPR:
2875 case TRUTH_OR_EXPR:
2876 case TRUTH_XOR_EXPR:
2877 case TRUTH_NOT_EXPR:
2878 /* Conversion from boolean to a signed:1 bit-field (which only
2879 can hold the values 0 and -1) doesn't lose information - but
2880 it does change the value. */
2881 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2882 warning_at (loc, OPT_Wconversion,
2883 "conversion to %qT from boolean expression", type);
2884 return;
2885
2886 case REAL_CST:
2887 case INTEGER_CST:
68fca595 2888 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
2889 if (conversion_kind == UNSAFE_REAL)
2890 warning_at (loc, OPT_Wfloat_conversion,
2891 "conversion to %qT alters %qT constant value",
2892 type, expr_type);
2893 else if (conversion_kind)
0e3a99ae
AS
2894 warning_at (loc, OPT_Wconversion,
2895 "conversion to %qT alters %qT constant value",
2896 type, expr_type);
2897 return;
2898
2899 case COND_EXPR:
2900 {
3f46d6a5
MLI
2901 /* In case of COND_EXPR, we do not care about the type of
2902 COND_EXPR, only about the conversion of each operand. */
2903 tree op1 = TREE_OPERAND (expr, 1);
2904 tree op2 = TREE_OPERAND (expr, 2);
2905
68fca595
MP
2906 conversion_warning (loc, type, op1);
2907 conversion_warning (loc, type, op2);
3f46d6a5 2908 return;
0e3a99ae
AS
2909 }
2910
2911 default: /* 'expr' is not a constant. */
68fca595 2912 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
2913 if (conversion_kind == UNSAFE_REAL)
2914 warning_at (loc, OPT_Wfloat_conversion,
2915 "conversion to %qT from %qT may alter its value",
2916 type, expr_type);
2917 else if (conversion_kind)
0e3a99ae 2918 warning_at (loc, OPT_Wconversion,
6312e84d
MLI
2919 "conversion to %qT from %qT may alter its value",
2920 type, expr_type);
422c3a54
MLI
2921 }
2922}
2923
07231d4f
MLI
2924/* Produce warnings after a conversion. RESULT is the result of
2925 converting EXPR to TYPE. This is a helper function for
2926 convert_and_check and cp_convert_and_check. */
d74154d5 2927
07231d4f 2928void
68fca595
MP
2929warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2930 tree result)
d74154d5 2931{
68fca595 2932 loc = expansion_point_location_if_in_system_header (loc);
5a3c9cf2 2933
91c41804
RS
2934 if (TREE_CODE (expr) == INTEGER_CST
2935 && (TREE_CODE (type) == INTEGER_TYPE
2936 || TREE_CODE (type) == ENUMERAL_TYPE)
2937 && !int_fits_type_p (expr, type))
2938 {
422c3a54
MLI
2939 /* Do not diagnose overflow in a constant expression merely
2940 because a conversion overflowed. */
91c41804 2941 if (TREE_OVERFLOW (result))
d95787e6
RS
2942 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2943
91c41804 2944 if (TYPE_UNSIGNED (type))
422c3a54 2945 {
91c41804
RS
2946 /* This detects cases like converting -129 or 256 to
2947 unsigned char. */
2948 if (!int_fits_type_p (expr, c_common_signed_type (type)))
5a3c9cf2
PC
2949 warning_at (loc, OPT_Woverflow,
2950 "large integer implicitly truncated to unsigned type");
7060db96 2951 else
68fca595 2952 conversion_warning (loc, type, expr);
91c41804 2953 }
b8698a0f 2954 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
68fca595 2955 warning_at (loc, OPT_Woverflow,
f73fe417
MLI
2956 "overflow in implicit constant conversion");
2957 /* No warning for converting 0x80000000 to int. */
2958 else if (pedantic
2959 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2960 || TYPE_PRECISION (TREE_TYPE (expr))
2961 != TYPE_PRECISION (type)))
5a3c9cf2
PC
2962 warning_at (loc, OPT_Woverflow,
2963 "overflow in implicit constant conversion");
f73fe417 2964
7060db96 2965 else
68fca595 2966 conversion_warning (loc, type, expr);
d74154d5 2967 }
ab22c1fa
CF
2968 else if ((TREE_CODE (result) == INTEGER_CST
2969 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
5a3c9cf2
PC
2970 warning_at (loc, OPT_Woverflow,
2971 "overflow in implicit constant conversion");
7060db96 2972 else
68fca595 2973 conversion_warning (loc, type, expr);
07231d4f
MLI
2974}
2975
2976
2977/* Convert EXPR to TYPE, warning about conversion problems with constants.
2978 Invoke this function on every expression that is converted implicitly,
2979 i.e. because of language rules and not because of an explicit cast. */
2980
2981tree
68fca595 2982convert_and_check (location_t loc, tree type, tree expr)
07231d4f
MLI
2983{
2984 tree result;
8ce94e44
JM
2985 tree expr_for_warning;
2986
2987 /* Convert from a value with possible excess precision rather than
2988 via the semantic type, but do not warn about values not fitting
2989 exactly in the semantic type. */
2990 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2991 {
2992 tree orig_type = TREE_TYPE (expr);
2993 expr = TREE_OPERAND (expr, 0);
2994 expr_for_warning = convert (orig_type, expr);
2995 if (orig_type == type)
2996 return expr_for_warning;
2997 }
2998 else
2999 expr_for_warning = expr;
07231d4f
MLI
3000
3001 if (TREE_TYPE (expr) == type)
3002 return expr;
b8698a0f 3003
07231d4f
MLI
3004 result = convert (type, expr);
3005
7d882b83
ILT
3006 if (c_inhibit_evaluation_warnings == 0
3007 && !TREE_OVERFLOW_P (expr)
3008 && result != error_mark_node)
68fca595 3009 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
07231d4f 3010
91c41804 3011 return result;
96571883
BK
3012}
3013\f
235cfbc4
BS
3014/* A node in a list that describes references to variables (EXPR), which are
3015 either read accesses if WRITER is zero, or write accesses, in which case
3016 WRITER is the parent of EXPR. */
3017struct tlist
3018{
3019 struct tlist *next;
3020 tree expr, writer;
3021};
3022
3023/* Used to implement a cache the results of a call to verify_tree. We only
3024 use this for SAVE_EXPRs. */
3025struct tlist_cache
3026{
3027 struct tlist_cache *next;
3028 struct tlist *cache_before_sp;
3029 struct tlist *cache_after_sp;
3030 tree expr;
2683ed8d
BS
3031};
3032
235cfbc4
BS
3033/* Obstack to use when allocating tlist structures, and corresponding
3034 firstobj. */
3035static struct obstack tlist_obstack;
3036static char *tlist_firstobj = 0;
3037
3038/* Keep track of the identifiers we've warned about, so we can avoid duplicate
3039 warnings. */
3040static struct tlist *warned_ids;
3041/* SAVE_EXPRs need special treatment. We process them only once and then
3042 cache the results. */
3043static struct tlist_cache *save_expr_cache;
3044
35b1a6fa
AJ
3045static void add_tlist (struct tlist **, struct tlist *, tree, int);
3046static void merge_tlist (struct tlist **, struct tlist *, int);
3047static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3048static int warning_candidate_p (tree);
1e4ae551 3049static bool candidate_equal_p (const_tree, const_tree);
35b1a6fa
AJ
3050static void warn_for_collisions (struct tlist *);
3051static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3052static struct tlist *new_tlist (struct tlist *, tree, tree);
2683ed8d 3053
235cfbc4
BS
3054/* Create a new struct tlist and fill in its fields. */
3055static struct tlist *
35b1a6fa 3056new_tlist (struct tlist *next, tree t, tree writer)
235cfbc4
BS
3057{
3058 struct tlist *l;
5d038c4c 3059 l = XOBNEW (&tlist_obstack, struct tlist);
235cfbc4
BS
3060 l->next = next;
3061 l->expr = t;
3062 l->writer = writer;
3063 return l;
3064}
3065
3066/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3067 is nonnull, we ignore any node we find which has a writer equal to it. */
3068
3069static void
35b1a6fa 3070add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
235cfbc4
BS
3071{
3072 while (add)
3073 {
3074 struct tlist *next = add->next;
3f75a254 3075 if (!copy)
235cfbc4 3076 add->next = *to;
1e4ae551 3077 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
235cfbc4
BS
3078 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3079 add = next;
3080 }
3081}
3082
3083/* Merge the nodes of ADD into TO. This merging process is done so that for
3084 each variable that already exists in TO, no new node is added; however if
3085 there is a write access recorded in ADD, and an occurrence on TO is only
3086 a read access, then the occurrence in TO will be modified to record the
3087 write. */
2683ed8d
BS
3088
3089static void
35b1a6fa 3090merge_tlist (struct tlist **to, struct tlist *add, int copy)
235cfbc4
BS
3091{
3092 struct tlist **end = to;
3093
3094 while (*end)
3095 end = &(*end)->next;
3096
3097 while (add)
3098 {
3099 int found = 0;
3100 struct tlist *tmp2;
3101 struct tlist *next = add->next;
3102
3103 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1e4ae551 3104 if (candidate_equal_p (tmp2->expr, add->expr))
235cfbc4
BS
3105 {
3106 found = 1;
3f75a254 3107 if (!tmp2->writer)
235cfbc4
BS
3108 tmp2->writer = add->writer;
3109 }
3f75a254 3110 if (!found)
235cfbc4 3111 {
c2bf53a1 3112 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
235cfbc4
BS
3113 end = &(*end)->next;
3114 *end = 0;
3115 }
3116 add = next;
3117 }
3118}
3119
3120/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3121 references in list LIST conflict with it, excluding reads if ONLY writers
3122 is nonzero. */
3123
3124static void
35b1a6fa
AJ
3125warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3126 int only_writes)
235cfbc4
BS
3127{
3128 struct tlist *tmp;
3129
3130 /* Avoid duplicate warnings. */
3131 for (tmp = warned_ids; tmp; tmp = tmp->next)
1e4ae551 3132 if (candidate_equal_p (tmp->expr, written))
235cfbc4
BS
3133 return;
3134
3135 while (list)
3136 {
1e4ae551
MLI
3137 if (candidate_equal_p (list->expr, written)
3138 && !candidate_equal_p (list->writer, writer)
3139 && (!only_writes || list->writer))
235cfbc4
BS
3140 {
3141 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
8400e75e 3142 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
ca085fd7
MLI
3143 OPT_Wsequence_point, "operation on %qE may be undefined",
3144 list->expr);
235cfbc4
BS
3145 }
3146 list = list->next;
3147 }
3148}
3149
3150/* Given a list LIST of references to variables, find whether any of these
3151 can cause conflicts due to missing sequence points. */
3152
3153static void
35b1a6fa 3154warn_for_collisions (struct tlist *list)
235cfbc4
BS
3155{
3156 struct tlist *tmp;
35b1a6fa 3157
235cfbc4
BS
3158 for (tmp = list; tmp; tmp = tmp->next)
3159 {
3160 if (tmp->writer)
3161 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3162 }
3163}
3164
684d9f3b 3165/* Return nonzero if X is a tree that can be verified by the sequence point
235cfbc4
BS
3166 warnings. */
3167static int
35b1a6fa 3168warning_candidate_p (tree x)
2683ed8d 3169{
07078664
JJ
3170 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3171 return 0;
3172
92e948a8
NF
3173 if (TREE_CODE (x) == BLOCK)
3174 return 0;
3175
07078664 3176 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1e4ae551 3177 (lvalue_p) crash on TRY/CATCH. */
07078664
JJ
3178 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3179 return 0;
3180
3181 if (!lvalue_p (x))
3182 return 0;
3183
3184 /* No point to track non-const calls, they will never satisfy
3185 operand_equal_p. */
3186 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3187 return 0;
3188
3189 if (TREE_CODE (x) == STRING_CST)
3190 return 0;
3191
3192 return 1;
1e4ae551
MLI
3193}
3194
3195/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3196static bool
3197candidate_equal_p (const_tree x, const_tree y)
3198{
3199 return (x == y) || (x && y && operand_equal_p (x, y, 0));
235cfbc4 3200}
2683ed8d 3201
235cfbc4
BS
3202/* Walk the tree X, and record accesses to variables. If X is written by the
3203 parent tree, WRITER is the parent.
3204 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3205 expression or its only operand forces a sequence point, then everything up
3206 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3207 in PNO_SP.
3208 Once we return, we will have emitted warnings if any subexpression before
3209 such a sequence point could be undefined. On a higher level, however, the
3210 sequence point may not be relevant, and we'll merge the two lists.
3211
3212 Example: (b++, a) + b;
3213 The call that processes the COMPOUND_EXPR will store the increment of B
3214 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3215 processes the PLUS_EXPR will need to merge the two lists so that
3216 eventually, all accesses end up on the same list (and we'll warn about the
3217 unordered subexpressions b++ and b.
3218
3219 A note on merging. If we modify the former example so that our expression
3220 becomes
3221 (b++, b) + a
3222 care must be taken not simply to add all three expressions into the final
3223 PNO_SP list. The function merge_tlist takes care of that by merging the
3224 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3225 way, so that no more than one access to B is recorded. */
2683ed8d 3226
235cfbc4 3227static void
35b1a6fa
AJ
3228verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3229 tree writer)
235cfbc4
BS
3230{
3231 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3232 enum tree_code code;
6615c446 3233 enum tree_code_class cl;
2683ed8d 3234
f9e1917e
JM
3235 /* X may be NULL if it is the operand of an empty statement expression
3236 ({ }). */
3237 if (x == NULL)
3238 return;
3239
235cfbc4
BS
3240 restart:
3241 code = TREE_CODE (x);
e3a64162 3242 cl = TREE_CODE_CLASS (code);
2683ed8d 3243
235cfbc4 3244 if (warning_candidate_p (x))
1e4ae551 3245 *pno_sp = new_tlist (*pno_sp, x, writer);
235cfbc4
BS
3246
3247 switch (code)
3248 {
52a84e42 3249 case CONSTRUCTOR:
f7716d57 3250 case SIZEOF_EXPR:
52a84e42
BS
3251 return;
3252
235cfbc4
BS
3253 case COMPOUND_EXPR:
3254 case TRUTH_ANDIF_EXPR:
3255 case TRUTH_ORIF_EXPR:
3256 tmp_before = tmp_nosp = tmp_list3 = 0;
3257 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3258 warn_for_collisions (tmp_nosp);
3259 merge_tlist (pbefore_sp, tmp_before, 0);
3260 merge_tlist (pbefore_sp, tmp_nosp, 0);
3261 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3262 merge_tlist (pbefore_sp, tmp_list3, 0);
3263 return;
3264
3265 case COND_EXPR:
3266 tmp_before = tmp_list2 = 0;
3267 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3268 warn_for_collisions (tmp_list2);
3269 merge_tlist (pbefore_sp, tmp_before, 0);
c2bf53a1 3270 merge_tlist (pbefore_sp, tmp_list2, 0);
235cfbc4
BS
3271
3272 tmp_list3 = tmp_nosp = 0;
3273 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3274 warn_for_collisions (tmp_nosp);
3275 merge_tlist (pbefore_sp, tmp_list3, 0);
3276
3277 tmp_list3 = tmp_list2 = 0;
3278 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3279 warn_for_collisions (tmp_list2);
3280 merge_tlist (pbefore_sp, tmp_list3, 0);
3281 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3282 two first, to avoid warning for (a ? b++ : b++). */
3283 merge_tlist (&tmp_nosp, tmp_list2, 0);
3284 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3285 return;
3286
2683ed8d
BS
3287 case PREDECREMENT_EXPR:
3288 case PREINCREMENT_EXPR:
3289 case POSTDECREMENT_EXPR:
3290 case POSTINCREMENT_EXPR:
235cfbc4
BS
3291 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3292 return;
3293
3294 case MODIFY_EXPR:
3295 tmp_before = tmp_nosp = tmp_list3 = 0;
3296 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3297 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3298 /* Expressions inside the LHS are not ordered wrt. the sequence points
3299 in the RHS. Example:
3300 *a = (a++, 2)
3301 Despite the fact that the modification of "a" is in the before_sp
3302 list (tmp_before), it conflicts with the use of "a" in the LHS.
3303 We can handle this by adding the contents of tmp_list3
3304 to those of tmp_before, and redoing the collision warnings for that
3305 list. */
3306 add_tlist (&tmp_before, tmp_list3, x, 1);
3307 warn_for_collisions (tmp_before);
3308 /* Exclude the LHS itself here; we first have to merge it into the
3309 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3310 didn't exclude the LHS, we'd get it twice, once as a read and once
3311 as a write. */
3312 add_tlist (pno_sp, tmp_list3, x, 0);
3313 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3314
3315 merge_tlist (pbefore_sp, tmp_before, 0);
3316 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3317 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3318 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3319 return;
2683ed8d
BS
3320
3321 case CALL_EXPR:
235cfbc4
BS
3322 /* We need to warn about conflicts among arguments and conflicts between
3323 args and the function address. Side effects of the function address,
3324 however, are not ordered by the sequence point of the call. */
5039610b
SL
3325 {
3326 call_expr_arg_iterator iter;
3327 tree arg;
b8698a0f 3328 tmp_before = tmp_nosp = 0;
5039610b
SL
3329 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3330 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3331 {
3332 tmp_list2 = tmp_list3 = 0;
3333 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3334 merge_tlist (&tmp_list3, tmp_list2, 0);
3335 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3336 }
3337 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3338 warn_for_collisions (tmp_before);
3339 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3340 return;
3341 }
2683ed8d
BS
3342
3343 case TREE_LIST:
3344 /* Scan all the list, e.g. indices of multi dimensional array. */
3345 while (x)
3346 {
235cfbc4
BS
3347 tmp_before = tmp_nosp = 0;
3348 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3349 merge_tlist (&tmp_nosp, tmp_before, 0);
3350 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2683ed8d
BS
3351 x = TREE_CHAIN (x);
3352 }
235cfbc4 3353 return;
2683ed8d 3354
235cfbc4
BS
3355 case SAVE_EXPR:
3356 {
3357 struct tlist_cache *t;
3358 for (t = save_expr_cache; t; t = t->next)
1e4ae551 3359 if (candidate_equal_p (t->expr, x))
235cfbc4 3360 break;
2683ed8d 3361
3f75a254 3362 if (!t)
2683ed8d 3363 {
5d038c4c 3364 t = XOBNEW (&tlist_obstack, struct tlist_cache);
235cfbc4
BS
3365 t->next = save_expr_cache;
3366 t->expr = x;
3367 save_expr_cache = t;
3368
3369 tmp_before = tmp_nosp = 0;
3370 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3371 warn_for_collisions (tmp_nosp);
3372
3373 tmp_list3 = 0;
c2bf53a1 3374 merge_tlist (&tmp_list3, tmp_nosp, 0);
235cfbc4
BS
3375 t->cache_before_sp = tmp_before;
3376 t->cache_after_sp = tmp_list3;
2683ed8d 3377 }
235cfbc4
BS
3378 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3379 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3380 return;
3381 }
2683ed8d 3382
528c22f4
MLI
3383 case ADDR_EXPR:
3384 x = TREE_OPERAND (x, 0);
3385 if (DECL_P (x))
3386 return;
3387 writer = 0;
3388 goto restart;
3389
6615c446
JO
3390 default:
3391 /* For other expressions, simply recurse on their operands.
c22cacf3 3392 Manual tail recursion for unary expressions.
6615c446
JO
3393 Other non-expressions need not be processed. */
3394 if (cl == tcc_unary)
3395 {
6615c446
JO
3396 x = TREE_OPERAND (x, 0);
3397 writer = 0;
3398 goto restart;
3399 }
3400 else if (IS_EXPR_CODE_CLASS (cl))
3401 {
3402 int lp;
5039610b 3403 int max = TREE_OPERAND_LENGTH (x);
6615c446
JO
3404 for (lp = 0; lp < max; lp++)
3405 {
3406 tmp_before = tmp_nosp = 0;
3407 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3408 merge_tlist (&tmp_nosp, tmp_before, 0);
3409 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3410 }
3411 }
3412 return;
2683ed8d 3413 }
2683ed8d
BS
3414}
3415
8d9afc4e 3416/* Try to warn for undefined behavior in EXPR due to missing sequence
2683ed8d
BS
3417 points. */
3418
24e47c76 3419DEBUG_FUNCTION void
35b1a6fa 3420verify_sequence_points (tree expr)
2683ed8d 3421{
235cfbc4 3422 struct tlist *before_sp = 0, *after_sp = 0;
2683ed8d 3423
235cfbc4
BS
3424 warned_ids = 0;
3425 save_expr_cache = 0;
3426 if (tlist_firstobj == 0)
2683ed8d 3427 {
235cfbc4 3428 gcc_obstack_init (&tlist_obstack);
28dab132 3429 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2683ed8d
BS
3430 }
3431
235cfbc4
BS
3432 verify_tree (expr, &before_sp, &after_sp, 0);
3433 warn_for_collisions (after_sp);
3434 obstack_free (&tlist_obstack, tlist_firstobj);
2683ed8d 3435}
b30f223b
RS
3436\f
3437/* Validate the expression after `case' and apply default promotions. */
3438
a6c0a76c 3439static tree
62e4eb35 3440check_case_value (location_t loc, tree value)
b30f223b
RS
3441{
3442 if (value == NULL_TREE)
3443 return value;
3444
522ddfa2
JM
3445 if (TREE_CODE (value) == INTEGER_CST)
3446 /* Promote char or short to int. */
3447 value = perform_integral_promotions (value);
3448 else if (value != error_mark_node)
b30f223b 3449 {
62e4eb35 3450 error_at (loc, "case label does not reduce to an integer constant");
b30f223b
RS
3451 value = error_mark_node;
3452 }
b30f223b 3453
bc690db1
RS
3454 constant_expression_warning (value);
3455
b30f223b
RS
3456 return value;
3457}
3458\f
a6c0a76c 3459/* See if the case values LOW and HIGH are in the range of the original
89dbed81 3460 type (i.e. before the default conversion to int) of the switch testing
a6c0a76c
SB
3461 expression.
3462 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2a7e31df 3463 the type before promoting it. CASE_LOW_P is a pointer to the lower
a6c0a76c
SB
3464 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3465 if the case is not a case range.
3466 The caller has to make sure that we are not called with NULL for
89dbed81 3467 CASE_LOW_P (i.e. the default case).
0fa2e4df 3468 Returns true if the case label is in range of ORIG_TYPE (saturated or
a6c0a76c
SB
3469 untouched) or false if the label is out of range. */
3470
3471static bool
9d548dfb 3472check_case_bounds (location_t loc, tree type, tree orig_type,
a6c0a76c
SB
3473 tree *case_low_p, tree *case_high_p)
3474{
3475 tree min_value, max_value;
3476 tree case_low = *case_low_p;
3477 tree case_high = case_high_p ? *case_high_p : case_low;
3478
3479 /* If there was a problem with the original type, do nothing. */
3480 if (orig_type == error_mark_node)
3481 return true;
3482
3483 min_value = TYPE_MIN_VALUE (orig_type);
3484 max_value = TYPE_MAX_VALUE (orig_type);
3485
3486 /* Case label is less than minimum for type. */
3487 if (tree_int_cst_compare (case_low, min_value) < 0
3488 && tree_int_cst_compare (case_high, min_value) < 0)
3489 {
9d548dfb
MP
3490 warning_at (loc, 0, "case label value is less than minimum value "
3491 "for type");
a6c0a76c
SB
3492 return false;
3493 }
9f63daea 3494
a6c0a76c
SB
3495 /* Case value is greater than maximum for type. */
3496 if (tree_int_cst_compare (case_low, max_value) > 0
3497 && tree_int_cst_compare (case_high, max_value) > 0)
3498 {
9d548dfb 3499 warning_at (loc, 0, "case label value exceeds maximum value for type");
a6c0a76c
SB
3500 return false;
3501 }
3502
3503 /* Saturate lower case label value to minimum. */
3504 if (tree_int_cst_compare (case_high, min_value) >= 0
3505 && tree_int_cst_compare (case_low, min_value) < 0)
3506 {
9d548dfb
MP
3507 warning_at (loc, 0, "lower value in case label range"
3508 " less than minimum value for type");
a6c0a76c
SB
3509 case_low = min_value;
3510 }
9f63daea 3511
a6c0a76c
SB
3512 /* Saturate upper case label value to maximum. */
3513 if (tree_int_cst_compare (case_low, max_value) <= 0
3514 && tree_int_cst_compare (case_high, max_value) > 0)
3515 {
9d548dfb
MP
3516 warning_at (loc, 0, "upper value in case label range"
3517 " exceeds maximum value for type");
a6c0a76c
SB
3518 case_high = max_value;
3519 }
3520
3521 if (*case_low_p != case_low)
3522 *case_low_p = convert (type, case_low);
3523 if (case_high_p && *case_high_p != case_high)
3524 *case_high_p = convert (type, case_high);
3525
3526 return true;
3527}
3528\f
b30f223b
RS
3529/* Return an integer type with BITS bits of precision,
3530 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3531
3532tree
35b1a6fa 3533c_common_type_for_size (unsigned int bits, int unsignedp)
b30f223b 3534{
78a7c317
DD
3535 int i;
3536
a311b52c
JM
3537 if (bits == TYPE_PRECISION (integer_type_node))
3538 return unsignedp ? unsigned_type_node : integer_type_node;
3539
3fc7e390 3540 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
3541 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3542
3fc7e390 3543 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
3544 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3545
3fc7e390 3546 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
3547 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3548
3fc7e390 3549 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
3550 return (unsignedp ? long_long_unsigned_type_node
3551 : long_long_integer_type_node);
3552
78a7c317
DD
3553 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3554 if (int_n_enabled_p[i]
3555 && bits == int_n_data[i].bitsize)
3556 return (unsignedp ? int_n_trees[i].unsigned_type
3557 : int_n_trees[i].signed_type);
a6766312 3558
835f9b4d
GRK
3559 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3560 return (unsignedp ? widest_unsigned_literal_type_node
3561 : widest_integer_literal_type_node);
3562
3fc7e390
RS
3563 if (bits <= TYPE_PRECISION (intQI_type_node))
3564 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3565
3566 if (bits <= TYPE_PRECISION (intHI_type_node))
3567 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3568
3569 if (bits <= TYPE_PRECISION (intSI_type_node))
3570 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3571
3572 if (bits <= TYPE_PRECISION (intDI_type_node))
3573 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3574
b30f223b
RS
3575 return 0;
3576}
3577
ab22c1fa
CF
3578/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3579 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3580 and saturating if SATP is nonzero, otherwise not saturating. */
3581
3582tree
3583c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3584 int unsignedp, int satp)
3585{
ef4bddc2 3586 machine_mode mode;
ab22c1fa
CF
3587 if (ibit == 0)
3588 mode = unsignedp ? UQQmode : QQmode;
3589 else
3590 mode = unsignedp ? UHAmode : HAmode;
3591
3592 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3593 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3594 break;
3595
3596 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3597 {
3598 sorry ("GCC cannot support operators with integer types and "
3599 "fixed-point types that have too many integral and "
3600 "fractional bits together");
3601 return 0;
3602 }
3603
3604 return c_common_type_for_mode (mode, satp);
3605}
3606
d1d3865f
ZW
3607/* Used for communication between c_common_type_for_mode and
3608 c_register_builtin_type. */
793c625f 3609tree registered_builtin_types;
d1d3865f 3610
b30f223b
RS
3611/* Return a data type that has machine mode MODE.
3612 If the mode is an integer,
ab22c1fa
CF
3613 then UNSIGNEDP selects between signed and unsigned types.
3614 If the mode is a fixed-point mode,
3615 then UNSIGNEDP selects between saturating and nonsaturating types. */
b30f223b
RS
3616
3617tree
ef4bddc2 3618c_common_type_for_mode (machine_mode mode, int unsignedp)
b30f223b 3619{
d1d3865f 3620 tree t;
78a7c317 3621 int i;
d1d3865f 3622
a311b52c
JM
3623 if (mode == TYPE_MODE (integer_type_node))
3624 return unsignedp ? unsigned_type_node : integer_type_node;
3625
b30f223b
RS
3626 if (mode == TYPE_MODE (signed_char_type_node))
3627 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3628
3629 if (mode == TYPE_MODE (short_integer_type_node))
3630 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3631
b30f223b
RS
3632 if (mode == TYPE_MODE (long_integer_type_node))
3633 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3634
3635 if (mode == TYPE_MODE (long_long_integer_type_node))
3636 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3637
78a7c317
DD
3638 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3639 if (int_n_enabled_p[i]
3640 && mode == int_n_data[i].m)
3641 return (unsignedp ? int_n_trees[i].unsigned_type
3642 : int_n_trees[i].signed_type);
a6766312 3643
835f9b4d 3644 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 3645 return unsignedp ? widest_unsigned_literal_type_node
6de9cd9a 3646 : widest_integer_literal_type_node;
835f9b4d 3647
0afeef64 3648 if (mode == QImode)
3fc7e390
RS
3649 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3650
0afeef64 3651 if (mode == HImode)
3fc7e390
RS
3652 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3653
0afeef64 3654 if (mode == SImode)
3fc7e390
RS
3655 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3656
0afeef64 3657 if (mode == DImode)
3fc7e390
RS
3658 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3659
21a9616b 3660#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
3661 if (mode == TYPE_MODE (intTI_type_node))
3662 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 3663#endif
a6d7e156 3664
b30f223b
RS
3665 if (mode == TYPE_MODE (float_type_node))
3666 return float_type_node;
3667
3668 if (mode == TYPE_MODE (double_type_node))
3669 return double_type_node;
3670
3671 if (mode == TYPE_MODE (long_double_type_node))
3672 return long_double_type_node;
3673
ff42324e
NS
3674 if (mode == TYPE_MODE (void_type_node))
3675 return void_type_node;
9f63daea 3676
b30f223b 3677 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
19b3ffbc
DD
3678 return (unsignedp
3679 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3680 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b
RS
3681
3682 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
19b3ffbc
DD
3683 return (unsignedp
3684 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3685 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b 3686
7e7e470f
RH
3687 if (COMPLEX_MODE_P (mode))
3688 {
ef4bddc2 3689 machine_mode inner_mode;
7e7e470f
RH
3690 tree inner_type;
3691
3692 if (mode == TYPE_MODE (complex_float_type_node))
3693 return complex_float_type_node;
3694 if (mode == TYPE_MODE (complex_double_type_node))
3695 return complex_double_type_node;
3696 if (mode == TYPE_MODE (complex_long_double_type_node))
3697 return complex_long_double_type_node;
3698
3699 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3700 return complex_integer_type_node;
3701
3702 inner_mode = GET_MODE_INNER (mode);
3703 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3704 if (inner_type != NULL_TREE)
3705 return build_complex_type (inner_type);
3706 }
3707 else if (VECTOR_MODE_P (mode))
4a5eab38 3708 {
ef4bddc2 3709 machine_mode inner_mode = GET_MODE_INNER (mode);
4a5eab38
PB
3710 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3711 if (inner_type != NULL_TREE)
3712 return build_vector_type_for_mode (inner_type, mode);
0afeef64 3713 }
4061f623 3714
9a8ce21f
JG
3715 if (mode == TYPE_MODE (dfloat32_type_node))
3716 return dfloat32_type_node;
3717 if (mode == TYPE_MODE (dfloat64_type_node))
3718 return dfloat64_type_node;
3719 if (mode == TYPE_MODE (dfloat128_type_node))
3720 return dfloat128_type_node;
3721
ab22c1fa
CF
3722 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3723 {
3724 if (mode == TYPE_MODE (short_fract_type_node))
3725 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3726 if (mode == TYPE_MODE (fract_type_node))
3727 return unsignedp ? sat_fract_type_node : fract_type_node;
3728 if (mode == TYPE_MODE (long_fract_type_node))
3729 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3730 if (mode == TYPE_MODE (long_long_fract_type_node))
3731 return unsignedp ? sat_long_long_fract_type_node
3732 : long_long_fract_type_node;
3733
3734 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3735 return unsignedp ? sat_unsigned_short_fract_type_node
3736 : unsigned_short_fract_type_node;
3737 if (mode == TYPE_MODE (unsigned_fract_type_node))
3738 return unsignedp ? sat_unsigned_fract_type_node
3739 : unsigned_fract_type_node;
3740 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3741 return unsignedp ? sat_unsigned_long_fract_type_node
3742 : unsigned_long_fract_type_node;
3743 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3744 return unsignedp ? sat_unsigned_long_long_fract_type_node
3745 : unsigned_long_long_fract_type_node;
3746
3747 if (mode == TYPE_MODE (short_accum_type_node))
3748 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3749 if (mode == TYPE_MODE (accum_type_node))
3750 return unsignedp ? sat_accum_type_node : accum_type_node;
3751 if (mode == TYPE_MODE (long_accum_type_node))
3752 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3753 if (mode == TYPE_MODE (long_long_accum_type_node))
3754 return unsignedp ? sat_long_long_accum_type_node
3755 : long_long_accum_type_node;
3756
3757 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3758 return unsignedp ? sat_unsigned_short_accum_type_node
3759 : unsigned_short_accum_type_node;
3760 if (mode == TYPE_MODE (unsigned_accum_type_node))
3761 return unsignedp ? sat_unsigned_accum_type_node
3762 : unsigned_accum_type_node;
3763 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3764 return unsignedp ? sat_unsigned_long_accum_type_node
3765 : unsigned_long_accum_type_node;
3766 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3767 return unsignedp ? sat_unsigned_long_long_accum_type_node
3768 : unsigned_long_long_accum_type_node;
3769
3770 if (mode == QQmode)
3771 return unsignedp ? sat_qq_type_node : qq_type_node;
3772 if (mode == HQmode)
3773 return unsignedp ? sat_hq_type_node : hq_type_node;
3774 if (mode == SQmode)
3775 return unsignedp ? sat_sq_type_node : sq_type_node;
3776 if (mode == DQmode)
3777 return unsignedp ? sat_dq_type_node : dq_type_node;
3778 if (mode == TQmode)
3779 return unsignedp ? sat_tq_type_node : tq_type_node;
3780
3781 if (mode == UQQmode)
3782 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3783 if (mode == UHQmode)
3784 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3785 if (mode == USQmode)
3786 return unsignedp ? sat_usq_type_node : usq_type_node;
3787 if (mode == UDQmode)
3788 return unsignedp ? sat_udq_type_node : udq_type_node;
3789 if (mode == UTQmode)
3790 return unsignedp ? sat_utq_type_node : utq_type_node;
3791
3792 if (mode == HAmode)
3793 return unsignedp ? sat_ha_type_node : ha_type_node;
3794 if (mode == SAmode)
3795 return unsignedp ? sat_sa_type_node : sa_type_node;
3796 if (mode == DAmode)
3797 return unsignedp ? sat_da_type_node : da_type_node;
3798 if (mode == TAmode)
3799 return unsignedp ? sat_ta_type_node : ta_type_node;
3800
3801 if (mode == UHAmode)
3802 return unsignedp ? sat_uha_type_node : uha_type_node;
3803 if (mode == USAmode)
3804 return unsignedp ? sat_usa_type_node : usa_type_node;
3805 if (mode == UDAmode)
3806 return unsignedp ? sat_uda_type_node : uda_type_node;
3807 if (mode == UTAmode)
3808 return unsignedp ? sat_uta_type_node : uta_type_node;
3809 }
3810
d1d3865f 3811 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
7a421706
MS
3812 if (TYPE_MODE (TREE_VALUE (t)) == mode
3813 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
d1d3865f
ZW
3814 return TREE_VALUE (t);
3815
b30f223b
RS
3816 return 0;
3817}
693a6128 3818
12753674
RE
3819tree
3820c_common_unsigned_type (tree type)
3821{
3822 return c_common_signed_or_unsigned_type (1, type);
3823}
3824
693a6128
GRK
3825/* Return a signed type the same as TYPE in other respects. */
3826
3827tree
35b1a6fa 3828c_common_signed_type (tree type)
693a6128 3829{
ceef8ce4 3830 return c_common_signed_or_unsigned_type (0, type);
693a6128
GRK
3831}
3832
3833/* Return a type the same as TYPE except unsigned or
3834 signed according to UNSIGNEDP. */
3835
3836tree
35b1a6fa 3837c_common_signed_or_unsigned_type (int unsignedp, tree type)
693a6128 3838{
c74a03d2 3839 tree type1;
78a7c317 3840 int i;
693a6128 3841
c74a03d2
RAE
3842 /* This block of code emulates the behavior of the old
3843 c_common_unsigned_type. In particular, it returns
3844 long_unsigned_type_node if passed a long, even when a int would
3845 have the same size. This is necessary for warnings to work
3846 correctly in archs where sizeof(int) == sizeof(long) */
3847
3848 type1 = TYPE_MAIN_VARIANT (type);
3849 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3850 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3851 if (type1 == integer_type_node || type1 == unsigned_type_node)
3852 return unsignedp ? unsigned_type_node : integer_type_node;
3853 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3854 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3855 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3856 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3857 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3858 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
78a7c317
DD
3859
3860 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3861 if (int_n_enabled_p[i]
3862 && (type1 == int_n_trees[i].unsigned_type
3863 || type1 == int_n_trees[i].signed_type))
3864 return (unsignedp ? int_n_trees[i].unsigned_type
3865 : int_n_trees[i].signed_type);
3866
c74a03d2
RAE
3867 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3868 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3869#if HOST_BITS_PER_WIDE_INT >= 64
3870 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3871 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3872#endif
3873 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3874 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3875 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3876 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3877 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3878 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3879 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3880 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3881
70d3fcab
AH
3882#define C_COMMON_FIXED_TYPES(NAME) \
3883 if (type1 == short_ ## NAME ## _type_node \
3884 || type1 == unsigned_short_ ## NAME ## _type_node) \
3885 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3886 : short_ ## NAME ## _type_node; \
3887 if (type1 == NAME ## _type_node \
3888 || type1 == unsigned_ ## NAME ## _type_node) \
3889 return unsignedp ? unsigned_ ## NAME ## _type_node \
3890 : NAME ## _type_node; \
3891 if (type1 == long_ ## NAME ## _type_node \
3892 || type1 == unsigned_long_ ## NAME ## _type_node) \
3893 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3894 : long_ ## NAME ## _type_node; \
3895 if (type1 == long_long_ ## NAME ## _type_node \
3896 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3897 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3898 : long_long_ ## NAME ## _type_node;
3899
3900#define C_COMMON_FIXED_MODE_TYPES(NAME) \
3901 if (type1 == NAME ## _type_node \
3902 || type1 == u ## NAME ## _type_node) \
3903 return unsignedp ? u ## NAME ## _type_node \
3904 : NAME ## _type_node;
3905
3906#define C_COMMON_FIXED_TYPES_SAT(NAME) \
3907 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3908 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3909 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3910 : sat_ ## short_ ## NAME ## _type_node; \
3911 if (type1 == sat_ ## NAME ## _type_node \
3912 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3913 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3914 : sat_ ## NAME ## _type_node; \
3915 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3916 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3917 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3918 : sat_ ## long_ ## NAME ## _type_node; \
3919 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3920 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3921 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3922 : sat_ ## long_long_ ## NAME ## _type_node;
3923
3924#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3925 if (type1 == sat_ ## NAME ## _type_node \
3926 || type1 == sat_ ## u ## NAME ## _type_node) \
3927 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3928 : sat_ ## NAME ## _type_node;
3929
3930 C_COMMON_FIXED_TYPES (fract);
3931 C_COMMON_FIXED_TYPES_SAT (fract);
3932 C_COMMON_FIXED_TYPES (accum);
3933 C_COMMON_FIXED_TYPES_SAT (accum);
3934
3935 C_COMMON_FIXED_MODE_TYPES (qq);
3936 C_COMMON_FIXED_MODE_TYPES (hq);
3937 C_COMMON_FIXED_MODE_TYPES (sq);
3938 C_COMMON_FIXED_MODE_TYPES (dq);
3939 C_COMMON_FIXED_MODE_TYPES (tq);
3940 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3941 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3942 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3943 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3944 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3945 C_COMMON_FIXED_MODE_TYPES (ha);
3946 C_COMMON_FIXED_MODE_TYPES (sa);
3947 C_COMMON_FIXED_MODE_TYPES (da);
3948 C_COMMON_FIXED_MODE_TYPES (ta);
3949 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3950 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3951 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3952 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
ab22c1fa 3953
bc15d0ef
JM
3954 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3955 the precision; they have precision set to match their range, but
3956 may use a wider mode to match an ABI. If we change modes, we may
3957 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3958 the precision as well, so as to yield correct results for
3959 bit-field types. C++ does not have these separate bit-field
3960 types, and producing a signed or unsigned variant of an
3961 ENUMERAL_TYPE may cause other problems as well. */
3962
1e204133
RAE
3963 if (!INTEGRAL_TYPE_P (type)
3964 || TYPE_UNSIGNED (type) == unsignedp)
3965 return type;
3966
bc15d0ef
JM
3967#define TYPE_OK(node) \
3968 (TYPE_MODE (type) == TYPE_MODE (node) \
41b81065 3969 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
bc15d0ef 3970 if (TYPE_OK (signed_char_type_node))
693a6128 3971 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
bc15d0ef 3972 if (TYPE_OK (integer_type_node))
693a6128 3973 return unsignedp ? unsigned_type_node : integer_type_node;
bc15d0ef 3974 if (TYPE_OK (short_integer_type_node))
693a6128 3975 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
bc15d0ef 3976 if (TYPE_OK (long_integer_type_node))
693a6128 3977 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
bc15d0ef 3978 if (TYPE_OK (long_long_integer_type_node))
693a6128
GRK
3979 return (unsignedp ? long_long_unsigned_type_node
3980 : long_long_integer_type_node);
78a7c317
DD
3981
3982 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3983 if (int_n_enabled_p[i]
3984 && TYPE_MODE (type) == int_n_data[i].m
3985 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3986 return (unsignedp ? int_n_trees[i].unsigned_type
3987 : int_n_trees[i].signed_type);
3988
bc15d0ef 3989 if (TYPE_OK (widest_integer_literal_type_node))
693a6128
GRK
3990 return (unsignedp ? widest_unsigned_literal_type_node
3991 : widest_integer_literal_type_node);
4a063bec
RH
3992
3993#if HOST_BITS_PER_WIDE_INT >= 64
bc15d0ef 3994 if (TYPE_OK (intTI_type_node))
4a063bec
RH
3995 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3996#endif
bc15d0ef 3997 if (TYPE_OK (intDI_type_node))
4a063bec 3998 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
bc15d0ef 3999 if (TYPE_OK (intSI_type_node))
4a063bec 4000 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
bc15d0ef 4001 if (TYPE_OK (intHI_type_node))
4a063bec 4002 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
bc15d0ef 4003 if (TYPE_OK (intQI_type_node))
4a063bec 4004 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
bc15d0ef 4005#undef TYPE_OK
4a063bec 4006
41b81065 4007 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
693a6128 4008}
9649812a 4009
38a4afee
MM
4010/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4011
4012tree
4013c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4014{
78a7c317
DD
4015 int i;
4016
38a4afee
MM
4017 /* Extended integer types of the same width as a standard type have
4018 lesser rank, so those of the same width as int promote to int or
4019 unsigned int and are valid for printf formats expecting int or
4020 unsigned int. To avoid such special cases, avoid creating
4021 extended integer types for bit-fields if a standard integer type
4022 is available. */
4023 if (width == TYPE_PRECISION (integer_type_node))
4024 return unsignedp ? unsigned_type_node : integer_type_node;
4025 if (width == TYPE_PRECISION (signed_char_type_node))
4026 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4027 if (width == TYPE_PRECISION (short_integer_type_node))
4028 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4029 if (width == TYPE_PRECISION (long_integer_type_node))
4030 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4031 if (width == TYPE_PRECISION (long_long_integer_type_node))
4032 return (unsignedp ? long_long_unsigned_type_node
4033 : long_long_integer_type_node);
78a7c317
DD
4034 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4035 if (int_n_enabled_p[i]
4036 && width == int_n_data[i].bitsize)
4037 return (unsignedp ? int_n_trees[i].unsigned_type
4038 : int_n_trees[i].signed_type);
38a4afee
MM
4039 return build_nonstandard_integer_type (width, unsignedp);
4040}
4041
9649812a
MM
4042/* The C version of the register_builtin_type langhook. */
4043
4044void
4045c_register_builtin_type (tree type, const char* name)
4046{
4047 tree decl;
4048
c2255bc4
AH
4049 decl = build_decl (UNKNOWN_LOCATION,
4050 TYPE_DECL, get_identifier (name), type);
9649812a
MM
4051 DECL_ARTIFICIAL (decl) = 1;
4052 if (!TYPE_NAME (type))
4053 TYPE_NAME (type) = decl;
4054 pushdecl (decl);
d1d3865f
ZW
4055
4056 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
9649812a 4057}
6acfe908 4058\f
78ef5b89 4059/* Print an error message for invalid operands to arith operation
ba47d38d
AH
4060 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4061 LOCATION is the location of the message. */
b30f223b
RS
4062
4063void
ba47d38d
AH
4064binary_op_error (location_t location, enum tree_code code,
4065 tree type0, tree type1)
b30f223b 4066{
b3694847 4067 const char *opname;
89c78d7d 4068
b30f223b
RS
4069 switch (code)
4070 {
b30f223b
RS
4071 case PLUS_EXPR:
4072 opname = "+"; break;
4073 case MINUS_EXPR:
4074 opname = "-"; break;
4075 case MULT_EXPR:
4076 opname = "*"; break;
4077 case MAX_EXPR:
4078 opname = "max"; break;
4079 case MIN_EXPR:
4080 opname = "min"; break;
4081 case EQ_EXPR:
4082 opname = "=="; break;
4083 case NE_EXPR:
4084 opname = "!="; break;
4085 case LE_EXPR:
4086 opname = "<="; break;
4087 case GE_EXPR:
4088 opname = ">="; break;
4089 case LT_EXPR:
4090 opname = "<"; break;
4091 case GT_EXPR:
4092 opname = ">"; break;
4093 case LSHIFT_EXPR:
4094 opname = "<<"; break;
4095 case RSHIFT_EXPR:
4096 opname = ">>"; break;
4097 case TRUNC_MOD_EXPR:
047de90b 4098 case FLOOR_MOD_EXPR:
b30f223b
RS
4099 opname = "%"; break;
4100 case TRUNC_DIV_EXPR:
047de90b 4101 case FLOOR_DIV_EXPR:
b30f223b
RS
4102 opname = "/"; break;
4103 case BIT_AND_EXPR:
4104 opname = "&"; break;
4105 case BIT_IOR_EXPR:
4106 opname = "|"; break;
4107 case TRUTH_ANDIF_EXPR:
4108 opname = "&&"; break;
4109 case TRUTH_ORIF_EXPR:
4110 opname = "||"; break;
4111 case BIT_XOR_EXPR:
4112 opname = "^"; break;
6d819282 4113 default:
37b2f290 4114 gcc_unreachable ();
b30f223b 4115 }
ba47d38d
AH
4116 error_at (location,
4117 "invalid operands to binary %s (have %qT and %qT)", opname,
4118 type0, type1);
b30f223b
RS
4119}
4120\f
50f305ca
MLI
4121/* Given an expression as a tree, return its original type. Do this
4122 by stripping any conversion that preserves the sign and precision. */
4123static tree
4124expr_original_type (tree expr)
4125{
4126 STRIP_SIGN_NOPS (expr);
4127 return TREE_TYPE (expr);
4128}
4129
b30f223b
RS
4130/* Subroutine of build_binary_op, used for comparison operations.
4131 See if the operands have both been converted from subword integer types
4132 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
4133 This function is also responsible for converting the two operands
4134 to the proper common type for comparison.
b30f223b
RS
4135
4136 The arguments of this function are all pointers to local variables
4137 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4138 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4139
393e8e8b
MP
4140 LOC is the location of the comparison.
4141
b30f223b
RS
4142 If this function returns nonzero, it means that the comparison has
4143 a constant value. What this function returns is an expression for
4144 that value. */
4145
4146tree
393e8e8b
MP
4147shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4148 tree *restype_ptr, enum tree_code *rescode_ptr)
b30f223b 4149{
b3694847 4150 tree type;
b30f223b
RS
4151 tree op0 = *op0_ptr;
4152 tree op1 = *op1_ptr;
4153 int unsignedp0, unsignedp1;
4154 int real1, real2;
4155 tree primop0, primop1;
4156 enum tree_code code = *rescode_ptr;
4157
4158 /* Throw away any conversions to wider types
4159 already present in the operands. */
4160
828fb3ba
JM
4161 primop0 = c_common_get_narrower (op0, &unsignedp0);
4162 primop1 = c_common_get_narrower (op1, &unsignedp1);
b30f223b 4163
126e6609
JJ
4164 /* If primopN is first sign-extended from primopN's precision to opN's
4165 precision, then zero-extended from opN's precision to
4166 *restype_ptr precision, shortenings might be invalid. */
4167 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4168 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4169 && !unsignedp0
4170 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4171 primop0 = op0;
4172 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4173 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4174 && !unsignedp1
4175 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4176 primop1 = op1;
4177
b30f223b
RS
4178 /* Handle the case that OP0 does not *contain* a conversion
4179 but it *requires* conversion to FINAL_TYPE. */
4180
4181 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
8df83eae 4182 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b30f223b 4183 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
8df83eae 4184 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b30f223b
RS
4185
4186 /* If one of the operands must be floated, we cannot optimize. */
4187 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4188 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4189
4190 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
4191 so value is preserved), for canonicalization. Don't do this if
4192 the second arg is 0. */
b30f223b 4193
5af6001b 4194 if (TREE_CONSTANT (primop0)
ab22c1fa
CF
4195 && !integer_zerop (primop1) && !real_zerop (primop1)
4196 && !fixed_zerop (primop1))
b30f223b 4197 {
b3694847
SS
4198 tree tem = primop0;
4199 int temi = unsignedp0;
b30f223b
RS
4200 primop0 = primop1;
4201 primop1 = tem;
4202 tem = op0;
4203 op0 = op1;
4204 op1 = tem;
4205 *op0_ptr = op0;
4206 *op1_ptr = op1;
4207 unsignedp0 = unsignedp1;
4208 unsignedp1 = temi;
4209 temi = real1;
4210 real1 = real2;
4211 real2 = temi;
4212
4213 switch (code)
4214 {
4215 case LT_EXPR:
4216 code = GT_EXPR;
4217 break;
4218 case GT_EXPR:
4219 code = LT_EXPR;
4220 break;
4221 case LE_EXPR:
4222 code = GE_EXPR;
4223 break;
4224 case GE_EXPR:
4225 code = LE_EXPR;
4226 break;
6d819282
MK
4227 default:
4228 break;
b30f223b
RS
4229 }
4230 *rescode_ptr = code;
4231 }
4232
4233 /* If comparing an integer against a constant more bits wide,
4234 maybe we can deduce a value of 1 or 0 independent of the data.
4235 Or else truncate the constant now
4236 rather than extend the variable at run time.
4237
4238 This is only interesting if the constant is the wider arg.
4239 Also, it is not safe if the constant is unsigned and the
4240 variable arg is signed, since in this case the variable
4241 would be sign-extended and then regarded as unsigned.
4242 Our technique fails in this case because the lowest/highest
4243 possible unsigned results don't follow naturally from the
4244 lowest/highest possible values of the variable operand.
4245 For just EQ_EXPR and NE_EXPR there is another technique that
4246 could be used: see if the constant can be faithfully represented
4247 in the other operand's type, by truncating it and reextending it
4248 and see if that preserves the constant's value. */
4249
4250 if (!real1 && !real2
ab22c1fa 4251 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b30f223b
RS
4252 && TREE_CODE (primop1) == INTEGER_CST
4253 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4254 {
4255 int min_gt, max_gt, min_lt, max_lt;
4256 tree maxval, minval;
4257 /* 1 if comparison is nominally unsigned. */
8df83eae 4258 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b30f223b
RS
4259 tree val;
4260
ceef8ce4
NB
4261 type = c_common_signed_or_unsigned_type (unsignedp0,
4262 TREE_TYPE (primop0));
8bbd5685 4263
b30f223b
RS
4264 maxval = TYPE_MAX_VALUE (type);
4265 minval = TYPE_MIN_VALUE (type);
4266
4267 if (unsignedp && !unsignedp0)
ceef8ce4 4268 *restype_ptr = c_common_signed_type (*restype_ptr);
b30f223b
RS
4269
4270 if (TREE_TYPE (primop1) != *restype_ptr)
fae1b38d 4271 {
af9c6659
NS
4272 /* Convert primop1 to target type, but do not introduce
4273 additional overflow. We know primop1 is an int_cst. */
807e902e
KZ
4274 primop1 = force_fit_type (*restype_ptr,
4275 wide_int::from
4276 (primop1,
4277 TYPE_PRECISION (*restype_ptr),
4278 TYPE_SIGN (TREE_TYPE (primop1))),
4279 0, TREE_OVERFLOW (primop1));
fae1b38d 4280 }
b30f223b
RS
4281 if (type != *restype_ptr)
4282 {
4283 minval = convert (*restype_ptr, minval);
4284 maxval = convert (*restype_ptr, maxval);
4285 }
4286
807e902e
KZ
4287 min_gt = tree_int_cst_lt (primop1, minval);
4288 max_gt = tree_int_cst_lt (primop1, maxval);
4289 min_lt = tree_int_cst_lt (minval, primop1);
4290 max_lt = tree_int_cst_lt (maxval, primop1);
b30f223b
RS
4291
4292 val = 0;
4293 /* This used to be a switch, but Genix compiler can't handle that. */
4294 if (code == NE_EXPR)
4295 {
4296 if (max_lt || min_gt)
de7df9eb 4297 val = truthvalue_true_node;
b30f223b
RS
4298 }
4299 else if (code == EQ_EXPR)
4300 {
4301 if (max_lt || min_gt)
de7df9eb 4302 val = truthvalue_false_node;
b30f223b
RS
4303 }
4304 else if (code == LT_EXPR)
4305 {
4306 if (max_lt)
de7df9eb 4307 val = truthvalue_true_node;
b30f223b 4308 if (!min_lt)
de7df9eb 4309 val = truthvalue_false_node;
b30f223b
RS
4310 }
4311 else if (code == GT_EXPR)
4312 {
4313 if (min_gt)
de7df9eb 4314 val = truthvalue_true_node;
b30f223b 4315 if (!max_gt)
de7df9eb 4316 val = truthvalue_false_node;
b30f223b
RS
4317 }
4318 else if (code == LE_EXPR)
4319 {
4320 if (!max_gt)
de7df9eb 4321 val = truthvalue_true_node;
b30f223b 4322 if (min_gt)
de7df9eb 4323 val = truthvalue_false_node;
b30f223b
RS
4324 }
4325 else if (code == GE_EXPR)
4326 {
4327 if (!min_lt)
de7df9eb 4328 val = truthvalue_true_node;
b30f223b 4329 if (max_lt)
de7df9eb 4330 val = truthvalue_false_node;
b30f223b
RS
4331 }
4332
4333 /* If primop0 was sign-extended and unsigned comparison specd,
4334 we did a signed comparison above using the signed type bounds.
4335 But the comparison we output must be unsigned.
4336
4337 Also, for inequalities, VAL is no good; but if the signed
4338 comparison had *any* fixed result, it follows that the
4339 unsigned comparison just tests the sign in reverse
4340 (positive values are LE, negative ones GE).
4341 So we can generate an unsigned comparison
4342 against an extreme value of the signed type. */
4343
4344 if (unsignedp && !unsignedp0)
4345 {
4346 if (val != 0)
4347 switch (code)
4348 {
4349 case LT_EXPR:
4350 case GE_EXPR:
4351 primop1 = TYPE_MIN_VALUE (type);
4352 val = 0;
4353 break;
4354
4355 case LE_EXPR:
4356 case GT_EXPR:
4357 primop1 = TYPE_MAX_VALUE (type);
4358 val = 0;
4359 break;
6d819282
MK
4360
4361 default:
4362 break;
b30f223b 4363 }
12753674 4364 type = c_common_unsigned_type (type);
b30f223b
RS
4365 }
4366
ca7e759d 4367 if (TREE_CODE (primop0) != INTEGER_CST)
b30f223b 4368 {
de7df9eb 4369 if (val == truthvalue_false_node)
50f305ca
MLI
4370 warning_at (loc, OPT_Wtype_limits,
4371 "comparison is always false due to limited range of data type");
de7df9eb 4372 if (val == truthvalue_true_node)
50f305ca
MLI
4373 warning_at (loc, OPT_Wtype_limits,
4374 "comparison is always true due to limited range of data type");
b30f223b
RS
4375 }
4376
4377 if (val != 0)
4378 {
4379 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4380 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3 4381 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b30f223b
RS
4382 return val;
4383 }
4384
4385 /* Value is not predetermined, but do the comparison
4386 in the type of the operand that is not constant.
4387 TYPE is already properly set. */
4388 }
9a8ce21f
JG
4389
4390 /* If either arg is decimal float and the other is float, find the
4391 proper common type to use for comparison. */
6f450181
RB
4392 else if (real1 && real2
4393 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4394 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4395 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4396
4397 /* If either arg is decimal float and the other is float, fail. */
9a8ce21f
JG
4398 else if (real1 && real2
4399 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4400 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
6f450181 4401 return 0;
9a8ce21f 4402
b30f223b 4403 else if (real1 && real2
766f6c30
RS
4404 && (TYPE_PRECISION (TREE_TYPE (primop0))
4405 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
4406 type = TREE_TYPE (primop0);
4407
4408 /* If args' natural types are both narrower than nominal type
4409 and both extend in the same manner, compare them
4410 in the type of the wider arg.
4411 Otherwise must actually extend both to the nominal
4412 common type lest different ways of extending
4413 alter the result.
4414 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4415
4416 else if (unsignedp0 == unsignedp1 && real1 == real2
4417 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4418 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4419 {
4420 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
ceef8ce4 4421 type = c_common_signed_or_unsigned_type (unsignedp0
8df83eae 4422 || TYPE_UNSIGNED (*restype_ptr),
ceef8ce4 4423 type);
b30f223b
RS
4424 /* Make sure shorter operand is extended the right way
4425 to match the longer operand. */
ceef8ce4
NB
4426 primop0
4427 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4428 TREE_TYPE (primop0)),
4429 primop0);
4430 primop1
4431 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4432 TREE_TYPE (primop1)),
4433 primop1);
b30f223b
RS
4434 }
4435 else
4436 {
4437 /* Here we must do the comparison on the nominal type
4438 using the args exactly as we received them. */
4439 type = *restype_ptr;
4440 primop0 = op0;
4441 primop1 = op1;
4442
4443 if (!real1 && !real2 && integer_zerop (primop1)
8df83eae 4444 && TYPE_UNSIGNED (*restype_ptr))
b30f223b
RS
4445 {
4446 tree value = 0;
50f305ca
MLI
4447 /* All unsigned values are >= 0, so we warn. However,
4448 if OP0 is a constant that is >= 0, the signedness of
4449 the comparison isn't an issue, so suppress the
4450 warning. */
4451 bool warn =
8400e75e 4452 warn_type_limits && !in_system_header_at (loc)
50f305ca
MLI
4453 && !(TREE_CODE (primop0) == INTEGER_CST
4454 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4455 primop0)))
4456 /* Do not warn for enumeration types. */
4457 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4458
b30f223b
RS
4459 switch (code)
4460 {
4461 case GE_EXPR:
50f305ca
MLI
4462 if (warn)
4463 warning_at (loc, OPT_Wtype_limits,
4464 "comparison of unsigned expression >= 0 is always true");
de7df9eb 4465 value = truthvalue_true_node;
b30f223b
RS
4466 break;
4467
4468 case LT_EXPR:
50f305ca
MLI
4469 if (warn)
4470 warning_at (loc, OPT_Wtype_limits,
4471 "comparison of unsigned expression < 0 is always false");
de7df9eb 4472 value = truthvalue_false_node;
6d819282
MK
4473 break;
4474
4475 default:
4476 break;
b30f223b
RS
4477 }
4478
4479 if (value != 0)
4480 {
4481 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4482 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3
RS
4483 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4484 primop0, value);
b30f223b
RS
4485 return value;
4486 }
4487 }
4488 }
4489
4490 *op0_ptr = convert (type, primop0);
4491 *op1_ptr = convert (type, primop1);
4492
de7df9eb 4493 *restype_ptr = truthvalue_type_node;
b30f223b
RS
4494
4495 return 0;
4496}
4497\f
7552da58
JJ
4498/* Return a tree for the sum or difference (RESULTCODE says which)
4499 of pointer PTROP and integer INTOP. */
4500
4501tree
db3927fb 4502pointer_int_sum (location_t loc, enum tree_code resultcode,
fd9b0f32 4503 tree ptrop, tree intop, bool complain)
7552da58 4504{
6ac01510 4505 tree size_exp, ret;
7552da58 4506
7552da58 4507 /* The result is a pointer of the same type that is being added. */
7552da58
JJ
4508 tree result_type = TREE_TYPE (ptrop);
4509
4510 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4511 {
fd9b0f32
PC
4512 if (complain && warn_pointer_arith)
4513 pedwarn (loc, OPT_Wpointer_arith,
4514 "pointer of type %<void *%> used in arithmetic");
4515 else if (!complain)
4516 return error_mark_node;
7552da58
JJ
4517 size_exp = integer_one_node;
4518 }
4519 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4520 {
fd9b0f32
PC
4521 if (complain && warn_pointer_arith)
4522 pedwarn (loc, OPT_Wpointer_arith,
4523 "pointer to a function used in arithmetic");
4524 else if (!complain)
4525 return error_mark_node;
7552da58
JJ
4526 size_exp = integer_one_node;
4527 }
7552da58
JJ
4528 else
4529 size_exp = size_in_bytes (TREE_TYPE (result_type));
4530
6ac01510
ILT
4531 /* We are manipulating pointer values, so we don't need to warn
4532 about relying on undefined signed overflow. We disable the
4533 warning here because we use integer types so fold won't know that
4534 they are really pointers. */
4535 fold_defer_overflow_warnings ();
4536
7552da58
JJ
4537 /* If what we are about to multiply by the size of the elements
4538 contains a constant term, apply distributive law
4539 and multiply that constant term separately.
4540 This helps produce common subexpressions. */
7552da58 4541 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3f75a254 4542 && !TREE_CONSTANT (intop)
7552da58
JJ
4543 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4544 && TREE_CONSTANT (size_exp)
4545 /* If the constant comes from pointer subtraction,
4546 skip this optimization--it would cause an error. */
4547 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4548 /* If the constant is unsigned, and smaller than the pointer size,
4549 then we must skip this optimization. This is because it could cause
4550 an overflow error if the constant is negative but INTOP is not. */
3f75a254 4551 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
7552da58
JJ
4552 || (TYPE_PRECISION (TREE_TYPE (intop))
4553 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4554 {
4555 enum tree_code subcode = resultcode;
4556 tree int_type = TREE_TYPE (intop);
4557 if (TREE_CODE (intop) == MINUS_EXPR)
4558 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4559 /* Convert both subexpression types to the type of intop,
4560 because weird cases involving pointer arithmetic
4561 can result in a sum or difference with different type args. */
ba47d38d
AH
4562 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4563 subcode, ptrop,
7552da58
JJ
4564 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4565 intop = convert (int_type, TREE_OPERAND (intop, 0));
4566 }
4567
4568 /* Convert the integer argument to a type the same size as sizetype
4569 so the multiply won't overflow spuriously. */
7552da58 4570 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
8df83eae 4571 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
35b1a6fa 4572 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
8df83eae 4573 TYPE_UNSIGNED (sizetype)), intop);
7552da58
JJ
4574
4575 /* Replace the integer argument with a suitable product by the object size.
9e9ef331 4576 Do this multiplication as signed, then convert to the appropriate type
65de6659 4577 for the pointer operation and disregard an overflow that occurred only
9e9ef331
EB
4578 because of the sign-extension change in the latter conversion. */
4579 {
4580 tree t = build_binary_op (loc,
4581 MULT_EXPR, intop,
4582 convert (TREE_TYPE (intop), size_exp), 1);
4583 intop = convert (sizetype, t);
4584 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
807e902e 4585 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
9e9ef331 4586 }
5be014d5 4587
280f1ffa 4588 /* Create the sum or difference. */
5be014d5 4589 if (resultcode == MINUS_EXPR)
db3927fb 4590 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
7552da58 4591
5d49b6a7 4592 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
6ac01510
ILT
4593
4594 fold_undefer_and_ignore_overflow_warnings ();
4595
4596 return ret;
7552da58
JJ
4597}
4598\f
e5a94231
JM
4599/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4600 and if NON_CONST is known not to be permitted in an evaluated part
4601 of a constant expression. */
4602
4603tree
4604c_wrap_maybe_const (tree expr, bool non_const)
4605{
4606 bool nowarning = TREE_NO_WARNING (expr);
4607 location_t loc = EXPR_LOCATION (expr);
4608
4609 /* This should never be called for C++. */
4610 if (c_dialect_cxx ())
4611 gcc_unreachable ();
4612
4613 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4614 STRIP_TYPE_NOPS (expr);
4615 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4616 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4617 if (nowarning)
4618 TREE_NO_WARNING (expr) = 1;
4619 protected_set_expr_location (expr, loc);
4620
4621 return expr;
4622}
4623
928c19bb
JM
4624/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4625 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4626 around the SAVE_EXPR if needed so that c_fully_fold does not need
4627 to look inside SAVE_EXPRs. */
4628
4629tree
4630c_save_expr (tree expr)
4631{
4632 bool maybe_const = true;
4633 if (c_dialect_cxx ())
4634 return save_expr (expr);
4635 expr = c_fully_fold (expr, false, &maybe_const);
4636 expr = save_expr (expr);
4637 if (!maybe_const)
e5a94231 4638 expr = c_wrap_maybe_const (expr, true);
928c19bb
JM
4639 return expr;
4640}
4641
b3c6d2ea
ILT
4642/* Return whether EXPR is a declaration whose address can never be
4643 NULL. */
4644
4645bool
58f9752a 4646decl_with_nonnull_addr_p (const_tree expr)
b3c6d2ea
ILT
4647{
4648 return (DECL_P (expr)
4649 && (TREE_CODE (expr) == PARM_DECL
4650 || TREE_CODE (expr) == LABEL_DECL
4651 || !DECL_WEAK (expr)));
4652}
4653
b30f223b 4654/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
85498824
JM
4655 or for an `if' or `while' statement or ?..: exp. It should already
4656 have been validated to be of suitable type; otherwise, a bad
4657 diagnostic may result.
b30f223b 4658
ba47d38d
AH
4659 The EXPR is located at LOCATION.
4660
b30f223b
RS
4661 This preparation consists of taking the ordinary
4662 representation of an expression expr and producing a valid tree
4663 boolean expression describing whether expr is nonzero. We could
de7df9eb 4664 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b30f223b
RS
4665 but we optimize comparisons, &&, ||, and !.
4666
de7df9eb 4667 The resulting type should always be `truthvalue_type_node'. */
b30f223b
RS
4668
4669tree
ba47d38d 4670c_common_truthvalue_conversion (location_t location, tree expr)
b30f223b 4671{
b30f223b
RS
4672 switch (TREE_CODE (expr))
4673 {
d1a7edaf 4674 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
6f312d18
ZW
4675 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4676 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4677 case ORDERED_EXPR: case UNORDERED_EXPR:
90ec750d
RS
4678 if (TREE_TYPE (expr) == truthvalue_type_node)
4679 return expr;
c2255bc4 4680 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
90ec750d 4681 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
c2255bc4 4682 goto ret;
90ec750d 4683
b30f223b
RS
4684 case TRUTH_ANDIF_EXPR:
4685 case TRUTH_ORIF_EXPR:
4686 case TRUTH_AND_EXPR:
4687 case TRUTH_OR_EXPR:
9379fac9 4688 case TRUTH_XOR_EXPR:
90ec750d
RS
4689 if (TREE_TYPE (expr) == truthvalue_type_node)
4690 return expr;
c2255bc4 4691 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
b8698a0f 4692 c_common_truthvalue_conversion (location,
c2255bc4
AH
4693 TREE_OPERAND (expr, 0)),
4694 c_common_truthvalue_conversion (location,
4695 TREE_OPERAND (expr, 1)));
4696 goto ret;
18c0f675 4697
18d00205 4698 case TRUTH_NOT_EXPR:
90ec750d
RS
4699 if (TREE_TYPE (expr) == truthvalue_type_node)
4700 return expr;
c2255bc4
AH
4701 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4702 c_common_truthvalue_conversion (location,
4703 TREE_OPERAND (expr, 0)));
4704 goto ret;
18d00205 4705
b30f223b
RS
4706 case ERROR_MARK:
4707 return expr;
4708
4709 case INTEGER_CST:
d95787e6
RS
4710 return integer_zerop (expr) ? truthvalue_false_node
4711 : truthvalue_true_node;
b30f223b
RS
4712
4713 case REAL_CST:
010c4d9c
RS
4714 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4715 ? truthvalue_true_node
4716 : truthvalue_false_node;
b30f223b 4717
ab22c1fa
CF
4718 case FIXED_CST:
4719 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4720 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4721 ? truthvalue_true_node
4722 : truthvalue_false_node;
4723
90ec750d 4724 case FUNCTION_DECL:
c9f9eb5d 4725 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
90ec750d
RS
4726 /* Fall through. */
4727
b30f223b 4728 case ADDR_EXPR:
1998463c 4729 {
f6f08360 4730 tree inner = TREE_OPERAND (expr, 0);
b3c6d2ea 4731 if (decl_with_nonnull_addr_p (inner))
1998463c 4732 {
b3c6d2ea 4733 /* Common Ada/Pascal programmer's mistake. */
ba47d38d
AH
4734 warning_at (location,
4735 OPT_Waddress,
4736 "the address of %qD will always evaluate as %<true%>",
4737 inner);
1998463c
SB
4738 return truthvalue_true_node;
4739 }
33766b66 4740 break;
1998463c 4741 }
b30f223b 4742
766f6c30 4743 case COMPLEX_EXPR:
c2255bc4 4744 expr = build_binary_op (EXPR_LOCATION (expr),
ba47d38d 4745 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 4746 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
ba47d38d
AH
4747 c_common_truthvalue_conversion (location,
4748 TREE_OPERAND (expr, 0)),
4749 c_common_truthvalue_conversion (location,
4750 TREE_OPERAND (expr, 1)),
766f6c30 4751 0);
c2255bc4 4752 goto ret;
766f6c30 4753
b30f223b
RS
4754 case NEGATE_EXPR:
4755 case ABS_EXPR:
4756 case FLOAT_EXPR:
8ce94e44 4757 case EXCESS_PRECISION_EXPR:
da7d8304 4758 /* These don't change whether an object is nonzero or zero. */
ba47d38d 4759 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b30f223b
RS
4760
4761 case LROTATE_EXPR:
4762 case RROTATE_EXPR:
da7d8304 4763 /* These don't change whether an object is zero or nonzero, but
b30f223b
RS
4764 we can't ignore them if their second arg has side-effects. */
4765 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
c2255bc4
AH
4766 {
4767 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4768 TREE_OPERAND (expr, 1),
b8698a0f 4769 c_common_truthvalue_conversion
c2255bc4
AH
4770 (location, TREE_OPERAND (expr, 0)));
4771 goto ret;
4772 }
b30f223b 4773 else
ba47d38d
AH
4774 return c_common_truthvalue_conversion (location,
4775 TREE_OPERAND (expr, 0));
b57062ca 4776
b30f223b
RS
4777 case COND_EXPR:
4778 /* Distribute the conversion into the arms of a COND_EXPR. */
928c19bb 4779 if (c_dialect_cxx ())
c2255bc4 4780 {
4cc4f2f4
JJ
4781 tree op1 = TREE_OPERAND (expr, 1);
4782 tree op2 = TREE_OPERAND (expr, 2);
4783 /* In C++ one of the arms might have void type if it is throw. */
4784 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4785 op1 = c_common_truthvalue_conversion (location, op1);
4786 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4787 op2 = c_common_truthvalue_conversion (location, op2);
db3927fb 4788 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4cc4f2f4 4789 TREE_OPERAND (expr, 0), op1, op2);
c2255bc4
AH
4790 goto ret;
4791 }
928c19bb 4792 else
c2255bc4
AH
4793 {
4794 /* Folding will happen later for C. */
4795 expr = build3 (COND_EXPR, truthvalue_type_node,
4796 TREE_OPERAND (expr, 0),
4797 c_common_truthvalue_conversion (location,
4798 TREE_OPERAND (expr, 1)),
4799 c_common_truthvalue_conversion (location,
4800 TREE_OPERAND (expr, 2)));
4801 goto ret;
4802 }
b30f223b 4803
1043771b 4804 CASE_CONVERT:
1ee44b26
JM
4805 {
4806 tree totype = TREE_TYPE (expr);
4807 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4808
4809 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4810 since that affects how `default_conversion' will behave. */
4811 if (TREE_CODE (totype) == REFERENCE_TYPE
4812 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4813 break;
4814 /* Don't strip a conversion from C++0x scoped enum, since they
4815 don't implicitly convert to other types. */
4816 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4817 && ENUM_IS_SCOPED (fromtype))
4818 break;
4819 /* If this isn't narrowing the argument, we can ignore it. */
4820 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4821 return c_common_truthvalue_conversion (location,
4822 TREE_OPERAND (expr, 0));
4823 }
b30f223b
RS
4824 break;
4825
e2aab13d 4826 case MODIFY_EXPR:
fbc8d2d3
ILT
4827 if (!TREE_NO_WARNING (expr)
4828 && warn_parentheses)
4829 {
4830 warning (OPT_Wparentheses,
4831 "suggest parentheses around assignment used as truth value");
4832 TREE_NO_WARNING (expr) = 1;
4833 }
e2aab13d 4834 break;
b57062ca 4835
6d819282
MK
4836 default:
4837 break;
b30f223b
RS
4838 }
4839
f0b996c5 4840 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa 4841 {
5386338c 4842 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
c2255bc4 4843 expr = (build_binary_op
ba47d38d
AH
4844 (EXPR_LOCATION (expr),
4845 (TREE_SIDE_EFFECTS (expr)
f0b8d9aa 4846 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
c9f9eb5d
AH
4847 c_common_truthvalue_conversion
4848 (location,
4849 build_unary_op (location, REALPART_EXPR, t, 0)),
4850 c_common_truthvalue_conversion
4851 (location,
4852 build_unary_op (location, IMAGPART_EXPR, t, 0)),
f0b8d9aa 4853 0));
c2255bc4 4854 goto ret;
f0b8d9aa 4855 }
f0b996c5 4856
ab22c1fa
CF
4857 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4858 {
4859 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4860 FCONST0 (TYPE_MODE
4861 (TREE_TYPE (expr))));
ca80e52b 4862 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
ab22c1fa 4863 }
c2255bc4
AH
4864 else
4865 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
ab22c1fa 4866
c2255bc4
AH
4867 ret:
4868 protected_set_expr_location (expr, location);
4869 return expr;
b30f223b
RS
4870}
4871\f
9bc15050
RG
4872static void def_builtin_1 (enum built_in_function fncode,
4873 const char *name,
4874 enum built_in_class fnclass,
4875 tree fntype, tree libtype,
4876 bool both_p, bool fallback_p, bool nonansi_p,
4877 tree fnattrs, bool implicit_p);
fc2aaf30 4878
3932261a
MM
4879
4880/* Apply the TYPE_QUALS to the new DECL. */
4881
4882void
35b1a6fa 4883c_apply_type_quals_to_decl (int type_quals, tree decl)
3932261a 4884{
4b011bbf 4885 tree type = TREE_TYPE (decl);
9f63daea 4886
5a6159dd
AP
4887 if (type == error_mark_node)
4888 return;
4b011bbf 4889
329af3c7
JM
4890 if ((type_quals & TYPE_QUAL_CONST)
4891 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4892 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4893 constructor can produce constant init, so rely on cp_finish_decl to
4894 clear TREE_READONLY if the variable has non-constant init. */
3932261a
MM
4895 TREE_READONLY (decl) = 1;
4896 if (type_quals & TYPE_QUAL_VOLATILE)
4897 {
4898 TREE_SIDE_EFFECTS (decl) = 1;
4899 TREE_THIS_VOLATILE (decl) = 1;
4900 }
6946bc60 4901 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 4902 {
4b011bbf
JM
4903 while (type && TREE_CODE (type) == ARRAY_TYPE)
4904 /* Allow 'restrict' on arrays of pointers.
4905 FIXME currently we just ignore it. */
4906 type = TREE_TYPE (type);
4907 if (!type
4908 || !POINTER_TYPE_P (type)
4909 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
bda67431 4910 error ("invalid use of %<restrict%>");
3932261a
MM
4911 }
4912}
4913
2a22f99c
TS
4914struct c_type_hasher : ggc_hasher<tree>
4915{
4916 static hashval_t hash (tree);
4917 static bool equal (tree, tree);
4918};
4919
6de9cd9a
DN
4920/* Hash function for the problem of multiple type definitions in
4921 different files. This must hash all types that will compare
4922 equal via comptypes to the same value. In practice it hashes
9cf737f8 4923 on some of the simple stuff and leaves the details to comptypes. */
6de9cd9a 4924
2a22f99c
TS
4925hashval_t
4926c_type_hasher::hash (tree t)
6de9cd9a 4927{
a19e4d44 4928 int n_elements;
6de9cd9a 4929 int shift, size;
6de9cd9a
DN
4930 tree t2;
4931 switch (TREE_CODE (t))
4932 {
8c27b7d4 4933 /* For pointers, hash on pointee type plus some swizzling. */
325c3691 4934 case POINTER_TYPE:
2a22f99c 4935 return hash (TREE_TYPE (t)) ^ 0x3003003;
325c3691
RH
4936 /* Hash on number of elements and total size. */
4937 case ENUMERAL_TYPE:
4938 shift = 3;
4939 t2 = TYPE_VALUES (t);
4940 break;
4941 case RECORD_TYPE:
4942 shift = 0;
4943 t2 = TYPE_FIELDS (t);
4944 break;
4945 case QUAL_UNION_TYPE:
4946 shift = 1;
4947 t2 = TYPE_FIELDS (t);
4948 break;
4949 case UNION_TYPE:
4950 shift = 2;
4951 t2 = TYPE_FIELDS (t);
4952 break;
4953 default:
366de0ce 4954 gcc_unreachable ();
6de9cd9a 4955 }
a19e4d44
NF
4956 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4957 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4958 n_elements = list_length (t2);
6fc3c3c0
TT
4959 /* We might have a VLA here. */
4960 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4961 size = 0;
4962 else
4963 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
a19e4d44 4964 return ((size << 24) | (n_elements << shift));
6de9cd9a
DN
4965}
4966
2a22f99c
TS
4967bool
4968c_type_hasher::equal (tree t1, tree t2)
4969{
4970 return lang_hooks.types_compatible_p (t1, t2);
4971}
4972
4973static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4fe52ce9 4974
41472af8 4975/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 4976 or a type. Return -1 if we don't do anything special. */
41472af8 4977
4862826d 4978alias_set_type
35b1a6fa 4979c_common_get_alias_set (tree t)
41472af8 4980{
08bc2431 4981 tree u;
35b1a6fa 4982
cb9c2485
JM
4983 /* For VLAs, use the alias set of the element type rather than the
4984 default of alias set 0 for types compared structurally. */
4985 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4986 {
4987 if (TREE_CODE (t) == ARRAY_TYPE)
4988 return get_alias_set (TREE_TYPE (t));
4989 return -1;
4990 }
4991
08bc2431
MM
4992 /* Permit type-punning when accessing a union, provided the access
4993 is directly through the union. For example, this code does not
4994 permit taking the address of a union member and then storing
4995 through it. Even the type-punning allowed here is a GCC
4996 extension, albeit a common and useful one; the C standard says
4997 that such accesses have implementation-defined behavior. */
4998 for (u = t;
4999 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5000 u = TREE_OPERAND (u, 0))
5001 if (TREE_CODE (u) == COMPONENT_REF
5002 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5003 return 0;
ece32014 5004
74d86f4f 5005 /* That's all the expressions we handle specially. */
3f75a254 5006 if (!TYPE_P (t))
74d86f4f
RH
5007 return -1;
5008
95bd1dd7 5009 /* The C standard guarantees that any object may be accessed via an
74d86f4f
RH
5010 lvalue that has character type. */
5011 if (t == char_type_node
5012 || t == signed_char_type_node
5013 || t == unsigned_char_type_node)
3bdf5ad1 5014 return 0;
3932261a 5015
f824e5c3
RK
5016 /* The C standard specifically allows aliasing between signed and
5017 unsigned variants of the same type. We treat the signed
5018 variant as canonical. */
8df83eae 5019 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
8f215dce 5020 {
ceef8ce4 5021 tree t1 = c_common_signed_type (t);
f824e5c3 5022
8f215dce
JJ
5023 /* t1 == t can happen for boolean nodes which are always unsigned. */
5024 if (t1 != t)
5025 return get_alias_set (t1);
5026 }
ece32014 5027
6de9cd9a
DN
5028 /* Handle the case of multiple type nodes referring to "the same" type,
5029 which occurs with IMA. These share an alias set. FIXME: Currently only
5030 C90 is handled. (In C99 type compatibility is not transitive, which
5031 complicates things mightily. The alias set splay trees can theoretically
5032 represent this, but insertion is tricky when you consider all the
5033 different orders things might arrive in.) */
5034
5035 if (c_language != clk_c || flag_isoc99)
5036 return -1;
5037
9cf737f8 5038 /* Save time if there's only one input file. */
d974312d 5039 if (num_in_fnames == 1)
6de9cd9a
DN
5040 return -1;
5041
5042 /* Pointers need special handling if they point to any type that
5043 needs special handling (below). */
5044 if (TREE_CODE (t) == POINTER_TYPE)
5045 {
5046 tree t2;
5047 /* Find bottom type under any nested POINTERs. */
9f63daea 5048 for (t2 = TREE_TYPE (t);
762f7d9d
TT
5049 TREE_CODE (t2) == POINTER_TYPE;
5050 t2 = TREE_TYPE (t2))
5051 ;
9f63daea 5052 if (TREE_CODE (t2) != RECORD_TYPE
762f7d9d
TT
5053 && TREE_CODE (t2) != ENUMERAL_TYPE
5054 && TREE_CODE (t2) != QUAL_UNION_TYPE
5055 && TREE_CODE (t2) != UNION_TYPE)
5056 return -1;
6de9cd9a 5057 if (TYPE_SIZE (t2) == 0)
762f7d9d 5058 return -1;
6de9cd9a
DN
5059 }
5060 /* These are the only cases that need special handling. */
9f63daea 5061 if (TREE_CODE (t) != RECORD_TYPE
6de9cd9a
DN
5062 && TREE_CODE (t) != ENUMERAL_TYPE
5063 && TREE_CODE (t) != QUAL_UNION_TYPE
5064 && TREE_CODE (t) != UNION_TYPE
5065 && TREE_CODE (t) != POINTER_TYPE)
5066 return -1;
5067 /* Undefined? */
5068 if (TYPE_SIZE (t) == 0)
5069 return -1;
5070
9f63daea 5071 /* Look up t in hash table. Only one of the compatible types within each
6de9cd9a
DN
5072 alias set is recorded in the table. */
5073 if (!type_hash_table)
2a22f99c
TS
5074 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5075 tree *slot = type_hash_table->find_slot (t, INSERT);
6de9cd9a 5076 if (*slot != NULL)
6a3203c8
AP
5077 {
5078 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5079 return TYPE_ALIAS_SET ((tree)*slot);
5080 }
6de9cd9a
DN
5081 else
5082 /* Our caller will assign and record (in t) a new alias set; all we need
5083 to do is remember t in the hash table. */
5084 *slot = t;
5085
3bdf5ad1 5086 return -1;
41472af8 5087}
0213a355 5088\f
c2255bc4 5089/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
296674db 5090 the IS_SIZEOF parameter indicates which operator is being applied.
c2255bc4
AH
5091 The COMPLAIN flag controls whether we should diagnose possibly
5092 ill-formed constructs or not. LOC is the location of the SIZEOF or
296674db
JM
5093 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5094 a type in any context should be returned, rather than the normal
5095 alignment for that type. */
03a08664 5096
0213a355 5097tree
c2255bc4 5098c_sizeof_or_alignof_type (location_t loc,
296674db
JM
5099 tree type, bool is_sizeof, bool min_alignof,
5100 int complain)
0213a355 5101{
fa72b064
GDR
5102 const char *op_name;
5103 tree value = NULL;
5104 enum tree_code type_code = TREE_CODE (type);
35b1a6fa 5105
03a08664 5106 op_name = is_sizeof ? "sizeof" : "__alignof__";
35b1a6fa 5107
fa72b064 5108 if (type_code == FUNCTION_TYPE)
0213a355 5109 {
03a08664 5110 if (is_sizeof)
fa72b064 5111 {
44d90fe1
PC
5112 if (complain && warn_pointer_arith)
5113 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 5114 "invalid application of %<sizeof%> to a function type");
5ade1ed2
DG
5115 else if (!complain)
5116 return error_mark_node;
fa72b064
GDR
5117 value = size_one_node;
5118 }
5119 else
d19fa6b5
JM
5120 {
5121 if (complain)
5122 {
5123 if (c_dialect_cxx ())
c1771a20 5124 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
d19fa6b5
JM
5125 "%<alignof%> applied to a function type");
5126 else
c1771a20 5127 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
d19fa6b5
JM
5128 "%<_Alignof%> applied to a function type");
5129 }
5130 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5131 }
fa72b064
GDR
5132 }
5133 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5134 {
35b1a6fa 5135 if (type_code == VOID_TYPE
44d90fe1
PC
5136 && complain && warn_pointer_arith)
5137 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 5138 "invalid application of %qs to a void type", op_name);
5ade1ed2
DG
5139 else if (!complain)
5140 return error_mark_node;
fa72b064 5141 value = size_one_node;
0213a355 5142 }
73ac190a
PC
5143 else if (!COMPLETE_TYPE_P (type)
5144 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
0213a355 5145 {
ea793912 5146 if (complain)
73ac190a 5147 error_at (loc, "invalid application of %qs to incomplete type %qT",
c2255bc4 5148 op_name, type);
cb6addf4 5149 return error_mark_node;
0213a355 5150 }
73ac190a
PC
5151 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5152 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5153 {
5154 if (complain)
5155 error_at (loc, "invalid application of %qs to array type %qT of "
5156 "incomplete element type", op_name, type);
5157 return error_mark_node;
5158 }
0213a355 5159 else
fa72b064 5160 {
03a08664 5161 if (is_sizeof)
fa72b064 5162 /* Convert in case a char is more than one unit. */
db3927fb
AH
5163 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5164 size_int (TYPE_PRECISION (char_type_node)
5165 / BITS_PER_UNIT));
296674db 5166 else if (min_alignof)
2793eeab 5167 value = size_int (min_align_of_type (type));
fa72b064 5168 else
a4e9ffe5 5169 value = size_int (TYPE_ALIGN_UNIT (type));
fa72b064 5170 }
0213a355 5171
3ac8781c
RG
5172 /* VALUE will have the middle-end integer type sizetype.
5173 However, we should really return a value of type `size_t',
5174 which is just a typedef for an ordinary integer type. */
db3927fb 5175 value = fold_convert_loc (loc, size_type_node, value);
35b1a6fa 5176
fa72b064 5177 return value;
0213a355
JM
5178}
5179
5180/* Implement the __alignof keyword: Return the minimum required
837edd5f
GK
5181 alignment of EXPR, measured in bytes. For VAR_DECLs,
5182 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
c2255bc4
AH
5183 from an "aligned" __attribute__ specification). LOC is the
5184 location of the ALIGNOF operator. */
7f4edbcb 5185
0213a355 5186tree
c2255bc4 5187c_alignof_expr (location_t loc, tree expr)
0213a355
JM
5188{
5189 tree t;
5190
837edd5f 5191 if (VAR_OR_FUNCTION_DECL_P (expr))
a4e9ffe5 5192 t = size_int (DECL_ALIGN_UNIT (expr));
35b1a6fa 5193
0213a355
JM
5194 else if (TREE_CODE (expr) == COMPONENT_REF
5195 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5196 {
c2255bc4 5197 error_at (loc, "%<__alignof%> applied to a bit-field");
0213a355
JM
5198 t = size_one_node;
5199 }
5200 else if (TREE_CODE (expr) == COMPONENT_REF
173bf5be 5201 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
a4e9ffe5 5202 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
35b1a6fa 5203
0213a355
JM
5204 else if (TREE_CODE (expr) == INDIRECT_REF)
5205 {
5206 tree t = TREE_OPERAND (expr, 0);
5207 tree best = t;
5208 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
35b1a6fa 5209
1043771b 5210 while (CONVERT_EXPR_P (t)
173bf5be 5211 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
0213a355
JM
5212 {
5213 int thisalign;
5214
5215 t = TREE_OPERAND (t, 0);
5216 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5217 if (thisalign > bestalign)
5218 best = t, bestalign = thisalign;
5219 }
c2255bc4 5220 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
0213a355
JM
5221 }
5222 else
c2255bc4 5223 return c_alignof (loc, TREE_TYPE (expr));
0213a355 5224
db3927fb 5225 return fold_convert_loc (loc, size_type_node, t);
0213a355
JM
5226}
5227\f
df061a43
RS
5228/* Handle C and C++ default attributes. */
5229
5230enum built_in_attribute
5231{
5232#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5233#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
e384e6b5 5234#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
df061a43
RS
5235#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5236#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
df061a43
RS
5237#include "builtin-attrs.def"
5238#undef DEF_ATTR_NULL_TREE
5239#undef DEF_ATTR_INT
e384e6b5 5240#undef DEF_ATTR_STRING
df061a43
RS
5241#undef DEF_ATTR_IDENT
5242#undef DEF_ATTR_TREE_LIST
df061a43
RS
5243 ATTR_LAST
5244};
5245
5246static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5247
35b1a6fa 5248static void c_init_attributes (void);
df061a43 5249
a0274e3e 5250enum c_builtin_type
7f4edbcb 5251{
10841285
MM
5252#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5253#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5254#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5255#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5256#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5257#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
10a0d495 5258#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
f6a7cffc
TS
5259#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5260 ARG6) NAME,
5261#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5262 ARG6, ARG7) NAME,
5263#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5264 ARG6, ARG7, ARG8) NAME,
10841285
MM
5265#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5266#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5267#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
08291658 5268#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
10a0d495 5269#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
f6a7cffc 5270#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
56a9f6bc
TS
5271 NAME,
5272#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5273 ARG6, ARG7) NAME,
5274#define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5275 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
10841285
MM
5276#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5277#include "builtin-types.def"
5278#undef DEF_PRIMITIVE_TYPE
5279#undef DEF_FUNCTION_TYPE_0
5280#undef DEF_FUNCTION_TYPE_1
5281#undef DEF_FUNCTION_TYPE_2
5282#undef DEF_FUNCTION_TYPE_3
5283#undef DEF_FUNCTION_TYPE_4
10a0d495
JJ
5284#undef DEF_FUNCTION_TYPE_5
5285#undef DEF_FUNCTION_TYPE_6
a0274e3e 5286#undef DEF_FUNCTION_TYPE_7
acf0174b 5287#undef DEF_FUNCTION_TYPE_8
10841285
MM
5288#undef DEF_FUNCTION_TYPE_VAR_0
5289#undef DEF_FUNCTION_TYPE_VAR_1
5290#undef DEF_FUNCTION_TYPE_VAR_2
08291658 5291#undef DEF_FUNCTION_TYPE_VAR_3
10a0d495
JJ
5292#undef DEF_FUNCTION_TYPE_VAR_4
5293#undef DEF_FUNCTION_TYPE_VAR_5
56a9f6bc
TS
5294#undef DEF_FUNCTION_TYPE_VAR_7
5295#undef DEF_FUNCTION_TYPE_VAR_11
10841285 5296#undef DEF_POINTER_TYPE
a0274e3e
JJ
5297 BT_LAST
5298};
5299
5300typedef enum c_builtin_type builtin_type;
10841285 5301
a0274e3e
JJ
5302/* A temporary array for c_common_nodes_and_builtins. Used in
5303 communication with def_fn_type. */
5304static tree builtin_types[(int) BT_LAST + 1];
10841285 5305
a0274e3e
JJ
5306/* A helper function for c_common_nodes_and_builtins. Build function type
5307 for DEF with return type RET and N arguments. If VAR is true, then the
5308 function should be variadic after those N arguments.
5309
5310 Takes special care not to ICE if any of the types involved are
5311 error_mark_node, which indicates that said type is not in fact available
5312 (see builtin_type_for_size). In which case the function type as a whole
5313 should be error_mark_node. */
5314
5315static void
5316def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5317{
8242dd04
NF
5318 tree t;
5319 tree *args = XALLOCAVEC (tree, n);
a0274e3e
JJ
5320 va_list list;
5321 int i;
5322
5323 va_start (list, n);
5324 for (i = 0; i < n; ++i)
5325 {
d75d71e0 5326 builtin_type a = (builtin_type) va_arg (list, int);
a0274e3e
JJ
5327 t = builtin_types[a];
5328 if (t == error_mark_node)
5329 goto egress;
8242dd04 5330 args[i] = t;
a0274e3e 5331 }
a0274e3e 5332
a0274e3e
JJ
5333 t = builtin_types[ret];
5334 if (t == error_mark_node)
5335 goto egress;
8242dd04
NF
5336 if (var)
5337 t = build_varargs_function_type_array (t, n, args);
5338 else
5339 t = build_function_type_array (t, n, args);
a0274e3e
JJ
5340
5341 egress:
5342 builtin_types[def] = t;
0edf1bb2 5343 va_end (list);
a0274e3e
JJ
5344}
5345
c6d86fce
ILT
5346/* Build builtin functions common to both C and C++ language
5347 frontends. */
5348
5349static void
5350c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5351{
5352#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5353 builtin_types[ENUM] = VALUE;
5354#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5355 def_fn_type (ENUM, RETURN, 0, 0);
5356#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5357 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5358#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5359 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5360#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5361 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5362#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5363 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5364#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5365 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5366#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5367 ARG6) \
5368 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5369#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5370 ARG6, ARG7) \
5371 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
acf0174b
JJ
5372#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5373 ARG6, ARG7, ARG8) \
5374 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5375 ARG7, ARG8);
c6d86fce
ILT
5376#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5377 def_fn_type (ENUM, RETURN, 1, 0);
5378#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5379 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5380#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5381 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5382#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5383 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5384#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5385 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5386#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5387 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
56a9f6bc
TS
5388#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5389 ARG6, ARG7) \
5390 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5391#define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5392 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5393 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5394 ARG7, ARG8, ARG9, ARG10, ARG11);
c6d86fce
ILT
5395#define DEF_POINTER_TYPE(ENUM, TYPE) \
5396 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5397
5398#include "builtin-types.def"
5399
5400#undef DEF_PRIMITIVE_TYPE
f6a7cffc 5401#undef DEF_FUNCTION_TYPE_0
c6d86fce
ILT
5402#undef DEF_FUNCTION_TYPE_1
5403#undef DEF_FUNCTION_TYPE_2
5404#undef DEF_FUNCTION_TYPE_3
5405#undef DEF_FUNCTION_TYPE_4
5406#undef DEF_FUNCTION_TYPE_5
5407#undef DEF_FUNCTION_TYPE_6
f6a7cffc
TS
5408#undef DEF_FUNCTION_TYPE_7
5409#undef DEF_FUNCTION_TYPE_8
c6d86fce
ILT
5410#undef DEF_FUNCTION_TYPE_VAR_0
5411#undef DEF_FUNCTION_TYPE_VAR_1
5412#undef DEF_FUNCTION_TYPE_VAR_2
5413#undef DEF_FUNCTION_TYPE_VAR_3
5414#undef DEF_FUNCTION_TYPE_VAR_4
5415#undef DEF_FUNCTION_TYPE_VAR_5
56a9f6bc
TS
5416#undef DEF_FUNCTION_TYPE_VAR_7
5417#undef DEF_FUNCTION_TYPE_VAR_11
c6d86fce
ILT
5418#undef DEF_POINTER_TYPE
5419 builtin_types[(int) BT_LAST] = NULL_TREE;
5420
5421 c_init_attributes ();
5422
5423#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5424 NONANSI_P, ATTRS, IMPLICIT, COND) \
5425 if (NAME && COND) \
5426 def_builtin_1 (ENUM, NAME, CLASS, \
5427 builtin_types[(int) TYPE], \
5428 builtin_types[(int) LIBTYPE], \
5429 BOTH_P, FALLBACK_P, NONANSI_P, \
5430 built_in_attributes[(int) ATTRS], IMPLICIT);
5431#include "builtins.def"
5432#undef DEF_BUILTIN
5433
8de7ef2a
UB
5434 targetm.init_builtins ();
5435
384c400a 5436 build_common_builtin_nodes ();
939b37da 5437
b72271b9 5438 if (flag_cilkplus)
939b37da 5439 cilk_init_builtins ();
c6d86fce
ILT
5440}
5441
c1b61fca
JM
5442/* Like get_identifier, but avoid warnings about null arguments when
5443 the argument may be NULL for targets where GCC lacks stdint.h type
5444 information. */
5445
5446static inline tree
5447c_get_ident (const char *id)
5448{
5449 return get_identifier (id);
5450}
5451
a0274e3e
JJ
5452/* Build tree nodes and builtin functions common to both C and C++ language
5453 frontends. */
5454
5455void
5456c_common_nodes_and_builtins (void)
5457{
b6baa67d
KVH
5458 int char16_type_size;
5459 int char32_type_size;
eaa7c03f
JM
5460 int wchar_type_size;
5461 tree array_domain_type;
9f720c3e 5462 tree va_list_ref_type_node;
daf68dd7 5463 tree va_list_arg_type_node;
78a7c317 5464 int i;
d3707adb 5465
1a072294 5466 build_common_tree_nodes (flag_signed_char, flag_short_double);
fce5dddd 5467
eaa7c03f 5468 /* Define `int' and `char' first so that dbx will output them first. */
6496a589 5469 record_builtin_type (RID_INT, NULL, integer_type_node);
eaa7c03f
JM
5470 record_builtin_type (RID_CHAR, "char", char_type_node);
5471
5472 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5473 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5474 but not C. Are the conditionals here needed? */
37fa72e9 5475 if (c_dialect_cxx ())
6496a589 5476 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
eaa7c03f
JM
5477 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5478 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5479 record_builtin_type (RID_MAX, "long unsigned int",
5480 long_unsigned_type_node);
78a7c317
DD
5481
5482 for (i = 0; i < NUM_INT_N_ENTS; i ++)
a6766312 5483 {
78a7c317
DD
5484 char name[25];
5485
5486 sprintf (name, "__int%d", int_n_data[i].bitsize);
17958621 5487 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
78a7c317
DD
5488 int_n_trees[i].signed_type);
5489 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
17958621 5490 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
a6766312 5491 }
78a7c317 5492
37fa72e9 5493 if (c_dialect_cxx ())
eaa7c03f
JM
5494 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5495 record_builtin_type (RID_MAX, "long long int",
5496 long_long_integer_type_node);
5497 record_builtin_type (RID_MAX, "long long unsigned int",
5498 long_long_unsigned_type_node);
37fa72e9 5499 if (c_dialect_cxx ())
eaa7c03f
JM
5500 record_builtin_type (RID_MAX, "long long unsigned",
5501 long_long_unsigned_type_node);
5502 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5503 record_builtin_type (RID_MAX, "short unsigned int",
5504 short_unsigned_type_node);
37fa72e9 5505 if (c_dialect_cxx ())
eaa7c03f
JM
5506 record_builtin_type (RID_MAX, "unsigned short",
5507 short_unsigned_type_node);
5508
5509 /* Define both `signed char' and `unsigned char'. */
5510 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5511 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5512
b0c48229
NB
5513 /* These are types that c_common_type_for_size and
5514 c_common_type_for_mode use. */
c2255bc4
AH
5515 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5516 TYPE_DECL, NULL_TREE,
ae2bcd98 5517 intQI_type_node));
c2255bc4
AH
5518 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5519 TYPE_DECL, NULL_TREE,
ae2bcd98 5520 intHI_type_node));
c2255bc4
AH
5521 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5522 TYPE_DECL, NULL_TREE,
ae2bcd98 5523 intSI_type_node));
c2255bc4
AH
5524 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5525 TYPE_DECL, NULL_TREE,
ae2bcd98 5526 intDI_type_node));
eaa7c03f 5527#if HOST_BITS_PER_WIDE_INT >= 64
78a7c317
DD
5528 /* Note that this is different than the __int128 type that's part of
5529 the generic __intN support. */
1e1b8649 5530 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5531 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5532 TYPE_DECL,
1e1b8649
AP
5533 get_identifier ("__int128_t"),
5534 intTI_type_node));
eaa7c03f 5535#endif
c2255bc4
AH
5536 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5537 TYPE_DECL, NULL_TREE,
ae2bcd98 5538 unsigned_intQI_type_node));
c2255bc4
AH
5539 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5540 TYPE_DECL, NULL_TREE,
ae2bcd98 5541 unsigned_intHI_type_node));
c2255bc4
AH
5542 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5543 TYPE_DECL, NULL_TREE,
ae2bcd98 5544 unsigned_intSI_type_node));
c2255bc4
AH
5545 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5546 TYPE_DECL, NULL_TREE,
ae2bcd98 5547 unsigned_intDI_type_node));
eaa7c03f 5548#if HOST_BITS_PER_WIDE_INT >= 64
1e1b8649 5549 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5550 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5551 TYPE_DECL,
1e1b8649
AP
5552 get_identifier ("__uint128_t"),
5553 unsigned_intTI_type_node));
eaa7c03f
JM
5554#endif
5555
5556 /* Create the widest literal types. */
5557 widest_integer_literal_type_node
5558 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5559 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5560 TYPE_DECL, NULL_TREE,
ae2bcd98 5561 widest_integer_literal_type_node));
eaa7c03f
JM
5562
5563 widest_unsigned_literal_type_node
5564 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5565 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5566 TYPE_DECL, NULL_TREE,
ae2bcd98 5567 widest_unsigned_literal_type_node));
eaa7c03f 5568
c9f8536c 5569 signed_size_type_node = c_common_signed_type (size_type_node);
eaa7c03f 5570
d1c38823
ZD
5571 pid_type_node =
5572 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5573
6496a589
KG
5574 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5575 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
eaa7c03f
JM
5576 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5577
9a8ce21f
JG
5578 /* Only supported decimal floating point extension if the target
5579 actually supports underlying modes. */
b8698a0f 5580 if (targetm.scalar_mode_supported_p (SDmode)
9a8ce21f
JG
5581 && targetm.scalar_mode_supported_p (DDmode)
5582 && targetm.scalar_mode_supported_p (TDmode))
5583 {
5584 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5585 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5586 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5587 }
5588
ab22c1fa
CF
5589 if (targetm.fixed_point_supported_p ())
5590 {
5591 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5592 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5593 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5594 record_builtin_type (RID_MAX, "long long _Fract",
5595 long_long_fract_type_node);
5596 record_builtin_type (RID_MAX, "unsigned short _Fract",
5597 unsigned_short_fract_type_node);
5598 record_builtin_type (RID_MAX, "unsigned _Fract",
5599 unsigned_fract_type_node);
5600 record_builtin_type (RID_MAX, "unsigned long _Fract",
5601 unsigned_long_fract_type_node);
5602 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5603 unsigned_long_long_fract_type_node);
5604 record_builtin_type (RID_MAX, "_Sat short _Fract",
5605 sat_short_fract_type_node);
5606 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5607 record_builtin_type (RID_MAX, "_Sat long _Fract",
5608 sat_long_fract_type_node);
5609 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5610 sat_long_long_fract_type_node);
5611 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5612 sat_unsigned_short_fract_type_node);
5613 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5614 sat_unsigned_fract_type_node);
5615 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5616 sat_unsigned_long_fract_type_node);
5617 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5618 sat_unsigned_long_long_fract_type_node);
5619 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5620 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5621 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5622 record_builtin_type (RID_MAX, "long long _Accum",
5623 long_long_accum_type_node);
5624 record_builtin_type (RID_MAX, "unsigned short _Accum",
5625 unsigned_short_accum_type_node);
5626 record_builtin_type (RID_MAX, "unsigned _Accum",
5627 unsigned_accum_type_node);
5628 record_builtin_type (RID_MAX, "unsigned long _Accum",
5629 unsigned_long_accum_type_node);
5630 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5631 unsigned_long_long_accum_type_node);
5632 record_builtin_type (RID_MAX, "_Sat short _Accum",
5633 sat_short_accum_type_node);
5634 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5635 record_builtin_type (RID_MAX, "_Sat long _Accum",
5636 sat_long_accum_type_node);
5637 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5638 sat_long_long_accum_type_node);
5639 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5640 sat_unsigned_short_accum_type_node);
5641 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5642 sat_unsigned_accum_type_node);
5643 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5644 sat_unsigned_long_accum_type_node);
5645 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5646 sat_unsigned_long_long_accum_type_node);
5647
5648 }
5649
c2255bc4
AH
5650 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5651 TYPE_DECL,
ae2bcd98
RS
5652 get_identifier ("complex int"),
5653 complex_integer_type_node));
c2255bc4
AH
5654 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5655 TYPE_DECL,
ae2bcd98
RS
5656 get_identifier ("complex float"),
5657 complex_float_type_node));
c2255bc4
AH
5658 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5659 TYPE_DECL,
ae2bcd98
RS
5660 get_identifier ("complex double"),
5661 complex_double_type_node));
5662 lang_hooks.decls.pushdecl
c2255bc4
AH
5663 (build_decl (UNKNOWN_LOCATION,
5664 TYPE_DECL, get_identifier ("complex long double"),
43577e6b 5665 complex_long_double_type_node));
eaa7c03f 5666
498c0f27
JJ
5667 if (c_dialect_cxx ())
5668 /* For C++, make fileptr_type_node a distinct void * type until
5669 FILE type is defined. */
8dd16ecc 5670 fileptr_type_node = build_variant_type_copy (ptr_type_node);
498c0f27 5671
6496a589 5672 record_builtin_type (RID_VOID, NULL, void_type_node);
eaa7c03f 5673
06d40de8
DG
5674 /* Set the TYPE_NAME for any variants that were built before
5675 record_builtin_type gave names to the built-in types. */
5676 {
5677 tree void_name = TYPE_NAME (void_type_node);
5678 TYPE_NAME (void_type_node) = NULL_TREE;
5679 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5680 = void_name;
5681 TYPE_NAME (void_type_node) = void_name;
5682 }
5683
eaa7c03f
JM
5684 void_list_node = build_void_list_node ();
5685
5686 /* Make a type to be the domain of a few array types
5687 whose domains don't really matter.
5688 200 is small enough that it always fits in size_t
5689 and large enough that it can hold most function names for the
5690 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5691 array_domain_type = build_index_type (size_int (200));
5692
5693 /* Make a type for arrays of characters.
5694 With luck nothing will ever really depend on the length of this
5695 array type. */
5696 char_array_type_node
5697 = build_array_type (char_type_node, array_domain_type);
5698
10841285
MM
5699 string_type_node = build_pointer_type (char_type_node);
5700 const_string_type_node
5701 = build_pointer_type (build_qualified_type
5702 (char_type_node, TYPE_QUAL_CONST));
5703
eaa7c03f 5704 /* This is special for C++ so functions can be overloaded. */
a11eba95 5705 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
eaa7c03f
JM
5706 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5707 wchar_type_size = TYPE_PRECISION (wchar_type_node);
207bf79d 5708 underlying_wchar_type_node = wchar_type_node;
37fa72e9 5709 if (c_dialect_cxx ())
eaa7c03f 5710 {
8df83eae 5711 if (TYPE_UNSIGNED (wchar_type_node))
eaa7c03f
JM
5712 wchar_type_node = make_unsigned_type (wchar_type_size);
5713 else
5714 wchar_type_node = make_signed_type (wchar_type_size);
5715 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5716 }
eaa7c03f
JM
5717
5718 /* This is for wide string constants. */
5719 wchar_array_type_node
5720 = build_array_type (wchar_type_node, array_domain_type);
5721
b6baa67d
KVH
5722 /* Define 'char16_t'. */
5723 char16_type_node = get_identifier (CHAR16_TYPE);
5724 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5725 char16_type_size = TYPE_PRECISION (char16_type_node);
5726 if (c_dialect_cxx ())
5727 {
5728 char16_type_node = make_unsigned_type (char16_type_size);
5729
604b2bfc 5730 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5731 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5732 }
5733
5734 /* This is for UTF-16 string constants. */
5735 char16_array_type_node
5736 = build_array_type (char16_type_node, array_domain_type);
5737
5738 /* Define 'char32_t'. */
5739 char32_type_node = get_identifier (CHAR32_TYPE);
5740 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5741 char32_type_size = TYPE_PRECISION (char32_type_node);
5742 if (c_dialect_cxx ())
5743 {
5744 char32_type_node = make_unsigned_type (char32_type_size);
5745
604b2bfc 5746 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5747 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5748 }
5749
5750 /* This is for UTF-32 string constants. */
5751 char32_array_type_node
5752 = build_array_type (char32_type_node, array_domain_type);
5753
5fd8e536
JM
5754 wint_type_node =
5755 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5756
5757 intmax_type_node =
5758 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5759 uintmax_type_node =
5760 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5761
207bf79d
JM
5762 if (SIG_ATOMIC_TYPE)
5763 sig_atomic_type_node =
c1b61fca 5764 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
207bf79d
JM
5765 if (INT8_TYPE)
5766 int8_type_node =
c1b61fca 5767 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
207bf79d
JM
5768 if (INT16_TYPE)
5769 int16_type_node =
c1b61fca 5770 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
207bf79d
JM
5771 if (INT32_TYPE)
5772 int32_type_node =
c1b61fca 5773 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
207bf79d
JM
5774 if (INT64_TYPE)
5775 int64_type_node =
c1b61fca 5776 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
207bf79d
JM
5777 if (UINT8_TYPE)
5778 uint8_type_node =
c1b61fca 5779 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
207bf79d 5780 if (UINT16_TYPE)
cca615af 5781 c_uint16_type_node = uint16_type_node =
c1b61fca 5782 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
207bf79d 5783 if (UINT32_TYPE)
cca615af 5784 c_uint32_type_node = uint32_type_node =
c1b61fca 5785 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
207bf79d 5786 if (UINT64_TYPE)
cca615af 5787 c_uint64_type_node = uint64_type_node =
c1b61fca 5788 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
207bf79d
JM
5789 if (INT_LEAST8_TYPE)
5790 int_least8_type_node =
c1b61fca 5791 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
207bf79d
JM
5792 if (INT_LEAST16_TYPE)
5793 int_least16_type_node =
c1b61fca 5794 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
207bf79d
JM
5795 if (INT_LEAST32_TYPE)
5796 int_least32_type_node =
c1b61fca 5797 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
207bf79d
JM
5798 if (INT_LEAST64_TYPE)
5799 int_least64_type_node =
c1b61fca 5800 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
207bf79d
JM
5801 if (UINT_LEAST8_TYPE)
5802 uint_least8_type_node =
c1b61fca 5803 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
207bf79d
JM
5804 if (UINT_LEAST16_TYPE)
5805 uint_least16_type_node =
c1b61fca 5806 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
207bf79d
JM
5807 if (UINT_LEAST32_TYPE)
5808 uint_least32_type_node =
c1b61fca 5809 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
207bf79d
JM
5810 if (UINT_LEAST64_TYPE)
5811 uint_least64_type_node =
c1b61fca 5812 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
207bf79d
JM
5813 if (INT_FAST8_TYPE)
5814 int_fast8_type_node =
c1b61fca 5815 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
207bf79d
JM
5816 if (INT_FAST16_TYPE)
5817 int_fast16_type_node =
c1b61fca 5818 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
207bf79d
JM
5819 if (INT_FAST32_TYPE)
5820 int_fast32_type_node =
c1b61fca 5821 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
207bf79d
JM
5822 if (INT_FAST64_TYPE)
5823 int_fast64_type_node =
c1b61fca 5824 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
207bf79d
JM
5825 if (UINT_FAST8_TYPE)
5826 uint_fast8_type_node =
c1b61fca 5827 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
207bf79d
JM
5828 if (UINT_FAST16_TYPE)
5829 uint_fast16_type_node =
c1b61fca 5830 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
207bf79d
JM
5831 if (UINT_FAST32_TYPE)
5832 uint_fast32_type_node =
c1b61fca 5833 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
207bf79d
JM
5834 if (UINT_FAST64_TYPE)
5835 uint_fast64_type_node =
c1b61fca 5836 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
207bf79d
JM
5837 if (INTPTR_TYPE)
5838 intptr_type_node =
c1b61fca 5839 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
207bf79d
JM
5840 if (UINTPTR_TYPE)
5841 uintptr_type_node =
c1b61fca 5842 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
207bf79d 5843
8242dd04
NF
5844 default_function_type
5845 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5fd8e536
JM
5846 ptrdiff_type_node
5847 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
12753674 5848 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5fd8e536 5849
ae2bcd98 5850 lang_hooks.decls.pushdecl
c2255bc4
AH
5851 (build_decl (UNKNOWN_LOCATION,
5852 TYPE_DECL, get_identifier ("__builtin_va_list"),
43577e6b 5853 va_list_type_node));
38f8b050 5854 if (targetm.enum_va_list_p)
d4048208
KT
5855 {
5856 int l;
5857 const char *pname;
5858 tree ptype;
35cbb299 5859
38f8b050 5860 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
d4048208
KT
5861 {
5862 lang_hooks.decls.pushdecl
5863 (build_decl (UNKNOWN_LOCATION,
5864 TYPE_DECL, get_identifier (pname),
5865 ptype));
5866
5867 }
5868 }
daf68dd7 5869
daf68dd7 5870 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
5871 {
5872 va_list_arg_type_node = va_list_ref_type_node =
5873 build_pointer_type (TREE_TYPE (va_list_type_node));
5874 }
daf68dd7 5875 else
9f720c3e
GK
5876 {
5877 va_list_arg_type_node = va_list_type_node;
5878 va_list_ref_type_node = build_reference_type (va_list_type_node);
5879 }
35b1a6fa 5880
c6d86fce
ILT
5881 if (!flag_preprocess_only)
5882 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4677862a 5883
5b47282c 5884 main_identifier_node = get_identifier ("main");
b2f97e4a
MM
5885
5886 /* Create the built-in __null node. It is important that this is
5887 not shared. */
807e902e 5888 null_node = make_int_cst (1, 1);
b2f97e4a 5889 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
a0274e3e
JJ
5890
5891 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5892 memset (builtin_types, 0, sizeof (builtin_types));
7f4edbcb 5893}
d3707adb 5894
3b2db49f
MM
5895/* The number of named compound-literals generated thus far. */
5896static GTY(()) int compound_literal_number;
5897
5898/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5899
5900void
5901set_compound_literal_name (tree decl)
5902{
5903 char *name;
5904 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5905 compound_literal_number);
5906 compound_literal_number++;
5907 DECL_NAME (decl) = get_identifier (name);
5908}
5909
d3707adb 5910tree
c2255bc4 5911build_va_arg (location_t loc, tree expr, tree type)
d3707adb 5912{
ecd0e562
TV
5913 /* In gimplify_va_arg_expr we take the address of the ap argument, mark it
5914 addressable now. */
5915 mark_addressable (expr);
5916
c2255bc4
AH
5917 expr = build1 (VA_ARG_EXPR, type, expr);
5918 SET_EXPR_LOCATION (expr, loc);
5919 return expr;
d3707adb 5920}
fc2aaf30
JM
5921
5922
7d14c755
JM
5923/* Linked list of disabled built-in functions. */
5924
5925typedef struct disabled_builtin
5926{
5927 const char *name;
5928 struct disabled_builtin *next;
5929} disabled_builtin;
5930static disabled_builtin *disabled_builtins = NULL;
5931
35b1a6fa 5932static bool builtin_function_disabled_p (const char *);
7d14c755
JM
5933
5934/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5935 begins with "__builtin_", give an error. */
5936
5937void
35b1a6fa 5938disable_builtin_function (const char *name)
7d14c755
JM
5939{
5940 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
bda67431 5941 error ("cannot disable built-in function %qs", name);
7d14c755
JM
5942 else
5943 {
1ad463f4
BI
5944 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5945 new_disabled_builtin->name = name;
5946 new_disabled_builtin->next = disabled_builtins;
5947 disabled_builtins = new_disabled_builtin;
7d14c755
JM
5948 }
5949}
5950
5951
5952/* Return true if the built-in function NAME has been disabled, false
5953 otherwise. */
5954
5955static bool
35b1a6fa 5956builtin_function_disabled_p (const char *name)
7d14c755
JM
5957{
5958 disabled_builtin *p;
5959 for (p = disabled_builtins; p != NULL; p = p->next)
5960 {
5961 if (strcmp (name, p->name) == 0)
5962 return true;
5963 }
5964 return false;
5965}
5966
5967
9bc15050
RG
5968/* Worker for DEF_BUILTIN.
5969 Possibly define a builtin function with one or two names.
5970 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5971 nonansi_p and flag_no_nonansi_builtin. */
fc2aaf30 5972
9bc15050
RG
5973static void
5974def_builtin_1 (enum built_in_function fncode,
5975 const char *name,
5976 enum built_in_class fnclass,
5977 tree fntype, tree libtype,
5978 bool both_p, bool fallback_p, bool nonansi_p,
5979 tree fnattrs, bool implicit_p)
fc2aaf30 5980{
9bc15050
RG
5981 tree decl;
5982 const char *libname;
5983
a0274e3e
JJ
5984 if (fntype == error_mark_node)
5985 return;
5986
9bc15050
RG
5987 gcc_assert ((!both_p && !fallback_p)
5988 || !strncmp (name, "__builtin_",
5989 strlen ("__builtin_")));
5990
5991 libname = name + strlen ("__builtin_");
c79efc4d
RÁE
5992 decl = add_builtin_function (name, fntype, fncode, fnclass,
5993 (fallback_p ? libname : NULL),
5994 fnattrs);
e79983f4
MM
5995
5996 set_builtin_decl (fncode, decl, implicit_p);
5997
9bc15050
RG
5998 if (both_p
5999 && !flag_no_builtin && !builtin_function_disabled_p (libname)
7d14c755 6000 && !(nonansi_p && flag_no_nonansi_builtin))
c79efc4d
RÁE
6001 add_builtin_function (libname, libtype, fncode, fnclass,
6002 NULL, fnattrs);
fc2aaf30 6003}
c530479e 6004\f
d72040f5
RH
6005/* Nonzero if the type T promotes to int. This is (nearly) the
6006 integral promotions defined in ISO C99 6.3.1.1/2. */
6007
6008bool
58f9752a 6009c_promoting_integer_type_p (const_tree t)
d72040f5
RH
6010{
6011 switch (TREE_CODE (t))
6012 {
6013 case INTEGER_TYPE:
6014 return (TYPE_MAIN_VARIANT (t) == char_type_node
6015 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6016 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6017 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
c6c04fca
RL
6018 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6019 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d72040f5
RH
6020
6021 case ENUMERAL_TYPE:
6022 /* ??? Technically all enumerations not larger than an int
6023 promote to an int. But this is used along code paths
6024 that only want to notice a size change. */
6025 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6026
6027 case BOOLEAN_TYPE:
6028 return 1;
6029
6030 default:
6031 return 0;
6032 }
6033}
6034
c530479e
RH
6035/* Return 1 if PARMS specifies a fixed number of parameters
6036 and none of their types is affected by default promotions. */
6037
6038int
58f9752a 6039self_promoting_args_p (const_tree parms)
c530479e 6040{
58f9752a 6041 const_tree t;
c530479e
RH
6042 for (t = parms; t; t = TREE_CHAIN (t))
6043 {
b3694847 6044 tree type = TREE_VALUE (t);
7e8176d7 6045
694fea20
VR
6046 if (type == error_mark_node)
6047 continue;
6048
c530479e
RH
6049 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6050 return 0;
6051
6052 if (type == 0)
6053 return 0;
6054
6055 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6056 return 0;
6057
d72040f5 6058 if (c_promoting_integer_type_p (type))
c530479e
RH
6059 return 0;
6060 }
6061 return 1;
6062}
5eda3d66 6063
12ea3302
GDR
6064/* Recursively remove any '*' or '&' operator from TYPE. */
6065tree
6066strip_pointer_operator (tree t)
6067{
6068 while (POINTER_TYPE_P (t))
6069 t = TREE_TYPE (t);
6070 return t;
6071}
6072
ba992967
SP
6073/* Recursively remove pointer or array type from TYPE. */
6074tree
6075strip_pointer_or_array_types (tree t)
6076{
6077 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6078 t = TREE_TYPE (t);
6079 return t;
6080}
6081
8f17b5c5
MM
6082/* Used to compare case labels. K1 and K2 are actually tree nodes
6083 representing case labels, or NULL_TREE for a `default' label.
6084 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6085 K2, and 0 if K1 and K2 are equal. */
6086
6087int
35b1a6fa 6088case_compare (splay_tree_key k1, splay_tree_key k2)
8f17b5c5
MM
6089{
6090 /* Consider a NULL key (such as arises with a `default' label) to be
6091 smaller than anything else. */
6092 if (!k1)
6093 return k2 ? -1 : 0;
6094 else if (!k2)
6095 return k1 ? 1 : 0;
6096
6097 return tree_int_cst_compare ((tree) k1, (tree) k2);
6098}
6099
c2255bc4
AH
6100/* Process a case label, located at LOC, for the range LOW_VALUE
6101 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6102 then this case label is actually a `default' label. If only
6103 HIGH_VALUE is NULL_TREE, then case label was declared using the
6104 usual C/C++ syntax, rather than the GNU case range extension.
6105 CASES is a tree containing all the case ranges processed so far;
6106 COND is the condition for the switch-statement itself. Returns the
6107 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6108 is created. */
8f17b5c5
MM
6109
6110tree
c2255bc4 6111c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
a6c0a76c 6112 tree low_value, tree high_value)
8f17b5c5
MM
6113{
6114 tree type;
6115 tree label;
6116 tree case_label;
6117 splay_tree_node node;
6118
6119 /* Create the LABEL_DECL itself. */
c2255bc4 6120 label = create_artificial_label (loc);
8f17b5c5
MM
6121
6122 /* If there was an error processing the switch condition, bail now
6123 before we get more confused. */
6124 if (!cond || cond == error_mark_node)
6de9cd9a 6125 goto error_out;
8f17b5c5 6126
35b1a6fa
AJ
6127 if ((low_value && TREE_TYPE (low_value)
6128 && POINTER_TYPE_P (TREE_TYPE (low_value)))
8f17b5c5
MM
6129 || (high_value && TREE_TYPE (high_value)
6130 && POINTER_TYPE_P (TREE_TYPE (high_value))))
522ddfa2 6131 {
c2255bc4 6132 error_at (loc, "pointers are not permitted as case values");
522ddfa2
JM
6133 goto error_out;
6134 }
8f17b5c5
MM
6135
6136 /* Case ranges are a GNU extension. */
fcf73884 6137 if (high_value)
c1771a20 6138 pedwarn (loc, OPT_Wpedantic,
fcf73884 6139 "range expressions in switch statements are non-standard");
8f17b5c5
MM
6140
6141 type = TREE_TYPE (cond);
6142 if (low_value)
6143 {
62e4eb35 6144 low_value = check_case_value (loc, low_value);
68fca595 6145 low_value = convert_and_check (loc, type, low_value);
c0e22534
NS
6146 if (low_value == error_mark_node)
6147 goto error_out;
8f17b5c5
MM
6148 }
6149 if (high_value)
6150 {
62e4eb35 6151 high_value = check_case_value (loc, high_value);
68fca595 6152 high_value = convert_and_check (loc, type, high_value);
c0e22534
NS
6153 if (high_value == error_mark_node)
6154 goto error_out;
8f17b5c5
MM
6155 }
6156
c0e22534
NS
6157 if (low_value && high_value)
6158 {
6159 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
c22cacf3
MS
6160 really a case range, even though it was written that way.
6161 Remove the HIGH_VALUE to simplify later processing. */
c0e22534
NS
6162 if (tree_int_cst_equal (low_value, high_value))
6163 high_value = NULL_TREE;
6164 else if (!tree_int_cst_lt (low_value, high_value))
c2255bc4 6165 warning_at (loc, 0, "empty range specified");
c0e22534 6166 }
8f17b5c5 6167
a6c0a76c
SB
6168 /* See if the case is in range of the type of the original testing
6169 expression. If both low_value and high_value are out of range,
6170 don't insert the case label and return NULL_TREE. */
6171 if (low_value
9d548dfb 6172 && !check_case_bounds (loc, type, orig_type,
3f75a254 6173 &low_value, high_value ? &high_value : NULL))
a6c0a76c
SB
6174 return NULL_TREE;
6175
8f17b5c5
MM
6176 /* Look up the LOW_VALUE in the table of case labels we already
6177 have. */
6178 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6179 /* If there was not an exact match, check for overlapping ranges.
6180 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6181 that's a `default' label and the only overlap is an exact match. */
6182 if (!node && (low_value || high_value))
6183 {
6184 splay_tree_node low_bound;
6185 splay_tree_node high_bound;
6186
6187 /* Even though there wasn't an exact match, there might be an
6188 overlap between this case range and another case range.
6189 Since we've (inductively) not allowed any overlapping case
6190 ranges, we simply need to find the greatest low case label
6191 that is smaller that LOW_VALUE, and the smallest low case
6192 label that is greater than LOW_VALUE. If there is an overlap
6193 it will occur in one of these two ranges. */
6194 low_bound = splay_tree_predecessor (cases,
6195 (splay_tree_key) low_value);
6196 high_bound = splay_tree_successor (cases,
6197 (splay_tree_key) low_value);
6198
6199 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6200 the LOW_VALUE, so there is no need to check unless the
6201 LOW_BOUND is in fact itself a case range. */
6202 if (low_bound
6203 && CASE_HIGH ((tree) low_bound->value)
6204 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6205 low_value) >= 0)
6206 node = low_bound;
6207 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6208 range is bigger than the low end of the current range, so we
6209 are only interested if the current range is a real range, and
6210 not an ordinary case label. */
35b1a6fa 6211 else if (high_bound
8f17b5c5
MM
6212 && high_value
6213 && (tree_int_cst_compare ((tree) high_bound->key,
6214 high_value)
6215 <= 0))
6216 node = high_bound;
6217 }
6218 /* If there was an overlap, issue an error. */
6219 if (node)
6220 {
8c161995 6221 tree duplicate = CASE_LABEL ((tree) node->value);
8f17b5c5
MM
6222
6223 if (high_value)
6224 {
c2255bc4
AH
6225 error_at (loc, "duplicate (or overlapping) case value");
6226 error_at (DECL_SOURCE_LOCATION (duplicate),
6227 "this is the first entry overlapping that value");
8f17b5c5
MM
6228 }
6229 else if (low_value)
6230 {
c2255bc4
AH
6231 error_at (loc, "duplicate case value") ;
6232 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
8f17b5c5
MM
6233 }
6234 else
6235 {
c2255bc4
AH
6236 error_at (loc, "multiple default labels in one switch");
6237 error_at (DECL_SOURCE_LOCATION (duplicate),
6238 "this is the first default label");
8f17b5c5 6239 }
6de9cd9a 6240 goto error_out;
8f17b5c5
MM
6241 }
6242
6243 /* Add a CASE_LABEL to the statement-tree. */
3d528853 6244 case_label = add_stmt (build_case_label (low_value, high_value, label));
8f17b5c5 6245 /* Register this case label in the splay tree. */
35b1a6fa 6246 splay_tree_insert (cases,
8f17b5c5
MM
6247 (splay_tree_key) low_value,
6248 (splay_tree_value) case_label);
6249
6250 return case_label;
6de9cd9a
DN
6251
6252 error_out:
9e14e18f 6253 /* Add a label so that the back-end doesn't think that the beginning of
6de9cd9a 6254 the switch is unreachable. Note that we do not add a case label, as
41806d92 6255 that just leads to duplicates and thence to failure later on. */
6de9cd9a
DN
6256 if (!cases->root)
6257 {
c2255bc4
AH
6258 tree t = create_artificial_label (loc);
6259 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6de9cd9a
DN
6260 }
6261 return error_mark_node;
6262}
6263
6264/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6265 Used to verify that case values match up with enumerator values. */
6266
6267static void
6268match_case_to_enum_1 (tree key, tree type, tree label)
6269{
807e902e
KZ
6270 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6271
6272 if (tree_fits_uhwi_p (key))
6273 print_dec (key, buf, UNSIGNED);
6274 else if (tree_fits_shwi_p (key))
6275 print_dec (key, buf, SIGNED);
6de9cd9a 6276 else
807e902e 6277 print_hex (key, buf);
6de9cd9a
DN
6278
6279 if (TYPE_NAME (type) == 0)
c5d75364
MLI
6280 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6281 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6282 "case value %qs not in enumerated type",
6283 buf);
6de9cd9a 6284 else
c5d75364
MLI
6285 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6286 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6287 "case value %qs not in enumerated type %qT",
6288 buf, type);
6de9cd9a
DN
6289}
6290
c782c2fe
RS
6291/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6292 Used to verify that case values match up with enumerator values. */
6293
6de9cd9a
DN
6294static int
6295match_case_to_enum (splay_tree_node node, void *data)
6296{
6297 tree label = (tree) node->value;
28dab132 6298 tree type = (tree) data;
6de9cd9a
DN
6299
6300 /* Skip default case. */
6301 if (!CASE_LOW (label))
6302 return 0;
6303
c782c2fe 6304 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6de9cd9a 6305 when we did our enum->case scan. Reset our scratch bit after. */
c782c2fe 6306 if (!CASE_LOW_SEEN (label))
6de9cd9a
DN
6307 match_case_to_enum_1 (CASE_LOW (label), type, label);
6308 else
c782c2fe 6309 CASE_LOW_SEEN (label) = 0;
6de9cd9a 6310
c782c2fe
RS
6311 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6312 not set, that means that CASE_HIGH did not appear when we did our
6313 enum->case scan. Reset our scratch bit after. */
6de9cd9a
DN
6314 if (CASE_HIGH (label))
6315 {
c782c2fe
RS
6316 if (!CASE_HIGH_SEEN (label))
6317 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6318 else
6319 CASE_HIGH_SEEN (label) = 0;
6de9cd9a
DN
6320 }
6321
6322 return 0;
6323}
6324
fbc315db
ILT
6325/* Handle -Wswitch*. Called from the front end after parsing the
6326 switch construct. */
6327/* ??? Should probably be somewhere generic, since other languages
6328 besides C and C++ would want this. At the moment, however, C/C++
6329 are the only tree-ssa languages that support enumerations at all,
6330 so the point is moot. */
6de9cd9a 6331
fbc315db
ILT
6332void
6333c_do_switch_warnings (splay_tree cases, location_t switch_location,
6334 tree type, tree cond)
6de9cd9a 6335{
9f63daea 6336 splay_tree_node default_node;
c782c2fe
RS
6337 splay_tree_node node;
6338 tree chain;
6de9cd9a
DN
6339
6340 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6341 return;
6342
6de9cd9a 6343 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
44c21c7f 6344 if (!default_node)
fab922b1
MLI
6345 warning_at (switch_location, OPT_Wswitch_default,
6346 "switch missing default case");
6de9cd9a 6347
c782c2fe
RS
6348 /* From here on, we only care about about enumerated types. */
6349 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6350 return;
6351
cdb88468
ILT
6352 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6353 if (!warn_switch_enum && !warn_switch)
c782c2fe
RS
6354 return;
6355
cdb88468
ILT
6356 /* Check the cases. Warn about case values which are not members of
6357 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6358 there is no default case, check that exactly all enumeration
6359 literals are covered by the cases. */
6360
c782c2fe
RS
6361 /* Clearing COND if it is not an integer constant simplifies
6362 the tests inside the loop below. */
6363 if (TREE_CODE (cond) != INTEGER_CST)
6364 cond = NULL_TREE;
6365
6366 /* The time complexity here is O(N*lg(N)) worst case, but for the
6367 common case of monotonically increasing enumerators, it is
6368 O(N), since the nature of the splay tree will keep the next
6369 element adjacent to the root at all times. */
6de9cd9a 6370
c782c2fe
RS
6371 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6372 {
6373 tree value = TREE_VALUE (chain);
adf2edec
DG
6374 if (TREE_CODE (value) == CONST_DECL)
6375 value = DECL_INITIAL (value);
c782c2fe
RS
6376 node = splay_tree_lookup (cases, (splay_tree_key) value);
6377 if (node)
6de9cd9a 6378 {
c782c2fe
RS
6379 /* Mark the CASE_LOW part of the case entry as seen. */
6380 tree label = (tree) node->value;
6381 CASE_LOW_SEEN (label) = 1;
6382 continue;
6383 }
6384
6385 /* Even though there wasn't an exact match, there might be a
fa10beec 6386 case range which includes the enumerator's value. */
c782c2fe
RS
6387 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6388 if (node && CASE_HIGH ((tree) node->value))
6389 {
6390 tree label = (tree) node->value;
6391 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6392 if (cmp >= 0)
6de9cd9a 6393 {
c782c2fe
RS
6394 /* If we match the upper bound exactly, mark the CASE_HIGH
6395 part of the case entry as seen. */
6396 if (cmp == 0)
6397 CASE_HIGH_SEEN (label) = 1;
6398 continue;
6de9cd9a
DN
6399 }
6400 }
6401
c782c2fe
RS
6402 /* We've now determined that this enumerated literal isn't
6403 handled by the case labels of the switch statement. */
6de9cd9a 6404
c782c2fe
RS
6405 /* If the switch expression is a constant, we only really care
6406 about whether that constant is handled by the switch. */
6407 if (cond && tree_int_cst_compare (cond, value))
6408 continue;
6de9cd9a 6409
683d6ff9 6410 /* If there is a default_node, the only relevant option is
cdb88468 6411 Wswitch-enum. Otherwise, if both are enabled then we prefer
683d6ff9
MLI
6412 to warn using -Wswitch because -Wswitch is enabled by -Wall
6413 while -Wswitch-enum is explicit. */
cdb88468
ILT
6414 warning_at (switch_location,
6415 (default_node || !warn_switch
6416 ? OPT_Wswitch_enum
6417 : OPT_Wswitch),
6418 "enumeration value %qE not handled in switch",
6419 TREE_PURPOSE (chain));
6de9cd9a 6420 }
c782c2fe
RS
6421
6422 /* Warn if there are case expressions that don't correspond to
6423 enumerators. This can occur since C and C++ don't enforce
6424 type-checking of assignments to enumeration variables.
6425
6426 The time complexity here is now always O(N) worst case, since
6427 we should have marked both the lower bound and upper bound of
6428 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6429 above. This scan also resets those fields. */
683d6ff9 6430
c782c2fe 6431 splay_tree_foreach (cases, match_case_to_enum, type);
8f17b5c5
MM
6432}
6433
6b665219 6434/* Finish an expression taking the address of LABEL (an
6a3799eb
AH
6435 IDENTIFIER_NODE). Returns an expression for the address.
6436
6437 LOC is the location for the expression returned. */
15b732b2 6438
35b1a6fa 6439tree
6a3799eb 6440finish_label_address_expr (tree label, location_t loc)
15b732b2
NB
6441{
6442 tree result;
6443
c1771a20 6444 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
15b732b2 6445
6b665219
MM
6446 if (label == error_mark_node)
6447 return error_mark_node;
6448
15b732b2
NB
6449 label = lookup_label (label);
6450 if (label == NULL_TREE)
6451 result = null_pointer_node;
6452 else
6453 {
6454 TREE_USED (label) = 1;
6455 result = build1 (ADDR_EXPR, ptr_type_node, label);
5805e48d 6456 /* The current function is not necessarily uninlinable.
15b732b2
NB
6457 Computed gotos are incompatible with inlining, but the value
6458 here could be used only in a diagnostic, for example. */
6a3799eb 6459 protected_set_expr_location (result, loc);
15b732b2
NB
6460 }
6461
6462 return result;
6463}
19552aa5
JM
6464\f
6465
6466/* Given a boolean expression ARG, return a tree representing an increment
6467 or decrement (as indicated by CODE) of ARG. The front end must check for
6468 invalid cases (e.g., decrement in C++). */
6469tree
35b1a6fa 6470boolean_increment (enum tree_code code, tree arg)
19552aa5
JM
6471{
6472 tree val;
b5119fa1 6473 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
37fa72e9 6474
19552aa5
JM
6475 arg = stabilize_reference (arg);
6476 switch (code)
6477 {
6478 case PREINCREMENT_EXPR:
53fb4de3 6479 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5
JM
6480 break;
6481 case POSTINCREMENT_EXPR:
53fb4de3 6482 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5 6483 arg = save_expr (arg);
53fb4de3
RS
6484 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6485 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6486 break;
6487 case PREDECREMENT_EXPR:
53fb4de3 6488 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6489 invert_truthvalue_loc (input_location, arg));
19552aa5
JM
6490 break;
6491 case POSTDECREMENT_EXPR:
53fb4de3 6492 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6493 invert_truthvalue_loc (input_location, arg));
19552aa5 6494 arg = save_expr (arg);
53fb4de3
RS
6495 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6496 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6497 break;
6498 default:
366de0ce 6499 gcc_unreachable ();
19552aa5
JM
6500 }
6501 TREE_SIDE_EFFECTS (val) = 1;
6502 return val;
6503}
03dc0325 6504\f
207bf79d
JM
6505/* Built-in macros for stddef.h and stdint.h, that require macros
6506 defined in this file. */
460bd0e3 6507void
35b1a6fa 6508c_stddef_cpp_builtins(void)
3df89291 6509{
5279d739
ZW
6510 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6511 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6512 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6513 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
85291069
JM
6514 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6515 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
c466b2cd
KVH
6516 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6517 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
207bf79d
JM
6518 if (SIG_ATOMIC_TYPE)
6519 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6520 if (INT8_TYPE)
6521 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6522 if (INT16_TYPE)
6523 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6524 if (INT32_TYPE)
6525 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6526 if (INT64_TYPE)
6527 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6528 if (UINT8_TYPE)
6529 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6530 if (UINT16_TYPE)
6531 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6532 if (UINT32_TYPE)
6533 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6534 if (UINT64_TYPE)
6535 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6536 if (INT_LEAST8_TYPE)
6537 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6538 if (INT_LEAST16_TYPE)
6539 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6540 if (INT_LEAST32_TYPE)
6541 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6542 if (INT_LEAST64_TYPE)
6543 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6544 if (UINT_LEAST8_TYPE)
6545 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6546 if (UINT_LEAST16_TYPE)
6547 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6548 if (UINT_LEAST32_TYPE)
6549 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6550 if (UINT_LEAST64_TYPE)
6551 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6552 if (INT_FAST8_TYPE)
6553 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6554 if (INT_FAST16_TYPE)
6555 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6556 if (INT_FAST32_TYPE)
6557 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6558 if (INT_FAST64_TYPE)
6559 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6560 if (UINT_FAST8_TYPE)
6561 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6562 if (UINT_FAST16_TYPE)
6563 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6564 if (UINT_FAST32_TYPE)
6565 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6566 if (UINT_FAST64_TYPE)
6567 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6568 if (INTPTR_TYPE)
6569 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6570 if (UINTPTR_TYPE)
6571 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
676997cf
RH
6572}
6573
6431177a 6574static void
35b1a6fa 6575c_init_attributes (void)
6431177a
JM
6576{
6577 /* Fill in the built_in_attributes array. */
4a90aeeb 6578#define DEF_ATTR_NULL_TREE(ENUM) \
6431177a 6579 built_in_attributes[(int) ENUM] = NULL_TREE;
4a90aeeb 6580#define DEF_ATTR_INT(ENUM, VALUE) \
c62c040f 6581 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
e384e6b5
BS
6582#define DEF_ATTR_STRING(ENUM, VALUE) \
6583 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6431177a
JM
6584#define DEF_ATTR_IDENT(ENUM, STRING) \
6585 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6586#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6587 built_in_attributes[(int) ENUM] \
6588 = tree_cons (built_in_attributes[(int) PURPOSE], \
6589 built_in_attributes[(int) VALUE], \
6590 built_in_attributes[(int) CHAIN]);
6431177a
JM
6591#include "builtin-attrs.def"
6592#undef DEF_ATTR_NULL_TREE
6593#undef DEF_ATTR_INT
6594#undef DEF_ATTR_IDENT
6595#undef DEF_ATTR_TREE_LIST
03dc0325 6596}
26f943fd 6597
943f82e7
JM
6598/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6599 identifier as an argument, so the front end shouldn't look it up. */
6600
6601bool
564a129d 6602attribute_takes_identifier_p (const_tree attr_id)
943f82e7 6603{
f231b5ff 6604 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
1b9b91a6
JM
6605 if (spec == NULL)
6606 /* Unknown attribute that we'll end up ignoring, return true so we
6607 don't complain about an identifier argument. */
6608 return true;
6609 else if (!strcmp ("mode", spec->name)
6610 || !strcmp ("format", spec->name)
6611 || !strcmp ("cleanup", spec->name))
564a129d
JM
6612 return true;
6613 else
6614 return targetm.attribute_takes_identifier_p (attr_id);
943f82e7
JM
6615}
6616
349ae713
NB
6617/* Attribute handlers common to C front ends. */
6618
6619/* Handle a "packed" attribute; arguments as in
6620 struct attribute_spec.handler. */
6621
6622static tree
e18476eb 6623handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
a742c759 6624 int flags, bool *no_add_attrs)
349ae713 6625{
c6e4cc53 6626 if (TYPE_P (*node))
349ae713
NB
6627 {
6628 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 6629 *node = build_variant_type_copy (*node);
c6e4cc53 6630 TYPE_PACKED (*node) = 1;
349ae713
NB
6631 }
6632 else if (TREE_CODE (*node) == FIELD_DECL)
646c0835 6633 {
2cd36c22
AN
6634 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6635 /* Still pack bitfields. */
6636 && ! DECL_INITIAL (*node))
646c0835
NS
6637 warning (OPT_Wattributes,
6638 "%qE attribute ignored for field of type %qT",
6639 name, TREE_TYPE (*node));
6640 else
6641 DECL_PACKED (*node) = 1;
6642 }
349ae713 6643 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
c6e4cc53
NS
6644 used for DECL_REGISTER. It wouldn't mean anything anyway.
6645 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6646 that changes what the typedef is typing. */
349ae713
NB
6647 else
6648 {
5c498b10 6649 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6650 *no_add_attrs = true;
6651 }
6652
6653 return NULL_TREE;
6654}
6655
6656/* Handle a "nocommon" attribute; arguments as in
6657 struct attribute_spec.handler. */
6658
6659static tree
35b1a6fa 6660handle_nocommon_attribute (tree *node, tree name,
e18476eb
BI
6661 tree ARG_UNUSED (args),
6662 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6663{
6664 if (TREE_CODE (*node) == VAR_DECL)
6665 DECL_COMMON (*node) = 0;
6666 else
6667 {
5c498b10 6668 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6669 *no_add_attrs = true;
6670 }
6671
6672 return NULL_TREE;
6673}
6674
6675/* Handle a "common" attribute; arguments as in
6676 struct attribute_spec.handler. */
6677
6678static tree
e18476eb
BI
6679handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6680 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6681{
6682 if (TREE_CODE (*node) == VAR_DECL)
6683 DECL_COMMON (*node) = 1;
6684 else
6685 {
5c498b10 6686 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6687 *no_add_attrs = true;
6688 }
6689
6690 return NULL_TREE;
6691}
6692
6693/* Handle a "noreturn" attribute; arguments as in
6694 struct attribute_spec.handler. */
6695
6696static tree
e18476eb
BI
6697handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6698 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6699{
6700 tree type = TREE_TYPE (*node);
6701
6702 /* See FIXME comment in c_common_attribute_table. */
2debdb4f
NP
6703 if (TREE_CODE (*node) == FUNCTION_DECL
6704 || objc_method_decl (TREE_CODE (*node)))
349ae713
NB
6705 TREE_THIS_VOLATILE (*node) = 1;
6706 else if (TREE_CODE (type) == POINTER_TYPE
6707 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6708 TREE_TYPE (*node)
6e7ceb17
PC
6709 = (build_qualified_type
6710 (build_pointer_type
6711 (build_type_variant (TREE_TYPE (type),
6712 TYPE_READONLY (TREE_TYPE (type)), 1)),
6713 TYPE_QUALS (type)));
349ae713
NB
6714 else
6715 {
5c498b10 6716 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6717 *no_add_attrs = true;
6718 }
6719
6720 return NULL_TREE;
6721}
6722
52bf96d2
JH
6723/* Handle a "hot" and attribute; arguments as in
6724 struct attribute_spec.handler. */
6725
6726static tree
6727handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
ab442df7 6728 int ARG_UNUSED (flags), bool *no_add_attrs)
52bf96d2 6729{
e45abe1f
RH
6730 if (TREE_CODE (*node) == FUNCTION_DECL
6731 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
6732 {
6733 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6734 {
45484dcf
MP
6735 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6736 "with attribute %qs", name, "cold");
52bf96d2
JH
6737 *no_add_attrs = true;
6738 }
5779e713
MM
6739 /* Most of the rest of the hot processing is done later with
6740 lookup_attribute. */
52bf96d2
JH
6741 }
6742 else
6743 {
6744 warning (OPT_Wattributes, "%qE attribute ignored", name);
6745 *no_add_attrs = true;
6746 }
6747
6748 return NULL_TREE;
6749}
e45abe1f 6750
52bf96d2
JH
6751/* Handle a "cold" and attribute; arguments as in
6752 struct attribute_spec.handler. */
6753
6754static tree
6755handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6756 int ARG_UNUSED (flags), bool *no_add_attrs)
6757{
e45abe1f
RH
6758 if (TREE_CODE (*node) == FUNCTION_DECL
6759 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
6760 {
6761 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6762 {
45484dcf
MP
6763 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6764 "with attribute %qs", name, "hot");
52bf96d2
JH
6765 *no_add_attrs = true;
6766 }
5779e713
MM
6767 /* Most of the rest of the cold processing is done later with
6768 lookup_attribute. */
52bf96d2
JH
6769 }
6770 else
6771 {
6772 warning (OPT_Wattributes, "%qE attribute ignored", name);
6773 *no_add_attrs = true;
6774 }
6775
6776 return NULL_TREE;
6777}
6778
e664c61c 6779/* Handle a "no_sanitize_address" attribute; arguments as in
77bc5132
JJ
6780 struct attribute_spec.handler. */
6781
6782static tree
e664c61c
KS
6783handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6784 bool *no_add_attrs)
77bc5132
JJ
6785{
6786 if (TREE_CODE (*node) != FUNCTION_DECL)
6787 {
6788 warning (OPT_Wattributes, "%qE attribute ignored", name);
6789 *no_add_attrs = true;
6790 }
6791
6792 return NULL_TREE;
6793}
6794
e664c61c
KS
6795/* Handle a "no_address_safety_analysis" attribute; arguments as in
6796 struct attribute_spec.handler. */
6797
6798static tree
6799handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6800 bool *no_add_attrs)
6801{
6802 if (TREE_CODE (*node) != FUNCTION_DECL)
6803 warning (OPT_Wattributes, "%qE attribute ignored", name);
6804 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6805 DECL_ATTRIBUTES (*node)
6806 = tree_cons (get_identifier ("no_sanitize_address"),
6807 NULL_TREE, DECL_ATTRIBUTES (*node));
6808 *no_add_attrs = true;
6809 return NULL_TREE;
6810}
6811
ce6923c5
MP
6812/* Handle a "no_sanitize_undefined" attribute; arguments as in
6813 struct attribute_spec.handler. */
6814
6815static tree
6816handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6817 bool *no_add_attrs)
6818{
6819 if (TREE_CODE (*node) != FUNCTION_DECL)
6820 {
6821 warning (OPT_Wattributes, "%qE attribute ignored", name);
6822 *no_add_attrs = true;
6823 }
6824
6825 return NULL_TREE;
6826}
6827
5434dc07
MD
6828/* Handle a "stack_protect" attribute; arguments as in
6829 struct attribute_spec.handler. */
6830static tree
6831handle_stack_protect_attribute (tree *node, tree name, tree, int,
6832 bool *no_add_attrs)
6833{
6834 if (TREE_CODE (*node) != FUNCTION_DECL)
6835 {
6836 warning (OPT_Wattributes, "%qE attribute ignored", name);
6837 *no_add_attrs = true;
6838 }
6839 else
6840 DECL_ATTRIBUTES (*node)
6841 = tree_cons (get_identifier ("stack_protect"),
6842 NULL_TREE, DECL_ATTRIBUTES (*node));
6843
6844 return NULL_TREE;
6845}
6846
349ae713
NB
6847/* Handle a "noinline" attribute; arguments as in
6848 struct attribute_spec.handler. */
6849
6850static tree
35b1a6fa 6851handle_noinline_attribute (tree *node, tree name,
e18476eb
BI
6852 tree ARG_UNUSED (args),
6853 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6854{
6855 if (TREE_CODE (*node) == FUNCTION_DECL)
45484dcf
MP
6856 {
6857 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6858 {
6859 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6860 "with attribute %qs", name, "always_inline");
6861 *no_add_attrs = true;
6862 }
6863 else
6864 DECL_UNINLINABLE (*node) = 1;
6865 }
349ae713
NB
6866 else
6867 {
5c498b10 6868 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6869 *no_add_attrs = true;
6870 }
6871
6872 return NULL_TREE;
6873}
6874
86631ea3
MJ
6875/* Handle a "noclone" attribute; arguments as in
6876 struct attribute_spec.handler. */
6877
6878static tree
6879handle_noclone_attribute (tree *node, tree name,
6880 tree ARG_UNUSED (args),
6881 int ARG_UNUSED (flags), bool *no_add_attrs)
6882{
6883 if (TREE_CODE (*node) != FUNCTION_DECL)
6884 {
6885 warning (OPT_Wattributes, "%qE attribute ignored", name);
6886 *no_add_attrs = true;
6887 }
6888
6889 return NULL_TREE;
6890}
6891
185c9e56
ML
6892/* Handle a "no_icf" attribute; arguments as in
6893 struct attribute_spec.handler. */
6894
6895static tree
6896handle_noicf_attribute (tree *node, tree name,
6897 tree ARG_UNUSED (args),
6898 int ARG_UNUSED (flags), bool *no_add_attrs)
6899{
6900 if (TREE_CODE (*node) != FUNCTION_DECL)
6901 {
6902 warning (OPT_Wattributes, "%qE attribute ignored", name);
6903 *no_add_attrs = true;
6904 }
6905
6906 return NULL_TREE;
6907}
6908
6909
349ae713
NB
6910/* Handle a "always_inline" attribute; arguments as in
6911 struct attribute_spec.handler. */
6912
6913static tree
35b1a6fa 6914handle_always_inline_attribute (tree *node, tree name,
e18476eb
BI
6915 tree ARG_UNUSED (args),
6916 int ARG_UNUSED (flags),
a742c759 6917 bool *no_add_attrs)
349ae713
NB
6918{
6919 if (TREE_CODE (*node) == FUNCTION_DECL)
6920 {
45484dcf
MP
6921 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6922 {
6923 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6924 "with %qs attribute", name, "noinline");
6925 *no_add_attrs = true;
6926 }
6927 else
6928 /* Set the attribute and mark it for disregarding inline
6929 limits. */
6930 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
349ae713
NB
6931 }
6932 else
6933 {
5c498b10 6934 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713 6935 *no_add_attrs = true;
4eb7fd83
JJ
6936 }
6937
6938 return NULL_TREE;
6939}
6940
6941/* Handle a "gnu_inline" attribute; arguments as in
6942 struct attribute_spec.handler. */
6943
6944static tree
6945handle_gnu_inline_attribute (tree *node, tree name,
6946 tree ARG_UNUSED (args),
6947 int ARG_UNUSED (flags),
6948 bool *no_add_attrs)
6949{
6950 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6951 {
6952 /* Do nothing else, just set the attribute. We'll get at
6953 it later with lookup_attribute. */
6954 }
6955 else
6956 {
6957 warning (OPT_Wattributes, "%qE attribute ignored", name);
6958 *no_add_attrs = true;
46a4da10
JH
6959 }
6960
6961 return NULL_TREE;
6962}
6963
6964/* Handle a "leaf" attribute; arguments as in
6965 struct attribute_spec.handler. */
6966
6967static tree
6968handle_leaf_attribute (tree *node, tree name,
6969 tree ARG_UNUSED (args),
6970 int ARG_UNUSED (flags), bool *no_add_attrs)
6971{
6972 if (TREE_CODE (*node) != FUNCTION_DECL)
6973 {
6974 warning (OPT_Wattributes, "%qE attribute ignored", name);
6975 *no_add_attrs = true;
6976 }
6977 if (!TREE_PUBLIC (*node))
6978 {
6979 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6980 *no_add_attrs = true;
349ae713
NB
6981 }
6982
6983 return NULL_TREE;
6984}
6985
d752cfdb
JJ
6986/* Handle an "artificial" attribute; arguments as in
6987 struct attribute_spec.handler. */
6988
6989static tree
6990handle_artificial_attribute (tree *node, tree name,
6991 tree ARG_UNUSED (args),
6992 int ARG_UNUSED (flags),
6993 bool *no_add_attrs)
6994{
6995 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6996 {
6997 /* Do nothing else, just set the attribute. We'll get at
6998 it later with lookup_attribute. */
6999 }
7000 else
7001 {
7002 warning (OPT_Wattributes, "%qE attribute ignored", name);
7003 *no_add_attrs = true;
7004 }
7005
7006 return NULL_TREE;
7007}
7008
0691d1d4
RG
7009/* Handle a "flatten" attribute; arguments as in
7010 struct attribute_spec.handler. */
7011
7012static tree
7013handle_flatten_attribute (tree *node, tree name,
c22cacf3
MS
7014 tree args ATTRIBUTE_UNUSED,
7015 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0691d1d4
RG
7016{
7017 if (TREE_CODE (*node) == FUNCTION_DECL)
7018 /* Do nothing else, just set the attribute. We'll get at
7019 it later with lookup_attribute. */
7020 ;
7021 else
7022 {
7023 warning (OPT_Wattributes, "%qE attribute ignored", name);
7024 *no_add_attrs = true;
7025 }
7026
7027 return NULL_TREE;
7028}
7029
d2af6a68
JJ
7030/* Handle a "warning" or "error" attribute; arguments as in
7031 struct attribute_spec.handler. */
7032
7033static tree
7034handle_error_attribute (tree *node, tree name, tree args,
7035 int ARG_UNUSED (flags), bool *no_add_attrs)
7036{
7037 if (TREE_CODE (*node) == FUNCTION_DECL
cf35e2b1 7038 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
d2af6a68
JJ
7039 /* Do nothing else, just set the attribute. We'll get at
7040 it later with lookup_attribute. */
7041 ;
7042 else
7043 {
7044 warning (OPT_Wattributes, "%qE attribute ignored", name);
7045 *no_add_attrs = true;
7046 }
7047
7048 return NULL_TREE;
7049}
0691d1d4 7050
349ae713
NB
7051/* Handle a "used" attribute; arguments as in
7052 struct attribute_spec.handler. */
7053
7054static tree
e18476eb
BI
7055handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7056 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 7057{
d7ddbe24
RH
7058 tree node = *pnode;
7059
7060 if (TREE_CODE (node) == FUNCTION_DECL
3797cb21
DS
7061 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7062 || (TREE_CODE (node) == TYPE_DECL))
4d7d0451 7063 {
4d7d0451 7064 TREE_USED (node) = 1;
8e3e233b 7065 DECL_PRESERVE_P (node) = 1;
ebfbbdc5
JJ
7066 if (TREE_CODE (node) == VAR_DECL)
7067 DECL_READ_P (node) = 1;
4d7d0451 7068 }
349ae713
NB
7069 else
7070 {
5c498b10 7071 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7072 *no_add_attrs = true;
7073 }
7074
7075 return NULL_TREE;
7076}
7077
7078/* Handle a "unused" attribute; arguments as in
7079 struct attribute_spec.handler. */
7080
7081static tree
e18476eb
BI
7082handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7083 int flags, bool *no_add_attrs)
349ae713
NB
7084{
7085 if (DECL_P (*node))
7086 {
7087 tree decl = *node;
7088
7089 if (TREE_CODE (decl) == PARM_DECL
7090 || TREE_CODE (decl) == VAR_DECL
7091 || TREE_CODE (decl) == FUNCTION_DECL
7092 || TREE_CODE (decl) == LABEL_DECL
7093 || TREE_CODE (decl) == TYPE_DECL)
ebfbbdc5
JJ
7094 {
7095 TREE_USED (decl) = 1;
7096 if (TREE_CODE (decl) == VAR_DECL
7097 || TREE_CODE (decl) == PARM_DECL)
7098 DECL_READ_P (decl) = 1;
7099 }
349ae713
NB
7100 else
7101 {
5c498b10 7102 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7103 *no_add_attrs = true;
7104 }
7105 }
7106 else
7107 {
7108 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 7109 *node = build_variant_type_copy (*node);
349ae713
NB
7110 TREE_USED (*node) = 1;
7111 }
7112
7113 return NULL_TREE;
7114}
7115
ce91e74c
JH
7116/* Handle a "externally_visible" attribute; arguments as in
7117 struct attribute_spec.handler. */
7118
7119static tree
7120handle_externally_visible_attribute (tree *pnode, tree name,
7121 tree ARG_UNUSED (args),
7122 int ARG_UNUSED (flags),
7123 bool *no_add_attrs)
7124{
7125 tree node = *pnode;
7126
343d4b27 7127 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
ce91e74c 7128 {
343d4b27
JJ
7129 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7130 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7131 {
7132 warning (OPT_Wattributes,
7133 "%qE attribute have effect only on public objects", name);
7134 *no_add_attrs = true;
7135 }
ce91e74c 7136 }
ce91e74c
JH
7137 else
7138 {
7139 warning (OPT_Wattributes, "%qE attribute ignored", name);
7140 *no_add_attrs = true;
7141 }
7142
7143 return NULL_TREE;
7144}
7145
7861b648
AK
7146/* Handle the "no_reorder" attribute. Arguments as in
7147 struct attribute_spec.handler. */
7148
7149static tree
7150handle_no_reorder_attribute (tree *pnode,
7151 tree name,
7152 tree,
7153 int,
7154 bool *no_add_attrs)
7155{
7156 tree node = *pnode;
7157
7158 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7159 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7160 {
7161 warning (OPT_Wattributes,
7162 "%qE attribute only affects top level objects",
7163 name);
7164 *no_add_attrs = true;
7165 }
7166
7167 return NULL_TREE;
7168}
7169
349ae713
NB
7170/* Handle a "const" attribute; arguments as in
7171 struct attribute_spec.handler. */
7172
7173static tree
e18476eb
BI
7174handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7175 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7176{
7177 tree type = TREE_TYPE (*node);
7178
7179 /* See FIXME comment on noreturn in c_common_attribute_table. */
7180 if (TREE_CODE (*node) == FUNCTION_DECL)
7181 TREE_READONLY (*node) = 1;
7182 else if (TREE_CODE (type) == POINTER_TYPE
7183 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7184 TREE_TYPE (*node)
6e7ceb17
PC
7185 = (build_qualified_type
7186 (build_pointer_type
7187 (build_type_variant (TREE_TYPE (type), 1,
7188 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7189 TYPE_QUALS (type)));
349ae713
NB
7190 else
7191 {
5c498b10 7192 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7193 *no_add_attrs = true;
7194 }
7195
7196 return NULL_TREE;
7197}
7198
7199/* Handle a "transparent_union" attribute; arguments as in
7200 struct attribute_spec.handler. */
7201
7202static tree
35b1a6fa 7203handle_transparent_union_attribute (tree *node, tree name,
e18476eb 7204 tree ARG_UNUSED (args), int flags,
a742c759 7205 bool *no_add_attrs)
349ae713 7206{
4009f2e7 7207 tree type;
52dd234b
RH
7208
7209 *no_add_attrs = true;
349ae713 7210
e28d52cf
DS
7211
7212 if (TREE_CODE (*node) == TYPE_DECL
7213 && ! (flags & ATTR_FLAG_CXX11))
4009f2e7
JM
7214 node = &TREE_TYPE (*node);
7215 type = *node;
349ae713 7216
52dd234b 7217 if (TREE_CODE (type) == UNION_TYPE)
349ae713 7218 {
d58d6eb5
JM
7219 /* Make sure that the first field will work for a transparent union.
7220 If the type isn't complete yet, leave the check to the code in
7221 finish_struct. */
7222 if (TYPE_SIZE (type))
7223 {
7224 tree first = first_field (type);
7225 if (first == NULL_TREE
7226 || DECL_ARTIFICIAL (first)
7227 || TYPE_MODE (type) != DECL_MODE (first))
7228 goto ignored;
7229 }
7230
349ae713 7231 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
52dd234b 7232 {
d58d6eb5
JM
7233 /* If the type isn't complete yet, setting the flag
7234 on a variant wouldn't ever be checked. */
7235 if (!TYPE_SIZE (type))
7236 goto ignored;
7237
7238 /* build_duplicate_type doesn't work for C++. */
7239 if (c_dialect_cxx ())
52dd234b
RH
7240 goto ignored;
7241
7242 /* A type variant isn't good enough, since we don't a cast
7243 to such a type removed as a no-op. */
7244 *node = type = build_duplicate_type (type);
7245 }
7246
ebf0bf7f 7247 TYPE_TRANSPARENT_AGGR (type) = 1;
52dd234b 7248 return NULL_TREE;
349ae713
NB
7249 }
7250
52dd234b
RH
7251 ignored:
7252 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7253 return NULL_TREE;
7254}
7255
fc8600f9
MM
7256/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7257 get the requested priority for a constructor or destructor,
7258 possibly issuing diagnostics for invalid or reserved
7259 priorities. */
7260
7261static priority_type
7262get_priority (tree args, bool is_destructor)
7263{
7264 HOST_WIDE_INT pri;
b2f4bed8 7265 tree arg;
fc8600f9
MM
7266
7267 if (!args)
7268 return DEFAULT_INIT_PRIORITY;
b8698a0f 7269
f6fc5c86
MM
7270 if (!SUPPORTS_INIT_PRIORITY)
7271 {
7272 if (is_destructor)
7273 error ("destructor priorities are not supported");
7274 else
7275 error ("constructor priorities are not supported");
7276 return DEFAULT_INIT_PRIORITY;
7277 }
7278
b2f4bed8 7279 arg = TREE_VALUE (args);
fad7652e
JJ
7280 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7281 goto invalid;
7282 if (arg == error_mark_node)
7283 return DEFAULT_INIT_PRIORITY;
8d0d1915 7284 arg = default_conversion (arg);
9541ffee 7285 if (!tree_fits_shwi_p (arg)
b2f4bed8 7286 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
fc8600f9
MM
7287 goto invalid;
7288
9439e9a1 7289 pri = tree_to_shwi (arg);
fc8600f9
MM
7290 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7291 goto invalid;
7292
7293 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7294 {
7295 if (is_destructor)
7296 warning (0,
7297 "destructor priorities from 0 to %d are reserved "
b8698a0f 7298 "for the implementation",
fc8600f9
MM
7299 MAX_RESERVED_INIT_PRIORITY);
7300 else
7301 warning (0,
7302 "constructor priorities from 0 to %d are reserved "
b8698a0f 7303 "for the implementation",
fc8600f9
MM
7304 MAX_RESERVED_INIT_PRIORITY);
7305 }
7306 return pri;
7307
7308 invalid:
7309 if (is_destructor)
7310 error ("destructor priorities must be integers from 0 to %d inclusive",
7311 MAX_INIT_PRIORITY);
7312 else
7313 error ("constructor priorities must be integers from 0 to %d inclusive",
7314 MAX_INIT_PRIORITY);
7315 return DEFAULT_INIT_PRIORITY;
7316}
7317
349ae713
NB
7318/* Handle a "constructor" attribute; arguments as in
7319 struct attribute_spec.handler. */
7320
7321static tree
fc8600f9 7322handle_constructor_attribute (tree *node, tree name, tree args,
e18476eb 7323 int ARG_UNUSED (flags),
a742c759 7324 bool *no_add_attrs)
349ae713
NB
7325{
7326 tree decl = *node;
7327 tree type = TREE_TYPE (decl);
7328
7329 if (TREE_CODE (decl) == FUNCTION_DECL
7330 && TREE_CODE (type) == FUNCTION_TYPE
7331 && decl_function_context (decl) == 0)
7332 {
fc8600f9 7333 priority_type priority;
349ae713 7334 DECL_STATIC_CONSTRUCTOR (decl) = 1;
fc8600f9
MM
7335 priority = get_priority (args, /*is_destructor=*/false);
7336 SET_DECL_INIT_PRIORITY (decl, priority);
349ae713
NB
7337 TREE_USED (decl) = 1;
7338 }
7339 else
7340 {
5c498b10 7341 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7342 *no_add_attrs = true;
7343 }
7344
7345 return NULL_TREE;
7346}
7347
7348/* Handle a "destructor" attribute; arguments as in
7349 struct attribute_spec.handler. */
7350
7351static tree
fc8600f9 7352handle_destructor_attribute (tree *node, tree name, tree args,
e18476eb 7353 int ARG_UNUSED (flags),
a742c759 7354 bool *no_add_attrs)
349ae713
NB
7355{
7356 tree decl = *node;
7357 tree type = TREE_TYPE (decl);
7358
7359 if (TREE_CODE (decl) == FUNCTION_DECL
7360 && TREE_CODE (type) == FUNCTION_TYPE
7361 && decl_function_context (decl) == 0)
7362 {
fc8600f9 7363 priority_type priority;
349ae713 7364 DECL_STATIC_DESTRUCTOR (decl) = 1;
fc8600f9
MM
7365 priority = get_priority (args, /*is_destructor=*/true);
7366 SET_DECL_FINI_PRIORITY (decl, priority);
349ae713
NB
7367 TREE_USED (decl) = 1;
7368 }
7369 else
7370 {
5c498b10 7371 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7372 *no_add_attrs = true;
7373 }
7374
7375 return NULL_TREE;
7376}
7377
4d451982
MLI
7378/* Nonzero if the mode is a valid vector mode for this architecture.
7379 This returns nonzero even if there is no hardware support for the
7380 vector mode, but we can emulate with narrower modes. */
7381
7382static int
ef4bddc2 7383vector_mode_valid_p (machine_mode mode)
4d451982
MLI
7384{
7385 enum mode_class mclass = GET_MODE_CLASS (mode);
ef4bddc2 7386 machine_mode innermode;
4d451982
MLI
7387
7388 /* Doh! What's going on? */
7389 if (mclass != MODE_VECTOR_INT
7390 && mclass != MODE_VECTOR_FLOAT
7391 && mclass != MODE_VECTOR_FRACT
7392 && mclass != MODE_VECTOR_UFRACT
7393 && mclass != MODE_VECTOR_ACCUM
7394 && mclass != MODE_VECTOR_UACCUM)
7395 return 0;
7396
7397 /* Hardware support. Woo hoo! */
7398 if (targetm.vector_mode_supported_p (mode))
7399 return 1;
7400
7401 innermode = GET_MODE_INNER (mode);
7402
7403 /* We should probably return 1 if requesting V4DI and we have no DI,
7404 but we have V2DI, but this is probably very unlikely. */
7405
7406 /* If we have support for the inner mode, we can safely emulate it.
7407 We may not have V2DI, but me can emulate with a pair of DIs. */
7408 return targetm.scalar_mode_supported_p (innermode);
7409}
7410
7411
349ae713
NB
7412/* Handle a "mode" attribute; arguments as in
7413 struct attribute_spec.handler. */
7414
7415static tree
e18476eb
BI
7416handle_mode_attribute (tree *node, tree name, tree args,
7417 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7418{
7419 tree type = *node;
88388a52 7420 tree ident = TREE_VALUE (args);
349ae713
NB
7421
7422 *no_add_attrs = true;
7423
88388a52 7424 if (TREE_CODE (ident) != IDENTIFIER_NODE)
5c498b10 7425 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7426 else
7427 {
7428 int j;
88388a52 7429 const char *p = IDENTIFIER_POINTER (ident);
349ae713 7430 int len = strlen (p);
ef4bddc2 7431 machine_mode mode = VOIDmode;
349ae713 7432 tree typefm;
6dd53648 7433 bool valid_mode;
349ae713
NB
7434
7435 if (len > 4 && p[0] == '_' && p[1] == '_'
7436 && p[len - 1] == '_' && p[len - 2] == '_')
7437 {
28dab132 7438 char *newp = (char *) alloca (len - 1);
349ae713
NB
7439
7440 strcpy (newp, &p[2]);
7441 newp[len - 4] = '\0';
7442 p = newp;
7443 }
7444
7445 /* Change this type to have a type with the specified mode.
7446 First check for the special modes. */
3f75a254 7447 if (!strcmp (p, "byte"))
349ae713
NB
7448 mode = byte_mode;
7449 else if (!strcmp (p, "word"))
7450 mode = word_mode;
3f75a254 7451 else if (!strcmp (p, "pointer"))
349ae713 7452 mode = ptr_mode;
c7ff6e7a
AK
7453 else if (!strcmp (p, "libgcc_cmp_return"))
7454 mode = targetm.libgcc_cmp_return_mode ();
7455 else if (!strcmp (p, "libgcc_shift_count"))
7456 mode = targetm.libgcc_shift_count_mode ();
7b0518e3
UW
7457 else if (!strcmp (p, "unwind_word"))
7458 mode = targetm.unwind_word_mode ();
349ae713
NB
7459 else
7460 for (j = 0; j < NUM_MACHINE_MODES; j++)
7461 if (!strcmp (p, GET_MODE_NAME (j)))
61f03aba 7462 {
ef4bddc2 7463 mode = (machine_mode) j;
61f03aba
RH
7464 break;
7465 }
349ae713
NB
7466
7467 if (mode == VOIDmode)
4a5eab38 7468 {
88388a52 7469 error ("unknown machine mode %qE", ident);
4a5eab38
PB
7470 return NULL_TREE;
7471 }
7472
6dd53648
RH
7473 valid_mode = false;
7474 switch (GET_MODE_CLASS (mode))
4a5eab38 7475 {
6dd53648
RH
7476 case MODE_INT:
7477 case MODE_PARTIAL_INT:
7478 case MODE_FLOAT:
9a8ce21f 7479 case MODE_DECIMAL_FLOAT:
ab22c1fa
CF
7480 case MODE_FRACT:
7481 case MODE_UFRACT:
7482 case MODE_ACCUM:
7483 case MODE_UACCUM:
6dd53648
RH
7484 valid_mode = targetm.scalar_mode_supported_p (mode);
7485 break;
7486
7487 case MODE_COMPLEX_INT:
7488 case MODE_COMPLEX_FLOAT:
7489 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7490 break;
7491
7492 case MODE_VECTOR_INT:
7493 case MODE_VECTOR_FLOAT:
ab22c1fa
CF
7494 case MODE_VECTOR_FRACT:
7495 case MODE_VECTOR_UFRACT:
7496 case MODE_VECTOR_ACCUM:
7497 case MODE_VECTOR_UACCUM:
5c498b10
DD
7498 warning (OPT_Wattributes, "specifying vector types with "
7499 "__attribute__ ((mode)) is deprecated");
7500 warning (OPT_Wattributes,
7501 "use __attribute__ ((vector_size)) instead");
6dd53648
RH
7502 valid_mode = vector_mode_valid_p (mode);
7503 break;
4a5eab38 7504
6dd53648
RH
7505 default:
7506 break;
7507 }
7508 if (!valid_mode)
7509 {
9e637a26 7510 error ("unable to emulate %qs", p);
6dd53648
RH
7511 return NULL_TREE;
7512 }
4a5eab38 7513
6dd53648 7514 if (POINTER_TYPE_P (type))
cb2a532e 7515 {
36c5e70a 7516 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
ef4bddc2 7517 tree (*fn)(tree, machine_mode, bool);
6dd53648 7518
36c5e70a 7519 if (!targetm.addr_space.valid_pointer_mode (mode, as))
cb2a532e 7520 {
9e637a26 7521 error ("invalid pointer mode %qs", p);
cb2a532e
AH
7522 return NULL_TREE;
7523 }
7524
c22cacf3 7525 if (TREE_CODE (type) == POINTER_TYPE)
6dd53648 7526 fn = build_pointer_type_for_mode;
4977bab6 7527 else
6dd53648
RH
7528 fn = build_reference_type_for_mode;
7529 typefm = fn (TREE_TYPE (type), mode, false);
cb2a532e 7530 }
6dd53648 7531 else
ab22c1fa
CF
7532 {
7533 /* For fixed-point modes, we need to test if the signness of type
7534 and the machine mode are consistent. */
7535 if (ALL_FIXED_POINT_MODE_P (mode)
7536 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7537 {
d8a07487 7538 error ("signedness of type and machine mode %qs don%'t match", p);
ab22c1fa
CF
7539 return NULL_TREE;
7540 }
7541 /* For fixed-point modes, we need to pass saturating info. */
7542 typefm = lang_hooks.types.type_for_mode (mode,
7543 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7544 : TYPE_UNSIGNED (type));
7545 }
ec8465a5 7546
6dd53648
RH
7547 if (typefm == NULL_TREE)
7548 {
61f03aba 7549 error ("no data type for mode %qs", p);
6dd53648
RH
7550 return NULL_TREE;
7551 }
ec8465a5
RK
7552 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7553 {
7554 /* For enumeral types, copy the precision from the integer
7555 type returned above. If not an INTEGER_TYPE, we can't use
7556 this mode for this type. */
7557 if (TREE_CODE (typefm) != INTEGER_TYPE)
7558 {
61f03aba 7559 error ("cannot use mode %qs for enumeral types", p);
ec8465a5
RK
7560 return NULL_TREE;
7561 }
7562
99db1ef0
RH
7563 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7564 {
7565 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7566 typefm = type;
7567 }
7568 else
7569 {
7570 /* We cannot build a type variant, as there's code that assumes
7571 that TYPE_MAIN_VARIANT has the same mode. This includes the
7572 debug generators. Instead, create a subrange type. This
7573 results in all of the enumeral values being emitted only once
7574 in the original, and the subtype gets them by reference. */
7575 if (TYPE_UNSIGNED (type))
7576 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7577 else
7578 typefm = make_signed_type (TYPE_PRECISION (typefm));
7579 TREE_TYPE (typefm) = type;
7580 }
ec8465a5 7581 }
a2d36602
RH
7582 else if (VECTOR_MODE_P (mode)
7583 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7584 : TREE_CODE (type) != TREE_CODE (typefm))
61f03aba
RH
7585 {
7586 error ("mode %qs applied to inappropriate type", p);
7587 return NULL_TREE;
7588 }
7589
6dd53648 7590 *node = typefm;
349ae713
NB
7591 }
7592
7593 return NULL_TREE;
7594}
7595
7596/* Handle a "section" attribute; arguments as in
7597 struct attribute_spec.handler. */
7598
7599static tree
e18476eb
BI
7600handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7601 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7602{
7603 tree decl = *node;
7604
677f3fa8 7605 if (targetm_common.have_named_sections)
349ae713 7606 {
9fb32434
CT
7607 user_defined_section_attribute = true;
7608
349ae713
NB
7609 if ((TREE_CODE (decl) == FUNCTION_DECL
7610 || TREE_CODE (decl) == VAR_DECL)
7611 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7612 {
7613 if (TREE_CODE (decl) == VAR_DECL
7614 && current_function_decl != NULL_TREE
3f75a254 7615 && !TREE_STATIC (decl))
349ae713 7616 {
b8698a0f 7617 error_at (DECL_SOURCE_LOCATION (decl),
c5d75364
MLI
7618 "section attribute cannot be specified for "
7619 "local variables");
349ae713
NB
7620 *no_add_attrs = true;
7621 }
7622
7623 /* The decl may have already been given a section attribute
7624 from a previous declaration. Ensure they match. */
f961457f
JH
7625 else if (DECL_SECTION_NAME (decl) != NULL
7626 && strcmp (DECL_SECTION_NAME (decl),
349ae713
NB
7627 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7628 {
dee15844
JM
7629 error ("section of %q+D conflicts with previous declaration",
7630 *node);
349ae713
NB
7631 *no_add_attrs = true;
7632 }
feb60f03
NS
7633 else if (TREE_CODE (decl) == VAR_DECL
7634 && !targetm.have_tls && targetm.emutls.tmpl_section
7635 && DECL_THREAD_LOCAL_P (decl))
7636 {
7637 error ("section of %q+D cannot be overridden", *node);
7638 *no_add_attrs = true;
7639 }
349ae713 7640 else
f961457f
JH
7641 set_decl_section_name (decl,
7642 TREE_STRING_POINTER (TREE_VALUE (args)));
349ae713
NB
7643 }
7644 else
7645 {
dee15844 7646 error ("section attribute not allowed for %q+D", *node);
349ae713
NB
7647 *no_add_attrs = true;
7648 }
7649 }
7650 else
7651 {
c5d75364
MLI
7652 error_at (DECL_SOURCE_LOCATION (*node),
7653 "section attributes are not supported for this target");
349ae713
NB
7654 *no_add_attrs = true;
7655 }
7656
7657 return NULL_TREE;
7658}
7659
d19fa6b5
JM
7660/* Check whether ALIGN is a valid user-specified alignment. If so,
7661 return its base-2 log; if not, output an error and return -1. If
7662 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7663 no error. */
7664int
7665check_user_alignment (const_tree align, bool allow_zero)
7666{
7667 int i;
7668
661a0813
MP
7669 if (error_operand_p (align))
7670 return -1;
a859517f
MP
7671 if (TREE_CODE (align) != INTEGER_CST
7672 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
d19fa6b5
JM
7673 {
7674 error ("requested alignment is not an integer constant");
7675 return -1;
7676 }
7677 else if (allow_zero && integer_zerop (align))
7678 return -1;
3f12f6e9
SKS
7679 else if (tree_int_cst_sgn (align) == -1
7680 || (i = tree_log2 (align)) == -1)
d19fa6b5 7681 {
3f12f6e9 7682 error ("requested alignment is not a positive power of 2");
d19fa6b5
JM
7683 return -1;
7684 }
7685 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7686 {
7687 error ("requested alignment is too large");
7688 return -1;
7689 }
7690 return i;
7691}
7692
e28d52cf
DS
7693/*
7694 If in c++-11, check if the c++-11 alignment constraint with respect
7695 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7696 c++-11 mode, does nothing.
7697
7698 [dcl.align]2/ says:
7699
7700 [* if the constant expression evaluates to a fundamental alignment,
7701 the alignment requirement of the declared entity shall be the
7702 specified fundamental alignment.
7703
7704 * if the constant expression evaluates to an extended alignment
7705 and the implementation supports that alignment in the context
7706 of the declaration, the alignment of the declared entity shall
7707 be that alignment
7708
7709 * if the constant expression evaluates to an extended alignment
7710 and the implementation does not support that alignment in the
7711 context of the declaration, the program is ill-formed]. */
7712
7713static bool
7714check_cxx_fundamental_alignment_constraints (tree node,
7715 unsigned align_log,
7716 int flags)
7717{
7718 bool alignment_too_large_p = false;
7719 unsigned requested_alignment = 1U << align_log;
7720 unsigned max_align = 0;
7721
7722 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7723 || (node == NULL_TREE || node == error_mark_node))
7724 return true;
7725
7726 if (cxx_fundamental_alignment_p (requested_alignment))
7727 return true;
7728
7729 if (DECL_P (node))
7730 {
7731 if (TREE_STATIC (node))
7732 {
7733 /* For file scope variables and static members, the target
7734 supports alignments that are at most
7735 MAX_OFILE_ALIGNMENT. */
7736 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7737 alignment_too_large_p = true;
7738 }
7739 else
7740 {
7741#ifdef BIGGEST_FIELD_ALIGNMENT
7742#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7743#else
7744#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7745#endif
7746 /* For non-static members, the target supports either
7747 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7748 if it is defined or BIGGEST_ALIGNMENT. */
7749 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7750 if (TREE_CODE (node) == FIELD_DECL
7751 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7752 alignment_too_large_p = true;
7753#undef MAX_TARGET_FIELD_ALIGNMENT
7754 /* For stack variables, the target supports at most
7755 MAX_STACK_ALIGNMENT. */
7756 else if (decl_function_context (node) != NULL
7757 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7758 alignment_too_large_p = true;
7759 }
7760 }
7761 else if (TYPE_P (node))
7762 {
7763 /* Let's be liberal for types. */
7764 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7765 alignment_too_large_p = true;
7766 }
7767
7768 if (alignment_too_large_p)
7769 pedwarn (input_location, OPT_Wattributes,
7770 "requested alignment %d is larger than %d",
7771 requested_alignment, max_align);
7772
7773 return !alignment_too_large_p;
7774}
7775
349ae713
NB
7776/* Handle a "aligned" attribute; arguments as in
7777 struct attribute_spec.handler. */
7778
7779static tree
e18476eb 7780handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
a742c759 7781 int flags, bool *no_add_attrs)
349ae713
NB
7782{
7783 tree decl = NULL_TREE;
7784 tree *type = NULL;
7785 int is_type = 0;
5d77fb19 7786 tree align_expr;
349ae713
NB
7787 int i;
7788
5d77fb19
MG
7789 if (args)
7790 {
7791 align_expr = TREE_VALUE (args);
661a0813
MP
7792 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7793 && TREE_CODE (align_expr) != FUNCTION_DECL)
5d77fb19
MG
7794 align_expr = default_conversion (align_expr);
7795 }
7796 else
7797 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7798
349ae713
NB
7799 if (DECL_P (*node))
7800 {
7801 decl = *node;
7802 type = &TREE_TYPE (decl);
7803 is_type = TREE_CODE (*node) == TYPE_DECL;
7804 }
7805 else if (TYPE_P (*node))
7806 type = node, is_type = 1;
7807
e28d52cf
DS
7808 if ((i = check_user_alignment (align_expr, false)) == -1
7809 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
d19fa6b5 7810 *no_add_attrs = true;
349ae713
NB
7811 else if (is_type)
7812 {
0f559c16
JM
7813 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7814 /* OK, modify the type in place. */;
349ae713
NB
7815 /* If we have a TYPE_DECL, then copy the type, so that we
7816 don't accidentally modify a builtin type. See pushdecl. */
0f559c16
JM
7817 else if (decl && TREE_TYPE (decl) != error_mark_node
7818 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
349ae713
NB
7819 {
7820 tree tt = TREE_TYPE (decl);
8dd16ecc 7821 *type = build_variant_type_copy (*type);
349ae713
NB
7822 DECL_ORIGINAL_TYPE (decl) = tt;
7823 TYPE_NAME (*type) = decl;
7824 TREE_USED (*type) = TREE_USED (decl);
7825 TREE_TYPE (decl) = *type;
7826 }
0f559c16 7827 else
8dd16ecc 7828 *type = build_variant_type_copy (*type);
349ae713 7829
d9223014 7830 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
349ae713
NB
7831 TYPE_USER_ALIGN (*type) = 1;
7832 }
837edd5f 7833 else if (! VAR_OR_FUNCTION_DECL_P (decl)
349ae713
NB
7834 && TREE_CODE (decl) != FIELD_DECL)
7835 {
dee15844 7836 error ("alignment may not be specified for %q+D", decl);
349ae713
NB
7837 *no_add_attrs = true;
7838 }
e28d52cf
DS
7839 else if (DECL_USER_ALIGN (decl)
7840 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7841 /* C++-11 [dcl.align/4]:
7842
7843 When multiple alignment-specifiers are specified for an
7844 entity, the alignment requirement shall be set to the
7845 strictest specified alignment.
7846
7847 This formally comes from the c++11 specification but we are
7848 doing it for the GNU attribute syntax as well. */
7849 *no_add_attrs = true;
837edd5f 7850 else if (TREE_CODE (decl) == FUNCTION_DECL
d9223014 7851 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
837edd5f
GK
7852 {
7853 if (DECL_USER_ALIGN (decl))
7854 error ("alignment for %q+D was previously specified as %d "
7855 "and may not be decreased", decl,
7856 DECL_ALIGN (decl) / BITS_PER_UNIT);
7857 else
7858 error ("alignment for %q+D must be at least %d", decl,
7859 DECL_ALIGN (decl) / BITS_PER_UNIT);
7860 *no_add_attrs = true;
7861 }
349ae713
NB
7862 else
7863 {
d9223014 7864 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
349ae713
NB
7865 DECL_USER_ALIGN (decl) = 1;
7866 }
7867
7868 return NULL_TREE;
7869}
7870
7871/* Handle a "weak" attribute; arguments as in
7872 struct attribute_spec.handler. */
7873
7874static tree
55af93a8 7875handle_weak_attribute (tree *node, tree name,
e18476eb
BI
7876 tree ARG_UNUSED (args),
7877 int ARG_UNUSED (flags),
7878 bool * ARG_UNUSED (no_add_attrs))
349ae713 7879{
55af93a8 7880 if (TREE_CODE (*node) == FUNCTION_DECL
6b4e94bc
RG
7881 && DECL_DECLARED_INLINE_P (*node))
7882 {
2aa9c6ae 7883 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6b4e94bc
RG
7884 *no_add_attrs = true;
7885 }
ba885ec5
NS
7886 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7887 {
7888 error ("indirect function %q+D cannot be declared weak", *node);
7889 *no_add_attrs = true;
7890 return NULL_TREE;
7891 }
6b4e94bc
RG
7892 else if (TREE_CODE (*node) == FUNCTION_DECL
7893 || TREE_CODE (*node) == VAR_DECL)
3cb251b7
JH
7894 {
7895 struct symtab_node *n = symtab_node::get (*node);
7896 if (n && n->refuse_visibility_changes)
7897 error ("%+D declared weak after being used", *node);
7898 declare_weak (*node);
7899 }
55af93a8
DS
7900 else
7901 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7902
7903 return NULL_TREE;
7904}
7905
ba885ec5
NS
7906/* Handle an "alias" or "ifunc" attribute; arguments as in
7907 struct attribute_spec.handler, except that IS_ALIAS tells us
7908 whether this is an alias as opposed to ifunc attribute. */
349ae713
NB
7909
7910static tree
ba885ec5
NS
7911handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7912 bool *no_add_attrs)
349ae713
NB
7913{
7914 tree decl = *node;
7915
ba885ec5
NS
7916 if (TREE_CODE (decl) != FUNCTION_DECL
7917 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
feab5a67
JM
7918 {
7919 warning (OPT_Wattributes, "%qE attribute ignored", name);
7920 *no_add_attrs = true;
7921 }
7922 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
b8698a0f 7923 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825
GK
7924 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7925 /* A static variable declaration is always a tentative definition,
7926 but the alias is a non-tentative definition which overrides. */
b8698a0f 7927 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825 7928 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
349ae713 7929 {
ba885ec5 7930 error ("%q+D defined both normally and as %qE attribute", decl, name);
349ae713 7931 *no_add_attrs = true;
ba885ec5 7932 return NULL_TREE;
349ae713 7933 }
ba885ec5
NS
7934 else if (!is_alias
7935 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7936 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7937 {
7938 error ("weak %q+D cannot be defined %qE", decl, name);
7939 *no_add_attrs = true;
7940 return NULL_TREE;
7941 }
f6a76b9f
RH
7942
7943 /* Note that the very first time we process a nested declaration,
7944 decl_function_context will not be set. Indeed, *would* never
7945 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7946 we do below. After such frobbery, pushdecl would set the context.
7947 In any case, this is never what we want. */
7948 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
349ae713
NB
7949 {
7950 tree id;
7951
7952 id = TREE_VALUE (args);
7953 if (TREE_CODE (id) != STRING_CST)
7954 {
ba885ec5 7955 error ("attribute %qE argument not a string", name);
349ae713
NB
7956 *no_add_attrs = true;
7957 return NULL_TREE;
7958 }
7959 id = get_identifier (TREE_STRING_POINTER (id));
7960 /* This counts as a use of the object pointed to. */
7961 TREE_USED (id) = 1;
7962
7963 if (TREE_CODE (decl) == FUNCTION_DECL)
7964 DECL_INITIAL (decl) = error_mark_node;
7965 else
08346abd 7966 TREE_STATIC (decl) = 1;
ba885ec5
NS
7967
7968 if (!is_alias)
7969 /* ifuncs are also aliases, so set that attribute too. */
7970 DECL_ATTRIBUTES (decl)
7971 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
349ae713
NB
7972 }
7973 else
7974 {
5c498b10 7975 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7976 *no_add_attrs = true;
7977 }
7978
f7217cde
JH
7979 if (decl_in_symtab_p (*node))
7980 {
7981 struct symtab_node *n = symtab_node::get (decl);
7982 if (n && n->refuse_visibility_changes)
7983 {
7984 if (is_alias)
7985 error ("%+D declared alias after being used", decl);
7986 else
7987 error ("%+D declared ifunc after being used", decl);
7988 }
7989 }
7990
7991
349ae713
NB
7992 return NULL_TREE;
7993}
7994
ba885ec5
NS
7995/* Handle an "alias" or "ifunc" attribute; arguments as in
7996 struct attribute_spec.handler. */
7997
7998static tree
7999handle_ifunc_attribute (tree *node, tree name, tree args,
8000 int ARG_UNUSED (flags), bool *no_add_attrs)
8001{
8002 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8003}
8004
8005/* Handle an "alias" or "ifunc" attribute; arguments as in
8006 struct attribute_spec.handler. */
8007
8008static tree
8009handle_alias_attribute (tree *node, tree name, tree args,
8010 int ARG_UNUSED (flags), bool *no_add_attrs)
8011{
8012 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8013}
8014
a0203ca7
AO
8015/* Handle a "weakref" attribute; arguments as in struct
8016 attribute_spec.handler. */
8017
8018static tree
8019handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8020 int flags, bool *no_add_attrs)
8021{
8022 tree attr = NULL_TREE;
8023
e1cf56b1
AO
8024 /* We must ignore the attribute when it is associated with
8025 local-scoped decls, since attribute alias is ignored and many
8026 such symbols do not even have a DECL_WEAK field. */
e7b012c0
JJ
8027 if (decl_function_context (*node)
8028 || current_function_decl
8029 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
e1cf56b1
AO
8030 {
8031 warning (OPT_Wattributes, "%qE attribute ignored", name);
8032 *no_add_attrs = true;
8033 return NULL_TREE;
8034 }
8035
ba885ec5
NS
8036 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8037 {
8038 error ("indirect function %q+D cannot be declared weakref", *node);
8039 *no_add_attrs = true;
8040 return NULL_TREE;
8041 }
8042
a0203ca7
AO
8043 /* The idea here is that `weakref("name")' mutates into `weakref,
8044 alias("name")', and weakref without arguments, in turn,
8045 implicitly adds weak. */
8046
8047 if (args)
8048 {
8049 attr = tree_cons (get_identifier ("alias"), args, attr);
8050 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8051
8052 *no_add_attrs = true;
a9b0b825
GK
8053
8054 decl_attributes (node, attr, flags);
a0203ca7
AO
8055 }
8056 else
8057 {
8058 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
c5d75364
MLI
8059 error_at (DECL_SOURCE_LOCATION (*node),
8060 "weakref attribute must appear before alias attribute");
a0203ca7 8061
a9b0b825
GK
8062 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8063 and that isn't supported; and because it wants to add it to
8064 the list of weak decls, which isn't helpful. */
8065 DECL_WEAK (*node) = 1;
a0203ca7
AO
8066 }
8067
f7217cde
JH
8068 if (decl_in_symtab_p (*node))
8069 {
8070 struct symtab_node *n = symtab_node::get (*node);
8071 if (n && n->refuse_visibility_changes)
8072 error ("%+D declared weakref after being used", *node);
8073 }
8074
a0203ca7
AO
8075 return NULL_TREE;
8076}
8077
349ae713
NB
8078/* Handle an "visibility" attribute; arguments as in
8079 struct attribute_spec.handler. */
8080
8081static tree
35b1a6fa 8082handle_visibility_attribute (tree *node, tree name, tree args,
e18476eb 8083 int ARG_UNUSED (flags),
b9e75696 8084 bool *ARG_UNUSED (no_add_attrs))
349ae713
NB
8085{
8086 tree decl = *node;
968b41a1 8087 tree id = TREE_VALUE (args);
b9e75696 8088 enum symbol_visibility vis;
349ae713 8089
d7afec4b
ND
8090 if (TYPE_P (*node))
8091 {
b9e75696
JM
8092 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8093 /* OK */;
8094 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8095 {
8096 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8097 name);
8098 return NULL_TREE;
8099 }
8100 else if (TYPE_FIELDS (*node))
8101 {
8102 error ("%qE attribute ignored because %qT is already defined",
8103 name, *node);
8104 return NULL_TREE;
8105 }
d7afec4b 8106 }
3f75a254 8107 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
349ae713 8108 {
5c498b10 8109 warning (OPT_Wattributes, "%qE attribute ignored", name);
968b41a1 8110 return NULL_TREE;
349ae713 8111 }
349ae713 8112
968b41a1
MA
8113 if (TREE_CODE (id) != STRING_CST)
8114 {
40b97a2e 8115 error ("visibility argument not a string");
968b41a1 8116 return NULL_TREE;
349ae713 8117 }
9f63daea 8118
d7afec4b
ND
8119 /* If this is a type, set the visibility on the type decl. */
8120 if (TYPE_P (decl))
8121 {
8122 decl = TYPE_NAME (decl);
3f75a254 8123 if (!decl)
c22cacf3 8124 return NULL_TREE;
e8233ac2
AP
8125 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8126 {
5c498b10 8127 warning (OPT_Wattributes, "%qE attribute ignored on types",
e8233ac2
AP
8128 name);
8129 return NULL_TREE;
8130 }
d7afec4b 8131 }
349ae713 8132
968b41a1 8133 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
b9e75696 8134 vis = VISIBILITY_DEFAULT;
968b41a1 8135 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
b9e75696 8136 vis = VISIBILITY_INTERNAL;
968b41a1 8137 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
b9e75696 8138 vis = VISIBILITY_HIDDEN;
968b41a1 8139 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
b9e75696 8140 vis = VISIBILITY_PROTECTED;
968b41a1 8141 else
b9e75696
JM
8142 {
8143 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8144 vis = VISIBILITY_DEFAULT;
8145 }
8146
8147 if (DECL_VISIBILITY_SPECIFIED (decl)
3a687f8b
MM
8148 && vis != DECL_VISIBILITY (decl))
8149 {
8150 tree attributes = (TYPE_P (*node)
8151 ? TYPE_ATTRIBUTES (*node)
8152 : DECL_ATTRIBUTES (decl));
8153 if (lookup_attribute ("visibility", attributes))
8154 error ("%qD redeclared with different visibility", decl);
8155 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8156 && lookup_attribute ("dllimport", attributes))
8157 error ("%qD was declared %qs which implies default visibility",
8158 decl, "dllimport");
8159 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8160 && lookup_attribute ("dllexport", attributes))
8161 error ("%qD was declared %qs which implies default visibility",
8162 decl, "dllexport");
8163 }
b9e75696
JM
8164
8165 DECL_VISIBILITY (decl) = vis;
d7afec4b
ND
8166 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8167
b9e75696
JM
8168 /* Go ahead and attach the attribute to the node as well. This is needed
8169 so we can determine whether we have VISIBILITY_DEFAULT because the
8170 visibility was not specified, or because it was explicitly overridden
8171 from the containing scope. */
968b41a1 8172
349ae713
NB
8173 return NULL_TREE;
8174}
8175
b2ca3702
MM
8176/* Determine the ELF symbol visibility for DECL, which is either a
8177 variable or a function. It is an error to use this function if a
8178 definition of DECL is not available in this translation unit.
8179 Returns true if the final visibility has been determined by this
8180 function; false if the caller is free to make additional
8181 modifications. */
8182
8183bool
8184c_determine_visibility (tree decl)
8185{
4094f4d2
NS
8186 gcc_assert (TREE_CODE (decl) == VAR_DECL
8187 || TREE_CODE (decl) == FUNCTION_DECL);
b2ca3702
MM
8188
8189 /* If the user explicitly specified the visibility with an
8190 attribute, honor that. DECL_VISIBILITY will have been set during
6d87092d
JM
8191 the processing of the attribute. We check for an explicit
8192 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8193 to distinguish the use of an attribute from the use of a "#pragma
8194 GCC visibility push(...)"; in the latter case we still want other
8195 considerations to be able to overrule the #pragma. */
8196 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8197 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8198 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8199 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
b2ca3702
MM
8200 return true;
8201
b9e75696
JM
8202 /* Set default visibility to whatever the user supplied with
8203 visibility_specified depending on #pragma GCC visibility. */
8204 if (!DECL_VISIBILITY_SPECIFIED (decl))
8205 {
09812622
JJ
8206 if (visibility_options.inpragma
8207 || DECL_VISIBILITY (decl) != default_visibility)
8208 {
8209 DECL_VISIBILITY (decl) = default_visibility;
8210 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8211 /* If visibility changed and DECL already has DECL_RTL, ensure
8212 symbol flags are updated. */
8213 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8214 || TREE_CODE (decl) == FUNCTION_DECL)
8215 && DECL_RTL_SET_P (decl))
8216 make_decl_rtl (decl);
8217 }
b9e75696 8218 }
b2ca3702
MM
8219 return false;
8220}
8221
dce81a1a
JJ
8222/* Handle an "tls_model" attribute; arguments as in
8223 struct attribute_spec.handler. */
8224
8225static tree
35b1a6fa 8226handle_tls_model_attribute (tree *node, tree name, tree args,
e18476eb 8227 int ARG_UNUSED (flags), bool *no_add_attrs)
dce81a1a 8228{
c2f7fa15 8229 tree id;
dce81a1a 8230 tree decl = *node;
c2f7fa15 8231 enum tls_model kind;
dce81a1a 8232
c2f7fa15
SB
8233 *no_add_attrs = true;
8234
5006381c 8235 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
dce81a1a 8236 {
5c498b10 8237 warning (OPT_Wattributes, "%qE attribute ignored", name);
c2f7fa15 8238 return NULL_TREE;
dce81a1a 8239 }
dce81a1a 8240
c2f7fa15
SB
8241 kind = DECL_TLS_MODEL (decl);
8242 id = TREE_VALUE (args);
8243 if (TREE_CODE (id) != STRING_CST)
8244 {
8245 error ("tls_model argument not a string");
8246 return NULL_TREE;
dce81a1a
JJ
8247 }
8248
c2f7fa15
SB
8249 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8250 kind = TLS_MODEL_LOCAL_EXEC;
8251 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8252 kind = TLS_MODEL_INITIAL_EXEC;
8253 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8254 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8255 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8256 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8257 else
8258 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8259
56363ffd 8260 set_decl_tls_model (decl, kind);
dce81a1a
JJ
8261 return NULL_TREE;
8262}
8263
349ae713
NB
8264/* Handle a "no_instrument_function" attribute; arguments as in
8265 struct attribute_spec.handler. */
8266
8267static tree
35b1a6fa 8268handle_no_instrument_function_attribute (tree *node, tree name,
e18476eb
BI
8269 tree ARG_UNUSED (args),
8270 int ARG_UNUSED (flags),
a742c759 8271 bool *no_add_attrs)
349ae713
NB
8272{
8273 tree decl = *node;
8274
8275 if (TREE_CODE (decl) != FUNCTION_DECL)
8276 {
c5d75364
MLI
8277 error_at (DECL_SOURCE_LOCATION (decl),
8278 "%qE attribute applies only to functions", name);
349ae713
NB
8279 *no_add_attrs = true;
8280 }
349ae713
NB
8281 else
8282 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8283
8284 return NULL_TREE;
8285}
8286
8287/* Handle a "malloc" attribute; arguments as in
8288 struct attribute_spec.handler. */
8289
8290static tree
e18476eb
BI
8291handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8292 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 8293{
3425638a
JM
8294 if (TREE_CODE (*node) == FUNCTION_DECL
8295 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
349ae713 8296 DECL_IS_MALLOC (*node) = 1;
349ae713
NB
8297 else
8298 {
5c498b10 8299 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8300 *no_add_attrs = true;
8301 }
8302
8303 return NULL_TREE;
8304}
8305
51bc54a6
DM
8306/* Handle a "alloc_size" attribute; arguments as in
8307 struct attribute_spec.handler. */
8308
8309static tree
8310handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8311 int ARG_UNUSED (flags), bool *no_add_attrs)
8312{
f3f75f69 8313 unsigned arg_count = type_num_arguments (*node);
51bc54a6
DM
8314 for (; args; args = TREE_CHAIN (args))
8315 {
8316 tree position = TREE_VALUE (args);
5d77fb19
MG
8317 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8318 && TREE_CODE (position) != FUNCTION_DECL)
8319 position = default_conversion (position);
51bc54a6 8320
8fcbce72
JJ
8321 if (!tree_fits_uhwi_p (position)
8322 || !arg_count
8323 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
51bc54a6 8324 {
b8698a0f 8325 warning (OPT_Wattributes,
51bc54a6
DM
8326 "alloc_size parameter outside range");
8327 *no_add_attrs = true;
8328 return NULL_TREE;
8329 }
8330 }
8331 return NULL_TREE;
8332}
8333
8fcbce72
JJ
8334/* Handle a "alloc_align" attribute; arguments as in
8335 struct attribute_spec.handler. */
8336
8337static tree
8338handle_alloc_align_attribute (tree *node, tree, tree args, int,
8339 bool *no_add_attrs)
8340{
8341 unsigned arg_count = type_num_arguments (*node);
8342 tree position = TREE_VALUE (args);
8343 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8344 position = default_conversion (position);
8345
8346 if (!tree_fits_uhwi_p (position)
8347 || !arg_count
8348 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8349 {
8350 warning (OPT_Wattributes,
8351 "alloc_align parameter outside range");
8352 *no_add_attrs = true;
8353 return NULL_TREE;
8354 }
8355 return NULL_TREE;
8356}
8357
8358/* Handle a "assume_aligned" attribute; arguments as in
8359 struct attribute_spec.handler. */
8360
8361static tree
8362handle_assume_aligned_attribute (tree *, tree, tree args, int,
8363 bool *no_add_attrs)
8364{
8365 for (; args; args = TREE_CHAIN (args))
8366 {
8367 tree position = TREE_VALUE (args);
8368 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8369 && TREE_CODE (position) != FUNCTION_DECL)
8370 position = default_conversion (position);
8371
8372 if (TREE_CODE (position) != INTEGER_CST)
8373 {
8374 warning (OPT_Wattributes,
8375 "assume_aligned parameter not integer constant");
8376 *no_add_attrs = true;
8377 return NULL_TREE;
8378 }
8379 }
8380 return NULL_TREE;
8381}
8382
0b7b376d
RG
8383/* Handle a "fn spec" attribute; arguments as in
8384 struct attribute_spec.handler. */
8385
8386static tree
8387handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8388 tree args, int ARG_UNUSED (flags),
8389 bool *no_add_attrs ATTRIBUTE_UNUSED)
8390{
8391 gcc_assert (args
8392 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8393 && !TREE_CHAIN (args));
8394 return NULL_TREE;
8395}
8396
d5e254e1
IE
8397/* Handle a "bnd_variable_size" attribute; arguments as in
8398 struct attribute_spec.handler. */
8399
8400static tree
8401handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8402 int ARG_UNUSED (flags), bool *no_add_attrs)
8403{
8404 if (TREE_CODE (*node) != FIELD_DECL)
8405 {
8406 warning (OPT_Wattributes, "%qE attribute ignored", name);
8407 *no_add_attrs = true;
8408 }
8409
8410 return NULL_TREE;
8411}
8412
8413/* Handle a "bnd_legacy" attribute; arguments as in
8414 struct attribute_spec.handler. */
8415
8416static tree
8417handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8418 int ARG_UNUSED (flags), bool *no_add_attrs)
8419{
8420 if (TREE_CODE (*node) != FUNCTION_DECL)
8421 {
8422 warning (OPT_Wattributes, "%qE attribute ignored", name);
8423 *no_add_attrs = true;
8424 }
8425
8426 return NULL_TREE;
8427}
8428
8429/* Handle a "bnd_instrument" attribute; arguments as in
8430 struct attribute_spec.handler. */
8431
8432static tree
8433handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8434 int ARG_UNUSED (flags), bool *no_add_attrs)
8435{
8436 if (TREE_CODE (*node) != FUNCTION_DECL)
8437 {
8438 warning (OPT_Wattributes, "%qE attribute ignored", name);
8439 *no_add_attrs = true;
8440 }
8441
8442 return NULL_TREE;
8443}
8444
2a99e5e6
LL
8445/* Handle a "warn_unused" attribute; arguments as in
8446 struct attribute_spec.handler. */
8447
8448static tree
8449handle_warn_unused_attribute (tree *node, tree name,
8450 tree args ATTRIBUTE_UNUSED,
8451 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8452{
8453 if (TYPE_P (*node))
8454 /* Do nothing else, just set the attribute. We'll get at
8455 it later with lookup_attribute. */
8456 ;
8457 else
8458 {
8459 warning (OPT_Wattributes, "%qE attribute ignored", name);
8460 *no_add_attrs = true;
8461 }
8462
8463 return NULL_TREE;
8464}
8465
acf0174b
JJ
8466/* Handle an "omp declare simd" attribute; arguments as in
8467 struct attribute_spec.handler. */
8468
8469static tree
8470handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8471{
8472 return NULL_TREE;
8473}
8474
8475/* Handle an "omp declare target" attribute; arguments as in
8476 struct attribute_spec.handler. */
8477
8478static tree
8479handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8480{
8481 return NULL_TREE;
8482}
8483
6e9a3221
AN
8484/* Handle a "returns_twice" attribute; arguments as in
8485 struct attribute_spec.handler. */
8486
8487static tree
8488handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8489 int ARG_UNUSED (flags), bool *no_add_attrs)
8490{
8491 if (TREE_CODE (*node) == FUNCTION_DECL)
8492 DECL_IS_RETURNS_TWICE (*node) = 1;
8493 else
8494 {
5c498b10 8495 warning (OPT_Wattributes, "%qE attribute ignored", name);
6e9a3221
AN
8496 *no_add_attrs = true;
8497 }
8498
8499 return NULL_TREE;
8500}
8501
349ae713
NB
8502/* Handle a "no_limit_stack" attribute; arguments as in
8503 struct attribute_spec.handler. */
8504
8505static tree
35b1a6fa 8506handle_no_limit_stack_attribute (tree *node, tree name,
e18476eb
BI
8507 tree ARG_UNUSED (args),
8508 int ARG_UNUSED (flags),
a742c759 8509 bool *no_add_attrs)
349ae713
NB
8510{
8511 tree decl = *node;
8512
8513 if (TREE_CODE (decl) != FUNCTION_DECL)
8514 {
c5d75364
MLI
8515 error_at (DECL_SOURCE_LOCATION (decl),
8516 "%qE attribute applies only to functions", name);
349ae713
NB
8517 *no_add_attrs = true;
8518 }
8519 else if (DECL_INITIAL (decl))
8520 {
c5d75364
MLI
8521 error_at (DECL_SOURCE_LOCATION (decl),
8522 "can%'t set %qE attribute after definition", name);
349ae713
NB
8523 *no_add_attrs = true;
8524 }
8525 else
8526 DECL_NO_LIMIT_STACK (decl) = 1;
8527
8528 return NULL_TREE;
8529}
8530
8531/* Handle a "pure" attribute; arguments as in
8532 struct attribute_spec.handler. */
8533
8534static tree
e18476eb
BI
8535handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8536 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
8537{
8538 if (TREE_CODE (*node) == FUNCTION_DECL)
becfd6e5 8539 DECL_PURE_P (*node) = 1;
349ae713
NB
8540 /* ??? TODO: Support types. */
8541 else
8542 {
5c498b10 8543 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8544 *no_add_attrs = true;
8545 }
8546
8547 return NULL_TREE;
8548}
8549
0a35513e
AH
8550/* Digest an attribute list destined for a transactional memory statement.
8551 ALLOWED is the set of attributes that are allowed for this statement;
8552 return the attribute we parsed. Multiple attributes are never allowed. */
8553
8554int
8555parse_tm_stmt_attr (tree attrs, int allowed)
8556{
8557 tree a_seen = NULL;
8558 int m_seen = 0;
8559
8560 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8561 {
8562 tree a = TREE_PURPOSE (attrs);
8563 int m = 0;
8564
8565 if (is_attribute_p ("outer", a))
8566 m = TM_STMT_ATTR_OUTER;
8567
8568 if ((m & allowed) == 0)
8569 {
8570 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8571 continue;
8572 }
8573
8574 if (m_seen == 0)
8575 {
8576 a_seen = a;
8577 m_seen = m;
8578 }
8579 else if (m_seen == m)
8580 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8581 else
8582 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8583 }
8584
8585 return m_seen;
8586}
8587
8588/* Transform a TM attribute name into a maskable integer and back.
8589 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8590 to how the lack of an attribute is treated. */
8591
8592int
8593tm_attr_to_mask (tree attr)
8594{
8595 if (attr == NULL)
8596 return 0;
8597 if (is_attribute_p ("transaction_safe", attr))
8598 return TM_ATTR_SAFE;
8599 if (is_attribute_p ("transaction_callable", attr))
8600 return TM_ATTR_CALLABLE;
8601 if (is_attribute_p ("transaction_pure", attr))
8602 return TM_ATTR_PURE;
8603 if (is_attribute_p ("transaction_unsafe", attr))
8604 return TM_ATTR_IRREVOCABLE;
8605 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8606 return TM_ATTR_MAY_CANCEL_OUTER;
8607 return 0;
8608}
8609
8610tree
8611tm_mask_to_attr (int mask)
8612{
8613 const char *str;
8614 switch (mask)
8615 {
8616 case TM_ATTR_SAFE:
8617 str = "transaction_safe";
8618 break;
8619 case TM_ATTR_CALLABLE:
8620 str = "transaction_callable";
8621 break;
8622 case TM_ATTR_PURE:
8623 str = "transaction_pure";
8624 break;
8625 case TM_ATTR_IRREVOCABLE:
8626 str = "transaction_unsafe";
8627 break;
8628 case TM_ATTR_MAY_CANCEL_OUTER:
8629 str = "transaction_may_cancel_outer";
8630 break;
8631 default:
8632 gcc_unreachable ();
8633 }
8634 return get_identifier (str);
8635}
8636
8637/* Return the first TM attribute seen in LIST. */
8638
8639tree
8640find_tm_attribute (tree list)
8641{
8642 for (; list ; list = TREE_CHAIN (list))
8643 {
8644 tree name = TREE_PURPOSE (list);
8645 if (tm_attr_to_mask (name) != 0)
8646 return name;
8647 }
8648 return NULL_TREE;
8649}
8650
8651/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8652 Here we accept only function types, and verify that none of the other
8653 function TM attributes are also applied. */
8654/* ??? We need to accept class types for C++, but not C. This greatly
8655 complicates this function, since we can no longer rely on the extra
8656 processing given by function_type_required. */
8657
8658static tree
8659handle_tm_attribute (tree *node, tree name, tree args,
8660 int flags, bool *no_add_attrs)
8661{
8662 /* Only one path adds the attribute; others don't. */
8663 *no_add_attrs = true;
8664
8665 switch (TREE_CODE (*node))
8666 {
8667 case RECORD_TYPE:
8668 case UNION_TYPE:
8669 /* Only tm_callable and tm_safe apply to classes. */
8670 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8671 goto ignored;
8672 /* FALLTHRU */
8673
8674 case FUNCTION_TYPE:
8675 case METHOD_TYPE:
8676 {
8677 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8678 if (old_name == name)
8679 ;
8680 else if (old_name != NULL_TREE)
8681 error ("type was previously declared %qE", old_name);
8682 else
8683 *no_add_attrs = false;
8684 }
8685 break;
8686
8687 case POINTER_TYPE:
8688 {
8689 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8690 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8691 {
8692 tree fn_tmp = TREE_TYPE (*node);
8693 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8694 *node = build_pointer_type (fn_tmp);
8695 break;
8696 }
8697 }
8698 /* FALLTHRU */
8699
8700 default:
8701 /* If a function is next, pass it on to be tried next. */
8702 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8703 return tree_cons (name, args, NULL);
8704
8705 ignored:
8706 warning (OPT_Wattributes, "%qE attribute ignored", name);
8707 break;
8708 }
8709
8710 return NULL_TREE;
8711}
8712
8713/* Handle the TM_WRAP attribute; arguments as in
8714 struct attribute_spec.handler. */
8715
8716static tree
8717handle_tm_wrap_attribute (tree *node, tree name, tree args,
8718 int ARG_UNUSED (flags), bool *no_add_attrs)
8719{
8720 tree decl = *node;
8721
8722 /* We don't need the attribute even on success, since we
8723 record the entry in an external table. */
8724 *no_add_attrs = true;
8725
8726 if (TREE_CODE (decl) != FUNCTION_DECL)
8727 warning (OPT_Wattributes, "%qE attribute ignored", name);
8728 else
8729 {
8730 tree wrap_decl = TREE_VALUE (args);
661a0813
MP
8731 if (error_operand_p (wrap_decl))
8732 ;
8733 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8734 && TREE_CODE (wrap_decl) != VAR_DECL
8735 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
0a35513e
AH
8736 error ("%qE argument not an identifier", name);
8737 else
8738 {
8739 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8740 wrap_decl = lookup_name (wrap_decl);
8741 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8742 {
8743 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8744 TREE_TYPE (wrap_decl)))
8745 record_tm_replacement (wrap_decl, decl);
8746 else
8747 error ("%qD is not compatible with %qD", wrap_decl, decl);
8748 }
8749 else
c07d7c02 8750 error ("%qE argument is not a function", name);
0a35513e
AH
8751 }
8752 }
8753
8754 return NULL_TREE;
8755}
8756
8757/* Ignore the given attribute. Used when this attribute may be usefully
8758 overridden by the target, but is not used generically. */
8759
8760static tree
8761ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8762 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8763 bool *no_add_attrs)
8764{
8765 *no_add_attrs = true;
8766 return NULL_TREE;
8767}
8768
dcd6de6d
ZD
8769/* Handle a "no vops" attribute; arguments as in
8770 struct attribute_spec.handler. */
8771
8772static tree
8773handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8774 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8775 bool *ARG_UNUSED (no_add_attrs))
8776{
8777 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8778 DECL_IS_NOVOPS (*node) = 1;
8779 return NULL_TREE;
8780}
8781
349ae713
NB
8782/* Handle a "deprecated" attribute; arguments as in
8783 struct attribute_spec.handler. */
35b1a6fa 8784
349ae713 8785static tree
35b1a6fa 8786handle_deprecated_attribute (tree *node, tree name,
9b86d6bb 8787 tree args, int flags,
a742c759 8788 bool *no_add_attrs)
349ae713
NB
8789{
8790 tree type = NULL_TREE;
8791 int warn = 0;
c51a1ba9 8792 tree what = NULL_TREE;
35b1a6fa 8793
9b86d6bb
L
8794 if (!args)
8795 *no_add_attrs = true;
8796 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8797 {
8798 error ("deprecated message is not a string");
8799 *no_add_attrs = true;
8800 }
8801
349ae713
NB
8802 if (DECL_P (*node))
8803 {
8804 tree decl = *node;
8805 type = TREE_TYPE (decl);
35b1a6fa 8806
349ae713
NB
8807 if (TREE_CODE (decl) == TYPE_DECL
8808 || TREE_CODE (decl) == PARM_DECL
8809 || TREE_CODE (decl) == VAR_DECL
8810 || TREE_CODE (decl) == FUNCTION_DECL
a1178b30
IS
8811 || TREE_CODE (decl) == FIELD_DECL
8812 || objc_method_decl (TREE_CODE (decl)))
349ae713
NB
8813 TREE_DEPRECATED (decl) = 1;
8814 else
8815 warn = 1;
8816 }
8817 else if (TYPE_P (*node))
8818 {
8819 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 8820 *node = build_variant_type_copy (*node);
349ae713
NB
8821 TREE_DEPRECATED (*node) = 1;
8822 type = *node;
8823 }
8824 else
8825 warn = 1;
35b1a6fa 8826
349ae713
NB
8827 if (warn)
8828 {
8829 *no_add_attrs = true;
8830 if (type && TYPE_NAME (type))
8831 {
8832 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9 8833 what = TYPE_NAME (*node);
349ae713
NB
8834 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8835 && DECL_NAME (TYPE_NAME (type)))
c51a1ba9 8836 what = DECL_NAME (TYPE_NAME (type));
349ae713
NB
8837 }
8838 if (what)
5c498b10 8839 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
349ae713 8840 else
5c498b10 8841 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8842 }
8843
8844 return NULL_TREE;
8845}
8846
349ae713
NB
8847/* Handle a "vector_size" attribute; arguments as in
8848 struct attribute_spec.handler. */
8849
8850static tree
35b1a6fa 8851handle_vector_size_attribute (tree *node, tree name, tree args,
e18476eb 8852 int ARG_UNUSED (flags),
a742c759 8853 bool *no_add_attrs)
349ae713
NB
8854{
8855 unsigned HOST_WIDE_INT vecsize, nunits;
ef4bddc2 8856 machine_mode orig_mode;
4a5eab38 8857 tree type = *node, new_type, size;
349ae713
NB
8858
8859 *no_add_attrs = true;
8860
4a5eab38 8861 size = TREE_VALUE (args);
661a0813
MP
8862 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8863 && TREE_CODE (size) != FUNCTION_DECL)
5d77fb19 8864 size = default_conversion (size);
4a5eab38 8865
cc269bb6 8866 if (!tree_fits_uhwi_p (size))
349ae713 8867 {
5c498b10 8868 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8869 return NULL_TREE;
8870 }
8871
8872 /* Get the vector size (in bytes). */
ae7e9ddd 8873 vecsize = tree_to_uhwi (size);
349ae713
NB
8874
8875 /* We need to provide for vector pointers, vector arrays, and
8876 functions returning vectors. For example:
8877
8878 __attribute__((vector_size(16))) short *foo;
8879
8880 In this case, the mode is SI, but the type being modified is
8881 HI, so we need to look further. */
8882
8883 while (POINTER_TYPE_P (type)
8884 || TREE_CODE (type) == FUNCTION_TYPE
43dc123f 8885 || TREE_CODE (type) == METHOD_TYPE
270e749d
JJ
8886 || TREE_CODE (type) == ARRAY_TYPE
8887 || TREE_CODE (type) == OFFSET_TYPE)
349ae713
NB
8888 type = TREE_TYPE (type);
8889
8890 /* Get the mode of the type being modified. */
8891 orig_mode = TYPE_MODE (type);
8892
270e749d
JJ
8893 if ((!INTEGRAL_TYPE_P (type)
8894 && !SCALAR_FLOAT_TYPE_P (type)
8895 && !FIXED_POINT_TYPE_P (type))
3d8bf70f 8896 || (!SCALAR_FLOAT_MODE_P (orig_mode)
ab22c1fa
CF
8897 && GET_MODE_CLASS (orig_mode) != MODE_INT
8898 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
cc269bb6 8899 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
e4e5261f 8900 || TREE_CODE (type) == BOOLEAN_TYPE)
349ae713 8901 {
c51a1ba9 8902 error ("invalid vector type for attribute %qE", name);
349ae713
NB
8903 return NULL_TREE;
8904 }
8905
ae7e9ddd 8906 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
ee8960e5
JJ
8907 {
8908 error ("vector size not an integral multiple of component size");
8909 return NULL;
8910 }
8911
8912 if (vecsize == 0)
8913 {
8914 error ("zero vector size");
8915 return NULL;
8916 }
8917
349ae713 8918 /* Calculate how many units fit in the vector. */
ae7e9ddd 8919 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
26277d41 8920 if (nunits & (nunits - 1))
349ae713 8921 {
26277d41 8922 error ("number of components of the vector not a power of two");
349ae713
NB
8923 return NULL_TREE;
8924 }
8925
26277d41 8926 new_type = build_vector_type (type, nunits);
349ae713
NB
8927
8928 /* Build back pointers if needed. */
5dc11954 8929 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
349ae713
NB
8930
8931 return NULL_TREE;
8932}
8933
b34c7881
JT
8934/* Handle the "nonnull" attribute. */
8935static tree
e18476eb
BI
8936handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8937 tree args, int ARG_UNUSED (flags),
a742c759 8938 bool *no_add_attrs)
b34c7881
JT
8939{
8940 tree type = *node;
8941 unsigned HOST_WIDE_INT attr_arg_num;
8942
8943 /* If no arguments are specified, all pointer arguments should be
95bd1dd7 8944 non-null. Verify a full prototype is given so that the arguments
b34c7881 8945 will have the correct types when we actually check them later. */
3f75a254 8946 if (!args)
b34c7881 8947 {
f4da8dce 8948 if (!prototype_p (type))
b34c7881
JT
8949 {
8950 error ("nonnull attribute without arguments on a non-prototype");
6de9cd9a 8951 *no_add_attrs = true;
b34c7881
JT
8952 }
8953 return NULL_TREE;
8954 }
8955
8956 /* Argument list specified. Verify that each argument number references
8957 a pointer argument. */
5d77fb19 8958 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
b34c7881 8959 {
6de9cd9a 8960 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
b34c7881 8961
5d77fb19
MG
8962 tree arg = TREE_VALUE (args);
8963 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8964 && TREE_CODE (arg) != FUNCTION_DECL)
8965 arg = default_conversion (arg);
8966
8967 if (!get_nonnull_operand (arg, &arg_num))
b34c7881 8968 {
40b97a2e 8969 error ("nonnull argument has invalid operand number (argument %lu)",
b34c7881
JT
8970 (unsigned long) attr_arg_num);
8971 *no_add_attrs = true;
8972 return NULL_TREE;
8973 }
8974
e19a18d4 8975 if (prototype_p (type))
b34c7881 8976 {
e19a18d4
NF
8977 function_args_iterator iter;
8978 tree argument;
8979
8980 function_args_iter_init (&iter, type);
8981 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
b34c7881 8982 {
e19a18d4
NF
8983 argument = function_args_iter_cond (&iter);
8984 if (argument == NULL_TREE || ck_num == arg_num)
b34c7881 8985 break;
b34c7881
JT
8986 }
8987
3f75a254 8988 if (!argument
e19a18d4 8989 || TREE_CODE (argument) == VOID_TYPE)
b34c7881 8990 {
40b97a2e 8991 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
b34c7881
JT
8992 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8993 *no_add_attrs = true;
8994 return NULL_TREE;
8995 }
8996
e19a18d4 8997 if (TREE_CODE (argument) != POINTER_TYPE)
b34c7881 8998 {
40b97a2e 8999 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
b34c7881
JT
9000 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9001 *no_add_attrs = true;
9002 return NULL_TREE;
9003 }
9004 }
9005 }
9006
9007 return NULL_TREE;
9008}
9009
9010/* Check the argument list of a function call for null in argument slots
94a0dd7b
SL
9011 that are marked as requiring a non-null pointer argument. The NARGS
9012 arguments are passed in the array ARGARRAY.
9013*/
b34c7881
JT
9014
9015static void
94a0dd7b 9016check_function_nonnull (tree attrs, int nargs, tree *argarray)
b34c7881 9017{
332f1d24 9018 tree a;
94a0dd7b 9019 int i;
b34c7881 9020
332f1d24
JJ
9021 attrs = lookup_attribute ("nonnull", attrs);
9022 if (attrs == NULL_TREE)
9023 return;
9024
9025 a = attrs;
9026 /* See if any of the nonnull attributes has no arguments. If so,
9027 then every pointer argument is checked (in which case the check
9028 for pointer type is done in check_nonnull_arg). */
9029 if (TREE_VALUE (a) != NULL_TREE)
9030 do
9031 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9032 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9033
9034 if (a != NULL_TREE)
9035 for (i = 0; i < nargs; i++)
9036 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9037 i + 1);
9038 else
b34c7881 9039 {
332f1d24
JJ
9040 /* Walk the argument list. If we encounter an argument number we
9041 should check for non-null, do it. */
9042 for (i = 0; i < nargs; i++)
b34c7881 9043 {
332f1d24 9044 for (a = attrs; ; a = TREE_CHAIN (a))
6de9cd9a 9045 {
332f1d24
JJ
9046 a = lookup_attribute ("nonnull", a);
9047 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9048 break;
6de9cd9a 9049 }
332f1d24
JJ
9050
9051 if (a != NULL_TREE)
9052 check_function_arguments_recurse (check_nonnull_arg, NULL,
9053 argarray[i], i + 1);
b34c7881
JT
9054 }
9055 }
9056}
9057
254986c7 9058/* Check that the Nth argument of a function call (counting backwards
94a0dd7b
SL
9059 from the end) is a (pointer)0. The NARGS arguments are passed in the
9060 array ARGARRAY. */
3d091dac
KG
9061
9062static void
dde05067 9063check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
3d091dac 9064{
dde05067 9065 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
3d091dac
KG
9066
9067 if (attr)
9068 {
94a0dd7b
SL
9069 int len = 0;
9070 int pos = 0;
9071 tree sentinel;
dde05067
NF
9072 function_args_iterator iter;
9073 tree t;
c22cacf3 9074
94a0dd7b 9075 /* Skip over the named arguments. */
dde05067 9076 FOREACH_FUNCTION_ARGS (fntype, t, iter)
c22cacf3 9077 {
dde05067
NF
9078 if (len == nargs)
9079 break;
94a0dd7b
SL
9080 len++;
9081 }
254986c7 9082
94a0dd7b
SL
9083 if (TREE_VALUE (attr))
9084 {
9085 tree p = TREE_VALUE (TREE_VALUE (attr));
9086 pos = TREE_INT_CST_LOW (p);
9087 }
254986c7 9088
94a0dd7b
SL
9089 /* The sentinel must be one of the varargs, i.e.
9090 in position >= the number of fixed arguments. */
9091 if ((nargs - 1 - pos) < len)
9092 {
7332899a 9093 warning (OPT_Wformat_,
94a0dd7b
SL
9094 "not enough variable arguments to fit a sentinel");
9095 return;
3d091dac 9096 }
94a0dd7b
SL
9097
9098 /* Validate the sentinel. */
9099 sentinel = argarray[nargs - 1 - pos];
9100 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9101 || !integer_zerop (sentinel))
9102 /* Although __null (in C++) is only an integer we allow it
9103 nevertheless, as we are guaranteed that it's exactly
9104 as wide as a pointer, and we don't want to force
9105 users to cast the NULL they have written there.
9106 We warn with -Wstrict-null-sentinel, though. */
9107 && (warn_strict_null_sentinel || null_node != sentinel))
7332899a 9108 warning (OPT_Wformat_, "missing sentinel in function call");
3d091dac
KG
9109 }
9110}
9111
b34c7881
JT
9112/* Helper for check_function_nonnull; given a list of operands which
9113 must be non-null in ARGS, determine if operand PARAM_NUM should be
9114 checked. */
9115
9116static bool
35b1a6fa 9117nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
b34c7881 9118{
6de9cd9a 9119 unsigned HOST_WIDE_INT arg_num = 0;
b34c7881
JT
9120
9121 for (; args; args = TREE_CHAIN (args))
9122 {
366de0ce
NS
9123 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9124
9125 gcc_assert (found);
b34c7881
JT
9126
9127 if (arg_num == param_num)
9128 return true;
9129 }
9130 return false;
9131}
9132
9133/* Check that the function argument PARAM (which is operand number
9134 PARAM_NUM) is non-null. This is called by check_function_nonnull
9135 via check_function_arguments_recurse. */
9136
9137static void
e18476eb 9138check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
35b1a6fa 9139 unsigned HOST_WIDE_INT param_num)
b34c7881
JT
9140{
9141 /* Just skip checking the argument if it's not a pointer. This can
9142 happen if the "nonnull" attribute was given without an operand
9143 list (which means to check every pointer argument). */
9144
9145 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9146 return;
9147
9148 if (integer_zerop (param))
30480ec4
DD
9149 warning (OPT_Wnonnull, "null argument where non-null required "
9150 "(argument %lu)", (unsigned long) param_num);
b34c7881
JT
9151}
9152
9153/* Helper for nonnull attribute handling; fetch the operand number
9154 from the attribute argument list. */
9155
9156static bool
35b1a6fa 9157get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
b34c7881 9158{
807e902e
KZ
9159 /* Verify the arg number is a small constant. */
9160 if (tree_fits_uhwi_p (arg_num_expr))
9161 {
9162 *valp = TREE_INT_CST_LOW (arg_num_expr);
9163 return true;
9164 }
9165 else
b34c7881 9166 return false;
b34c7881 9167}
39f2f3c8
RS
9168
9169/* Handle a "nothrow" attribute; arguments as in
9170 struct attribute_spec.handler. */
9171
9172static tree
e18476eb
BI
9173handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9174 int ARG_UNUSED (flags), bool *no_add_attrs)
39f2f3c8
RS
9175{
9176 if (TREE_CODE (*node) == FUNCTION_DECL)
9177 TREE_NOTHROW (*node) = 1;
9178 /* ??? TODO: Support types. */
9179 else
9180 {
5c498b10 9181 warning (OPT_Wattributes, "%qE attribute ignored", name);
39f2f3c8
RS
9182 *no_add_attrs = true;
9183 }
9184
9185 return NULL_TREE;
9186}
0bfa5f65
RH
9187
9188/* Handle a "cleanup" attribute; arguments as in
9189 struct attribute_spec.handler. */
9190
9191static tree
35b1a6fa 9192handle_cleanup_attribute (tree *node, tree name, tree args,
e18476eb 9193 int ARG_UNUSED (flags), bool *no_add_attrs)
0bfa5f65
RH
9194{
9195 tree decl = *node;
9196 tree cleanup_id, cleanup_decl;
9197
9198 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9199 for global destructors in C++. This requires infrastructure that
9200 we don't have generically at the moment. It's also not a feature
9201 we'd be missing too much, since we do have attribute constructor. */
9202 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9203 {
5c498b10 9204 warning (OPT_Wattributes, "%qE attribute ignored", name);
0bfa5f65
RH
9205 *no_add_attrs = true;
9206 return NULL_TREE;
9207 }
9208
9209 /* Verify that the argument is a function in scope. */
9210 /* ??? We could support pointers to functions here as well, if
9211 that was considered desirable. */
9212 cleanup_id = TREE_VALUE (args);
9213 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9214 {
40b97a2e 9215 error ("cleanup argument not an identifier");
0bfa5f65
RH
9216 *no_add_attrs = true;
9217 return NULL_TREE;
9218 }
10e6657a 9219 cleanup_decl = lookup_name (cleanup_id);
0bfa5f65
RH
9220 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9221 {
40b97a2e 9222 error ("cleanup argument not a function");
0bfa5f65
RH
9223 *no_add_attrs = true;
9224 return NULL_TREE;
9225 }
9226
35b1a6fa 9227 /* That the function has proper type is checked with the
0bfa5f65
RH
9228 eventual call to build_function_call. */
9229
9230 return NULL_TREE;
9231}
72954a4f
JM
9232
9233/* Handle a "warn_unused_result" attribute. No special handling. */
9234
9235static tree
9236handle_warn_unused_result_attribute (tree *node, tree name,
e18476eb
BI
9237 tree ARG_UNUSED (args),
9238 int ARG_UNUSED (flags), bool *no_add_attrs)
72954a4f
JM
9239{
9240 /* Ignore the attribute for functions not returning any value. */
9241 if (VOID_TYPE_P (TREE_TYPE (*node)))
9242 {
5c498b10 9243 warning (OPT_Wattributes, "%qE attribute ignored", name);
72954a4f
JM
9244 *no_add_attrs = true;
9245 }
9246
9247 return NULL_TREE;
9248}
3d091dac
KG
9249
9250/* Handle a "sentinel" attribute. */
9251
9252static tree
254986c7 9253handle_sentinel_attribute (tree *node, tree name, tree args,
3d091dac
KG
9254 int ARG_UNUSED (flags), bool *no_add_attrs)
9255{
f4da8dce 9256 if (!prototype_p (*node))
3d091dac 9257 {
5c498b10
DD
9258 warning (OPT_Wattributes,
9259 "%qE attribute requires prototypes with named arguments", name);
3d091dac 9260 *no_add_attrs = true;
3d091dac 9261 }
254986c7
KG
9262 else
9263 {
dcf0c47e 9264 if (!stdarg_p (*node))
c22cacf3 9265 {
5c498b10
DD
9266 warning (OPT_Wattributes,
9267 "%qE attribute only applies to variadic functions", name);
254986c7
KG
9268 *no_add_attrs = true;
9269 }
9270 }
c22cacf3 9271
254986c7 9272 if (args)
3d091dac 9273 {
254986c7 9274 tree position = TREE_VALUE (args);
661a0813
MP
9275 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9276 && TREE_CODE (position) != FUNCTION_DECL)
9277 position = default_conversion (position);
254986c7 9278
661a0813
MP
9279 if (TREE_CODE (position) != INTEGER_CST
9280 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
c22cacf3 9281 {
b8698a0f 9282 warning (OPT_Wattributes,
aa86a51b 9283 "requested position is not an integer constant");
254986c7
KG
9284 *no_add_attrs = true;
9285 }
9286 else
c22cacf3 9287 {
254986c7
KG
9288 if (tree_int_cst_lt (position, integer_zero_node))
9289 {
aa86a51b
DM
9290 warning (OPT_Wattributes,
9291 "requested position is less than zero");
254986c7
KG
9292 *no_add_attrs = true;
9293 }
9294 }
3d091dac 9295 }
c22cacf3 9296
3d091dac
KG
9297 return NULL_TREE;
9298}
b5d32c25
KG
9299
9300/* Handle a "type_generic" attribute. */
9301
9302static tree
9303handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9304 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9305 bool * ARG_UNUSED (no_add_attrs))
9306{
3bf5906b
KG
9307 /* Ensure we have a function type. */
9308 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
b8698a0f 9309
3bf5906b 9310 /* Ensure we have a variadic function. */
dcf0c47e 9311 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5d32c25
KG
9312
9313 return NULL_TREE;
9314}
ab442df7 9315
5779e713 9316/* Handle a "target" attribute. */
ab442df7
MM
9317
9318static tree
5779e713 9319handle_target_attribute (tree *node, tree name, tree args, int flags,
ab442df7
MM
9320 bool *no_add_attrs)
9321{
9322 /* Ensure we have a function type. */
9323 if (TREE_CODE (*node) != FUNCTION_DECL)
9324 {
9325 warning (OPT_Wattributes, "%qE attribute ignored", name);
9326 *no_add_attrs = true;
9327 }
ab442df7 9328 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
32887460 9329 flags))
ab442df7
MM
9330 *no_add_attrs = true;
9331
9332 return NULL_TREE;
9333}
9334
9335/* Arguments being collected for optimization. */
9336typedef const char *const_char_p; /* For DEF_VEC_P. */
9771b263 9337static GTY(()) vec<const_char_p, va_gc> *optimize_args;
ab442df7
MM
9338
9339
9340/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9341 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9342 false for #pragma GCC optimize. */
9343
9344bool
9345parse_optimize_options (tree args, bool attr_p)
9346{
9347 bool ret = true;
9348 unsigned opt_argc;
9349 unsigned i;
cbc19f39 9350 int saved_flag_strict_aliasing;
ab442df7 9351 const char **opt_argv;
6e2f1956
JM
9352 struct cl_decoded_option *decoded_options;
9353 unsigned int decoded_options_count;
ab442df7
MM
9354 tree ap;
9355
9356 /* Build up argv vector. Just in case the string is stored away, use garbage
9357 collected strings. */
9771b263
DN
9358 vec_safe_truncate (optimize_args, 0);
9359 vec_safe_push (optimize_args, (const char *) NULL);
ab442df7
MM
9360
9361 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9362 {
9363 tree value = TREE_VALUE (ap);
9364
9365 if (TREE_CODE (value) == INTEGER_CST)
9366 {
9367 char buffer[20];
9368 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9771b263 9369 vec_safe_push (optimize_args, ggc_strdup (buffer));
ab442df7
MM
9370 }
9371
9372 else if (TREE_CODE (value) == STRING_CST)
9373 {
9374 /* Split string into multiple substrings. */
9375 size_t len = TREE_STRING_LENGTH (value);
9376 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9377 char *end = p + len;
9378 char *comma;
9379 char *next_p = p;
9380
9381 while (next_p != NULL)
9382 {
9383 size_t len2;
9384 char *q, *r;
9385
9386 p = next_p;
9387 comma = strchr (p, ',');
9388 if (comma)
9389 {
9390 len2 = comma - p;
9391 *comma = '\0';
9392 next_p = comma+1;
9393 }
9394 else
9395 {
9396 len2 = end - p;
9397 next_p = NULL;
9398 }
9399
a9429e29 9400 r = q = (char *) ggc_alloc_atomic (len2 + 3);
ab442df7
MM
9401
9402 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9403 options. */
9404 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9405 {
9406 ret = false;
9407 if (attr_p)
9408 warning (OPT_Wattributes,
06730c5d 9409 "bad option %s to optimize attribute", p);
ab442df7
MM
9410 else
9411 warning (OPT_Wpragmas,
de621752 9412 "bad option %s to pragma attribute", p);
ab442df7
MM
9413 continue;
9414 }
9415
9416 if (*p != '-')
9417 {
9418 *r++ = '-';
9419
9420 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9421 itself is -Os, and any other switch begins with a -f. */
9422 if ((*p >= '0' && *p <= '9')
9423 || (p[0] == 's' && p[1] == '\0'))
9424 *r++ = 'O';
9425 else if (*p != 'O')
9426 *r++ = 'f';
9427 }
9428
9429 memcpy (r, p, len2);
9430 r[len2] = '\0';
9771b263 9431 vec_safe_push (optimize_args, (const char *) q);
ab442df7
MM
9432 }
9433
9434 }
9435 }
9436
9771b263 9437 opt_argc = optimize_args->length ();
ab442df7
MM
9438 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9439
9440 for (i = 1; i < opt_argc; i++)
9771b263 9441 opt_argv[i] = (*optimize_args)[i];
ab442df7 9442
cbc19f39
JJ
9443 saved_flag_strict_aliasing = flag_strict_aliasing;
9444
ab442df7 9445 /* Now parse the options. */
a75bfaa6
JM
9446 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9447 &decoded_options,
9448 &decoded_options_count);
9449 decode_options (&global_options, &global_options_set,
a4d8c676
JM
9450 decoded_options, decoded_options_count,
9451 input_location, global_dc);
ab442df7 9452
2b7e2984
SE
9453 targetm.override_options_after_change();
9454
cbc19f39
JJ
9455 /* Don't allow changing -fstrict-aliasing. */
9456 flag_strict_aliasing = saved_flag_strict_aliasing;
9457
9771b263 9458 optimize_args->truncate (0);
ab442df7
MM
9459 return ret;
9460}
9461
9462/* For handling "optimize" attribute. arguments as in
9463 struct attribute_spec.handler. */
9464
9465static tree
9466handle_optimize_attribute (tree *node, tree name, tree args,
9467 int ARG_UNUSED (flags), bool *no_add_attrs)
9468{
9469 /* Ensure we have a function type. */
9470 if (TREE_CODE (*node) != FUNCTION_DECL)
9471 {
9472 warning (OPT_Wattributes, "%qE attribute ignored", name);
9473 *no_add_attrs = true;
9474 }
9475 else
9476 {
9477 struct cl_optimization cur_opts;
9478 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9479
9480 /* Save current options. */
46625112 9481 cl_optimization_save (&cur_opts, &global_options);
ab442df7
MM
9482
9483 /* If we previously had some optimization options, use them as the
9484 default. */
9485 if (old_opts)
46625112
JM
9486 cl_optimization_restore (&global_options,
9487 TREE_OPTIMIZATION (old_opts));
ab442df7
MM
9488
9489 /* Parse options, and update the vector. */
9490 parse_optimize_options (args, true);
9491 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
bf7b5747 9492 = build_optimization_node (&global_options);
ab442df7
MM
9493
9494 /* Restore current options. */
46625112 9495 cl_optimization_restore (&global_options, &cur_opts);
ab442df7
MM
9496 }
9497
9498 return NULL_TREE;
9499}
7458026b
ILT
9500
9501/* Handle a "no_split_stack" attribute. */
9502
9503static tree
9504handle_no_split_stack_attribute (tree *node, tree name,
9505 tree ARG_UNUSED (args),
9506 int ARG_UNUSED (flags),
9507 bool *no_add_attrs)
9508{
9509 tree decl = *node;
9510
9511 if (TREE_CODE (decl) != FUNCTION_DECL)
9512 {
9513 error_at (DECL_SOURCE_LOCATION (decl),
9514 "%qE attribute applies only to functions", name);
9515 *no_add_attrs = true;
9516 }
9517 else if (DECL_INITIAL (decl))
9518 {
9519 error_at (DECL_SOURCE_LOCATION (decl),
9520 "can%'t set %qE attribute after definition", name);
9521 *no_add_attrs = true;
9522 }
9523
9524 return NULL_TREE;
9525}
826cacfe
MG
9526
9527/* Handle a "returns_nonnull" attribute; arguments as in
9528 struct attribute_spec.handler. */
9529
9530static tree
9531handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9532 bool *no_add_attrs)
9533{
9534 // Even without a prototype we still have a return type we can check.
9535 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9536 {
9537 error ("returns_nonnull attribute on a function not returning a pointer");
9538 *no_add_attrs = true;
9539 }
9540 return NULL_TREE;
9541}
9542
976d5a22
TT
9543/* Handle a "designated_init" attribute; arguments as in
9544 struct attribute_spec.handler. */
9545
9546static tree
9547handle_designated_init_attribute (tree *node, tree name, tree, int,
9548 bool *no_add_attrs)
9549{
9550 if (TREE_CODE (*node) != RECORD_TYPE)
9551 {
9552 error ("%qE attribute is only valid on %<struct%> type", name);
9553 *no_add_attrs = true;
9554 }
9555 return NULL_TREE;
9556}
9557
b34c7881 9558\f
dde05067
NF
9559/* Check for valid arguments being passed to a function with FNTYPE.
9560 There are NARGS arguments in the array ARGARRAY. */
b34c7881 9561void
dde05067 9562check_function_arguments (const_tree fntype, int nargs, tree *argarray)
b34c7881
JT
9563{
9564 /* Check for null being passed in a pointer argument that must be
9565 non-null. We also need to do this if format checking is enabled. */
9566
9567 if (warn_nonnull)
dde05067 9568 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
b34c7881
JT
9569
9570 /* Check for errors in format strings. */
9571
e6c69da0 9572 if (warn_format || warn_suggest_attribute_format)
dde05067 9573 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
7876a414
KG
9574
9575 if (warn_format)
dde05067 9576 check_function_sentinel (fntype, nargs, argarray);
b34c7881
JT
9577}
9578
9579/* Generic argument checking recursion routine. PARAM is the argument to
9580 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9581 once the argument is resolved. CTX is context for the callback. */
9582void
35b1a6fa
AJ
9583check_function_arguments_recurse (void (*callback)
9584 (void *, tree, unsigned HOST_WIDE_INT),
9585 void *ctx, tree param,
9586 unsigned HOST_WIDE_INT param_num)
b34c7881 9587{
1043771b 9588 if (CONVERT_EXPR_P (param)
1344f9a3
JM
9589 && (TYPE_PRECISION (TREE_TYPE (param))
9590 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
b34c7881
JT
9591 {
9592 /* Strip coercion. */
9593 check_function_arguments_recurse (callback, ctx,
6de9cd9a 9594 TREE_OPERAND (param, 0), param_num);
b34c7881
JT
9595 return;
9596 }
9597
9598 if (TREE_CODE (param) == CALL_EXPR)
9599 {
5039610b 9600 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
b34c7881
JT
9601 tree attrs;
9602 bool found_format_arg = false;
9603
9604 /* See if this is a call to a known internationalization function
9605 that modifies a format arg. Such a function may have multiple
9606 format_arg attributes (for example, ngettext). */
9607
9608 for (attrs = TYPE_ATTRIBUTES (type);
9609 attrs;
9610 attrs = TREE_CHAIN (attrs))
9611 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9612 {
5039610b 9613 tree inner_arg;
b34c7881
JT
9614 tree format_num_expr;
9615 int format_num;
9616 int i;
5039610b 9617 call_expr_arg_iterator iter;
b34c7881
JT
9618
9619 /* Extract the argument number, which was previously checked
9620 to be valid. */
9621 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
b34c7881 9622
6b3b8c27 9623 format_num = tree_to_uhwi (format_num_expr);
b34c7881 9624
5039610b
SL
9625 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9626 inner_arg != 0;
9627 inner_arg = next_call_expr_arg (&iter), i++)
b34c7881
JT
9628 if (i == format_num)
9629 {
9630 check_function_arguments_recurse (callback, ctx,
5039610b 9631 inner_arg, param_num);
b34c7881
JT
9632 found_format_arg = true;
9633 break;
9634 }
9635 }
9636
9637 /* If we found a format_arg attribute and did a recursive check,
9638 we are done with checking this argument. Otherwise, we continue
9639 and this will be considered a non-literal. */
9640 if (found_format_arg)
9641 return;
9642 }
9643
9644 if (TREE_CODE (param) == COND_EXPR)
9645 {
9646 /* Check both halves of the conditional expression. */
9647 check_function_arguments_recurse (callback, ctx,
6de9cd9a 9648 TREE_OPERAND (param, 1), param_num);
b34c7881 9649 check_function_arguments_recurse (callback, ctx,
6de9cd9a 9650 TREE_OPERAND (param, 2), param_num);
b34c7881
JT
9651 return;
9652 }
9653
9654 (*callback) (ctx, param, param_num);
9655}
e2500fed 9656
a98c2819
MLI
9657/* Checks for a builtin function FNDECL that the number of arguments
9658 NARGS against the required number REQUIRED and issues an error if
9659 there is a mismatch. Returns true if the number of arguments is
9660 correct, otherwise false. */
83322951
RG
9661
9662static bool
a98c2819 9663builtin_function_validate_nargs (tree fndecl, int nargs, int required)
83322951
RG
9664{
9665 if (nargs < required)
9666 {
a98c2819
MLI
9667 error_at (input_location,
9668 "not enough arguments to function %qE", fndecl);
83322951
RG
9669 return false;
9670 }
9671 else if (nargs > required)
9672 {
a98c2819
MLI
9673 error_at (input_location,
9674 "too many arguments to function %qE", fndecl);
83322951
RG
9675 return false;
9676 }
9677 return true;
9678}
9679
9680/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9681 Returns false if there was an error, otherwise true. */
9682
9683bool
9684check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9685{
9686 if (!DECL_BUILT_IN (fndecl)
9687 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9688 return true;
9689
9690 switch (DECL_FUNCTION_CODE (fndecl))
9691 {
9692 case BUILT_IN_CONSTANT_P:
a98c2819 9693 return builtin_function_validate_nargs (fndecl, nargs, 1);
83322951
RG
9694
9695 case BUILT_IN_ISFINITE:
9696 case BUILT_IN_ISINF:
05f41289 9697 case BUILT_IN_ISINF_SIGN:
83322951
RG
9698 case BUILT_IN_ISNAN:
9699 case BUILT_IN_ISNORMAL:
a98c2819 9700 if (builtin_function_validate_nargs (fndecl, nargs, 1))
83322951
RG
9701 {
9702 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9703 {
9704 error ("non-floating-point argument in call to "
9705 "function %qE", fndecl);
9706 return false;
9707 }
9708 return true;
9709 }
9710 return false;
9711
9712 case BUILT_IN_ISGREATER:
9713 case BUILT_IN_ISGREATEREQUAL:
9714 case BUILT_IN_ISLESS:
9715 case BUILT_IN_ISLESSEQUAL:
9716 case BUILT_IN_ISLESSGREATER:
9717 case BUILT_IN_ISUNORDERED:
a98c2819 9718 if (builtin_function_validate_nargs (fndecl, nargs, 2))
83322951
RG
9719 {
9720 enum tree_code code0, code1;
9721 code0 = TREE_CODE (TREE_TYPE (args[0]));
9722 code1 = TREE_CODE (TREE_TYPE (args[1]));
9723 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9724 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9725 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9726 {
9727 error ("non-floating-point arguments in call to "
9728 "function %qE", fndecl);
9729 return false;
9730 }
9731 return true;
9732 }
9733 return false;
9734
3bf5906b 9735 case BUILT_IN_FPCLASSIFY:
a98c2819 9736 if (builtin_function_validate_nargs (fndecl, nargs, 6))
3bf5906b
KG
9737 {
9738 unsigned i;
b8698a0f 9739
3bf5906b
KG
9740 for (i=0; i<5; i++)
9741 if (TREE_CODE (args[i]) != INTEGER_CST)
9742 {
9743 error ("non-const integer argument %u in call to function %qE",
9744 i+1, fndecl);
9745 return false;
9746 }
9747
9748 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9749 {
9750 error ("non-floating-point argument in call to function %qE",
9751 fndecl);
9752 return false;
9753 }
9754 return true;
9755 }
9756 return false;
9757
45d439ac
JJ
9758 case BUILT_IN_ASSUME_ALIGNED:
9759 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9760 {
9761 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9762 {
9763 error ("non-integer argument 3 in call to function %qE", fndecl);
9764 return false;
9765 }
9766 return true;
9767 }
9768 return false;
9769
1304953e
JJ
9770 case BUILT_IN_ADD_OVERFLOW:
9771 case BUILT_IN_SUB_OVERFLOW:
9772 case BUILT_IN_MUL_OVERFLOW:
9773 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9774 {
9775 unsigned i;
9776 for (i = 0; i < 2; i++)
9777 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9778 {
9779 error ("argument %u in call to function %qE does not have "
9780 "integral type", i + 1, fndecl);
9781 return false;
9782 }
9783 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9784 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9785 {
9786 error ("argument 3 in call to function %qE does not have "
9787 "pointer to integer type", fndecl);
9788 return false;
9789 }
9790 return true;
9791 }
9792 return false;
9793
83322951
RG
9794 default:
9795 return true;
9796 }
9797}
9798
d07605f5
AP
9799/* Function to help qsort sort FIELD_DECLs by name order. */
9800
9801int
9802field_decl_cmp (const void *x_p, const void *y_p)
9803{
28dab132
BI
9804 const tree *const x = (const tree *const) x_p;
9805 const tree *const y = (const tree *const) y_p;
9806
d07605f5
AP
9807 if (DECL_NAME (*x) == DECL_NAME (*y))
9808 /* A nontype is "greater" than a type. */
9809 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9810 if (DECL_NAME (*x) == NULL_TREE)
9811 return -1;
9812 if (DECL_NAME (*y) == NULL_TREE)
9813 return 1;
9814 if (DECL_NAME (*x) < DECL_NAME (*y))
9815 return -1;
9816 return 1;
9817}
9818
9819static struct {
9820 gt_pointer_operator new_value;
9821 void *cookie;
9822} resort_data;
9823
9824/* This routine compares two fields like field_decl_cmp but using the
9825pointer operator in resort_data. */
9826
9827static int
9828resort_field_decl_cmp (const void *x_p, const void *y_p)
9829{
28dab132
BI
9830 const tree *const x = (const tree *const) x_p;
9831 const tree *const y = (const tree *const) y_p;
d07605f5
AP
9832
9833 if (DECL_NAME (*x) == DECL_NAME (*y))
9834 /* A nontype is "greater" than a type. */
9835 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9836 if (DECL_NAME (*x) == NULL_TREE)
9837 return -1;
9838 if (DECL_NAME (*y) == NULL_TREE)
9839 return 1;
9840 {
9841 tree d1 = DECL_NAME (*x);
9842 tree d2 = DECL_NAME (*y);
9843 resort_data.new_value (&d1, resort_data.cookie);
9844 resort_data.new_value (&d2, resort_data.cookie);
9845 if (d1 < d2)
9846 return -1;
9847 }
9848 return 1;
9849}
9850
9851/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9852
9853void
9854resort_sorted_fields (void *obj,
e18476eb 9855 void * ARG_UNUSED (orig_obj),
6de9cd9a
DN
9856 gt_pointer_operator new_value,
9857 void *cookie)
d07605f5 9858{
e18476eb 9859 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
d07605f5
AP
9860 resort_data.new_value = new_value;
9861 resort_data.cookie = cookie;
9862 qsort (&sf->elts[0], sf->len, sizeof (tree),
6de9cd9a 9863 resort_field_decl_cmp);
d07605f5
AP
9864}
9865
0a3ee0fd
GDR
9866/* Subroutine of c_parse_error.
9867 Return the result of concatenating LHS and RHS. RHS is really
9868 a string literal, its first character is indicated by RHS_START and
3292fb42 9869 RHS_SIZE is its length (including the terminating NUL character).
0a3ee0fd
GDR
9870
9871 The caller is responsible for deleting the returned pointer. */
9872
9873static char *
9874catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9875{
9876 const int lhs_size = strlen (lhs);
9877 char *result = XNEWVEC (char, lhs_size + rhs_size);
9878 strncpy (result, lhs, lhs_size);
9879 strncpy (result + lhs_size, rhs_start, rhs_size);
9880 return result;
9881}
9882
4b794eaf 9883/* Issue the error given by GMSGID, indicating that it occurred before
4bb8ca28
MM
9884 TOKEN, which had the associated VALUE. */
9885
9886void
b8698a0f 9887c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
cfc93532 9888 tree value, unsigned char token_flags)
4bb8ca28 9889{
0a3ee0fd
GDR
9890#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9891
9892 char *message = NULL;
4bb8ca28 9893
cfc93532 9894 if (token_type == CPP_EOF)
4b794eaf 9895 message = catenate_messages (gmsgid, " at end of input");
b8698a0f
L
9896 else if (token_type == CPP_CHAR
9897 || token_type == CPP_WCHAR
cfc93532
MLI
9898 || token_type == CPP_CHAR16
9899 || token_type == CPP_CHAR32)
4bb8ca28
MM
9900 {
9901 unsigned int val = TREE_INT_CST_LOW (value);
b6baa67d
KVH
9902 const char *prefix;
9903
cfc93532 9904 switch (token_type)
b6baa67d
KVH
9905 {
9906 default:
9907 prefix = "";
9908 break;
9909 case CPP_WCHAR:
9910 prefix = "L";
9911 break;
9912 case CPP_CHAR16:
9913 prefix = "u";
9914 break;
9915 case CPP_CHAR32:
9916 prefix = "U";
9917 break;
9918 }
9919
4bb8ca28 9920 if (val <= UCHAR_MAX && ISGRAPH (val))
c22cacf3 9921 message = catenate_messages (gmsgid, " before %s'%c'");
4bb8ca28 9922 else
c22cacf3 9923 message = catenate_messages (gmsgid, " before %s'\\x%x'");
0a3ee0fd 9924
b6baa67d 9925 error (message, prefix, val);
0a3ee0fd
GDR
9926 free (message);
9927 message = NULL;
4bb8ca28 9928 }
65e5a578
ESR
9929 else if (token_type == CPP_CHAR_USERDEF
9930 || token_type == CPP_WCHAR_USERDEF
9931 || token_type == CPP_CHAR16_USERDEF
9932 || token_type == CPP_CHAR32_USERDEF)
9933 message = catenate_messages (gmsgid,
9934 " before user-defined character literal");
9935 else if (token_type == CPP_STRING_USERDEF
9936 || token_type == CPP_WSTRING_USERDEF
9937 || token_type == CPP_STRING16_USERDEF
9938 || token_type == CPP_STRING32_USERDEF
9939 || token_type == CPP_UTF8STRING_USERDEF)
9940 message = catenate_messages (gmsgid, " before user-defined string literal");
b8698a0f
L
9941 else if (token_type == CPP_STRING
9942 || token_type == CPP_WSTRING
cfc93532 9943 || token_type == CPP_STRING16
2c6e3f55
JJ
9944 || token_type == CPP_STRING32
9945 || token_type == CPP_UTF8STRING)
4b794eaf 9946 message = catenate_messages (gmsgid, " before string constant");
cfc93532 9947 else if (token_type == CPP_NUMBER)
4b794eaf 9948 message = catenate_messages (gmsgid, " before numeric constant");
cfc93532 9949 else if (token_type == CPP_NAME)
0a3ee0fd 9950 {
4b794eaf 9951 message = catenate_messages (gmsgid, " before %qE");
c51a1ba9 9952 error (message, value);
0a3ee0fd
GDR
9953 free (message);
9954 message = NULL;
9955 }
cfc93532 9956 else if (token_type == CPP_PRAGMA)
bc4071dd 9957 message = catenate_messages (gmsgid, " before %<#pragma%>");
cfc93532 9958 else if (token_type == CPP_PRAGMA_EOL)
bc4071dd 9959 message = catenate_messages (gmsgid, " before end of line");
34429675
JM
9960 else if (token_type == CPP_DECLTYPE)
9961 message = catenate_messages (gmsgid, " before %<decltype%>");
cfc93532 9962 else if (token_type < N_TTYPES)
0a3ee0fd 9963 {
4b794eaf 9964 message = catenate_messages (gmsgid, " before %qs token");
cfc93532 9965 error (message, cpp_type2name (token_type, token_flags));
0a3ee0fd
GDR
9966 free (message);
9967 message = NULL;
9968 }
4bb8ca28 9969 else
4b794eaf 9970 error (gmsgid);
0a3ee0fd
GDR
9971
9972 if (message)
9973 {
9974 error (message);
9975 free (message);
9976 }
c22cacf3 9977#undef catenate_messages
4bb8ca28
MM
9978}
9979
87cf0651
SB
9980/* Return the gcc option code associated with the reason for a cpp
9981 message, or 0 if none. */
9982
9983static int
9984c_option_controlling_cpp_error (int reason)
9985{
b559c810 9986 const struct cpp_reason_option_codes_t *entry;
87cf0651 9987
b559c810 9988 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
87cf0651
SB
9989 {
9990 if (entry->reason == reason)
9991 return entry->option_code;
9992 }
9993 return 0;
9994}
9995
148e4216 9996/* Callback from cpp_error for PFILE to print diagnostics from the
87cf0651
SB
9997 preprocessor. The diagnostic is of type LEVEL, with REASON set
9998 to the reason code if LEVEL is represents a warning, at location
148e4216
JM
9999 LOCATION unless this is after lexing and the compiler's location
10000 should be used instead, with column number possibly overridden by
10001 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10002 the arguments. Returns true if a diagnostic was emitted, false
10003 otherwise. */
10004
10005bool
87cf0651 10006c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
148e4216
JM
10007 location_t location, unsigned int column_override,
10008 const char *msg, va_list *ap)
10009{
10010 diagnostic_info diagnostic;
10011 diagnostic_t dlevel;
e3339d0f 10012 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
148e4216
JM
10013 bool ret;
10014
10015 switch (level)
10016 {
10017 case CPP_DL_WARNING_SYSHDR:
10018 if (flag_no_output)
10019 return false;
e3339d0f 10020 global_dc->dc_warn_system_headers = 1;
148e4216
JM
10021 /* Fall through. */
10022 case CPP_DL_WARNING:
10023 if (flag_no_output)
10024 return false;
10025 dlevel = DK_WARNING;
10026 break;
10027 case CPP_DL_PEDWARN:
10028 if (flag_no_output && !flag_pedantic_errors)
10029 return false;
10030 dlevel = DK_PEDWARN;
10031 break;
10032 case CPP_DL_ERROR:
10033 dlevel = DK_ERROR;
10034 break;
10035 case CPP_DL_ICE:
10036 dlevel = DK_ICE;
10037 break;
10038 case CPP_DL_NOTE:
10039 dlevel = DK_NOTE;
10040 break;
47580d22
JM
10041 case CPP_DL_FATAL:
10042 dlevel = DK_FATAL;
10043 break;
148e4216
JM
10044 default:
10045 gcc_unreachable ();
10046 }
10047 if (done_lexing)
10048 location = input_location;
10049 diagnostic_set_info_translated (&diagnostic, msg, ap,
10050 location, dlevel);
10051 if (column_override)
10052 diagnostic_override_column (&diagnostic, column_override);
87cf0651
SB
10053 diagnostic_override_option_index (&diagnostic,
10054 c_option_controlling_cpp_error (reason));
148e4216
JM
10055 ret = report_diagnostic (&diagnostic);
10056 if (level == CPP_DL_WARNING_SYSHDR)
e3339d0f 10057 global_dc->dc_warn_system_headers = save_warn_system_headers;
148e4216
JM
10058 return ret;
10059}
10060
c5ff069d
ZW
10061/* Convert a character from the host to the target execution character
10062 set. cpplib handles this, mostly. */
10063
10064HOST_WIDE_INT
10065c_common_to_target_charset (HOST_WIDE_INT c)
10066{
10067 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10068 zero-extended under -fno-signed-char. cpplib insists that characters
10069 and character constants are always unsigned. Hence we must convert
10070 back and forth. */
10071 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10072
10073 uc = cpp_host_to_exec_charset (parse_in, uc);
10074
10075 if (flag_signed_char)
10076 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10077 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10078 else
10079 return uc;
10080}
10081
cf9e9959
EB
10082/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10083 references with an INDIRECT_REF of a constant at the bottom; much like the
10084 traditional rendering of offsetof as a macro. Return the folded result. */
ee8a6a3e 10085
cf9e9959
EB
10086tree
10087fold_offsetof_1 (tree expr)
ee8a6a3e 10088{
ee8a6a3e
RH
10089 tree base, off, t;
10090
10091 switch (TREE_CODE (expr))
10092 {
10093 case ERROR_MARK:
10094 return expr;
10095
545b7d8c
VR
10096 case VAR_DECL:
10097 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10098 return error_mark_node;
10099
6d4d7b0e 10100 case CALL_EXPR:
8d5f60ac 10101 case TARGET_EXPR:
6d4d7b0e
PB
10102 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10103 return error_mark_node;
10104
6d4d7b0e
PB
10105 case NOP_EXPR:
10106 case INDIRECT_REF:
cf9e9959 10107 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
61c3c490
DS
10108 {
10109 error ("cannot apply %<offsetof%> to a non constant address");
10110 return error_mark_node;
10111 }
cf9e9959 10112 return TREE_OPERAND (expr, 0);
6d4d7b0e 10113
ee8a6a3e 10114 case COMPONENT_REF:
cf9e9959 10115 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
ee8a6a3e
RH
10116 if (base == error_mark_node)
10117 return base;
10118
10119 t = TREE_OPERAND (expr, 1);
10120 if (DECL_C_BIT_FIELD (t))
10121 {
10122 error ("attempt to take address of bit-field structure "
c51a1ba9 10123 "member %qD", t);
ee8a6a3e
RH
10124 return error_mark_node;
10125 }
db3927fb 10126 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
386b1f1f 10127 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
db3927fb 10128 / BITS_PER_UNIT));
ee8a6a3e
RH
10129 break;
10130
10131 case ARRAY_REF:
cf9e9959 10132 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
ee8a6a3e
RH
10133 if (base == error_mark_node)
10134 return base;
10135
10136 t = TREE_OPERAND (expr, 1);
61c3c490
DS
10137
10138 /* Check if the offset goes beyond the upper bound of the array. */
cf9e9959 10139 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
d32599a6
JJ
10140 {
10141 tree upbound = array_ref_up_bound (expr);
10142 if (upbound != NULL_TREE
10143 && TREE_CODE (upbound) == INTEGER_CST
10144 && !tree_int_cst_equal (upbound,
10145 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10146 {
10147 upbound = size_binop (PLUS_EXPR, upbound,
10148 build_int_cst (TREE_TYPE (upbound), 1));
10149 if (tree_int_cst_lt (upbound, t))
10150 {
10151 tree v;
10152
10153 for (v = TREE_OPERAND (expr, 0);
10154 TREE_CODE (v) == COMPONENT_REF;
10155 v = TREE_OPERAND (v, 0))
10156 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10157 == RECORD_TYPE)
10158 {
910ad8de
NF
10159 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10160 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
d32599a6
JJ
10161 if (TREE_CODE (fld_chain) == FIELD_DECL)
10162 break;
10163
10164 if (fld_chain)
10165 break;
10166 }
10167 /* Don't warn if the array might be considered a poor
10168 man's flexible array member with a very permissive
10169 definition thereof. */
10170 if (TREE_CODE (v) == ARRAY_REF
10171 || TREE_CODE (v) == COMPONENT_REF)
10172 warning (OPT_Warray_bounds,
10173 "index %E denotes an offset "
10174 "greater than size of %qT",
10175 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10176 }
10177 }
10178 }
cf9e9959
EB
10179
10180 t = convert (sizetype, t);
10181 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
ee8a6a3e
RH
10182 break;
10183
1916c916
VR
10184 case COMPOUND_EXPR:
10185 /* Handle static members of volatile structs. */
10186 t = TREE_OPERAND (expr, 1);
10187 gcc_assert (TREE_CODE (t) == VAR_DECL);
cf9e9959 10188 return fold_offsetof_1 (t);
1916c916 10189
ee8a6a3e 10190 default:
366de0ce 10191 gcc_unreachable ();
ee8a6a3e
RH
10192 }
10193
cf9e9959 10194 return fold_build_pointer_plus (base, off);
ee8a6a3e
RH
10195}
10196
cf9e9959
EB
10197/* Likewise, but convert it to the return type of offsetof. */
10198
ee8a6a3e 10199tree
cf9e9959 10200fold_offsetof (tree expr)
ee8a6a3e 10201{
cf9e9959 10202 return convert (size_type_node, fold_offsetof_1 (expr));
ee8a6a3e
RH
10203}
10204
d166d4c3
AK
10205/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10206 expression, because B will always be true. */
10207
10208void
10209warn_for_omitted_condop (location_t location, tree cond)
10210{
10211 if (truth_value_p (TREE_CODE (cond)))
10212 warning_at (location, OPT_Wparentheses,
10213 "the omitted middle operand in ?: will always be %<true%>, "
10214 "suggest explicit middle operand");
10215}
10216
4816c593
NF
10217/* Give an error for storing into ARG, which is 'const'. USE indicates
10218 how ARG was being used. */
10219
10220void
c02065fc 10221readonly_error (location_t loc, tree arg, enum lvalue_use use)
4816c593
NF
10222{
10223 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10224 || use == lv_asm);
10225 /* Using this macro rather than (for example) arrays of messages
10226 ensures that all the format strings are checked at compile
10227 time. */
10228#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10229 : (use == lv_increment ? (I) \
10230 : (use == lv_decrement ? (D) : (AS))))
10231 if (TREE_CODE (arg) == COMPONENT_REF)
10232 {
10233 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
c02065fc
AH
10234 error_at (loc, READONLY_MSG (G_("assignment of member "
10235 "%qD in read-only object"),
10236 G_("increment of member "
10237 "%qD in read-only object"),
10238 G_("decrement of member "
10239 "%qD in read-only object"),
10240 G_("member %qD in read-only object "
10241 "used as %<asm%> output")),
10242 TREE_OPERAND (arg, 1));
4816c593 10243 else
c02065fc
AH
10244 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10245 G_("increment of read-only member %qD"),
10246 G_("decrement of read-only member %qD"),
10247 G_("read-only member %qD used as %<asm%> output")),
10248 TREE_OPERAND (arg, 1));
4816c593
NF
10249 }
10250 else if (TREE_CODE (arg) == VAR_DECL)
c02065fc
AH
10251 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10252 G_("increment of read-only variable %qD"),
10253 G_("decrement of read-only variable %qD"),
10254 G_("read-only variable %qD used as %<asm%> output")),
10255 arg);
4816c593 10256 else if (TREE_CODE (arg) == PARM_DECL)
c02065fc
AH
10257 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10258 G_("increment of read-only parameter %qD"),
10259 G_("decrement of read-only parameter %qD"),
10260 G_("read-only parameter %qD use as %<asm%> output")),
10261 arg);
4816c593
NF
10262 else if (TREE_CODE (arg) == RESULT_DECL)
10263 {
10264 gcc_assert (c_dialect_cxx ());
c02065fc
AH
10265 error_at (loc, READONLY_MSG (G_("assignment of "
10266 "read-only named return value %qD"),
10267 G_("increment of "
10268 "read-only named return value %qD"),
10269 G_("decrement of "
10270 "read-only named return value %qD"),
10271 G_("read-only named return value %qD "
10272 "used as %<asm%>output")),
10273 arg);
4816c593
NF
10274 }
10275 else if (TREE_CODE (arg) == FUNCTION_DECL)
c02065fc
AH
10276 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10277 G_("increment of function %qD"),
10278 G_("decrement of function %qD"),
10279 G_("function %qD used as %<asm%> output")),
10280 arg);
4816c593 10281 else
c02065fc
AH
10282 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10283 G_("increment of read-only location %qE"),
10284 G_("decrement of read-only location %qE"),
10285 G_("read-only location %qE used as %<asm%> output")),
10286 arg);
4816c593
NF
10287}
10288
37dc0d8d 10289/* Print an error message for an invalid lvalue. USE says
7bd11157
TT
10290 how the lvalue is being used and so selects the error message. LOC
10291 is the location for the error. */
5ae9ba3e 10292
37dc0d8d 10293void
7bd11157 10294lvalue_error (location_t loc, enum lvalue_use use)
5ae9ba3e 10295{
37dc0d8d 10296 switch (use)
5ae9ba3e 10297 {
37dc0d8d 10298 case lv_assign:
7bd11157 10299 error_at (loc, "lvalue required as left operand of assignment");
37dc0d8d
JM
10300 break;
10301 case lv_increment:
7bd11157 10302 error_at (loc, "lvalue required as increment operand");
37dc0d8d
JM
10303 break;
10304 case lv_decrement:
7bd11157 10305 error_at (loc, "lvalue required as decrement operand");
37dc0d8d
JM
10306 break;
10307 case lv_addressof:
7bd11157 10308 error_at (loc, "lvalue required as unary %<&%> operand");
37dc0d8d
JM
10309 break;
10310 case lv_asm:
7bd11157 10311 error_at (loc, "lvalue required in asm statement");
37dc0d8d
JM
10312 break;
10313 default:
10314 gcc_unreachable ();
5ae9ba3e 10315 }
5ae9ba3e 10316}
7a6daeb0
NF
10317
10318/* Print an error message for an invalid indirection of type TYPE.
10319 ERRSTRING is the name of the operator for the indirection. */
10320
10321void
10322invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10323{
10324 switch (errstring)
10325 {
10326 case RO_NULL:
10327 gcc_assert (c_dialect_cxx ());
10328 error_at (loc, "invalid type argument (have %qT)", type);
10329 break;
10330 case RO_ARRAY_INDEXING:
10331 error_at (loc,
10332 "invalid type argument of array indexing (have %qT)",
10333 type);
10334 break;
10335 case RO_UNARY_STAR:
10336 error_at (loc,
10337 "invalid type argument of unary %<*%> (have %qT)",
10338 type);
10339 break;
10340 case RO_ARROW:
10341 error_at (loc,
10342 "invalid type argument of %<->%> (have %qT)",
10343 type);
10344 break;
55a7f02f
MP
10345 case RO_ARROW_STAR:
10346 error_at (loc,
10347 "invalid type argument of %<->*%> (have %qT)",
10348 type);
10349 break;
7a6daeb0
NF
10350 case RO_IMPLICIT_CONVERSION:
10351 error_at (loc,
10352 "invalid type argument of implicit conversion (have %qT)",
10353 type);
10354 break;
10355 default:
10356 gcc_unreachable ();
10357 }
10358}
aab038d5
RH
10359\f
10360/* *PTYPE is an incomplete array. Complete it with a domain based on
10361 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10362 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10363 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10364
10365int
10366complete_array_type (tree *ptype, tree initial_value, bool do_default)
10367{
10368 tree maxindex, type, main_type, elt, unqual_elt;
10369 int failure = 0, quals;
06d40de8 10370 hashval_t hashcode = 0;
40d3d530 10371 bool overflow_p = false;
aab038d5
RH
10372
10373 maxindex = size_zero_node;
10374 if (initial_value)
10375 {
10376 if (TREE_CODE (initial_value) == STRING_CST)
10377 {
10378 int eltsize
10379 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10380 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10381 }
10382 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10383 {
9771b263 10384 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
aab038d5 10385
9771b263 10386 if (vec_safe_is_empty (v))
aab038d5
RH
10387 {
10388 if (pedantic)
10389 failure = 3;
830c740f 10390 maxindex = ssize_int (-1);
aab038d5
RH
10391 }
10392 else
10393 {
10394 tree curindex;
4038c495
GB
10395 unsigned HOST_WIDE_INT cnt;
10396 constructor_elt *ce;
cff7525f 10397 bool fold_p = false;
aab038d5 10398
9771b263 10399 if ((*v)[0].index)
40d3d530
JR
10400 maxindex = (*v)[0].index, fold_p = true;
10401
aab038d5
RH
10402 curindex = maxindex;
10403
9771b263 10404 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
aab038d5 10405 {
cff7525f 10406 bool curfold_p = false;
4038c495 10407 if (ce->index)
cff7525f 10408 curindex = ce->index, curfold_p = true;
aab038d5 10409 else
cff7525f
JH
10410 {
10411 if (fold_p)
40d3d530
JR
10412 {
10413 /* Since we treat size types now as ordinary
10414 unsigned types, we need an explicit overflow
10415 check. */
10416 tree orig = curindex;
10417 curindex = fold_convert (sizetype, curindex);
10418 overflow_p |= tree_int_cst_lt (curindex, orig);
10419 }
db3927fb
AH
10420 curindex = size_binop (PLUS_EXPR, curindex,
10421 size_one_node);
cff7525f 10422 }
aab038d5 10423 if (tree_int_cst_lt (maxindex, curindex))
cff7525f 10424 maxindex = curindex, fold_p = curfold_p;
aab038d5 10425 }
40d3d530
JR
10426 if (fold_p)
10427 {
10428 tree orig = maxindex;
10429 maxindex = fold_convert (sizetype, maxindex);
10430 overflow_p |= tree_int_cst_lt (maxindex, orig);
10431 }
aab038d5
RH
10432 }
10433 }
10434 else
10435 {
10436 /* Make an error message unless that happened already. */
10437 if (initial_value != error_mark_node)
10438 failure = 1;
10439 }
10440 }
10441 else
10442 {
10443 failure = 2;
10444 if (!do_default)
10445 return failure;
10446 }
10447
10448 type = *ptype;
10449 elt = TREE_TYPE (type);
10450 quals = TYPE_QUALS (strip_array_types (elt));
10451 if (quals == 0)
10452 unqual_elt = elt;
10453 else
36c5e70a 10454 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
aab038d5
RH
10455
10456 /* Using build_distinct_type_copy and modifying things afterward instead
10457 of using build_array_type to create a new type preserves all of the
10458 TYPE_LANG_FLAG_? bits that the front end may have set. */
10459 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10460 TREE_TYPE (main_type) = unqual_elt;
e6313a78
RG
10461 TYPE_DOMAIN (main_type)
10462 = build_range_type (TREE_TYPE (maxindex),
10463 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
aab038d5
RH
10464 layout_type (main_type);
10465
06d40de8
DG
10466 /* Make sure we have the canonical MAIN_TYPE. */
10467 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
b8698a0f 10468 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
06d40de8
DG
10469 hashcode);
10470 main_type = type_hash_canon (hashcode, main_type);
10471
9ae165a0
DG
10472 /* Fix the canonical type. */
10473 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10474 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10475 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10476 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10477 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10478 != TYPE_DOMAIN (main_type)))
b8698a0f 10479 TYPE_CANONICAL (main_type)
9ae165a0
DG
10480 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10481 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10482 else
10483 TYPE_CANONICAL (main_type) = main_type;
10484
aab038d5
RH
10485 if (quals == 0)
10486 type = main_type;
10487 else
10488 type = c_build_qualified_type (main_type, quals);
10489
7bfcb402
JM
10490 if (COMPLETE_TYPE_P (type)
10491 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
40d3d530 10492 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7bfcb402
JM
10493 {
10494 error ("size of array is too large");
10495 /* If we proceed with the array type as it is, we'll eventually
386b1f1f 10496 crash in tree_to_[su]hwi(). */
7bfcb402
JM
10497 type = error_mark_node;
10498 }
10499
aab038d5
RH
10500 *ptype = type;
10501 return failure;
10502}
5ae9ba3e 10503
30cd1c5d
AS
10504/* Like c_mark_addressable but don't check register qualifier. */
10505void
10506c_common_mark_addressable_vec (tree t)
10507{
10508 while (handled_component_p (t))
10509 t = TREE_OPERAND (t, 0);
10510 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10511 return;
10512 TREE_ADDRESSABLE (t) = 1;
10513}
10514
10515
48ae6c13
RH
10516\f
10517/* Used to help initialize the builtin-types.def table. When a type of
10518 the correct size doesn't exist, use error_mark_node instead of NULL.
10519 The later results in segfaults even when a decl using the type doesn't
10520 get invoked. */
10521
10522tree
10523builtin_type_for_size (int size, bool unsignedp)
10524{
21fa2faf 10525 tree type = c_common_type_for_size (size, unsignedp);
48ae6c13
RH
10526 return type ? type : error_mark_node;
10527}
10528
10529/* A helper function for resolve_overloaded_builtin in resolving the
10530 overloaded __sync_ builtins. Returns a positive power of 2 if the
10531 first operand of PARAMS is a pointer to a supported data type.
10532 Returns 0 if an error is encountered. */
10533
10534static int
9771b263 10535sync_resolve_size (tree function, vec<tree, va_gc> *params)
48ae6c13
RH
10536{
10537 tree type;
10538 int size;
10539
9771b263 10540 if (!params)
48ae6c13
RH
10541 {
10542 error ("too few arguments to function %qE", function);
10543 return 0;
10544 }
10545
9771b263 10546 type = TREE_TYPE ((*params)[0]);
6415bd5d
JM
10547 if (TREE_CODE (type) == ARRAY_TYPE)
10548 {
10549 /* Force array-to-pointer decay for C++. */
10550 gcc_assert (c_dialect_cxx());
10551 (*params)[0] = default_conversion ((*params)[0]);
10552 type = TREE_TYPE ((*params)[0]);
10553 }
48ae6c13
RH
10554 if (TREE_CODE (type) != POINTER_TYPE)
10555 goto incompatible;
10556
10557 type = TREE_TYPE (type);
10558 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10559 goto incompatible;
10560
ae7e9ddd 10561 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
a0274e3e 10562 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
48ae6c13
RH
10563 return size;
10564
10565 incompatible:
10566 error ("incompatible type for argument %d of %qE", 1, function);
10567 return 0;
10568}
10569
c22cacf3 10570/* A helper function for resolve_overloaded_builtin. Adds casts to
48ae6c13
RH
10571 PARAMS to make arguments match up with those of FUNCTION. Drops
10572 the variadic arguments at the end. Returns false if some error
10573 was encountered; true on success. */
10574
10575static bool
86951993 10576sync_resolve_params (location_t loc, tree orig_function, tree function,
9771b263 10577 vec<tree, va_gc> *params, bool orig_format)
48ae6c13 10578{
e19a18d4 10579 function_args_iterator iter;
48ae6c13 10580 tree ptype;
bbbbb16a 10581 unsigned int parmnum;
48ae6c13 10582
e19a18d4 10583 function_args_iter_init (&iter, TREE_TYPE (function));
48ae6c13
RH
10584 /* We've declared the implementation functions to use "volatile void *"
10585 as the pointer parameter, so we shouldn't get any complaints from the
10586 call to check_function_arguments what ever type the user used. */
e19a18d4 10587 function_args_iter_next (&iter);
9771b263 10588 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
267bac10 10589 ptype = TYPE_MAIN_VARIANT (ptype);
48ae6c13
RH
10590
10591 /* For the rest of the values, we need to cast these to FTYPE, so that we
10592 don't get warnings for passing pointer types, etc. */
bbbbb16a 10593 parmnum = 0;
e19a18d4 10594 while (1)
48ae6c13 10595 {
e19a18d4
NF
10596 tree val, arg_type;
10597
10598 arg_type = function_args_iter_cond (&iter);
10599 /* XXX void_type_node belies the abstraction. */
10600 if (arg_type == void_type_node)
10601 break;
48ae6c13 10602
bbbbb16a 10603 ++parmnum;
9771b263 10604 if (params->length () <= parmnum)
48ae6c13 10605 {
86951993 10606 error_at (loc, "too few arguments to function %qE", orig_function);
48ae6c13
RH
10607 return false;
10608 }
10609
e3793c6f
JJ
10610 /* Only convert parameters if arg_type is unsigned integer type with
10611 new format sync routines, i.e. don't attempt to convert pointer
10612 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10613 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10614 kinds). */
10615 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
86951993
AM
10616 {
10617 /* Ideally for the first conversion we'd use convert_for_assignment
10618 so that we get warnings for anything that doesn't match the pointer
10619 type. This isn't portable across the C and C++ front ends atm. */
9771b263 10620 val = (*params)[parmnum];
86951993
AM
10621 val = convert (ptype, val);
10622 val = convert (arg_type, val);
9771b263 10623 (*params)[parmnum] = val;
86951993 10624 }
48ae6c13 10625
e19a18d4 10626 function_args_iter_next (&iter);
48ae6c13
RH
10627 }
10628
86951993 10629 /* __atomic routines are not variadic. */
9771b263 10630 if (!orig_format && params->length () != parmnum + 1)
86951993
AM
10631 {
10632 error_at (loc, "too many arguments to function %qE", orig_function);
10633 return false;
10634 }
10635
48ae6c13
RH
10636 /* The definition of these primitives is variadic, with the remaining
10637 being "an optional list of variables protected by the memory barrier".
10638 No clue what that's supposed to mean, precisely, but we consider all
10639 call-clobbered variables to be protected so we're safe. */
9771b263 10640 params->truncate (parmnum + 1);
48ae6c13
RH
10641
10642 return true;
10643}
10644
c22cacf3 10645/* A helper function for resolve_overloaded_builtin. Adds a cast to
48ae6c13
RH
10646 RESULT to make it match the type of the first pointer argument in
10647 PARAMS. */
10648
10649static tree
86951993 10650sync_resolve_return (tree first_param, tree result, bool orig_format)
48ae6c13 10651{
bbbbb16a 10652 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
86951993 10653 tree rtype = TREE_TYPE (result);
99db1ef0 10654 ptype = TYPE_MAIN_VARIANT (ptype);
86951993
AM
10655
10656 /* New format doesn't require casting unless the types are the same size. */
10657 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10658 return convert (ptype, result);
10659 else
10660 return result;
10661}
10662
10663/* This function verifies the PARAMS to generic atomic FUNCTION.
10664 It returns the size if all the parameters are the same size, otherwise
10665 0 is returned if the parameters are invalid. */
10666
10667static int
9771b263
DN
10668get_atomic_generic_size (location_t loc, tree function,
10669 vec<tree, va_gc> *params)
86951993
AM
10670{
10671 unsigned int n_param;
10672 unsigned int n_model;
10673 unsigned int x;
10674 int size_0;
10675 tree type_0;
10676
10677 /* Determine the parameter makeup. */
10678 switch (DECL_FUNCTION_CODE (function))
10679 {
10680 case BUILT_IN_ATOMIC_EXCHANGE:
10681 n_param = 4;
10682 n_model = 1;
10683 break;
10684 case BUILT_IN_ATOMIC_LOAD:
10685 case BUILT_IN_ATOMIC_STORE:
10686 n_param = 3;
10687 n_model = 1;
10688 break;
10689 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10690 n_param = 6;
10691 n_model = 2;
10692 break;
10693 default:
c466c4ff 10694 gcc_unreachable ();
86951993
AM
10695 }
10696
9771b263 10697 if (vec_safe_length (params) != n_param)
86951993
AM
10698 {
10699 error_at (loc, "incorrect number of arguments to function %qE", function);
10700 return 0;
10701 }
10702
10703 /* Get type of first parameter, and determine its size. */
9771b263 10704 type_0 = TREE_TYPE ((*params)[0]);
6415bd5d
JM
10705 if (TREE_CODE (type_0) == ARRAY_TYPE)
10706 {
10707 /* Force array-to-pointer decay for C++. */
10708 gcc_assert (c_dialect_cxx());
10709 (*params)[0] = default_conversion ((*params)[0]);
10710 type_0 = TREE_TYPE ((*params)[0]);
10711 }
c466c4ff
AM
10712 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10713 {
10714 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10715 function);
10716 return 0;
10717 }
10718
10719 /* Types must be compile time constant sizes. */
10720 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
86951993 10721 {
c466c4ff
AM
10722 error_at (loc,
10723 "argument 1 of %qE must be a pointer to a constant size type",
10724 function);
86951993
AM
10725 return 0;
10726 }
c466c4ff 10727
ae7e9ddd 10728 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
86951993 10729
c466c4ff
AM
10730 /* Zero size objects are not allowed. */
10731 if (size_0 == 0)
10732 {
10733 error_at (loc,
10734 "argument 1 of %qE must be a pointer to a nonzero size object",
10735 function);
10736 return 0;
10737 }
10738
86951993
AM
10739 /* Check each other parameter is a pointer and the same size. */
10740 for (x = 0; x < n_param - n_model; x++)
10741 {
10742 int size;
9771b263 10743 tree type = TREE_TYPE ((*params)[x]);
688010ba 10744 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
86951993
AM
10745 if (n_param == 6 && x == 3)
10746 continue;
10747 if (!POINTER_TYPE_P (type))
10748 {
10749 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10750 function);
10751 return 0;
10752 }
7b56b2f8
MP
10753 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10754 size = type_size ? tree_to_uhwi (type_size) : 0;
86951993
AM
10755 if (size != size_0)
10756 {
10757 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10758 function);
10759 return 0;
10760 }
10761 }
10762
10763 /* Check memory model parameters for validity. */
10764 for (x = n_param - n_model ; x < n_param; x++)
10765 {
9771b263 10766 tree p = (*params)[x];
86951993
AM
10767 if (TREE_CODE (p) == INTEGER_CST)
10768 {
ae7e9ddd 10769 int i = tree_to_uhwi (p);
ad7bac31 10770 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
86951993
AM
10771 {
10772 warning_at (loc, OPT_Winvalid_memory_model,
10773 "invalid memory model argument %d of %qE", x + 1,
10774 function);
86951993
AM
10775 }
10776 }
10777 else
10778 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10779 {
10780 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10781 function);
10782 return 0;
10783 }
10784 }
10785
10786 return size_0;
10787}
10788
10789
10790/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10791 at the beginning of the parameter list PARAMS representing the size of the
10792 objects. This is to match the library ABI requirement. LOC is the location
10793 of the function call.
10794 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10795 returned to allow the external call to be constructed. */
10796
10797static tree
10798add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9771b263 10799 vec<tree, va_gc> *params)
86951993
AM
10800{
10801 tree size_node;
10802
10803 /* Insert a SIZE_T parameter as the first param. If there isn't
10804 enough space, allocate a new vector and recursively re-build with that. */
9771b263 10805 if (!params->space (1))
86951993
AM
10806 {
10807 unsigned int z, len;
9771b263 10808 vec<tree, va_gc> *v;
86951993
AM
10809 tree f;
10810
9771b263
DN
10811 len = params->length ();
10812 vec_alloc (v, len + 1);
8edbfaa6 10813 v->quick_push (build_int_cst (size_type_node, n));
86951993 10814 for (z = 0; z < len; z++)
9771b263 10815 v->quick_push ((*params)[z]);
81e5eca8 10816 f = build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 10817 vec_free (v);
86951993
AM
10818 return f;
10819 }
10820
10821 /* Add the size parameter and leave as a function call for processing. */
10822 size_node = build_int_cst (size_type_node, n);
9771b263 10823 params->quick_insert (0, size_node);
86951993
AM
10824 return NULL_TREE;
10825}
10826
10827
6b28e197
JM
10828/* Return whether atomic operations for naturally aligned N-byte
10829 arguments are supported, whether inline or through libatomic. */
10830static bool
10831atomic_size_supported_p (int n)
10832{
10833 switch (n)
10834 {
10835 case 1:
10836 case 2:
10837 case 4:
10838 case 8:
10839 return true;
10840
10841 case 16:
10842 return targetm.scalar_mode_supported_p (TImode);
10843
10844 default:
10845 return false;
10846 }
10847}
10848
86951993
AM
10849/* This will process an __atomic_exchange function call, determine whether it
10850 needs to be mapped to the _N variation, or turned into a library call.
10851 LOC is the location of the builtin call.
10852 FUNCTION is the DECL that has been invoked;
10853 PARAMS is the argument list for the call. The return value is non-null
10854 TRUE is returned if it is translated into the proper format for a call to the
10855 external library, and NEW_RETURN is set the tree for that function.
10856 FALSE is returned if processing for the _N variation is required, and
10857 NEW_RETURN is set to the the return value the result is copied into. */
10858static bool
10859resolve_overloaded_atomic_exchange (location_t loc, tree function,
9771b263 10860 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
10861{
10862 tree p0, p1, p2, p3;
10863 tree I_type, I_type_ptr;
10864 int n = get_atomic_generic_size (loc, function, params);
10865
c466c4ff
AM
10866 /* Size of 0 is an error condition. */
10867 if (n == 0)
10868 {
10869 *new_return = error_mark_node;
10870 return true;
10871 }
10872
86951993 10873 /* If not a lock-free size, change to the library generic format. */
6b28e197 10874 if (!atomic_size_supported_p (n))
86951993
AM
10875 {
10876 *new_return = add_atomic_size_parameter (n, loc, function, params);
10877 return true;
10878 }
10879
10880 /* Otherwise there is a lockfree match, transform the call from:
10881 void fn(T* mem, T* desired, T* return, model)
10882 into
10883 *return = (T) (fn (In* mem, (In) *desired, model)) */
10884
9771b263
DN
10885 p0 = (*params)[0];
10886 p1 = (*params)[1];
10887 p2 = (*params)[2];
10888 p3 = (*params)[3];
86951993
AM
10889
10890 /* Create pointer to appropriate size. */
10891 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10892 I_type_ptr = build_pointer_type (I_type);
10893
10894 /* Convert object pointer to required type. */
10895 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 10896 (*params)[0] = p0;
86951993
AM
10897 /* Convert new value to required type, and dereference it. */
10898 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10899 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 10900 (*params)[1] = p1;
86951993
AM
10901
10902 /* Move memory model to the 3rd position, and end param list. */
9771b263
DN
10903 (*params)[2] = p3;
10904 params->truncate (3);
86951993
AM
10905
10906 /* Convert return pointer and dereference it for later assignment. */
10907 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10908
10909 return false;
48ae6c13
RH
10910}
10911
86951993
AM
10912
10913/* This will process an __atomic_compare_exchange function call, determine
10914 whether it needs to be mapped to the _N variation, or turned into a lib call.
10915 LOC is the location of the builtin call.
10916 FUNCTION is the DECL that has been invoked;
10917 PARAMS is the argument list for the call. The return value is non-null
10918 TRUE is returned if it is translated into the proper format for a call to the
10919 external library, and NEW_RETURN is set the tree for that function.
10920 FALSE is returned if processing for the _N variation is required. */
10921
10922static bool
10923resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9771b263 10924 vec<tree, va_gc> *params,
86951993
AM
10925 tree *new_return)
10926{
10927 tree p0, p1, p2;
10928 tree I_type, I_type_ptr;
10929 int n = get_atomic_generic_size (loc, function, params);
10930
c466c4ff
AM
10931 /* Size of 0 is an error condition. */
10932 if (n == 0)
10933 {
10934 *new_return = error_mark_node;
10935 return true;
10936 }
10937
86951993 10938 /* If not a lock-free size, change to the library generic format. */
6b28e197 10939 if (!atomic_size_supported_p (n))
86951993
AM
10940 {
10941 /* The library generic format does not have the weak parameter, so
10942 remove it from the param list. Since a parameter has been removed,
10943 we can be sure that there is room for the SIZE_T parameter, meaning
10944 there will not be a recursive rebuilding of the parameter list, so
10945 there is no danger this will be done twice. */
10946 if (n > 0)
10947 {
9771b263
DN
10948 (*params)[3] = (*params)[4];
10949 (*params)[4] = (*params)[5];
10950 params->truncate (5);
86951993
AM
10951 }
10952 *new_return = add_atomic_size_parameter (n, loc, function, params);
10953 return true;
10954 }
10955
10956 /* Otherwise, there is a match, so the call needs to be transformed from:
10957 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10958 into
10959 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10960
9771b263
DN
10961 p0 = (*params)[0];
10962 p1 = (*params)[1];
10963 p2 = (*params)[2];
86951993
AM
10964
10965 /* Create pointer to appropriate size. */
10966 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10967 I_type_ptr = build_pointer_type (I_type);
10968
10969 /* Convert object pointer to required type. */
10970 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 10971 (*params)[0] = p0;
86951993
AM
10972
10973 /* Convert expected pointer to required type. */
10974 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9771b263 10975 (*params)[1] = p1;
86951993
AM
10976
10977 /* Convert desired value to required type, and dereference it. */
10978 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10979 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9771b263 10980 (*params)[2] = p2;
86951993
AM
10981
10982 /* The rest of the parameters are fine. NULL means no special return value
10983 processing.*/
10984 *new_return = NULL;
10985 return false;
10986}
10987
10988
10989/* This will process an __atomic_load function call, determine whether it
10990 needs to be mapped to the _N variation, or turned into a library call.
10991 LOC is the location of the builtin call.
10992 FUNCTION is the DECL that has been invoked;
10993 PARAMS is the argument list for the call. The return value is non-null
10994 TRUE is returned if it is translated into the proper format for a call to the
10995 external library, and NEW_RETURN is set the tree for that function.
10996 FALSE is returned if processing for the _N variation is required, and
10997 NEW_RETURN is set to the the return value the result is copied into. */
10998
10999static bool
11000resolve_overloaded_atomic_load (location_t loc, tree function,
9771b263 11001 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11002{
11003 tree p0, p1, p2;
11004 tree I_type, I_type_ptr;
11005 int n = get_atomic_generic_size (loc, function, params);
11006
c466c4ff
AM
11007 /* Size of 0 is an error condition. */
11008 if (n == 0)
11009 {
11010 *new_return = error_mark_node;
11011 return true;
11012 }
11013
86951993 11014 /* If not a lock-free size, change to the library generic format. */
6b28e197 11015 if (!atomic_size_supported_p (n))
86951993
AM
11016 {
11017 *new_return = add_atomic_size_parameter (n, loc, function, params);
11018 return true;
11019 }
11020
11021 /* Otherwise, there is a match, so the call needs to be transformed from:
11022 void fn(T* mem, T* return, model)
11023 into
11024 *return = (T) (fn ((In *) mem, model)) */
11025
9771b263
DN
11026 p0 = (*params)[0];
11027 p1 = (*params)[1];
11028 p2 = (*params)[2];
86951993
AM
11029
11030 /* Create pointer to appropriate size. */
11031 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11032 I_type_ptr = build_pointer_type (I_type);
11033
11034 /* Convert object pointer to required type. */
11035 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11036 (*params)[0] = p0;
86951993
AM
11037
11038 /* Move memory model to the 2nd position, and end param list. */
9771b263
DN
11039 (*params)[1] = p2;
11040 params->truncate (2);
86951993
AM
11041
11042 /* Convert return pointer and dereference it for later assignment. */
11043 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11044
11045 return false;
11046}
11047
11048
11049/* This will process an __atomic_store function call, determine whether it
11050 needs to be mapped to the _N variation, or turned into a library call.
11051 LOC is the location of the builtin call.
11052 FUNCTION is the DECL that has been invoked;
11053 PARAMS is the argument list for the call. The return value is non-null
11054 TRUE is returned if it is translated into the proper format for a call to the
11055 external library, and NEW_RETURN is set the tree for that function.
11056 FALSE is returned if processing for the _N variation is required, and
11057 NEW_RETURN is set to the the return value the result is copied into. */
11058
11059static bool
11060resolve_overloaded_atomic_store (location_t loc, tree function,
9771b263 11061 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11062{
11063 tree p0, p1;
11064 tree I_type, I_type_ptr;
11065 int n = get_atomic_generic_size (loc, function, params);
11066
c466c4ff
AM
11067 /* Size of 0 is an error condition. */
11068 if (n == 0)
11069 {
11070 *new_return = error_mark_node;
11071 return true;
11072 }
11073
86951993 11074 /* If not a lock-free size, change to the library generic format. */
6b28e197 11075 if (!atomic_size_supported_p (n))
86951993
AM
11076 {
11077 *new_return = add_atomic_size_parameter (n, loc, function, params);
11078 return true;
11079 }
11080
11081 /* Otherwise, there is a match, so the call needs to be transformed from:
11082 void fn(T* mem, T* value, model)
11083 into
11084 fn ((In *) mem, (In) *value, model) */
11085
9771b263
DN
11086 p0 = (*params)[0];
11087 p1 = (*params)[1];
86951993
AM
11088
11089 /* Create pointer to appropriate size. */
11090 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11091 I_type_ptr = build_pointer_type (I_type);
11092
11093 /* Convert object pointer to required type. */
11094 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11095 (*params)[0] = p0;
86951993
AM
11096
11097 /* Convert new value to required type, and dereference it. */
11098 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11099 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11100 (*params)[1] = p1;
86951993
AM
11101
11102 /* The memory model is in the right spot already. Return is void. */
11103 *new_return = NULL_TREE;
11104
11105 return false;
11106}
11107
11108
48ae6c13
RH
11109/* Some builtin functions are placeholders for other expressions. This
11110 function should be called immediately after parsing the call expression
11111 before surrounding code has committed to the type of the expression.
11112
c2255bc4
AH
11113 LOC is the location of the builtin call.
11114
48ae6c13
RH
11115 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11116 PARAMS is the argument list for the call. The return value is non-null
11117 when expansion is complete, and null if normal processing should
11118 continue. */
11119
11120tree
9771b263
DN
11121resolve_overloaded_builtin (location_t loc, tree function,
11122 vec<tree, va_gc> *params)
48ae6c13
RH
11123{
11124 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
86951993
AM
11125 bool orig_format = true;
11126 tree new_return = NULL_TREE;
11127
58646b77
PB
11128 switch (DECL_BUILT_IN_CLASS (function))
11129 {
11130 case BUILT_IN_NORMAL:
11131 break;
11132 case BUILT_IN_MD:
11133 if (targetm.resolve_overloaded_builtin)
c2255bc4 11134 return targetm.resolve_overloaded_builtin (loc, function, params);
58646b77 11135 else
c22cacf3 11136 return NULL_TREE;
58646b77
PB
11137 default:
11138 return NULL_TREE;
11139 }
c22cacf3 11140
58646b77 11141 /* Handle BUILT_IN_NORMAL here. */
48ae6c13
RH
11142 switch (orig_code)
11143 {
86951993
AM
11144 case BUILT_IN_ATOMIC_EXCHANGE:
11145 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11146 case BUILT_IN_ATOMIC_LOAD:
11147 case BUILT_IN_ATOMIC_STORE:
11148 {
11149 /* Handle these 4 together so that they can fall through to the next
11150 case if the call is transformed to an _N variant. */
11151 switch (orig_code)
11152 {
11153 case BUILT_IN_ATOMIC_EXCHANGE:
11154 {
11155 if (resolve_overloaded_atomic_exchange (loc, function, params,
11156 &new_return))
11157 return new_return;
11158 /* Change to the _N variant. */
11159 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11160 break;
11161 }
11162
11163 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11164 {
11165 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11166 params,
11167 &new_return))
11168 return new_return;
11169 /* Change to the _N variant. */
11170 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11171 break;
11172 }
11173 case BUILT_IN_ATOMIC_LOAD:
11174 {
11175 if (resolve_overloaded_atomic_load (loc, function, params,
11176 &new_return))
11177 return new_return;
11178 /* Change to the _N variant. */
11179 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11180 break;
11181 }
11182 case BUILT_IN_ATOMIC_STORE:
11183 {
11184 if (resolve_overloaded_atomic_store (loc, function, params,
11185 &new_return))
11186 return new_return;
11187 /* Change to the _N variant. */
11188 orig_code = BUILT_IN_ATOMIC_STORE_N;
11189 break;
11190 }
11191 default:
11192 gcc_unreachable ();
11193 }
11194 /* Fallthrough to the normal processing. */
11195 }
11196 case BUILT_IN_ATOMIC_EXCHANGE_N:
11197 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11198 case BUILT_IN_ATOMIC_LOAD_N:
11199 case BUILT_IN_ATOMIC_STORE_N:
11200 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11201 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11202 case BUILT_IN_ATOMIC_AND_FETCH_N:
11203 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11204 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11205 case BUILT_IN_ATOMIC_OR_FETCH_N:
11206 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11207 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11208 case BUILT_IN_ATOMIC_FETCH_AND_N:
11209 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11210 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11211 case BUILT_IN_ATOMIC_FETCH_OR_N:
11212 {
11213 orig_format = false;
11214 /* Fallthru for parameter processing. */
11215 }
e0a8ecf2
AM
11216 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11217 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11218 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11219 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11220 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11221 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11222 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11223 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11224 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11225 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11226 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11227 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11228 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11229 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11230 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11231 case BUILT_IN_SYNC_LOCK_RELEASE_N:
48ae6c13
RH
11232 {
11233 int n = sync_resolve_size (function, params);
bbbbb16a 11234 tree new_function, first_param, result;
e79983f4 11235 enum built_in_function fncode;
48ae6c13
RH
11236
11237 if (n == 0)
11238 return error_mark_node;
11239
e79983f4
MM
11240 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11241 new_function = builtin_decl_explicit (fncode);
86951993
AM
11242 if (!sync_resolve_params (loc, function, new_function, params,
11243 orig_format))
48ae6c13
RH
11244 return error_mark_node;
11245
9771b263 11246 first_param = (*params)[0];
81e5eca8
MP
11247 result = build_function_call_vec (loc, vNULL, new_function, params,
11248 NULL);
86951993
AM
11249 if (result == error_mark_node)
11250 return result;
e0a8ecf2 11251 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
86951993
AM
11252 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11253 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11254 result = sync_resolve_return (first_param, result, orig_format);
48ae6c13 11255
86951993
AM
11256 /* If new_return is set, assign function to that expr and cast the
11257 result to void since the generic interface returned void. */
11258 if (new_return)
11259 {
11260 /* Cast function result from I{1,2,4,8,16} to the required type. */
11261 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11262 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11263 result);
11264 TREE_SIDE_EFFECTS (result) = 1;
11265 protected_set_expr_location (result, loc);
11266 result = convert (void_type_node, result);
11267 }
48ae6c13
RH
11268 return result;
11269 }
11270
11271 default:
58646b77 11272 return NULL_TREE;
48ae6c13
RH
11273 }
11274}
11275
0af94e6f
JR
11276/* vector_types_compatible_elements_p is used in type checks of vectors
11277 values used as operands of binary operators. Where it returns true, and
11278 the other checks of the caller succeed (being vector types in he first
11279 place, and matching number of elements), we can just treat the types
11280 as essentially the same.
11281 Contrast with vector_targets_convertible_p, which is used for vector
11282 pointer types, and vector_types_convertible_p, which will allow
11283 language-specific matches under the control of flag_lax_vector_conversions,
11284 and might still require a conversion. */
11285/* True if vector types T1 and T2 can be inputs to the same binary
11286 operator without conversion.
11287 We don't check the overall vector size here because some of our callers
11288 want to give different error messages when the vectors are compatible
11289 except for the element count. */
11290
5bed876a 11291bool
0af94e6f 11292vector_types_compatible_elements_p (tree t1, tree t2)
5bed876a 11293{
0af94e6f
JR
11294 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11295 t1 = TREE_TYPE (t1);
11296 t2 = TREE_TYPE (t2);
11297
5bed876a
AH
11298 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11299
ab22c1fa
CF
11300 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11301 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11302 || c2 == FIXED_POINT_TYPE));
5bed876a 11303
0af94e6f
JR
11304 t1 = c_common_signed_type (t1);
11305 t2 = c_common_signed_type (t2);
5bed876a
AH
11306 /* Equality works here because c_common_signed_type uses
11307 TYPE_MAIN_VARIANT. */
0af94e6f
JR
11308 if (t1 == t2)
11309 return true;
11310 if (opaque && c1 == c2
11311 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11312 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11313 return true;
11314 return false;
5bed876a
AH
11315}
11316
104f8784
KG
11317/* Check for missing format attributes on function pointers. LTYPE is
11318 the new type or left-hand side type. RTYPE is the old type or
11319 right-hand side type. Returns TRUE if LTYPE is missing the desired
11320 attribute. */
11321
11322bool
11323check_missing_format_attribute (tree ltype, tree rtype)
11324{
11325 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11326 tree ra;
11327
11328 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11329 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11330 break;
11331 if (ra)
11332 {
11333 tree la;
11334 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11335 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11336 break;
11337 return !la;
11338 }
11339 else
11340 return false;
11341}
11342
ff6b6641
GDR
11343/* Subscripting with type char is likely to lose on a machine where
11344 chars are signed. So warn on any machine, but optionally. Don't
11345 warn for unsigned char since that type is safe. Don't warn for
11346 signed char because anyone who uses that must have done so
11347 deliberately. Furthermore, we reduce the false positive load by
11348 warning only for non-constant value of type char. */
11349
11350void
5bd012f8 11351warn_array_subscript_with_type_char (location_t loc, tree index)
ff6b6641
GDR
11352{
11353 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11354 && TREE_CODE (index) != INTEGER_CST)
5bd012f8
MP
11355 warning_at (loc, OPT_Wchar_subscripts,
11356 "array subscript has type %<char%>");
ff6b6641
GDR
11357}
11358
2a67bec2
ILT
11359/* Implement -Wparentheses for the unexpected C precedence rules, to
11360 cover cases like x + y << z which readers are likely to
11361 misinterpret. We have seen an expression in which CODE is a binary
100d537d
MLI
11362 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11363 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11364 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11365 expression was not formed using a binary or unary operator, or it
11366 was enclosed in parentheses. */
2a67bec2
ILT
11367
11368void
5d9de0d0 11369warn_about_parentheses (location_t loc, enum tree_code code,
fb3e178a 11370 enum tree_code code_left, tree arg_left,
100d537d 11371 enum tree_code code_right, tree arg_right)
2a67bec2
ILT
11372{
11373 if (!warn_parentheses)
11374 return;
11375
100d537d
MLI
11376 /* This macro tests that the expression ARG with original tree code
11377 CODE appears to be a boolean expression. or the result of folding a
11378 boolean expression. */
11379#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11380 (truth_value_p (TREE_CODE (ARG)) \
11381 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11382 /* Folding may create 0 or 1 integers from other expressions. */ \
11383 || ((CODE) != INTEGER_CST \
11384 && (integer_onep (ARG) || integer_zerop (ARG))))
11385
b8698a0f 11386 switch (code)
2a67bec2 11387 {
100d537d 11388 case LSHIFT_EXPR:
5d9de0d0
PC
11389 if (code_left == PLUS_EXPR)
11390 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11391 "suggest parentheses around %<+%> inside %<<<%>");
11392 else if (code_right == PLUS_EXPR)
11393 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11394 "suggest parentheses around %<+%> inside %<<<%>");
11395 else if (code_left == MINUS_EXPR)
11396 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11397 "suggest parentheses around %<-%> inside %<<<%>");
11398 else if (code_right == MINUS_EXPR)
11399 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11400 "suggest parentheses around %<-%> inside %<<<%>");
100d537d 11401 return;
2a67bec2 11402
100d537d 11403 case RSHIFT_EXPR:
5d9de0d0
PC
11404 if (code_left == PLUS_EXPR)
11405 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11406 "suggest parentheses around %<+%> inside %<>>%>");
11407 else if (code_right == PLUS_EXPR)
11408 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11409 "suggest parentheses around %<+%> inside %<>>%>");
11410 else if (code_left == MINUS_EXPR)
11411 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11412 "suggest parentheses around %<-%> inside %<>>%>");
11413 else if (code_right == MINUS_EXPR)
11414 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11415 "suggest parentheses around %<-%> inside %<>>%>");
100d537d 11416 return;
2a67bec2 11417
100d537d 11418 case TRUTH_ORIF_EXPR:
5d9de0d0
PC
11419 if (code_left == TRUTH_ANDIF_EXPR)
11420 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11421 "suggest parentheses around %<&&%> within %<||%>");
11422 else if (code_right == TRUTH_ANDIF_EXPR)
11423 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11424 "suggest parentheses around %<&&%> within %<||%>");
100d537d
MLI
11425 return;
11426
11427 case BIT_IOR_EXPR:
2a67bec2 11428 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
5d9de0d0
PC
11429 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11430 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11431 "suggest parentheses around arithmetic in operand of %<|%>");
11432 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11433 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11434 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11435 "suggest parentheses around arithmetic in operand of %<|%>");
2a67bec2 11436 /* Check cases like x|y==z */
5d9de0d0
PC
11437 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11438 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11439 "suggest parentheses around comparison in operand of %<|%>");
11440 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11441 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11442 "suggest parentheses around comparison in operand of %<|%>");
11443 /* Check cases like !x | y */
11444 else if (code_left == TRUTH_NOT_EXPR
11445 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11446 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11447 "suggest parentheses around operand of "
11448 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
100d537d 11449 return;
2a67bec2 11450
100d537d 11451 case BIT_XOR_EXPR:
2a67bec2 11452 if (code_left == BIT_AND_EXPR
5d9de0d0
PC
11453 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11454 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11455 "suggest parentheses around arithmetic in operand of %<^%>");
11456 else if (code_right == BIT_AND_EXPR
11457 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11458 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11459 "suggest parentheses around arithmetic in operand of %<^%>");
2a67bec2 11460 /* Check cases like x^y==z */
5d9de0d0
PC
11461 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11462 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11463 "suggest parentheses around comparison in operand of %<^%>");
11464 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11465 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11466 "suggest parentheses around comparison in operand of %<^%>");
11467 return;
2a67bec2 11468
100d537d 11469 case BIT_AND_EXPR:
5d9de0d0
PC
11470 if (code_left == PLUS_EXPR)
11471 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11472 "suggest parentheses around %<+%> in operand of %<&%>");
11473 else if (code_right == PLUS_EXPR)
11474 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11475 "suggest parentheses around %<+%> in operand of %<&%>");
5d9de0d0
PC
11476 else if (code_left == MINUS_EXPR)
11477 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11478 "suggest parentheses around %<-%> in operand of %<&%>");
11479 else if (code_right == MINUS_EXPR)
11480 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11481 "suggest parentheses around %<-%> in operand of %<&%>");
2a67bec2 11482 /* Check cases like x&y==z */
5d9de0d0
PC
11483 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11484 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11485 "suggest parentheses around comparison in operand of %<&%>");
11486 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11487 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11488 "suggest parentheses around comparison in operand of %<&%>");
11489 /* Check cases like !x & y */
11490 else if (code_left == TRUTH_NOT_EXPR
11491 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11492 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11493 "suggest parentheses around operand of "
11494 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
100d537d 11495 return;
2a67bec2 11496
100d537d 11497 case EQ_EXPR:
5d9de0d0
PC
11498 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11499 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11500 "suggest parentheses around comparison in operand of %<==%>");
11501 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11502 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11503 "suggest parentheses around comparison in operand of %<==%>");
11504 return;
11505 case NE_EXPR:
5d9de0d0
PC
11506 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11507 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11508 "suggest parentheses around comparison in operand of %<!=%>");
11509 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11510 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11511 "suggest parentheses around comparison in operand of %<!=%>");
11512 return;
11513
11514 default:
5d9de0d0
PC
11515 if (TREE_CODE_CLASS (code) == tcc_comparison)
11516 {
11517 if (TREE_CODE_CLASS (code_left) == tcc_comparison
fb3e178a
JJ
11518 && code_left != NE_EXPR && code_left != EQ_EXPR
11519 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
5d9de0d0
PC
11520 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11521 "comparisons like %<X<=Y<=Z%> do not "
11522 "have their mathematical meaning");
11523 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
fb3e178a 11524 && code_right != NE_EXPR && code_right != EQ_EXPR
5d9de0d0
PC
11525 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11526 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11527 "comparisons like %<X<=Y<=Z%> do not "
11528 "have their mathematical meaning");
11529 }
100d537d 11530 return;
e7917d06 11531 }
100d537d 11532#undef NOT_A_BOOLEAN_EXPR_P
2a67bec2
ILT
11533}
11534
c616e51b
MLI
11535/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11536
11537void
11538warn_for_unused_label (tree label)
11539{
11540 if (!TREE_USED (label))
11541 {
11542 if (DECL_INITIAL (label))
11543 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11544 else
11545 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11546 }
11547}
ff6b6641 11548
c9f9eb5d
AH
11549/* Warn for division by zero according to the value of DIVISOR. LOC
11550 is the location of the division operator. */
2e9cb75e
MLI
11551
11552void
c9f9eb5d 11553warn_for_div_by_zero (location_t loc, tree divisor)
2e9cb75e 11554{
ab22c1fa
CF
11555 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11556 about division by zero. Do not issue a warning if DIVISOR has a
2e9cb75e
MLI
11557 floating-point type, since we consider 0.0/0.0 a valid way of
11558 generating a NaN. */
7d882b83 11559 if (c_inhibit_evaluation_warnings == 0
ab22c1fa 11560 && (integer_zerop (divisor) || fixed_zerop (divisor)))
c9f9eb5d 11561 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
2e9cb75e
MLI
11562}
11563
2d12797c
MLI
11564/* Subroutine of build_binary_op. Give warnings for comparisons
11565 between signed and unsigned quantities that may fail. Do the
11566 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11567 so that casts will be considered, but default promotions won't
ba47d38d
AH
11568 be.
11569
11570 LOCATION is the location of the comparison operator.
2d12797c
MLI
11571
11572 The arguments of this function map directly to local variables
11573 of build_binary_op. */
11574
b8698a0f 11575void
ba47d38d 11576warn_for_sign_compare (location_t location,
b8698a0f
L
11577 tree orig_op0, tree orig_op1,
11578 tree op0, tree op1,
2d12797c
MLI
11579 tree result_type, enum tree_code resultcode)
11580{
11581 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11582 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11583 int unsignedp0, unsignedp1;
b8698a0f 11584
2d12797c
MLI
11585 /* In C++, check for comparison of different enum types. */
11586 if (c_dialect_cxx()
11587 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11588 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11589 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
ead51d36 11590 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
2d12797c 11591 {
ba47d38d
AH
11592 warning_at (location,
11593 OPT_Wsign_compare, "comparison between types %qT and %qT",
11594 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
2d12797c
MLI
11595 }
11596
11597 /* Do not warn if the comparison is being done in a signed type,
11598 since the signed type will only be chosen if it can represent
11599 all the values of the unsigned type. */
11600 if (!TYPE_UNSIGNED (result_type))
11601 /* OK */;
11602 /* Do not warn if both operands are unsigned. */
11603 else if (op0_signed == op1_signed)
11604 /* OK */;
11605 else
11606 {
ead51d36 11607 tree sop, uop, base_type;
2d12797c 11608 bool ovf;
ead51d36 11609
2d12797c
MLI
11610 if (op0_signed)
11611 sop = orig_op0, uop = orig_op1;
b8698a0f 11612 else
2d12797c
MLI
11613 sop = orig_op1, uop = orig_op0;
11614
b8698a0f 11615 STRIP_TYPE_NOPS (sop);
2d12797c 11616 STRIP_TYPE_NOPS (uop);
ead51d36
JJ
11617 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11618 ? TREE_TYPE (result_type) : result_type);
2d12797c
MLI
11619
11620 /* Do not warn if the signed quantity is an unsuffixed integer
11621 literal (or some static constant expression involving such
11622 literals or a conditional expression involving such literals)
11623 and it is non-negative. */
11624 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11625 /* OK */;
11626 /* Do not warn if the comparison is an equality operation, the
11627 unsigned quantity is an integral constant, and it would fit
11628 in the result if the result were signed. */
11629 else if (TREE_CODE (uop) == INTEGER_CST
11630 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
ead51d36 11631 && int_fits_type_p (uop, c_common_signed_type (base_type)))
2d12797c
MLI
11632 /* OK */;
11633 /* In C, do not warn if the unsigned quantity is an enumeration
11634 constant and its maximum value would fit in the result if the
11635 result were signed. */
11636 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11637 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11638 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
ead51d36 11639 c_common_signed_type (base_type)))
2d12797c 11640 /* OK */;
b8698a0f 11641 else
ba47d38d 11642 warning_at (location,
b8698a0f 11643 OPT_Wsign_compare,
ba47d38d 11644 "comparison between signed and unsigned integer expressions");
2d12797c 11645 }
b8698a0f 11646
2d12797c
MLI
11647 /* Warn if two unsigned values are being compared in a size larger
11648 than their original size, and one (and only one) is the result of
11649 a `~' operator. This comparison will always fail.
b8698a0f 11650
2d12797c
MLI
11651 Also warn if one operand is a constant, and the constant does not
11652 have all bits set that are set in the ~ operand when it is
11653 extended. */
11654
828fb3ba
JM
11655 op0 = c_common_get_narrower (op0, &unsignedp0);
11656 op1 = c_common_get_narrower (op1, &unsignedp1);
b8698a0f 11657
2d12797c
MLI
11658 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11659 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11660 {
11661 if (TREE_CODE (op0) == BIT_NOT_EXPR)
828fb3ba 11662 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2d12797c 11663 if (TREE_CODE (op1) == BIT_NOT_EXPR)
828fb3ba 11664 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2d12797c 11665
9541ffee 11666 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2d12797c
MLI
11667 {
11668 tree primop;
11669 HOST_WIDE_INT constant, mask;
11670 int unsignedp;
11671 unsigned int bits;
b8698a0f 11672
9541ffee 11673 if (tree_fits_shwi_p (op0))
2d12797c
MLI
11674 {
11675 primop = op1;
11676 unsignedp = unsignedp1;
9439e9a1 11677 constant = tree_to_shwi (op0);
2d12797c
MLI
11678 }
11679 else
11680 {
11681 primop = op0;
11682 unsignedp = unsignedp0;
9439e9a1 11683 constant = tree_to_shwi (op1);
2d12797c 11684 }
b8698a0f 11685
2d12797c
MLI
11686 bits = TYPE_PRECISION (TREE_TYPE (primop));
11687 if (bits < TYPE_PRECISION (result_type)
11688 && bits < HOST_BITS_PER_LONG && unsignedp)
11689 {
11690 mask = (~ (HOST_WIDE_INT) 0) << bits;
11691 if ((mask & constant) != mask)
11692 {
11693 if (constant == 0)
0a756a3f
MP
11694 warning_at (location, OPT_Wsign_compare,
11695 "promoted ~unsigned is always non-zero");
2d12797c 11696 else
b8698a0f 11697 warning_at (location, OPT_Wsign_compare,
ba47d38d 11698 "comparison of promoted ~unsigned with constant");
2d12797c
MLI
11699 }
11700 }
11701 }
11702 else if (unsignedp0 && unsignedp1
11703 && (TYPE_PRECISION (TREE_TYPE (op0))
11704 < TYPE_PRECISION (result_type))
11705 && (TYPE_PRECISION (TREE_TYPE (op1))
11706 < TYPE_PRECISION (result_type)))
ba47d38d 11707 warning_at (location, OPT_Wsign_compare,
2d12797c
MLI
11708 "comparison of promoted ~unsigned with unsigned");
11709 }
11710}
11711
c5ee1358
MM
11712/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11713 type via c_common_type. If -Wdouble-promotion is in use, and the
11714 conditions for warning have been met, issue a warning. GMSGID is
11715 the warning message. It must have two %T specifiers for the type
11716 that was converted (generally "float") and the type to which it was
11717 converted (generally "double), respectively. LOC is the location
11718 to which the awrning should refer. */
11719
11720void
11721do_warn_double_promotion (tree result_type, tree type1, tree type2,
11722 const char *gmsgid, location_t loc)
11723{
11724 tree source_type;
11725
11726 if (!warn_double_promotion)
11727 return;
11728 /* If the conversion will not occur at run-time, there is no need to
11729 warn about it. */
11730 if (c_inhibit_evaluation_warnings)
11731 return;
11732 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11733 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11734 return;
11735 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11736 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11737 source_type = type1;
11738 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11739 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11740 source_type = type2;
11741 else
11742 return;
11743 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11744}
11745
d0940d56
DS
11746/* Setup a TYPE_DECL node as a typedef representation.
11747
11748 X is a TYPE_DECL for a typedef statement. Create a brand new
11749 ..._TYPE node (which will be just a variant of the existing
11750 ..._TYPE node with identical properties) and then install X
11751 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11752
11753 The whole point here is to end up with a situation where each
11754 and every ..._TYPE node the compiler creates will be uniquely
11755 associated with AT MOST one node representing a typedef name.
11756 This way, even though the compiler substitutes corresponding
11757 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11758 early on, later parts of the compiler can always do the reverse
11759 translation and get back the corresponding typedef name. For
11760 example, given:
11761
11762 typedef struct S MY_TYPE;
11763 MY_TYPE object;
11764
11765 Later parts of the compiler might only know that `object' was of
11766 type `struct S' if it were not for code just below. With this
11767 code however, later parts of the compiler see something like:
11768
11769 struct S' == struct S
11770 typedef struct S' MY_TYPE;
11771 struct S' object;
11772
11773 And they can then deduce (from the node for type struct S') that
11774 the original object declaration was:
11775
11776 MY_TYPE object;
11777
11778 Being able to do this is important for proper support of protoize,
11779 and also for generating precise symbolic debugging information
11780 which takes full account of the programmer's (typedef) vocabulary.
11781
11782 Obviously, we don't want to generate a duplicate ..._TYPE node if
11783 the TYPE_DECL node that we are now processing really represents a
11784 standard built-in type. */
11785
11786void
11787set_underlying_type (tree x)
11788{
11789 if (x == error_mark_node)
11790 return;
11791 if (DECL_IS_BUILTIN (x))
11792 {
11793 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11794 TYPE_NAME (TREE_TYPE (x)) = x;
11795 }
11796 else if (TREE_TYPE (x) != error_mark_node
11797 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11798 {
11799 tree tt = TREE_TYPE (x);
11800 DECL_ORIGINAL_TYPE (x) = tt;
11801 tt = build_variant_type_copy (tt);
11802 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11803 TYPE_NAME (tt) = x;
11804 TREE_USED (tt) = TREE_USED (x);
11805 TREE_TYPE (x) = tt;
11806 }
11807}
11808
b646ba3f
DS
11809/* Record the types used by the current global variable declaration
11810 being parsed, so that we can decide later to emit their debug info.
11811 Those types are in types_used_by_cur_var_decl, and we are going to
11812 store them in the types_used_by_vars_hash hash table.
11813 DECL is the declaration of the global variable that has been parsed. */
11814
11815void
11816record_types_used_by_current_var_decl (tree decl)
11817{
11818 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11819
9771b263 11820 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
b646ba3f 11821 {
9771b263 11822 tree type = types_used_by_cur_var_decl->pop ();
bc87224e 11823 types_used_by_var_decl_insert (type, decl);
b646ba3f
DS
11824 }
11825}
11826
3797cb21
DS
11827/* If DECL is a typedef that is declared in the current function,
11828 record it for the purpose of -Wunused-local-typedefs. */
11829
11830void
11831record_locally_defined_typedef (tree decl)
11832{
11833 struct c_language_function *l;
11834
11835 if (!warn_unused_local_typedefs
11836 || cfun == NULL
11837 /* if this is not a locally defined typedef then we are not
11838 interested. */
11839 || !is_typedef_decl (decl)
11840 || !decl_function_context (decl))
11841 return;
11842
11843 l = (struct c_language_function *) cfun->language;
9771b263 11844 vec_safe_push (l->local_typedefs, decl);
3797cb21
DS
11845}
11846
11847/* If T is a TYPE_DECL declared locally, mark it as used. */
11848
11849void
11850maybe_record_typedef_use (tree t)
11851{
11852 if (!is_typedef_decl (t))
11853 return;
11854
11855 TREE_USED (t) = true;
11856}
11857
11858/* Warn if there are some unused locally defined typedefs in the
11859 current function. */
11860
11861void
11862maybe_warn_unused_local_typedefs (void)
11863{
11864 int i;
11865 tree decl;
11866 /* The number of times we have emitted -Wunused-local-typedefs
11867 warnings. If this is different from errorcount, that means some
11868 unrelated errors have been issued. In which case, we'll avoid
11869 emitting "unused-local-typedefs" warnings. */
11870 static int unused_local_typedefs_warn_count;
11871 struct c_language_function *l;
11872
11873 if (cfun == NULL)
11874 return;
11875
11876 if ((l = (struct c_language_function *) cfun->language) == NULL)
11877 return;
11878
11879 if (warn_unused_local_typedefs
11880 && errorcount == unused_local_typedefs_warn_count)
11881 {
9771b263 11882 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
3797cb21
DS
11883 if (!TREE_USED (decl))
11884 warning_at (DECL_SOURCE_LOCATION (decl),
11885 OPT_Wunused_local_typedefs,
11886 "typedef %qD locally defined but not used", decl);
11887 unused_local_typedefs_warn_count = errorcount;
11888 }
11889
9771b263 11890 vec_free (l->local_typedefs);
3797cb21
DS
11891}
11892
04159acf
MP
11893/* Warn about boolean expression compared with an integer value different
11894 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11895 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11896 are the operands of the comparison. The caller must ensure that
11897 either operand is a boolean expression. */
11898
11899void
11900maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11901 tree op1)
11902{
11903 if (TREE_CODE_CLASS (code) != tcc_comparison)
11904 return;
11905
11906 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11907 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11908 if (!cst)
11909 return;
11910
11911 if (!integer_zerop (cst) && !integer_onep (cst))
11912 {
11913 int sign = (TREE_CODE (op0) == INTEGER_CST)
11914 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
11915 if (code == EQ_EXPR
11916 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11917 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11918 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11919 "with boolean expression is always false", cst);
11920 else
11921 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11922 "with boolean expression is always true", cst);
11923 }
11924}
11925
c166b898
ILT
11926/* The C and C++ parsers both use vectors to hold function arguments.
11927 For efficiency, we keep a cache of unused vectors. This is the
11928 cache. */
11929
9771b263
DN
11930typedef vec<tree, va_gc> *tree_gc_vec;
11931static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
c166b898
ILT
11932
11933/* Return a new vector from the cache. If the cache is empty,
11934 allocate a new vector. These vectors are GC'ed, so it is OK if the
11935 pointer is not released.. */
11936
9771b263 11937vec<tree, va_gc> *
c166b898
ILT
11938make_tree_vector (void)
11939{
9771b263
DN
11940 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11941 return tree_vector_cache->pop ();
c166b898
ILT
11942 else
11943 {
9771b263 11944 /* Passing 0 to vec::alloc returns NULL, and our callers require
c166b898
ILT
11945 that we always return a non-NULL value. The vector code uses
11946 4 when growing a NULL vector, so we do too. */
9771b263
DN
11947 vec<tree, va_gc> *v;
11948 vec_alloc (v, 4);
11949 return v;
c166b898
ILT
11950 }
11951}
11952
11953/* Release a vector of trees back to the cache. */
11954
11955void
9771b263 11956release_tree_vector (vec<tree, va_gc> *vec)
c166b898
ILT
11957{
11958 if (vec != NULL)
11959 {
9771b263
DN
11960 vec->truncate (0);
11961 vec_safe_push (tree_vector_cache, vec);
c166b898
ILT
11962 }
11963}
11964
11965/* Get a new tree vector holding a single tree. */
11966
9771b263 11967vec<tree, va_gc> *
c166b898
ILT
11968make_tree_vector_single (tree t)
11969{
9771b263
DN
11970 vec<tree, va_gc> *ret = make_tree_vector ();
11971 ret->quick_push (t);
c166b898
ILT
11972 return ret;
11973}
11974
c12ff9d8
JM
11975/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11976
9771b263 11977vec<tree, va_gc> *
c12ff9d8
JM
11978make_tree_vector_from_list (tree list)
11979{
9771b263 11980 vec<tree, va_gc> *ret = make_tree_vector ();
c12ff9d8 11981 for (; list; list = TREE_CHAIN (list))
9771b263 11982 vec_safe_push (ret, TREE_VALUE (list));
c12ff9d8
JM
11983 return ret;
11984}
11985
c166b898
ILT
11986/* Get a new tree vector which is a copy of an existing one. */
11987
9771b263
DN
11988vec<tree, va_gc> *
11989make_tree_vector_copy (const vec<tree, va_gc> *orig)
c166b898 11990{
9771b263 11991 vec<tree, va_gc> *ret;
c166b898
ILT
11992 unsigned int ix;
11993 tree t;
11994
11995 ret = make_tree_vector ();
9771b263
DN
11996 vec_safe_reserve (ret, vec_safe_length (orig));
11997 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11998 ret->quick_push (t);
c166b898
ILT
11999 return ret;
12000}
12001
a9aa2c3a
NF
12002/* Return true if KEYWORD starts a type specifier. */
12003
12004bool
12005keyword_begins_type_specifier (enum rid keyword)
12006{
12007 switch (keyword)
12008 {
38b7bc7f 12009 case RID_AUTO_TYPE:
a9aa2c3a
NF
12010 case RID_INT:
12011 case RID_CHAR:
12012 case RID_FLOAT:
12013 case RID_DOUBLE:
12014 case RID_VOID:
a9aa2c3a
NF
12015 case RID_UNSIGNED:
12016 case RID_LONG:
12017 case RID_SHORT:
12018 case RID_SIGNED:
12019 case RID_DFLOAT32:
12020 case RID_DFLOAT64:
12021 case RID_DFLOAT128:
12022 case RID_FRACT:
12023 case RID_ACCUM:
12024 case RID_BOOL:
12025 case RID_WCHAR:
12026 case RID_CHAR16:
12027 case RID_CHAR32:
12028 case RID_SAT:
12029 case RID_COMPLEX:
12030 case RID_TYPEOF:
12031 case RID_STRUCT:
12032 case RID_CLASS:
12033 case RID_UNION:
12034 case RID_ENUM:
12035 return true;
12036 default:
78a7c317
DD
12037 if (keyword >= RID_FIRST_INT_N
12038 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12039 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12040 return true;
a9aa2c3a
NF
12041 return false;
12042 }
12043}
12044
12045/* Return true if KEYWORD names a type qualifier. */
12046
12047bool
12048keyword_is_type_qualifier (enum rid keyword)
12049{
12050 switch (keyword)
12051 {
12052 case RID_CONST:
12053 case RID_VOLATILE:
12054 case RID_RESTRICT:
267bac10 12055 case RID_ATOMIC:
a9aa2c3a
NF
12056 return true;
12057 default:
12058 return false;
12059 }
12060}
12061
12062/* Return true if KEYWORD names a storage class specifier.
12063
12064 RID_TYPEDEF is not included in this list despite `typedef' being
12065 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12066 such for syntactic convenience only. */
12067
12068bool
12069keyword_is_storage_class_specifier (enum rid keyword)
12070{
12071 switch (keyword)
12072 {
12073 case RID_STATIC:
12074 case RID_EXTERN:
12075 case RID_REGISTER:
12076 case RID_AUTO:
12077 case RID_MUTABLE:
12078 case RID_THREAD:
12079 return true;
12080 default:
12081 return false;
12082 }
12083}
12084
ba9e6dd5
NF
12085/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12086
12087static bool
12088keyword_is_function_specifier (enum rid keyword)
12089{
12090 switch (keyword)
12091 {
12092 case RID_INLINE:
bbceee64 12093 case RID_NORETURN:
ba9e6dd5
NF
12094 case RID_VIRTUAL:
12095 case RID_EXPLICIT:
12096 return true;
12097 default:
12098 return false;
12099 }
12100}
12101
12102/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12103 declaration-specifier (C99 6.7). */
12104
12105bool
12106keyword_is_decl_specifier (enum rid keyword)
12107{
12108 if (keyword_is_storage_class_specifier (keyword)
12109 || keyword_is_type_qualifier (keyword)
12110 || keyword_is_function_specifier (keyword))
12111 return true;
12112
12113 switch (keyword)
12114 {
12115 case RID_TYPEDEF:
12116 case RID_FRIEND:
12117 case RID_CONSTEXPR:
12118 return true;
12119 default:
12120 return false;
12121 }
12122}
12123
81f653d6
NF
12124/* Initialize language-specific-bits of tree_contains_struct. */
12125
12126void
12127c_common_init_ts (void)
12128{
12129 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12130 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
36536d79 12131 MARK_TS_TYPED (ARRAY_NOTATION_REF);
81f653d6
NF
12132}
12133
3ce4f9e4
ESR
12134/* Build a user-defined numeric literal out of an integer constant type VALUE
12135 with identifier SUFFIX. */
12136
12137tree
2d7aa578
ESR
12138build_userdef_literal (tree suffix_id, tree value,
12139 enum overflow_type overflow, tree num_string)
3ce4f9e4
ESR
12140{
12141 tree literal = make_node (USERDEF_LITERAL);
12142 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12143 USERDEF_LITERAL_VALUE (literal) = value;
2d7aa578 12144 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
3ce4f9e4
ESR
12145 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12146 return literal;
12147}
12148
7edaa4d2 12149/* For vector[index], convert the vector to a
aa7da51a
JJ
12150 pointer of the underlying type. Return true if the resulting
12151 ARRAY_REF should not be an lvalue. */
12152
12153bool
7edaa4d2 12154convert_vector_to_pointer_for_subscript (location_t loc,
aa7da51a 12155 tree *vecp, tree index)
7edaa4d2 12156{
aa7da51a 12157 bool ret = false;
7edaa4d2
MG
12158 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12159 {
12160 tree type = TREE_TYPE (*vecp);
12161 tree type1;
12162
aa7da51a 12163 ret = !lvalue_p (*vecp);
7edaa4d2 12164 if (TREE_CODE (index) == INTEGER_CST)
cc269bb6 12165 if (!tree_fits_uhwi_p (index)
7d362f6c 12166 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7edaa4d2
MG
12167 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12168
aa7da51a
JJ
12169 if (ret)
12170 {
b731b390 12171 tree tmp = create_tmp_var_raw (type);
aa7da51a
JJ
12172 DECL_SOURCE_LOCATION (tmp) = loc;
12173 *vecp = c_save_expr (*vecp);
12174 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12175 {
12176 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12177 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12178 *vecp
12179 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12180 *vecp, NULL_TREE, NULL_TREE),
12181 non_const);
12182 }
12183 else
12184 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12185 NULL_TREE, NULL_TREE);
12186 SET_EXPR_LOCATION (*vecp, loc);
12187 c_common_mark_addressable_vec (tmp);
12188 }
12189 else
12190 c_common_mark_addressable_vec (*vecp);
7edaa4d2 12191 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7edaa4d2 12192 type1 = build_pointer_type (TREE_TYPE (*vecp));
b0f1bf36
RB
12193 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12194 if (!ref_all
12195 && !DECL_P (*vecp))
12196 {
12197 /* If the original vector isn't declared may_alias and it
12198 isn't a bare vector look if the subscripting would
12199 alias the vector we subscript, and if not, force ref-all. */
12200 alias_set_type vecset = get_alias_set (*vecp);
12201 alias_set_type sset = get_alias_set (type);
12202 if (!alias_sets_must_conflict_p (sset, vecset)
12203 && !alias_set_subset_of (sset, vecset))
12204 ref_all = true;
12205 }
12206 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7edaa4d2
MG
12207 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12208 *vecp = convert (type, *vecp);
12209 }
aa7da51a 12210 return ret;
7edaa4d2
MG
12211}
12212
a212e43f
MG
12213/* Determine which of the operands, if any, is a scalar that needs to be
12214 converted to a vector, for the range of operations. */
12215enum stv_conv
12216scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12217 bool complain)
12218{
12219 tree type0 = TREE_TYPE (op0);
12220 tree type1 = TREE_TYPE (op1);
12221 bool integer_only_op = false;
12222 enum stv_conv ret = stv_firstarg;
12223
12224 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12225 || TREE_CODE (type1) == VECTOR_TYPE);
12226 switch (code)
12227 {
12228 /* Most GENERIC binary expressions require homogeneous arguments.
12229 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12230 argument that is a vector and a second one that is a scalar, so
12231 we never return stv_secondarg for them. */
12232 case RSHIFT_EXPR:
12233 case LSHIFT_EXPR:
12234 if (TREE_CODE (type0) == INTEGER_TYPE
12235 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12236 {
68fca595 12237 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12238 {
12239 if (complain)
12240 error_at (loc, "conversion of scalar %qT to vector %qT "
12241 "involves truncation", type0, type1);
12242 return stv_error;
12243 }
12244 else
12245 return stv_firstarg;
12246 }
12247 break;
12248
12249 case BIT_IOR_EXPR:
12250 case BIT_XOR_EXPR:
12251 case BIT_AND_EXPR:
12252 integer_only_op = true;
12253 /* ... fall through ... */
12254
93100c6b
MG
12255 case VEC_COND_EXPR:
12256
a212e43f
MG
12257 case PLUS_EXPR:
12258 case MINUS_EXPR:
12259 case MULT_EXPR:
12260 case TRUNC_DIV_EXPR:
12261 case CEIL_DIV_EXPR:
12262 case FLOOR_DIV_EXPR:
12263 case ROUND_DIV_EXPR:
12264 case EXACT_DIV_EXPR:
12265 case TRUNC_MOD_EXPR:
12266 case FLOOR_MOD_EXPR:
12267 case RDIV_EXPR:
12268 case EQ_EXPR:
12269 case NE_EXPR:
12270 case LE_EXPR:
12271 case GE_EXPR:
12272 case LT_EXPR:
12273 case GT_EXPR:
12274 /* What about UNLT_EXPR? */
12275 if (TREE_CODE (type0) == VECTOR_TYPE)
12276 {
12277 tree tmp;
12278 ret = stv_secondarg;
12279 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12280 tmp = type0; type0 = type1; type1 = tmp;
12281 tmp = op0; op0 = op1; op1 = tmp;
12282 }
12283
12284 if (TREE_CODE (type0) == INTEGER_TYPE
12285 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12286 {
68fca595 12287 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12288 {
12289 if (complain)
12290 error_at (loc, "conversion of scalar %qT to vector %qT "
12291 "involves truncation", type0, type1);
12292 return stv_error;
12293 }
12294 return ret;
12295 }
12296 else if (!integer_only_op
12297 /* Allow integer --> real conversion if safe. */
12298 && (TREE_CODE (type0) == REAL_TYPE
12299 || TREE_CODE (type0) == INTEGER_TYPE)
12300 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12301 {
68fca595 12302 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12303 {
12304 if (complain)
12305 error_at (loc, "conversion of scalar %qT to vector %qT "
12306 "involves truncation", type0, type1);
12307 return stv_error;
12308 }
12309 return ret;
12310 }
12311 default:
12312 break;
12313 }
12314
12315 return stv_nothing;
12316}
12317
e28d52cf
DS
12318/* Return true iff ALIGN is an integral constant that is a fundamental
12319 alignment, as defined by [basic.align] in the c++-11
12320 specifications.
12321
12322 That is:
12323
12324 [A fundamental alignment is represented by an alignment less than or
12325 equal to the greatest alignment supported by the implementation
12326 in all contexts, which is equal to
12327 alignof(max_align_t)]. */
12328
12329bool
12330cxx_fundamental_alignment_p (unsigned align)
12331{
12332 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12333 TYPE_ALIGN (long_double_type_node)));
12334}
12335
f04dda30
MP
12336/* Return true if T is a pointer to a zero-sized aggregate. */
12337
12338bool
12339pointer_to_zero_sized_aggr_p (tree t)
12340{
12341 if (!POINTER_TYPE_P (t))
12342 return false;
12343 t = TREE_TYPE (t);
12344 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12345}
12346
39dabefd 12347#include "gt-c-family-c-common.h"