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