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