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