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